chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / serial.c
1 /*
2  * serial.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.5  2008/02/15 19:09:00  james
14  * *** empty log message ***
15  *
16  * Revision 1.4  2008/02/15 16:48:56  james
17  * *** empty log message ***
18  *
19  * Revision 1.3  2008/02/15 03:32:07  james
20  * *** empty log message ***
21  *
22  * Revision 1.2  2008/02/14 16:21:17  james
23  * *** empty log message ***
24  *
25  * Revision 1.1  2008/02/14 12:51:14  james
26  * *** empty log message ***
27  *
28  * Revision 1.4  2008/02/14 10:39:14  james
29  * *** empty log message ***
30  *
31  * Revision 1.3  2008/02/13 09:12:21  james
32  * *** empty log message ***
33  *
34  * Revision 1.2  2008/02/12 22:36:46  james
35  * *** empty log message ***
36  *
37  * Revision 1.1  2008/02/09 15:47:28  james
38  * *** empty log message ***
39  *
40  * Revision 1.2  2008/02/07 11:11:14  staffcvs
41  * *** empty log message ***
42  *
43  * Revision 1.1  2008/02/07 01:02:52  james
44  * *** empty log message ***
45  *
46  * Revision 1.3  2008/02/06 17:53:28  james
47  * *** empty log message ***
48  *
49  * Revision 1.2  2008/02/04 02:05:06  james
50  * *** empty log message ***
51  *
52  * Revision 1.1  2008/02/04 01:32:39  james
53  * *** empty log message ***
54  *
55  */
56
57 #include "project.h"
58 #include <pwd.h>
59 #include <dirent.h>
60 #include <sys/stat.h>
61
62
63
64 typedef struct
65 {
66   int mode;
67   int i;
68
69   struct timeval last_stale_purge;
70   Filelist locks_to_check;
71   Filelist locks_held;
72 } Serial_lock;
73
74 typedef struct
75 {
76   TTY_SIGNATURE;
77   Serial_lock lock;
78   int fd;
79 } Serial;
80
81
82 static void
83 serial_check_lock (Serial * t)
84 {
85 }
86
87
88 static void
89 serial_close (TTY * _t)
90 {
91   Serial *t = (Serial *) _t;
92
93   if (!t)
94     return;
95
96   close (t->fd);
97   free (t);
98 }
99
100
101
102 static int
103 serial_read (TTY * _t, void *buf, int len)
104 {
105   Serial *t = (Serial *) _t;
106   int red, done = 0;
107
108   serial_check_lock (t);
109   if (t->blocked)
110     return 0;
111
112   do
113     {
114
115       red = wrap_read (t->fd, buf, len);
116       if (red < 0)
117         return -1;
118       if (!red)
119         return done;
120
121       buf += red;
122       len -= red;
123       done += red;
124     }
125   while (len);
126
127
128   return done;
129 }
130
131
132 static int
133 ptty_write (TTY * _t, void *buf, int len)
134 {
135   int writ, done = 0;
136   Serial *t = (Serial *) _t;
137
138   serial_check_lock (t);
139   if (t->blocked)
140     return 0;
141
142   do
143     {
144
145       writ = wrap_write (t->fd, buf, len);
146       if (writ < 0)
147         return -1;
148       if (!writ)
149         sleep (1);
150
151       buf += writ;
152       len -= writ;
153       done += writ;
154     }
155   while (len);
156
157
158   return done;
159 }
160
161 TTY *
162 serial_open (char *path)
163 {
164   Serial *t;
165   pid_t child;
166   char name[1024];
167   struct winsize winsize = { 0 };
168   struct termios termios;
169   int fd;
170
171
172   default_termios (&termios);
173
174   fd = open (path, O_RDWR);
175
176   set_nonblocking (fd);
177
178   t = (Serial *) malloc (sizeof (Serial));
179
180   strncpy (t->name, path, sizeof (t->name));
181   t->name[sizeof (t->name) - 1] = 0;
182
183   t->recv = serial_read;
184   //t->xmit = serial_write;
185   t->close = serial_close;
186   t->fd = fd;
187   t->rfd = t->fd;
188   t->wfd = t->fd;
189   t->size.x = VT102_COLS;
190   t->size.y = VT102_ROWS;
191   t->blocked = 0;
192
193   return (TTY *) t;
194 }