4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.13 2008/02/26 23:56:12 james
15 * *** empty log message ***
17 * Revision 1.12 2008/02/26 23:23:16 james
18 * *** empty log message ***
20 * Revision 1.11 2008/02/24 00:43:55 james
21 * *** empty log message ***
23 * Revision 1.10 2008/02/23 13:05:57 staffcvs
24 * *** empty log message ***
26 * Revision 1.9 2008/02/23 11:48:51 james
27 * *** empty log message ***
29 * Revision 1.8 2008/02/22 23:39:30 james
30 * *** empty log message ***
32 * Revision 1.7 2008/02/20 20:16:07 james
33 * *** empty log message ***
35 * Revision 1.6 2008/02/20 19:44:37 james
38 * Revision 1.5 2008/02/20 18:31:44 james
39 * *** empty log message ***
41 * Revision 1.4 2008/02/20 17:18:33 james
42 * *** empty log message ***
44 * Revision 1.3 2008/02/20 02:11:35 james
45 * *** empty log message ***
47 * Revision 1.2 2008/02/16 10:58:52 james
48 * *** empty log message ***
50 * Revision 1.13 2008/02/15 23:52:12 james
51 * *** empty log message ***
53 * Revision 1.12 2008/02/15 03:32:07 james
54 * *** empty log message ***
56 * Revision 1.11 2008/02/14 16:21:17 james
57 * *** empty log message ***
59 * Revision 1.10 2008/02/14 10:39:14 james
60 * *** empty log message ***
62 * Revision 1.9 2008/02/14 10:34:47 james
63 * *** empty log message ***
65 * Revision 1.8 2008/02/14 10:34:30 james
66 * *** empty log message ***
68 * Revision 1.7 2008/02/14 02:46:44 james
69 * *** empty log message ***
71 * Revision 1.6 2008/02/14 00:57:58 james
72 * *** empty log message ***
74 * Revision 1.5 2008/02/13 18:05:06 james
75 * *** empty log message ***
77 * Revision 1.4 2008/02/13 17:21:55 james
78 * *** empty log message ***
80 * Revision 1.3 2008/02/08 15:06:52 james
81 * *** empty log message ***
83 * Revision 1.2 2008/02/07 15:42:49 james
84 * *** empty log message ***
86 * Revision 1.1 2008/02/05 14:25:49 james
87 * *** empty log message ***
111 get_status (TTY * t, Clients * cs)
113 static struct timeval last_cd_edge = { 0 };
114 static int last_cd_state = -1;
116 struct timeval now, dif;
118 TTY_Status tty_status = { 0 };
121 tty_get_status (t, &tty_status);
123 status.bootstrap = 1;
126 status.nclients = cs->n;
130 status.lines = tty_status.lines;
131 status.baud = tty_status.baud;
132 status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
133 status.blocked = tty_status.blocked;
135 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
137 if (cd != last_cd_state)
139 gettimeofday (&last_cd_edge, NULL);
143 gettimeofday (&now, NULL);
144 timersub (&now, &last_cd_edge, &dif);
145 status.cd_edge_sec = dif.tv_sec;
197 log_line_changes (Context * ctx, int old, int new)
201 char buf[1024], *ptr = buf;
209 n = "<Modem lines changed:";
220 *(ptr++) = (new & c) ? '+' : '-';
221 n = line_to_name (c);
232 ctx->l->log (ctx->l, buf);
237 do_line (char *ptr, int lines, int line)
243 lname = line_to_name (line);
247 *(ptr++) = *(lname++);
255 check_status (Context * c, Clients * cs)
257 static Status old_status = { 0 };
263 status = get_status (c->t, cs);
264 if (!memcmp (&status, &old_status, sizeof (status)))
267 log_line_changes (c, old_status.lines, status.lines);
271 ptr += sprintf (ptr, "CTRL-B ");
274 if (!strncmp (t, "/dev/", 5))
279 ptr += sprintf (ptr, " %db", status.baud);
281 ptr = do_line (ptr, status.lines, TIOCM_RTS);
282 ptr = do_line (ptr, status.lines, TIOCM_CTS);
283 ptr = do_line (ptr, status.lines, TIOCM_DTR);
284 ptr = do_line (ptr, status.lines, TIOCM_DSR);
285 ptr = do_line (ptr, status.lines, TIOCM_RI);
286 ptr = do_line (ptr, status.lines, TIOCM_CD);
303 if (status.lines & TIOCM_CD)
306 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
307 status.cd_edge_sec % 60);
312 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
313 status.cd_edge_sec % 60);
318 sprintf (ptr, ", %d client%s", status.nclients,
319 (status.nclients == 1) ? "" : "s");
325 sprintf (ptr, ", %d err%s", c->tp->biterrs,
326 (c->tp->biterrs == 1) ? "" : "s");
328 if (c->tp->guessed_baud == -1)
330 ptr += sprintf (ptr, " try higher");
332 else if (c->tp->guessed_baud > 0)
334 ptr += sprintf (ptr, " try %db", c->tp->guessed_baud);
341 log_f (c->l, "%s:%d %s", __FILE__, __LINE__, buf);
345 send_status (cs, buf);
347 cmd_new_status (c->d, c, buf);
353 msg_from_server (ANSI * a, IPC_Msg * m, Context * c)
358 case IPC_MSG_TYPE_NOOP:
360 case IPC_MSG_TYPE_DEBUG:
361 // fprintf (stderr,"%p [%d] %s\n", m, m->hdr.size , m->debug.msg );
363 case IPC_MSG_TYPE_HISTORY:
364 history_add (c->h, m->history.history.line);
366 case IPC_MSG_TYPE_VT102:
367 if (sizeof (VT102) != m->vt102.len)
370 *(c->v) = m->vt102.vt102;
374 a->one_shot (a, &c->v->crt);
379 case IPC_MSG_TYPE_TERM:
380 tty_parse (c, m->term.term, m->term.len);
382 case IPC_MSG_TYPE_STATUS:
383 cmd_new_status (c->d, c, m->status.status);
386 fprintf (stderr, "Unhandeled message type %d\n", m->hdr.type);
393 mainloop (TTY * tty, Socket * server_socket, Socket * client_socket,
394 ANSI * ansi, Log * log, int nhistory)
402 c.tp = tty_parser_new ();
406 c.h = history_new (nhistory);
408 /* are we being fed by a tty or a socket */
413 c.k = keydis_ipc_new (client_socket);
420 c.k = keydis_vt102_new ();
423 /* do we have an upstream terminal to talk to */
424 /* if so start a command parser */
439 clients = clients_new ();
448 struct timeval tv = { 0, 250000 };
450 if ((c.d) && (c.d->disconnect))
453 /*update the status lines, locally or remotely */
455 check_status (&c, clients);
461 tty_pre_select (c.t, &rfds, &wfds);
465 FD_SET (server_socket->fd, &rfds);
466 clients_pre_select (clients, &rfds, &wfds);
470 socket_pre_select (client_socket, &rfds, &wfds);
472 if (ansi && ansi->terminal)
473 tty_pre_select (ansi->terminal, &rfds, &wfds);
475 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
477 /*any message from clients, or new connexions */
480 Socket *new_client_socket;
481 if (FD_ISSET (server_socket->fd, &rfds)
482 && ((new_client_socket = socket_accept (server_socket))))
486 /*New client connexion */
488 clients_new_client (clients, new_client_socket, &c);
490 send_history (c.h, new_client);
491 send_vt102 (c.v, new_client);
496 clients_post_select (clients, &c, &rfds, &wfds);
499 /*any data from the port */
500 if (c.t && FD_ISSET (c.t->rfd, &rfds))
502 char buf[IPC_MAX_BUF];
505 red = c.t->recv (c.t, buf, sizeof (buf));
513 send_output (clients, buf, red);
514 tty_parse (&c, buf, red);
520 /*any data from the server */
524 if (socket_post_select (client_socket, &rfds, &wfds))
527 while (client_socket->msg)
529 if (msg_from_server (ansi, client_socket->msg, &c))
532 socket_consume_msg (client_socket);
539 /*update our local screen */
543 ansi->dispatch (ansi, &c);
546 ansi->update (ansi, &c);
551 clients_shutdown (clients);