home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / process.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-15  |  87.1 KB  |  3,103 lines

  1. /* Asynchronous subprocess control for XEmacs.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994
  3.    Free Software Foundation, Inc.
  4.    Copyright (C) 1995 Amdahl Corporation.
  5.    Copyright (C) 1995 Sun Microsystems.
  6.  
  7. This file is part of XEmacs.
  8.  
  9. XEmacs is free software; you can redistribute it and/or modify it
  10. under the terms of the GNU General Public License as published by the
  11. Free Software Foundation; either version 2, or (at your option) any
  12. later version.
  13.  
  14. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  15. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17. for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with XEmacs; see the file COPYING.  If not, write to the Free
  21. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  22.  
  23. /* Synched up with: Mule 2.0, FSF 19.28. */
  24.  
  25. #include <config.h>
  26.  
  27. #if !defined (NO_SUBPROCESSES)
  28.  
  29. #include "lisp.h"
  30.  
  31. /* The entire file is within this conditional */
  32.  
  33. #include "buffer.h"
  34. #include "commands.h"
  35. #include "events.h"
  36. #include "frame.h"
  37. #include "insdel.h"
  38. #include "lstream.h"
  39. #include "mule.h"
  40. #include "opaque.h"
  41. #include "process.h"
  42. #include "sysdep.h"
  43. #include "window.h"
  44.  
  45. #include <setjmp.h>
  46. #include "sysfile.h"
  47. #include "sysproc.h"
  48. #include "systime.h"
  49. #include "syssignal.h" /* Always include before systty.h */
  50. #include "systty.h"
  51. #include "syswait.h"
  52.  
  53. /* a process object is a network connection when its pid field a cons
  54.    (name of name of port we are connected to . foreign host name) */
  55.  
  56. /* Valid values of process->status_symbol */
  57. Lisp_Object Qrun, Qstop; /* Qexit from eval.c, Qsignal from data.c. */
  58. /* Qrun => Qopen, Qexit => Qclosed for "network connection" processes */
  59. Lisp_Object Qopen, Qclosed;
  60.  
  61. /* t means use pty, nil means use a pipe,
  62.    maybe other values to come.  */
  63. static Lisp_Object Vprocess_connection_type;
  64.  
  65. /* Number of events of change of status of a process.  */
  66. static volatile int process_tick;
  67.  
  68. /* Number of events for which the user or sentinel has been notified.  */
  69. static int update_tick;
  70.  
  71. /* Nonzero means delete a process right away if it exits.  */
  72. int delete_exited_processes;
  73.  
  74. /* Indexed by descriptor, gives the process (if any) for that descriptor */
  75. Lisp_Object descriptor_to_process[MAXDESC];
  76.  
  77. /* List of process objects. */
  78. Lisp_Object Vprocess_list;
  79.  
  80. Lisp_Object Qprocessp;
  81.  
  82.  
  83. /************************************************************************/
  84. /*                        the process Lisp object                       */
  85. /************************************************************************/
  86.  
  87. /*
  88.  * Structure records pertinent information about open channels.
  89.  * There is one channel associated with each process.
  90.  */
  91.  
  92. struct Lisp_Process
  93.   {
  94.     struct lcrecord_header header;
  95.     /* Name of this process */
  96.     Lisp_Object name;
  97.     /* List of command arguments that this process was run with */
  98.     Lisp_Object command;
  99.     /* (funcall FILTER PROC STRING)  (if FILTER is non-nil)
  100.        to dispose of a bunch of chars from the process all at once */
  101.     Lisp_Object filter;
  102.     /* (funcall SENTINEL PROCESS) when process state changes */
  103.     Lisp_Object sentinel;
  104.     /* Buffer that output is going to */
  105.     Lisp_Object buffer;
  106.     /* Marker set to end of last buffer-inserted output from this process */
  107.     Lisp_Object mark;
  108.     /* Lisp_Int of subprocess' PID, or a cons of
  109.        service/host if this is really a network connection */
  110.     Lisp_Object pid;
  111.  
  112.     /* Symbol indicating status of process.
  113.        This may be a symbol: run, stop, exit, signal */
  114.     Lisp_Object status_symbol;
  115.  
  116.     /* Non-0 if this is really a TooTalk channel. */
  117.     int connected_via_filedesc_p;
  118.  
  119.     /* Exit code if process has terminated,
  120.        signal which stopped/interrupted process
  121.        or 0 if process is running */
  122.     int exit_code;
  123.     /* Number of this process */
  124.     /* Non-false if process has exited and "dumped core" on its way down */
  125.     char core_dumped;
  126.     /* Descriptor by which we read from this process.  -1 for dead process */
  127.     int infd;
  128.     /* Descriptor by which we write to this process. -1 for dead process */
  129.     int outfd;
  130.     /* Descriptor for the tty which this process is using.
  131.        -1 if we didn't record it (on some systems, there's no need).  */
  132.     int subtty;
  133.     /* Name of subprocess terminal. */
  134.     Lisp_Object tty_name;
  135.     /* Non-false if communicating through a pty.  */
  136.     char pty_flag;
  137.     /* This next field is only actually used #ifdef ENERGIZE */
  138.     /* if this flag is not NIL, then filter will do the read on the
  139.        channel, rather than having a call to make_string.
  140.        This only works if the filter is a subr. */
  141.     char filter_does_read;
  142.     /* Non-nil means kill silently if Emacs is exited.  */
  143.     char kill_without_query;
  144.     /* Event-count of last event in which this process changed status.  */
  145.     volatile int tick;
  146.     /* Event-count of last such event reported.  */
  147.     int update_tick;
  148.     /* streams used in input and output */
  149.     Lisp_Object instream;
  150.     Lisp_Object outstream;
  151.   };
  152.  
  153. static Lisp_Object mark_process (Lisp_Object, void (*) (Lisp_Object));
  154. static void print_process (Lisp_Object, Lisp_Object, int);
  155. static void finalize_process (void *, int);
  156. DEFINE_LRECORD_IMPLEMENTATION ("process", process,
  157.                                mark_process, print_process, finalize_process,
  158.                                0, 0, struct Lisp_Process);
  159.  
  160. static Lisp_Object
  161. mark_process (Lisp_Object obj, void (*markobj) (Lisp_Object))
  162. {
  163.   struct Lisp_Process *proc = XPROCESS (obj);
  164.   ((markobj) (proc->name));
  165.   ((markobj) (proc->command));
  166.   ((markobj) (proc->filter));
  167.   ((markobj) (proc->sentinel));
  168.   ((markobj) (proc->buffer));
  169.   ((markobj) (proc->mark));
  170.   ((markobj) (proc->pid));
  171.   ((markobj) (proc->tty_name));
  172.   ((markobj) (proc->instream));
  173.   ((markobj) (proc->outstream));
  174.   return (proc->status_symbol);
  175. }
  176.  
  177. static void
  178. print_process (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
  179. {
  180.   struct Lisp_Process *proc = XPROCESS (obj);
  181.   
  182.   if (print_readably)
  183.     error ("printing unreadable object #<process %s>",
  184.            string_data (XSTRING (proc->name)));
  185.       
  186.   if (!escapeflag)
  187.     {
  188.       print_internal (proc->name, printcharfun, 0);
  189.     }
  190.   else
  191.     {
  192.       int netp = network_connection_p (obj);
  193.       write_c_string (((netp) ? GETTEXT ("#<network connection ") :
  194.                GETTEXT ("#<process ")), printcharfun);
  195.       print_internal (proc->name, printcharfun, 1);
  196.       write_c_string (((netp) ? " " : " pid "), printcharfun);
  197.       print_internal (proc->pid, printcharfun, 1);
  198.       write_c_string (" state:", printcharfun);
  199.       print_internal (proc->status_symbol, printcharfun, 1);
  200.       write_c_string (">", printcharfun);
  201.     }
  202. }
  203.  
  204. #ifdef HAVE_WINDOW_SYSTEM
  205. extern void debug_process_finalization (struct Lisp_Process *p);
  206. #endif /* HAVE_WINDOW_SYSTEM */
  207.  
  208. static void
  209. finalize_process (void *header, int for_disksave)
  210. {
  211.   if (for_disksave) return; /* hmm, what would this do anyway? */
  212.   /* #### this probably needs to be tied into the tty event loop */
  213.   /* #### when there is one */
  214. #ifdef HAVE_WINDOW_SYSTEM
  215.   {
  216.     struct Lisp_Process *p = (struct Lisp_Process *) header;
  217.     debug_process_finalization (p);
  218.   }
  219. #endif /* HAVE_WINDOW_SYSTEM */
  220. }
  221.  
  222.  
  223. /************************************************************************/
  224. /*                       basic process accessors                        */
  225. /************************************************************************/
  226.  
  227. static SIGTYPE
  228. close_safely_handler (int signo)
  229. {
  230.   EMACS_REESTABLISH_SIGNAL (signo, close_safely_handler);
  231.   SIGRETURN;
  232. }
  233.  
  234. static void
  235. close_safely (int fd)
  236. {
  237.   stop_interrupts ();
  238.   signal (SIGALRM, close_safely_handler);
  239.   alarm (1);
  240.   close (fd);
  241.   alarm (0);
  242.   start_interrupts ();
  243. }
  244.  
  245. static void
  246. close_descriptor_pair (int in, int out)
  247. {
  248.   if (in >= 0)
  249.     close (in);
  250.   if (out != in && out >= 0)
  251.     close (out);
  252. }
  253.  
  254. /* Close all descriptors currently in use for communication
  255.    with subprocess.  This is used in a newly-forked subprocess
  256.    to get rid of irrelevant descriptors.  */
  257.  
  258. void
  259. close_process_descs (void)
  260. {
  261.   int i;
  262.   for (i = 0; i < MAXDESC; i++)
  263.     {
  264.       Lisp_Object process;
  265.       process = descriptor_to_process[i];
  266.       if (!NILP (process))
  267.     {
  268.       close_descriptor_pair (XPROCESS (process)->infd,
  269.                  XPROCESS (process)->outfd);
  270.     }
  271.     }
  272. }
  273.  
  274. void
  275. get_process_file_descriptors (struct Lisp_Process *p, int *infd, int *outfd)
  276. {
  277.   if (! p) abort ();
  278.   /* the cast of MAXDESC is needed for some versions of Linux */
  279.   assert (p->infd  >= -1 && p->infd  < ((int) (MAXDESC)));
  280.   assert (p->outfd >= -1 && p->outfd < ((int) (MAXDESC)));
  281.   *infd = p->infd;
  282.   *outfd = p->outfd;
  283. }
  284.  
  285. struct Lisp_Process *
  286. get_process_from_input_descriptor (int infd)
  287. {
  288.   Lisp_Object proc;
  289.  
  290.   if ((infd  < 0) || (infd  >= ((int) (MAXDESC)))) abort ();
  291.   proc = descriptor_to_process[infd];
  292.   if (NILP (proc))
  293.     return 0;
  294.   else
  295.     return XPROCESS (proc);
  296. }
  297.  
  298. #ifdef HAVE_SOCKETS
  299. int
  300. network_connection_p (Lisp_Object process)
  301. {
  302.   return (XGCTYPE (XPROCESS (process)->pid) == Lisp_Cons);
  303. }
  304. #endif
  305.  
  306. int
  307. connected_via_filedesc_p (struct Lisp_Process *p)
  308. {
  309.   return p->connected_via_filedesc_p;
  310. }
  311.  
  312. DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0,
  313.   "Return t if OBJECT is a process.")
  314.   (obj)
  315.      Lisp_Object obj;
  316. {
  317.   return ((PROCESSP (obj)) ? Qt : Qnil);
  318. }
  319.  
  320. DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
  321.   "Return a list of all processes.")
  322.   ()
  323. {
  324.   return Fcopy_sequence (Vprocess_list);
  325. }
  326.  
  327. DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0,
  328.   "Return the process named NAME, or nil if there is none.")
  329.   (name)
  330.      Lisp_Object name;
  331. {
  332.   Lisp_Object tail;
  333.  
  334.   if (PROCESSP (name))
  335.     return (name);
  336.  
  337.   if (!gc_in_progress)
  338.     /* this only gets called during GC when emacs is going away as a result
  339.        of a signal or crash. */
  340.     CHECK_STRING (name, 0);
  341.  
  342.   for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
  343.     {
  344.       Lisp_Object proc = XCAR (tail);
  345.       QUIT;
  346.       if (!NILP (Fequal (name, XPROCESS (proc)->name)))
  347.         return (XCAR (tail));
  348.     }
  349.   return Qnil;
  350. }
  351.  
  352. DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
  353.   "Return the (or, a) process associated with BUFFER.\n\
  354. BUFFER may be a buffer or the name of one.")
  355.   (name)
  356.      Lisp_Object name;
  357. {
  358.   Lisp_Object buf, tail, proc;
  359.  
  360.   if (NILP (name)) return Qnil;
  361.   buf = Fget_buffer (name);
  362.   if (NILP (buf)) return Qnil;
  363.  
  364. #ifdef ENERGIZE
  365.   {
  366.     Lisp_Object p = energize_get_buffer_process (buf);
  367.     if (!NILP (p)) return p;
  368.   }
  369. #endif
  370.  
  371.   for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
  372.     {
  373.       /* jwz: do not quit here - it isn't necessary, as there is no way for
  374.      Vprocess_list to get circular or overwhelmingly long, and this
  375.      function is called from layout_mode_element under redisplay. */
  376.       /* QUIT; */
  377.       proc = XCAR (tail);
  378.       if (PROCESSP (proc) && EQ (XPROCESS (proc)->buffer, buf))
  379.     return proc;
  380.     }
  381.   return Qnil;
  382. }
  383.  
  384. /* This is how commands for the user decode process arguments.  It
  385.    accepts a process, a process name, a buffer, a buffer name, or nil.
  386.    Buffers denote the first process in the buffer, and nil denotes the
  387.    current buffer.  */
  388.  
  389. static Lisp_Object
  390. get_process (Lisp_Object name)
  391. {
  392.   Lisp_Object proc;
  393.  
  394. #ifdef I18N3
  395.   /* #### Look more closely into translating process names. */
  396. #endif
  397.  
  398.   /* This may be called during a GC from process_send_signal() from
  399.      kill_buffer_processes() if emacs decides to abort(). */
  400.   if (gc_record_type_p (name, lrecord_process))
  401.     return name;
  402.  
  403.   if (NILP (name))
  404.     proc = Fget_buffer_process (Fcurrent_buffer ());
  405.   else
  406.     {
  407.       proc = Fget_process (name);
  408.       if (NILP (proc))
  409.     proc = Fget_buffer_process (Fget_buffer (name));
  410.     }
  411.  
  412.   if (!NILP (proc))
  413.     return proc;
  414.  
  415.   if (NILP (name))
  416.     error ("Current buffer has no process");
  417.   else
  418.     error ("Process %s does not exist", string_data (XSTRING (name)));
  419.   /* NOTREACHED */
  420.   return Qnil; /* warning suppression */
  421. }
  422.  
  423. DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0,
  424.   "Return the process id of PROCESS.\n\
  425. This is the pid of the Unix process which PROCESS uses or talks to.\n\
  426. For a network connection, this value is a cons of\n\
  427.  (foreign-network-port . foreign-host-name).")
  428.   (proc)
  429.      Lisp_Object proc;
  430. {
  431.   Lisp_Object pid;
  432.   CHECK_PROCESS (proc, 0);
  433.  
  434.   pid = XPROCESS (proc)->pid;
  435.   if (network_connection_p (proc))
  436.     /* return (Qnil); */
  437.     return (Fcons (Fcar (pid), Fcdr (pid)));
  438.   else
  439.     return (pid);
  440. }
  441.  
  442. DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0,
  443.   "Return the name of PROCESS, as a string.\n\
  444. This is the name of the program invoked in PROCESS,\n\
  445. possibly modified to make it unique among process names.")
  446.   (proc)
  447.      Lisp_Object proc;
  448. {
  449.   CHECK_PROCESS (proc, 0);
  450.   return XPROCESS (proc)->name;
  451. }
  452.  
  453. DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0,
  454.   "Return the command that was executed to start PROCESS.\n\
  455. This is a list of strings, the first string being the program executed\n\
  456. and the rest of the strings being the arguments given to it.")
  457.   (proc)
  458.      Lisp_Object proc;
  459. {
  460.   CHECK_PROCESS (proc, 0);
  461.   return XPROCESS (proc)->command;
  462. }
  463.  
  464.  
  465. /************************************************************************/
  466. /*                          creating a process                          */
  467. /************************************************************************/
  468.  
  469. static Lisp_Object
  470. make_process_internal (Lisp_Object name)
  471. {
  472.   Lisp_Object val, name1;
  473.   int i;
  474.   struct Lisp_Process *p
  475.     = alloc_lcrecord (sizeof (struct Lisp_Process), lrecord_process);
  476.  
  477.   /* If name is already in use, modify it until it is unused.  */
  478.   name1 = name;
  479.   for (i = 1; ; i++)
  480.     {
  481.       char suffix[10];
  482.       Lisp_Object tem = Fget_process (name1);
  483.       if (NILP (tem)) 
  484.         break;
  485.       sprintf (suffix, "<%d>", i);
  486.       name1 = concat2 (name, build_string (suffix));
  487.     }
  488.   name = name1;
  489.   p->name = name;
  490.  
  491.   p->command = Qnil;
  492.   p->filter = Qnil;
  493.   p->sentinel = Qnil;
  494.   p->buffer = Qnil;
  495.   p->mark = Fmake_marker ();
  496.   p->pid = Qnil;
  497.   p->status_symbol = Qrun;
  498.   p->connected_via_filedesc_p = 0;
  499.   p->exit_code = 0;
  500.   p->core_dumped = 0;
  501.   p->infd = -1;
  502.   p->outfd = -1;
  503.   p->subtty = 0;
  504.   p->tty_name = Qnil;
  505.   p->pty_flag = 0;
  506.   p->filter_does_read = 0;
  507.   p->kill_without_query = 0;
  508.   p->tick = 0;
  509.   p->update_tick = 0;
  510.   p->instream = Qnil;
  511.   p->outstream = Qnil;
  512.  
  513.   XSETPROCESS (val, p);
  514.  
  515.   Vprocess_list = Fcons (val, Vprocess_list);
  516.   return (val);
  517. }
  518.  
  519. #ifdef HAVE_PTYS
  520.  
  521. /* Open an available pty, returning a file descriptor.
  522.    Return -1 on failure.
  523.    The file name of the terminal corresponding to the pty
  524.    is left in the variable pty_name.  */
  525.  
  526. char pty_name[24];
  527.  
  528. static int
  529. allocate_pty (void)
  530. {
  531. #ifdef OSF1
  532.   /* use osf pty support for more pty channels */
  533.   int inch, outch;
  534.   openpty (&outch, &inch, pty_name, 0, 0);
  535.   close (inch);
  536.   setup_pty (outch);
  537.   return outch;
  538. #else /* OSF1 */
  539.   struct stat stb;
  540.   int c, i;
  541.   int fd;
  542.  
  543.   /* Some systems name their pseudoterminals so that there are gaps in
  544.      the usual sequence - for example, on HP9000/S700 systems, there
  545.      are no pseudoterminals with names ending in 'f'.  So we wait for
  546.      three failures in a row before deciding that we've reached the
  547.      end of the ptys.  */
  548.   int failed_count = 0;
  549.  
  550. #ifdef PTY_ITERATION
  551.   PTY_ITERATION
  552. #else
  553.   for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
  554.     for (i = 0; i < 16; i++)
  555. #endif
  556.       {
  557. #ifdef PTY_NAME_SPRINTF
  558.     PTY_NAME_SPRINTF
  559. #else
  560.     sprintf (pty_name, "/dev/pty%c%x", c, i);
  561. #endif /* no PTY_NAME_SPRINTF */
  562.  
  563. #ifdef PTY_OPEN
  564.     PTY_OPEN;
  565. #else /* no PTY_OPEN */
  566. #ifdef IRIS
  567.     /* Unusual IRIS code */
  568.      *ptyv = open ("/dev/ptc", O_RDWR | O_NDELAY, 0);
  569.      if (fd < 0)
  570.        return -1;
  571.     if (fstat (fd, &stb) < 0)
  572.       return -1;
  573. #else /* not IRIS */
  574.     if (stat (pty_name, &stb) < 0)
  575.       {
  576.         failed_count++;
  577.         if (failed_count >= 3)
  578.           return -1;
  579.       }
  580.     else
  581.       failed_count = 0;
  582. #ifdef O_NONBLOCK
  583.     fd = open (pty_name, O_RDWR | O_NONBLOCK, 0);
  584. #else
  585.     fd = open (pty_name, O_RDWR | O_NDELAY, 0);
  586. #endif
  587. #endif /* not IRIS */
  588. #endif /* no PTY_OPEN */
  589.  
  590.     if (fd >= 0)
  591.       {
  592.         /* check to make certain that both sides are available
  593.            this avoids a nasty yet stupid bug in rlogins */
  594. #ifdef PTY_TTY_NAME_SPRINTF
  595.         PTY_TTY_NAME_SPRINTF
  596. #else
  597.             sprintf (pty_name, "/dev/tty%c%x", c, i);
  598. #endif /* no PTY_TTY_NAME_SPRINTF */
  599. #ifndef UNIPLUS
  600.         if (access (pty_name, 6) != 0)
  601.           {
  602.         close (fd);
  603. #if !defined(IRIS) && !defined(__sgi)
  604.         continue;
  605. #else
  606.         return -1;
  607. #endif /* IRIS */
  608.           }
  609. #endif /* not UNIPLUS */
  610.         setup_pty (fd);
  611.         return fd;
  612.       }
  613.       }
  614.   return -1;
  615. #endif /* OSF1 */
  616. }
  617. #endif /* HAVE_PTYS */
  618.  
  619. static int
  620. create_bidirectional_pipe (int *inchannel, int *outchannel,
  621.                int *forkin, int *forkout)
  622. {
  623.   int sv[2];
  624.  
  625. #ifdef SKTPAIR
  626.   if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) < 0)
  627.     return -1;
  628.   *outchannel = inchannel = sv[0];
  629.   *forkout = forkin = sv[1];
  630. #else /* not SKTPAIR */
  631.   int temp;
  632.   temp = pipe (sv);
  633.   if (temp < 0) return -1;
  634.   *inchannel = sv[0];
  635.   *forkout = sv[1];
  636.   temp = pipe (sv);
  637.   if (temp < 0) return -1;
  638.   *outchannel = sv[1];
  639.   *forkin = sv[0];
  640. #endif /* not SKTPAIR */
  641.   return 0;
  642. }
  643.  
  644. #ifndef VMS /* VMS version of this function is in vmsproc.c.  */
  645.  
  646. static Bufbyte
  647. get_eof_char (struct Lisp_Process *p)
  648. {
  649.   /* Figure out the eof character for the outfd of the given process.
  650.    * The following code is similar to that in process_send_signal, and 
  651.    * should probably be merged with that code somehow. */
  652.  
  653. #ifdef HAVE_TERMIOS
  654.   struct termios t;
  655.   tcgetattr (p->outfd, &t);
  656.   if (strlen ((CONST char *) t.c_cc) < (unsigned int) (VEOF + 1))
  657.     return (Bufbyte) '\004';
  658.   else
  659.     return (Bufbyte) t.c_cc[VEOF];
  660. #else /* ! HAVE_TERMIOS */
  661.   /* On Berkeley descendants, the following IOCTL's retrieve the
  662.     current control characters.  */
  663. #if defined (TIOCGETC)
  664.   struct tchars c;
  665.   ioctl (p->outfd, TIOCGETC, &c);
  666.   return (Bufbyte) c.t_eofc;
  667. #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
  668.   /* On SYSV descendants, the TCGETA ioctl retrieves the current control
  669.      characters.  */
  670. #ifdef TCGETA
  671.   struct termio t;
  672.   ioctl (p->outfd, TCGETA, &t);
  673.   if (strlen ((CONST char *) t.c_cc) < (unsigned int) (VINTR + 1))
  674.     return (Bufbyte) '\004';
  675.   else
  676.     return (Bufbyte) t.c_cc[VINTR];
  677. #else /* ! defined (TCGETA) */
  678.   /* Rather than complain, we'll just guess ^D, which is what 
  679.    * earlier emacsen always used. */
  680.   return (Bufbyte) '\004';
  681. #endif /* ! defined (TCGETA) */
  682. #endif /* ! defined (TIOCGETC) */
  683. #endif /* ! defined (HAVE_TERMIOS) */
  684. }
  685.  
  686. static int
  687. get_pty_max_bytes (struct Lisp_Process *p)
  688. {
  689.   int pty_max_bytes;
  690.  
  691. #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
  692.   pty_max_bytes = fpathconf (p->outfd, _PC_MAX_CANON);
  693.   if (pty_max_bytes < 0)
  694.     pty_max_bytes = 250;
  695. #else
  696.   pty_max_bytes = 250;
  697. #endif
  698.   /* Deduct one, to leave space for the eof.  */
  699.   pty_max_bytes--;
  700.  
  701.   return pty_max_bytes;
  702. }
  703.  
  704. static void
  705. init_process_fds (struct Lisp_Process *p, int in, int out)
  706. {
  707.   p->infd = in;
  708.   p->outfd = out;
  709.   p->instream = make_filedesc_stream (in, 0);
  710.   p->outstream = make_filedesc_stream (out, 0);
  711.   if (p->pty_flag)
  712.     {
  713.       Bufbyte eof_char = get_eof_char (p);
  714.       int pty_max_bytes = get_pty_max_bytes (p);
  715.       filedesc_stream_set_pty_flushing (XLSTREAM (p->outstream),
  716.                     pty_max_bytes, eof_char);
  717.     }
  718. #ifdef MULE
  719.   p->instream = make_mule_encoding_stream (XLSTREAM (p->instream), Qnil);
  720.   p->outstream = make_mule_decoding_stream (XLSTREAM (p->instream), Qnil);
  721.   CODE_CNTL (&out_state[outchannel]) |= CC_END; /* !!#### */
  722. #endif /* MULE */
  723. }
  724.  
  725. static void
  726. create_process (Lisp_Object process, 
  727.                 char **new_argv, CONST char *current_dir)
  728. {
  729.   /* This function rewritten by wing@netcom.com. */
  730.  
  731.   int pid, inchannel, outchannel, forkin, forkout;
  732.   int pty_flag = 0;
  733.   char **env;
  734.   struct Lisp_Process *p = XPROCESS (process);
  735.  
  736.   env = environ;
  737.  
  738.   inchannel = outchannel = forkin = forkout = -1;
  739.  
  740. #ifdef HAVE_PTYS
  741.   if (!NILP (Vprocess_connection_type))
  742.     {
  743.       /* find a new pty, open the master side, return the opened
  744.      file handle, and store the name of the corresponding slave
  745.      side in global variable pty_name. */
  746.       outchannel = inchannel = allocate_pty ();
  747.     }
  748.  
  749.   if (inchannel >= 0)
  750.     {
  751.       /* You're "supposed" to now open the slave in the child.
  752.      On some systems, we can open it here; this allows for
  753.      better error checking. */
  754. #ifndef USG
  755.       /* On USG systems it does not work to open the pty's tty here
  756.            and then close and reopen it in the child.  */
  757. #ifdef O_NOCTTY
  758.       /* Don't let this terminal become our controlling terminal
  759.      (in case we don't have one).  */
  760.       forkout = forkin = open (pty_name, O_RDWR | O_NOCTTY, 0);
  761. #else
  762.       forkout = forkin = open (pty_name, O_RDWR, 0);
  763. #endif
  764.       if (forkin < 0)
  765.     goto io_failure;
  766. #endif /* not USG */
  767.       pty_flag = 1;
  768.     }
  769.   else
  770. #endif /* HAVE_PTYS */
  771.     if (create_bidirectional_pipe (&inchannel, &outchannel,
  772.                    &forkin, &forkout) < 0)
  773.       goto io_failure;
  774.  
  775. #if 0
  776.   /* Replaced by close_process_descs */
  777.   set_exclusive_use (inchannel);
  778.   set_exclusive_use (outchannel);
  779. #endif
  780.  
  781.   set_descriptor_non_blocking (inchannel);
  782.  
  783.   /* Record this as an active process, with its channels.
  784.      As a result, child_setup will close Emacs's side of the pipes.  */
  785.   descriptor_to_process[inchannel] = process;
  786.   init_process_fds (p, inchannel, outchannel);
  787.   /* Record the tty descriptor used in the subprocess.  */
  788.   p->subtty = forkin;
  789.   p->pty_flag = pty_flag;
  790.   p->status_symbol = Qrun;
  791.   p->exit_code = 0;
  792.  
  793.   {
  794.     /* child_setup must clobber environ on systems with true vfork.
  795.        Protect it from permanent change.  */
  796.     char **save_environ = environ;
  797.  
  798. #ifdef EMACS_BTL
  799.     /* when performance monitoring is on, turn it off before the vfork(),
  800.        as the child has no handler for the signal -- when back in the
  801.        parent process, turn it back on if it was really on when you "turned
  802.        it off" */
  803.     int logging_on = cadillac_stop_logging ();    /* #### rename me */
  804. #endif
  805.  
  806.     pid = vfork ();
  807.     if (pid == 0)
  808.       {
  809.     /**** Now we're in the child process ****/
  810.     int xforkin = forkin;
  811.     int xforkout = forkout;
  812.  
  813.     if (!pty_flag)
  814.       EMACS_SEPARATE_PROCESS_GROUP ();
  815. #ifdef HAVE_PTYS
  816.     else
  817.       {
  818.         /* ***** Step 1: *****
  819.  
  820.            Disconnect the current controlling terminal, pursuant to
  821.            making the pty be the controlling terminal of the process.
  822.            Also put us in our own process group. */
  823.  
  824.         disconnect_controlling_terminal ();
  825.  
  826.         /* ***** Step 2: *****
  827.  
  828.            Open the pty connection and make the pty's terminal
  829.            our controlling terminal.
  830.  
  831.            On systems with TIOCSCTTY, we just use it to set
  832.            the controlling terminal.  On other systems, the
  833.            first TTY we open becomes the controlling terminal.
  834.            So, we end up with four possibilities:
  835.  
  836.            (1) on USG and TIOCSCTTY systems, we open the pty
  837.                and use TIOCSCTTY.
  838.            (2) on other USG systems, we just open the pty.
  839.            (3) on non-USG systems with TIOCSCTTY, we
  840.                just use TIOCSCTTY. (On non-USG systems, we
  841.            already opened the pty in the parent process.)
  842.            (4) on non-USG systems without TIOCSCTTY, we
  843.                close the pty and reopen it.
  844.  
  845.            This would be cleaner if we didn't open the pty
  846.            in the parent process, but doing it that way
  847.            makes it possible to trap error conditions.
  848.            It's harder to convey an error from the child
  849.            process, and I don't feel like messing with
  850.            this now. */
  851.  
  852.         /* There was some weirdo, probably wrong,
  853.            conditionalization on RTU and UNIPLUS here.
  854.            I deleted it.  So sue me. */
  855.  
  856.         /* SunOS has TIOCSCTTY but the close/open method
  857.            also works. */
  858.  
  859. #  if defined (USG) || !defined (TIOCSCTTY)
  860.         /* Now close the pty (if we had it open) and reopen it.
  861.            This makes the pty the controlling terminal of the
  862.            subprocess.  */
  863.         /* I wonder if close (open (pty_name, ...)) would work?  */
  864.         if (xforkin >= 0)
  865.           close (xforkin);
  866.         xforkout = xforkin = open (pty_name, O_RDWR, 0);
  867.         if (xforkin < 0)
  868.           abort ();
  869. #  endif /* USG or not TIOCSCTTY */
  870.  
  871. #  ifdef TIOCSCTTY
  872.         /* We ignore the return value
  873.            because faith@cs.unc.edu says that is necessary on Linux.  */
  874.         ioctl (xforkin, TIOCSCTTY, 0);
  875. #  endif /* TIOCSCTTY */
  876.  
  877.         /* ***** Step 3: *****
  878.  
  879.            Change the line discipline. */
  880.  
  881. # if defined (HAVE_TERMIOS) && defined (LDISC1)
  882.         {
  883.           struct termios t;
  884.           tcgetattr (xforkin, &t);
  885.           t.c_lflag = LDISC1;
  886.           if (tcsetattr (xforkin, TCSANOW, &t) < 0)
  887.         stdout_out ("create_process/tcsetattr LDISC1 failed\n");
  888.         }
  889. # elif defined (NTTYDISC) && defined (TIOCSETD)
  890.         {
  891.           /* Use new line discipline.  */
  892.           int ldisc = NTTYDISC;
  893.           ioctl (xforkin, TIOCSETD, &ldisc);
  894.         }
  895. # endif /* TIOCSETD & NTTYDISC */
  896.  
  897.         /* ***** Step 4: *****
  898.  
  899.            Make our process group be the foreground group
  900.            of our new controlling terminal. */
  901.  
  902.         {
  903.           int pid = EMACS_GET_PROCESS_GROUP ();
  904.           EMACS_SET_TTY_PROCESS_GROUP (xforkin, &pid);
  905.         }
  906.  
  907.  
  908.         /* ***** Step 5: *****
  909.  
  910.            Miscellaneous setup. */
  911.  
  912. # ifdef SETUP_SLAVE_PTY
  913.         SETUP_SLAVE_PTY;
  914. # endif /* SETUP_SLAVE_PTY */
  915.  
  916. # ifdef AIX
  917.         /* On AIX, we've disabled SIGHUP above once we start a
  918.            child on a pty.  Now reenable it in the child, so it
  919.            will die when we want it to.  */
  920.         signal (SIGHUP, SIG_DFL);
  921. # endif /* AIX */
  922.       }
  923. #endif /* HAVE_PTYS */
  924.  
  925.     signal (SIGINT, SIG_DFL);
  926.     signal (SIGQUIT, SIG_DFL);
  927.  
  928. #ifndef MSDOS
  929.     if (pty_flag)
  930.       {
  931.         /* Set up the terminal characteristics of the pty. */
  932.         child_setup_tty (xforkout);
  933.       }
  934.  
  935.     child_setup (xforkin, xforkout, xforkout, new_argv, env, current_dir);
  936. #endif
  937.       }
  938. #ifdef EMACS_BTL
  939.     else if (logging_on)
  940.       cadillac_start_logging ();    /* #### rename me */
  941. #endif
  942.  
  943.     environ = save_environ;
  944.   }
  945.  
  946.   if (pid < 0)
  947.     {
  948.       close_descriptor_pair (forkin, forkout);
  949.       report_file_error ("Doing vfork", Qnil);
  950.     }
  951.  
  952.   p->pid = make_number (pid);
  953.   p->subtty = -1;
  954.  
  955.   /* If the subfork execv fails, and it exits,
  956.      this close hangs.  I don't know why.
  957.      So have an interrupt jar it loose.  */
  958.   if (forkin >= 0)
  959.     close_safely (forkin);
  960.   if (forkin != forkout && forkout >= 0)
  961.     close (forkout);
  962.  
  963.   XPROCESS (process)->tty_name = build_string (pty_name);
  964.  
  965.   /* Notice that SIGCHLD was not blocked. (This is not possible on
  966.      some systems.) No biggie if SIGCHLD occurs right around the
  967.      time that this call happens, because SIGCHLD() does not actually
  968.      deselect the process (that doesn't occur until the next time
  969.      we're waiting for an event, when status_notify() is called). */
  970.   event_stream_select_process (XPROCESS (process));
  971.  
  972.   return;
  973.  
  974. io_failure:
  975.   {
  976.     int temp = errno;
  977.     close_descriptor_pair (forkin, forkout);
  978.     close_descriptor_pair (inchannel, outchannel);
  979.     errno = temp;
  980.     report_file_error ("Opening pty or pipe", Qnil);
  981.   }
  982. }
  983. #endif /* not VMS */
  984.  
  985. /* This function is the unwind_protect form for Fstart_process_internal.  If
  986.    PROC doesn't have its pid set, then we know someone has signalled
  987.    an error and the process wasn't started successfully, so we should
  988.    remove it from the process list.  */
  989. static void remove_process (Lisp_Object proc);
  990. static Lisp_Object
  991. start_process_unwind (Lisp_Object proc)
  992. {
  993.   /* Was PROC started successfully?  */
  994.   if (EQ (XPROCESS (proc)->pid, Qnil))
  995.     remove_process (proc);
  996.   return Qnil;
  997. }
  998.  
  999. DEFUN ("start-process-internal", Fstart_process_internal,
  1000.        Sstart_process_internal, 3, MANY, 0,
  1001.   "Start a program in a subprocess.  Return the process object for it.\n\
  1002. Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
  1003. NAME is name for process.  It is modified if necessary to make it unique.\n\
  1004. BUFFER is the buffer or (buffer-name) to associate with the process.\n\
  1005.  Process output goes at end of that buffer, unless you specify\n\
  1006.  an output stream or filter function to handle the output.\n\
  1007.  BUFFER may be also nil, meaning that this process is not associated\n\
  1008.  with any buffer\n\
  1009. Third arg is program file name.  It is searched for as in the shell.\n\
  1010. Remaining arguments are strings to give program as arguments.\n\
  1011. INCODE and OUTCODE specify the coding-system objects used in input/output\n\
  1012.  from/to the process.")
  1013.   (nargs, args)
  1014.      int nargs;
  1015.      Lisp_Object *args;
  1016. {
  1017.   /* !!#### This function has not been Mule-ized */
  1018.   /* This function can GC */
  1019.   Lisp_Object buffer, name, program, proc, current_dir;
  1020.   Lisp_Object tem;
  1021.   int speccount = specpdl_depth ();
  1022. #ifdef VMS
  1023.   char *new_argv;
  1024.   int len;
  1025. #else
  1026.   char **new_argv;
  1027. #endif
  1028.   int i;
  1029.  
  1030.   buffer = args[1];
  1031.   if (!NILP (buffer))
  1032.     buffer = Fget_buffer_create (buffer);
  1033.  
  1034.   CHECK_STRING (args[0], 0);    /* name */
  1035.   CHECK_STRING (args[2], 2);    /* program */
  1036.  
  1037.   /* Make sure that the child will be able to chdir to the current
  1038.      buffer's current directory, or its unhandled equivalent.  We
  1039.      can't just have the child check for an error when it does the
  1040.      chdir, since it's in a vfork.
  1041.  
  1042.      We have to GCPRO around this because Fexpand_file_name and
  1043.      Funhandled_file_name_directory might call a file name handling
  1044.      function.  The argument list is protected by the caller, so all
  1045.      we really have to worry about is buffer.  */
  1046.   {
  1047.     struct gcpro gcpro1, gcpro2; /* Caller gc-protects args[] */
  1048.  
  1049.     current_dir = current_buffer->directory;
  1050.  
  1051.     GCPRO2 (buffer, current_dir);
  1052.  
  1053.     current_dir = 
  1054.       expand_and_dir_to_file
  1055.     (Funhandled_file_name_directory (current_dir), Qnil);
  1056. #if 0    /* This loser breaks ange-ftp */
  1057.     if (NILP (Ffile_accessible_directory_p (current_dir)))
  1058.       report_file_error ("Setting current directory",
  1059.              list1 (current_buffer->directory));
  1060. #endif /* 0 */
  1061.  
  1062.     UNGCPRO;
  1063.   }
  1064.  
  1065.   name = args[0];
  1066.   program = args[2];
  1067.  
  1068. #ifdef VMS
  1069.   /* Make a one member argv with all args concatenated
  1070.      together separated by a blank.  */
  1071.   len = string_length (XSTRING (program)) + 2;
  1072.   for (i = 3; i < nargs; i++)
  1073.     {
  1074.       tem = args[i];
  1075.       CHECK_STRING (tem, i);
  1076.       len += string_length (XSTRING (tem)) + 1;    /* count the blank */
  1077.     }
  1078.   new_argv = (char *) alloca (len);
  1079.   strcpy (new_argv, string_data (XSTRING (program)));
  1080.   for (i = 3; i < nargs; i++)
  1081.     {
  1082.       tem = args[i];
  1083.       CHECK_STRING (tem, i);
  1084.       strcat (new_argv, " ");
  1085.       strcat (new_argv, string_data (XSTRING (tem)));
  1086.     }
  1087.   /* Need to add code here to check for program existence on VMS */
  1088.  
  1089. #else /* not VMS */
  1090.   new_argv = (char **)
  1091.     alloca ((nargs - 1) * sizeof (char *));
  1092.  
  1093.   new_argv[0] = (char *) string_data (XSTRING (program));
  1094.  
  1095.   /* If program file name is not absolute, search our path for it */
  1096.   if (new_argv[0][0] != '/') /* #### !complete_filename_p */
  1097.     {
  1098.       struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; /* Caller protects args[] */
  1099.       GCPRO4 (buffer, current_dir, name, program);
  1100.  
  1101.       tem = Qnil;
  1102.       locate_file (Vexec_path, program, EXEC_SUFFIXES, &tem,
  1103.            X_OK);
  1104.       UNGCPRO;
  1105.       if (NILP (tem))
  1106.     report_file_error ("Searching for program", list1 (program));
  1107.       new_argv[0] = (char *) string_data (XSTRING (tem));
  1108.     }
  1109.  
  1110.   for (i = 3; i < nargs; i++)
  1111.     {
  1112.       tem = args[i];
  1113.       CHECK_STRING (tem, i);
  1114.       new_argv[i - 2] =
  1115.     (char *) string_data (XSTRING (tem));
  1116.     }
  1117.   new_argv[i - 2] = 0;
  1118.  
  1119. #endif /* not VMS */
  1120.  
  1121.   proc = make_process_internal (name);
  1122.  
  1123.   XPROCESS (proc)->buffer = buffer;
  1124.   XPROCESS (proc)->command = Flist (nargs - 2,
  1125.                     args + 2);
  1126.  
  1127.   /* If an error occurs and we can't start the process, we want to
  1128.      remove it from the process list.  This means that each error
  1129.      check in create_process doesn't need to call remove_process
  1130.      itself; it's all taken care of here.  */
  1131.   record_unwind_protect (start_process_unwind, proc);
  1132.  
  1133.   create_process (proc, new_argv,
  1134.           (char *) string_data (XSTRING (current_dir)));
  1135.  
  1136.   return unbind_to (speccount, proc);
  1137. }
  1138.  
  1139.  
  1140. /* connect to an existing file descriptor.  This is very similar to
  1141.    open-network-stream except that it assumes that the connection has
  1142.    already been initialized.  It is currently used for ToolTalk
  1143.    communication. */
  1144.  
  1145. /* This function used to be visible on the Lisp level, but there is no
  1146.    real point in doing that.  Here is the doc string:
  1147.  
  1148.   "Connect to an existing file descriptor.\n\
  1149. Returns a subprocess-object to represent the connection.\n\
  1150. Input and output work as for subprocesses; `delete-process' closes it.\n\
  1151. Args are NAME BUFFER INFD OUTFD.\n\
  1152. NAME is name for process.  It is modified if necessary to make it unique.\n\
  1153. BUFFER is the buffer (or buffer-name) to associate with the process.\n\
  1154.  Process output goes at end of that buffer, unless you specify\n\
  1155.  an output stream or filter function to handle the output.\n\
  1156.  BUFFER may be also nil, meaning that this process is not associated\n\
  1157.  with any buffer\n\
  1158. INFD and OUTFD specify the file descriptors to use for input and\n\
  1159.  output, respectively."
  1160. */
  1161.  
  1162. Lisp_Object
  1163. connect_to_file_descriptor (Lisp_Object name, Lisp_Object buffer,
  1164.                 Lisp_Object infd, Lisp_Object outfd)
  1165. {
  1166.   /* This function can GC */
  1167.   Lisp_Object proc;
  1168.   int inch;
  1169.  
  1170.   CHECK_STRING (name, 0);
  1171.   CHECK_INT (infd, 0);
  1172.   CHECK_INT (outfd, 0);
  1173.  
  1174.   inch = XINT (infd);
  1175.   if (!NILP (descriptor_to_process[inch]))
  1176.     error ("There is already a process connected to fd %d", inch);
  1177.   if (!NILP (buffer))
  1178.     buffer = Fget_buffer_create (buffer);
  1179.   proc = make_process_internal (name);
  1180.  
  1181.   descriptor_to_process[inch] = proc;
  1182.  
  1183.   XPROCESS (proc)->pid = Fcons (infd, name);
  1184.   XPROCESS (proc)->buffer = buffer;
  1185.   init_process_fds (XPROCESS (proc), inch, XINT (outfd));
  1186.   XPROCESS (proc)->connected_via_filedesc_p = 1;
  1187.  
  1188.   event_stream_select_process (XPROCESS (proc));
  1189.  
  1190.   return proc;
  1191. }
  1192.  
  1193.  
  1194. #ifdef HAVE_SOCKETS
  1195.  
  1196. /* open a TCP network connection to a given HOST/SERVICE.  Treated
  1197.    exactly like a normal process when reading and writing.  Only
  1198.    differences are in status display and process deletion.  A network
  1199.    connection has no PID; you cannot signal it.  All you can do is
  1200.    deactivate and close it via delete-process */
  1201.  
  1202. DEFUN ("open-network-stream-internal", Fopen_network_stream_internal,
  1203.        Sopen_network_stream_internal, 
  1204.        4, 4, 0, 
  1205.   "Open a TCP connection for a service to a host.\n\
  1206. Returns a subprocess-object to represent the connection.\n\
  1207. Input and output work as for subprocesses; `delete-process' closes it.\n\
  1208. Args are NAME BUFFER HOST SERVICE.\n\
  1209. NAME is name for process.  It is modified if necessary to make it unique.\n\
  1210. BUFFER is the buffer (or buffer-name) to associate with the process.\n\
  1211.  Process output goes at end of that buffer, unless you specify\n\
  1212.  an output stream or filter function to handle the output.\n\
  1213.  BUFFER may be also nil, meaning that this process is not associated\n\
  1214.  with any buffer\n\
  1215. Third arg is name of the host to connect to, or its IP address.\n\
  1216. Fourth arg SERVICE is name of the service desired, or an integer\n\
  1217.  specifying a port number to connect to.")
  1218.    (name, buffer, host, service)
  1219.       Lisp_Object name, buffer, host, service;
  1220. {
  1221.   /* !!#### This function has not been Mule-ized */
  1222.   /* This function can GC */
  1223.   Lisp_Object proc;
  1224.   struct sockaddr_in address;
  1225.   struct servent *svc_info;
  1226.   struct hostent *host_info_ptr, host_info;
  1227.   char *(addr_list[2]);
  1228.   IN_ADDR numeric_addr;
  1229.   int s, outch, inch;
  1230.   int port;
  1231.   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  1232.   int retry = 0;
  1233.   int retval;
  1234.  
  1235.   GCPRO4 (name, buffer, host, service);
  1236.   CHECK_STRING (name, 0);
  1237.   CHECK_STRING (host, 0);
  1238.   if (INTP (service))
  1239.     port = htons ((unsigned short) XINT (service));
  1240.   else
  1241.     {
  1242.       CHECK_STRING (service, 0);
  1243.       svc_info = getservbyname ((char *) string_data (XSTRING (service)),
  1244.                 "tcp");
  1245.       if (svc_info == 0)
  1246. #ifdef WIN32
  1247.     error ("Unknown service \"%s\" (%d)",
  1248.            string_data (XSTRING (service)), WSAGetLastError ());
  1249. #else
  1250.     error ("Unknown service \"%s\"", string_data (XSTRING (service)));
  1251. #endif
  1252.       port = svc_info->s_port;
  1253.     }
  1254.  
  1255. #ifndef TERM
  1256.   while (1)
  1257.     {
  1258. #ifdef TRY_AGAIN
  1259.       h_errno = 0;
  1260. #endif
  1261.       /* Some systems can't handle SIGIO/SIGALARM in gethostbyname. */
  1262.       slow_down_interrupts ();
  1263.       host_info_ptr = gethostbyname ((char *) string_data (XSTRING (host)));
  1264.       speed_up_interrupts ();
  1265. #ifdef TRY_AGAIN
  1266.       if (! (host_info_ptr == 0 && h_errno == TRY_AGAIN))
  1267. #endif
  1268.     break;
  1269.       Fsleep_for (make_number (1));
  1270.     }
  1271.   if (host_info_ptr == 0)
  1272.     /* Attempt to interpret host as numeric inet address */
  1273.     {
  1274.       numeric_addr = inet_addr ((char *) string_data (XSTRING (host)));
  1275.       if (NUMERIC_ADDR_ERROR)
  1276.     error ("Unknown host \"%s\"", string_data (XSTRING (host)));
  1277.  
  1278.       host_info_ptr = &host_info;
  1279.       host_info.h_name = 0;
  1280.       host_info.h_aliases = 0;
  1281.       host_info.h_addrtype = AF_INET;
  1282. #ifdef h_addr
  1283.       /* Older machines have only one address slot called h_addr.
  1284.      Newer machines have h_addr_list, but #define h_addr to
  1285.      be its first element.  */
  1286.       host_info.h_addr_list = &(addr_list[0]);
  1287. #endif
  1288.       host_info.h_addr = (char*)(&numeric_addr);
  1289.       addr_list[1] = 0;
  1290.       host_info.h_length = strlen (addr_list[0]);
  1291.     }
  1292.  
  1293.   memset (&address, 0, sizeof (address));
  1294.   memcpy (&address.sin_addr, host_info_ptr->h_addr, host_info_ptr->h_length);
  1295.   address.sin_family = host_info_ptr->h_addrtype;
  1296.   address.sin_port = port;
  1297.  
  1298.   s = socket (host_info_ptr->h_addrtype, SOCK_STREAM, 0);
  1299.   if (s < 0) 
  1300.     report_file_error ("error creating socket", list1 (name));
  1301.  
  1302.   /* Turn off interrupts here -- see comments below.  There used to
  1303.      be code which called bind_polling_period() to slow the polling
  1304.      period down rather than turn it off, but that seems rather
  1305.      bogus to me.  Best thing here is to use a non-blocking connect
  1306.      or something, to check for QUIT. */
  1307.  
  1308.   /* Comments that are not quite valid: */
  1309.  
  1310.   /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
  1311.      when connect is interrupted.  So let's not let it get interrupted.
  1312.      Note we do not turn off polling, because polling is only used
  1313.      when not interrupt_input, and thus not normally used on the systems
  1314.      which have this bug.  On systems which use polling, there's no way
  1315.      to quit if polling is turned off.  */
  1316.  
  1317.   /* Slow down polling.  Some kernels have a bug which causes retrying
  1318.      connect to fail after a connect.  */
  1319.  
  1320.   slow_down_interrupts ();
  1321.  
  1322.  loop:
  1323.  
  1324.   /* A system call interrupted with a SIGALRM or SIGIO comes back
  1325.      here, with can_break_system_calls reset to 0. */
  1326.   setjmp (break_system_call_jump);
  1327.   if (QUITP)
  1328.     {
  1329.       speed_up_interrupts ();
  1330.       REALLY_QUIT;
  1331.       /* In case something really weird happens ... */
  1332.       slow_down_interrupts ();
  1333.     }
  1334.  
  1335.   /* Break out of connect with a signal (it isn't otherwise possible).
  1336.      Thus you don't get screwed with a hung network. */
  1337.   can_break_system_calls = 1;
  1338.   retval = connect (s, (struct sockaddr *) &address, sizeof (address));
  1339.   can_break_system_calls = 0;
  1340.   if (retval == -1 && errno != EISCONN)
  1341.     {
  1342.       int xerrno = errno;
  1343.       if (errno == EINTR)
  1344.     goto loop;
  1345.       if (errno == EADDRINUSE && retry < 20)
  1346.     {
  1347.       retry++;
  1348.       goto loop;
  1349.     }
  1350.  
  1351.       close (s);
  1352.  
  1353.       speed_up_interrupts ();
  1354.  
  1355.       errno = xerrno;
  1356.       report_file_error ("connection failed", list2 (host, name));
  1357.     }
  1358.  
  1359.   speed_up_interrupts ();
  1360.  
  1361. #else /* TERM */
  1362.   s = connect_server (0);
  1363.   if (s < 0)
  1364.     report_file_error ("error creating socket", Fcons (name, Qnil));
  1365.   send_command (s, C_PORT, 0, "%s:%d", string_data (XSTRING (host)), ntohs (port));
  1366.   send_command (s, C_DUMB, 1, 0);
  1367. #endif /* TERM */
  1368.  
  1369.   inch = s;
  1370.   outch = dup (s);
  1371.   if (outch < 0)
  1372.     {
  1373.       close (s); /* this used to be leaked; from Kyle Jones */
  1374.       report_file_error ("error duplicating socket", list1 (name));
  1375.     }
  1376.  
  1377.   if (!NILP (buffer))
  1378.     buffer = Fget_buffer_create (buffer);
  1379.   proc = make_process_internal (name);
  1380.  
  1381.   descriptor_to_process[inch] = proc;
  1382.  
  1383.   set_descriptor_non_blocking (inch);
  1384.  
  1385.   XPROCESS (proc)->pid = Fcons (service, host);
  1386.   XPROCESS (proc)->buffer = buffer;
  1387.   init_process_fds (XPROCESS (proc), inch, outch);
  1388.   XPROCESS (proc)->connected_via_filedesc_p = 0;
  1389.  
  1390.   event_stream_select_process (XPROCESS (proc));
  1391.  
  1392.   UNGCPRO;
  1393.   return proc;
  1394. }
  1395.  
  1396. #endif    /* HAVE_SOCKETS */
  1397.  
  1398.  
  1399. DEFUN ("set-process-window-size", Fset_process_window_size,
  1400.   Sset_process_window_size, 3, 3, 0,
  1401.   "Tell PROCESS that it has logical window size HEIGHT and WIDTH.")
  1402.   (proc, height, width)
  1403.      Lisp_Object proc, height, width;
  1404. {
  1405.   CHECK_PROCESS (proc, 0);
  1406.   CHECK_NATNUM (height, 0);
  1407.   CHECK_NATNUM (width, 0);
  1408.   if (set_window_size (XPROCESS (proc)->infd, XINT (height), XINT (width))
  1409.       <= 0)
  1410.     return Qnil;
  1411.   else
  1412.     return Qt;
  1413. }
  1414.  
  1415.  
  1416. /************************************************************************/
  1417. /*                              Process I/O                             */
  1418. /************************************************************************/
  1419.  
  1420. /*  (Faccept_process_output is now in event-stream.c) */
  1421.  
  1422. /* Read pending output from the process channel,
  1423.    starting with our buffered-ahead character if we have one.
  1424.    Yield number of characters read.
  1425.  
  1426.    This function reads at most 1024 bytes.
  1427.    If you want to read all available subprocess output,
  1428.    you must call it repeatedly until it returns zero.  */
  1429.  
  1430. Charcount
  1431. read_process_output (Lisp_Object proc)
  1432. {
  1433.   /* This function can GC */
  1434.   Bytecount nbytes, nchars;
  1435. #ifdef VMS
  1436.   char *chars;
  1437. #else
  1438.   Bufbyte chars[1024];
  1439. #endif
  1440.   Lisp_Object outstream;
  1441.   struct Lisp_Process *p = XPROCESS (proc);
  1442.  
  1443.   /* If there is a lot of output from the subprocess, the loop in
  1444.      execute_internal_event() might call read_process_output() more
  1445.      than once.  If the filter that was executed from one of these
  1446.      calls set the filter to t, we have to stop now.  Return -1 rather
  1447.      than 0 so execute_internal_event() doesn't close the process.
  1448.      Really, the loop in execute_internal_event() should check itself
  1449.      for a process-filter change, like in status_notify(); but the
  1450.      struct Lisp_Process is not exported outside of this file. */
  1451.   if (p->infd < 0)
  1452.     return -1; /* already closed */
  1453.  
  1454.   if (!NILP (p->filter) && (p->filter_does_read))
  1455.     {
  1456.       Lisp_Object filter_result = call2 (p->filter, proc, Qnil);
  1457.       CHECK_INT (filter_result, 0);
  1458.       return XINT (filter_result);
  1459.     }
  1460.  
  1461. #ifdef VMS
  1462.   VMS_PROC_STUFF *vs, *get_vms_process_pointer();
  1463.  
  1464.   vs = get_vms_process_pointer (XINT (p->pid));
  1465.   if (vs)
  1466.     {
  1467.       if (!vs->iosb[0])
  1468.     return(0);        /* Really weird if it does this */
  1469.       if (!(vs->iosb[0] & 1))
  1470.     return -1;        /* I/O error */
  1471.     }
  1472.   else
  1473.     error ("Could not get VMS process pointer");
  1474.   chars = vs->inputBuffer;
  1475.   nbytes = clean_vms_buffer (chars, vs->iosb[1]);
  1476.   if (nbytes <= 0)
  1477.     {
  1478.       start_vms_process_read (vs); /* Crank up the next read on the process */
  1479.       return 1;            /* Nothing worth printing, say we got 1 */
  1480.     }
  1481. #else /* not VMS */
  1482.   nbytes = Lstream_read (XLSTREAM (p->instream), chars, sizeof (chars));
  1483.   if (nbytes <= 0) return nbytes;
  1484. #endif /* not VMS */
  1485.  
  1486.   nchars = bytecount_to_charcount (chars, nbytes);
  1487.   outstream = p->filter;
  1488.   if (!NILP (outstream))
  1489.     {
  1490.       /* We inhibit quit here instead of just catching it so that 
  1491.      hitting ^G when a filter happens to be running won't screw
  1492.      it up.  */
  1493.       int speccount = specpdl_depth ();
  1494.  
  1495.       specbind (Qinhibit_quit, Qt);
  1496.       call2 (outstream, proc, make_string (chars, nbytes));
  1497.  
  1498. #ifdef VMS
  1499.       start_vms_process_read (vs);
  1500. #endif
  1501.       unbind_to (speccount, Qnil);
  1502.       return (nchars);
  1503.     }
  1504.  
  1505.   /* If no filter, write into buffer if it isn't dead.  */
  1506.   if (!NILP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
  1507.     {
  1508.       Lisp_Object old_read_only = Qnil;
  1509.       Bufpos old_point;
  1510.       Bufpos old_begv;
  1511.       Bufpos old_zv;
  1512.       int old_zmacs_region_stays = zmacs_region_stays;
  1513.       struct gcpro gcpro1, gcpro2;
  1514.       struct buffer *buf = XBUFFER (p->buffer);
  1515.  
  1516.       GCPRO2 (proc, old_read_only);
  1517.  
  1518.       old_point = BUF_PT (buf);
  1519.       old_begv = BUF_BEGV (buf);
  1520.       old_zv = BUF_ZV (buf);
  1521.       old_read_only = buf->read_only;
  1522.       buf->read_only = Qnil;
  1523.  
  1524.       /* Insert new output into buffer
  1525.      at the current end-of-output marker,
  1526.      thus preserving logical ordering of input and output.  */
  1527.       if (XMARKER (p->mark)->buffer)
  1528.     BUF_SET_PT (buf,
  1529.             bufpos_clip_to_bounds (old_begv, marker_position (p->mark),
  1530.                        old_zv));
  1531.       else
  1532.     BUF_SET_PT (buf, old_zv);
  1533.  
  1534.       /* If the output marker is outside of the visible region, save
  1535.      the restriction and widen.  */
  1536.       if (! (BUF_BEGV (buf) <= BUF_PT (buf) &&
  1537.          BUF_PT (buf) <= BUF_ZV (buf)))
  1538.     Fwiden (p->buffer);
  1539.  
  1540.       /* Make sure opoint floats ahead of any new text, just as point
  1541.      would.  */
  1542.       if (BUF_PT (buf) <= old_point)
  1543.     old_point += nchars;
  1544.  
  1545.       /* Insert after old_begv, but before old_zv.  */
  1546.       if (BUF_PT (buf) < old_begv)
  1547.     old_begv += nchars;
  1548.       if (BUF_PT (buf) <= old_zv)
  1549.     old_zv += nchars;
  1550.  
  1551. #if 0
  1552.       /* This screws up intial display of the window.  jla */
  1553.  
  1554.       /* Insert before markers in case we are inserting where
  1555.      the buffer's mark is, and the user's next command is Meta-y.  */
  1556.       buffer_insert_raw_string_1 (buf, -1, chars,
  1557.                   nbytes, INSDEL_BEFORE_MARKERS);
  1558. #else
  1559.       buffer_insert_raw_string (buf, chars, nbytes);
  1560. #endif
  1561.  
  1562.       Fset_marker (p->mark, make_number (BUF_PT (buf)), p->buffer);
  1563.  
  1564.       MARK_MODELINE_CHANGED;
  1565.  
  1566.       /* If the restriction isn't what it should be, set it.  */
  1567.       if (old_begv != BUF_BEGV (buf) || old_zv != BUF_ZV (buf))
  1568.     Fnarrow_to_region (make_number (old_begv), make_number (old_zv),
  1569.                p->buffer);
  1570.  
  1571.       /* Handling the process output should not deactivate the mark.  */
  1572.       zmacs_region_stays = old_zmacs_region_stays;
  1573.       buf->read_only = old_read_only;
  1574.       BUF_SET_PT (buf, old_point);
  1575.  
  1576.       UNGCPRO;
  1577.     }
  1578. #ifdef VMS
  1579.   start_vms_process_read (vs);
  1580. #endif
  1581.   return (nchars);
  1582. }
  1583.  
  1584. /* Sending data to subprocess */
  1585.  
  1586. static jmp_buf send_process_frame;
  1587.  
  1588. static SIGTYPE
  1589. send_process_trap (int signum)
  1590. {
  1591.   EMACS_REESTABLISH_SIGNAL (signum, send_process_trap);
  1592.   EMACS_UNBLOCK_SIGNAL (signum);
  1593.   longjmp (send_process_frame, 1);
  1594. }
  1595.  
  1596. /* send some data to process PROC.  If NONRELOCATABLE is non-NULL, it
  1597.    specifies the address of the data.  Otherwise, the data comes from the
  1598.    object RELOCATABLE (either a string or a buffer).  START and LEN
  1599.    specify the offset and length of the data to send. */
  1600.  
  1601. static void
  1602. send_process (Lisp_Object proc,
  1603.               Lisp_Object relocatable, CONST Bufbyte *nonrelocatable,
  1604.               int start, int len)
  1605. {
  1606.   /* This function can GC */
  1607.   /* Don't use register vars; longjmp can lose them.  */
  1608.   int rv;
  1609.   struct gcpro gcpro1, gcpro2;
  1610.   SIGTYPE (*old_sigpipe)() = 0;
  1611.   Lisp_Object lstream_obj = Qnil;
  1612.   Lstream *lstream;
  1613.   struct Lisp_Process *p = XPROCESS (proc);
  1614.  
  1615. #ifdef VMS
  1616.   VMS_PROC_STUFF *vs, *get_vms_process_pointer (int);
  1617. #endif /* VMS */
  1618.  
  1619.   GCPRO2 (proc, lstream_obj);
  1620.  
  1621. #ifdef VMS
  1622.   vs = get_vms_process_pointer (XINT (p->pid));
  1623.   if (vs == 0)
  1624.     error ("Could not find this process: %x",
  1625.        XINT (p->pid));
  1626.   else if (write_to_vms_process (vs, buf, len))
  1627.     ;
  1628. #else
  1629.  
  1630.   if (nonrelocatable)
  1631.     lstream_obj =
  1632.       make_fixed_buffer_stream ((unsigned char *) nonrelocatable + start, len);
  1633.   else if (BUFFERP (relocatable))
  1634.     lstream_obj = make_lisp_buffer_stream (XBUFFER (relocatable),
  1635.                        start, start + len, 0);
  1636.   else
  1637.     lstream_obj = make_lisp_string_stream (relocatable, start, len);
  1638.   lstream = XLSTREAM (lstream_obj);
  1639.  
  1640.   if (!setjmp (send_process_frame))
  1641.     {
  1642.       int ch;
  1643.  
  1644.       /* !!#### This will run into problems under Mule --
  1645.      writing out a single byte might result in multiple bytes getting
  1646.      sent, and an EWOULDBLOCK might occur in the middle of this.
  1647.      This will probably result in data loss.  Need to think about
  1648.      this. */
  1649.       while ((ch = Lstream_getc (lstream)) != EOF)
  1650.     {
  1651.       /* Attempt to write the byte out.  This may take more than
  1652.          one try if the write would block. */
  1653.       do
  1654.         {
  1655.           if (!old_sigpipe)
  1656.         {
  1657.           old_sigpipe =
  1658.             (SIGTYPE (*) ()) signal (SIGPIPE, send_process_trap);
  1659.         }
  1660.           rv = Lstream_putc (XLSTREAM (p->outstream), ch);
  1661.           if (rv >= 0)
  1662.         break;
  1663.           else
  1664.         {
  1665.           signal (SIGPIPE, old_sigpipe);
  1666.           old_sigpipe = 0;
  1667.           if (0
  1668. #ifdef EWOULDBLOCK
  1669.               || errno == EWOULDBLOCK
  1670. #endif
  1671. #ifdef EAGAIN
  1672.               || errno == EAGAIN
  1673. #endif
  1674.               )
  1675.             /* Buffer is full.  Wait, accepting input; 
  1676.                that may allow the program
  1677.                to finish doing output and read more.  */
  1678.             {
  1679.               Faccept_process_output (Qnil, make_number (1), Qnil);
  1680.               
  1681.               rv = 0;
  1682.             }
  1683.           else
  1684.             /* This is a real error.  */
  1685.             report_file_error ("writing to process",
  1686.                        list1 (proc));
  1687.         }
  1688.         } while (1);
  1689.     }
  1690.     }
  1691. #endif /* !VMS */
  1692.   else
  1693.     { /* We got here from a longjmp() from the SIGPIPE handler */
  1694.       if (old_sigpipe)
  1695.         signal (SIGPIPE, old_sigpipe);
  1696.       p->status_symbol = Qexit;
  1697.       p->exit_code = 256; /* #### SIGPIPE ??? */
  1698.       p->core_dumped = 0;
  1699.       p->tick++;
  1700.       process_tick++;
  1701.       deactivate_process (proc);
  1702. #ifdef VMS
  1703.       error ("Error writing to process %s; closed it",
  1704.          string_data (XSTRING (p->name)));
  1705. #else
  1706.       error ("SIGPIPE raised on process %s; closed it",
  1707.          string_data (XSTRING (p->name)));
  1708. #endif
  1709.     }
  1710.   Lstream_flush (XLSTREAM (p->outstream));
  1711.   UNGCPRO;
  1712. }
  1713.  
  1714. DEFUN ("process-tty-name", Fprocess_tty_name, Sprocess_tty_name, 1, 1, 0,
  1715.   "Return the name of the terminal PROCESS uses, or nil if none.\n\
  1716. This is the terminal that the process itself reads and writes on,\n\
  1717. not the name of the pty that Emacs uses to talk with that terminal.")
  1718.      (proc)
  1719.      Lisp_Object proc;
  1720. {
  1721.   CHECK_PROCESS (proc, 0);
  1722.   return XPROCESS (proc)->tty_name;
  1723. }
  1724.  
  1725. DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer,
  1726.   2, 2, 0,
  1727.   "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
  1728.   (proc, buffer)
  1729.      Lisp_Object proc, buffer;
  1730. {
  1731.   CHECK_PROCESS (proc, 0);
  1732.   if (!NILP (buffer))
  1733.     CHECK_BUFFER (buffer, 1);
  1734.   XPROCESS (proc)->buffer = buffer;
  1735.   return buffer;
  1736. }
  1737.  
  1738. DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer,
  1739.   1, 1, 0,
  1740.   "Return the buffer PROCESS is associated with.\n\
  1741. Output from PROCESS is inserted in this buffer\n\
  1742. unless PROCESS has a filter.")
  1743.   (proc)
  1744.      Lisp_Object proc;
  1745. {
  1746.   CHECK_PROCESS (proc, 0);
  1747.   return XPROCESS (proc)->buffer;
  1748. }
  1749.  
  1750. DEFUN ("process-mark", Fprocess_mark, Sprocess_mark,
  1751.   1, 1, 0,
  1752.   "Return the marker for the end of the last output from PROCESS.")
  1753.   (proc)
  1754.      Lisp_Object proc;
  1755. {
  1756.   CHECK_PROCESS (proc, 0);
  1757. #ifdef ENERGIZE
  1758.   if (EQ (proc, Venergize_process)) /* per buffer rather than per process */
  1759.     return Fenergize_user_input_buffer_mark (Qnil); /* ## current_buffer ok? */
  1760. #endif
  1761.   return XPROCESS (proc)->mark;
  1762. }
  1763.  
  1764. void
  1765. set_process_filter (Lisp_Object proc, Lisp_Object filter, int filter_does_read)
  1766. {
  1767.   CHECK_PROCESS (proc, 0);
  1768.   if (EQ (filter, Qt))
  1769.     {
  1770.       event_stream_unselect_process (XPROCESS (proc));
  1771.     }
  1772.   else if (EQ (XPROCESS (proc)->filter, Qt))
  1773.     {
  1774.       event_stream_select_process (XPROCESS (proc));
  1775.     }
  1776.   XPROCESS (proc)->filter = filter;
  1777.   XPROCESS (proc)->filter_does_read = filter_does_read;
  1778. }
  1779.  
  1780. DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter,
  1781.   2, 2, 0,
  1782.   "Give PROCESS the filter function FILTER; nil means no filter.\n\
  1783. t means stop accepting output from the process.\n\
  1784. When a process has a filter, each time it does output\n\
  1785. the entire string of output is passed to the filter.\n\
  1786. The filter gets two arguments: the process and the string of output.\n\
  1787. If the process has a filter, its buffer is not used for output.")
  1788.   (proc, filter)
  1789.      Lisp_Object proc, filter;
  1790. {
  1791.   set_process_filter (proc, filter, 0);
  1792.   return filter;
  1793. }
  1794.  
  1795. DEFUN ("process-filter", Fprocess_filter, Sprocess_filter,
  1796.   1, 1, 0,
  1797.   "Return the filter function of PROCESS; nil if none.\n\
  1798. See `set-process-filter' for more info on filter functions.")
  1799.   (proc)
  1800.      Lisp_Object proc;
  1801. {
  1802.   CHECK_PROCESS (proc, 0);
  1803.   return XPROCESS (proc)->filter;
  1804. }
  1805.  
  1806. DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region,
  1807.   3, 3, 0,
  1808.   "Send current contents of region as input to PROCESS.\n\
  1809. PROCESS may be a process name or an actual process.\n\
  1810. Called from program, takes three arguments, PROCESS, START and END.\n\
  1811. If the region is more than 500 characters long,\n\
  1812. it is sent in several bunches.  This may happen even for shorter regions.\n\
  1813. Output from processes can arrive in between bunches.")
  1814.   (process, start, end)
  1815.      Lisp_Object process, start, end;
  1816. {
  1817.   /* This function can GC */
  1818.   Lisp_Object proc = get_process (process);
  1819.   Bufpos st, en;
  1820.  
  1821.   get_bufrange (current_buffer, start, end, &st, &en, 0);
  1822.  
  1823.   send_process (proc, Fcurrent_buffer (), 0,
  1824.                 st, en - st);
  1825.   return (Qnil);
  1826. }
  1827.  
  1828. DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string,
  1829.   2, 4, 0,
  1830.   "Send PROCESS the contents of STRING as input.\n\
  1831. PROCESS may be a process name or an actual process.\n\
  1832. Optional arguments FROM and TO specify part of STRING, see `substring'.\n\
  1833. If STRING is more than 500 or so characters long,\n\
  1834. it is sent in several bunches.  This may happen even for shorter strings.\n\
  1835. Output from processes can arrive in between bunches.")
  1836.   (process, string, from, to)
  1837.      Lisp_Object process, string, from, to;
  1838. {
  1839.   /* This function can GC */
  1840.   Lisp_Object proc;
  1841.   Bytecount len;
  1842.   Bytecount bfr, bto;
  1843.  
  1844.   proc = get_process (process);
  1845.   len = get_string_range (string, from, to, &bfr, &bto);
  1846.  
  1847.   send_process (proc, string, 0, bfr, len);
  1848.   return (Qnil);
  1849. }
  1850.  
  1851. #ifdef MULE
  1852.  
  1853. DEFUN ("process-input-coding-system",
  1854.        Fprocess_input_coding_system, Sprocess_input_coding_system, 1, 1, 0,
  1855.   "Return PROCESS's input coding system.")
  1856.   (process)
  1857.      Lisp_Object process;
  1858. {
  1859.   Lisp_Object obj = Qnil;
  1860.  
  1861.   process = get_process (process);
  1862.   XSETCODING_SYSTEM (obj, mule_encoding_stream_codesys
  1863.              (XPROCESS (process)->instream));
  1864.   return obj;
  1865. }
  1866.  
  1867. DEFUN ("process-output-coding-system",
  1868.        Fprocess_output_coding_system, Sprocess_output_coding_system, 1, 1, 0,
  1869.   "Return PROCESS's output coding system.")
  1870.   (process)
  1871.      Lisp_Object process;
  1872. {
  1873.   Lisp_Object obj = Qnil;
  1874.  
  1875.   process = get_process (process);
  1876.   XSETCODING_SYSTEM (obj, mule_decoding_stream_codesys
  1877.              (XPROCESS (process)->outstream));
  1878.   return obj;
  1879. }
  1880.  
  1881. DEFUN ("set-process-input-coding-system", Fset_process_input_coding_system,
  1882.        Sset_process_input_coding_system, 2, 2, 0,
  1883.   "Set PROCESS's input coding system to INCODE.")
  1884.   (process, incode)
  1885.      Lisp_Object proc, incode;
  1886. {
  1887.   struct Lisp_Coding_System *codesys = decode_coding_system (incode);
  1888.  
  1889.   process = get_process (process);
  1890.   set_mule_encoding_stream_codesys (XPROCESS (process)->instream, codesys);
  1891.   return Qnil;
  1892. }
  1893.  
  1894. DEFUN ("set-process-output-coding-system", Fset_process_output_coding_system,
  1895.        Sset_process_output_coding_system, 2, 2, 0,
  1896.   "Set PROCESS's output coding system to OUTCODE.")
  1897.   (process, outcode)
  1898.      Lisp_Object proc, outcode;
  1899. {
  1900.   struct Lisp_Coding_System *codesys = decode_coding_system (outcode);
  1901.  
  1902.   process = get_process (process);
  1903.   set_mule_decoding_stream_codesys (XPROCESS (process)->outstream, codesys);
  1904.   return Qnil;
  1905. }
  1906.  
  1907. #endif /* MULE */
  1908.  
  1909.  
  1910. /************************************************************************/
  1911. /*                             process status                           */
  1912. /************************************************************************/
  1913.  
  1914. static Lisp_Object
  1915. exec_sentinel_unwind (Lisp_Object datum)
  1916. {
  1917.   struct Lisp_Cons *d = XCONS (datum);
  1918.   XPROCESS (d->car)->sentinel = d->cdr;
  1919.   free_cons (d);
  1920.   return Qnil;
  1921. }
  1922.  
  1923. static void
  1924. exec_sentinel (Lisp_Object proc, Lisp_Object reason)
  1925. {
  1926.   /* This function can GC */
  1927.   Lisp_Object sentinel;
  1928.   struct Lisp_Process *p = XPROCESS (proc);
  1929.   int speccount = specpdl_depth ();
  1930.  
  1931.   sentinel = p->sentinel;
  1932.   if (NILP (sentinel))
  1933.     return;
  1934.  
  1935.   /* Zilch the sentinel while it's running, to avoid recursive invocations;
  1936.      assure that it gets restored no matter how the sentinel exits.  */
  1937.   p->sentinel = Qnil;
  1938.   record_unwind_protect (exec_sentinel_unwind, Fcons (proc, sentinel));
  1939.   /* Inhibit quit so that random quits don't screw up a running filter.  */
  1940.   specbind (Qinhibit_quit, Qt);
  1941.   call2 (sentinel, proc, reason);
  1942.   unbind_to (speccount, Qnil);
  1943. }
  1944.  
  1945. DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel,
  1946.   2, 2, 0,
  1947.   "Give PROCESS the sentinel SENTINEL; nil for none.\n\
  1948. The sentinel is called as a function when the process changes state.\n\
  1949. It gets two arguments: the process, and a string describing the change.")
  1950.   (proc, sentinel)
  1951.      Lisp_Object proc, sentinel;
  1952. {
  1953.   CHECK_PROCESS (proc, 0);
  1954.   XPROCESS (proc)->sentinel = sentinel;
  1955.   return sentinel;
  1956. }
  1957.  
  1958. DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
  1959.   1, 1, 0,
  1960.   "Return the sentinel of PROCESS; nil if none.\n\
  1961. See `set-process-sentinel' for more info on sentinels.")
  1962.   (proc)
  1963.      Lisp_Object proc;
  1964. {
  1965.   CHECK_PROCESS (proc, 0);
  1966.   return XPROCESS (proc)->sentinel;
  1967. }
  1968.  
  1969.  
  1970. CONST char *
  1971. signal_name (int signum)
  1972. {
  1973.   if (signum >= 0 && signum < NSIG)
  1974.     return ((CONST char *) sys_siglist[signum]);
  1975.   return ((CONST char *) GETTEXT ("unknown signal"));
  1976. }
  1977.  
  1978. /* Compute the Lisp form of the process status from
  1979.    the numeric status that was returned by `wait'.  */
  1980.  
  1981. static void
  1982. update_status_from_wait_code (struct Lisp_Process *p, WAITTYPE *w_fmh)
  1983. {
  1984.   /* C compiler lossage when attempting to pass w directly */
  1985.   WAITTYPE w = *w_fmh;
  1986.  
  1987.   if (WIFSTOPPED (w))
  1988.     {
  1989.       p->status_symbol = Qstop;
  1990.       p->exit_code = WSTOPSIG (w);
  1991.       p->core_dumped = 0;
  1992.     }
  1993.   else if (WIFEXITED (w))
  1994.     {
  1995.       p->status_symbol = Qexit;
  1996.       p->exit_code = WRETCODE (w);
  1997.       p->core_dumped = ((WCOREDUMP (w)) ? 1 : 0);
  1998.     }
  1999.   else if (WIFSIGNALED (w))
  2000.     {
  2001.       p->status_symbol = Qsignal;
  2002.       p->exit_code = (int) WTERMSIG (w);
  2003.       p->core_dumped = ((WCOREDUMP (w)) ? 1 : 0);
  2004.     }
  2005.   else
  2006.     {
  2007.       p->status_symbol = Qrun;
  2008.       p->exit_code = 0;
  2009.     }
  2010. }
  2011.  
  2012. void
  2013. update_process_status (Lisp_Object p,
  2014.                Lisp_Object status_symbol,
  2015.                int exit_code,
  2016.                int core_dumped)
  2017. {
  2018.   XPROCESS (p)->tick++;
  2019.   process_tick++;
  2020.   XPROCESS (p)->status_symbol = status_symbol;
  2021.   XPROCESS (p)->exit_code = exit_code;
  2022.   XPROCESS (p)->core_dumped = core_dumped;
  2023. }
  2024.  
  2025. #ifdef SIGCHLD
  2026.  
  2027. #define MAX_EXITED_PROCESSES 1000
  2028. static volatile pid_t exited_processes[MAX_EXITED_PROCESSES];
  2029. static volatile WAITTYPE exited_processes_status[MAX_EXITED_PROCESSES];
  2030. static volatile int exited_processes_index;
  2031.  
  2032. static volatile int sigchld_happened;
  2033.  
  2034. /* For any processes that have changed status and are recorded
  2035.    and such, update the corresponding struct Lisp_Process.
  2036.    We separate this from record_exited_processes() so that
  2037.    we never have to call this function from within a signal
  2038.    handler.  We block SIGCHLD in case record_exited_processes()
  2039.    is called from a signal handler. */
  2040.  
  2041. static void
  2042. reap_exited_processes (void)
  2043. {
  2044.   int i;
  2045.   struct Lisp_Process *p;
  2046.  
  2047.   EMACS_BLOCK_SIGNAL (SIGCHLD);
  2048.   for (i = 0; i < exited_processes_index; i++)
  2049.     {
  2050.       int pid = exited_processes[i];
  2051.       WAITTYPE w = exited_processes_status[i];
  2052.  
  2053.       /* Find the process that signaled us, and record its status.  */
  2054.  
  2055.       p = 0;
  2056.       {
  2057.         Lisp_Object tail;
  2058.     LIST_LOOP (tail, Vprocess_list)
  2059.       {
  2060.         Lisp_Object proc = XCAR (tail);
  2061.         p = XPROCESS (proc);
  2062.         if (INTP (p->pid) && XINT (p->pid) == pid)
  2063.           break;
  2064.         p = 0;
  2065.       }
  2066.       }
  2067.  
  2068.       if (p)
  2069.     {
  2070.       /* Change the status of the process that was found.  */
  2071.       p->tick++;
  2072.       process_tick++;
  2073.       update_status_from_wait_code (p, &w);
  2074.       
  2075.           /* If process has terminated, stop waiting for its output.  */
  2076.       if (WIFSIGNALED (w) || WIFEXITED (w))
  2077.         {
  2078.           if (p->infd >= 0)
  2079.         {
  2080.           /* We can't just call event_stream->unselect_process_cb (p)
  2081.              here, because that calls XtRemoveInput, which is not
  2082.              necessarily reentrant, so we can't call this at interrupt
  2083.              level.
  2084.            */
  2085.         }
  2086.         }
  2087.     }
  2088.       else
  2089.     {
  2090.           /* There was no asynchronous process found for that id.  Check
  2091.          if we have a synchronous process. Only set sync process status
  2092.          if there is one, so we work OK with the waitpid() call in
  2093.          wait_for_termination(). */
  2094.       if (synch_process_alive != 0)
  2095.         { /* Set the global sync process status variables. */
  2096.           synch_process_alive = 0;
  2097.  
  2098.           /* Report the status of the synchronous process.  */
  2099.           if (WIFEXITED (w))
  2100.         synch_process_retcode = WRETCODE (w);
  2101.           else if (WIFSIGNALED (w))
  2102.         synch_process_death = signal_name (WTERMSIG (w));
  2103.         }
  2104.         }
  2105.     }
  2106.  
  2107.   exited_processes_index = 0;
  2108.       
  2109.   EMACS_UNBLOCK_SIGNAL (SIGCHLD);
  2110. }
  2111.  
  2112. /* On receipt of a signal that a child status has changed,
  2113.  loop asking about children with changed statuses until
  2114.  the system says there are no more.  All we do is record
  2115.  the processes and wait status.
  2116.  
  2117.  This function could be called from within the SIGCHLD
  2118.  handler, so it must be completely reentrant.  When
  2119.  not called from a SIGCHLD handler, BLOCK_SIGCHLD should
  2120.  be non-zero so that SIGCHLD is blocked while this
  2121.  function is running. (This is necessary so avoid
  2122.  race conditions with the SIGCHLD_HAPPENED flag). */
  2123.  
  2124. static void
  2125. record_exited_processes (int block_sigchld)
  2126. {
  2127.   if (block_sigchld)
  2128.     EMACS_BLOCK_SIGNAL (SIGCHLD);
  2129.  
  2130.   while (sigchld_happened)
  2131.     {
  2132.       int pid;
  2133.       WAITTYPE w;
  2134.       
  2135.       /* Keep trying to get a status until we get a definitive result.  */
  2136.       do 
  2137.     {
  2138.       errno = 0;
  2139. #ifdef WNOHANG
  2140. #  ifndef WUNTRACED
  2141. #    define WUNTRACED 0
  2142. #  endif /* not WUNTRACED */
  2143. #  ifdef HAVE_WAITPID
  2144.       pid = waitpid ((pid_t) -1, &w, WNOHANG | WUNTRACED);
  2145. #  else
  2146.       pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
  2147. #  endif
  2148. #else /* not WNOHANG */
  2149.       pid = wait (&w);
  2150. #endif /* not WNOHANG */
  2151.     }
  2152.       while (pid <= 0 && errno == EINTR);
  2153.       
  2154.       if (pid <= 0)
  2155.     break;
  2156.       
  2157.       if (exited_processes_index < MAX_EXITED_PROCESSES)
  2158.     {
  2159.       exited_processes[exited_processes_index] = pid;
  2160.       exited_processes_status[exited_processes_index] = w;
  2161.       exited_processes_index++;
  2162.     }
  2163.       
  2164.       /* On systems with WNOHANG, we just ignore the number
  2165.      of times that SIGCHLD was signalled, and keep looping
  2166.      until there are no more processes to wait on.  If we
  2167.      don't have WNOHANG, we have to rely on the count in
  2168.      SIGCHLD_HAPPENED. */
  2169. #ifndef WNOHANG
  2170.       sigchld_happened--;
  2171. #endif /* not WNOHANG */
  2172.     }
  2173.  
  2174.   sigchld_happened = 0;
  2175.  
  2176.   if (block_sigchld)
  2177.     EMACS_UNBLOCK_SIGNAL (SIGCHLD);
  2178. }
  2179.  
  2180. /** USG WARNING:  Although it is not obvious from the documentation
  2181.  in signal(2), on a USG system the SIGCLD handler MUST NOT call
  2182.  signal() before executing at least one wait(), otherwise the handler
  2183.  will be called again, resulting in an infinite loop.  The relevant
  2184.  portion of the documentation reads "SIGCLD signals will be queued
  2185.  and the signal-catching function will be continually reentered until
  2186.  the queue is empty".  Invoking signal() causes the kernel to reexamine
  2187.  the SIGCLD queue.   Fred Fish, UniSoft Systems Inc.
  2188.  
  2189.  (Note that now this only applies in SYS V Release 2 and before.
  2190.  On SYS V Release 3, we use sigset() to set the signal handler for
  2191.  the first time, and so we don't have to reestablish the signal handler
  2192.  in the handler below.  On SYS V Release 4, we don't get this weirdo
  2193.  behavior when we use sigaction(), which we do use.) */
  2194.  
  2195. static SIGTYPE
  2196. sigchld_handler (int signo)
  2197. {
  2198. #ifdef OBNOXIOUS_SYSV_SIGCLD_BEHAVIOR
  2199.   int old_errno = errno;
  2200.  
  2201.   sigchld_happened++;
  2202.   record_exited_processes (0);
  2203.   errno = old_errno;
  2204. #else
  2205.   sigchld_happened++;
  2206. #endif
  2207.   signal_fake_event ();
  2208.   /* WARNING - must come after wait3() for USG systems */
  2209.   EMACS_REESTABLISH_SIGNAL (signo, sigchld_handler);
  2210.   SIGRETURN;
  2211. }
  2212.  
  2213. #endif /* SIGCHLD */
  2214.  
  2215. /* Return a string describing a process status list.  */
  2216.  
  2217. static Lisp_Object 
  2218. status_message (struct Lisp_Process *p)
  2219. {
  2220.   /* !!#### This function has not been Mule-ized */
  2221.   Lisp_Object symbol = p->status_symbol;
  2222.   int code = p->exit_code;
  2223.   int coredump = p->core_dumped;
  2224.   Lisp_Object string, string2;
  2225.  
  2226.   if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
  2227.     {
  2228.       string = build_string (signal_name (code));
  2229.       if (coredump)
  2230.     string2 = build_translated_string (" (core dumped)\n");
  2231.       else
  2232.     string2 = build_string ("\n");
  2233.       set_string_char (XSTRING (string), 0,
  2234.                DOWNCASE (current_buffer,
  2235.                  string_char (XSTRING (string), 0)));
  2236.       return concat2 (string, string2);
  2237.     }
  2238.   else if (EQ (symbol, Qexit))
  2239.     {
  2240.       if (code == 0)
  2241.     return build_translated_string ("finished\n");
  2242.       string = Fnumber_to_string (make_number (code));
  2243.       if (coredump)
  2244.     string2 = build_translated_string (" (core dumped)\n");
  2245.       else
  2246.     string2 = build_string ("\n");
  2247.       return concat2 (build_translated_string ("exited abnormally with code "),
  2248.               concat2 (string, string2));
  2249.     }
  2250.   else
  2251.     return Fcopy_sequence (Fsymbol_name (symbol));
  2252. }
  2253.  
  2254. /* Tell status_notify() to check for terminated processes.  We do this
  2255.    because on some systems we sometimes miss SIGCHLD calls. (Not sure
  2256.    why.) */
  2257.  
  2258. void
  2259. kick_status_notify (void)
  2260. {
  2261.   process_tick++;
  2262. }
  2263.  
  2264. /* Report all recent events of a change in process status
  2265.    (either run the sentinel or output a message).
  2266.    This is done while Emacs is waiting for keyboard input.  */
  2267.  
  2268. void
  2269. status_notify (void)
  2270. {
  2271.   /* !!#### This function has not been Mule-ized */
  2272.   /* This function can GC */
  2273.   Lisp_Object tail = Qnil;
  2274.   Lisp_Object symbol = Qnil;
  2275.   Lisp_Object msg = Qnil;
  2276.   struct gcpro gcpro1, gcpro2, gcpro3;
  2277.   /* process_tick is volatile, so we have to remember it now.
  2278.      Otherwise, we get a race condition is SIGCHLD happens during
  2279.      this function.
  2280.  
  2281.      (Actually, this is not the case anymore.  The code to
  2282.      update the process structures has been moved out of the
  2283.      SIGCHLD handler.  But for the moment I'm leaving this
  2284.      stuff in -- it can't hurt.) */
  2285.   int temp_process_tick;
  2286.  
  2287. #ifdef SIGCHLD
  2288. #ifndef OBNOXIOUS_SYSV_SIGCLD_BEHAVIOR
  2289.   record_exited_processes (1);
  2290. #endif
  2291.   reap_exited_processes ();
  2292. #endif
  2293.  
  2294.   temp_process_tick = process_tick;
  2295.  
  2296.   if (update_tick == temp_process_tick)
  2297.     return;
  2298.  
  2299.   /* We need to gcpro tail; if read_process_output calls a filter
  2300.      which deletes a process and removes the cons to which tail points
  2301.      from Vprocess_alist, and then causes a GC, tail is an unprotected
  2302.      reference.  */
  2303.   GCPRO3 (tail, symbol, msg);
  2304.  
  2305.   for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
  2306.     {
  2307.       Lisp_Object proc = XCAR (tail);
  2308.       struct Lisp_Process *p = XPROCESS (proc);
  2309.       /* p->tick is also volatile.  Same thing as above applies. */
  2310.       int this_process_tick;
  2311.  
  2312. #ifdef HAVE_WAITPID
  2313.       /* #### extra check for terminated processes, in case a SIGCHLD
  2314.      got missed (this seems to happen sometimes, I'm not sure why).
  2315.        */
  2316.       {
  2317.     WAITTYPE w;
  2318. #ifdef SIGCHLD
  2319.     EMACS_BLOCK_SIGNAL (SIGCHLD);
  2320. #endif
  2321.     if (INTP (p->pid) &&
  2322.         waitpid (XINT (p->pid), &w, WNOHANG) == XINT (p->pid))
  2323.       {
  2324.         p->tick++;
  2325.         update_status_from_wait_code (p, &w);
  2326.       }
  2327. #ifdef SIGCHLD
  2328.     EMACS_UNBLOCK_SIGNAL (SIGCHLD);
  2329. #endif
  2330.       }
  2331. #endif
  2332.       this_process_tick = p->tick;
  2333.       if (this_process_tick != p->update_tick)
  2334.     {
  2335.       p->update_tick = this_process_tick;
  2336.  
  2337.       /* If process is still active, read any output that remains.  */
  2338.           while (!EQ (p->filter, Qt)
  2339.          && read_process_output (proc) > 0)
  2340.             ;
  2341.  
  2342.       /* Get the text to use for the message.  */
  2343.       msg = status_message (p);
  2344.  
  2345.       /* If process is terminated, deactivate it or delete it.  */
  2346.       symbol = p->status_symbol;
  2347.  
  2348.       if (EQ (symbol, Qsignal) 
  2349.               || EQ (symbol, Qexit))
  2350.         {
  2351.           if (delete_exited_processes)
  2352.         remove_process (proc);
  2353.           else
  2354.         deactivate_process (proc);
  2355.         }
  2356.  
  2357.       /* Now output the message suitably.  */
  2358.       if (!NILP (p->sentinel))
  2359.         exec_sentinel (proc, msg);
  2360.       /* Don't bother with a message in the buffer
  2361.          when a process becomes runnable.  */
  2362.       else if (!EQ (symbol, Qrun) && !NILP (p->buffer))
  2363.         {
  2364.           Lisp_Object old_read_only = Qnil;
  2365.           Lisp_Object old = Fcurrent_buffer ();
  2366.           int opoint;
  2367.               struct gcpro gcpro1, gcpro2;
  2368.  
  2369.           /* Avoid error if buffer is deleted
  2370.          (probably that's why the process is dead, too) */
  2371.           if (!BUFFER_LIVE_P (XBUFFER (p->buffer)))
  2372.         continue;
  2373.  
  2374.               GCPRO2 (old, old_read_only);
  2375.           Fset_buffer (p->buffer);
  2376.           opoint = BUF_PT (current_buffer);
  2377.           /* Insert new output into buffer
  2378.          at the current end-of-output marker,
  2379.          thus preserving logical ordering of input and output.  */
  2380.           if (XMARKER (p->mark)->buffer)
  2381.         BUF_SET_PT (current_buffer, marker_position (p->mark));
  2382.           else
  2383.         BUF_SET_PT (current_buffer, BUF_ZV (current_buffer));
  2384.           if (BUF_PT (current_buffer) <= opoint)
  2385.         opoint += (string_length (XSTRING (msg))
  2386.                            + string_length (XSTRING (p->name))
  2387.                            + 10);
  2388.  
  2389.           old_read_only = current_buffer->read_only;
  2390.           current_buffer->read_only = Qnil;
  2391.           buffer_insert_c_string (current_buffer, "\nProcess ");
  2392.           Finsert (1, &p->name);
  2393.           buffer_insert_c_string (current_buffer, " ");
  2394.           Finsert (1, &msg);
  2395.           current_buffer->read_only = old_read_only;
  2396.           Fset_marker (p->mark, make_number (BUF_PT (current_buffer)),
  2397.                p->buffer);
  2398.  
  2399.           BUF_SET_PT (current_buffer, opoint);
  2400.           Fset_buffer (old);
  2401.               UNGCPRO;
  2402.         }
  2403.     }
  2404.     } /* end for */
  2405.  
  2406.   /* in case buffers use %s in modeline-format */
  2407.   MARK_MODELINE_CHANGED;
  2408.   redisplay ();
  2409.  
  2410.   update_tick = temp_process_tick;
  2411.  
  2412.   UNGCPRO;
  2413. }
  2414.  
  2415. DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0,
  2416.   "Return the status of PROCESS.\n\
  2417. This is a symbol, one of these:\n\
  2418. \n\
  2419. run  -- for a process that is running.\n\
  2420. stop -- for a process stopped but continuable.\n\
  2421. exit -- for a process that has exited.\n\
  2422. signal -- for a process that has got a fatal signal.\n\
  2423. open -- for a network stream connection that is open.\n\
  2424. closed -- for a network stream connection that is closed.\n\
  2425. nil -- if arg is a process name and no such process exists.\n\
  2426. PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
  2427. nil, indicating the current buffer's process.")
  2428.   (proc)
  2429.      Lisp_Object proc;
  2430. {
  2431.   Lisp_Object status;
  2432.  
  2433.   if (STRINGP (proc))
  2434.     proc = Fget_process (proc);
  2435.   else
  2436.     proc = get_process (proc);
  2437.  
  2438.   if (NILP (proc))
  2439.     return proc;
  2440.  
  2441.   status = XPROCESS (proc)->status_symbol;
  2442.   if (network_connection_p (proc))
  2443.     {
  2444.       if (EQ (status, Qrun))
  2445.     status = Qopen;
  2446.       else if (EQ (status, Qexit))
  2447.     status = Qclosed;
  2448.     }
  2449.   return (status);
  2450. }
  2451.  
  2452. DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status,
  2453.        1, 1, 0,
  2454.   "Return the exit status of PROCESS or the signal number that killed it.\n\
  2455. If PROCESS has not yet exited or died, return 0.")
  2456.   (proc)
  2457.      Lisp_Object proc;
  2458. {
  2459.   CHECK_PROCESS (proc, 0);
  2460.   return (make_number (XPROCESS (proc)->exit_code));
  2461. }
  2462.  
  2463.  
  2464. /* send a signal number SIGNO to PROCESS.
  2465.    CURRENT_GROUP means send to the process group that currently owns
  2466.    the terminal being used to communicate with PROCESS.
  2467.    This is used for various commands in shell mode.
  2468.    If NOMSG is zero, insert signal-announcements into process's buffers
  2469.    right away.
  2470.  
  2471.    If we can, we try to signal PROCESS by sending control characters
  2472.    down the pty.  This allows us to signal inferiors who have changed
  2473.    their uid, for which killpg would return an EPERM error.  */
  2474.  
  2475. static void
  2476. process_send_signal (Lisp_Object process0, int signo,
  2477.                      int current_group, int nomsg)
  2478. {
  2479.   /* !!#### This function has not been Mule-ized */
  2480.   /* This function can GC */
  2481.   Lisp_Object proc = get_process (process0);
  2482.   struct Lisp_Process *p = XPROCESS (proc);
  2483.   int gid;
  2484.   int no_pgrp = 0;
  2485.  
  2486.   if (network_connection_p (proc))
  2487.     error ("Network connection %s is not a subprocess",
  2488.        string_data (XSTRING (p->name)));
  2489.   if (p->infd < 0)
  2490.     error ("Process %s is not active",
  2491.        string_data (XSTRING (p->name)));
  2492.  
  2493.   if (!p->pty_flag)
  2494.     current_group = 0;
  2495.  
  2496.   /* If we are using pgrps, get a pgrp number and make it negative.  */
  2497.   if (current_group)
  2498.     {
  2499. #ifdef SIGNALS_VIA_CHARACTERS
  2500.       /* If possible, send signals to the entire pgrp
  2501.      by sending an input character to it.  */
  2502.  
  2503.       /* TERMIOS is the latest and bestest, and seems most likely to
  2504.          work.  If the system has it, use it.  */
  2505. #ifdef HAVE_TERMIOS
  2506.       struct termios t;
  2507.  
  2508.       switch (signo)
  2509.     {
  2510.     case SIGINT:
  2511.       tcgetattr (p->infd, &t);
  2512.       send_process (proc, Qnil, (Bufbyte *) &t.c_cc[VINTR], 0,
  2513.             1);
  2514.       return;
  2515.  
  2516.     case SIGQUIT:
  2517.       tcgetattr (p->infd, &t);
  2518.         send_process (proc, Qnil, (Bufbyte *) &t.c_cc[VQUIT], 0,
  2519.             1);
  2520.         return;
  2521.  
  2522.       case SIGTSTP:
  2523.       tcgetattr (p->infd, &t);
  2524. #if defined (VSWTCH) && !defined (PREFER_VSUSP)
  2525.         send_process (proc, Qnil, (Bufbyte *) &t.c_cc[VSWTCH], 0,
  2526.             1);
  2527. #else
  2528.       send_process (proc, Qnil, (Bufbyte *) &t.c_cc[VSUSP], 0,
  2529.             1);
  2530. #endif
  2531.         return;
  2532.     }
  2533.  
  2534. #else /* ! HAVE_TERMIOS */
  2535.  
  2536.       /* On Berkeley descendants, the following IOCTL's retrieve the
  2537.      current control characters.  */
  2538. #if defined (TIOCGLTC) && defined (TIOCGETC)
  2539.       struct tchars c;
  2540.       struct ltchars lc;
  2541.  
  2542.       switch (signo)
  2543.     {
  2544.     case SIGINT:
  2545.       ioctl (p->infd, TIOCGETC, &c);
  2546.       send_process (proc, Qnil, (Bufbyte *) &c.t_intrc, 0,
  2547.             1);
  2548.       return;
  2549.     case SIGQUIT:
  2550.       ioctl (p->infd, TIOCGETC, &c);
  2551.       send_process (proc, Qnil, (Bufbyte *) &c.t_quitc, 0,
  2552.             1);
  2553.       return;
  2554. #ifdef SIGTSTP
  2555.     case SIGTSTP:
  2556.       ioctl (p->infd, TIOCGLTC, &lc);
  2557.       send_process (proc, Qnil, (Bufbyte *) &lc.t_suspc, 0,
  2558.             1);
  2559.       return;
  2560. #endif /* ! defined (SIGTSTP) */
  2561.     }
  2562.  
  2563. #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
  2564.  
  2565.       /* On SYSV descendants, the TCGETA ioctl retrieves the current control
  2566.      characters.  */
  2567. #ifdef TCGETA
  2568.       struct termio t;
  2569.       switch (signo)
  2570.     {
  2571.     case SIGINT:
  2572.       ioctl (p->infd, TCGETA, &t);
  2573.       send_process (proc, Qnil, (Bufbyte *) &t.c_cc[VINTR], 0,
  2574.             1);
  2575.       return;
  2576.     case SIGQUIT:
  2577.       ioctl (p->infd, TCGETA, &t);
  2578.       send_process (proc, Qnil, (Bufbyte *) &t.c_cc[VQUIT], 0,
  2579.             1);
  2580.       return;
  2581. #ifdef SIGTSTP
  2582.     case SIGTSTP:
  2583.       ioctl (p->infd, TCGETA, &t);
  2584.       send_process (proc, Qnil, (Bufbyte *) &t.c_cc[VSWTCH], 0,
  2585.             1);
  2586.       return;
  2587. #endif /* ! defined (SIGTSTP) */
  2588.     }
  2589. #else /* ! defined (TCGETA) */
  2590. ERROR! Using SIGNALS_VIA_CHARACTERS, but not (TIOCGLTC && TIOCGETC) || TCGETA
  2591.       /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
  2592.      you'd better be using one of the alternatives above!  */
  2593. #endif /* ! defined (TCGETA) */
  2594. #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
  2595. #endif /* ! defined HAVE_TERMIOS */
  2596. #endif /* ! defined (SIGNALS_VIA_CHARACTERS) */
  2597.  
  2598. #ifdef TIOCGPGRP 
  2599.       /* Get the pgrp using the tty itself, if we have that.
  2600.      Otherwise, use the pty to get the pgrp.
  2601.      On pfa systems, saka@pfu.fujitsu.co.JP writes:
  2602.      "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
  2603.      But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
  2604.      His patch indicates that if TIOCGPGRP returns an error, then
  2605.      we should just assume that p->pid is also the process group id.  */
  2606.       {
  2607.     int err;
  2608.  
  2609.     if (p->subtty != -1)
  2610.       err = ioctl (p->subtty, TIOCGPGRP, &gid);
  2611.     else
  2612.       err = ioctl (p->infd, TIOCGPGRP, &gid);
  2613.  
  2614. #ifdef pfa
  2615.     if (err == -1)
  2616.       gid = - XINT (p->pid);
  2617. #endif /* ! defined (pfa) */
  2618.       }
  2619.       if (gid == -1)
  2620.     no_pgrp = 1;
  2621.       else
  2622.     gid = - gid;
  2623. #else /* ! defined (TIOCGPGRP ) */
  2624.       /* Can't select pgrps on this system, so we know that
  2625.      the child itself heads the pgrp.  */
  2626.       gid = - XINT (p->pid);
  2627. #endif /* ! defined (TIOCGPGRP ) */
  2628.     }
  2629.   else
  2630.     gid = - XINT (p->pid);
  2631.  
  2632.   switch (signo)
  2633.     {
  2634. #ifdef SIGCONT
  2635.     case SIGCONT:
  2636.       p->status_symbol = Qrun;
  2637.       p->exit_code = 0;
  2638.       p->tick++;
  2639.       process_tick++;
  2640.       if (!nomsg)
  2641.     status_notify ();
  2642.       break;
  2643. #endif /* ! defined (SIGCONT) */
  2644.     case SIGINT:
  2645. #ifdef VMS
  2646.       send_process (proc, Qnil, (Bufbyte *) "\003", 0,
  2647.             1); /* ^C */
  2648.       goto whoosh;
  2649. #endif
  2650.     case SIGQUIT:
  2651. #ifdef VMS
  2652.       send_process (proc, Qnil, (Bufbyte *) "\031", 0,
  2653.             1); /* ^Y */
  2654.       goto whoosh;
  2655. #endif
  2656.     case SIGKILL:
  2657. #ifdef VMS
  2658.       sys$forcex (&(XINT (p->pid)), 0, 1);
  2659.       whoosh:
  2660. #endif
  2661.       flush_pending_output (p->infd);
  2662.       break;
  2663.     }
  2664.  
  2665.   /* If we don't have process groups, send the signal to the immediate
  2666.      subprocess.  That isn't really right, but it's better than any
  2667.      obvious alternative.  */
  2668.   if (no_pgrp)
  2669.     {
  2670.       kill (XINT (p->pid), signo);
  2671.       return;
  2672.     }
  2673.  
  2674.   /* gid may be a pid, or minus a pgrp's number */
  2675. #ifdef TIOCSIGSEND
  2676.   if (current_group)
  2677.     ioctl (p->infd, TIOCSIGSEND, signo);
  2678.   else
  2679.     {
  2680.       gid = - XINT (p->pid);
  2681.       kill (gid, signo);
  2682.     }
  2683. #else /* ! defined (TIOCSIGSEND) */
  2684.   EMACS_KILLPG (-gid, signo);
  2685. #endif /* ! defined (TIOCSIGSEND) */
  2686. }
  2687.  
  2688. DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0,
  2689.   "Interrupt process PROCESS.  May be process or name of one.\n\
  2690. Nil or no arg means current buffer's process.\n\
  2691. Second arg CURRENT-GROUP non-nil means send signal to\n\
  2692. the current process-group of the process's controlling terminal\n\
  2693. rather than to the process's own process group.\n\
  2694. If the process is a shell, this means interrupt current subjob\n\
  2695. rather than the shell.")
  2696.   (process, current_group)
  2697.      Lisp_Object process, current_group;
  2698. {
  2699.   /* This function can GC */
  2700.   process_send_signal (process, SIGINT, !NILP (current_group), 0);
  2701.   return process;
  2702. }
  2703.  
  2704. DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0,
  2705.   "Kill process PROCESS.  May be process or name of one.\n\
  2706. See function `interrupt-process' for more details on usage.")
  2707.   (process, current_group)
  2708.      Lisp_Object process, current_group;
  2709. {
  2710.   /* This function can GC */
  2711.   process_send_signal (process, SIGKILL, !NILP (current_group),
  2712.                0);
  2713.   return process;
  2714. }
  2715.  
  2716. DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0,
  2717.   "Send QUIT signal to process PROCESS.  May be process or name of one.\n\
  2718. See function `interrupt-process' for more details on usage.")
  2719.   (process, current_group)
  2720.      Lisp_Object process, current_group;
  2721. {
  2722.   /* This function can GC */
  2723.   process_send_signal (process, SIGQUIT, !NILP (current_group),
  2724.                0);
  2725.   return process;
  2726. }
  2727.  
  2728. DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0,
  2729.   "Stop process PROCESS.  May be process or name of one.\n\
  2730. See function `interrupt-process' for more details on usage.")
  2731.   (process, current_group)
  2732.      Lisp_Object process, current_group;
  2733. {
  2734.   /* This function can GC */
  2735. #ifndef SIGTSTP
  2736.   error ("no SIGTSTP support");
  2737. #else
  2738.   process_send_signal (process, SIGTSTP, !NILP (current_group),
  2739.                0);
  2740. #endif
  2741.   return process;
  2742. }
  2743.  
  2744. DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0,
  2745.   "Continue process PROCESS.  May be process or name of one.\n\
  2746. See function `interrupt-process' for more details on usage.")
  2747.   (process, current_group)
  2748.      Lisp_Object process, current_group;
  2749. {
  2750.   /* This function can GC */
  2751. #ifdef SIGCONT
  2752.     process_send_signal (process, SIGCONT, !NILP (current_group),
  2753.              0);
  2754. #else
  2755.     error ("no SIGCONT support");
  2756. #endif
  2757.   return process;
  2758. }
  2759.  
  2760. DEFUN ("signal-process", Fsignal_process, Ssignal_process,
  2761.   2, 2, "nProcess number: \nnSignal code: ",
  2762.   "Send the process with number PID the signal with code CODE.\n\
  2763. Both PID and CODE are integers.")
  2764.   (pid, sig)
  2765.      Lisp_Object pid, sig;
  2766. {
  2767.   CHECK_INT (pid, 0);
  2768.   CHECK_INT (sig, 1);
  2769.   return make_number (kill (XINT (pid), XINT (sig)));
  2770. }
  2771.  
  2772. DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0,
  2773.   "Make PROCESS see end-of-file in its input.\n\
  2774. PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
  2775. nil, indicating the current buffer's process.\n\
  2776. If PROCESS is a network connection, or is a process communicating\n\
  2777. through a pipe (as opposed to a pty), then you cannot send any more\n\
  2778. text to PROCESS after you call this function.")
  2779.   (process)
  2780.      Lisp_Object process;
  2781. {
  2782.   /* !!#### This function has not been Mule-ized */
  2783.   /* This function can GC */
  2784.   Lisp_Object proc;
  2785.  
  2786.   proc = get_process (process);
  2787.  
  2788.   /* Make sure the process is really alive.  */
  2789.   if (! EQ (XPROCESS (proc)->status_symbol, Qrun))
  2790.     error ("Process %s not running",
  2791.        string_data (XSTRING (XPROCESS (proc)->name)));
  2792.  
  2793.   /* Sending a zero-length record is supposed to mean eof
  2794.      when TIOCREMOTE is turned on.  */
  2795. #ifdef DID_REMOTE /* #### Nothing defines this, nothing else uses this.  What does "DID_REMOTE" mean? */
  2796.   {
  2797.     char buf[1];
  2798.     write (XPROCESS (proc)->outfd, buf, 0);
  2799.   }
  2800. #else /* did not do TOICREMOTE */
  2801. #ifdef VMS
  2802.   send_process (proc, Qnil, (Bufbyte *) "\032", 0, 1);   /* ^Z */
  2803. #else
  2804.   if (XPROCESS (proc)->pty_flag)
  2805.     {
  2806.       /* #### get_eof_char simply doesn't return the correct character
  2807.          here.  Maybe it is needed to determine the right eof
  2808.          character in init_process_fds but here it simply screws
  2809.          things up. */
  2810. #if 0
  2811.       Bufbyte eof_char = get_eof_char (XPROCESS (proc));
  2812.       send_process (proc, Qnil, &eof_char, 0, 1);
  2813. #else
  2814.       send_process (proc, Qnil, (Bufbyte *) "\004", 0, 1);
  2815. #endif
  2816.     }
  2817.   else
  2818.     {
  2819.       close (XPROCESS (proc)->outfd);
  2820.       XPROCESS (proc)->outfd = open (NULL_DEVICE, O_WRONLY, 0);
  2821.     }
  2822. #endif /* !VMS */
  2823. #endif /* did not do TOICREMOTE */
  2824.   return process;
  2825. }
  2826.  
  2827.  
  2828. /************************************************************************/
  2829. /*                          deleting a process                          */
  2830. /************************************************************************/
  2831.  
  2832. void
  2833. deactivate_process (Lisp_Object proc)
  2834. {
  2835.   int inchannel, outchannel;
  2836.   struct Lisp_Process *p = XPROCESS (proc);
  2837.   SIGTYPE (*old_sigpipe)() = 0;
  2838.  
  2839.   inchannel = p->infd;
  2840.   outchannel = p->outfd;
  2841.  
  2842.   /* closing the outstream could result in SIGPIPE, so ignore it. */
  2843.   old_sigpipe =
  2844.     (SIGTYPE (*) ()) signal (SIGPIPE, SIG_IGN);
  2845.   Lstream_close (XLSTREAM (p->instream));
  2846.   Lstream_close (XLSTREAM (p->outstream));
  2847.   signal (SIGPIPE, old_sigpipe);
  2848.  
  2849.   if (inchannel >= 0)
  2850.     {
  2851.       /* Beware SIGCHLD hereabouts. */
  2852.       flush_pending_output (inchannel);
  2853.       close_descriptor_pair (inchannel, outchannel);
  2854.       if (!NILP (p->pid))
  2855.     {
  2856.       /* It's possible that we got as far in the process-creation
  2857.          process as creating the descriptors but didn't get so
  2858.          far as selecting the process for input.  In this
  2859.          case, p->pid is nil: p->pid is set at the same time that
  2860.          the process is selected for input. */
  2861. #ifdef VMS
  2862.       {
  2863.         VMS_PROC_STUFF *get_vms_process_pointer (), *vs;
  2864.         if (outchannel >= 0) 
  2865.           sys$dassgn (outchannel);
  2866.         vs = get_vms_process_pointer (XINT (p->pid));
  2867.         if (vs)
  2868.           give_back_vms_process_stuff (vs);
  2869.       }
  2870. #endif /* VMS */
  2871.       /* Must call this before setting the file descriptors to 0 */
  2872.       event_stream_unselect_process (p);
  2873.     }
  2874.  
  2875.       p->infd = -1;
  2876.       p->outfd = -1;
  2877.       descriptor_to_process[inchannel] = Qnil;
  2878.     }
  2879. }
  2880.  
  2881. static void
  2882. remove_process (Lisp_Object proc)
  2883. {
  2884.   Vprocess_list = delq_no_quit (proc, Vprocess_list);
  2885.   Fset_marker (XPROCESS (proc)->mark, Qnil, Qnil);
  2886.  
  2887.   deactivate_process (proc);
  2888. }
  2889.  
  2890. DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0,
  2891.   "Delete PROCESS: kill it and forget about it immediately.\n\
  2892. PROCESS may be a process or the name of one, or a buffer name.")
  2893.   (proc)
  2894.      Lisp_Object proc;
  2895. {
  2896.   /* This function can GC */
  2897.   struct Lisp_Process *p;
  2898.   proc = get_process (proc);
  2899.   p = XPROCESS (proc);
  2900.   if (network_connection_p (proc))
  2901.     {
  2902.       p->status_symbol = Qexit;
  2903.       p->exit_code = 0;
  2904.       p->core_dumped = 0;
  2905.       p->tick++;
  2906.       process_tick++;
  2907.     }
  2908.   else if (p->infd >= 0)
  2909.     {
  2910.       Fkill_process (proc, Qnil);
  2911.       /* Do this now, since remove_process will make sigchld_handler do nothing.  */
  2912.       p->status_symbol = Qsignal;
  2913.       p->exit_code = SIGKILL;
  2914.       p->core_dumped = 0;
  2915.       p->tick++;
  2916.       process_tick++;
  2917.       status_notify ();
  2918.     }
  2919.   remove_process (proc);
  2920.   return Qnil;
  2921. }
  2922.  
  2923. /* Kill all processes associated with `buffer'.
  2924.  If `buffer' is nil, kill all processes  */
  2925.  
  2926. void
  2927. kill_buffer_processes (Lisp_Object buffer)
  2928. {
  2929.   Lisp_Object tail;
  2930.  
  2931.   for (tail = Vprocess_list; XGCTYPE (tail) == Lisp_Cons;
  2932.        tail = XCDR (tail))
  2933.     {
  2934.       Lisp_Object proc = XCAR (tail);
  2935.       if (gc_record_type_p (proc, lrecord_process)
  2936.       && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer)))
  2937.     {
  2938.       if (network_connection_p (proc))
  2939.         Fdelete_process (proc);
  2940.       else if (XPROCESS (proc)->infd >= 0)
  2941.         process_send_signal (proc, SIGHUP, 0, 1);
  2942.     }
  2943.     }
  2944. }
  2945.  
  2946. #if 0 /* Unused */
  2947. int
  2948. count_active_processes (void)
  2949. {
  2950.   Lisp_Object tail;
  2951.   int count = 0;
  2952.  
  2953.   for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
  2954.     {
  2955.       Lisp_Object status = XPROCESS (XCAR (tail))->status_symbol;
  2956.       if ((EQ (status, Qrun) || EQ (status, Qstop)))
  2957.     count++;
  2958.     }
  2959.  
  2960.   return count;
  2961. }
  2962. #endif /* Unused */
  2963.  
  2964. DEFUN ("process-kill-without-query", Fprocess_kill_without_query,
  2965.   Sprocess_kill_without_query, 1, 2, 0,
  2966.   "Say no query needed if PROCESS is running when Emacs is exited.\n\
  2967. Optional second argument if non-nil says to require a query.\n\
  2968. Value is t if a query was formerly required.")
  2969.   (proc, require_query_p)
  2970.      Lisp_Object proc, require_query_p;
  2971. {
  2972.   int tem;
  2973.  
  2974.   CHECK_PROCESS (proc, 0);
  2975.   tem = XPROCESS (proc)->kill_without_query;
  2976.   XPROCESS (proc)->kill_without_query = NILP (require_query_p);
  2977.  
  2978.   return (tem ? Qnil : Qt);
  2979. }
  2980.  
  2981. DEFUN ("process-kill-without-query-p", Fprocess_kill_without_query_p,
  2982.   Sprocess_kill_without_query_p, 1, 1, 0,
  2983.   "Whether PROC will be killed without query if running when emacs is exited.")
  2984.   (proc)
  2985.      Lisp_Object proc;
  2986. {
  2987.   CHECK_PROCESS (proc, 0);
  2988.   return (XPROCESS (proc)->kill_without_query ? Qt : Qnil);
  2989. }
  2990.  
  2991.  
  2992. /* This is not named init_process in order to avoid a conflict with NS 3.3 */
  2993. void
  2994. init_xemacs_process (void)
  2995. {
  2996.   int i;
  2997.  
  2998. #ifdef SIGCHLD
  2999. # ifndef CANNOT_DUMP
  3000.   if (! noninteractive || initialized)
  3001. # endif
  3002.     signal (SIGCHLD, sigchld_handler);
  3003. #endif /* SIGCHLD */
  3004.  
  3005.   Vprocess_list = Qnil;
  3006.   for (i = 0; i < MAXDESC; i++)
  3007.     {
  3008.       descriptor_to_process[i] = Qnil;
  3009.     }
  3010. }
  3011. /* 
  3012.  * DEFUN ("process-connection",Fprocess_connection,Sprocess_connection,0,1,0,
  3013.  *  "Return the connection type of `PROCESS'.  This can be nil (pipe),\n\
  3014.  * t or pty (pty) or stream (socket connection).")
  3015.  *   (process)
  3016.  *      Lisp_Object process;
  3017.  * {
  3018.  *   return XPROCESS (process)->type;
  3019.  * }
  3020.  */
  3021.  
  3022. void
  3023. syms_of_process (void)
  3024. {
  3025.   defsymbol (&Qprocessp, "processp");
  3026.   defsymbol (&Qrun, "run");
  3027.   defsymbol (&Qstop, "stop");
  3028.   defsymbol (&Qsignal, "signal");
  3029.   /* Qexit is already defined by syms_of_eval
  3030.    * defsymbol (&Qexit, "exit"); 
  3031.    */
  3032.   defsymbol (&Qopen, "open");
  3033.   defsymbol (&Qclosed, "closed");
  3034.  
  3035.   defsubr (&Sprocessp);
  3036.   defsubr (&Sget_process);
  3037.   defsubr (&Sget_buffer_process);
  3038.   defsubr (&Sdelete_process);
  3039.   defsubr (&Sprocess_status);
  3040.   defsubr (&Sprocess_exit_status);
  3041.   defsubr (&Sprocess_id);
  3042.   defsubr (&Sprocess_name);
  3043.   defsubr (&Sprocess_tty_name);
  3044.   defsubr (&Sprocess_command);
  3045.   defsubr (&Sset_process_buffer);
  3046.   defsubr (&Sprocess_buffer);
  3047.   defsubr (&Sprocess_mark);
  3048.   defsubr (&Sset_process_filter);
  3049.   defsubr (&Sprocess_filter);
  3050.   defsubr (&Sset_process_window_size);
  3051.   defsubr (&Sset_process_sentinel);
  3052.   defsubr (&Sprocess_sentinel);
  3053.   defsubr (&Sprocess_kill_without_query);
  3054.   defsubr (&Sprocess_kill_without_query_p);
  3055.   defsubr (&Sprocess_list);
  3056.   defsubr (&Sstart_process_internal);
  3057. #ifdef HAVE_SOCKETS
  3058.   defsubr (&Sopen_network_stream_internal);
  3059. #endif /* HAVE_SOCKETS */
  3060.   defsubr (&Sprocess_send_region);
  3061.   defsubr (&Sprocess_send_string);
  3062.   defsubr (&Sinterrupt_process);
  3063.   defsubr (&Skill_process);
  3064.   defsubr (&Squit_process);
  3065.   defsubr (&Sstop_process);
  3066.   defsubr (&Scontinue_process);
  3067.   defsubr (&Sprocess_send_eof);
  3068.   defsubr (&Ssignal_process);
  3069. /*  defsubr (&Sprocess_connection); */
  3070. #ifdef MULE
  3071.   defsubr (&Sprocess_input_coding_system);
  3072.   defsubr (&Sprocess_output_coding_system);
  3073.   defsubr (&Sset_process_input_coding_system);
  3074.   defsubr (&Sset_process_output_coding_system);
  3075. #endif /* MULE */
  3076. }
  3077.  
  3078. void
  3079. vars_of_process (void)
  3080. {
  3081.   Fprovide (intern ("subprocesses"));
  3082. #ifdef HAVE_SOCKETS
  3083.   Fprovide (intern ("network-streams"));
  3084. #endif
  3085.   staticpro (&Vprocess_list);
  3086.  
  3087.   DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
  3088.     "*Non-nil means delete processes immediately when they exit.\n\
  3089. nil means don't delete them until `list-processes' is run.");
  3090.  
  3091.   delete_exited_processes = 1;
  3092.  
  3093.   DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type,
  3094.     "Control type of device used to communicate with subprocesses.\n\
  3095. Values are nil to use a pipe, or t or `pty' to use a pty.\n\
  3096. The value has no effect if the system has no ptys or if all ptys are busy:\n\
  3097. then a pipe is used in any case.\n\
  3098. The value takes effect when `start-process' is called.");
  3099.   Vprocess_connection_type = Qt;
  3100. }
  3101.  
  3102. #endif /* not NO_SUBPROCESSES */
  3103.