]> code.delx.au - gnu-emacs/blobdiff - src/process.c
Restore inadvertently-changed non-ASCII character.
[gnu-emacs] / src / process.c
index 9fa3c19df2d58a8f9682a11c7893b6fd99746fb9..540b10681a588d61a904b61cf95ebc7a3838fb22 100644 (file)
@@ -44,7 +44,7 @@ Boston, MA 02111-1307, USA.  */
 #include <unistd.h>
 #endif
 
-#ifdef WINDOWSNT
+#if defined(WINDOWSNT) || defined(UNIX98_PTYS)
 #include <stdlib.h>
 #include <fcntl.h>
 #endif /* not WINDOWSNT */
@@ -108,6 +108,8 @@ Boston, MA 02111-1307, USA.  */
 #include "blockinput.h"
 #include "keyboard.h"
 #include "dispextern.h"
+#include "composite.h"
+#include "atimer.h"
 
 #define max(a, b) ((a) > (b) ? (a) : (b))
 
@@ -1021,7 +1023,6 @@ static Lisp_Object start_process_unwind ();
 
 DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0,
   "Start a program in a subprocess.  Return the process object for it.\n\
-Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
 NAME is name for process.  It is modified if necessary to make it unique.\n\
 BUFFER is the buffer or (buffer-name) to associate with the process.\n\
  Process output goes at end of that buffer, unless you specify\n\
@@ -1081,63 +1082,6 @@ Remaining arguments are strings to give program as arguments.")
 
   CHECK_STRING (program, 2);
 
-#ifdef VMS
-  /* Make a one member argv with all args concatenated
-     together separated by a blank.  */
-  len = STRING_BYTES (XSTRING (program)) + 2;
-  for (i = 3; i < nargs; i++)
-    {
-      tem = args[i];
-      CHECK_STRING (tem, i);
-      len += STRING_BYTES (XSTRING (tem)) + 1; /* count the blank */
-    }
-  new_argv = (unsigned char *) alloca (len);
-  strcpy (new_argv, XSTRING (program)->data);
-  for (i = 3; i < nargs; i++)
-    {
-      tem = args[i];
-      CHECK_STRING (tem, i);
-      strcat (new_argv, " ");
-      strcat (new_argv, XSTRING (tem)->data);
-    }
-  /* Need to add code here to check for program existence on VMS */
-  
-#else /* not VMS */
-  new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
-
-  /* If program file name is not absolute, search our path for it */
-  if (!IS_DIRECTORY_SEP (XSTRING (program)->data[0])
-      && !(XSTRING (program)->size > 1
-          && IS_DEVICE_SEP (XSTRING (program)->data[1])))
-    {
-      struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
-
-      tem = Qnil;
-      GCPRO4 (name, program, buffer, current_dir);
-      openp (Vexec_path, program, EXEC_SUFFIXES, &tem, 1);
-      UNGCPRO;
-      if (NILP (tem))
-       report_file_error ("Searching for program", Fcons (program, Qnil));
-      tem = Fexpand_file_name (tem, Qnil);
-      new_argv[0] = XSTRING (tem)->data;
-    }
-  else
-    {
-      if (!NILP (Ffile_directory_p (program)))
-       error ("Specified program for new process is a directory");
-
-      new_argv[0] = XSTRING (program)->data;
-    }
-
-  for (i = 3; i < nargs; i++)
-    {
-      tem = args[i];
-      CHECK_STRING (tem, i);
-      new_argv[i - 2] = XSTRING (tem)->data;
-    }
-  new_argv[i - 2] = 0;
-#endif /* not VMS */
-
   proc = make_process (name);
   /* If an error occurs and we can't start the process, we want to
      remove it from the process list.  This means that each error
@@ -1166,7 +1110,7 @@ Remaining arguments are strings to give program as arguments.")
     /* Qt denotes we have not yet called Ffind_operation_coding_system.  */
     Lisp_Object coding_systems = Qt;
     Lisp_Object val, *args2;
