4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.21 2008/02/27 16:01:24 james
15 * *** empty log message ***
17 * Revision 1.20 2008/02/27 10:00:34 james
18 * *** empty log message ***
20 * Revision 1.19 2008/02/27 09:47:05 james
21 * *** empty log message ***
23 * Revision 1.18 2008/02/27 09:42:53 james
24 * *** empty log message ***
26 * Revision 1.17 2008/02/27 09:42:21 james
27 * *** empty log message ***
29 * Revision 1.16 2008/02/27 01:31:38 james
30 * *** empty log message ***
32 * Revision 1.15 2008/02/27 01:31:14 james
33 * *** empty log message ***
35 * Revision 1.14 2008/02/24 00:43:55 james
36 * *** empty log message ***
38 * Revision 1.13 2008/02/24 00:42:53 james
39 * *** empty log message ***
41 * Revision 1.12 2008/02/23 11:48:52 james
42 * *** empty log message ***
44 * Revision 1.11 2008/02/20 20:16:07 james
45 * *** empty log message ***
47 * Revision 1.10 2008/02/20 19:44:37 james
50 * Revision 1.9 2008/02/20 18:49:11 staffcvs
51 * *** empty log message ***
53 * Revision 1.8 2008/02/20 18:33:37 james
54 * *** empty log message ***
56 * Revision 1.7 2008/02/20 18:31:44 james
57 * *** empty log message ***
59 * Revision 1.6 2008/02/20 17:18:33 james
60 * *** empty log message ***
62 * Revision 1.5 2008/02/20 15:50:14 james
63 * *** empty log message ***
65 * Revision 1.4 2008/02/20 02:11:35 james
66 * *** empty log message ***
68 * Revision 1.3 2008/02/14 02:46:44 james
69 * *** empty log message ***
71 * Revision 1.2 2008/02/14 00:57:58 james
72 * *** empty log message ***
74 * Revision 1.1 2008/02/05 14:25:49 james
75 * *** empty log message ***
79 #include <sys/types.h>
83 #include <sys/utsname.h>
92 extern void usage (void);
94 static char hostname[1024];
105 ret = (int) strtol (a, &end, 0);
114 fatal_moan (char *fmt, ...)
120 vfprintf (stderr, fmt, ap);
129 /*make the path in fmt from home (hence the name) */
132 mome (char *fmt, ...)
142 home = getenv ("HOME");
146 homelen = strlen (home) + 1;
148 size = 1024 + homelen;
153 fatal_moan ("malloc failed");
163 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
166 if (n > -1 && n < (size - homelen))
169 if (n > -1) /* glibc 2.1 */
170 size = homelen + n + 1;
172 size *= 2; /* twice the old size */
174 buf = realloc (buf, size);
177 fatal_moan ("malloc failed");
189 char *sockdir = mome ("/.sympathy/");
190 DIR *dir = opendir (sockdir);
194 int hostname_len = strlen (hostname);
197 fatal_moan ("can't examine %s for sockets", sockdir);
202 while ((ent = readdir (dir)))
204 sn = mome (".sympathy/%s", ent->d_name);
205 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
211 s = socket_connect (sn);
215 printf ("\t%s (Active)\n", ent->d_name);
220 if (strncmp (ent->d_name, hostname, hostname_len))
222 printf ("\t%s (Unknown - not this host)\n", ent->d_name);
226 printf ("\t%s (Dead, wiped)\n", ent->d_name);
247 strcpy (hostname, "unknown.");
252 strcpy (hostname, name.nodename);
253 strcat (hostname, ".");
258 main (int argc, char *argv[])
262 extern int optind, opterr, optopt;
263 CRT_Pos size = { VT102_COLS_80, VT102_ROWS };
268 Socket *server_socket = NULL, *client_socket = NULL;
278 memset (oflags, 0, sizeof (oflags));
279 memset (oargs, 0, sizeof (oargs));
280 while ((c = getopt (argc, argv, "vw:utscr:lKHd:pb:fL:Fk:n:")) != EOF)
289 if ((c > 64) && (c < 91))
294 else if ((c > 96) && (c < 123))
301 fprintf (stderr, "unknown option %c\n", c);
308 /*Compatability for screen's ls */
316 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
322 /*If no mode is specified behave like screen */
330 ("specifiy exactly one of ( -c and or -s ), -t, -r, -l and -v");
335 fprintf (stderr, "Version: %s\n", libsympathy_version ());
336 fprintf (stderr, "Version: %s\n", rcsid);
341 return list_sockets ();
344 if (oflags['r'] && oflags['k'])
345 fatal_moan ("-k is incompatible with -r");
350 history = safe_atoi (oargs['n']);
352 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
355 fatal_moan ("agrument to -n must be greater than zero");
361 char *id = oargs['r'];
363 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
366 if (safe_atoi(id)>0) {
367 oargs['k'] = mome ("/.sympathy/%s%d", hostname, safe_atoi(id));
369 oargs['k'] = mome ("/.sympathy/%s", id);
375 if (oflags['p'] && oflags['d'])
376 fatal_moan ("-p incompatible with -d");
378 /*implement server and client by opening the server socket to prevent */
379 /*a race condition, and then forking and munging the cmd line options */
380 /*in the parent and child so that the child is the server and the */
381 /*parent becomes its client */
383 if (oflags['c'] && oflags['s'] && oflags['F'])
384 fatal_moan ("-F is incompatible with -c -s");
386 if (oflags['s'] && !oflags['k'])
389 path = mome ("/.sympathy");
393 oargs['k'] = mome ("/.sympathy/%s%d", hostname, getpid ());
399 server_socket = socket_listen (oargs['k']);
401 fatal_moan ("failed to create socket %s for listening", oargs['k']);
404 if (oflags['s'] && oflags['c'])
408 case 0: /*child becomes the server */
413 fatal_moan ("fork failed");
426 socket_free_parent (server_socket);
427 server_socket = NULL;
433 if (oflags['c'] && !oflags['k'])
434 fatal_moan ("-c requires a socket to be specified with -s or -k");
436 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
437 || oflags['L']) && oflags['c'])
438 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
440 if (oflags['t'] || oflags['s'])
442 if (!oflags['p'] && !oflags['d'])
449 strcpy (buf, oargs['w']);
450 ptr = index (buf, 'x');
455 size.y = safe_atoi (ptr);
457 size.x = safe_atoi (buf);
459 if ((size.x > VT102_MAX_COLS) || (size.x < 1))
460 fatal_moan ("-w requires a width between 1 and %d\n", VT102_MAX_COLS);
462 if ((size.y > VT102_ROWS) || (size.y < 1))
463 fatal_moan ("-w requires a height between 1 and %d\n",
468 if (oflags['s'] && !oflags['F'])
470 daemon (1, 0); /*incase socket is relative path, unlink then will fail */
474 if (oflags['s'] || oflags['t'])
479 log = file_log_new (oargs['L']);
481 fatal_moan ("unable to access log file %s", oargs['L']);
486 tty = ptty_open (NULL, NULL, &size);
488 fatal_moan ("unable to open a ptty");
493 serial_open (oargs['d'],
494 oflags['K'] ? SERIAL_LOCK_ACTIVE :
495 SERIAL_LOCK_PASSIVE);
497 fatal_moan ("unable to open serial port %s", oargs['d']);
502 int baud = safe_atoi (oargs['b']);
505 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
507 tty_set_baud (tty, baud);
511 tty_set_flow (tty, oflags['f'] ? 1 : 0);
518 client_socket = socket_connect (oargs['k']);
520 fatal_moan ("failed to connect to socket %s", oargs['k']);
526 if (oflags['c'] || oflags['t'])
531 ansi = ansi_new_html (stdout);
535 terminal_register_handlers ();
537 ansi_new_from_terminal (terminal_open (0, 1),
538 oflags['u'] ? 0 : 1);
539 ansi->reset (ansi, NULL);
543 mainloop (tty, server_socket, client_socket, ansi, log, history, &size);
557 socket_free (server_socket);
559 socket_free (client_socket);
562 printf ("you have now exited sympathy\n");