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