+ /*Fold -r into -c */
+ if (oflags['r'])
+ {
+ int id = safe_atoi (oargs['r']);
+ if (id < 0)
+ fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
+
+ oflags['k']++;
+ oargs['k'] = mome ("/.sympathy/%d", id);
+ oflags['r'] = 0;
+ oflags['c']++;
+ }
+
+ if (oflags['c'] && !oflags['k'])
+ fatal_moan ("-c requires a socket to be specified with -s or -k");
+
+ if (oflags['p'] && oflags['d'])
+ fatal_moan ("-p incompatible with -d");
+
+ /*implement server and client by opening the server socket to prevent */
+ /*a race condition, and then forking and munging the cmd line options */
+ /*in the parent and child so that the child is the server and the */
+ /*parent becomes its client */
+
+ if (oflags['c'] && oflags['s'] && oflags['F'])
+ fatal_moan ("-F is incompatible with -c -s");
+
+ if (oflags['s'] && !oflags['k'])
+ {
+ char *path;
+ path = mome ("/.sympathy");
+ mkdir (path, 0700);
+ free (path);
+
+ oargs['k'] = mome ("/.sympathy/%d", getpid ());
+ oflags['k']++;
+ }
+
+ if (oflags['s'])
+ {
+ server_socket = socket_listen (oargs['k']);
+ if (!server_socket)
+ fatal_moan ("failed to create socket %s for listening", oargs['k']);
+ }
+
+ switch (fork ())
+ {
+ case 0: /*child becomes the server */
+ oflags['c'] = 0;
+ oflags['H'] = 0;
+ break;
+ case -1:
+ fatal_moan ("fork failed");
+ default:
+ oflags['s'] = 0;
+ oflags['K'] = 0;
+ oflags['d'] = 0;
+ oflags['p'] = 0;
+ oflags['b'] = 0;
+ oflags['f'] = 0;
+ oflags['L'] = 0;
+ oflags['n'] = 0;
+ }
+
+
+ if ((oflags['p'] || oflags['d'] || oflags['K'] || oflags['b'] || oflags['f']
+ || oflags['L']) && oflags['c'])
+ fatal_moan ("-c or -r are incompatible with -p, -d, -K, -b, -f or -L");
+
+ if (oflags['t'] || oflags['s'])
+ {
+ if (!oflags['p'] && !oflags['d'])
+ oflags['p']++;
+ }
+
+
+ if (oflags['s'] || oflags['t'])
+ {
+
+ if (oflags['L'])
+ {
+ log = file_log_new (oargs['L']);
+ if (!log)
+ fatal_moan ("unable to access log file %s", oargs['L']);
+ }
+
+ if (oflags['p'])
+ {
+ tty = ptty_open (NULL, NULL);
+ }
+ else
+ {
+ tty =
+ serial_open (oargs['d'],
+ oflags['K'] ? SERIAL_LOCK_ACTIVE :
+ SERIAL_LOCK_PASSIVE);
+ if (tty)
+ fatal_moan ("unable to open serial port %s", oargs['d']);
+ }
+
+ if (oflags['b'])
+ {
+ int baud = safe_atoi (oargs['b']);
+
+ if (baud < 0)
+ fatal_maon ("Unable to parse baudrate %s", oargs['b']);
+
+ tty_set_baud (tty, baud);
+
+ }
+
+ tty_set_flow (tty, oflags['f'] ? 1 : 0);
+
+ }
+
+ if (oflags['c'])
+ {
+
+ client_socket = socket_connect (oargs['k']);
+ if (!client_socket)
+ fatal_moan ("failed to connect to socket %s", oargs['k']);
+
+
+ }
+
+ if (oflags['c'] || oflags['t'])
+ {
+
+ if (oflags['H'])
+ {
+ fatal_moan ("fix a bug in HTML dispatcher before this works");
+ }
+ else
+ {
+ ansi = (ANSI *) malloc (sizeof (ANSI));
+ memset (ansi, 0, sizeof (ANSI));
+
+ terminal_register_handlers ();
+ ansi->terminal = terminal_open (0, 1);
+ ansi_reset (ansi, NULL);
+ }
+ }
+
+ if (oflags['s'] && !oflags['F']) {
+ /*FIXME become a daemon*/
+ }
+
+ mainloop (tty, server_socket, client_socket, ansi, log);
+
+ if (ansi)
+ {
+ ansi_terminal_reset (ansi);
+ terminal_atexit ();
+ }