4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.10 2008/02/14 10:39:14 james
15 * *** empty log message ***
17 * Revision 1.9 2008/02/14 10:34:47 james
18 * *** empty log message ***
20 * Revision 1.8 2008/02/14 10:34:30 james
21 * *** empty log message ***
23 * Revision 1.7 2008/02/14 02:46:44 james
24 * *** empty log message ***
26 * Revision 1.6 2008/02/14 00:57:58 james
27 * *** empty log message ***
29 * Revision 1.5 2008/02/13 18:05:06 james
30 * *** empty log message ***
32 * Revision 1.4 2008/02/13 17:21:55 james
33 * *** empty log message ***
35 * Revision 1.3 2008/02/08 15:06:52 james
36 * *** empty log message ***
38 * Revision 1.2 2008/02/07 15:42:49 james
39 * *** empty log message ***
41 * Revision 1.1 2008/02/05 14:25:49 james
42 * *** empty log message ***
64 get_status (TTY * t, Clients * cs)
66 static struct timeval last_cd_edge = { 0 };
67 static int last_cd_state = -1;
69 struct timeval now, dif;
71 TTY_Status tty_status = { 0 };
74 tty_get_status (t, &tty_status);
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;
83 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
85 if (cd != last_cd_state)
87 gettimeofday (&last_cd_edge, NULL);
91 gettimeofday (&now, NULL);
92 timersub (&now, &last_cd_edge, &dif);
93 status.cd_edge_sec = dif.tv_sec;
145 log_line_changes (Context * ctx, int old, int new)
149 char buf[1024], *ptr = buf;
157 n = "<Modem lines changed:";
168 *(ptr++) = (new & c) ? '+' : '-';
169 n = line_to_name (c);
180 ctx->l->log (ctx->l, buf);
185 do_line (char *ptr, int lines, int line)
191 lname = line_to_name (line);
194 *(ptr++) = *(lname++);
202 check_status (Context * c, Clients * cs)
204 static Status old_status = { 0 };
210 status = get_status (c->t, cs);
211 if (!memcmp (&status, &old_status, sizeof (status)))
216 log_line_changes (c, old_status.lines, status.lines);
219 if (!strncmp (t, "/dev/", 5))
224 ptr += sprintf (ptr, " %db", status.baud);
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);
246 if (status.lines & TIOCM_CD)
249 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
250 status.cd_edge_sec % 60);
255 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
256 status.cd_edge_sec % 60);
260 sprintf (ptr, ", %d client%s", status.nclients,
261 (status.nclients == 1) ? "" : "s");
265 send_status (cs, buf);
269 main (int argc, char *argv[])
277 s = socket_listen ("socket");
279 c.t = ptty_open (NULL, NULL);
281 c.h = history_new (200);
282 c.l = file_log_new ("log");
283 c.k = keydis_vt102_new (&c);
286 clients = clients_new ();
290 struct timeval tv = { 1, 0 };
292 check_status (&c, clients);
297 tty_pre_select (c.t, &rfds, &wfds);
299 FD_SET (s->fd, &rfds);
301 socket_pre_select (s, &rfds, &wfds);
303 clients_pre_select (clients, &rfds, &wfds);
305 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
307 if (FD_ISSET (s->fd, &rfds) && ((cs = socket_accept (s))))
311 /*New client connexion */
312 cl = clients_new_client (clients, cs, &c);
314 send_history (c.h, cl);
315 send_vt102 (c.v, cl);
321 clients_post_select (clients, &c, &rfds, &wfds);
323 if (FD_ISSET (c.t->rfd, &rfds))
325 char buf[IPC_MAX_BUF];
328 red = c.t->recv (c.t, buf, sizeof (buf));
335 send_output (clients, buf, red);
336 vt102_parse (&c, buf, red);
344 clients_shutdown (clients);