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 if (!NILP (Ffile_directory_p (program)))
612 error ("Specified program for new process is a directory");
615 proc = make_process_internal (name);
617 XPROCESS (proc)->buffer = buffer;
618 XPROCESS (proc)->command = Flist (nargs - 2,
621 /* Make the process marker point into the process buffer (if any). */
623 Fset_marker (XPROCESS (proc)->mark,
624 make_int (BUF_ZV (XBUFFER (buffer))), buffer);
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);
632 create_process (proc, args + 3, nargs - 3, program, current_dir);
635 return unbind_to (speccount, proc);
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.
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.
653 Both UNIX and Win32 support BSD sockets, and there are many extensions
654 available (Sockets 2 spec).
656 A todo is define a consistent set of properties abstracting a
657 network connection. -kkm
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 */
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.
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
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.
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
692 (name, buffer, host, service, protocol))
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;
700 GCPRO5 (name, buffer, host, service, protocol);
706 CHECK_SYMBOL (protocol);
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,
714 buffer = Fget_buffer_create (buffer);
715 proc = make_process_internal (name);
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);
723 event_stream_select_process (XPROCESS (proc));
730 #ifdef HAVE_MULTICAST
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.
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
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)
748 (name, buffer, dest, port, ttl))
750 /* !!#### This function has not been Mule-ized */
751 /* This function can GC */
752 Lisp_Object proc = Qnil;
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,
764 buffer = Fget_buffer_create (buffer);
766 proc = make_process_internal (name);
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);
774 event_stream_select_process (XPROCESS (proc));
779 #endif /* HAVE_MULTICAST */
781 #endif /* HAVE_SOCKETS */
784 canonicalize_host_name (Lisp_Object host)
786 return PROCMETH_OR_GIVEN (canonicalize_host_name, (host), host);
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.
793 (proc, height, width))
795 CHECK_PROCESS (proc);
796 CHECK_NATNUM (height);
797 CHECK_NATNUM (width);
799 MAYBE_INT_PROCMETH (set_window_size, (XPROCESS (proc), XINT (height), XINT (width))) <= 0
804 /************************************************************************/
806 /************************************************************************/
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.
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. */
817 read_process_output (Lisp_Object proc)
819 /* This function can GC */
820 Bytecount nbytes, nchars;
822 Lisp_Object outstream;
823 Lisp_Process *p = XPROCESS (proc);
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 */
836 if (!NILP (p->filter) && (p->filter_does_read))
838 Lisp_Object filter_result;
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);
851 nbytes = Lstream_read (XLSTREAM (DATA_INSTREAM(p)), chars, sizeof (chars));
852 if (nbytes <= 0) return nbytes;
854 nchars = bytecount_to_charcount (chars, nbytes);
855 outstream = p->filter;
856 if (!NILP (outstream))
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 ();
868 /* If no filter, write into buffer if it isn't dead. */
869 if (!NILP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
871 Lisp_Object old_read_only = Qnil;
875 int old_zmacs_region_stays = zmacs_region_stays;
876 struct gcpro gcpro1, gcpro2;
877 struct buffer *buf = XBUFFER (p->buffer);
879 GCPRO2 (proc, old_read_only);
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;
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)
892 bufpos_clip_to_bounds (old_begv, marker_position (p->mark),
895 BUF_SET_PT (buf, old_zv);
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)))
903 /* Make sure opoint floats ahead of any new text, just as point
905 if (BUF_PT (buf) <= old_point)
908 /* Insert after old_begv, but before old_zv. */
909 if (BUF_PT (buf) < old_begv)
911 if (BUF_PT (buf) <= old_zv)
915 /* This screws up initial display of the window. jla */
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);
922 buffer_insert_raw_string (buf, chars, nbytes);
925 Fset_marker (p->mark, make_int (BUF_PT (buf)), p->buffer);
927 MARK_MODELINE_CHANGED;
929 /* If the restriction isn't what it should be, set it. */
930 if (old_begv != BUF_BEGV (buf) || old_zv != BUF_ZV (buf))
933 old_begv = bufpos_clip_to_bounds (BUF_BEG (buf),
936 old_zv = bufpos_clip_to_bounds (BUF_BEG (buf),
939 Fnarrow_to_region (make_int (old_begv), make_int (old_zv),
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),
949 BUF_SET_PT (buf, old_point);
956 /* Sending data to subprocess */
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.
963 Note that START and LEN are in Bufpos's if RELOCATABLE is a buffer,
964 and in Bytecounts otherwise. */
967 send_process (Lisp_Object proc,
968 Lisp_Object relocatable, const Bufbyte *nonrelocatable,
971 /* This function can GC */
972 struct gcpro gcpro1, gcpro2;
973 Lisp_Object lstream = Qnil;
975 GCPRO2 (proc, lstream);
977 if (NILP (DATA_OUTSTREAM (XPROCESS (proc))))
978 signal_simple_error ("Process not open for writing", proc);
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);
987 lstream = make_lisp_string_input_stream (relocatable, start, len);
989 PROCMETH (send_process, (proc, XLSTREAM (lstream)));
992 Lstream_delete (XLSTREAM (lstream));
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.
1002 CHECK_PROCESS (proc);
1003 return MAYBE_LISP_PROCMETH (get_tty_name, (XPROCESS (proc)));
1006 DEFUN ("set-process-buffer", Fset_process_buffer, 2, 2, 0, /*
1007 Set buffer associated with PROCESS to BUFFER (a buffer, or nil).
1011 CHECK_PROCESS (proc);
1013 CHECK_BUFFER (buffer);
1014 XPROCESS (proc)->buffer = buffer;
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.
1025 CHECK_PROCESS (proc);
1026 return XPROCESS (proc)->buffer;
1029 DEFUN ("process-mark", Fprocess_mark, 1, 1, 0, /*
1030 Return the marker for the end of the last output from PROCESS.
1034 CHECK_PROCESS (proc);
1035 return XPROCESS (proc)->mark;
1039 set_process_filter (Lisp_Object proc, Lisp_Object filter, int filter_does_read)
1041 CHECK_PROCESS (proc);
1042 if (PROCESS_LIVE_P (XPROCESS (proc))) {
1043 if (EQ (filter, Qt))
1044 event_stream_unselect_process (XPROCESS (proc));
1046 event_stream_select_process (XPROCESS (proc));
1049 XPROCESS (proc)->filter = filter;
1050 XPROCESS (proc)->filter_does_read = filter_does_read;
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.
1063 set_process_filter (proc, filter, 0);
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.
1073 CHECK_PROCESS (proc);
1074 return XPROCESS (proc)->filter;
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.
1085 (process, start, end, buffer))
1087 /* This function can GC */
1088 Lisp_Object proc = get_process (process);
1090 struct buffer *buf = decode_buffer (buffer, 0);
1092 XSETBUFFER (buffer, buf);
1093 get_buffer_range_char (buf, start, end, &st, &en, 0);
1095 send_process (proc, buffer, 0, st, en - st);
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.
1107 (process, string, from, to))
1109 /* This function can GC */
1114 proc = get_process (process);
1115 CHECK_STRING (string);
1116 get_string_range_byte (string, from, to, &bfr, &bto,
1117 GB_HISTORICAL_STRING_BEHAVIOR);
1120 send_process (proc, string, 0, bfr, len);
1126 DEFUN ("process-input-coding-system", Fprocess_input_coding_system, 1, 1, 0, /*
1127 Return PROCESS's input coding system.
1131 process = get_process (process);
1132 CHECK_LIVE_PROCESS (process);
1133 return decoding_stream_coding_system (XLSTREAM (XPROCESS (process)->coding_instream) );
1136 DEFUN ("process-output-coding-system", Fprocess_output_coding_system, 1, 1, 0, /*
1137 Return PROCESS's output coding system.
1141 process = get_process (process);
1142 CHECK_LIVE_PROCESS (process);
1143 return encoding_stream_coding_system (XLSTREAM (XPROCESS (process)->coding_outstream));
1146 DEFUN ("process-coding-system", Fprocess_coding_system, 1, 1, 0, /*
1147 Return a pair of coding-system for decoding and encoding of PROCESS.
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)));
1159 DEFUN ("set-process-input-coding-system", Fset_process_input_coding_system,
1161 Set PROCESS's input coding system to CODESYS.
1165 codesys = Fget_coding_system (codesys);
1166 process = get_process (process);
1167 CHECK_LIVE_PROCESS (process);
1169 set_decoding_stream_coding_system
1170 (XLSTREAM (XPROCESS (process)->coding_instream), codesys);
1174 DEFUN ("set-process-output-coding-system", Fset_process_output_coding_system,
1176 Set PROCESS's output coding system to CODESYS.
1180 codesys = Fget_coding_system (codesys);
1181 process = get_process (process);
1182 CHECK_LIVE_PROCESS (process);
1184 set_encoding_stream_coding_system
1185 (XLSTREAM (XPROCESS (process)->coding_outstream), codesys);
1189 DEFUN ("set-process-coding-system", Fset_process_coding_system,
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.
1195 (process, decoding, encoding))
1197 if (!NILP (decoding))
1198 Fset_process_input_coding_system (process, decoding);
1200 if (!NILP (encoding))
1201 Fset_process_output_coding_system (process, encoding);
1206 #endif /* FILE_CODING */
1208 /************************************************************************/
1209 /* process status */
1210 /************************************************************************/
1213 exec_sentinel_unwind (Lisp_Object datum)
1215 Lisp_Cons *d = XCONS (datum);
1216 XPROCESS (d->car)->sentinel = d->cdr;
1222 exec_sentinel (Lisp_Object proc, Lisp_Object reason)
1224 /* This function can GC */
1225 int speccount = specpdl_depth ();
1226 Lisp_Process *p = XPROCESS (proc);
1227 Lisp_Object sentinel = p->sentinel;
1229 if (NILP (sentinel))
1232 /* Some weird FSFmacs crap here with
1233 Vdeactivate_mark and current_buffer->keymap */
1235 /* Zilch the sentinel while it's running, to avoid recursive invocations;
1236 assure that it gets restored no matter how the sentinel exits. */
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);
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.
1255 CHECK_PROCESS (proc);
1256 XPROCESS (proc)->sentinel = sentinel;
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.
1266 CHECK_PROCESS (proc);
1267 return XPROCESS (proc)->sentinel;
1272 signal_name (int signum)
1274 if (signum >= 0 && signum < NSIG)
1275 return (const char *) sys_siglist[signum];
1277 return (const char *) GETTEXT ("unknown signal");
1281 update_process_status (Lisp_Object p,
1282 Lisp_Object status_symbol,
1286 XPROCESS (p)->tick++;
1288 XPROCESS (p)->status_symbol = status_symbol;
1289 XPROCESS (p)->exit_code = exit_code;
1290 XPROCESS (p)->core_dumped = core_dumped;
1293 /* Return a string describing a process status list. */
1296 status_message (Lisp_Process *p)
1298 Lisp_Object symbol = p->status_symbol;
1299 int code = p->exit_code;
1300 int coredump = p->core_dumped;
1301 Lisp_Object string, string2;
1303 if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
1305 string = build_string (signal_name (code));
1307 string2 = build_translated_string (" (core dumped)\n");
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);
1315 else if (EQ (symbol, Qexit))
1318 return build_translated_string ("finished\n");
1319 string = Fnumber_to_string (make_int (code));
1321 string2 = build_translated_string (" (core dumped)\n");
1323 string2 = build_string ("\n");
1324 return concat2 (build_translated_string ("exited abnormally with code "),
1325 concat2 (string, string2));
1328 return Fcopy_sequence (Fsymbol_name (symbol));
1331 /* Tell status_notify() to check for terminated processes. We do this
1332 because on some systems we sometimes miss SIGCHLD calls. (Not sure
1336 kick_status_notify (void)
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. */
1347 status_notify (void)
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
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;
1364 MAYBE_PROCMETH (reap_exited_processes, ());
1366 temp_process_tick = process_tick;
1368 if (update_tick == temp_process_tick)
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
1375 GCPRO3 (tail, symbol, msg);
1377 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
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;
1384 /* #### extra check for terminated processes, in case a SIGCHLD
1385 got missed (this seems to happen sometimes, I'm not sure why).
1388 MAYBE_PROCMETH (update_status_if_terminated, (p));
1390 this_process_tick = p->tick;
1391 if (this_process_tick != p->update_tick)
1393 p->update_tick = this_process_tick;
1395 /* If process is still active, read any output that remains. */
1396 while (!EQ (p->filter, Qt)
1397 && read_process_output (proc) > 0)
1400 /* Get the text to use for the message. */
1401 msg = status_message (p);
1403 /* If process is terminated, deactivate it or delete it. */
1404 symbol = p->status_symbol;
1406 if (EQ (symbol, Qsignal)
1407 || EQ (symbol, Qexit))
1409 if (delete_exited_processes)
1410 remove_process (proc);
1412 deactivate_process (proc);
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))
1422 Lisp_Object old_read_only = Qnil;
1423 Lisp_Object old = Fcurrent_buffer ();
1425 struct gcpro ngcpro1, ngcpro2;
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)))
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));
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))
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, " ");
1453 current_buffer->read_only = old_read_only;
1454 Fset_marker (p->mark, make_int (BUF_PT (current_buffer)),
1457 opoint = bufpos_clip_to_bounds(BUF_BEGV (XBUFFER (p->buffer)),
1459 BUF_ZV (XBUFFER (p->buffer)));
1460 BUF_SET_PT (current_buffer, opoint);
1467 /* in case buffers use %s in modeline-format */
1468 MARK_MODELINE_CHANGED;
1471 update_tick = temp_process_tick;
1476 DEFUN ("process-status", Fprocess_status, 1, 1, 0, /*
1477 Return the status of PROCESS.
1478 This is a symbol, one of these:
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.
1488 PROCESS may be a process, a buffer, the name of a process or buffer, or
1489 nil, indicating the current buffer's process.
1493 Lisp_Object status_symbol;
1496 proc = Fget_process (proc);
1498 proc = get_process (proc);
1503 status_symbol = XPROCESS (proc)->status_symbol;
1504 if (network_connection_p (proc))
1506 if (EQ (status_symbol, Qrun))
1507 status_symbol = Qopen;
1508 else if (EQ (status_symbol, Qexit))
1509 status_symbol = Qclosed;
1511 return status_symbol;
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.
1520 CHECK_PROCESS (proc);
1521 return make_int (XPROCESS (proc)->exit_code);
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
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. */
1538 process_send_signal (Lisp_Object process, int signo,
1539 int current_group, int nomsg)
1541 /* This function can GC */
1542 Lisp_Object proc = get_process (process);
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);
1549 MAYBE_PROCMETH (kill_child_process, (proc, signo, current_group, nomsg));
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.
1561 (process, current_group))
1563 /* This function can GC */
1564 process_send_signal (process, SIGINT, !NILP (current_group), 0);
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.
1572 (process, current_group))
1574 /* This function can GC */
1576 process_send_signal (process, SIGKILL, !NILP (current_group), 0);
1578 error ("kill-process: Not supported on this system");
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.
1587 (process, current_group))
1589 /* This function can GC */
1591 process_send_signal (process, SIGQUIT, !NILP (current_group), 0);
1593 error ("quit-process: Not supported on this system");
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.
1602 (process, current_group))
1604 /* This function can GC */
1606 process_send_signal (process, SIGTSTP, !NILP (current_group), 0);
1608 error ("stop-process: Not supported on this system");
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.
1617 (process, current_group))
1619 /* This function can GC */
1621 process_send_signal (process, SIGCONT, !NILP (current_group), 0);
1623 error ("continue-process: Not supported on this system");
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.
1644 CHECK_SYMBOL (sigcode);
1645 name = string_data (XSYMBOL (sigcode)->name);
1647 #define handle_signal(signal) \
1648 else if (!strcmp ((const char *) name, #signal)) \
1649 XSETINT (sigcode, signal)
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 */
1661 handle_signal (SIGHUP); /* POSIX */
1664 handle_signal (SIGQUIT); /* POSIX */
1667 handle_signal (SIGTRAP); /* POSIX */
1670 handle_signal (SIGKILL); /* POSIX */
1673 handle_signal (SIGUSR1); /* POSIX */
1676 handle_signal (SIGUSR2); /* POSIX */
1679 handle_signal (SIGPIPE); /* POSIX */
1682 handle_signal (SIGALRM); /* POSIX */
1685 handle_signal (SIGCHLD); /* POSIX */
1688 handle_signal (SIGCONT); /* POSIX */
1691 handle_signal (SIGSTOP); /* POSIX */
1694 handle_signal (SIGTSTP); /* POSIX */
1697 handle_signal (SIGTTIN); /* POSIX */
1700 handle_signal (SIGTTOU); /* POSIX */
1704 handle_signal (SIGBUS); /* XPG5 */
1707 handle_signal (SIGPOLL); /* XPG5 */
1710 handle_signal (SIGPROF); /* XPG5 */
1713 handle_signal (SIGSYS); /* XPG5 */
1716 handle_signal (SIGURG); /* XPG5 */
1719 handle_signal (SIGXCPU); /* XPG5 */
1722 handle_signal (SIGXFSZ); /* XPG5 */
1725 handle_signal (SIGVTALRM); /* XPG5 */
1729 handle_signal (SIGIO); /* BSD 4.2 */
1732 handle_signal (SIGWINCH); /* BSD 4.3 */
1736 handle_signal (SIGEMT);
1739 handle_signal (SIGINFO);
1742 handle_signal (SIGHWE);
1745 handle_signal (SIGPRE);
1748 handle_signal (SIGUME);
1751 handle_signal (SIGDLK);
1754 handle_signal (SIGCPULIM);
1757 handle_signal (SIGIOT);
1760 handle_signal (SIGLOST);
1763 handle_signal (SIGSTKFLT);
1766 handle_signal (SIGUNUSED);
1769 handle_signal (SIGDANGER); /* AIX */
1772 handle_signal (SIGMSG);
1775 handle_signal (SIGSOUND);
1778 handle_signal (SIGRETRACT);
1781 handle_signal (SIGGRANT);
1784 handle_signal (SIGPWR);
1787 error ("Undefined signal name %s", name);
1790 #undef handle_signal
1792 return make_int (PROCMETH_OR_GIVEN (kill_process_by_pid,
1793 (XINT (pid), XINT (sigcode)), -1));
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.
1806 /* This function can GC */
1807 Lisp_Object proc = get_process (process);
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));
1813 if (!MAYBE_INT_PROCMETH (process_send_eof, (proc)))
1815 if (!NILP (DATA_OUTSTREAM (XPROCESS (proc))))
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;
1821 XPROCESS (proc)->coding_outstream = Qnil;
1830 /************************************************************************/
1831 /* deleting a process */
1832 /************************************************************************/
1835 deactivate_process (Lisp_Object proc)
1837 Lisp_Process *p = XPROCESS (proc);
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);
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)));
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));
1860 usid = event_stream_delete_stream_pair (p->pipe_instream,
1863 if (usid != USID_DONTHASH)
1864 remhash ((const void*)usid, usid_to_process);
1866 p->pipe_instream = Qnil;
1867 p->pipe_outstream = Qnil;
1869 p->coding_instream = Qnil;
1870 p->coding_outstream = Qnil;
1875 remove_process (Lisp_Object proc)
1877 Vprocess_list = delq_no_quit (proc, Vprocess_list);
1878 Fset_marker (XPROCESS (proc)->mark, Qnil, Qnil);
1880 deactivate_process (proc);
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.
1889 /* This function can GC */
1891 proc = get_process (proc);
1892 p = XPROCESS (proc);
1893 if (network_connection_p (proc))
1895 p->status_symbol = Qexit;
1901 else if (PROCESS_LIVE_P (p))
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;
1912 remove_process (proc);
1916 /* Kill all processes associated with `buffer'.
1917 If `buffer' is nil, kill all processes */
1920 kill_buffer_processes (Lisp_Object buffer)
1924 for (tail = Vprocess_list; CONSP (tail);
1927 Lisp_Object proc = XCAR (tail);
1929 && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer)))
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);
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.
1944 (proc, require_query_p))
1948 CHECK_PROCESS (proc);
1949 tem = XPROCESS (proc)->kill_without_query;
1950 XPROCESS (proc)->kill_without_query = NILP (require_query_p);
1952 return tem ? Qnil : Qt;
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.
1960 CHECK_PROCESS (proc);
1961 return XPROCESS (proc)->kill_without_query ? Qt : Qnil;
1965 /* This is not named init_process in order to avoid a conflict with NS 3.3 */
1967 init_xemacs_process (void)
1969 MAYBE_PROCMETH (init_process, ());
1971 Vprocess_list = Qnil;
1973 if (usid_to_process)
1974 clrhash (usid_to_process);
1976 usid_to_process = make_hash_table (32);
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).
1987 return XPROCESS (process)->type;
1993 syms_of_process (void)
1995 INIT_LRECORD_IMPLEMENTATION (process);
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");
2004 defsymbol (&Qtcp, "tcp");
2005 defsymbol (&Qudp, "udp");
2007 #ifdef HAVE_MULTICAST
2008 defsymbol(&Qmulticast, "multicast"); /* Used for occasional warnings */
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);
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); */
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 */
2061 vars_of_process (void)
2063 Fprovide (intern ("subprocesses"));
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);
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.
2077 delete_exited_processes = 1;
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').
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.
2093 Vnull_device = build_string (NULL_DEVICE);
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.
2102 Vprocess_connection_type = Qt;
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.
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.
2117 NOTE: You should never set this variable, only bind it.
2119 Only Windows processes can be "windowed" or "console". This variable has no
2120 effect on UNIX processes, because all UNIX processes are "console".
2122 windowed_process_io = 0;
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.
2130 network_stream_blocking_port_list = Qnil;
2131 #endif /* PROCESS_IO_BLOCKING */
2134 #endif /* not NO_SUBPROCESSES */