]> code.delx.au - gnu-emacs/blobdiff - lib-src/emacsclient.c
Several additions.
[gnu-emacs] / lib-src / emacsclient.c
index 2a53b1626934c20c731ef81d93c3747a8371e86d..dbdde16db0293516723c6eba3824b69f3718d67c 100644 (file)
@@ -67,6 +67,9 @@ char *display = NULL;
    is not running.  --alternate-editor.   */
 const char * alternate_editor = NULL;
 
+/* If non-NULL, the filename of the UNIX socket.  */
+char *socket_name = NULL;
+
 void print_help_and_exit ();
 
 struct option longopts[] =
@@ -76,6 +79,7 @@ struct option longopts[] =
   { "help",    no_argument,       NULL, 'H' },
   { "version", no_argument,       NULL, 'V' },
   { "alternate-editor", required_argument, NULL, 'a' },
+  { "socket-name",     required_argument, NULL, 's' },
   { "display", required_argument, NULL, 'd' },
   { 0, 0, 0, 0 }
 };
@@ -91,7 +95,7 @@ decode_options (argc, argv)
   while (1)
     {
       int opt = getopt_long (argc, argv,
-                            "VHnea:d:", longopts, 0);
+                            "VHnea:s:d:", longopts, 0);
 
       if (opt == EOF)
        break;
@@ -109,6 +113,10 @@ decode_options (argc, argv)
          alternate_editor = optarg;
          break;
 
+       case 's':
+         socket_name = optarg;
+         break;
+
        case 'd':
          display = optarg;
          break;
@@ -152,6 +160,8 @@ The following OPTIONS are accepted:\n\
 -n, --no-wait           Don't wait for the server to return\n\
 -e, --eval              Evaluate the FILE arguments as ELisp expressions\n\
 -d, --display=DISPLAY   Visit the file in the given display\n\
+-s, --socket-name=FILENAME\n\
+                        Set the filename of the UNIX socket for communication\n\
 -a, --alternate-editor=EDITOR\n\
                         Editor to fallback to if the server is not running\n\
 \n\
@@ -159,14 +169,15 @@ Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
   exit (0);
 }
 
-/* Return a copy of NAME, inserting a &
+/* Inserting a &
    before each &, each space, each newline, and any initial -.
    Change spaces to underscores, too, so that the
    return value never contains a space.  */
 
-char *
-quote_file_name (name)
+void
+quote_file_name (name, stream)
      char *name;
+     FILE *stream;
 {
   char *copy = (char *) malloc (strlen (name) * 2 + 1);
   char *p, *q;
@@ -196,7 +207,9 @@ quote_file_name (name)
     }
   *q++ = 0;
 
-  return copy;
+  fprintf (stream, copy);
+
+  free (copy);
 }
 
 /* Like malloc but get fatal error if memory is exhausted.  */
@@ -300,7 +313,7 @@ main (argc, argv)
   /* Process options.  */
   decode_options (argc, argv);
 
-  if (argc - optind < 1)
+  if ((argc - optind < 1) && !eval)
     {
       fprintf (stderr, "%s: file name or argument required\n", progname);
       fprintf (stderr, "Try `%s --help' for more information\n", progname);
@@ -347,7 +360,18 @@ main (argc, argv)
   {
     int sock_status = 0;
 
-    sprintf (server.sun_path, "/tmp/emacs%d-%s/server", (int) geteuid (), system_name);
+    if (! socket_name)
+      {
+       socket_name = alloca (system_name_length + 100);
+       sprintf (socket_name, "/tmp/emacs%d-%s/server",
+                (int) geteuid (), system_name);
+      }
+
+    if (strlen (socket_name) < sizeof (server.sun_path))
+      strcpy (server.sun_path, socket_name);
+    else
+      fprintf (stderr, "%s: socket-name %s too long",
+              argv[0], socket_name);
 
     /* See if the socket exists, and if it's owned by us. */
     sock_status = socket_status (server.sun_path);
@@ -455,24 +479,47 @@ To start the server in Emacs, type \"M-x server-start\".\n",
     fprintf (out, "-eval ");
 
   if (display)
-    fprintf (out, "-display %s ", quote_file_name (display));
+    {
+      fprintf (out, "-display ");
+      quote_file_name (display, out);
+      fprintf (out, " ");
+    }
 
-  for (i = optind; i < argc; i++)
+  if ((argc - optind > 0))
     {
-      if (eval)
-       ; /* Don't prepend any cwd or anything like that.  */
-      else if (*argv[i] == '+')
+      for (i = optind; i < argc; i++)
        {
-         char *p = argv[i] + 1;
-         while (isdigit ((unsigned char) *p) || *p == ':') p++;
-         if (*p != 0)
-           fprintf (out, "%s/", quote_file_name (cwd));
+         if (eval)
+           ; /* Don't prepend any cwd or anything like that.  */
+         else if (*argv[i] == '+')
+           {
+             char *p = argv[i] + 1;
+             while (isdigit ((unsigned char) *p) || *p == ':') p++;
+             if (*p != 0)
+               {
+                 quote_file_name (cwd, out);
+                 fprintf (out, "/");
+               }
+           }
+         else if (*argv[i] != '/')
+           {
+             quote_file_name (cwd, out);
+             fprintf (out, "/");
+           }
+
+         quote_file_name (argv[i], out);
+         fprintf (out, " ");
        }
-      else if (*argv[i] != '/')
-       fprintf (out, "%s/", quote_file_name (cwd));
-
-      fprintf (out, "%s ", quote_file_name (argv[i]));
     }
+  else
+    {
+      while ((str = fgets (string, BUFSIZ, stdin)))
+       {
+         quote_file_name (str, out);
+       }
+      fprintf (out, " ");
+    }
+  
   fprintf (out, "\n");
   fflush (out);