4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.6 2008/02/20 19:44:37 james
16 * Revision 1.5 2008/02/20 18:31:44 james
17 * *** empty log message ***
19 * Revision 1.4 2008/02/20 17:18:33 james
20 * *** empty log message ***
22 * Revision 1.3 2008/02/20 02:11:35 james
23 * *** empty log message ***
25 * Revision 1.2 2008/02/16 10:58:52 james
26 * *** empty log message ***
28 * Revision 1.13 2008/02/15 23:52:12 james
29 * *** empty log message ***
31 * Revision 1.12 2008/02/15 03:32:07 james
32 * *** empty log message ***
34 * Revision 1.11 2008/02/14 16:21:17 james
35 * *** empty log message ***
37 * Revision 1.10 2008/02/14 10:39:14 james
38 * *** empty log message ***
40 * Revision 1.9 2008/02/14 10:34:47 james
41 * *** empty log message ***
43 * Revision 1.8 2008/02/14 10:34:30 james
44 * *** empty log message ***
46 * Revision 1.7 2008/02/14 02:46:44 james
47 * *** empty log message ***
49 * Revision 1.6 2008/02/14 00:57:58 james
50 * *** empty log message ***
52 * Revision 1.5 2008/02/13 18:05:06 james
53 * *** empty log message ***
55 * Revision 1.4 2008/02/13 17:21:55 james
56 * *** empty log message ***
58 * Revision 1.3 2008/02/08 15:06:52 james
59 * *** empty log message ***
61 * Revision 1.2 2008/02/07 15:42:49 james
62 * *** empty log message ***
64 * Revision 1.1 2008/02/05 14:25:49 james
65 * *** empty log message ***
87 get_status (TTY * t, Clients * cs)
89 static struct timeval last_cd_edge = { 0 };
90 static int last_cd_state = -1;
92 struct timeval now, dif;
94 TTY_Status tty_status = { 0 };
97 tty_get_status (t, &tty_status);
102 status.nclients = cs->n;
106 status.lines = tty_status.lines;
107 status.baud = tty_status.baud;
108 status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
109 status.blocked = tty_status.blocked;
111 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
113 if (cd != last_cd_state)
115 gettimeofday (&last_cd_edge, NULL);
119 gettimeofday (&now, NULL);
120 timersub (&now, &last_cd_edge, &dif);
121 status.cd_edge_sec = dif.tv_sec;
173 log_line_changes (Context * ctx, int old, int new)
177 char buf[1024], *ptr = buf;
185 n = "<Modem lines changed:";
196 *(ptr++) = (new & c) ? '+' : '-';
197 n = line_to_name (c);
208 ctx->l->log (ctx->l, buf);
213 do_line (char *ptr, int lines, int line)
219 lname = line_to_name (line);
223 *(ptr++) = *(lname++);
231 check_status (Context * c, Clients * cs)
233 static Status old_status = { 0 };
239 status = get_status (c->t, cs);
240 if (!memcmp (&status, &old_status, sizeof (status)))
243 log_line_changes (c, old_status.lines, status.lines);
247 ptr += sprintf (ptr, "CTRL-B ");
250 if (!strncmp (t, "/dev/", 5))
255 ptr += sprintf (ptr, " %db", status.baud);
257 ptr = do_line (ptr, status.lines, TIOCM_RTS);
258 ptr = do_line (ptr, status.lines, TIOCM_CTS);
259 ptr = do_line (ptr, status.lines, TIOCM_DTR);
260 ptr = do_line (ptr, status.lines, TIOCM_DSR);
261 ptr = do_line (ptr, status.lines, TIOCM_RI);
262 ptr = do_line (ptr, status.lines, TIOCM_CD);
279 if (status.lines & TIOCM_CD)
282 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
283 status.cd_edge_sec % 60);
288 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
289 status.cd_edge_sec % 60);
294 sprintf (ptr, ", %d client%s", status.nclients,
295 (status.nclients == 1) ? "" : "s");
300 send_status (cs, buf);
302 cmd_new_status (c->d, c, buf);
308 msg_from_server (ANSI *a,IPC_Msg * m, Context * c)
313 case IPC_MSG_TYPE_NOOP:
315 case IPC_MSG_TYPE_DEBUG:
316 // fprintf (stderr,"%p [%d] %s\n", m, m->hdr.size , m->debug.msg );
318 case IPC_MSG_TYPE_HISTORY:
319 history_add (c->h, m->history.history.line);
321 case IPC_MSG_TYPE_VT102:
322 if (sizeof (VT102) != m->vt102.len)
325 *(c->v) = m->vt102.vt102;
328 a->one_shot(a,&c->v->crt);
333 case IPC_MSG_TYPE_TERM:
334 vt102_parse (c, m->term.term, m->term.len);
336 case IPC_MSG_TYPE_STATUS:
337 cmd_new_status (c->d, c, m->status.status);
340 fprintf (stderr, "Unhandeled message type %d\n", m->hdr.type);
347 mainloop (TTY * tty, Socket * server_socket, Socket * client_socket,
348 ANSI * ansi, Log * log, int nhistory)
356 c.h = history_new (nhistory);
358 /* are we being fed by a tty or a socket */
363 c.k = keydis_ipc_new (client_socket);
370 c.k = keydis_vt102_new ();
373 /* do we have an upstream terminal to talk to */
374 /* if so start a command parser */
389 clients = clients_new ();
398 struct timeval tv = { 1, 0 };
400 if ((c.d) && (c.d->disconnect))
403 /*update the status lines, locally or remotely */
405 check_status (&c, clients);
411 tty_pre_select (c.t, &rfds, &wfds);
415 FD_SET (server_socket->fd, &rfds);
416 clients_pre_select (clients, &rfds, &wfds);
420 socket_pre_select (client_socket, &rfds, &wfds);
423 tty_pre_select (ansi->terminal, &rfds, &wfds);
425 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
427 /*any message from clients, or new connexions */
430 Socket *new_client_socket;
431 if (FD_ISSET (server_socket->fd, &rfds)
432 && ((new_client_socket = socket_accept (server_socket))))
436 /*New client connexion */
438 clients_new_client (clients, new_client_socket, &c);
440 send_history (c.h, new_client);
441 send_vt102 (c.v, new_client);
446 clients_post_select (clients, &c, &rfds, &wfds);
449 /*any data from the port */
450 if (c.t && FD_ISSET (c.t->rfd, &rfds))
452 char buf[IPC_MAX_BUF];
455 red = c.t->recv (c.t, buf, sizeof (buf));
463 send_output (clients, buf, red);
464 vt102_parse (&c, buf, red);
470 /*any data from the server */
473 if (socket_post_select (client_socket, &rfds, &wfds))
476 while (client_socket->msg)
478 if (msg_from_server (ansi,client_socket->msg, &c))
480 socket_consume_msg (client_socket);
485 /*update our local screen */
489 ansi->dispatch (ansi, &c);
492 ansi->update (ansi, &c);
497 clients_shutdown (clients);