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