4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.9 2008/02/14 10:34:47 james
15 * *** empty log message ***
17 * Revision 1.8 2008/02/14 10:34:30 james
18 * *** empty log message ***
20 * Revision 1.7 2008/02/14 02:46:44 james
21 * *** empty log message ***
23 * Revision 1.6 2008/02/14 00:57:58 james
24 * *** empty log message ***
26 * Revision 1.5 2008/02/13 18:05:06 james
27 * *** empty log message ***
29 * Revision 1.4 2008/02/13 17:21:55 james
30 * *** empty log message ***
32 * Revision 1.3 2008/02/08 15:06:52 james
33 * *** empty log message ***
35 * Revision 1.2 2008/02/07 15:42:49 james
36 * *** empty log message ***
38 * Revision 1.1 2008/02/05 14:25:49 james
39 * *** empty log message ***
60 get_status (TTY * t, Clients * cs)
62 static struct timeval last_cd_edge = { 0 };
63 static int last_cd_state = -1;
65 struct timeval now, dif;
67 TTY_Status tty_status = { 0 };
70 tty_get_status (t, &tty_status);
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;
78 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
80 if (cd != last_cd_state)
82 gettimeofday (&last_cd_edge, NULL);
86 gettimeofday (&now, NULL);
87 timersub (&now, &last_cd_edge, &dif);
88 status.cd_edge_sec = dif.tv_sec;
140 log_line_changes (Context * ctx, int old, int new)
144 char buf[1024], *ptr = buf;
152 n = "<Modem lines changed:";
163 *(ptr++) = (new & c) ? '+' : '-';
164 n = line_to_name (c);
175 ctx->l->log (ctx->l, buf);
180 do_line (char *ptr, int lines, int line)
186 lname = line_to_name (line);
189 *(ptr++) = *(lname++);
197 check_status (Context * c, Clients * cs)
199 static Status old_status = { 0 };
205 status = get_status (c->t, cs);
206 if (!memcmp (&status, &old_status, sizeof (status)))
211 log_line_changes (c, old_status.lines, status.lines);
214 if (!strncmp (t, "/dev/", 5))
219 ptr += sprintf (ptr, " %db", status.baud);
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);
234 if (status.lines & TIOCM_CD)
237 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
238 status.cd_edge_sec % 60);
243 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
244 status.cd_edge_sec % 60);
248 sprintf (ptr, ", %d client%s", status.nclients,
249 (status.nclients == 1) ? "" : "s");
253 send_status (cs, buf);
257 main (int argc, char *argv[])
265 s = socket_listen ("socket");
267 c.t = ptty_open (NULL, NULL);
269 c.h = history_new (200);
270 c.l = file_log_new ("log");
271 c.k = keydis_vt102_new (&c);
274 clients = clients_new ();
278 struct timeval tv = { 1, 0 };
280 check_status (&c, clients);
285 tty_pre_select (c.t, &rfds, &wfds);
287 FD_SET (s->fd, &rfds);
289 socket_pre_select (s, &rfds, &wfds);
291 clients_pre_select (clients, &rfds, &wfds);
293 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
295 if (FD_ISSET (s->fd, &rfds) && ((cs = socket_accept (s))))
299 /*New client connexion */
300 cl = clients_new_client (clients, cs, &c);
302 send_history (c.h, cl);
303 send_vt102 (c.v, cl);
309 clients_post_select (clients, &c, &rfds, &wfds);
311 if (FD_ISSET (c.t->rfd, &rfds))
313 char buf[IPC_MAX_BUF];
316 red = c.t->recv (c.t, buf, sizeof (buf));
323 send_output (clients, buf, red);
324 vt102_parse (&c, buf, red);
332 clients_shutdown (clients);