(undo_limit, undo_strong_limit, Vundo_outer_limit): Quadruple undo
[bpt/emacs.git] / src / callproc.c
index 3a725b8..e84c0c6 100644 (file)
@@ -1,14 +1,14 @@
 /* Synchronous subprocess invocation for GNU Emacs.
    Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
-                 2002, 2003, 2004, 2005, 2006, 2007, 2008
+                 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
                  Free Software Foundation, Inc.
 
 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 3, 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
@@ -16,9 +16,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 <config.h>
@@ -76,7 +74,7 @@ extern int errno;
 #include "lisp.h"
 #include "commands.h"
 #include "buffer.h"
-#include "charset.h"
+#include "character.h"
 #include "ccl.h"
 #include "coding.h"
 #include "composite.h"
@@ -92,13 +90,9 @@ extern int errno;
 #include "msdos.h"
 #endif
 
-#ifdef VMS
-extern noshare char **environ;
-#else
 #ifndef USE_CRT_DLL
 extern char **environ;
 #endif
-#endif
 
 #ifdef HAVE_SETPGID
 #if !defined (USG) || defined (BSD_PGRPS)
@@ -144,8 +138,6 @@ static int call_process_exited;
 
 EXFUN (Fgetenv_internal, 2);
 
-#ifndef VMS  /* VMS version is in vmsproc.c.  */
-
 static Lisp_Object
 call_process_kill (fdpid)
      Lisp_Object fdpid;
@@ -234,8 +226,7 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
   int bufsize = CALLPROC_BUFFER_SIZE_MIN;
   int count = SPECPDL_INDEX ();
 
-  register const unsigned char **new_argv
-    = (const unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
+  register const unsigned char **new_argv;
   struct buffer *old = current_buffer;
   /* File to use for stderr in the child.
      t means use same as standard output.  */
@@ -243,9 +234,6 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
 #ifdef MSDOS   /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
   char *outf, *tempfile;
   int outfilefd;
-#endif
-#if 0
-  int mask;
 #endif
   struct coding_system process_coding; /* coding-system of process output */
   struct coding_system argument_coding;        /* coding-system of arguments */
@@ -275,6 +263,7 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
     if (nargs >= 5)
       {
        int must_encode = 0;
+       Lisp_Object coding_attrs;
 
        for (i = 4; i < nargs; i++)
          CHECK_STRING (args[i]);
@@ -300,11 +289,15 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
            else
              val = Qnil;
          }
+       val = coding_inherit_eol_type (val, Qnil);
        setup_coding_system (Fcheck_coding_system (val), &argument_coding);
-       if (argument_coding.common_flags & CODING_ASCII_INCOMPATIBLE_MASK)
-         setup_coding_system (Qraw_text, &argument_coding);
-       if (argument_coding.eol_type == CODING_EOL_UNDECIDED)
-         argument_coding.eol_type = system_eol_type;
+       coding_attrs = CODING_ID_ATTRS (argument_coding.id);
+       if (NILP (CODING_ATTR_ASCII_COMPAT (coding_attrs)))
+         {
+           /* We should not use an ASCII incompatible coding system.  */
+           val = raw_text_coding_system (val);
+           setup_coding_system (val, &argument_coding);
+         }
       }
   }
 
@@ -371,9 +364,14 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
 
     GCPRO4 (infile, buffer, current_dir, error_file);
 
-    current_dir
-      = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
-                               Qnil);
+    current_dir = Funhandled_file_name_directory (current_dir);
+    if (NILP (current_dir))
+      /* If the file name handler says that current_dir is unreachable, use
+        a sensible default. */
+      current_dir = build_string ("~/");
+    current_dir = expand_and_dir_to_file (current_dir, Qnil);
+    current_dir = Ffile_name_as_directory (current_dir);
+
     if (NILP (Ffile_accessible_directory_p (current_dir)))
       report_file_error ("Setting current directory",
                         Fcons (current_buffer->directory, Qnil));
@@ -415,7 +413,8 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
       && SREF (path, 1) == ':')
     path = Fsubstring (path, make_number (2), Qnil);
 
