4 * Copyright (c) 2008 James McKenzie <sympathy@madingley.org>,
9 static char rcsid[] = "$Id: util.c,v 1.11 2008/03/07 14:13:40 james Exp $";
13 * Revision 1.11 2008/03/07 14:13:40 james
14 * *** empty log message ***
16 * Revision 1.10 2008/03/07 13:16:02 james
17 * *** empty log message ***
19 * Revision 1.9 2008/03/07 12:37:04 james
20 * *** empty log message ***
22 * Revision 1.8 2008/03/02 10:50:32 staffcvs
23 * *** empty log message ***
25 * Revision 1.7 2008/02/27 01:31:14 james
26 * *** empty log message ***
28 * Revision 1.6 2008/02/27 00:54:16 james
29 * *** empty log message ***
31 * Revision 1.5 2008/02/24 00:42:53 james
32 * *** empty log message ***
34 * Revision 1.4 2008/02/23 13:05:58 staffcvs
35 * *** empty log message ***
37 * Revision 1.3 2008/02/13 16:57:29 james
38 * *** empty log message ***
40 * Revision 1.2 2008/02/13 09:12:21 james
41 * *** empty log message ***
43 * Revision 1.1 2008/02/13 01:08:38 james
44 * *** empty log message ***
51 wrap_read (int fd, void *buf, int len)
55 red = read (fd, buf, len);
61 if ((red < 0) && (errno == EAGAIN))
68 wrap_write (int fd, void *buf, int len)
74 writ = write (fd, buf, len);
79 if ((writ < 0) && (errno == EAGAIN))
87 set_nonblocking (int fd)
90 arg = fcntl (fd, F_GETFL, arg);
92 fcntl (fd, F_SETFL, arg);
99 arg = fcntl (fd, F_GETFL, arg);
101 fcntl (fd, F_SETFL, arg);
107 default_termios (struct termios *termios)
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;
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;
133 client_termios (struct termios *termios)
135 memset (termios, 0, sizeof (termios));
137 termios->c_iflag = ICRNL | IXON | PARMRK | INPCK;
138 termios->c_oflag = OPOST | ONLCR | NL0 | CR0 | TAB0 | BS0 | VT0 | FF0;
140 ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE;
141 termios->c_cflag = CS8 | CREAD | CLOCAL;
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;
159 cfsetispeed (termios, B9600);
160 cfsetospeed (termios, B9600);
164 fput_cp (FILE * f, uint32_t ch)
168 i = utf8_encode (buf, ch);
173 return fwrite (buf, i, 1, f);
177 crash_out (char *why)
180 fprintf (stderr, "sympathy is aborting: %s\n", why ? why : "");
187 void *ret = malloc (s);
189 crash_out ("malloc failed");
194 xrealloc (void *p, size_t s)
198 crash_out ("realloc failed");
203 xstrdup (const char *s)
205 char *ret = strdup (s);
207 crash_out ("strdup failed");