home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / emacs / etc / emacscli.c < prev    next >
C/C++ Source or Header  |  1992-09-20  |  9KB  |  383 lines

  1. /* Client process that communicates with GNU Emacs acting as server.
  2.    Copyright (C) 1986, 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #define NO_SHORTNAMES
  22. /* #include "../src/config.h" */
  23. #undef read
  24. #undef write
  25. #undef open
  26. #ifdef close
  27. #undef close
  28. #endif
  29.  
  30. #if defined (OS2)
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34.  
  35. #define INCL_DOSQUEUES
  36. #define INCL_DOSPROCESS
  37. #include <os2.h>
  38.  
  39. static void error (ULONG rc, const char *fun)
  40. {
  41.   (void)fprintf (stderr, "%s failed, rc=%lu\n", fun, rc);
  42.   exit (1);
  43. }
  44.  
  45. #define ERROR(fun) if (rc != 0) error (rc, fun)
  46.  
  47. int main (int argc, char *argv[])
  48. {
  49.   int i, done, wait = 1;
  50.   ULONG rc, len;
  51.   HQUEUE hq_client, hq_server;
  52.   PID owner_pid;
  53.   REQUESTDATA request;
  54.   BYTE priority;
  55.   PVOID data;
  56.   char string[1024], *p;
  57.  
  58.   _wildcard (&argc, &argv);
  59.   if (argc < 2)
  60.     {
  61.       (void)fprintf (stderr, "Usage: %s [-n] [+line#] filename ...\n", argv[0]);
  62.       return (1);
  63.     }
  64.   if (stricmp(argv[1], "-n") == 0)
  65.     {
  66.       wait = 0;
  67.       argc--;
  68.       argv++;
  69.     }
  70.   (void)sprintf (string, "/queues/emacs/clients/%d", (int)getpid ());
  71.   rc = DosCreateQueue (&hq_client, QUE_FIFO | QUE_CONVERT_ADDRESS, string);
  72.   ERROR ("DosCreateQueue");
  73.   rc = DosOpenQueue (&owner_pid, &hq_server, "/queues/emacs/server");
  74.   ERROR ("DosOpenQueue");
  75.   len = 2;
  76.   for (i = 1; i < argc; i++)
  77.     {
  78.       if (argv[i][0] == '+')
  79.     strcpy (string, argv[i]);
  80.       else
  81.     _abspath (string, argv[i], sizeof (string));
  82.       len += strlen (string) + 1;
  83.     }
  84.   rc = DosAllocSharedMem (&data, 0, len,
  85.               PAG_COMMIT | OBJ_GIVEABLE | PAG_READ | PAG_WRITE);
  86.   ERROR ("DosAllocSharedMem");
  87.   rc = DosGiveSharedMem (data, owner_pid, PAG_READ);
  88.   ERROR ("DosGiveSharedMem");
  89.   p = data;
  90.   for (i = 1; i < argc; ++i)
  91.     {
  92.       if (argv[i][0] == '+')
  93.     strcpy (string, argv[i]);
  94.       else
  95.     _abspath (string, argv[i], sizeof (string));
  96.       (void)strcpy (p, string);
  97.       p += strlen (string);
  98.       *p++ = ' ';
  99.     }
  100.   *p++ = '\n';
  101.   *p = 0;
  102.   rc = DosWriteQueue (hq_server, 0, len, data, 0);
  103.   ERROR ("DosWriteQueue");
  104.   rc = DosFreeMem (data);
  105.   ERROR ("DosFreeMem");
  106.  
  107.   if (wait)
  108.     {
  109.       (void)printf ("Waiting for Emacs...");
  110.       (void)fflush (stdout);
  111.       do
  112.     {
  113.       rc = DosReadQueue (hq_client, &request, &len, &data, 0,
  114.                  DCWW_WAIT, &priority, 0);
  115.       ERROR ("DosReadQueue");
  116.       (void)fputs (data, stdout);
  117.       done = (memcmp ("Close:", data, 6) == 0);
  118.       rc = DosFreeMem (data);
  119.       ERROR ("DosFreeMem");
  120.     } while (!done);
  121.       rc = DosCloseQueue (hq_server);
  122.       ERROR ("DosCloseQueue");
  123.       rc = DosCloseQueue (hq_client);
  124.       ERROR ("DosCloseQueue");
  125.       if (getenv("EMACS_PID") == NULL)
  126.     WinSwitchToProgram(WinQuerySwitchHandle(NULL, getpid()));
  127.     }
  128.   return (0);
  129. }
  130.  
  131. #else
  132.  
  133. #if !defined(BSD) && !defined(HAVE_SYSVIPC)
  134. #include <stdio.h>
  135.  
  136. main (argc, argv)
  137.      int argc;
  138.      char **argv;
  139. {
  140.   fprintf (stderr, "%s: Sorry, the Emacs server is supported only\n",
  141.        argv[0]);
  142.   fprintf (stderr, "on systems with Berkeley sockets or System V IPC.\n");
  143.   exit (1);
  144. }
  145.  
  146. #else /* BSD or HAVE_SYSVIPC */
  147.  
  148. #if defined(BSD) && ! defined (HAVE_SYSVIPC)
  149. /* BSD code is very different from SYSV IPC code */
  150.  
  151. #include <sys/types.h>
  152. #include <sys/socket.h>
  153. #include <sys/un.h>
  154. #include <stdio.h>
  155. #include <errno.h>
  156. #include <sys/stat.h>
  157.  
  158. extern int sys_nerr;
  159. extern char *sys_errlist[];
  160. extern int errno;
  161.  
  162. main (argc, argv)
  163.      int argc;
  164.      char **argv;
  165. {
  166.   char system_name[32];
  167.   int s, i;
  168.   FILE *out;
  169.   struct sockaddr_un server;
  170.   char *homedir, *cwd, *str;
  171.   char string[BUFSIZ];
  172.   struct stat statbfr;
  173.  
  174.   char *getenv (), *getwd ();
  175.  
  176.   if (argc < 2)
  177.     {
  178.       fprintf (stderr, "Usage: %s filename\n", argv[0]);
  179.       exit (1);
  180.     }
  181.  
  182.   /* 
  183.    * Open up an AF_UNIX socket in this person's home directory
  184.    */
  185.  
  186.   if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
  187.     {
  188.       fprintf (stderr, "%s: ", argv[0]);
  189.       perror ("socket");
  190.       exit (1);
  191.     }
  192.   server.sun_family = AF_UNIX;
  193. #ifndef SERVER_HOME_DIR
  194.   gethostname (system_name, sizeof (system_name));
  195.   sprintf (server.sun_path, "/tmp/esrv%d-%s", geteuid (), system_name);
  196.  
  197.   if (stat (server.sun_path, &statbfr) == -1)
  198.     {
  199.       perror ("stat");
  200.       exit (1);
  201.     }
  202.   if (statbfr.st_uid != geteuid())
  203.     {
  204.       fprintf (stderr, "Illegal socket owner\n");
  205.       exit (1);
  206.     }
  207. #else
  208.   if ((homedir = getenv ("HOME")) == NULL)
  209.     {
  210.       fprintf (stderr, "%s: No home directory\n", argv[0]);
  211.       exit (1);
  212.     }
  213.   strcpy (server.sun_path, homedir);
  214.   strcat (server.sun_path, "/.emacs_server");
  215. #endif
  216.  
  217.   if (connect (s, &server, strlen (server.sun_path) + 2) < 0)
  218.     {
  219.       fprintf (stderr, "%s: ", argv[0]);
  220.       perror ("connect");
  221.       exit (1);
  222.     }
  223.   if ((out = fdopen (s, "r+")) == NULL)
  224.     {
  225.       fprintf (stderr, "%s: ", argv[0]);
  226.       perror ("fdopen");
  227.       exit (1);
  228.     }
  229.  
  230.   cwd = getwd (string);
  231.   if (cwd == 0)
  232.     {
  233.       /* getwd puts message in STRING if it fails.  */
  234.       fprintf (stderr, "%s: %s (%s)\n", argv[0], string,
  235.            (errno < sys_nerr) ? sys_errlist[errno] : "unknown error");
  236.       exit (1);
  237.     }
  238.  
  239.   for (i = 1; i < argc; i++)
  240.     {
  241.       if (*argv[i] == '+')
  242.     {
  243.       char *p = argv[i] + 1;
  244.       while (*p >= '0' && *p <= '9') p++;
  245.       if (*p != 0)
  246.         fprintf (out, "%s/", cwd);
  247.     }
  248.       else if (*argv[i] != '/')
  249.     fprintf (out, "%s/", cwd);
  250.       fprintf (out, "%s ", argv[i]);
  251.     }
  252.   fprintf (out, "\n");
  253.   fflush (out);
  254.  
  255.   printf ("Waiting for Emacs...");
  256.   fflush (stdout);
  257.  
  258.   rewind (out); /* re-read the output */
  259.   str = fgets (string, BUFSIZ, out); 
  260.  
  261.   /* Now, wait for an answer and print any messages.  */
  262.   
  263.   while (str = fgets (string, BUFSIZ, out))
  264.     printf ("%s", str);
  265.   
  266.   exit (0);
  267. }
  268.  
  269. #else /* This is the SYSV IPC section */
  270.  
  271. #include <sys/types.h>
  272. #include <sys/ipc.h>
  273. #include <sys/msg.h>
  274. #include <stdio.h>
  275.  
  276. main (argc, argv)
  277.      int argc;
  278.      char **argv;
  279. {
  280.   int s;
  281.   key_t key;
  282.   struct msgbuf * msgp =
  283.       (struct msgbuf *) malloc (sizeof *msgp + BUFSIZ);
  284.   struct msqid_ds * msg_st;
  285.   char *homedir, buf[BUFSIZ];
  286.   char gwdirb[BUFSIZ];
  287.   char *cwd;
  288.   char *temp;
  289.   char *getwd (), *getcwd (), *getenv ();
  290.  
  291.   if (argc < 2)
  292.     {
  293.       fprintf (stderr, "Usage: %s filename\n", argv[0]);
  294.       exit (1);
  295.     }
  296.  
  297.   /*
  298.    * Create a message queue using ~/.emacs_server as the path for ftok
  299.    */
  300.   if ((homedir = getenv ("HOME")) == NULL)
  301.     {
  302.       fprintf (stderr, "%s: No home directory\n", argv[0]);
  303.       exit (1);
  304.     }
  305.   strcpy (buf, homedir);
  306.   strcat (buf, "/.emacs_server");
  307.   creat (buf, 0600);
  308.   key = ftok (buf, 1);    /* unlikely to be anyone else using it */
  309.   s = msgget (key, 0600);
  310.   if (s == -1)
  311.     {
  312.       fprintf (stderr, "%s: ", argv[0]);
  313.       perror ("msgget");
  314.       exit (1);
  315.     }
  316.  
  317.   /* Determine working dir, so we can prefix it to all the arguments.  */
  318. #ifdef BSD
  319.   temp = getwd (gwdirb);
  320. #else
  321.   temp = getcwd (gwdirb, sizeof gwdirb);
  322. #endif
  323.  
  324.   cwd = gwdirb;
  325.   if (temp != 0)
  326.     {
  327.       /* On some systems, cwd can look like `@machine/...';
  328.      ignore everything before the first slash in such a case.  */
  329.       while (*cwd && *cwd != '/')
  330.     cwd++;
  331.       strcat (cwd, "/");
  332.     }
  333.   else
  334.     {
  335.       fprintf (stderr, cwd);
  336.       exit (1);
  337.     }
  338.  
  339.   msgp->mtext[0] = 0;
  340.   argc--; argv++;
  341.   while (argc)
  342.     {
  343.       if (*argv[0] == '+')
  344.     {
  345.       char *p = argv[0] + 1;
  346.       while (*p >= '0' && *p <= '9') p++;
  347.       if (*p != 0)
  348.         strcat (msgp->mtext, cwd);
  349.     }
  350.       else if (*argv[0] != '/')
  351.     strcat (msgp->mtext, cwd);
  352.  
  353.       strcat (msgp->mtext, argv[0]);
  354.       strcat (msgp->mtext, " ");
  355.       argv++; argc--;
  356.     }
  357.   strcat (msgp->mtext, "\n");
  358.   msgp->mtype = 1;
  359.   if (msgsnd (s, msgp, strlen (msgp->mtext)+1, 0) < 0)
  360.     {
  361.       fprintf (stderr, "%s: ", argv[0]);
  362.       perror ("msgsnd");
  363.       exit (1);
  364.     }
  365.   /*
  366.    * Now, wait for an answer
  367.    */
  368.   printf ("Waiting for Emacs...");
  369.   fflush (stdout);
  370.  
  371.   msgrcv (s, msgp, BUFSIZ, getpid (), 0);    /* wait for anything back */
  372.   strcpy (buf, msgp->mtext);
  373.  
  374.   printf ("\n%s\n", buf);
  375.   exit (0);
  376. }
  377.  
  378. #endif /* HAVE_SYSVIPC */
  379.  
  380. #endif /* BSD or HAVE_SYSVIPC */
  381.  
  382. #endif /* OS2 */
  383.