4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.8 2008/02/14 10:34:30 james
15 * *** empty log message ***
17 * Revision 1.7 2008/02/14 02:46:44 james
18 * *** empty log message ***
20 * Revision 1.6 2008/02/14 00:57:58 james
21 * *** empty log message ***
23 * Revision 1.5 2008/02/13 18:05:06 james
24 * *** empty log message ***
26 * Revision 1.4 2008/02/13 17:21:55 james
27 * *** empty log message ***
29 * Revision 1.3 2008/02/08 15:06:52 james
30 * *** empty log message ***
32 * Revision 1.2 2008/02/07 15:42:49 james
33 * *** empty log message ***
35 * Revision 1.1 2008/02/05 14:25:49 james
36 * *** empty log message ***
57 get_status (TTY * t, Clients * cs)
59 static struct timeval last_cd_edge = { 0 };
60 static int last_cd_state = -1;
62 struct timeval now, dif;
64 TTY_Status tty_status = { 0 };
67 tty_get_status (t, &tty_status);
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;
75 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
77 if (cd != last_cd_state)
79 gettimeofday (&last_cd_edge, NULL);
83 gettimeofday (&now, NULL);
84 timersub (&now, &last_cd_edge, &dif);
85 status.cd_edge_sec = dif.tv_sec;
137 log_line_changes (Context * ctx, int old, int new)
141 char buf[1024], *ptr = buf;
149 n = "<Modem lines changed:";
160 *(ptr++) = (new & c) ? '+' : '-';
161 n = line_to_name (c);
172 ctx->l->log (ctx->l, buf);
177 do_line (char *ptr,int lines, int line)
183 lname = line_to_name (line);
186 *(ptr++) = *(lname++);
194 check_status (Context * c, Clients * cs)
196 static Status old_status = { 0 };
202 status = get_status (c->t, cs);
203 if (!memcmp (&status, &old_status, sizeof (status)))
208 log_line_changes (c, old_status.lines, status.lines);
211 if (!strncmp(t,"/dev/",5)) t+=5;
215 ptr += sprintf (ptr, " %db", status.baud);
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);
223 if (status.crtscts) {
229 if (status.lines & TIOCM_CD)
232 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
233 status.cd_edge_sec % 60);
238 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
239 status.cd_edge_sec % 60);
242 ptr += sprintf (ptr, ", %d client%s", status.nclients,(status.nclients==1) ? "":"s");
246 send_status (cs, buf);
250 main (int argc, char *argv[])
258 s = socket_listen ("socket");
260 c.t = ptty_open (NULL, NULL);
262 c.h = history_new (200);
263 c.l = file_log_new ("log");
264 c.k = keydis_vt102_new (&c);
267 clients = clients_new ();
271 struct timeval tv = { 1, 0 };
273 check_status (&c, clients);
278 tty_pre_select (c.t, &rfds, &wfds);
280 FD_SET (s->fd, &rfds);
282 socket_pre_select (s, &rfds, &wfds);
284 clients_pre_select (clients, &rfds, &wfds);
286 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
288 if (FD_ISSET (s->fd, &rfds) && ((cs = socket_accept (s))))
292 /*New client connexion */
293 cl = clients_new_client (clients, cs, &c);
295 send_history (c.h, cl);
296 send_vt102 (c.v, cl);
302 clients_post_select (clients, &c, &rfds, &wfds);
304 if (FD_ISSET (c.t->rfd, &rfds))
306 char buf[IPC_MAX_BUF];
309 red = c.t->recv (c.t, buf, sizeof (buf));
316 send_output (clients, buf, red);
317 vt102_parse (&c, buf, red);
325 clients_shutdown (clients);