4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.4 2008/02/20 17:18:33 james
14 * *** empty log message ***
16 * Revision 1.3 2008/02/20 02:11:35 james
17 * *** empty log message ***
19 * Revision 1.2 2008/02/16 10:58:52 james
20 * *** empty log message ***
22 * Revision 1.13 2008/02/15 23:52:12 james
23 * *** empty log message ***
25 * Revision 1.12 2008/02/15 03:32:07 james
26 * *** empty log message ***
28 * Revision 1.11 2008/02/14 16:21:17 james
29 * *** empty log message ***
31 * Revision 1.10 2008/02/14 10:39:14 james
32 * *** empty log message ***
34 * Revision 1.9 2008/02/14 10:34:47 james
35 * *** empty log message ***
37 * Revision 1.8 2008/02/14 10:34:30 james
38 * *** empty log message ***
40 * Revision 1.7 2008/02/14 02:46:44 james
41 * *** empty log message ***
43 * Revision 1.6 2008/02/14 00:57:58 james
44 * *** empty log message ***
46 * Revision 1.5 2008/02/13 18:05:06 james
47 * *** empty log message ***
49 * Revision 1.4 2008/02/13 17:21:55 james
50 * *** empty log message ***
52 * Revision 1.3 2008/02/08 15:06:52 james
53 * *** empty log message ***
55 * Revision 1.2 2008/02/07 15:42:49 james
56 * *** empty log message ***
58 * Revision 1.1 2008/02/05 14:25:49 james
59 * *** empty log message ***
81 get_status (TTY * t, Clients * cs)
83 static struct timeval last_cd_edge = { 0 };
84 static int last_cd_state = -1;
86 struct timeval now, dif;
88 TTY_Status tty_status = { 0 };
91 tty_get_status (t, &tty_status);
96 status.nclients = cs->n;
100 status.lines = tty_status.lines;
101 status.baud = tty_status.baud;
102 status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
103 status.blocked = tty_status.blocked;
105 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
107 if (cd != last_cd_state)
109 gettimeofday (&last_cd_edge, NULL);
113 gettimeofday (&now, NULL);
114 timersub (&now, &last_cd_edge, &dif);
115 status.cd_edge_sec = dif.tv_sec;
167 log_line_changes (Context * ctx, int old, int new)
171 char buf[1024], *ptr = buf;
179 n = "<Modem lines changed:";
190 *(ptr++) = (new & c) ? '+' : '-';
191 n = line_to_name (c);
202 ctx->l->log (ctx->l, buf);
207 do_line (char *ptr, int lines, int line)
213 lname = line_to_name (line);
217 *(ptr++) = *(lname++);
225 check_status (Context * c, Clients * cs)
227 static Status old_status = { 0 };
233 status = get_status (c->t, cs);
234 if (!memcmp (&status, &old_status, sizeof (status)))
239 log_line_changes (c, old_status.lines, status.lines);
241 ptr += sprintf (ptr, "CTRL-B ");
244 if (!strncmp (t, "/dev/", 5))
249 ptr += sprintf (ptr, " %db", status.baud);
251 ptr = do_line (ptr, status.lines, TIOCM_RTS);
252 ptr = do_line (ptr, status.lines, TIOCM_CTS);
253 ptr = do_line (ptr, status.lines, TIOCM_DTR);
254 ptr = do_line (ptr, status.lines, TIOCM_DSR);
255 ptr = do_line (ptr, status.lines, TIOCM_RI);
256 ptr = do_line (ptr, status.lines, TIOCM_CD);
273 if (status.lines & TIOCM_CD)
276 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
277 status.cd_edge_sec % 60);
282 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
283 status.cd_edge_sec % 60);
288 sprintf (ptr, ", %d client%s", status.nclients,
289 (status.nclients == 1) ? "" : "s");
294 send_status (cs, buf);
296 cmd_new_status (c->d, c, m->status.status);
302 msg_from_server (IPC_Msg * m, Context * c)
307 case IPC_MSG_TYPE_NOOP:
309 case IPC_MSG_TYPE_DEBUG:
310 // fprintf (stderr,"%p [%d] %s\n", m, m->hdr.size , m->debug.msg );
312 case IPC_MSG_TYPE_HISTORY:
313 history_add (c->h, m->history.history.line);
315 case IPC_MSG_TYPE_VT102:
316 if (sizeof (VT102) != m->vt102.len)
319 *(c->v) = m->vt102.vt102;
322 case IPC_MSG_TYPE_TERM:
323 vt102_parse (c, m->term.term, m->term.len);
325 case IPC_MSG_TYPE_STATUS:
326 cmd_new_status (c->d, c, m->status.status);
329 fprintf (stderr, "Unhandeled message type %d\n", m->hdr.type);
335 mainloop (TTY * tty, Socket * server_socket, Socket * client_socket, Ansi * a,
344 c.h = history_new (200);
346 /* are we being fed by a tty or a socket */
352 c.k = keydis_ipc_new (client_socket);
359 c.k = keydis_vt102_new ();
362 /* do we have an upstream terminal to talk to */
363 /* if so start a command parser */
378 clients = clients_new ();
387 struct timeval tv = { 1, 0 };
389 /*update the status lines, locally or remotely*/
390 check_status (&c, clients);
396 tty_pre_select (c.t, &rfds, &wfds);
400 FD_SET (server_socket->fd, &rfds);
401 clients_pre_select (clients, &rfds, &wfds);
405 socket_pre_select (client_socket, &rfds, &wfds);
408 tty_pre_select (ansi->terminal, &rfds, &wfds);
410 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
412 /*any message from clients, or new connexions */
415 Socket *new_client_socket;
416 if (FD_ISSET (server_socket->fd, &rfds)
417 && ((new_client_socket = socket_accept (s))))
421 /*New client connexion */
423 clients_new_client (clients, new_client_socket, &c);
425 send_history (c.h, new_client);
426 send_vt102 (c.v, new_client);
431 clients_post_select (clients, &c, &rfds, &wfds);
434 /*any data from the port */
435 if (c.t && FD_ISSET (c.t->rfd, &rfds))
437 char buf[IPC_MAX_BUF];
440 red = c.t->recv (c.t, buf, sizeof (buf));
447 send_output (clients, buf, red);
448 vt102_parse (&c, buf, red);
454 /*any data from the server*/
457 if (socket_post_select (client_socket, &rfds, &wfds))
460 while (client_socket->msg)
462 msg_from_server (client_socket->msg, &c);
463 socket_consume_msg (client_socket);
468 /*update our local screen*/
471 if (ansi_dispatch (ansi, &c))
474 ansi_update (ansi, &c);
480 clients_shutdown (clients);