home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / emacs-18.59-src.tgz / emacs-18.59-src.tar / fsf / emacs18 / src / process.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  76KB  |  2,790 lines

  1. /* Asynchronous subprocess control for GNU Emacs.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* This must precede sys/signal.h on certain machines.  */
  22. #include <sys/types.h>
  23. #include <signal.h>
  24.  
  25. #include "config.h"
  26.  
  27. #ifdef VMS
  28. /* Prevent the file from being totally empty.  */
  29. static dummy () {}
  30. #endif
  31.  
  32. #ifdef subprocesses
  33. /* The entire file is within this conditional */
  34.  
  35. #include <stdio.h>
  36. #include <errno.h>
  37. #include <setjmp.h>
  38. #include <sys/file.h>
  39. #include <sys/stat.h>
  40.  
  41. #ifdef HAVE_SOCKETS    /* TCP connection support, if kernel can do it */
  42. #include <sys/socket.h>
  43. #include <netdb.h>
  44. #include <netinet/in.h>
  45. #endif /* HAVE_SOCKETS */
  46.  
  47. #if defined(BSD) || defined(STRIDE)
  48. #include <sys/ioctl.h>
  49. #if !defined (O_NDELAY) && defined (HAVE_PTYS)
  50. #include <fcntl.h>
  51. #endif /* HAVE_PTYS and no O_NDELAY */
  52. #endif /* BSD or STRIDE */
  53. #ifdef USG
  54. #ifndef NO_TERMIO
  55. #include <termio.h>
  56. #endif
  57. #include <fcntl.h>
  58. #endif /* USG */
  59.  
  60. #ifdef NEED_BSDTTY
  61. #include <sys/bsdtty.h>
  62. #endif
  63.  
  64. #ifdef NEED_TERMIOS
  65. #include <sys/termios.h>
  66. #endif
  67.  
  68. #ifdef TRITON88            /* To make emacs send C-c correctly in shell */
  69. #define TIOCGPGRP FIOGETOWN
  70. #endif
  71.  
  72. #ifdef HPUX
  73. #undef TIOCGPGRP
  74. #endif
  75.  
  76. /* Include time.h or sys/time.h or both.  */
  77. #include "gettime.h"
  78.  
  79. #if defined (HPUX) && defined (HAVE_PTYS)
  80. #include <sys/ptyio.h>
  81. #endif
  82.   
  83. #ifdef AIX
  84. #include <sys/pty.h>
  85. #include <unistd.h>
  86. #endif /* AIX */
  87.  
  88. #ifdef SYSV_PTYS
  89. #include <sys/tty.h>
  90. #include <sys/pty.h>
  91. #endif
  92.  
  93. #ifdef XENIX
  94. #undef TIOCGETC  /* Avoid confusing some conditionals that test this.  */
  95. #endif
  96.  
  97. #ifdef BROKEN_TIOCGETC
  98. #undef TIOCGETC
  99. #endif
  100.  
  101. #ifdef BROKEN_O_NONBLOCK
  102. #undef O_NONBLOCK
  103. #endif
  104.  
  105. #undef NULL
  106. #include "lisp.h"
  107. #include "window.h"
  108. #include "buffer.h"
  109. #include "process.h"
  110. #include "termhooks.h"
  111. #include "termopts.h"
  112. #include "commands.h"
  113. #include "dispextern.h"
  114.  
  115. Lisp_Object Qrun, Qstop, Qsignal, Qopen, Qclosed;
  116. extern Lisp_Object Qexit;
  117.  
  118. /* a process object is a network connection when its childp field is neither
  119.    Qt nor Qnil but is instead a string (name of foreign host we
  120.    are connected to + name of port we are connected to) */
  121.  
  122. #ifdef HAVE_SOCKETS
  123. #define NETCONN_P(p) (XGCTYPE (XPROCESS (p)->childp) == Lisp_String)
  124. #else
  125. #define NETCONN_P(p) 0
  126. #endif /* HAVE_SOCKETS */
  127.  
  128. /* Define SIGCHLD as an alias for SIGCLD.  There are many conditionals
  129.    testing SIGCHLD.  */
  130.  
  131. #if !defined (SIGCHLD) && defined (SIGCLD)
  132. #define SIGCHLD SIGCLD
  133. #endif /* SIGCLD */
  134.  
  135. #include "emacssignal.h"
  136.  
  137. /* Define the structure that the wait system call stores.
  138.    On many systems, there is a structure defined for this.
  139.    But on vanilla-ish USG systems there is not.  */
  140.  
  141. #ifndef WAITTYPE
  142. #if !defined (BSD) && !defined (UNIPLUS) && !defined (STRIDE) && !(defined (HPUX) && !defined (NOMULTIPLEJOBS)) && !defined (HAVE_WAIT_HEADER)
  143. #define WAITTYPE int
  144. #define WIFSTOPPED(w) ((w&0377) == 0177)
  145. #define WIFSIGNALED(w) ((w&0377) != 0177 && (w&~0377) == 0)
  146. #define WIFEXITED(w) ((w&0377) == 0)
  147. #define WRETCODE(w) (w >> 8)
  148. #define WSTOPSIG(w) (w >> 8)
  149. #define WTERMSIG(w) (w & 0377)
  150. #ifndef WCOREDUMP
  151. #define WCOREDUMP(w) ((w&0200) != 0)
  152. #endif
  153. #else
  154. #ifdef BSD4_1
  155. #include <wait.h>
  156. #else
  157. #include <sys/wait.h>
  158. #endif /* not BSD 4.1 */
  159.  
  160. #define WAITTYPE union wait
  161. #define WRETCODE(w) w.w_retcode
  162. #define WCOREDUMP(w) w.w_coredump
  163.  
  164. #ifdef HPUX
  165. /* HPUX version 7 has broken definitions of these.  */
  166. #undef WTERMSIG
  167. #undef WSTOPSIG
  168. #undef WIFSTOPPED
  169. #undef WIFSIGNALED
  170. #undef WIFEXITED
  171. #endif
  172.  
  173. #ifndef WTERMSIG
  174. #define WTERMSIG(w) w.w_termsig
  175. #endif
  176. #ifndef WSTOPSIG
  177. #define WSTOPSIG(w) w.w_stopsig
  178. #endif
  179. #ifndef WIFSTOPPED
  180. #define WIFSTOPPED(w) (WTERMSIG (w) == 0177)
  181. #endif
  182. #ifndef WIFSIGNALED
  183. #define WIFSIGNALED(w) (WTERMSIG (w) != 0177 && (WSTOPSIG (w)) == 0)
  184. #endif
  185. #ifndef WIFEXITED
  186. #define WIFEXITED(w) (WTERMSIG (w) == 0)
  187. #endif
  188. #endif /* BSD or UNIPLUS or STRIDE */
  189. #endif /* no WAITTYPE */
  190.  
  191. extern errno;
  192. extern sys_nerr;
  193. extern char *sys_errlist[];
  194.  
  195. #ifndef BSD4_1
  196. extern char *sys_siglist[];
  197. #else
  198. char *sys_siglist[] =
  199.   {
  200.     "bum signal!!",
  201.     "hangup",
  202.     "interrupt",
  203.     "quit",
  204.     "illegal instruction",
  205.     "trace trap",
  206.     "iot instruction",
  207.     "emt instruction",
  208.     "floating point exception",
  209.     "kill",
  210.     "bus error",
  211.     "segmentation violation",
  212.     "bad argument to system call",
  213.     "write on a pipe with no one to read it",
  214.     "alarm clock",
  215.     "software termination signal from kill",
  216.     "status signal",
  217.     "sendable stop signal not from tty",
  218.     "stop signal from tty",
  219.     "continue a stopped process",
  220.     "child status has changed",
  221.     "background read attempted from control tty",
  222.     "background write attempted from control tty",
  223.     "input record available at control tty",
  224.     "exceeded CPU time limit",
  225.     "exceeded file size limit"
  226.     };
  227. #endif
  228.  
  229. #ifdef vipc
  230.  
  231. #include "vipc.h"
  232. extern int comm_server;
  233. extern int net_listen_address;
  234. #endif /* vipc */
  235.  
  236. /* Communicate exit status of synch process to callproc.c.  */
  237. extern int synch_process_retcode;
  238. extern char *synch_process_death;
  239.  
  240. /* t means use pty, nil means use a pipe,
  241.    maybe other values to come.  */
  242. Lisp_Object Vprocess_connection_type;
  243.  
  244. #ifdef SKTPAIR
  245. #ifndef HAVE_SOCKETS
  246. #include <sys/socket.h>
  247. #endif
  248. #endif /* SKTPAIR */
  249.  
  250. /* Number of events of change of status of a process.  */
  251. int process_tick;
  252.  
  253. /* Number of events for which the user or sentinel has been notified.  */
  254. int update_tick;
  255.  
  256. int delete_exited_processes;
  257.  
  258. #ifdef FD_SET
  259. /* We could get this from param.h, but better not to depend on finding that.
  260.    And better not to risk that it might define other symbols used in this
  261.    file.  */
  262. #define MAXDESC 64
  263. #define SELECT_TYPE fd_set
  264. #else /* no FD_SET */
  265. #define MAXDESC 32
  266. #define SELECT_TYPE int
  267.  
  268. /* Define the macros to access a single-int bitmap of descriptors.  */
  269. #define FD_SET(n, p) (*(p) |= (1 << (n)))
  270. #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
  271. #define FD_ISSET(n, p) (*(p) & (1 << (n)))
  272. #define FD_ZERO(p) (*(p) = 0)
  273. #endif /* no FD_SET */
  274.  
  275. /* Mask of bits indicating the descriptors that we wait for input on */
  276.  
  277. SELECT_TYPE input_wait_mask;
  278.  
  279. /* Indexed by descriptor, gives the process (if any) for that descriptor */
  280. Lisp_Object chan_process[MAXDESC];
  281.  
  282. /* Alist of elements (NAME . PROCESS) */
  283. Lisp_Object Vprocess_alist;
  284.  
  285. Lisp_Object Qprocessp;
  286.  
  287. Lisp_Object get_process ();
  288.  
  289. /* Buffered-ahead input char from process, indexed by channel.
  290.    -1 means empty (no char is buffered).
  291.    Used on sys V where the only way to tell if there is any
  292.    output from the process is to read at least one char.
  293.    Always -1 on systems that support FIONREAD.  */
  294.  
  295. int proc_buffered_char[MAXDESC];
  296.  
  297. /* These variables hold the filter about to be run, and its args,
  298.    between read_process_output and run_filter.
  299.    Also used in exec_sentinel for sentinels.  */
  300. Lisp_Object this_filter;
  301. Lisp_Object filter_process, filter_string;
  302.  
  303. /* Compute the Lisp form of the process status, p->status,
  304.    from the numeric status that was returned by `wait'.  */
  305.  
  306. update_status (p)
  307.      struct Lisp_Process *p;
  308. {
  309.   union { int i; WAITTYPE wt; } u;
  310.   u.i = XFASTINT (p->raw_status_low) + (XFASTINT (p->raw_status_high) << 16);
  311.   p->status = status_convert (u.wt);
  312.   p->raw_status_low = Qnil;
  313.   p->raw_status_high = Qnil;
  314. }
  315.  
  316. /* Convert a process status word in Unix format
  317.    to the list that we use internally.  */
  318.  
  319. Lisp_Object
  320. status_convert (w)
  321.      WAITTYPE w;
  322. {
  323.   if (WIFSTOPPED (w))
  324.     return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil));
  325.   else if (WIFEXITED (w))
  326.     return Fcons (Qexit, Fcons (make_number (WRETCODE (w)),
  327.                 WCOREDUMP (w) ? Qt : Qnil));
  328.   else if (WIFSIGNALED (w))
  329.     return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)),
  330.                   WCOREDUMP (w) ? Qt : Qnil));
  331.   else
  332.     return Qrun;
  333. }
  334.  
  335. /* Given a status-list, extract the three pieces of information
  336.    and store them individually through the three pointers.  */
  337.  
  338. void
  339. decode_status (l, symbol, code, coredump)
  340.      Lisp_Object l;
  341.      Lisp_Object *symbol;
  342.      int *code;
  343.      int *coredump;
  344. {
  345.   Lisp_Object tem;
  346.  
  347.   if (XTYPE (l) == Lisp_Symbol)
  348.     {
  349.       *symbol = l;
  350.       *code = 0;
  351.       *coredump = 0;
  352.     }
  353.   else
  354.     {
  355.       *symbol = XCONS (l)->car;
  356.       tem = XCONS (l)->cdr;
  357.       *code = XFASTINT (XCONS (tem)->car);
  358.       tem = XFASTINT (XCONS (tem)->cdr);
  359.       *coredump = !NULL (tem);
  360.     }
  361. }
  362.  
  363. /* Return a string describing a process status list.  */
  364.  
  365. Lisp_Object 
  366. status_message (status)
  367.      Lisp_Object status;
  368. {
  369.   Lisp_Object symbol;
  370.   int code, coredump;
  371.   Lisp_Object string, string2;
  372.  
  373.   decode_status (status, &symbol, &code, &coredump);
  374.  
  375.   if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
  376.     {
  377.       string = build_string (code < NSIG ? sys_siglist[code] : "unknown");
  378.       string2 = build_string (coredump ? " (core dumped)\n" : "\n");
  379.       XSTRING (string)->data[0] = DOWNCASE (XSTRING (string)->data[0]);
  380.       return concat2 (string, string2);
  381.     }
  382.   else if (EQ (symbol, Qexit))
  383.     {
  384.       if (code == 0)
  385.     return build_string ("finished\n");
  386.       string = Fint_to_string (make_number (code));
  387.       string2 = build_string (coredump ? " (core dumped)\n" : "\n");
  388.       return concat2 (build_string ("exited abnormally with code "),
  389.               concat2 (string, string2));
  390.     }
  391.   else
  392.     return Fcopy_sequence (Fsymbol_name (symbol));
  393. }
  394.  
  395. #ifdef HAVE_PTYS
  396.  
  397. /* Open an available pty, returning a file descriptor.
  398.    Return -1 on failure.
  399.    The file name of the terminal corresponding to the pty
  400.    is left in the variable pty_name.  */
  401.  
  402. char pty_name[24];
  403.  
  404. int
  405. allocate_pty ()
  406. {
  407.   struct stat stb;
  408.   register c, i;
  409.   int fd;
  410.  
  411.   /* Some systems name their pseudoterminals so that there are gaps in
  412.      the usual sequence - for example, on HP9000/S700 systems, there
  413.      are no pseudoterminals with names ending in 'f'.  So we wait for
  414.      three failures in a row before deciding that we've reached the
  415.      end of the ptys.  */
  416.   int failed_count = 0;
  417.  
  418. #ifdef PTY_ITERATION
  419.   PTY_ITERATION
  420. #else
  421.   for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
  422.     for (i = 0; i < 16; i++)
  423. #endif
  424.       {
  425. #ifdef PTY_NAME_SPRINTF
  426.     PTY_NAME_SPRINTF
  427. #else
  428. #ifdef HPUX
  429.     sprintf (pty_name, "/dev/ptym/pty%c%x", c, i);
  430. #else
  431. #ifdef RTU
  432.     sprintf (pty_name, "/dev/pty%x", i);
  433. #else
  434.     sprintf (pty_name, "/dev/pty%c%x", c, i);
  435. #endif /* not RTU */
  436. #endif /* not HPUX */
  437. #endif /* no PTY_NAME_SPRINTF */
  438.  
  439. #ifdef PTY_OPEN
  440.     PTY_OPEN;
  441. #else /* no PTY_OPEN */
  442. #ifndef IRIS
  443.     if (stat (pty_name, &stb) < 0)
  444.       {
  445.         failed_count++;
  446.         if (failed_count >= 3)
  447.           return -1;
  448.       }
  449.     else
  450.       failed_count = 0;
  451. #ifdef O_NONBLOCK
  452.     fd = open (pty_name, O_RDWR | O_NONBLOCK, 0);
  453. #else
  454.     fd = open (pty_name, O_RDWR | O_NDELAY, 0);
  455. #endif
  456. #else /* Unusual IRIS code */
  457.      fd = open ("/dev/ptc", O_RDWR | O_NDELAY, 0);
  458.      if (fd < 0)
  459.        return -1;
  460.     if (fstat (fd, &stb) < 0)
  461.       return -1;
  462. #endif /* IRIS */
  463. #endif /* no PTY_OPEN */
  464.  
  465.     if (fd >= 0)
  466.       {
  467.         /* check to make certain that both sides are available
  468.            this avoids a nasty yet stupid bug in rlogins */
  469. #ifdef PTY_TTY_NAME_SPRINTF
  470.         PTY_TTY_NAME_SPRINTF
  471. #else
  472.         /* In version 19, make these special cases use the macro above.  */
  473. #ifdef HPUX
  474.             sprintf (pty_name, "/dev/pty/tty%c%x", c, i);
  475. #else
  476. #ifdef RTU
  477.             sprintf (pty_name, "/dev/ttyp%x", i);
  478. #else
  479. #ifdef IRIS
  480.          sprintf (pty_name, "/dev/ttyq%d", minor (stb.st_rdev));
  481. #else
  482.             sprintf (pty_name, "/dev/tty%c%x", c, i);
  483. #endif /* not IRIS */
  484. #endif /* not RTU */
  485. #endif /* not HPUX */
  486. #endif /* no PTY_TTY_NAME_SPRINTF */
  487. #ifndef UNIPLUS
  488.         if (access (pty_name, 6) != 0)
  489.           {
  490.         close (fd);
  491. #ifndef IRIS
  492.         continue;
  493. #else
  494.         return -1;
  495. #endif /* IRIS */
  496.           }
  497. #endif /* not UNIPLUS */
  498.         setup_pty (fd);
  499.         return fd;
  500.       }
  501.       }
  502.   return -1;
  503. }
  504. #endif /* HAVE_PTYS */
  505.  
  506. Lisp_Object
  507. make_process (name)
  508.      Lisp_Object name;
  509. {
  510.   register Lisp_Object val, tem, name1;
  511.   register struct Lisp_Process *p;
  512.   char suffix[10];
  513.   register int i;
  514.  
  515.   /* size of process structure includes the vector header,
  516.      so deduct for that.  But struct Lisp_Vector includes the first
  517.      element, thus deducts too much, so add it back.  */
  518.   val = Fmake_vector (make_number ((sizeof (struct Lisp_Process)
  519.                     - sizeof (struct Lisp_Vector)
  520.                     + sizeof (Lisp_Object))
  521.                    / sizeof (Lisp_Object)),
  522.               Qnil);
  523.   XSETTYPE (val, Lisp_Process);
  524.  
  525.   p = XPROCESS (val);
  526.   XFASTINT (p->infd) = 0;
  527.   XFASTINT (p->outfd) = 0;
  528.   XFASTINT (p->pid) = 0;
  529.   XFASTINT (p->tick) = 0;
  530.   XFASTINT (p->update_tick) = 0;
  531.   p->raw_status_low = Qnil;
  532.   p->raw_status_high = Qnil;
  533.   p->status = Qrun;
  534.   p->mark = Fmake_marker ();
  535.  
  536.   /* If name is already in use, modify it until it is unused.  */
  537.  
  538.   name1 = name;
  539.   for (i = 1; ; i++)
  540.     {
  541.       tem = Fget_process (name1);
  542.       if (NULL (tem)) break;
  543.       sprintf (suffix, "<%d>", i);
  544.       name1 = concat2 (name, build_string (suffix));
  545.     }
  546.   name = name1;
  547.   p->name = name;
  548.   Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
  549.   return val;
  550. }
  551.  
  552. remove_process (proc)
  553.      register Lisp_Object proc;
  554. {
  555.   register Lisp_Object pair;
  556.  
  557.   pair = Frassq (proc, Vprocess_alist);
  558.   Vprocess_alist = Fdelq (pair, Vprocess_alist);
  559.   Fset_marker (XPROCESS (proc)->mark, Qnil, Qnil);
  560.  
  561.   deactivate_process (proc);
  562. }
  563.  
  564. DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0,
  565.   "Return t if OBJECT is a process.")
  566.   (obj)
  567.      Lisp_Object obj;
  568. {
  569.   return XTYPE (obj) == Lisp_Process ? Qt : Qnil;
  570. }
  571.  
  572. DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0,
  573.   "Return the process named NAME, or nil if there is none.")
  574.   (name)
  575.      register Lisp_Object name;
  576. {
  577.   if (XTYPE (name) == Lisp_Process)
  578.     return name;
  579.   CHECK_STRING (name, 0);
  580.   return Fcdr (Fassoc (name, Vprocess_alist));
  581. }
  582.  
  583. DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
  584.   "Return the (or, a) process associated with BUFFER.\n\
  585. BUFFER may be a buffer or the name of one.")
  586.   (name)
  587.      register Lisp_Object name;
  588. {
  589.   register Lisp_Object buf, tail, proc;
  590.  
  591.   if (NULL (name)) return Qnil;
  592.   buf = Fget_buffer (name);
  593.   if (NULL (buf)) return Qnil;
  594.  
  595.   for (tail = Vprocess_alist; !NULL (tail); tail = Fcdr (tail))
  596.     {
  597.       proc = Fcdr (Fcar (tail));
  598.       if (XTYPE (proc) == Lisp_Process && EQ (XPROCESS (proc)->buffer, buf))
  599.     return proc;
  600.     }
  601.   return Qnil;
  602. }
  603.  
  604. /* This is how commands for the user decode process arguments */
  605.  
  606. Lisp_Object
  607. get_process (name)
  608.      register Lisp_Object name;
  609. {
  610.   register Lisp_Object proc;
  611.   if (NULL (name))
  612.     proc = Fget_buffer_process (Fcurrent_buffer ());
  613.   else
  614.     {
  615.       proc = Fget_process (name);
  616.       if (NULL (proc))
  617.     proc = Fget_buffer_process (Fget_buffer (name));
  618.     }
  619.  
  620.   if (!NULL (proc))
  621.     return proc;
  622.  
  623.   if (NULL (name))
  624.     error ("Current buffer has no process");
  625.   else
  626.     error ("Process %s does not exist", XSTRING (name)->data);
  627.   /* NOTREACHED */
  628. }
  629.  
  630. DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0,
  631.   "Delete PROCESS: kill it and forget about it immediately.\n\
  632. PROCESS may be a process or the name of one, or a buffer name.")
  633.   (proc)
  634.      register Lisp_Object proc;
  635. {
  636.   proc = get_process (proc);
  637.   XPROCESS (proc)->raw_status_low = Qnil;
  638.   XPROCESS (proc)->raw_status_high = Qnil;
  639.   if (NETCONN_P (proc))
  640.     {
  641.       XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (0), Qnil));
  642.       XSETINT (XPROCESS (proc)->tick, ++process_tick);
  643.     }
  644.   else if (XFASTINT (XPROCESS (proc)->infd))
  645.     {
  646.       Fkill_process (proc, Qnil);
  647.       /* Do this now, since remove_process will make sigchld_handler do nothing.  */
  648.       XPROCESS (proc)->status
  649.     = Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil));
  650.       XSETINT (XPROCESS (proc)->tick, ++process_tick);
  651.       status_notify ();
  652.     }
  653.   remove_process (proc);
  654.   return Qnil;
  655. }
  656.  
  657. DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0,
  658.   "Return the status of PROCESS: a symbol, one of these:\n\
  659. run  -- for a process that is running.\n\
  660. stop -- for a process stopped but continuable.\n\
  661. exit -- for a process that has exited.\n\
  662. signal -- for a process that has got a fatal signal.\n\
  663. open -- for a network stream connection that is open.\n\
  664. closed -- for a network stream connection that is closed.\n\
  665. nil -- if arg is a process name and no such process exists.")
  666. /* command -- for a command channel opened to Emacs by another process.\n\
  667.    external -- for an i/o channel opened to Emacs by another process.\n\  */
  668.   (proc)
  669.      register Lisp_Object proc;
  670. {
  671.   register struct Lisp_Process *p;
  672.   register Lisp_Object status;
  673.   proc = Fget_process (proc);
  674.   if (NULL (proc))
  675.     return proc;
  676.   p = XPROCESS (proc);
  677.   if (!NULL (p->raw_status_low))
  678.     update_status (p);
  679.   status = p->status;
  680.   if (XTYPE (status) == Lisp_Cons)
  681.     status = XCONS (status)->car;
  682.   if (NETCONN_P (proc))
  683.     {
  684.       if (EQ (status, Qrun))
  685.     status = Qopen;
  686.       else if (EQ (status, Qexit))
  687.     status = Qclosed;
  688.     }
  689.   return status;
  690. }
  691.  
  692. DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status,
  693.        1, 1, 0,
  694.   "Return the exit status of PROCESS or the signal number that killed it.\n\
  695. If PROCESS has not yet exited or died, return 0.\n\
  696. If PROCESS is a net connection that was closed remotely, return 256.")
  697.   (proc)
  698.      register Lisp_Object proc;
  699. {
  700.   CHECK_PROCESS (proc, 0);
  701.   if (!NULL (XPROCESS (proc)->raw_status_low))
  702.     update_status (XPROCESS (proc));
  703.   if (XTYPE (XPROCESS (proc)->status) == Lisp_Cons)
  704.     return XCONS (XCONS (XPROCESS (proc)->status)->cdr)->car;
  705.   return make_number (0);
  706. }
  707.  
  708. DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0,
  709.   "Return the process id of PROCESS.\n\
  710. This is the pid of the Unix process which PROCESS uses or talks to.\n\
  711. For a network connection, this value is nil.")
  712.   (proc)
  713.      register Lisp_Object proc;
  714. {
  715.   CHECK_PROCESS (proc, 0);
  716.   return XPROCESS (proc)->pid;
  717. }
  718.  
  719. DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0,
  720.   "Return the name of PROCESS, as a string.\n\
  721. This is the name of the program invoked in PROCESS,\n\
  722. possibly modified to make it unique among process names.")
  723.   (proc)
  724.      register Lisp_Object proc;
  725. {
  726.   CHECK_PROCESS (proc, 0);
  727.   return XPROCESS (proc)->name;
  728. }
  729.  
  730. DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0,
  731.   "Return the command that was executed to start PROCESS.\n\
  732. This is a list of strings, the first string being the program executed\n\
  733. and the rest of the strings being the arguments given to it.\n\
  734. For a non-child channel, this is nil.")
  735.   (proc)
  736.      register Lisp_Object proc;
  737. {
  738.   CHECK_PROCESS (proc, 0);
  739.   return XPROCESS (proc)->command;
  740. }
  741.  
  742. DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer,
  743.   2, 2, 0,
  744.   "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
  745.   (proc, buffer)
  746.      register Lisp_Object proc, buffer;
  747. {
  748.   CHECK_PROCESS (proc, 0);
  749.   if (!NULL (buffer))
  750.     CHECK_BUFFER (buffer, 1);
  751.   XPROCESS (proc)->buffer = buffer;
  752.   return buffer;
  753. }
  754.  
  755. DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer,
  756.   1, 1, 0,
  757.   "Return the buffer PROCESS is associated with.\n\
  758. Output from PROCESS is inserted in this buffer\n\
  759. unless PROCESS has a filter.")
  760.   (proc)
  761.      register Lisp_Object proc;
  762. {
  763.   CHECK_PROCESS (proc, 0);
  764.   return XPROCESS (proc)->buffer;
  765. }
  766.  
  767. DEFUN ("process-mark", Fprocess_mark, Sprocess_mark,
  768.   1, 1, 0,
  769.   "Return the marker for the end of the last output from PROCESS.")
  770.   (proc)
  771.      register Lisp_Object proc;
  772. {
  773.   CHECK_PROCESS (proc, 0);
  774.   return XPROCESS (proc)->mark;
  775. }
  776.  
  777. DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter,
  778.   2, 2, 0,
  779.   "Give PROCESS the filter function FILTER; nil means no filter.\n\
  780. When a process has a filter, each time it does output\n\
  781. the entire string of output is passed to the filter.\n\
  782. The filter gets two arguments: the process and the string of output.\n\
  783. If the process has a filter, its buffer is not used for output.")
  784.   (proc, filter)
  785.      register Lisp_Object proc, filter;
  786. {
  787.   CHECK_PROCESS (proc, 0);
  788.   XPROCESS (proc)->filter = filter;
  789.   return filter;
  790. }
  791.  
  792. DEFUN ("process-filter", Fprocess_filter, Sprocess_filter,
  793.   1, 1, 0,
  794.   "Returns the filter function of PROCESS; nil if none.\n\
  795. See set-process-filter for more info on filter functions.")
  796.   (proc)
  797.      register Lisp_Object proc;
  798. {
  799.   CHECK_PROCESS (proc, 0);
  800.   return XPROCESS (proc)->filter;
  801. }
  802.  
  803. DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel,
  804.   2, 2, 0,
  805.   "Give PROCESS the sentinel SENTINEL; nil for none.\n\
  806. The sentinel is called as a function when the process changes state.\n\
  807. It gets two arguments: the process, and a string describing the change.")
  808.   (proc, sentinel)
  809.      register Lisp_Object proc, sentinel;
  810. {
  811.   CHECK_PROCESS (proc, 0);
  812.   XPROCESS (proc)->sentinel = sentinel;
  813.   return sentinel;
  814. }
  815.  
  816. DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
  817.   1, 1, 0,
  818.   "Return the sentinel of PROCESS; nil if none.\n\
  819. See set-process-sentinel for more info on sentinels.")
  820.   (proc)
  821.      register Lisp_Object proc;
  822. {
  823.   CHECK_PROCESS (proc, 0);
  824.   return XPROCESS (proc)->sentinel;
  825. }
  826.  
  827. DEFUN ("process-kill-without-query", Fprocess_kill_without_query,
  828.   Sprocess_kill_without_query, 1, 2, 0,
  829.   "Say no query needed if PROCESS is running when Emacs is exited.\n\
  830. Optional second argument if non-nil says to require a query.\n\
  831. Value is t if a query was formerly required.")
  832.   (proc, value)
  833.      register Lisp_Object proc, value;
  834. {
  835.   Lisp_Object tem;
  836.   CHECK_PROCESS (proc, 0);
  837.   tem = XPROCESS (proc)->kill_without_query;
  838.   XPROCESS (proc)->kill_without_query = Fnull (value);
  839.   return Fnull (tem);
  840. }
  841.  
  842. Lisp_Object
  843. list_processes_1 ()
  844. {
  845.   register Lisp_Object tail, tem;
  846.   Lisp_Object proc, minspace, tem1;
  847.   register struct buffer *old = current_buffer;
  848.   register struct Lisp_Process *p;
  849.   register int state;
  850.   char tembuf[80];
  851.  
  852.   XFASTINT (minspace) = 1;
  853.  
  854.   set_buffer_internal (XBUFFER (Vstandard_output));
  855.   Fbuffer_flush_undo (Vstandard_output);
  856.  
  857.   current_buffer->truncate_lines = Qt;
  858.  
  859.   write_string ("\
  860. Proc         Status   Buffer         Command\n\
  861. ----         ------   ------         -------\n", -1);
  862.  
  863.   for (tail = Vprocess_alist; !NULL (tail); tail = Fcdr (tail))
  864.     {
  865.       Lisp_Object symbol;
  866.  
  867.       proc = Fcdr (Fcar (tail));
  868.       p = XPROCESS (proc);
  869.       if (NULL (p->childp))
  870.     continue;
  871.  
  872.       Finsert (1, &p->name);
  873.       Findent_to (make_number (13), minspace);
  874.  
  875.       if (!NULL (p->raw_status_low))
  876.     update_status (p);
  877.       symbol = p->status;
  878.       if (XTYPE (p->status) == Lisp_Cons)
  879.     symbol = XCONS (p->status)->car;
  880.  
  881.       if (EQ (symbol, Qsignal))
  882.     {
  883.       Lisp_Object tem;
  884.       tem = Fcar (Fcdr (p->status));
  885.       if (XINT (tem) < NSIG)
  886.         write_string (sys_siglist [XINT (tem)], -1);
  887.       else
  888.         Fprinc (symbol, Qnil);
  889.     }
  890.       else if (NETCONN_P (proc))
  891.     {
  892.       if (EQ (symbol, Qrun))
  893.         write_string ("open", -1);
  894.       else if (EQ (symbol, Qexit))
  895.         write_string ("closed", -1);
  896.       else
  897.         Fprinc (symbol, Qnil);
  898.     }
  899.       else
  900.     Fprinc (symbol, Qnil);
  901.  
  902.       if (EQ (symbol, Qexit))
  903.     {
  904.       Lisp_Object tem;
  905.       tem = Fcar (Fcdr (p->status));
  906.       if (XFASTINT (tem))
  907.         {
  908.           sprintf (tembuf, " %d", XFASTINT (tem));
  909.           write_string (tembuf, -1);
  910.         }
  911.     }
  912.  
  913.       if (EQ (symbol, Qsignal) || EQ (symbol, Qexit))
  914.     remove_process (proc);
  915.  
  916.       Findent_to (make_number (22), minspace);
  917.       if (NULL (p->buffer))
  918.     InsStr ("(none)");
  919.       else if (NULL (XBUFFER (p->buffer)->name))
  920.     InsStr ("(Killed)");
  921.       else
  922.     Finsert (1, &XBUFFER (p->buffer)->name);
  923.  
  924.       Findent_to (make_number (37), minspace);
  925.  
  926.       if (NETCONN_P (proc))
  927.         {
  928.       sprintf (tembuf, "(network stream connection to %s)\n",
  929.            XSTRING (p->childp)->data);
  930.       InsStr (tembuf);
  931.         }
  932.       else 
  933.     {
  934.       tem = p->command;
  935.       while (1)
  936.         {
  937.           tem1 = Fcar (tem);
  938.           Finsert (1, &tem1);
  939.           tem = Fcdr (tem);
  940.           if (NULL (tem))
  941.         break;
  942.           InsStr (" ");
  943.         }
  944.       InsStr ("\n");
  945.        }
  946.     }
  947.  
  948.   return Qnil;
  949. }
  950.  
  951. DEFUN ("list-processes", Flist_processes, Slist_processes, 0, 0, "",
  952.   "Display a list of all processes.\n\
  953. \(Any processes listed as Exited or Signaled are actually eliminated\n\
  954. after the listing is made.)")
  955.   ()
  956. {
  957.   internal_with_output_to_temp_buffer ("*Process List*",
  958.                        list_processes_1, Qnil);
  959.   return Qnil;
  960. }
  961.  
  962. DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
  963.   "Return a list of all processes.")
  964.   ()
  965. {
  966.   return Fmapcar (Qcdr, Vprocess_alist);
  967. }
  968.  
  969. DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0,
  970.   "Start a program in a subprocess.  Return the process object for it.\n\
  971. Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
  972. NAME is name for process.  It is modified if necessary to make it unique.\n\
  973. BUFFER is the buffer or (buffer-name) to associate with the process.\n\
  974.  Process output goes at end of that buffer, unless you specify\n\
  975.  an output stream or filter function to handle the output.\n\
  976.  BUFFER may be also nil, meaning that this process is not associated\n\
  977.  with any buffer\n\
  978. Third arg is program file name.  It is searched for as in the shell.\n\
  979. Remaining arguments are strings to give program as arguments.")
  980.   (nargs, args)
  981.      int nargs;
  982.      register Lisp_Object *args;
  983. {
  984.   Lisp_Object buffer, name, program, proc, tem;
  985.   register unsigned char **new_argv;
  986.   register int i;
  987.  
  988.   buffer = args[1];
  989.   if (!NULL (buffer))
  990.     buffer = Fget_buffer_create (buffer);
  991.  
  992.   name = args[0];
  993.   CHECK_STRING (name, 0);
  994.  
  995.   program = args[2];
  996.  
  997.   CHECK_STRING (program, 2);
  998.  
  999.   new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
  1000.  
  1001.   for (i = 3; i < nargs; i++)
  1002.     {
  1003.       tem = args[i];
  1004.       CHECK_STRING (tem, i);
  1005.       new_argv[i - 2] = XSTRING (tem)->data;
  1006.     }
  1007.   new_argv[i - 2] = 0;
  1008.   new_argv[0] = XSTRING (program)->data;
  1009.  
  1010.   /* If program file name is not absolute, search our path for it */
  1011.   if (new_argv[0][0] != '/')
  1012.     {
  1013.       tem = Qnil;
  1014.       openp (Vexec_path, program, "", &tem, 1);
  1015.       if (NULL (tem))
  1016.     report_file_error ("Searching for program", Fcons (program, Qnil));
  1017.       new_argv[0] = XSTRING (tem)->data;
  1018.     }
  1019.  
  1020.   proc = make_process (name);
  1021.  
  1022.   XPROCESS (proc)->childp = Qt;
  1023.   XPROCESS (proc)->command_channel_p = Qnil;
  1024.   XPROCESS (proc)->buffer = buffer;
  1025.   XPROCESS (proc)->sentinel = Qnil;
  1026.   XPROCESS (proc)->filter = Qnil;
  1027.   XPROCESS (proc)->command = Flist (nargs - 2, args + 2);
  1028.  
  1029.   create_process (proc, new_argv);
  1030.  
  1031.   return proc;
  1032. }
  1033.  
  1034. create_process_1 (signo)
  1035.      int signo;
  1036. {
  1037. #ifdef USG
  1038.   /* USG systems forget handlers when they are used;
  1039.      must reestablish each time */
  1040.   signal (signo, create_process_1);
  1041. #endif /* USG */
  1042. }
  1043.  
  1044. #if 0  /* This doesn't work; see the note before sigchld_handler.  */
  1045. #ifdef USG
  1046. #ifdef SIGCHLD
  1047. /* Mimic blocking of signals on system V, which doesn't really have it.  */
  1048.  
  1049. /* Nonzero means we got a SIGCHLD when it was supposed to be blocked.  */
  1050. int sigchld_deferred;
  1051.  
  1052. create_process_sigchld ()
  1053. {
  1054.   signal (SIGCHLD, create_process_sigchld);
  1055.  
  1056.   sigchld_deferred = 1;
  1057. }
  1058. #endif
  1059. #endif
  1060. #endif
  1061.  
  1062. create_process (process, new_argv)
  1063.      Lisp_Object process;
  1064.      char **new_argv;
  1065. {
  1066.   int pid, inchannel, outchannel, forkin, forkout;
  1067.   int sv[2];
  1068. #ifdef SIGCHLD
  1069.   int (*sigchld)();
  1070. #endif
  1071.   char **env;
  1072.   int pty_flag = 0;
  1073.   extern char **environ;
  1074.  
  1075. #ifdef MAINTAIN_ENVIRONMENT
  1076.   env = (char **) alloca (size_of_current_environ ());
  1077.   get_current_environ (env);
  1078. #else
  1079.   env = environ;
  1080. #endif /* MAINTAIN_ENVIRONMENT */
  1081.  
  1082.   inchannel = outchannel = -1;
  1083.  
  1084. #ifdef HAVE_PTYS
  1085.   if (EQ (Vprocess_connection_type, Qt))
  1086.     outchannel = inchannel = allocate_pty ();
  1087.  
  1088.   if (inchannel >= 0)
  1089.     {
  1090. #ifndef USG
  1091.       /* On USG systems it does not work to open
  1092.      the pty's tty here and then close and reopen it in the child.  */
  1093. #ifdef O_NOCTTY
  1094.       /* Don't let this terminal become our controlling terminal
  1095.      (in case we don't have one).  */
  1096.       forkout = forkin = open (pty_name, O_RDWR | O_NOCTTY, 0);
  1097. #else
  1098.       forkout = forkin = open (pty_name, O_RDWR, 0);
  1099. #endif
  1100.       if (forkin < 0)
  1101.     report_file_error ("Opening pty", Qnil);
  1102. #else
  1103.       forkin = forkout = -1;
  1104. #endif
  1105.       pty_flag = 1;
  1106.     }
  1107.   else
  1108. #endif /* HAVE_PTYS */
  1109. #ifdef SKTPAIR
  1110.     {
  1111.       if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) < 0)
  1112.     report_file_error ("Opening socketpair", Qnil);
  1113.       outchannel = inchannel = sv[0];
  1114.       forkout = forkin = sv[1];
  1115.     }
  1116. #else /* not SKTPAIR */
  1117.     {
  1118.       int temp;
  1119.       temp = pipe (sv);
  1120.       if (temp < 0) goto io_failure;
  1121.       inchannel = sv[0];
  1122.       forkout = sv[1];
  1123.       temp = pipe (sv);
  1124.       if (temp < 0) goto io_failure;
  1125.       outchannel = sv[1];
  1126.       forkin = sv[0];
  1127.     }
  1128. #endif /* not SKTPAIR */
  1129.  
  1130. #if 0
  1131.   /* Replaced by close_process_descs */
  1132.   set_exclusive_use (inchannel);
  1133.   set_exclusive_use (outchannel);
  1134. #endif
  1135.  
  1136. /* Stride people say it's a mystery why this is needed
  1137.    as well as the O_NDELAY, but that it fails without this.  */
  1138. #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
  1139.   {
  1140.     int one = 1;
  1141.     ioctl (inchannel, FIONBIO, &one);
  1142.   }
  1143. #endif
  1144.  
  1145. #ifdef O_NONBLOCK
  1146.   fcntl (inchannel, F_SETFL, O_NONBLOCK);
  1147. #else
  1148. #ifdef O_NDELAY
  1149.   fcntl (inchannel, F_SETFL, O_NDELAY);
  1150. #endif
  1151. #endif
  1152.  
  1153.   XFASTINT (XPROCESS (process)->infd) = inchannel;
  1154.   XFASTINT (XPROCESS (process)->outfd) = outchannel;
  1155.   /* Record the tty descriptor used in the subprocess.  */
  1156. #ifdef SYSV4_PTYS
  1157.   /* On system V.4, if using a pty, we need to keep a descriptor
  1158.      for the tty that the inferior uses, in order to get the pgrp.
  1159.      If this uses too many descriptors, we could instead save the tty name
  1160.      and reopen it to send signals.  */
  1161.   if (pty_flag)
  1162.     {
  1163.       int temp = dup (forkin);
  1164.       if (temp < 0) goto io_failure;
  1165.       XFASTINT (XPROCESS (process)->subtty) = temp;
  1166.     }
  1167.   else
  1168. #endif
  1169.     XPROCESS (process)->subtty = Qnil;
  1170.   XPROCESS (process)->pty_flag = (pty_flag ? Qt : Qnil);
  1171.   XPROCESS (process)->status = Qrun;
  1172.   /* Record this as an active process, with its channels.
  1173.      As a result, child_setup will close Emacs's side of the pipes.  */
  1174.   chan_process[inchannel] = process;
  1175.  
  1176.   /* Delay interrupts until we have a chance to store
  1177.      the new fork's pid in its process structure */
  1178. #ifdef SIGCHLD
  1179. #ifdef BSD4_1
  1180.   sighold (SIGCHLD);
  1181. #else /* not BSD4_1 */
  1182. #ifdef HPUX
  1183.   sigsetmask (sigmask (SIGCHLD));
  1184. #else /* not HPUX */
  1185. #if defined (BSD) || defined (UNIPLUS)
  1186.   sigsetmask (sigmask (SIGCHLD));
  1187. #else /* ordinary USG */
  1188. #if 0
  1189.   sigchld_deferred = 0;
  1190.   sigchld = (int (*)()) signal (SIGCHLD, create_process_sigchld);
  1191. #endif
  1192. #endif /* ordinary USG */
  1193. #endif /* not HPUX */
  1194. #endif /* not BSD4_1 */
  1195. #endif /* SIGCHLD */
  1196.  
  1197.   /* Until we store the proper pid, enable sigchld_handler
  1198.      to recognize an unknown pid as standing for this process.  */
  1199.   XSETINT (XPROCESS (process)->pid, -1);
  1200.   /* Turn on the bit for our input from this process now,
  1201.      so that even if the process terminates very soon,
  1202.      we can clear the bit properly on termination.
  1203.      If fork fails, remove_process will clear the bit.  */
  1204.   FD_SET (inchannel, &input_wait_mask);
  1205.  
  1206. #ifdef AMIGA
  1207.   {
  1208.     register unsigned char *temp;
  1209.  
  1210.     if (XTYPE (current_buffer->directory) == Lisp_String)
  1211.       {
  1212.     register int i;
  1213.  
  1214.     i = XSTRING (current_buffer->directory)->size;
  1215.     temp = (unsigned char *) alloca (i + 1);
  1216.     bcopy (XSTRING (current_buffer->directory)->data, temp, i);
  1217.     temp[i] = 0;
  1218.       }
  1219.     pid = exec(new_argv[0], new_argv, forkin, forkout, temp,
  1220.            amiga_process_stack_size);
  1221.   }
  1222. #else /* not AMIGA */
  1223.   {
  1224.     /* child_setup must clobber environ on systems with true vfork.
  1225.        Protect it from permanent change.  */
  1226.     char **save_environ = environ;
  1227.  
  1228.     pid = vfork ();
  1229.     if (pid == 0)
  1230.       {
  1231.     int xforkin = forkin;
  1232.     int xforkout = forkout;
  1233.  
  1234. #if 0 /* This was probably a mistake--it duplicates code later on,
  1235.      but fails to handle all the cases.  */
  1236.     /* Make SIGCHLD work again in the child.  */
  1237.     sigsetmask (SIGEMPTYMASK);
  1238. #endif
  1239.  
  1240.     /* Make the pty be the controlling terminal of the process.  */
  1241. #ifdef HAVE_PTYS
  1242.     /* First, disconnect its current controlling terminal.  */
  1243. #ifdef HAVE_SETSID
  1244.     setsid ();
  1245. #ifdef TIOCSCTTY
  1246.     /* Make the pty's terminal the controlling terminal.  */
  1247.     if (pty_flag && (ioctl (xforkin, TIOCSCTTY, 0) < 0))
  1248.       abort ();
  1249. #endif
  1250. #else /* not HAVE_SETSID */
  1251. #ifdef USG
  1252.     /* It's very important to call setpgrp() here and no time
  1253.        afterwards.  Otherwise, we lose our controlling tty which
  1254.        is set when we open the pty. */
  1255.     setpgrp ();
  1256. #endif /* USG */
  1257. #endif /* not HAVE_SETSID */
  1258. #ifdef TIOCNOTTY
  1259.     /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
  1260.        can do TIOCSPGRP only to the process's controlling tty.  */
  1261.     if (pty_flag)
  1262.       {
  1263.         /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here? 
  1264.            I can't test it since I don't have 4.3.  */
  1265.         int j = open ("/dev/tty", O_RDWR, 0);
  1266.         ioctl (j, TIOCNOTTY, 0);
  1267.         close (j);
  1268. #ifndef USG
  1269.         /* In order to get a controlling terminal on some versions
  1270.            of BSD, it is necessary to put the process in pgrp 0
  1271.            before it opens the terminal.  */
  1272.         setpgrp (0, 0);
  1273. #endif
  1274.       }
  1275. #endif /* TIOCNOTTY */
  1276.  
  1277. #if !defined (RTU) && !defined (UNIPLUS)
  1278. /*** There is a suggestion that this ought to be a
  1279.      conditional on TIOCSPGRP.  */
  1280.     /* Now close the pty (if we had it open) and reopen it.
  1281.        This makes the pty the controlling terminal of the subprocess.  */
  1282.     if (pty_flag)
  1283.       {
  1284.         /* I wonder if close (open (pty_name, ...)) would work?  */
  1285.         if (xforkin >= 0)
  1286.           close (xforkin);
  1287.         xforkout = xforkin = open (pty_name, O_RDWR, 0);
  1288.  
  1289.         if (xforkin < 0)
  1290.           abort ();
  1291.       }
  1292. #endif /* not UNIPLUS and not RTU */
  1293. #ifdef SETUP_SLAVE_PTY
  1294.     if (pty_flag)
  1295.       {
  1296.         SETUP_SLAVE_PTY;
  1297.       }
  1298. #endif /* SETUP_SLAVE_PTY */
  1299. #ifdef AIX
  1300.     /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
  1301.        Now reenable it in the child, so it will die when we want it to.  */
  1302.     if (pty_flag)
  1303.       signal (SIGHUP, SIG_DFL);
  1304. #endif
  1305. #endif /* HAVE_PTYS */
  1306. #ifdef SIGCHLD
  1307. #ifdef BSD4_1
  1308.     sigrelse (SIGCHLD);
  1309. #else /* not BSD4_1 */
  1310. #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
  1311.     sigsetmask (SIGEMPTYMASK);
  1312. #else /* ordinary USG */
  1313. #if 0
  1314.     signal (SIGCHLD, sigchld);
  1315. #endif
  1316. #endif /* ordinary USG */
  1317. #endif /* not BSD4_1 */
  1318. #endif /* SIGCHLD */
  1319.     if (pty_flag)
  1320.       child_setup_tty (xforkout);
  1321.     child_setup (xforkin, xforkout, xforkout, new_argv, env);
  1322.       }
  1323.     environ = save_environ;
  1324.   }
  1325. #endif /* not AMIGA */
  1326.  
  1327.   if (pid < 0)
  1328.     {
  1329.       remove_process (process);
  1330.       report_file_error ("Doing vfork", Qnil);
  1331.     }
  1332.  
  1333.   XFASTINT (XPROCESS (process)->pid) = pid;
  1334.  
  1335. #ifndef AMIGA
  1336.   /* If the subfork execv fails, and it exits,
  1337.      this close hangs.  I don't know why.
  1338.      So have an interrupt jar it loose.  */
  1339.   stop_polling ();
  1340.   signal (SIGALRM, create_process_1);
  1341.   alarm (1);
  1342.   if (forkin >= 0)
  1343.     close (forkin);
  1344.   alarm (0);
  1345.   start_polling ();
  1346.   if (forkin != forkout && forkout >= 0)
  1347.     close (forkout);
  1348. #endif
  1349.  
  1350. #ifdef SIGCHLD
  1351. #ifdef BSD4_1
  1352.   sigrelse (SIGCHLD);
  1353. #else /* not BSD4_1 */
  1354. #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
  1355.   sigsetmask (SIGEMPTYMASK);
  1356. #else /* ordinary USG */
  1357. #if 0
  1358.   signal (SIGCHLD, sigchld);
  1359.   /* Now really handle any of these signals
  1360.      that came in during this function.  */
  1361.   if (sigchld_deferred)
  1362.     kill (getpid (), SIGCHLD);
  1363. #endif
  1364. #endif /* ordinary USG */
  1365. #endif /* not BSD4_1 */
  1366. #endif /* SIGCHLD */
  1367.   return;
  1368.  
  1369. io_failure:
  1370.   {
  1371.     int temp = errno;
  1372.     close (forkin);
  1373.     close (forkout);
  1374.     close (inchannel);
  1375.     close (outchannel);
  1376.     errno = temp;
  1377.     report_file_error ("Opening pty or pipe", Qnil);
  1378.   }
  1379. }
  1380.  
  1381. #ifdef HAVE_SOCKETS
  1382.  
  1383. /* open a TCP network connection to a given HOST/SERVICE.  Treated
  1384.    exactly like a normal process when reading and writing.  Only
  1385.    differences are in status display and process deletion.  A network
  1386.    connection has no PID; you cannot signal it.  All you can do is
  1387.    deactivate and close it via delete-process */
  1388.  
  1389. DEFUN ("open-network-stream", Fopen_network_stream, Sopen_network_stream, 
  1390.        4, 4, 0, 
  1391.   "Open a TCP connection for a service to a host.\n\
  1392. Returns a subprocess-object to represent the connection.\n\
  1393. Input and output work as for subprocesses; `delete-process' closes it.\n\
  1394. Args are NAME BUFFER HOST SERVICE.\n\
  1395. NAME is name for process.  It is modified if necessary to make it unique.\n\
  1396. BUFFER is the buffer (or buffer-name) to associate with the process.\n\
  1397.  Process output goes at end of that buffer, unless you specify\n\
  1398.  an output stream or filter function to handle the output.\n\
  1399.  BUFFER may be also nil, meaning that this process is not associated\n\
  1400.  with any buffer\n\
  1401. Third arg is name of the host to connect to.\n\
  1402. Fourth arg SERVICE is name of the service desired, or an integer\n\
  1403.  specifying a port number to connect to.")
  1404.    (name, buffer, host, service)
  1405.       Lisp_Object name, buffer, host, service;
  1406. {
  1407.   Lisp_Object proc;
  1408.   register int i;
  1409.   struct sockaddr_in address;
  1410.   struct servent *svc_info;
  1411.   struct hostent *host_info;
  1412.   int s, outch, inch;
  1413.   char errstring[80];
  1414.   int port;
  1415.   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  1416.  
  1417.   GCPRO4 (name, buffer, host, service);
  1418.   CHECK_STRING (name, 0);
  1419.   CHECK_STRING (host, 0);
  1420.   if (XTYPE (service) == Lisp_Int)
  1421.     port = htons ((unsigned short) XINT (service));
  1422.   else
  1423.     {
  1424.       CHECK_STRING (service, 0);
  1425.       svc_info = getservbyname (XSTRING (service)->data, "tcp");
  1426.       if (svc_info == 0)
  1427.     error ("Unknown service \"%s\"", XSTRING (service)->data);
  1428.       port = svc_info->s_port;
  1429.     }
  1430.  
  1431.   bzero (&address, sizeof address);
  1432.   address.sin_addr.s_addr = inet_addr (XSTRING (host)->data);
  1433.   if (address.sin_addr.s_addr != -1)
  1434.     address.sin_family = AF_INET;
  1435.   else
  1436.     {
  1437.       host_info = gethostbyname (XSTRING (host)->data);
  1438.       if (host_info == 0)
  1439.     error ("Unknown host \"%s\"", XSTRING (host)->data);
  1440.       bcopy (host_info->h_addr, (char *) &address.sin_addr, host_info->h_length);
  1441.       address.sin_family = host_info->h_addrtype;
  1442.     }
  1443.   address.sin_port = port;
  1444.  
  1445.   s = socket (address.sin_family, SOCK_STREAM, 0);
  1446.   if (s < 0) 
  1447.     report_file_error ("error creating socket", Fcons (name, Qnil));
  1448.  
  1449.   /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
  1450.      when connect is interrupted.  So let's not let it get interrupted.  */
  1451.   if (interrupt_input)
  1452.     unrequest_sigio ();
  1453.   stop_polling ();
  1454.  
  1455.   while (1)
  1456.     {
  1457.       int value = connect (s, &address, sizeof address);
  1458.       /* Continue if successeful.  */
  1459.       if (value != -1)
  1460.     break;
  1461.       /* Report a "real" error.  */
  1462.       if (errno != EINTR)
  1463.     {
  1464.       close (s);
  1465.       error ("Host \"%s\" not responding", XSTRING (host)->data);
  1466.     }
  1467.       /* Loop around after temporary error.  */
  1468.     }
  1469.  
  1470.   if (interrupt_input)
  1471.     request_sigio ();
  1472.   start_polling ();
  1473.  
  1474.   inch = s;
  1475.   outch = dup (s);
  1476.   if (outch < 0) 
  1477.     report_file_error ("error duplicating socket", Fcons (name, Qnil));
  1478.  
  1479.   if (!NULL (buffer))
  1480.     buffer = Fget_buffer_create (buffer);
  1481.   proc = make_process (name);
  1482.  
  1483.   chan_process[inch] = proc;
  1484.  
  1485. #ifdef O_NONBLOCK
  1486.   fcntl (inch, F_SETFL, O_NONBLOCK);
  1487. #else
  1488. #ifdef O_NDELAY
  1489.   fcntl (inch, F_SETFL, O_NDELAY);
  1490. #endif
  1491. #endif
  1492.  
  1493.   XPROCESS (proc)->childp = host;
  1494.   XPROCESS (proc)->command_channel_p = Qnil;
  1495.   XPROCESS (proc)->buffer = buffer;
  1496.   XPROCESS (proc)->sentinel = Qnil;
  1497.   XPROCESS (proc)->filter = Qnil;
  1498.   XPROCESS (proc)->command = Qnil;
  1499.   XPROCESS (proc)->pid = Qnil;
  1500.   XPROCESS (proc)->kill_without_query = Qt;
  1501.   XFASTINT (XPROCESS (proc)->infd) = s;
  1502.   XFASTINT (XPROCESS (proc)->outfd) = outch;
  1503.   XPROCESS (proc)->status = Qrun;
  1504.   FD_SET (inch, &input_wait_mask);
  1505.  
  1506.   UNGCPRO;
  1507.   return proc;
  1508. }
  1509. #endif    /* HAVE_SOCKETS */
  1510.  
  1511. deactivate_process (proc)
  1512.      Lisp_Object proc;
  1513. {
  1514.   register int inchannel, outchannel;
  1515.   register struct Lisp_Process *p = XPROCESS (proc);
  1516.  
  1517.   inchannel = XFASTINT (p->infd);
  1518.   outchannel = XFASTINT (p->outfd);
  1519.  
  1520.   if (inchannel)
  1521.     {
  1522.       /* Beware SIGCHLD hereabouts. */
  1523.       flush_pending_output (inchannel);
  1524.       close (inchannel);
  1525.       if (outchannel  &&  outchannel != inchannel)
  1526.      close (outchannel);
  1527.  
  1528.       XFASTINT (p->infd) = 0;
  1529.       XFASTINT (p->outfd) = 0;
  1530.       chan_process[inchannel] = Qnil;
  1531.       FD_CLR (inchannel, &input_wait_mask);
  1532.     }
  1533. }
  1534.  
  1535. /* Close all descriptors currently in use for communication
  1536.    with subprocess.  This is used in a newly-forked subprocess
  1537.    to get rid of irrelevant descriptors.  */
  1538.  
  1539. close_process_descs ()
  1540. {
  1541.   int i;
  1542.   for (i = 0; i < MAXDESC; i++)
  1543.     {
  1544.       Lisp_Object process;
  1545.       process = chan_process[i];
  1546.       if (!NULL (process))
  1547.     {
  1548.       int in = XFASTINT (XPROCESS (process)->infd);
  1549.       int out = XFASTINT (XPROCESS (process)->outfd);
  1550.  
  1551.       if (in != 0)
  1552.         close (in);
  1553.       if (out != 0 && out != in)
  1554.         close (out);
  1555.       if (!NULL (XPROCESS (process)->subtty))
  1556.         close (XFASTINT (XPROCESS (process)->subtty));
  1557.     }
  1558.     }
  1559. }
  1560.  
  1561. DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output,
  1562.   0, 1, 0,
  1563.   "Allow any pending output from subprocesses to be read by Emacs.\n\
  1564. It is read into the process' buffers or given to their filter functions.\n\
  1565. Non-nil arg PROCESS means do not return until some output has been received\n\
  1566. from PROCESS.")
  1567.   (proc)
  1568.      register Lisp_Object proc;
  1569. {
  1570.   if (NULL (proc))
  1571.     wait_reading_process_input (-1, 0, 0);
  1572.   else
  1573.     {
  1574.       proc = get_process (proc);
  1575.       wait_reading_process_input (0, XPROCESS (proc), 0);
  1576.     }
  1577.   return Qnil;
  1578. }
  1579.  
  1580. /* This variable is different from waiting_for_input in keyboard.c.
  1581.    It is used to communicate to a lisp process-filter/sentinel (via the
  1582.    function Fwaiting_for_user_input_p below) whether emacs was waiting
  1583.    for user-input when that process-filter was called.
  1584.    waiting_for_input cannot be used as that is by definition 0 when
  1585.    lisp code is being evalled */
  1586. static int waiting_for_user_input_p;
  1587.  
  1588. /* Read and dispose of subprocess output
  1589.  while waiting for timeout to elapse and/or keyboard input to be available.
  1590.  
  1591.  time_limit is the timeout in seconds, or zero for no limit.
  1592.  -1 means gobble data available immediately but don't wait for any.
  1593.  
  1594.  read_kbd is 1 to return when input is available.
  1595.  -1 means caller will actually read the input.
  1596.  A pointer to a struct Lisp_Process means wait until
  1597.  something arrives from that process.
  1598.  
  1599.  do_display means redisplay should be done to show
  1600.  subprocess output that arrives.  */
  1601.  
  1602. wait_reading_process_input (time_limit, read_kbd, do_display)
  1603.      int time_limit, read_kbd, do_display;
  1604. {
  1605.   register int channel, nfds, m;
  1606.   SELECT_TYPE Available;
  1607.   SELECT_TYPE Exception;
  1608.   int xerrno;
  1609.   Lisp_Object proc;
  1610. #ifdef HAVE_TIMEVAL
  1611.   struct timeval timeout, end_time, garbage;
  1612. #else
  1613.   long timeout, end_time, temp;
  1614. #endif /* not HAVE_TIMEVAL */
  1615.   SELECT_TYPE Atemp;
  1616.   int wait_channel = 0;
  1617.   struct Lisp_Process *wait_proc = 0;
  1618.   extern kbd_count;
  1619.  
  1620.   /* Detect when read_kbd is really the address of a Lisp_Process.  */
  1621.   if (read_kbd > 10 || read_kbd < -1)
  1622.     {
  1623.       wait_proc = (struct Lisp_Process *) read_kbd;
  1624.       wait_channel = XFASTINT (wait_proc->infd);
  1625.       read_kbd = 0;
  1626.     }
  1627.   waiting_for_user_input_p = read_kbd;
  1628.  
  1629.   /* Since we may need to wait several times,
  1630.      compute the absolute time to return at.  */
  1631.   if (time_limit)
  1632.     {
  1633. #ifdef HAVE_TIMEVAL
  1634.       gettimeofday (&end_time, &garbage);
  1635.       end_time.tv_sec += time_limit;
  1636. #else /* not HAVE_TIMEVAL */
  1637.       time (&end_time);
  1638.       end_time += time_limit;
  1639. #endif /* not HAVE_TIMEVAL */
  1640.     }
  1641.  
  1642. #if 0  /* Select emulator claims to preserve alarms.
  1643.       And there are many ways to get out of this function by longjmp.  */
  1644.   /* Turn off periodic alarms (in case they are in use)
  1645.      because the select emulator uses alarms.  */
  1646.   stop_polling ();
  1647. #endif
  1648.  
  1649.   while (1)
  1650.     {
  1651.       /* If calling from keyboard input, do not quit
  1652.      since we want to return C-g as an input character.
  1653.      Otherwise, do pending quit if requested.  */
  1654.       if (read_kbd >= 0)
  1655.     {
  1656. #if 0
  1657.       /* This is the same condition tested by QUIT.
  1658.          We need to resume polling if we are going to quit.  */
  1659.       if (!NULL (Vquit_flag) && NULL (Vinhibit_quit))
  1660.         {
  1661.           start_polling ();
  1662.           QUIT;
  1663.         }
  1664. #endif
  1665.       QUIT;
  1666.     }
  1667.  
  1668.       /* If status of something has changed, and no input is available,
  1669.      notify the user of the change right away */
  1670.       if (update_tick != process_tick && do_display)
  1671.     {
  1672.       Atemp = input_wait_mask;
  1673. #ifdef HAVE_TIMEVAL
  1674.       timeout.tv_sec=0; timeout.tv_usec=0;
  1675. #else /* not HAVE_TIMEVAL */
  1676.       timeout = 0;
  1677. #endif /* not HAVE_TIMEVAL */
  1678.       if (select (MAXDESC, &Atemp, 0, 0, &timeout) <= 0)
  1679.         status_notify ();
  1680.     }
  1681.  
  1682.       /* Don't wait for output from a non-running process.  */
  1683.       if (wait_proc != 0 && !NULL (wait_proc->raw_status_low))
  1684.     update_status (wait_proc);
  1685.       if (wait_proc != 0
  1686.       && ! EQ (wait_proc->status, Qrun))
  1687.     break;
  1688.  
  1689.       if (fix_screen_hook)
  1690.     (*fix_screen_hook) ();
  1691.  
  1692.       /* Compute time from now till when time limit is up */
  1693.       /* Exit if already run out */
  1694.       if (time_limit == -1)
  1695.     {
  1696.       /* -1 specified for timeout means
  1697.          gobble output available now
  1698.          but don't wait at all. */
  1699. #ifdef HAVE_TIMEVAL
  1700.       timeout.tv_sec = 0;
  1701.       timeout.tv_usec = 0;
  1702. #else
  1703.       timeout = 0;
  1704. #endif /* not HAVE_TIMEVAL */
  1705.     }
  1706.       else if (time_limit)
  1707.     {
  1708. #ifdef HAVE_TIMEVAL
  1709.       gettimeofday (&timeout, &garbage);
  1710.  
  1711.       /* In effect, timeout = end_time - timeout.
  1712.          Break if result would be negative.  */
  1713.       if (timeval_subtract (&timeout, end_time, timeout))
  1714.         break;
  1715. #else /* not HAVE_TIMEVAL */
  1716.           time (&temp);
  1717.       timeout = end_time - temp;
  1718.       if (timeout < 0)
  1719.         break;
  1720. #endif /* not HAVE_TIMEVAL */
  1721.     }
  1722.       else
  1723.     {
  1724. #ifdef HAVE_TIMEVAL
  1725.       /* If no real timeout, loop sleeping with a big timeout
  1726.          so that input interrupt can wake us up by zeroing it  */
  1727.       timeout.tv_sec = 100;
  1728.       timeout.tv_usec = 0;
  1729. #else /* not HAVE_TIMEVAL */
  1730.           timeout = 100000;    /* 100000 recognized by the select emulator */
  1731. #endif /* not HAVE_TIMEVAL */
  1732.     }
  1733.  
  1734.       /* Cause quitting and alarm signals to take immediate action,
  1735.      and cause input available signals to zero out timeout */
  1736.       if (read_kbd < 0)
  1737.     set_waiting_for_input (&timeout);
  1738.  
  1739.       /* Wait till there is something to do */
  1740.  
  1741.       Available = Exception = input_wait_mask;
  1742.       if (!read_kbd)
  1743.     FD_CLR (0, &Available);
  1744.  
  1745.       if (read_kbd && kbd_count)
  1746.     nfds = 0;
  1747.       else
  1748.     /* Since we don't do anything abt Exceptions,
  1749.        let's notw wake up for them.  */
  1750.     nfds = select (MAXDESC, &Available, 0, 0, &timeout);
  1751. #if 0
  1752. #ifdef IBMRTAIX
  1753.     nfds = select (MAXDESC, &Available, 0, 0, &timeout);
  1754. #else
  1755. #ifdef HPUX
  1756.     nfds = select (MAXDESC, &Available, 0, 0, &timeout);
  1757. #else
  1758.     nfds = select (MAXDESC, &Available, 0, &Exception, &timeout);
  1759. #endif
  1760. #endif
  1761. #endif /* 0 */
  1762.       xerrno = errno;
  1763.  
  1764.       if (fix_screen_hook)
  1765.     (*fix_screen_hook) ();
  1766.  
  1767.       /* Make C-g and alarm signals set flags again */
  1768.       clear_waiting_for_input ();
  1769.  
  1770.       /* If we woke up due to SIGWINCH, actually change size now.  */
  1771.       if (read_kbd)
  1772.     do_pending_window_change ();
  1773.  
  1774.       if (time_limit && nfds == 0)    /* timeout elapsed */
  1775.     break;
  1776.       if (nfds < 0)
  1777.     {
  1778.       if (xerrno == EINTR)
  1779.         FD_ZERO (&Available);
  1780. #ifdef ALLIANT
  1781.       /* This happens for no known reason on ALLIANT.
  1782.          I am guessing that this is the right response. -- RMS.  */
  1783.       else if (xerrno == EFAULT)
  1784.         FD_ZERO (&Available);
  1785. #endif
  1786.       else if (xerrno == EBADF)
  1787. #ifdef AIX
  1788.       /* AIX will return EBADF on a call to select involving a ptc if the
  1789.          associated pts isn't open.  Since this will only happen just as
  1790.          a child is dying, just ignore the situation -- SIGCHLD will come
  1791.          along quite quickly, and after cleanup the ptc will no longer be
  1792.          checked, so this error will stop recurring.  */
  1793.         FD_ZERO (&Available);     /* Cannot depend on values returned.  */
  1794. #else /* not AIX */
  1795.         abort ();
  1796. #endif /* not AIX */
  1797.       else
  1798.         error("select error: %s", sys_errlist[xerrno]);
  1799.     }
  1800. #ifdef SIGIO
  1801. #if defined (sun) || defined (APOLLO)
  1802.       else if (nfds > 0 && FD_ISSET (0, &Available) && interrupt_input)
  1803.     /* System sometimes fails to deliver SIGIO.  */
  1804.     kill (getpid (), SIGIO);
  1805. #endif
  1806. #endif
  1807.  
  1808.       /* Check for keyboard input */
  1809.       /* If there is any, return immediately
  1810.      to give it higher priority than subprocesses */
  1811.  
  1812.       if (read_kbd && detect_input_pending ())
  1813.     break;
  1814.  
  1815.       /* If checking input just got us a size-change event from X,
  1816.      obey it now if we should.  */
  1817.       if (read_kbd)
  1818.     do_pending_window_change ();
  1819.  
  1820.       /* If screen size has changed, redisplay now
  1821.      for either sit-for or keyboard input.  */
  1822.       if (read_kbd && screen_garbaged)
  1823.     redisplay_preserve_echo_area ();
  1824.  
  1825. #ifdef vipc
  1826.       /* Check for connection from other process */
  1827.  
  1828.       if (FD_ISSET (comm_server, &Available))
  1829.     {
  1830.       FD_CLR (comm_server, &Available);
  1831.       create_commchan ();
  1832.     }
  1833. #endif /* vipc */
  1834.  
  1835.       /* Check for data from a process or a command channel */
  1836.  
  1837.       for (channel = 3; channel < MAXDESC; channel++)
  1838.     {
  1839.       if (FD_ISSET (channel, &Available))
  1840.         {
  1841.           int nread;
  1842.  
  1843.           FD_CLR (channel, &Available);
  1844.           /* If waiting for this channel,
  1845.          arrange to return as soon as no more input
  1846.          to be processed.  No more waiting.  */
  1847.           if (wait_channel == channel)
  1848.         {
  1849.           wait_channel = 0;
  1850.           time_limit = -1;
  1851.         }
  1852.           proc = chan_process[channel];
  1853.           if (NULL (proc))
  1854.         continue;
  1855.  
  1856. #ifdef vipc
  1857.           /* It's a command channel */
  1858.           if (!NULL (XPROCESS (proc)->command_channel_p))
  1859.         {
  1860.           ProcessCommChan (channel, proc);
  1861.           if (NULL (XPROCESS (proc)->command_channel_p))
  1862.             {
  1863.               /* It has ceased to be a command channel! */
  1864.               int bytes_available;
  1865.               if (ioctl (channel, FIONREAD, &bytes_available) < 0)
  1866.             bytes_available = 0;
  1867.               if (bytes_available)
  1868.             FD_SET (channel, &Available);
  1869.             }
  1870.           continue;
  1871.         }
  1872. #endif /* vipc */
  1873.  
  1874.           /* Read data from the process, starting with our
  1875.          buffered-ahead character if we have one.  */
  1876.  
  1877.           nread = read_process_output (proc, channel);
  1878.           if (nread > 0)
  1879.         {
  1880.           /* Since read_process_output can run a filter,
  1881.              which can call accept-process-output,
  1882.              don't try to read from any other processes
  1883.              before doing the select again.  */
  1884.           FD_ZERO (&Available);
  1885.  
  1886.           if (do_display)
  1887.             redisplay_preserve_echo_area ();
  1888.         }
  1889. #ifdef EWOULDBLOCK
  1890.           else if (nread == -1 && errno == EWOULDBLOCK)
  1891.         ;
  1892. #else
  1893. #ifdef O_NONBLOCK
  1894.           else if (nread == -1 && errno == EAGAIN)
  1895.         ;
  1896. #else
  1897. #ifdef O_NDELAY
  1898.           else if (nread == -1 && errno == EAGAIN)
  1899.         ;
  1900.           /* Note that we cannot distinguish between no input
  1901.          available now and a closed pipe.
  1902.          With luck, a closed pipe will be accompanied by
  1903.          subprocess termination and SIGCHLD.  */
  1904.           else if (nread == 0 && !NETCONN_P (proc))
  1905.         ;
  1906. #endif /* O_NDELAY */
  1907. #endif /* O_NONBLOCK */
  1908. #endif /* EWOULDBLOCK */
  1909. #ifdef HAVE_PTYS
  1910.           /* On some OSs with ptys, when the process on one end of
  1911.          a pty exits, the other end gets an error reading with
  1912.          errno = EIO instead of getting an EOF (0 bytes read).
  1913.          Therefore, if we get an error reading and errno =
  1914.          EIO, just continue, because the child process has
  1915.          exited and should clean itself up soon (e.g. when we
  1916.          get a SIGCHLD). */
  1917.           else if (nread == -1 && errno == EIO && !NETCONN_P (proc))
  1918.         ;
  1919. #endif /* HAVE_PTYS */
  1920. /* If we can detect process termination, don't consider the process
  1921.    gone just because its pipe is closed.  */
  1922. #ifdef SIGCHLD
  1923.           else if (nread == 0 && !NETCONN_P (proc))
  1924.         ;
  1925. #endif
  1926.           else
  1927.         {
  1928.           /* Preserve status of processes already terminated.  */
  1929.           XSETINT (XPROCESS (proc)->tick, ++process_tick);
  1930.           deactivate_process (proc);
  1931.           if (!NULL (XPROCESS (proc)->raw_status_low))
  1932.             update_status (XPROCESS (proc));
  1933.           if (EQ (XPROCESS (proc)->status, Qrun))
  1934.             XPROCESS (proc)->status
  1935.               = Fcons (Qexit, Fcons (make_number (256), Qnil));
  1936.         }
  1937.         }
  1938.     } /* end for */
  1939.     } /* end while */
  1940.  
  1941.   /* If calling from keyboard input, do not quit
  1942.      since we want to return C-g as an input character.
  1943.      Otherwise, do pending quit if requested.  */
  1944.   if (read_kbd >= 0)
  1945.     {
  1946.       /* Prevent input_pending from remaining set if we quit.  */
  1947.       clear_input_pending ();
  1948.       QUIT;
  1949.     }
  1950. }
  1951.  
  1952. /* Actually call the filter.  This gets the information via variables
  1953.    because internal_condition_case won't pass arguments.  */
  1954.  
  1955. Lisp_Object
  1956. run_filter ()
  1957. {
  1958.   return call2 (this_filter, filter_process, filter_string);
  1959. }
  1960.  
  1961. /* Read pending output from the process channel,
  1962.    starting with our buffered-ahead character if we have one.
  1963.    Yield number of characters read.
  1964.  
  1965.    This function reads at most 1024 characters.
  1966.    If you want to read all available subprocess output,
  1967.    you must call it repeatedly until it returns zero.  */
  1968.  
  1969. read_process_output (proc, channel)
  1970.      Lisp_Object proc;
  1971.      register int channel;
  1972. {
  1973.   register int nchars;
  1974.   char chars[1024];
  1975.   register Lisp_Object outstream;
  1976.   register struct buffer *old = current_buffer;
  1977.   register struct Lisp_Process *p = XPROCESS (proc);
  1978.   register int opoint;
  1979.  
  1980.   if (proc_buffered_char[channel] < 0)
  1981.     nchars = read (channel, chars, sizeof chars);
  1982.   else
  1983.     {
  1984.       chars[0] = proc_buffered_char[channel];
  1985.       proc_buffered_char[channel] = -1;
  1986.       nchars = read (channel, chars + 1, sizeof chars - 1);
  1987.       if (nchars < 0)
  1988.     nchars = 1;
  1989.       else
  1990.     nchars = nchars + 1;
  1991.     }
  1992.  
  1993.   if (nchars <= 0) return nchars;
  1994.  
  1995.   outstream = p->filter;
  1996.   if (!NULL (outstream))
  1997.     {
  1998.       int count = specpdl_ptr - specpdl;
  1999.       specbind (Qinhibit_quit, Qt);
  2000.       this_filter = outstream;
  2001.       filter_process = proc;
  2002.       filter_string = make_string (chars, nchars);
  2003.       call2 (this_filter, filter_process, filter_string);
  2004.       /*   internal_condition_case (run_filter, Qerror, Fidentity);  */
  2005.       unbind_to (count);
  2006.       return nchars;
  2007.     }
  2008.  
  2009.   /* If no filter, write into buffer if it isn't dead.  */
  2010.   if (!NULL (p->buffer) && !NULL (XBUFFER (p->buffer)->name))
  2011.     {
  2012.       Lisp_Object tem;
  2013.  
  2014.       Fset_buffer (p->buffer);
  2015.       opoint = point;
  2016.  
  2017.       /* Insert new output into buffer
  2018.      at the current end-of-output marker,
  2019.      thus preserving logical ordering of input and output.  */
  2020.       if (XMARKER (p->mark)->buffer)
  2021.     SET_PT (marker_position (p->mark));
  2022.       else
  2023.     SET_PT (ZV);
  2024.       if (point <= opoint)
  2025.     opoint += nchars;
  2026.  
  2027.       tem = current_buffer->read_only;
  2028.       current_buffer->read_only = Qnil;
  2029.       insert (chars, nchars);
  2030.       current_buffer->read_only = tem;
  2031.       Fset_marker (p->mark, make_number (point), p->buffer);
  2032.       update_mode_lines++;
  2033.  
  2034.       SET_PT (opoint);
  2035.       set_buffer_internal (old);
  2036.     }
  2037.   return nchars;
  2038. }
  2039.  
  2040. DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, Swaiting_for_user_input_p,
  2041.        0, 0, 0,
  2042.   "Returns non-NIL if emacs is waiting for input from the user.\n\
  2043. This is intended for use by asynchronous process output filters and sentinels.")
  2044.        ()
  2045. {
  2046.   return ((waiting_for_user_input_p) ? Qt : Qnil);
  2047. }
  2048.  
  2049. /* Sending data to subprocess */
  2050.  
  2051. jmp_buf send_process_frame;
  2052.  
  2053. send_process_trap ()
  2054. {
  2055. #ifdef BSD4_1
  2056.   sigrelse (SIGPIPE);
  2057.   sigrelse (SIGALRM);
  2058. #endif /* BSD4_1 */
  2059.   longjmp (send_process_frame, 1);
  2060. }
  2061.  
  2062. send_process (proc, buf, len)
  2063.      Lisp_Object proc;
  2064.      char *buf;
  2065.      int len;
  2066. {
  2067.   /* Don't use register vars; longjmp can lose them.  */
  2068.   int rv;
  2069.   unsigned char *procname = XSTRING (XPROCESS (proc)->name)->data;
  2070.  
  2071.   if (!NULL (XPROCESS (proc)->raw_status_low))
  2072.     update_status (XPROCESS (proc));
  2073.   if (! EQ (XPROCESS (proc)->status, Qrun))
  2074.     error ("Process %s not running", procname);
  2075.  
  2076.   if (!setjmp (send_process_frame))
  2077.     while (len > 0)
  2078.       {
  2079.     signal (SIGPIPE, send_process_trap);
  2080.     rv = write (XFASTINT (XPROCESS (proc)->outfd), buf, len);
  2081.     signal (SIGPIPE, SIG_DFL);
  2082.     if (rv < 0)
  2083.       {
  2084.         if (0
  2085. #ifdef EWOULDBLOCK
  2086.         || errno == EWOULDBLOCK
  2087. #endif
  2088. #ifdef EAGAIN
  2089.         || errno == EAGAIN
  2090. #endif
  2091.         )
  2092.           {
  2093.         /* It would be nice to accept process output here,
  2094.            but that is difficult.  For example, it could
  2095.            garbage what we are sending if that is from a buffer.  */
  2096.         immediate_quit = 1;
  2097.         QUIT;
  2098.         sleep (1);
  2099.         immediate_quit = 0;
  2100.         continue;
  2101.           }
  2102.         report_file_error ("writing to process", Fcons (proc, Qnil));
  2103.       }
  2104.     buf += rv;
  2105.     len -= rv;
  2106.       }
  2107.   else
  2108.     {
  2109.       XPROCESS (proc)->raw_status_low = Qnil;
  2110.       XPROCESS (proc)->raw_status_high = Qnil;
  2111.       XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (256), Qnil));
  2112.       XSETINT (XPROCESS (proc)->tick, ++process_tick);
  2113.       deactivate_process (proc);
  2114.       error ("SIGPIPE raised on process %s; closed it", procname);
  2115.     }
  2116. }
  2117.  
  2118. DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region,
  2119.   3, 3, 0,
  2120.   "Send current contents of region as input to PROCESS.\n\
  2121. PROCESS may be a process name.\n\
  2122. Called from program, takes three arguments, PROCESS, START and END.")
  2123.   (process, start, end)
  2124.      Lisp_Object process, start, end;
  2125. {
  2126.   Lisp_Object proc;
  2127.   int start1;
  2128.  
  2129.   proc = get_process (process);
  2130.   validate_region (&start, &end);
  2131.  
  2132.   if (XINT (start) < GPT && XINT (end) > GPT)
  2133.     move_gap (start);
  2134.  
  2135.   start1 = XINT (start);
  2136.   send_process (proc, &FETCH_CHAR (start1), XINT (end) - XINT (start));
  2137.  
  2138.   return Qnil;
  2139. }
  2140.  
  2141. DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string,
  2142.   2, 2, 0,
  2143.   "Send PROCESS the contents of STRING as input.\n\
  2144. PROCESS may be a process name.")
  2145.   (process, string)
  2146.      Lisp_Object process, string;
  2147. {
  2148.   Lisp_Object proc;
  2149.   CHECK_STRING (string, 1);
  2150.   proc = get_process (process);
  2151.   send_process (proc, XSTRING (string)->data, XSTRING (string)->size);
  2152.   return Qnil;
  2153. }
  2154.  
  2155. /* send a signal number SIGNO to PROCESS.
  2156.    CURRENT_GROUP means send to the process group that currently owns
  2157.    the terminal being used to communicate with PROCESS.
  2158.    This is used for various commands in shell mode.
  2159.    If NOMSG is zero, insert signal-announcements into process's buffers
  2160.    right away.  */
  2161.  
  2162. process_send_signal (process, signo, current_group, nomsg)
  2163.      Lisp_Object process;
  2164.      int signo;
  2165.      Lisp_Object current_group;
  2166.      int nomsg;
  2167. {
  2168.   Lisp_Object proc;
  2169.   register struct Lisp_Process *p;
  2170.   int gid;
  2171.   int no_pgrp = 0;
  2172.  
  2173.   proc = get_process (process);
  2174.   p = XPROCESS (proc);
  2175.  
  2176.   if (!EQ (p->childp, Qt))
  2177.     error ("Process %s is not a subprocess",
  2178.        XSTRING (p->name)->data);
  2179.   if (!XFASTINT (p->infd))
  2180.     error ("Process %s is not active",
  2181.        XSTRING (p->name)->data);
  2182.  
  2183.   if (NULL (p->pty_flag))
  2184.     current_group = Qnil;
  2185.  
  2186. #ifdef TIOCGPGRP        /* Not sure about this! (fnf) */
  2187.   /* If we are using pgrps, get a pgrp number and make it negative.  */
  2188.   if (!NULL (current_group))
  2189.     {
  2190.       /* If possible, send signals to the entire pgrp
  2191.      by sending an input character to it.  */
  2192. #ifndef SIGNALS_VIA_CHARACTERS
  2193. #if defined (TIOCGLTC) && defined (TIOCGETC)
  2194.       struct tchars c;
  2195.       struct ltchars lc;
  2196.  
  2197.       switch (signo)
  2198.     {
  2199.     case SIGINT:
  2200.       ioctl (XFASTINT (p->infd), TIOCGETC, &c);
  2201.       send_process (proc, &c.t_intrc, 1);
  2202.       return Qnil;
  2203.     case SIGQUIT:
  2204.       ioctl (XFASTINT (p->infd), TIOCGETC, &c);
  2205.       send_process (proc, &c.t_quitc, 1);
  2206.       return Qnil;
  2207. #ifdef SIGTSTP
  2208.     case SIGTSTP:
  2209.       ioctl (XFASTINT (p->infd), TIOCGLTC, &lc);
  2210.       send_process (proc, &lc.t_suspc, 1);
  2211.       return Qnil;
  2212. #endif
  2213.     }
  2214. #endif /* have TIOCGLTC and have TIOCGETC */
  2215. #endif /* not SIGNALS_VIA_CHARACTERS */
  2216.       /* It is possible that the following code would work
  2217.      on other kinds of USG systems, not just on the IRIS.
  2218.      This should be tried in Emacs 19.  */
  2219. #ifdef SIGNALS_VIA_CHARACTERS
  2220.       struct termio t;
  2221.       switch (signo)
  2222.     {
  2223.     case SIGINT:
  2224.       ioctl (XFASTINT (p->infd), TCGETA, &t);
  2225.       send_process (proc, &t.c_cc[VINTR], 1);
  2226.       return Qnil;
  2227.     case SIGQUIT:
  2228.       ioctl (XFASTINT (p->infd), TCGETA, &t);
  2229.       send_process (proc, &t.c_cc[VQUIT], 1);
  2230.       return Qnil;
  2231. #ifdef SIGTSTP
  2232.     case SIGTSTP:
  2233.       ioctl (XFASTINT (p->infd), TCGETA, &t);
  2234.       send_process (proc, &t.c_cc[VSWTCH], 1);
  2235.       return Qnil;
  2236. #endif
  2237.     }
  2238. #endif /* SIGNALS_VIA_CHARACTERS */
  2239.  
  2240.       /* Get the pgrp using the tty itself, if we have that.
  2241.      Otherwise, use the pty to get the pgrp.  */
  2242. #if defined (pfa)
  2243.       /* TICGPGRP symbol defined in sys/ioctl.h at E50.
  2244.      But, TIOCGPGRP does not work on E50.
  2245.      This way, we will use -1, since the ioctl won't change it.
  2246.      (saka@pfu.fujitsu.co.JP.)  */
  2247.       gid = -1;
  2248. #endif
  2249.       if (!NULL (p->subtty))
  2250.     ioctl (XFASTINT (p->subtty), TIOCGPGRP, &gid);
  2251.       else
  2252.     ioctl (XFASTINT (p->infd), TIOCGPGRP, &gid);
  2253.       if (gid == -1)
  2254.     no_pgrp = 1;
  2255.       else
  2256.     gid = - gid;
  2257.     }
  2258.   else
  2259.     gid = - XFASTINT (p->pid);
  2260. #else /* not using pgrps */
  2261.   /* Can't select pgrps on this system, so we know that
  2262.      the child itself heads the pgrp.  */
  2263.   gid = - XFASTINT (p->pid);
  2264. #endif /* not using pgrps */
  2265.  
  2266.   switch (signo)
  2267.     {
  2268. #ifdef SIGCONT
  2269.     case SIGCONT:
  2270.       p->raw_status_low = Qnil;
  2271.       p->raw_status_high = Qnil;
  2272.       p->status = Qrun;
  2273.       XSETINT (p->tick, ++process_tick);
  2274.       if (!nomsg)
  2275.     status_notify ();
  2276.       break;
  2277. #endif
  2278.     case SIGINT:
  2279.     case SIGQUIT:
  2280.     case SIGKILL:
  2281.       flush_pending_output (XFASTINT (p->infd));
  2282.       break;
  2283.     }
  2284.  
  2285.   /* If we don't have process groups, send the signal to the immediate subprocess.
  2286.      That isn't really right, but it's better than any obvious alternative.  */
  2287.   if (no_pgrp)
  2288.     {
  2289.       kill (XFASTINT (p->pid), signo);
  2290.       return;
  2291.     }
  2292.  
  2293.   /* gid may be a pid, or minus a pgrp's number */
  2294. #ifdef TIOCSIGSEND
  2295.   if (!NULL (current_group))
  2296.     ioctl (XFASTINT (p->infd), TIOCSIGSEND, signo);
  2297.   else
  2298.     {
  2299.       gid = - XFASTINT (p->pid);
  2300.       kill (gid, signo);
  2301.     }
  2302. #else /* no TIOCSIGSEND */
  2303. #ifdef BSD
  2304.   /* On bsd, [man says] kill does not accept a negative number to kill a pgrp.
  2305.      Must do that differently.  */
  2306.   killpg (-gid, signo);
  2307. #else /* Not BSD.  */
  2308.   kill (gid, signo);
  2309. #endif /* Not BSD.  */
  2310. #endif /* no TIOCSIGSEND */
  2311. }
  2312.  
  2313. DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0,
  2314.   "Interrupt process PROCESS.  May be process or name of one.\n\
  2315. Nil or no arg means current buffer's process.\n\
  2316. Second arg CURRENT-GROUP non-nil means send signal to\n\
  2317. the current process-group of the process's controlling terminal\n\
  2318. rather than to the process's own process group.\n\
  2319. If the process is a shell, this means interrupt current subjob\n\
  2320. rather than the shell.")
  2321.   (process, current_group)
  2322.      Lisp_Object process, current_group;
  2323. {
  2324.   process_send_signal (process, SIGINT, current_group, 0);
  2325.   return process;
  2326. }
  2327.  
  2328. DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0,
  2329.   "Kill process PROCESS.  May be process or name of one.\n\
  2330. See function interrupt-process for more details on usage.")
  2331.   (process, current_group)
  2332.      Lisp_Object process, current_group;
  2333. {
  2334.   process_send_signal (process, SIGKILL, current_group, 0);
  2335.   return process;
  2336. }
  2337.  
  2338. DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0,
  2339.   "Send QUIT signal to process PROCESS.  May be process or name of one.\n\
  2340. See function interrupt-process for more details on usage.")
  2341.   (process, current_group)
  2342.      Lisp_Object process, current_group;
  2343. {
  2344.   process_send_signal (process, SIGQUIT, current_group, 0);
  2345.   return process;
  2346. }
  2347.  
  2348. DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0,
  2349.   "Stop process PROCESS.  May be process or name of one.\n\
  2350. See function interrupt-process for more details on usage.")
  2351.   (process, current_group)
  2352.      Lisp_Object process, current_group;
  2353. {
  2354. #ifndef SIGTSTP
  2355.   error ("no SIGTSTP support");
  2356. #else
  2357.   process_send_signal (process, SIGTSTP, current_group, 0);
  2358. #endif
  2359.   return process;
  2360. }
  2361.  
  2362. DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0,
  2363.   "Continue process PROCESS.  May be process or name of one.\n\
  2364. See function interrupt-process for more details on usage.")
  2365.   (process, current_group)
  2366.      Lisp_Object process, current_group;
  2367. {
  2368. #ifdef SIGCONT
  2369.     process_send_signal (process, SIGCONT, current_group, 0);
  2370. #else
  2371.     error ("no SIGCONT support");
  2372. #endif
  2373.   return process;
  2374. }
  2375.  
  2376. DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0,
  2377.   "Make PROCESS see end-of-file in its input.\n\
  2378. Eof comes after any text already sent to it.\n\
  2379. nil or no arg means current buffer's process.")
  2380.   (process)
  2381.      Lisp_Object process;
  2382. {
  2383.   Lisp_Object proc;
  2384.  
  2385.   proc = get_process (process);
  2386.   /* Sending a zero-length record is supposed to mean eof
  2387.      when TIOCREMOTE is turned on.  */
  2388. #ifdef DID_REMOTE
  2389.   {
  2390.     char buf[1];
  2391.     write (XFASTINT (XPROCESS (proc)->outfd), buf, 0);
  2392.   }
  2393. #else /* did not do TOICREMOTE */
  2394.   if (!NULL (XPROCESS (proc)->pty_flag))
  2395.     send_process (proc, "\004", 1);
  2396.   else
  2397.     {
  2398.       close (XPROCESS (proc)->outfd);
  2399.       XFASTINT (XPROCESS (proc)->outfd) = open ("/dev/null", O_WRONLY);
  2400.     }
  2401.  
  2402. #endif /* did not do TOICREMOTE */
  2403.   return process;
  2404. }
  2405.  
  2406. /* Kill all processes associated with `buffer'.
  2407.  If `buffer' is nil, kill all processes  */
  2408.  
  2409. kill_buffer_processes (buffer)
  2410.      Lisp_Object buffer;
  2411. {
  2412.   Lisp_Object tail, proc;
  2413.  
  2414.   for (tail = Vprocess_alist; XGCTYPE (tail) == Lisp_Cons;
  2415.        tail = XCONS (tail)->cdr)
  2416.     {
  2417.       proc = XCONS (XCONS (tail)->car)->cdr;
  2418.       if (XGCTYPE (proc) == Lisp_Process
  2419.       && (NULL (buffer) || EQ (XPROCESS (proc)->buffer, buffer)))
  2420.     {
  2421.       if (NETCONN_P (proc))
  2422.         deactivate_process (proc);
  2423.       else if (XFASTINT (XPROCESS (proc)->infd))
  2424.         process_send_signal (proc, SIGHUP, Qnil, 1);
  2425.     }
  2426.     }
  2427. }
  2428.  
  2429. /* On receipt of a signal that a child status has changed,
  2430.  loop asking about children with changed statuses until
  2431.  the system says there are no more.
  2432.    All we do is change the status;
  2433.  we do not run sentinels or print notifications.
  2434.  That is saved for the next time keyboard input is done,
  2435.  in order to avoid timing errors.  */
  2436.  
  2437. /** WARNING: this can be called during garbage collection.
  2438.  Therefore, it must not be fooled by the presence of mark bits in
  2439.  Lisp objects.  */
  2440.  
  2441. /** USG WARNING:  Although it is not obvious from the documentation
  2442.  in signal(2), on a USG system the SIGCLD handler MUST NOT call
  2443.  signal() before executing at least one wait(), otherwise the handler
  2444.  will be called again, resulting in an infinite loop.  The relevant
  2445.  portion of the documentation reads "SIGCLD signals will be queued
  2446.  and the signal-catching function will be continually reentered until
  2447.  the queue is empty".  Invoking signal() causes the kernel to reexamine
  2448.  the SIGCLD queue.   Fred Fish, UniSoft Systems Inc. */
  2449.  
  2450. sigchld_handler (signo)
  2451.      int signo;
  2452. {
  2453.   int old_errno = errno;
  2454.   Lisp_Object proc;
  2455.   register struct Lisp_Process *p;
  2456.  
  2457. #ifdef BSD4_1
  2458.   extern int synch_process_pid;
  2459.   extern int sigheld;
  2460.   sigheld |= sigbit (SIGCHLD);
  2461. #endif
  2462.  
  2463.   while (1)
  2464.     {
  2465.       register int pid;
  2466.       WAITTYPE w;
  2467.       Lisp_Object tail;
  2468.  
  2469. #ifdef WNOHANG
  2470. #ifndef WUNTRACED
  2471. #define WUNTRACED 0
  2472. #endif /* no WUNTRACED */
  2473.       /* Keep trying to get a status until we get a definitive result.  */
  2474.       do 
  2475.     {
  2476.       errno = 0;
  2477.       pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
  2478.     }
  2479.       while (pid <= 0 && errno == EINTR);
  2480.  
  2481.       if (pid <= 0)
  2482.     {
  2483.       /* A real failure.  We have done all our job, so return.  */
  2484.  
  2485.       /* USG systems forget handlers when they are used;
  2486.          must reestablish each time */
  2487. #ifdef USG
  2488.       signal (signo, sigchld_handler);   /* WARNING - must come after wait3() */
  2489. #endif
  2490. #ifdef  BSD4_1
  2491.       sigheld &= ~sigbit (SIGCHLD);
  2492.       sigrelse (SIGCHLD);
  2493. #endif
  2494.       errno = old_errno;
  2495.       return;
  2496.     }
  2497. #else
  2498.       pid = wait (&w);
  2499. #endif /* no WNOHANG */
  2500.  
  2501. #ifdef BSD4_1
  2502.       if (synch_process_pid == pid)
  2503.     synch_process_pid = 0;         /* Zero it to show process has died. */
  2504. #endif
  2505.  
  2506.       /* Find the process that signaled us, and record its status.  */
  2507.  
  2508.       p = 0;
  2509.       for (tail = Vprocess_alist; XSYMBOL (tail) != XSYMBOL (Qnil); tail = XCONS (tail)->cdr)
  2510.     {
  2511.       proc = XCONS (XCONS (tail)->car)->cdr;
  2512.       p = XPROCESS (proc);
  2513.       if (EQ (p->childp, Qt) && XFASTINT (p->pid) == pid)
  2514.         break;
  2515.       p = 0;
  2516.     }
  2517.  
  2518.       /* If we don't recognize the pid number,
  2519.      look for a process being created.  */
  2520.  
  2521.       if (p == 0)
  2522.     for (tail = Vprocess_alist; XSYMBOL (tail) != XSYMBOL (Qnil); tail = XCONS (tail)->cdr)
  2523.       {
  2524.         proc = XCONS (XCONS (tail)->car)->cdr;
  2525.         p = XPROCESS (proc);
  2526.         if (XINT (p->pid) == -1)
  2527.           break;
  2528.         p = 0;
  2529.       }
  2530.  
  2531.       /* Change the status of the process that was found.  */
  2532.  
  2533.       if (p != 0)
  2534.     {
  2535.       union { int i; WAITTYPE wt; } u;
  2536.  
  2537.       XSETINT (p->tick, ++process_tick);
  2538.       u.wt = w;
  2539.       XFASTINT (p->raw_status_low) = u.i & 0xffff;
  2540.       XFASTINT (p->raw_status_high) = u.i >> 16;
  2541.  
  2542.       /* If process has terminated, stop waiting for its output.  */
  2543.       if (WIFSIGNALED (w) || WIFEXITED (w))
  2544.         if (p->infd)
  2545.           FD_CLR (p->infd, &input_wait_mask);
  2546.     }
  2547.       else
  2548.     {
  2549.       /* Report the status of the synchronous process.  */
  2550.       if (WIFEXITED (w))
  2551.         synch_process_retcode = WRETCODE (w);
  2552.       else if (WIFSIGNALED (w))
  2553.         synch_process_death = sys_siglist[WTERMSIG (w)];
  2554.     }
  2555.  
  2556.       /* On some systems, we must return right away.
  2557.      If any more processes want to signal us, we will
  2558.      get another signal.
  2559.      Otherwise (on systems that have WNOHANG), loop around
  2560.      to use up all the processes that have something to tell us.  */
  2561. #if defined (USG) && ! (defined (HPUX) && defined (WNOHANG))
  2562. #ifdef USG
  2563.       signal (signo, sigchld_handler);
  2564. #endif
  2565.       errno = old_errno;
  2566.       return;
  2567. #endif /* USG, but not HPUX with WNOHANG */
  2568.     }
  2569. }
  2570.  
  2571. /* Report all recent events of a change in process status
  2572.    (either run the sentinel or output a message).
  2573.    This is done while Emacs is waiting for keyboard input.  */
  2574.  
  2575. status_notify ()
  2576. {
  2577.   register Lisp_Object proc, buffer;
  2578.   Lisp_Object tail = Qnil;
  2579.   Lisp_Object msg = Qnil;
  2580.   struct gcpro gcpro1, gcpro2;
  2581.  
  2582.   /* We need to gcpro tail; if read_process_output calls a filter
  2583.      which deletes a process and removes the cons to which tail points
  2584.      from Vprocess_alist, tail becomes an unprotected reference.  */
  2585.   GCPRO2 (tail, msg);
  2586.  
  2587.   for (tail = Vprocess_alist; !NULL (tail); tail = Fcdr (tail))
  2588.     {
  2589.       Lisp_Object symbol;
  2590.       register struct Lisp_Process *p;
  2591.  
  2592.       proc = Fcdr (Fcar (tail));
  2593.       p = XPROCESS (proc);
  2594.  
  2595.       if (XINT (p->tick) != XINT (p->update_tick))
  2596.     {
  2597.       XSETINT (p->update_tick, XINT (p->tick));
  2598.  
  2599.       /* If process is still active, read any output that remains.  */
  2600.       if (XFASTINT (p->infd))
  2601.         while (read_process_output (proc, XFASTINT (p->infd)) > 0);
  2602.  
  2603.       buffer = p->buffer;
  2604.  
  2605.       /* Get the text to use for the message.  */
  2606.       if (!NULL (p->raw_status_low))
  2607.         update_status (p);
  2608.       msg = status_message (p->status);
  2609.  
  2610.       /* If process is terminated, deactivate it or delete it.  */
  2611.       symbol = p->status;
  2612.       if (XTYPE (p->status) == Lisp_Cons)
  2613.         symbol = XCONS (p->status)->car;
  2614.  
  2615.       if (EQ (symbol, Qsignal) || EQ (symbol, Qexit)
  2616.           || EQ (symbol, Qclosed))
  2617.         {
  2618.           if (delete_exited_processes)
  2619.         remove_process (proc);
  2620.           else
  2621.         deactivate_process (proc);
  2622.         }
  2623.  
  2624.       /* Now output the message suitably.  */
  2625.       if (!NULL (p->sentinel))
  2626.         exec_sentinel (proc, msg);
  2627.       /* Don't bother with a message in the buffer
  2628.          when a process becomes runnable.  */
  2629.       else if (!EQ (symbol, Qrun) && !NULL (buffer))
  2630.         {
  2631.           Lisp_Object ro = XBUFFER (buffer)->read_only;
  2632.           Lisp_Object tem;
  2633.           struct buffer *old = current_buffer;
  2634.           int opoint;
  2635.  
  2636.           /* Avoid error if buffer is deleted
  2637.          (probably that's why the process is dead, too) */
  2638.           if (NULL (XBUFFER (buffer)->name))
  2639.         continue;
  2640.           Fset_buffer (buffer);
  2641.           opoint = point;
  2642.           /* Insert new output into buffer
  2643.          at the current end-of-output marker,
  2644.          thus preserving logical ordering of input and output.  */
  2645.           if (XMARKER (p->mark)->buffer)
  2646.         SET_PT (marker_position (p->mark));
  2647.           else
  2648.         SET_PT (ZV);
  2649.           if (point <= opoint)
  2650.         opoint += XSTRING (msg)->size + XSTRING (p->name)->size + 10;
  2651.  
  2652.           tem = current_buffer->read_only;
  2653.           current_buffer->read_only = Qnil;
  2654.           InsStr ("\nProcess ");
  2655.           Finsert (1, &p->name);
  2656.           InsStr (" ");
  2657.           Finsert (1, &msg);
  2658.           current_buffer->read_only = tem;
  2659.           Fset_marker (p->mark, make_number (point), p->buffer);
  2660.  
  2661.           SET_PT (opoint);
  2662.           set_buffer_internal (old);
  2663.         }
  2664.     }
  2665.     } /* end for */
  2666.  
  2667.   update_mode_lines++;  /* in case buffers use %s in mode-line-format */
  2668.   redisplay_preserve_echo_area ();
  2669.  
  2670.   update_tick = process_tick;
  2671.  
  2672.   UNGCPRO;
  2673. }
  2674.  
  2675. exec_sentinel (proc, reason)
  2676.      Lisp_Object proc, reason;
  2677. {
  2678.   Lisp_Object sentinel;
  2679.   register struct Lisp_Process *p = XPROCESS (proc);
  2680.   int count = specpdl_ptr - specpdl;
  2681.  
  2682.   sentinel = p->sentinel;
  2683.   if (NULL (sentinel))
  2684.     return;
  2685.  
  2686.   p->sentinel = Qnil;
  2687.   specbind (Qinhibit_quit, Qt);
  2688.   this_filter = sentinel;
  2689.   filter_process = proc;
  2690.   filter_string = reason;
  2691.   call2 (this_filter, filter_process, filter_string);
  2692. /*   internal_condition_case (run_filter, Qerror, Fidentity);  */
  2693.   unbind_to (count);
  2694.   p->sentinel = sentinel;
  2695. }
  2696.  
  2697. init_process ()
  2698. {
  2699.   register int i;
  2700.  
  2701. #ifdef SIGCHLD
  2702. #ifndef CANNOT_DUMP
  2703.   if (! noninteractive || initialized)
  2704. #endif
  2705.     signal (SIGCHLD, sigchld_handler);
  2706. #endif
  2707.  
  2708.   FD_ZERO (&input_wait_mask);
  2709.   FD_SET (0, &input_wait_mask);
  2710.   Vprocess_alist = Qnil;
  2711.   for (i = 0; i < MAXDESC; i++)
  2712.     {
  2713.       chan_process[i] = Qnil;
  2714.       proc_buffered_char[i] = -1;
  2715.     }
  2716. }
  2717.  
  2718. syms_of_process ()
  2719. {
  2720.   Qprocessp = intern ("processp");
  2721.   staticpro (&Qprocessp);
  2722.   Qrun = intern ("run");
  2723.   staticpro (&Qrun);
  2724.   Qstop = intern ("stop");
  2725.   staticpro (&Qstop);
  2726.   Qsignal = intern ("signal");
  2727.   staticpro (&Qsignal);
  2728.  
  2729.   /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
  2730.      here again.
  2731.  
  2732.      Qexit = intern ("exit");
  2733.      staticpro (&Qexit); */
  2734.  
  2735.   Qopen = intern ("open");
  2736.   staticpro (&Qopen);
  2737.   Qclosed = intern ("closed");
  2738.   staticpro (&Qclosed);
  2739.  
  2740.   staticpro (&Vprocess_alist);
  2741.  
  2742.   DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
  2743.     "*Non-nil means delete processes immediately when they exit.\n\
  2744. nil means don't delete them until `list-processes' is run.");
  2745.  
  2746.   delete_exited_processes = 1;
  2747.  
  2748.   DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type,
  2749.     "Control type of device used to communicate with subprocesses.\n\
  2750. Values are nil to use a pipe, t for a pty (or pipe if ptys not supported).\n\
  2751. Value takes effect when `start-process' is called.");
  2752.   Vprocess_connection_type = Qt;
  2753.  
  2754.   defsubr (&Sprocessp);
  2755.   defsubr (&Sget_process);
  2756.   defsubr (&Sget_buffer_process);
  2757.   defsubr (&Sdelete_process);
  2758.   defsubr (&Sprocess_status);
  2759.   defsubr (&Sprocess_exit_status);
  2760.   defsubr (&Sprocess_id);
  2761.   defsubr (&Sprocess_name);
  2762.   defsubr (&Sprocess_command);
  2763.   defsubr (&Sset_process_buffer);
  2764.   defsubr (&Sprocess_buffer);
  2765.   defsubr (&Sprocess_mark);
  2766.   defsubr (&Sset_process_filter);
  2767.   defsubr (&Sprocess_filter);
  2768.   defsubr (&Sset_process_sentinel);
  2769.   defsubr (&Sprocess_sentinel);
  2770.   defsubr (&Sprocess_kill_without_query);
  2771.   defsubr (&Slist_processes);
  2772.   defsubr (&Sprocess_list);
  2773.   defsubr (&Sstart_process);
  2774. #ifdef HAVE_SOCKETS
  2775.   defsubr (&Sopen_network_stream);
  2776. #endif /* HAVE_SOCKETS */
  2777.   defsubr (&Saccept_process_output);
  2778.   defsubr (&Sprocess_send_region);
  2779.   defsubr (&Sprocess_send_string);
  2780.   defsubr (&Sinterrupt_process);
  2781.   defsubr (&Skill_process);
  2782.   defsubr (&Squit_process);
  2783.   defsubr (&Sstop_process);
  2784.   defsubr (&Scontinue_process);
  2785.   defsubr (&Sprocess_send_eof);
  2786.   defsubr (&Swaiting_for_user_input_p);
  2787. }
  2788.  
  2789. #endif /* subprocesses */
  2790.