home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / e20313sr.zip / emacs / 20.3.1 / src / process.c < prev    next >
C/C++ Source or Header  |  1999-07-31  |  146KB  |  4,900 lines

  1. /* Asynchronous subprocess control for GNU Emacs.
  2.    Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 96, 1998
  3.       Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Emacs.
  6.  
  7. GNU Emacs is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU Emacs is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU Emacs; see the file COPYING.  If not, write to
  19. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.  */
  21.  
  22. /* Modified for emx by Jeremy Bowen, May 1999 based on patches
  23.    to v19.33 by Eberhard Mattes */
  24.  
  25. #include <signal.h>
  26.  
  27. #include <config.h>
  28.  
  29. /* This file is split into two parts by the following preprocessor
  30.    conditional.  The 'then' clause contains all of the support for
  31.    asynchronous subprocesses.  The 'else' clause contains stub
  32.    versions of some of the asynchronous subprocess routines that are
  33.    often called elsewhere in Emacs, so we don't have to #ifdef the
  34.    sections that call them.  */
  35.  
  36.  
  37. #ifdef subprocesses
  38.  
  39. #include <stdio.h>
  40. #include <errno.h>
  41. #include <setjmp.h>
  42. #include <sys/types.h>        /* some typedefs are used in sys/file.h */
  43. #include <sys/file.h>
  44. #include <sys/stat.h>
  45. #ifdef HAVE_UNISTD_H
  46. #include <unistd.h>
  47. #endif
  48.  
  49. #ifdef EMX
  50. #include <stdlib.h>
  51. #endif /* EMX */
  52.  
  53. #ifdef WINDOWSNT
  54. #include <stdlib.h>
  55. #include <fcntl.h>
  56. #endif /* not WINDOWSNT */
  57.  
  58. #ifdef HAVE_SOCKETS    /* TCP connection support, if kernel can do it */
  59. #include <sys/socket.h>
  60. #include <netdb.h>
  61. #include <netinet/in.h>
  62. #include <arpa/inet.h>
  63. #ifdef NEED_NET_ERRNO_H
  64. #include <net/errno.h>
  65. #endif /* NEED_NET_ERRNO_H */
  66. #endif /* HAVE_SOCKETS */
  67.  
  68. /* TERM is a poor-man's SLIP, used on Linux.  */
  69. #ifdef TERM
  70. #include <client.h>
  71. #endif
  72.  
  73. /* On some systems, e.g. DGUX, inet_addr returns a 'struct in_addr'. */
  74. #ifdef HAVE_BROKEN_INET_ADDR
  75. #define IN_ADDR struct in_addr
  76. #define NUMERIC_ADDR_ERROR (numeric_addr.s_addr == -1)
  77. #else
  78. #define IN_ADDR unsigned long
  79. #define NUMERIC_ADDR_ERROR (numeric_addr == -1)
  80. #endif
  81.  
  82. #if defined(BSD_SYSTEM) || defined(STRIDE)
  83. #include <sys/ioctl.h>
  84. #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
  85. #include <fcntl.h>
  86. #endif /* HAVE_PTYS and no O_NDELAY */
  87. #endif /* BSD_SYSTEM || STRIDE */
  88.  
  89. #ifdef BROKEN_O_NONBLOCK
  90. #undef O_NONBLOCK
  91. #endif /* BROKEN_O_NONBLOCK */
  92.  
  93. #ifdef NEED_BSDTTY
  94. #include <bsdtty.h>
  95. #endif
  96.  
  97. #ifdef IRIS
  98. #include <sys/sysmacros.h>    /* for "minor" */
  99. #endif /* not IRIS */
  100.  
  101. #include "systime.h"
  102. #include "systty.h"
  103.  
  104. #include "lisp.h"
  105. #include "window.h"
  106. #include "buffer.h"
  107. #include "charset.h"
  108. #include "coding.h"
  109. #include "process.h"
  110. #include "termhooks.h"
  111. #include "termopts.h"
  112. #include "commands.h"
  113. #include "frame.h"
  114. #include "blockinput.h"
  115. #include "keyboard.h"
  116. #include "dispextern.h"
  117.  
  118. #ifdef EMX
  119. extern Lisp_Object find_program_name_handler ();
  120. extern Lisp_Object Qprogram_name_handler_alist;
  121. extern Lisp_Object Vprogram_name_handler_alist;
  122. static Lisp_Object Qstart_process;
  123. #endif /* EMX */
  124.  
  125. #define max(a, b) ((a) > (b) ? (a) : (b))
  126.  
  127. Lisp_Object Qprocessp;
  128. Lisp_Object Qrun, Qstop, Qsignal, Qopen, Qclosed;
  129. Lisp_Object Qlast_nonmenu_event;
  130. /* Qexit is declared and initialized in eval.c.  */
  131.  
  132. /* a process object is a network connection when its childp field is neither
  133.    Qt nor Qnil but is instead a cons cell (HOSTNAME PORTNUM).  */
  134.  
  135. #ifdef HAVE_SOCKETS
  136. #define NETCONN_P(p) (GC_CONSP (XPROCESS (p)->childp))
  137. #else
  138. #define NETCONN_P(p) 0
  139. #endif /* HAVE_SOCKETS */
  140.  
  141. /* Define first descriptor number available for subprocesses.  */
  142. #ifdef VMS
  143. #define FIRST_PROC_DESC 1
  144. #else /* Not VMS */
  145. #define FIRST_PROC_DESC 3
  146. #endif
  147.  
  148. /* Define SIGCHLD as an alias for SIGCLD.  There are many conditionals
  149.    testing SIGCHLD.  */
  150.  
  151. #if !defined (SIGCHLD) && defined (SIGCLD)
  152. #define SIGCHLD SIGCLD
  153. #endif /* SIGCLD */
  154.  
  155. #include "syssignal.h"
  156.  
  157. #include "syswait.h"
  158.  
  159. extern int errno;
  160. extern char *strerror ();
  161. #ifdef VMS
  162. extern char *sys_errlist[];
  163. #endif
  164.  
  165. #ifndef HAVE_H_ERRNO
  166. extern int h_errno;
  167. #endif
  168.  
  169. #ifndef SYS_SIGLIST_DECLARED
  170. #ifndef VMS
  171. #ifndef BSD4_1
  172. #ifndef WINDOWSNT
  173. #ifndef LINUX
  174. extern char *sys_siglist[];
  175. #endif /* not LINUX */
  176. #else /* BSD4_1 */
  177. char *sys_siglist[] =
  178.   {
  179.     "bum signal!!",
  180.     "hangup",
  181.     "interrupt",
  182.     "quit",
  183.     "illegal instruction",
  184.     "trace trap",
  185.     "iot instruction",
  186.     "emt instruction",
  187.     "floating point exception",
  188.     "kill",
  189.     "bus error",
  190.     "segmentation violation",
  191.     "bad argument to system call",
  192.     "write on a pipe with no one to read it",
  193.     "alarm clock",
  194.     "software termination signal from kill",
  195.     "status signal",
  196.     "sendable stop signal not from tty",
  197.     "stop signal from tty",
  198.     "continue a stopped process",
  199.     "child status has changed",
  200.     "background read attempted from control tty",
  201.     "background write attempted from control tty",
  202.     "input record available at control tty",
  203.     "exceeded CPU time limit",
  204.     "exceeded file size limit"
  205.     };
  206. #endif /* not WINDOWSNT */
  207. #endif
  208. #endif /* VMS */
  209. #endif /* ! SYS_SIGLIST_DECLARED */
  210.  
  211. /* t means use pty, nil means use a pipe,
  212.    maybe other values to come.  */
  213. static Lisp_Object Vprocess_connection_type;
  214.  
  215. #ifdef SKTPAIR
  216. #ifndef HAVE_SOCKETS
  217. #include <sys/socket.h>
  218. #endif
  219. #endif /* SKTPAIR */
  220.  
  221. /* These next two vars are non-static since sysdep.c uses them in the
  222.    emulation of `select'.  */
  223. /* Number of events of change of status of a process.  */
  224. int process_tick;
  225. /* Number of events for which the user or sentinel has been notified.  */
  226. int update_tick;
  227.  
  228. #include "sysselect.h"
  229.  
  230. /* If we support a window system, turn on the code to poll periodically
  231.    to detect C-g.  It isn't actually used when doing interrupt input.  */
  232. #ifdef HAVE_WINDOW_SYSTEM
  233. #ifndef HAVE_PM
  234. #define POLL_FOR_INPUT
  235. #endif /* not HAVE_PM */
  236. #endif
  237.  
  238. /* Mask of bits indicating the descriptors that we wait for input on.  */
  239.  
  240. static SELECT_TYPE input_wait_mask;
  241.  
  242. /* Mask that excludes keyboard input descriptor (s).  */
  243.  
  244. static SELECT_TYPE non_keyboard_wait_mask;
  245.  
  246. /* Mask that excludes process input descriptor (s).  */
  247.  
  248. static SELECT_TYPE non_process_wait_mask;
  249.  
  250. /* The largest descriptor currently in use for a process object.  */
  251. static int max_process_desc;
  252.  
  253. /* The largest descriptor currently in use for keyboard input.  */
  254. static int max_keyboard_desc;
  255.  
  256. /* Nonzero means delete a process right away if it exits.  */
  257. static int delete_exited_processes;
  258.  
  259. /* Indexed by descriptor, gives the process (if any) for that descriptor */
  260. Lisp_Object chan_process[MAXDESC];
  261.  
  262. /* Alist of elements (NAME . PROCESS) */
  263. Lisp_Object Vprocess_alist;
  264.  
  265. /* Buffered-ahead input char from process, indexed by channel.
  266.    -1 means empty (no char is buffered).
  267.    Used on sys V where the only way to tell if there is any
  268.    output from the process is to read at least one char.
  269.    Always -1 on systems that support FIONREAD.  */
  270.  
  271. /* Don't make static; need to access externally.  */
  272. int proc_buffered_char[MAXDESC];
  273.  
  274. /* Table of `struct coding-system' for each process.  */
  275. static struct coding_system *proc_decode_coding_system[MAXDESC];
  276. static struct coding_system *proc_encode_coding_system[MAXDESC];
  277.  
  278. static Lisp_Object get_process ();
  279.  
  280. extern EMACS_TIME timer_check ();
  281. extern int timers_run;
  282.  
  283. /* Maximum number of bytes to send to a pty without an eof.  */
  284. static int pty_max_bytes;
  285.  
  286. extern Lisp_Object Vfile_name_coding_system, Vdefault_file_name_coding_system;
  287.  
  288. #ifdef HAVE_PTYS
  289. /* The file name of the pty opened by allocate_pty.  */
  290.  
  291. static char pty_name[24];
  292. #endif
  293.  
  294. /* Compute the Lisp form of the process status, p->status, from
  295.    the numeric status that was returned by `wait'.  */
  296.  
  297. Lisp_Object status_convert ();
  298.  
  299. void
  300. update_status (p)
  301.      struct Lisp_Process *p;
  302. {
  303.   union { int i; WAITTYPE wt; } u;
  304.   u.i = XFASTINT (p->raw_status_low) + (XFASTINT (p->raw_status_high) << 16);
  305.   p->status = status_convert (u.wt);
  306.   p->raw_status_low = Qnil;
  307.   p->raw_status_high = Qnil;
  308. }
  309.  
  310. /*  Convert a process status word in Unix format to 
  311.     the list that we use internally.  */
  312.  
  313. Lisp_Object
  314. status_convert (w)
  315.      WAITTYPE w;
  316. {
  317.   if (WIFSTOPPED (w))
  318.     return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil));
  319.   else if (WIFEXITED (w))
  320.     return Fcons (Qexit, Fcons (make_number (WRETCODE (w)),
  321.                 WCOREDUMP (w) ? Qt : Qnil));
  322.   else if (WIFSIGNALED (w))
  323.     return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)),
  324.                   WCOREDUMP (w) ? Qt : Qnil));
  325.   else
  326.     return Qrun;
  327. }
  328.  
  329. /* Given a status-list, extract the three pieces of information
  330.    and store them individually through the three pointers.  */
  331.  
  332. void
  333. decode_status (l, symbol, code, coredump)
  334.      Lisp_Object l;
  335.      Lisp_Object *symbol;
  336.      int *code;
  337.      int *coredump;
  338. {
  339.   Lisp_Object tem;
  340.  
  341.   if (SYMBOLP (l))
  342.     {
  343.       *symbol = l;
  344.       *code = 0;
  345.       *coredump = 0;
  346.     }
  347.   else
  348.     {
  349.       *symbol = XCONS (l)->car;
  350.       tem = XCONS (l)->cdr;
  351.       *code = XFASTINT (XCONS (tem)->car);
  352.       tem = XCONS (tem)->cdr;
  353.       *coredump = !NILP (tem);
  354.     }
  355. }
  356.  
  357. /* Return a string describing a process status list.  */
  358.  
  359. Lisp_Object 
  360. status_message (status)
  361.      Lisp_Object status;
  362. {
  363.   Lisp_Object symbol;
  364.   int code, coredump;
  365.   Lisp_Object string, string2;
  366.  
  367.   decode_status (status, &symbol, &code, &coredump);
  368.  
  369.   if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
  370.     {
  371.       char *signame = 0;
  372.       if (code < NSIG)
  373.     {
  374. #ifndef VMS
  375.       /* Cast to suppress warning if the table has const char *.  */
  376.       signame = (char *) sys_siglist[code];
  377. #else
  378.       signame = sys_errlist[code];
  379. #endif
  380.     }
  381.       if (signame == 0)
  382.     signame = "unknown";
  383.       string = build_string (signame);
  384.       string2 = build_string (coredump ? " (core dumped)\n" : "\n");
  385.       XSTRING (string)->data[0] = DOWNCASE (XSTRING (string)->data[0]);
  386.       return concat2 (string, string2);
  387.     }
  388.   else if (EQ (symbol, Qexit))
  389.     {
  390.       if (code == 0)
  391.     return build_string ("finished\n");
  392.       string = Fnumber_to_string (make_number (code));
  393.       string2 = build_string (coredump ? " (core dumped)\n" : "\n");
  394.       return concat2 (build_string ("exited abnormally with code "),
  395.               concat2 (string, string2));
  396.     }
  397.   else
  398.     return Fcopy_sequence (Fsymbol_name (symbol));
  399. }
  400.  
  401. #ifdef HAVE_PTYS
  402.  
  403. /* Open an available pty, returning a file descriptor.
  404.    Return -1 on failure.
  405.    The file name of the terminal corresponding to the pty
  406.    is left in the variable pty_name.  */
  407.  
  408. int
  409. allocate_pty ()
  410. {
  411.   struct stat stb;
  412.   register int c, i;
  413.   int fd;
  414.  
  415.   /* Some systems name their pseudoterminals so that there are gaps in
  416.      the usual sequence - for example, on HP9000/S700 systems, there
  417.      are no pseudoterminals with names ending in 'f'.  So we wait for
  418.      three failures in a row before deciding that we've reached the
  419.      end of the ptys.  */
  420.   int failed_count = 0;
  421.  
  422. #ifdef EMX
  423.   if (_osmode != OS2_MODE || _emx_rev < 37)
  424.     return -1;
  425. #endif /* EMX */
  426.  
  427. #ifdef PTY_ITERATION
  428.   PTY_ITERATION
  429. #else
  430.   for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
  431.     for (i = 0; i < 16; i++)
  432. #endif
  433.       {
  434. #ifdef PTY_NAME_SPRINTF
  435.     PTY_NAME_SPRINTF
  436. #else
  437.     sprintf (pty_name, "/dev/pty%c%x", c, i);
  438. #endif /* no PTY_NAME_SPRINTF */
  439.  
  440. #ifdef PTY_OPEN
  441.     PTY_OPEN;
  442. #else /* no PTY_OPEN */
  443. #ifdef IRIS
  444.     /* Unusual IRIS code */
  445.      *ptyv = open ("/dev/ptc", O_RDWR | O_NDELAY, 0);
  446.      if (fd < 0)
  447.        return -1;
  448.     if (fstat (fd, &stb) < 0)
  449.       return -1;
  450. #else /* not IRIS */
  451. #ifndef EMX
  452.     if (stat (pty_name, &stb) < 0)
  453.       {
  454.         failed_count++;
  455.         if (failed_count >= 3)
  456.           return -1;
  457.       }
  458.     else
  459.       failed_count = 0;
  460. #endif /* not EMX */
  461. #ifdef O_NONBLOCK
  462.     fd = open (pty_name, O_RDWR | O_NONBLOCK, 0);
  463. #else
  464.     fd = open (pty_name, O_RDWR | O_NDELAY, 0);
  465. #endif
  466. #endif /* not IRIS */
  467. #endif /* no PTY_OPEN */
  468.  
  469.     if (fd >= 0)
  470.       {
  471.         /* check to make certain that both sides are available
  472.            this avoids a nasty yet stupid bug in rlogins */
  473. #ifdef PTY_TTY_NAME_SPRINTF
  474.         PTY_TTY_NAME_SPRINTF
  475. #else
  476.             sprintf (pty_name, "/dev/tty%c%x", c, i);
  477. #endif /* no PTY_TTY_NAME_SPRINTF */
  478. #ifndef EMX
  479. #ifndef UNIPLUS
  480.         if (access (pty_name, 6) != 0)
  481.           {
  482.         close (fd);
  483. #if !defined(IRIS) && !defined(__sgi)
  484.         continue;
  485. #else
  486.         return -1;
  487. #endif /* IRIS */
  488.           }
  489. #endif /* not UNIPLUS */
  490. #endif /* not EMX */
  491.         setup_pty (fd);
  492.         return fd;
  493.       }
  494.       }
  495.   return -1;
  496. }
  497. #endif /* HAVE_PTYS */
  498.  
  499. Lisp_Object
  500. make_process (name)
  501.      Lisp_Object name;
  502. {
  503.   struct Lisp_Vector *vec;
  504.   register Lisp_Object val, tem, name1;
  505.   register struct Lisp_Process *p;
  506.   char suffix[10];
  507.   register int i;
  508.  
  509.   vec = allocate_vectorlike ((EMACS_INT) VECSIZE (struct Lisp_Process));
  510.   for (i = 0; i < VECSIZE (struct Lisp_Process); i++)
  511.     vec->contents[i] = Qnil;
  512.   vec->size = VECSIZE (struct Lisp_Process);
  513.   p = (struct Lisp_Process *)vec;
  514.  
  515.   XSETINT (p->infd, -1);
  516.   XSETINT (p->outfd, -1);
  517.   XSETFASTINT (p->pid, 0);
  518.   XSETFASTINT (p->tick, 0);
  519.   XSETFASTINT (p->update_tick, 0);
  520.   p->raw_status_low = Qnil;
  521.   p->raw_status_high = Qnil;
  522.   p->status = Qrun;
  523.   p->mark = Fmake_marker ();
  524.  
  525.   /* If name is already in use, modify it until it is unused.  */
  526.  
  527.   name1 = name;
  528.   for (i = 1; ; i++)
  529.     {
  530.       tem = Fget_process (name1);
  531.       if (NILP (tem)) break;
  532.       sprintf (suffix, "<%d>", i);
  533.       name1 = concat2 (name, build_string (suffix));
  534.     }
  535.   name = name1;
  536.   p->name = name;
  537.   XSETPROCESS (val, p);
  538.   Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
  539.   return val;
  540. }
  541.  
  542. void
  543. remove_process (proc)
  544.      register Lisp_Object proc;
  545. {
  546.   register Lisp_Object pair;
  547.  
  548.   pair = Frassq (proc, Vprocess_alist);
  549.   Vprocess_alist = Fdelq (pair, Vprocess_alist);
  550.  
  551.   deactivate_process (proc);
  552. }
  553.  
  554. DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0,
  555.   "Return t if OBJECT is a process.")
  556.   (object)
  557.      Lisp_Object object;
  558. {
  559.   return PROCESSP (object) ? Qt : Qnil;
  560. }
  561.  
  562. DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0,
  563.   "Return the process named NAME, or nil if there is none.")
  564.   (name)
  565.      register Lisp_Object name;
  566. {
  567.   if (PROCESSP (name))
  568.     return name;
  569.   CHECK_STRING (name, 0);
  570.   return Fcdr (Fassoc (name, Vprocess_alist));
  571. }
  572.  
  573. DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
  574.   "Return the (or, a) process associated with BUFFER.\n\
  575. BUFFER may be a buffer or the name of one.")
  576.   (buffer)
  577.      register Lisp_Object buffer;
  578. {
  579.   register Lisp_Object buf, tail, proc;
  580.  
  581.   if (NILP (buffer)) return Qnil;
  582.   buf = Fget_buffer (buffer);
  583.   if (NILP (buf)) return Qnil;
  584.  
  585.   for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
  586.     {
  587.       proc = Fcdr (Fcar (tail));
  588.       if (PROCESSP (proc) && EQ (XPROCESS (proc)->buffer, buf))
  589.     return proc;
  590.     }
  591.   return Qnil;
  592. }
  593.  
  594. /* This is how commands for the user decode process arguments.  It
  595.    accepts a process, a process name, a buffer, a buffer name, or nil.
  596.    Buffers denote the first process in the buffer, and nil denotes the
  597.    current buffer.  */
  598.  
  599. static Lisp_Object
  600. get_process (name)
  601.      register Lisp_Object name;
  602. {
  603.   register Lisp_Object proc, obj;
  604.   if (STRINGP (name))
  605.     {
  606.       obj = Fget_process (name);
  607.       if (NILP (obj))
  608.     obj = Fget_buffer (name);
  609.       if (NILP (obj))
  610.     error ("Process %s does not exist", XSTRING (name)->data);
  611.     }
  612.   else if (NILP (name))
  613.     obj = Fcurrent_buffer ();
  614.   else
  615.     obj = name;
  616.  
  617.   /* Now obj should be either a buffer object or a process object.
  618.    */
  619.   if (BUFFERP (obj))
  620.     {
  621.       proc = Fget_buffer_process (obj);
  622.       if (NILP (proc))
  623.     error ("Buffer %s has no process", XSTRING (XBUFFER (obj)->name)->data);
  624.     }
  625.   else
  626.     {
  627.       CHECK_PROCESS (obj, 0);
  628.       proc = obj;
  629.     }
  630.   return proc;
  631. }
  632.  
  633. DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0,
  634.   "Delete PROCESS: kill it and forget about it immediately.\n\
  635. PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
  636. nil, indicating the current buffer's process.")
  637.   (process)
  638.      register Lisp_Object process;
  639. {
  640.   process = get_process (process);
  641.   XPROCESS (process)->raw_status_low = Qnil;
  642.   XPROCESS (process)->raw_status_high = Qnil;
  643.   if (NETCONN_P (process))
  644.     {
  645.       XPROCESS (process)->status = Fcons (Qexit, Fcons (make_number (0), Qnil));
  646.       XSETINT (XPROCESS (process)->tick, ++process_tick);
  647.     }
  648.   else if (XINT (XPROCESS (process)->infd) >= 0)
  649.     {
  650.       Fkill_process (process, Qnil);
  651.       /* Do this now, since remove_process will make sigchld_handler do nothing.  */
  652.       XPROCESS (process)->status 
  653.     = Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil));
  654.       XSETINT (XPROCESS (process)->tick, ++process_tick);
  655.       status_notify ();
  656.     }
  657.   remove_process (process);
  658.   return Qnil;
  659. }
  660.  
  661. DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0,
  662.   "Return the status of PROCESS: a symbol, one of these:\n\
  663. run  -- for a process that is running.\n\
  664. stop -- for a process stopped but continuable.\n\
  665. exit -- for a process that has exited.\n\
  666. signal -- for a process that has got a fatal signal.\n\
  667. open -- for a network stream connection that is open.\n\
  668. closed -- for a network stream connection that is closed.\n\
  669. nil -- if arg is a process name and no such process exists.\n\
  670. PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
  671. nil, indicating the current buffer's process.")
  672.   (process)
  673.      register Lisp_Object process;
  674. {
  675.   register struct Lisp_Process *p;
  676.   register Lisp_Object status;
  677.  
  678.   if (STRINGP (process))
  679.     process = Fget_process (process);
  680.   else
  681.     process = get_process (process);
  682.  
  683.   if (NILP (process))
  684.     return process;
  685.  
  686.   p = XPROCESS (process);
  687.   if (!NILP (p->raw_status_low))
  688.     update_status (p);
  689.   status = p->status;
  690.   if (CONSP (status))
  691.     status = XCONS (status)->car;
  692.   if (NETCONN_P (process))
  693.     {
  694.       if (EQ (status, Qrun))
  695.     status = Qopen;
  696.       else if (EQ (status, Qexit))
  697.     status = Qclosed;
  698.     }
  699.   return status;
  700. }
  701.  
  702. DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status,
  703.        1, 1, 0,
  704.   "Return the exit status of PROCESS or the signal number that killed it.\n\
  705. If PROCESS has not yet exited or died, return 0.")
  706.   (process)
  707.      register Lisp_Object process;
  708. {
  709.   CHECK_PROCESS (process, 0);
  710.   if (!NILP (XPROCESS (process)->raw_status_low))
  711.     update_status (XPROCESS (process));
  712.   if (CONSP (XPROCESS (process)->status))
  713.     return XCONS (XCONS (XPROCESS (process)->status)->cdr)->car;
  714.   return make_number (0);
  715. }
  716.  
  717. DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0,
  718.   "Return the process id of PROCESS.\n\
  719. This is the pid of the Unix process which PROCESS uses or talks to.\n\
  720. For a network connection, this value is nil.")
  721.   (process)
  722.      register Lisp_Object process;
  723. {
  724.   CHECK_PROCESS (process, 0);
  725.   return XPROCESS (process)->pid;
  726. }
  727.  
  728. DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0,
  729.   "Return the name of PROCESS, as a string.\n\
  730. This is the name of the program invoked in PROCESS,\n\
  731. possibly modified to make it unique among process names.")
  732.   (process)
  733.      register Lisp_Object process;
  734. {
  735.   CHECK_PROCESS (process, 0);
  736.   return XPROCESS (process)->name;
  737. }
  738.  
  739. DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0,
  740.   "Return the command that was executed to start PROCESS.\n\
  741. This is a list of strings, the first string being the program executed\n\
  742. and the rest of the strings being the arguments given to it.\n\
  743. For a non-child channel, this is nil.")
  744.   (process)
  745.      register Lisp_Object process;
  746. {
  747.   CHECK_PROCESS (process, 0);
  748.   return XPROCESS (process)->command;
  749. }
  750.  
  751. DEFUN ("process-tty-name", Fprocess_tty_name, Sprocess_tty_name, 1, 1, 0,
  752.   "Return the name of the terminal PROCESS uses, or nil if none.\n\
  753. This is the terminal that the process itself reads and writes on,\n\
  754. not the name of the pty that Emacs uses to talk with that terminal.")
  755.   (process)
  756.      register Lisp_Object process;
  757. {
  758.   CHECK_PROCESS (process, 0);
  759.   return XPROCESS (process)->tty_name;
  760. }
  761.  
  762. DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer,
  763.   2, 2, 0,
  764.   "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
  765.   (process, buffer)
  766.      register Lisp_Object process, buffer;
  767. {
  768.   CHECK_PROCESS (process, 0);
  769.   if (!NILP (buffer))
  770.     CHECK_BUFFER (buffer, 1);
  771.   XPROCESS (process)->buffer = buffer;
  772.   return buffer;
  773. }
  774.  
  775. DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer,
  776.   1, 1, 0,
  777.   "Return the buffer PROCESS is associated with.\n\
  778. Output from PROCESS is inserted in this buffer\n\
  779. unless PROCESS has a filter.")
  780.   (process)
  781.      register Lisp_Object process;
  782. {
  783.   CHECK_PROCESS (process, 0);
  784.   return XPROCESS (process)->buffer;
  785. }
  786.  
  787. DEFUN ("process-mark", Fprocess_mark, Sprocess_mark,
  788.   1, 1, 0,
  789.   "Return the marker for the end of the last output from PROCESS.")
  790.   (process)
  791.      register Lisp_Object process;
  792. {
  793.   CHECK_PROCESS (process, 0);
  794.   return XPROCESS (process)->mark;
  795. }
  796.  
  797. DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter,
  798.   2, 2, 0,
  799.   "Give PROCESS the filter function FILTER; nil means no filter.\n\
  800. t means stop accepting output from the process.\n\
  801. When a process has a filter, each time it does output\n\
  802. the entire string of output is passed to the filter.\n\
  803. The filter gets two arguments: the process and the string of output.\n\
  804. If the process has a filter, its buffer is not used for output.")
  805.   (process, filter)
  806.      register Lisp_Object process, filter;
  807. {
  808.   CHECK_PROCESS (process, 0);
  809.   if (EQ (filter, Qt))
  810.     {
  811.       FD_CLR (XINT (XPROCESS (process)->infd), &input_wait_mask);
  812.       FD_CLR (XINT (XPROCESS (process)->infd), &non_keyboard_wait_mask);
  813.     }
  814.   else if (EQ (XPROCESS (process)->filter, Qt))
  815.     {
  816.       FD_SET (XINT (XPROCESS (process)->infd), &input_wait_mask);
  817.       FD_SET (XINT (XPROCESS (process)->infd), &non_keyboard_wait_mask);
  818.     }
  819.   XPROCESS (process)->filter = filter;
  820.   return filter;
  821. }
  822.  
  823. DEFUN ("process-filter", Fprocess_filter, Sprocess_filter,
  824.   1, 1, 0,
  825.   "Returns the filter function of PROCESS; nil if none.\n\
  826. See `set-process-filter' for more info on filter functions.")
  827.   (process)
  828.      register Lisp_Object process;
  829. {
  830.   CHECK_PROCESS (process, 0);
  831.   return XPROCESS (process)->filter;
  832. }
  833.  
  834. DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel,
  835.   2, 2, 0,
  836.   "Give PROCESS the sentinel SENTINEL; nil for none.\n\
  837. The sentinel is called as a function when the process changes state.\n\
  838. It gets two arguments: the process, and a string describing the change.")
  839.   (process, sentinel)
  840.      register Lisp_Object process, sentinel;
  841. {
  842.   CHECK_PROCESS (process, 0);
  843.   XPROCESS (process)->sentinel = sentinel;
  844.   return sentinel;
  845. }
  846.  
  847. DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
  848.   1, 1, 0,
  849.   "Return the sentinel of PROCESS; nil if none.\n\
  850. See `set-process-sentinel' for more info on sentinels.")
  851.   (process)
  852.      register Lisp_Object process;
  853. {
  854.   CHECK_PROCESS (process, 0);
  855.   return XPROCESS (process)->sentinel;
  856. }
  857.  
  858. DEFUN ("set-process-window-size", Fset_process_window_size,
  859.   Sset_process_window_size, 3, 3, 0,
  860.   "Tell PROCESS that it has logical window size HEIGHT and WIDTH.")
  861.   (process, height, width)
  862.      register Lisp_Object process, height, width;
  863. {
  864.   CHECK_PROCESS (process, 0);
  865.   CHECK_NATNUM (height, 0);
  866.   CHECK_NATNUM (width, 0);
  867.   if (set_window_size (XINT (XPROCESS (process)->infd),
  868.                XINT (height), XINT(width)) <= 0)
  869.     return Qnil;
  870.   else
  871.     return Qt;
  872. }
  873.  
  874. DEFUN ("set-process-inherit-coding-system-flag",
  875.   Fset_process_inherit_coding_system_flag,
  876.   Sset_process_inherit_coding_system_flag, 2, 2, 0,
  877.   "Determine whether buffer of PROCESS will inherit coding-system.\n\
  878. If the second argument FLAG is non-nil, then the variable\n\
  879. `buffer-file-coding-system' of the buffer associated with PROCESS\n\
  880. will be bound to the value of the coding system used to decode\n\
  881. the process output.\n\
  882. \n\
  883. This is useful when the coding system specified for the process buffer\n\
  884. leaves either the character code conversion or the end-of-line conversion\n\
  885. unspecified, or if the coding system used to decode the process output\n\
  886. is more appropriate for saving the process buffer.\n\
  887. \n\
  888. Binding the variable `inherit-process-coding-system' to non-nil before\n\
  889. starting the process is an alternative way of setting the inherit flag\n\
  890. for the process which will run.")
  891.   (process, flag)
  892.      register Lisp_Object process, flag;
  893. {
  894.   CHECK_PROCESS (process, 0);
  895.   XPROCESS (process)->inherit_coding_system_flag = flag;
  896.   return flag;
  897. }
  898.  
  899. DEFUN ("process-inherit-coding-system-flag",
  900.   Fprocess_inherit_coding_system_flag, Sprocess_inherit_coding_system_flag,
  901.   1, 1, 0,
  902.   "Return the value of inherit-coding-system flag for PROCESS.\n\
  903. If this flag is t, `buffer-file-coding-system' of the buffer\n\
  904. associated with PROCESS will inherit the coding system used to decode\n\
  905. the process output.")
  906.   (process)
  907.      register Lisp_Object process;
  908. {
  909.   CHECK_PROCESS (process, 0);
  910.   return XPROCESS (process)->inherit_coding_system_flag;
  911. }
  912.  
  913. DEFUN ("process-kill-without-query", Fprocess_kill_without_query,
  914.   Sprocess_kill_without_query, 1, 2, 0,
  915.   "Say no query needed if PROCESS is running when Emacs is exited.\n\
  916. Optional second argument if non-nil says to require a query.\n\
  917. Value is t if a query was formerly required.")
  918.   (process, value)
  919.      register Lisp_Object process, value;
  920. {
  921.   Lisp_Object tem;
  922.  
  923.   CHECK_PROCESS (process, 0);
  924.   tem = XPROCESS (process)->kill_without_query;
  925.   XPROCESS (process)->kill_without_query = Fnull (value);
  926.  
  927.   return Fnull (tem);
  928. }
  929.  
  930. DEFUN ("process-contact", Fprocess_contact, Sprocess_contact,
  931.   1, 1, 0,
  932.   "Return the contact info of PROCESS; t for a real child.\n\
  933. For a net connection, the value is a cons cell of the form (HOST SERVICE).")
  934.   (process)
  935.      register Lisp_Object process;
  936. {
  937.   CHECK_PROCESS (process, 0);
  938.   return XPROCESS (process)->childp;
  939. }
  940.  
  941. #if 0 /* Turned off because we don't currently record this info
  942.      in the process.  Perhaps add it.  */
  943. DEFUN ("process-connection", Fprocess_connection, Sprocess_connection, 1, 1, 0,
  944.  "Return the connection type of `PROCESS'.\n\
  945. The value is `nil' for a pipe,\n\
  946. `t' or `pty' for a pty, or `stream' for a socket connection.")
  947.   (process)
  948.      Lisp_Object process;
  949. {
  950.   return XPROCESS (process)->type;
  951. }
  952. #endif
  953.  
  954. Lisp_Object
  955. list_processes_1 ()
  956. {
  957.   register Lisp_Object tail, tem;
  958.   Lisp_Object proc, minspace, tem1;
  959.   register struct buffer *old = current_buffer;
  960.   register struct Lisp_Process *p;
  961.   register int state;
  962.   char tembuf[80];
  963.  
  964.   XSETFASTINT (minspace, 1);
  965.  
  966.   set_buffer_internal (XBUFFER (Vstandard_output));
  967.   Fbuffer_disable_undo (Vstandard_output);
  968.  
  969.   current_buffer->truncate_lines = Qt;
  970.  
  971.   write_string ("\
  972. Proc         Status   Buffer         Tty         Command\n\
  973. ----         ------   ------         ---         -------\n", -1);
  974.  
  975.   for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
  976.     {
  977.       Lisp_Object symbol;
  978.  
  979.       proc = Fcdr (Fcar (tail));
  980.       p = XPROCESS (proc);
  981.       if (NILP (p->childp))
  982.     continue;
  983.  
  984.       Finsert (1, &p->name);
  985.       Findent_to (make_number (13), minspace);
  986.  
  987.       if (!NILP (p->raw_status_low))
  988.     update_status (p);
  989.       symbol = p->status;
  990.       if (CONSP (p->status))
  991.     symbol = XCONS (p->status)->car;
  992.  
  993.       
  994.       if (EQ (symbol, Qsignal))
  995.     {
  996.       Lisp_Object tem;
  997.       tem = Fcar (Fcdr (p->status));
  998. #ifdef VMS
  999.       if (XINT (tem) < NSIG)
  1000.         write_string (sys_errlist [XINT (tem)], -1);
  1001.       else
  1002. #endif
  1003.         Fprinc (symbol, Qnil);
  1004.     }
  1005.       else if (NETCONN_P (proc))
  1006.     {
  1007.       if (EQ (symbol, Qrun))
  1008.         write_string ("open", -1);
  1009.       else if (EQ (symbol, Qexit))
  1010.         write_string ("closed", -1);
  1011.       else
  1012.         Fprinc (symbol, Qnil);
  1013.     }
  1014.       else
  1015.     Fprinc (symbol, Qnil);
  1016.  
  1017.       if (EQ (symbol, Qexit))
  1018.     {
  1019.       Lisp_Object tem;
  1020.       tem = Fcar (Fcdr (p->status));
  1021.       if (XFASTINT (tem))
  1022.         {
  1023.           sprintf (tembuf, " %d", (int) XFASTINT (tem));
  1024.           write_string (tembuf, -1);
  1025.         }
  1026.     }
  1027.  
  1028.       if (EQ (symbol, Qsignal) || EQ (symbol, Qexit))
  1029.     remove_process (proc);
  1030.  
  1031.       Findent_to (make_number (22), minspace);
  1032.       if (NILP (p->buffer))
  1033.     insert_string ("(none)");
  1034.       else if (NILP (XBUFFER (p->buffer)->name))
  1035.     insert_string ("(Killed)");
  1036.       else
  1037.     Finsert (1, &XBUFFER (p->buffer)->name);
  1038.  
  1039.       Findent_to (make_number (37), minspace);
  1040.  
  1041.       if (STRINGP (p->tty_name))
  1042.     Finsert (1, &p->tty_name);
  1043.       else
  1044.     insert_string ("(none)");
  1045.  
  1046.       Findent_to (make_number (49), minspace);
  1047.  
  1048.       if (NETCONN_P (proc))
  1049.         {
  1050.       sprintf (tembuf, "(network stream connection to %s)\n",
  1051.            XSTRING (XCONS (p->childp)->car)->data);
  1052.       insert_string (tembuf);
  1053.         }
  1054.       else 
  1055.     {
  1056.       tem = p->command;
  1057.       while (1)
  1058.         {
  1059.           tem1 = Fcar (tem);
  1060.           Finsert (1, &tem1);
  1061.           tem = Fcdr (tem);
  1062.           if (NILP (tem))
  1063.         break;
  1064.           insert_string (" ");
  1065.         }
  1066.       insert_string ("\n");
  1067.        }
  1068.     }
  1069.   return Qnil;
  1070. }
  1071.  
  1072. DEFUN ("list-processes", Flist_processes, Slist_processes, 0, 0, "",
  1073.   "Display a list of all processes.\n\
  1074. \(Any processes listed as Exited or Signaled are actually eliminated\n\
  1075. after the listing is made.)")
  1076.   ()
  1077. {
  1078.   internal_with_output_to_temp_buffer ("*Process List*",
  1079.                        list_processes_1, Qnil);
  1080.   return Qnil;
  1081. }
  1082.  
  1083. DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
  1084.   "Return a list of all processes.")
  1085.   ()
  1086. {
  1087.   return Fmapcar (Qcdr, Vprocess_alist);
  1088. }
  1089.  
  1090. /* Starting asynchronous inferior processes.  */
  1091.  
  1092. static Lisp_Object start_process_unwind ();
  1093.  
  1094. DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0,
  1095.   "Start a program in a subprocess.  Return the process object for it.\n\
  1096. Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
  1097. NAME is name for process.  It is modified if necessary to make it unique.\n\
  1098. BUFFER is the buffer or (buffer-name) to associate with the process.\n\
  1099.  Process output goes at end of that buffer, unless you specify\n\
  1100.  an output stream or filter function to handle the output.\n\
  1101.  BUFFER may be also nil, meaning that this process is not associated\n\
  1102.  with any buffer.\n\
  1103. Third arg is program file name.  It is searched for in PATH.\n\
  1104. Remaining arguments are strings to give program as arguments.")
  1105.   (nargs, args)
  1106.      int nargs;
  1107.      register Lisp_Object *args;
  1108. {
  1109.   Lisp_Object buffer, name, program, proc, current_dir, tem;
  1110. #ifdef VMS
  1111.   register unsigned char *new_argv;
  1112.   int len;
  1113. #else
  1114.   register unsigned char **new_argv;
  1115. #endif
  1116.   register int i;
  1117.   int count = specpdl_ptr - specpdl;
  1118.  
  1119. #ifdef EMX
  1120.   {
  1121.     Lisp_Object handler;
  1122.  
  1123.     emx_setup_start_process (); /* Calls specbind() */
  1124.     handler = find_program_name_handler (args[2]);
  1125.     if (!NILP (handler))
  1126.       return unbind_to (count,
  1127.                         call3 (handler,
  1128.                                Fcons (Qstart_process, Flist (2, args + 0)),
  1129.                                args[2],
  1130.                                Flist (nargs - 3, args + 3)));
  1131.   }
  1132.   if (_osmode == DOS_MODE)
  1133.     error ("start-process not supported under MS-DOS");
  1134.   check_process_priority ();
  1135. #endif /* EMX */
  1136.  
  1137.   buffer = args[1];
  1138.   if (!NILP (buffer))
  1139.     buffer = Fget_buffer_create (buffer);
  1140.  
  1141.   /* Make sure that the child will be able to chdir to the current
  1142.      buffer's current directory, or its unhandled equivalent.  We
  1143.      can't just have the child check for an error when it does the
  1144.      chdir, since it's in a vfork.
  1145.  
  1146.      We have to GCPRO around this because Fexpand_file_name and
  1147.      Funhandled_file_name_directory might call a file name handling
  1148.      function.  The argument list is protected by the caller, so all
  1149.      we really have to worry about is buffer.  */
  1150.   {
  1151.     struct gcpro gcpro1, gcpro2;
  1152.  
  1153.     current_dir = current_buffer->directory;
  1154.  
  1155.     GCPRO2 (buffer, current_dir);
  1156.  
  1157. #ifdef EMX
  1158.     current_dir = Fexpand_file_name (current_dir, Qnil);
  1159. #endif /* EMX */
  1160.  
  1161.     current_dir 
  1162.       = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
  1163.                 Qnil);
  1164.     if (NILP (Ffile_accessible_directory_p (current_dir)))
  1165.       report_file_error ("Setting current directory",
  1166.              Fcons (current_buffer->directory, Qnil));
  1167.  
  1168.     UNGCPRO;
  1169.   }
  1170.  
  1171.   name = args[0];
  1172.   CHECK_STRING (name, 0);
  1173.  
  1174.   program = args[2];
  1175.  
  1176.   CHECK_STRING (program, 2);
  1177.  
  1178. #ifdef VMS
  1179.   /* Make a one member argv with all args concatenated
  1180.      together separated by a blank.  */
  1181.   len = STRING_BYTES (XSTRING (program)) + 2;
  1182.   for (i = 3; i < nargs; i++)
  1183.     {
  1184.       tem = args[i];
  1185.       CHECK_STRING (tem, i);
  1186.       len += STRING_BYTES (XSTRING (tem)) + 1;    /* count the blank */
  1187.     }
  1188.   new_argv = (unsigned char *) alloca (len);
  1189.   strcpy (new_argv, XSTRING (program)->data);
  1190.   for (i = 3; i < nargs; i++)
  1191.     {
  1192.       tem = args[i];
  1193.       CHECK_STRING (tem, i);
  1194.       strcat (new_argv, " ");
  1195.       strcat (new_argv, XSTRING (tem)->data);
  1196.     }
  1197.   /* Need to add code here to check for program existence on VMS */
  1198.   
  1199. #else /* not VMS */
  1200.   new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
  1201.  
  1202.   /* If program file name is not absolute, search our path for it */
  1203. #ifdef EMX
  1204.   if (!_fnisabs (XSTRING (program)->data))
  1205. #else /* not EMX */
  1206.   if (!IS_DIRECTORY_SEP (XSTRING (program)->data[0])
  1207.       && !(XSTRING (program)->size > 1
  1208.        && IS_DEVICE_SEP (XSTRING (program)->data[1])))
  1209. #endif /* not EMX */
  1210.     {
  1211.       struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  1212.  
  1213.       tem = Qnil;
  1214.       GCPRO4 (name, program, buffer, current_dir);
  1215.       openp (Vexec_path, program, EXEC_SUFFIXES, &tem, 1);
  1216.       UNGCPRO;
  1217.       if (NILP (tem))
  1218.     report_file_error ("Searching for program", Fcons (program, Qnil));
  1219.       tem = Fexpand_file_name (tem, Qnil);
  1220.       new_argv[0] = XSTRING (tem)->data;
  1221.     }
  1222.   else
  1223.     {
  1224.       if (!NILP (Ffile_directory_p (program)))
  1225.     error ("Specified program for new process is a directory");
  1226.  
  1227.       new_argv[0] = XSTRING (program)->data;
  1228.     }
  1229.  
  1230.   for (i = 3; i < nargs; i++)
  1231.     {
  1232.       tem = args[i];
  1233.       CHECK_STRING (tem, i);
  1234.       new_argv[i - 2] = XSTRING (tem)->data;
  1235.     }
  1236.   new_argv[i - 2] = 0;
  1237. #endif /* not VMS */
  1238.  
  1239.   proc = make_process (name);
  1240.   /* If an error occurs and we can't start the process, we want to
  1241.      remove it from the process list.  This means that each error
  1242.      check in create_process doesn't need to call remove_process
  1243.      itself; it's all taken care of here.  */
  1244.   record_unwind_protect (start_process_unwind, proc);
  1245.  
  1246.   XPROCESS (proc)->childp = Qt;
  1247.   XPROCESS (proc)->command_channel_p = Qnil;
  1248.   XPROCESS (proc)->buffer = buffer;
  1249.   XPROCESS (proc)->sentinel = Qnil;
  1250.   XPROCESS (proc)->filter = Qnil;
  1251.   XPROCESS (proc)->command = Flist (nargs - 2, args + 2);
  1252.  
  1253.   /* Make the process marker point into the process buffer (if any).  */
  1254.   if (!NILP (buffer))
  1255.     set_marker_both (XPROCESS (proc)->mark, buffer,
  1256.              BUF_ZV (XBUFFER (buffer)),
  1257.              BUF_ZV_BYTE (XBUFFER (buffer)));
  1258.  
  1259.   {
  1260.     /* Decide coding systems for communicating with the process.  Here
  1261.        we don't setup the structure coding_system nor pay attention to
  1262.        unibyte mode.  They are done in create_process.  */
  1263.  
  1264.     /* Qt denotes we have not yet called Ffind_operation_coding_system.  */
  1265.     Lisp_Object coding_systems = Qt;
  1266.     Lisp_Object val, *args2;
  1267.     struct gcpro gcpro1;
  1268.  
  1269.     val = Vcoding_system_for_read;
  1270.     if (NILP (val))
  1271.       {
  1272.     args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
  1273.     args2[0] = Qstart_process;
  1274.     for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
  1275.     GCPRO1 (proc);
  1276.     coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
  1277.     UNGCPRO;
  1278.     if (CONSP (coding_systems))
  1279.       val = XCONS (coding_systems)->car;
  1280.     else if (CONSP (Vdefault_process_coding_system))
  1281.       val = XCONS (Vdefault_process_coding_system)->car;
  1282.       }
  1283.     XPROCESS (proc)->decode_coding_system = val;
  1284.  
  1285.     val = Vcoding_system_for_write;
  1286.     if (NILP (val))
  1287.       {
  1288.     if (EQ (coding_systems, Qt))
  1289.       {
  1290.         args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof args2);
  1291.         args2[0] = Qstart_process;
  1292.         for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
  1293.         GCPRO1 (proc);
  1294.         coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
  1295.         UNGCPRO;
  1296.       }
  1297.     if (CONSP (coding_systems))
  1298.       val = XCONS (coding_systems)->cdr;
  1299.     else if (CONSP (Vdefault_process_coding_system))
  1300.       val = XCONS (Vdefault_process_coding_system)->cdr;
  1301.       }
  1302.     XPROCESS (proc)->encode_coding_system = val;
  1303.   }
  1304.  
  1305.   XPROCESS (proc)->decoding_buf = make_uninit_string (0);
  1306.   XPROCESS (proc)->decoding_carryover = make_number (0);
  1307.   XPROCESS (proc)->encoding_buf = make_uninit_string (0);
  1308.   XPROCESS (proc)->encoding_carryover = make_number (0);
  1309.  
  1310.   XPROCESS (proc)->inherit_coding_system_flag
  1311.     = (NILP (buffer) || !inherit_process_coding_system
  1312.        ? Qnil : Qt);
  1313.  
  1314.   create_process (proc, (char **) new_argv, current_dir);
  1315.  
  1316.   return unbind_to (count, proc);
  1317. }
  1318.  
  1319. /* This function is the unwind_protect form for Fstart_process.  If
  1320.    PROC doesn't have its pid set, then we know someone has signaled
  1321.    an error and the process wasn't started successfully, so we should
  1322.    remove it from the process list.  */
  1323. static Lisp_Object
  1324. start_process_unwind (proc)
  1325.      Lisp_Object proc;
  1326. {
  1327.   if (!PROCESSP (proc))
  1328.     abort ();
  1329.  
  1330.   /* Was PROC started successfully?  */
  1331.   if (XINT (XPROCESS (proc)->pid) <= 0)
  1332.     remove_process (proc);
  1333.  
  1334.   return Qnil;
  1335. }
  1336.  
  1337.  
  1338. SIGTYPE
  1339. create_process_1 (signo)
  1340.      int signo;
  1341. {
  1342. #if defined (USG) && !defined (POSIX_SIGNALS)
  1343.   /* USG systems forget handlers when they are used;
  1344.      must reestablish each time */
  1345.   signal (signo, create_process_1);
  1346. #endif /* USG */
  1347. }
  1348.  
  1349. #if 0  /* This doesn't work; see the note before sigchld_handler.  */
  1350. #ifdef USG
  1351. #ifdef SIGCHLD
  1352. /* Mimic blocking of signals on system V, which doesn't really have it.  */
  1353.  
  1354. /* Nonzero means we got a SIGCHLD when it was supposed to be blocked.  */
  1355. int sigchld_deferred;
  1356.  
  1357. SIGTYPE
  1358. create_process_sigchld ()
  1359. {
  1360.   signal (SIGCHLD, create_process_sigchld);
  1361.  
  1362.   sigchld_deferred = 1;
  1363. }
  1364. #endif
  1365. #endif
  1366. #endif
  1367.  
  1368. #ifndef VMS /* VMS version of this function is in vmsproc.c.  */
  1369. void
  1370. create_process (process, new_argv, current_dir)
  1371.      Lisp_Object process;
  1372.      char **new_argv;
  1373.      Lisp_Object current_dir;
  1374. {
  1375.   int pid, inchannel, outchannel;
  1376.   int sv[2];
  1377. #ifdef POSIX_SIGNALS
  1378.   sigset_t procmask;
  1379.   sigset_t blocked;
  1380.   struct sigaction sigint_action;
  1381.   struct sigaction sigquit_action;
  1382. #ifdef AIX
  1383.   struct sigaction sighup_action;
  1384. #endif
  1385. #else /* !POSIX_SIGNALS */
  1386. #ifdef SIGCHLD
  1387.   SIGTYPE (*sigchld)();
  1388. #endif
  1389. #endif /* !POSIX_SIGNALS */
  1390.   /* Use volatile to protect variables from being clobbered by longjmp.  */
  1391.   volatile int forkin, forkout;
  1392.   volatile int pty_flag = 0;
  1393.   extern char **environ;
  1394.   Lisp_Object buffer = XPROCESS (process)->buffer;
  1395.  
  1396.   inchannel = outchannel = -1;
  1397.  
  1398. #ifdef HAVE_PTYS
  1399.   if (!NILP (Vprocess_connection_type))
  1400.     outchannel = inchannel = allocate_pty ();
  1401.  
  1402.   if (inchannel >= 0)
  1403.     {
  1404. #ifdef EMX
  1405.       /* Make another descriptor for the pty, to be able to use
  1406.          different O_BINARY settings for reading and writing.  */
  1407.       outchannel = dup (inchannel);
  1408. #endif 
  1409. #ifndef USG 
  1410.       /* On USG systems it does not work to open the pty's tty here
  1411.            and then close and reopen it in the child.  */
  1412. #ifdef O_NOCTTY
  1413.       /* Don't let this terminal become our controlling terminal
  1414.      (in case we don't have one).  */
  1415.       forkout = forkin = open (pty_name, O_RDWR | O_NOCTTY, 0);
  1416. #else
  1417.       forkout = forkin = open (pty_name, O_RDWR, 0);
  1418. #endif
  1419.       if (forkin < 0)
  1420.     report_file_error ("Opening pty", Qnil);
  1421. #else
  1422.       forkin = forkout = -1;
  1423. #endif /* not USG */
  1424.       pty_flag = 1;
  1425.     }
  1426.   else
  1427. #endif /* HAVE_PTYS */
  1428. #ifdef SKTPAIR
  1429.     {
  1430.       if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) < 0)
  1431.     report_file_error ("Opening socketpair", Qnil);
  1432.       outchannel = inchannel = sv[0];
  1433.       forkout = forkin = sv[1];
  1434.     }
  1435. #else /* not SKTPAIR */
  1436.     {
  1437.       pipe (sv);
  1438.       inchannel = sv[0];
  1439.       forkout = sv[1];
  1440.       pipe (sv);
  1441.       outchannel = sv[1];
  1442.       forkin = sv[0];
  1443.     }
  1444. #endif /* not SKTPAIR */
  1445.  
  1446. #ifdef EMX
  1447.   emx_proc_output_pipe (inchannel);
  1448.   emx_proc_input_pipe (outchannel);
  1449. #endif /* EMX */
  1450.  
  1451. #if 0
  1452.   /* Replaced by close_process_descs */
  1453.   set_exclusive_use (inchannel);
  1454.   set_exclusive_use (outchannel);
  1455. #endif
  1456.  
  1457. /* Stride people say it's a mystery why this is needed
  1458.    as well as the O_NDELAY, but that it fails without this.  */
  1459. #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
  1460.   {
  1461.     int one = 1;
  1462.     ioctl (inchannel, FIONBIO, &one);
  1463.   }
  1464. #endif
  1465.  
  1466. #ifdef O_NONBLOCK
  1467.   fcntl (inchannel, F_SETFL, O_NONBLOCK);
  1468.   fcntl (outchannel, F_SETFL, O_NONBLOCK);
  1469. #else
  1470. #ifdef O_NDELAY
  1471.   fcntl (inchannel, F_SETFL, O_NDELAY);
  1472.   fcntl (outchannel, F_SETFL, O_NDELAY);
  1473. #endif
  1474. #endif
  1475.  
  1476.   /* Record this as an active process, with its channels.
  1477.      As a result, child_setup will close Emacs's side of the pipes.  */
  1478.   chan_process[inchannel] = process;
  1479.   XSETINT (XPROCESS (process)->infd, inchannel);
  1480.   XSETINT (XPROCESS (process)->outfd, outchannel);
  1481.   /* Record the tty descriptor used in the subprocess.  */
  1482.   if (forkin < 0)
  1483.     XPROCESS (process)->subtty = Qnil;
  1484.   else
  1485.     XSETFASTINT (XPROCESS (process)->subtty, forkin);
  1486.   XPROCESS (process)->pty_flag = (pty_flag ? Qt : Qnil);
  1487.   XPROCESS (process)->status = Qrun;
  1488.   if (!proc_decode_coding_system[inchannel])
  1489.     proc_decode_coding_system[inchannel]
  1490.       = (struct coding_system *) xmalloc (sizeof (struct coding_system));
  1491.   setup_coding_system (XPROCESS (process)->decode_coding_system,
  1492.                proc_decode_coding_system[inchannel]);
  1493.   if (!proc_encode_coding_system[outchannel])
  1494.     proc_encode_coding_system[outchannel]
  1495.       = (struct coding_system *) xmalloc (sizeof (struct coding_system));
  1496.   setup_coding_system (XPROCESS (process)->encode_coding_system,
  1497.                proc_encode_coding_system[outchannel]);
  1498.  
  1499.   if (!NILP (buffer) && NILP (XBUFFER (buffer)->enable_multibyte_characters)
  1500.       || (NILP (buffer) && NILP (buffer_defaults.enable_multibyte_characters)))
  1501.     {
  1502.       /* In unibyte mode, character code conversion should not take
  1503.      place but EOL conversion should.  So, setup raw-text or one
  1504.      of the subsidiary according to the information just setup.  */
  1505.       if (NILP (Vcoding_system_for_read)
  1506.       && !NILP (XPROCESS (process)->decode_coding_system))
  1507.     setup_raw_text_coding_system (proc_decode_coding_system[inchannel]);
  1508.       if (NILP (Vcoding_system_for_write)
  1509.       && !NILP (XPROCESS (process)->encode_coding_system))
  1510.     setup_raw_text_coding_system (proc_encode_coding_system[outchannel]);
  1511.     }
  1512.  
  1513.   if (CODING_REQUIRE_ENCODING (proc_encode_coding_system[outchannel]))
  1514.     {
  1515.       /* Here we encode arguments by the coding system used for
  1516.      sending data to the process.  We don't support using
  1517.      different coding systems for encoding arguments and for
  1518.      encoding data sent to the process.  */
  1519.       struct gcpro gcpro1;
  1520.       int i = 1;
  1521.       struct coding_system *coding = proc_encode_coding_system[outchannel];
  1522.  
  1523.       coding->mode |= CODING_MODE_LAST_BLOCK;
  1524.       GCPRO1 (process);
  1525.       while (new_argv[i] != 0)
  1526.     {
  1527.       int len = strlen (new_argv[i]);
  1528.       int size = encoding_buffer_size (coding, len);
  1529.       unsigned char *buf = (unsigned char *) alloca (size);
  1530.  
  1531.       encode_coding (coding, (unsigned char *)new_argv[i], buf, len, size);
  1532.       buf[coding->produced] = 0;
  1533.       /* We don't have to free new_argv[i] because it points to a
  1534.              Lisp string given as an argument to `start-process'.  */
  1535.       new_argv[i++] = (char *) buf;
  1536.     }
  1537.       UNGCPRO;
  1538.       coding->mode &= ~CODING_MODE_LAST_BLOCK;
  1539.     }
  1540.  
  1541.   /* Delay interrupts until we have a chance to store
  1542.      the new fork's pid in its process structure */
  1543. #ifdef POSIX_SIGNALS
  1544.   sigemptyset (&blocked);
  1545. #ifdef SIGCHLD
  1546.   sigaddset (&blocked, SIGCHLD);
  1547. #endif
  1548. #ifdef HAVE_VFORK
  1549.   /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
  1550.      this sets the parent's signal handlers as well as the child's.
  1551.      So delay all interrupts whose handlers the child might munge,
  1552.      and record the current handlers so they can be restored later.  */
  1553.   sigaddset (&blocked, SIGINT );  sigaction (SIGINT , 0, &sigint_action );
  1554.   sigaddset (&blocked, SIGQUIT);  sigaction (SIGQUIT, 0, &sigquit_action);
  1555. #ifdef AIX
  1556.   sigaddset (&blocked, SIGHUP );  sigaction (SIGHUP , 0, &sighup_action );
  1557. #endif
  1558. #endif /* HAVE_VFORK */
  1559.   sigprocmask (SIG_BLOCK, &blocked, &procmask);
  1560. #else /* !POSIX_SIGNALS */
  1561. #ifdef SIGCHLD
  1562. #ifdef BSD4_1
  1563.   sighold (SIGCHLD);
  1564. #else /* not BSD4_1 */
  1565. #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
  1566.   sigsetmask (sigmask (SIGCHLD));
  1567. #else /* ordinary USG */
  1568. #if 0
  1569.   sigchld_deferred = 0;
  1570.   sigchld = signal (SIGCHLD, create_process_sigchld);
  1571. #endif
  1572. #endif /* ordinary USG */
  1573. #endif /* not BSD4_1 */
  1574. #endif /* SIGCHLD */
  1575. #endif /* !POSIX_SIGNALS */
  1576.  
  1577.   FD_SET (inchannel, &input_wait_mask);
  1578.   FD_SET (inchannel, &non_keyboard_wait_mask);
  1579.   if (inchannel > max_process_desc)
  1580.     max_process_desc = inchannel;
  1581.  
  1582.   /* Until we store the proper pid, enable sigchld_handler
  1583.      to recognize an unknown pid as standing for this process.
  1584.      It is very important not to let this `marker' value stay
  1585.      in the table after this function has returned; if it does
  1586.      it might cause call-process to hang and subsequent asynchronous
  1587.      processes to get their return values scrambled.  */
  1588.   XSETINT (XPROCESS (process)->pid, -1);
  1589.  
  1590.   BLOCK_INPUT;
  1591.   
  1592.   {
  1593.     /* child_setup must clobber environ on systems with true vfork.
  1594.        Protect it from permanent change.  */
  1595.     char **save_environ = environ;
  1596.  
  1597.     current_dir = ENCODE_FILE (current_dir);
  1598.  
  1599. #ifndef WINDOWSNT
  1600.     pid = vfork ();
  1601.     if (pid == 0)
  1602. #endif /* not WINDOWSNT */
  1603.       {
  1604.     int xforkin = forkin;
  1605.     int xforkout = forkout;
  1606.  
  1607. #if 0 /* This was probably a mistake--it duplicates code later on,
  1608.      but fails to handle all the cases.  */
  1609.     /* Make sure SIGCHLD is not blocked in the child.  */
  1610.     sigsetmask (SIGEMPTYMASK);
  1611. #endif
  1612.  
  1613.     /* Make the pty be the controlling terminal of the process.  */
  1614. #ifdef HAVE_PTYS
  1615.     /* First, disconnect its current controlling terminal.  */
  1616. #ifdef HAVE_SETSID
  1617.     /* We tried doing setsid only if pty_flag, but it caused
  1618.        process_set_signal to fail on SGI when using a pipe.  */
  1619.     setsid ();
  1620.     /* Make the pty's terminal the controlling terminal.  */
  1621.     if (pty_flag)
  1622.       {
  1623. #ifdef TIOCSCTTY
  1624.         /* We ignore the return value
  1625.            because faith@cs.unc.edu says that is necessary on Linux.  */
  1626.         ioctl (xforkin, TIOCSCTTY, 0);
  1627. #endif
  1628.       }
  1629. #else /* not HAVE_SETSID */
  1630. #ifdef USG
  1631.     /* It's very important to call setpgrp here and no time
  1632.        afterwards.  Otherwise, we lose our controlling tty which
  1633.        is set when we open the pty. */
  1634.     setpgrp ();
  1635. #endif /* USG */
  1636. #endif /* not HAVE_SETSID */
  1637. #if defined (HAVE_TERMIOS) && defined (LDISC1)
  1638.     if (pty_flag && xforkin >= 0)
  1639.       {
  1640.         struct termios t;
  1641.         tcgetattr (xforkin, &t);
  1642.         t.c_lflag = LDISC1;
  1643.         if (tcsetattr (xforkin, TCSANOW, &t) < 0)
  1644.           write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
  1645.       }
  1646. #else
  1647. #if defined (NTTYDISC) && defined (TIOCSETD)
  1648.     if (pty_flag && xforkin >= 0)
  1649.       {
  1650.         /* Use new line discipline.  */
  1651.         int ldisc = NTTYDISC;
  1652.         ioctl (xforkin, TIOCSETD, &ldisc);
  1653.       }
  1654. #endif
  1655. #endif
  1656. #ifdef TIOCNOTTY 
  1657.     /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
  1658.        can do TIOCSPGRP only to the process's controlling tty.  */
  1659.     if (pty_flag)
  1660.       {
  1661.         /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here? 
  1662.            I can't test it since I don't have 4.3.  */
  1663.         int j = open ("/dev/tty", O_RDWR, 0);
  1664.         ioctl (j, TIOCNOTTY, 0);
  1665.         close (j);
  1666. #ifndef USG
  1667.         /* In order to get a controlling terminal on some versions
  1668.            of BSD, it is necessary to put the process in pgrp 0
  1669.            before it opens the terminal.  */
  1670. #ifdef HAVE_SETPGID
  1671.         setpgid (0, 0);
  1672. #else
  1673.         setpgrp (0, 0);
  1674. #endif
  1675. #endif
  1676.       }
  1677. #endif /* TIOCNOTTY */
  1678.  
  1679. #if !defined (RTU) && !defined (UNIPLUS) && !defined (DONT_REOPEN_PTY)
  1680. /*** There is a suggestion that this ought to be a
  1681.      conditional on TIOCSPGRP,
  1682.      or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
  1683.      Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
  1684.      that system does seem to need this code, even though
  1685.      both HAVE_SETSID and TIOCSCTTY are defined.  */
  1686.     /* Now close the pty (if we had it open) and reopen it.
  1687.        This makes the pty the controlling terminal of the subprocess.  */
  1688.     if (pty_flag)
  1689.       {
  1690. #ifdef SET_CHILD_PTY_PGRP
  1691.         int pgrp = getpid ();
  1692. #endif
  1693.  
  1694.         /* I wonder if close (open (pty_name, ...)) would work?  */
  1695.         if (xforkin >= 0)
  1696.           close (xforkin);
  1697.         xforkout = xforkin = open (pty_name, O_RDWR, 0);
  1698.  
  1699.         if (xforkin < 0)
  1700.           {
  1701.         write (1, "Couldn't open the pty terminal ", 31);
  1702.         write (1, pty_name, strlen (pty_name));
  1703.         write (1, "\n", 1);
  1704.         _exit (1);
  1705.           }
  1706.  
  1707. #ifdef SET_CHILD_PTY_PGRP
  1708.         ioctl (xforkin, TIOCSPGRP, &pgrp);
  1709.         ioctl (xforkout, TIOCSPGRP, &pgrp);
  1710. #endif
  1711.       }
  1712. #endif /* not UNIPLUS and not RTU and not DONT_REOPEN_PTY */
  1713.  
  1714. #ifdef SETUP_SLAVE_PTY
  1715.     if (pty_flag)
  1716.       {
  1717.         SETUP_SLAVE_PTY;
  1718.       }
  1719. #endif /* SETUP_SLAVE_PTY */
  1720. #ifdef AIX
  1721.     /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
  1722.        Now reenable it in the child, so it will die when we want it to.  */
  1723.     if (pty_flag)
  1724.       signal (SIGHUP, SIG_DFL);
  1725. #endif
  1726. #endif /* HAVE_PTYS */
  1727.  
  1728. #ifdef EMX
  1729.     pid = emx_child_setup (xforkin, xforkout, xforkout,
  1730.                                new_argv, 1, current_dir);
  1731. #else /*  not EMX */
  1732.     signal (SIGINT, SIG_DFL);
  1733.     signal (SIGQUIT, SIG_DFL);
  1734.  
  1735.     /* Stop blocking signals in the child.  */
  1736. #ifdef POSIX_SIGNALS
  1737.     sigprocmask (SIG_SETMASK, &procmask, 0);
  1738. #else /* !POSIX_SIGNALS */
  1739. #ifdef SIGCHLD
  1740. #ifdef BSD4_1
  1741.     sigrelse (SIGCHLD);
  1742. #else /* not BSD4_1 */
  1743. #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
  1744.     sigsetmask (SIGEMPTYMASK);
  1745. #else /* ordinary USG */
  1746. #if 0
  1747.     signal (SIGCHLD, sigchld);
  1748. #endif
  1749. #endif /* ordinary USG */
  1750. #endif /* not BSD4_1 */
  1751. #endif /* SIGCHLD */
  1752. #endif /* !POSIX_SIGNALS */
  1753.  
  1754.     if (pty_flag)
  1755.       child_setup_tty (xforkout);
  1756. #ifdef WINDOWSNT
  1757.     pid = child_setup (xforkin, xforkout, xforkout,
  1758.                new_argv, 1, current_dir);
  1759. #else  /* not WINDOWSNT */    
  1760.     child_setup (xforkin, xforkout, xforkout,
  1761.              new_argv, 1, current_dir);
  1762. #endif /* not WINDOWSNT */
  1763. #endif /* not EMX */
  1764.       }
  1765.     environ = save_environ;
  1766.   }
  1767.  
  1768.   UNBLOCK_INPUT;
  1769.  
  1770.   /* This runs in the Emacs process.  */
  1771.   if (pid < 0)
  1772.     {
  1773.       if (forkin >= 0)
  1774.     close (forkin);
  1775.       if (forkin != forkout && forkout >= 0)
  1776.     close (forkout);
  1777.     }
  1778.   else
  1779.     {
  1780.       /* vfork succeeded.  */
  1781.       XSETFASTINT (XPROCESS (process)->pid, pid);
  1782.  
  1783. #ifdef WINDOWSNT
  1784.       register_child (pid, inchannel);
  1785. #endif /* WINDOWSNT */
  1786.  
  1787.       /* If the subfork execv fails, and it exits,
  1788.      this close hangs.  I don't know why.
  1789.      So have an interrupt jar it loose.  */
  1790.       stop_polling ();
  1791.       signal (SIGALRM, create_process_1);
  1792.       alarm (1);
  1793.       XPROCESS (process)->subtty = Qnil;
  1794.       if (forkin >= 0)
  1795.     close (forkin);
  1796.       alarm (0);
  1797.       start_polling ();
  1798.       if (forkin != forkout && forkout >= 0)
  1799.     close (forkout);
  1800.  
  1801. #ifdef HAVE_PTYS
  1802.       if (pty_flag)
  1803.     XPROCESS (process)->tty_name = build_string (pty_name);
  1804.       else
  1805. #endif
  1806.     XPROCESS (process)->tty_name = Qnil;
  1807.     }
  1808.  
  1809.   /* Restore the signal state whether vfork succeeded or not.
  1810.      (We will signal an error, below, if it failed.)  */
  1811. #ifdef POSIX_SIGNALS
  1812. #ifdef HAVE_VFORK
  1813.   /* Restore the parent's signal handlers.  */
  1814.   sigaction (SIGINT, &sigint_action, 0);
  1815.   sigaction (SIGQUIT, &sigquit_action, 0);
  1816. #ifdef AIX
  1817.   sigaction (SIGHUP, &sighup_action, 0);
  1818. #endif
  1819. #endif /* HAVE_VFORK */
  1820.   /* Stop blocking signals in the parent.  */
  1821.   sigprocmask (SIG_SETMASK, &procmask, 0);
  1822. #else /* !POSIX_SIGNALS */
  1823. #ifdef SIGCHLD
  1824. #ifdef BSD4_1
  1825.   sigrelse (SIGCHLD);
  1826. #else /* not BSD4_1 */
  1827. #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
  1828.   sigsetmask (SIGEMPTYMASK);
  1829. #else /* ordinary USG */
  1830. #if 0
  1831.   signal (SIGCHLD, sigchld);
  1832.   /* Now really handle any of these signals
  1833.      that came in during this function.  */
  1834.   if (sigchld_deferred)
  1835.     kill (getpid (), SIGCHLD);
  1836. #endif
  1837. #endif /* ordinary USG */
  1838. #endif /* not BSD4_1 */
  1839. #endif /* SIGCHLD */
  1840. #endif /* !POSIX_SIGNALS */
  1841.  
  1842.   /* Now generate the error if vfork failed.  */
  1843.   if (pid < 0)
  1844. #ifndef EMX
  1845.     report_file_error ("Doing vfork", Qnil);
  1846. #else /* EMX */
  1847.     report_file_error ("Starting process", Qnil);
  1848. #endif /* EMX */
  1849. }
  1850. #endif /* not VMS */
  1851.  
  1852. #ifdef HAVE_SOCKETS
  1853.  
  1854. /* open a TCP network connection to a given HOST/SERVICE.  Treated
  1855.    exactly like a normal process when reading and writing.  Only
  1856.    differences are in status display and process deletion.  A network
  1857.    connection has no PID; you cannot signal it.  All you can do is
  1858.    deactivate and close it via delete-process */
  1859.  
  1860. DEFUN ("open-network-stream", Fopen_network_stream, Sopen_network_stream, 
  1861.        4, 4, 0, 
  1862.   "Open a TCP connection for a service to a host.\n\
  1863. Returns a subprocess-object to represent the connection.\n\
  1864. Input and output work as for subprocesses; `delete-process' closes it.\n\
  1865. Args are NAME BUFFER HOST SERVICE.\n\
  1866. NAME is name for process.  It is modified if necessary to make it unique.\n\
  1867. BUFFER is the buffer (or buffer-name) to associate with the process.\n\
  1868.  Process output goes at end of that buffer, unless you specify\n\
  1869.  an output stream or filter function to handle the output.\n\
  1870.  BUFFER may be also nil, meaning that this process is not associated\n\
  1871.  with any buffer\n\
  1872. Third arg is name of the host to connect to, or its IP address.\n\
  1873. Fourth arg SERVICE is name of the service desired, or an integer\n\
  1874.  specifying a port number to connect to.")
  1875.    (name, buffer, host, service)
  1876.       Lisp_Object name, buffer, host, service;
  1877. {
  1878.   Lisp_Object proc;
  1879.   register int i;
  1880.   struct sockaddr_in address;
  1881.   struct servent *svc_info;
  1882.   struct hostent *host_info_ptr, host_info;
  1883.   char *(addr_list[2]);
  1884.   IN_ADDR numeric_addr;
  1885.   int s, outch, inch;
  1886.   char errstring[80];
  1887.   int port;
  1888.   struct hostent host_info_fixed;
  1889.   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  1890.   int retry = 0;
  1891.   int count = specpdl_ptr - specpdl;
  1892.  
  1893. #ifdef WINDOWSNT
  1894.   /* Ensure socket support is loaded if available. */
  1895.   init_winsock (TRUE);
  1896. #endif
  1897.  
  1898.   GCPRO4 (name, buffer, host, service);
  1899.   CHECK_STRING (name, 0);
  1900.   CHECK_STRING (host, 0);
  1901.   if (INTEGERP (service))
  1902.     port = htons ((unsigned short) XINT (service));
  1903.   else
  1904.     {
  1905.       CHECK_STRING (service, 0);
  1906.       svc_info = getservbyname (XSTRING (service)->data, "tcp");
  1907.       if (svc_info == 0)
  1908.     error ("Unknown service \"%s\"", XSTRING (service)->data);
  1909.       port = svc_info->s_port;
  1910.     }
  1911.  
  1912.   /* Slow down polling to every ten seconds.
  1913.      Some kernels have a bug which causes retrying connect to fail
  1914.      after a connect.  Polling can interfere with gethostbyname too.  */
  1915. #ifdef POLL_FOR_INPUT
  1916.   bind_polling_period (10);
  1917. #endif
  1918.  
  1919. #ifndef TERM
  1920.   while (1)
  1921.     {
  1922. #ifdef TRY_AGAIN
  1923.       h_errno = 0;
  1924. #endif
  1925.       immediate_quit = 1;
  1926.       QUIT;
  1927.       host_info_ptr = gethostbyname (XSTRING (host)->data);
  1928.       immediate_quit = 0;
  1929. #ifdef TRY_AGAIN
  1930.       if (! (host_info_ptr == 0 && h_errno == TRY_AGAIN))
  1931. #endif
  1932.     break;
  1933.       Fsleep_for (make_number (1), Qnil);
  1934.     }
  1935.   if (host_info_ptr == 0)
  1936.     /* Attempt to interpret host as numeric inet address */
  1937.     {
  1938.       numeric_addr = inet_addr ((char *) XSTRING (host)->data);
  1939.       if (NUMERIC_ADDR_ERROR)
  1940.     error ("Unknown host \"%s\"", XSTRING (host)->data);
  1941.  
  1942.       host_info_ptr = &host_info;
  1943.       host_info.h_name = 0;
  1944.       host_info.h_aliases = 0;
  1945.       host_info.h_addrtype = AF_INET;
  1946. #ifdef h_addr
  1947.       /* Older machines have only one address slot called h_addr.
  1948.      Newer machines have h_addr_list, but #define h_addr to
  1949.      be its first element.  */
  1950.       host_info.h_addr_list = &(addr_list[0]);
  1951. #endif
  1952.       host_info.h_addr = (char*)(&numeric_addr);
  1953.       addr_list[1] = 0;
  1954.       /* numeric_addr isn't null-terminated; it has fixed length.  */
  1955.       host_info.h_length = sizeof (numeric_addr);
  1956.     }
  1957.  
  1958.   bzero (&address, sizeof address);
  1959.   bcopy (host_info_ptr->h_addr, (char *) &address.sin_addr,
  1960.      host_info_ptr->h_length);
  1961.   address.sin_family = host_info_ptr->h_addrtype;
  1962.   address.sin_port = port;
  1963.  
  1964.   s = socket (host_info_ptr->h_addrtype, SOCK_STREAM, 0);
  1965.   if (s < 0) 
  1966.     report_file_error ("error creating socket", Fcons (name, Qnil));
  1967.  
  1968.   /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
  1969.      when connect is interrupted.  So let's not let it get interrupted.
  1970.      Note we do not turn off polling, because polling is only used
  1971.      when not interrupt_input, and thus not normally used on the systems
  1972.      which have this bug.  On systems which use polling, there's no way
  1973.      to quit if polling is turned off.  */
  1974.   if (interrupt_input)
  1975.     unrequest_sigio ();
  1976.  
  1977.  loop:
  1978.  
  1979.   immediate_quit = 1;
  1980.   QUIT;
  1981.  
  1982.   if (connect (s, (struct sockaddr *) &address, sizeof address) == -1
  1983.       && errno != EISCONN)
  1984.     {
  1985.       int xerrno = errno;
  1986.  
  1987.       immediate_quit = 0;
  1988.  
  1989.       if (errno == EINTR)
  1990.     goto loop;
  1991.       if (errno == EADDRINUSE && retry < 20)
  1992.     {
  1993.       /* A delay here is needed on some FreeBSD systems,
  1994.          and it is harmless, since this retrying takes time anyway
  1995.          and should be infrequent.  */
  1996.       Fsleep_for (make_number (1), Qnil);
  1997.       retry++;
  1998.       goto loop;
  1999.     }
  2000.  
  2001.       close (s);
  2002.  
  2003.       if (interrupt_input)
  2004.     request_sigio ();
  2005.  
  2006.       errno = xerrno;
  2007.       report_file_error ("connection failed",
  2008.              Fcons (host, Fcons (name, Qnil)));
  2009.     }
  2010.  
  2011.   immediate_quit = 0;
  2012.  
  2013. #ifdef POLL_FOR_INPUT
  2014.   unbind_to (count, Qnil);
  2015. #endif
  2016.  
  2017.   if (interrupt_input)
  2018.     request_sigio ();
  2019.  
  2020. #else /* TERM */
  2021.   s = connect_server (0);
  2022.   if (s < 0)
  2023.     report_file_error ("error creating socket", Fcons (name, Qnil));
  2024.   send_command (s, C_PORT, 0, "%s:%d", XSTRING (host)->data, ntohs (port));
  2025.   send_command (s, C_DUMB, 1, 0);
  2026. #endif /* TERM */
  2027.  
  2028.   inch = s;
  2029.   outch = s;
  2030.  
  2031.   if (!NILP (buffer))
  2032.     buffer = Fget_buffer_create (buffer);
  2033.   proc = make_process (name);
  2034.  
  2035.   chan_process[inch] = proc;
  2036.  
  2037. #ifdef O_NONBLOCK
  2038.   fcntl (inch, F_SETFL, O_NONBLOCK);
  2039. #else
  2040. #ifdef O_NDELAY
  2041.   fcntl (inch, F_SETFL, O_NDELAY);
  2042. #endif
  2043. #endif
  2044.  
  2045.   XPROCESS (proc)->childp = Fcons (host, Fcons (service, Qnil));
  2046.   XPROCESS (proc)->command_channel_p = Qnil;
  2047.   XPROCESS (proc)->buffer = buffer;
  2048.   XPROCESS (proc)->sentinel = Qnil;
  2049.   XPROCESS (proc)->filter = Qnil;
  2050.   XPROCESS (proc)->command = Qnil;
  2051.   XPROCESS (proc)->pid = Qnil;
  2052.   XSETINT (XPROCESS (proc)->infd, inch);
  2053.   XSETINT (XPROCESS (proc)->outfd, outch);
  2054.   XPROCESS (proc)->status = Qrun;
  2055.   FD_SET (inch, &input_wait_mask);
  2056.   FD_SET (inch, &non_keyboard_wait_mask);
  2057.   if (inch > max_process_desc)
  2058.     max_process_desc = inch;
  2059.  
  2060.   {
  2061.     /* Setup coding systems for communicating with the network stream.  */
  2062.     struct gcpro gcpro1;
  2063.     /* Qt denotes we have not yet called Ffind_operation_coding_system.  */
  2064.     Lisp_Object coding_systems = Qt;
  2065.     Lisp_Object args[5], val;
  2066.  
  2067.     if (!NILP (Vcoding_system_for_read))
  2068.       val = Vcoding_system_for_read;
  2069.     else if (!NILP (buffer) && NILP (XBUFFER (buffer)->enable_multibyte_characters)
  2070.          || NILP (buffer) && NILP (buffer_defaults.enable_multibyte_characters))
  2071.       /* We dare not decode end-of-line format by setting VAL to
  2072.      Qraw_text, because the existing Emacs Lisp libraries
  2073.      assume that they receive bare code including a sequene of
  2074.      CR LF.  */
  2075.       val = Qnil;
  2076.     else
  2077.       {
  2078.     args[0] = Qopen_network_stream, args[1] = name,
  2079.       args[2] = buffer, args[3] = host, args[4] = service;
  2080.     GCPRO1 (proc);
  2081.     coding_systems = Ffind_operation_coding_system (5, args);
  2082.     UNGCPRO;
  2083.     if (CONSP (coding_systems))
  2084.       val = XCONS (coding_systems)->car;
  2085.     else if (CONSP (Vdefault_process_coding_system))
  2086.       val = XCONS (Vdefault_process_coding_system)->car;
  2087.     else
  2088.       val = Qnil;
  2089.       }
  2090.     XPROCESS (proc)->decode_coding_system = val;
  2091.  
  2092.     if (!NILP (Vcoding_system_for_write))
  2093.       val = Vcoding_system_for_write;
  2094.     else if (NILP (current_buffer->enable_multibyte_characters))
  2095.       val = Qnil;
  2096.     else
  2097.       {
  2098.     if (EQ (coding_systems, Qt))
  2099.       {
  2100.         args[0] = Qopen_network_stream, args[1] = name,
  2101.           args[2] = buffer, args[3] = host, args[4] = service;
  2102.         GCPRO1 (proc);
  2103.         coding_systems = Ffind_operation_coding_system (5, args);
  2104.         UNGCPRO;
  2105.       }
  2106.     if (CONSP (coding_systems))
  2107.       val = XCONS (coding_systems)->cdr;
  2108.     else if (CONSP (Vdefault_process_coding_system))
  2109.       val = XCONS (Vdefault_process_coding_system)->cdr;
  2110.     else
  2111.       val = Qnil;
  2112.       }
  2113.     XPROCESS (proc)->encode_coding_system = val;
  2114.   }
  2115.  
  2116.   if (!proc_decode_coding_system[inch])
  2117.     proc_decode_coding_system[inch]
  2118.       = (struct coding_system *) xmalloc (sizeof (struct coding_system));
  2119.   setup_coding_system (XPROCESS (proc)->decode_coding_system,
  2120.                proc_decode_coding_system[inch]);
  2121.   if (!proc_encode_coding_system[outch])
  2122.     proc_encode_coding_system[outch]
  2123.       = (struct coding_system *) xmalloc (sizeof (struct coding_system));
  2124.   setup_coding_system (XPROCESS (proc)->encode_coding_system,
  2125.                proc_encode_coding_system[outch]);
  2126.  
  2127.   XPROCESS (proc)->decoding_buf = make_uninit_string (0);
  2128.   XPROCESS (proc)->decoding_carryover = make_number (0);
  2129.   XPROCESS (proc)->encoding_buf = make_uninit_string (0);
  2130.   XPROCESS (proc)->encoding_carryover = make_number (0);
  2131.  
  2132.   XPROCESS (proc)->inherit_coding_system_flag
  2133.     = (NILP (buffer) || !inherit_process_coding_system
  2134.        ? Qnil : Qt);
  2135.  
  2136.   UNGCPRO;
  2137.   return proc;
  2138. }
  2139. #endif    /* HAVE_SOCKETS */
  2140.  
  2141. void
  2142. deactivate_process (proc)
  2143.      Lisp_Object proc;
  2144. {
  2145.   register int inchannel, outchannel;
  2146.   register struct Lisp_Process *p = XPROCESS (proc);
  2147.  
  2148.   inchannel = XINT (p->infd);
  2149.   outchannel = XINT (p->outfd);
  2150.  
  2151.   if (inchannel >= 0)
  2152.     {
  2153.       /* Beware SIGCHLD hereabouts. */
  2154.       flush_pending_output (inchannel);
  2155. #ifdef VMS
  2156.       {
  2157.     VMS_PROC_STUFF *get_vms_process_pointer (), *vs;
  2158.     sys$dassgn (outchannel);
  2159.     vs = get_vms_process_pointer (p->pid);
  2160.     if (vs)
  2161.       give_back_vms_process_stuff (vs);
  2162.       }
  2163. #else
  2164.       close (inchannel);
  2165.       if (outchannel >= 0 && outchannel != inchannel)
  2166.      close (outchannel);
  2167. #endif
  2168.  
  2169.       XSETINT (p->infd, -1);
  2170.       XSETINT (p->outfd, -1);
  2171.       chan_process[inchannel] = Qnil;
  2172.       FD_CLR (inchannel, &input_wait_mask);
  2173.       FD_CLR (inchannel, &non_keyboard_wait_mask);
  2174.       if (inchannel == max_process_desc)
  2175.     {
  2176.       int i;
  2177.       /* We just closed the highest-numbered process input descriptor,
  2178.          so recompute the highest-numbered one now.  */
  2179.       max_process_desc = 0;
  2180.       for (i = 0; i < MAXDESC; i++)
  2181.         if (!NILP (chan_process[i]))
  2182.           max_process_desc = i;
  2183.     }
  2184.     }
  2185. }
  2186.  
  2187. /* Close all descriptors currently in use for communication
  2188.    with subprocess.  This is used in a newly-forked subprocess
  2189.    to get rid of irrelevant descriptors.  */
  2190.  
  2191. void
  2192. close_process_descs ()
  2193. {
  2194. #ifndef WINDOWSNT
  2195.   int i;
  2196. #ifndef EMX
  2197.   for (i = 0; i < MAXDESC; i++)
  2198.     {
  2199.       Lisp_Object process;
  2200.       process = chan_process[i];
  2201.       if (!NILP (process))
  2202.     {
  2203.       int in = XINT (XPROCESS (process)->infd);
  2204.       int out = XINT (XPROCESS (process)->outfd);
  2205.       if (in >= 0)
  2206.         close (in);
  2207.       if (out >= 0 && in != out)
  2208.         close (out);
  2209.     }
  2210.     }
  2211. #else /* EMX */
  2212.       for (i = 3; i < MAXDESC; i++)
  2213.         fcntl (i, F_SETFD, 1);
  2214. #endif /* EMX */
  2215. #endif
  2216. }
  2217.  
  2218. DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output,
  2219.   0, 3, 0,
  2220.   "Allow any pending output from subprocesses to be read by Emacs.\n\
  2221. It is read into the process' buffers or given to their filter functions.\n\
  2222. Non-nil arg PROCESS means do not return until some output has been received\n\
  2223. from PROCESS.\n\
  2224. Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of\n\
  2225. seconds and microseconds to wait; return after that much time whether\n\
  2226. or not there is input.\n\
  2227. Return non-nil iff we received any output before the timeout expired.")
  2228.   (process, timeout, timeout_msecs)
  2229.      register Lisp_Object process, timeout, timeout_msecs;
  2230. {
  2231.   int seconds;
  2232.   int useconds;
  2233.  
  2234.   if (! NILP (timeout_msecs))
  2235.     {
  2236.       CHECK_NUMBER (timeout_msecs, 2);
  2237.       useconds = XINT (timeout_msecs);
  2238.       if (!INTEGERP (timeout))
  2239.     XSETINT (timeout, 0);
  2240.  
  2241.       {
  2242.     int carry = useconds / 1000000;
  2243.  
  2244.     XSETINT (timeout, XINT (timeout) + carry);
  2245.     useconds -= carry * 1000000;
  2246.  
  2247.     /* I think this clause is necessary because C doesn't
  2248.        guarantee a particular rounding direction for negative
  2249.        integers.  */
  2250.     if (useconds < 0)
  2251.       {
  2252.         XSETINT (timeout, XINT (timeout) - 1);
  2253.         useconds += 1000000;
  2254.       }
  2255.       }
  2256.     }
  2257.   else
  2258.     useconds = 0;
  2259.  
  2260.   if (! NILP (timeout))
  2261.     {
  2262.       CHECK_NUMBER (timeout, 1);
  2263.       seconds = XINT (timeout);
  2264.       if (seconds < 0 || (seconds == 0 && useconds == 0))
  2265.     seconds = -1;
  2266.     }
  2267.   else
  2268.     {
  2269.       if (NILP (process))
  2270.     seconds = -1;
  2271.       else
  2272.     seconds = 0;
  2273.     }
  2274.  
  2275.   if (NILP (process))
  2276.     XSETFASTINT (process, 0);
  2277.  
  2278.   return
  2279.     (wait_reading_process_input (seconds, useconds, process, 0)
  2280.      ? Qt : Qnil);
  2281. }
  2282.  
  2283. /* This variable is different from waiting_for_input in keyboard.c.
  2284.    It is used to communicate to a lisp process-filter/sentinel (via the
  2285.    function Fwaiting_for_user_input_p below) whether emacs was waiting
  2286.    for user-input when that process-filter was called.
  2287.    waiting_for_input cannot be used as that is by definition 0 when
  2288.    lisp code is being evalled.
  2289.    This is also used in record_asynch_buffer_change.
  2290.    For that purpose, this must be 0
  2291.    when not inside wait_reading_process_input.  */
  2292. static int waiting_for_user_input_p;
  2293.  
  2294. /* This is here so breakpoints can be put on it.  */
  2295. static void
  2296. wait_reading_process_input_1 ()
  2297. {
  2298. }
  2299.  
  2300. /* Read and dispose of subprocess output while waiting for timeout to
  2301.    elapse and/or keyboard input to be available.
  2302.  
  2303.    TIME_LIMIT is:
  2304.      timeout in seconds, or
  2305.      zero for no limit, or
  2306.      -1 means gobble data immediately available but don't wait for any.
  2307.  
  2308.    MICROSECS is:
  2309.      an additional duration to wait, measured in microseconds.
  2310.      If this is nonzero and time_limit is 0, then the timeout
  2311.      consists of MICROSECS only.
  2312.  
  2313.    READ_KBD is a lisp value:
  2314.      0 to ignore keyboard input, or
  2315.      1 to return when input is available, or
  2316.      -1 meaning caller will actually read the input, so don't throw to
  2317.        the quit handler, or
  2318.      a cons cell, meaning wait until its car is non-nil
  2319.        (and gobble terminal input into the buffer if any arrives), or
  2320.      a process object, meaning wait until something arrives from that
  2321.        process.  The return value is true iff we read some input from
  2322.        that process.
  2323.  
  2324.    DO_DISPLAY != 0 means redisplay should be done to show subprocess
  2325.    output that arrives.
  2326.  
  2327.    If READ_KBD is a pointer to a struct Lisp_Process, then the
  2328.      function returns true iff we received input from that process
  2329.      before the timeout elapsed.
  2330.    Otherwise, return true iff we received input from any process.  */
  2331.  
  2332. int
  2333. wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
  2334.      int time_limit, microsecs;
  2335.      Lisp_Object read_kbd;
  2336.      int do_display;
  2337. {
  2338.   register int channel, nfds, m;
  2339.   static SELECT_TYPE Available;
  2340.   int xerrno;
  2341.   Lisp_Object proc;
  2342.   EMACS_TIME timeout, end_time, garbage;
  2343.   SELECT_TYPE Atemp;
  2344.   int wait_channel = -1;
  2345.   struct Lisp_Process *wait_proc = 0;
  2346.   int got_some_input = 0;
  2347.   Lisp_Object *wait_for_cell = 0;
  2348.  
  2349.   FD_ZERO (&Available);
  2350.  
  2351.   /* If read_kbd is a process to watch, set wait_proc and wait_channel
  2352.      accordingly.  */
  2353.   if (PROCESSP (read_kbd))
  2354.     {
  2355.       wait_proc = XPROCESS (read_kbd);
  2356.       wait_channel = XINT (wait_proc->infd);
  2357.       XSETFASTINT (read_kbd, 0);
  2358.     }
  2359.  
  2360.   /* If waiting for non-nil in a cell, record where.  */
  2361.   if (CONSP (read_kbd))
  2362.     {
  2363.       wait_for_cell = &XCONS (read_kbd)->car;
  2364.       XSETFASTINT (read_kbd, 0);
  2365.     }
  2366.  
  2367.   waiting_for_user_input_p = XINT (read_kbd);
  2368.  
  2369.   /* Since we may need to wait several times,
  2370.      compute the absolute time to return at.  */
  2371.   if (time_limit || microsecs)
  2372.     {
  2373.       EMACS_GET_TIME (end_time);
  2374.       EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
  2375.       EMACS_ADD_TIME (end_time, end_time, timeout);
  2376.     }
  2377. #ifdef hpux
  2378.   /* AlainF 5-Jul-1996
  2379.      HP-UX 10.10 seem to have problems with signals coming in
  2380.      Causes "poll: interrupted system call" messages when Emacs is run
  2381.      in an X window
  2382.      Turn off periodic alarms (in case they are in use) */
  2383.   stop_polling ();
  2384. #endif
  2385.  
  2386.   while (1)
  2387.     {
  2388.       int timeout_reduced_for_timers = 0;
  2389.  
  2390.       /* If calling from keyboard input, do not quit
  2391.      since we want to return C-g as an input character.
  2392.      Otherwise, do pending quit if requested.  */
  2393.       if (XINT (read_kbd) >= 0)
  2394.     QUIT;
  2395.  
  2396.       /* Exit now if the cell we're waiting for became non-nil.  */
  2397.       if (wait_for_cell && ! NILP (*wait_for_cell))
  2398.     break;
  2399.  
  2400.       /* Compute time from now till when time limit is up */
  2401.       /* Exit if already run out */
  2402.       if (time_limit == -1)
  2403.     {
  2404.       /* -1 specified for timeout means
  2405.          gobble output available now
  2406.          but don't wait at all. */
  2407.  
  2408.       EMACS_SET_SECS_USECS (timeout, 0, 0);
  2409.     }
  2410.       else if (time_limit || microsecs)
  2411.     {
  2412.       EMACS_GET_TIME (timeout);
  2413.       EMACS_SUB_TIME (timeout, end_time, timeout);
  2414.       if (EMACS_TIME_NEG_P (timeout))
  2415.         break;
  2416.     }
  2417.       else
  2418.     {
  2419.       EMACS_SET_SECS_USECS (timeout, 100000, 0);
  2420.     }
  2421.  
  2422.       /* Normally we run timers here.
  2423.      But not if wait_for_cell; in those cases,
  2424.      the wait is supposed to be short,
  2425.      and those callers cannot handle running arbitrary Lisp code here.  */
  2426.       if (! wait_for_cell)
  2427.     {
  2428.       EMACS_TIME timer_delay;
  2429.       int old_timers_run;
  2430.  
  2431.     retry:
  2432.       old_timers_run = timers_run;
  2433.       timer_delay = timer_check (1);
  2434.       if (timers_run != old_timers_run && do_display)
  2435.         {
  2436.           redisplay_preserve_echo_area ();
  2437.           /* We must retry, since a timer may have requeued itself
  2438.          and that could alter the time_delay.  */
  2439.           goto retry;
  2440.         }
  2441.  
  2442.       /* If there is unread keyboard input, also return.  */
  2443.       if (XINT (read_kbd) != 0
  2444.           && requeued_events_pending_p ())
  2445.         break;
  2446.  
  2447.       if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1)
  2448.         {
  2449.           EMACS_TIME difference;
  2450.           EMACS_SUB_TIME (difference, timer_delay, timeout);
  2451.           if (EMACS_TIME_NEG_P (difference))
  2452.         {
  2453.           timeout = timer_delay;
  2454.           timeout_reduced_for_timers = 1;
  2455.         }
  2456.         }
  2457.       /* If time_limit is -1, we are not going to wait at all.  */
  2458.       else if (time_limit != -1)
  2459.         {
  2460.           /* This is so a breakpoint can be put here.  */
  2461.           wait_reading_process_input_1 ();
  2462.         }
  2463.     }
  2464.  
  2465.       /* Cause C-g and alarm signals to take immediate action,
  2466.      and cause input available signals to zero out timeout.
  2467.  
  2468.      It is important that we do this before checking for process
  2469.      activity.  If we get a SIGCHLD after the explicit checks for
  2470.      process activity, timeout is the only way we will know.  */
  2471.       if (XINT (read_kbd) < 0)
  2472.     set_waiting_for_input (&timeout);
  2473.  
  2474.       /* If status of something has changed, and no input is
  2475.      available, notify the user of the change right away.  After
  2476.      this explicit check, we'll let the SIGCHLD handler zap
  2477.      timeout to get our attention.  */
  2478.       if (update_tick != process_tick && do_display)
  2479.     {
  2480.       Atemp = input_wait_mask;
  2481.       EMACS_SET_SECS_USECS (timeout, 0, 0);
  2482.       if ((select (max (max_process_desc, max_keyboard_desc) + 1,
  2483.                &Atemp, (SELECT_TYPE *)0, (SELECT_TYPE *)0,
  2484.                &timeout)
  2485.            <= 0))
  2486.         {
  2487.           /* It's okay for us to do this and then continue with
  2488.          the loop, since timeout has already been zeroed out.  */
  2489.           clear_waiting_for_input ();
  2490.           status_notify ();
  2491.         }
  2492.     }
  2493.  
  2494.       /* Don't wait for output from a non-running process.  */
  2495.       if (wait_proc != 0 && !NILP (wait_proc->raw_status_low))
  2496.     update_status (wait_proc);
  2497.       if (wait_proc != 0
  2498.       && ! EQ (wait_proc->status, Qrun))
  2499.     {
  2500.       int nread, total_nread = 0;
  2501.  
  2502.       clear_waiting_for_input ();
  2503.       XSETPROCESS (proc, wait_proc);
  2504.  
  2505.       /* Read data from the process, until we exhaust it.  */
  2506.       while (XINT (wait_proc->infd) >= 0
  2507.          && (nread
  2508.              = read_process_output (proc, XINT (wait_proc->infd))))
  2509.         {
  2510.               if (0 < nread) 
  2511.                 total_nread += nread;
  2512. #ifdef EIO
  2513.           else if (nread == -1 && EIO == errno)
  2514.                 break;
  2515. #endif
  2516.         }
  2517.       if (total_nread > 0 && do_display)
  2518.         redisplay_preserve_echo_area ();
  2519.  
  2520.       break;
  2521.     }
  2522.  
  2523.       /* Wait till there is something to do */
  2524.  
  2525.       if (wait_for_cell)
  2526.     Available = non_process_wait_mask;
  2527.       else if (! XINT (read_kbd))
  2528.     Available = non_keyboard_wait_mask;
  2529.       else
  2530.     Available = input_wait_mask;
  2531.  
  2532.       /* If frame size has changed or the window is newly mapped,
  2533.      redisplay now, before we start to wait.  There is a race
  2534.      condition here; if a SIGIO arrives between now and the select
  2535.      and indicates that a frame is trashed, the select may block
  2536.      displaying a trashed screen.  */
  2537.       if (frame_garbaged && do_display)
  2538.     {
  2539.       clear_waiting_for_input ();
  2540.       redisplay_preserve_echo_area ();
  2541.       if (XINT (read_kbd) < 0)
  2542.         set_waiting_for_input (&timeout);
  2543.     }
  2544.  
  2545.       if (XINT (read_kbd) && detect_input_pending ())
  2546.     {
  2547.       nfds = 0;
  2548.       FD_ZERO (&Available);
  2549.     }
  2550.       else
  2551.     nfds = select (max (max_process_desc, max_keyboard_desc) + 1,
  2552.                &Available, (SELECT_TYPE *)0, (SELECT_TYPE *)0,
  2553.                &timeout);
  2554.  
  2555.       xerrno = errno;
  2556.  
  2557.       /* Make C-g and alarm signals set flags again */
  2558.       clear_waiting_for_input ();
  2559.  
  2560.       /*  If we woke up due to SIGWINCH, actually change size now.  */
  2561.       do_pending_window_change ();
  2562.  
  2563.       if (time_limit && nfds == 0 && ! timeout_reduced_for_timers)
  2564.     /* We wanted the full specified time, so return now.  */
  2565.     break;
  2566.       if (nfds < 0)
  2567.     {
  2568.       if (xerrno == EINTR)
  2569.         FD_ZERO (&Available);
  2570. #ifdef ultrix
  2571.       /* Ultrix select seems to return ENOMEM when it is
  2572.          interrupted.  Treat it just like EINTR.  Bleah.  Note
  2573.          that we want to test for the "ultrix" CPP symbol, not
  2574.          "__ultrix__"; the latter is only defined under GCC, but
  2575.          not by DEC's bundled CC.  -JimB  */
  2576.       else if (xerrno == ENOMEM)
  2577.         FD_ZERO (&Available);
  2578. #endif
  2579. #ifdef ALLIANT
  2580.       /* This happens for no known reason on ALLIANT.
  2581.          I am guessing that this is the right response. -- RMS.  */
  2582.       else if (xerrno == EFAULT)
  2583.         FD_ZERO (&Available);
  2584. #endif
  2585.       else if (xerrno == EBADF)
  2586.         {
  2587. #ifdef AIX
  2588.           /* AIX doesn't handle PTY closure the same way BSD does.  On AIX,
  2589.          the child's closure of the pts gives the parent a SIGHUP, and
  2590.          the ptc file descriptor is automatically closed,
  2591.          yielding EBADF here or at select() call above.
  2592.          So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
  2593.          in m/ibmrt-aix.h), and here we just ignore the select error.
  2594.          Cleanup occurs c/o status_notify after SIGCLD. */
  2595.           FD_ZERO (&Available); /* Cannot depend on values returned */
  2596. #else
  2597.           abort ();
  2598. #endif
  2599.         }
  2600.       else
  2601.         error ("select error: %s", strerror (xerrno));
  2602.     }
  2603. #if defined(sun) && !defined(USG5_4)
  2604.       else if (nfds > 0 && keyboard_bit_set (&Available)
  2605.            && interrupt_input)
  2606.     /* System sometimes fails to deliver SIGIO.
  2607.  
  2608.        David J. Mackenzie says that Emacs doesn't compile under
  2609.        Solaris if this code is enabled, thus the USG5_4 in the CPP
  2610.        conditional.  "I haven't noticed any ill effects so far.
  2611.        If you find a Solaris expert somewhere, they might know
  2612.        better." */
  2613.     kill (getpid (), SIGIO);
  2614. #endif
  2615.  
  2616. #if 0 /* When polling is used, interrupt_input is 0,
  2617.      so get_input_pending should read the input.
  2618.      So this should not be needed.  */
  2619.       /* If we are using polling for input,
  2620.      and we see input available, make it get read now.
  2621.      Otherwise it might not actually get read for a second.
  2622.      And on hpux, since we turn off polling in wait_reading_process_input,
  2623.      it might never get read at all if we don't spend much time
  2624.      outside of wait_reading_process_input.  */
  2625.       if (XINT (read_kbd) && interrupt_input
  2626.       && keyboard_bit_set (&Available)
  2627.       && input_polling_used ())
  2628.     kill (getpid (), SIGALRM);
  2629. #endif
  2630.  
  2631.       /* Check for keyboard input */
  2632.       /* If there is any, return immediately
  2633.      to give it higher priority than subprocesses */
  2634.  
  2635.       if (XINT (read_kbd) != 0
  2636.       && detect_input_pending_run_timers (do_display))
  2637.     {
  2638.       swallow_events (do_display);
  2639.       if (detect_input_pending_run_timers (do_display))
  2640.         break;
  2641.     }
  2642.  
  2643.       /* If there is unread keyboard input, also return.  */
  2644.       if (XINT (read_kbd) != 0
  2645.       && requeued_events_pending_p ())
  2646.     break;
  2647.  
  2648.       /* If we are not checking for keyboard input now,
  2649.      do process events (but don't run any timers).
  2650.      This is so that X events will be processed.
  2651.      Otherwise they may have to wait until polling takes place.
  2652.      That would causes delays in pasting selections, for example.
  2653.  
  2654.      (We used to do this only if wait_for_cell.)  */
  2655.       if (XINT (read_kbd) == 0 && detect_input_pending ())
  2656.     {
  2657.       swallow_events (do_display);
  2658. #if 0  /* Exiting when read_kbd doesn't request that seems wrong, though.  */
  2659.       if (detect_input_pending ())
  2660.         break;
  2661. #endif
  2662.     }
  2663.  
  2664.       /* Exit now if the cell we're waiting for became non-nil.  */
  2665.       if (wait_for_cell && ! NILP (*wait_for_cell))
  2666.     break;
  2667.  
  2668. #ifdef SIGIO
  2669.       /* If we think we have keyboard input waiting, but didn't get SIGIO,
  2670.      go read it.  This can happen with X on BSD after logging out.
  2671.      In that case, there really is no input and no SIGIO,
  2672.      but select says there is input.  */
  2673.  
  2674.       if (XINT (read_kbd) && interrupt_input
  2675.       && keyboard_bit_set (&Available))
  2676.     kill (getpid (), SIGIO);
  2677. #endif
  2678.  
  2679.       if (! wait_proc)
  2680.     got_some_input |= nfds > 0;
  2681.  
  2682.       /* If checking input just got us a size-change event from X,
  2683.      obey it now if we should.  */
  2684.       if (XINT (read_kbd) || wait_for_cell)
  2685.     do_pending_window_change ();
  2686.  
  2687.       /* Check for data from a process.  */
  2688.       /* Really FIRST_PROC_DESC should be 0 on Unix,
  2689.      but this is safer in the short run.  */
  2690.       for (channel = 0; channel <= max_process_desc; channel++)
  2691.     {
  2692.       if (FD_ISSET (channel, &Available)
  2693.           && FD_ISSET (channel, &non_keyboard_wait_mask))
  2694.         {
  2695.           int nread;
  2696.  
  2697.           /* If waiting for this channel, arrange to return as
  2698.          soon as no more input to be processed.  No more
  2699.          waiting.  */
  2700.           if (wait_channel == channel)
  2701.         {
  2702.           wait_channel = -1;
  2703.           time_limit = -1;
  2704.           got_some_input = 1;
  2705.         }
  2706.           proc = chan_process[channel];
  2707.           if (NILP (proc))
  2708.         continue;
  2709.  
  2710.           /* Read data from the process, starting with our
  2711.          buffered-ahead character if we have one.  */
  2712.  
  2713.           nread = read_process_output (proc, channel);
  2714.           if (nread > 0)
  2715.         {
  2716.           /* Since read_process_output can run a filter,
  2717.              which can call accept-process-output,
  2718.              don't try to read from any other processes
  2719.              before doing the select again.  */
  2720.           FD_ZERO (&Available);
  2721.  
  2722.           if (do_display)
  2723.             redisplay_preserve_echo_area ();
  2724.         }
  2725. #ifdef EWOULDBLOCK
  2726.           else if (nread == -1 && errno == EWOULDBLOCK)
  2727.         ;
  2728. #endif
  2729.           /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
  2730.          and Emacs uses O_NONBLOCK, so what we get is EAGAIN.  */
  2731. #ifdef O_NONBLOCK
  2732.           else if (nread == -1 && errno == EAGAIN)
  2733.         ;
  2734. #else
  2735. #ifdef O_NDELAY
  2736.           else if (nread == -1 && errno == EAGAIN)
  2737.         ;
  2738.           /* Note that we cannot distinguish between no input
  2739.          available now and a closed pipe.
  2740.          With luck, a closed pipe will be accompanied by
  2741.          subprocess termination and SIGCHLD.  */
  2742.           else if (nread == 0 && !NETCONN_P (proc))
  2743.         ;
  2744. #endif                /* O_NDELAY */
  2745. #endif                /* O_NONBLOCK */
  2746. #ifdef HAVE_PTYS
  2747.           /* On some OSs with ptys, when the process on one end of
  2748.          a pty exits, the other end gets an error reading with
  2749.          errno = EIO instead of getting an EOF (0 bytes read).
  2750.          Therefore, if we get an error reading and errno =
  2751.          EIO, just continue, because the child process has
  2752.          exited and should clean itself up soon (e.g. when we
  2753.          get a SIGCHLD).
  2754.  
  2755.          However, it has been known to happen that the SIGCHLD
  2756.          got lost.  So raise the signl again just in case.
  2757.          It can't hurt.  */
  2758.           else if (nread == -1 && errno == EIO)
  2759.         kill (getpid (), SIGCHLD);
  2760. #endif                /* HAVE_PTYS */
  2761.           /* If we can detect process termination, don't consider the process
  2762.          gone just because its pipe is closed.  */
  2763. #ifdef SIGCHLD
  2764.           else if (nread == 0 && !NETCONN_P (proc))
  2765.         ;
  2766. #endif
  2767.           else
  2768.         {
  2769.           /* Preserve status of processes already terminated.  */
  2770.           XSETINT (XPROCESS (proc)->tick, ++process_tick);
  2771.           deactivate_process (proc);
  2772.           if (!NILP (XPROCESS (proc)->raw_status_low))
  2773.             update_status (XPROCESS (proc));
  2774.           if (EQ (XPROCESS (proc)->status, Qrun))
  2775.             XPROCESS (proc)->status
  2776.               = Fcons (Qexit, Fcons (make_number (256), Qnil));
  2777.         }
  2778.         }
  2779.     }            /* end for each file descriptor */
  2780.     }                /* end while exit conditions not met */
  2781.  
  2782.   waiting_for_user_input_p = 0;
  2783.  
  2784.   /* If calling from keyboard input, do not quit
  2785.      since we want to return C-g as an input character.
  2786.      Otherwise, do pending quit if requested.  */
  2787.   if (XINT (read_kbd) >= 0)
  2788.     {
  2789.       /* Prevent input_pending from remaining set if we quit.  */
  2790.       clear_input_pending ();
  2791.       QUIT;
  2792.     }
  2793. #ifdef hpux
  2794.   /* AlainF 5-Jul-1996
  2795.      HP-UX 10.10 seems to have problems with signals coming in
  2796.      Causes "poll: interrupted system call" messages when Emacs is run
  2797.      in an X window
  2798.      Turn periodic alarms back on */
  2799.   start_polling ();
  2800. #endif
  2801.    
  2802.   return got_some_input;
  2803. }
  2804.  
  2805. /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS.  */
  2806.  
  2807. static Lisp_Object
  2808. read_process_output_call (fun_and_args)
  2809.      Lisp_Object fun_and_args;
  2810. {
  2811.   return apply1 (XCONS (fun_and_args)->car, XCONS (fun_and_args)->cdr);
  2812. }
  2813.  
  2814. static Lisp_Object
  2815. read_process_output_error_handler (error)
  2816.      Lisp_Object error;
  2817. {
  2818.   cmd_error_internal (error, "error in process filter: ");
  2819.   Vinhibit_quit = Qt;
  2820.   update_echo_area ();
  2821.   Fsleep_for (make_number (2), Qnil);
  2822. }
  2823.  
  2824. /* Read pending output from the process channel,
  2825.    starting with our buffered-ahead character if we have one.
  2826.    Yield number of decoded characters read.
  2827.  
  2828.    This function reads at most 1024 characters.
  2829.    If you want to read all available subprocess output,
  2830.    you must call it repeatedly until it returns zero.
  2831.  
  2832.    The characters read are decoded according to PROC's coding-system
  2833.    for decoding.  */
  2834.  
  2835. int
  2836. read_process_output (proc, channel)
  2837.      Lisp_Object proc;
  2838.      register int channel;
  2839. {
  2840.   register int nchars, nbytes;
  2841.   char *chars;
  2842. #ifdef VMS
  2843.   int chars_allocated = 0;    /* If 1, `chars' should be freed later.  */
  2844. #else
  2845.   char buf[1024];
  2846. #endif
  2847.   register Lisp_Object outstream;
  2848.   register struct buffer *old = current_buffer;
  2849.   register struct Lisp_Process *p = XPROCESS (proc);
  2850.   register int opoint;
  2851.   struct coding_system *coding = proc_decode_coding_system[channel];
  2852.   int chars_in_decoding_buf = 0; /* If 1, `chars' points
  2853.                     XSTRING (p->decoding_buf)->data.  */
  2854.   int carryover = XINT (p->decoding_carryover);
  2855.  
  2856. #ifdef VMS
  2857.   VMS_PROC_STUFF *vs, *get_vms_process_pointer();
  2858.  
  2859.   vs = get_vms_process_pointer (p->pid);
  2860.   if (vs)
  2861.     {
  2862.       if (!vs->iosb[0])
  2863.     return(0);        /* Really weird if it does this */
  2864.       if (!(vs->iosb[0] & 1))
  2865.     return -1;        /* I/O error */
  2866.     }
  2867.   else
  2868.     error ("Could not get VMS process pointer");
  2869.   chars = vs->inputBuffer;
  2870.   nbytes = clean_vms_buffer (chars, vs->iosb[1]);
  2871.   if (nbytes <= 0)
  2872.     {
  2873.       start_vms_process_read (vs); /* Crank up the next read on the process */
  2874.       return 1;            /* Nothing worth printing, say we got 1 */
  2875.     }
  2876.   if (carryover > 0)
  2877.     {
  2878.       /* The data carried over in the previous decoding (which are at
  2879.          the tail of decoding buffer) should be prepended to the new
  2880.          data read to decode all together.  */
  2881.       char *buf = (char *) xmalloc (nbytes + carryover);
  2882.  
  2883.       bcopy (XSTRING (p->decoding_buf)->data
  2884.          + STRING_BYTES (XSTRING (p->decoding_buf)) - carryover,
  2885.          buf, carryover);
  2886.       bcopy (chars, buf + carryover, nbytes);
  2887.       chars = buf;
  2888.       chars_allocated = 1;
  2889.     }
  2890. #else /* not VMS */
  2891.  
  2892.   if (carryover)
  2893.     /* See the comment above.  */
  2894.     bcopy (XSTRING (p->decoding_buf)->data
  2895.        + STRING_BYTES (XSTRING (p->decoding_buf)) - carryover,
  2896.        buf, carryover);
  2897.  
  2898. #ifdef EMX
  2899.   fcntl (channel, F_SETFL, O_NDELAY);
  2900. #endif /* EMX */
  2901.  
  2902.   if (proc_buffered_char[channel] < 0)
  2903.     nbytes = read (channel, buf + carryover, (sizeof buf) - carryover);
  2904.   else
  2905.     {
  2906.       buf[carryover] = proc_buffered_char[channel];
  2907.       proc_buffered_char[channel] = -1;
  2908.       nbytes = read (channel, buf + carryover + 1,
  2909.              (sizeof buf) - carryover - 1);
  2910.       if (nbytes < 0)
  2911.     nbytes = 1;
  2912.       else
  2913.     nbytes = nbytes + 1;
  2914.     }
  2915.   chars = buf;
  2916. #endif /* not VMS */
  2917.  
  2918.   XSETINT (p->decoding_carryover, 0);
  2919.  
  2920.   /* At this point, NBYTES holds number of characters just received
  2921.      (including the one in proc_buffered_char[channel]).  */
  2922.   if (nbytes <= 0) return nbytes;
  2923.  
  2924.   /* Now set NBYTES how many bytes we must decode.  */
  2925.   nbytes += carryover;
  2926.   nchars = nbytes;
  2927.  
  2928.   if (CODING_MAY_REQUIRE_DECODING (coding))
  2929.     {
  2930.       int require = decoding_buffer_size (coding, nbytes);
  2931.       int result;
  2932.       
  2933.       if (STRING_BYTES (XSTRING (p->decoding_buf)) < require)
  2934.     p->decoding_buf = make_uninit_string (require);
  2935.       result = decode_coding (coding, chars, XSTRING (p->decoding_buf)->data,
  2936.                   nbytes, STRING_BYTES (XSTRING (p->decoding_buf)));
  2937.       carryover = nbytes - coding->consumed;
  2938.       if (carryover > 0)
  2939.     {
  2940.       /* Copy the carryover bytes to the end of p->decoding_buf, to
  2941.          be processed on the next read.  Since decoding_buffer_size
  2942.          asks for an extra amount of space beyond the maximum
  2943.          expected for the output, there should always be sufficient
  2944.          space for the carryover (which is by definition a sequence
  2945.          of bytes that was not long enough to be decoded, and thus
  2946.          has a bounded length).  */
  2947.       if (STRING_BYTES (XSTRING (p->decoding_buf))
  2948.           < coding->produced + carryover)
  2949.         abort ();
  2950.       bcopy (chars + coding->consumed,
  2951.          XSTRING (p->decoding_buf)->data
  2952.          + STRING_BYTES (XSTRING (p->decoding_buf)) - carryover,
  2953.          carryover);
  2954.       XSETINT (p->decoding_carryover, carryover);
  2955.     }
  2956.  
  2957.       /* A new coding system might be found by `decode_coding'.  */
  2958.       if (!EQ (p->decode_coding_system, coding->symbol))
  2959.     {
  2960.       p->decode_coding_system = coding->symbol;
  2961.  
  2962.       /* Don't call setup_coding_system for
  2963.              proc_decode_coding_system[channel] here.  It is done in
  2964.              detect_coding called via decode_coding above.  */
  2965.  
  2966.       /* If a coding system for encoding is not yet decided, we set
  2967.          it as the same as coding-system for decoding.
  2968.  
  2969.          But, before doing that we must check if
  2970.          proc_encode_coding_system[p->outfd] surely points to a
  2971.          valid memory because p->outfd will be changed once EOF is
  2972.          sent to the process.  */
  2973.       if (NILP (p->encode_coding_system)
  2974.           && proc_encode_coding_system[XINT (p->outfd)])
  2975.         {
  2976.           p->encode_coding_system = coding->symbol;
  2977.           setup_coding_system (coding->symbol,
  2978.                    proc_encode_coding_system[XINT (p->outfd)]);
  2979.         }
  2980.     }
  2981.  
  2982. #ifdef VMS
  2983.       /*  Now we don't need the contents of `chars'.  */
  2984.       if (chars_allocated)
  2985.     free (chars);
  2986. #endif
  2987.       if (coding->produced == 0)
  2988.     return 0;
  2989.       chars = (char *) XSTRING (p->decoding_buf)->data;
  2990.       nbytes = coding->produced;
  2991.       nchars = (coding->fake_multibyte
  2992.         ? multibyte_chars_in_text (chars, nbytes)
  2993.         : coding->produced_char);
  2994.       chars_in_decoding_buf = 1;
  2995.     }
  2996.   else
  2997.     {
  2998. #ifdef VMS
  2999.       if (chars_allocated)
  3000.     {
  3001.       /* Although we don't have to decode the received data, we
  3002.          must move it to an area which we don't have to free.  */
  3003.       if (! STRINGP (p->decoding_buf)
  3004.           || STRING_BYTES (XSTRING (p->decoding_buf)) < nbytes)
  3005.         p->decoding_buf = make_uninit_string (nbytes);
  3006.       bcopy (chars, XSTRING (p->decoding_buf)->data, nbytes);
  3007.       free (chars);
  3008.       chars = XSTRING (p->decoding_buf)->data;
  3009.       chars_in_decoding_buf = 1;
  3010.     }
  3011. #endif
  3012.       nchars = multibyte_chars_in_text (chars, nbytes);
  3013.     }
  3014.  
  3015.   Vlast_coding_system_used = coding->symbol;
  3016.  
  3017.   /* If the caller required, let the process associated buffer
  3018.      inherit the coding-system used to decode the process output.  */
  3019.   if (! NILP (p->inherit_coding_system_flag)
  3020.       && !NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name))
  3021.     {
  3022.       struct buffer *prev_buf = current_buffer;
  3023.  
  3024.       Fset_buffer (p->buffer);
  3025.       call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
  3026.          make_number (nbytes));
  3027.       set_buffer_internal (prev_buf);
  3028.     }
  3029.  
  3030.   /* Read and dispose of the process output.  */
  3031.   outstream = p->filter;
  3032.   if (!NILP (outstream))
  3033.     {
  3034.       /* We inhibit quit here instead of just catching it so that 
  3035.      hitting ^G when a filter happens to be running won't screw
  3036.      it up.  */
  3037.       int count = specpdl_ptr - specpdl;
  3038.       Lisp_Object odeactivate;
  3039.       Lisp_Object obuffer, okeymap;
  3040.       Lisp_Object text;
  3041.       int outer_running_asynch_code = running_asynch_code;
  3042.  
  3043.       /* No need to gcpro these, because all we do with them later
  3044.      is test them for EQness, and none of them should be a string.  */
  3045.       odeactivate = Vdeactivate_mark;
  3046.       XSETBUFFER (obuffer, current_buffer);
  3047.       okeymap = current_buffer->keymap;
  3048.  
  3049.       specbind (Qinhibit_quit, Qt);
  3050.       specbind (Qlast_nonmenu_event, Qt);
  3051.  
  3052.       /* In case we get recursively called,
  3053.      and we already saved the match data nonrecursively,
  3054.      save the same match data in safely recursive fashion.  */
  3055.       if (outer_running_asynch_code)
  3056.     {
  3057.       Lisp_Object tem;
  3058.       /* Don't clobber the CURRENT match data, either!  */
  3059.       tem = Fmatch_data (Qnil, Qnil);
  3060.       restore_match_data ();
  3061.       record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
  3062.       Fset_match_data (tem);
  3063.     }
  3064.  
  3065.       /* For speed, if a search happens within this code,
  3066.      save the match data in a special nonrecursive fashion.  */
  3067.       running_asynch_code = 1;
  3068.  
  3069.       text = make_string_from_bytes (chars, nchars, nbytes);
  3070.       internal_condition_case_1 (read_process_output_call,
  3071.                  Fcons (outstream,
  3072.                     Fcons (proc, Fcons (text, Qnil))),
  3073.                  !NILP (Vdebug_on_error) ? Qnil : Qerror,
  3074.                  read_process_output_error_handler);
  3075.  
  3076.       /* If we saved the match data nonrecursively, restore it now.  */
  3077.       restore_match_data ();
  3078.       running_asynch_code = outer_running_asynch_code;
  3079.  
  3080.       /* Handling the process output should not deactivate the mark.  */
  3081.       Vdeactivate_mark = odeactivate;
  3082.  
  3083. #if 0 /* Call record_asynch_buffer_change unconditionally,
  3084.      because we might have changed minor modes or other things
  3085.      that affect key bindings.  */
  3086.       if (! EQ (Fcurrent_buffer (), obuffer)
  3087.       || ! EQ (current_buffer->keymap, okeymap))
  3088. #endif
  3089.     /* But do it only if the caller is actually going to read events.
  3090.        Otherwise there's no need to make him wake up, and it could
  3091.        cause trouble (for example it would make Fsit_for return).  */
  3092.     if (waiting_for_user_input_p == -1)
  3093.       record_asynch_buffer_change ();
  3094.  
  3095. #ifdef VMS
  3096.       start_vms_process_read (vs);
  3097. #endif
  3098.       unbind_to (count, Qnil);
  3099.       return nchars;
  3100.     }
  3101.  
  3102.   /* If no filter, write into buffer if it isn't dead.  */
  3103.   if (!NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name))
  3104.     {
  3105.       Lisp_Object old_read_only;
  3106.       int old_begv, old_zv;
  3107.       int old_begv_byte, old_zv_byte;
  3108.       Lisp_Object odeactivate;
  3109.       int before, before_byte;
  3110.       int opoint_byte;
  3111.  
  3112.       odeactivate = Vdeactivate_mark;
  3113.  
  3114.       Fset_buffer (p->buffer);
  3115.       opoint = PT;
  3116.       opoint_byte = PT_BYTE;
  3117.       old_read_only = current_buffer->read_only;
  3118.       old_begv = BEGV;
  3119.       old_zv = ZV;
  3120.       old_begv_byte = BEGV_BYTE;
  3121.       old_zv_byte = ZV_BYTE;
  3122.  
  3123.       current_buffer->read_only = Qnil;
  3124.  
  3125.       /* Insert new output into buffer
  3126.      at the current end-of-output marker,
  3127.      thus preserving logical ordering of input and output.  */
  3128.       if (XMARKER (p->mark)->buffer)
  3129.     SET_PT_BOTH (clip_to_bounds (BEGV, marker_position (p->mark), ZV),
  3130.              clip_to_bounds (BEGV_BYTE, marker_byte_position (p->mark),
  3131.                      ZV_BYTE));
  3132.       else
  3133.     SET_PT_BOTH (ZV, ZV_BYTE);
  3134.       before = PT;
  3135.       before_byte = PT_BYTE;
  3136.  
  3137.       /* If the output marker is outside of the visible region, save
  3138.      the restriction and widen.  */
  3139.       if (! (BEGV <= PT && PT <= ZV))
  3140.     Fwiden ();
  3141.  
  3142.       if (NILP (current_buffer->enable_multibyte_characters))
  3143.     nchars = nbytes;
  3144.  
  3145.       /* Insert before markers in case we are inserting where
  3146.      the buffer's mark is, and the user's next command is Meta-y.  */
  3147.       if (chars_in_decoding_buf)
  3148.     {
  3149.       /* Since multibyteness of p->docoding_buf is corrupted, we
  3150.              can't use insert_from_string_before_markers.  */
  3151.       char *temp_buf;
  3152.  
  3153.       temp_buf = (char *) alloca (nbytes);
  3154.       bcopy (XSTRING (p->decoding_buf)->data, temp_buf, nbytes);
  3155.       insert_before_markers (temp_buf, nbytes);
  3156.     }
  3157.       else
  3158.     insert_1_both (chars, nchars, nbytes, 0, 1, 1);
  3159.       set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
  3160.  
  3161.       update_mode_lines++;
  3162.  
  3163.       /* Make sure opoint and the old restrictions
  3164.      float ahead of any new text just as point would.  */
  3165.       if (opoint >= before)
  3166.     {
  3167.       opoint += PT - before;
  3168.       opoint_byte += PT_BYTE - before_byte;
  3169.     }
  3170.       if (old_begv > before)
  3171.     {
  3172.       old_begv += PT - before;
  3173.       old_begv_byte += PT_BYTE - before_byte;
  3174.     }
  3175.       if (old_zv >= before)
  3176.     {
  3177.       old_zv += PT - before;
  3178.       old_zv_byte += PT_BYTE - before_byte;
  3179.     }
  3180.  
  3181.       /* If the restriction isn't what it should be, set it.  */
  3182.       if (old_begv != BEGV || old_zv != ZV)
  3183.     Fnarrow_to_region (make_number (old_begv), make_number (old_zv));
  3184.  
  3185.       /* Handling the process output should not deactivate the mark.  */
  3186.       Vdeactivate_mark = odeactivate;
  3187.  
  3188.       current_buffer->read_only = old_read_only;
  3189.       SET_PT_BOTH (opoint, opoint_byte);
  3190.       set_buffer_internal (old);
  3191.     }
  3192. #ifdef VMS
  3193.   start_vms_process_read (vs);
  3194. #endif
  3195.   return nbytes;
  3196. }
  3197.  
  3198. DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, Swaiting_for_user_input_p,
  3199.        0, 0, 0,
  3200.   "Returns non-nil if emacs is waiting for input from the user.\n\
  3201. This is intended for use by asynchronous process output filters and sentinels.")
  3202.        ()
  3203. {
  3204.   return (waiting_for_user_input_p ? Qt : Qnil);
  3205. }
  3206.  
  3207. /* Sending data to subprocess */
  3208.  
  3209. jmp_buf send_process_frame;
  3210.  
  3211. SIGTYPE
  3212. send_process_trap ()
  3213. {
  3214. #ifdef BSD4_1
  3215.   sigrelse (SIGPIPE);
  3216.   sigrelse (SIGALRM);
  3217. #endif /* BSD4_1 */
  3218.   longjmp (send_process_frame, 1);
  3219. }
  3220.  
  3221. /* Send some data to process PROC.
  3222.    BUF is the beginning of the data; LEN is the number of characters.
  3223.    OBJECT is the Lisp object that the data comes from.
  3224.  
  3225.    The data is encoded by PROC's coding-system for encoding before it
  3226.    is sent.  But if the data ends at the middle of multi-byte
  3227.    representation, that incomplete sequence of bytes are sent without
  3228.    being encoded.  Should we store them in a buffer to prepend them to
  3229.    the data send later?  */
  3230.  
  3231. void
  3232. send_process (proc, buf, len, object)
  3233.      volatile Lisp_Object proc;
  3234.      unsigned char *buf;
  3235.      int len;
  3236.      Lisp_Object object;
  3237. {
  3238.   /* Use volatile to protect variables from being clobbered by longjmp.  */
  3239.   int rv;
  3240.   volatile unsigned char *procname = XSTRING (XPROCESS (proc)->name)->data;
  3241.   struct coding_system *coding;
  3242.   struct gcpro gcpro1;
  3243.   int carryover = XINT (XPROCESS (proc)->encoding_carryover);
  3244.  
  3245.   GCPRO1 (object);
  3246.  
  3247. #ifdef VMS
  3248.   struct Lisp_Process *p = XPROCESS (proc);
  3249.   VMS_PROC_STUFF *vs, *get_vms_process_pointer();
  3250. #endif /* VMS */
  3251.  
  3252.   if (! NILP (XPROCESS (proc)->raw_status_low))
  3253.     update_status (XPROCESS (proc));
  3254.   if (! EQ (XPROCESS (proc)->status, Qrun))
  3255.     error ("Process %s not running", procname);
  3256.   if (XINT (XPROCESS (proc)->outfd) < 0)
  3257.     error ("Output file descriptor of %s is closed", procname);
  3258.  
  3259.   coding = proc_encode_coding_system[XINT (XPROCESS (proc)->outfd)];
  3260.   Vlast_coding_system_used = coding->symbol;
  3261.  
  3262.   if (CODING_REQUIRE_ENCODING (coding))
  3263.     {
  3264.       int require = encoding_buffer_size (coding, len);
  3265.       int offset, dummy;
  3266.       unsigned char *temp_buf = NULL;
  3267.  
  3268.       /* Remember the offset of data because a string or a buffer may
  3269.          be relocated.  Setting OFFSET to -1 means we don't have to
  3270.          care relocation.  */
  3271.       offset = (BUFFERP (object)
  3272.         ? BUF_PTR_BYTE_POS (XBUFFER (object), buf)
  3273.         : (STRINGP (object)
  3274.            ? offset = buf - XSTRING (object)->data
  3275.            : -1));
  3276.  
  3277.       if (carryover > 0)
  3278.     {
  3279.       temp_buf = (unsigned char *) xmalloc (len + carryover);
  3280.  
  3281.       if (offset >= 0)
  3282.         {
  3283.           if (BUFFERP (object))
  3284.         buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
  3285.           else if (STRINGP (object))
  3286.         buf = offset + XSTRING (object)->data;
  3287.           /* Now we don't have to care relocation.  */
  3288.           offset = -1;
  3289.         }
  3290.       bcopy ((XSTRING (XPROCESS (proc)->encoding_buf)->data
  3291.           + STRING_BYTES (XSTRING (XPROCESS (proc)->encoding_buf))
  3292.           - carryover),
  3293.          temp_buf,
  3294.          carryover);
  3295.       bcopy (buf, temp_buf + carryover, len);
  3296.       buf = temp_buf;
  3297.     }
  3298.  
  3299.       if (STRING_BYTES (XSTRING (XPROCESS (proc)->encoding_buf)) < require)
  3300.     {
  3301.       XPROCESS (proc)->encoding_buf = make_uninit_string (require);
  3302.  
  3303.       if (offset >= 0)
  3304.         {
  3305.           if (BUFFERP (object))
  3306.         buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
  3307.           else if (STRINGP (object))
  3308.         buf = offset + XSTRING (object)->data;
  3309.         }
  3310.     }
  3311.       object = XPROCESS (proc)->encoding_buf;
  3312.       encode_coding (coding, buf, XSTRING (object)->data,
  3313.              len, STRING_BYTES (XSTRING (object)));
  3314.       len = coding->produced;
  3315.       buf = XSTRING (object)->data;
  3316.       if (temp_buf)
  3317.     xfree (temp_buf);
  3318.     }
  3319.  
  3320. #ifdef VMS
  3321.   vs = get_vms_process_pointer (p->pid);
  3322.   if (vs == 0)
  3323.     error ("Could not find this process: %x", p->pid);
  3324.   else if (write_to_vms_process (vs, buf, len))
  3325.     ;
  3326. #else
  3327.  
  3328.   if (pty_max_bytes == 0)
  3329.     {
  3330. #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
  3331.       pty_max_bytes = fpathconf (XFASTINT (XPROCESS (proc)->outfd),
  3332.                  _PC_MAX_CANON);
  3333.       if (pty_max_bytes < 0)
  3334.     pty_max_bytes = 250;
  3335. #else
  3336.       pty_max_bytes = 250;
  3337. #endif
  3338.       /* Deduct one, to leave space for the eof.  */
  3339.       pty_max_bytes--;
  3340.     }
  3341.  
  3342.   if (!setjmp (send_process_frame))
  3343.     while (len > 0)
  3344.       {
  3345.     int this = len;
  3346.     SIGTYPE (*old_sigpipe)();
  3347.     int flush_pty = 0;
  3348.  
  3349.     /* Decide how much data we can send in one batch.
  3350.        Long lines need to be split into multiple batches.  */
  3351.     if (!NILP (XPROCESS (proc)->pty_flag))
  3352.       {
  3353.         /* Starting this at zero is always correct when not the first iteration
  3354.            because the previous iteration ended by sending C-d.
  3355.            It may not be correct for the first iteration
  3356.            if a partial line was sent in a separate send_process call.
  3357.            If that proves worth handling, we need to save linepos
  3358.            in the process object.  */
  3359.         int linepos = 0;
  3360.         unsigned char *ptr = buf;
  3361.         unsigned char *end = buf + len;
  3362.  
  3363.         /* Scan through this text for a line that is too long.  */
  3364.         while (ptr != end && linepos < pty_max_bytes)
  3365.           {
  3366.         if (*ptr == '\n')
  3367.           linepos = 0;
  3368.         else
  3369.           linepos++;
  3370.         ptr++;
  3371.           }
  3372.         /* If we found one, break the line there
  3373.            and put in a C-d to force the buffer through.  */
  3374.         this = ptr - buf;
  3375.       }
  3376.  
  3377.     /* Send this batch, using one or more write calls.  */
  3378.     while (this > 0)
  3379.       {
  3380.         old_sigpipe = (SIGTYPE (*) ()) signal (SIGPIPE, send_process_trap);
  3381.         rv = write (XINT (XPROCESS (proc)->outfd), buf, this);
  3382.         signal (SIGPIPE, old_sigpipe);
  3383.  
  3384.         if (rv < 0)
  3385.           {
  3386.         if (0
  3387. #ifdef EWOULDBLOCK
  3388.             || errno == EWOULDBLOCK
  3389. #endif
  3390. #ifdef EAGAIN
  3391.             || errno == EAGAIN
  3392. #endif
  3393.             )
  3394.           /* Buffer is full.  Wait, accepting input; 
  3395.              that may allow the program
  3396.              to finish doing output and read more.  */
  3397.           {
  3398.             Lisp_Object zero;
  3399.             int offset;
  3400.  
  3401.             /* Running filters might relocate buffers or strings.
  3402.                Arrange to relocate BUF.  */
  3403.             if (BUFFERP (object))
  3404.               offset = BUF_PTR_BYTE_POS (XBUFFER (object), buf);
  3405.             else if (STRINGP (object))
  3406.               offset = buf - XSTRING (object)->data;
  3407.  
  3408.             XSETFASTINT (zero, 0);
  3409. #ifdef EMACS_HAS_USECS
  3410.             wait_reading_process_input (0, 20000, zero, 0);
  3411. #else
  3412.             wait_reading_process_input (1, 0, zero, 0);
  3413. #endif
  3414.  
  3415.             if (BUFFERP (object))
  3416.               buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
  3417.             else if (STRINGP (object))
  3418.               buf = offset + XSTRING (object)->data;
  3419.  
  3420.             rv = 0;
  3421.           }
  3422.         else
  3423.           /* This is a real error.  */
  3424.           report_file_error ("writing to process", Fcons (proc, Qnil));
  3425.           }
  3426.         buf += rv;
  3427.         len -= rv;
  3428.         this -= rv;
  3429.       }
  3430.  
  3431.     /* If we sent just part of the string, put in an EOF
  3432.        to force it through, before we send the rest.  */
  3433.     if (len > 0)
  3434.       Fprocess_send_eof (proc);
  3435.       }
  3436. #endif
  3437.   else
  3438.     {
  3439.       XPROCESS (proc)->raw_status_low = Qnil;
  3440.       XPROCESS (proc)->raw_status_high = Qnil;
  3441.       XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (256), Qnil));
  3442.       XSETINT (XPROCESS (proc)->tick, ++process_tick);
  3443.       deactivate_process (proc);
  3444. #ifdef VMS
  3445.       error ("Error writing to process %s; closed it", procname);
  3446. #else
  3447.       error ("SIGPIPE raised on process %s; closed it", procname);
  3448. #endif
  3449.     }
  3450.  
  3451.   UNGCPRO;
  3452. }
  3453.  
  3454. DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region,
  3455.   3, 3, 0,
  3456.   "Send current contents of region as input to PROCESS.\n\
  3457. PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
  3458. nil, indicating the current buffer's process.\n\
  3459. Called from program, takes three arguments, PROCESS, START and END.\n\
  3460. If the region is more than 500 characters long,\n\
  3461. it is sent in several bunches.  This may happen even for shorter regions.\n\
  3462. Output from processes can arrive in between bunches.")
  3463.   (process, start, end)
  3464.      Lisp_Object process, start, end;
  3465. {
  3466.   Lisp_Object proc;
  3467.   int start1, end1;
  3468.  
  3469.   proc = get_process (process);
  3470.   validate_region (&start, &end);
  3471.  
  3472.   if (XINT (start) < GPT && XINT (end) > GPT)
  3473.     move_gap (XINT (start));
  3474.  
  3475.   start1 = CHAR_TO_BYTE (XINT (start));
  3476.   end1 = CHAR_TO_BYTE (XINT (end));
  3477.   send_process (proc, BYTE_POS_ADDR (start1), end1 - start1,
  3478.         Fcurrent_buffer ());
  3479.  
  3480.   return Qnil;
  3481. }
  3482.  
  3483. DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string,
  3484.   2, 2, 0,
  3485.   "Send PROCESS the contents of STRING as input.\n\
  3486. PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
  3487. nil, indicating the current buffer's process.\n\
  3488. If STRING is more than 500 characters long,\n\
  3489. it is sent in several bunches.  This may happen even for shorter strings.\n\
  3490. Output from processes can arrive in between bunches.")
  3491.   (process, string)
  3492.      Lisp_Object process, string;
  3493. {
  3494.   Lisp_Object proc;
  3495.   CHECK_STRING (string, 1);
  3496.   proc = get_process (process);
  3497.   send_process (proc, XSTRING (string)->data,
  3498.         STRING_BYTES (XSTRING (string)), string);
  3499.   return Qnil;
  3500. }
  3501.  
  3502. /* send a signal number SIGNO to PROCESS.
  3503.    CURRENT_GROUP means send to the process group that currently owns
  3504.    the terminal being used to communicate with PROCESS.
  3505.    This is used for various commands in shell mode.
  3506.    If NOMSG is zero, insert signal-announcements into process's buffers
  3507.    right away.
  3508.  
  3509.    If we can, we try to signal PROCESS by sending control characters
  3510.    down the pty.  This allows us to signal inferiors who have changed
  3511.    their uid, for which killpg would return an EPERM error.  */
  3512.  
  3513. static void
  3514. process_send_signal (process, signo, current_group, nomsg)
  3515.      Lisp_Object process;
  3516.      int signo;
  3517.      Lisp_Object current_group;
  3518.      int nomsg;
  3519. {
  3520.   Lisp_Object proc;
  3521.   register struct Lisp_Process *p;
  3522.   int gid;
  3523.   int no_pgrp = 0;
  3524.  
  3525.   proc = get_process (process);
  3526.   p = XPROCESS (proc);
  3527.  
  3528.   if (!EQ (p->childp, Qt))
  3529.     error ("Process %s is not a subprocess",
  3530.        XSTRING (p->name)->data);
  3531.   if (XINT (p->infd) < 0)
  3532.     error ("Process %s is not active",
  3533.        XSTRING (p->name)->data);
  3534.  
  3535.   if (NILP (p->pty_flag))
  3536.     current_group = Qnil;
  3537.  
  3538.   /* If we are using pgrps, get a pgrp number and make it negative.  */
  3539.   if (!NILP (current_group))
  3540.     {
  3541. #ifdef SIGNALS_VIA_CHARACTERS
  3542.       /* If possible, send signals to the entire pgrp
  3543.      by sending an input character to it.  */
  3544.  
  3545.       /* TERMIOS is the latest and bestest, and seems most likely to
  3546.          work.  If the system has it, use it.  */
  3547. #ifdef HAVE_TERMIOS
  3548.       struct termios t;
  3549.  
  3550.       switch (signo)
  3551.     {
  3552.     case SIGINT:
  3553.       tcgetattr (XINT (p->infd), &t);
  3554.       send_process (proc, &t.c_cc[VINTR], 1, Qnil);
  3555.       return;
  3556.  
  3557.     case SIGQUIT:
  3558.       tcgetattr (XINT (p->infd), &t);
  3559.         send_process (proc, &t.c_cc[VQUIT], 1, Qnil);
  3560.         return;
  3561.  
  3562.       case SIGTSTP:
  3563.       tcgetattr (XINT (p->infd), &t);
  3564. #if defined (VSWTCH) && !defined (PREFER_VSUSP)
  3565.         send_process (proc, &t.c_cc[VSWTCH], 1, Qnil);
  3566. #else
  3567.       send_process (proc, &t.c_cc[VSUSP], 1, Qnil);
  3568. #endif
  3569.         return;
  3570.     }
  3571.  
  3572. #else /* ! HAVE_TERMIOS */
  3573.  
  3574.       /* On Berkeley descendants, the following IOCTL's retrieve the
  3575.      current control characters.  */
  3576. #if defined (TIOCGLTC) && defined (TIOCGETC)
  3577.  
  3578.       struct tchars c;
  3579.       struct ltchars lc;
  3580.  
  3581.       switch (signo)
  3582.     {
  3583.     case SIGINT:
  3584.       ioctl (XINT (p->infd), TIOCGETC, &c);
  3585.       send_process (proc, &c.t_intrc, 1, Qnil);
  3586.       return;
  3587.     case SIGQUIT:
  3588.       ioctl (XINT (p->infd), TIOCGETC, &c);
  3589.       send_process (proc, &c.t_quitc, 1, Qnil);
  3590.       return;
  3591. #ifdef SIGTSTP
  3592.     case SIGTSTP:
  3593.       ioctl (XINT (p->infd), TIOCGLTC, &lc);
  3594.       send_process (proc, &lc.t_suspc, 1, Qnil);
  3595.       return;
  3596. #endif /* ! defined (SIGTSTP) */
  3597.     }
  3598.  
  3599. #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
  3600.  
  3601.       /* On SYSV descendants, the TCGETA ioctl retrieves the current control
  3602.      characters.  */
  3603. #ifdef TCGETA
  3604.       struct termio t;
  3605.       switch (signo)
  3606.     {
  3607.     case SIGINT:
  3608.       ioctl (XINT (p->infd), TCGETA, &t);
  3609.       send_process (proc, &t.c_cc[VINTR], 1, Qnil);
  3610.       return;
  3611.     case SIGQUIT:
  3612.       ioctl (XINT (p->infd), TCGETA, &t);
  3613.       send_process (proc, &t.c_cc[VQUIT], 1, Qnil);
  3614.       return;
  3615. #ifdef SIGTSTP
  3616.     case SIGTSTP:
  3617.       ioctl (XINT (p->infd), TCGETA, &t);
  3618.       send_process (proc, &t.c_cc[VSWTCH], 1, Qnil);
  3619.       return;
  3620. #endif /* ! defined (SIGTSTP) */
  3621.     }
  3622. #else /* ! defined (TCGETA) */
  3623.       Your configuration files are messed up.
  3624.       /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
  3625.      you'd better be using one of the alternatives above!  */
  3626. #endif /* ! defined (TCGETA) */
  3627. #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
  3628. #endif /* ! defined HAVE_TERMIOS */
  3629. #endif /* ! defined (SIGNALS_VIA_CHARACTERS) */
  3630.  
  3631. #ifdef TIOCGPGRP 
  3632.       /* Get the pgrp using the tty itself, if we have that.
  3633.      Otherwise, use the pty to get the pgrp.
  3634.      On pfa systems, saka@pfu.fujitsu.co.JP writes:
  3635.      "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
  3636.      But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
  3637.      His patch indicates that if TIOCGPGRP returns an error, then
  3638.      we should just assume that p->pid is also the process group id.  */
  3639.       {
  3640.     int err;
  3641.  
  3642.     if (!NILP (p->subtty))
  3643.       err = ioctl (XFASTINT (p->subtty), TIOCGPGRP, &gid);
  3644.     else
  3645.       err = ioctl (XINT (p->infd), TIOCGPGRP, &gid);
  3646.  
  3647. #ifdef pfa
  3648.     if (err == -1)
  3649.       gid = - XFASTINT (p->pid);
  3650. #endif /* ! defined (pfa) */
  3651.       }
  3652.       if (gid == -1)
  3653.     no_pgrp = 1;
  3654.       else
  3655.     gid = - gid;
  3656. #else  /* ! defined (TIOCGPGRP ) */
  3657.       /* Can't select pgrps on this system, so we know that
  3658.      the child itself heads the pgrp.  */
  3659.       gid = - XFASTINT (p->pid);
  3660. #endif /* ! defined (TIOCGPGRP ) */
  3661.     }
  3662.   else
  3663.     gid = - XFASTINT (p->pid);
  3664.  
  3665.   switch (signo)
  3666.     {
  3667. #ifdef SIGCONT
  3668.     case SIGCONT:
  3669.       p->raw_status_low = Qnil;
  3670.       p->raw_status_high = Qnil;
  3671.       p->status = Qrun;
  3672.       XSETINT (p->tick, ++process_tick);
  3673.       if (!nomsg)
  3674.     status_notify ();
  3675.       break;
  3676. #endif /* ! defined (SIGCONT) */
  3677.     case SIGINT:
  3678. #ifdef VMS
  3679.       send_process (proc, "\003", 1, Qnil);    /* ^C */
  3680.       goto whoosh;
  3681. #endif
  3682.     case SIGQUIT:
  3683. #ifdef VMS
  3684.       send_process (proc, "\031", 1, Qnil);    /* ^Y */
  3685.       goto whoosh;
  3686. #endif
  3687.     case SIGKILL:
  3688. #ifdef VMS
  3689.       sys$forcex (&(XFASTINT (p->pid)), 0, 1);
  3690.       whoosh:
  3691. #endif
  3692.       flush_pending_output (XINT (p->infd));
  3693.       break;
  3694.     }
  3695.  
  3696.   /* If we don't have process groups, send the signal to the immediate
  3697.      subprocess.  That isn't really right, but it's better than any
  3698.      obvious alternative.  */
  3699.   if (no_pgrp)
  3700.     {
  3701.       kill (XFASTINT (p->pid), signo);
  3702.       return;
  3703.     }
  3704.  
  3705.   /* gid may be a pid, or minus a pgrp's number */
  3706. #ifdef TIOCSIGSEND
  3707.   if (!NILP (current_group))
  3708.     ioctl (XINT (p->infd), TIOCSIGSEND, signo);
  3709.   else
  3710.     {
  3711.       gid = - XFASTINT (p->pid);
  3712.       kill (gid, signo);
  3713.     }
  3714. #else /* ! defined (TIOCSIGSEND) */
  3715.   EMACS_KILLPG (-gid, signo);
  3716. #endif /* ! defined (TIOCSIGSEND) */
  3717. }
  3718.  
  3719. DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0,
  3720.   "Interrupt process PROCESS.  May be process or name of one.\n\
  3721. PROCESS may be a process, a buffer, or the name of a process or buffer.\n\
  3722. nil or no arg means current buffer's process.\n\
  3723. Second arg CURRENT-GROUP non-nil means send signal to\n\
  3724. the current process-group of the process's controlling terminal\n\
  3725. rather than to the process's own process group.\n\
  3726. If the process is a shell, this means interrupt current subjob\n\
  3727. rather than the shell.")
  3728.   (process, current_group)
  3729.      Lisp_Object process, current_group;
  3730. {
  3731.   process_send_signal (process, SIGINT, current_group, 0);
  3732.   return process;
  3733. }
  3734.  
  3735. DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0,
  3736.   "Kill process PROCESS.  May be process or name of one.\n\
  3737. See function `interrupt-process' for more details on usage.")
  3738.   (process, current_group)
  3739.      Lisp_Object process, current_group;
  3740. {
  3741.   process_send_signal (process, SIGKILL, current_group, 0);
  3742.   return process;
  3743. }
  3744.  
  3745. DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0,
  3746.   "Send QUIT signal to process PROCESS.  May be process or name of one.\n\
  3747. See function `interrupt-process' for more details on usage.")
  3748.   (process, current_group)
  3749.      Lisp_Object process, current_group;
  3750. {
  3751.   process_send_signal (process, SIGQUIT, current_group, 0);
  3752.   return process;
  3753. }
  3754.  
  3755. DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0,
  3756.   "Stop process PROCESS.  May be process or name of one.\n\
  3757. See function `interrupt-process' for more details on usage.")
  3758.   (process, current_group)
  3759.      Lisp_Object process, current_group;
  3760. {
  3761. #ifndef SIGTSTP
  3762.   error ("no SIGTSTP support");
  3763. #else
  3764.   process_send_signal (process, SIGTSTP, current_group, 0);
  3765. #endif
  3766.   return process;
  3767. }
  3768.  
  3769. DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0,
  3770.   "Continue process PROCESS.  May be process or name of one.\n\
  3771. See function `interrupt-process' for more details on usage.")
  3772.   (process, current_group)
  3773.      Lisp_Object process, current_group;
  3774. {
  3775. #ifdef SIGCONT
  3776.     process_send_signal (process, SIGCONT, current_group, 0);
  3777. #else
  3778.     error ("no SIGCONT support");
  3779. #endif
  3780.   return process;
  3781. }
  3782.  
  3783. DEFUN ("signal-process", Fsignal_process, Ssignal_process,
  3784.   2, 2, "nProcess number: \nnSignal code: ",
  3785.   "Send the process with process id PID the signal with code SIGCODE.\n\
  3786. PID must be an integer.  The process need not be a child of this Emacs.\n\
  3787. SIGCODE may be an integer, or a symbol whose name is a signal name.")
  3788.   (pid, sigcode)
  3789.      Lisp_Object pid, sigcode;
  3790. {
  3791.   CHECK_NUMBER (pid, 0);
  3792.  
  3793. #define handle_signal(NAME, VALUE)        \
  3794.   else if (!strcmp (name, NAME))        \
  3795.     XSETINT (sigcode, VALUE)
  3796.  
  3797.   if (INTEGERP (sigcode))
  3798.     ;
  3799.   else
  3800.     {
  3801.       unsigned char *name;
  3802.  
  3803.       CHECK_SYMBOL (sigcode, 1);
  3804.       name = XSYMBOL (sigcode)->name->data;
  3805.  
  3806.       if (0)
  3807.     ;
  3808. #ifdef SIGHUP
  3809.       handle_signal ("SIGHUP", SIGHUP);
  3810. #endif
  3811. #ifdef SIGINT
  3812.       handle_signal ("SIGINT", SIGINT);
  3813. #endif
  3814. #ifdef SIGQUIT
  3815.       handle_signal ("SIGQUIT", SIGQUIT);
  3816. #endif
  3817. #ifdef SIGILL
  3818.       handle_signal ("SIGILL", SIGILL);
  3819. #endif
  3820. #ifdef SIGABRT
  3821.       handle_signal ("SIGABRT", SIGABRT);
  3822. #endif
  3823. #ifdef SIGEMT
  3824.       handle_signal ("SIGEMT", SIGEMT);
  3825. #endif
  3826. #ifdef SIGKILL
  3827.       handle_signal ("SIGKILL", SIGKILL);
  3828. #endif
  3829. #ifdef SIGFPE
  3830.       handle_signal ("SIGFPE", SIGFPE);
  3831. #endif
  3832. #ifdef SIGBUS
  3833.       handle_signal ("SIGBUS", SIGBUS);
  3834. #endif
  3835. #ifdef SIGSEGV
  3836.       handle_signal ("SIGSEGV", SIGSEGV);
  3837. #endif
  3838. #ifdef SIGSYS
  3839.       handle_signal ("SIGSYS", SIGSYS);
  3840. #endif
  3841. #ifdef SIGPIPE
  3842.       handle_signal ("SIGPIPE", SIGPIPE);
  3843. #endif
  3844. #ifdef SIGALRM
  3845.       handle_signal ("SIGALRM", SIGALRM);
  3846. #endif
  3847. #ifdef SIGTERM
  3848.       handle_signal ("SIGTERM", SIGTERM);
  3849. #endif
  3850. #ifdef SIGURG
  3851.       handle_signal ("SIGURG", SIGURG);
  3852. #endif
  3853. #ifdef SIGSTOP
  3854.       handle_signal ("SIGSTOP", SIGSTOP);
  3855. #endif
  3856. #ifdef SIGTSTP
  3857.       handle_signal ("SIGTSTP", SIGTSTP);
  3858. #endif
  3859. #ifdef SIGCONT
  3860.       handle_signal ("SIGCONT", SIGCONT);
  3861. #endif
  3862. #ifdef SIGCHLD
  3863.       handle_signal ("SIGCHLD", SIGCHLD);
  3864. #endif
  3865. #ifdef SIGTTIN
  3866.       handle_signal ("SIGTTIN", SIGTTIN);
  3867. #endif
  3868. #ifdef SIGTTOU
  3869.       handle_signal ("SIGTTOU", SIGTTOU);
  3870. #endif
  3871. #ifdef SIGIO
  3872.       handle_signal ("SIGIO", SIGIO);
  3873. #endif
  3874. #ifdef SIGXCPU
  3875.       handle_signal ("SIGXCPU", SIGXCPU);
  3876. #endif
  3877. #ifdef SIGXFSZ
  3878.       handle_signal ("SIGXFSZ", SIGXFSZ);
  3879. #endif
  3880. #ifdef SIGVTALRM
  3881.       handle_signal ("SIGVTALRM", SIGVTALRM);
  3882. #endif
  3883. #ifdef SIGPROF
  3884.       handle_signal ("SIGPROF", SIGPROF);
  3885. #endif
  3886. #ifdef SIGWINCH
  3887.       handle_signal ("SIGWINCH", SIGWINCH);
  3888. #endif
  3889. #ifdef SIGINFO
  3890.       handle_signal ("SIGINFO", SIGINFO);
  3891. #endif
  3892. #ifdef SIGUSR1
  3893.       handle_signal ("SIGUSR1", SIGUSR1);
  3894. #endif
  3895. #ifdef SIGUSR2
  3896.       handle_signal ("SIGUSR2", SIGUSR2);
  3897. #endif
  3898.       else
  3899.     error ("Undefined signal name %s", name);
  3900.     }
  3901.  
  3902. #undef handle_signal
  3903.  
  3904.   return make_number (kill (XINT (pid), XINT (sigcode)));
  3905. }
  3906.  
  3907. DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0,
  3908.   "Make PROCESS see end-of-file in its input.\n\
  3909. Eof comes after any text already sent to it.\n\
  3910. PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
  3911. nil, indicating the current buffer's process.\n\
  3912. If PROCESS is a network connection, or is a process communicating\n\
  3913. through a pipe (as opposed to a pty), then you cannot send any more\n\
  3914. text to PROCESS after you call this function.")
  3915.   (process)
  3916.      Lisp_Object process;
  3917. {
  3918.   Lisp_Object proc;
  3919.  
  3920.   proc = get_process (process);
  3921.  
  3922.   /* Make sure the process is really alive.  */
  3923.   if (! NILP (XPROCESS (proc)->raw_status_low))
  3924.     update_status (XPROCESS (proc));
  3925.   if (! EQ (XPROCESS (proc)->status, Qrun))
  3926.     error ("Process %s not running", XSTRING (XPROCESS (proc)->name)->data);
  3927.  
  3928. #ifdef VMS
  3929.   send_process (proc, "\032", 1, Qnil);     /* ^z */
  3930. #else
  3931.   if (!NILP (XPROCESS (proc)->pty_flag))
  3932.     send_process (proc, "\004", 1, Qnil);
  3933.   else
  3934.     {
  3935.       int old_outfd, new_outfd;
  3936.  
  3937. #ifdef HAVE_SHUTDOWN
  3938.       /* If this is a network connection, or socketpair is used
  3939.      for communication with the subprocess, call shutdown to cause EOF.
  3940.      (In some old system, shutdown to socketpair doesn't work.
  3941.      Then we just can't win.)  */
  3942.       if (NILP (XPROCESS (proc)->pid)
  3943.       || XINT (XPROCESS (proc)->outfd) == XINT (XPROCESS (proc)->infd))
  3944.     shutdown (XINT (XPROCESS (proc)->outfd), 1);
  3945.       /* In case of socketpair, outfd == infd, so don't close it.  */
  3946.       if (XINT (XPROCESS (proc)->outfd) != XINT (XPROCESS (proc)->infd))
  3947.     close (XINT (XPROCESS (proc)->outfd));
  3948. #else /* not HAVE_SHUTDOWN */
  3949.       close (XINT (XPROCESS (proc)->outfd));
  3950. #endif /* not HAVE_SHUTDOWN */
  3951.       new_outfd = open (NULL_DEVICE, O_WRONLY);
  3952.       old_outfd = XINT (XPROCESS (proc)->outfd);
  3953.  
  3954.       if (!proc_encode_coding_system[new_outfd])
  3955.     proc_encode_coding_system[new_outfd]
  3956.       = (struct coding_system *) xmalloc (sizeof (struct coding_system));
  3957.       bcopy (proc_encode_coding_system[old_outfd],
  3958.          proc_encode_coding_system[new_outfd],
  3959.          sizeof (struct coding_system));
  3960.       bzero (proc_encode_coding_system[old_outfd],
  3961.          sizeof (struct coding_system));
  3962.  
  3963.       XSETINT (XPROCESS (proc)->outfd, new_outfd);
  3964.     }
  3965. #endif /* VMS */
  3966.   return process;
  3967. }
  3968.  
  3969. /* Kill all processes associated with `buffer'.
  3970.  If `buffer' is nil, kill all processes  */
  3971.  
  3972. void
  3973. kill_buffer_processes (buffer)
  3974.      Lisp_Object buffer;
  3975. {
  3976.   Lisp_Object tail, proc;
  3977.  
  3978.   for (tail = Vprocess_alist; GC_CONSP (tail); tail = XCONS (tail)->cdr)
  3979.     {
  3980.       proc = XCONS (XCONS (tail)->car)->cdr;
  3981.       if (GC_PROCESSP (proc)
  3982.       && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer)))
  3983.     {
  3984.       if (NETCONN_P (proc))
  3985.         Fdelete_process (proc);
  3986.       else if (XINT (XPROCESS (proc)->infd) >= 0)
  3987.         process_send_signal (proc, SIGHUP, Qnil, 1);
  3988.     }
  3989.     }
  3990. }
  3991.  
  3992. /* On receipt of a signal that a child status has changed,
  3993.  loop asking about children with changed statuses until
  3994.  the system says there are no more.
  3995.    All we do is change the status;
  3996.  we do not run sentinels or print notifications.
  3997.  That is saved for the next time keyboard input is done,
  3998.  in order to avoid timing errors.  */
  3999.  
  4000. /** WARNING: this can be called during garbage collection.
  4001.  Therefore, it must not be fooled by the presence of mark bits in
  4002.  Lisp objects.  */
  4003.  
  4004. /** USG WARNING:  Although it is not obvious from the documentation
  4005.  in signal(2), on a USG system the SIGCLD handler MUST NOT call
  4006.  signal() before executing at least one wait(), otherwise the handler
  4007.  will be called again, resulting in an infinite loop.  The relevant
  4008.  portion of the documentation reads "SIGCLD signals will be queued
  4009.  and the signal-catching function will be continually reentered until
  4010.  the queue is empty".  Invoking signal() causes the kernel to reexamine
  4011.  the SIGCLD queue.   Fred Fish, UniSoft Systems Inc. */
  4012.  
  4013. SIGTYPE
  4014. sigchld_handler (signo)
  4015.      int signo;
  4016. {
  4017.   int old_errno = errno;
  4018.   Lisp_Object proc;
  4019.   register struct Lisp_Process *p;
  4020.   extern EMACS_TIME *input_available_clear_time;
  4021.  
  4022. #ifdef BSD4_1
  4023.   extern int sigheld;
  4024.   sigheld |= sigbit (SIGCHLD);
  4025. #endif
  4026.  
  4027.   while (1)
  4028.     {
  4029.       register int pid;
  4030.       WAITTYPE w;
  4031.       Lisp_Object tail;
  4032.  
  4033. #ifdef WNOHANG
  4034. #ifndef WUNTRACED
  4035. #define WUNTRACED 0
  4036. #endif /* no WUNTRACED */
  4037.       /* Keep trying to get a status until we get a definitive result.  */
  4038.       do 
  4039.     {
  4040.       errno = 0;
  4041.       pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
  4042.     }
  4043.       while (pid <= 0 && errno == EINTR);
  4044.  
  4045.       if (pid <= 0)
  4046.     {
  4047.       /* A real failure.  We have done all our job, so return.  */
  4048.  
  4049.       /* USG systems forget handlers when they are used;
  4050.          must reestablish each time */
  4051. #if defined (USG) && !defined (POSIX_SIGNALS)
  4052.       signal (signo, sigchld_handler);   /* WARNING - must come after wait3() */
  4053. #endif
  4054. #ifdef  BSD4_1
  4055.       sigheld &= ~sigbit (SIGCHLD);
  4056.       sigrelse (SIGCHLD);
  4057. #endif
  4058.       errno = old_errno;
  4059.       return;
  4060.     }
  4061. #else
  4062.       pid = wait (&w);
  4063. #endif /* no WNOHANG */
  4064.  
  4065.       /* Find the process that signaled us, and record its status.  */
  4066.  
  4067.       p = 0;
  4068.       for (tail = Vprocess_alist; CONSP (tail); tail = XCONS (tail)->cdr)
  4069.     {
  4070.       proc = XCONS (XCONS (tail)->car)->cdr;
  4071.       p = XPROCESS (proc);
  4072.       if (EQ (p->childp, Qt) && XFASTINT (p->pid) == pid)
  4073.         break;
  4074.       p = 0;
  4075.     }
  4076.  
  4077.       /* Look for an asynchronous process whose pid hasn't been filled
  4078.      in yet.  */
  4079.       if (p == 0)
  4080.     for (tail = Vprocess_alist; CONSP (tail); tail = XCONS (tail)->cdr)
  4081.       {
  4082.         proc = XCONS (XCONS (tail)->car)->cdr;
  4083.         p = XPROCESS (proc);
  4084.         if (INTEGERP (p->pid) && XINT (p->pid) == -1)
  4085.           break;
  4086.         p = 0;
  4087.       }
  4088.       
  4089.       /* Change the status of the process that was found.  */
  4090.       if (p != 0)
  4091.     {
  4092.       union { int i; WAITTYPE wt; } u;
  4093.       int clear_desc_flag = 0;
  4094.       
  4095.       XSETINT (p->tick, ++process_tick);
  4096.       u.wt = w;
  4097.       XSETINT (p->raw_status_low, u.i & 0xffff);
  4098.       XSETINT (p->raw_status_high, u.i >> 16);
  4099.       
  4100.       /* If process has terminated, stop waiting for its output.  */
  4101.       if ((WIFSIGNALED (w) || WIFEXITED (w))
  4102.           && XINT (p->infd) >= 0)
  4103.         clear_desc_flag = 1;
  4104.  
  4105.       /* We use clear_desc_flag to avoid a compiler bug in Microsoft C.  */
  4106.       if (clear_desc_flag)
  4107.         {
  4108.           FD_CLR (XINT (p->infd), &input_wait_mask);
  4109.           FD_CLR (XINT (p->infd), &non_keyboard_wait_mask);
  4110.         }
  4111.  
  4112.       /* Tell wait_reading_process_input that it needs to wake up and
  4113.          look around.  */
  4114.       if (input_available_clear_time)
  4115.         EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
  4116.     }
  4117.  
  4118.     /* There was no asynchronous process found for that id.  Check
  4119.        if we have a synchronous process.  */
  4120.       else
  4121.     {
  4122.       synch_process_alive = 0;
  4123.  
  4124.       /* Report the status of the synchronous process.  */
  4125.       if (WIFEXITED (w))
  4126.         synch_process_retcode = WRETCODE (w);
  4127.       else if (WIFSIGNALED (w))
  4128.         {
  4129.           int code = WTERMSIG (w);
  4130.           char *signame = 0;
  4131.  
  4132.           if (code < NSIG)
  4133.         {
  4134. #ifndef VMS
  4135.           /* Suppress warning if the table has const char *.  */
  4136.           signame = (char *) sys_siglist[code];
  4137. #else
  4138.           signame = sys_errlist[code];
  4139. #endif
  4140.         }
  4141.           if (signame == 0)
  4142.         signame = "unknown";
  4143.  
  4144.           synch_process_death = signame;
  4145.         }
  4146.  
  4147.       /* Tell wait_reading_process_input that it needs to wake up and
  4148.          look around.  */
  4149.       if (input_available_clear_time)
  4150.         EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
  4151.     }
  4152.  
  4153. #ifdef EMX
  4154.       emx_async_delete_now ();
  4155. #endif /* EMX */
  4156.       /* On some systems, we must return right away.
  4157.      If any more processes want to signal us, we will
  4158.      get another signal.
  4159.      Otherwise (on systems that have WNOHANG), loop around
  4160.      to use up all the processes that have something to tell us.  */
  4161. #if defined (EMX) && !defined (POSIX_SIGNALS)
  4162.       signal (signo, SIG_ACK);
  4163. #else /* not EMX, or POSIX_SIGNALS */
  4164. #if defined (USG) && ! (defined (HPUX) && defined (WNOHANG)) || defined (WINDOWSNT)
  4165. #if defined (USG) && ! defined (POSIX_SIGNALS)
  4166.       signal (signo, sigchld_handler);
  4167. #endif
  4168. #endif /* not EMX, or POSIX_SIGNALS*/
  4169.       errno = old_errno;
  4170.       return;
  4171. #endif /* USG, but not HPUX with WNOHANG */
  4172.     }
  4173. }
  4174.  
  4175.  
  4176. static Lisp_Object
  4177. exec_sentinel_unwind (data)
  4178.      Lisp_Object data;
  4179. {
  4180.   XPROCESS (XCONS (data)->car)->sentinel = XCONS (data)->cdr;
  4181.   return Qnil;
  4182. }
  4183.  
  4184. static Lisp_Object
  4185. exec_sentinel_error_handler (error)
  4186.      Lisp_Object error;
  4187. {
  4188.   cmd_error_internal (error, "error in process sentinel: ");
  4189.   Vinhibit_quit = Qt;
  4190.   update_echo_area ();
  4191.   Fsleep_for (make_number (2), Qnil);
  4192. }
  4193.  
  4194. static void
  4195. exec_sentinel (proc, reason)
  4196.      Lisp_Object proc, reason;
  4197. {
  4198.   Lisp_Object sentinel, obuffer, odeactivate, okeymap;
  4199.   register struct Lisp_Process *p = XPROCESS (proc);
  4200.   int count = specpdl_ptr - specpdl;
  4201.   int outer_running_asynch_code = running_asynch_code;
  4202.  
  4203.   /* No need to gcpro these, because all we do with them later
  4204.      is test them for EQness, and none of them should be a string.  */
  4205.   odeactivate = Vdeactivate_mark;
  4206.   XSETBUFFER (obuffer, current_buffer);
  4207.   okeymap = current_buffer->keymap;
  4208.  
  4209.   sentinel = p->sentinel;
  4210.   if (NILP (sentinel))
  4211.     return;
  4212.  
  4213.   /* Zilch the sentinel while it's running, to avoid recursive invocations;
  4214.      assure that it gets restored no matter how the sentinel exits.  */
  4215.   p->sentinel = Qnil;
  4216.   record_unwind_protect (exec_sentinel_unwind, Fcons (proc, sentinel));
  4217.   /* Inhibit quit so that random quits don't screw up a running filter.  */
  4218.   specbind (Qinhibit_quit, Qt);
  4219.   specbind (Qlast_nonmenu_event, Qt);
  4220.  
  4221.   /* In case we get recursively called,
  4222.      and we already saved the match data nonrecursively,
  4223.      save the same match data in safely recursive fashion.  */
  4224.   if (outer_running_asynch_code)
  4225.     {
  4226.       Lisp_Object tem;
  4227.       tem = Fmatch_data (Qnil, Qnil);
  4228.       restore_match_data ();
  4229.       record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
  4230.       Fset_match_data (tem);
  4231.     }
  4232.  
  4233.   /* For speed, if a search happens within this code,
  4234.      save the match data in a special nonrecursive fashion.  */
  4235.   running_asynch_code = 1;
  4236.  
  4237.   internal_condition_case_1 (read_process_output_call,
  4238.                  Fcons (sentinel,
  4239.                     Fcons (proc, Fcons (reason, Qnil))),
  4240.                  !NILP (Vdebug_on_error) ? Qnil : Qerror,
  4241.                  exec_sentinel_error_handler);
  4242.  
  4243.   /* If we saved the match data nonrecursively, restore it now.  */
  4244.   restore_match_data ();
  4245.   running_asynch_code = outer_running_asynch_code;
  4246.  
  4247.   Vdeactivate_mark = odeactivate;
  4248. #if 0
  4249.   if (! EQ (Fcurrent_buffer (), obuffer)
  4250.       || ! EQ (current_buffer->keymap, okeymap))
  4251. #endif
  4252.     /* But do it only if the caller is actually going to read events.
  4253.        Otherwise there's no need to make him wake up, and it could
  4254.        cause trouble (for example it would make Fsit_for return).  */
  4255.     if (waiting_for_user_input_p == -1)
  4256.       record_asynch_buffer_change ();
  4257.  
  4258.   unbind_to (count, Qnil);
  4259. }
  4260.  
  4261. /* Report all recent events of a change in process status
  4262.    (either run the sentinel or output a message).
  4263.    This is done while Emacs is waiting for keyboard input.  */
  4264.  
  4265. void
  4266. status_notify ()
  4267. {
  4268.   register Lisp_Object proc, buffer;
  4269.   Lisp_Object tail, msg;
  4270.   struct gcpro gcpro1, gcpro2;
  4271.  
  4272.   tail = Qnil;
  4273.   msg = Qnil;
  4274.   /* We need to gcpro tail; if read_process_output calls a filter
  4275.      which deletes a process and removes the cons to which tail points
  4276.      from Vprocess_alist, and then causes a GC, tail is an unprotected
  4277.      reference.  */
  4278.   GCPRO2 (tail, msg);
  4279.  
  4280.   /* Set this now, so that if new processes are created by sentinels
  4281.      that we run, we get called again to handle their status changes.  */
  4282.   update_tick = process_tick;
  4283.  
  4284.   for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
  4285.     {
  4286.       Lisp_Object symbol;
  4287.       register struct Lisp_Process *p;
  4288.  
  4289.       proc = Fcdr (Fcar (tail));
  4290.       p = XPROCESS (proc);
  4291.  
  4292.       if (XINT (p->tick) != XINT (p->update_tick))
  4293.     {
  4294.       XSETINT (p->update_tick, XINT (p->tick));
  4295.  
  4296.       /* If process is still active, read any output that remains.  */
  4297.       while (! EQ (p->filter, Qt)
  4298.          && XINT (p->infd) >= 0
  4299.          && read_process_output (proc, XINT (p->infd)) > 0);
  4300.  
  4301.       buffer = p->buffer;
  4302.  
  4303.       /* Get the text to use for the message.  */
  4304.       if (!NILP (p->raw_status_low))
  4305.         update_status (p);
  4306.       msg = status_message (p->status);
  4307.  
  4308.       /* If process is terminated, deactivate it or delete it.  */
  4309.       symbol = p->status;
  4310.       if (CONSP (p->status))
  4311.         symbol = XCONS (p->status)->car;
  4312.  
  4313.       if (EQ (symbol, Qsignal) || EQ (symbol, Qexit)
  4314.           || EQ (symbol, Qclosed))
  4315.         {
  4316.           if (delete_exited_processes)
  4317.         remove_process (proc);
  4318.           else
  4319.         deactivate_process (proc);
  4320.         }
  4321.  
  4322.       /* The actions above may have further incremented p->tick.
  4323.          So set p->update_tick again
  4324.          so that an error in the sentinel will not cause
  4325.          this code to be run again.  */
  4326.       XSETINT (p->update_tick, XINT (p->tick));
  4327.       /* Now output the message suitably.  */
  4328.       if (!NILP (p->sentinel))
  4329.         exec_sentinel (proc, msg);
  4330.       /* Don't bother with a message in the buffer
  4331.          when a process becomes runnable.  */
  4332.       else if (!EQ (symbol, Qrun) && !NILP (buffer))
  4333.         {
  4334.           Lisp_Object ro, tem;
  4335.           struct buffer *old = current_buffer;
  4336.           int opoint, opoint_byte;
  4337.           int before, before_byte;
  4338.  
  4339.           ro = XBUFFER (buffer)->read_only;
  4340.  
  4341.           /* Avoid error if buffer is deleted
  4342.          (probably that's why the process is dead, too) */
  4343.           if (NILP (XBUFFER (buffer)->name))
  4344.         continue;
  4345.           Fset_buffer (buffer);
  4346.  
  4347.           opoint = PT;
  4348.           opoint_byte = PT_BYTE;
  4349.           /* Insert new output into buffer
  4350.          at the current end-of-output marker,
  4351.          thus preserving logical ordering of input and output.  */
  4352.           if (XMARKER (p->mark)->buffer)
  4353.         Fgoto_char (p->mark);
  4354.           else
  4355.         SET_PT_BOTH (ZV, ZV_BYTE);
  4356.  
  4357.           before = PT;
  4358.           before_byte = PT_BYTE;
  4359.  
  4360.           tem = current_buffer->read_only;
  4361.           current_buffer->read_only = Qnil;
  4362.           insert_string ("\nProcess ");
  4363.           Finsert (1, &p->name);
  4364.           insert_string (" ");
  4365.           Finsert (1, &msg);
  4366.           current_buffer->read_only = tem;
  4367.           set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
  4368.  
  4369.           if (opoint >= before)
  4370.         SET_PT_BOTH (opoint + (PT - before),
  4371.                  opoint_byte + (PT_BYTE - before_byte));
  4372.           else
  4373.         SET_PT_BOTH (opoint, opoint_byte);
  4374.  
  4375.           set_buffer_internal (old);
  4376.         }
  4377.     }
  4378.     } /* end for */
  4379.  
  4380.   update_mode_lines++;  /* in case buffers use %s in mode-line-format */
  4381.   redisplay_preserve_echo_area ();
  4382.  
  4383.   UNGCPRO;
  4384. }
  4385.  
  4386.  
  4387. DEFUN ("set-process-coding-system", Fset_process_coding_system,
  4388.        Sset_process_coding_system, 1, 3, 0,
  4389.   "Set coding systems of PROCESS to DECODING (input from the process) and\n\
  4390. ENCODING (output to the process).")
  4391.   (proc, decoding, encoding)
  4392.      register Lisp_Object proc, decoding, encoding;
  4393. {
  4394.   register struct Lisp_Process *p;
  4395.  
  4396.   CHECK_PROCESS (proc, 0);
  4397.   p = XPROCESS (proc);
  4398.   if (XINT (p->infd) < 0)
  4399.     error ("Input file descriptor of %s closed", XSTRING (p->name)->data);
  4400.   if (XINT (p->outfd) < 0)
  4401.     error ("Output file descriptor of %s closed", XSTRING (p->name)->data);
  4402.  
  4403.   p->decode_coding_system = Fcheck_coding_system (decoding);
  4404.   p->encode_coding_system = Fcheck_coding_system (encoding);
  4405.   setup_coding_system (decoding,
  4406.                proc_decode_coding_system[XINT (p->infd)]);
  4407.   setup_coding_system (encoding,
  4408.                proc_encode_coding_system[XINT (p->outfd)]);
  4409.  
  4410.   return Qnil;
  4411. }
  4412.  
  4413. DEFUN ("process-coding-system",
  4414.        Fprocess_coding_system, Sprocess_coding_system, 1, 1, 0,
  4415.   "Return a cons of coding systems for decoding and encoding of PROCESS.")
  4416.   (proc)
  4417.      register Lisp_Object proc;
  4418. {
  4419.   CHECK_PROCESS (proc, 0);
  4420.   return Fcons (XPROCESS (proc)->decode_coding_system,
  4421.         XPROCESS (proc)->encode_coding_system);
  4422. }
  4423.  
  4424. /* The first time this is called, assume keyboard input comes from DESC
  4425.    instead of from where we used to expect it.
  4426.    Subsequent calls mean assume input keyboard can come from DESC
  4427.    in addition to other places.  */
  4428.  
  4429. static int add_keyboard_wait_descriptor_called_flag;
  4430.  
  4431. void
  4432. add_keyboard_wait_descriptor (desc)
  4433.      int desc;
  4434. {
  4435.   if (! add_keyboard_wait_descriptor_called_flag)
  4436.     FD_CLR (0, &input_wait_mask);
  4437.   add_keyboard_wait_descriptor_called_flag = 1;
  4438.   FD_SET (desc, &input_wait_mask);
  4439.   FD_SET (desc, &non_process_wait_mask);
  4440.   if (desc > max_keyboard_desc)
  4441.     max_keyboard_desc = desc;
  4442. }
  4443.  
  4444. /* From now on, do not expect DESC to give keyboard input.  */
  4445.  
  4446. void
  4447. delete_keyboard_wait_descriptor (desc)
  4448.      int desc;
  4449. {
  4450.   int fd;
  4451.   int lim = max_keyboard_desc;
  4452.  
  4453.   FD_CLR (desc, &input_wait_mask);
  4454.   FD_CLR (desc, &non_process_wait_mask);
  4455.  
  4456.   if (desc == max_keyboard_desc)
  4457.     for (fd = 0; fd < lim; fd++)
  4458.       if (FD_ISSET (fd, &input_wait_mask)
  4459.       && !FD_ISSET (fd, &non_keyboard_wait_mask))
  4460.     max_keyboard_desc = fd;
  4461. }
  4462.  
  4463. /* Return nonzero if *MASK has a bit set
  4464.    that corresponds to one of the keyboard input descriptors.  */
  4465.  
  4466. int
  4467. keyboard_bit_set (mask)
  4468.      SELECT_TYPE *mask;
  4469. {
  4470.   int fd;
  4471.  
  4472.   for (fd = 0; fd <= max_keyboard_desc; fd++)
  4473.     if (FD_ISSET (fd, mask) && FD_ISSET (fd, &input_wait_mask)
  4474.     && !FD_ISSET (fd, &non_keyboard_wait_mask))
  4475.       return 1;
  4476.  
  4477.   return 0;
  4478. }
  4479.  
  4480. void
  4481. init_process ()
  4482. {
  4483.   register int i;
  4484.  
  4485. #ifdef SIGCHLD
  4486. #ifndef CANNOT_DUMP
  4487.   if (! noninteractive || initialized)
  4488. #endif
  4489.     signal (SIGCHLD, sigchld_handler);
  4490. #endif
  4491.  
  4492.   FD_ZERO (&input_wait_mask);
  4493.   FD_ZERO (&non_keyboard_wait_mask);
  4494.   FD_ZERO (&non_process_wait_mask);
  4495.   max_process_desc = 0;
  4496.  
  4497.   FD_SET (0, &input_wait_mask);
  4498.  
  4499.   Vprocess_alist = Qnil;
  4500.   for (i = 0; i < MAXDESC; i++)
  4501.     {
  4502.       chan_process[i] = Qnil;
  4503.       proc_buffered_char[i] = -1;
  4504.     }
  4505.   bzero (proc_decode_coding_system, sizeof proc_decode_coding_system);
  4506.   bzero (proc_encode_coding_system, sizeof proc_encode_coding_system);
  4507.  
  4508.   Vdefault_process_coding_system
  4509.     = (NILP (buffer_defaults.enable_multibyte_characters)
  4510.        ? Fcons (Qraw_text, Qnil)
  4511.        : Fcons (Qemacs_mule, Qnil));
  4512. }
  4513.  
  4514. void
  4515. syms_of_process ()
  4516. {
  4517. #ifdef EMX
  4518.   Qstart_process = intern ("start-process");
  4519.   staticpro (&Qstart_process);
  4520. #endif /* EMX */
  4521.   Qprocessp = intern ("processp");
  4522.   staticpro (&Qprocessp);
  4523.   Qrun = intern ("run");
  4524.   staticpro (&Qrun);
  4525.   Qstop = intern ("stop");
  4526.   staticpro (&Qstop);
  4527.   Qsignal = intern ("signal");
  4528.   staticpro (&Qsignal);
  4529.  
  4530.   /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
  4531.      here again.
  4532.  
  4533.      Qexit = intern ("exit");
  4534.      staticpro (&Qexit); */
  4535.  
  4536.   Qopen = intern ("open");
  4537.   staticpro (&Qopen);
  4538.   Qclosed = intern ("closed");
  4539.   staticpro (&Qclosed);
  4540.  
  4541.   Qlast_nonmenu_event = intern ("last-nonmenu-event");
  4542.   staticpro (&Qlast_nonmenu_event);
  4543.  
  4544.   staticpro (&Vprocess_alist);
  4545.  
  4546.   DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
  4547.     "*Non-nil means delete processes immediately when they exit.\n\
  4548. nil means don't delete them until `list-processes' is run.");
  4549.  
  4550.   delete_exited_processes = 1;
  4551.  
  4552.   DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type,
  4553.     "Control type of device used to communicate with subprocesses.\n\
  4554. Values are nil to use a pipe, or t or `pty' to use a pty.\n\
  4555. The value has no effect if the system has no ptys or if all ptys are busy:\n\
  4556. then a pipe is used in any case.\n\
  4557. The value takes effect when `start-process' is called.");
  4558.   Vprocess_connection_type = Qt;
  4559.  
  4560.   defsubr (&Sprocessp);
  4561.   defsubr (&Sget_process);
  4562.   defsubr (&Sget_buffer_process);
  4563.   defsubr (&Sdelete_process);
  4564.   defsubr (&Sprocess_status);
  4565.   defsubr (&Sprocess_exit_status);
  4566.   defsubr (&Sprocess_id);
  4567.   defsubr (&Sprocess_name);
  4568.   defsubr (&Sprocess_tty_name);
  4569.   defsubr (&Sprocess_command);
  4570.   defsubr (&Sset_process_buffer);
  4571.   defsubr (&Sprocess_buffer);
  4572.   defsubr (&Sprocess_mark);
  4573.   defsubr (&Sset_process_filter);
  4574.   defsubr (&Sprocess_filter);
  4575.   defsubr (&Sset_process_sentinel);
  4576.   defsubr (&Sprocess_sentinel);
  4577.   defsubr (&Sset_process_window_size);
  4578.   defsubr (&Sset_process_inherit_coding_system_flag);
  4579.   defsubr (&Sprocess_inherit_coding_system_flag);
  4580.   defsubr (&Sprocess_kill_without_query);
  4581.   defsubr (&Sprocess_contact);
  4582.   defsubr (&Slist_processes);
  4583.   defsubr (&Sprocess_list);
  4584.   defsubr (&Sstart_process);
  4585. #ifdef HAVE_SOCKETS
  4586.   defsubr (&Sopen_network_stream);
  4587. #endif /* HAVE_SOCKETS */
  4588.   defsubr (&Saccept_process_output);
  4589.   defsubr (&Sprocess_send_region);
  4590.   defsubr (&Sprocess_send_string);
  4591.   defsubr (&Sinterrupt_process);
  4592.   defsubr (&Skill_process);
  4593.   defsubr (&Squit_process);
  4594.   defsubr (&Sstop_process);
  4595.   defsubr (&Scontinue_process);
  4596.   defsubr (&Sprocess_send_eof);
  4597.   defsubr (&Ssignal_process);
  4598.   defsubr (&Swaiting_for_user_input_p);
  4599. /*  defsubr (&Sprocess_connection); */
  4600.   defsubr (&Sset_process_coding_system);
  4601.   defsubr (&Sprocess_coding_system);
  4602. }
  4603.  
  4604.  
  4605. #else /* not subprocesses */
  4606.  
  4607. #include <sys/types.h>
  4608. #include <errno.h>
  4609.  
  4610. #include "lisp.h"
  4611. #include "systime.h"
  4612. #include "charset.h"
  4613. #include "coding.h"
  4614. #include "termopts.h"
  4615. #include "sysselect.h"
  4616.  
  4617. extern int frame_garbaged;
  4618.  
  4619. extern EMACS_TIME timer_check ();
  4620. extern int timers_run;
  4621.  
  4622. /* As described above, except assuming that there are no subprocesses:
  4623.  
  4624.    Wait for timeout to elapse and/or keyboard input to be available.
  4625.  
  4626.    time_limit is:
  4627.      timeout in seconds, or
  4628.      zero for no limit, or
  4629.      -1 means gobble data immediately available but don't wait for any.
  4630.  
  4631.    read_kbd is a Lisp_Object:
  4632.      0 to ignore keyboard input, or
  4633.      1 to return when input is available, or
  4634.      -1 means caller will actually read the input, so don't throw to
  4635.        the quit handler.
  4636.      a cons cell, meaning wait until its car is non-nil
  4637.        (and gobble terminal input into the buffer if any arrives), or
  4638.      We know that read_kbd will never be a Lisp_Process, since
  4639.      `subprocesses' isn't defined.
  4640.  
  4641.    do_display != 0 means redisplay should be done to show subprocess
  4642.    output that arrives.
  4643.  
  4644.    Return true iff we received input from any process.  */
  4645.  
  4646. int
  4647. wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
  4648.      int time_limit, microsecs;
  4649.      Lisp_Object read_kbd;
  4650.      int do_display;
  4651. {
  4652.   register int nfds;
  4653.   EMACS_TIME end_time, timeout;
  4654.   SELECT_TYPE waitchannels;
  4655.   int xerrno;
  4656.   Lisp_Object *wait_for_cell = 0;
  4657.  
  4658.   /* If waiting for non-nil in a cell, record where.  */
  4659.   if (CONSP (read_kbd))
  4660.     {
  4661.       wait_for_cell = &XCONS (read_kbd)->car;
  4662.       XSETFASTINT (read_kbd, 0);
  4663.     }
  4664.  
  4665.   /* What does time_limit really mean?  */
  4666.   if (time_limit || microsecs)
  4667.     {
  4668.       EMACS_GET_TIME (end_time);
  4669.       EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
  4670.       EMACS_ADD_TIME (end_time, end_time, timeout);
  4671.     }
  4672.  
  4673.   /* Turn off periodic alarms (in case they are in use)
  4674.      because the select emulator uses alarms.  */
  4675.   stop_polling ();
  4676.  
  4677.   while (1)
  4678.     {
  4679.       int timeout_reduced_for_timers = 0;
  4680.  
  4681.       /* If calling from keyboard input, do not quit
  4682.      since we want to return C-g as an input character.
  4683.      Otherwise, do pending quit if requested.  */
  4684.       if (XINT (read_kbd) >= 0)
  4685.     QUIT;
  4686.  
  4687.       /* Exit now if the cell we're waiting for became non-nil.  */
  4688.       if (wait_for_cell && ! NILP (*wait_for_cell))
  4689.     break;
  4690.  
  4691.       /* Compute time from now till when time limit is up */
  4692.       /* Exit if already run out */
  4693.       if (time_limit == -1)
  4694.     {
  4695.       /* -1 specified for timeout means
  4696.          gobble output available now
  4697.          but don't wait at all. */
  4698.  
  4699.       EMACS_SET_SECS_USECS (timeout, 0, 0);
  4700.     }
  4701.       else if (time_limit || microsecs)
  4702.     {
  4703.       EMACS_GET_TIME (timeout);
  4704.       EMACS_SUB_TIME (timeout, end_time, timeout);
  4705.       if (EMACS_TIME_NEG_P (timeout))
  4706.         break;
  4707.     }
  4708.       else
  4709.     {
  4710.       EMACS_SET_SECS_USECS (timeout, 100000, 0);
  4711.     }
  4712.  
  4713.       /* If our caller will not immediately handle keyboard events,
  4714.      run timer events directly.
  4715.      (Callers that will immediately read keyboard events
  4716.      call timer_delay on their own.)  */
  4717.       if (! wait_for_cell)
  4718.     {
  4719.       EMACS_TIME timer_delay;
  4720.       int old_timers_run;
  4721.  
  4722.     retry:
  4723.       old_timers_run = timers_run;
  4724.       timer_delay = timer_check (1);
  4725.       if (timers_run != old_timers_run && do_display)
  4726.         {
  4727.           redisplay_preserve_echo_area ();
  4728.           /* We must retry, since a timer may have requeued itself
  4729.          and that could alter the time delay.  */
  4730.           goto retry;
  4731.         }
  4732.  
  4733.       /* If there is unread keyboard input, also return.  */
  4734.       if (XINT (read_kbd) != 0
  4735.           && requeued_events_pending_p ())
  4736.         break;
  4737.  
  4738.       if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1)
  4739.         {
  4740.           EMACS_TIME difference;
  4741.           EMACS_SUB_TIME (difference, timer_delay, timeout);
  4742.           if (EMACS_TIME_NEG_P (difference))
  4743.         {
  4744.           timeout = timer_delay;
  4745.           timeout_reduced_for_timers = 1;
  4746.         }
  4747.         }
  4748.     }
  4749.  
  4750.       /* Cause C-g and alarm signals to take immediate action,
  4751.      and cause input available signals to zero out timeout.  */
  4752.       if (XINT (read_kbd) < 0)
  4753.     set_waiting_for_input (&timeout);
  4754.  
  4755.       /* Wait till there is something to do.  */
  4756.  
  4757.       if (! XINT (read_kbd) && wait_for_cell == 0)
  4758.     FD_ZERO (&waitchannels);
  4759.       else
  4760.     FD_SET (0, &waitchannels);
  4761.  
  4762.       /* If a frame has been newly mapped and needs updating,
  4763.      reprocess its display stuff.  */
  4764.       if (frame_garbaged && do_display)
  4765.     {
  4766.       clear_waiting_for_input ();
  4767.       redisplay_preserve_echo_area ();
  4768.       if (XINT (read_kbd) < 0)
  4769.         set_waiting_for_input (&timeout);
  4770.     }
  4771.  
  4772.       if (XINT (read_kbd) && detect_input_pending ())
  4773.     {
  4774.       nfds = 0;
  4775.       FD_ZERO (&waitchannels);
  4776.     }
  4777.       else
  4778.     nfds = select (1, &waitchannels, (SELECT_TYPE *)0, (SELECT_TYPE *)0,
  4779.                &timeout);
  4780.  
  4781.       xerrno = errno;
  4782.  
  4783.       /* Make C-g and alarm signals set flags again */
  4784.       clear_waiting_for_input ();
  4785.  
  4786.       /*  If we woke up due to SIGWINCH, actually change size now.  */
  4787.       do_pending_window_change ();
  4788.  
  4789.       if (time_limit && nfds == 0 && ! timeout_reduced_for_timers)
  4790.     /* We waited the full specified time, so return now.  */
  4791.     break;
  4792.  
  4793.       if (nfds == -1)
  4794.     {
  4795.       /* If the system call was interrupted, then go around the
  4796.          loop again.  */
  4797.       if (xerrno == EINTR)
  4798.         FD_ZERO (&waitchannels);
  4799.       else
  4800.         error ("select error: %s", strerror (xerrno));
  4801.     }
  4802. #ifdef sun
  4803.       else if (nfds > 0 && (waitchannels & 1)  && interrupt_input)
  4804.     /* System sometimes fails to deliver SIGIO.  */
  4805.     kill (getpid (), SIGIO);
  4806. #endif
  4807. #ifdef SIGIO
  4808.       if (XINT (read_kbd) && interrupt_input && (waitchannels & 1))
  4809.     kill (getpid (), SIGIO);
  4810. #endif
  4811.  
  4812.       /* Check for keyboard input */
  4813.  
  4814.       if ((XINT (read_kbd) != 0)
  4815.       && detect_input_pending_run_timers (do_display))
  4816.     {
  4817.       swallow_events (do_display);
  4818.       if (detect_input_pending_run_timers (do_display))
  4819.         break;
  4820.     }
  4821.  
  4822.       /* If there is unread keyboard input, also return.  */
  4823.       if (XINT (read_kbd) != 0
  4824.       && requeued_events_pending_p ())
  4825.     break;
  4826.  
  4827.       /* If wait_for_cell. check for keyboard input
  4828.      but don't run any timers.
  4829.      ??? (It seems wrong to me to check for keyboard
  4830.      input at all when wait_for_cell, but the code
  4831.      has been this way since July 1994.
  4832.      Try changing this after version 19.31.)  */
  4833.       if (wait_for_cell
  4834.       && detect_input_pending ())
  4835.     {
  4836.       swallow_events (do_display);
  4837.       if (detect_input_pending ())
  4838.         break;
  4839.     }
  4840.  
  4841.       /* Exit now if the cell we're waiting for became non-nil.  */
  4842.       if (wait_for_cell && ! NILP (*wait_for_cell))
  4843.     break;
  4844.     }
  4845.  
  4846.   start_polling ();
  4847.  
  4848.   return 0;
  4849. }
  4850.  
  4851.  
  4852. DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
  4853.   /* Don't confuse make-docfile by having two doc strings for this function.
  4854.      make-docfile does not pay attention to #if, for good reason!  */
  4855.   0)
  4856.   (name)
  4857.      register Lisp_Object name;
  4858. {
  4859.   return Qnil;
  4860. }
  4861.  
  4862. DEFUN ("process-inherit-coding-system-flag",
  4863.   Fprocess_inherit_coding_system_flag, Sprocess_inherit_coding_system_flag,
  4864.   1, 1, 0,
  4865.   /* Don't confuse make-docfile by having two doc strings for this function.
  4866.      make-docfile does not pay attention to #if, for good reason!  */
  4867.   0)
  4868.   (process)
  4869.      register Lisp_Object process;
  4870. {
  4871.   /* Ignore the argument and return the value of
  4872.      inherit-process-coding-system.  */
  4873.   return inherit_process_coding_system ? Qt : Qnil;
  4874. }
  4875.  
  4876. /* Kill all processes associated with `buffer'.
  4877.    If `buffer' is nil, kill all processes.
  4878.    Since we have no subprocesses, this does nothing.  */
  4879.  
  4880. void
  4881. kill_buffer_processes (buffer)
  4882.      Lisp_Object buffer;
  4883. {
  4884. }
  4885.  
  4886. void
  4887. init_process ()
  4888. {
  4889. }
  4890.  
  4891. void
  4892. syms_of_process ()
  4893. {
  4894.   defsubr (&Sget_buffer_process);
  4895.   defsubr (&Sprocess_inherit_coding_system_flag);
  4896. }
  4897.  
  4898.  
  4899. #endif /* not subprocesses */
  4900.