chiark / gitweb /
ca781fce283bcd9dd741507beb2aee351d7e170a
[sympathy.git] / src / util.c
1 /* 
2  * util.c:
3  *
4  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] = "$Id: util.c,v 1.11 2008/03/07 14:13:40 james Exp $";
10
11 /* 
12  * $Log: util.c,v $
13  * Revision 1.11  2008/03/07 14:13:40  james
14  * *** empty log message ***
15  *
16  * Revision 1.10  2008/03/07 13:16:02  james
17  * *** empty log message ***
18  *
19  * Revision 1.9  2008/03/07 12:37:04  james
20  * *** empty log message ***
21  *
22  * Revision 1.8  2008/03/02 10:50:32  staffcvs
23  * *** empty log message ***
24  *
25  * Revision 1.7  2008/02/27 01:31:14  james
26  * *** empty log message ***
27  *
28  * Revision 1.6  2008/02/27 00:54:16  james
29  * *** empty log message ***
30  *
31  * Revision 1.5  2008/02/24 00:42:53  james
32  * *** empty log message ***
33  *
34  * Revision 1.4  2008/02/23 13:05:58  staffcvs
35  * *** empty log message ***
36  *
37  * Revision 1.3  2008/02/13 16:57:29  james
38  * *** empty log message ***
39  *
40  * Revision 1.2  2008/02/13 09:12:21  james
41  * *** empty log message ***
42  *
43  * Revision 1.1  2008/02/13 01:08:38  james
44  * *** empty log message ***
45  *
46  */
47
48 #include "project.h"
49
50 int
51 wrap_read (int fd, void *buf, int len)
52 {
53   int red;
54
55   red = read (fd, buf, len);
56 #if 0
57   if (!red)
58     return -1;
59 #endif
60
61   if ((red < 0) && (errno == EAGAIN))
62     red = 0;
63
64   return red;
65 }
66
67 int
68 wrap_write (int fd, void *buf, int len)
69 {
70   int writ;
71
72   errno = 0;
73
74   writ = write (fd, buf, len);
75
76   if (!writ)
77     return -1;
78
79   if ((writ < 0) && (errno == EAGAIN))
80     writ = 0;
81
82   return writ;
83 }
84
85
86 void
87 set_nonblocking (int fd)
88 {
89   long arg;
90   arg = fcntl (fd, F_GETFL, arg);
91   arg |= O_NONBLOCK;
92   fcntl (fd, F_SETFL, arg);
93 }
94
95 void
96 set_blocking (int fd)
97 {
98   long arg;
99   arg = fcntl (fd, F_GETFL, arg);
100   arg &= ~O_NONBLOCK;
101   fcntl (fd, F_SETFL, arg);
102 }
103
104
105
106 void
107 default_termios (struct termios *termios)
108 {
109   termios->c_iflag = PARMRK | INPCK;
110   termios->c_oflag = NL0 | CR0 | TAB0 | BS0 | VT0 | FF0;
111   termios->c_lflag = 0;
112   termios->c_cflag = CS8 | CREAD | CLOCAL;
113
114   termios->c_cc[VINTR] = 003;
115   termios->c_cc[VQUIT] = 034;
116   termios->c_cc[VERASE] = 0177;
117   termios->c_cc[VKILL] = 025;
118   termios->c_cc[VEOF] = 004;
119   termios->c_cc[VEOL] = 0;
120   termios->c_cc[VEOL2] = 0;
121   termios->c_cc[VSTART] = 021;
122   termios->c_cc[VSTOP] = 023;
123   termios->c_cc[VSUSP] = 032;
124   termios->c_cc[VLNEXT] = 026;
125   termios->c_cc[VWERASE] = 027;
126   termios->c_cc[VREPRINT] = 022;
127   termios->c_cc[VDISCARD] = 017;
128
129
130 }
131
132 void
133 client_termios (struct termios *termios)
134 {
135   memset (termios, 0, sizeof (termios));
136
137   termios->c_iflag = ICRNL | IXON | PARMRK | INPCK;
138   termios->c_oflag = OPOST | ONLCR | NL0 | CR0 | TAB0 | BS0 | VT0 | FF0;
139   termios->c_lflag =
140     ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE;
141   termios->c_cflag = CS8 | CREAD | CLOCAL;
142
143   termios->c_cc[VINTR] = 003;
144   termios->c_cc[VQUIT] = 034;
145   termios->c_cc[VERASE] = 0177;
146   termios->c_cc[VKILL] = 025;
147   termios->c_cc[VEOF] = 004;
148   termios->c_cc[VEOL] = 0;
149   termios->c_cc[VEOL2] = 0;
150   termios->c_cc[VSTART] = 021;
151   termios->c_cc[VSTOP] = 023;
152   termios->c_cc[VSUSP] = 032;
153   termios->c_cc[VLNEXT] = 026;
154   termios->c_cc[VWERASE] = 027;
155   termios->c_cc[VREPRINT] = 022;
156   termios->c_cc[VDISCARD] = 017;
157
158
159   cfsetispeed (termios, B9600);
160   cfsetospeed (termios, B9600);
161 }
162
163 int
164 fput_cp (FILE * f, uint32_t ch)
165 {
166   char buf[4];
167   int i;
168   i = utf8_encode (buf, ch);
169
170   if (!i)
171     return 0;
172
173   return fwrite (buf, i, 1, f);
174 }
175
176 void
177 crash_out (char *why)
178 {
179   terminal_atexit ();
180   fprintf (stderr, "sympathy is aborting: %s\n", why ? why : "");
181   exit (1);
182 }
183
184 void *
185 xmalloc (size_t s)
186 {
187   void *ret = malloc (s);
188   if (!ret)
189     crash_out ("malloc failed");
190   return ret;
191 }
192
193 void *
194 xrealloc (void *p, size_t s)
195 {
196   p = realloc (p, s);
197   if (!p)
198     crash_out ("realloc failed");
199   return p;
200 }
201
202 char *
203 xstrdup (const char *s)
204 {
205   char *ret = strdup (s);
206   if (!ret)
207     crash_out ("strdup failed");
208   return ret;
209 }