(both versions): Handle -nowait and --nowait by sending data to the server.
[bpt/emacs.git] / lib-src / emacsclient.c
index 4219d0a..c294029 100644 (file)
@@ -1,11 +1,11 @@
 /* Client process that communicates with GNU Emacs acting as server.
-   Copyright (C) 1986, 1987 Free Software Foundation, Inc.
+   Copyright (C) 1986, 1987, 1994 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
 GNU Emacs is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 1, or (at your option)
+the Free Software Foundation; either version 2, or (at your option)
 any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
@@ -15,7 +15,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
+the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+Boston, MA 02111-1307, USA.  */
 
 
 #define NO_SHORTNAMES
@@ -27,7 +28,7 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #undef signal
 
 
-#if !defined(HAVE_SOCKETS) && !defined(HAVE_SYSVIPC)
+#if !defined (HAVE_SOCKETS) && !defined (HAVE_SYSVIPC)
 #include <stdio.h>
 
 main (argc, argv)
@@ -42,7 +43,7 @@ main (argc, argv)
 
 #else /* HAVE_SOCKETS or HAVE_SYSVIPC */
 
-#if ! defined (HAVE_SYSVIPC)
+#if defined (HAVE_SOCKETS) && ! defined (NO_SOCKETS_IN_FILE_SYSTEM)
 /* BSD code is very different from SYSV IPC code */
 
 #include <sys/types.h>
@@ -52,23 +53,25 @@ main (argc, argv)
 #include <stdio.h>
 #include <errno.h>
 
-extern int sys_nerr;
-extern char *sys_errlist[];
+extern char *strerror ();
 extern int errno;
 
+int
 main (argc, argv)
      int argc;
      char **argv;
 {
   char system_name[32];
   int s, i;
-  FILE *out;
+  FILE *out, *in;
   struct sockaddr_un server;
   char *homedir, *cwd, *str;
   char string[BUFSIZ];
 
   char *getenv (), *getwd ();
+  char *getcwd ();
   int geteuid ();
+  int nowait = 0;
 
   if (argc < 2)
     {
@@ -98,14 +101,16 @@ main (argc, argv)
       {
        if (errno == ENOENT)
          fprintf (stderr,
-                  "Can't find socket; have you started the server?\n");
+                  "%s: can't find socket; have you started the server?\n",
+                  argv[0]);
        else
-         perror ("stat");
+         fprintf (stderr, "%s: can't stat %s: %s\n",
+                  argv[0], server.sun_path, strerror (errno));
        exit (1);
       }
-    if (statbfr.st_uid != geteuid())
+    if (statbfr.st_uid != geteuid ())
       {
-       fprintf (stderr, "Illegal socket owner\n");
+       fprintf (stderr, "%s: Invalid socket owner\n", argv[0]);
        exit (1);
       }
   }
@@ -116,7 +121,9 @@ main (argc, argv)
       exit (1);
     }
   strcpy (server.sun_path, homedir);
-  strcat (server.sun_path, "/.emacs_server");
+  strcat (server.sun_path, "/.emacs-server-");
+  gethostname (system_name, sizeof (system_name));
+  strcat (server.sun_path, system_name);
 #endif
 
   if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
@@ -126,6 +133,8 @@ main (argc, argv)
       perror ("connect");
       exit (1);
     }
+
+  /* We use the stream OUT to send our command to the server.  */
   if ((out = fdopen (s, "r+")) == NULL)
     {
       fprintf (stderr, "%s: ", argv[0]);
@@ -133,17 +142,43 @@ main (argc, argv)
       exit (1);
     }
 
+  /* We use the stream IN to read the response.
+     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;
+     on other systems it does not.  */
+  if ((in = fdopen (s, "r+")) == NULL)
+    {
+      fprintf (stderr, "%s: ", argv[0]);
+      perror ("fdopen");
+      exit (1);
+    }
+
+#ifdef BSD
   cwd = getwd (string);
