chiark / gitweb /
*** empty log message ***
[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$";
10
11 /*
12  * $Log$
13  * Revision 1.7  2008/02/27 01:31:14  james
14  * *** empty log message ***
15  *
16  * Revision 1.6  2008/02/27 00:54:16  james
17  * *** empty log message ***
18  *
19  * Revision 1.5  2008/02/24 00:42:53  james
20  * *** empty log message ***
21  *
22  * Revision 1.4  2008/02/23 13:05:58  staffcvs
23  * *** empty log message ***
24  *
25  * Revision 1.3  2008/02/13 16:57:29  james
26  * *** empty log message ***
27  *
28  * Revision 1.2  2008/02/13 09:12:21  james
29  * *** empty log message ***
30  *
31  * Revision 1.1  2008/02/13 01:08:38  james
32  * *** empty log message ***
33  *
34  */
35
36 #include "project.h"
37
38 int
39 wrap_read (int fd, void *buf, int len)
40 {
41   int red;
42
43   red = read (fd, buf, len);
44   if (!red)
45     return -1;
46
47   if ((red < 0) && (errno == EAGAIN))
48     red = 0;
49
50   return red;
51 }
52
53 int
54 wrap_write (int fd, void *buf, int len)
55 {
56   int writ;
57
58   errno = 0;
59
60   writ = write (fd, buf, len);
61
62   if (!writ)
63     return -1;
64
65   if ((writ < 0) && (errno == EAGAIN))
66     writ = 0;
67
68   return writ;
69 }
70
71
72 void
73 set_nonblocking (int fd)
74 {
75   long arg;
76   arg = fcntl (fd, F_GETFL, arg);
77   arg |= O_NONBLOCK;
78   fcntl (fd, F_SETFL, arg);
79 }
80
81 void
82 set_blocking (int fd)
83 {
84   long arg;
85   arg = fcntl (fd, F_GETFL, arg);
86   arg &= ~O_NONBLOCK;
87   fcntl (fd, F_SETFL, arg);
88 }
89
90
91
92 void
93 default_termios (struct termios *termios)
94 {
95   termios->c_iflag = PARMRK | INPCK;
96   termios->c_oflag = NL0 | CR0 | TAB0 | BS0 | VT0 | FF0;
97   termios->c_lflag = 0;
98   termios->c_cflag = CS8 | CREAD | CLOCAL;
99
100   termios->c_cc[VINTR] = 003;
101   termios->c_cc[VQUIT] = 034;
102   termios->c_cc[VERASE] = 0177;
103   termios->c_cc[VKILL] = 025;
104   termios->c_cc[VEOF] = 004;
105   termios->c_cc[VEOL] = 0;
106   termios->c_cc[VEOL2] = 0;
107   termios->c_cc[VSTART] = 021;
108   termios->c_cc[VSTOP] = 023;
109   termios->c_cc[VSUSP] = 032;
110   termios->c_cc[VLNEXT] = 026;
111   termios->c_cc[VWERASE] = 027;
112   termios->c_cc[VREPRINT] = 022;
113   termios->c_cc[VDISCARD] = 017;
114
115
116 }
117
118 void
119 client_termios (struct termios *termios)
120 {
121   memset (termios, 0, sizeof (termios));
122
123   termios->c_iflag = ICRNL | IXON | PARMRK | INPCK;
124   termios->c_oflag = OPOST | ONLCR | NL0 | CR0 | TAB0 | BS0 | VT0 | FF0;
125   termios->c_lflag =
126     ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE;
127   termios->c_cflag = CS8 | CREAD | CLOCAL;
128
129   termios->c_cc[VINTR] = 003;
130   termios->c_cc[VQUIT] = 034;
131   termios->c_cc[VERASE] = 0177;
132   termios->c_cc[VKILL] = 025;
133   termios->c_cc[VEOF] = 004;
134   termios->c_cc[VEOL] = 0;
135   termios->c_cc[VEOL2] = 0;
136   termios->c_cc[VSTART] = 021;
137   termios->c_cc[VSTOP] = 023;
138   termios->c_cc[VSUSP] = 032;
139   termios->c_cc[VLNEXT] = 026;
140   termios->c_cc[VWERASE] = 027;
141   termios->c_cc[VREPRINT] = 022;
142   termios->c_cc[VDISCARD] = 017;
143
144
145   cfsetispeed (termios, B9600);
146   cfsetospeed (termios, B9600);
147 }
148
149 int
150 fput_cp (FILE * f, uint32_t ch)
151 {
152   char buf[4];
153   int i;
154   i = utf8_encode (buf, ch);
155
156   if (!i)
157     return 0;
158
159   return fwrite (buf, i, 1, f);
160 }