From 6a4255cf4a29954108907716e9e69c72f041d5da Mon Sep 17 00:00:00 2001 From: james <> Date: Fri, 9 May 2008 12:35:57 +0000 Subject: *** empty log message *** --- apps/clients.c | 199 +++++++------- apps/clients.h | 9 +- apps/mainloop.c | 400 +++++++++++++++------------- apps/sympathy.c | 799 ++++++++++++++++++++++++++++++++------------------------ apps/usage.c | 10 +- sympathy.1 | 62 ++--- 6 files changed, 826 insertions(+), 653 deletions(-) diff --git a/apps/clients.c b/apps/clients.c index 5971a67..0a38095 100644 --- a/apps/clients.c +++ b/apps/clients.c @@ -10,6 +10,9 @@ static char rcsid[] = "$Id$"; /* * $Log$ + * Revision 1.25 2008/05/09 12:35:57 james + * *** empty log message *** + * * Revision 1.24 2008/03/11 17:56:04 james * *** empty log message *** * @@ -104,51 +107,53 @@ client_initialize (Client * c, Context * ctx) void client_execute_message (Client * client, IPC_Msg * m, Context * c) { - switch (m->hdr.type) { - case IPC_MSG_TYPE_NOOP: - break; - case IPC_MSG_TYPE_DEBUG: - log_f (c->l, "", m->debug.msg); - break; - case IPC_MSG_TYPE_KEY: - vt102_send (c, m->key.key); - break; - case IPC_MSG_TYPE_SETBAUD: - tty_set_baud (c->t, m->setbaud.baud); - tty_parse_reset (c); - log_f (c->l, "", m->setbaud.baud); - break; - case IPC_MSG_TYPE_SENDBREAK: - log_f (c->l, ""); - tty_send_break (c->t); - break; - case IPC_MSG_TYPE_SETFLOW: - log_f (c->l, "", m->setflow.flow ? "on" : "off"); - tty_set_flow (c->t, m->setflow.flow); - break; - case IPC_MSG_TYPE_SETANSI: - vt102_set_ansi (c->v, m->setansi.ansi); - break; - case IPC_MSG_TYPE_HANGUP: - log_f (c->l, ""); - tty_hangup (c->t); - break; - case IPC_MSG_TYPE_SETSIZE: - vt102_resize (c, m->setsize.winsize); - break; - case IPC_MSG_TYPE_RESET: - vt102_reset (c); - break; - case IPC_MSG_TYPE_INITIALIZE: - client_initialize (client, c); - break; - case IPC_MSG_TYPE_KILLME: - client->dead++; - break; - - default: - log_f (c->l, "", m->hdr.type); - } + switch (m->hdr.type) + { + case IPC_MSG_TYPE_NOOP: + break; + case IPC_MSG_TYPE_DEBUG: + log_f (c->l, "", m->debug.msg); + break; + case IPC_MSG_TYPE_KEY: + vt102_send (c, m->key.key); + break; + case IPC_MSG_TYPE_SETBAUD: + tty_set_baud (c->t, m->setbaud.baud); + tty_parse_reset (c); + log_f (c->l, "", m->setbaud.baud); + break; + case IPC_MSG_TYPE_SENDBREAK: + log_f (c->l, ""); + tty_send_break (c->t); + break; + case IPC_MSG_TYPE_SETFLOW: + log_f (c->l, "", + m->setflow.flow ? "on" : "off"); + tty_set_flow (c->t, m->setflow.flow); + break; + case IPC_MSG_TYPE_SETANSI: + vt102_set_ansi (c->v, m->setansi.ansi); + break; + case IPC_MSG_TYPE_HANGUP: + log_f (c->l, ""); + tty_hangup (c->t); + break; + case IPC_MSG_TYPE_SETSIZE: + vt102_resize (c, m->setsize.winsize); + break; + case IPC_MSG_TYPE_RESET: + vt102_reset (c); + break; + case IPC_MSG_TYPE_INITIALIZE: + client_initialize (client, c); + break; + case IPC_MSG_TYPE_KILLME: + client->dead++; + break; + + default: + log_f (c->l, "", m->hdr.type); + } } @@ -197,18 +202,23 @@ clients_reap (Clients * cs, Context * ctx) Client **p, *c; - for (p = &cs->head; *p;) { - Client *c = *p; + for (p = &cs->head; *p;) + { + Client *c = *p; - if (c->dead) { - *p = c->next; - client_free (c); - cs->n--; - log_f (ctx->l, "", c, cs->n); - } else { - p = &(c->next); + if (c->dead) + { + *p = c->next; + client_free (c); + cs->n--; + log_f (ctx->l, "", c, + cs->n); + } + else + { + p = &(c->next); + } } - } } Clients * @@ -227,9 +237,10 @@ clients_pre_select (Clients * cs, fd_set * rfds, fd_set * wfds) { Client *c; - for (c = cs->head; c; c = c->next) { - socket_pre_select (c->s, rfds, wfds); - } + for (c = cs->head; c; c = c->next) + { + socket_pre_select (c->s, rfds, wfds); + } } void @@ -239,21 +250,24 @@ clients_post_select (Clients * cs, Context * ctx, fd_set * rfds, Client *c; int deaded = 0; - for (c = cs->head; c; c = c->next) { - if (socket_post_select (c->s, rfds, wfds)) { - c->dead++; - deaded++; - } + for (c = cs->head; c; c = c->next) + { + if (socket_post_select (c->s, rfds, wfds)) + { + c->dead++; + deaded++; + } + + if (c->s->msg) + { + client_execute_message (c, c->s->msg, ctx); + socket_consume_msg (c->s); + if (c->dead) + deaded++; + } - if (c->s->msg) { - client_execute_message (c, c->s->msg, ctx); - socket_consume_msg (c->s); - if (c->dead) - deaded++; } - } - if (deaded) clients_reap (cs, ctx); } @@ -264,9 +278,10 @@ clients_shutdown (Clients * cs, Context * ctx) { Client *c; - for (c = cs->head; c; c = c->next) { - c->dead++; - } + for (c = cs->head; c; c = c->next) + { + c->dead++; + } clients_reap (cs, ctx); @@ -300,11 +315,12 @@ send_status (Clients * cs, char *msg) strncpy (m->status, msg, IPC_MAX_BUF); m->status[IPC_MAX_BUF - 1] = 0; - for (c = cs->head; c; c = c->next) { - if (!c->dead && c->initialized) - if (ipc_msg_send (c->s, (IPC_Msg *) m)) - c->dead++; - } + for (c = cs->head; c; c = c->next) + { + if (!c->dead && c->initialized) + if (ipc_msg_send (c->s, (IPC_Msg *) m)) + c->dead++; + } return len; } @@ -328,11 +344,12 @@ send_output (Clients * cs, void *buf, int len) m->len = len; memcpy (m->term, buf, len); - for (c = cs->head; c; c = c->next) { - if (!c->dead && c->initialized) - if (ipc_msg_send (c->s, (IPC_Msg *) m)) - c->dead++; - } + for (c = cs->head; c; c = c->next) + { + if (!c->dead && c->initialized) + if (ipc_msg_send (c->s, (IPC_Msg *) m)) + c->dead++; + } return len; @@ -346,16 +363,18 @@ send_history (History * h, Client * c) HISTORY_INC (h, rptr); HISTORY_INC (h, rptr); - while (rptr != h->wptr) { - History_ent *l = &h->lines[rptr]; - if (l->valid) { + while (rptr != h->wptr) + { + History_ent *l = &h->lines[rptr]; + if (l->valid) + { - if (ipc_msg_send_history (c->s, l)) - c->dead++; + if (ipc_msg_send_history (c->s, l)) + c->dead++; + } + HISTORY_INC (h, rptr); } - HISTORY_INC (h, rptr); - } } void diff --git a/apps/clients.h b/apps/clients.h index d0bffd3..e467c67 100644 --- a/apps/clients.h +++ b/apps/clients.h @@ -12,6 +12,9 @@ /* * $Log$ + * Revision 1.12 2008/05/09 12:35:57 james + * *** empty log message *** + * * Revision 1.11 2008/03/11 17:56:04 james * *** empty log message *** * @@ -50,14 +53,16 @@ #ifndef __CLIENTS_H__ #define __CLIENTS_H__ -typedef struct Client_struct { +typedef struct Client_struct +{ struct Client_struct *next; int initialized; Socket *s; int dead; } Client; -typedef struct { +typedef struct +{ Client *head; int n; } Clients; diff --git a/apps/mainloop.c b/apps/mainloop.c index fe9fa50..5237bfe 100644 --- a/apps/mainloop.c +++ b/apps/mainloop.c @@ -11,6 +11,9 @@ static char rcsid[] = /* * $Log$ + * Revision 1.33 2008/05/09 12:35:57 james + * *** empty log message *** + * * Revision 1.32 2008/03/11 17:56:50 james * *** empty log message *** * @@ -152,7 +155,8 @@ static char rcsid[] = #include "clients.h" -typedef struct { +typedef struct +{ int nclients; int lines; int baud; @@ -189,10 +193,11 @@ get_status (TTY * t, Clients * cs) cd = (tty_status.lines & TIOCM_CD) ? 1 : 0; - if (cd != last_cd_state) { - gettimeofday (&last_cd_edge, NULL); - last_cd_state = cd; - } + if (cd != last_cd_state) + { + gettimeofday (&last_cd_edge, NULL); + last_cd_state = cd; + } gettimeofday (&now, NULL); timersub (&now, &last_cd_edge, &dif); @@ -205,44 +210,45 @@ static char * line_to_name (int l) { - switch (l) { + switch (l) + { #ifdef TIOCM_LE - case TIOCM_LE: - return "LE"; + case TIOCM_LE: + return "LE"; #endif #ifdef TIOCM_DTR - case TIOCM_DTR: - return "DTR"; + case TIOCM_DTR: + return "DTR"; #endif #ifdef TIOCM_RTS - case TIOCM_RTS: - return "RTS"; + case TIOCM_RTS: + return "RTS"; #endif #ifdef TIOCM_ST - case TIOCM_ST: - return "ST"; + case TIOCM_ST: + return "ST"; #endif #ifdef TIOCM_SR - case TIOCM_SR: - return "SR"; + case TIOCM_SR: + return "SR"; #endif #ifdef TIOCM_CTS - case TIOCM_CTS: - return "CTS"; + case TIOCM_CTS: + return "CTS"; #endif #ifdef TIOCM_CD - case TIOCM_CD: - return "CD"; + case TIOCM_CD: + return "CD"; #endif #ifdef TIOCM_RI - case TIOCM_RI: - return "RI"; + case TIOCM_RI: + return "RI"; #endif #ifdef TIOCM_DSR - case TIOCM_DSR: - return "DSR"; + case TIOCM_DSR: + return "DSR"; #endif - } + } return "??"; } @@ -264,18 +270,20 @@ log_line_changes (Context * ctx, int old, int new) while (*n) *(ptr++) = *(n++); - while (dif >= c) { + while (dif >= c) + { - if (dif & c) { - *(ptr++) = ' '; - *(ptr++) = (new & c) ? '+' : '-'; - n = line_to_name (c); - while (*n) - *(ptr++) = *(n++); - } + if (dif & c) + { + *(ptr++) = ' '; + *(ptr++) = (new & c) ? '+' : '-'; + n = line_to_name (c); + while (*n) + *(ptr++) = *(n++); + } - c <<= 1; - } + c <<= 1; + } *(ptr++) = '>'; *ptr = 0; @@ -336,45 +344,54 @@ check_status (Context * c, Clients * cs) ptr = do_line (ptr, status.lines, TIOCM_RI); ptr = do_line (ptr, status.lines, TIOCM_CD); - if (status.blocked) { - t = ", Locked"; - while (*t) - *(ptr++) = *(t++); - } - - if (status.crtscts) { - t = ", Flow"; - while (*t) - *(ptr++) = *(t++); - } + if (status.blocked) + { + t = ", Locked"; + while (*t) + *(ptr++) = *(t++); + } + + if (status.crtscts) + { + t = ", Flow"; + while (*t) + *(ptr++) = *(t++); + } #if 0 - if (status.lines & TIOCM_CD) { - ptr += - sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60, - status.cd_edge_sec % 60); - } else { - ptr += - sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60, - status.cd_edge_sec % 60); - } + if (status.lines & TIOCM_CD) + { + ptr += + sprintf (ptr, ", On %d.%d", status.cd_edge_sec / 60, + status.cd_edge_sec % 60); + } + else + { + ptr += + sprintf (ptr, ", Off %d.%d", status.cd_edge_sec / 60, + status.cd_edge_sec % 60); + } #endif ptr += sprintf (ptr, ", %d client%s", status.nclients, (status.nclients == 1) ? "" : "s"); - if (c->tp->biterrs) { + if (c->tp->biterrs) + { - ptr += - sprintf (ptr, ", %d err%s", c->tp->biterrs, - (c->tp->biterrs == 1) ? "" : "s"); + ptr += + sprintf (ptr, ", %d err%s", c->tp->biterrs, + (c->tp->biterrs == 1) ? "" : "s"); - if (c->tp->guessed_baud == -1) { - ptr += sprintf (ptr, " try higher"); - } else if (c->tp->guessed_baud > 0) { - ptr += sprintf (ptr, " try %db", c->tp->guessed_baud); + if (c->tp->guessed_baud == -1) + { + ptr += sprintf (ptr, " try higher"); + } + else if (c->tp->guessed_baud > 0) + { + ptr += sprintf (ptr, " try %db", c->tp->guessed_baud); + } } - } *ptr = 0; @@ -394,37 +411,39 @@ static int msg_from_server (ANSI * a, IPC_Msg * m, Context * c) { int err = 0; - switch (m->hdr.type) { - - case IPC_MSG_TYPE_NOOP: - break; - case IPC_MSG_TYPE_DEBUG: - // fprintf (stderr,"%p [%d] %s\n", m, m->hdr.size , m->debug.msg ); - break; - case IPC_MSG_TYPE_HISTORY: - history_add (c->h, m->history.history.line); - break; - case IPC_MSG_TYPE_VT102: - if (sizeof (VT102) != m->vt102.len) - crash_out ("sizeof(VT102) differs in client and server"); - - *(c->v) = m->vt102.vt102; - - if (a->one_shot) { - a->one_shot (a, &c->v->crt); - err++; /* Simulate a fatal write error enclosing tty + switch (m->hdr.type) + { + + case IPC_MSG_TYPE_NOOP: + break; + case IPC_MSG_TYPE_DEBUG: + // fprintf (stderr,"%p [%d] %s\n", m, m->hdr.size , m->debug.msg ); + break; + case IPC_MSG_TYPE_HISTORY: + history_add (c->h, m->history.history.line); + break; + case IPC_MSG_TYPE_VT102: + if (sizeof (VT102) != m->vt102.len) + crash_out ("sizeof(VT102) differs in client and server"); + + *(c->v) = m->vt102.vt102; + + if (a->one_shot) + { + a->one_shot (a, &c->v->crt); + err++; /* Simulate a fatal write error enclosing tty */ + } + break; + case IPC_MSG_TYPE_TERM: + err += tty_parse (c, m->term.term, m->term.len); + break; + case IPC_MSG_TYPE_STATUS: + cmd_new_status (c->d, c, m->status.status); + break; + default: + fprintf (stderr, "Unhandeled message type %d\n", m->hdr.type); } - break; - case IPC_MSG_TYPE_TERM: - err += tty_parse (c, m->term.term, m->term.len); - break; - case IPC_MSG_TYPE_STATUS: - cmd_new_status (c->d, c, m->status.status); - break; - default: - fprintf (stderr, "Unhandeled message type %d\n", m->hdr.type); - } return err; } @@ -440,130 +459,149 @@ mainloop (Context * c, ANSI * ansi, Socket * server_socket, c->u = utf8_new (); /* are we being fed by a tty or a socket */ - if (client_socket) { - if (server_socket) - crash_out ("mainloop cannot both be a server and a client"); - c->k = keydis_ipc_new (client_socket); - } else { - if (!c->t) - crash_out ("mainloop must have either a client_socket or a terminal"); - c->k = keydis_vt102_new (); - } + if (client_socket) + { + if (server_socket) + crash_out ("mainloop cannot both be a server and a client"); + c->k = keydis_ipc_new (client_socket); + } + else + { + if (!c->t) + crash_out ("mainloop must have either a client_socket or a terminal"); + c->k = keydis_vt102_new (); + } /* do we have an upstream terminal to talk to */ /* if so start a command parser */ - if (ansi) { - c->d = cmd_new (); - } else { - c->d = NULL; - } + if (ansi) + { + c->d = cmd_new (); + } + else + { + c->d = NULL; + } vt102_reset (c); - if (server_socket) { - if (client_socket) - crash_out ("mainloop cannot both be a server and a client"); - clients = clients_new (); - } else { - clients = NULL; - } + if (server_socket) + { + if (client_socket) + crash_out ("mainloop cannot both be a server and a client"); + clients = clients_new (); + } + else + { + clients = NULL; + } - for (;;) { - struct timeval tv = { 0, 250000 }; + for (;;) + { + struct timeval tv = { 0, 250000 }; - if ((c->d) && (c->d->disconnect)) - break; + if ((c->d) && (c->d->disconnect)) + break; - /* update the status lines, locally or remotely */ - if (c->t) - check_status (c, clients); + /* update the status lines, locally or remotely */ + if (c->t) + check_status (c, clients); - FD_ZERO (&rfds); - FD_ZERO (&wfds); + FD_ZERO (&rfds); + FD_ZERO (&wfds); - if (c->t) - tty_pre_select (c->t, &rfds, &wfds); + if (c->t) + tty_pre_select (c->t, &rfds, &wfds); - if (server_socket) { - FD_SET (server_socket->fd, &rfds); - clients_pre_select (clients, &rfds, &wfds); - } + if (server_socket) + { + FD_SET (server_socket->fd, &rfds); + clients_pre_select (clients, &rfds, &wfds); + } - if (client_socket) - socket_pre_select (client_socket, &rfds, &wfds); + if (client_socket) + socket_pre_select (client_socket, &rfds, &wfds); - if (ansi && ansi->terminal) - tty_pre_select (ansi->terminal, &rfds, &wfds); + if (ansi && ansi->terminal) + tty_pre_select (ansi->terminal, &rfds, &wfds); - select (FD_SETSIZE, &rfds, &wfds, NULL, &tv); + select (FD_SETSIZE, &rfds, &wfds, NULL, &tv); - /* any message from clients, or new connexions */ - if (server_socket) { - Socket *new_client_socket; - if (FD_ISSET (server_socket->fd, &rfds) - && ((new_client_socket = socket_accept (server_socket)))) { + /* any message from clients, or new connexions */ + if (server_socket) { - Client *new_client; - /* New client connexion */ - new_client = clients_new_client (clients, new_client_socket, c); + Socket *new_client_socket; + if (FD_ISSET (server_socket->fd, &rfds) + && ((new_client_socket = socket_accept (server_socket)))) + { + { + Client *new_client; + /* New client connexion */ + new_client = + clients_new_client (clients, new_client_socket, c); + } + } + + clients_post_select (clients, c, &rfds, &wfds); } - } - clients_post_select (clients, c, &rfds, &wfds); - } + /* any data from the port */ + if (c->t && FD_ISSET (c->t->rfd, &rfds)) + { + char buf[IPC_MAX_BUF]; + int red; - /* any data from the port */ - if (c->t && FD_ISSET (c->t->rfd, &rfds)) { - char buf[IPC_MAX_BUF]; - int red; + red = c->t->recv (c->t, buf, sizeof (buf)); - red = c->t->recv (c->t, buf, sizeof (buf)); + if (red < 0) + break; - if (red < 0) - break; - - if (red) { - if (clients) - send_output (clients, buf, red); - if (tty_parse (c, buf, red)) - break; - } - } + if (red) + { + if (clients) + send_output (clients, buf, red); + if (tty_parse (c, buf, red)) + break; + } + } - /* any data from the server */ - if (client_socket) { - int err = 0; + /* any data from the server */ + if (client_socket) + { + int err = 0; - if (socket_post_select (client_socket, &rfds, &wfds)) - break; + if (socket_post_select (client_socket, &rfds, &wfds)) + break; - while (client_socket->msg && !err) { - err += msg_from_server (ansi, client_socket->msg, c); - socket_consume_msg (client_socket); - } + while (client_socket->msg && !err) + { + err += msg_from_server (ansi, client_socket->msg, c); + socket_consume_msg (client_socket); + } - if (err) - break; - } + if (err) + break; + } - /* update our local screen */ - if (ansi) { - if (ansi->dispatch) - if (ansi->dispatch (ansi, c)) - break; + /* update our local screen */ + if (ansi) + { + if (ansi->dispatch) + if (ansi->dispatch (ansi, c)) + break; - if (ansi->update) - if (ansi->update (ansi, c)) - break; + if (ansi->update) + if (ansi->update (ansi, c)) + break; + } } - } if (clients) clients_shutdown (clients, c); diff --git a/apps/sympathy.c b/apps/sympathy.c index c85bdae..776ae4f 100644 --- a/apps/sympathy.c +++ b/apps/sympathy.c @@ -11,6 +11,9 @@ static char rcsid[] = /* * $Log$ + * Revision 1.48 2008/05/09 12:35:57 james + * *** empty log message *** + * * Revision 1.47 2008/05/09 12:26:58 staffcvs * *** empty log message *** * @@ -172,7 +175,7 @@ static char rcsid[] = #include "mainloop.h" -int use_syslog=0; +int use_syslog = 0; extern void usage (void); extern char *expand (const char *, int *); @@ -205,10 +208,18 @@ fatal_moan (char *fmt, ...) va_start (ap, fmt); - vfprintf (stderr, fmt, ap); + + if (use_syslog) + { + vsyslog (LOG_ERR, fmt, ap); + } + else + { + vfprintf (stderr, fmt, ap); + putc ('\n', stderr); + } va_end (ap); - putc ('\n', stderr); exit (1); } @@ -241,25 +252,26 @@ teedious_snprintf (char *fmt, va_list ap) if (!buf) fatal_moan ("malloc failed"); - while (1) { + while (1) + { - va_copy (aq, ap); - n = vsnprintf (buf, size, fmt, aq); - va_end (aq); + va_copy (aq, ap); + n = vsnprintf (buf, size, fmt, aq); + va_end (aq); - if (n > -1 && n < (size)) - return buf; + if (n > -1 && n < (size)) + return buf; - if (n > -1) /* glibc 2.1 */ - size = n + 1; - else /* glibc 2.0 */ - size *= 2; /* twice the old size */ + if (n > -1) /* glibc 2.1 */ + size = n + 1; + else /* glibc 2.0 */ + size *= 2; /* twice the old size */ - buf = realloc (buf, size); + buf = realloc (buf, size); - if (!buf) - fatal_moan ("malloc failed"); - } + if (!buf) + fatal_moan ("malloc failed"); + } } @@ -297,13 +309,16 @@ mome (char *fmt, ...) if (!home) return NULL; - if (fmt) { - va_start (ap, fmt); - leaf = teedious_snprintf (fmt, ap); - va_end (ap); - } else { - leaf = NULL; - } + if (fmt) + { + va_start (ap, fmt); + leaf = teedious_snprintf (fmt, ap); + va_end (ap); + } + else + { + leaf = NULL; + } ret = gloo_paths (home, leaf); if (leaf) @@ -322,45 +337,56 @@ find_socket (char **retpath, char *fmt, ...) va_list ap; - if (fmt) { - va_start (ap, fmt); - leaf = teedious_snprintf (fmt, ap); - va_end (ap); - } else { - leaf = NULL; - } + if (fmt) + { + va_start (ap, fmt); + leaf = teedious_snprintf (fmt, ap); + va_end (ap); + } + else + { + leaf = NULL; + } - for (ptr = socket_dirs; *ptr; ptr++) { - if (**ptr == '~') { - h = mome (*ptr + 1); - } else { - h = *ptr; - } + for (ptr = socket_dirs; *ptr; ptr++) + { + if (**ptr == '~') + { + h = mome (*ptr + 1); + } + else + { + h = *ptr; + } - if (!h) - continue; + if (!h) + continue; - path = gloo_paths (h, leaf); - if (**ptr == '~') - free (h); + path = gloo_paths (h, leaf); + if (**ptr == '~') + free (h); - ret = socket_connect (path); + ret = socket_connect (path); - if (ret) { - if (retpath) { - *retpath = path; - } else { - free (path); - } - free (leaf); - return ret; - } - free (path); + if (ret) + { + if (retpath) + { + *retpath = path; + } + else + { + free (path); + } + free (leaf); + return ret; + } + free (path); - } + } free (leaf); return NULL; @@ -388,29 +414,37 @@ list_sockets_in_dir (char *sockdir) rewinddir (dir); - while ((ent = readdir (dir))) { - sn = gloo_paths (sockdir, ent->d_name); - if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode))) { - free (sn); - continue; - } + while ((ent = readdir (dir))) + { + sn = gloo_paths (sockdir, ent->d_name); + if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode))) + { + free (sn); + continue; + } - s = socket_connect (sn); - - if (s) { - printf ("\t%s (Active)\n", sn); - socket_free (s); - } else { - if (strncmp (ent->d_name, hostname, hostname_len)) { - printf ("\t%s (Unknown - not this host)\n", sn); - } else { - printf ("\t%s (Dead, wiped)\n", sn); - unlink (sn); - } - } + s = socket_connect (sn); - free (sn); - } + if (s) + { + printf ("\t%s (Active)\n", sn); + socket_free (s); + } + else + { + if (strncmp (ent->d_name, hostname, hostname_len)) + { + printf ("\t%s (Unknown - not this host)\n", sn); + } + else + { + printf ("\t%s (Dead, wiped)\n", sn); + unlink (sn); + } + } + + free (sn); + } closedir (dir); @@ -423,22 +457,26 @@ list_sockets (void) char **ptr, *h; - for (ptr = socket_dirs; *ptr; ptr++) { - if (**ptr == '~') { - h = mome (*ptr + 1); - } else { - h = *ptr; - } + for (ptr = socket_dirs; *ptr; ptr++) + { + if (**ptr == '~') + { + h = mome (*ptr + 1); + } + else + { + h = *ptr; + } - if (!h) - continue; + if (!h) + continue; - list_sockets_in_dir (h); + list_sockets_in_dir (h); - if (**ptr == '~') - free (h); + if (**ptr == '~') + free (h); - } + } return 0; } @@ -448,10 +486,11 @@ get_hostname (void) { struct utsname name; - if (uname (&name)) { - strcpy (hostname, "unknown."); - return; - } + if (uname (&name)) + { + strcpy (hostname, "unknown."); + return; + } strcpy (hostname, name.nodename); @@ -468,10 +507,11 @@ send_to_server (Socket * c, char *s) if (!n) return; - while (n--) { - ipc_msg_send_key (c, *(uint8_t *) s); - s++; - } + while (n--) + { + ipc_msg_send_key (c, *(uint8_t *) s); + s++; + } ipc_msg_send_killme (c); } @@ -504,26 +544,45 @@ main (int argc, char *argv[]) memset (oflags, 0, sizeof (oflags)); memset (oargs, 0, sizeof (oargs)); - while ((c = getopt (argc, argv, "I:NCRP:vw:utscr:lKHd:pb:fL:Fk:n:")) != EOF) { - switch (c) { - case ':': - case 'h': - case '?': - usage (); - default: - if ((c >= 'A') && (c <= 'Z')) { - oflags[c]++; - oargs[c] = optarg; - } else if ((c >= 'a') && (c <= 'z')) { - oflags[c]++; - oargs[c] = optarg; - } else { - fprintf (stderr, "unknown option %c\n", c); - usage (); - } + while ((c = getopt (argc, argv, "I:NCRP:vw:utscr:lKHd:pb:fL:Fk:n:")) != EOF) + { + switch (c) + { + case ':': + case 'h': + case '?': + usage (); + case 'S': + use_syslog++; + openlog ("sympathy", LOG_PID | LOG_CONSOLE, LOG_DAEMON); + /*fall through */ + default: + if ((c >= 'A') && (c <= 'Z')) + { + oflags[c]++; + oargs[c] = optarg; + } + else if ((c >= 'a') && (c <= 'z')) + { + oflags[c]++; + oargs[c] = optarg; + } + else + { + if (use_syslog) + { + syslog (LOG_ERR, "unknown option %c\n", c); + } + else + { + fprintf (stderr, "unknown option %c\n", c); + } + usage (); + } + } + } - } /* Compatability for screen's ls */ if (oflags['l']) @@ -539,23 +598,25 @@ main (int argc, char *argv[]) sum += oflags['v']; sum += oflags['C']; - if (!sum) { - /* If no mode is specified behave like screen */ - oflags['s']++; - oflags['c']++; - sum++; - } + if (!sum) + { + /* If no mode is specified behave like screen */ + oflags['s']++; + oflags['c']++; + sum++; + } if (sum != 1) fatal_moan ("specifiy exactly one of ( -c and or -s ), -t, -r, -C, -l and -v"); } - if (oflags['v']) { - fprintf (stderr, "Version: %s\n", libsympathy_version ()); - fprintf (stderr, "Version: %s\n", rcsid); - return 0; - } + if (oflags['v']) + { + fprintf (stderr, "Version: %s\n", libsympathy_version ()); + fprintf (stderr, "Version: %s\n", rcsid); + return 0; + } if (oflags['l']) return list_sockets (); @@ -565,14 +626,15 @@ main (int argc, char *argv[]) fatal_moan ("-k is incompatible with -r"); - if (oflags['n']) { - history = safe_atoi (oargs['n']); - if (history < 0) - fatal_moan ("cannot parse -n %s as an integer", oargs['n']); + if (oflags['n']) + { + history = safe_atoi (oargs['n']); + if (history < 0) + fatal_moan ("cannot parse -n %s as an integer", oargs['n']); - if (!history) - fatal_moan ("agrument to -n must be greater than zero"); - } + if (!history) + fatal_moan ("agrument to -n must be greater than zero"); + } /* Fold -r implies -c */ if (oflags['r']) @@ -593,57 +655,60 @@ main (int argc, char *argv[]) /* server. If there's no -k argument the client (parent) needs */ /* to find out the pid of the server, we use a pipe */ - if (oflags['s'] && oflags['c']) { - cs++; - pipe (cs_pipe); - - switch (pid = fork ()) { - case 0: /* child becomes the server */ - oflags['c'] = 0; - oflags['H'] = 0; - oflags['N'] = 0; - oflags['I'] = 0; - - close (cs_pipe[0]); - break; - case -1: - fatal_moan ("fork failed"); - default: /* parent becomes client */ - oflags['s'] = 0; - oflags['K'] = 0; - oflags['d'] = 0; - oflags['p'] = 0; - oflags['b'] = 0; - oflags['f'] = 0; - oflags['L'] = 0; - oflags['R'] = 0; - oflags['P'] = 0; - oflags['n'] = 0; - oflags['w'] = 0; - - /* Collect the child */ - waitpid (pid, NULL, 0); - - /* if there was no k argument we need to find the */ - /* pid of the server process so that we can work out */ - /* what the socket is called. The server tells us on */ - /* a pipe. We do this even if k is specified to avoid */ - /* a race, the server writes to the pipe when the socket */ - /* is opened */ - - close (cs_pipe[1]); - - if (read (cs_pipe[0], &pid, sizeof (pid)) != sizeof (pid)) - fatal_moan ("Failed to receive pid of server process"); - - close (cs_pipe[0]); - - if (!oflags['k']) { - oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid); - oflags['k']++; - } + if (oflags['s'] && oflags['c']) + { + cs++; + pipe (cs_pipe); + + switch (pid = fork ()) + { + case 0: /* child becomes the server */ + oflags['c'] = 0; + oflags['H'] = 0; + oflags['N'] = 0; + oflags['I'] = 0; + + close (cs_pipe[0]); + break; + case -1: + fatal_moan ("fork failed"); + default: /* parent becomes client */ + oflags['s'] = 0; + oflags['K'] = 0; + oflags['d'] = 0; + oflags['p'] = 0; + oflags['b'] = 0; + oflags['f'] = 0; + oflags['L'] = 0; + oflags['R'] = 0; + oflags['P'] = 0; + oflags['n'] = 0; + oflags['w'] = 0; + + /* Collect the child */ + waitpid (pid, NULL, 0); + + /* if there was no k argument we need to find the */ + /* pid of the server process so that we can work out */ + /* what the socket is called. The server tells us on */ + /* a pipe. We do this even if k is specified to avoid */ + /* a race, the server writes to the pipe when the socket */ + /* is opened */ + + close (cs_pipe[1]); + + if (read (cs_pipe[0], &pid, sizeof (pid)) != sizeof (pid)) + fatal_moan ("Failed to receive pid of server process"); + + close (cs_pipe[0]); + + if (!oflags['k']) + { + oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid); + oflags['k']++; + } + } } - } @@ -658,219 +723,261 @@ main (int argc, char *argv[]) fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f, -R, -P or -L"); - if (oflags['C'] || !oflags['d']) - fatal_moan("-C requires -d"); + if (oflags['C'] || !oflags['d']) + fatal_moan ("-C requires -d"); - if (oflags['t'] || oflags['s']) { - if (!oflags['p'] && !oflags['d']) - oflags['p']++; - } - - if (oflags['w']) { - char buf[128], *ptr; - strcpy (buf, oargs['w']); - ptr = index (buf, 'x'); - if (ptr) { - *ptr = 0; - ptr++; - size.y = safe_atoi (ptr); + if (oflags['t'] || oflags['s']) + { + if (!oflags['p'] && !oflags['d']) + oflags['p']++; } - size.x = safe_atoi (buf); - - if ((size.x > VT102_MAX_COLS) || (size.x < 1)) - fatal_moan ("-w requires a width between 1 and %d\n", VT102_MAX_COLS); - - if ((size.y > VT102_MAX_ROWS) || (size.y < 1)) - fatal_moan ("-w requires a height between 1 and %d\n", VT102_MAX_ROWS); - - } - - if (oflags['s'] && !oflags['F']) { - /* nochdir incase socket is relative path, unlink then will fail */ - daemon (1, 0); - - } - garlic (); - - if (oflags['s']) { - char *path; - path = mome ("/.sympathy"); - mkdir (path, 0700); - free (path); + if (oflags['w']) + { + char buf[128], *ptr; + strcpy (buf, oargs['w']); + ptr = index (buf, 'x'); + if (ptr) + { + *ptr = 0; + ptr++; + size.y = safe_atoi (ptr); + } + size.x = safe_atoi (buf); + if ((size.x > VT102_MAX_COLS) || (size.x < 1)) + fatal_moan ("-w requires a width between 1 and %d\n", VT102_MAX_COLS); - if (!oflags['k']) { - pid = getpid (); + if ((size.y > VT102_MAX_ROWS) || (size.y < 1)) + fatal_moan ("-w requires a height between 1 and %d\n", + VT102_MAX_ROWS); - oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid); - oflags['k']++; } - server_socket = socket_listen (oargs['k']); - - /* Tell our parent's parent what our pid is */ - /* and that we've opened the server socket */ - if (cs) { - pid = getpid (); + if (oflags['s'] && !oflags['F']) + { + /* nochdir incase socket is relative path, unlink then will fail */ + daemon (1, 0); - write (cs_pipe[1], &pid, sizeof (pid)); - close (cs_pipe[1]); } - if (!server_socket) - fatal_moan ("failed to create socket %s for listening", oargs['k']); + garlic (); - } + if (oflags['s']) + { + char *path; + path = mome ("/.sympathy"); + mkdir (path, 0700); + free (path); - if (oflags['s'] || oflags['t'] || oflags['C']) { - if (oflags['P']) { - FILE *fp; - pid_file = oargs['P']; - fp = fopen (pid_file, "w"); - if (fp) { - fprintf (fp, "%d", getpid ()); - fclose (fp); - } - } - if (oflags['L']) { - ctx->l = file_log_new (oargs['L'], oflags['R']); - if (!ctx->l) - fatal_moan ("unable to access log file %s", oargs['L']); - } + if (!oflags['k']) + { + pid = getpid (); - if (oflags['p']) { - if (optind < argc) { - ctx->t = ptty_open (argv[optind], &argv[optind], &size); - } else { - ctx->t = ptty_open (NULL, NULL, &size); - } + oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid); + oflags['k']++; + } - if (!ctx->t) - fatal_moan ("unable to open a ptty"); - } else { - /* HACK-- check that console=device does not occur in */ - /* /proc/cmdline */ - if (!oargs['d']) - fatal_moan ("no argument to -d"); + server_socket = socket_listen (oargs['k']); - { - char kernel_cmdline[4096] = { 0 }; - char search_string[1024] = "console="; - char *ptr = oargs['d']; - int fd; + /* Tell our parent's parent what our pid is */ + /* and that we've opened the server socket */ + if (cs) + { + pid = getpid (); - if (!strncmp ("/dev/", ptr, 5)) - ptr += 5; + write (cs_pipe[1], &pid, sizeof (pid)); + close (cs_pipe[1]); + } - strcat (search_string, ptr); + if (!server_socket) + fatal_moan ("failed to create socket %s for listening", oargs['k']); - fd = open ("/proc/cmdline", O_RDONLY); - read (fd, kernel_cmdline, sizeof (kernel_cmdline)); - close (fd); + } - kernel_cmdline[sizeof (kernel_cmdline) - 1] = 0; + if (oflags['s'] || oflags['t'] || oflags['C']) + { + if (oflags['P']) + { + FILE *fp; + pid_file = oargs['P']; + fp = fopen (pid_file, "w"); + if (fp) + { + fprintf (fp, "%d", getpid ()); + fclose (fp); + } + } - if (strstr (kernel_cmdline, search_string)) - fatal_moan ("/proc/cmdline contains %s", search_string); - } + if (oflags['L']) + { + ctx->l = file_log_new (oargs['L'], oflags['R']); + if (!ctx->l) + fatal_moan ("unable to access log file %s", oargs['L']); + } - ctx->t = - serial_open (oargs['d'], - oflags['K'] ? SERIAL_LOCK_ACTIVE : SERIAL_LOCK_PASSIVE); - if (!ctx->t) - fatal_moan ("unable to open serial port %s", oargs['d']); + if (oflags['p']) + { + if (optind < argc) + { + ctx->t = ptty_open (argv[optind], &argv[optind], &size); + } + else + { + ctx->t = ptty_open (NULL, NULL, &size); + } + + if (!ctx->t) + fatal_moan ("unable to open a ptty"); + } + else + { + /* HACK-- check that console=device does not occur in */ + /* /proc/cmdline */ + if (!oargs['d']) + fatal_moan ("no argument to -d"); + + { + char kernel_cmdline[4096] = { 0 }; + char search_string[1024] = "console="; + char *ptr = oargs['d']; + int fd; + + if (!strncmp ("/dev/", ptr, 5)) + ptr += 5; + + strcat (search_string, ptr); + + fd = open ("/proc/cmdline", O_RDONLY); + read (fd, kernel_cmdline, sizeof (kernel_cmdline)); + close (fd); + + kernel_cmdline[sizeof (kernel_cmdline) - 1] = 0; + + if (strstr (kernel_cmdline, search_string)) + fatal_moan ("/proc/cmdline contains %s", search_string); + } + + ctx->t = + serial_open (oargs['d'], + oflags['K'] ? SERIAL_LOCK_ACTIVE : + SERIAL_LOCK_PASSIVE); + if (!ctx->t) + fatal_moan ("unable to open serial port %s", oargs['d']); + + if (oflags['C']) + { + ctx->t->close (ctx->t); + return 0; + } + } - if (oflags['C']) { - ctx->t->close(ctx->t); - return 0; - } - } + if (oflags['b']) + { + int baud = safe_atoi (oargs['b']); - if (oflags['b']) { - int baud = safe_atoi (oargs['b']); + if (baud < 0) + fatal_moan ("Unable to parse baudrate %s", oargs['b']); - if (baud < 0) - fatal_moan ("Unable to parse baudrate %s", oargs['b']); + tty_set_baud (ctx->t, baud); - tty_set_baud (ctx->t, baud); + } + tty_set_flow (ctx->t, oflags['f'] ? 1 : 0); } - tty_set_flow (ctx->t, oflags['f'] ? 1 : 0); - } - - if (oflags['r']) { - char *id = oargs['r']; - - if (!id) - fatal_moan ("-r requires an argument"); + if (oflags['r']) + { + char *id = oargs['r']; - if (safe_atoi (id) > 0) { - client_socket = - find_socket (&oargs['k'], "%s%d", hostname, safe_atoi (id)); - } else { - client_socket = find_socket (&oargs['k'], "%s", id); - } + if (!id) + fatal_moan ("-r requires an argument"); - if (!client_socket) - fatal_moan ("failed to find socket %s", oargs['r']); + if (safe_atoi (id) > 0) + { + client_socket = + find_socket (&oargs['k'], "%s%d", hostname, safe_atoi (id)); + } + else + { + client_socket = find_socket (&oargs['k'], "%s", id); + } - } else if (oflags['c']) { - client_socket = socket_connect (oargs['k']); + if (!client_socket) + fatal_moan ("failed to find socket %s", oargs['r']); - if (!client_socket) - fatal_moan ("failed to connect to socket %s", oargs['k']); + } + else if (oflags['c']) + { + client_socket = socket_connect (oargs['k']); - } + if (!client_socket) + fatal_moan ("failed to connect to socket %s", oargs['k']); - if (oflags['I']) { - if (!client_socket) - fatal_moan ("-I requires either -c or -r", oargs['k']); - if (!oargs['I']) - fatal_moan ("-I requires an arugment"); - send_to_server (client_socket, oargs['I']); - } else { - if (client_socket) - ipc_msg_send_initialize (client_socket); - - if (oflags['c'] || oflags['t']) { - if (oflags['N']) { - ctx->r = rx_new_raw (0, 1); - ansi = ansi_new_raw (0, 1); - } else if (oflags['H']) { - ansi = ansi_new_html (stdout); - } else { - terminal_register_handlers (); - ansi = - ansi_new_from_terminal (terminal_open (0, 1), oflags['u'] ? 0 : 1); - ansi->reset (ansi, NULL); + } - } + if (oflags['I']) + { + if (!client_socket) + fatal_moan ("-I requires either -c or -r", oargs['k']); + if (!oargs['I']) + fatal_moan ("-I requires an arugment"); + send_to_server (client_socket, oargs['I']); + } + else + { + if (client_socket) + ipc_msg_send_initialize (client_socket); + + if (oflags['c'] || oflags['t']) + { + if (oflags['N']) + { + ctx->r = rx_new_raw (0, 1); + ansi = ansi_new_raw (0, 1); + } + else if (oflags['H']) + { + ansi = ansi_new_html (stdout); + } + else + { + terminal_register_handlers (); + ansi = + ansi_new_from_terminal (terminal_open (0, 1), + oflags['u'] ? 0 : 1); + ansi->reset (ansi, NULL); + + } + + if (ansi->set_title) + { + if (oflags['c'] && oargs['k']) + { + ansi->set_title (ansi, oargs['k']); + } + else if ((ctx->t) && (ctx->t->name)) + { + ansi->set_title (ansi, ctx->t->name); + } + } - if (ansi->set_title) { - if (oflags['c'] && oargs['k']) { - ansi->set_title (ansi, oargs['k']); - } else if ((ctx->t) && (ctx->t->name)) { - ansi->set_title (ansi, ctx->t->name); } - } - } - } ctx->v = vt102_new (&size); ctx->h = history_new (history); mainloop (ctx, ansi, server_socket, client_socket); - if (ansi) { - ansi->close (ansi); - terminal_atexit (); - } + if (ansi) + { + ansi->close (ansi); + terminal_atexit (); + } if (ctx->t) ctx->t->close (ctx->t); diff --git a/apps/usage.c b/apps/usage.c index 3715916..032f9a6 100644 --- a/apps/usage.c +++ b/apps/usage.c @@ -6,10 +6,14 @@ * */ -static char rcsid[] = "$Id$"; +static char rcsid[] = + "$Id$"; /* * $Log$ + * Revision 1.27 2008/05/09 12:35:57 james + * *** empty log message *** + * * Revision 1.26 2008/05/09 12:26:58 staffcvs * *** empty log message *** * @@ -127,7 +131,7 @@ usage (void) " ~/.sympathy/id or ~/.sympathy/hostname.id if id is an\n" " integer\n" " -l or -ls list active sockets in ~/.sympathy\n" - " -C clear inactive locks\n" + " -C clear inactive locks\n" " -v show version\n" " -h show help\n" "\n" @@ -155,7 +159,7 @@ usage (void) " server, that are replayed on connexion\n" " -P pidfile write the pid of the server/terminal process to pidfile\n" " -R rotate logile specified with -L option\n" - " -S log errors to syslog\n" + " -S log errors to syslog\n" " -u don't emit utf-8 try to use ISO-2202 to the local terminal\n" " -w W[xH] start session with a screen of size W by H. 0