-    struct gcpro gcpro1;
+    struct gcpro gcpro1, gcpro2;
 
     val = Vcoding_system_for_read;
     if (NILP (val))
@@ -1174,7 +1118,7 @@ Remaining arguments are strings to give program as arguments.")
        args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
        args2[0] = Qstart_process;
        for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
-       GCPRO1 (proc);
+       GCPRO2 (proc, current_dir);
        coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
        UNGCPRO;
        if (CONSP (coding_systems))
@@ -1192,7 +1136,7 @@ Remaining arguments are strings to give program as arguments.")
            args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof args2);
            args2[0] = Qstart_process;
            for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
-           GCPRO1 (proc);
+           GCPRO2 (proc, current_dir);
            coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
            UNGCPRO;
          }
@@ -1204,6 +1148,73 @@ Remaining arguments are strings to give program as arguments.")
     XPROCESS (proc)->encode_coding_system = val;
   }
 
+#ifdef VMS
+  /* Make a one member argv with all args concatenated
+     together separated by a blank.  */
+  len = STRING_BYTES (XSTRING (program)) + 2;
+  for (i = 3; i < nargs; i++)
+    {
+      tem = args[i];
+      CHECK_STRING (tem, i);
+      len += STRING_BYTES (XSTRING (tem)) + 1; /* count the blank */
+    }
+  new_argv = (unsigned char *) alloca (len);
+  strcpy (new_argv, XSTRING (program)->data);
+  for (i = 3; i < nargs; i++)
+    {
+      tem = args[i];
+      CHECK_STRING (tem, i);
+      strcat (new_argv, " ");
+      strcat (new_argv, XSTRING (tem)->data);
+    }
+  /* Need to add code here to check for program existence on VMS */
+  
+#else /* not VMS */
+  new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
+
+  /* If program file name is not absolute, search our path for it */
+  if (!IS_DIRECTORY_SEP (XSTRING (program)->data[0])
+      && !(XSTRING (program)->size > 1
+          && IS_DEVICE_SEP (XSTRING (program)->data[1])))
+    {
+      struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
+
+      tem = Qnil;
+      GCPRO4 (name, program, buffer, current_dir);
+      openp (Vexec_path, program, EXEC_SUFFIXES, &tem, 1);
+      UNGCPRO;
+      if (NILP (tem))
+       report_file_error ("Searching for program", Fcons (program, Qnil));
+      tem = Fexpand_file_name (tem, Qnil);
+      tem = ENCODE_FILE (tem);
+      new_argv[0] = XSTRING (tem)->data;
+    }
+  else
+    {
+      if (!NILP (Ffile_directory_p (program)))
+       error ("Specified program for new process is a directory");
+
+      tem = ENCODE_FILE (program);
+      new_argv[0] = XSTRING (tem)->data;
+    }
+
+  /* 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 = args[i];
+      CHECK_STRING (tem, i);
+      if (STRING_MULTIBYTE (tem))
+       tem = (code_convert_string_norecord
+              (tem, XPROCESS (proc)->encode_coding_system, 1));
+      new_argv[i - 2] = XSTRING (tem)->data;
+    }
+  new_argv[i - 2] = 0;
+#endif /* not VMS */
+
   XPROCESS (proc)->decoding_buf = make_uninit_string (0);
   XPROCESS (proc)->decoding_carryover = make_number (0);
   XPROCESS (proc)->encoding_buf = make_uninit_string (0);
@@ -1236,18 +1247,14 @@ start_process_unwind (proc)
   return Qnil;
 }
 
-
-SIGTYPE
-create_process_1 (signo)
-     int signo;
+void
+create_process_1 (timer)
+     struct atimer *timer;
 {
-#if defined (USG) && !defined (POSIX_SIGNALS)
-  /* USG systems forget handlers when they are used;
-     must reestablish each time */
-  signal (signo, create_process_1);
-#endif /* USG */
+  /* Nothing to do.  */
 }
 
+
 #if 0  /* This doesn't work; see the note before sigchld_handler.  */
 #ifdef USG
 #ifdef SIGCHLD
