Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / nt / cmdproxy.c
index d01e7f3..9a98e1e 100644 (file)
@@ -1,8 +1,7 @@
 /* Proxy shell designed for use with Emacs on Windows 95 and NT.
-   Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005,
-      2006, 2007  Free Software Foundation, Inc.
+   Copyright (C) 1997, 2001-2011  Free Software Foundation, Inc.
 
-   Accepts subset of Unix sh(1) command-line options, for compatability
+   Accepts subset of Unix sh(1) command-line options, for compatibility
    with elisp code written for Unix.  When possible, executes external
    programs directly (a common use of /bin/sh by Emacs), otherwise
    invokes the user-specified command processor to handle built-in shell
 
 This file is part of GNU Emacs.
 
-GNU Emacs is free software; you can redistribute it and/or modify
+GNU Emacs is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -26,9 +25,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU Emacs; see the file COPYING.  If not, write to
-the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include <windows.h>
 
@@ -37,17 +34,19 @@ Boston, MA 02110-1301, USA.  */
 #include <stdlib.h>  /* getenv */
 #include <string.h>  /* strlen */
 
+/* We don't want to include stdio.h because we are already duplicating
+   lots of it here */
+extern int _snprintf (char *buffer, size_t count, const char *format, ...);
 
 /*******  Mock C library routines  *********************************/
 
 /* These routines are used primarily to minimize the executable size.  */
 
-#define stdin  GetStdHandle (STD_INPUT_HANDLE)
 #define stdout GetStdHandle (STD_OUTPUT_HANDLE)
 #define stderr GetStdHandle (STD_ERROR_HANDLE)
 
 int
-vfprintf(HANDLE hnd, char * msg, va_list args)
+vfprintf (HANDLE hnd, const char * msg, va_list args)
 {
   DWORD bytes_written;
   char buf[1024];
@@ -57,7 +56,7 @@ vfprintf(HANDLE hnd, char * msg, va_list args)
 }
 
 int
-fprintf(HANDLE hnd, char * msg, ...)
+fprintf (HANDLE hnd, const char * msg, ...)
 {
   va_list args;
   int rc;
@@ -70,7 +69,7 @@ fprintf(HANDLE hnd, char * msg, ...)
 }
 
 int
-printf(char * msg, ...)
+printf (const char * msg, ...)
 {
   va_list args;
   int rc;
@@ -83,7 +82,7 @@ printf(char * msg, ...)
 }
 
 void
-fail (char * msg, ...)
+fail (const char * msg, ...)
 {
   va_list args;
 
@@ -95,7 +94,7 @@ fail (char * msg, ...)
 }
 
 void
-warn (char * msg, ...)
+warn (const char * msg, ...)
 {
   va_list args;
 
@@ -121,15 +120,15 @@ canon_filename (char *fname)
   return fname;
 }
 
-char *
-skip_space (char *str)
+const char *
+skip_space (const char *str)
 {
   while (isspace (*str)) str++;
   return str;
 }
 
