update.
[chise/xemacs-chise.git.1] / lib-src / gnuclient.c
index a89641f..311249e 100644 (file)
@@ -27,6 +27,17 @@ Boston, MA 02111-1307, USA.
  Please mail bugs and suggestions to the XEmacs maintainer.
 */
 
+/* #### This file should be a windows-mode, not console-mode program under
+   Windows. (i.e. its entry point should be WinMain.) gnuattach functionality,
+   to the extent it's used at all, should be retrieved using a script that
+   calls the i.exe wrapper program, to obtain stdio handles.
+
+   #### For that matter, both the functionality of gnuclient and gnuserv
+   should be merged into XEmacs itself using a -remote arg, just like
+   Netscape and other modern programs.
+
+   --ben */
+
 /*
  * This file incorporates new features added by Bob Weiner <weiner@mot.com>,
  * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>.
@@ -37,17 +48,20 @@ Boston, MA 02111-1307, USA.
  * ../etc/gnuserv.README relative to the directory containing this file)
  */
 
-#if 0
-/* Hand-munged RCS header */
-static char rcsid [] = "!Header: gnuclient.c,v 2.2 95/12/12 01:39:21 wing nene !";
+#ifdef  CYGWIN
+extern void cygwin_conv_to_posix_path(const char *path, char *posix_path);
 #endif
 
 #include "gnuserv.h"
+
+char gnuserv_version[] = "gnuclient version " GNUSERV_VERSION;
+
 #include "getopt.h"
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
+#include <sysfile.h>
 
 #ifdef HAVE_STRING_H
 #include <string.h>
@@ -89,9 +103,11 @@ tell_emacs_to_resume (int sig)
 
   /* Why is SYSV so retarded? */
   /* We want emacs to realize that we are resuming */
+#ifdef SIGCONT
   signal(SIGCONT, tell_emacs_to_resume);
+#endif
 
-  connect_type = make_connection (NULL, (u_short) 0, &s);
+  connect_type = make_connection (NULL, 0, &s);
 
   sprintf(buffer,"(gnuserv-eval '(resume-pid-console %d))", (int)getpid());
   send_string(s, buffer);
@@ -117,7 +133,7 @@ pass_signal_to_emacs (int sig)
 }
 
 void
-initialize_signals ()
+initialize_signals (void)
 {
   /* Set up signal handler to pass relevant signals to emacs process.
      We used to send SIGSEGV, SIGBUS, SIGPIPE, SIGILL and others to
@@ -129,8 +145,10 @@ initialize_signals ()
   signal (SIGWINCH, pass_signal_to_emacs);
 #endif
 
+#ifdef SIGCONT
   /* We want emacs to realize that we are resuming */
   signal (SIGCONT, tell_emacs_to_resume);
+#endif
 }
 
 
