home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnusrvr2.zip / gnudoit.c < prev    next >
C/C++ Source or Header  |  1995-02-16  |  4KB  |  146 lines

  1. /* -*-C-*-
  2.  Client code to locally and remotely evaluate lisp forms using GNU Emacs.
  3.  
  4.  This file is part of GNU Emacs.
  5.  
  6.  Copying is permitted under those conditions described by the GNU
  7.  General Public License.
  8.  
  9.  Copyright (C) 1989 Free Software Foundation, Inc.
  10.  
  11.  Author: Andy Norman (ange@hplb.hpl.hp.com).
  12.  
  13.  Please mail bugs and suggestions to the author at the above address.
  14. */
  15.  
  16. /*
  17.  * This file incorporates new features added by Bob Weiner <weiner@mot.com>,
  18.  * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>.
  19.  * Please see the note at the end of the README file for details.
  20.  *
  21.  * (If gnuserv came bundled with your emacs, the README file is probably
  22.  * ../etc/gnuserv.README relative to the directory containing this file)
  23.  */
  24.  
  25. static char rcsid [] = "$Header: gnudoit.c,v 2.1 95/02/16 11:59:02 arup alpha $";
  26.  
  27. #include "gnuserv.h"
  28.  
  29. #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && !defined(INTERNET_DOMAIN_SOCKETS)
  30. main ()
  31. {
  32.   fprintf (stderr,"Sorry, the Emacs server is only supported on systems that have\n");
  33.   fprintf (stderr,"Unix Domain sockets, Internet Domain sockets or System V IPC.\n");
  34.   exit (1);
  35. } /* main */
  36. #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
  37.  
  38. void
  39. main(argc,argv)
  40.      int argc;
  41.      char *argv[];
  42. {
  43.   int qflg = 0;                    /* don't wait around for 
  44.                          * gnu emacs to eval cmd */
  45.   int errflg = 0;                /* option error */
  46.   int c;                    /* char from getopt */
  47.   int s;                    /* socket / msqid to server */
  48.   int connect_type;                       /* CONN_UNIX, CONN_INTERNET, or
  49.                          * CONN_IPC */
  50. #ifdef INTERNET_DOMAIN_SOCKETS
  51.   char *hostarg = NULL;             /* remote hostname argument */
  52.   u_short portarg = 0;                /* port number */
  53. #endif /* INTERNET_DOMAIN_SOCKETS */
  54. #ifdef SYSV_IPC
  55.   struct msgbuf *msgp;                /* message */
  56. #endif /* SYSV_IPC */
  57.  
  58.   progname = argv[0];
  59.  
  60.   while ((c = getopt(argc, argv,
  61. #ifdef INTERNET_DOMAIN_SOCKETS
  62.              "qh:p:"
  63. #else /* !INTERNET_DOMAIN_SOCKETS */
  64.              "q"
  65. #endif /* !INTERNET_DOMAIN_SOCKETS */
  66.              )) != EOF)
  67.     switch (c) {
  68. #ifdef INTERNET_DOMAIN_SOCKETS
  69.     case 'h':                    /* host name specified */
  70.       hostarg = optarg;
  71.       break;
  72.     case 'p':                    /* port number specified */
  73.       portarg = atoi(optarg);
  74.       break;
  75. #endif /* INTERNET_DOMAIN_SOCKETS */
  76.     case 'q':                    /* quick mode specified */
  77.       qflg++;
  78.       break;
  79.     case '?':
  80.       errflg++;
  81.     }; /* switch */
  82.  
  83.   if (errflg) {
  84.     fprintf(stderr,
  85. #ifdef INTERNET_DOMAIN_SOCKETS
  86.         "usage: %s [-q] [-h hostname] [-p port] [sexpr]...\n",
  87. #else /* !INTERNET_DOMAIN_SOCKETS */
  88.         "usage: %s [-q] [sexpr]...\n",
  89. #endif /* !INTERNET_DOMAIN_SOCKETS */
  90.         progname);
  91.     exit (1);
  92.   }; /* if */
  93.  
  94. #ifdef INTERNET_DOMAIN_SOCKETS
  95.   connect_type = make_connection(hostarg, portarg, &s);
  96. #else
  97.   connect_type = make_connection(NULL, (u_short) 0, &s);
  98. #endif
  99.  
  100. #ifdef SYSV_IPC
  101.   if ((msgp = (struct msgbuf *) 
  102.               malloc(sizeof *msgp + GSERV_BUFSZ)) == NULL) {
  103.     fprintf(stderr,"%s: not enough memory for message buffer\n",progname);
  104.     exit(1);
  105.   }; /* if */
  106.  
  107.   msgp->mtext[0] = '\0';            /* ready for later strcats */
  108. #endif /* SYSV_IPC */
  109.  
  110.   if (qflg) {
  111.     send_string(s,"(server-eval-quickly '(progn ");
  112.   }
  113.   else {
  114.     send_string(s,"(server-eval '(progn ");
  115.   };
  116.  
  117.   if (optind < argc) {
  118.     for (; optind < argc; optind++) {
  119.       send_string(s, argv[optind]);
  120.       send_string(s, " ");
  121.     }
  122.   } else {
  123.     /* no sexp on the command line, so read it from stdin */
  124.     int nb;
  125.     char buf[GSERV_BUFSZ];
  126.     while ((nb = read(fileno(stdin), buf, GSERV_BUFSZ-1)) > 0) {
  127.       buf[nb] = '\0';
  128.       send_string(s, buf);
  129.     }
  130.   }
  131.   send_string(s,"))");
  132.  
  133. #ifdef SYSV_IPC
  134.   if (connect_type == (int) CONN_IPC)
  135.     disconnect_from_ipc_server(s,msgp,!qflg);
  136.   else
  137. #else /* !SYSV_IPC */
  138.     disconnect_from_server(s,!qflg);
  139. #endif /* !SYSV_IPC */
  140.  
  141.   exit(0);
  142.  
  143. } /* main */
  144.  
  145. #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
  146.