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.
7 This file is part of XEmacs.
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
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
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. */
24 /* This file has been Mule-ized except for `start-process-internal',
25 `open-network-stream-internal' and `open-multicast-group-internal'. */
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) */
33 #if !defined (NO_SUBPROCESSES)
35 /* The entire file is within this conditional */
51 #include "file-coding.h"
57 #include "syssignal.h" /* Always include before systty.h */
61 Lisp_Object Qprocessp, Qprocess_live_p;
64 struct process_methods the_process_methods;
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) */
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;
77 Lisp_Object Qmulticast; /* Will be used for occasional warnings */
80 /* t means use pty, nil means use a pipe,
81 maybe other values to come. */
82 Lisp_Object Vprocess_connection_type;
84 /* Read comments to DEFVAR of this */
85 int windowed_process_io;
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 */
93 /* Number of events of change of status of a process. */
94 volatile int process_tick;
96 /* Number of events for which the user or sentinel has been notified. */
97 static int update_tick;
99 /* Nonzero means delete a process right away if it exits. */
100 int delete_exited_processes;
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;
106 /* List of process objects. */
107 Lisp_Object Vprocess_list;
109 extern Lisp_Object Vlisp_EXEC_SUFFIXES;
110 Lisp_Object Vnull_device;
115 mark_process (Lisp_Object obj)
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);
129 mark_object (proc->coding_instream);
130 mark_object (proc->coding_outstream);
132 return proc->status_symbol;
136 print_process (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
138 Lisp_Process *proc = XPROCESS (obj);
141 error ("printing unreadable object #<process %s>",
142 XSTRING_DATA (proc->name));
146 print_internal (proc->name, printcharfun, 0);
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);
163 #ifdef HAVE_WINDOW_SYSTEM
164 extern void debug_process_finalization (Lisp_Process *p);
165 #endif /* HAVE_WINDOW_SYSTEM */
168 finalize_process (void *header, int for_disksave)
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
176 debug_process_finalization (p);
178 #endif /* HAVE_WINDOW_SYSTEM */
182 MAYBE_PROCMETH (finalize_process_data, (p, for_disksave));
184 xfree (p->process_data);
188 DEFINE_LRECORD_IMPLEMENTATION ("process", process,
189 mark_process, print_process, finalize_process,
190 0, 0, 0, Lisp_Process);
192 /************************************************************************/
193 /* basic process accessors */
194 /************************************************************************/
196 /* Under FILE_CODING, this function returns low-level streams, connected
197 directly to the child process, rather than en/decoding FILE_CODING
200 get_process_streams (Lisp_Process *p, Lisp_Object *instr, Lisp_Object *outstr)
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;
210 get_process_from_usid (USID usid)
214 assert (usid != USID_ERROR && usid != USID_DONTHASH);
216 if (gethash ((const void*)usid, usid_to_process, &vval))
219 CVOID_TO_LISP (proc, vval);
220 return XPROCESS (proc);
227 get_process_selected_p (Lisp_Process *p)
233 set_process_selected_p (Lisp_Process *p, int selected_p)
235 p->selected = !!selected_p;
239 connected_via_filedesc_p (Lisp_Process *p)
241 return MAYBE_INT_PROCMETH (tooltalk_connection_p, (p));
246 network_connection_p (Lisp_Object process)
248 return CONSP (XPROCESS (process)->pid);
252 DEFUN ("processp", Fprocessp, 1, 1, 0, /*
253 Return t if OBJECT is a process.
257 return PROCESSP (obj) ? Qt : Qnil;
260 DEFUN ("process-live-p", Fprocess_live_p, 1, 1, 0, /*
261 Return t if OBJECT is a process that is alive.
265 return PROCESSP (obj) && PROCESS_LIVE_P (XPROCESS (obj)) ? Qt : Qnil;
268 DEFUN ("process-list", Fprocess_list, 0, 0, 0, /*
269 Return a list of all processes.
273 return Fcopy_sequence (Vprocess_list);
276 DEFUN ("get-process", Fget_process, 1, 1, 0, /*
277 Return the process named NAME, or nil if there is none.
287 /* this only gets called during GC when emacs is going away as a result
288 of a signal or crash. */
291 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
293 Lisp_Object proc = XCAR (tail);
295 if (internal_equal (name, XPROCESS (proc)->name, 0))
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.
307 Lisp_Object buf, tail, proc;
309 if (NILP (name)) return Qnil;
310 buf = Fget_buffer (name);
311 if (NILP (buf)) return Qnil;
313 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
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. */
320 if (PROCESSP (proc) && EQ (XPROCESS (proc)->buffer, buf))
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
332 get_process (Lisp_Object name)
334 Lisp_Object proc, obj;
337 /* #### Look more closely into translating process names. */
340 /* This may be called during a GC from process_send_signal() from
341 kill_buffer_processes() if emacs decides to abort(). */
347 obj = Fget_process (name);
349 obj = Fget_buffer (name);
351 error ("Process %s does not exist", XSTRING_DATA (name));
353 else if (NILP (name))
354 obj = Fcurrent_buffer ();
358 /* Now obj should be either a buffer object or a process object.
362 proc = Fget_buffer_process (obj);
364 error ("Buffer %s has no process", XSTRING_DATA (XBUFFER(obj)->name));
368 /* #### This was commented out. Although, simple
369 (kill-process 7 "qqq") resulted in a fatal error. - kkm */
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).
385 CHECK_PROCESS (proc);
387 pid = XPROCESS (proc)->pid;
388 if (network_connection_p (proc))
390 return Fcons (Fcar (pid), Fcdr (pid));
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.
402 CHECK_PROCESS (proc);
403 return XPROCESS (proc)->name;
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.
413 CHECK_PROCESS (proc);
414 return XPROCESS (proc)->command;
418 /************************************************************************/
419 /* creating a process */
420 /************************************************************************/
423 make_process_internal (Lisp_Object name)
425 Lisp_Object val, name1;
427 Lisp_Process *p = alloc_lcrecord_type (Lisp_Process, &lrecord_process);
429 /* If name is already in use, modify it until it is unused. */
434 Lisp_Object tem = Fget_process (name1);
437 sprintf (suffix, "<%d>", i);
438 name1 = concat2 (name, build_string (suffix));
447 p->mark = Fmake_marker ();
449 p->status_symbol = Qrun;
452 p->filter_does_read = 0;
453 p->kill_without_query = 0;
457 p->pipe_instream = Qnil;
458 p->pipe_outstream = Qnil;
460 p->coding_instream = Qnil;
461 p->coding_outstream = Qnil;
465 MAYBE_PROCMETH (alloc_process_data, (p));
467 XSETPROCESS (val, p);
469 Vprocess_list = Fcons (val, Vprocess_list);
474 init_process_io_handles (Lisp_Process *p, void* in, void* out, int flags)
476 USID usid = event_stream_create_stream_pair (in, out,
477 &p->pipe_instream, &p->pipe_outstream,
480 if (usid == USID_ERROR)
481 report_file_error ("Setting up communication with subprocess", Qnil);
483 if (usid != USID_DONTHASH)
485 Lisp_Object proc = Qnil;
486 XSETPROCESS (proc, p);
487 puthash ((const void*)usid, LISP_TO_VOID (proc), usid_to_process);
490 MAYBE_PROCMETH (init_process_io_handles, (p, in, out, flags));
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 */
506 create_process (Lisp_Object process, Lisp_Object *argv, int nargv,
507 Lisp_Object program, Lisp_Object cur_dir)
509 Lisp_Process *p = XPROCESS (process);
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;
517 pid = PROCMETH (create_process, (p, argv, nargv, program, cur_dir));
519 p->pid = make_int (pid);
520 if (PROCESS_LIVE_P (p))
521 event_stream_select_process (p);
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);
530 start_process_unwind (Lisp_Object proc)
532 /* Was PROC started successfully? */
533 if (EQ (XPROCESS (proc)->pid, Qnil))
534 remove_process (proc);
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
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
552 (int nargs, Lisp_Object *args))
554 /* This function can call lisp */
555 /* !!#### This function has not been Mule-ized */
556 Lisp_Object buffer, name, program, proc, current_dir;
558 int speccount = specpdl_depth ();
559 struct gcpro gcpro1, gcpro2, gcpro3;
566 /* Protect against various file handlers doing GCs below. */
567 GCPRO3 (buffer, program, current_dir);
570 buffer = Fget_buffer_create (buffer);
573 CHECK_STRING (program);
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.
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);
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));
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))))
599 struct gcpro ngcpro1;
603 locate_file (Vexec_path, program, Vlisp_EXEC_SUFFIXES, &tem, X_OK);
605 report_file_error ("Searching for program", list1 (program));
606 program = Fexpand_file_name (tem, Qnil);
611 /* we still need to canonicalize it and ensure it has the proper
613 struct gcpro ngcpro1;
617 locate_file (list1 (build_string ("")), program, Vlisp_EXEC_SUFFIXES,
620 report_file_error ("Searching for program", list1 (program));
625 if (!NILP (Ffile_directory_p (program)))
626 invalid_operation ("Specified program for new process is a directory",
629 proc = make_process_internal (name);
631 XPROCESS (proc)->buffer = buffer;
632 XPROCESS (proc)->command = Flist (nargs - 2,
635 /* Make the process marker point into the process buffer (if any). */
637 Fset_marker (XPROCESS (proc)->mark,
638 make_int (BUF_ZV (XBUFFER (buffer))), buffer);
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);
646 create_process (proc, args + 3, nargs - 3, program, current_dir);
649 return unbind_to (speccount, proc);
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.
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.
667 Both UNIX and Win32 support BSD sockets, and there are many extensions
668 available (Sockets 2 spec).
670 A todo is define a consistent set of properties abstracting a
671 network connection. -kkm
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 */
681 DEFUN ("open-network-stream-internal", Fopen_network_stream_internal, 4, 5,
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.
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
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.
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
707 (name, buffer, host, service, protocol))
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;
715 GCPRO5 (name, buffer, host, service, protocol);
721 CHECK_SYMBOL (protocol);
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,
729 buffer = Fget_buffer_create (buffer);
730 proc = make_process_internal (name);
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);
738 event_stream_select_process (XPROCESS (proc));
745 #ifdef HAVE_MULTICAST
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.
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
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)
763 (name, buffer, dest, port, ttl))
765 /* !!#### This function has not been Mule-ized */
766 /* This function can GC */
767 Lisp_Object proc = Qnil;
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,
779 buffer = Fget_buffer_create (buffer);
781 proc = make_process_internal (name);
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);
789 event_stream_select_process (XPROCESS (proc));
794 #endif /* HAVE_MULTICAST */
796 #endif /* HAVE_SOCKETS */
799 canonicalize_host_name (Lisp_Object host)
801 return PROCMETH_OR_GIVEN (canonicalize_host_name, (host), host);
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.
808 (proc, height, width))
810 CHECK_PROCESS (proc);
811 CHECK_NATNUM (height);
812 CHECK_NATNUM (width);
814 MAYBE_INT_PROCMETH (set_window_size, (XPROCESS (proc), XINT (height), XINT (width))) <= 0
819 /************************************************************************/
821 /************************************************************************/
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.
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. */
832 read_process_output (Lisp_Object proc)
834 /* This function can GC */
835 Bytecount nbytes, nchars;
837 Lisp_Object outstream;
838 Lisp_Process *p = XPROCESS (proc);
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 */
851 if (!NILP (p->filter) && (p->filter_does_read))
853 Lisp_Object filter_result;
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);
866 nbytes = Lstream_read (XLSTREAM (DATA_INSTREAM(p)), chars, sizeof (chars));
867 if (nbytes <= 0) return nbytes;
869 nchars = bytecount_to_charcount (chars, nbytes);
870 outstream = p->filter;
871 if (!NILP (outstream))
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 ();
883 /* If no filter, write into buffer if it isn't dead. */
884 if (!NILP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
886 Lisp_Object old_read_only = Qnil;
890 int old_zmacs_region_stays = zmacs_region_stays;
891 struct gcpro gcpro1, gcpro2;
892 struct buffer *buf = XBUFFER (p->buffer);
894 GCPRO2 (proc, old_read_only);
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;
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)
907 bufpos_clip_to_bounds (old_begv, marker_position (p->mark),
910 BUF_SET_PT (buf, old_zv);
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)))
918 /* Make sure opoint floats ahead of any new text, just as point
920 if (BUF_PT (buf) <= old_point)
923 /* Insert after old_begv, but before old_zv. */
924 if (BUF_PT (buf) < old_begv)
926 if (BUF_PT (buf) <= old_zv)
930 /* This screws up initial display of the window. jla */
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);
937 buffer_insert_raw_string (buf, chars, nbytes);
940 Fset_marker (p->mark, make_int (BUF_PT (buf)), p->buffer);
942 MARK_MODELINE_CHANGED;
944 /* If the restriction isn't what it should be, set it. */
945 if (old_begv != BUF_BEGV (buf) || old_zv != BUF_ZV (buf))
948 old_begv = bufpos_clip_to_bounds (BUF_BEG (buf),
951 old_zv = bufpos_clip_to_bounds (BUF_BEG (buf),
954 Fnarrow_to_region (make_int (old_begv), make_int (old_zv),
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),
964 BUF_SET_PT (buf, old_point);
971 /* Sending data to subprocess */
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.
978 Note that START and LEN are in Bufpos's if RELOCATABLE is a buffer,
979 and in Bytecounts otherwise. */
982 send_process (Lisp_Object proc,
983 Lisp_Object relocatable, const Bufbyte *nonrelocatable,
986 /* This function can GC */
987 struct gcpro gcpro1, gcpro2;
988 Lisp_Object lstream = Qnil;
990 GCPRO2 (proc, lstream);
992 if (NILP (DATA_OUTSTREAM (XPROCESS (proc))))
993 signal_simple_error ("Process not open for writing", proc);
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);
1002 lstream = make_lisp_string_input_stream (relocatable, start, len);
1004 PROCMETH (send_process, (proc, XLSTREAM (lstream)));
1007 Lstream_delete (XLSTREAM (lstream));
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.
1017 CHECK_PROCESS (proc);
1018 return MAYBE_LISP_PROCMETH (get_tty_name, (XPROCESS (proc)));
1021 DEFUN ("set-process-buffer", Fset_process_buffer, 2, 2, 0, /*
1022 Set buffer associated with PROCESS to BUFFER (a buffer, or nil).
1026 CHECK_PROCESS (proc);
1028 CHECK_BUFFER (buffer);
1029 XPROCESS (proc)->buffer = buffer;
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.
1040 CHECK_PROCESS (proc);
1041 return XPROCESS (proc)->buffer;
1044 DEFUN ("process-mark", Fprocess_mark, 1, 1, 0, /*
1045 Return the marker for the end of the last output from PROCESS.
1049 CHECK_PROCESS (proc);
1050 return XPROCESS (proc)->mark;
1054 set_process_filter (Lisp_Object proc, Lisp_Object filter, int filter_does_read)
1056 CHECK_PROCESS (proc);
1057 if (PROCESS_LIVE_P (XPROCESS (proc))) {
1058 if (EQ (filter, Qt))
1059 event_stream_unselect_process (XPROCESS (proc));
1061 event_stream_select_process (XPROCESS (proc));
1064 XPROCESS (proc)->filter = filter;
1065 XPROCESS (proc)->filter_does_read = filter_does_read;
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.
1078 set_process_filter (proc, filter, 0);
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.
1088 CHECK_PROCESS (proc);
1089 return XPROCESS (proc)->filter;
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.
1100 (process, start, end, buffer))
1102 /* This function can GC */
1103 Lisp_Object proc = get_process (process);
1105 struct buffer *buf = decode_buffer (buffer, 0);
1107 XSETBUFFER (buffer, buf);
1108 get_buffer_range_char (buf, start, end, &st, &en, 0);
1110 send_process (proc, buffer, 0, st, en - st);
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.
1122 (process, string, from, to))
1124 /* This function can GC */
1129 proc = get_process (process);
1130 CHECK_STRING (string);
1131 get_string_range_byte (string, from, to, &bfr, &bto,
1132 GB_HISTORICAL_STRING_BEHAVIOR);
1135 send_process (proc, string, 0, bfr, len);
1141 DEFUN ("process-input-coding-system", Fprocess_input_coding_system, 1, 1, 0, /*
1142 Return PROCESS's input coding system.
1146 process = get_process (process);
1147 CHECK_LIVE_PROCESS (process);
1148 return decoding_stream_coding_system (XLSTREAM (XPROCESS (process)->coding_instream) );
1151 DEFUN ("process-output-coding-system", Fprocess_output_coding_system, 1, 1, 0, /*
1152 Return PROCESS's output coding system.
1156 process = get_process (process);
1157 CHECK_LIVE_PROCESS (process);
1158 return encoding_stream_coding_system (XLSTREAM (XPROCESS (process)->coding_outstream));
1161 DEFUN ("process-coding-system", Fprocess_coding_system, 1, 1, 0, /*
1162 Return a pair of coding-system for decoding and encoding of PROCESS.
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)));
1174 DEFUN ("set-process-input-coding-system", Fset_process_input_coding_system,
1176 Set PROCESS's input coding system to CODESYS.
1180 codesys = Fget_coding_system (codesys);
1181 process = get_process (process);
1182 CHECK_LIVE_PROCESS (process);
1184 set_decoding_stream_coding_system
1185 (XLSTREAM (XPROCESS (process)->coding_instream), codesys);
1189 DEFUN ("set-process-output-coding-system", Fset_process_output_coding_system,
1191 Set PROCESS's output coding system to CODESYS.
1195 codesys = Fget_coding_system (codesys);
1196 process = get_process (process);
1197 CHECK_LIVE_PROCESS (process);
1199 set_encoding_stream_coding_system
1200 (XLSTREAM (XPROCESS (process)->coding_outstream), codesys);
1204 DEFUN ("set-process-coding-system", Fset_process_coding_system,
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.
1210 (process, decoding, encoding))
1212 if (!NILP (decoding))
1213 Fset_process_input_coding_system (process, decoding);
1215 if (!NILP (encoding))
1216 Fset_process_output_coding_system (process, encoding);
1221 #endif /* FILE_CODING */
1223 /************************************************************************/
1224 /* process status */
1225 /************************************************************************/
1228 exec_sentinel_unwind (Lisp_Object datum)
1230 Lisp_Cons *d = XCONS (datum);
1231 XPROCESS (d->car)->sentinel = d->cdr;
1237 exec_sentinel (Lisp_Object proc, Lisp_Object reason)
1239 /* This function can GC */
1240 int speccount = specpdl_depth ();
1241 Lisp_Process *p = XPROCESS (proc);
1242 Lisp_Object sentinel = p->sentinel;
1244 if (NILP (sentinel))
1247 /* Some weird FSFmacs crap here with
1248 Vdeactivate_mark and current_buffer->keymap */
1250 /* Zilch the sentinel while it's running, to avoid recursive invocations;
1251 assure that it gets restored no matter how the sentinel exits. */
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);
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.
1270 CHECK_PROCESS (proc);
1271 XPROCESS (proc)->sentinel = sentinel;
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.
1281 CHECK_PROCESS (proc);
1282 return XPROCESS (proc)->sentinel;
1287 signal_name (int signum)
1289 if (signum >= 0 && signum < NSIG)
1290 return (const char *) sys_siglist[signum];
1292 return (const char *) GETTEXT ("unknown signal");
1296 update_process_status (Lisp_Object p,
1297 Lisp_Object status_symbol,
1301 XPROCESS (p)->tick++;
1303 XPROCESS (p)->status_symbol = status_symbol;
1304 XPROCESS (p)->exit_code = exit_code;
1305 XPROCESS (p)->core_dumped = core_dumped;
1308 /* Return a string describing a process status list. */
1311 status_message (Lisp_Process *p)
1313 Lisp_Object symbol = p->status_symbol;
1314 int code = p->exit_code;
1315 int coredump = p->core_dumped;
1316 Lisp_Object string, string2;
1318 if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
1320 string = build_string (signal_name (code));
1322 string2 = build_translated_string (" (core dumped)\n");
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);
1330 else if (EQ (symbol, Qexit))
1333 return build_translated_string ("finished\n");
1334 string = Fnumber_to_string (make_int (code));
1336 string2 = build_translated_string (" (core dumped)\n");
1338 string2 = build_string ("\n");
1339 return concat2 (build_translated_string ("exited abnormally with code "),
1340 concat2 (string, string2));
1343 return Fcopy_sequence (Fsymbol_name (symbol));
1346 /* Tell status_notify() to check for terminated processes. We do this
1347 because on some systems we sometimes miss SIGCHLD calls. (Not sure
1351 kick_status_notify (void)
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. */
1362 status_notify (void)
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
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;
1379 MAYBE_PROCMETH (reap_exited_processes, ());
1381 temp_process_tick = process_tick;
1383 if (update_tick == temp_process_tick)
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
1390 GCPRO3 (tail, symbol, msg);
1392 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
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;
1399 /* #### extra check for terminated processes, in case a SIGCHLD
1400 got missed (this seems to happen sometimes, I'm not sure why).
1403 MAYBE_PROCMETH (update_status_if_terminated, (p));
1405 this_process_tick = p->tick;
1406 if (this_process_tick != p->update_tick)
1408 p->update_tick = this_process_tick;
1410 /* If process is still active, read any output that remains. */
1411 while (!EQ (p->filter, Qt)
1412 && read_process_output (proc) > 0)
1415 /* Get the text to use for the message. */
1416 msg = status_message (p);
1418 /* If process is terminated, deactivate it or delete it. */
1419 symbol = p->status_symbol;
1421 if (EQ (symbol, Qsignal)
1422 || EQ (symbol, Qexit))
1424 if (delete_exited_processes)
1425 remove_process (proc);
1427 deactivate_process (proc);
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))
1437 Lisp_Object old_read_only = Qnil;
1438 Lisp_Object old = Fcurrent_buffer ();
1440 struct gcpro ngcpro1, ngcpro2;
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)))
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));
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))
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, " ");
1468 current_buffer->read_only = old_read_only;
1469 Fset_marker (p->mark, make_int (BUF_PT (current_buffer)),
1472 opoint = bufpos_clip_to_bounds(BUF_BEGV (XBUFFER (p->buffer)),
1474 BUF_ZV (XBUFFER (p->buffer)));
1475 BUF_SET_PT (current_buffer, opoint);
1482 /* in case buffers use %s in modeline-format */
1483 MARK_MODELINE_CHANGED;
1486 update_tick = temp_process_tick;
1491 DEFUN ("process-status", Fprocess_status, 1, 1, 0, /*
1492 Return the status of PROCESS.
1493 This is a symbol, one of these:
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.
1503 PROCESS may be a process, a buffer, the name of a process or buffer, or
1504 nil, indicating the current buffer's process.
1508 Lisp_Object status_symbol;
1511 proc = Fget_process (proc);
1513 proc = get_process (proc);
1518 status_symbol = XPROCESS (proc)->status_symbol;
1519 if (network_connection_p (proc))
1521 if (EQ (status_symbol, Qrun))
1522 status_symbol = Qopen;
1523 else if (EQ (status_symbol, Qexit))
1524 status_symbol = Qclosed;
1526 return status_symbol;
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.
1535 CHECK_PROCESS (proc);
1536 return make_int (XPROCESS (proc)->exit_code);
1542 decode_signal (Lisp_Object signal_)
1545 return XINT (signal_);
1550 CHECK_SYMBOL (signal_);
1551 name = string_data (XSYMBOL (signal_)->name);
1553 #define handle_signal(sym) do { \
1554 if (!strcmp ((const char *) name, #sym)) \
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 */
1566 handle_signal (SIGHUP); /* POSIX */
1569 handle_signal (SIGQUIT); /* POSIX */
1572 handle_signal (SIGTRAP); /* POSIX */
1575 handle_signal (SIGKILL); /* POSIX */
1578 handle_signal (SIGUSR1); /* POSIX */
1581 handle_signal (SIGUSR2); /* POSIX */
1584 handle_signal (SIGPIPE); /* POSIX */
1587 handle_signal (SIGALRM); /* POSIX */
1590 handle_signal (SIGCHLD); /* POSIX */
1593 handle_signal (SIGCONT); /* POSIX */
1596 handle_signal (SIGSTOP); /* POSIX */
1599 handle_signal (SIGTSTP); /* POSIX */
1602 handle_signal (SIGTTIN); /* POSIX */
1605 handle_signal (SIGTTOU); /* POSIX */
1609 handle_signal (SIGBUS); /* XPG5 */
1612 handle_signal (SIGPOLL); /* XPG5 */
1615 handle_signal (SIGPROF); /* XPG5 */
1618 handle_signal (SIGSYS); /* XPG5 */
1621 handle_signal (SIGURG); /* XPG5 */
1624 handle_signal (SIGXCPU); /* XPG5 */
1627 handle_signal (SIGXFSZ); /* XPG5 */
1630 handle_signal (SIGVTALRM); /* XPG5 */
1634 handle_signal (SIGIO); /* BSD 4.2 */
1637 handle_signal (SIGWINCH); /* BSD 4.3 */
1641 handle_signal (SIGEMT);
1644 handle_signal (SIGINFO);
1647 handle_signal (SIGHWE);
1650 handle_signal (SIGPRE);
1653 handle_signal (SIGUME);
1656 handle_signal (SIGDLK);
1659 handle_signal (SIGCPULIM);
1662 handle_signal (SIGIOT);
1665 handle_signal (SIGLOST);
1668 handle_signal (SIGSTKFLT);
1671 handle_signal (SIGUNUSED);
1674 handle_signal (SIGDANGER); /* AIX */
1677 handle_signal (SIGMSG);
1680 handle_signal (SIGSOUND);
1683 handle_signal (SIGRETRACT);
1686 handle_signal (SIGGRANT);
1689 handle_signal (SIGPWR);
1692 #undef handle_signal
1694 error ("Undefined signal name %s", name);
1695 return 0; /* Unreached */
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
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. */
1713 process_send_signal (Lisp_Object process, int signo,
1714 int current_group, int nomsg)
1716 /* This function can GC */
1717 Lisp_Object proc = get_process (process);
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);
1724 MAYBE_PROCMETH (kill_child_process, (proc, signo, current_group, nomsg));
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.
1738 (signal_, process, current_group))
1740 /* This function can GC */
1741 process_send_signal (process, decode_signal (signal_),
1742 !NILP (current_group), 0);
1746 DEFUN ("interrupt-process", Finterrupt_process, 0, 2, 0, /*
1747 Interrupt process PROCESS.
1748 See function `process-send-signal' for more details on usage.
1750 (process, current_group))
1752 /* This function can GC */
1753 process_send_signal (process, SIGINT, !NILP (current_group), 0);
1757 DEFUN ("kill-process", Fkill_process, 0, 2, 0, /*
1758 Kill process PROCESS.
1759 See function `process-send-signal' for more details on usage.
1761 (process, current_group))
1763 /* This function can GC */
1765 process_send_signal (process, SIGKILL, !NILP (current_group), 0);
1767 error ("kill-process: Not supported on this system");
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.
1776 (process, current_group))
1778 /* This function can GC */
1780 process_send_signal (process, SIGQUIT, !NILP (current_group), 0);
1782 error ("quit-process: Not supported on this system");
1787 DEFUN ("stop-process", Fstop_process, 0, 2, 0, /*
1788 Stop process PROCESS.
1789 See function `process-send-signal' for more details on usage.
1791 (process, current_group))
1793 /* This function can GC */
1795 process_send_signal (process, SIGTSTP, !NILP (current_group), 0);
1797 error ("stop-process: Not supported on this system");
1802 DEFUN ("continue-process", Fcontinue_process, 0, 2, 0, /*
1803 Continue process PROCESS.
1804 See function `process-send-signal' for more details on usage.
1806 (process, current_group))
1808 /* This function can GC */
1810 process_send_signal (process, SIGCONT, !NILP (current_group), 0);
1812 error ("continue-process: Not supported on this system");
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'.
1827 return make_int (PROCMETH_OR_GIVEN (kill_process_by_pid,
1828 (XINT (pid), decode_signal (signal_)),
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.
1842 /* This function can GC */
1843 Lisp_Object proc = get_process (process);
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));
1849 if (!MAYBE_INT_PROCMETH (process_send_eof, (proc)))
1851 if (!NILP (DATA_OUTSTREAM (XPROCESS (proc))))
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;
1857 XPROCESS (proc)->coding_outstream = Qnil;
1866 /************************************************************************/
1867 /* deleting a process */
1868 /************************************************************************/
1871 deactivate_process (Lisp_Object proc)
1873 Lisp_Process *p = XPROCESS (proc);
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);
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)));
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));
1896 usid = event_stream_delete_stream_pair (p->pipe_instream,
1899 if (usid != USID_DONTHASH)
1900 remhash ((const void*)usid, usid_to_process);
1902 p->pipe_instream = Qnil;
1903 p->pipe_outstream = Qnil;
1905 p->coding_instream = Qnil;
1906 p->coding_outstream = Qnil;
1911 remove_process (Lisp_Object proc)
1913 Vprocess_list = delq_no_quit (proc, Vprocess_list);
1914 Fset_marker (XPROCESS (proc)->mark, Qnil, Qnil);
1916 deactivate_process (proc);
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.
1925 /* This function can GC */
1927 proc = get_process (proc);
1928 p = XPROCESS (proc);
1929 if (network_connection_p (proc))
1931 p->status_symbol = Qexit;
1937 else if (PROCESS_LIVE_P (p))
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;
1948 remove_process (proc);
1952 /* Kill all processes associated with `buffer'.
1953 If `buffer' is nil, kill all processes */
1956 kill_buffer_processes (Lisp_Object buffer)
1960 for (tail = Vprocess_list; CONSP (tail);
1963 Lisp_Object proc = XCAR (tail);
1965 && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer)))
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);
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.
1980 (proc, require_query_p))
1984 CHECK_PROCESS (proc);
1985 tem = XPROCESS (proc)->kill_without_query;
1986 XPROCESS (proc)->kill_without_query = NILP (require_query_p);
1988 return tem ? Qnil : Qt;
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.
1996 CHECK_PROCESS (proc);
1997 return XPROCESS (proc)->kill_without_query ? Qt : Qnil;
2001 /* This is not named init_process in order to avoid a conflict with NS 3.3 */
2003 init_xemacs_process (void)
2005 MAYBE_PROCMETH (init_process, ());
2007 Vprocess_list = Qnil;
2009 if (usid_to_process)
2010 clrhash (usid_to_process);
2012 usid_to_process = make_hash_table (32);
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).
2023 return XPROCESS (process)->type;
2029 syms_of_process (void)
2031 INIT_LRECORD_IMPLEMENTATION (process);
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");
2040 defsymbol (&Qtcp, "tcp");
2041 defsymbol (&Qudp, "udp");
2043 #ifdef HAVE_MULTICAST
2044 defsymbol(&Qmulticast, "multicast"); /* Used for occasional warnings */
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);
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); */
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 */
2098 vars_of_process (void)
2100 Fprovide (intern ("subprocesses"));
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);
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.
2114 delete_exited_processes = 1;
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').
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.
2130 Vnull_device = build_string (NULL_DEVICE);
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.
2139 Vprocess_connection_type = Qt;
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.
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.
2154 NOTE: You should never set this variable, only bind it.
2156 Only Windows processes can be "windowed" or "console". This variable has no
2157 effect on UNIX processes, because all UNIX processes are "console".
2159 windowed_process_io = 0;
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.
2167 network_stream_blocking_port_list = Qnil;
2168 #endif /* PROCESS_IO_BLOCKING */
2171 #endif /* not NO_SUBPROCESSES */