4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.11 2008/02/24 00:43:55 james
15 * *** empty log message ***
17 * Revision 1.10 2008/02/23 13:05:57 staffcvs
18 * *** empty log message ***
20 * Revision 1.9 2008/02/23 11:48:51 james
21 * *** empty log message ***
23 * Revision 1.8 2008/02/22 23:39:30 james
24 * *** empty log message ***
26 * Revision 1.7 2008/02/20 20:16:07 james
27 * *** empty log message ***
29 * Revision 1.6 2008/02/20 19:44:37 james
32 * Revision 1.5 2008/02/20 18:31:44 james
33 * *** empty log message ***
35 * Revision 1.4 2008/02/20 17:18:33 james
36 * *** empty log message ***
38 * Revision 1.3 2008/02/20 02:11:35 james
39 * *** empty log message ***
41 * Revision 1.2 2008/02/16 10:58:52 james
42 * *** empty log message ***
44 * Revision 1.13 2008/02/15 23:52:12 james
45 * *** empty log message ***
47 * Revision 1.12 2008/02/15 03:32:07 james
48 * *** empty log message ***
50 * Revision 1.11 2008/02/14 16:21:17 james
51 * *** empty log message ***
53 * Revision 1.10 2008/02/14 10:39:14 james
54 * *** empty log message ***
56 * Revision 1.9 2008/02/14 10:34:47 james
57 * *** empty log message ***
59 * Revision 1.8 2008/02/14 10:34:30 james
60 * *** empty log message ***
62 * Revision 1.7 2008/02/14 02:46:44 james
63 * *** empty log message ***
65 * Revision 1.6 2008/02/14 00:57:58 james
66 * *** empty log message ***
68 * Revision 1.5 2008/02/13 18:05:06 james
69 * *** empty log message ***
71 * Revision 1.4 2008/02/13 17:21:55 james
72 * *** empty log message ***
74 * Revision 1.3 2008/02/08 15:06:52 james
75 * *** empty log message ***
77 * Revision 1.2 2008/02/07 15:42:49 james
78 * *** empty log message ***
80 * Revision 1.1 2008/02/05 14:25:49 james
81 * *** empty log message ***
103 get_status (TTY * t, Clients * cs)
105 static struct timeval last_cd_edge = { 0 };
106 static int last_cd_state = -1;
108 struct timeval now, dif;
110 TTY_Status tty_status = { 0 };
113 tty_get_status (t, &tty_status);
115 status.bootstrap = 1;
118 status.nclients = cs->n;
122 status.lines = tty_status.lines;
123 status.baud = tty_status.baud;
124 status.crtscts = (tty_status.termios.c_cflag & CRTSCTS) ? 1 : 0;
125 status.blocked = tty_status.blocked;
127 cd = (tty_status.lines & TIOCM_CD) ? 1 : 0;
129 if (cd != last_cd_state)
131 gettimeofday (&last_cd_edge, NULL);
135 gettimeofday (&now, NULL);
136 timersub (&now, &last_cd_edge, &dif);
137 status.cd_edge_sec = dif.tv_sec;
189 log_line_changes (Context * ctx, int old, int new)
193 char buf[1024], *ptr = buf;
201 n = "<Modem lines changed:";
212 *(ptr++) = (new & c) ? '+' : '-';
213 n = line_to_name (c);
224 ctx->l->log (ctx->l, buf);
229 do_line (char *ptr, int lines, int line)
235 lname = line_to_name (line);
239 *(ptr++) = *(lname++);
247 check_status (Context * c, Clients * cs)
249 static Status old_status = { 0 };
255 status = get_status (c->t, cs);
256 if (!memcmp (&status, &old_status, sizeof (status)))
259 log_line_changes (c, old_status.lines, status.lines);
263 ptr += sprintf (ptr, "CTRL-B ");
266 if (!strncmp (t, "/dev/", 5))
271 ptr += sprintf (ptr, " %db", status.baud);
273 ptr = do_line (ptr, status.lines, TIOCM_RTS);
274 ptr = do_line (ptr, status.lines, TIOCM_CTS);
275 ptr = do_line (ptr, status.lines, TIOCM_DTR);
276 ptr = do_line (ptr, status.lines, TIOCM_DSR);
277 ptr = do_line (ptr, status.lines, TIOCM_RI);
278 ptr = do_line (ptr, status.lines, TIOCM_CD);
295 if (status.lines & TIOCM_CD)
298 sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60,
299 status.cd_edge_sec % 60);
304 sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60,
305 status.cd_edge_sec % 60);
310 sprintf (ptr, ", %d client%s", status.nclients,
311 (status.nclients == 1) ? "" : "s");
317 sprintf (ptr, ", %d err%s", c->tp->biterrs,
318 (c->tp->biterrs == 1) ? "" : "s");
320 if (c->tp->guessed_baud == -1)
322 ptr += sprintf (ptr, " try higher");
324 else if (c->tp->guessed_baud > 0)
326 ptr += sprintf (ptr, " try %db", c->tp->guessed_baud);
333 log_f (c->l, "%s:%d %s", __FILE__, __LINE__, buf);
337 send_status (cs, buf);
339 cmd_new_status (c->d, c, buf);
345 msg_from_server (ANSI * a, IPC_Msg * m, Context * c)
350 case IPC_MSG_TYPE_NOOP:
352 case IPC_MSG_TYPE_DEBUG:
353 // fprintf (stderr,"%p [%d] %s\n", m, m->hdr.size , m->debug.msg );
355 case IPC_MSG_TYPE_HISTORY:
356 history_add (c->h, m->history.history.line);
358 case IPC_MSG_TYPE_VT102:
359 if (sizeof (VT102) != m->vt102.len)
362 *(c->v) = m->vt102.vt102;
366 a->one_shot (a, &c->v->crt);
371 case IPC_MSG_TYPE_TERM:
372 tty_parse (c, m->term.term, m->term.len);
374 case IPC_MSG_TYPE_STATUS:
375 cmd_new_status (c->d, c, m->status.status);
378 fprintf (stderr, "Unhandeled message type %d\n", m->hdr.type);
385 mainloop (TTY * tty, Socket * server_socket, Socket * client_socket,
386 ANSI * ansi, Log * log, int nhistory)
392 c.tp = tty_parser_new ();
396 c.h = history_new (nhistory);
398 /* are we being fed by a tty or a socket */
403 c.k = keydis_ipc_new (client_socket);
410 c.k = keydis_vt102_new ();
413 /* do we have an upstream terminal to talk to */
414 /* if so start a command parser */
429 clients = clients_new ();
438 struct timeval tv = { 0, 250000 };
440 if ((c.d) && (c.d->disconnect))
443 /*update the status lines, locally or remotely */
445 check_status (&c, clients);
451 tty_pre_select (c.t, &rfds, &wfds);
455 FD_SET (server_socket->fd, &rfds);
456 clients_pre_select (clients, &rfds, &wfds);
460 socket_pre_select (client_socket, &rfds, &wfds);
462 if (ansi && ansi->terminal)
463 tty_pre_select (ansi->terminal, &rfds, &wfds);
465 select (FD_SETSIZE, &rfds, &wfds, NULL, &tv);
467 /*any message from clients, or new connexions */
470 Socket *new_client_socket;
471 if (FD_ISSET (server_socket->fd, &rfds)
472 && ((new_client_socket = socket_accept (server_socket))))
476 /*New client connexion */
478 clients_new_client (clients, new_client_socket, &c);
480 send_history (c.h, new_client);
481 send_vt102 (c.v, new_client);
486 clients_post_select (clients, &c, &rfds, &wfds);
489 /*any data from the port */
490 if (c.t && FD_ISSET (c.t->rfd, &rfds))
492 char buf[IPC_MAX_BUF];
495 red = c.t->recv (c.t, buf, sizeof (buf));
503 send_output (clients, buf, red);
504 tty_parse (&c, buf, red);
510 /*any data from the server */
514 if (socket_post_select (client_socket, &rfds, &wfds))
517 while (client_socket->msg)
519 if (msg_from_server (ansi, client_socket->msg, &c))
522 socket_consume_msg (client_socket);
529 /*update our local screen */
533 ansi->dispatch (ansi, &c);
536 ansi->update (ansi, &c);
541 clients_shutdown (clients);