@@ -1411,34 +1418,6 @@ create_process (process, new_argv, current_dir)
        setup_raw_text_coding_system (proc_encode_coding_system[outchannel]);
     }
 
-  if (CODING_REQUIRE_ENCODING (proc_encode_coding_system[outchannel]))
-    {
-      /* 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.  */
-      struct gcpro gcpro1;
-      int i = 1;
-      struct coding_system *coding = proc_encode_coding_system[outchannel];
-
-      coding->mode |= CODING_MODE_LAST_BLOCK;
-      GCPRO1 (process);
-      while (new_argv[i] != 0)
-       {
-         int len = strlen (new_argv[i]);
-         int size = encoding_buffer_size (coding, len);
-         unsigned char *buf = (unsigned char *) alloca (size);
-
-         encode_coding (coding, (unsigned char *)new_argv[i], buf, len, size);
-         buf[coding->produced] = 0;
-         /* We don't have to free new_argv[i] because it points to a
-             Lisp string given as an argument to `start-process'.  */
-         new_argv[i++] = (char *) buf;
-       }
-      UNGCPRO;
-      coding->mode &= ~CODING_MODE_LAST_BLOCK;
-    }
-
   /* Delay interrupts until we have a chance to store
      the new fork's pid in its process structure */
 #ifdef POSIX_SIGNALS
@@ -1684,14 +1663,22 @@ create_process (process, new_argv, current_dir)
       /* If the subfork execv fails, and it exits,
         this close hangs.  I don't know why.
         So have an interrupt jar it loose.  */
-      stop_polling ();
-      signal (SIGALRM, create_process_1);
-      alarm (1);
-      XPROCESS (process)->subtty = Qnil;
-      if (forkin >= 0)
-       emacs_close (forkin);
-      alarm (0);
-      start_polling ();
+      {
+       struct atimer *timer;
+       EMACS_TIME offset;
+       
+       stop_polling ();
+       EMACS_SET_SECS_USECS (offset, 1, 0);
+       timer = start_atimer (ATIMER_RELATIVE, offset, create_process_1, 0);
+       
+       XPROCESS (process)->subtty = Qnil;
+       if (forkin >= 0)
+         emacs_close (forkin);
+
+       cancel_atimer (timer);
+       start_polling ();
+      }
+      
       if (forkin != forkout && forkout >= 0)
        emacs_close (forkout);
 
@@ -1830,6 +1817,7 @@ Fourth arg SERVICE is name of the service desired, or an integer\n\
      Some kernels have a bug which causes retrying connect to fail
      after a connect.  Polling can interfere with gethostbyname too.  */
 #ifdef POLL_FOR_INPUT
+  record_unwind_protect (unwind_stop_other_atimers, Qnil);
   bind_polling_period (10);
 #endif
 
@@ -1874,7 +1862,16 @@ Fourth arg SERVICE is name of the service desired, or an integer\n\
       immediate_quit = 1;
       QUIT;
 
+      /* This turns off all alarm-based interrupts; the
+        bind_polling_period call above doesn't always turn all the
+        short-interval ones off, especially if interrupt_input is
+        set.
+
+        It'd be nice to be able to control the connect timeout
+        though.  Would non-blocking connect calls be portable?  */
+      turn_on_atimers (0);
       ret = connect (s, lres->ai_addr, lres->ai_addrlen);
+      turn_on_atimers (1);
       if (ret == 0)
        break;
       emacs_close (s);
@@ -2108,11 +2105,15 @@ Fourth arg SERVICE is name of the service desired, or an integer\n\
       = (struct coding_system *) xmalloc (sizeof (struct coding_system));
   setup_coding_system (XPROCESS (proc)->decode_coding_system,
                       proc_decode_coding_system[inch]);
+  proc_decode_coding_system[inch]->src_multibyte = 1;
+  proc_decode_coding_system[inch]->dst_multibyte = 0;
   if (!proc_encode_coding_system[outch])
     proc_encode_coding_system[outch]
       = (struct coding_system *) xmalloc (sizeof (struct coding_system));
   setup_coding_system (XPROCESS (proc)->encode_coding_system,
                       proc_encode_coding_system[outch]);
