]> code.delx.au - gnu-emacs/blobdiff - lib-src/emacsclient.c
(rmail-resend): Let MAIL-ALIAS-FILE arg override mail-personal-alias-file.
[gnu-emacs] / lib-src / emacsclient.c
index 8ac1902929e37966b9b5ff2f09c85a9e9d47174d..24dcdffff2c2cd1f7db622278782515c7f82f7c8 100644 (file)
@@ -1,5 +1,5 @@
 /* Client process that communicates with GNU Emacs acting as server.
 /* Client process that communicates with GNU Emacs acting as server.
-   Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2003
+   Copyright (C) 1986, 1987, 1994, 1999, 2000, 2001, 2003, 2004
    Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
    Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
@@ -44,9 +44,6 @@ Boston, MA 02111-1307, USA.  */
 char *getenv (), *getwd ();
 char *getcwd ();
 
 char *getenv (), *getwd ();
 char *getcwd ();
 
-/* This is defined with -D from the compilation command,
-   which extracts it from ../lisp/version.el.  */
-
 #ifndef VERSION
 #define VERSION "unspecified"
 #endif
 #ifndef VERSION
 #define VERSION "unspecified"
 #endif
@@ -92,6 +89,8 @@ decode_options (argc, argv)
      int argc;
      char **argv;
 {
      int argc;
      char **argv;
 {
+  alternate_editor = getenv ("ALTERNATE_EDITOR");
+
   while (1)
     {
       int opt = getopt_long (argc, argv,
   while (1)
     {
       int opt = getopt_long (argc, argv,
@@ -100,8 +99,6 @@ decode_options (argc, argv)
       if (opt == EOF)
        break;
 
       if (opt == EOF)
        break;
 
-      alternate_editor = getenv ("ALTERNATE_EDITOR");
-
       switch (opt)
        {
        case 0:
       switch (opt)
        {
        case 0:
@@ -131,7 +128,7 @@ decode_options (argc, argv)
 
        case 'V':
          printf ("emacsclient %s\n", VERSION);
 
        case 'V':
          printf ("emacsclient %s\n", VERSION);
-         exit (0);
+         exit (EXIT_SUCCESS);
          break;
 
        case 'H':
          break;
 
        case 'H':
@@ -140,7 +137,7 @@ decode_options (argc, argv)
 
        default:
          fprintf (stderr, "Try `%s --help' for more information\n", progname);
 
        default:
          fprintf (stderr, "Try `%s --help' for more information\n", progname);
-         exit (1);
+         exit (EXIT_FAILURE);
          break;
        }
     }
          break;
        }
     }
@@ -166,7 +163,7 @@ The following OPTIONS are accepted:\n\
                         Editor to fallback to if the server is not running\n\
 \n\
 Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
                         Editor to fallback to if the server is not running\n\
 \n\
 Report bugs to bug-gnu-emacs@gnu.org.\n", progname);
-  exit (0);
+  exit (EXIT_SUCCESS);
 }
 
 /* In NAME, insert a & before each &, each space, each newline, and
 }
 
 /* In NAME, insert a & before each &, each space, each newline, and
@@ -206,7 +203,7 @@ quote_file_name (name, stream)
     }
   *q++ = 0;
 
     }
   *q++ = 0;
 
-  fprintf (stream, copy);
+  fprintf (stream, "%s", copy);
 
   free (copy);
 }
 
   free (copy);
 }
@@ -221,7 +218,7 @@ xmalloc (size)
   if (result == NULL)
   {
     perror ("malloc");
   if (result == NULL)
   {
     perror ("malloc");
-    exit (1);
+    exit (EXIT_FAILURE);
   }
   return result;
 }
   }
   return result;
 }
@@ -243,7 +240,7 @@ fail (argc, argv)
     }
   else
     {
     }
   else
     {
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 }
 
     }
 }
 
@@ -299,8 +296,6 @@ main (argc, argv)
      int argc;
      char **argv;
 {
      int argc;
      char **argv;
 {
-  char *system_name;
-  int system_name_length;
   int s, i, needlf = 0;
   FILE *out, *in;
   struct sockaddr_un server;
   int s, i, needlf = 0;
   FILE *out, *in;
   struct sockaddr_un server;
@@ -316,7 +311,7 @@ main (argc, argv)
     {
       fprintf (stderr, "%s: file name or argument required\n", progname);
       fprintf (stderr, "Try `%s --help' for more information\n", progname);
     {
       fprintf (stderr, "%s: file name or argument required\n", progname);
       fprintf (stderr, "Try `%s --help' for more information\n", progname);
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 
   /*
     }
 
   /*
@@ -332,49 +327,39 @@ main (argc, argv)
 
   server.sun_family = AF_UNIX;
 
 
   server.sun_family = AF_UNIX;
 
-  {
-    char *dot;
-    system_name_length = 32;
-
-    while (1)
-      {
-       system_name = (char *) xmalloc (system_name_length + 1);
-
-       /* system_name must be null-terminated string.  */
-       system_name[system_name_length] = '\0';
-
-       if (gethostname (system_name, system_name_length) == 0)
-         break;
-
-       free (system_name);
-       system_name_length *= 2;
-      }
-
-    /* We always use the non-dotted host name, for simplicity.  */
-    dot = index (system_name, '.');
-    if (dot)
-      *dot = '\0';
-  }
-
   {
     int sock_status = 0;
   {
     int sock_status = 0;
+    int default_sock = !socket_name;
+    int saved_errno;
+    char *server_name = "server";
+
+    if (socket_name && !index (socket_name, '/') && !index (socket_name, '\\'))
+      { /* socket_name is a file name component.  */
+       server_name = socket_name;
+       socket_name = NULL;
+       default_sock = 1;       /* Try both UIDs.  */
+      }
 
 
-    if (! socket_name)
+    if (default_sock)
       {
       {
-       socket_name = alloca (system_name_length + 100);
-       sprintf (socket_name, "/tmp/emacs%d-%s/server",
-                (int) geteuid (), system_name);
+       socket_name = alloca (100 + strlen (server_name));
+       sprintf (socket_name, "/tmp/emacs%d/%s",
+                (int) geteuid (), server_name);
       }
 
     if (strlen (socket_name) < sizeof (server.sun_path))
       strcpy (server.sun_path, socket_name);
     else
       }
 
     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);
+      {
+       fprintf (stderr, "%s: socket-name %s too long",
+                argv[0], socket_name);
+       exit (EXIT_FAILURE);
+      }
 
     /* See if the socket exists, and if it's owned by us. */
     sock_status = socket_status (server.sun_path);
 
     /* See if the socket exists, and if it's owned by us. */
     sock_status = socket_status (server.sun_path);
-    if (sock_status)
+    saved_errno = errno;
+    if (sock_status && default_sock)
       {
        /* Failing that, see if LOGNAME or USER exist and differ from
           our euid.  If so, look for a socket based on the UID
       {
        /* Failing that, see if LOGNAME or USER exist and differ from
           our euid.  If so, look for a socket based on the UID
@@ -382,19 +367,35 @@ main (argc, argv)
           that init_editfns uses to set the global Vuser_full_name.  */
 
        char *user_name = (char *) getenv ("LOGNAME");
           that init_editfns uses to set the global Vuser_full_name.  */
 
        char *user_name = (char *) getenv ("LOGNAME");
+
        if (!user_name)
          user_name = (char *) getenv ("USER");
 
        if (user_name)
          {
            struct passwd *pw = getpwnam (user_name);
        if (!user_name)
          user_name = (char *) getenv ("USER");
 
        if (user_name)
          {
            struct passwd *pw = getpwnam (user_name);
+
            if (pw && (pw->pw_uid != geteuid ()))
              {
                /* We're running under su, apparently. */
            if (pw && (pw->pw_uid != geteuid ()))
              {
                /* We're running under su, apparently. */
-               sprintf (server.sun_path, "/tmp/emacs%d-%s/server",
-                        (int) pw->pw_uid, system_name);
+               socket_name = alloca (100 + strlen (server_name));
+               sprintf (socket_name, "/tmp/emacs%d/%s",
+                        (int) pw->pw_uid, server_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);
+                   exit (EXIT_FAILURE);
+                 }
+
                sock_status = socket_status (server.sun_path);
                sock_status = socket_status (server.sun_path);
+               saved_errno = errno;
              }
              }
+           else
+             errno = saved_errno;
          }
       }
 
          }
       }
 