-char *
-skip_nonspace (char *str)
+const char *
+skip_nonspace (const char *str)
 {
   while (*str && !isspace (*str)) str++;
   return str;
@@ -139,9 +138,9 @@ int escape_char = '\\';
 
 /* Get next token from input, advancing pointer.  */
 int
-get_next_token (char * buf, char ** pSrc)
+get_next_token (char * buf, const char ** pSrc)
 {
-  char * p = *pSrc;
+  const char * p = *pSrc;
   char * o = buf;
 
   p = skip_space (p);
@@ -208,7 +207,7 @@ get_next_token (char * buf, char ** pSrc)
   else
     {
       /* Next token is delimited by whitespace.  */
-      char * p1 = skip_nonspace (p);
+      const char * p1 = skip_nonspace (p);
       memcpy (o, p, p1 - p);
       o += (p1 - p);
       *o = '\0';
@@ -223,9 +222,9 @@ get_next_token (char * buf, char ** pSrc)
 /* Search for EXEC file in DIR.  If EXEC does not have an extension,
    DIR is searched for EXEC with the standard extensions appended.  */
 int
-search_dir (char *dir, char *exec, int bufsize, char *buffer)
+search_dir (const char *dir, const char *exec, int bufsize, char *buffer)
 {
-  char *exts[] = {".bat", ".cmd", ".exe", ".com"};
+  const char *exts[] = {".bat", ".cmd", ".exe", ".com"};
   int n_exts = sizeof (exts) / sizeof (char *);
   char *dummy;
   int i, rc;
@@ -245,13 +244,13 @@ search_dir (char *dir, char *exec, int bufsize, char *buffer)
    any file extensions.  If an absolute name for PROG cannot be found,
    return NULL.  */
 char *
-make_absolute (char *prog)
+make_absolute (const char *prog)
 {
   char absname[MAX_PATH];
   char dir[MAX_PATH];
   char curdir[MAX_PATH];
-  char *p, *fname;
-  char *path;
+  char *p, *path;
+  const char *fname;
   int i;
 
   /* At least partial absolute path specified; search there.  */
@@ -343,7 +342,7 @@ console_event_handler (DWORD event)
     case CTRL_BREAK_EVENT:
       if (!interactive)
        {
-         /* Both command.com and cmd.exe have the annoying behaviour of
+         /* Both command.com and cmd.exe have the annoying behavior of
             prompting "Terminate batch job (y/n)?" when interrupted
             while running a batch file, even if running in
             non-interactive (-c) mode.  Try to make up for this
@@ -371,7 +370,7 @@ console_event_handler (DWORD event)
 /* Change from normal usage; return value indicates whether spawn
    succeeded or failed - program return code is returned separately.  */
 int
-spawn (char * progname, char * cmdline, char * dir, int * retcode)
+spawn (const char *progname, char *cmdline, const char *dir, int *retcode)
 {
   BOOL success = FALSE;
   SECURITY_ATTRIBUTES sec_attrs;
@@ -409,7 +408,7 @@ spawn (char * progname, char * cmdline, char * dir, int * retcode)
 
 /* Return size of current environment block.  */
 int
-get_env_size ()
+get_env_size (void)
 {
   char * start = GetEnvironmentStrings ();
   char * tmp = start;
@@ -469,8 +468,8 @@ main (int argc, char ** argv)
   /* Due to problems with interaction between API functions that use "OEM"
      codepage vs API functions that use the "ANSI" codepage, we need to
      make things consistent by choosing one and sticking with it.  */
-  SetConsoleCP (GetACP());
-  SetConsoleOutputCP (GetACP());
+  SetConsoleCP (GetACP ());
+  SetConsoleOutputCP (GetACP ());
 
   /* Although Emacs always sets argv[0] to an absolute pathname, we
      might get run in other ways as well, so convert argv[0] to an
@@ -508,17 +507,17 @@ main (int argc, char ** argv)
   /* Ask command.com to create an environment block with a reasonable
      amount of free space.  */
   envsize = get_env_size () + 300;
-  pass_through_args = (char **) alloca (argc * sizeof(char *));
+  pass_through_args = (char **) alloca (argc * sizeof (char *));
   num_pass_through_args = 0;
 
   while (--argc > 0)
     {
       ++argv;
       /* Act on switches we recognize (mostly single letter switches,
-        except for -e); all unrecognised switches and extra args are
+        except for -e); all unrecognized switches and extra args are
         passed on to real shell if used (only really of benefit for
         interactive use, but allow for batch use as well).  Accept / as
-        switch char for compatability with cmd.exe.  */
+        switch char for compatibility with cmd.exe.  */
       if (((*argv)[0] == '-' || (*argv)[0] == '/') && (*argv)[1] != '\0')
        {
          if (((*argv)[1] == 'c' || (*argv)[1] == 'C') && ((*argv)[2] == '\0'))
@@ -582,7 +581,7 @@ main (int argc, char ** argv)
 
       if (strpbrk (cmdline, copout_chars) == NULL)
        {
-         char *args;
+         const char *args;
 
          /* The program name is the first token of cmdline.  Since
             filenames cannot legally contain embedded quotes, the value
@@ -606,6 +605,7 @@ main (int argc, char ** argv)
     {
       char * p;
       int    extra_arg_space = 0;
+      int    maxlen, remlen;
       int    run_command_dot_com;
 
       progname = getenv ("COMSPEC");
@@ -637,21 +637,26 @@ main (int argc, char ** argv)
             case path contains spaces (fortunately it can't contain
             quotes, since they are illegal in path names).  */
 
-         buf = p = alloca (strlen (progname) + extra_arg_space +
-                           strlen (cmdline) + 16);
+         remlen = maxlen =
+           strlen (progname) + extra_arg_space + strlen (cmdline) + 16;
+         buf = p = alloca (maxlen + 1);
 
          /* Quote progname in case it contains spaces.  */
-         p += wsprintf (p, "\"%s\"", progname);
+         p += _snprintf (p, remlen, "\"%s\"", progname);
+         remlen = maxlen - (p - buf);
 
          /* Include pass_through_args verbatim; these are just switches
              so should not need quoting.  */
          for (argv = pass_through_args; *argv != NULL; ++argv)
-           p += wsprintf (p, " %s", *argv);
+           {
+             p += _snprintf (p, remlen, " %s", *argv);
+             remlen = maxlen - (p - buf);
+           }
 
          if (run_command_dot_com)
-           wsprintf(p, " /e:%d /c %s", envsize, cmdline);
+           _snprintf (p, remlen, " /e:%d /c %s", envsize, cmdline);
          else
-           wsprintf(p, " /c %s", cmdline);
+           _snprintf (p, remlen, " /c %s", cmdline);
          cmdline = buf;
        }
       else
@@ -671,19 +676,24 @@ main (int argc, char ** argv)
          else
            path[0] = '\0';
 
-         cmdline = p = alloca (strlen (progname) + extra_arg_space +
-                               strlen (path) + 13);
+         remlen = maxlen =
+           strlen (progname) + extra_arg_space + strlen (path) + 13;
+         cmdline = p = alloca (maxlen + 1);
 
          /* Quote progname in case it contains spaces.  */
-         p += wsprintf (p, "\"%s\" %s", progname, path);
+         p += _snprintf (p, remlen, "\"%s\" %s", progname, path);
+         remlen = maxlen - (p - cmdline);
 
          /* Include pass_through_args verbatim; these are just switches
              so should not need quoting.  */
          for (argv = pass_through_args; *argv != NULL; ++argv)
-           p += wsprintf (p, " %s", *argv);
+           {
+             p += _snprintf (p, remlen, " %s", *argv);
+             remlen = maxlen - (p - cmdline);
+           }
 
          if (run_command_dot_com)
-           wsprintf (p, " /e:%d", envsize);
+           _snprintf (p, remlen, " /e:%d", envsize);
        }
     }
 
@@ -707,5 +717,3 @@ main (int argc, char ** argv)
   return 0;
 }
 
-/* arch-tag: 88678d93-07ac-4e2f-ad63-d4a740ca69ac
-   (do not change this comment) */