4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.10 2008/02/23 13:05:57 staffcvs
14 * *** empty log message ***
16 * Revision 1.9 2008/02/23 11:48:51 james
17 * *** empty log message ***
19 * Revision 1.8 2008/02/22 23:39:30 james
20 * *** empty log message ***
22 * Revision 1.7 2008/02/20 20:16:07 james
23 * *** empty log message ***
25 * Revision 1.6 2008/02/20 19:44:37 james
28 * Revision 1.5 2008/02/20 18:31:44 james
29 * *** empty log message ***
31 * Revision 1.4 2008/02/20 17:18:33 james
32 * *** empty log message ***
34 * Revision 1.3 2008/02/20 02:11:35 james
35 * *** empty log message ***
37 * Revision 1.2 2008/02/16 10:58:52 james
38 * *** empty log message ***
40 * Revision 1.13 2008/02/15 23:52:12 james
41 * *** empty log message ***
43 * Revision 1.12 2008/02/15 03:32:07 james
44 * *** empty log message ***
46 * Revision 1.11 2008/02/14 16:21:17 james
47 * *** empty log message ***
49 * Revision 1.10 2008/02/14 10:39:14 james
50 * *** empty log message ***
52 * Revision 1.9 2008/02/14 10:34:47 james
53 * *** empty log message ***
55 * Revision 1.8 2008/02/14 10:34:30 james
56 * *** empty log message ***
58 * Revision 1.7 2008/02/14 02:46:44 james
59 * *** empty log message ***
61 * Revision 1.6 2008/02/14 00:57:58 james
62 * *** empty log message ***
64 * Revision 1.5 2008/02/13 18:05:06 james
65 * *** empty log message ***
67 * Revision 1.4 2008/02/13 17:21:55 james
68 * *** empty log message ***
70 * Revision 1.3 2008/02/08 15:06:52 james
71 * *** empty log message ***
73 * Revision 1.2 2008/02/07 15:42:49 james
74 * *** empty log message ***
76 * Revision 1.1 2008/02/05 14:25:49 james
77 * *** empty log message ***
99 get_status (TTY * t, Clients * cs)
101 static struct timeval last_cd_edge = { 0 };
102 static int last_cd_state = -1;
104 struct timeval now, dif;
106 TTY_Status tty_status = { 0 };
109 tty_get_status (t, &tty_status);
111 status.bootstrap = 1;
114 status.nclients = cs->n;
118 status.lines = tty_status.lines;
119 status.baud = tty_status.baud;
120 status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
121 status.blocked = tty_status.blocked;
123 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
125 if (cd != last_cd_state)
127 gettimeofday (&last_cd_edge, NULL);
131 gettimeofday (&now, NULL);
132 timersub (&now, &last_cd_edge, &dif);
133 status.cd_edge_sec = dif.tv_sec;
185 log_line_changes (Context * ctx, int old, int new)
189 char buf[1024], *ptr = buf;
197 n = "<Modem lines changed:";
208 *(ptr++) = (new & c) ? '+' : '-';
209 n = line_to_name (c);
220 ctx->l->log (ctx->l, buf);
225 do_line (char *ptr, int lines, int line)
231 lname = line_to_name (line);
235 *(ptr++) = *(lname++);
243 check_status (Context * c, Clients * cs)
245 static Status old_status = { 0 };
251 status = get_status (c->t, cs);
252 if (!memcmp (&status, &old_status, sizeof (status)))
255 log_line_changes (c, old_status.lines, status.lines);
259 ptr += sprintf (ptr, "CTRL-B ");
262 if (!strncmp (t, "/dev/", 5))
267 ptr += sprintf (ptr, " %db", status.baud);
269 ptr = do_line (ptr, status.lines, TIOCM_RTS);
270 ptr = do_line (ptr, status.lines, TIOCM_CTS);
271 ptr = do_line (ptr, status.lines, TIOCM_DTR);
272 ptr = do_line (ptr, status.lines, TIOCM_DSR);
273 ptr = do_line (ptr, status.lines, TIOCM_RI);
274 ptr = do_line (ptr, status.lines, TIOCM_CD);
291 if (status.lines & TIOCM_CD)
294 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
295 status.cd_edge_sec % 60);
300 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
301 status.cd_edge_sec % 60);
306 sprintf (ptr, ", %d client%s", status.nclients,
307 (status.nclients == 1) ? "" : "s");
313 sprintf (ptr, ", %d err%s", c->tp->biterrs,
314 (c->tp->biterrs == 1) ? "" : "s");
316 if (c->tp->guessed_baud == -1)
318 ptr += sprintf (ptr, " try higher");
320 else if (c->tp->guessed_baud > 0)
322 ptr += sprintf (ptr, " try %db", c->tp->guessed_baud);
329 log_f (c->l, "%s:%d %s", __FILE__, __LINE__, buf);
333 send_status (cs, buf);
335 cmd_new_status (c->d, c, buf);
341 msg_from_server (ANSI * a, IPC_Msg * m, Context * c)
346 case IPC_MSG_TYPE_NOOP:
348 case IPC_MSG_TYPE_DEBUG:
349 // fprintf (stderr,"%p [%d] %s\n", m, m->hdr.size , m->debug.msg );
351 case IPC_MSG_TYPE_HISTORY:
352 history_add (c->h, m->history.history.line);
354 case IPC_MSG_TYPE_VT102:
355 if (sizeof (VT102) != m->vt102.len)
358 *(c->v) = m->vt102.vt102;
362 a->one_shot (a, &c->v->crt);
367 case IPC_MSG_TYPE_TERM:
368 tty_parse (c, m->term.term, m->term.len);
370 case IPC_MSG_TYPE_STATUS:
371 cmd_new_status (c->d, c, m->status.status);
374 fprintf (stderr, "Unhandeled message type %d\n", m->hdr.type);
381 mainloop (TTY * tty, Socket * server_socket, Socket * client_socket,
382 ANSI * ansi, Log * log, int nhistory)
388 c.tp = tty_parser_new ();
392 c.h = history_new (nhistory);
394 /* are we being fed by a tty or a socket */
399 c.k = keydis_ipc_new (client_socket);
406 c.k = keydis_vt102_new ();
409 /* do we have an upstream terminal to talk to */
410 /* if so start a command parser */
425 clients = clients_new ();
434 struct timeval tv = { 0, 250000 };
436 if ((c.d) && (c.d->disconnect))
439 /*update the status lines, locally or remotely */
441 check_status (&c, clients);
447 tty_pre_select (c.t, &rfds, &wfds);
451 FD_SET (server_socket->fd, &rfds);
452 clients_pre_select (clients, &rfds, &wfds);
456 socket_pre_select (client_socket, &rfds, &wfds);
458 if (ansi && ansi->terminal)
459 tty_pre_select (ansi->terminal, &rfds, &wfds);
461 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
463 /*any message from clients, or new connexions */
466 Socket *new_client_socket;
467 if (FD_ISSET (server_socket->fd, &rfds)
468 && ((new_client_socket = socket_accept (server_socket))))
472 /*New client connexion */
474 clients_new_client (clients, new_client_socket, &c);
476 send_history (c.h, new_client);
477 send_vt102 (c.v, new_client);
482 clients_post_select (clients, &c, &rfds, &wfds);
485 /*any data from the port */
486 if (c.t && FD_ISSET (c.t->rfd, &rfds))
488 char buf[IPC_MAX_BUF];
491 red = c.t->recv (c.t, buf, sizeof (buf));
499 send_output (clients, buf, red);
500 tty_parse (&c, buf, red);
506 /*any data from the server */
510 if (socket_post_select (client_socket, &rfds, &wfds))
513 while (client_socket->msg)
515 if (msg_from_server (ansi, client_socket->msg, &c))
518 socket_consume_msg (client_socket);
525 /*update our local screen */
529 ansi->dispatch (ansi, &c);
532 ansi->update (ansi, &c);
537 clients_shutdown (clients);