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