+  proc_encode_coding_system[inch]->src_multibyte = 0;
+  proc_encode_coding_system[inch]->dst_multibyte = 1;
 
   XPROCESS (proc)->decoding_buf = make_uninit_string (0);
   XPROCESS (proc)->decoding_carryover = make_number (0);
@@ -2368,18 +2369,13 @@ wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
      Causes "poll: interrupted system call" messages when Emacs is run
      in an X window
      Turn off periodic alarms (in case they are in use) */
-  stop_polling ();
+  turn_on_atimers (0);
 #endif
 
   while (1)
     {
       int timeout_reduced_for_timers = 0;
 
-#ifdef HAVE_X_WINDOWS
-      if (display_busy_cursor_p)
-       Fx_hide_busy_cursor (Qnil);
-#endif
-
       /* If calling from keyboard input, do not quit
         since we want to return C-g as an input character.
         Otherwise, do pending quit if requested.  */
@@ -2803,12 +2799,6 @@ wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
   start_polling ();
 #endif
 
-#ifdef HAVE_X_WINDOWS
-  if (display_busy_cursor_p)
-    if (!inhibit_busy_cursor)
-      Fx_show_busy_cursor ();
-#endif
-   
   return got_some_input;
 }
 \f
@@ -2829,6 +2819,7 @@ read_process_output_error_handler (error)
   Vinhibit_quit = Qt;
   update_echo_area ();
   Fsleep_for (make_number (2), Qnil);
+  return Qt;
 }
 
 /* Read pending output from the process channel,
@@ -2849,18 +2840,11 @@ read_process_output (proc, channel)
 {
   register int nchars, nbytes;
   char *chars;
-#ifdef VMS
-  int chars_allocated = 0;     /* If 1, `chars' should be freed later.  */
-#else
-  char buf[1024];
-#endif
   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];
-  int chars_in_decoding_buf = 0; /* If 1, `chars' points
-                                   XSTRING (p->decoding_buf)->data.  */
   int carryover = XINT (p->decoding_carryover);
 
 #ifdef VMS
@@ -2888,42 +2872,33 @@ read_process_output (proc, channel)
       /* The data carried over in the previous decoding (which are at
          the tail of decoding buffer) should be prepended to the new
          data read to decode all together.  */
-      char *buf = (char *) xmalloc (nbytes + carryover);
-
-      bcopy (XSTRING (p->decoding_buf)->data
-            + STRING_BYTES (XSTRING (p->decoding_buf)) - carryover,
-            buf, carryover);
-      bcopy (chars, buf + carryover, nbytes);
-      chars = buf;
-      chars_allocated = 1;
+      chars = (char *) alloca (nbytes + carryover);
+      bcopy (XSTRING (p->decoding_buf)->data, buf, carryover);
+      bcopy (vs->inputBuffer, chars + carryover, nbytes);
     }
 #else /* not VMS */
-
+  chars = (char *) alloca (carryover + 1024);
   if (carryover)
     /* See the comment above.  */
-    bcopy (XSTRING (p->decoding_buf)->data
-          + STRING_BYTES (XSTRING (p->decoding_buf)) - carryover,
-          buf, carryover);
+    bcopy (XSTRING (p->decoding_buf)->data, chars, carryover);
 
   if (proc_buffered_char[channel] < 0)
-    nbytes = emacs_read (channel, buf + carryover, (sizeof buf) - carryover);
+    nbytes = emacs_read (channel, chars + carryover, 1024 - carryover);
   else
     {
-      buf[carryover] = proc_buffered_char[channel];
+      chars[carryover] = proc_buffered_char[channel];
       proc_buffered_char[channel] = -1;
-      nbytes = emacs_read (channel, buf + carryover + 1,
-                          (sizeof buf) - carryover - 1);
+      nbytes = emacs_read (channel, chars + carryover + 1,  1023 - carryover);
       if (nbytes < 0)
        nbytes = 1;
       else
        nbytes = nbytes + 1;
     }