@@ -412,14 +413,14 @@ main (argc, argv)
 
        case 2:
         /* `stat' failed */
 
        case 2:
         /* `stat' failed */
-        if (errno == ENOENT)
+        if (saved_errno == ENOENT)
           fprintf (stderr,
                    "%s: can't find socket; have you started the server?\n\
 To start the server in Emacs, type \"M-x server-start\".\n",
                    argv[0]);
         else
           fprintf (stderr, "%s: can't stat %s: %s\n",
           fprintf (stderr,
                    "%s: can't find socket; have you started the server?\n\
 To start the server in Emacs, type \"M-x server-start\".\n",
                    argv[0]);
         else
           fprintf (stderr, "%s: can't stat %s: %s\n",
-                   argv[0], server.sun_path, strerror (errno));
+                   argv[0], server.sun_path, strerror (saved_errno));
         fail (argc, argv);
         break;
        }
         fail (argc, argv);
         break;
        }
@@ -518,13 +519,13 @@ To start the server in Emacs, type \"M-x server-start\".\n",
        }
       fprintf (out, " ");
     }
        }
       fprintf (out, " ");
     }
-  
+
   fprintf (out, "\n");
   fflush (out);
 
   /* Maybe wait for an answer.   */
   if (nowait)
   fprintf (out, "\n");
   fflush (out);
 
   /* Maybe wait for an answer.   */
   if (nowait)
-    return 0;
+    return EXIT_SUCCESS;
 
   if (!eval)
     {
 
   if (!eval)
     {
@@ -546,7 +547,7 @@ To start the server in Emacs, type \"M-x server-start\".\n",
     printf ("\n");
   fflush (stdout);
 
     printf ("\n");
   fflush (stdout);
 
-  return 0;
+  return EXIT_SUCCESS;
 }
 
 #endif /* HAVE_SOCKETS */
 }
 
 #endif /* HAVE_SOCKETS */
@@ -568,3 +569,5 @@ strerror (errnum)
 
 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
    (do not change this comment) */
 
 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
    (do not change this comment) */
+
+/* emacsclient.c ends here */