home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gnusrvr2.zip / gnuclient.c < prev    next >
C/C++ Source or Header  |  1997-05-15  |  9KB  |  325 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. /*
  18.  * This file incorporates new features added by Bob Weiner <weiner@mot.com>,
  19.  * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>.
  20.  * Please see the note at the end of the README file for details.
  21.  *
  22.  * (If gnuserv came bundled with your emacs, the README file is probably
  23.  * ../etc/gnuserv.README relative to the directory containing this file)
  24.  */
  25.  
  26. static char rcsid [] = "$Header: gnuclient.c,v 2.1 95/02/16 11:59:21 arup alpha $";
  27.  
  28.  
  29. #if defined( OS2_EMX )
  30. #define INC_DOSFILEMGR
  31. #include <os2.h>
  32. #endif
  33.  
  34.  
  35. #include "gnuserv.h"
  36.  
  37. #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
  38.     !defined(INTERNET_DOMAIN_SOCKETS)
  39. main ()
  40. {
  41.   fprintf (stderr,"Sorry, the Emacs server is only supported on systems that have\n");
  42.   fprintf (stderr,"Unix Domain sockets, Internet Domain sockets or System V IPC.\n");
  43.   exit (1);
  44. } /* main */
  45. #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
  46.  
  47. static char cwd[MAXPATHLEN+2];            /* current working directory when calculated */
  48. static char *cp = NULL;                /* ptr into valid bit of cwd above */
  49.  
  50.  
  51. /*
  52.   get_current_working_directory -- return the cwd.
  53. */
  54. char *get_current_working_directory()
  55. {
  56.     
  57. #if defined( OS2_EMX )
  58.   unsigned long drive_num, drive_mask;
  59. #endif  
  60.   
  61.   if (cp == NULL) {                /* haven't calculated it yet */
  62.  
  63. #if !defined( OS2_EMX )          
  64. #ifdef BSD
  65.     if (getwd(cwd) == 0) {
  66. #else /* !BSD */
  67.     if (getcwd(cwd,MAXPATHLEN) == NULL) {
  68. #endif /* !BSD */
  69.       perror(progname);
  70.       fprintf(stderr,"%s: unable to get current working directory\n",progname);
  71.       exit(1);
  72.     }; /* if */
  73.     
  74.     /* on some systems, cwd can look like '@machine/' ... */
  75.     /* ignore everything before the first '/' */
  76.     for (cp = cwd; *cp && *cp != '/'; ++cp)
  77.       ;                           
  78. #else
  79.  
  80.     DosQueryCurrentDisk( &drive_num, &drive_mask );
  81.  
  82.     *cwd = 'A' + drive_num - 1;
  83.     *(cwd + 1) = ':';
  84.         
  85.     if( getcwd( cwd + 2, MAXPATHLEN - 2 ) == NULL )
  86.     {
  87.         perror(progname);
  88.         fprintf(stderr,
  89.                 "%s: unable to get current working directory\n",
  90.                 progname);
  91.         exit(1);
  92.     }
  93.  
  94.     cp = cwd;
  95.     
  96. #endif
  97.     
  98.   }; /* if */
  99.  
  100.   return cp;
  101.     
  102. } /* get_current_working_directory */
  103.  
  104.  
  105. /*
  106.   filename_expand -- try to convert the given filename into a fully-qualified
  107.                pathname.
  108. */
  109. void filename_expand(fullpath,filename)
  110.      char *fullpath;                /* returned full pathname */
  111.      char *filename;                /* filename to expand */
  112. {
  113.   int len;
  114. #if defined( OS2_EMX )
  115.   char  *pwd, *fixer;
  116. #endif
  117.   
  118.   
  119.   fullpath[0] = '\0';
  120.  
  121.   
  122. #if defined( OS2_EMX )
  123.  
  124.   pwd = get_current_working_directory();
  125.  
  126.   if( ( filename[0] == '\\' && filename[1] != '\\' ) ||
  127.       ( filename[0] == '/' && filename[1] != '/' ) )
  128.   {
  129.       fullpath[0] = *pwd;
  130.       fullpath[1] = ':';
  131.       fullpath[2] = '\0';
  132.       strcat( fullpath, filename );
  133.   }
  134.   else if( filename[1] != ':' )
  135.   {
  136.       strcpy( fullpath, pwd );
  137.       strcat( fullpath, "/" );
  138.       strcat( fullpath, filename );
  139.   }
  140.   else
  141.   {
  142.       strcpy( fullpath, filename );
  143.   }
  144.  
  145.   /*
  146.    * Change all the \'s to /'s as Emacs doesn't
  147.    *    like the OS/2 \ ones.
  148.    */
  149.   fixer = fullpath;
  150.   for(; *fixer != NULL; fixer = fixer + 1 )
  151.       if( *fixer == '\\' )
  152.           *fixer = '/';
  153.   
  154.   return;
  155.   
  156. #else
  157.   
  158.   if(filename[0] && filename[0] != '/') {    /* relative filename */
  159.     
  160.     strcat(fullpath,get_current_working_directory());
  161.     len = strlen(fullpath);
  162.      
  163.     if (len > 0 && fullpath[len-1] == '/')    /* trailing slash already? */
  164.       ;                        /* yep */
  165.     else
  166.       strcat(fullpath,"/");            /* nope, append trailing slash */
  167.   }; /* if */
  168.  
  169.   strcat(fullpath,filename);
  170.   
  171. #endif /* OS2_EMX */
  172.   
  173. } /* filename_expand */
  174.  
  175. void
  176. main(argc,argv)
  177.      int argc;
  178.      char *argv[];
  179. {
  180.   int starting_line = 1;            /* line to start editing at */
  181.   char command[MAXPATHLEN+50];            /* emacs command buffer */
  182.   char fullpath[MAXPATHLEN+1];            /* full pathname to file */
  183.   int qflg = 0;                    /* quick edit, don't wait for 
  184.                          * user to finish */
  185.   int errflg = 0;                /* option error */
  186.   int c;                    /* char from getopt */
  187.   int s;                    /* socket / msqid to server */
  188.   int connect_type;                       /* CONN_UNIX, CONN_INTERNET, or
  189.                          * CONN_IPC */
  190. #ifdef INTERNET_DOMAIN_SOCKETS
  191.   char *hostarg = NULL;                /* remote hostname */
  192.   char thishost[HOSTNAMSZ];            /* this hostname */
  193.   char remotepath[MAXPATHLEN+1];        /* remote pathname */
  194.   int rflg = 0;                    /* pathname given on cmdline */
  195.   u_short portarg = 0;                /* port to server */
  196.   char *ptr;                    /* return from getenv */
  197. #endif /* INTERNET_DOMAIN_SOCKETS */
  198. #ifdef SYSV_IPC
  199.   struct msgbuf *msgp;                /* message */
  200. #endif /* SYSV_IPC */
  201.  
  202.   progname = argv[0];
  203.  
  204.   while ((c = getopt(argc, argv,
  205.  
  206. #ifdef INTERNET_DOMAIN_SOCKETS
  207.              "h:p:r:q"
  208. #else /* !INTERNET_DOMAIN_SOCKETS */
  209.              "q"
  210. #endif /* !INTERNET_DOMAIN_SOCKETS */
  211.  
  212.              )) != EOF)
  213.     switch (c) {
  214.     case 'q':                    /* quick mode specified */
  215.       qflg++;
  216.       break;
  217.  
  218. #ifdef INTERNET_DOMAIN_SOCKETS
  219.     case 'h':                /* server host name specified */
  220.       hostarg = optarg;
  221.       break;
  222.     case 'r':                /* remote path from server specifed */
  223.       strcpy(remotepath,optarg);
  224.       rflg++;
  225.       break;
  226.     case 'p':                /* port number specified */
  227.       portarg = atoi(optarg);
  228.       break;
  229. #endif /* INTERNET_DOMAIN_SOCKETS */
  230.  
  231.     case '?':
  232.       errflg++;
  233.     }; /* switch */
  234.  
  235.   if (errflg) {
  236.     fprintf(stderr,
  237. #ifdef INTERNET_DOMAIN_SOCKETS
  238.         "usage: %s [-q] [-h hostname] [-p port] [-r pathname] [[+line] path] ...\n",
  239. #else /* !INTERNET_DOMAIN_SOCKETS */
  240.         "usage: %s [-q] [[+line] path] ...\n",
  241. #endif /* !INTERNET_DOMAIN_SOCKETS */
  242.         progname);
  243.     exit (1);
  244.   }; /* if */
  245.  
  246. #ifdef INTERNET_DOMAIN_SOCKETS
  247.   connect_type = make_connection(hostarg, portarg, &s);
  248. #else
  249.   connect_type = make_connection(NULL, (u_short) 0, &s);
  250. #endif
  251.  
  252. #ifdef INTERNET_DOMAIN_SOCKETS
  253.   if (connect_type == (int) CONN_INTERNET) {
  254.     gethostname(thishost,HOSTNAMSZ);
  255.     if(!rflg) {                    /* attempt to generate a path 
  256.                          * to this machine */
  257.       if((ptr=getenv("GNU_NODE")) != NULL)    /* user specified a path */
  258.     strcpy(remotepath,ptr);
  259.     }
  260. #if 0  /* This is really bogus... re-enable it if you must have it! */
  261. #if defined(hp9000s300) || defined(hp9000s800)
  262.     else if (strcmp(thishost,hostarg)) {    /* try /net/thishost */
  263.       strcpy(remotepath,"/net/");         /* (this fails using internet 
  264.                            addresses) */
  265.       strcat(remotepath,thishost);
  266.     }
  267. #endif
  268. #endif
  269.   }  else {                    /* same machines, no need for path */
  270.       remotepath[0] = '\0';            /* default is the empty path */
  271.   }
  272. #endif /* INTERNET_DOMAIN_SOCKETS */
  273.  
  274. #ifdef SYSV_IPC
  275.   if ((msgp = (struct msgbuf *) 
  276.               malloc(sizeof *msgp + GSERV_BUFSZ)) == NULL) {
  277.     fprintf(stderr,"%s: not enough memory for message buffer\n",progname);
  278.     exit(1);
  279.   }; /* if */
  280.  
  281.   msgp->mtext[0] = '\0';            /* ready for later strcats */
  282. #endif /* SYSV_IPC */
  283.  
  284.   if (qflg) {
  285.     send_string(s,"(server-edit-files-quickly '(");
  286.   }
  287.   else {
  288.     send_string(s,"(server-edit-files '(");
  289.   };
  290.  
  291.   for (; optind < argc; optind++) {
  292.     if (*argv[optind] == '+')
  293.       starting_line = atoi(argv[optind]);
  294.     else {
  295.  
  296.       filename_expand(fullpath,argv[optind]);
  297.       sprintf(command,"(%d . \"%s%s\")",starting_line,
  298.  
  299. #ifdef INTERNET_DOMAIN_SOCKETS
  300.           remotepath,
  301. #else /* !INTERNET_DOMAIN_SOCKETS */
  302.           "",
  303. #endif
  304.           fullpath);
  305.       send_string(s,command);
  306.       starting_line = 1;
  307.     }; /* else */
  308.   }; /* for */
  309.  
  310.   send_string(s,"))");
  311.  
  312. #ifdef SYSV_IPC
  313.   if (connect_type == (int) CONN_IPC)
  314.     disconnect_from_ipc_server(s,msgp,FALSE);
  315. #else /* !SYSV_IPC */
  316.   if (connect_type != (int) CONN_IPC)
  317.     disconnect_from_server(s,FALSE);
  318. #endif /* !SYSV_IPC */
  319.  
  320.   exit(0);
  321.  
  322. } /* main */
  323.  
  324. #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
  325.