Loads of build chain fixups from Sebastian
[sxemacs] / lib-src / gnuclient.c
index 576581e..203f76d 100644 (file)
@@ -20,22 +20,11 @@ You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
  Author: Andy Norman (ange@hplb.hpl.hp.com), based on
-         'etc/emacsclient.c' from the GNU Emacs 18.52 distribution.
+        'etc/emacsclient.c' from the GNU Emacs 18.52 distribution.
 
  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>.
@@ -70,6 +59,13 @@ char gnuserv_version[] = "gnuclient version " GNUSERV_VERSION;
 
 #include <signal.h>
 
+#define xstrncpy(d_,s_,l_)                     \
+       do {                                    \
+               char* dst_=d_;                  \
+               dst_[0]='\0';                   \
+               strncat((dst_),(s_),(l_)-1);    \
+       } while(0)
+
 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
     !defined(INTERNET_DOMAIN_SOCKETS)
 int main(int argc, char *argv[])
@@ -106,9 +102,8 @@ static void tell_emacs_to_resume(int sig)
 
        connect_type = make_connection(NULL, 0, &s);
 
-       sz = snprintf(buffer, sizeof(buffer), "(gnuserv-eval '(resume-pid-console %d))",
-                     (int)getpid());
-       assert(sz>=0 && (size_t)sz<sizeof(buffer));
+       SNPRINTF(sz, buffer, sizeof(buffer),
+                "(gnuserv-eval '(resume-pid-console %d))", (int)getpid());
        send_string(s, buffer);
 
 #ifdef SYSV_IPC
@@ -125,7 +120,7 @@ pass_signal_to_emacs(int sig)
 {
        if (kill(emacs_pid, sig) == -1) {
                fprintf(stderr,
-                       "gnuattach: Could not pass signal to emacs process\n");
+                       "gnuclient: Could not pass signal to emacs process\n");
                exit(1);
        }
        initialize_signals();
@@ -182,34 +177,39 @@ static char *get_current_working_directory(void)
 
 /*
   filename_expand -- try to convert the given filename into a fully-qualified
-                    pathname.
+                    pathname.
 */
-static void filename_expand(char *fullpath, char *filename, size_t fullsize)
-  /* fullpath - returned full pathname */
-  /* filename - filename to expand */
+static void
+filename_expand(char *fullpath, char *filename, size_t fullsize)
 {
-       int len;
+/* fullpath - returned full pathname */
+/* filename - filename to expand */
+       size_t len;
        fullpath[0] = '\0';
 
        if (filename[0] && filename[0] == '/') {
                /* Absolute (unix-style) pathname.  Do nothing */
-               strncat(fullpath, filename, fullsize-1);
+               strncat(fullpath, filename, fullsize-1);
        } 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. */
-
-               strncat(fullpath, get_current_working_directory(), fullsize-1);
+                * and prepend it.  FIXME: need to fix the case of DOS paths
+                * like "\foo", where we need to get the current drive. */
+               strncat(fullpath, get_current_working_directory(), fullsize-1);
                len = strlen(fullpath);
 
-               if (len > 0 && fullpath[len - 1] == '/')        /* trailing slash already? */
-                       ;       /* yep */
-               else if (len >=0 && (size_t)len < fullsize-1)
-                       strcat(fullpath, "/");  /* nope, append trailing slash */
+               /* trailing slash already? */
+               if (len > 0 && fullpath[len - 1] == '/') {
+                       /* yep */
+                       ;
+               } else if (len < fullsize-1) {
+                       /* nope, append trailing slash */
+                       strcat(fullpath, "/");
+               }
                /* Don't forget to add the filename! */
-               strncat(fullpath, filename, fullsize-len-1);
+               strncat(fullpath, filename, fullsize - len - 1);
        }
-}                              /* filename_expand */
+       return;
+}
 
 /* Encase the string in quotes, escape all the backslashes and quotes
    in string.  */
@@ -217,21 +217,20 @@ static char *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 == '\"')
-                               ++i;
-                       else if (*const_p == '\004')
-                               i += 3;
-               }
+
+       for (const_p = s; *const_p; const_p++, i++) {
+               if (*const_p == '\\' || *const_p == '\"')
+                       ++i;
+               else if (*const_p == '\004')
+                       i += 3;
        }
 
        p = res = (char *)malloc(i + 2 + 1);
        *p++ = '\"';
