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