home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / packages / gnuserv / gnuclient.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-23  |  6.7 KB  |  250 lines

  1. /* -*-C-*-
  2.  Client code to allow local and remote editing of files by 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), based on 
  12.          'etc/emacsclient.c' from the GNU Emacs 18.52 distribution.
  13.  
  14.  Please mail bugs and suggestions to the author at the above address.
  15. */
  16.  
  17. static char rcsid [] = "$Header: gnuclient.c,v 1.8 89/07/24 12:46:46 ange Exp $";
  18.  
  19. #include "gnuserv.h"
  20.  
  21. #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && !defined(INTERNET_DOMAIN_SOCKETS)
  22. main ()
  23. {
  24.   fprintf (stderr,"Sorry, the Emacs server is only supported on systems that have\n");
  25.   fprintf (stderr,"Unix Domain sockets, Internet Domain sockets or System V IPC.\n");
  26.   exit (1);
  27. } /* main */
  28. #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
  29.  
  30. static char cwd[MAXPATHLEN+2];            /* current working directory when calculated */
  31. static char *cp = NULL;                /* ptr into valid bit of cwd above */
  32.  
  33.  
  34. /*
  35.   get_current_working_directory -- return the cwd.
  36. */
  37. char *get_current_working_directory()
  38. {
  39.   if (cp == NULL) {                /* haven't calculated it yet */
  40. #ifdef BSD
  41.     if (getwd(cwd) == 0) {
  42. #else /* !BSD */
  43.     if (getcwd(cwd,MAXPATHLEN) == NULL) {
  44. #endif /* !BSD */
  45.       perror(progname);
  46.       fprintf(stderr,"%s: unable to get current working directory\n",progname);
  47.       exit(1);
  48.     }; /* if */
  49.     
  50.     /* on some systems, cwd can look like '@machine/' ... */
  51.     /* ignore everything before the first '/' */
  52.     for (cp = cwd; *cp && *cp != '/'; ++cp)
  53.       ;                           
  54.  
  55.   }; /* if */
  56.  
  57.   return cp;
  58.     
  59. } /* get_current_working_directory */
  60.  
  61.  
  62. /*
  63.   filename_expand -- try to convert the given filename into a fully-qualified
  64.                pathname.
  65. */
  66. void filename_expand(fullpath,filename)
  67.      char *fullpath;                /* returned full pathname */
  68.      char *filename;                /* filename to expand */
  69. {
  70.   int len;
  71.  
  72.   fullpath[0] = '\0';
  73.   
  74.   if(filename[0] && filename[0] != '/') {    /* relative filename */
  75.     
  76.     strcat(fullpath,get_current_working_directory());
  77.     len = strlen(fullpath);
  78.      
  79.     if (len > 0 && fullpath[len-1] == '/')    /* trailing slash already? */
  80.       ;                        /* yep */
  81.     else
  82.       strcat(fullpath,"/");            /* nope, append trailing slash */
  83.   }; /* if */
  84.  
  85.   strcat(fullpath,filename);
  86.  
  87. } /* filename_expand */
  88.  
  89.  
  90. main(argc,argv)
  91.      int argc;
  92.      char *argv[];
  93. {
  94.   int starting_line = 1;            /* line to start editing at */
  95.   char command[MAXPATHLEN+50];            /* emacs command buffer */
  96.   char fullpath[MAXPATHLEN+1];            /* full pathname to file */
  97.   int qflg = 0;                    /* quick edit, don't wait for user to finish */
  98.   int errflg = 0;                /* option error */
  99.   int c;                    /* char from getopt */
  100.   int s;                    /* socket / msqid to server */
  101. #ifdef INTERNET_DOMAIN_SOCKETS
  102.   char thishost[HOSTNAMSZ];            /* this hostname */
  103.   char remotehost[HOSTNAMSZ];            /* remote hostname */
  104.   char remotepath[MAXPATHLEN+1];        /* remote pathname */
  105.   int hflg = 0;                    /* hostname given on command line */
  106.   int rflg = 0;                    /* pathname given on command line */
  107.   u_short port = 0;                /* port to server */
  108.   char *ptr;                    /* return from getenv */
  109. #endif /* INTERNET_DOMAIN_SOCKETS */
  110. #ifdef SYSV_IPC
  111.   struct msgbuf *msgp;                /* message */
  112. #endif /* SYSV_IPC */
  113.  
  114.   progname = argv[0];
  115.  
  116.   while ((c = getopt(argc, argv,
  117.  
  118. #ifdef INTERNET_DOMAIN_SOCKETS
  119.              "h:p:r:q"
  120. #else /* !INTERNET_DOMAIN_SOCKETS */
  121.              "q"
  122. #endif /* !INTERNET_DOMAIN_SOCKETS */
  123.  
  124.              )) != EOF)
  125.     switch (c) {
  126.     case 'q':                    /* quick mode specified */
  127.       qflg++;
  128.       break;
  129.  
  130. #ifdef INTERNET_DOMAIN_SOCKETS
  131.     case 'h':                    /* server host name specified */
  132.       strcpy(remotehost,optarg);
  133.       hflg++;
  134.       break;
  135.     case 'r':                    /* remote path from server specifed */
  136.       strcpy(remotepath,optarg);
  137.       rflg++;
  138.       break;
  139.     case 'p':                    /* port number specified */
  140.       port = atoi(optarg);
  141.       break;
  142. #endif /* INTERNET_DOMAIN_SOCKETS */
  143.  
  144.     case '?':
  145.       errflg++;
  146.     }; /* switch */
  147.  
  148.   if (errflg) {
  149.     fprintf(stderr,
  150. #ifdef INTERNET_DOMAIN_SOCKETS
  151.         "usage: %s [-q] [-h hostname] [-p port] [-r pathname] [[+line] path] ...\n",
  152. #else /* !INTERNET_DOMAIN_SOCKETS */
  153.         "usage: %s [-q] [[+line] path] ...\n",
  154. #endif /* !INTERNET_DOMAIN_SOCKETS */
  155.         progname);
  156.     exit (1);
  157.   }; /* if */
  158.  
  159. #ifdef INTERNET_DOMAIN_SOCKETS
  160.   gethostname(thishost,HOSTNAMSZ);
  161.  
  162.   if (!hflg) {                    /* attempt to find the server host */
  163.     if((ptr=getenv("GNU_HOST")) != NULL)    /* user specified a host */
  164.       strcpy(remotehost,ptr);
  165.     else                    /* use this host by default */
  166.       strcpy(remotehost,thishost);
  167.   }; /* if */
  168.  
  169.   if(!rflg) {                    /* attempt to generate a path to this machine */
  170.     if((ptr=getenv("GNU_NODE")) != NULL)    /* user specified a path */
  171.       strcpy(remotepath,ptr);
  172.  
  173. #if defined(hp9000s300) || defined(hp9000s800)
  174.     else if (strcmp(thishost,remotehost)) {    /* try /net/thishost */
  175.       strcpy(remotepath,"/net/");         /* (this fails using internet addresses) */
  176.       strcat(remotepath,thishost);
  177.     }
  178. #endif
  179.  
  180.     else                    /* same machines, no need for path */
  181.       remotepath[0] = '\0';            /* default is the empty path */
  182.   }; /* if */
  183.  
  184.   if (port == 0 && (ptr=getenv("GNU_PORT")) != NULL)
  185.     port = atoi(ptr);
  186.  
  187. #ifdef UNIX_DOMAIN_SOCKETS
  188.   if (!strcmp(remotehost,"unix"))
  189.     s = connect_to_unix_server();
  190.   else
  191. #endif /* UNIX_DOMAIN_SOCKETS */
  192.     s = connect_to_internet_server(remotehost,port);
  193. #else  /* !INTERNET_DOMAIN_SOCKETS */
  194.  
  195. #ifdef UNIX_DOMAIN_SOCKETS
  196.   s = connect_to_unix_server();
  197. #endif /* UNIX_DOMAIN_SOCKETS */
  198.  
  199. #ifdef SYSV_IPC
  200.   if ((msgp = (struct msgbuf *) malloc(sizeof *msgp + BUFSIZ)) == NULL) {
  201.     fprintf(stderr,"%s: not enough memory for message buffer\n",progname);
  202.     exit(1);
  203.   }; /* if */
  204.  
  205.   msgp->mtext[0] = '\0';            /* ready for later strcats */
  206.   s = connect_to_ipc_server();
  207. #endif /* SYSV_IPC */
  208.  
  209. #endif /* !INTERNET_DOMAIN_SOCKETS */
  210.  
  211.   if (qflg) {
  212.     send_string(s,"(server-edit-files-quickly '(");
  213.   }
  214.   else {
  215.     send_string(s,"(server-edit-files '(");
  216.   };
  217.  
  218.   for (; optind < argc; optind++) {
  219.     if (*argv[optind] == '+')
  220.       starting_line = atoi(argv[optind]);
  221.     else {
  222.  
  223.       filename_expand(fullpath,argv[optind]);
  224.       sprintf(command,"(%d . \"%s%s\")",starting_line,
  225.  
  226. #ifdef INTERNET_DOMAIN_SOCKETS
  227.           remotepath,
  228. #else /* !INTERNET_DOMAIN_SOCKETS */
  229.           "",
  230. #endif
  231.           fullpath);
  232.       send_string(s,command);
  233.       starting_line = 1;
  234.     }; /* else */
  235.   }; /* for */
  236.  
  237.   send_string(s,"))");
  238.  
  239. #ifdef SYSV_IPC
  240.   disconnect_from_ipc_server(s,msgp,FALSE);
  241. #else /* !SYSV_IPC */
  242.   disconnect_from_server(s,FALSE);
  243. #endif /* !SYSV_IPC */
  244.  
  245.   exit(0);
  246.  
  247. } /* main */
  248.  
  249. #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
  250.