4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
14 * Revision 1.15 2008/02/27 01:31:14 james
15 * *** empty log message ***
17 * Revision 1.14 2008/02/24 00:43:55 james
18 * *** empty log message ***
20 * Revision 1.13 2008/02/24 00:42:53 james
21 * *** empty log message ***
23 * Revision 1.12 2008/02/23 11:48:52 james
24 * *** empty log message ***
26 * Revision 1.11 2008/02/20 20:16:07 james
27 * *** empty log message ***
29 * Revision 1.10 2008/02/20 19:44:37 james
32 * Revision 1.9 2008/02/20 18:49:11 staffcvs
33 * *** empty log message ***
35 * Revision 1.8 2008/02/20 18:33:37 james
36 * *** empty log message ***
38 * Revision 1.7 2008/02/20 18:31:44 james
39 * *** empty log message ***
41 * Revision 1.6 2008/02/20 17:18:33 james
42 * *** empty log message ***
44 * Revision 1.5 2008/02/20 15:50:14 james
45 * *** empty log message ***
47 * Revision 1.4 2008/02/20 02:11:35 james
48 * *** empty log message ***
50 * Revision 1.3 2008/02/14 02:46:44 james
51 * *** empty log message ***
53 * Revision 1.2 2008/02/14 00:57:58 james
54 * *** empty log message ***
56 * Revision 1.1 2008/02/05 14:25:49 james
57 * *** empty log message ***
61 #include <sys/types.h>
65 #include <sys/utsname.h>
74 extern void usage (void);
76 static char hostname[1024];
87 ret = (int) strtol (a, &end, 0);
96 fatal_moan (char *fmt, ...)
102 vfprintf (stderr, fmt, ap);
111 /*make the path in fmt from home (hence the name) */
114 mome (char *fmt, ...)
124 home = getenv ("HOME");
128 homelen = strlen (home) + 1;
130 size = 1024 + homelen;
135 fatal_moan ("malloc failed");
145 n = vsnprintf (buf + homelen, size - homelen, fmt, ap);
148 if (n > -1 && n < (size - homelen))
151 if (n > -1) /* glibc 2.1 */
152 size = homelen + n + 1;
154 size *= 2; /* twice the old size */
156 buf = realloc (buf, size);
159 fatal_moan ("malloc failed");
171 char *sockdir = mome ("/.sympathy/");
172 DIR *dir = opendir (sockdir);
176 int hostname_len = strlen (hostname);
179 fatal_moan ("can't examine %s for sockets", sockdir);
184 while ((ent = readdir (dir)))
186 sn = mome (".sympathy/%s", ent->d_name);
187 if (stat (sn, &buf) || (!S_ISSOCK (buf.st_mode)))
193 s = socket_connect (sn);
197 printf ("\t%s (Active)\n", ent->d_name);
202 if (strncmp (ent->d_name, hostname, hostname_len))
204 printf ("\t%s (Unknown - not this host)\n", ent->d_name);
208 printf ("\t%s (Dead, wiped)\n", ent->d_name);
229 strcpy (hostname, "unknown.");
234 strcpy (hostname, name.nodename);
235 strcat (hostname, ".");
240 main (int argc, char *argv[])
244 extern int optind, opterr, optopt;
245 int width=VT102_COLS_80;
250 Socket *server_socket = NULL, *client_socket = NULL;
260 memset (oflags, 0, sizeof (oflags));
261 memset (oargs, 0, sizeof (oargs));
262 while ((c = getopt (argc, argv, "w:utscr:lKHd:pb:fL:Fk:n:")) != EOF)
271 if ((c > 64) && (c < 91))
276 else if ((c > 96) && (c < 123))
283 fprintf (stderr, "unknown option %c\n", c);
290 /*Compatability for screen's ls */
295 if (!oflags['s'] && !oflags['c'] && !oflags['t'] && !oflags['r']
298 /*If no mode is specified behave like screen */
307 sum += (oflags['s'] || oflags['c']) ? 1 : 0;
312 fatal_moan ("specifiy exactly one of ( -c and or -s ), -t, -r and -l");
317 return list_sockets ();
320 if (oflags['r'] && oflags['k'])
321 fatal_moan ("-k is incompatible with -r");
326 history = safe_atoi (oargs['n']);
328 fatal_moan ("cannot parse -n %s as an integer", oargs['n']);
331 fatal_moan ("agrument to -n must be greater than zero");
337 int id = safe_atoi (oargs['r']);
339 fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
342 oargs['k'] = mome ("/.sympathy/%s%d", hostname, id);
347 if (oflags['p'] && oflags['d'])
348 fatal_moan ("-p incompatible with -d");
350 /*implement server and client by opening the server socket to prevent */
351 /*a race condition, and then forking and munging the cmd line options */
352 /*in the parent and child so that the child is the server and the */
353 /*parent becomes its client */
355 if (oflags['c'] && oflags['s'] && oflags['F'])
356 fatal_moan ("-F is incompatible with -c -s");
358 if (oflags['s'] && !oflags['k'])
361 path = mome ("/.sympathy");
365 oargs['k'] = mome ("/.sympathy/%s%d", hostname, getpid ());
371 server_socket = socket_listen (oargs['k']);
373 fatal_moan ("failed to create socket %s for listening", oargs['k']);
376 if (oflags['s'] && oflags['c'])
380 case 0: /*child becomes the server */
385 fatal_moan ("fork failed");
398 socket_free_parent (server_socket);
399 server_socket = NULL;
405 if (oflags['c'] && !oflags['k'])
406 fatal_moan ("-c requires a socket to be specified with -s or -k");
408 if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
409 || oflags['L']) && oflags['c'])
410 fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
412 if (oflags['t'] || oflags['s'])
414 if (!oflags['p'] && !oflags['d'])
419 width=safe_atoi(oargs['w']);
420 if ((width>VT102_MAX_COLS) || (width<1))
421 fatal_moan("-w requires a width between 1 and %d\n",VT102_MAX_COLS);
424 if (oflags['s'] && !oflags['F'])
426 daemon (1, 0); /*incase socket is relative path, unlink then will fail */
430 if (oflags['s'] || oflags['t'])
435 log = file_log_new (oargs['L']);
437 fatal_moan ("unable to access log file %s", oargs['L']);
442 tty = ptty_open (NULL, NULL, width);
444 fatal_moan ("unable to open a ptty");
449 serial_open (oargs['d'],
450 oflags['K'] ? SERIAL_LOCK_ACTIVE :
451 SERIAL_LOCK_PASSIVE);
453 fatal_moan ("unable to open serial port %s", oargs['d']);
458 int baud = safe_atoi (oargs['b']);
461 fatal_moan ("Unable to parse baudrate %s", oargs['b']);
463 tty_set_baud (tty, baud);
467 tty_set_flow (tty, oflags['f'] ? 1 : 0);
474 client_socket = socket_connect (oargs['k']);
476 fatal_moan ("failed to connect to socket %s", oargs['k']);
482 if (oflags['c'] || oflags['t'])
487 ansi = ansi_new_html (stdout);
491 terminal_register_handlers ();
493 ansi_new_from_terminal (terminal_open (0, 1),
494 oflags['u'] ? 0 : 1);
495 ansi->reset (ansi, NULL);
499 mainloop (tty, server_socket, client_socket, ansi, log, history,width);
513 socket_free (server_socket);
515 socket_free (client_socket);
518 printf ("you have now exited sympathy\n");