-  new_argv[0] = SDATA (path);
+  new_argv = (const unsigned char **)
+    alloca (max (2, nargs - 2) * sizeof (char *));
   if (nargs > 4)
     {
       register int i;
@@ -427,19 +426,17 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
        {
          argument_coding.src_multibyte = STRING_MULTIBYTE (args[i]);
          if (CODING_REQUIRE_ENCODING (&argument_coding))
-           {
-             /* We must encode this argument.  */
-             args[i] = encode_coding_string (args[i], &argument_coding, 1);
-             if (argument_coding.type == coding_type_ccl)
-               setup_ccl_program (&(argument_coding.spec.ccl.encoder), Qnil);
-           }
-         new_argv[i - 3] = SDATA (args[i]);
+           /* We must encode this argument.  */
+           args[i] = encode_coding_string (&argument_coding, args[i], 1);
        }
       UNGCPRO;
-      new_argv[nargs - 3] = 0;
+      for (i = 4; i < nargs; i++)
+       new_argv[i - 3] = SDATA (args[i]);
+      new_argv[i - 3] = 0;
     }
   else
     new_argv[1] = 0;
+  new_argv[0] = SDATA (path);
 
 #ifdef MSDOS /* MW, July 1993 */
   if ((outf = egetenv ("TMPDIR")))
@@ -477,10 +474,6 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
          emacs_close (filefd);
          report_file_error ("Creating process pipe", Qnil);
        }
-#endif
-#if 0
-      /* Replaced by close_process_descs */
-      set_exclusive_use (fd[0]);
 #endif
     }
 
@@ -675,19 +668,15 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
          else
            val = Qnil;
        }
-      setup_coding_system (Fcheck_coding_system (val), &process_coding);
+      Fcheck_coding_system (val);
       /* In unibyte mode, character code conversion should not take
         place but EOL conversion should.  So, setup raw-text or one
         of the subsidiary according to the information just setup.  */
       if (NILP (current_buffer->enable_multibyte_characters)
          && !NILP (val))
-       setup_raw_text_coding_system (&process_coding);
+       val = raw_text_coding_system (val);
+      setup_coding_system (val, &process_coding);
     }
-  process_coding.src_multibyte = 0;
-  process_coding.dst_multibyte
-    = (BUFFERP (buffer)
-       ? ! NILP (XBUFFER (buffer)->enable_multibyte_characters)
-       : ! NILP (current_buffer->enable_multibyte_characters));
 
   immediate_quit = 1;
   QUIT;
@@ -699,12 +688,8 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
     int carryover = 0;
     int display_on_the_fly = display_p;
     struct coding_system saved_coding;
-    int pt_orig = PT, pt_byte_orig = PT_BYTE;
-    int inserted;
 
     saved_coding = process_coding;
-    if (process_coding.composing != COMPOSITION_DISABLED)
-      coding_allocate_composition_data (&process_coding, PT);
     while (1)
       {
        /* Repeatedly read until we've filled as much as possible
@@ -737,133 +722,49 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
 
        if (!NILP (buffer))
          {
-           if (! CODING_MAY_REQUIRE_DECODING (&process_coding))
+           if (NILP (current_buffer->enable_multibyte_characters)
+               && ! CODING_MAY_REQUIRE_DECODING (&process_coding))
              insert_1_both (buf, nread, nread, 0, 1, 0);
            else
              {                 /* We have to decode the input.  */
-               int size;
-               char *decoding_buf;
-
-             repeat_decoding:
-               size = decoding_buffer_size (&process_coding, nread);
-               decoding_buf = (char *) xmalloc (size);
-
-               /* We can't use the macro CODING_REQUIRE_DETECTION
-                  because it always returns nonzero if the coding
-                  system requires EOL detection.  Here, we have to
-                  check only whether or not the coding system
-                  requires text-encoding detection.  */
-               if (process_coding.type == coding_type_undecided)
-                 {
-                   detect_coding (&process_coding, buf, nread);
-                   if (process_coding.composing != COMPOSITION_DISABLED)
-                     /* We have not yet allocated the composition
-                        data because the coding type was undecided.  */
-                     coding_allocate_composition_data (&process_coding, PT);
-                 }
-               if (process_coding.cmp_data)
-                 process_coding.cmp_data->char_offset = PT;
-
-               decode_coding (&process_coding, buf, decoding_buf,
-                              nread, size);
+               Lisp_Object curbuf;
 
+               XSETBUFFER (curbuf, current_buffer);
+               decode_coding_c_string (&process_coding, buf, nread,
+                                       curbuf);
                if (display_on_the_fly
-                   && saved_coding.type == coding_type_undecided
-                   && process_coding.type != coding_type_undecided)
+                   && CODING_REQUIRE_DETECTION (&saved_coding)
+                   && ! CODING_REQUIRE_DETECTION (&process_coding))
                  {
                    /* We have detected some coding system.  But,
                       there's a possibility that the detection was
-                      done by insufficient data.  So, we try the code
-                      detection again with more data.  */
-                   xfree (decoding_buf);
+                      done by insufficient data.  So, we give up
+                      displaying on the fly.  */
+                   if (process_coding.produced > 0)
+                     del_range_2 (process_coding.dst_pos,
+                                  process_coding.dst_pos_byte,
+                                  process_coding.dst_pos
+                                  + process_coding.produced_char,
+                                  process_coding.dst_pos_byte
+                                  + process_coding.produced, 0);
                    display_on_the_fly = 0;
                    process_coding = saved_coding;
                    carryover = nread;
                    /* This is to make the above condition always
                       fails in the future.  */
-                   saved_coding.type = coding_type_no_conversion;
+                   saved_coding.common_flags
+                     &= ~CODING_REQUIRE_DETECTION_MASK;
                    continue;
                  }
 
