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