-       for (; *s; p++, s++) {
-               switch (*s) {
+       for (const_p = s; *const_p; p++, const_p++) {
+               switch (*const_p) {
                case '\\':
                        *p++ = '\\';
                        *p = '\\';
@@ -247,7 +246,7 @@ static char *clean_string(const char *s)
                        *p = 'd';
                        break;
                default:
-                       *p = *s;
+                       *p = *const_p;
                }
        }
        *p++ = '\"';
@@ -407,7 +406,7 @@ int main(int argc, char *argv[])
                                        break;
                                case 'r':
                                        GET_ARGUMENT(remotearg, "-r");
-                                       strncpy(remotepath, remotearg, sizeof(remotepath));
+                                       xstrncpy(remotepath, remotearg, sizeof(remotepath));
                                        remotepath[sizeof(remotepath)-1]='\0';
                                        rflg = 1;
                                        break;
@@ -451,9 +450,8 @@ int main(int argc, char *argv[])
 #else
                connect_type = make_connection(NULL, 0, &s);
 #endif
-               sz = snprintf(command, sizeof(command), "(gnuserv-eval%s '(progn ",
-                        quick ? "-quickly" : "");
-               assert(sz>=0 && (size_t)sz<sizeof(command));
+               SNPRINTF(sz, command, sizeof(command),
+                        "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
                send_string(s, command);
                if (load_library) {
                        send_string(s, "(load-library ");
@@ -487,10 +485,8 @@ int main(int argc, char *argv[])
 #else
                connect_type = make_connection(NULL, 0, &s);
 #endif
-               sz = snprintf(command, sizeof(command),
-                             "(gnuserv-eval%s '(progn ",
-                             quick ? "-quickly" : "");
-               assert(sz>=0 && (size_t)sz<sizeof(command));
+               SNPRINTF(sz, command, sizeof(command),
+                        "(gnuserv-eval%s '(progn ", quick ? "-quickly" : "");
                send_string(s, command);
 
                while ((nb = read(fileno(stdin), buffer, GSERV_BUFSZ - 1)) > 0) {
@@ -555,7 +551,7 @@ int main(int argc, char *argv[])
                                         * to this machine */
                                if ((ptr = getenv("GNU_NODE")) != NULL) {
                                        /* user specified a path */
-                                       strncpy(remotepath, ptr, sizeof(remotepath)-1);
+                                       xstrncpy(remotepath, ptr, sizeof(remotepath)-1);
                                        remotepath[sizeof(remotepath)-1]='\0';
                                }
                        }
@@ -594,28 +590,18 @@ int main(int argc, char *argv[])
                                        progname);
                                exit(1);
                        }
-                       sz = snprintf(
-                               command, sizeof(command),
-                               "(gnuserv-edit-files '(tty %s %s %d) '(",
-                               clean_string(tty), clean_string(term),
-                               (int)pid);
-                       assert(sz >= 0 && (size_t)sz < sizeof(command));
+                       SNPRINTF(sz, command, sizeof(command),
+                                "(gnuserv-edit-files '(tty %s %s %d) '(",
+                                clean_string(tty), clean_string(term),
+                                (int)pid);
                } else {        /* !suppress_windows_system */
 
                        if (0) ;
 #ifdef HAVE_X_WINDOWS
                        else if (display) {
-                               sz = snprintf(
-                                       command, sizeof(command),
-                                       "(gnuserv-edit-files '(x %s) '(",
-                                       clean_string(display));
-                               assert(sz >= 0 && (size_t)sz < sizeof(command));
-                       }
-#endif
-#ifdef HAVE_GTK
-                       else if (display) {
-                               strcpy(command,
-                                      "(gnuserv-edit-files '(gtk nil) '(");
+                               SNPRINTF(sz, command, sizeof(command),
+                                        "(gnuserv-edit-files '(x %s) '(",
+                                        clean_string(display));
                        }
 #endif
                }               /* !suppress_windows_system */
@@ -640,27 +626,21 @@ int main(int argc, char *argv[])
 #ifdef INTERNET_DOMAIN_SOCKETS
                        msz = strlen(remotepath) + strlen(fullpath) + 1;
                        path = (char*)malloc(msz);
-                       sz = snprintf(path, msz, "%s%s", remotepath, fullpath);
-                       assert(sz >= 0 && (size_t)sz < msz);
+                       SNPRINTF(sz, path, msz, "%s%s", remotepath, fullpath);
 #else  /* !INTERNET_DOMAIN_SOCKETS */
                        path = my_strdup(fullpath);
 #endif /* INTERNET_DOMAIN_SOCKETS */
-                       sz = snprintf(
-                               command, sizeof(command),
-                               "(%d . %s)", starting_line,
-                               clean_string(path));
-                       assert(sz >= 0 && (size_t)sz < sizeof(command));
+                       SNPRINTF(sz, command, sizeof(command),
+                               "(%d . %s)", starting_line, clean_string(path));
                        send_string(s, command);
                        free(path);
                }
-               
-               sz = snprintf(
-                       command, sizeof(command), ")%s%s",
-                       (quick || (nofiles && !suppress_windows_system))
-                       ? " 'quick"
-                       : "",
-                       view ? " 'view" : "");
-               assert(sz >= 0 && (size_t)sz < sizeof(command));
+
+               SNPRINTF(sz, command, sizeof(command), ")%s%s",
+                        (quick || (nofiles && !suppress_windows_system))
+                        ? " 'quick"
+                        : "",
+                        view ? " 'view" : "");
                send_string(s, command);
                send_string(s, ")");