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