From e87e545fd99fa20f207e6960b1ee7647fd5573b3 Mon Sep 17 00:00:00 2001 From: james <> Date: Wed, 20 Feb 2008 18:31:44 +0000 Subject: *** empty log message *** --- apps/Makefile.am | 5 +- apps/clients.c | 7 ++ apps/mainloop.c | 48 +++++---- apps/sympathy.c | 319 ++++++++++++++++++++++++++++++++++++++++++------------- 4 files changed, 282 insertions(+), 97 deletions(-) (limited to 'apps') diff --git a/apps/Makefile.am b/apps/Makefile.am index cb1c675..57a6f69 100644 --- a/apps/Makefile.am +++ b/apps/Makefile.am @@ -7,6 +7,9 @@ # $Id$ # # $Log$ +# Revision 1.8 2008/02/20 18:31:44 james +# *** empty log message *** +# # Revision 1.7 2008/02/16 01:30:56 james # *** empty log message *** # @@ -37,7 +40,7 @@ noinst_PROGRAMS = sympathyd sympathy noinst_HEADERS=clients.h client.h -sympathy_SOURCES = sympathy.c client.c usage.c clients.c mainloop.c +sympathy_SOURCES = sympathy.c usage.c clients.c mainloop.c sympathy_LDADD = ../src/libsympathy.a -lutil sympathyd_SOURCES = sympathyd.c clients.c client.c diff --git a/apps/clients.c b/apps/clients.c index 04aafc1..73bf655 100644 --- a/apps/clients.c +++ b/apps/clients.c @@ -10,6 +10,9 @@ static char rcsid[] = "$Id$"; /* * $Log$ + * Revision 1.8 2008/02/20 18:31:44 james + * *** empty log message *** + * * Revision 1.7 2008/02/15 23:52:12 james * *** empty log message *** * @@ -74,7 +77,9 @@ client_free (Client * c) socket_free (c->s); free (c); +#if 0 fprintf (stderr, "Client at %p freed\n", c); +#endif } Client * @@ -91,7 +96,9 @@ clients_new_client (Clients * cs, Socket * s, Context * ctx) cs->head = c; cs->n++; +#if 0 fprintf (stderr, "Client at %p created\n", c); +#endif if (ipc_msg_send_debug (s, "new_client")) diff --git a/apps/mainloop.c b/apps/mainloop.c index 851f296..fd2947f 100644 --- a/apps/mainloop.c +++ b/apps/mainloop.c @@ -10,6 +10,9 @@ static char rcsid[] = "$Id$"; /* * $Log$ + * Revision 1.5 2008/02/20 18:31:44 james + * *** empty log message *** + * * Revision 1.4 2008/02/20 17:18:33 james * *** empty log message *** * @@ -62,8 +65,8 @@ static char rcsid[] = "$Id$"; #include #include +#include "mainloop.h" -#include "client.h" #include "clients.h" typedef struct @@ -233,11 +236,11 @@ check_status (Context * c, Clients * cs) status = get_status (c->t, cs); if (!memcmp (&status, &old_status, sizeof (status))) return; - old_status = status; - log_line_changes (c, old_status.lines, status.lines); + old_status = status; + ptr += sprintf (ptr, "CTRL-B "); t = c->t->name; @@ -293,7 +296,7 @@ check_status (Context * c, Clients * cs) if (cs) send_status (cs, buf); else - cmd_new_status (c->d, c, m->status.status); + cmd_new_status (c->d, c, buf); } @@ -317,7 +320,7 @@ msg_from_server (IPC_Msg * m, Context * c) abort (); *(c->v) = m->vt102.vt102; - //FIXME HTML hook + //FIXME HTML hook break; case IPC_MSG_TYPE_TERM: vt102_parse (c, m->term.term, m->term.len); @@ -332,23 +335,22 @@ msg_from_server (IPC_Msg * m, Context * c) void -mainloop (TTY * tty, Socket * server_socket, Socket * client_socket, Ansi * a, - Log * log) +mainloop (TTY * tty, Socket * server_socket, Socket * client_socket, + ANSI * ansi, Log * log, int nhistory) { fd_set rfds, wfds; - Context c={0}; + Context c = { 0 }; Clients *clients; c.v = vt102_new (); - c.h = history_new (200); + c.h = history_new (nhistory); c.l = log; - /* are we being fed by a tty or a socket */ - if (client_socket) + /* are we being fed by a tty or a socket */ + if (client_socket) { if (server_socket) abort (); - c.s = client_socket; c.k = keydis_ipc_new (client_socket); } else @@ -386,8 +388,12 @@ mainloop (TTY * tty, Socket * server_socket, Socket * client_socket, Ansi * a, { struct timeval tv = { 1, 0 }; - /*update the status lines, locally or remotely*/ - check_status (&c, clients); + if ((c.d) && (c.d->disconnect)) + break; + + /*update the status lines, locally or remotely */ + if (c.t) + check_status (&c, clients); FD_ZERO (&rfds); FD_ZERO (&wfds); @@ -414,7 +420,7 @@ mainloop (TTY * tty, Socket * server_socket, Socket * client_socket, Ansi * a, { Socket *new_client_socket; if (FD_ISSET (server_socket->fd, &rfds) - && ((new_client_socket = socket_accept (s)))) + && ((new_client_socket = socket_accept (server_socket)))) { { Client *new_client; @@ -444,14 +450,15 @@ mainloop (TTY * tty, Socket * server_socket, Socket * client_socket, Ansi * a, if (red) { - send_output (clients, buf, red); + if (clients) + send_output (clients, buf, red); vt102_parse (&c, buf, red); } } - /*any data from the server*/ + /*any data from the server */ if (client_socket) { if (socket_post_select (client_socket, &rfds, &wfds)) @@ -465,7 +472,7 @@ mainloop (TTY * tty, Socket * server_socket, Socket * client_socket, Ansi * a, } - /*update our local screen*/ + /*update our local screen */ if (ansi) { if (ansi_dispatch (ansi, &c)) @@ -476,8 +483,5 @@ mainloop (TTY * tty, Socket * server_socket, Socket * client_socket, Ansi * a, } if (clients) - { - clients_shutdown (clients); - } - printf ("QUAT\n"); + clients_shutdown (clients); } diff --git a/apps/sympathy.c b/apps/sympathy.c index 964d84c..29cf0ac 100644 --- a/apps/sympathy.c +++ b/apps/sympathy.c @@ -10,6 +10,9 @@ static char rcsid[] = "$Id$"; /* * $Log$ + * Revision 1.7 2008/02/20 18:31:44 james + * *** empty log message *** + * * Revision 1.6 2008/02/20 17:18:33 james * *** empty log message *** * @@ -30,75 +33,180 @@ static char rcsid[] = "$Id$"; * */ +#include #include #include +#include +#include +#include +#include + #include "mainloop.h" -char *fatal_moan(char *fmt,...) +extern void usage (void); + +static char hostname[1024]; + +int +safe_atoi (char *a) +{ + char *end; + int ret; + + if (!a) + return -1; + + ret = (int) strtol (a, &end, 0); + + if (end == a) + return -1; + + return ret; +} + +char * +fatal_moan (char *fmt, ...) { -va_list ap; + va_list ap; -va_start(ap, fmt); -n = vfprintf (stderr,fmt,ap); -va_end(ap); + va_start (ap, fmt); + vfprintf (stderr, fmt, ap); + va_end (ap); -putc('\n',stderr); -exit(1); + putc ('\n', stderr); + exit (1); } /*make the path in fmt from home (hence the name) */ -char *mome(char *fmt,...) +char * +mome (char *fmt, ...) { - int n; - int homelen; - char *buf,*home; - va_list ap; + int n; + int homelen; + char *buf, *home; + va_list ap; + int size; + + home = getenv ("HOME"); + if (!home) + return NULL; - home=getenv("HOME"); - if (!home) return NULL; + homelen = strlen (home) + 1; - homelen=strlen(home)+1; + size = 1024 + homelen; - size=1024+homelen; + buf = malloc (size); - buf = malloc (size); + if (!buf) + fatal_moan ("malloc failed"); - if (!buf) - fatal_moan("malloc failed"); + strcpy (buf, home); + strcat (buf, "/"); - strcpy(buf,home); - strcat(buf,"/"); + while (1) + { - while (1) { + va_start (ap, fmt); + n = vsnprintf (buf + homelen, size - homelen, fmt, ap); + va_end (ap); - va_start(ap, fmt); - n = vsnprintf (buf+homelen, size-homelen, fmt, ap); - va_end(ap); + if (n > -1 && n < (size - homelen)) + return buf; - if (n > -1 && n < len) - return buf; + if (n > -1) /* glibc 2.1 */ + size = homelen + n + 1; + else /* glibc 2.0 */ + size *= 2; /* twice the old size */ - if (n > -1) /* glibc 2.1 */ - size = homelen+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"); + } } +int +list_sockets (void) +{ + struct dirent *ent; + struct stat buf; + char *sn = NULL; + Socket *s; + char *sockdir = mome ("/.sympathy/"); + DIR *dir = opendir (sockdir); + + + + int hostname_len = strlen (hostname); + + if (!dir) + fatal_moan ("can't examine %s for sockets", sockdir); + + rewinddir (dir); + + + while ((ent = readdir (dir))) + { + sn = mome (".sympathy/%s", 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", ent->d_name); + socket_free (s); + } + else + { + if (strncmp (ent->d_name, hostname, hostname_len)) + { + printf ("\t%s (Unknown - not this host)\n", ent->d_name); + } + else + { + printf ("\t%s (Dead, wiped)\n", ent->d_name); + unlink (sn); + } + } + + free (sn); + } + + closedir (dir); + + return 0; +} + + +void +get_hostname (void) +{ + struct utsname name; + + if (uname (&name)) + { + strcpy (hostname, "unknown."); + return; + } + + + strcpy (hostname, name.nodename); + strcat (hostname, "."); +} + int main (int argc, char *argv[]) @@ -113,11 +221,25 @@ main (int argc, char *argv[]) Socket *server_socket = NULL, *client_socket = NULL; ANSI *ansi = NULL; TTY *tty = NULL; + Log *log = NULL; + + int history = 200; + - memset (oflags[128], 0, sizeof (oflags)); - memset (oargs[128], 0, sizeof (oargs)); + get_hostname (); - while ((c = getopt (argc, argv, "tscr:d:pb:fL:Fk:n:")) != EOF) + memset (oflags, 0, sizeof (oflags)); + memset (oargs, 0, sizeof (oargs)); +#if 0 + "sympathy -t [-K] [-d serialdev|-p] [-b baud] [-f] [-L log]\n" + "sympathy -s [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-F] [-k skt]\n" + " [-n hlines]\n" + "sympathy [-s -c] [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-k skt]\n" + " [-n hlines]\n" + "sympathy -c [-H] -k skt\n" + "sympathy -r id [-H]\n" "sympathy {-l|-ls}\n" +#endif + while ((c = getopt (argc, argv, "tscr:lKHd:pb:fL:Fk:n:")) != EOF) { switch (c) { @@ -138,29 +260,57 @@ main (int argc, char *argv[]) } else { - moan ("unknown option %c", c); + fprintf (stderr, "unknown option %c\n", c); usage (); } } } - if (!oflags['s'] && !oflags['c'] && !oflags['t'] && !oflags['r']) + /*Compatability for screen's ls */ + if (oflags['l']) + oflags['s'] = 0; + + + if (!oflags['s'] && !oflags['c'] && !oflags['t'] && !oflags['r'] + && !oflags['l']) { /*If no mode is specified behave like screen */ oflags['s']++; oflags['c']++; } - if ((oflags['t'] && ((oflags['s'] || oflags['c']) || oflags['r'])) || - (oflags['r'] && ((oflags['s'] || oflags['c']) || oflags['t'])) || - ((oflags['s'] || oflags['c']) && (oflags['r'] || oflags['t']))) - fatal_moan ("specifiy exactly one of ( -c and or -s ), -t and -r"); + + { + int sum = 0; + sum += oflags['t']; + sum += (oflags['s'] || oflags['c']) ? 1 : 0; + sum += oflags['r']; + sum += oflags['l']; + + if (sum != 1) + fatal_moan ("specifiy exactly one of ( -c and or -s ), -t, -r and -l"); + } + + + if (oflags['l']) + return list_sockets (); + if (oflags['r'] && oflags['k']) fatal_moan ("-k is incompatible with -r"); + if (oflags['n']) + { + nhistory = safe_atoi (oargs['n']); + if (nhistory < 0) + fatal_moan ("cannot parse -n %s as an integer", oargs['n']); + + if (!nhistory) + fatal_moan ("agrument to -n must be greater than zero"); + } + /*Fold -r into -c */ if (oflags['r']) { @@ -169,14 +319,11 @@ main (int argc, char *argv[]) fatal_moan ("cannot parse -r %s as an integer", oargs['r']); oflags['k']++; - oargs['k'] = mome ("/.sympathy/%d", id); + oargs['k'] = mome ("/.sympathy/%s%d", hostname, id); oflags['r'] = 0; oflags['c']++; } - if (oflags['c'] && !oflags['k']) - fatal_moan ("-c requires a socket to be specified with -s or -k"); - if (oflags['p'] && oflags['d']) fatal_moan ("-p incompatible with -d"); @@ -195,7 +342,7 @@ main (int argc, char *argv[]) mkdir (path, 0700); free (path); - oargs['k'] = mome ("/.sympathy/%d", getpid ()); + oargs['k'] = mome ("/.sympathy/%s%d", hostname, getpid ()); oflags['k']++; } @@ -206,25 +353,35 @@ main (int argc, char *argv[]) fatal_moan ("failed to create socket %s for listening", oargs['k']); } - switch (fork ()) + if (oflags['s'] && oflags['c']) { - case 0: /*child becomes the server */ - oflags['c'] = 0; - oflags['H'] = 0; - break; - case -1: - fatal_moan ("fork failed"); - default: - oflags['s'] = 0; - oflags['K'] = 0; - oflags['d'] = 0; - oflags['p'] = 0; - oflags['b'] = 0; - oflags['f'] = 0; - oflags['L'] = 0; - oflags['n'] = 0; + switch (fork ()) + { + case 0: /*child becomes the server */ + oflags['c'] = 0; + oflags['H'] = 0; + break; + case -1: + fatal_moan ("fork failed"); + default: + oflags['s'] = 0; + oflags['K'] = 0; + oflags['d'] = 0; + oflags['p'] = 0; + oflags['b'] = 0; + oflags['f'] = 0; + oflags['L'] = 0; + oflags['n'] = 0; + if (server_socket) + { + socket_free_parent (server_socket); + server_socket = NULL; + } + } } + if (oflags['c'] && !oflags['k']) + fatal_moan ("-c requires a socket to be specified with -s or -k"); if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f'] || oflags['L']) && oflags['c']) @@ -250,6 +407,8 @@ main (int argc, char *argv[]) if (oflags['p']) { tty = ptty_open (NULL, NULL); + if (!tty) + fatal_moan ("unable to open a ptty"); } else { @@ -257,7 +416,7 @@ main (int argc, char *argv[]) serial_open (oargs['d'], oflags['K'] ? SERIAL_LOCK_ACTIVE : SERIAL_LOCK_PASSIVE); - if (tty) + if (!tty) fatal_moan ("unable to open serial port %s", oargs['d']); } @@ -266,7 +425,7 @@ main (int argc, char *argv[]) int baud = safe_atoi (oargs['b']); if (baud < 0) - fatal_maon ("Unable to parse baudrate %s", oargs['b']); + fatal_moan ("Unable to parse baudrate %s", oargs['b']); tty_set_baud (tty, baud); @@ -286,6 +445,11 @@ main (int argc, char *argv[]) } + if (oflags['s'] && !oflags['F']) + { + daemon (1, 0); /*incase socket is relative path, unlink then will fail */ + } + if (oflags['c'] || oflags['t']) { @@ -304,11 +468,7 @@ main (int argc, char *argv[]) } } - if (oflags['s'] && !oflags['F']) { - /*FIXME become a daemon*/ - } - - mainloop (tty, server_socket, client_socket, ansi, log); + mainloop (tty, server_socket, client_socket, ansi, log, nhistory); if (ansi) { @@ -316,5 +476,16 @@ main (int argc, char *argv[]) terminal_atexit (); } + if (tty) + tty->close (tty); + + if (log) + log->close (log); + if (server_socket) + socket_free (server_socket); + if (client_socket) + socket_free (client_socket); + + printf ("you have now exited sympathy\n"); return 0; } -- cgit v1.2.3