X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=apps%2Fsympathy.c;h=df351f3e9111f56c40082daeeeb5655cfd2e6937;hb=ece1299582523fd5a339398bdddb77f525e2a2fc;hp=8bed9296bb5f1445ea020d0346e0ac5f3efa9c30;hpb=b02dedb723ba0fec51420d49a5f8085397752147;p=sympathy.git diff --git a/apps/sympathy.c b/apps/sympathy.c index 8bed929..df351f3 100644 --- a/apps/sympathy.c +++ b/apps/sympathy.c @@ -10,13 +10,495 @@ static char rcsid[] = "$Id$"; /* * $Log$ + * Revision 1.10 2008/02/20 19:44:37 james + * @@ + * + * Revision 1.9 2008/02/20 18:49:11 staffcvs + * *** empty log message *** + * + * Revision 1.8 2008/02/20 18:33:37 james + * *** empty log message *** + * + * 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 *** + * + * Revision 1.5 2008/02/20 15:50:14 james + * *** empty log message *** + * + * Revision 1.4 2008/02/20 02:11:35 james + * *** empty log message *** + * + * Revision 1.3 2008/02/14 02:46:44 james + * *** empty log message *** + * + * Revision 1.2 2008/02/14 00:57:58 james + * *** empty log message *** + * * Revision 1.1 2008/02/05 14:25:49 james * *** empty log message *** * */ -#include "sympathy.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mainloop.h" + +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_start (ap, fmt); + vfprintf (stderr, fmt, ap); + va_end (ap); + + putc ('\n', stderr); + exit (1); +} + + + +/*make the path in fmt from home (hence the name) */ + +char * +mome (char *fmt, ...) +{ + + int n; + int homelen; + char *buf, *home; + va_list ap; + int size; + + + home = getenv ("HOME"); + if (!home) + return NULL; + + homelen = strlen (home) + 1; + + size = 1024 + homelen; + + buf = malloc (size); + + if (!buf) + fatal_moan ("malloc failed"); + + strcpy (buf, home); + strcat (buf, "/"); + + while (1) + { + + + 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) /* glibc 2.1 */ + size = homelen + n + 1; + else /* glibc 2.0 */ + size *= 2; /* twice the old size */ + + buf = realloc (buf, size); + + 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); + } -int main(int argc,char *argv[]) + 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[]) +{ + int c; + extern char *optarg; + extern int optind, opterr, optopt; + + int oflags[128]; + char *oargs[128]; + + Socket *server_socket = NULL, *client_socket = NULL; + ANSI *ansi = NULL; + TTY *tty = NULL; + Log *log = NULL; + + int history = 200; + + + get_hostname (); + + 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) + { + case ':': + case 'h': + case '?': + usage (); + default: + if ((c > 64) && (c < 91)) + { + oflags[c]++; + oargs[c] = optarg; + } + else if ((c > 96) && (c < 123)) + { + oflags[c]++; + oargs[c] = optarg; + } + else + { + fprintf (stderr, "unknown option %c\n", c); + usage (); + } + } + + } + + /*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']++; + } + + + { + 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']) + { + 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"); + } + + /*Fold -r into -c */ + if (oflags['r']) + { + int id = safe_atoi (oargs['r']); + if (id < 0) + fatal_moan ("cannot parse -r %s as an integer", oargs['r']); + + oflags['k']++; + oargs['k'] = mome ("/.sympathy/%s%d", hostname, id); + oflags['r'] = 0; + oflags['c']++; + } + + if (oflags['p'] && oflags['d']) + fatal_moan ("-p incompatible with -d"); + + /*implement server and client by opening the server socket to prevent */ + /*a race condition, and then forking and munging the cmd line options */ + /*in the parent and child so that the child is the server and the */ + /*parent becomes its client */ + + if (oflags['c'] && oflags['s'] && oflags['F']) + fatal_moan ("-F is incompatible with -c -s"); + + if (oflags['s'] && !oflags['k']) + { + char *path; + path = mome ("/.sympathy"); + mkdir (path, 0700); + free (path); + + oargs['k'] = mome ("/.sympathy/%s%d", hostname, getpid ()); + oflags['k']++; + } + + if (oflags['s']) + { + server_socket = socket_listen (oargs['k']); + if (!server_socket) + fatal_moan ("failed to create socket %s for listening", oargs['k']); + } + + if (oflags['s'] && oflags['c']) + { + 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']) + fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L"); + + if (oflags['t'] || oflags['s']) + { + if (!oflags['p'] && !oflags['d']) + oflags['p']++; + } + + if (oflags['s'] && !oflags['F']) + { + daemon (1, 0); /*incase socket is relative path, unlink then will fail */ + } + + + if (oflags['s'] || oflags['t']) + { + + if (oflags['L']) + { + log = file_log_new (oargs['L']); + if (!log) + fatal_moan ("unable to access log file %s", oargs['L']); + } + + if (oflags['p']) + { + tty = ptty_open (NULL, NULL); + if (!tty) + fatal_moan ("unable to open a ptty"); + } + else + { + tty = + serial_open (oargs['d'], + oflags['K'] ? SERIAL_LOCK_ACTIVE : + SERIAL_LOCK_PASSIVE); + if (!tty) + fatal_moan ("unable to open serial port %s", oargs['d']); + } + + if (oflags['b']) + { + int baud = safe_atoi (oargs['b']); + + if (baud < 0) + fatal_moan ("Unable to parse baudrate %s", oargs['b']); + + tty_set_baud (tty, baud); + + } + + tty_set_flow (tty, oflags['f'] ? 1 : 0); + + } + + if (oflags['c']) + { + + client_socket = socket_connect (oargs['k']); + if (!client_socket) + fatal_moan ("failed to connect to socket %s", oargs['k']); + + + } + + + if (oflags['c'] || oflags['t']) + { + + if (oflags['H']) + { + fatal_moan ("fix a bug in HTML dispatcher before this works"); + } + else + { + ansi = (ANSI *) malloc (sizeof (ANSI)); + memset (ansi, 0, sizeof (ANSI)); + + terminal_register_handlers (); + ansi->terminal = terminal_open (0, 1); + ansi->reset(ansi, NULL); + } + } + + mainloop (tty, server_socket, client_socket, ansi, log, history); + + if (ansi) + { + ansi->close(ansi); + 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; }