4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.29 2008/03/02 10:37:56 james
15 * *** empty log message ***
17 * Revision 1.28 2008/03/02 10:27:24 james
18 * *** empty log message ***
20 * Revision 1.27 2008/03/02 09:55:37 james
21 * *** empty log message ***
23 * Revision 1.26 2008/02/29 14:55:09 james
24 * *** empty log message ***
26 * Revision 1.25 2008/02/28 22:43:25 james
27 * *** empty log message ***
29 * Revision 1.24 2008/02/28 22:00:42 james
30 * *** empty log message ***
32 * Revision 1.23 2008/02/28 16:57:51 james
33 * *** empty log message ***
35 * Revision 1.22 2008/02/28 01:47:44 james
36 * *** empty log message ***
38 * Revision 1.21 2008/02/27 16:01:24 james
39 * *** empty log message ***
41 * Revision 1.20 2008/02/27 10:00:34 james
42 * *** empty log message ***
44 * Revision 1.19 2008/02/27 09:47:05 james
45 * *** empty log message ***
47 * Revision 1.18 2008/02/27 09:42:53 james
48 * *** empty log message ***
50 * Revision 1.17 2008/02/27 09:42:21 james
51 * *** empty log message ***
53 * Revision 1.16 2008/02/27 01:31:38 james
54 * *** empty log message ***
56 * Revision 1.15 2008/02/27 01:31:14 james
57 * *** empty log message ***
59 * Revision 1.14 2008/02/24 00:43:55 james
60 * *** empty log message ***
62 * Revision 1.13 2008/02/24 00:42:53 james
63 * *** empty log message ***
65 * Revision 1.12 2008/02/23 11:48:52 james
66 * *** empty log message ***
68 * Revision 1.11 2008/02/20 20:16:07 james
69 * *** empty log message ***
71 * Revision 1.10 2008/02/20 19:44:37 james
74 * Revision 1.9 2008/02/20 18:49:11 staffcvs
75 * *** empty log message ***
77 * Revision 1.8 2008/02/20 18:33:37 james
78 * *** empty log message ***
80 * Revision 1.7 2008/02/20 18:31:44 james
81 * *** empty log message ***
83 * Revision 1.6 2008/02/20 17:18:33 james
84 * *** empty log message ***
86 * Revision 1.5 2008/02/20 15:50:14 james
87 * *** empty log message ***
89 * Revision 1.4 2008/02/20 02:11:35 james
90 * *** empty log message ***
92 * Revision 1.3 2008/02/14 02:46:44 james
93 * *** empty log message ***
95 * Revision 1.2 2008/02/14 00:57:58 james
96 * *** empty log message ***
98 * Revision 1.1 2008/02/05 14:25:49 james
99 * *** empty log message ***
103 #include <sys/types.h>
105 #include <sympathy.h>
107 #include <sys/utsname.h>
108 #include <sys/stat.h>
115 #include "mainloop.h"
117 extern void usage (void);
118 static char hostname[1024];
120 char *socket_dirs[] =
121 { "~/.sympathy", "~/sympathy", "/etc/sympathy", "/var/sympathy", NULL };
132 ret = (int) strtol (a, &end, 0);
141 fatal_moan (char *fmt, ...)
147 vfprintf (stderr, fmt, ap);
156 teedious_snprintf (char *fmt, va_list ap)
160 char *buf = malloc (size);
164 fatal_moan ("malloc failed");
169 n = vsnprintf (buf, size, fmt, aq);
172 if (n > -1 && n < (size))
175 if (n > -1) /* glibc 2.1 */
178 size *= 2; /* twice the old size */
180 buf = realloc (buf, size);
183 fatal_moan ("malloc failed");
189 gloo_paths (char *dir, char *leaf)
197 ret = ptr = malloc (strlen (dir) + strlen (leaf) + 2);
203 *(ptr++) = *(leaf++);
210 /*make the path in fmt from home (hence the name) */
212 mome (char *fmt, ...)
214 char *ret, *home, *leaf;
218 home = getenv ("HOME");
224 leaf = teedious_snprintf (fmt, ap);
230 ret = gloo_paths (home, leaf);
239 find_socket (char *fmt, ...)
242 char *path, *leaf, *h, **ptr;
248 leaf = teedious_snprintf (fmt, ap);
255 for (ptr = socket_dirs; *ptr; ptr++) {
266 path = gloo_paths (h, leaf);
270 ret = socket_connect (path);
289 list_sockets_in_dir (char *sockdir)
295 DIR *dir = opendir (sockdir);
299 int hostname_len = strlen (hostname);
306 while ((ent = readdir (dir))) {
307 sn = gloo_paths (sockdir, ent->d_name);
308 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode))) {
313 s = socket_connect (sn);
316 printf ("\t%s (Active)\n", sn);
319 if (strncmp (ent->d_name, hostname, hostname_len)) {
320 printf ("\t%s (Unknown - not this host)\n", sn);
322 printf ("\t%s (Dead, wiped)\n", sn);
341 for (ptr = socket_dirs; *ptr; ptr++) {
351 list_sockets_in_dir (h);
368 strcpy (hostname, "unknown.");
373 strcpy (hostname, name.nodename);
374 strcat (hostname, ".");
379 main (int argc, char *argv[])
383 extern int optind, opterr, optopt;
384 CRT_Pos size = { VT102_COLS_80, VT102_ROWS_24 };
386 int csnok_pipe[2] = { 0 };
392 Socket *server_socket = NULL, *client_socket = NULL;
402 memset (oflags, 0, sizeof (oflags));
403 memset (oargs, 0, sizeof (oargs));
404 while ((c = getopt (argc, argv, "vw:utscr:lKHd:pb:fL:Fk:n:")) != EOF) {
411 if ((c > 64) && (c < 91)) {
414 } else if ((c > 96) && (c < 123)) {
418 fprintf (stderr, "unknown option %c\n", c);
425 /*Compatability for screen's ls */
433 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
439 /*If no mode is specified behave like screen */
447 ("specifiy exactly one of ( -c and or -s ), -t, -r, -l and -v");
451 fprintf (stderr, "Version: %s\n", libsympathy_version ());
452 fprintf (stderr, "Version: %s\n", rcsid);
457 return list_sockets ();
460 if (oflags['r'] && oflags['k'])
461 fatal_moan ("-k is incompatible with -r");
465 history = safe_atoi (oargs['n']);
467 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
470 fatal_moan ("agrument to -n must be greater than zero");
473 /*Fold -r implies -c */
477 if (oflags['p'] && oflags['d'])
478 fatal_moan ("-p incompatible with -d");
481 if (oflags['c'] && oflags['s'] && oflags['F'])
482 fatal_moan ("-F is incompatible with -c -s");
484 /*implement server and client: this process forks. The parent */
485 /*becomes the client and the child forks again to become the */
486 /*server. If there's no -k argument the client (parent) needs */
487 /*to find out the pid of the server, we use a pipe */
489 if (oflags['s'] && oflags['c']) {
495 switch (pid = fork ()) {
496 case 0: /*child becomes the server */
501 close (csnok_pipe[0]);
504 fatal_moan ("fork failed");
505 default: /*parent becomes client */
516 /*Collect the child */
517 waitpid (pid, NULL, 0);
519 /*if there was no k argument we need to find the */
520 /*pid of the server process so that we can work out */
521 /*what the socket is called. The server tells us on */
525 close (csnok_pipe[1]);
527 if (read (csnok_pipe[0], &pid, sizeof (pid)) != sizeof (pid))
528 fatal_moan ("Failed to receive pid of server process");
530 close (csnok_pipe[0]);
532 oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
539 if (oflags['c'] && !oflags['k'] && !oflags['r'])
540 fatal_moan ("-c requires a socket to be specified with -s or -k or -r");
542 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
543 || oflags['L']) && oflags['c'])
544 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
546 if (oflags['t'] || oflags['s']) {
547 if (!oflags['p'] && !oflags['d'])
553 strcpy (buf, oargs['w']);
554 ptr = index (buf, 'x');
558 size.y = safe_atoi (ptr);
560 size.x = safe_atoi (buf);
562 if ((size.x > VT102_MAX_COLS) || (size.x < 1))
563 fatal_moan ("-w requires a width between 1 and %d\n", VT102_MAX_COLS);
565 if ((size.y > VT102_MAX_ROWS) || (size.y < 1))
566 fatal_moan ("-w requires a height between 1 and %d\n", VT102_MAX_ROWS);
570 if (oflags['s'] && !oflags['F']) {
571 /*nochdir incase socket is relative path, unlink then will fail */
579 path = mome ("/.sympathy");
587 oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
591 server_socket = socket_listen (oargs['k']);
593 /*Tell our parent's parent what our pid is */
597 write (csnok_pipe[1], &pid, sizeof (pid));
598 close (csnok_pipe[1]);
602 fatal_moan ("failed to create socket %s for listening", oargs['k']);
606 if (oflags['s'] || oflags['t']) {
609 log = file_log_new (oargs['L']);
611 fatal_moan ("unable to access log file %s", oargs['L']);
615 tty = ptty_open (NULL, NULL, &size);
617 fatal_moan ("unable to open a ptty");
619 /*HACK-- check that console=device does not occur in */
622 fatal_moan ("no argument to -d");
625 char kernel_cmdline[4096] = { 0 };
626 char search_string[1024] = "console=";
627 char *ptr = oargs['d'];
630 if (!strncmp ("/dev/", ptr, 5))
633 strcat (search_string, ptr);
635 fd = open ("/proc/cmdline", O_RDONLY);
636 read (fd, kernel_cmdline, sizeof (kernel_cmdline));
639 kernel_cmdline[sizeof (kernel_cmdline) - 1] = 0;
641 if (strstr (kernel_cmdline, search_string))
642 fatal_moan ("/proc/cmdline contains %s", search_string);
646 serial_open (oargs['d'],
647 oflags['K'] ? SERIAL_LOCK_ACTIVE : SERIAL_LOCK_PASSIVE);
649 fatal_moan ("unable to open serial port %s", oargs['d']);
653 int baud = safe_atoi (oargs['b']);
656 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
658 tty_set_baud (tty, baud);
662 tty_set_flow (tty, oflags['f'] ? 1 : 0);
667 char *id = oargs['r'];
670 fatal_moan ("-r requires an argument");
672 if (safe_atoi (id) > 0) {
673 client_socket = find_socket ("%s%d", hostname, safe_atoi (id));
675 client_socket = find_socket ("%s", id);
679 fatal_moan ("failed to find socket %s", oargs['r']);
681 } else if (oflags['c']) {
682 client_socket = socket_connect (oargs['k']);
685 fatal_moan ("failed to connect to socket %s", oargs['k']);
690 if (oflags['c'] || oflags['t']) {
693 ansi = ansi_new_html (stdout);
695 terminal_register_handlers ();
697 ansi_new_from_terminal (terminal_open (0, 1), oflags['u'] ? 0 : 1);
698 ansi->reset (ansi, NULL);
702 mainloop (tty, server_socket, client_socket, ansi, log, history, &size);
715 socket_free (server_socket);
717 socket_free (client_socket);
720 printf ("you have now exited sympathy\n");