(read_process_output): Make sure the current buffer is
[bpt/emacs.git] / src / process.c
index 892a779..5fb3662 100644 (file)
@@ -1698,8 +1698,6 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)  */)
     XPROCESS (proc)->encode_coding_system = val;
   }
 
-  new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
-
   /* If program file name is not absolute, search our path for it.
      Put the name we will really use in TEM.  */
   if (!IS_DIRECTORY_SEP (SREF (program, 0))
@@ -1729,26 +1727,42 @@ usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)  */)
       && SREF (tem, 1) == ':')
     tem = Fsubstring (tem, make_number (2), Qnil);
 
-  /* Encode the file name and put it in NEW_ARGV.
-     That's where the child will use it to execute the program.  */
-  tem = ENCODE_FILE (tem);
-  new_argv[0] = SDATA (tem);
+  {
+    struct gcpro gcpro1;
+    GCPRO1 (tem);
+
+    /* Encode the file name and put it in NEW_ARGV.
+       That's where the child will use it to execute the program.  */
+    tem = Fcons (ENCODE_FILE (tem), Qnil);
+
+    /* Here we encode arguments by the coding system used for sending
+       data to the process.  We don't support using different coding
+       systems for encoding arguments and for encoding data sent to the
+       process.  */
+
+    for (i = 3; i < nargs; i++)
+      {
+       tem = Fcons (args[i], tem);
+       CHECK_STRING (XCAR (tem));
+       if (STRING_MULTIBYTE (XCAR (tem)))
+         XSETCAR (tem,
+                  code_convert_string_norecord
+                  (XCAR (tem), XPROCESS (proc)->encode_coding_system, 1));
+      }
+
+    UNGCPRO;
+  }
 
-  /* Here we encode arguments by the coding system used for sending
-     data to the process.  We don't support using different coding
-     systems for encoding arguments and for encoding data sent to the
-     process.  */
+  /* Now that everything is encoded we can collect the strings into
+     NEW_ARGV.  */
+  new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
+  new_argv[nargs - 2] = 0;
 
-  for (i = 3; i < nargs; i++)
+  for (i = nargs - 3; i >= 0; i--)
     {
-      tem = args[i];
-      CHECK_STRING (tem);
-      if (STRING_MULTIBYTE (tem))
-       tem = (code_convert_string_norecord
-              (tem, XPROCESS (proc)->encode_coding_system, 1));
-      new_argv[i - 2] = SDATA (tem);
+      new_argv[i] = SDATA (XCAR (tem));
+      tem = XCDR (tem);
     }
-  new_argv[i - 2] = 0;
 
   XPROCESS (proc)->decoding_buf = make_uninit_string (0);
   XPROCESS (proc)->decoding_carryover = 0;
@@ -1861,12 +1875,6 @@ create_process (process, new_argv, current_dir)
 #endif
       if (forkin < 0)
        report_file_error ("Opening pty", Qnil);
-#if defined (DONT_REOPEN_PTY)
-      /* In the case that vfork is defined as fork, the parent process
-        (Emacs) may send some data before the child process completes
-        tty options setup.  So we setup tty before forking.  */
-      child_setup_tty (forkout);
-#endif /* DONT_REOPEN_PTY */
 #else
       forkin = forkout = -1;
 #endif /* not USG, or USG_SUBTTY_WORKS */
@@ -2137,10 +2145,8 @@ create_process (process, new_argv, current_dir)
 #endif /* SIGCHLD */
 #endif /* !POSIX_SIGNALS */
 
-#if !defined (DONT_REOPEN_PTY)
        if (pty_flag)
          child_setup_tty (xforkout);
-#endif /* not DONT_REOPEN_PTY */
 #ifdef WINDOWSNT
        pid = child_setup (xforkin, xforkout, xforkout,
                           new_argv, 1, current_dir);
@@ -5179,7 +5185,6 @@ read_process_output (proc, channel)
   register int nbytes;
   char *chars;
   register Lisp_Object outstream;
-  register struct buffer *old = current_buffer;
   register struct Lisp_Process *p = XPROCESS (proc);
   register int opoint;
   struct coding_system *coding = proc_decode_coding_system[channel];
