Annotation of 43BSDReno/contrib/emacs-18.55/etc/emacsclient.c, revision 1.1

1.1     ! root        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 distributed in the hope that it will be useful,
        !             7: but without any warranty.  No author or distributor
        !             8: accepts responsibility to anyone for the consequences of using it
        !             9: or for whether it serves any particular purpose or works at all,
        !            10: unless he says so in writing.
        !            11: 
        !            12: Everyone is granted permission to copy, modify and redistribute
        !            13: GNU Emacs, but only under the conditions described in the
        !            14: document "GNU Emacs copying permission notice".   An exact copy
        !            15: of the document is supposed to have been given to you along with
        !            16: GNU Emacs so that you can know how you may redistribute it all.
        !            17: It should be in a file named COPYING.  Among other things, the
        !            18: copyright notice and this notice must be preserved on all copies.  */
        !            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: 
        !            31: #if !defined(BSD) && !defined(HAVE_SYSVIPC)
        !            32: #include <stdio.h>
        !            33: 
        !            34: main ()
        !            35: {
        !            36:   fprintf (stderr, "Sorry, the Emacs server is supported only on Berkeley Unix\n");
        !            37:   fprintf (stderr, "or System V systems with IPC\n");
        !            38:   exit (1);
        !            39: }
        !            40: 
        !            41: #else /* BSD or HAVE_SYSVIPC */
        !            42: 
        !            43: #if defined(BSD) && ! defined (HAVE_SYSVIPC)
        !            44: /* BSD code is very different from SYSV IPC code */
        !            45: 
        !            46: #include <sys/types.h>
        !            47: #include <sys/socket.h>
        !            48: #include <sys/un.h>
        !            49: #include <stdio.h>
        !            50: 
        !            51: main (argc, argv)
        !            52:      int argc;
        !            53:      char **argv;
        !            54: {
        !            55:   int s, n, i;
        !            56:   FILE *out;
        !            57:   struct sockaddr_un server;
        !            58:   char *homedir, *cwd, *str;
        !            59:   char string[BUFSIZ];
        !            60: 
        !            61:   char *getenv (), *getwd ();
        !            62: 
        !            63:   if (argc < 2)
        !            64:     {
        !            65:       printf ("Usage: %s [filename]\n", argv[0]);
        !            66:       exit (1);
        !            67:     }
        !            68: 
        !            69:   /* 
        !            70:    * Open up an AF_UNIX socket in this person's home directory
        !            71:    */
        !            72: 
        !            73:   if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
        !            74:     {
        !            75:       perror ("socket");
        !            76:       exit (1);
        !            77:     }
        !            78:   server.sun_family = AF_UNIX;
        !            79:   if ((homedir = getenv ("HOME")) == NULL)
        !            80:     {
        !            81:       fprintf (stderr, "No home directory\n");
        !            82:       exit (1);
        !            83:     }
        !            84:   strcpy (server.sun_path, homedir);
        !            85:   strcat (server.sun_path, "/.emacs_server");
        !            86:   if (connect (s, &server, strlen (server.sun_path) + 2) < 0)
        !            87:     {
        !            88:       perror ("connect");
        !            89:       exit (1);
        !            90:     }
        !            91:   if ((out = fdopen (s, "r+")) == NULL)
        !            92:     {
        !            93:       perror ("fdopen");
        !            94:       exit (1);
        !            95:     }
        !            96: 
        !            97:   cwd = getwd (string);
        !            98:   if (cwd == 0)
        !            99:     abort ();
        !           100: 
        !           101:   for (i = 1; i < argc; i++)
        !           102:     {
        !           103:       if (*argv[i] == '+')
        !           104:        {
        !           105:          char *p = argv[i] + 1;
        !           106:          while (*p >= '0' && *p <= '9') p++;
        !           107:          if (*p != 0)
        !           108:            fprintf (out, "%s/", cwd);
        !           109:        }
        !           110:       else if (*argv[i] != '/')
        !           111:        fprintf (out, "%s/", cwd);
        !           112:       fprintf (out, "%s ", argv[i]);
        !           113:     }
        !           114:   fprintf (out, "\n");
        !           115:   fflush (out);
        !           116: 
        !           117:   printf ("Waiting for Emacs...");
        !           118:   fflush (stdout);
        !           119: 
        !           120:   rewind (out); /* re-read the output */
        !           121:   str = fgets (string, BUFSIZ, out); 
        !           122: 
        !           123:   /* Now, wait for an answer and print any messages.  */
        !           124:   
        !           125:   while (str = fgets (string, BUFSIZ, out))
        !           126:     printf ("%s", str);
        !           127:   
        !           128:   exit (0);
        !           129: }
        !           130: 
        !           131: #else /* This is the SYSV IPC section */
        !           132: 
        !           133: #include <sys/types.h>
        !           134: #include <sys/ipc.h>
        !           135: #include <sys/msg.h>
        !           136: #include <stdio.h>
        !           137: 
        !           138: main (argc, argv)
        !           139:      int argc;
        !           140:      char **argv;
        !           141: {
        !           142:   int s;
        !           143:   key_t key;
        !           144:   struct msgbuf * msgp =
        !           145:       (struct msgbuf *) malloc (sizeof *msgp + BUFSIZ);
        !           146:   struct msqid_ds * msg_st;
        !           147:   char *homedir, *getenv (), buf[BUFSIZ];
        !           148:   char *getwd (), *getcwd (), gwdirb[BUFSIZ], *cwd;
        !           149:   if (argc < 2)
        !           150:     {
        !           151:       printf ("Usage: %s [filename]\n", argv[0]);
        !           152:       exit (1);
        !           153:     }
        !           154: 
        !           155:   /*
        !           156:    * Create a message queue using ~/.emacs_server as the path for ftok
        !           157:    */
        !           158:   if ((homedir = getenv ("HOME")) == NULL)
        !           159:     {
        !           160:       fprintf (stderr,"No home directory\n");
        !           161:       exit (1);
        !           162:     }
        !           163:   strcpy (buf, homedir);
        !           164:   strcat (buf, "/.emacs_server");
        !           165:   creat (buf, 0600);
        !           166:   key = ftok (buf, 1); /* unlikely to be anyone else using it */
        !           167:   s = msgget (key, 0600);
        !           168:   if (s == -1)
        !           169:     {
        !           170:       perror ("msgget");
        !           171:       exit (1);
        !           172:     }
        !           173: 
        !           174:   msgp->mtext[0] = 0;
        !           175:   argc--; argv++;
        !           176:   while (argc)
        !           177:     {
        !           178:       if (*argv[0] != '/')
        !           179:        {
        !           180:          char *val;
        !           181:          cwd = gwdirb; *cwd = '\0';
        !           182: #ifdef BSD
        !           183:          val = getwd (gwdirb);
        !           184: #else
        !           185:          val = getcwd (gwdirb, sizeof gwdirb);
        !           186: #endif
        !           187:          if (val != 0)
        !           188:            {
        !           189:              strcat (cwd, "/");
        !           190:            }
        !           191:          else
        !           192:            {
        !           193:              fprintf (stderr, cwd);
        !           194:              *cwd = '\0';
        !           195:            }
        !           196:          strcat (msgp->mtext, cwd);
        !           197:        }
        !           198:        
        !           199:       strcat (msgp->mtext, argv[0]);
        !           200:       strcat (msgp->mtext, " ");
        !           201:       argv++; argc--;
        !           202:     }
        !           203:   strcat (msgp->mtext, "\n");
        !           204:   msgp->mtype = 1;
        !           205:   if (msgsnd (s, msgp, strlen (msgp->mtext)+1, 1, 0) < 0)
        !           206:     {
        !           207:       perror ("msgsnd");
        !           208:       exit (1);
        !           209:     }
        !           210:   /*
        !           211:    * Now, wait for an answer
        !           212:    */
        !           213:   printf ("Waiting\n");
        !           214: 
        !           215:   msgrcv (s, msgp, BUFSIZ, getpid (), 0);      /* wait for anything back */
        !           216:   strcpy (buf, msgp->mtext);
        !           217: 
        !           218:   printf ("Got back: %s\n", buf);
        !           219:   exit (0);
        !           220: }
        !           221: 
        !           222: #endif /* IPC */
        !           223: 
        !           224: #endif /* BSD && IPC */
        !           225: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.