4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.18 2008/02/27 09:42:53 james
15 * *** empty log message ***
17 * Revision 1.17 2008/02/27 09:42:21 james
18 * *** empty log message ***
20 * Revision 1.16 2008/02/27 01:31:38 james
21 * *** empty log message ***
23 * Revision 1.15 2008/02/27 01:31:14 james
24 * *** empty log message ***
26 * Revision 1.14 2008/02/24 00:43:55 james
27 * *** empty log message ***
29 * Revision 1.13 2008/02/24 00:42:53 james
30 * *** empty log message ***
32 * Revision 1.12 2008/02/23 11:48:52 james
33 * *** empty log message ***
35 * Revision 1.11 2008/02/20 20:16:07 james
36 * *** empty log message ***
38 * Revision 1.10 2008/02/20 19:44:37 james
41 * Revision 1.9 2008/02/20 18:49:11 staffcvs
42 * *** empty log message ***
44 * Revision 1.8 2008/02/20 18:33:37 james
45 * *** empty log message ***
47 * Revision 1.7 2008/02/20 18:31:44 james
48 * *** empty log message ***
50 * Revision 1.6 2008/02/20 17:18:33 james
51 * *** empty log message ***
53 * Revision 1.5 2008/02/20 15:50:14 james
54 * *** empty log message ***
56 * Revision 1.4 2008/02/20 02:11:35 james
57 * *** empty log message ***
59 * Revision 1.3 2008/02/14 02:46:44 james
60 * *** empty log message ***
62 * Revision 1.2 2008/02/14 00:57:58 james
63 * *** empty log message ***
65 * Revision 1.1 2008/02/05 14:25:49 james
66 * *** empty log message ***
70 #include <sys/types.h>
74 #include <sys/utsname.h>
83 extern void usage (void);
85 static char hostname[1024];
96 ret = (int) strtol (a, &end, 0);
105 fatal_moan (char *fmt, ...)
111 vfprintf (stderr, fmt, ap);
120 /*make the path in fmt from home (hence the name) */
123 mome (char *fmt, ...)
133 home = getenv ("HOME");
137 homelen = strlen (home) + 1;
139 size = 1024 + homelen;
144 fatal_moan ("malloc failed");
154 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
157 if (n > -1 && n < (size - homelen))
160 if (n > -1) /* glibc 2.1 */
161 size = homelen + n + 1;
163 size *= 2; /* twice the old size */
165 buf = realloc (buf, size);
168 fatal_moan ("malloc failed");
180 char *sockdir = mome ("/.sympathy/");
181 DIR *dir = opendir (sockdir);
185 int hostname_len = strlen (hostname);
188 fatal_moan ("can't examine %s for sockets", sockdir);
193 while ((ent = readdir (dir)))
195 sn = mome (".sympathy/%s", ent->d_name);
196 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
202 s = socket_connect (sn);
206 printf ("\t%s (Active)\n", ent->d_name);
211 if (strncmp (ent->d_name, hostname, hostname_len))
213 printf ("\t%s (Unknown - not this host)\n", ent->d_name);
217 printf ("\t%s (Dead, wiped)\n", ent->d_name);
238 strcpy (hostname, "unknown.");
243 strcpy (hostname, name.nodename);
244 strcat (hostname, ".");
249 main (int argc, char *argv[])
253 extern int optind, opterr, optopt;
254 CRT_Pos size = { VT102_COLS_80, VT102_ROWS };
259 Socket *server_socket = NULL, *client_socket = NULL;
269 memset (oflags, 0, sizeof (oflags));
270 memset (oargs, 0, sizeof (oargs));
271 while ((c = getopt (argc, argv, "vw:utscr:lKHd:pb:fL:Fk:n:")) != EOF)
280 if ((c > 64) && (c < 91))
285 else if ((c > 96) && (c < 123))
292 fprintf (stderr, "unknown option %c\n", c);
299 /*Compatability for screen's ls */
304 if (!oflags['s'] && !oflags['c'] && !oflags['t'] && !oflags['r']
307 /*If no mode is specified behave like screen */
316 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
323 ("specifiy exactly one of ( -c and or -s ), -t, -r, -l and -v");
328 fprintf ("Version: %s\n", libsympathy_version ());
333 return list_sockets ();
336 if (oflags['r'] && oflags['k'])
337 fatal_moan ("-k is incompatible with -r");
342 history = safe_atoi (oargs['n']);
344 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
347 fatal_moan ("agrument to -n must be greater than zero");
353 int id = safe_atoi (oargs['r']);
355 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
358 oargs['k'] = mome ("/.sympathy/%s%d", hostname, id);
363 if (oflags['p'] && oflags['d'])
364 fatal_moan ("-p incompatible with -d");
366 /*implement server and client by opening the server socket to prevent */
367 /*a race condition, and then forking and munging the cmd line options */
368 /*in the parent and child so that the child is the server and the */
369 /*parent becomes its client */
371 if (oflags['c'] && oflags['s'] && oflags['F'])
372 fatal_moan ("-F is incompatible with -c -s");
374 if (oflags['s'] && !oflags['k'])
377 path = mome ("/.sympathy");
381 oargs['k'] = mome ("/.sympathy/%s%d", hostname, getpid ());
387 server_socket = socket_listen (oargs['k']);
389 fatal_moan ("failed to create socket %s for listening", oargs['k']);
392 if (oflags['s'] && oflags['c'])
396 case 0: /*child becomes the server */
401 fatal_moan ("fork failed");
414 socket_free_parent (server_socket);
415 server_socket = NULL;
421 if (oflags['c'] && !oflags['k'])
422 fatal_moan ("-c requires a socket to be specified with -s or -k");
424 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
425 || oflags['L']) && oflags['c'])
426 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
428 if (oflags['t'] || oflags['s'])
430 if (!oflags['p'] && !oflags['d'])
437 strcpy (buf, oargs['w']);
438 ptr = index (buf, 'x');
443 size.y = safe_atoi (ptr);
445 size.x = safe_atoi (buf);
447 if ((size.x > VT102_MAX_COLS) || (size.x < 1))
448 fatal_moan ("-w requires a width between 1 and %d\n", VT102_MAX_COLS);
450 if ((size.y > VT102_ROWS) || (size.y < 1))
451 fatal_moan ("-w requires a height between 1 and %d\n",
456 if (oflags['s'] && !oflags['F'])
458 daemon (1, 0); /*incase socket is relative path, unlink then will fail */
462 if (oflags['s'] || oflags['t'])
467 log = file_log_new (oargs['L']);
469 fatal_moan ("unable to access log file %s", oargs['L']);
474 tty = ptty_open (NULL, NULL, &size);
476 fatal_moan ("unable to open a ptty");
481 serial_open (oargs['d'],
482 oflags['K'] ? SERIAL_LOCK_ACTIVE :
483 SERIAL_LOCK_PASSIVE);
485 fatal_moan ("unable to open serial port %s", oargs['d']);
490 int baud = safe_atoi (oargs['b']);
493 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
495 tty_set_baud (tty, baud);
499 tty_set_flow (tty, oflags['f'] ? 1 : 0);
506 client_socket = socket_connect (oargs['k']);
508 fatal_moan ("failed to connect to socket %s", oargs['k']);
514 if (oflags['c'] || oflags['t'])
519 ansi = ansi_new_html (stdout);
523 terminal_register_handlers ();
525 ansi_new_from_terminal (terminal_open (0, 1),
526 oflags['u'] ? 0 : 1);
527 ansi->reset (ansi, NULL);
531 mainloop (tty, server_socket, client_socket, ansi, log, history, &size);
545 socket_free (server_socket);
547 socket_free (client_socket);
550 printf ("you have now exited sympathy\n");