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