+ if (oflags['c'] && !oflags['k'] && !oflags['r'])
+ fatal_moan ("-c requires a socket to be specified with -s or -k or -r");
+
+ 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['w'])
+ {
+ char buf[128], *ptr;
+ strcpy (buf, oargs['w']);
+ ptr = index (buf, 'x');
+ if (ptr)
+ {
+ *ptr = 0;
+ ptr++;
+ size.y = safe_atoi (ptr);
+ }
+ size.x = safe_atoi (buf);
+
+ if ((size.x > VT102_MAX_COLS) || (size.x < 1))
+ fatal_moan ("-w requires a width between 1 and %d\n", VT102_MAX_COLS);
+
+ if ((size.y > VT102_MAX_ROWS) || (size.y < 1))
+ fatal_moan ("-w requires a height between 1 and %d\n",
+ VT102_MAX_ROWS);
+
+ }
+
+ if (oflags['s'] && !oflags['F'])
+ {
+ /*nochdir incase socket is relative path, unlink then will fail */
+ daemon (1, 0);
+
+ }
+
+
+ if (oflags['s'])
+ {
+ char *path;
+ path = mome ("/.sympathy");
+ mkdir (path, 0700);
+ free (path);
+
+
+ if (!oflags['k'])
+ {
+ pid = getpid ();
+
+ oargs['k'] = mome ("/.sympathy/%s%d", hostname, pid);
+ oflags['k']++;
+ }
+
+ server_socket = socket_listen (oargs['k']);
+
+ /*Tell our parent's parent what our pid is */
+ if (csnok)
+ {
+ pid = getpid ();
+
+ write (csnok_pipe[1], &pid, sizeof (pid));
+ close (csnok_pipe[1]);
+ }
+
+ if (!server_socket)
+ fatal_moan ("failed to create socket %s for listening", oargs['k']);
+
+ }
+
+ 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, &size);
+ if (!tty)
+ fatal_moan ("unable to open a ptty");
+ }
+ else
+ {
+ /*HACK-- check that console=device does not occur in */
+ /*/proc/cmdline */
+ if (!oargs['d'])
+ fatal_moan ("no argument to -d");
+
+ {
+ char kernel_cmdline[4096] = { 0 };
+ char search_string[1024] = "console=";
+ char *ptr = oargs['d'];
+ int fd;
+
+ if (!strncmp ("/dev/", ptr, 5))
+ ptr += 5;
+
+ strcat (search_string, ptr);
+
+ fd = open ("/proc/cmdline", O_RDONLY);
+ read (fd, kernel_cmdline, sizeof (kernel_cmdline));
+ close (fd);
+
+ kernel_cmdline[sizeof (kernel_cmdline) - 1] = 0;
+
+ if (strstr (kernel_cmdline, search_string))
+ fatal_moan ("/proc/cmdline contains %s", search_string);
+ }
+
+ 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_moan ("Unable to parse baudrate %s", oargs['b']);
+
+ tty_set_baud (tty, baud);
+
+ }
+
+ tty_set_flow (tty, oflags['f'] ? 1 : 0);
+
+ }
+
+
+ FISH
+ {
+ char *id = oargs['r'];
+ if (id < 0)
+ fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
+
+ oflags['k']++;
+ if (safe_atoi (id) > 0)
+ {
+ oargs['k'] = find_socket ("%s%d", hostname, safe_atoi (id));
+ }
+ else
+ {
+ oargs['k'] = find_socket ("%s", id);
+ }
+ oflags['r'] = 0;
+ oflags['c']++;
+ }
+
+ 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'])
+ {
+ ansi = ansi_new_html (stdout);
+ }
+ else
+ {
+ terminal_register_handlers ();
+ ansi =
+ ansi_new_from_terminal (terminal_open (0, 1),
+ oflags['u'] ? 0 : 1);
+ ansi->reset (ansi, NULL);
+ }
+ }
+
+ mainloop (tty, server_socket, client_socket, ansi, log, history, &size);
+
+ if (ansi)
+ {
+ ansi->close (ansi);
+ terminal_atexit ();
+ }
+
+ if (tty)
+ tty->close (tty);