-  chars = buf;
 #endif /* not VMS */
 
   XSETINT (p->decoding_carryover, 0);
 
-  /* At this point, NBYTES holds number of characters just received
+  /* At this point, NBYTES holds number of bytes just received
      (including the one in proc_buffered_char[channel]).  */
   if (nbytes <= 0)
     {
@@ -2934,109 +2909,6 @@ read_process_output (proc, channel)
 
   /* Now set NBYTES how many bytes we must decode.  */
   nbytes += carryover;
-  nchars = nbytes;
-
-  if (CODING_MAY_REQUIRE_DECODING (coding))
-    {
-      int require = decoding_buffer_size (coding, nbytes);
-      int result;
-      
-      if (STRING_BYTES (XSTRING (p->decoding_buf)) < require)
-       p->decoding_buf = make_uninit_string (require);
-      result = decode_coding (coding, chars, XSTRING (p->decoding_buf)->data,
-                             nbytes, STRING_BYTES (XSTRING (p->decoding_buf)));
-      carryover = nbytes - coding->consumed;
-      if (carryover > 0)
-       {
-         /* Copy the carryover bytes to the end of p->decoding_buf, to
-            be processed on the next read.  Since decoding_buffer_size
-            asks for an extra amount of space beyond the maximum
-            expected for the output, there should always be sufficient
-            space for the carryover (which is by definition a sequence
-            of bytes that was not long enough to be decoded, and thus
-            has a bounded length).  */
-         if (STRING_BYTES (XSTRING (p->decoding_buf))
-             < coding->produced + carryover)
-           abort ();
-         bcopy (chars + coding->consumed,
-                XSTRING (p->decoding_buf)->data
-                + STRING_BYTES (XSTRING (p->decoding_buf)) - carryover,
-                carryover);
-         XSETINT (p->decoding_carryover, carryover);
-       }
-
-      /* A new coding system might be found by `decode_coding'.  */
-      if (!EQ (p->decode_coding_system, coding->symbol))
-       {
-         p->decode_coding_system = coding->symbol;
-
-         /* Don't call setup_coding_system for
-             proc_decode_coding_system[channel] here.  It is done in
-             detect_coding called via decode_coding above.  */
-
-         /* If a coding system for encoding is not yet decided, we set
-            it as the same as coding-system for decoding.
-
-            But, before doing that we must check if
-            proc_encode_coding_system[p->outfd] surely points to a
-            valid memory because p->outfd will be changed once EOF is
-            sent to the process.  */
-         if (NILP (p->encode_coding_system)
-             && proc_encode_coding_system[XINT (p->outfd)])
-           {
-             p->encode_coding_system = coding->symbol;
-             setup_coding_system (coding->symbol,
-                                  proc_encode_coding_system[XINT (p->outfd)]);
-           }
-       }
-
-#ifdef VMS
-      /*  Now we don't need the contents of `chars'.  */
-      if (chars_allocated)
-       free (chars);
-#endif
-      if (coding->produced == 0)
-       return 0;
-      chars = (char *) XSTRING (p->decoding_buf)->data;
-      nbytes = coding->produced;
-      nchars = (coding->fake_multibyte
-               ? multibyte_chars_in_text (chars, nbytes)
-               : coding->produced_char);
-      chars_in_decoding_buf = 1;
-    }
-  else
-    {
-#ifdef VMS
-      if (chars_allocated)
-       {
-         /* Although we don't have to decode the received data, we
-            must move it to an area which we don't have to free.  */
-         if (! STRINGP (p->decoding_buf)
-             || STRING_BYTES (XSTRING (p->decoding_buf)) < nbytes)
-           p->decoding_buf = make_uninit_string (nbytes);
-         bcopy (chars, XSTRING (p->decoding_buf)->data, nbytes);
-         free (chars);
-         chars = XSTRING (p->decoding_buf)->data;
-         chars_in_decoding_buf = 1;
-       }
-#endif
-      nchars = multibyte_chars_in_text (chars, nbytes);
-    }
-
-  Vlast_coding_system_used = coding->symbol;
-
-  /* If the caller required, let the process associated buffer
-     inherit the coding-system used to decode the process output.  */
-  if (! NILP (p->inherit_coding_system_flag)
-      && !NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name))
-    {
-      struct buffer *prev_buf = current_buffer;
-
-      Fset_buffer (p->buffer);
-      call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
-            make_number (nbytes));
-      set_buffer_internal (prev_buf);
-    }
 
   /* Read and dispose of the process output.  */
   outstream = p->filter;