@@ -5379,9 +5384,11 @@ read_process_output (proc, channel)
       int opoint_byte;
       Lisp_Object text;
       struct buffer *b;
+      int count = SPECPDL_INDEX ();
 
       odeactivate = Vdeactivate_mark;
 
+      record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
       Fset_buffer (p->buffer);
       opoint = PT;
       opoint_byte = PT_BYTE;
@@ -5484,7 +5491,7 @@ read_process_output (proc, channel)
 
       current_buffer->read_only = old_read_only;
       SET_PT_BOTH (opoint, opoint_byte);
-      set_buffer_internal (old);
+      unbind_to (count, Qnil);
     }
   return nbytes;
 }
@@ -5745,7 +5752,9 @@ send_process (proc, buf, len, object)
 
                      /* Running filters might relocate buffers or strings.
                         Arrange to relocate BUF.  */
-                     if (BUFFERP (object))
+                     if (CODING_REQUIRE_ENCODING (coding))
+                       offset = buf - SDATA (coding->dst_object);
+                     else if (BUFFERP (object))
                        offset = BUF_PTR_BYTE_POS (XBUFFER (object), buf);
                      else if (STRINGP (object))
                        offset = buf - SDATA (object);
@@ -5756,7 +5765,9 @@ send_process (proc, buf, len, object)
                      wait_reading_process_output (1, 0, 0, 0, Qnil, NULL, 0);
 #endif
 
-                     if (BUFFERP (object))
+                     if (CODING_REQUIRE_ENCODING (coding))
+                       buf = offset + SDATA (coding->dst_object);
+                     else if (BUFFERP (object))
                        buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
                      else if (STRINGP (object))
                        buf = offset + SDATA (object);
@@ -7071,15 +7082,14 @@ DEFUN ("list-system-processes", Flist_system_processes, Slist_system_processes,
        doc: /* Return a list of numerical process IDs of all running processes.
 If this functionality is unsupported, return nil.
 
-See `system-process-attributes' for getting attributes of a process
-given its ID.  */)
+See `process-attributes' for getting attributes of a process given its ID.  */)
     ()
 {
   return list_system_processes ();
 }
 
-DEFUN ("system-process-attributes", Fsystem_process_attributes,
-       Ssystem_process_attributes, 1, 1, 0,
+DEFUN ("process-attributes", Fprocess_attributes,
+       Sprocess_attributes, 1, 1, 0,
        doc: /* Return attributes of the process given by its PID, a number.
 
 Value is an alist where each element is a cons cell of the form
@@ -7507,7 +7517,7 @@ The variable takes effect when `start-process' is called.  */);
   defsubr (&Sset_process_filter_multibyte);
   defsubr (&Sprocess_filter_multibyte_p);
   defsubr (&Slist_system_processes);
-  defsubr (&Ssystem_process_attributes);
+  defsubr (&Sprocess_attributes);
 }
 
 \f
@@ -7806,15 +7816,14 @@ DEFUN ("list-system-processes", Flist_system_processes, Slist_system_processes,
        doc: /* Return a list of numerical process IDs of all running processes.
 If this functionality is unsupported, return nil.
 
-See `system-process-attributes' for getting attributes of a process
-given its ID.  */)
+See `process-attributes' for getting attributes of a process given its ID.  */)
     ()
 {
   return list_system_processes ();
 }
 
-DEFUN ("system-process-attributes", Fsystem_process_attributes,
-       Ssystem_process_attributes, 1, 1, 0,
+DEFUN ("process-attributes", Fprocess_attributes,
+       Sprocess_attributes, 1, 1, 0,
        doc: /* Return attributes of the process given by its PID, a number.
 
 Value is an alist where each element is a cons cell of the form
@@ -7953,7 +7962,7 @@ syms_of_process ()
   defsubr (&Sget_buffer_process);
   defsubr (&Sprocess_inherit_coding_system_flag);
   defsubr (&Slist_system_processes);
-  defsubr (&Ssystem_process_attributes);
+  defsubr (&Sprocess_attributes);
 }
 
 \f