chiark / gitweb /
*** empty log message ***
[sympathy.git] / apps / sympathy.c
index 94105a3437bd9ffc0c5a75e449e63d508f18e3a8..b5bf326f1395e46d4e17502153690afa49a4393b 100644 (file)
@@ -6,10 +6,53 @@
  *
  */
 
-static char rcsid[] = "$Id$";
+static char rcsid[] =
+  "$Id$";
 
 /*
  * $Log$
+ * Revision 1.23  2008/02/28 16:57:51  james
+ * *** empty log message ***
+ *
+ * Revision 1.22  2008/02/28 01:47:44  james
+ * *** empty log message ***
+ *
+ * Revision 1.21  2008/02/27 16:01:24  james
+ * *** empty log message ***
+ *
+ * Revision 1.20  2008/02/27 10:00:34  james
+ * *** empty log message ***
+ *
+ * Revision 1.19  2008/02/27 09:47:05  james
+ * *** empty log message ***
+ *
+ * Revision 1.18  2008/02/27 09:42:53  james
+ * *** empty log message ***
+ *
+ * Revision 1.17  2008/02/27 09:42:21  james
+ * *** empty log message ***
+ *
+ * Revision 1.16  2008/02/27 01:31:38  james
+ * *** empty log message ***
+ *
+ * Revision 1.15  2008/02/27 01:31:14  james
+ * *** empty log message ***
+ *
+ * Revision 1.14  2008/02/24 00:43:55  james
+ * *** empty log message ***
+ *
+ * Revision 1.13  2008/02/24 00:42:53  james
+ * *** empty log message ***
+ *
+ * Revision 1.12  2008/02/23 11:48:52  james
+ * *** empty log message ***
+ *
+ * Revision 1.11  2008/02/20 20:16:07  james
+ * *** empty log message ***
+ *
+ * Revision 1.10  2008/02/20 19:44:37  james
+ * @@
+ *
  * Revision 1.9  2008/02/20 18:49:11  staffcvs
  * *** empty log message ***
  *
@@ -49,6 +92,7 @@ static char rcsid[] = "$Id$";
 #include <string.h>
 #include <strings.h>
 #include <malloc.h>
+#include <fcntl.h>
 
 #include "mainloop.h"
 
@@ -223,6 +267,7 @@ main (int argc, char *argv[])
   int c;
   extern char *optarg;
   extern int optind, opterr, optopt;
+  CRT_Pos size = { VT102_COLS_80, VT102_ROWS_24 };
 
   int oflags[128];
   char *oargs[128];
@@ -239,16 +284,7 @@ main (int argc, char *argv[])
 
   memset (oflags, 0, sizeof (oflags));
   memset (oargs, 0, sizeof (oargs));
-#if 0
-  "sympathy -t      [-K] [-d serialdev|-p] [-b baud] [-f] [-L log]\n"
-    "sympathy -s      [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-F] [-k skt]\n"
-    "                      [-n hlines]\n"
-    "sympathy [-s -c] [-K] [-d serialdev|-p] [-b baud] [-f] [-L log] [-k skt]\n"
-    "                      [-n hlines]\n"
-    "sympathy -c      [-H] -k skt\n"
-    "sympathy -r id   [-H]\n" "sympathy {-l|-ls}\n"
-#endif
-    while ((c = getopt (argc, argv, "tscr:lKHd:pb:fL:Fk:n:")) != EOF)
+  while ((c = getopt (argc, argv, "vw:utscr:lKHd:pb:fL:Fk:n:")) != EOF)
     {
       switch (c)
         {
@@ -281,26 +317,33 @@ main (int argc, char *argv[])
     oflags['s'] = 0;
 
 
-  if (!oflags['s'] && !oflags['c'] && !oflags['t'] && !oflags['r']
-      && !oflags['l'])
-    {
-      /*If no mode is specified behave like screen */
-      oflags['s']++;
-      oflags['c']++;
-    }
-
-
   {
     int sum = 0;
     sum += oflags['t'];
     sum += (oflags['s'] || oflags['c']) ? 1 : 0;
     sum += oflags['r'];
     sum += oflags['l'];
+    sum += oflags['v'];
+
+    if (!sum)
+      {
+        /*If no mode is specified behave like screen */
+        oflags['s']++;
+        oflags['c']++;
+        sum++;
+      }
 
     if (sum != 1)
-      fatal_moan ("specifiy exactly one of ( -c and or -s ), -t, -r and -l");
+      fatal_moan
+        ("specifiy exactly one of ( -c and or -s ), -t, -r, -l and -v");
   }
 
+  if (oflags['v'])
+    {
+      fprintf (stderr, "Version: %s\n", libsympathy_version ());
+      fprintf (stderr, "Version: %s\n", rcsid);
+      return 0;
+    }
 
   if (oflags['l'])
     return list_sockets ();
@@ -323,12 +366,19 @@ main (int argc, char *argv[])
   /*Fold -r into -c */
   if (oflags['r'])
     {
-      int id = safe_atoi (oargs['r']);
+      char *id = oargs['r'];
       if (id < 0)
         fatal_moan ("cannot parse -r %s as an integer", oargs['r']);
 
       oflags['k']++;
-      oargs['k'] = mome ("/.sympathy/%s%d", hostname, id);
+      if (safe_atoi (id) > 0)
+        {
+          oargs['k'] = mome ("/.sympathy/%s%d", hostname, safe_atoi (id));
+        }
+      else
+        {
+          oargs['k'] = mome ("/.sympathy/%s", id);
+        }
       oflags['r'] = 0;
       oflags['c']++;
     }
@@ -381,6 +431,7 @@ main (int argc, char *argv[])
           oflags['f'] = 0;
           oflags['L'] = 0;
           oflags['n'] = 0;
+          oflags['w'] = 0;
           if (server_socket)
             {
               socket_free_parent (server_socket);
@@ -389,6 +440,7 @@ main (int argc, char *argv[])
         }
     }
 
+
   if (oflags['c'] && !oflags['k'])
     fatal_moan ("-c requires a socket to be specified with -s or -k");
 
@@ -402,6 +454,28 @@ main (int argc, char *argv[])
         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_COLS);
+
+    }
+
   if (oflags['s'] && !oflags['F'])
     {
       daemon (1, 0);            /*incase socket is relative path, unlink then will fail */
@@ -420,12 +494,38 @@ main (int argc, char *argv[])
 
       if (oflags['p'])
         {
-          tty = ptty_open (NULL, NULL);
+          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 :
@@ -465,24 +565,23 @@ main (int argc, char *argv[])
 
       if (oflags['H'])
         {
-          fatal_moan ("fix a bug in HTML dispatcher before this works");
+          ansi = ansi_new_html (stdout);
         }
       else
         {
-          ansi = (ANSI *) malloc (sizeof (ANSI));
-          memset (ansi, 0, sizeof (ANSI));
-
           terminal_register_handlers ();
-          ansi->terminal = terminal_open (0, 1);
-          ansi_reset (ansi, NULL);
+          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);
+  mainloop (tty, server_socket, client_socket, ansi, log, history, &size);
 
   if (ansi)
     {
-      ansi_terminal_reset (ansi);
+      ansi->close (ansi);
       terminal_atexit ();
     }
 
@@ -496,6 +595,7 @@ main (int argc, char *argv[])
   if (client_socket)
     socket_free (client_socket);
 
-  printf ("you have now exited sympathy\n");
+  if (!oflags['H'])
+    printf ("you have now exited sympathy\n");
   return 0;
 }