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