]> code.delx.au - gnu-emacs/blobdiff - lib-src/emacsclient.c
Merged in changes from CVS trunk.
[gnu-emacs] / lib-src / emacsclient.c
index 79642cbe47e30d100d3ecefb513518ab12726233..888c85e868538d1ba429f50135e05b6edab06ebf 100644 (file)
@@ -48,9 +48,6 @@ Boston, MA 02111-1307, USA.  */
 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
@@ -157,7 +154,7 @@ decode_options (argc, argv)
 
        case 'V':
          printf ("emacsclient %s\n", VERSION);
-         exit (0);
+         exit (EXIT_SUCCESS);
          break;
 
         case 't':
@@ -176,7 +173,7 @@ decode_options (argc, argv)
 
        default:
          fprintf (stderr, "Try `%s --help' for more information\n", progname);
-         exit (1);
+         exit (EXIT_FAILURE);
          break;
        }
     }
@@ -209,7 +206,36 @@ 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);
-  exit (0);
+  exit (EXIT_SUCCESS);
+}
+
+/* Like malloc but get fatal error if memory is exhausted.  */
+
+long *
+xmalloc (size)
+     unsigned int size;
+{
+  long *result = (long *) malloc (size);
+  if (result == NULL)
+    {
+      perror ("malloc");
+      exit (EXIT_FAILURE);
+    }
+  return result;
+}
+
+/* Like strdup but get a fatal error if memory is exhausted. */
+
+char *
+xstrdup (const char *s)
+{
+  char *result = strdup (s);
+  if (result == NULL)
+    {
+      perror ("strdup");
+      exit (EXIT_FAILURE);
+    }
+  return result;
 }
 
 /* In STR, insert a & before each &, each space, each newline, and
@@ -223,7 +249,7 @@ quote_argument (str, stream)
      char *str;
      FILE *stream;
 {
-  char *copy = (char *) malloc (strlen (str) * 2 + 1);
+  char *copy = (char *) xmalloc (strlen (str) * 2 + 1);
   char *p, *q;
 
   p = str;
@@ -251,7 +277,7 @@ quote_argument (str, stream)
     }
   *q++ = 0;
 
-  fprintf (stream, copy);
+  fprintf (stream, "%s", copy);
 
   free (copy);
 }
@@ -291,20 +317,6 @@ unquote_argument (str)
   return str;
 }
 
-/* Like malloc but get fatal error if memory is exhausted.  */
-
-long *
-xmalloc (size)
-     unsigned int size;
-{
-  long *result = (long *) malloc (size);
-  if (result == NULL)
-  {
-    perror ("malloc");
-    exit (1);
-  }
-  return result;
-}
 \f
 /*
   Try to run a different command, or --if no alternate editor is
@@ -321,7 +333,7 @@ fail (void)
     }
   else
     {
-      exit (1);
+      exit (EXIT_FAILURE);
     }
 }
 
@@ -505,7 +517,7 @@ main (argc, argv)
     {
       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);
     }
 
   /*
@@ -583,7 +595,7 @@ main (argc, argv)
                  {
                    fprintf (stderr, "%s: socket-name %s too long",
                             argv[0], socket_name);
-                   exit (1);
+                   exit (EXIT_FAILURE);
                  }
 
                sock_status = socket_status (server.sun_path);
@@ -610,11 +622,11 @@ main (argc, argv)
         /* `stat' failed */
         if (saved_errno == ENOENT)
           fprintf (stderr,
-                   "%s: Can't find socket; have you started the server?\n\
+                   "%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 stat %s: %s\n",
                    argv[0], server.sun_path, strerror (saved_errno));
         fail ();
         break;
@@ -629,7 +641,7 @@ To start the server in Emacs, type \"M-x server-start\".\n",
       fail ();
     }
 
-  /* We use the stream OUT to send our command to the server.  */
+  /* We use the stream OUT to send our commands to the server.  */
   if ((out = fdopen (s, "r+")) == NULL)
     {
       fprintf (stderr, "%s: ", argv[0]);
@@ -637,7 +649,7 @@ To start the server in Emacs, type \"M-x server-start\".\n",
       fail ();
     }
 
-  /* We use the stream IN to read the response.
+  /* We use the stream IN to read the responses.
      We used to use just one stream for both output and input
      on the socket, but reversing direction works nonportably:
      on some systems, the output appears as the first input;
@@ -660,7 +672,7 @@ To start the server in Emacs, type \"M-x server-start\".\n",
 
 #ifdef HAVE_GETCWD
       fprintf (stderr, "%s: %s (%s)\n", argv[0],
-              "Cannot get current working directory", strerror (errno));
+              "cannot get current working directory", strerror (errno));
 #else
       fprintf (stderr, "%s: %s (%s)\n", argv[0], string, strerror (errno));
 #endif
@@ -670,6 +682,28 @@ To start the server in Emacs, type \"M-x server-start\".\n",
   /* First of all, send our version number for verification. */
   fprintf (out, "-version %s ", VERSION);
 
+  /* Send over our environment. */
+  {
+    extern char **environ;
+    int i;
+    for (i = 0; environ[i]; i++)
+      {
+        char *name = xstrdup (environ[i]);
+        char *value = strchr (name, '=');
+        if (value && strlen (value) > 1)
+          {
+            *value++ = 0;
+            fprintf (out, "-env ");
+            quote_argument (name, out);
+            fprintf (out, " ");
+            quote_argument (value, out);
+            fprintf (out, " ");
+            fflush (out);
+          }
+        free (name);
+      }
+  }
+
   if (nowait)
     fprintf (out, "-nowait ");
 
@@ -780,13 +814,8 @@ To start the server in Emacs, type \"M-x server-start\".\n",
   fflush (out);
   fsync (fileno (out));
 
-  /* Maybe wait for an answer.   */
-  if (nowait)
-    {
-      return 0;
-    }
-
-  if (!eval && !tty)
+  /* Wait for an answer. */
+  if (!eval && !tty && !nowait)
     {
       printf ("Waiting for Emacs...");
       needlf = 2;
@@ -846,7 +875,7 @@ To start the server in Emacs, type \"M-x server-start\".\n",
   fflush (stdout);
   fsync (1);
 
-  return 0;
+  return EXIT_SUCCESS;
 }
 
 #endif /* HAVE_SOCKETS */
@@ -868,3 +897,5 @@ strerror (errnum)
 
 /* arch-tag: f39bb9c4-73eb-477e-896d-50832e2ca9a7
    (do not change this comment) */
+
+/* emacsclient.c ends here */