4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.12 2008/02/26 23:23:16 james
15 * *** empty log message ***
17 * Revision 1.11 2008/02/24 00:43:55 james
18 * *** empty log message ***
20 * Revision 1.10 2008/02/23 13:05:57 staffcvs
21 * *** empty log message ***
23 * Revision 1.9 2008/02/23 11:48:51 james
24 * *** empty log message ***
26 * Revision 1.8 2008/02/22 23:39:30 james
27 * *** empty log message ***
29 * Revision 1.7 2008/02/20 20:16:07 james
30 * *** empty log message ***
32 * Revision 1.6 2008/02/20 19:44:37 james
35 * Revision 1.5 2008/02/20 18:31:44 james
36 * *** empty log message ***
38 * Revision 1.4 2008/02/20 17:18:33 james
39 * *** empty log message ***
41 * Revision 1.3 2008/02/20 02:11:35 james
42 * *** empty log message ***
44 * Revision 1.2 2008/02/16 10:58:52 james
45 * *** empty log message ***
47 * Revision 1.13 2008/02/15 23:52:12 james
48 * *** empty log message ***
50 * Revision 1.12 2008/02/15 03:32:07 james
51 * *** empty log message ***
53 * Revision 1.11 2008/02/14 16:21:17 james
54 * *** empty log message ***
56 * Revision 1.10 2008/02/14 10:39:14 james
57 * *** empty log message ***
59 * Revision 1.9 2008/02/14 10:34:47 james
60 * *** empty log message ***
62 * Revision 1.8 2008/02/14 10:34:30 james
63 * *** empty log message ***
65 * Revision 1.7 2008/02/14 02:46:44 james
66 * *** empty log message ***
68 * Revision 1.6 2008/02/14 00:57:58 james
69 * *** empty log message ***
71 * Revision 1.5 2008/02/13 18:05:06 james
72 * *** empty log message ***
74 * Revision 1.4 2008/02/13 17:21:55 james
75 * *** empty log message ***
77 * Revision 1.3 2008/02/08 15:06:52 james
78 * *** empty log message ***
80 * Revision 1.2 2008/02/07 15:42:49 james
81 * *** empty log message ***
83 * Revision 1.1 2008/02/05 14:25:49 james
84 * *** empty log message ***
108 get_status (TTY * t, Clients * cs)
110 static struct timeval last_cd_edge = { 0 };
111 static int last_cd_state = -1;
113 struct timeval now, dif;
115 TTY_Status tty_status = { 0 };
118 tty_get_status (t, &tty_status);
120 status.bootstrap = 1;
123 status.nclients = cs->n;
127 status.lines = tty_status.lines;
128 status.baud = tty_status.baud;
129 status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
130 status.blocked = tty_status.blocked;
132 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
134 if (cd != last_cd_state)
136 gettimeofday (&last_cd_edge, NULL);
140 gettimeofday (&now, NULL);
141 timersub (&now, &last_cd_edge, &dif);
142 status.cd_edge_sec = dif.tv_sec;
194 log_line_changes (Context * ctx, int old, int new)
198 char buf[1024], *ptr = buf;
206 n = "<Modem lines changed:";
217 *(ptr++) = (new & c) ? '+' : '-';
218 n = line_to_name (c);
229 ctx->l->log (ctx->l, buf);
234 do_line (char *ptr, int lines, int line)
240 lname = line_to_name (line);
244 *(ptr++) = *(lname++);
252 check_status (Context * c, Clients * cs)
254 static Status old_status = { 0 };
260 status = get_status (c->t, cs);
261 if (!memcmp (&status, &old_status, sizeof (status)))
264 log_line_changes (c, old_status.lines, status.lines);
268 ptr += sprintf (ptr, "CTRL-B ");
271 if (!strncmp (t, "/dev/", 5))
276 ptr += sprintf (ptr, " %db", status.baud);
278 ptr = do_line (ptr, status.lines, TIOCM_RTS);
279 ptr = do_line (ptr, status.lines, TIOCM_CTS);
280 ptr = do_line (ptr, status.lines, TIOCM_DTR);
281 ptr = do_line (ptr, status.lines, TIOCM_DSR);
282 ptr = do_line (ptr, status.lines, TIOCM_RI);
283 ptr = do_line (ptr, status.lines, TIOCM_CD);
300 if (status.lines & TIOCM_CD)
303 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
304 status.cd_edge_sec % 60);
309 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
310 status.cd_edge_sec % 60);
315 sprintf (ptr, ", %d client%s", status.nclients,
316 (status.nclients == 1) ? "" : "s");
322 sprintf (ptr, ", %d err%s", c->tp->biterrs,
323 (c->tp->biterrs == 1) ? "" : "s");
325 if (c->tp->guessed_baud == -1)
327 ptr += sprintf (ptr, " try higher");
329 else if (c->tp->guessed_baud > 0)
331 ptr += sprintf (ptr, " try %db", c->tp->guessed_baud);
338 log_f (c->l, "%s:%d %s", __FILE__, __LINE__, buf);
342 send_status (cs, buf);
344 cmd_new_status (c->d, c, buf);
350 msg_from_server (ANSI * a, IPC_Msg * m, Context * c)
355 case IPC_MSG_TYPE_NOOP:
357 case IPC_MSG_TYPE_DEBUG:
358 // fprintf (stderr,"%p [%d] %s\n", m, m->hdr.size , m->debug.msg );
360 case IPC_MSG_TYPE_HISTORY:
361 history_add (c->h, m->history.history.line);
363 case IPC_MSG_TYPE_VT102:
364 if (sizeof (VT102) != m->vt102.len)
367 *(c->v) = m->vt102.vt102;
371 a->one_shot (a, &c->v->crt);
376 case IPC_MSG_TYPE_TERM:
377 tty_parse (c, m->term.term, m->term.len);
379 case IPC_MSG_TYPE_STATUS:
380 cmd_new_status (c->d, c, m->status.status);
383 fprintf (stderr, "Unhandeled message type %d\n", m->hdr.type);
390 mainloop (TTY * tty, Socket * server_socket, Socket * client_socket,
391 ANSI * ansi, Log * log, int nhistory)
399 c.tp = tty_parser_new ();
403 c.h = history_new (nhistory);
405 /* are we being fed by a tty or a socket */
410 c.k = keydis_ipc_new (client_socket);
417 c.k = keydis_vt102_new ();
420 /* do we have an upstream terminal to talk to */
421 /* if so start a command parser */
436 clients = clients_new ();
445 struct timeval tv = { 0, 250000 };
447 if ((c.d) && (c.d->disconnect))
450 /*update the status lines, locally or remotely */
452 check_status (&c, clients);
458 tty_pre_select (c.t, &rfds, &wfds);
462 FD_SET (server_socket->fd, &rfds);
463 clients_pre_select (clients, &rfds, &wfds);
467 socket_pre_select (client_socket, &rfds, &wfds);
469 if (ansi && ansi->terminal)
470 tty_pre_select (ansi->terminal, &rfds, &wfds);
472 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
474 /*any message from clients, or new connexions */
477 Socket *new_client_socket;
478 if (FD_ISSET (server_socket->fd, &rfds)
479 && ((new_client_socket = socket_accept (server_socket))))
483 /*New client connexion */
485 clients_new_client (clients, new_client_socket, &c);
487 send_history (c.h, new_client);
488 send_vt102 (c.v, new_client);
493 clients_post_select (clients, &c, &rfds, &wfds);
496 /*any data from the port */
497 if (c.t && FD_ISSET (c.t->rfd, &rfds))
499 char buf[IPC_MAX_BUF];
502 red = c.t->recv (c.t, buf, sizeof (buf));
510 send_output (clients, buf, red);
511 tty_parse (&c, buf, red);
517 /*any data from the server */
521 if (socket_post_select (client_socket, &rfds, &wfds))
524 while (client_socket->msg)
526 if (msg_from_server (ansi, client_socket->msg, &c))
529 socket_consume_msg (client_socket);
536 /*update our local screen */
540 ansi->dispatch (ansi, &c);
543 ansi->update (ansi, &c);
548 clients_shutdown (clients);