-               if (process_coding.produced > 0)
-                 insert_1_both (decoding_buf, process_coding.produced_char,
-                                process_coding.produced, 0, 1, 0);
-               xfree (decoding_buf);
-
-               if (process_coding.result == CODING_FINISH_INCONSISTENT_EOL)
-                 {
-                   Lisp_Object eol_type, coding;
-
-                   if (process_coding.eol_type == CODING_EOL_CR)
-                     {
-                       /* CRs have been replaced with LFs.  Undo
-                          that in the text inserted above.  */
-                       unsigned char *p;
-
-                       move_gap_both (PT, PT_BYTE);
-
-                       p = BYTE_POS_ADDR (pt_byte_orig);
-                       for (; p < GPT_ADDR; ++p)
-                         if (*p == '\n')
-                           *p = '\r';
-                     }
-                   else if (process_coding.eol_type == CODING_EOL_CRLF)
-                     {
-                       /* CR LFs have been replaced with LFs.  Undo
-                          that by inserting CRs in front of LFs in
-                          the text inserted above.  */
-                       EMACS_INT bytepos, old_pt, old_pt_byte, nCR;
-
-                       old_pt = PT;
-                       old_pt_byte = PT_BYTE;
-                       nCR = 0;
-
-                       for (bytepos = PT_BYTE - 1;
-                            bytepos >= pt_byte_orig;
-                            --bytepos)
-                         if (FETCH_BYTE (bytepos) == '\n')
-                           {
-                             EMACS_INT charpos = BYTE_TO_CHAR (bytepos);
-                             TEMP_SET_PT_BOTH (charpos, bytepos);
-                             insert_1_both ("\r", 1, 1, 0, 1, 0);
-                             ++nCR;
-                           }
-
-                       TEMP_SET_PT_BOTH (old_pt + nCR, old_pt_byte + nCR);
-                     }
-
-                   /* Set the coding system symbol to that for
-                      Unix-like EOL.  */
-                   eol_type = Fget (saved_coding.symbol, Qeol_type);
-                   if (VECTORP (eol_type)
-                       && ASIZE (eol_type) == 3
-                       && SYMBOLP (AREF (eol_type, CODING_EOL_LF)))
-                     coding = AREF (eol_type, CODING_EOL_LF);
-                   else
-                     coding = saved_coding.symbol;
-
-                   process_coding.symbol = coding;
-                   process_coding.eol_type = CODING_EOL_LF;
-                   process_coding.mode
-                     &= ~CODING_MODE_INHIBIT_INCONSISTENT_EOL;
-                 }
-
-               nread -= process_coding.consumed;
-               carryover = nread;
+               TEMP_SET_PT_BOTH (PT + process_coding.produced_char,
+                                 PT_BYTE + process_coding.produced);
+               carryover = process_coding.carryover_bytes;
                if (carryover > 0)
                  /* As CARRYOVER should not be that large, we had
                     better avoid overhead of bcopy.  */
-                 BCOPY_SHORT (buf + process_coding.consumed, buf,
-                              carryover);
-               if (process_coding.result == CODING_FINISH_INSUFFICIENT_CMP)
-                 {
-                   /* The decoding ended because of insufficient data
-                      area to record information about composition.
-                      We must try decoding with additional data area
-                      before reading more output for the process.  */
-                   coding_allocate_composition_data (&process_coding, PT);
-                   goto repeat_decoding;
-                 }
+                 BCOPY_SHORT (process_coding.carryover, buf,
+                              process_coding.carryover_bytes);
              }
          }
 
