(lwlib_toolkit_type): New variable.
[bpt/emacs.git] / lib-src / emacsclient.c
index e3c63b6..56f1dd3 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,
@@ -19,16 +19,15 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 
 #define NO_SHORTNAMES
-#include "../src/config.h"
+#include <../src/config.h>
 #undef read
 #undef write
 #undef open
-#ifdef close
 #undef close
-#endif
+#undef signal
 
 
-#if !defined(HAVE_SOCKETS) && !defined(HAVE_SYSVIPC)
+#if !defined (HAVE_SOCKETS) && !defined (HAVE_SYSVIPC)
 #include <stdio.h>
 
 main (argc, argv)
@@ -43,7 +42,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>
@@ -53,10 +52,10 @@ 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;
@@ -69,6 +68,7 @@ main (argc, argv)
   char string[BUFSIZ];
 
   char *getenv (), *getwd ();
+  char *getcwd ();
   int geteuid ();
 
   if (argc < 2)
@@ -99,14 +99,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);
       }
   }
@@ -117,10 +119,13 @@ 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, &server, strlen (server.sun_path) + 2) < 0)
+  if (connect (s, (struct sockaddr *) &server, strlen (server.sun_path) + 2)
+      < 0)
     {
       fprintf (stderr, "%s: ", argv[0]);
       perror ("connect");
@@ -133,12 +138,15 @@ main (argc, argv)
       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);
     }
 
@@ -169,7 +177,7 @@ main (argc, argv)
   while (str = fgets (string, BUFSIZ, out))
     printf ("%s", str);
   
-  exit (0);
+  return 0;
 }
 
 #else /* This is the SYSV IPC section */
@@ -177,22 +185,30 @@ 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];
 
   if (argc < 2)
     {
@@ -201,7 +217,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 +225,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,33 +261,57 @@ 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 (*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);
     }
@@ -284,3 +331,18 @@ main (argc, argv)
 #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 */