@@ -142,11 +160,11 @@ get_current_working_directory (void)
 {
   if (cp == NULL)
     {                          /* haven't calculated it yet */
-#ifdef BSD
-      if (getwd (cwd) == 0)
-#else /* !BSD */
+#ifdef HAVE_GETCWD
       if (getcwd (cwd,MAXPATHLEN) == NULL)
-#endif /* !BSD */
+#else
+      if (getwd (cwd) == 0)
+#endif /* HAVE_GETCWD */
        {
          perror (progname);
          fprintf (stderr, "%s: unable to get current working directory\n",
@@ -175,35 +193,57 @@ filename_expand (char *fullpath, char *filename)
   /* fullpath - returned full pathname */
   /* filename - filename to expand */
 {
+#ifdef  CYGWIN
+  char cygwinFilename[MAXPATHLEN+1];
+#endif
+
   int len;
+  fullpath[0] = fullpath[MAXPATHLEN] = '\0';
+
+#ifdef  CYGWIN
+  /*
+    If we're in cygwin, just convert it and let the unix stuff handle it.
+  */
+  cygwin_conv_to_posix_path(filename, cygwinFilename);
+  filename = cygwinFilename;
+#endif
 
-  fullpath[0] = '\0';
+  if (filename[0] && filename[0] == '/')
+     {
+       /* Absolute (unix-style) pathname.  Do nothing */
+       strncat (fullpath, filename, MAXPATHLEN);
+     }
+  else
+    {
+      /* Assume relative Unix style path.  Get the current directory
+       and prepend it.  FIXME: need to fix the case of DOS paths like
+       "\foo", where we need to get the current drive. */
 
-  if (filename[0] && filename[0] != '/')
-    {  /* relative filename */
-      strcat (fullpath, get_current_working_directory ());
+      strncat (fullpath, get_current_working_directory (), MAXPATHLEN);
       len = strlen (fullpath);
 
-      if (len > 0 && fullpath[len-1] == '/')   /* trailing slash already? */
-       ;                                       /* yep */
-      else
-       strcat (fullpath, "/");         /* nope, append trailing slash */
-    } /* if */
-
-  strcat (fullpath,filename);
+      /* If no trailing slash, add one */
+      if (len <= 0 || (fullpath[len - 1] != '/' && len < MAXPATHLEN))
+       {
+         strcat (fullpath, "/");
+         len++;
+       }
 
+      /* Don't forget to add the filename! */
+      strncat (fullpath, filename, MAXPATHLEN - len);
+    }
 } /* filename_expand */
 
 /* Encase the string in quotes, escape all the backslashes and quotes
    in string.  */
 static char *
-clean_string (CONST char *s)
+clean_string (const char *s)
 {
   int i = 0;
   char *p, *res;
 
   {
-    CONST char *const_p;
+    const char *const_p;
     for (const_p = s; *const_p; const_p++, i++)
       {
        if (*const_p == '\\' || *const_p == '\"')
@@ -257,14 +297,14 @@ clean_string (CONST char *s)
   over = 1;                                                               \
 } while (0)
 
-/* A strdup immitation. */
+/* A strdup imitation. */
 static char *
-my_strdup (CONST char *s)
+my_strdup (const char *s)
 {
-  char *new = malloc (strlen (s) + 1);
-  if (new)
-    strcpy (new, s);
-  return new;
+  char *new_s = (char *) malloc (strlen (s) + 1);
+  if (new_s)
+    strcpy (new_s, s);
+  return new_s;
 }
 
 int
@@ -292,11 +332,11 @@ main (int argc, char *argv[])
   char *remotearg;
   char thishost[HOSTNAMSZ];    /* this hostname */
   char remotepath[MAXPATHLEN+1]; /* remote pathname */
-  char *path;
   int rflg = 0;                        /* pathname given on cmdline */
   char *portarg;
-  u_short port = 0;            /* port to server */
+  unsigned short port = 0;     /* port to server */
 #endif /* INTERNET_DOMAIN_SOCKETS */
+  char *path;                   /* used indiscriminately */
 #ifdef SYSV_IPC
   struct msgbuf *msgp;         /* message */
 #endif /* SYSV_IPC */
@@ -402,7 +442,7 @@ main (int argc, char *argv[])
                  break;
                case 'r':
                  GET_ARGUMENT (remotearg, "-r");
-                 strcpy (remotepath, remotearg);
+                 strncpy (remotepath, remotearg, MAXPATHLEN);
                  rflg = 1;
                  break;
 #endif /* INTERNET_DOMAIN_SOCKETS */
@@ -417,11 +457,11 @@ main (int argc, char *argv[])
     {
       fprintf (stderr,
 #ifdef INTERNET_DOMAIN_SOCKETS
-              "usage: %s [-nw] [-display display] [-q] [-v] [-l library]\n"
+              "Usage: %s [-nw] [-display display] [-q] [-v] [-l library]\n"
                "       [-batch] [-f function] [-eval form]\n"
               "       [-h host] [-p port] [-r remote-path] [[+line] file] ...\n",
 #else /* !INTERNET_DOMAIN_SOCKETS */
-              "usage: %s [-nw] [-q] [-v] [-l library] [-f function] [-eval form] "
+              "Usage: %s [-nw] [-q] [-v] [-l library] [-f function] [-eval form] "
               "[[+line] path] ...\n",
 #endif /* !INTERNET_DOMAIN_SOCKETS */
               progname);
@@ -433,20 +473,21 @@ main (int argc, char *argv[])
               progname);
       exit (1);
     }
+#if defined(INTERNET_DOMAIN_SOCKETS)
   if (suppress_windows_system && hostarg)
     {
       fprintf (stderr, "%s: Remote editing is available only on X\n",
               progname);
       exit (1);
     }
-
+#endif
   *result = '\0';
   if (eval_function || eval_form || load_library)
     {
 #if defined(INTERNET_DOMAIN_SOCKETS)
       connect_type = make_connection (hostarg, port, &s);
 #else
-      connect_type = make_connection (NULL, (u_short) 0, &s);
+      connect_type = make_connection (NULL, 0, &s);
 #endif
       sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
       send_string (s, command);
@@ -484,7 +525,7 @@ main (int argc, char *argv[])
 #if defined(INTERNET_DOMAIN_SOCKETS)
       connect_type = make_connection (hostarg, port, &s);
 #else
-      connect_type = make_connection (NULL, (u_short) 0, &s);
+      connect_type = make_connection (NULL, 0, &s);
 #endif
       sprintf (command, "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
       send_string (s, command);
@@ -518,20 +559,20 @@ main (int argc, char *argv[])
 #if defined(INTERNET_DOMAIN_SOCKETS)
          connect_type = make_connection (hostarg, port, &s);
 #else
-         connect_type = make_connection (NULL, (u_short) 0, &s);
+         connect_type = make_connection (NULL, 0, &s);
 #endif
          send_string (s, "(gnuserv-eval '(emacs-pid))");
          send_string (s, EOT_STR);
 
          if (read_line (s, buffer) == 0)
            {
-             fprintf (stderr, "%s: Could not establish Emacs procces id\n",
+             fprintf (stderr, "%s: Could not establish Emacs process id\n",
                       progname);
              exit (1);
            }
-      /* Don't do disconnect_from_server becasue we have already read
+      /* Don't do disconnect_from_server because we have already read
         data, and disconnect doesn't do anything else. */
-#ifndef INTERNET_DOMAIN_SOCKETS
+#ifdef SYSV_IPC
          if (connect_type == (int) CONN_IPC)
            disconnect_from_ipc_server (s, msgp, FALSE);
 #endif /* !SYSV_IPC */
@@ -543,7 +584,7 @@ main (int argc, char *argv[])
 #if defined(INTERNET_DOMAIN_SOCKETS)
       connect_type = make_connection (hostarg, port, &s);
 #else
-      connect_type = make_connection (NULL, (u_short) 0, &s);
+      connect_type = make_connection (NULL, 0, &s);
 #endif
 
 #ifdef INTERNET_DOMAIN_SOCKETS
@@ -556,7 +597,7 @@ main (int argc, char *argv[])
                                         * to this machine */
              if ((ptr = getenv ("GNU_NODE")) != NULL)
                /* user specified a path */
-               strcpy (remotepath, ptr);
+               strncpy (remotepath, ptr, MAXPATHLEN);
            }
 #if 0  /* This is really bogus... re-enable it if you must have it! */
 #if defined (hp9000s300) || defined (hp9000s800)
@@ -599,9 +640,17 @@ main (int argc, char *argv[])
        }
       else /* !suppress_windows_system */
        {
-         if (display)
+         if (0)
+           ;
+#ifdef HAVE_X_WINDOWS
+         else if (display)
            sprintf (command, "(gnuserv-edit-files '(x %s) '(",
                     clean_string (display));
+#endif
+#ifdef HAVE_GTK
+         else if (display)
+           strcpy (command, "(gnuserv-edit-files '(gtk nil) '(");
+#endif
 #ifdef HAVE_MS_WINDOWS
          else
            sprintf (command, "(gnuserv-edit-files '(mswindows nil) '(");
@@ -626,7 +675,7 @@ main (int argc, char *argv[])
            }
          filename_expand (fullpath, argv[i]);
 #ifdef INTERNET_DOMAIN_SOCKETS
-         path = malloc (strlen (remotepath) + strlen (fullpath) + 1);
+         path = (char *) malloc (strlen (remotepath) + strlen (fullpath) + 1);
          sprintf (path, "%s%s", remotepath, fullpath);
 #else
          path = my_strdup (fullpath);