(compute_motion): Source code was improperly indented.
[bpt/emacs.git] / src / process.c
index 491baa6..25b0ef2 100644 (file)
@@ -1,5 +1,5 @@
 /* Asynchronous subprocess control for GNU Emacs.
-   Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
+   Copyright (C) 1985, 86, 87, 88, 93, 94 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -46,6 +46,20 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include <arpa/inet.h>
 #endif /* HAVE_SOCKETS */
 
+/* TERM is a poor-man's SLIP, used on Linux.  */
+#ifdef TERM
+#include <client.h>
+#endif
+
+/* DGUX inet_addr returns a 'struct in_addr'. */
+#ifdef DGUX
+#define IN_ADDR struct in_addr
+#define NUMERIC_ADDR_ERROR (numeric_addr.s_addr == -1)
+#else
+#define IN_ADDR unsigned long
+#define NUMERIC_ADDR_ERROR (numeric_addr == -1)
+#endif
+
 #if defined(BSD) || defined(STRIDE)
 #include <sys/ioctl.h>
 #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
@@ -53,6 +67,10 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #endif /* HAVE_PTYS and no O_NDELAY */
 #endif /* BSD or STRIDE */
 
+#ifdef BROKEN_O_NONBLOCK
+#undef O_NONBLOCK
+#endif /* BROKEN_O_NONBLOCK */
+
 #ifdef NEED_BSDTTY
 #include <bsdtty.h>
 #endif
@@ -107,16 +125,18 @@ static Lisp_Object stream_process;
 
 #include "syswait.h"
 
-extern errno;
-extern sys_nerr;
+extern int errno;
+extern char *strerror ();
+#ifdef VMS
 extern char *sys_errlist[];
+#endif
 
 #ifndef VMS
 #ifndef BSD4_1
 #ifndef LINUX
 extern char *sys_siglist[];
-#endif
-#else
+#endif /* not LINUX */
+#else /* BSD4_1 */
 char *sys_siglist[] =
   {
     "bum signal!!",
@@ -288,11 +308,18 @@ status_message (status)
 
   if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
     {
+      char *signame = 0;
+      if (code < NSIG)
+       {
 #ifndef VMS
-      string = build_string (code < NSIG ? sys_siglist[code] : "unknown");
+         signame = sys_siglist[code];
 #else
-      string = build_string (code < NSIG ? sys_errlist[code] : "unknown");
+         signame = sys_errlist[code];
 #endif
+       }
+      if (signame == 0)
+       signame = "unknown";
+      string = build_string (signame);
       string2 = build_string (coredump ? " (core dumped)\n" : "\n");
       XSTRING (string)->data[0] = DOWNCASE (XSTRING (string)->data[0]);
       return concat2 (string, string2);
@@ -421,8 +448,8 @@ make_process (name)
   XSETTYPE (val, Lisp_Process);
 
   p = XPROCESS (val);
-  XFASTINT (p->infd) = 0;
-  XFASTINT (p->outfd) = 0;
+  XSET (p->infd, Lisp_Int, -1);
+  XSET (p->outfd, Lisp_Int, -1);
   XFASTINT (p->pid) = 0;
   XFASTINT (p->tick) = 0;
   XFASTINT (p->update_tick) = 0;
@@ -504,7 +531,7 @@ BUFFER may be a buffer or the name of one.")
    Buffers denote the first process in the buffer, and nil denotes the
    current buffer.  */
 
-Lisp_Object
+static Lisp_Object
 get_process (name)
      register Lisp_Object name;
 {
@@ -543,7 +570,7 @@ nil, indicating the current buffer's process.")
       XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (0), Qnil));
       XSETINT (XPROCESS (proc)->tick, ++process_tick);
     }
