(both versions): Handle -nowait and --nowait by sending data to the server.
[bpt/emacs.git] / lib-src / emacsclient.c
index 254ba88..c294029 100644 (file)
@@ -63,7 +63,7 @@ main (argc, argv)
 {
   char system_name[32];
   int s, i;
-  FILE *out;
+  FILE *out, *in;
   struct sockaddr_un server;
   char *homedir, *cwd, *str;
   char string[BUFSIZ];
@@ -71,6 +71,7 @@ main (argc, argv)
   char *getenv (), *getwd ();
   char *getcwd ();
   int geteuid ();
+  int nowait = 0;
 
   if (argc < 2)
     {
@@ -132,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]);
@@ -139,6 +142,18 @@ 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
@@ -153,6 +168,17 @@ main (argc, argv)
 
   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;
@@ -167,17 +193,21 @@ 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);
-  
+
   return 0;
 }
 
@@ -210,6 +240,7 @@ main (argc, argv)
   char *cwd;
   char *temp;
   char *progname = argv[0];
+  int nowait = 0;
 
   if (argc < 2)
     {
@@ -273,7 +304,17 @@ main (argc, argv)
     {
       int need_cwd = 0;
       char *modified_arg = argv[0];
-      if (*modified_arg == '+')
+
+      /* 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 = modified_arg + 1;
          while (*p >= '0' && *p <= '9') p++;
@@ -316,16 +357,20 @@ main (argc, argv)
       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);
 }