+#else
+  cwd = getcwd (string, sizeof string);
+#endif
   if (cwd == 0)
     {
       /* getwd puts message in STRING if it fails.  */
-      fprintf (stderr, "%s: %s (%s)\n", argv[0], string,
-              (errno < sys_nerr) ? sys_errlist[errno] : "unknown error");
+      fprintf (stderr, "%s: %s (%s)\n", argv[0], string, strerror (errno));
       exit (1);
     }
 
   for (i = 1; i < argc; i++)
     {
+      /* If -nowait or --nowait option is used,
+        report it to the server.  */
+      if (!strcmp (argv[i], "-nowait")
+         || (!strncmp (argv[i], "--nowait", strlen (argv[i]))
+             && strlen (argv[i]) >= 3))
+       {
+         fprintf (out, "-nowait ");
+         nowait = 1;
+         continue;
+       }
+
       if (*argv[i] == '+')
        {
          char *p = argv[i] + 1;
@@ -158,18 +193,22 @@ main (argc, argv)
   fprintf (out, "\n");
   fflush (out);
 
+  /* Maybe wait for an answer.   */
+  if (nowait)
+    return 0;
+
   printf ("Waiting for Emacs...");
   fflush (stdout);
 
-  rewind (out); /* re-read the output */
-  str = fgets (string, BUFSIZ, out); 
-
-  /* Now, wait for an answer and print any messages.  */
+  /* Now, wait for an answer and print any messages.  On some systems,
+     the first line we read will actually be the output we just sent.
+     We can't predict whether that will happen, so if it does, we
+     detect it by recognizing `Client: ' at the beginning.  */
   
-  while (str = fgets (string, BUFSIZ, out))
+  while (str = fgets (string, BUFSIZ, in))
     printf ("%s", str);
-  
-  exit (0);
+
+  return 0;
 }
 
 #else /* This is the SYSV IPC section */
@@ -177,22 +216,31 @@ main (argc, argv)
 #include <sys/types.h>
 #include <sys/ipc.h>
 #include <sys/msg.h>
+#include <sys/utsname.h>
 #include <stdio.h>
 
+char *getwd (), *getcwd (), *getenv ();
+struct utsname system_name;
+
 main (argc, argv)
      int argc;
      char **argv;
 {
   int s;
   key_t key;
-  struct msgbuf * msgp =
-      (struct msgbuf *) malloc (sizeof *msgp + BUFSIZ);
+  /* Size of text allocated in MSGP.  */
+  int size_allocated = BUFSIZ;
+  /* Amount of text used in MSGP.  */
+  int used;
+  struct msgbuf *msgp
+    = (struct msgbuf *) malloc (sizeof (struct msgbuf) + size_allocated);
   struct msqid_ds * msg_st;
   char *homedir, buf[BUFSIZ];
   char gwdirb[BUFSIZ];
   char *cwd;
   char *temp;
-  char *getwd (), *getcwd (), *getenv ();
+  char *progname = argv[0];
+  int nowait = 0;
 
   if (argc < 2)
     {
@@ -201,7 +249,7 @@ main (argc, argv)
     }
 
   /*
-   * Create a message queue using ~/.emacs_server as the path for ftok
+   * Create a message queue using ~/.emacs-server as the path for ftok
    */
   if ((homedir = getenv ("HOME")) == NULL)
     {
@@ -209,7 +257,14 @@ main (argc, argv)
       exit (1);
     }
   strcpy (buf, homedir);
-  strcat (buf, "/.emacs_server");
+#ifndef HAVE_LONG_FILE_NAMES
+  /* If file names are short, we can't fit the host name.  */
+  strcat (buf, "/.emacs-server");
+#else
+  strcat (buf, "/.emacs-server-");
+  uname (&system_name);
+  strcat (buf, system_name.nodename);
+#endif
   creat (buf, 0600);
   key = ftok (buf, 1); /* unlikely to be anyone else using it */
   s = msgget (key, 0600 | IPC_CREAT);
@@ -238,49 +293,102 @@ main (argc, argv)
     }
   else
     {
-      fprintf (stderr, cwd);
+      fprintf (stderr, "%s: %s\n", argv[0], cwd);
       exit (1);
     }
 
   msgp->mtext[0] = 0;
+  used = 0;
   argc--; argv++;
   while (argc)
     {
-      if (*argv[0] == '+')
+      int need_cwd = 0;
+      char *modified_arg = argv[0];
+
+      /* If -nowait or --nowait option is used,
+        report it to the server.  */
+      if (!strcmp (modified_arg, "-nowait")
+         || (!strncmp (modified_arg, "--nowait", strlen (modified_arg))
+             && strlen (modified_arg) >= 3))
+       {
+         modified_arg = "-nowait";
+         nowait = 1;
+       }
+      else if (*modified_arg == '+')
        {
-         char *p = argv[0] + 1;
+         char *p = modified_arg + 1;
          while (*p >= '0' && *p <= '9') p++;
          if (*p != 0)
-           strcat (msgp->mtext, cwd);
+           need_cwd = 1;
+       }
+      else if (*modified_arg != '/')
+       need_cwd = 1;
+
+      if (need_cwd)
+       used += strlen (cwd);
+      used += strlen (modified_arg) + 1;
+      while (used + 2 > size_allocated)
+       {
+         size_allocated *= 2;
+         msgp = (struct msgbuf *) realloc (msgp,
+                                           (sizeof (struct msgbuf)
+                                            + size_allocated));
        }
-      else if (*argv[0] != '/')
+
+      if (need_cwd)
        strcat (msgp->mtext, cwd);
 
-      strcat (msgp->mtext, argv[0]);
+      strcat (msgp->mtext, modified_arg);
       strcat (msgp->mtext, " ");
       argv++; argc--;
     }
   strcat (msgp->mtext, "\n");
+#ifdef HPUX /* HPUX has a bug.  */
+  if (strlen (msgp->mtext) >= 512)
+    {
+      fprintf (stderr, "%s: args too long for msgsnd\n", progname);
+      exit (1);
+    }
+#endif
   msgp->mtype = 1;
   if (msgsnd (s, msgp, strlen (msgp->mtext)+1, 0) < 0)
     {
-      fprintf (stderr, "%s: ", argv[0]);
+      fprintf (stderr, "%s: ", progname);
       perror ("msgsnd");
       exit (1);
     }
-  /*
-   * Now, wait for an answer
-   */
+
+  /* Maybe wait for an answer.   */
+  if (nowait)
+    return 0;
+
   printf ("Waiting for Emacs...");
   fflush (stdout);
 
   msgrcv (s, msgp, BUFSIZ, getpid (), 0);      /* wait for anything back */
   strcpy (buf, msgp->mtext);
 
-  printf ("\n%s\n", buf);
+  printf ("\n");
+  if (*buf)
+    printf ("%s\n", buf);
   exit (0);
 }
 
 #endif /* HAVE_SYSVIPC */
 
 #endif /* HAVE_SOCKETS or HAVE_SYSVIPC */
+\f
+#ifndef HAVE_STRERROR
+char *
+strerror (errnum)
+     int errnum;
+{
+  extern char *sys_errlist[];
+  extern int sys_nerr;
+
+  if (errnum >= 0 && errnum < sys_nerr)
+    return sys_errlist[errnum];
+  return (char *) "Unknown error";
+}
+
+#endif /* ! HAVE_STRERROR */