From 77b31c3e51edfefc9b82b7ba31944c4161997ef4 Mon Sep 17 00:00:00 2001 From: james <> Date: Sun, 2 Mar 2008 10:27:24 +0000 Subject: *** empty log message *** --- apps/sympathy.c | 668 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 340 insertions(+), 328 deletions(-) (limited to 'apps/sympathy.c') diff --git a/apps/sympathy.c b/apps/sympathy.c index 41d0bbb..e1ed6e6 100644 --- a/apps/sympathy.c +++ b/apps/sympathy.c @@ -11,6 +11,9 @@ static char rcsid[] = /* * $Log$ + * Revision 1.28 2008/03/02 10:27:24 james + * *** empty log message *** + * * Revision 1.27 2008/03/02 09:55:37 james * *** empty log message *** * @@ -112,7 +115,7 @@ extern void usage (void); static char hostname[1024]; char *socket_dirs[] = - { ".sympathy", "sympathy", "/etc/sympathy", "/var/sympathy", NULL }; + { "~/.sympathy", "~/sympathy", "/etc/sympathy", "/var/sympathy", NULL }; int safe_atoi (char *a) @@ -146,60 +149,36 @@ fatal_moan (char *fmt, ...) } - -/*make the path in fmt from home (hence the name) */ - char * -mome (char *fmt, ...) +teedious_snprint (char *fmt, va_alist * ap) { - - 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); + va_alist *aq; + int size = 1024; + char *buf = malloc (size); if (!buf) fatal_moan ("malloc failed"); - strcpy (buf, home); - strcat (buf, "/"); + while (1) { - if (!fmt) - return buf; + va_copy (aq, ap); + n = vsnprintf (buf, size, fmt, aq); + va_end (aq); - while (1) - { + 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 */ - va_start (ap, fmt); - n = vsnprintf (buf + homelen, size - homelen, fmt, ap); - va_end (ap); + buf = realloc (buf, size); - 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"); + } - if (!buf) - fatal_moan ("malloc failed"); - } } char * @@ -207,6 +186,10 @@ gloo_paths (char *dir, char *leaf) { int i; char *ret, *ptr; + if (!dir) + dir = ""; + if (!leaf) + leaf = ""; ret = ptr = malloc (strlen (dir) + strlen (leaf) + 2); while (*dir) @@ -220,6 +203,84 @@ gloo_paths (char *dir, char *leaf) } +/*make the path in fmt from home (hence the name) */ +char * +mome (char *fmt, ...) +{ + + char *rt, *home, *leaf; + va_list ap; + + + home = getenv ("HOME"); + if (!home) + return 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) + free (leaf); + + return ret; +} + + +char * +find_socket (char *fmt, ...) +{ + Socket *ret; + char *path, *leaf, *h, **ptr; + va_list ap; + + + 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; + } + + if (!h) + continue; + + + path = gloo_paths (h, leaf); + if (**ptr == '~') + free (h); + + ret = socket_connect (path); + + free (path); + + if (ret) { + free (leaf); + return ret; + } + + } + + free (leaf); + return NULL; + +} + + int list_sockets_in_dir (char *sockdir) @@ -240,38 +301,30 @@ 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; - } - - 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); - } - } - + 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); + } } + free (sn); + } + closedir (dir); return 0; @@ -283,17 +336,19 @@ list_sockets (void) char **ptr, *h; - for (ptr = socket_dirs; *ptr; ptr++) - { - h = mome (*ptr); + for (ptr = socket_dirs; *ptr; ptr++) { + if (*ptr == '~') { + h = mome (ptr + 1); + } else { + h = strdup (*ptr); + } - if (h) - { - list_sockets_in_dir (h); - free (h); - } + if (!h) + continue; + list_sockets_in_dir (h); + free (h); - } + } return 0; } @@ -304,11 +359,10 @@ get_hostname (void) { struct utsname name; - if (uname (&name)) - { - strcpy (hostname, "unknown."); - return; - } + if (uname (&name)) { + strcpy (hostname, "unknown."); + return; + } strcpy (hostname, name.nodename); @@ -342,34 +396,27 @@ main (int argc, char *argv[]) memset (oflags, 0, sizeof (oflags)); memset (oargs, 0, sizeof (oargs)); - while ((c = getopt (argc, argv, "vw:utscr: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 (); - } - } - + while ((c = getopt (argc, argv, "vw:utscr: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; @@ -383,25 +430,23 @@ main (int argc, char *argv[]) sum += oflags['l']; sum += oflags['v']; - 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, -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 (); @@ -411,15 +456,14 @@ 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']) @@ -437,59 +481,55 @@ 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']) - { - if (!oflags['k']) - { - csnok++; - pipe (csnok_pipe); - } - - switch (pid = fork ()) - { - case 0: /*child becomes the server */ - oflags['c'] = 0; - oflags['H'] = 0; - - if (csnok) - close (csnok_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['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 */ - - if (csnok) - { - close (csnok_pipe[1]); - - if (read (csnok_pipe[0], &pid, sizeof (pid)) != sizeof (pid)) - fatal_moan ("Failed to receive pid of server process"); - - close (csnok_pipe[0]); - - oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid); - oflags['k']++; - } - } + if (oflags['s'] && oflags['c']) { + if (!oflags['k']) { + csnok++; + pipe (csnok_pipe); } + switch (pid = fork ()) { + case 0: /*child becomes the server */ + oflags['c'] = 0; + oflags['H'] = 0; + + if (csnok) + close (csnok_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['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 */ + + if (csnok) { + close (csnok_pipe[1]); + + if (read (csnok_pipe[0], &pid, sizeof (pid)) != sizeof (pid)) + fatal_moan ("Failed to receive pid of server process"); + + close (csnok_pipe[0]); + + oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid); + oflags['k']++; + } + } + } + if (oflags['c'] && !oflags['k'] && !oflags['r']) fatal_moan ("-c requires a socket to be specified with -s or -k or -r"); @@ -498,196 +538,168 @@ main (int argc, char *argv[]) || 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['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 ((size.y > VT102_MAX_ROWS) || (size.y < 1)) - fatal_moan ("-w requires a height between 1 and %d\n", - VT102_MAX_ROWS); + 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); } + size.x = safe_atoi (buf); - if (oflags['s'] && !oflags['F']) - { - /*nochdir incase socket is relative path, unlink then will fail */ - daemon (1, 0); + 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); - if (oflags['s']) - { - char *path; - path = mome ("/.sympathy"); - mkdir (path, 0700); - free (path); + } - if (!oflags['k']) - { - pid = getpid (); + if (oflags['s']) { + char *path; + path = mome ("/.sympathy"); + mkdir (path, 0700); + free (path); - oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid); - oflags['k']++; - } - server_socket = socket_listen (oargs['k']); + if (!oflags['k']) { + pid = getpid (); - /*Tell our parent's parent what our pid is */ - if (csnok) - { - pid = getpid (); + oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid); + oflags['k']++; + } - write (csnok_pipe[1], &pid, sizeof (pid)); - close (csnok_pipe[1]); - } + server_socket = socket_listen (oargs['k']); - if (!server_socket) - fatal_moan ("failed to create socket %s for listening", oargs['k']); + /*Tell our parent's parent what our pid is */ + if (csnok) { + pid = getpid (); + write (csnok_pipe[1], &pid, sizeof (pid)); + close (csnok_pipe[1]); } - if (oflags['s'] || oflags['t']) - { + if (!server_socket) + fatal_moan ("failed to create socket %s for listening", oargs['k']); - 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, &size); - if (!tty) - 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"); + if (oflags['s'] || oflags['t']) { - { - char kernel_cmdline[4096] = { 0 }; - char search_string[1024] = "console="; - char *ptr = oargs['d']; - int fd; + if (oflags['L']) { + log = file_log_new (oargs['L']); + if (!log) + fatal_moan ("unable to access log file %s", oargs['L']); + } - if (!strncmp ("/dev/", ptr, 5)) - ptr += 5; + if (oflags['p']) { + tty = ptty_open (NULL, NULL, &size); + if (!tty) + 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"); - strcat (search_string, ptr); + { + char kernel_cmdline[4096] = { 0 }; + char search_string[1024] = "console="; + char *ptr = oargs['d']; + int fd; - fd = open ("/proc/cmdline", O_RDONLY); - read (fd, kernel_cmdline, sizeof (kernel_cmdline)); - close (fd); + if (!strncmp ("/dev/", ptr, 5)) + ptr += 5; - kernel_cmdline[sizeof (kernel_cmdline) - 1] = 0; + strcat (search_string, ptr); - if (strstr (kernel_cmdline, search_string)) - fatal_moan ("/proc/cmdline contains %s", search_string); - } + fd = open ("/proc/cmdline", O_RDONLY); + read (fd, kernel_cmdline, sizeof (kernel_cmdline)); + close (fd); - 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']); - } + kernel_cmdline[sizeof (kernel_cmdline) - 1] = 0; - if (oflags['b']) - { - int baud = safe_atoi (oargs['b']); + if (strstr (kernel_cmdline, search_string)) + fatal_moan ("/proc/cmdline contains %s", search_string); + } - if (baud < 0) - fatal_moan ("Unable to parse baudrate %s", oargs['b']); + 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']); + } - tty_set_baud (tty, baud); + if (oflags['b']) { + int baud = safe_atoi (oargs['b']); - } + if (baud < 0) + fatal_moan ("Unable to parse baudrate %s", oargs['b']); - tty_set_flow (tty, oflags['f'] ? 1 : 0); + tty_set_baud (tty, baud); } + tty_set_flow (tty, oflags['f'] ? 1 : 0); + } - FISH - { + + if (oflags['r']) { char *id = oargs['r']; - if (id < 0) - fatal_moan ("cannot parse -r %s as an integer", oargs['r']); - oflags['k']++; - if (safe_atoi (id) > 0) - { - oargs['k'] = find_socket ("%s%d", hostname, safe_atoi (id)); - } - else - { - oargs['k'] = find_socket ("%s", id); - } - oflags['r'] = 0; - oflags['c']++; - } + if (!id) + fatal_moan ("-r requires an argument"); - if (oflags['c']) - { + if (safe_atoi (id) > 0) { + client_socket = find_socket ("%s%d", hostname, safe_atoi (id)); + } else { + client_socket = find_socket ("%s", id); + } - client_socket = socket_connect (oargs['k']); - if (!client_socket) - fatal_moan ("failed to connect to socket %s", oargs['k']); + if (!client_socket) + fatal_moan ("failed to find socket %s", oargs['r']); + } 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['c'] || oflags['t']) { - if (oflags['c'] || oflags['t']) - { - - 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['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); } + } mainloop (tty, server_socket, client_socket, ansi, log, history, &size); - if (ansi) - { - ansi->close (ansi); - terminal_atexit (); - } + if (ansi) { + ansi->close (ansi); + terminal_atexit (); + } if (tty) tty->close (tty); -- cgit v1.2.3