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, Qprocess_readable_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 object)
117 Lisp_Process *process = XPROCESS (object);
118 MAYBE_PROCMETH (mark_process_data, (process));
119 mark_object (process->name);
120 mark_object (process->command);
121 mark_object (process->filter);
122 mark_object (process->sentinel);
123 mark_object (process->buffer);
124 mark_object (process->mark);
125 mark_object (process->pid);
126 mark_object (process->pipe_instream);
127 mark_object (process->pipe_outstream);
129 mark_object (process->coding_instream);
130 mark_object (process->coding_outstream);
132 return process->status_symbol;
136 print_process (Lisp_Object object, Lisp_Object printcharfun, int escapeflag)
138 Lisp_Process *process = XPROCESS (object);
141 error ("printing unreadable object #<process %s>",
142 XSTRING_DATA (process->name));
146 print_internal (process->name, printcharfun, 0);
150 int netp = network_connection_p (object);
151 write_c_string ((netp ? GETTEXT ("#<network connection ") :
152 GETTEXT ("#<process ")), printcharfun);
153 print_internal (process->name, printcharfun, 1);
154 write_c_string ((netp ? " " : " pid "), printcharfun);
155 print_internal (process->pid, printcharfun, 1);
156 write_c_string (" state:", printcharfun);
157 print_internal (process->status_symbol, printcharfun, 1);
158 MAYBE_PROCMETH (print_process_data, (process, printcharfun));
159 write_c_string (">", printcharfun);
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 (process, vval);
220 return XPROCESS (process);
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 (object) ? 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 (object) && PROCESS_LIVE_P (XPROCESS (object))
270 /* This is a reasonable definition for this new primitive. Kyle sez:
272 "The patch looks OK to me except for the creation and exporting of the
273 Fprocess_readable_p function. I don't think a new Lisp function
274 should be created until we know something actually needs it. If
275 we later want to give process-readable-p different semantics it
276 may be hard to do it and stay compatible with what we hastily
279 He's right, not yet. Let's discuss the semantics on XEmacs Design
280 before enabling this.
282 DEFUN ("process-readable-p", Fprocess_readable_p, 1, 1, 0, /*
283 Return t if OBJECT is a process from which input may be available.
287 return PROCESSP (object) && PROCESS_READABLE_P (XPROCESS (object))
292 DEFUN ("process-list", Fprocess_list, 0, 0, 0, /*
293 Return a list of all processes.
297 return Fcopy_sequence (Vprocess_list);
300 DEFUN ("get-process", Fget_process, 1, 1, 0, /*
301 Return the process named PROCESS-NAME (a string), or nil if there is none.
302 PROCESS-NAME may also be a process; if so, the value is that process.
306 if (PROCESSP (process_name))
310 /* this only gets called during GC when emacs is going away as a result
311 of a signal or crash. */
312 CHECK_STRING (process_name);
315 LIST_LOOP_2 (process, Vprocess_list)
316 if (internal_equal (process_name, XPROCESS (process)->name, 0))
322 DEFUN ("get-buffer-process", Fget_buffer_process, 1, 1, 0, /*
323 Return the (or, a) process associated with BUFFER.
324 BUFFER may be a buffer or the name of one.
328 if (NILP (buffer)) return Qnil;
329 buffer = Fget_buffer (buffer);
330 if (NILP (buffer)) return Qnil;
333 LIST_LOOP_2 (process, Vprocess_list)
334 if (EQ (XPROCESS (process)->buffer, buffer))
340 /* This is how commands for the user decode process arguments. It
341 accepts a process, a process name, a buffer, a buffer name, or nil.
342 Buffers denote the first process in the buffer, and nil denotes the
346 get_process (Lisp_Object name)
351 /* #### Look more closely into translating process names. */
354 /* This may be called during a GC from process_send_signal() from
355 kill_buffer_processes() if emacs decides to abort(). */
358 else if (STRINGP (name))
360 Lisp_Object object = Fget_process (name);
361 if (PROCESSP (object))
364 buffer = Fget_buffer (name);
365 if (BUFFERP (buffer))
366 goto have_buffer_object;
368 error ("Process %s does not exist", XSTRING_DATA (name));
370 else if (NILP (name))
372 buffer = Fcurrent_buffer ();
373 goto have_buffer_object;
375 else if (BUFFERP (name))
381 process = Fget_buffer_process (buffer);
382 if (PROCESSP (process))
385 error ("Buffer %s has no process",
386 XSTRING_DATA (XBUFFER (buffer)->name));
389 return get_process (Fsignal (Qwrong_type_argument,
390 (list2 (build_string ("process or buffer or nil"),
394 DEFUN ("process-id", Fprocess_id, 1, 1, 0, /*
395 Return the process id of PROCESS.
396 This is the pid of the Unix process which PROCESS uses or talks to.
397 For a network connection, this value is a cons of
398 (foreign-network-port . foreign-host-name).
403 CHECK_PROCESS (process);
405 pid = XPROCESS (process)->pid;
406 if (network_connection_p (process))
408 return Fcons (Fcar (pid), Fcdr (pid));
413 DEFUN ("process-name", Fprocess_name, 1, 1, 0, /*
414 Return the name of PROCESS, as a string.
415 This is the name of the program invoked in PROCESS,
416 possibly modified to make it unique among process names.
420 CHECK_PROCESS (process);
421 return XPROCESS (process)->name;
424 DEFUN ("process-command", Fprocess_command, 1, 1, 0, /*
425 Return the command that was executed to start PROCESS.
426 This is a list of strings, the first string being the program executed
427 and the rest of the strings being the arguments given to it.
431 CHECK_PROCESS (process);
432 return XPROCESS (process)->command;
436 /************************************************************************/
437 /* creating a process */
438 /************************************************************************/
441 make_process_internal (Lisp_Object name)
443 Lisp_Object val, name1;
445 Lisp_Process *p = alloc_lcrecord_type (Lisp_Process, &lrecord_process);
447 /* If name is already in use, modify it until it is unused. */
452 Lisp_Object tem = Fget_process (name1);
455 sprintf (suffix, "<%d>", i);
456 name1 = concat2 (name, build_string (suffix));
465 p->mark = Fmake_marker ();
467 p->status_symbol = Qrun;
470 p->filter_does_read = 0;
471 p->kill_without_query = 0;
475 p->pipe_instream = Qnil;
476 p->pipe_outstream = Qnil;
478 p->coding_instream = Qnil;
479 p->coding_outstream = Qnil;
483 MAYBE_PROCMETH (alloc_process_data, (p));
485 XSETPROCESS (val, p);
487 Vprocess_list = Fcons (val, Vprocess_list);
492 init_process_io_handles (Lisp_Process *p, void* in, void* out, int flags)
494 USID usid = event_stream_create_stream_pair (in, out,
495 &p->pipe_instream, &p->pipe_outstream,
498 if (usid == USID_ERROR)
499 report_file_error ("Setting up communication with subprocess", Qnil);
501 if (usid != USID_DONTHASH)
503 Lisp_Object process = Qnil;
504 XSETPROCESS (process, p);
505 puthash ((const void*)usid, LISP_TO_VOID (process), usid_to_process);
508 MAYBE_PROCMETH (init_process_io_handles, (p, in, out, flags));
511 p->coding_instream = make_decoding_input_stream
512 (XLSTREAM (p->pipe_instream),
513 Fget_coding_system (Vcoding_system_for_read));
514 Lstream_set_character_mode (XLSTREAM (p->coding_instream));
515 p->coding_outstream = make_encoding_output_stream
516 (XLSTREAM (p->pipe_outstream),
517 Fget_coding_system (Vcoding_system_for_write));
518 /* CODE_CNTL (&out_state[outchannel]) |= CC_END; !!####
519 What's going on here? */
520 #endif /* FILE_CODING */
524 create_process (Lisp_Object process, Lisp_Object *argv, int nargv,
525 Lisp_Object program, Lisp_Object cur_dir)
527 Lisp_Process *p = XPROCESS (process);
530 /* *_create_process may change status_symbol, if the process
531 is a kind of "fire-and-forget" (no I/O, unwaitable) */
532 p->status_symbol = Qrun;
535 pid = PROCMETH (create_process, (p, argv, nargv, program, cur_dir));
537 p->pid = make_int (pid);
538 if (PROCESS_READABLE_P (p))
539 event_stream_select_process (p);
542 /* This function is the unwind_protect form for Fstart_process_internal. If
543 PROCESS doesn't have its pid set, then we know someone has signalled
544 an error and the process wasn't started successfully, so we should
545 remove it from the process list. */
546 static void remove_process (Lisp_Object process);
548 start_process_unwind (Lisp_Object process)
550 /* Was PROCESS started successfully? */
551 if (EQ (XPROCESS (process)->pid, Qnil))
552 remove_process (process);
556 DEFUN ("start-process-internal", Fstart_process_internal, 3, MANY, 0, /*
557 Start a program in a subprocess. Return the process object for it.
558 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS
559 NAME is name for process. It is modified if necessary to make it unique.
560 BUFFER is the buffer or (buffer-name) to associate with the process.
561 Process output goes at end of that buffer, unless you specify
562 an output stream or filter function to handle the output.
563 BUFFER may be also nil, meaning that this process is not associated
565 Third arg is program file name. It is searched for as in the shell.
566 Remaining arguments are strings to give program as arguments.
567 INCODE and OUTCODE specify the coding-system objects used in input/output
570 (int nargs, Lisp_Object *args))
572 /* This function can call lisp */
573 /* !!#### This function has not been Mule-ized */
574 Lisp_Object buffer, name, program, process, current_dir;
576 int speccount = specpdl_depth ();
577 struct gcpro gcpro1, gcpro2, gcpro3;
584 /* Protect against various file handlers doing GCs below. */
585 GCPRO3 (buffer, program, current_dir);
588 buffer = Fget_buffer_create (buffer);
591 CHECK_STRING (program);
593 /* Make sure that the child will be able to chdir to the current
594 buffer's current directory, or its unhandled equivalent. We
595 can't just have the child check for an error when it does the
596 chdir, since it's in a vfork.
598 Note: these assignments and calls are like this in order to insure
599 "caller protects args" GC semantics. */
600 current_dir = current_buffer->directory;
601 current_dir = Funhandled_file_name_directory (current_dir);
602 current_dir = expand_and_dir_to_file (current_dir, Qnil);
604 #if 0 /* This loser breaks ange-ftp */
605 /* dmoore - if you re-enable this code, you have to gcprotect
606 current_buffer through the above calls. */
607 if (NILP (Ffile_accessible_directory_p (current_dir)))
608 report_file_error ("Setting current directory",
609 list1 (current_buffer->directory));
612 /* If program file name is not absolute, search our path for it */
613 if (!IS_DIRECTORY_SEP (XSTRING_BYTE (program, 0))
614 && !(XSTRING_LENGTH (program) > 1
615 && IS_DEVICE_SEP (XSTRING_BYTE (program, 1))))
617 struct gcpro ngcpro1;
621 locate_file (Vexec_path, program, Vlisp_EXEC_SUFFIXES, &tem, X_OK);
623 report_file_error ("Searching for program", list1 (program));
624 program = Fexpand_file_name (tem, Qnil);
629 /* we still need to canonicalize it and ensure it has the proper
631 struct gcpro ngcpro1;
635 locate_file (list1 (build_string ("")), program, Vlisp_EXEC_SUFFIXES,
638 report_file_error ("Searching for program", list1 (program));
643 if (!NILP (Ffile_directory_p (program)))
644 invalid_operation ("Specified program for new process is a directory",
647 process = make_process_internal (name);
649 XPROCESS (process)->buffer = buffer;
650 XPROCESS (process)->command = Flist (nargs - 2,
653 /* Make the process marker point into the process buffer (if any). */
655 Fset_marker (XPROCESS (process)->mark,
656 make_int (BUF_ZV (XBUFFER (buffer))), buffer);
658 /* If an error occurs and we can't start the process, we want to
659 remove it from the process list. This means that each error
660 check in create_process doesn't need to call remove_process
661 itself; it's all taken care of here. */
662 record_unwind_protect (start_process_unwind, process);
664 create_process (process, args + 3, nargs - 3, program, current_dir);
667 return unbind_to (speccount, process);
674 /* #### The network support is fairly synthetical. What we actually
675 need is a single function, which supports all datagram, stream and
676 packet stream connections, arbitrary protocol families should they
677 be supported by the target system, multicast groups, in both data
678 and control rooted/nonrooted flavors, service quality etc whatever
679 is supported by the underlying network.
681 It must accept a property list describing the connection. The current
682 functions must then go to lisp and provide a suitable list for the
683 generalized connection function.
685 Both UNIX and Win32 support BSD sockets, and there are many extensions
686 available (Sockets 2 spec).
688 A todo is define a consistent set of properties abstracting a
689 network connection. -kkm
693 /* open a TCP network connection to a given HOST/SERVICE. Treated
694 exactly like a normal process when reading and writing. Only
695 differences are in status display and process deletion. A network
696 connection has no PID; you cannot signal it. All you can do is
697 deactivate and close it via delete-process */
699 DEFUN ("open-network-stream-internal", Fopen_network_stream_internal, 4, 5,
701 Open a TCP connection for a service to a host.
702 Return a process object to represent the connection.
703 Input and output work as for subprocesses; `delete-process' closes it.
705 NAME is name for process. It is modified if necessary to make it unique.
706 BUFFER is the buffer (or buffer-name) to associate with the process.
707 Process output goes at end of that buffer, unless you specify
708 an output stream or filter function to handle the output.
709 BUFFER may also be nil, meaning that this process is not associated
711 Third arg HOST (a string) is the name of the host to connect to,
713 Fourth arg SERVICE is the name of the service desired (a string),
714 or an integer specifying a port number to connect to.
715 Optional fifth arg PROTOCOL is a network protocol. Currently only 'tcp
716 (Transmission Control Protocol) and 'udp (User Datagram Protocol) are
717 supported. When omitted, 'tcp is assumed.
719 Output via `process-send-string' and input via buffer or filter (see
720 `set-process-filter') are stream-oriented. That means UDP datagrams are
721 not guaranteed to be sent and received in discrete packets. (But small
722 datagrams around 500 bytes that are not truncated by `process-send-string'
723 are usually fine.) Note further that the UDP protocol does not guard
724 against lost packets.
726 (name, buffer, host, service, protocol))
728 /* !!#### This function has not been Mule-ized */
729 /* This function can GC */
730 Lisp_Object process = Qnil;
731 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, ngcpro1;
734 GCPRO5 (name, buffer, host, service, protocol);
740 CHECK_SYMBOL (protocol);
742 /* Since this code is inside HAVE_SOCKETS, existence of
743 open_network_stream is mandatory */
744 PROCMETH (open_network_stream, (name, host, service, protocol,
748 buffer = Fget_buffer_create (buffer);
749 process = make_process_internal (name);
752 XPROCESS (process)->pid = Fcons (service, host);
753 XPROCESS (process)->buffer = buffer;
754 init_process_io_handles (XPROCESS (process), (void*)inch, (void*)outch,
755 STREAM_NETWORK_CONNECTION);
757 event_stream_select_process (XPROCESS (process));
764 #ifdef HAVE_MULTICAST
766 DEFUN ("open-multicast-group-internal", Fopen_multicast_group_internal, 5, 5, 0, /*
767 Open a multicast connection on the specified dest/port/ttl.
768 Return a process object to represent the connection.
769 Input and output work as for subprocesses; `delete-process' closes it.
771 NAME is name for process. It is modified if necessary to make it unique.
772 BUFFER is the buffer (or buffer-name) to associate with the process.
773 Process output goes at end of that buffer, unless you specify
774 an output stream or filter function to handle the output.
775 BUFFER may also be nil, meaning that this process is not associated
777 Third, fourth and fifth args are the multicast destination group, port and ttl.
778 dest must be an internet address between 224.0.0.0 and 239.255.255.255
779 port is a communication port like in traditional unicast
780 ttl is the time-to-live (15 for site, 63 for region and 127 for world)
782 (name, buffer, dest, port, ttl))
784 /* !!#### This function has not been Mule-ized */
785 /* This function can GC */
786 Lisp_Object process = Qnil;
792 /* Since this code is inside HAVE_MULTICAST, existence of
793 open_network_stream is mandatory */
794 PROCMETH (open_multicast_group, (name, dest, port, ttl,
798 buffer = Fget_buffer_create (buffer);
800 process = make_process_internal (name);
803 XPROCESS (process)->pid = Fcons (port, dest);
804 XPROCESS (process)->buffer = buffer;
805 init_process_io_handles (XPROCESS (process), (void*)inch, (void*)outch,
806 STREAM_NETWORK_CONNECTION);
808 event_stream_select_process (XPROCESS (process));
813 #endif /* HAVE_MULTICAST */
815 #endif /* HAVE_SOCKETS */
818 canonicalize_host_name (Lisp_Object host)
820 return PROCMETH_OR_GIVEN (canonicalize_host_name, (host), host);
824 DEFUN ("set-process-window-size", Fset_process_window_size, 3, 3, 0, /*
825 Tell PROCESS that it has logical window size HEIGHT and WIDTH.
827 (process, height, width))
829 CHECK_PROCESS (process);
830 CHECK_NATNUM (height);
831 CHECK_NATNUM (width);
833 MAYBE_INT_PROCMETH (set_window_size,
834 (XPROCESS (process), XINT (height), XINT (width))) <= 0
839 /************************************************************************/
841 /************************************************************************/
843 /* Read pending output from the process channel,
844 starting with our buffered-ahead character if we have one.
845 Yield number of characters read.
847 This function reads at most 1024 bytes.
848 If you want to read all available subprocess output,
849 you must call it repeatedly until it returns zero. */
852 read_process_output (Lisp_Object process)
854 /* This function can GC */
855 Bytecount nbytes, nchars;
857 Lisp_Object outstream;
858 Lisp_Process *p = XPROCESS (process);
860 /* If there is a lot of output from the subprocess, the loop in
861 execute_internal_event() might call read_process_output() more
862 than once. If the filter that was executed from one of these
863 calls set the filter to t, we have to stop now. Return -1 rather
864 than 0 so execute_internal_event() doesn't close the process.
865 Really, the loop in execute_internal_event() should check itself
866 for a process-filter change, like in status_notify(); but the
867 struct Lisp_Process is not exported outside of this file. */
868 if (!PROCESS_READABLE_P (p))
869 return -1; /* already closed */
871 if (!NILP (p->filter) && (p->filter_does_read))
873 Lisp_Object filter_result;
875 /* Some weird FSFmacs crap here with
876 Vdeactivate_mark and current_buffer->keymap */
877 running_asynch_code = 1;
878 filter_result = call2_trapping_errors ("Error in process filter",
879 p->filter, process, Qnil);
880 running_asynch_code = 0;
881 restore_match_data ();
882 CHECK_INT (filter_result);
883 return XINT (filter_result);
886 nbytes = Lstream_read (XLSTREAM (DATA_INSTREAM(p)), chars, sizeof (chars));
887 if (nbytes <= 0) return nbytes;
889 nchars = bytecount_to_charcount (chars, nbytes);
890 outstream = p->filter;
891 if (!NILP (outstream))
893 /* We used to bind inhibit-quit to t here, but
894 call2_trapping_errors() does that for us. */
895 running_asynch_code = 1;
896 call2_trapping_errors ("Error in process filter",
897 outstream, process, make_string (chars, nbytes));
898 running_asynch_code = 0;
899 restore_match_data ();
903 /* If no filter, write into buffer if it isn't dead. */
904 if (!NILP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
906 Lisp_Object old_read_only = Qnil;
910 int old_zmacs_region_stays = zmacs_region_stays;
911 struct gcpro gcpro1, gcpro2;
912 struct buffer *buf = XBUFFER (p->buffer);
914 GCPRO2 (process, old_read_only);
916 old_point = BUF_PT (buf);
917 old_begv = BUF_BEGV (buf);
918 old_zv = BUF_ZV (buf);
919 old_read_only = buf->read_only;
920 buf->read_only = Qnil;
922 /* Insert new output into buffer
923 at the current end-of-output marker,
924 thus preserving logical ordering of input and output. */
925 if (XMARKER (p->mark)->buffer)
927 bufpos_clip_to_bounds (old_begv, marker_position (p->mark),
930 BUF_SET_PT (buf, old_zv);
932 /* If the output marker is outside of the visible region, save
933 the restriction and widen. */
934 if (! (BUF_BEGV (buf) <= BUF_PT (buf) &&
935 BUF_PT (buf) <= BUF_ZV (buf)))
938 /* Make sure opoint floats ahead of any new text, just as point
940 if (BUF_PT (buf) <= old_point)
943 /* Insert after old_begv, but before old_zv. */
944 if (BUF_PT (buf) < old_begv)
946 if (BUF_PT (buf) <= old_zv)
950 /* This screws up initial display of the window. jla */
952 /* Insert before markers in case we are inserting where
953 the buffer's mark is, and the user's next command is Meta-y. */
954 buffer_insert_raw_string_1 (buf, -1, chars,
955 nbytes, INSDEL_BEFORE_MARKERS);
957 buffer_insert_raw_string (buf, chars, nbytes);
960 Fset_marker (p->mark, make_int (BUF_PT (buf)), p->buffer);
962 MARK_MODELINE_CHANGED;
964 /* If the restriction isn't what it should be, set it. */
965 if (old_begv != BUF_BEGV (buf) || old_zv != BUF_ZV (buf))
968 old_begv = bufpos_clip_to_bounds (BUF_BEG (buf),
971 old_zv = bufpos_clip_to_bounds (BUF_BEG (buf),
974 Fnarrow_to_region (make_int (old_begv), make_int (old_zv),
978 /* Handling the process output should not deactivate the mark. */
979 zmacs_region_stays = old_zmacs_region_stays;
980 buf->read_only = old_read_only;
981 old_point = bufpos_clip_to_bounds (BUF_BEGV (buf),
984 BUF_SET_PT (buf, old_point);
991 /* Sending data to subprocess */
993 /* send some data to process PROCESS. If NONRELOCATABLE is non-NULL, it
994 specifies the address of the data. Otherwise, the data comes from the
995 object RELOCATABLE (either a string or a buffer). START and LEN
996 specify the offset and length of the data to send.
998 Note that START and LEN are in Bufpos's if RELOCATABLE is a buffer,
999 and in Bytecounts otherwise. */
1002 send_process (Lisp_Object process,
1003 Lisp_Object relocatable, const Bufbyte *nonrelocatable,
1006 /* This function can GC */
1007 struct gcpro gcpro1, gcpro2;
1008 Lisp_Object lstream = Qnil;
1010 GCPRO2 (process, lstream);
1012 if (NILP (DATA_OUTSTREAM (XPROCESS (process))))
1013 signal_simple_error ("Process not open for writing", process);
1017 make_fixed_buffer_input_stream (nonrelocatable + start, len);
1018 else if (BUFFERP (relocatable))
1019 lstream = make_lisp_buffer_input_stream (XBUFFER (relocatable),
1020 start, start + len, 0);
1022 lstream = make_lisp_string_input_stream (relocatable, start, len);
1024 PROCMETH (send_process, (process, XLSTREAM (lstream)));
1027 Lstream_delete (XLSTREAM (lstream));
1030 DEFUN ("process-tty-name", Fprocess_tty_name, 1, 1, 0, /*
1031 Return the name of the terminal PROCESS uses, or nil if none.
1032 This is the terminal that the process itself reads and writes on,
1033 not the name of the pty that Emacs uses to talk with that terminal.
1037 CHECK_PROCESS (process);
1038 return MAYBE_LISP_PROCMETH (get_tty_name, (XPROCESS (process)));
1041 DEFUN ("set-process-buffer", Fset_process_buffer, 2, 2, 0, /*
1042 Set buffer associated with PROCESS to BUFFER (a buffer, or nil).
1046 CHECK_PROCESS (process);
1048 CHECK_BUFFER (buffer);
1049 XPROCESS (process)->buffer = buffer;
1053 DEFUN ("process-buffer", Fprocess_buffer, 1, 1, 0, /*
1054 Return the buffer PROCESS is associated with.
1055 Output from PROCESS is inserted in this buffer
1056 unless PROCESS has a filter.
1060 CHECK_PROCESS (process);
1061 return XPROCESS (process)->buffer;
1064 DEFUN ("process-mark", Fprocess_mark, 1, 1, 0, /*
1065 Return the marker for the end of the last output from PROCESS.
1069 CHECK_PROCESS (process);
1070 return XPROCESS (process)->mark;
1074 set_process_filter (Lisp_Object process, Lisp_Object filter, int filter_does_read)
1076 CHECK_PROCESS (process);
1077 if (PROCESS_READABLE_P (XPROCESS (process))) {
1078 if (EQ (filter, Qt))
1079 event_stream_unselect_process (XPROCESS (process));
1081 event_stream_select_process (XPROCESS (process));
1084 XPROCESS (process)->filter = filter;
1085 XPROCESS (process)->filter_does_read = filter_does_read;
1088 DEFUN ("set-process-filter", Fset_process_filter, 2, 2, 0, /*
1089 Give PROCESS the filter function FILTER; nil means no filter.
1090 t means stop accepting output from the process.
1091 When a process has a filter, each time it does output
1092 the entire string of output is passed to the filter.
1093 The filter gets two arguments: the process and the string of output.
1094 If the process has a filter, its buffer is not used for output.
1098 set_process_filter (process, filter, 0);
1102 DEFUN ("process-filter", Fprocess_filter, 1, 1, 0, /*
1103 Return the filter function of PROCESS; nil if none.
1104 See `set-process-filter' for more info on filter functions.
1108 CHECK_PROCESS (process);
1109 return XPROCESS (process)->filter;
1112 DEFUN ("process-send-region", Fprocess_send_region, 3, 4, 0, /*
1113 Send current contents of the region between START and END as input to PROCESS.
1114 PROCESS may be a process or the name of a process, or a buffer or the
1115 name of a buffer, in which case the buffer's process is used. If it
1116 is nil, the current buffer's process is used.
1117 BUFFER specifies the buffer to look in; if nil, the current buffer is used.
1118 If STRING is more than 100 or so characters long, it may be sent in
1119 several chunks. This may happen even for shorter strings. Output
1120 from processes can arrive in between chunks.
1122 (process, start, end, buffer))
1124 /* This function can GC */
1125 Bufpos bstart, bend;
1126 struct buffer *buf = decode_buffer (buffer, 0);
1128 XSETBUFFER (buffer, buf);
1129 process = get_process (process);
1130 get_buffer_range_char (buf, start, end, &bstart, &bend, 0);
1132 send_process (process, buffer, 0, bstart, bend - bstart);
1136 DEFUN ("process-send-string", Fprocess_send_string, 2, 4, 0, /*
1137 Send PROCESS the contents of STRING as input.
1138 PROCESS may be a process or the name of a process, or a buffer or the
1139 name of a buffer, in which case the buffer's process is used. If it
1140 is nil, the current buffer's process is used.
1141 Optional arguments START and END specify part of STRING; see `substring'.
1142 If STRING is more than 100 or so characters long, it may be sent in
1143 several chunks. This may happen even for shorter strings. Output
1144 from processes can arrive in between chunks.
1146 (process, string, start, end))
1148 /* This function can GC */
1149 Bytecount bstart, bend;
1151 process = get_process (process);
1152 CHECK_STRING (string);
1153 get_string_range_byte (string, start, end, &bstart, &bend,
1154 GB_HISTORICAL_STRING_BEHAVIOR);
1156 send_process (process, string, 0, bstart, bend - bstart);
1162 DEFUN ("process-input-coding-system", Fprocess_input_coding_system, 1, 1, 0, /*
1163 Return PROCESS's input coding system.
1167 process = get_process (process);
1168 CHECK_READABLE_PROCESS (process);
1169 return decoding_stream_coding_system (XLSTREAM (XPROCESS (process)->coding_instream) );
1172 DEFUN ("process-output-coding-system", Fprocess_output_coding_system, 1, 1, 0, /*
1173 Return PROCESS's output coding system.
1177 process = get_process (process);
1178 CHECK_LIVE_PROCESS (process);
1179 return encoding_stream_coding_system (XLSTREAM (XPROCESS (process)->coding_outstream));
1182 DEFUN ("process-coding-system", Fprocess_coding_system, 1, 1, 0, /*
1183 Return a pair of coding-system for decoding and encoding of PROCESS.
1187 process = get_process (process);
1188 CHECK_READABLE_PROCESS (process);
1189 return Fcons (decoding_stream_coding_system
1190 (XLSTREAM (XPROCESS (process)->coding_instream)),
1191 encoding_stream_coding_system
1192 (XLSTREAM (XPROCESS (process)->coding_outstream)));
1195 DEFUN ("set-process-input-coding-system", Fset_process_input_coding_system,
1197 Set PROCESS's input coding system to CODESYS.
1201 codesys = Fget_coding_system (codesys);
1202 process = get_process (process);
1203 CHECK_READABLE_PROCESS (process);
1205 set_decoding_stream_coding_system
1206 (XLSTREAM (XPROCESS (process)->coding_instream), codesys);
1210 DEFUN ("set-process-output-coding-system", Fset_process_output_coding_system,
1212 Set PROCESS's output coding system to CODESYS.
1216 codesys = Fget_coding_system (codesys);
1217 process = get_process (process);
1218 CHECK_LIVE_PROCESS (process);
1220 set_encoding_stream_coding_system
1221 (XLSTREAM (XPROCESS (process)->coding_outstream), codesys);
1225 DEFUN ("set-process-coding-system", Fset_process_coding_system,
1227 Set coding-systems of PROCESS to DECODING and ENCODING.
1228 DECODING will be used to decode subprocess output and ENCODING to
1229 encode subprocess input.
1231 (process, decoding, encoding))
1233 if (!NILP (decoding))
1234 Fset_process_input_coding_system (process, decoding);
1236 if (!NILP (encoding))
1237 Fset_process_output_coding_system (process, encoding);
1242 #endif /* FILE_CODING */
1244 /************************************************************************/
1245 /* process status */
1246 /************************************************************************/
1249 exec_sentinel_unwind (Lisp_Object datum)
1251 Lisp_Cons *d = XCONS (datum);
1252 XPROCESS (d->car)->sentinel = d->cdr;
1258 exec_sentinel (Lisp_Object process, Lisp_Object reason)
1260 /* This function can GC */
1261 int speccount = specpdl_depth ();
1262 Lisp_Process *p = XPROCESS (process);
1263 Lisp_Object sentinel = p->sentinel;
1265 if (NILP (sentinel))
1268 /* Some weird FSFmacs crap here with
1269 Vdeactivate_mark and current_buffer->keymap */
1271 /* Zilch the sentinel while it's running, to avoid recursive invocations;
1272 assure that it gets restored no matter how the sentinel exits. */
1274 record_unwind_protect (exec_sentinel_unwind, noseeum_cons (process, sentinel));
1275 /* We used to bind inhibit-quit to t here, but call2_trapping_errors()
1276 does that for us. */
1277 running_asynch_code = 1;
1278 call2_trapping_errors ("Error in process sentinel", sentinel, process, reason);
1279 running_asynch_code = 0;
1280 restore_match_data ();
1281 unbind_to (speccount, Qnil);
1284 DEFUN ("set-process-sentinel", Fset_process_sentinel, 2, 2, 0, /*
1285 Give PROCESS the sentinel SENTINEL; nil for none.
1286 The sentinel is called as a function when the process changes state.
1287 It gets two arguments: the process, and a string describing the change.
1289 (process, sentinel))
1291 CHECK_PROCESS (process);
1292 XPROCESS (process)->sentinel = sentinel;
1296 DEFUN ("process-sentinel", Fprocess_sentinel, 1, 1, 0, /*
1297 Return the sentinel of PROCESS; nil if none.
1298 See `set-process-sentinel' for more info on sentinels.
1302 CHECK_PROCESS (process);
1303 return XPROCESS (process)->sentinel;
1308 signal_name (int signum)
1310 if (signum >= 0 && signum < NSIG)
1311 return (const char *) sys_siglist[signum];
1313 return (const char *) GETTEXT ("unknown signal");
1317 update_process_status (Lisp_Object p,
1318 Lisp_Object status_symbol,
1322 XPROCESS (p)->tick++;
1324 XPROCESS (p)->status_symbol = status_symbol;
1325 XPROCESS (p)->exit_code = exit_code;
1326 XPROCESS (p)->core_dumped = core_dumped;
1329 /* Return a string describing a process status list. */
1332 status_message (Lisp_Process *p)
1334 Lisp_Object symbol = p->status_symbol;
1335 int code = p->exit_code;
1336 int coredump = p->core_dumped;
1337 Lisp_Object string, string2;
1339 if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
1341 string = build_string (signal_name (code));
1343 string2 = build_translated_string (" (core dumped)\n");
1345 string2 = build_string ("\n");
1346 set_string_char (XSTRING (string), 0,
1347 DOWNCASE (current_buffer,
1348 string_char (XSTRING (string), 0)));
1349 return concat2 (string, string2);
1351 else if (EQ (symbol, Qexit))
1354 return build_translated_string ("finished\n");
1355 string = Fnumber_to_string (make_int (code));
1357 string2 = build_translated_string (" (core dumped)\n");
1359 string2 = build_string ("\n");
1360 return concat2 (build_translated_string ("exited abnormally with code "),
1361 concat2 (string, string2));
1364 return Fcopy_sequence (Fsymbol_name (symbol));
1367 /* Tell status_notify() to check for terminated processes. We do this
1368 because on some systems we sometimes miss SIGCHLD calls. (Not sure
1372 kick_status_notify (void)
1378 /* Report all recent events of a change in process status
1379 (either run the sentinel or output a message).
1380 This is done while Emacs is waiting for keyboard input. */
1383 status_notify (void)
1385 /* This function can GC */
1386 Lisp_Object tail = Qnil;
1387 Lisp_Object symbol = Qnil;
1388 Lisp_Object msg = Qnil;
1389 struct gcpro gcpro1, gcpro2, gcpro3;
1390 /* process_tick is volatile, so we have to remember it now.
1391 Otherwise, we get a race condition if SIGCHLD happens during
1394 (Actually, this is not the case anymore. The code to
1395 update the process structures has been moved out of the
1396 SIGCHLD handler. But for the moment I'm leaving this
1397 stuff in -- it can't hurt.) */
1398 int temp_process_tick;
1400 MAYBE_PROCMETH (reap_exited_processes, ());
1402 temp_process_tick = process_tick;
1404 if (update_tick == temp_process_tick)
1407 /* We need to gcpro tail; if read_process_output calls a filter
1408 which deletes a process and removes the cons to which tail points
1409 from Vprocess_alist, and then causes a GC, tail is an unprotected
1411 GCPRO3 (tail, symbol, msg);
1413 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
1415 Lisp_Object process = XCAR (tail);
1416 Lisp_Process *p = XPROCESS (process);
1417 /* p->tick is also volatile. Same thing as above applies. */
1418 int this_process_tick;
1420 /* #### extra check for terminated processes, in case a SIGCHLD
1421 got missed (this seems to happen sometimes, I'm not sure why).
1424 MAYBE_PROCMETH (update_status_if_terminated, (p));
1426 this_process_tick = p->tick;
1427 if (this_process_tick != p->update_tick)
1429 p->update_tick = this_process_tick;
1431 /* If process is still active, read any output that remains. */
1432 while (!EQ (p->filter, Qt)
1433 && read_process_output (process) > 0)
1436 /* Get the text to use for the message. */
1437 msg = status_message (p);
1439 /* If process is terminated, deactivate it or delete it. */
1440 symbol = p->status_symbol;
1442 if (EQ (symbol, Qsignal)
1443 || EQ (symbol, Qexit))
1445 if (delete_exited_processes)
1446 remove_process (process);
1448 deactivate_process (process);
1451 /* Now output the message suitably. */
1452 if (!NILP (p->sentinel))
1453 exec_sentinel (process, msg);
1454 /* Don't bother with a message in the buffer
1455 when a process becomes runnable. */
1456 else if (!EQ (symbol, Qrun) && !NILP (p->buffer))
1458 Lisp_Object old_read_only = Qnil;
1459 Lisp_Object old = Fcurrent_buffer ();
1461 struct gcpro ngcpro1, ngcpro2;
1463 /* Avoid error if buffer is deleted
1464 (probably that's why the process is dead, too) */
1465 if (!BUFFER_LIVE_P (XBUFFER (p->buffer)))
1468 NGCPRO2 (old, old_read_only);
1469 Fset_buffer (p->buffer);
1470 opoint = BUF_PT (current_buffer);
1471 /* Insert new output into buffer
1472 at the current end-of-output marker,
1473 thus preserving logical ordering of input and output. */
1474 if (XMARKER (p->mark)->buffer)
1475 BUF_SET_PT (current_buffer, marker_position (p->mark));
1477 BUF_SET_PT (current_buffer, BUF_ZV (current_buffer));
1478 if (BUF_PT (current_buffer) <= opoint)
1479 opoint += (string_char_length (XSTRING (msg))
1480 + string_char_length (XSTRING (p->name))
1483 old_read_only = current_buffer->read_only;
1484 current_buffer->read_only = Qnil;
1485 buffer_insert_c_string (current_buffer, "\nProcess ");
1486 Finsert (1, &p->name);
1487 buffer_insert_c_string (current_buffer, " ");
1489 current_buffer->read_only = old_read_only;
1490 Fset_marker (p->mark, make_int (BUF_PT (current_buffer)),
1493 opoint = bufpos_clip_to_bounds(BUF_BEGV (XBUFFER (p->buffer)),
1495 BUF_ZV (XBUFFER (p->buffer)));
1496 BUF_SET_PT (current_buffer, opoint);
1503 /* in case buffers use %s in modeline-format */
1504 MARK_MODELINE_CHANGED;
1507 update_tick = temp_process_tick;
1512 DEFUN ("process-status", Fprocess_status, 1, 1, 0, /*
1513 Return the status of PROCESS.
1514 This is a symbol, one of these:
1516 run -- for a process that is running.
1517 stop -- for a process stopped but continuable.
1518 exit -- for a process that has exited.
1519 signal -- for a process that has got a fatal signal.
1520 open -- for a network stream connection that is open.
1521 closed -- for a network stream connection that is closed.
1522 nil -- if arg is a process name and no such process exists.
1524 PROCESS may be a process, a buffer, the name of a process or buffer, or
1525 nil, indicating the current buffer's process.
1529 Lisp_Object status_symbol;
1531 if (STRINGP (process))
1532 process = Fget_process (process);
1534 process = get_process (process);
1539 status_symbol = XPROCESS (process)->status_symbol;
1540 if (network_connection_p (process))
1542 if (EQ (status_symbol, Qrun))
1543 status_symbol = Qopen;
1544 else if (EQ (status_symbol, Qexit))
1545 status_symbol = Qclosed;
1547 return status_symbol;
1550 DEFUN ("process-exit-status", Fprocess_exit_status, 1, 1, 0, /*
1551 Return the exit status of PROCESS or the signal number that killed it.
1552 If PROCESS has not yet exited or died, return 0.
1556 CHECK_PROCESS (process);
1557 return make_int (XPROCESS (process)->exit_code);
1563 decode_signal (Lisp_Object signal_)
1566 return XINT (signal_);
1571 CHECK_SYMBOL (signal_);
1572 name = string_data (XSYMBOL (signal_)->name);
1574 #define handle_signal(sym) do { \
1575 if (!strcmp ((const char *) name, #sym)) \
1579 handle_signal (SIGINT); /* ANSI */
1580 handle_signal (SIGILL); /* ANSI */
1581 handle_signal (SIGABRT); /* ANSI */
1582 handle_signal (SIGFPE); /* ANSI */
1583 handle_signal (SIGSEGV); /* ANSI */
1584 handle_signal (SIGTERM); /* ANSI */
1587 handle_signal (SIGHUP); /* POSIX */
1590 handle_signal (SIGQUIT); /* POSIX */
1593 handle_signal (SIGTRAP); /* POSIX */
1596 handle_signal (SIGKILL); /* POSIX */
1599 handle_signal (SIGUSR1); /* POSIX */
1602 handle_signal (SIGUSR2); /* POSIX */
1605 handle_signal (SIGPIPE); /* POSIX */
1608 handle_signal (SIGALRM); /* POSIX */
1611 handle_signal (SIGCHLD); /* POSIX */
1614 handle_signal (SIGCONT); /* POSIX */
1617 handle_signal (SIGSTOP); /* POSIX */
1620 handle_signal (SIGTSTP); /* POSIX */
1623 handle_signal (SIGTTIN); /* POSIX */
1626 handle_signal (SIGTTOU); /* POSIX */
1630 handle_signal (SIGBUS); /* XPG5 */
1633 handle_signal (SIGPOLL); /* XPG5 */
1636 handle_signal (SIGPROF); /* XPG5 */
1639 handle_signal (SIGSYS); /* XPG5 */
1642 handle_signal (SIGURG); /* XPG5 */
1645 handle_signal (SIGXCPU); /* XPG5 */
1648 handle_signal (SIGXFSZ); /* XPG5 */
1651 handle_signal (SIGVTALRM); /* XPG5 */
1655 handle_signal (SIGIO); /* BSD 4.2 */
1658 handle_signal (SIGWINCH); /* BSD 4.3 */
1662 handle_signal (SIGEMT);
1665 handle_signal (SIGINFO);
1668 handle_signal (SIGHWE);
1671 handle_signal (SIGPRE);
1674 handle_signal (SIGUME);
1677 handle_signal (SIGDLK);
1680 handle_signal (SIGCPULIM);
1683 handle_signal (SIGIOT);
1686 handle_signal (SIGLOST);
1689 handle_signal (SIGSTKFLT);
1692 handle_signal (SIGUNUSED);
1695 handle_signal (SIGDANGER); /* AIX */
1698 handle_signal (SIGMSG);
1701 handle_signal (SIGSOUND);
1704 handle_signal (SIGRETRACT);
1707 handle_signal (SIGGRANT);
1710 handle_signal (SIGPWR);
1713 #undef handle_signal
1715 error ("Undefined signal name %s", name);
1716 return 0; /* Unreached */
1720 /* Send signal number SIGNO to PROCESS.
1721 CURRENT-GROUP non-nil means send signal to the current
1722 foreground process group of the process's controlling terminal rather
1723 than to the process's own process group.
1724 This is used for various commands in shell mode.
1725 If NOMSG is zero, insert signal-announcements into process's buffers
1728 If we can, we try to signal PROCESS by sending control characters
1729 down the pty. This allows us to signal inferiors who have changed
1730 their uid, for which kill() would return an EPERM error, or to
1731 processes running on another computer through a remote login. */
1734 process_send_signal (Lisp_Object process, int signo,
1735 int current_group, int nomsg)
1737 /* This function can GC */
1738 process = get_process (process);
1740 if (network_connection_p (process))
1741 error ("Network connection %s is not a subprocess",
1742 XSTRING_DATA (XPROCESS(process)->name));
1743 CHECK_LIVE_PROCESS (process);
1745 MAYBE_PROCMETH (kill_child_process, (process, signo, current_group, nomsg));
1748 DEFUN ("process-send-signal", Fprocess_send_signal, 1, 3, 0, /*
1749 Send signal SIGNAL to process PROCESS.
1750 SIGNAL may be an integer, or a symbol naming a signal, like `SIGSEGV'.
1751 PROCESS may be a process, a buffer, the name of a process or buffer, or
1752 nil, indicating the current buffer's process.
1753 Third arg CURRENT-GROUP non-nil means send signal to the current
1754 foreground process group of the process's controlling terminal rather
1755 than to the process's own process group.
1756 If the process is a shell that supports job control, this means
1757 send the signal to the current subjob rather than the shell.
1759 (signal_, process, current_group))
1761 /* This function can GC */
1762 process_send_signal (process, decode_signal (signal_),
1763 !NILP (current_group), 0);
1767 DEFUN ("interrupt-process", Finterrupt_process, 0, 2, 0, /*
1768 Interrupt process PROCESS.
1769 See function `process-send-signal' for more details on usage.
1771 (process, current_group))
1773 /* This function can GC */
1774 process_send_signal (process, SIGINT, !NILP (current_group), 0);
1778 DEFUN ("kill-process", Fkill_process, 0, 2, 0, /*
1779 Kill process PROCESS.
1780 See function `process-send-signal' for more details on usage.
1782 (process, current_group))
1784 /* This function can GC */
1786 process_send_signal (process, SIGKILL, !NILP (current_group), 0);
1788 error ("kill-process: Not supported on this system");
1793 DEFUN ("quit-process", Fquit_process, 0, 2, 0, /*
1794 Send QUIT signal to process PROCESS.
1795 See function `process-send-signal' for more details on usage.
1797 (process, current_group))
1799 /* This function can GC */
1801 process_send_signal (process, SIGQUIT, !NILP (current_group), 0);
1803 error ("quit-process: Not supported on this system");
1808 DEFUN ("stop-process", Fstop_process, 0, 2, 0, /*
1809 Stop process PROCESS.
1810 See function `process-send-signal' for more details on usage.
1812 (process, current_group))
1814 /* This function can GC */
1816 process_send_signal (process, SIGTSTP, !NILP (current_group), 0);
1818 error ("stop-process: Not supported on this system");
1823 DEFUN ("continue-process", Fcontinue_process, 0, 2, 0, /*
1824 Continue process PROCESS.
1825 See function `process-send-signal' for more details on usage.
1827 (process, current_group))
1829 /* This function can GC */
1831 process_send_signal (process, SIGCONT, !NILP (current_group), 0);
1833 error ("continue-process: Not supported on this system");
1838 DEFUN ("signal-process", Fsignal_process, 2, 2,
1839 "nProcess number: \nnSignal code: ", /*
1840 Send the process with process id PID the signal with code SIGNAL.
1841 PID must be an integer. The process need not be a child of this Emacs.
1842 SIGNAL may be an integer, or a symbol naming a signal, like `SIGSEGV'.
1848 return make_int (PROCMETH_OR_GIVEN (kill_process_by_pid,
1849 (XINT (pid), decode_signal (signal_)),
1853 DEFUN ("process-send-eof", Fprocess_send_eof, 0, 1, 0, /*
1854 Make PROCESS see end-of-file in its input.
1855 PROCESS may be a process, a buffer, the name of a process or buffer, or
1856 nil, indicating the current buffer's process.
1857 If PROCESS is a network connection, or is a process communicating
1858 through a pipe (as opposed to a pty), then you cannot send any more
1859 text to PROCESS after you call this function.
1863 /* This function can GC */
1864 process = get_process (process);
1866 /* Make sure the process is really alive. */
1867 if (! EQ (XPROCESS (process)->status_symbol, Qrun))
1868 error ("Process %s not running", XSTRING_DATA (XPROCESS (process)->name));
1870 if (!MAYBE_INT_PROCMETH (process_send_eof, (process)))
1872 if (!NILP (DATA_OUTSTREAM (XPROCESS (process))))
1874 Lstream_close (XLSTREAM (DATA_OUTSTREAM (XPROCESS (process))));
1875 event_stream_delete_stream_pair (Qnil, XPROCESS (process)->pipe_outstream);
1876 XPROCESS (process)->pipe_outstream = Qnil;
1878 XPROCESS (process)->coding_outstream = Qnil;
1887 /************************************************************************/
1888 /* deleting a process */
1889 /************************************************************************/
1892 deactivate_process (Lisp_Object process)
1894 Lisp_Process *p = XPROCESS (process);
1897 /* It's possible that we got as far in the process-creation
1898 process as creating the descriptors but didn't get so
1899 far as selecting the process for input. In this
1900 case, p->pid is nil: p->pid is set at the same time that
1901 the process is selected for input. */
1902 /* #### The comment does not look correct. event_stream_unselect_process
1903 is guarded by process->selected, so this is not a problem. - kkm*/
1904 /* Must call this before setting the streams to nil */
1905 event_stream_unselect_process (p);
1907 if (!NILP (DATA_OUTSTREAM (p)))
1908 Lstream_close (XLSTREAM (DATA_OUTSTREAM (p)));
1909 if (!NILP (DATA_INSTREAM (p)))
1910 Lstream_close (XLSTREAM (DATA_INSTREAM (p)));
1912 /* Provide minimal implementation for deactivate_process
1913 if there's no process-specific one */
1914 if (HAS_PROCMETH_P (deactivate_process))
1915 usid = PROCMETH (deactivate_process, (p));
1917 usid = event_stream_delete_stream_pair (p->pipe_instream,
1920 if (usid != USID_DONTHASH)
1921 remhash ((const void*)usid, usid_to_process);
1923 p->pipe_instream = Qnil;
1924 p->pipe_outstream = Qnil;
1926 p->coding_instream = Qnil;
1927 p->coding_outstream = Qnil;
1932 remove_process (Lisp_Object process)
1934 Vprocess_list = delq_no_quit (process, Vprocess_list);
1935 Fset_marker (XPROCESS (process)->mark, Qnil, Qnil);
1937 deactivate_process (process);
1940 DEFUN ("delete-process", Fdelete_process, 1, 1, 0, /*
1941 Delete PROCESS: kill it and forget about it immediately.
1942 PROCESS may be a process or the name of one, or a buffer name.
1946 /* This function can GC */
1948 process = get_process (process);
1949 p = XPROCESS (process);
1950 if (network_connection_p (process))
1952 p->status_symbol = Qexit;
1958 else if (PROCESS_LIVE_P (p))
1960 Fkill_process (process, Qnil);
1961 /* Do this now, since remove_process will make sigchld_handler do nothing. */
1962 p->status_symbol = Qsignal;
1963 p->exit_code = SIGKILL;
1969 remove_process (process);
1973 /* Kill all processes associated with `buffer'.
1974 If `buffer' is nil, kill all processes */
1977 kill_buffer_processes (Lisp_Object buffer)
1979 LIST_LOOP_2 (process, Vprocess_list)
1980 if ((NILP (buffer) || EQ (XPROCESS (process)->buffer, buffer)))
1982 if (network_connection_p (process))
1983 Fdelete_process (process);
1984 else if (PROCESS_LIVE_P (XPROCESS (process)))
1985 process_send_signal (process, SIGHUP, 0, 1);
1989 DEFUN ("process-kill-without-query", Fprocess_kill_without_query, 1, 2, 0, /*
1990 Say no query needed if PROCESS is running when Emacs is exited.
1991 Optional second argument if non-nil says to require a query.
1992 Value is t if a query was formerly required.
1994 (process, require_query_p))
1998 CHECK_PROCESS (process);
1999 tem = XPROCESS (process)->kill_without_query;
2000 XPROCESS (process)->kill_without_query = NILP (require_query_p);
2002 return tem ? Qnil : Qt;
2005 DEFUN ("process-kill-without-query-p", Fprocess_kill_without_query_p, 1, 1, 0, /*
2006 Return t if PROCESS will be killed without query when emacs is exited.
2010 CHECK_PROCESS (process);
2011 return XPROCESS (process)->kill_without_query ? Qt : Qnil;
2015 /* This is not named init_process in order to avoid a conflict with NS 3.3 */
2017 init_xemacs_process (void)
2019 MAYBE_PROCMETH (init_process, ());
2021 Vprocess_list = Qnil;
2023 if (usid_to_process)
2024 clrhash (usid_to_process);
2026 usid_to_process = make_hash_table (32);
2031 xxDEFUN ("process-connection", Fprocess_connection, 0, 1, 0, /*
2032 Return the connection type of `PROCESS'. This can be nil (pipe),
2033 t or pty (pty) or stream (socket connection).
2037 return XPROCESS (process)->type;
2043 syms_of_process (void)
2045 INIT_LRECORD_IMPLEMENTATION (process);
2047 defsymbol (&Qprocessp, "processp");
2048 defsymbol (&Qprocess_live_p, "process-live-p");
2050 /* see comment at Fprocess_readable_p */
2051 defsymbol (&Qprocess_readable_p, "process-readable-p");
2053 defsymbol (&Qrun, "run");
2054 defsymbol (&Qstop, "stop");
2055 defsymbol (&Qopen, "open");
2056 defsymbol (&Qclosed, "closed");
2058 defsymbol (&Qtcp, "tcp");
2059 defsymbol (&Qudp, "udp");
2061 #ifdef HAVE_MULTICAST
2062 defsymbol(&Qmulticast, "multicast"); /* Used for occasional warnings */
2065 DEFSUBR (Fprocessp);
2066 DEFSUBR (Fprocess_live_p);
2068 /* see comment at Fprocess_readable_p */
2069 DEFSUBR (Fprocess_readable_p);
2071 DEFSUBR (Fget_process);
2072 DEFSUBR (Fget_buffer_process);
2073 DEFSUBR (Fdelete_process);
2074 DEFSUBR (Fprocess_status);
2075 DEFSUBR (Fprocess_exit_status);
2076 DEFSUBR (Fprocess_id);
2077 DEFSUBR (Fprocess_name);
2078 DEFSUBR (Fprocess_tty_name);
2079 DEFSUBR (Fprocess_command);
2080 DEFSUBR (Fset_process_buffer);
2081 DEFSUBR (Fprocess_buffer);
2082 DEFSUBR (Fprocess_mark);
2083 DEFSUBR (Fset_process_filter);
2084 DEFSUBR (Fprocess_filter);
2085 DEFSUBR (Fset_process_window_size);
2086 DEFSUBR (Fset_process_sentinel);
2087 DEFSUBR (Fprocess_sentinel);
2088 DEFSUBR (Fprocess_kill_without_query);
2089 DEFSUBR (Fprocess_kill_without_query_p);
2090 DEFSUBR (Fprocess_list);
2091 DEFSUBR (Fstart_process_internal);
2093 DEFSUBR (Fopen_network_stream_internal);
2094 #ifdef HAVE_MULTICAST
2095 DEFSUBR (Fopen_multicast_group_internal);
2096 #endif /* HAVE_MULTICAST */
2097 #endif /* HAVE_SOCKETS */
2098 DEFSUBR (Fprocess_send_region);
2099 DEFSUBR (Fprocess_send_string);
2100 DEFSUBR (Fprocess_send_signal);
2101 DEFSUBR (Finterrupt_process);
2102 DEFSUBR (Fkill_process);
2103 DEFSUBR (Fquit_process);
2104 DEFSUBR (Fstop_process);
2105 DEFSUBR (Fcontinue_process);
2106 DEFSUBR (Fprocess_send_eof);
2107 DEFSUBR (Fsignal_process);
2108 /* DEFSUBR (Fprocess_connection); */
2110 DEFSUBR (Fprocess_input_coding_system);
2111 DEFSUBR (Fprocess_output_coding_system);
2112 DEFSUBR (Fset_process_input_coding_system);
2113 DEFSUBR (Fset_process_output_coding_system);
2114 DEFSUBR (Fprocess_coding_system);
2115 DEFSUBR (Fset_process_coding_system);
2116 #endif /* FILE_CODING */
2120 vars_of_process (void)
2122 Fprovide (intern ("subprocesses"));
2124 Fprovide (intern ("network-streams"));
2125 #ifdef HAVE_MULTICAST
2126 Fprovide (intern ("multicast"));
2127 #endif /* HAVE_MULTICAST */
2128 #endif /* HAVE_SOCKETS */
2129 staticpro (&Vprocess_list);
2131 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes /*
2132 *Non-nil means delete processes immediately when they exit.
2133 nil means don't delete them until `list-processes' is run.
2136 delete_exited_processes = 1;
2138 DEFVAR_CONST_LISP ("null-device", &Vnull_device /*
2139 Name of the null device, which differs from system to system.
2140 The null device is a filename that acts as a sink for arbitrary amounts of
2141 data, which is discarded, or as a source for a zero-length file.
2142 It is available on all the systems that we currently support, but with
2143 different names (typically either `/dev/null' or `nul').
2145 Note that there is also a /dev/zero on most modern Unix versions (including
2146 Cygwin), which acts like /dev/null when used as a sink, but as a source
2147 it sends a non-ending stream of zero bytes. It's used most often along
2148 with memory-mapping. We don't provide a Lisp variable for this because
2149 the operations needing this are lower level than what ELisp programs
2150 typically do, and in any case no equivalent exists under native MS Windows.
2152 Vnull_device = build_string (NULL_DEVICE);
2154 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type /*
2155 Control type of device used to communicate with subprocesses.
2156 Values are nil to use a pipe, or t or `pty' to use a pty.
2157 The value has no effect if the system has no ptys or if all ptys are busy:
2158 then a pipe is used in any case.
2159 The value takes effect when `start-process' is called.
2161 Vprocess_connection_type = Qt;
2163 DEFVAR_BOOL ("windowed-process-io", &windowed_process_io /*
2164 Enables input/output on standard handles of a windowed process.
2165 When this variable is nil (the default), XEmacs does not attempt to read
2166 standard output handle of a windowed process. Instead, the process is
2167 immediately marked as exited immediately upon successful launching. This is
2168 done because normal windowed processes do not use standard I/O, as they are
2169 not connected to any console.
2171 When launching a specially crafted windowed process, which expects to be
2172 launched by XEmacs, or by other program which pipes its standard input and
2173 output, this variable must be set to non-nil, in which case XEmacs will
2174 treat this process just like a console process.
2176 NOTE: You should never set this variable, only bind it.
2178 Only Windows processes can be "windowed" or "console". This variable has no
2179 effect on UNIX processes, because all UNIX processes are "console".
2181 windowed_process_io = 0;
2183 #ifdef PROCESS_IO_BLOCKING
2184 DEFVAR_LISP ("network-stream-blocking-port-list", &network_stream_blocking_port_list /*
2185 List of port numbers or port names to set a blocking I/O mode with connection.
2186 Nil value means to set a default(non-blocking) I/O mode.
2187 The value takes effect when `open-network-stream-internal' is called.
2189 network_stream_blocking_port_list = Qnil;
2190 #endif /* PROCESS_IO_BLOCKING */
2193 #endif /* not NO_SUBPROCESSES */