4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.19 2008/02/27 09:47:05 james
15 * *** empty log message ***
17 * Revision 1.18 2008/02/27 09:42:53 james
18 * *** empty log message ***
20 * Revision 1.17 2008/02/27 09:42:21 james
21 * *** empty log message ***
23 * Revision 1.16 2008/02/27 01:31:38 james
24 * *** empty log message ***
26 * Revision 1.15 2008/02/27 01:31:14 james
27 * *** empty log message ***
29 * Revision 1.14 2008/02/24 00:43:55 james
30 * *** empty log message ***
32 * Revision 1.13 2008/02/24 00:42:53 james
33 * *** empty log message ***
35 * Revision 1.12 2008/02/23 11:48:52 james
36 * *** empty log message ***
38 * Revision 1.11 2008/02/20 20:16:07 james
39 * *** empty log message ***
41 * Revision 1.10 2008/02/20 19:44:37 james
44 * Revision 1.9 2008/02/20 18:49:11 staffcvs
45 * *** empty log message ***
47 * Revision 1.8 2008/02/20 18:33:37 james
48 * *** empty log message ***
50 * Revision 1.7 2008/02/20 18:31:44 james
51 * *** empty log message ***
53 * Revision 1.6 2008/02/20 17:18:33 james
54 * *** empty log message ***
56 * Revision 1.5 2008/02/20 15:50:14 james
57 * *** empty log message ***
59 * Revision 1.4 2008/02/20 02:11:35 james
60 * *** empty log message ***
62 * Revision 1.3 2008/02/14 02:46:44 james
63 * *** empty log message ***
65 * Revision 1.2 2008/02/14 00:57:58 james
66 * *** empty log message ***
68 * Revision 1.1 2008/02/05 14:25:49 james
69 * *** empty log message ***
73 #include <sys/types.h>
77 #include <sys/utsname.h>
86 extern void usage (void);
88 static char hostname[1024];
99 ret = (int) strtol (a, &end, 0);
108 fatal_moan (char *fmt, ...)
114 vfprintf (stderr, fmt, ap);
123 /*make the path in fmt from home (hence the name) */
126 mome (char *fmt, ...)
136 home = getenv ("HOME");
140 homelen = strlen (home) + 1;
142 size = 1024 + homelen;
147 fatal_moan ("malloc failed");
157 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
160 if (n > -1 && n < (size - homelen))
163 if (n > -1) /* glibc 2.1 */
164 size = homelen + n + 1;
166 size *= 2; /* twice the old size */
168 buf = realloc (buf, size);
171 fatal_moan ("malloc failed");
183 char *sockdir = mome ("/.sympathy/");
184 DIR *dir = opendir (sockdir);
188 int hostname_len = strlen (hostname);
191 fatal_moan ("can't examine %s for sockets", sockdir);
196 while ((ent = readdir (dir)))
198 sn = mome (".sympathy/%s", ent->d_name);
199 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
205 s = socket_connect (sn);
209 printf ("\t%s (Active)\n", ent->d_name);
214 if (strncmp (ent->d_name, hostname, hostname_len))
216 printf ("\t%s (Unknown - not this host)\n", ent->d_name);
220 printf ("\t%s (Dead, wiped)\n", ent->d_name);
241 strcpy (hostname, "unknown.");
246 strcpy (hostname, name.nodename);
247 strcat (hostname, ".");
252 main (int argc, char *argv[])
256 extern int optind, opterr, optopt;
257 CRT_Pos size = { VT102_COLS_80, VT102_ROWS };
262 Socket *server_socket = NULL, *client_socket = NULL;
272 memset (oflags, 0, sizeof (oflags));
273 memset (oargs, 0, sizeof (oargs));
274 while ((c = getopt (argc, argv, "vw:utscr:lKHd:pb:fL:Fk:n:")) != EOF)
283 if ((c > 64) && (c < 91))
288 else if ((c > 96) && (c < 123))
295 fprintf (stderr, "unknown option %c\n", c);
302 /*Compatability for screen's ls */
310 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
316 /*If no mode is specified behave like screen */
323 ("specifiy exactly one of ( -c and or -s ), -t, -r, -l and -v");
328 fprintf (stderr, "Version: %s\n", libsympathy_version ());
329 fprintf (stderr, "Version: %s\n", rcsid);
334 return list_sockets ();
337 if (oflags['r'] && oflags['k'])
338 fatal_moan ("-k is incompatible with -r");
343 history = safe_atoi (oargs['n']);
345 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
348 fatal_moan ("agrument to -n must be greater than zero");
354 int id = safe_atoi (oargs['r']);
356 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
359 oargs['k'] = mome ("/.sympathy/%s%d", hostname, id);
364 if (oflags['p'] && oflags['d'])
365 fatal_moan ("-p incompatible with -d");
367 /*implement server and client by opening the server socket to prevent */
368 /*a race condition, and then forking and munging the cmd line options */
369 /*in the parent and child so that the child is the server and the */
370 /*parent becomes its client */
372 if (oflags['c'] && oflags['s'] && oflags['F'])
373 fatal_moan ("-F is incompatible with -c -s");
375 if (oflags['s'] && !oflags['k'])
378 path = mome ("/.sympathy");
382 oargs['k'] = mome ("/.sympathy/%s%d", hostname, getpid ());
388 server_socket = socket_listen (oargs['k']);
390 fatal_moan ("failed to create socket %s for listening", oargs['k']);
393 if (oflags['s'] && oflags['c'])
397 case 0: /*child becomes the server */
402 fatal_moan ("fork failed");
415 socket_free_parent (server_socket);
416 server_socket = NULL;
422 if (oflags['c'] && !oflags['k'])
423 fatal_moan ("-c requires a socket to be specified with -s or -k");
425 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
426 || oflags['L']) && oflags['c'])
427 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
429 if (oflags['t'] || oflags['s'])
431 if (!oflags['p'] && !oflags['d'])
438 strcpy (buf, oargs['w']);
439 ptr = index (buf, 'x');
444 size.y = safe_atoi (ptr);
446 size.x = safe_atoi (buf);
448 if ((size.x > VT102_MAX_COLS) || (size.x < 1))
449 fatal_moan ("-w requires a width between 1 and %d\n", VT102_MAX_COLS);
451 if ((size.y > VT102_ROWS) || (size.y < 1))
452 fatal_moan ("-w requires a height between 1 and %d\n",
457 if (oflags['s'] && !oflags['F'])
459 daemon (1, 0); /*incase socket is relative path, unlink then will fail */
463 if (oflags['s'] || oflags['t'])
468 log = file_log_new (oargs['L']);
470 fatal_moan ("unable to access log file %s", oargs['L']);
475 tty = ptty_open (NULL, NULL, &size);
477 fatal_moan ("unable to open a ptty");
482 serial_open (oargs['d'],
483 oflags['K'] ? SERIAL_LOCK_ACTIVE :
484 SERIAL_LOCK_PASSIVE);
486 fatal_moan ("unable to open serial port %s", oargs['d']);
491 int baud = safe_atoi (oargs['b']);
494 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
496 tty_set_baud (tty, baud);
500 tty_set_flow (tty, oflags['f'] ? 1 : 0);
507 client_socket = socket_connect (oargs['k']);
509 fatal_moan ("failed to connect to socket %s", oargs['k']);
515 if (oflags['c'] || oflags['t'])
520 ansi = ansi_new_html (stdout);
524 terminal_register_handlers ();
526 ansi_new_from_terminal (terminal_open (0, 1),
527 oflags['u'] ? 0 : 1);
528 ansi->reset (ansi, NULL);
532 mainloop (tty, server_socket, client_socket, ansi, log, history, &size);
546 socket_free (server_socket);
548 socket_free (client_socket);
551 printf ("you have now exited sympathy\n");