-  else if (XFASTINT (XPROCESS (proc)->infd))
+  else if (XINT (XPROCESS (proc)->infd) >= 0)
     {
       Fkill_process (proc, Qnil);
       /* Do this now, since remove_process will make sigchld_handler do nothing.  */
@@ -572,7 +599,7 @@ nil, indicating the current buffer's process.")
 {
   register struct Lisp_Process *p;
   register Lisp_Object status;
-  proc = Fget_process (proc);
+  proc = get_process (proc);
   if (NILP (proc))
     return proc;
   p = XPROCESS (proc);
@@ -688,9 +715,9 @@ If the process has a filter, its buffer is not used for output.")
 {
   CHECK_PROCESS (proc, 0);
   if (EQ (filter, Qt))
-    FD_CLR (XPROCESS (proc)->infd, &input_wait_mask);
+    FD_CLR (XINT (XPROCESS (proc)->infd), &input_wait_mask);
   else if (EQ (XPROCESS (proc)->filter, Qt))
-    FD_SET (XPROCESS (proc)->infd, &input_wait_mask);
+    FD_SET (XINT (XPROCESS (proc)->infd), &input_wait_mask);
   XPROCESS (proc)->filter = filter;
   return filter;
 }
@@ -1152,8 +1179,8 @@ create_process (process, new_argv, current_dir)
   /* Record this as an active process, with its channels.
      As a result, child_setup will close Emacs's side of the pipes.  */
   chan_process[inchannel] = process;
-  XFASTINT (XPROCESS (process)->infd) = inchannel;
-  XFASTINT (XPROCESS (process)->outfd) = outchannel;
+  XSET (XPROCESS (process)->infd, Lisp_Int, inchannel);
+  XSET (XPROCESS (process)->outfd, Lisp_Int, outchannel);
   /* Record the tty descriptor used in the subprocess.  */
   if (forkin < 0)
     XPROCESS (process)->subtty = Qnil;
@@ -1217,13 +1244,22 @@ create_process (process, new_argv, current_dir)
          ioctl (xforkin, TIOCSCTTY, 0);
 #endif
 #else /* not HAVE_SETSID */
-#if defined (USG) && !defined (IRIX)
-       /* It's very important to call setpgrp() here and no time
+#ifdef USG
+       /* It's very important to call setpgrp here and no time
           afterwards.  Otherwise, we lose our controlling tty which
           is set when we open the pty. */
        setpgrp ();
 #endif /* USG */
 #endif /* not HAVE_SETSID */
+#ifdef NTTYDISC
+       if (pty_flag)
+         {
+           /* Use new line discipline.  */
+           int ldisc = NTTYDISC;
+           if (ioctl (xforkin, TIOCSETD, &ldisc) < 0)
+             write (1, "create_process/TIOCSETD failed\n", 31);
+         }
+#endif
 #ifdef TIOCNOTTY 
        /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
           can do TIOCSPGRP only to the process's controlling tty.  */
@@ -1250,11 +1286,20 @@ create_process (process, new_argv, current_dir)
           This makes the pty the controlling terminal of the subprocess.  */
        if (pty_flag)
          {
+#ifdef SET_CHILD_PTY_PGRP
+           int pgrp = getpid ();
+#endif
+
            /* I wonder if close (open (pty_name, ...)) would work?  */
            if (xforkin >= 0)
              close (xforkin);
            xforkout = xforkin = open (pty_name, O_RDWR, 0);
 
+#ifdef SET_CHILD_PTY_PGRP
+           ioctl (xforkin, TIOCSPGRP, &pgrp);
+           ioctl (xforkout, TIOCSPGRP, &pgrp);
+#endif
+
            if (xforkin < 0)
              abort ();
          }
@@ -1304,20 +1349,9 @@ create_process (process, new_argv, current_dir)
   stop_polling ();
   signal (SIGALRM, create_process_1);
   alarm (1);
-#ifdef SYSV4_PTYS
-  /* OK to close only if it's not a pty.  Otherwise we need to leave
-     it open for ioctl to get pgrp when signals are sent, or to send
-     the interrupt characters through if that's how we're signalling
-     subprocesses.  Alternately if you are concerned about running out
-     of file descriptors, you could just save the tty name and open
-     just to do the ioctl.  */
-  if (NILP (XFASTINT (XPROCESS (process)->pty_flag)))
-#endif
-    {
-      XPROCESS (process)->subtty = Qnil;
-      if (forkin >= 0)
-       close (forkin);
-    }
+  XPROCESS (process)->subtty = Qnil;
+  if (forkin >= 0)
+    close (forkin);
   alarm (0);
   start_polling ();
   if (forkin != forkout && forkout >= 0)
@@ -1375,7 +1409,7 @@ Fourth arg SERVICE is name of the service desired, or an integer\n\
   struct servent *svc_info;
   struct hostent *host_info_ptr, host_info;
   char *(addr_list[2]);
-  unsigned long numeric_addr;
+  IN_ADDR numeric_addr;
   int s, outch, inch;
   char errstring[80];
   int port;
@@ -1396,12 +1430,13 @@ Fourth arg SERVICE is name of the service desired, or an integer\n\
       port = svc_info->s_port;
     }
 
+#ifndef TERM
   host_info_ptr = gethostbyname (XSTRING (host)->data);
   if (host_info_ptr == 0)
     /* Attempt to interpret host as numeric inet address */
     {
-      numeric_addr = inet_addr (XSTRING (host)->data);
-      if (numeric_addr == -1)
+      numeric_addr = inet_addr ((char *) XSTRING (host)->data);
+      if (NUMERIC_ADDR_ERROR)
        error ("Unknown host \"%s\"", XSTRING (host)->data);
 
       host_info_ptr = &host_info;
@@ -1429,6 +1464,15 @@ Fourth arg SERVICE is name of the service desired, or an integer\n\
   if (s < 0) 
     report_file_error ("error creating socket", Fcons (name, Qnil));
 
+  /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
+     when connect is interrupted.  So let's not let it get interrupted.
+     Note we do not turn off polling, because polling is only used
+     when not interrupt_input, and thus not normally used on the systems
+     which have this bug.  On systems which use polling, there's no way
+     to quit if polling is turned off.  */
+  if (interrupt_input)
+    unrequest_sigio ();
+
  loop:
   if (connect (s, (struct sockaddr *) &address, sizeof address) == -1)
     {
@@ -1436,11 +1480,26 @@ Fourth arg SERVICE is name of the service desired, or an integer\n\
       if (errno == EINTR)
        goto loop;
       close (s);
+
+      if (interrupt_input)
+       request_sigio ();
+
       errno = xerrno;
       report_file_error ("connection failed",
                         Fcons (host, Fcons (name, Qnil)));
     }
 
+  if (interrupt_input)
+    request_sigio ();
+
+#else /* TERM */
+  s = connect_server (0);
+  if (s < 0)
+    report_file_error ("error creating socket", Fcons (name, Qnil));
+  send_command (s, C_PORT, 0, "%s:%d", XSTRING (host)->data, ntohs (port));
+  send_command (s, C_DUMB, 1, 0);
+#endif /* TERM */
+
   inch = s;
   outch = dup (s);
   if (outch < 0) 
@@ -1467,8 +1526,8 @@ Fourth arg SERVICE is name of the service desired, or an integer\n\
   XPROCESS (proc)->filter = Qnil;
   XPROCESS (proc)->command = Qnil;
   XPROCESS (proc)->pid = Qnil;
-  XFASTINT (XPROCESS (proc)->infd) = s;
-  XFASTINT (XPROCESS (proc)->outfd) = outch;
+  XSET (XPROCESS (proc)->infd, Lisp_Int, s);
+  XSET (XPROCESS (proc)->outfd, Lisp_Int, outch);
   XPROCESS (proc)->status = Qrun;
   FD_SET (inch, &input_wait_mask);
 
@@ -1483,10 +1542,10 @@ deactivate_process (proc)
   register int inchannel, outchannel;
   register struct Lisp_Process *p = XPROCESS (proc);
 
-  inchannel = XFASTINT (p->infd);
-  outchannel = XFASTINT (p->outfd);
+  inchannel = XINT (p->infd);
+  outchannel = XINT (p->outfd);
 
-  if (inchannel)
+  if (inchannel >= 0)
     {
       /* Beware SIGCHLD hereabouts. */
       flush_pending_output (inchannel);
@@ -1500,12 +1559,12 @@ deactivate_process (proc)
       }
 #else
       close (inchannel);
-      if (outchannel && outchannel != inchannel)
+      if (outchannel >= 0 && outchannel != inchannel)
        close (outchannel);
 #endif
 
-      XFASTINT (p->infd) = 0;
-      XFASTINT (p->outfd) = 0;
+      XSET (p->infd, Lisp_Int, -1);
+      XSET (p->outfd, Lisp_Int, -1);
       chan_process[inchannel] = Qnil;
       FD_CLR (inchannel, &input_wait_mask);
     }
@@ -1524,11 +1583,11 @@ close_process_descs ()
       process = chan_process[i];
       if (!NILP (process))
        {
-         int in = XFASTINT (XPROCESS (process)->infd);
-         int out = XFASTINT (XPROCESS (process)->outfd);
-         if (in)
+         int in = XINT (XPROCESS (process)->infd);
+         int out = XINT (XPROCESS (process)->outfd);
+         if (in >= 0)
            close (in);
-         if (out && in != out)
+         if (out >= 0 && in != out)
            close (out);
        }
     }
