chiark / gitweb /
bdf81323008ab1e7967169d7d91c814bcc677493
[sympathy.git] / apps / sympathyd.c
1 /*
2  * sympathy.c:
3  *
4  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] =
10   "$Id$";
11
12 /*
13  * $Log$
14  * Revision 1.12  2008/02/15 03:32:07  james
15  * *** empty log message ***
16  *
17  * Revision 1.11  2008/02/14 16:21:17  james
18  * *** empty log message ***
19  *
20  * Revision 1.10  2008/02/14 10:39:14  james
21  * *** empty log message ***
22  *
23  * Revision 1.9  2008/02/14 10:34:47  james
24  * *** empty log message ***
25  *
26  * Revision 1.8  2008/02/14 10:34:30  james
27  * *** empty log message ***
28  *
29  * Revision 1.7  2008/02/14 02:46:44  james
30  * *** empty log message ***
31  *
32  * Revision 1.6  2008/02/14 00:57:58  james
33  * *** empty log message ***
34  *
35  * Revision 1.5  2008/02/13 18:05:06  james
36  * *** empty log message ***
37  *
38  * Revision 1.4  2008/02/13 17:21:55  james
39  * *** empty log message ***
40  *
41  * Revision 1.3  2008/02/08 15:06:52  james
42  * *** empty log message ***
43  *
44  * Revision 1.2  2008/02/07 15:42:49  james
45  * *** empty log message ***
46  *
47  * Revision 1.1  2008/02/05 14:25:49  james
48  * *** empty log message ***
49  *
50  */
51
52 #include <sys/time.h>
53 #include <sympathy.h>
54
55 #include "client.h"
56 #include "clients.h"
57
58 typedef struct
59 {
60   int nclients;
61   int lines;
62   int baud;
63   int crtscts;
64   int cd_edge_sec;
65   int blocked;
66   int bootstrap;
67 } Status;
68
69 static Status
70 get_status (TTY * t, Clients * cs)
71 {
72   static struct timeval last_cd_edge = { 0 };
73   static int last_cd_state = -1;
74   int cd;
75   struct timeval now, dif;
76
77   TTY_Status tty_status = { 0 };
78   Status status;
79
80   tty_get_status (t, &tty_status);
81
82   status.bootstrap = 1;
83   status.nclients = cs->n;
84   status.lines = tty_status.lines;
85   status.baud = tty_status.baud;
86   status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
87   status.blocked = tty_status.blocked;
88
89   cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
90
91   if (cd != last_cd_state)
92     {
93       gettimeofday (&last_cd_edge, NULL);
94       last_cd_state = cd;
95     }
96
97   gettimeofday (&now, NULL);
98   timersub (&now, &last_cd_edge, &dif);
99   status.cd_edge_sec = dif.tv_sec;
100
101   return status;
102 }
103
104 static char *
105 line_to_name (int l)
106 {
107
108   switch (l)
109     {
110 #ifdef TIOCM_LE
111     case TIOCM_LE:
112       return "LE";
113 #endif
114 #ifdef TIOCM_DTR
115     case TIOCM_DTR:
116       return "DTR";
117 #endif
118 #ifdef TIOCM_RTS
119     case TIOCM_RTS:
120       return "RTS";
121 #endif
122 #ifdef TIOCM_ST
123     case TIOCM_ST:
124       return "ST";
125 #endif
126 #ifdef TIOCM_SR
127     case TIOCM_SR:
128       return "SR";
129 #endif
130 #ifdef TIOCM_CTS
131     case TIOCM_CTS:
132       return "CTS";
133 #endif
134 #ifdef TIOCM_CD
135     case TIOCM_CD:
136       return "CD";
137 #endif
138 #ifdef TIOCM_RI
139     case TIOCM_RI:
140       return "RI";
141 #endif
142 #ifdef TIOCM_DSR
143     case TIOCM_DSR:
144       return "DSR";
145 #endif
146     }
147   return "??";
148 }
149
150 static void
151 log_line_changes (Context * ctx, int old, int new)
152 {
153   int dif = old ^ new;
154   int c = 1;
155   char buf[1024], *ptr = buf;
156   char *n;
157
158   if (!dif)
159     return;
160   if (!ctx->l)
161     return;
162
163   n = "<Modem lines changed:";
164
165   while (*n)
166     *(ptr++) = *(n++);
167
168   while (dif >= c)
169     {
170
171       if (dif & c)
172         {
173           *(ptr++) = ' ';
174           *(ptr++) = (new & c) ? '+' : '-';
175           n = line_to_name (c);
176           while (*n)
177             *(ptr++) = *(n++);
178         }
179
180       c <<= 1;
181     }
182   *(ptr++) = '>';
183   *ptr = 0;
184
185
186   ctx->l->log (ctx->l, buf);
187
188 }
189
190 static char *
191 do_line (char *ptr, int lines, int line)
192 {
193   char *lname;
194
195   if (!(lines & line))
196     return ptr;
197   lname = line_to_name (line);
198
199   while (*lname)
200     *(ptr++) = *(lname++);
201
202   return ptr;
203 }
204
205
206
207 static void
208 check_status (Context * c, Clients * cs)
209 {
210   static Status old_status = { 0 };
211   Status status;
212   char buf[1024];
213   char *ptr = buf;
214   char *t;
215
216   status = get_status (c->t, cs);
217   if (!memcmp (&status, &old_status, sizeof (status)))
218     return;
219   old_status = status;
220
221
222   log_line_changes (c, old_status.lines, status.lines);
223
224   ptr += sprintf (ptr, "CTRL-B ");
225
226   t = c->t->name;
227   if (!strncmp (t, "/dev/", 5))
228     t += 5;
229   while (*t)
230     *(ptr++) = *(t++);
231
232   ptr += sprintf (ptr, " %db", status.baud);
233
234   ptr = do_line (ptr, status.lines, TIOCM_RTS);
235   ptr = do_line (ptr, status.lines, TIOCM_CTS);
236   ptr = do_line (ptr, status.lines, TIOCM_DTR);
237   ptr = do_line (ptr, status.lines, TIOCM_DSR);
238   ptr = do_line (ptr, status.lines, TIOCM_RI);
239   ptr = do_line (ptr, status.lines, TIOCM_CD);
240
241   if (status.blocked)
242     {
243       t = ", Locked";
244       while (*t)
245         *(ptr++) = *(t++);
246     }
247
248   if (status.crtscts)
249     {
250       t = ", Flow";
251       while (*t)
252         *(ptr++) = *(t++);
253     }
254
255 #if 0
256   if (status.lines & TIOCM_CD)
257     {
258       ptr +=
259         sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
260                  status.cd_edge_sec % 60);
261     }
262   else
263     {
264       ptr +=
265         sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
266                  status.cd_edge_sec % 60);
267     }
268 #endif
269
270   ptr +=
271     sprintf (ptr, ", %d client%s", status.nclients,
272              (status.nclients == 1) ? "" : "s");
273
274   *ptr = 0;
275
276   send_status (cs, buf);
277 }
278
279 int
280 main (int argc, char *argv[])
281 {
282   fd_set rfds, wfds;
283   Context c;
284   Socket *s, *cs;
285   Clients *clients;
286
287
288 #if 0
289   construct_possible_lock_files ("/dev/modem");
290   return 0;
291 #endif
292
293   s = socket_listen ("socket");
294
295   c.t = ptty_open (NULL, NULL);
296   c.v = vt102_new ();
297   c.h = history_new (200);
298   c.l = file_log_new ("log");
299   c.k = keydis_vt102_new ();
300   c.d = NULL;
301
302
303   clients = clients_new ();
304
305   for (;;)
306     {
307       struct timeval tv = { 1, 0 };
308
309       check_status (&c, clients);
310
311       FD_ZERO (&rfds);
312       FD_ZERO (&wfds);
313
314       tty_pre_select (c.t, &rfds, &wfds);
315
316       FD_SET (s->fd, &rfds);
317
318       socket_pre_select (s, &rfds, &wfds);
319
320       clients_pre_select (clients, &rfds, &wfds);
321
322       select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
323
324       if (FD_ISSET (s->fd, &rfds) && ((cs = socket_accept (s))))
325         {
326           {
327             Client *cl;
328             /*New client connexion */
329             cl = clients_new_client (clients, cs, &c);
330
331             send_history (c.h, cl);
332             send_vt102 (c.v, cl);
333
334           }
335         }
336
337
338       clients_post_select (clients, &c, &rfds, &wfds);
339
340       if (FD_ISSET (c.t->rfd, &rfds))
341         {
342           char buf[IPC_MAX_BUF];
343           int red;
344
345           red = c.t->recv (c.t, buf, sizeof (buf));
346
347           if (red < 0)
348             break;
349
350           if (red)
351             {
352               send_output (clients, buf, red);
353               vt102_parse (&c, buf, red);
354             }
355         }
356
357
358
359     }
360
361   clients_shutdown (clients);
362   terminal_atexit ();
363   printf ("QUAT\n");
364 }