home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / Editors / emacs / Emacs-1.14b1-sources / sources / orig-emacs-src / process.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-23  |  73.8 KB  |  2,771 lines  |  [TEXT/EMAC]

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