@@ -1650,7 +1709,7 @@ wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
   Lisp_Object proc;
   EMACS_TIME timeout, end_time, garbage;
   SELECT_TYPE Atemp;
-  int wait_channel = 0;
+  int wait_channel = -1;
   struct Lisp_Process *wait_proc = 0;
   int got_some_input = 0;
   Lisp_Object *wait_for_cell = 0;
@@ -1662,7 +1721,7 @@ wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
   if (XTYPE (read_kbd) == Lisp_Process)
     {
       wait_proc = XPROCESS (read_kbd);
-      wait_channel = XFASTINT (wait_proc->infd);
+      wait_channel = XINT (wait_proc->infd);
       XFASTINT (read_kbd) = 0;
     }
 
@@ -1684,6 +1743,10 @@ wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
       EMACS_ADD_TIME (end_time, end_time, timeout);
     }
 
+  /* It would not be safe to call this below,
+     where we call redisplay_preserve_echo_area.  */
+  prepare_menu_bars ();
+
   while (1)
     {
       /* If calling from keyboard input, do not quit
@@ -1738,7 +1801,7 @@ wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
          if (select (MAXDESC, &Atemp, 0, 0, &timeout) <= 0)
            {
              /* It's okay for us to do this and then continue with
-                the loop, since timeout has already been zeroed out. */
+                the loop, since timeout has already been zeroed out.  */
              clear_waiting_for_input ();
              status_notify ();
            }
