home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / src / process.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-13  |  67.9 KB  |  2,579 lines

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