@@ -894,33 +795,12 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
       }
   give_up: ;
 
-    if (!NILP (buffer)
-       && process_coding.cmp_data)
-      {
-       coding_restore_composition (&process_coding, Fcurrent_buffer ());
-       coding_free_composition_data (&process_coding);
-      }
-
-    {
-      int post_read_count = SPECPDL_INDEX ();
-
-      record_unwind_protect (save_excursion_restore, save_excursion_save ());
-      inserted = PT - pt_orig;
-      TEMP_SET_PT_BOTH (pt_orig, pt_byte_orig);
-      if (SYMBOLP (process_coding.post_read_conversion)
-         && !NILP (Ffboundp (process_coding.post_read_conversion)))
-       call1 (process_coding.post_read_conversion, make_number (inserted));
-
-      Vlast_coding_system_used = process_coding.symbol;
-
-      /* If the caller required, let the buffer inherit the
-        coding-system used to decode the process output.  */
-      if (inherit_process_coding_system)
-       call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
-              make_number (total_read));
-
-      unbind_to (post_read_count, Qnil);
-    }
+    Vlast_coding_system_used = CODING_ID_NAME (process_coding.id);
+    /* If the caller required, let the buffer inherit the
+       coding-system used to decode the process output.  */
+    if (inherit_process_coding_system)
+      call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
+              make_number (total_read));
   }
 
   /* Wait for it to terminate, unless it already has.  */
@@ -954,7 +834,6 @@ usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS)  */)
                                         Vlocale_coding_system, 0);
   return make_number (synch_process_retcode);
 }
-#endif
 \f
 static Lisp_Object
 delete_temp_file (name)
@@ -1110,8 +989,6 @@ usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &r
   RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs, args)));
 }
 \f
-#ifndef VMS /* VMS version is in vmsproc.c.  */
-
 static int relocate_fd ();
 
 static char **
@@ -1513,15 +1390,11 @@ DEFUN ("getenv-internal", Fgetenv_internal, Sgetenv_internal, 1, 2, 0,
 VARIABLE should be a string.  Value is nil if VARIABLE is undefined in
 the environment.  Otherwise, value is a string.
 
-This function searches `process-environment' for VARIABLE.  If it is
-not found there, then it continues the search in the environment list
-of the selected frame.
+This function searches `process-environment' for VARIABLE.
 
 If optional parameter ENV is a list, then search this list instead of
-`process-environment', and return t when encountering a negative entry.
-
-If it is a frame, then this function will ignore `process-environment' and
-will simply look up the variable in that frame's environment.  */)
+`process-environment', and return t when encountering a negative entry
+\(an entry for a variable with no value).  */)
      (variable, env)
      Lisp_Object variable, env;
 {
@@ -1559,7 +1432,6 @@ egetenv (var)
     return 0;
 }
 
-#endif /* not VMS */
 \f
 /* This is run before init_cmdargs.  */
 
@@ -1662,16 +1534,9 @@ init_callproc ()
     dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
                 Vdata_directory);
 
-#ifdef VMS
-  Vshell_file_name = build_string ("*dcl*");
-#else
   sh = (char *) getenv ("SHELL");
   Vshell_file_name = build_string (sh ? sh : "/bin/sh");
-#endif
 
-#ifdef VMS
-  Vtemp_file_name_pattern = build_string ("tmp:emacsXXXXXX.");
-#else
   if (getenv ("TMPDIR"))
     {
       char *dir = getenv ("TMPDIR");
@@ -1681,7 +1546,6 @@ init_callproc ()
     }
   else
     Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX");
-#endif
 
 #ifdef DOS_NT
   Vshared_game_score_directory = Qnil;
@@ -1698,8 +1562,11 @@ set_initial_environment ()
   register char **envp;
 #ifndef CANNOT_DUMP
   if (initialized)
-#endif
     {
+#else
+    {
+      Vprocess_environment = Qnil;
+#endif
       for (envp = environ; *envp; envp++)
        Vprocess_environment = Fcons (build_string (*envp),
                                      Vprocess_environment);
@@ -1794,10 +1661,8 @@ use.
 See `setenv' and `getenv'.  */);
   Vprocess_environment = Qnil;
 
-#ifndef VMS
   defsubr (&Scall_process);
   defsubr (&Sgetenv_internal);
-#endif
   defsubr (&Scall_process_region);
 }