@@ -1772,7 +1835,10 @@ wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
        redisplay_preserve_echo_area ();
 
       if (XINT (read_kbd) && detect_input_pending ())
-       nfds = 0;
+       {
+         nfds = 0;
+         FD_ZERO (&Available);
+       }
       else
        nfds = select (MAXDESC, &Available, 0, 0, &timeout);
 
@@ -1813,7 +1879,7 @@ wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
                 the ptc file descriptor is automatically closed,
                 yielding EBADF here or at select() call above.
                 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
-                in m-ibmrt-aix.h), and here we just ignore the select error.
+                in m/ibmrt-aix.h), and here we just ignore the select error.
                 Cleanup occurs c/o status_notify after SIGCLD. */
              FD_ZERO (&Available); /* Cannot depend on values returned */
 #else
@@ -1821,7 +1887,7 @@ wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
 #endif
            }
          else
-           error("select error: %s", sys_errlist[xerrno]);
+           error("select error: %s", strerror (xerrno));
        }
 #if defined(sun) && !defined(USG5_4)
       else if (nfds > 0 && FD_ISSET (keyboard_descriptor, &Available)
@@ -1873,8 +1939,11 @@ wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
       if (XINT (read_kbd) || wait_for_cell)
        do_pending_window_change ();
 
-      /* Check for data from a process or a command channel */
-      for (channel = FIRST_PROC_DESC; channel < MAXDESC; channel++)
+      /* Check for data from a process.  */
+      /* Really FIRST_PROC_DESC should be 0 on Unix,
+        but this is safer in the short run.  */
+      for (channel = keyboard_descriptor == 0 ? FIRST_PROC_DESC : 0;
+          channel < MAXDESC; channel++)
        {
          if (FD_ISSET (channel, &Available))
            {
@@ -1885,7 +1954,7 @@ wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
                 waiting.  */
              if (wait_channel == channel)
                {
-                 wait_channel = 0;
+                 wait_channel = -1;
                  time_limit = -1;
                  got_some_input = 1;
                }
@@ -2054,7 +2123,7 @@ read_process_output (proc, channel)
 #ifdef VMS
       start_vms_process_read (vs);
 #endif
-      unbind_to (count);
+      unbind_to (count, Qnil);
       return nchars;
     }
 
@@ -2079,7 +2148,7 @@ read_process_output (proc, channel)
         at the current end-of-output marker,
         thus preserving logical ordering of input and output.  */
       if (XMARKER (p->mark)->buffer)
-       SET_PT (marker_position (p->mark));
+       SET_PT (clip_to_bounds (BEGV, marker_position (p->mark), ZV));
       else
        SET_PT (ZV);
 
@@ -2183,7 +2252,7 @@ send_process (proc, buf, len)
        if (this > 500)
          this = 500;
        old_sigpipe = (SIGTYPE (*) ()) signal (SIGPIPE, send_process_trap);
-       rv = write (XFASTINT (XPROCESS (proc)->outfd), buf, this);
+       rv = write (XINT (XPROCESS (proc)->outfd), buf, this);
        signal (SIGPIPE, old_sigpipe);
        if (rv < 0)
          {
@@ -2309,7 +2378,7 @@ process_send_signal (process, signo, current_group, nomsg)
   if (!EQ (p->childp, Qt))
     error ("Process %s is not a subprocess",
           XSTRING (p->name)->data);
-  if (!XFASTINT (p->infd))
+  if (XINT (p->infd) < 0)
     error ("Process %s is not active",
           XSTRING (p->name)->data);
 
@@ -2331,18 +2400,18 @@ process_send_signal (process, signo, current_group, nomsg)
       switch (signo)
        {
        case SIGINT:
-         tcgetattr (XFASTINT (p->infd), &t);
+         tcgetattr (XINT (p->infd), &t);
          send_process (proc, &t.c_cc[VINTR], 1);
          return;
 
        case SIGQUIT:
-         tcgetattr (XFASTINT (p->infd), &t);
+         tcgetattr (XINT (p->infd), &t);
          send_process (proc, &t.c_cc[VQUIT], 1);
          return;
 
        case SIGTSTP:
-         tcgetattr (XFASTINT (p->infd), &t);
-#ifdef VSWTCH
+         tcgetattr (XINT (p->infd), &t);
+#if defined (VSWTCH) && !defined (IRIX5)
          send_process (proc, &t.c_cc[VSWTCH], 1);
 #else
          send_process (proc, &t.c_cc[VSUSP], 1);
@@ -2362,16 +2431,16 @@ process_send_signal (process, signo, current_group, nomsg)
       switch (signo)
        {
        case SIGINT:
-         ioctl (XFASTINT (p->infd), TIOCGETC, &c);
+         ioctl (XINT (p->infd), TIOCGETC, &c);
          send_process (proc, &c.t_intrc, 1);
          return;
        case SIGQUIT:
-         ioctl (XFASTINT (p->infd), TIOCGETC, &c);
+         ioctl (XINT (p->infd), TIOCGETC, &c);
          send_process (proc, &c.t_quitc, 1);
          return;
 #ifdef SIGTSTP
        case SIGTSTP:
-         ioctl (XFASTINT (p->infd), TIOCGLTC, &lc);
+         ioctl (XINT (p->infd), TIOCGLTC, &lc);
          send_process (proc, &lc.t_suspc, 1);
          return;
 #endif /* ! defined (SIGTSTP) */
@@ -2386,16 +2455,16 @@ process_send_signal (process, signo, current_group, nomsg)
       switch (signo)
        {
        case SIGINT:
-         ioctl (XFASTINT (p->infd), TCGETA, &t);
+         ioctl (XINT (p->infd), TCGETA, &t);
          send_process (proc, &t.c_cc[VINTR], 1);
          return;
        case SIGQUIT:
-         ioctl (XFASTINT (p->infd), TCGETA, &t);
+         ioctl (XINT (p->infd), TCGETA, &t);
          send_process (proc, &t.c_cc[VQUIT], 1);
          return;
 #ifdef SIGTSTP
        case SIGTSTP:
-         ioctl (XFASTINT (p->infd), TCGETA, &t);
+         ioctl (XINT (p->infd), TCGETA, &t);
          send_process (proc, &t.c_cc[VSWTCH], 1);
          return;
 #endif /* ! defined (SIGTSTP) */
@@ -2423,7 +2492,7 @@ process_send_signal (process, signo, current_group, nomsg)
        if (!NILP (p->subtty))
          err = ioctl (XFASTINT (p->subtty), TIOCGPGRP, &gid);
        else
-         err = ioctl (XFASTINT (p->infd), TIOCGPGRP, &gid);
+         err = ioctl (XINT (p->infd), TIOCGPGRP, &gid);
 
 #ifdef pfa
        if (err == -1)
@@ -2470,7 +2539,7 @@ process_send_signal (process, signo, current_group, nomsg)
       sys$forcex (&(XFASTINT (p->pid)), 0, 1);
       whoosh:
 #endif
-      flush_pending_output (XFASTINT (p->infd));
+      flush_pending_output (XINT (p->infd));
       break;
     }
 
@@ -2486,7 +2555,7 @@ process_send_signal (process, signo, current_group, nomsg)
   /* gid may be a pid, or minus a pgrp's number */
 #ifdef TIOCSIGSEND
   if (!NILP (current_group))
-    ioctl (XFASTINT (p->infd), TIOCSIGSEND, signo);
+    ioctl (XINT (p->infd), TIOCSIGSEND, signo);
   else
     {
       gid = - XFASTINT (p->pid);
@@ -2596,7 +2665,7 @@ nil, indicating the current buffer's process.")
 #ifdef DID_REMOTE
   {
     char buf[1];
-    write (XFASTINT (XPROCESS (proc)->outfd), buf, 0);
+    write (XINT (XPROCESS (proc)->outfd), buf, 0);
   }
 #else /* did not do TOICREMOTE */
 #ifdef VMS
@@ -2606,8 +2675,8 @@ nil, indicating the current buffer's process.")
     send_process (proc, "\004", 1);
   else
     {
-      close (XPROCESS (proc)->outfd);
-      XFASTINT (XPROCESS (proc)->outfd) = open (NULL_DEVICE, O_WRONLY);
+      close (XINT (XPROCESS (proc)->outfd));
+      XSET (XPROCESS (proc)->outfd, Lisp_Int, open (NULL_DEVICE, O_WRONLY));
     }
 #endif /* VMS */
 #endif /* did not do TOICREMOTE */
@@ -2631,7 +2700,7 @@ kill_buffer_processes (buffer)
        {
          if (NETCONN_P (proc))
            deactivate_process (proc);
-         else if (XFASTINT (XPROCESS (proc)->infd))
+         else if (XINT (XPROCESS (proc)->infd) >= 0)
            process_send_signal (proc, SIGHUP, Qnil, 1);
        }
     }
@@ -2746,8 +2815,8 @@ sigchld_handler (signo)
          
          /* If process has terminated, stop waiting for its output.  */
          if (WIFSIGNALED (w) || WIFEXITED (w))
-           if (XFASTINT (p->infd))
-             FD_CLR (XFASTINT (p->infd), &input_wait_mask);
+           if (XINT (p->infd) >= 0)
+             FD_CLR (XINT (p->infd), &input_wait_mask);
 
          /* Tell wait_reading_process_input that it needs to wake up and
             look around.  */
@@ -2765,11 +2834,23 @@ sigchld_handler (signo)
          if (WIFEXITED (w))
            synch_process_retcode = WRETCODE (w);
          else if (WIFSIGNALED (w))
+           {
+             int code = WTERMSIG (w);
+             char *signame = 0;
+
+             if (code < NSIG)
+               {
 #ifndef VMS
-           synch_process_death = (char *) sys_siglist[WTERMSIG (w)];
+                 signame = sys_siglist[code];
 #else
-           synch_process_death = sys_errlist[WTERMSIG (w)];
+                 signame = sys_errlist[code];
 #endif
+               }
+             if (signame == 0)
+               signame = "unknown";
+
+             synch_process_death = signame;
+           }
 
          /* Tell wait_reading_process_input that it needs to wake up and
             look around.  */
@@ -2820,7 +2901,7 @@ exec_sentinel (proc, reason)
   /* Inhibit quit so that random quits don't screw up a running filter.  */
   specbind (Qinhibit_quit, Qt);
   call2 (sentinel, proc, reason);
-  unbind_to (count);
+  unbind_to (count, Qnil);
 }
 
 /* Report all recent events of a change in process status
@@ -2853,9 +2934,9 @@ status_notify ()
          XSETINT (p->update_tick, XINT (p->tick));
 
          /* If process is still active, read any output that remains.  */
-         if (XFASTINT (p->infd))
+         if (XINT (p->infd) >= 0)
            while (! EQ (p->filter, Qt)
-                  && read_process_output (proc, XFASTINT (p->infd)) > 0);
+                  && read_process_output (proc, XINT (p->infd)) > 0);
 
          buffer = p->buffer;
 
@@ -3105,6 +3186,9 @@ wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
     /* It's infinite.  */
     timeout_p = 0;
 
+  /* This must come before stop_polling.  */
+  prepare_menu_bars ();
+
   /* Turn off periodic alarms (in case they are in use)
      because the select emulator uses alarms.  */
   stop_polling ();