import xemacs-21.2.37
[chise/xemacs-chise.git.1] / src / process.c
1 /* Asynchronous subprocess control for XEmacs.
2    Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994, 1995
3    Free Software Foundation, Inc.
4    Copyright (C) 1995 Sun Microsystems, Inc.
5    Copyright (C) 1995, 1996 Ben Wing.
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
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.  */
23
24 /* This file has been Mule-ized except for `start-process-internal',
25    `open-network-stream-internal' and `open-multicast-group-internal'. */
26
27 /* This file has been split into process.c and process-unix.c by
28    Kirill M. Katsnelson <kkm@kis.ru>, so please bash him and not
29    the original author(s) */
30
31 #include <config.h>
32
33 #if !defined (NO_SUBPROCESSES)
34
35 /* The entire file is within this conditional */
36
37 #include "lisp.h"
38
39 #include "buffer.h"
40 #include "commands.h"
41 #include "events.h"
42 #include "frame.h"
43 #include "hash.h"
44 #include "insdel.h"
45 #include "lstream.h"
46 #include "opaque.h"
47 #include "process.h"
48 #include "procimpl.h"
49 #include "window.h"
50 #ifdef FILE_CODING
51 #include "file-coding.h"
52 #endif
53
54 #include "sysfile.h"
55 #include "sysproc.h"
56 #include "systime.h"
57 #include "syssignal.h" /* Always include before systty.h */
58 #include "systty.h"
59 #include "syswait.h"
60
61 Lisp_Object Qprocessp, Qprocess_live_p;
62
63 /* Process methods */
64 struct process_methods the_process_methods;
65
66 /* a process object is a network connection when its pid field a cons
67    (name of name of port we are connected to . foreign host name) */
68
69 /* Valid values of process->status_symbol */
70 Lisp_Object Qrun, Qstop;
71 /* Qrun => Qopen, Qexit => Qclosed for "network connection" processes */
72 Lisp_Object Qopen, Qclosed;
73 /* Protocol families */
74 Lisp_Object Qtcp, Qudp;
75
76 #ifdef HAVE_MULTICAST
77 Lisp_Object Qmulticast; /* Will be used for occasional warnings */
78 #endif
79
80 /* t means use pty, nil means use a pipe,
81    maybe other values to come.  */
82 Lisp_Object Vprocess_connection_type;
83
84 /* Read comments to DEFVAR of this */
85 int windowed_process_io;
86
87 #ifdef PROCESS_IO_BLOCKING
88 /* List of port numbers or port names to set a blocking I/O mode.
89    Nil means set a non-blocking I/O mode [default]. */
90 Lisp_Object network_stream_blocking_port_list;
91 #endif  /* PROCESS_IO_BLOCKING */
92
93 /* Number of events of change of status of a process.  */
94 volatile int process_tick;
95
96 /* Number of events for which the user or sentinel has been notified.  */
97 static int update_tick;
98
99 /* Nonzero means delete a process right away if it exits.  */
100 int delete_exited_processes;
101
102 /* Hash table which maps USIDs as returned by create_stream_pair_cb to
103    process objects. Processes are not GC-protected through this! */
104 struct hash_table *usid_to_process;
105
106 /* List of process objects. */
107 Lisp_Object Vprocess_list;
108
109 extern Lisp_Object Vlisp_EXEC_SUFFIXES;
110 Lisp_Object Vnull_device;
111
112 \f
113
114 static Lisp_Object
115 mark_process (Lisp_Object object)
116 {
117   Lisp_Process *process = XPROCESS (object);
118   MAYBE_PROCMETH (mark_process_data, (process));
119   mark_object (process->name);
120   mark_object (process->command);
121   mark_object (process->filter);
122   mark_object (process->sentinel);
123   mark_object (process->buffer);
124   mark_object (process->mark);
125   mark_object (process->pid);
126   mark_object (process->pipe_instream);
127   mark_object (process->pipe_outstream);
128 #ifdef FILE_CODING
129   mark_object (process->coding_instream);
130   mark_object (process->coding_outstream);
131 #endif
132   return process->status_symbol;
133 }
134
135 static void
136 print_process (Lisp_Object object, Lisp_Object printcharfun, int escapeflag)
137 {
138   Lisp_Process *process = XPROCESS (object);
139
140   if (print_readably)
141     error ("printing unreadable object #<process %s>",
142            XSTRING_DATA (process->name));
143
144   if (!escapeflag)
145     {
146       print_internal (process->name, printcharfun, 0);
147     }
148   else
149     {
150       int netp = network_connection_p (object);
151       write_c_string ((netp ? GETTEXT ("#<network connection ") :
152                        GETTEXT ("#<process ")), printcharfun);
153       print_internal (process->name, printcharfun, 1);
154       write_c_string ((netp ? " " : " pid "), printcharfun);
155       print_internal (process->pid, printcharfun, 1);
156       write_c_string (" state:", printcharfun);
157       print_internal (process->status_symbol, printcharfun, 1);
158       MAYBE_PROCMETH (print_process_data, (process, printcharfun));
159       write_c_string (">", printcharfun);
160     }
161 }
162
163 #ifdef HAVE_WINDOW_SYSTEM
164 extern void debug_process_finalization (Lisp_Process *p);
165 #endif /* HAVE_WINDOW_SYSTEM */
166
167 static void
168 finalize_process (void *header, int for_disksave)
169 {
170   /* #### this probably needs to be tied into the tty event loop */
171   /* #### when there is one */
172   Lisp_Process *p = (Lisp_Process *) header;
173 #ifdef HAVE_WINDOW_SYSTEM
174   if (!for_disksave)
175     {
176       debug_process_finalization (p);
177     }
178 #endif /* HAVE_WINDOW_SYSTEM */
179
180   if (p->process_data)
181     {
182       MAYBE_PROCMETH (finalize_process_data, (p, for_disksave));
183       if (!for_disksave)
184         xfree (p->process_data);
185     }
186 }
187
188 DEFINE_LRECORD_IMPLEMENTATION ("process", process,
189                                mark_process, print_process, finalize_process,
190                                0, 0, 0, Lisp_Process);
191 \f
192 /************************************************************************/
193 /*                       basic process accessors                        */
194 /************************************************************************/
195
196 /* Under FILE_CODING, this function returns low-level streams, connected
197    directly to the child process, rather than en/decoding FILE_CODING
198    streams */
199 void
200 get_process_streams (Lisp_Process *p, Lisp_Object *instr, Lisp_Object *outstr)
201 {
202   assert (p);
203   assert (NILP (p->pipe_instream) || LSTREAMP(p->pipe_instream));
204   assert (NILP (p->pipe_outstream) || LSTREAMP(p->pipe_outstream));
205   *instr = p->pipe_instream;
206   *outstr = p->pipe_outstream;
207 }
208
209 Lisp_Process *
210 get_process_from_usid (USID usid)
211 {
212   const void *vval;
213
214   assert (usid != USID_ERROR && usid != USID_DONTHASH);
215
216   if (gethash ((const void*)usid, usid_to_process, &vval))
217     {
218       Lisp_Object process;
219       CVOID_TO_LISP (process, vval);
220       return XPROCESS (process);
221     }
222   else
223     return 0;
224 }
225
226 int
227 get_process_selected_p (Lisp_Process *p)
228 {
229   return p->selected;
230 }
231
232 void
233 set_process_selected_p (Lisp_Process *p, int selected_p)
234 {
235   p->selected = !!selected_p;
236 }
237
238 int
239 connected_via_filedesc_p (Lisp_Process *p)
240 {
241   return MAYBE_INT_PROCMETH (tooltalk_connection_p, (p));
242 }
243
244 #ifdef HAVE_SOCKETS
245 int
246 network_connection_p (Lisp_Object process)
247 {
248   return CONSP (XPROCESS (process)->pid);
249 }
250 #endif
251
252 DEFUN ("processp", Fprocessp, 1, 1, 0, /*
253 Return t if OBJECT is a process.
254 */
255        (object))
256 {
257   return PROCESSP (object) ? Qt : Qnil;
258 }
259
260 DEFUN ("process-live-p", Fprocess_live_p, 1, 1, 0, /*
261 Return t if OBJECT is a process that is alive.
262 */
263        (object))
264 {
265   return PROCESSP (object) && PROCESS_LIVE_P (XPROCESS (object))
266     ? Qt : Qnil;
267 }
268
269 DEFUN ("process-list", Fprocess_list, 0, 0, 0, /*
270 Return a list of all processes.
271 */
272        ())
273 {
274   return Fcopy_sequence (Vprocess_list);
275 }
276
277 DEFUN ("get-process", Fget_process, 1, 1, 0, /*
278 Return the process named PROCESS-NAME (a string), or nil if there is none.
279 PROCESS-NAME may also be a process; if so, the value is that process.
280 */
281        (process_name))
282 {
283   if (PROCESSP (process_name))
284     return process_name;
285
286   if (!gc_in_progress)
287     /* this only gets called during GC when emacs is going away as a result
288        of a signal or crash. */
289     CHECK_STRING (process_name);
290
291   {
292     LIST_LOOP_2 (process, Vprocess_list)
293       if (internal_equal (process_name, XPROCESS (process)->name, 0))
294         return process;
295   }
296   return Qnil;
297 }
298
299 DEFUN ("get-buffer-process", Fget_buffer_process, 1, 1, 0, /*
300 Return the (or, a) process associated with BUFFER.
301 BUFFER may be a buffer or the name of one.
302 */
303        (buffer))
304 {
305   if (NILP (buffer)) return Qnil;
306   buffer = Fget_buffer (buffer);
307   if (NILP (buffer)) return Qnil;
308
309   {
310     LIST_LOOP_2 (process, Vprocess_list)
311       if (EQ (XPROCESS (process)->buffer, buffer))
312         return process;
313   }
314   return Qnil;
315 }
316
317 /* This is how commands for the user decode process arguments.  It
318    accepts a process, a process name, a buffer, a buffer name, or nil.
319    Buffers denote the first process in the buffer, and nil denotes the
320    current buffer.  */
321
322 static Lisp_Object
323 get_process (Lisp_Object name)
324 {
325   Lisp_Object buffer;
326
327 #ifdef I18N3
328   /* #### Look more closely into translating process names. */
329 #endif
330
331   /* This may be called during a GC from process_send_signal() from
332      kill_buffer_processes() if emacs decides to abort(). */
333   if (PROCESSP (name))
334     return name;
335   else if (STRINGP (name))
336     {
337       Lisp_Object object = Fget_process (name);
338       if (PROCESSP (object))
339         return object;
340
341       buffer = Fget_buffer (name);
342       if (BUFFERP (buffer))
343         goto have_buffer_object;
344
345       error ("Process %s does not exist", XSTRING_DATA (name));
346     }
347   else if (NILP (name))
348     {
349       buffer = Fcurrent_buffer ();
350       goto have_buffer_object;
351     }
352   else if (BUFFERP (name))
353     {
354       Lisp_Object process;
355       buffer = name;
356
357     have_buffer_object:
358       process = Fget_buffer_process (buffer);
359       if (PROCESSP (process))
360         return process;
361
362       error ("Buffer %s has no process",
363              XSTRING_DATA (XBUFFER (buffer)->name));
364     }
365   else
366     return get_process (Fsignal (Qwrong_type_argument,
367                                  (list2 (build_string ("process or buffer or nil"),
368                                  name))));
369 }
370
371 DEFUN ("process-id", Fprocess_id, 1, 1, 0, /*
372 Return the process id of PROCESS.
373 This is the pid of the Unix process which PROCESS uses or talks to.
374 For a network connection, this value is a cons of
375  (foreign-network-port . foreign-host-name).
376 */
377        (process))
378 {
379   Lisp_Object pid;
380   CHECK_PROCESS (process);
381
382   pid = XPROCESS (process)->pid;
383   if (network_connection_p (process))
384     /* return Qnil; */
385     return Fcons (Fcar (pid), Fcdr (pid));
386   else
387     return pid;
388 }
389
390 DEFUN ("process-name", Fprocess_name, 1, 1, 0, /*
391 Return the name of PROCESS, as a string.
392 This is the name of the program invoked in PROCESS,
393 possibly modified to make it unique among process names.
394 */
395        (process))
396 {
397   CHECK_PROCESS (process);
398   return XPROCESS (process)->name;
399 }
400
401 DEFUN ("process-command", Fprocess_command, 1, 1, 0, /*
402 Return the command that was executed to start PROCESS.
403 This is a list of strings, the first string being the program executed
404 and the rest of the strings being the arguments given to it.
405 */
406        (process))
407 {
408   CHECK_PROCESS (process);
409   return XPROCESS (process)->command;
410 }
411
412 \f
413 /************************************************************************/
414 /*                          creating a process                          */
415 /************************************************************************/
416
417 Lisp_Object
418 make_process_internal (Lisp_Object name)
419 {
420   Lisp_Object val, name1;
421   int i;
422   Lisp_Process *p = alloc_lcrecord_type (Lisp_Process, &lrecord_process);
423
424   /* If name is already in use, modify it until it is unused.  */
425   name1 = name;
426   for (i = 1; ; i++)
427     {
428       char suffix[10];
429       Lisp_Object tem = Fget_process (name1);
430       if (NILP (tem))
431         break;
432       sprintf (suffix, "<%d>", i);
433       name1 = concat2 (name, build_string (suffix));
434     }
435   name = name1;
436   p->name = name;
437
438   p->command  = Qnil;
439   p->filter   = Qnil;
440   p->sentinel = Qnil;
441   p->buffer   = Qnil;
442   p->mark = Fmake_marker ();
443   p->pid = Qnil;
444   p->status_symbol = Qrun;
445   p->exit_code = 0;
446   p->core_dumped = 0;
447   p->filter_does_read = 0;
448   p->kill_without_query = 0;
449   p->selected = 0;
450   p->tick = 0;
451   p->update_tick = 0;
452   p->pipe_instream  = Qnil;
453   p->pipe_outstream = Qnil;
454 #ifdef FILE_CODING
455   p->coding_instream  = Qnil;
456   p->coding_outstream = Qnil;
457 #endif
458
459   p->process_data = 0;
460   MAYBE_PROCMETH (alloc_process_data, (p));
461
462   XSETPROCESS (val, p);
463
464   Vprocess_list = Fcons (val, Vprocess_list);
465   return val;
466 }
467
468 void
469 init_process_io_handles (Lisp_Process *p, void* in, void* out, int flags)
470 {
471   USID usid = event_stream_create_stream_pair (in, out,
472                                                &p->pipe_instream, &p->pipe_outstream,
473                                                flags);
474
475   if (usid == USID_ERROR)
476     report_file_error ("Setting up communication with subprocess", Qnil);
477
478   if (usid != USID_DONTHASH)
479     {
480       Lisp_Object process = Qnil;
481       XSETPROCESS (process, p);
482       puthash ((const void*)usid, LISP_TO_VOID (process), usid_to_process);
483     }
484
485   MAYBE_PROCMETH (init_process_io_handles, (p, in, out, flags));
486
487 #ifdef FILE_CODING
488   p->coding_instream = make_decoding_input_stream
489     (XLSTREAM (p->pipe_instream),
490      Fget_coding_system (Vcoding_system_for_read));
491   Lstream_set_character_mode (XLSTREAM (p->coding_instream));
492   p->coding_outstream = make_encoding_output_stream
493     (XLSTREAM (p->pipe_outstream),
494      Fget_coding_system (Vcoding_system_for_write));
495   /* CODE_CNTL (&out_state[outchannel]) |= CC_END; !!####
496      What's going on here? */
497 #endif /* FILE_CODING */
498 }
499
500 static void
501 create_process (Lisp_Object process, Lisp_Object *argv, int nargv,
502                 Lisp_Object program, Lisp_Object cur_dir)
503 {
504   Lisp_Process *p = XPROCESS (process);
505   int pid;
506
507   /* *_create_process may change status_symbol, if the process
508      is a kind of "fire-and-forget" (no I/O, unwaitable) */
509   p->status_symbol = Qrun;
510   p->exit_code = 0;
511
512   pid = PROCMETH (create_process, (p, argv, nargv, program, cur_dir));
513
514   p->pid = make_int (pid);
515   if (PROCESS_LIVE_P (p))
516     event_stream_select_process (p);
517 }
518
519 /* This function is the unwind_protect form for Fstart_process_internal.  If
520    PROCESS doesn't have its pid set, then we know someone has signalled
521    an error and the process wasn't started successfully, so we should
522    remove it from the process list.  */
523 static void remove_process (Lisp_Object process);
524 static Lisp_Object
525 start_process_unwind (Lisp_Object process)
526 {
527   /* Was PROCESS started successfully?  */
528   if (EQ (XPROCESS (process)->pid, Qnil))
529     remove_process (process);
530   return Qnil;
531 }
532
533 DEFUN ("start-process-internal", Fstart_process_internal, 3, MANY, 0, /*
534 Start a program in a subprocess.  Return the process object for it.
535 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS
536 NAME is name for process.  It is modified if necessary to make it unique.
537 BUFFER is the buffer or (buffer-name) to associate with the process.
538  Process output goes at end of that buffer, unless you specify
539  an output stream or filter function to handle the output.
540  BUFFER may be also nil, meaning that this process is not associated
541  with any buffer
542 Third arg is program file name.  It is searched for as in the shell.
543 Remaining arguments are strings to give program as arguments.
544 INCODE and OUTCODE specify the coding-system objects used in input/output
545  from/to the process.
546 */
547        (int nargs, Lisp_Object *args))
548 {
549   /* This function can call lisp */
550   /* !!#### This function has not been Mule-ized */
551   Lisp_Object buffer, name, program, process, current_dir;
552   Lisp_Object tem;
553   int speccount = specpdl_depth ();
554   struct gcpro gcpro1, gcpro2, gcpro3;
555
556   name = args[0];
557   buffer = args[1];
558   program = args[2];
559   current_dir = Qnil;
560
561   /* Protect against various file handlers doing GCs below. */
562   GCPRO3 (buffer, program, current_dir);
563
564   if (!NILP (buffer))
565     buffer = Fget_buffer_create (buffer);
566
567   CHECK_STRING (name);
568   CHECK_STRING (program);
569
570   /* Make sure that the child will be able to chdir to the current
571      buffer's current directory, or its unhandled equivalent.  We
572      can't just have the child check for an error when it does the
573      chdir, since it's in a vfork.
574
575      Note: these assignments and calls are like this in order to insure
576      "caller protects args" GC semantics. */
577   current_dir = current_buffer->directory;
578   current_dir = Funhandled_file_name_directory (current_dir);
579   current_dir = expand_and_dir_to_file (current_dir, Qnil);
580
581 #if 0   /* This loser breaks ange-ftp */
582   /* dmoore - if you re-enable this code, you have to gcprotect
583      current_buffer through the above calls. */
584   if (NILP (Ffile_accessible_directory_p (current_dir)))
585     report_file_error ("Setting current directory",
586                        list1 (current_buffer->directory));
587 #endif /* 0 */
588
589   /* If program file name is not absolute, search our path for it */
590   if (!IS_DIRECTORY_SEP (XSTRING_BYTE (program, 0))
591       && !(XSTRING_LENGTH (program) > 1
592            && IS_DEVICE_SEP (XSTRING_BYTE (program, 1))))
593     {
594       struct gcpro ngcpro1;
595
596       tem = Qnil;
597       NGCPRO1 (tem);
598       locate_file (Vexec_path, program, Vlisp_EXEC_SUFFIXES, &tem, X_OK);
599       if (NILP (tem))
600         report_file_error ("Searching for program", list1 (program));
601       program = Fexpand_file_name (tem, Qnil);
602       NUNGCPRO;
603     }
604   else
605     {
606       /* we still need to canonicalize it and ensure it has the proper
607          ending, e.g. .exe */
608       struct gcpro ngcpro1;
609
610       tem = Qnil;
611       NGCPRO1 (tem);
612       locate_file (list1 (build_string ("")), program, Vlisp_EXEC_SUFFIXES,
613                    &tem, X_OK);
614       if (NILP (tem))
615         report_file_error ("Searching for program", list1 (program));
616       program = tem;
617       NUNGCPRO;
618     }
619
620   if (!NILP (Ffile_directory_p (program)))
621     invalid_operation ("Specified program for new process is a directory",
622                        program);
623
624   process = make_process_internal (name);
625
626   XPROCESS (process)->buffer = buffer;
627   XPROCESS (process)->command = Flist (nargs - 2,
628                                     args + 2);
629
630   /* Make the process marker point into the process buffer (if any).  */
631   if (!NILP (buffer))
632     Fset_marker (XPROCESS (process)->mark,
633                  make_int (BUF_ZV (XBUFFER (buffer))), buffer);
634
635   /* If an error occurs and we can't start the process, we want to
636      remove it from the process list.  This means that each error
637      check in create_process doesn't need to call remove_process
638      itself; it's all taken care of here.  */
639   record_unwind_protect (start_process_unwind, process);
640
641   create_process (process, args + 3, nargs - 3, program, current_dir);
642
643   UNGCPRO;
644   return unbind_to (speccount, process);
645 }
646
647 \f
648 #ifdef HAVE_SOCKETS
649
650
651 /* #### The network support is fairly synthetical. What we actually
652    need is a single function, which supports all datagram, stream and
653    packet stream connections, arbitrary protocol families should they
654    be supported by the target system, multicast groups, in both data
655    and control rooted/nonrooted flavors, service quality etc whatever
656    is supported by the underlying network.
657
658    It must accept a property list describing the connection. The current
659    functions must then go to lisp and provide a suitable list for the
660    generalized connection function.
661
662    Both UNIX and Win32 support BSD sockets, and there are many extensions
663    available (Sockets 2 spec).
664
665    A todo is define a consistent set of properties abstracting a
666    network connection.   -kkm
667 */
668
669
670 /* open a TCP network connection to a given HOST/SERVICE.  Treated
671    exactly like a normal process when reading and writing.  Only
672    differences are in status display and process deletion.  A network
673    connection has no PID; you cannot signal it.  All you can do is
674    deactivate and close it via delete-process */
675
676 DEFUN ("open-network-stream-internal", Fopen_network_stream_internal, 4, 5,
677        0, /*
678 Open a TCP connection for a service to a host.
679 Return a process object to represent the connection.
680 Input and output work as for subprocesses; `delete-process' closes it.
681
682 NAME is name for process.  It is modified if necessary to make it unique.
683 BUFFER is the buffer (or buffer-name) to associate with the process.
684  Process output goes at end of that buffer, unless you specify
685  an output stream or filter function to handle the output.
686  BUFFER may also be nil, meaning that this process is not associated
687  with any buffer.
688 Third arg HOST (a string) is  the name of the host to connect to,
689  or its IP address.
690 Fourth arg SERVICE is the name of the service desired (a string),
691  or an integer specifying a port number to connect to.
692 Optional fifth arg PROTOCOL is a network protocol.  Currently only 'tcp
693  (Transmission Control Protocol) and 'udp (User Datagram Protocol) are
694  supported.  When omitted, 'tcp is assumed.
695
696 Output via `process-send-string' and input via buffer or filter (see
697 `set-process-filter') are stream-oriented.  That means UDP datagrams are
698 not guaranteed to be sent and received in discrete packets. (But small
699 datagrams around 500 bytes that are not truncated by `process-send-string'
700 are usually fine.)  Note further that the UDP protocol does not guard
701 against lost packets.
702 */
703        (name, buffer, host, service, protocol))
704 {
705   /* !!#### This function has not been Mule-ized */
706   /* This function can GC */
707   Lisp_Object process = Qnil;
708   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, ngcpro1;
709   void *inch, *outch;
710
711   GCPRO5 (name, buffer, host, service, protocol);
712   CHECK_STRING (name);
713
714   if (NILP(protocol))
715     protocol = Qtcp;
716   else
717     CHECK_SYMBOL (protocol);
718
719   /* Since this code is inside HAVE_SOCKETS, existence of
720      open_network_stream is mandatory */
721   PROCMETH (open_network_stream, (name, host, service, protocol,
722                                   &inch, &outch));
723
724   if (!NILP (buffer))
725     buffer = Fget_buffer_create (buffer);
726   process = make_process_internal (name);
727   NGCPRO1 (process);
728
729   XPROCESS (process)->pid = Fcons (service, host);
730   XPROCESS (process)->buffer = buffer;
731   init_process_io_handles (XPROCESS (process), (void*)inch, (void*)outch,
732                            STREAM_NETWORK_CONNECTION);
733
734   event_stream_select_process (XPROCESS (process));
735
736   UNGCPRO;
737   NUNGCPRO;
738   return process;
739 }
740
741 #ifdef HAVE_MULTICAST
742
743 DEFUN ("open-multicast-group-internal", Fopen_multicast_group_internal, 5, 5, 0, /*
744 Open a multicast connection on the specified dest/port/ttl.
745 Return a process object to represent the connection.
746 Input and output work as for subprocesses; `delete-process' closes it.
747
748 NAME is name for process.  It is modified if necessary to make it unique.
749 BUFFER is the buffer (or buffer-name) to associate with the process.
750  Process output goes at end of that buffer, unless you specify
751  an output stream or filter function to handle the output.
752  BUFFER may also be nil, meaning that this process is not associated
753  with any buffer.
754 Third, fourth and fifth args are the multicast destination group, port and ttl.
755  dest must be an internet address between 224.0.0.0 and 239.255.255.255
756  port is a communication port like in traditional unicast
757  ttl is the time-to-live (15 for site, 63 for region and 127 for world)
758 */
759        (name, buffer, dest, port, ttl))
760 {
761   /* !!#### This function has not been Mule-ized */
762   /* This function can GC */
763   Lisp_Object process = Qnil;
764   struct gcpro gcpro1;
765   void *inch, *outch;
766
767   CHECK_STRING (name);
768
769   /* Since this code is inside HAVE_MULTICAST, existence of
770      open_network_stream is mandatory */
771   PROCMETH (open_multicast_group, (name, dest, port, ttl,
772                                    &inch, &outch));
773
774   if (!NILP (buffer))
775     buffer = Fget_buffer_create (buffer);
776
777   process = make_process_internal (name);
778   GCPRO1 (process);
779
780   XPROCESS (process)->pid = Fcons (port, dest);
781   XPROCESS (process)->buffer = buffer;
782   init_process_io_handles (XPROCESS (process), (void*)inch, (void*)outch,
783                            STREAM_NETWORK_CONNECTION);
784
785   event_stream_select_process (XPROCESS (process));
786
787   UNGCPRO;
788   return process;
789 }
790 #endif /* HAVE_MULTICAST */
791
792 #endif  /* HAVE_SOCKETS */
793
794 Lisp_Object
795 canonicalize_host_name (Lisp_Object host)
796 {
797   return PROCMETH_OR_GIVEN (canonicalize_host_name, (host), host);
798 }
799
800 \f
801 DEFUN ("set-process-window-size", Fset_process_window_size, 3, 3, 0, /*
802 Tell PROCESS that it has logical window size HEIGHT and WIDTH.
803 */
804        (process, height, width))
805 {
806   CHECK_PROCESS (process);
807   CHECK_NATNUM (height);
808   CHECK_NATNUM (width);
809   return
810     MAYBE_INT_PROCMETH (set_window_size,
811                         (XPROCESS (process), XINT (height), XINT (width))) <= 0
812     ? Qnil : Qt;
813 }
814
815 \f
816 /************************************************************************/
817 /*                              Process I/O                             */
818 /************************************************************************/
819
820 /* Read pending output from the process channel,
821    starting with our buffered-ahead character if we have one.
822    Yield number of characters read.
823
824    This function reads at most 1024 bytes.
825    If you want to read all available subprocess output,
826    you must call it repeatedly until it returns zero.  */
827
828 Charcount
829 read_process_output (Lisp_Object process)
830 {
831   /* This function can GC */
832   Bytecount nbytes, nchars;
833   Bufbyte chars[1024];
834   Lisp_Object outstream;
835   Lisp_Process *p = XPROCESS (process);
836
837   /* If there is a lot of output from the subprocess, the loop in
838      execute_internal_event() might call read_process_output() more
839      than once.  If the filter that was executed from one of these
840      calls set the filter to t, we have to stop now.  Return -1 rather
841      than 0 so execute_internal_event() doesn't close the process.
842      Really, the loop in execute_internal_event() should check itself
843      for a process-filter change, like in status_notify(); but the
844      struct Lisp_Process is not exported outside of this file. */
845   if (!PROCESS_LIVE_P (p))
846     return -1; /* already closed */
847
848   if (!NILP (p->filter) && (p->filter_does_read))
849     {
850       Lisp_Object filter_result;
851
852       /* Some weird FSFmacs crap here with
853          Vdeactivate_mark and current_buffer->keymap */
854       running_asynch_code = 1;
855       filter_result = call2_trapping_errors ("Error in process filter",
856                                              p->filter, process, Qnil);
857       running_asynch_code = 0;
858       restore_match_data ();
859       CHECK_INT (filter_result);
860       return XINT (filter_result);
861     }
862
863   nbytes = Lstream_read (XLSTREAM (DATA_INSTREAM(p)), chars, sizeof (chars));
864   if (nbytes <= 0) return nbytes;
865
866   nchars = bytecount_to_charcount (chars, nbytes);
867   outstream = p->filter;
868   if (!NILP (outstream))
869     {
870       /* We used to bind inhibit-quit to t here, but
871          call2_trapping_errors() does that for us. */
872       running_asynch_code = 1;
873       call2_trapping_errors ("Error in process filter",
874                              outstream, process, make_string (chars, nbytes));
875       running_asynch_code = 0;
876       restore_match_data ();
877       return nchars;
878     }
879
880   /* If no filter, write into buffer if it isn't dead.  */
881   if (!NILP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
882     {
883       Lisp_Object old_read_only = Qnil;
884       Bufpos old_point;
885       Bufpos old_begv;
886       Bufpos old_zv;
887       int old_zmacs_region_stays = zmacs_region_stays;
888       struct gcpro gcpro1, gcpro2;
889       struct buffer *buf = XBUFFER (p->buffer);
890
891       GCPRO2 (process, old_read_only);
892
893       old_point = BUF_PT (buf);
894       old_begv = BUF_BEGV (buf);
895       old_zv = BUF_ZV (buf);
896       old_read_only = buf->read_only;
897       buf->read_only = Qnil;
898
899       /* Insert new output into buffer
900          at the current end-of-output marker,
901          thus preserving logical ordering of input and output.  */
902       if (XMARKER (p->mark)->buffer)
903         BUF_SET_PT (buf,
904                     bufpos_clip_to_bounds (old_begv, marker_position (p->mark),
905                                            old_zv));
906       else
907         BUF_SET_PT (buf, old_zv);
908
909       /* If the output marker is outside of the visible region, save
910          the restriction and widen.  */
911       if (! (BUF_BEGV (buf) <= BUF_PT (buf) &&
912              BUF_PT (buf) <= BUF_ZV (buf)))
913         Fwiden (p->buffer);
914
915       /* Make sure opoint floats ahead of any new text, just as point
916          would.  */
917       if (BUF_PT (buf) <= old_point)
918         old_point += nchars;
919
920       /* Insert after old_begv, but before old_zv.  */
921       if (BUF_PT (buf) < old_begv)
922         old_begv += nchars;
923       if (BUF_PT (buf) <= old_zv)
924         old_zv += nchars;
925
926 #if 0
927       /* This screws up initial display of the window.  jla */
928
929       /* Insert before markers in case we are inserting where
930          the buffer's mark is, and the user's next command is Meta-y.  */
931       buffer_insert_raw_string_1 (buf, -1, chars,
932                                   nbytes, INSDEL_BEFORE_MARKERS);
933 #else
934       buffer_insert_raw_string (buf, chars, nbytes);
935 #endif
936
937       Fset_marker (p->mark, make_int (BUF_PT (buf)), p->buffer);
938
939       MARK_MODELINE_CHANGED;
940
941       /* If the restriction isn't what it should be, set it.  */
942       if (old_begv != BUF_BEGV (buf) || old_zv != BUF_ZV (buf))
943         {
944           Fwiden(p->buffer);
945           old_begv = bufpos_clip_to_bounds (BUF_BEG (buf),
946                                             old_begv,
947                                             BUF_Z (buf));
948           old_zv = bufpos_clip_to_bounds (BUF_BEG (buf),
949                                           old_zv,
950                                           BUF_Z (buf));
951           Fnarrow_to_region (make_int (old_begv), make_int (old_zv),
952                              p->buffer);
953         }
954
955       /* Handling the process output should not deactivate the mark.  */
956       zmacs_region_stays = old_zmacs_region_stays;
957       buf->read_only = old_read_only;
958       old_point = bufpos_clip_to_bounds (BUF_BEGV (buf),
959                                          old_point,
960                                          BUF_ZV (buf));
961       BUF_SET_PT (buf, old_point);
962
963       UNGCPRO;
964     }
965   return nchars;
966 }
967 \f
968 /* Sending data to subprocess */
969
970 /* send some data to process PROCESS.  If NONRELOCATABLE is non-NULL, it
971    specifies the address of the data.  Otherwise, the data comes from the
972    object RELOCATABLE (either a string or a buffer).  START and LEN
973    specify the offset and length of the data to send.
974
975    Note that START and LEN are in Bufpos's if RELOCATABLE is a buffer,
976    and in Bytecounts otherwise. */
977
978 void
979 send_process (Lisp_Object process,
980               Lisp_Object relocatable, const Bufbyte *nonrelocatable,
981               int start, int len)
982 {
983   /* This function can GC */
984   struct gcpro gcpro1, gcpro2;
985   Lisp_Object lstream = Qnil;
986
987   GCPRO2 (process, lstream);
988
989   if (NILP (DATA_OUTSTREAM (XPROCESS (process))))
990     signal_simple_error ("Process not open for writing", process);
991
992   if (nonrelocatable)
993     lstream =
994       make_fixed_buffer_input_stream (nonrelocatable + start, len);
995   else if (BUFFERP (relocatable))
996     lstream = make_lisp_buffer_input_stream (XBUFFER (relocatable),
997                                              start, start + len, 0);
998   else
999     lstream = make_lisp_string_input_stream (relocatable, start, len);
1000
1001   PROCMETH (send_process, (process, XLSTREAM (lstream)));
1002
1003   UNGCPRO;
1004   Lstream_delete (XLSTREAM (lstream));
1005 }
1006
1007 DEFUN ("process-tty-name", Fprocess_tty_name, 1, 1, 0, /*
1008 Return the name of the terminal PROCESS uses, or nil if none.
1009 This is the terminal that the process itself reads and writes on,
1010 not the name of the pty that Emacs uses to talk with that terminal.
1011 */
1012        (process))
1013 {
1014   CHECK_PROCESS (process);
1015   return MAYBE_LISP_PROCMETH (get_tty_name, (XPROCESS (process)));
1016 }
1017
1018 DEFUN ("set-process-buffer", Fset_process_buffer, 2, 2, 0, /*
1019 Set buffer associated with PROCESS to BUFFER (a buffer, or nil).
1020 */
1021        (process, buffer))
1022 {
1023   CHECK_PROCESS (process);
1024   if (!NILP (buffer))
1025     CHECK_BUFFER (buffer);
1026   XPROCESS (process)->buffer = buffer;
1027   return buffer;
1028 }
1029
1030 DEFUN ("process-buffer", Fprocess_buffer, 1, 1, 0, /*
1031 Return the buffer PROCESS is associated with.
1032 Output from PROCESS is inserted in this buffer
1033 unless PROCESS has a filter.
1034 */
1035        (process))
1036 {
1037   CHECK_PROCESS (process);
1038   return XPROCESS (process)->buffer;
1039 }
1040
1041 DEFUN ("process-mark", Fprocess_mark, 1, 1, 0, /*
1042 Return the marker for the end of the last output from PROCESS.
1043 */
1044        (process))
1045 {
1046   CHECK_PROCESS (process);
1047   return XPROCESS (process)->mark;
1048 }
1049
1050 void
1051 set_process_filter (Lisp_Object process, Lisp_Object filter, int filter_does_read)
1052 {
1053   CHECK_PROCESS (process);
1054   if (PROCESS_LIVE_P (XPROCESS (process))) {
1055     if (EQ (filter, Qt))
1056       event_stream_unselect_process (XPROCESS (process));
1057     else
1058       event_stream_select_process (XPROCESS (process));
1059   }
1060
1061   XPROCESS (process)->filter = filter;
1062   XPROCESS (process)->filter_does_read = filter_does_read;
1063 }
1064
1065 DEFUN ("set-process-filter", Fset_process_filter, 2, 2, 0, /*
1066 Give PROCESS the filter function FILTER; nil means no filter.
1067 t means stop accepting output from the process.
1068 When a process has a filter, each time it does output
1069 the entire string of output is passed to the filter.
1070 The filter gets two arguments: the process and the string of output.
1071 If the process has a filter, its buffer is not used for output.
1072 */
1073        (process, filter))
1074 {
1075   set_process_filter (process, filter, 0);
1076   return filter;
1077 }
1078
1079 DEFUN ("process-filter", Fprocess_filter, 1, 1, 0, /*
1080 Return the filter function of PROCESS; nil if none.
1081 See `set-process-filter' for more info on filter functions.
1082 */
1083        (process))
1084 {
1085   CHECK_PROCESS (process);
1086   return XPROCESS (process)->filter;
1087 }
1088
1089 DEFUN ("process-send-region", Fprocess_send_region, 3, 4, 0, /*
1090 Send current contents of the region between START and END as input to PROCESS.
1091 PROCESS may be a process or the name of a process, or a buffer or the
1092 name of a buffer, in which case the buffer's process is used.  If it
1093 is nil, the current buffer's process is used.
1094 BUFFER specifies the buffer to look in; if nil, the current buffer is used.
1095 If STRING is more than 100 or so characters long, it may be sent in
1096 several chunks.  This may happen even for shorter strings.  Output
1097 from processes can arrive in between chunks.
1098 */
1099        (process, start, end, buffer))
1100 {
1101   /* This function can GC */
1102   Bufpos bstart, bend;
1103   struct buffer *buf = decode_buffer (buffer, 0);
1104
1105   XSETBUFFER (buffer, buf);
1106   process = get_process (process);
1107   get_buffer_range_char (buf, start, end, &bstart, &bend, 0);
1108
1109   send_process (process, buffer, 0, bstart, bend - bstart);
1110   return Qnil;
1111 }
1112
1113 DEFUN ("process-send-string", Fprocess_send_string, 2, 4, 0, /*
1114 Send PROCESS the contents of STRING as input.
1115 PROCESS may be a process or the name of a process, or a buffer or the
1116 name of a buffer, in which case the buffer's process is used.  If it
1117 is nil, the current buffer's process is used.
1118 Optional arguments START and END specify part of STRING; see `substring'.
1119 If STRING is more than 100 or so characters long, it may be sent in
1120 several chunks.  This may happen even for shorter strings.  Output
1121 from processes can arrive in between chunks.
1122 */
1123        (process, string, start, end))
1124 {
1125   /* This function can GC */
1126   Bytecount bstart, bend;
1127
1128   process = get_process (process);
1129   CHECK_STRING (string);
1130   get_string_range_byte (string, start, end, &bstart, &bend,
1131                          GB_HISTORICAL_STRING_BEHAVIOR);
1132
1133   send_process (process, string, 0, bstart, bend - bstart);
1134   return Qnil;
1135 }
1136
1137 #ifdef FILE_CODING
1138
1139 DEFUN ("process-input-coding-system", Fprocess_input_coding_system, 1, 1, 0, /*
1140 Return PROCESS's input coding system.
1141 */
1142        (process))
1143 {
1144   process = get_process (process);
1145   CHECK_LIVE_PROCESS (process);
1146   return decoding_stream_coding_system (XLSTREAM (XPROCESS (process)->coding_instream) );
1147 }
1148
1149 DEFUN ("process-output-coding-system", Fprocess_output_coding_system, 1, 1, 0, /*
1150 Return PROCESS's output coding system.
1151 */
1152        (process))
1153 {
1154   process = get_process (process);
1155   CHECK_LIVE_PROCESS (process);
1156   return encoding_stream_coding_system (XLSTREAM (XPROCESS (process)->coding_outstream));
1157 }
1158
1159 DEFUN ("process-coding-system", Fprocess_coding_system, 1, 1, 0, /*
1160 Return a pair of coding-system for decoding and encoding of PROCESS.
1161 */
1162        (process))
1163 {
1164   process = get_process (process);
1165   CHECK_LIVE_PROCESS (process);
1166   return Fcons (decoding_stream_coding_system
1167                 (XLSTREAM (XPROCESS (process)->coding_instream)),
1168                 encoding_stream_coding_system
1169                 (XLSTREAM (XPROCESS (process)->coding_outstream)));
1170 }
1171
1172 DEFUN ("set-process-input-coding-system", Fset_process_input_coding_system,
1173        2, 2, 0, /*
1174 Set PROCESS's input coding system to CODESYS.
1175 */
1176        (process, codesys))
1177 {
1178   codesys = Fget_coding_system (codesys);
1179   process = get_process (process);
1180   CHECK_LIVE_PROCESS (process);
1181
1182   set_decoding_stream_coding_system
1183     (XLSTREAM (XPROCESS (process)->coding_instream), codesys);
1184   return Qnil;
1185 }
1186
1187 DEFUN ("set-process-output-coding-system", Fset_process_output_coding_system,
1188        2, 2, 0, /*
1189 Set PROCESS's output coding system to CODESYS.
1190 */
1191        (process, codesys))
1192 {
1193   codesys = Fget_coding_system (codesys);
1194   process = get_process (process);
1195   CHECK_LIVE_PROCESS (process);
1196
1197   set_encoding_stream_coding_system
1198     (XLSTREAM (XPROCESS (process)->coding_outstream), codesys);
1199   return Qnil;
1200 }
1201
1202 DEFUN ("set-process-coding-system", Fset_process_coding_system,
1203        1, 3, 0, /*
1204 Set coding-systems of PROCESS to DECODING and ENCODING.
1205 DECODING will be used to decode subprocess output and ENCODING to
1206 encode subprocess input.
1207 */
1208        (process, decoding, encoding))
1209 {
1210   if (!NILP (decoding))
1211     Fset_process_input_coding_system (process, decoding);
1212
1213   if (!NILP (encoding))
1214     Fset_process_output_coding_system (process, encoding);
1215
1216   return Qnil;
1217 }
1218
1219 #endif /* FILE_CODING */
1220 \f
1221 /************************************************************************/
1222 /*                             process status                           */
1223 /************************************************************************/
1224
1225 static Lisp_Object
1226 exec_sentinel_unwind (Lisp_Object datum)
1227 {
1228   Lisp_Cons *d = XCONS (datum);
1229   XPROCESS (d->car)->sentinel = d->cdr;
1230   free_cons (d);
1231   return Qnil;
1232 }
1233
1234 static void
1235 exec_sentinel (Lisp_Object process, Lisp_Object reason)
1236 {
1237   /* This function can GC */
1238   int speccount = specpdl_depth ();
1239   Lisp_Process *p = XPROCESS (process);
1240   Lisp_Object sentinel = p->sentinel;
1241
1242   if (NILP (sentinel))
1243     return;
1244
1245   /* Some weird FSFmacs crap here with
1246      Vdeactivate_mark and current_buffer->keymap */
1247
1248   /* Zilch the sentinel while it's running, to avoid recursive invocations;
1249      assure that it gets restored no matter how the sentinel exits.  */
1250   p->sentinel = Qnil;
1251   record_unwind_protect (exec_sentinel_unwind, noseeum_cons (process, sentinel));
1252   /* We used to bind inhibit-quit to t here, but call2_trapping_errors()
1253      does that for us. */
1254   running_asynch_code = 1;
1255   call2_trapping_errors ("Error in process sentinel", sentinel, process, reason);
1256   running_asynch_code = 0;
1257   restore_match_data ();
1258   unbind_to (speccount, Qnil);
1259 }
1260
1261 DEFUN ("set-process-sentinel", Fset_process_sentinel, 2, 2, 0, /*
1262 Give PROCESS the sentinel SENTINEL; nil for none.
1263 The sentinel is called as a function when the process changes state.
1264 It gets two arguments: the process, and a string describing the change.
1265 */
1266        (process, sentinel))
1267 {
1268   CHECK_PROCESS (process);
1269   XPROCESS (process)->sentinel = sentinel;
1270   return sentinel;
1271 }
1272
1273 DEFUN ("process-sentinel", Fprocess_sentinel, 1, 1, 0, /*
1274 Return the sentinel of PROCESS; nil if none.
1275 See `set-process-sentinel' for more info on sentinels.
1276 */
1277        (process))
1278 {
1279   CHECK_PROCESS (process);
1280   return XPROCESS (process)->sentinel;
1281 }
1282
1283 \f
1284 const char *
1285 signal_name (int signum)
1286 {
1287   if (signum >= 0 && signum < NSIG)
1288     return (const char *) sys_siglist[signum];
1289
1290   return (const char *) GETTEXT ("unknown signal");
1291 }
1292
1293 void
1294 update_process_status (Lisp_Object p,
1295                        Lisp_Object status_symbol,
1296                        int exit_code,
1297                        int core_dumped)
1298 {
1299   XPROCESS (p)->tick++;
1300   process_tick++;
1301   XPROCESS (p)->status_symbol = status_symbol;
1302   XPROCESS (p)->exit_code = exit_code;
1303   XPROCESS (p)->core_dumped = core_dumped;
1304 }
1305
1306 /* Return a string describing a process status list.  */
1307
1308 static Lisp_Object
1309 status_message (Lisp_Process *p)
1310 {
1311   Lisp_Object symbol = p->status_symbol;
1312   int code = p->exit_code;
1313   int coredump = p->core_dumped;
1314   Lisp_Object string, string2;
1315
1316   if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
1317     {
1318       string = build_string (signal_name (code));
1319       if (coredump)
1320         string2 = build_translated_string (" (core dumped)\n");
1321       else
1322         string2 = build_string ("\n");
1323       set_string_char (XSTRING (string), 0,
1324                        DOWNCASE (current_buffer,
1325                                  string_char (XSTRING (string), 0)));
1326       return concat2 (string, string2);
1327     }
1328   else if (EQ (symbol, Qexit))
1329     {
1330       if (code == 0)
1331         return build_translated_string ("finished\n");
1332       string = Fnumber_to_string (make_int (code));
1333       if (coredump)
1334         string2 = build_translated_string (" (core dumped)\n");
1335       else
1336         string2 = build_string ("\n");
1337       return concat2 (build_translated_string ("exited abnormally with code "),
1338                       concat2 (string, string2));
1339     }
1340   else
1341     return Fcopy_sequence (Fsymbol_name (symbol));
1342 }
1343
1344 /* Tell status_notify() to check for terminated processes.  We do this
1345    because on some systems we sometimes miss SIGCHLD calls. (Not sure
1346    why.) */
1347
1348 void
1349 kick_status_notify (void)
1350 {
1351   process_tick++;
1352 }
1353
1354
1355 /* Report all recent events of a change in process status
1356    (either run the sentinel or output a message).
1357    This is done while Emacs is waiting for keyboard input.  */
1358
1359 void
1360 status_notify (void)
1361 {
1362   /* This function can GC */
1363   Lisp_Object tail = Qnil;
1364   Lisp_Object symbol = Qnil;
1365   Lisp_Object msg = Qnil;
1366   struct gcpro gcpro1, gcpro2, gcpro3;
1367   /* process_tick is volatile, so we have to remember it now.
1368      Otherwise, we get a race condition if SIGCHLD happens during
1369      this function.
1370
1371      (Actually, this is not the case anymore.  The code to
1372      update the process structures has been moved out of the
1373      SIGCHLD handler.  But for the moment I'm leaving this
1374      stuff in -- it can't hurt.) */
1375   int temp_process_tick;
1376
1377   MAYBE_PROCMETH (reap_exited_processes, ());
1378
1379   temp_process_tick = process_tick;
1380
1381   if (update_tick == temp_process_tick)
1382     return;
1383
1384   /* We need to gcpro tail; if read_process_output calls a filter
1385      which deletes a process and removes the cons to which tail points
1386      from Vprocess_alist, and then causes a GC, tail is an unprotected
1387      reference.  */
1388   GCPRO3 (tail, symbol, msg);
1389
1390   for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
1391     {
1392       Lisp_Object process = XCAR (tail);
1393       Lisp_Process *p = XPROCESS (process);
1394       /* p->tick is also volatile.  Same thing as above applies. */
1395       int this_process_tick;
1396
1397       /* #### extra check for terminated processes, in case a SIGCHLD
1398          got missed (this seems to happen sometimes, I'm not sure why).
1399        */
1400       if (INTP (p->pid))
1401         MAYBE_PROCMETH (update_status_if_terminated, (p));
1402
1403       this_process_tick = p->tick;
1404       if (this_process_tick != p->update_tick)
1405         {
1406           p->update_tick = this_process_tick;
1407
1408           /* If process is still active, read any output that remains.  */
1409           while (!EQ (p->filter, Qt)
1410                  && read_process_output (process) > 0)
1411             ;
1412
1413           /* Get the text to use for the message.  */
1414           msg = status_message (p);
1415
1416           /* If process is terminated, deactivate it or delete it.  */
1417           symbol = p->status_symbol;
1418
1419           if (EQ (symbol, Qsignal)
1420               || EQ (symbol, Qexit))
1421             {
1422               if (delete_exited_processes)
1423                 remove_process (process);
1424               else
1425                 deactivate_process (process);
1426             }
1427
1428           /* Now output the message suitably.  */
1429           if (!NILP (p->sentinel))
1430             exec_sentinel (process, msg);
1431           /* Don't bother with a message in the buffer
1432              when a process becomes runnable.  */
1433           else if (!EQ (symbol, Qrun) && !NILP (p->buffer))
1434             {
1435               Lisp_Object old_read_only = Qnil;
1436               Lisp_Object old = Fcurrent_buffer ();
1437               Bufpos opoint;
1438               struct gcpro ngcpro1, ngcpro2;
1439
1440               /* Avoid error if buffer is deleted
1441                  (probably that's why the process is dead, too) */
1442               if (!BUFFER_LIVE_P (XBUFFER (p->buffer)))
1443                 continue;
1444
1445               NGCPRO2 (old, old_read_only);
1446               Fset_buffer (p->buffer);
1447               opoint = BUF_PT (current_buffer);
1448               /* Insert new output into buffer
1449                  at the current end-of-output marker,
1450                  thus preserving logical ordering of input and output.  */
1451               if (XMARKER (p->mark)->buffer)
1452                 BUF_SET_PT (current_buffer, marker_position (p->mark));
1453               else
1454                 BUF_SET_PT (current_buffer, BUF_ZV (current_buffer));
1455               if (BUF_PT (current_buffer) <= opoint)
1456                 opoint += (string_char_length (XSTRING (msg))
1457                            + string_char_length (XSTRING (p->name))
1458                            + 10);
1459
1460               old_read_only = current_buffer->read_only;
1461               current_buffer->read_only = Qnil;
1462               buffer_insert_c_string (current_buffer, "\nProcess ");
1463               Finsert (1, &p->name);
1464               buffer_insert_c_string (current_buffer, " ");
1465               Finsert (1, &msg);
1466               current_buffer->read_only = old_read_only;
1467               Fset_marker (p->mark, make_int (BUF_PT (current_buffer)),
1468                            p->buffer);
1469
1470               opoint = bufpos_clip_to_bounds(BUF_BEGV (XBUFFER (p->buffer)),
1471                                              opoint,
1472                                              BUF_ZV (XBUFFER (p->buffer)));
1473               BUF_SET_PT (current_buffer, opoint);
1474               Fset_buffer (old);
1475               NUNGCPRO;
1476             }
1477         }
1478     } /* end for */
1479
1480   /* in case buffers use %s in modeline-format */
1481   MARK_MODELINE_CHANGED;
1482   redisplay ();
1483
1484   update_tick = temp_process_tick;
1485
1486   UNGCPRO;
1487 }
1488
1489 DEFUN ("process-status", Fprocess_status, 1, 1, 0, /*
1490 Return the status of PROCESS.
1491 This is a symbol, one of these:
1492
1493 run    -- for a process that is running.
1494 stop   -- for a process stopped but continuable.
1495 exit   -- for a process that has exited.
1496 signal -- for a process that has got a fatal signal.
1497 open   -- for a network stream connection that is open.
1498 closed -- for a network stream connection that is closed.
1499 nil    -- if arg is a process name and no such process exists.
1500
1501 PROCESS may be a process, a buffer, the name of a process or buffer, or
1502 nil, indicating the current buffer's process.
1503 */
1504        (process))
1505 {
1506   Lisp_Object status_symbol;
1507
1508   if (STRINGP (process))
1509     process = Fget_process (process);
1510   else
1511     process = get_process (process);
1512
1513   if (NILP (process))
1514     return Qnil;
1515
1516   status_symbol = XPROCESS (process)->status_symbol;
1517   if (network_connection_p (process))
1518     {
1519       if (EQ (status_symbol, Qrun))
1520         status_symbol = Qopen;
1521       else if (EQ (status_symbol, Qexit))
1522         status_symbol = Qclosed;
1523     }
1524   return status_symbol;
1525 }
1526
1527 DEFUN ("process-exit-status", Fprocess_exit_status, 1, 1, 0, /*
1528 Return the exit status of PROCESS or the signal number that killed it.
1529 If PROCESS has not yet exited or died, return 0.
1530 */
1531        (process))
1532 {
1533   CHECK_PROCESS (process);
1534   return make_int (XPROCESS (process)->exit_code);
1535 }
1536
1537 \f
1538
1539 static int
1540 decode_signal (Lisp_Object signal_)
1541 {
1542   if (INTP (signal_))
1543     return XINT (signal_);
1544   else
1545     {
1546       Bufbyte *name;
1547
1548       CHECK_SYMBOL (signal_);
1549       name = string_data (XSYMBOL (signal_)->name);
1550
1551 #define handle_signal(sym) do {                         \
1552         if (!strcmp ((const char *) name, #sym))        \
1553           return sym;                                   \
1554       } while (0)
1555
1556       handle_signal (SIGINT);  /* ANSI */
1557       handle_signal (SIGILL);  /* ANSI */
1558       handle_signal (SIGABRT); /* ANSI */
1559       handle_signal (SIGFPE);  /* ANSI */
1560       handle_signal (SIGSEGV); /* ANSI */
1561       handle_signal (SIGTERM); /* ANSI */
1562
1563 #ifdef SIGHUP
1564       handle_signal (SIGHUP);  /* POSIX */
1565 #endif
1566 #ifdef SIGQUIT
1567       handle_signal (SIGQUIT); /* POSIX */
1568 #endif
1569 #ifdef SIGTRAP
1570       handle_signal (SIGTRAP); /* POSIX */
1571 #endif
1572 #ifdef SIGKILL
1573       handle_signal (SIGKILL); /* POSIX */
1574 #endif
1575 #ifdef SIGUSR1
1576       handle_signal (SIGUSR1); /* POSIX */
1577 #endif
1578 #ifdef SIGUSR2
1579       handle_signal (SIGUSR2); /* POSIX */
1580 #endif
1581 #ifdef SIGPIPE
1582       handle_signal (SIGPIPE); /* POSIX */
1583 #endif
1584 #ifdef SIGALRM
1585       handle_signal (SIGALRM); /* POSIX */
1586 #endif
1587 #ifdef SIGCHLD
1588       handle_signal (SIGCHLD); /* POSIX */
1589 #endif
1590 #ifdef SIGCONT
1591       handle_signal (SIGCONT); /* POSIX */
1592 #endif
1593 #ifdef SIGSTOP
1594       handle_signal (SIGSTOP); /* POSIX */
1595 #endif
1596 #ifdef SIGTSTP
1597       handle_signal (SIGTSTP); /* POSIX */
1598 #endif
1599 #ifdef SIGTTIN
1600       handle_signal (SIGTTIN); /* POSIX */
1601 #endif
1602 #ifdef SIGTTOU
1603       handle_signal (SIGTTOU); /* POSIX */
1604 #endif
1605
1606 #ifdef SIGBUS
1607       handle_signal (SIGBUS);  /* XPG5 */
1608 #endif
1609 #ifdef SIGPOLL
1610       handle_signal (SIGPOLL); /* XPG5 */
1611 #endif
1612 #ifdef SIGPROF
1613       handle_signal (SIGPROF); /* XPG5 */
1614 #endif
1615 #ifdef SIGSYS
1616       handle_signal (SIGSYS);  /* XPG5 */
1617 #endif
1618 #ifdef SIGURG
1619       handle_signal (SIGURG);  /* XPG5 */
1620 #endif
1621 #ifdef SIGXCPU
1622       handle_signal (SIGXCPU); /* XPG5 */
1623 #endif
1624 #ifdef SIGXFSZ
1625       handle_signal (SIGXFSZ); /* XPG5 */
1626 #endif
1627 #ifdef SIGVTALRM
1628       handle_signal (SIGVTALRM); /* XPG5 */
1629 #endif
1630
1631 #ifdef SIGIO
1632       handle_signal (SIGIO); /* BSD 4.2 */
1633 #endif
1634 #ifdef SIGWINCH
1635       handle_signal (SIGWINCH); /* BSD 4.3 */
1636 #endif
1637
1638 #ifdef SIGEMT
1639       handle_signal (SIGEMT);
1640 #endif
1641 #ifdef SIGINFO
1642       handle_signal (SIGINFO);
1643 #endif
1644 #ifdef SIGHWE
1645       handle_signal (SIGHWE);
1646 #endif
1647 #ifdef SIGPRE
1648       handle_signal (SIGPRE);
1649 #endif
1650 #ifdef SIGUME
1651       handle_signal (SIGUME);
1652 #endif
1653 #ifdef SIGDLK
1654       handle_signal (SIGDLK);
1655 #endif
1656 #ifdef SIGCPULIM
1657       handle_signal (SIGCPULIM);
1658 #endif
1659 #ifdef SIGIOT
1660       handle_signal (SIGIOT);
1661 #endif
1662 #ifdef SIGLOST
1663       handle_signal (SIGLOST);
1664 #endif
1665 #ifdef SIGSTKFLT
1666       handle_signal (SIGSTKFLT);
1667 #endif
1668 #ifdef SIGUNUSED
1669       handle_signal (SIGUNUSED);
1670 #endif
1671 #ifdef SIGDANGER
1672       handle_signal (SIGDANGER); /* AIX */
1673 #endif
1674 #ifdef SIGMSG
1675       handle_signal (SIGMSG);
1676 #endif
1677 #ifdef SIGSOUND
1678       handle_signal (SIGSOUND);
1679 #endif
1680 #ifdef SIGRETRACT
1681       handle_signal (SIGRETRACT);
1682 #endif
1683 #ifdef SIGGRANT
1684       handle_signal (SIGGRANT);
1685 #endif
1686 #ifdef SIGPWR
1687       handle_signal (SIGPWR);
1688 #endif
1689
1690 #undef handle_signal
1691
1692       error ("Undefined signal name %s", name);
1693       return 0; /* Unreached */
1694     }
1695 }
1696
1697 /* Send signal number SIGNO to PROCESS.
1698    CURRENT-GROUP non-nil means send signal to the current
1699    foreground process group of the process's controlling terminal rather
1700    than to the process's own process group.
1701    This is used for various commands in shell mode.
1702    If NOMSG is zero, insert signal-announcements into process's buffers
1703    right away.
1704
1705    If we can, we try to signal PROCESS by sending control characters
1706    down the pty.  This allows us to signal inferiors who have changed
1707    their uid, for which kill() would return an EPERM error, or to
1708    processes running on another computer through a remote login.  */
1709
1710 static void
1711 process_send_signal (Lisp_Object process, int signo,
1712                      int current_group, int nomsg)
1713 {
1714   /* This function can GC */
1715   process = get_process (process);
1716
1717   if (network_connection_p (process))
1718     error ("Network connection %s is not a subprocess",
1719            XSTRING_DATA (XPROCESS(process)->name));
1720   CHECK_LIVE_PROCESS (process);
1721
1722   MAYBE_PROCMETH (kill_child_process, (process, signo, current_group, nomsg));
1723 }
1724
1725 DEFUN ("process-send-signal", Fprocess_send_signal, 1, 3, 0, /*
1726 Send signal SIGNAL to process PROCESS.
1727 SIGNAL may be an integer, or a symbol naming a signal, like `SIGSEGV'.
1728 PROCESS may be a process, a buffer, the name of a process or buffer, or
1729 nil, indicating the current buffer's process.
1730 Third arg CURRENT-GROUP non-nil means send signal to the current
1731 foreground process group of the process's controlling terminal rather
1732 than to the process's own process group.
1733 If the process is a shell that supports job control, this means
1734 send the signal to the current subjob rather than the shell.
1735 */
1736        (signal_, process, current_group))
1737 {
1738   /* This function can GC */
1739   process_send_signal (process, decode_signal (signal_),
1740                        !NILP (current_group), 0);
1741   return process;
1742 }
1743
1744 DEFUN ("interrupt-process", Finterrupt_process, 0, 2, 0, /*
1745 Interrupt process PROCESS.
1746 See function `process-send-signal' for more details on usage.
1747 */
1748        (process, current_group))
1749 {
1750   /* This function can GC */
1751   process_send_signal (process, SIGINT, !NILP (current_group), 0);
1752   return process;
1753 }
1754
1755 DEFUN ("kill-process", Fkill_process, 0, 2, 0, /*
1756 Kill process PROCESS.
1757 See function `process-send-signal' for more details on usage.
1758 */
1759        (process, current_group))
1760 {
1761   /* This function can GC */
1762 #ifdef SIGKILL
1763   process_send_signal (process, SIGKILL, !NILP (current_group), 0);
1764 #else
1765   error ("kill-process: Not supported on this system");
1766 #endif
1767   return process;
1768 }
1769
1770 DEFUN ("quit-process", Fquit_process, 0, 2, 0, /*
1771 Send QUIT signal to process PROCESS.
1772 See function `process-send-signal' for more details on usage.
1773 */
1774        (process, current_group))
1775 {
1776   /* This function can GC */
1777 #ifdef SIGQUIT
1778   process_send_signal (process, SIGQUIT, !NILP (current_group), 0);
1779 #else
1780   error ("quit-process: Not supported on this system");
1781 #endif
1782   return process;
1783 }
1784
1785 DEFUN ("stop-process", Fstop_process, 0, 2, 0, /*
1786 Stop process PROCESS.
1787 See function `process-send-signal' for more details on usage.
1788 */
1789        (process, current_group))
1790 {
1791   /* This function can GC */
1792 #ifdef SIGTSTP
1793   process_send_signal (process, SIGTSTP, !NILP (current_group), 0);
1794 #else
1795   error ("stop-process: Not supported on this system");
1796 #endif
1797   return process;
1798 }
1799
1800 DEFUN ("continue-process", Fcontinue_process, 0, 2, 0, /*
1801 Continue process PROCESS.
1802 See function `process-send-signal' for more details on usage.
1803 */
1804        (process, current_group))
1805 {
1806   /* This function can GC */
1807 #ifdef SIGCONT
1808   process_send_signal (process, SIGCONT, !NILP (current_group), 0);
1809 #else
1810   error ("continue-process: Not supported on this system");
1811 #endif
1812   return process;
1813 }
1814
1815 DEFUN ("signal-process", Fsignal_process, 2, 2,
1816        "nProcess number: \nnSignal code: ", /*
1817 Send the process with process id PID the signal with code SIGNAL.
1818 PID must be an integer.  The process need not be a child of this Emacs.
1819 SIGNAL may be an integer, or a symbol naming a signal, like `SIGSEGV'.
1820 */
1821        (pid, signal_))
1822 {
1823   CHECK_INT (pid);
1824
1825   return make_int (PROCMETH_OR_GIVEN (kill_process_by_pid,
1826                                       (XINT (pid), decode_signal (signal_)),
1827                                       -1));
1828 }
1829
1830 DEFUN ("process-send-eof", Fprocess_send_eof, 0, 1, 0, /*
1831 Make PROCESS see end-of-file in its input.
1832 PROCESS may be a process, a buffer, the name of a process or buffer, or
1833 nil, indicating the current buffer's process.
1834 If PROCESS is a network connection, or is a process communicating
1835 through a pipe (as opposed to a pty), then you cannot send any more
1836 text to PROCESS after you call this function.
1837 */
1838        (process))
1839 {
1840   /* This function can GC */
1841   process = get_process (process);
1842
1843   /* Make sure the process is really alive.  */
1844   if (! EQ (XPROCESS (process)->status_symbol, Qrun))
1845     error ("Process %s not running", XSTRING_DATA (XPROCESS (process)->name));
1846
1847   if (!MAYBE_INT_PROCMETH (process_send_eof, (process)))
1848     {
1849       if (!NILP (DATA_OUTSTREAM (XPROCESS (process))))
1850         {
1851           Lstream_close (XLSTREAM (DATA_OUTSTREAM (XPROCESS (process))));
1852           event_stream_delete_stream_pair (Qnil, XPROCESS (process)->pipe_outstream);
1853           XPROCESS (process)->pipe_outstream = Qnil;
1854 #ifdef FILE_CODING
1855           XPROCESS (process)->coding_outstream = Qnil;
1856 #endif
1857         }
1858     }
1859
1860   return process;
1861 }
1862
1863 \f
1864 /************************************************************************/
1865 /*                          deleting a process                          */
1866 /************************************************************************/
1867
1868 void
1869 deactivate_process (Lisp_Object process)
1870 {
1871   Lisp_Process *p = XPROCESS (process);
1872   USID usid;
1873
1874   /* It's possible that we got as far in the process-creation
1875      process as creating the descriptors but didn't get so
1876      far as selecting the process for input.  In this
1877      case, p->pid is nil: p->pid is set at the same time that
1878      the process is selected for input. */
1879   /* #### The comment does not look correct. event_stream_unselect_process
1880      is guarded by process->selected, so this is not a problem. - kkm*/
1881   /* Must call this before setting the streams to nil */
1882   event_stream_unselect_process (p);
1883
1884   if (!NILP (DATA_OUTSTREAM (p)))
1885     Lstream_close (XLSTREAM (DATA_OUTSTREAM (p)));
1886   if (!NILP (DATA_INSTREAM (p)))
1887     Lstream_close (XLSTREAM (DATA_INSTREAM (p)));
1888
1889   /* Provide minimal implementation for deactivate_process
1890      if there's no process-specific one */
1891   if (HAS_PROCMETH_P (deactivate_process))
1892     usid = PROCMETH (deactivate_process, (p));
1893   else
1894     usid = event_stream_delete_stream_pair (p->pipe_instream,
1895                                             p->pipe_outstream);
1896
1897   if (usid != USID_DONTHASH)
1898     remhash ((const void*)usid, usid_to_process);
1899
1900   p->pipe_instream = Qnil;
1901   p->pipe_outstream = Qnil;
1902 #ifdef FILE_CODING
1903   p->coding_instream = Qnil;
1904   p->coding_outstream = Qnil;
1905 #endif
1906 }
1907
1908 static void
1909 remove_process (Lisp_Object process)
1910 {
1911   Vprocess_list = delq_no_quit (process, Vprocess_list);
1912   Fset_marker (XPROCESS (process)->mark, Qnil, Qnil);
1913
1914   deactivate_process (process);
1915 }
1916
1917 DEFUN ("delete-process", Fdelete_process, 1, 1, 0, /*
1918 Delete PROCESS: kill it and forget about it immediately.
1919 PROCESS may be a process or the name of one, or a buffer name.
1920 */
1921        (process))
1922 {
1923   /* This function can GC */
1924   Lisp_Process *p;
1925   process = get_process (process);
1926   p = XPROCESS (process);
1927   if (network_connection_p (process))
1928     {
1929       p->status_symbol = Qexit;
1930       p->exit_code = 0;
1931       p->core_dumped = 0;
1932       p->tick++;
1933       process_tick++;
1934     }
1935   else if (PROCESS_LIVE_P (p))
1936     {
1937       Fkill_process (process, Qnil);
1938       /* Do this now, since remove_process will make sigchld_handler do nothing.  */
1939       p->status_symbol = Qsignal;
1940       p->exit_code = SIGKILL;
1941       p->core_dumped = 0;
1942       p->tick++;
1943       process_tick++;
1944       status_notify ();
1945     }
1946   remove_process (process);
1947   return Qnil;
1948 }
1949
1950 /* Kill all processes associated with `buffer'.
1951  If `buffer' is nil, kill all processes  */
1952
1953 void
1954 kill_buffer_processes (Lisp_Object buffer)
1955 {
1956   LIST_LOOP_2 (process, Vprocess_list)
1957     if ((NILP (buffer) || EQ (XPROCESS (process)->buffer, buffer)))
1958       {
1959         if (network_connection_p (process))
1960           Fdelete_process (process);
1961         else if (PROCESS_LIVE_P (XPROCESS (process)))
1962           process_send_signal (process, SIGHUP, 0, 1);
1963       }
1964 }
1965
1966 DEFUN ("process-kill-without-query", Fprocess_kill_without_query, 1, 2, 0, /*
1967 Say no query needed if PROCESS is running when Emacs is exited.
1968 Optional second argument if non-nil says to require a query.
1969 Value is t if a query was formerly required.
1970 */
1971        (process, require_query_p))
1972 {
1973   int tem;
1974
1975   CHECK_PROCESS (process);
1976   tem = XPROCESS (process)->kill_without_query;
1977   XPROCESS (process)->kill_without_query = NILP (require_query_p);
1978
1979   return tem ? Qnil : Qt;
1980 }
1981
1982 DEFUN ("process-kill-without-query-p", Fprocess_kill_without_query_p, 1, 1, 0, /*
1983 Return t if PROCESS will be killed without query when emacs is exited.
1984 */
1985        (process))
1986 {
1987   CHECK_PROCESS (process);
1988   return XPROCESS (process)->kill_without_query ? Qt : Qnil;
1989 }
1990
1991 \f
1992 /* This is not named init_process in order to avoid a conflict with NS 3.3 */
1993 void
1994 init_xemacs_process (void)
1995 {
1996   MAYBE_PROCMETH (init_process, ());
1997
1998   Vprocess_list = Qnil;
1999
2000   if (usid_to_process)
2001     clrhash (usid_to_process);
2002   else
2003     usid_to_process = make_hash_table (32);
2004 }
2005
2006 #if 0
2007
2008 xxDEFUN ("process-connection", Fprocess_connection, 0, 1, 0, /*
2009 Return the connection type of `PROCESS'.  This can be nil (pipe),
2010 t or pty (pty) or stream (socket connection).
2011 */
2012          (process))
2013 {
2014   return XPROCESS (process)->type;
2015 }
2016
2017 #endif /* 0 */
2018
2019 void
2020 syms_of_process (void)
2021 {
2022   INIT_LRECORD_IMPLEMENTATION (process);
2023
2024   defsymbol (&Qprocessp, "processp");
2025   defsymbol (&Qprocess_live_p, "process-live-p");
2026   defsymbol (&Qrun, "run");
2027   defsymbol (&Qstop, "stop");
2028   defsymbol (&Qopen, "open");
2029   defsymbol (&Qclosed, "closed");
2030
2031   defsymbol (&Qtcp, "tcp");
2032   defsymbol (&Qudp, "udp");
2033
2034 #ifdef HAVE_MULTICAST
2035   defsymbol(&Qmulticast, "multicast"); /* Used for occasional warnings */
2036 #endif
2037
2038   DEFSUBR (Fprocessp);
2039   DEFSUBR (Fprocess_live_p);
2040   DEFSUBR (Fget_process);
2041   DEFSUBR (Fget_buffer_process);
2042   DEFSUBR (Fdelete_process);
2043   DEFSUBR (Fprocess_status);
2044   DEFSUBR (Fprocess_exit_status);
2045   DEFSUBR (Fprocess_id);
2046   DEFSUBR (Fprocess_name);
2047   DEFSUBR (Fprocess_tty_name);
2048   DEFSUBR (Fprocess_command);
2049   DEFSUBR (Fset_process_buffer);
2050   DEFSUBR (Fprocess_buffer);
2051   DEFSUBR (Fprocess_mark);
2052   DEFSUBR (Fset_process_filter);
2053   DEFSUBR (Fprocess_filter);
2054   DEFSUBR (Fset_process_window_size);
2055   DEFSUBR (Fset_process_sentinel);
2056   DEFSUBR (Fprocess_sentinel);
2057   DEFSUBR (Fprocess_kill_without_query);
2058   DEFSUBR (Fprocess_kill_without_query_p);
2059   DEFSUBR (Fprocess_list);
2060   DEFSUBR (Fstart_process_internal);
2061 #ifdef HAVE_SOCKETS
2062   DEFSUBR (Fopen_network_stream_internal);
2063 #ifdef HAVE_MULTICAST
2064   DEFSUBR (Fopen_multicast_group_internal);
2065 #endif /* HAVE_MULTICAST */
2066 #endif /* HAVE_SOCKETS */
2067   DEFSUBR (Fprocess_send_region);
2068   DEFSUBR (Fprocess_send_string);
2069   DEFSUBR (Fprocess_send_signal);
2070   DEFSUBR (Finterrupt_process);
2071   DEFSUBR (Fkill_process);
2072   DEFSUBR (Fquit_process);
2073   DEFSUBR (Fstop_process);
2074   DEFSUBR (Fcontinue_process);
2075   DEFSUBR (Fprocess_send_eof);
2076   DEFSUBR (Fsignal_process);
2077 /*  DEFSUBR (Fprocess_connection); */
2078 #ifdef FILE_CODING
2079   DEFSUBR (Fprocess_input_coding_system);
2080   DEFSUBR (Fprocess_output_coding_system);
2081   DEFSUBR (Fset_process_input_coding_system);
2082   DEFSUBR (Fset_process_output_coding_system);
2083   DEFSUBR (Fprocess_coding_system);
2084   DEFSUBR (Fset_process_coding_system);
2085 #endif /* FILE_CODING */
2086 }
2087
2088 void
2089 vars_of_process (void)
2090 {
2091   Fprovide (intern ("subprocesses"));
2092 #ifdef HAVE_SOCKETS
2093   Fprovide (intern ("network-streams"));
2094 #ifdef HAVE_MULTICAST
2095   Fprovide (intern ("multicast"));
2096 #endif /* HAVE_MULTICAST */
2097 #endif /* HAVE_SOCKETS */
2098   staticpro (&Vprocess_list);
2099
2100   DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes /*
2101 *Non-nil means delete processes immediately when they exit.
2102 nil means don't delete them until `list-processes' is run.
2103 */ );
2104
2105   delete_exited_processes = 1;
2106
2107   DEFVAR_CONST_LISP ("null-device", &Vnull_device /*
2108 Name of the null device, which differs from system to system.
2109 The null device is a filename that acts as a sink for arbitrary amounts of
2110 data, which is discarded, or as a source for a zero-length file.
2111 It is available on all the systems that we currently support, but with
2112 different names (typically either `/dev/null' or `nul').
2113
2114 Note that there is also a /dev/zero on most modern Unix versions (including
2115 Cygwin), which acts like /dev/null when used as a sink, but as a source
2116 it sends a non-ending stream of zero bytes.  It's used most often along
2117 with memory-mapping.  We don't provide a Lisp variable for this because
2118 the operations needing this are lower level than what ELisp programs
2119 typically do, and in any case no equivalent exists under native MS Windows.
2120 */ );
2121   Vnull_device = build_string (NULL_DEVICE);
2122
2123   DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type /*
2124 Control type of device used to communicate with subprocesses.
2125 Values are nil to use a pipe, or t or `pty' to use a pty.
2126 The value has no effect if the system has no ptys or if all ptys are busy:
2127 then a pipe is used in any case.
2128 The value takes effect when `start-process' is called.
2129 */ );
2130   Vprocess_connection_type = Qt;
2131
2132   DEFVAR_BOOL ("windowed-process-io", &windowed_process_io /*
2133 Enables input/output on standard handles of a windowed process.
2134 When this variable is nil (the default), XEmacs does not attempt to read
2135 standard output handle of a windowed process. Instead, the process is
2136 immediately marked as exited immediately upon successful launching. This is
2137 done because normal windowed processes do not use standard I/O, as they are
2138 not connected to any console.
2139
2140 When launching a specially crafted windowed process, which expects to be
2141 launched by XEmacs, or by other program which pipes its standard input and
2142 output, this variable must be set to non-nil, in which case XEmacs will
2143 treat this process just like a console process.
2144
2145 NOTE: You should never set this variable, only bind it.
2146
2147 Only Windows processes can be "windowed" or "console". This variable has no
2148 effect on UNIX processes, because all UNIX processes are "console".
2149 */ );
2150   windowed_process_io = 0;
2151
2152 #ifdef PROCESS_IO_BLOCKING
2153   DEFVAR_LISP ("network-stream-blocking-port-list", &network_stream_blocking_port_list /*
2154 List of port numbers or port names to set a blocking I/O mode with connection.
2155 Nil value means to set a default(non-blocking) I/O mode.
2156 The value takes effect when `open-network-stream-internal' is called.
2157 */ );
2158   network_stream_blocking_port_list = Qnil;
2159 #endif  /* PROCESS_IO_BLOCKING */
2160 }
2161
2162 #endif /* not NO_SUBPROCESSES */