@@ -3078,14 +2950,40 @@ read_process_output (proc, channel)
         save the match data in a special nonrecursive fashion.  */
       running_asynch_code = 1;
 
-      /* The multibyteness of a string given to the filter is decided
-         by which coding system we used for decoding.  */
-      if (coding->type == coding_type_no_conversion
-         || coding->type == coding_type_raw_text)
-       text = make_unibyte_string (chars, nbytes);
-      else
-       text = make_multibyte_string (chars, nchars, nbytes);
+      text = decode_coding_string (make_unibyte_string (chars, nbytes),
+                                  coding, 0);
+      Vlast_coding_system_used = coding->symbol;
+      /* A new coding system might be found.  */
+      if (!EQ (p->decode_coding_system, coding->symbol))
+       {
+         p->decode_coding_system = coding->symbol;
+
+         /* Don't call setup_coding_system for
+            proc_decode_coding_system[channel] here.  It is done in
+            detect_coding called via decode_coding above.  */
+
+         /* If a coding system for encoding is not yet decided, we set
+            it as the same as coding-system for decoding.
 
+            But, before doing that we must check if
+            proc_encode_coding_system[p->outfd] surely points to a
+            valid memory because p->outfd will be changed once EOF is
+            sent to the process.  */
+         if (NILP (p->encode_coding_system)
+             && proc_encode_coding_system[XINT (p->outfd)])
+           {
+             p->encode_coding_system = coding->symbol;
+             setup_coding_system (coding->symbol,
+                                  proc_encode_coding_system[XINT (p->outfd)]);
+           }
+       }
+
+      carryover = nbytes - coding->consumed;
+      bcopy (chars + coding->consumed, XSTRING (p->decoding_buf)->data,
+            carryover);
+      XSETINT (p->decoding_carryover, carryover);
+      nbytes = STRING_BYTES (XSTRING (text));
+      nchars = XSTRING (text)->size;
       internal_condition_case_1 (read_process_output_call,
                                 Fcons (outstream,
                                        Fcons (proc, Fcons (text, Qnil))),
@@ -3131,6 +3029,7 @@ read_process_output (proc, channel)
       Lisp_Object odeactivate;
       int before, before_byte;
       int opoint_byte;
+      Lisp_Object text;
 
       odeactivate = Vdeactivate_mark;
 
@@ -3162,26 +3061,34 @@ read_process_output (proc, channel)
       if (! (BEGV <= PT && PT <= ZV))
        Fwiden ();
 
-      if (NILP (current_buffer->enable_multibyte_characters))
-       nchars = nbytes;
-
       /* Insert before markers in case we are inserting where
         the buffer's mark is, and the user's next command is Meta-y.  */
-      if (chars_in_decoding_buf)
-       {
-         /* Since multibyteness of p->docoding_buf is corrupted, we
-             can't use insert_from_string_before_markers.  */
-         char *temp_buf;
-
-         temp_buf = (char *) alloca (nbytes);
-         bcopy (XSTRING (p->decoding_buf)->data, temp_buf, nbytes);
-         insert_before_markers (temp_buf, nbytes);
-       }
-      else
+      text = decode_coding_string (make_unibyte_string (chars, nbytes),
+                                  coding, 0);
+      Vlast_coding_system_used = coding->symbol;
+      /* A new coding system might be found.  See the comment in the
+        similar code in the previous `if' block.  */
+      if (!EQ (p->decode_coding_system, coding->symbol))
        {
-         insert_1_both (chars, nchars, nbytes, 0, 1, 1);
-         signal_after_change (opoint, 0, PT - opoint);
+         p->decode_coding_system = coding->symbol;
+         if (NILP (p->encode_coding_system)
+             && proc_encode_coding_system[XINT (p->outfd)])
+           {
+             p->encode_coding_system = coding->symbol;
+             setup_coding_system (coding->symbol,
+                                  proc_encode_coding_system[XINT (p->outfd)]);
+           }
        }
+      carryover = nbytes - coding->consumed;
+      bcopy (chars + coding->consumed, XSTRING (p->decoding_buf)->data,
+            carryover);
+      XSETINT (p->decoding_carryover, carryover);
+      nbytes = STRING_BYTES (XSTRING (text));
+      nchars = XSTRING (text)->size;
+      insert_from_string_before_markers (text, 0, 0, nchars, nbytes, 0);
+      signal_after_change (before, 0, PT - before);
+      update_compositions (before, PT, CHECK_BORDER);
+
       set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
 
       update_mode_lines++;
@@ -3252,7 +3159,9 @@ send_process_trap ()
    is sent.  But if the data ends at the middle of multi-byte
    representation, that incomplete sequence of bytes are sent without
    being encoded.  Should we store them in a buffer to prepend them to
-   the data send later?  */
+   the data send later? 
+
+   This function can evaluate Lisp code and can garbage collect.  */
 
 void
 send_process (proc, buf, len, object)
@@ -3263,10 +3172,8 @@ send_process (proc, buf, len, object)
 {
   /* Use volatile to protect variables from being clobbered by longjmp.  */
   int rv;
-  volatile unsigned char *procname = XSTRING (XPROCESS (proc)->name)->data;
   struct coding_system *coding;
   struct gcpro gcpro1;
-  int carryover = XINT (XPROCESS (proc)->encoding_carryover);
 
   GCPRO1 (object);
 
@@ -3278,62 +3185,51 @@ send_process (proc, buf, len, object)
   if (! NILP (XPROCESS (proc)->raw_status_low))
     update_status (XPROCESS (proc));
   if (! EQ (XPROCESS (proc)->status, Qrun))
-    error ("Process %s not running", procname);
+    error ("Process %s not running",
+          XSTRING (XPROCESS (proc)->name)->data);
   if (XINT (XPROCESS (proc)->outfd) < 0)
-    error ("Output file descriptor of %s is closed", procname);
+    error ("Output file descriptor of %s is closed",
+          XSTRING (XPROCESS (proc)->name)->data);
 
   coding = proc_encode_coding_system[XINT (XPROCESS (proc)->outfd)];
   Vlast_coding_system_used = coding->symbol;
 
+  if ((STRINGP (object) && STRING_MULTIBYTE (object))
+      || (BUFFERP (object)
+         && !NILP (XBUFFER (object)->enable_multibyte_characters)))
+    coding->src_multibyte = 1;
+  coding->dst_multibyte = 0;
+
   if (CODING_REQUIRE_ENCODING (coding))
     {
       int require = encoding_buffer_size (coding, len);
-      int offset;
+      int from_byte = -1, from, to;
       unsigned char *temp_buf = NULL;
 
-      /* Remember the offset of data because a string or a buffer may
-         be relocated.  Setting OFFSET to -1 means we don't have to
-         care about relocation.  */
-      offset = (BUFFERP (object)
-               ? BUF_PTR_BYTE_POS (XBUFFER (object), buf)
-               : (STRINGP (object)
-                  ? buf - XSTRING (object)->data
-                  : -1));
-
-      if (carryover > 0)
+      if (BUFFERP (object))
        {
-         temp_buf = (unsigned char *) xmalloc (len + carryover);
-
-         if (offset >= 0)
-           {
-             if (BUFFERP (object))
-               buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
-             else if (STRINGP (object))
-               buf = offset + XSTRING (object)->data;
-             /* Now we don't have to care about relocation.  */
-             offset = -1;
-           }
-         bcopy ((XSTRING (XPROCESS (proc)->encoding_buf)->data
-                 + STRING_BYTES (XSTRING (XPROCESS (proc)->encoding_buf))
-                 - carryover),
-                temp_buf,
-                carryover);
-         bcopy (buf, temp_buf + carryover, len);
-         buf = temp_buf;
+         from_byte = BUF_PTR_BYTE_POS (XBUFFER (object), buf);
+         from = buf_bytepos_to_charpos (XBUFFER (object), from_byte);
+         to = buf_bytepos_to_charpos (XBUFFER (object), from_byte + len);
+       }
+      else if (STRINGP (object))
+       {
+         from_byte = buf - XSTRING (object)->data;
+         from = string_byte_to_char (object, from_byte);
+         to =  string_byte_to_char (object, from_byte + len);
        }
 
+      if (from_byte >= 0 && coding->composing != COMPOSITION_DISABLED)
+       coding_save_composition (coding, from, to, object);
+
       if (STRING_BYTES (XSTRING (XPROCESS (proc)->encoding_buf)) < require)
-       {
-         XPROCESS (proc)->encoding_buf = make_uninit_string (require);
+       XPROCESS (proc)->encoding_buf = make_uninit_string (require);
+
+      if (from_byte >= 0)
+       buf = (BUFFERP (object)
+              ? BUF_BYTE_ADDRESS (XBUFFER (object), from_byte)
+              : XSTRING (object)->data + from_byte);
 
-         if (offset >= 0)
-           {
-             if (BUFFERP (object))
-               buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
-             else if (STRINGP (object))
-               buf = offset + XSTRING (object)->data;
-           }
-       }
       object = XPROCESS (proc)->encoding_buf;
       encode_coding (coding, buf, XSTRING (object)->data,
                     len, STRING_BYTES (XSTRING (object)));
@@ -3423,6 +3319,32 @@ send_process (proc, buf, len, object)
                    Lisp_Object zero;
                    int offset;
 
+#ifdef BROKEN_PTY_READ_AFTER_EAGAIN
+                   /* A gross hack to work around a bug in FreeBSD.
+                      In the following sequence, read(2) returns
+                      bogus data:
+
+                      write(2)  1022 bytes
+                      write(2)   954 bytes, get EAGAIN
+                      read(2)   1024 bytes in process_read_output
+                      read(2)     11 bytes in process_read_output
+
+                      That is, read(2) returns more bytes than have
+                      ever been written successfully.  The 1033 bytes
+                      read are the 1022 bytes written successfully
+                      after processing (for example with CRs added if
+                      the terminal is set up that way which it is
+                      here).  The same bytes will be seen again in a
+                      later read(2), without the CRs.  */
+                   
+                   if (errno == EAGAIN)
+                     {
+                       int flags = FWRITE;
+                       ioctl (XINT (XPROCESS (proc)->outfd), TIOCFLUSH,
+                              &flags);
+                     }
+#endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
+                   
                    /* Running filters might relocate buffers or strings.
                       Arrange to relocate BUF.  */
                    if (BUFFERP (object))
@@ -3467,9 +3389,11 @@ send_process (proc, buf, len, object)
       XSETINT (XPROCESS (proc)->tick, ++process_tick);
       deactivate_process (proc);
 #ifdef VMS
-      error ("Error writing to process %s; closed it", procname);
+      error ("Error writing to process %s; closed it", 
+            XSTRING (XPROCESS (proc)->name)->data);
 #else
-      error ("SIGPIPE raised on process %s; closed it", procname);
+      error ("SIGPIPE raised on process %s; closed it",
+            XSTRING (XPROCESS (proc)->name)->data);
 #endif
     }
 
@@ -4256,6 +4180,7 @@ exec_sentinel_error_handler (error)
   Vinhibit_quit = Qt;
   update_echo_area ();
   Fsleep_for (make_number (2), Qnil);
+  return Qt;
 }
 
 static void
@@ -4743,7 +4668,7 @@ wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
 
   /* Turn off periodic alarms (in case they are in use)
      because the select emulator uses alarms.  */
-  stop_polling ();
+  turn_on_atimers (0);
 
   while (1)
     {