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