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;
114 mark_process (Lisp_Object obj)
116 Lisp_Process *proc = XPROCESS (obj);
117 MAYBE_PROCMETH (mark_process_data, (proc));
118 mark_object (proc->name);
119 mark_object (proc->command);
120 mark_object (proc->filter);
121 mark_object (proc->sentinel);
122 mark_object (proc->buffer);
123 mark_object (proc->mark);
124 mark_object (proc->pid);
125 mark_object (proc->pipe_instream);
126 mark_object (proc->pipe_outstream);
128 mark_object (proc->coding_instream);
129 mark_object (proc->coding_outstream);
131 return proc->status_symbol;
135 print_process (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
137 Lisp_Process *proc = XPROCESS (obj);
140 error ("printing unreadable object #<process %s>",
141 XSTRING_DATA (proc->name));
145 print_internal (proc->name, printcharfun, 0);
149 int netp = network_connection_p (obj);
150 write_c_string ((netp ? GETTEXT ("#<network connection ") :
151 GETTEXT ("#<process ")), printcharfun);
152 print_internal (proc->name, printcharfun, 1);
153 write_c_string ((netp ? " " : " pid "), printcharfun);
154 print_internal (proc->pid, printcharfun, 1);
155 write_c_string (" state:", printcharfun);
156 print_internal (proc->status_symbol, printcharfun, 1);
157 MAYBE_PROCMETH (print_process_data, (proc, printcharfun));
158 write_c_string (">", printcharfun);
162 #ifdef HAVE_WINDOW_SYSTEM
163 extern void debug_process_finalization (Lisp_Process *p);
164 #endif /* HAVE_WINDOW_SYSTEM */
167 finalize_process (void *header, int for_disksave)
169 /* #### this probably needs to be tied into the tty event loop */
170 /* #### when there is one */
171 Lisp_Process *p = (Lisp_Process *) header;
172 #ifdef HAVE_WINDOW_SYSTEM
175 debug_process_finalization (p);
177 #endif /* HAVE_WINDOW_SYSTEM */
181 MAYBE_PROCMETH (finalize_process_data, (p, for_disksave));
183 xfree (p->process_data);
187 DEFINE_LRECORD_IMPLEMENTATION ("process", process,
188 mark_process, print_process, finalize_process,
189 0, 0, 0, Lisp_Process);
191 /************************************************************************/
192 /* basic process accessors */
193 /************************************************************************/
195 /* Under FILE_CODING, this function returns low-level streams, connected
196 directly to the child process, rather than en/decoding FILE_CODING
199 get_process_streams (Lisp_Process *p, Lisp_Object *instr, Lisp_Object *outstr)
202 assert (NILP (p->pipe_instream) || LSTREAMP(p->pipe_instream));
203 assert (NILP (p->pipe_outstream) || LSTREAMP(p->pipe_outstream));
204 *instr = p->pipe_instream;
205 *outstr = p->pipe_outstream;
209 get_process_from_usid (USID usid)
213 assert (usid != USID_ERROR && usid != USID_DONTHASH);
215 if (gethash ((const void*)usid, usid_to_process, &vval))
218 CVOID_TO_LISP (proc, vval);
219 return XPROCESS (proc);
226 get_process_selected_p (Lisp_Process *p)
232 set_process_selected_p (Lisp_Process *p, int selected_p)
234 p->selected = !!selected_p;
238 connected_via_filedesc_p (Lisp_Process *p)
240 return MAYBE_INT_PROCMETH (tooltalk_connection_p, (p));
245 network_connection_p (Lisp_Object process)
247 return CONSP (XPROCESS (process)->pid);
251 DEFUN ("processp", Fprocessp, 1, 1, 0, /*
252 Return t if OBJECT is a process.
256 return PROCESSP (obj) ? Qt : Qnil;
259 DEFUN ("process-live-p", Fprocess_live_p, 1, 1, 0, /*
260 Return t if OBJECT is a process that is alive.
264 return PROCESSP (obj) && PROCESS_LIVE_P (XPROCESS (obj)) ? Qt : Qnil;
267 DEFUN ("process-list", Fprocess_list, 0, 0, 0, /*
268 Return a list of all processes.
272 return Fcopy_sequence (Vprocess_list);
275 DEFUN ("get-process", Fget_process, 1, 1, 0, /*
276 Return the process named NAME, or nil if there is none.
286 /* this only gets called during GC when emacs is going away as a result
287 of a signal or crash. */
290 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
292 Lisp_Object proc = XCAR (tail);
294 if (internal_equal (name, XPROCESS (proc)->name, 0))
300 DEFUN ("get-buffer-process", Fget_buffer_process, 1, 1, 0, /*
301 Return the (or, a) process associated with BUFFER.
302 BUFFER may be a buffer or the name of one.
306 Lisp_Object buf, tail, proc;
308 if (NILP (name)) return Qnil;
309 buf = Fget_buffer (name);
310 if (NILP (buf)) return Qnil;
312 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
314 /* jwz: do not quit here - it isn't necessary, as there is no way for
315 Vprocess_list to get circular or overwhelmingly long, and this
316 function is called from layout_mode_element under redisplay. */
319 if (PROCESSP (proc) && EQ (XPROCESS (proc)->buffer, buf))
325 /* This is how commands for the user decode process arguments. It
326 accepts a process, a process name, a buffer, a buffer name, or nil.
327 Buffers denote the first process in the buffer, and nil denotes the
331 get_process (Lisp_Object name)
333 Lisp_Object proc, obj;
336 /* #### Look more closely into translating process names. */
339 /* This may be called during a GC from process_send_signal() from
340 kill_buffer_processes() if emacs decides to abort(). */
346 obj = Fget_process (name);
348 obj = Fget_buffer (name);
350 error ("Process %s does not exist", XSTRING_DATA (name));
352 else if (NILP (name))
353 obj = Fcurrent_buffer ();
357 /* Now obj should be either a buffer object or a process object.
361 proc = Fget_buffer_process (obj);
363 error ("Buffer %s has no process", XSTRING_DATA (XBUFFER(obj)->name));
367 /* #### This was commented out. Although, simple
368 (kill-process 7 "qqq") resulted in a fatal error. - kkm */
375 DEFUN ("process-id", Fprocess_id, 1, 1, 0, /*
376 Return the process id of PROCESS.
377 This is the pid of the Unix process which PROCESS uses or talks to.
378 For a network connection, this value is a cons of
379 (foreign-network-port . foreign-host-name).
384 CHECK_PROCESS (proc);
386 pid = XPROCESS (proc)->pid;
387 if (network_connection_p (proc))
389 return Fcons (Fcar (pid), Fcdr (pid));
394 DEFUN ("process-name", Fprocess_name, 1, 1, 0, /*
395 Return the name of PROCESS, as a string.
396 This is the name of the program invoked in PROCESS,
397 possibly modified to make it unique among process names.
401 CHECK_PROCESS (proc);
402 return XPROCESS (proc)->name;
405 DEFUN ("process-command", Fprocess_command, 1, 1, 0, /*
406 Return the command that was executed to start PROCESS.
407 This is a list of strings, the first string being the program executed
408 and the rest of the strings being the arguments given to it.
412 CHECK_PROCESS (proc);
413 return XPROCESS (proc)->command;
417 /************************************************************************/
418 /* creating a process */
419 /************************************************************************/
422 make_process_internal (Lisp_Object name)
424 Lisp_Object val, name1;
426 Lisp_Process *p = alloc_lcrecord_type (Lisp_Process, &lrecord_process);
428 /* If name is already in use, modify it until it is unused. */
433 Lisp_Object tem = Fget_process (name1);
436 sprintf (suffix, "<%d>", i);
437 name1 = concat2 (name, build_string (suffix));
446 p->mark = Fmake_marker ();
448 p->status_symbol = Qrun;
451 p->filter_does_read = 0;
452 p->kill_without_query = 0;
456 p->pipe_instream = Qnil;
457 p->pipe_outstream = Qnil;
459 p->coding_instream = Qnil;
460 p->coding_outstream = Qnil;
464 MAYBE_PROCMETH (alloc_process_data, (p));
466 XSETPROCESS (val, p);
468 Vprocess_list = Fcons (val, Vprocess_list);
473 init_process_io_handles (Lisp_Process *p, void* in, void* out, int flags)
475 USID usid = event_stream_create_stream_pair (in, out,
476 &p->pipe_instream, &p->pipe_outstream,
479 if (usid == USID_ERROR)
480 report_file_error ("Setting up communication with subprocess", Qnil);
482 if (usid != USID_DONTHASH)
484 Lisp_Object proc = Qnil;
485 XSETPROCESS (proc, p);
486 puthash ((const void*)usid, LISP_TO_VOID (proc), usid_to_process);
489 MAYBE_PROCMETH (init_process_io_handles, (p, in, out, flags));
492 p->coding_instream = make_decoding_input_stream
493 (XLSTREAM (p->pipe_instream),
494 Fget_coding_system (Vcoding_system_for_read));
495 Lstream_set_character_mode (XLSTREAM (p->coding_instream));
496 p->coding_outstream = make_encoding_output_stream
497 (XLSTREAM (p->pipe_outstream),
498 Fget_coding_system (Vcoding_system_for_write));
499 /* CODE_CNTL (&out_state[outchannel]) |= CC_END; !!####
500 What's going on here? */
501 #endif /* FILE_CODING */
505 create_process (Lisp_Object process, Lisp_Object *argv, int nargv,
506 Lisp_Object program, Lisp_Object cur_dir)
508 Lisp_Process *p = XPROCESS (process);
511 /* *_create_process may change status_symbol, if the process
512 is a kind of "fire-and-forget" (no I/O, unwaitable) */
513 p->status_symbol = Qrun;
516 pid = PROCMETH (create_process, (p, argv, nargv, program, cur_dir));
518 p->pid = make_int (pid);
519 if (PROCESS_LIVE_P (p))
520 event_stream_select_process (p);
523 /* This function is the unwind_protect form for Fstart_process_internal. If
524 PROC doesn't have its pid set, then we know someone has signalled
525 an error and the process wasn't started successfully, so we should
526 remove it from the process list. */
527 static void remove_process (Lisp_Object proc);
529 start_process_unwind (Lisp_Object proc)
531 /* Was PROC started successfully? */
532 if (EQ (XPROCESS (proc)->pid, Qnil))
533 remove_process (proc);
537 DEFUN ("start-process-internal", Fstart_process_internal, 3, MANY, 0, /*
538 Start a program in a subprocess. Return the process object for it.
539 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS
540 NAME is name for process. It is modified if necessary to make it unique.
541 BUFFER is the buffer or (buffer-name) to associate with the process.
542 Process output goes at end of that buffer, unless you specify
543 an output stream or filter function to handle the output.
544 BUFFER may be also nil, meaning that this process is not associated
546 Third arg is program file name. It is searched for as in the shell.
547 Remaining arguments are strings to give program as arguments.
548 INCODE and OUTCODE specify the coding-system objects used in input/output
551 (int nargs, Lisp_Object *args))
553 /* This function can call lisp */
554 /* !!#### This function has not been Mule-ized */
555 Lisp_Object buffer, name, program, proc, current_dir;
557 int speccount = specpdl_depth ();
558 struct gcpro gcpro1, gcpro2, gcpro3;
565 /* Protect against various file handlers doing GCs below. */
566 GCPRO3 (buffer, program, current_dir);
569 buffer = Fget_buffer_create (buffer);
572 CHECK_STRING (program);
574 /* Make sure that the child will be able to chdir to the current
575 buffer's current directory, or its unhandled equivalent. We
576 can't just have the child check for an error when it does the
577 chdir, since it's in a vfork.
579 Note: these assignments and calls are like this in order to insure
580 "caller protects args" GC semantics. */
581 current_dir = current_buffer->directory;
582 current_dir = Funhandled_file_name_directory (current_dir);
583 current_dir = expand_and_dir_to_file (current_dir, Qnil);
585 #if 0 /* This loser breaks ange-ftp */
586 /* dmoore - if you re-enable this code, you have to gcprotect
587 current_buffer through the above calls. */
588 if (NILP (Ffile_accessible_directory_p (current_dir)))
589 report_file_error ("Setting current directory",
590 list1 (current_buffer->directory));
593 /* If program file name is not absolute, search our path for it */
594 if (!IS_DIRECTORY_SEP (XSTRING_BYTE (program, 0))
595 && !(XSTRING_LENGTH (program) > 1
596 && IS_DEVICE_SEP (XSTRING_BYTE (program, 1))))
598 struct gcpro ngcpro1;
602 locate_file (Vexec_path, program, Vlisp_EXEC_SUFFIXES, &tem, X_OK);
604 report_file_error ("Searching for program", list1 (program));
605 program = Fexpand_file_name (tem, Qnil);
610 if (!NILP (Ffile_directory_p (program)))
611 error ("Specified program for new process is a directory");
614 proc = make_process_internal (name);
616 XPROCESS (proc)->buffer = buffer;
617 XPROCESS (proc)->command = Flist (nargs - 2,
620 /* Make the process marker point into the process buffer (if any). */
622 Fset_marker (XPROCESS (proc)->mark,
623 make_int (BUF_ZV (XBUFFER (buffer))), buffer);
625 /* If an error occurs and we can't start the process, we want to
626 remove it from the process list. This means that each error
627 check in create_process doesn't need to call remove_process
628 itself; it's all taken care of here. */
629 record_unwind_protect (start_process_unwind, proc);
631 create_process (proc, args + 3, nargs - 3, program, current_dir);
634 return unbind_to (speccount, proc);
641 /* #### The network support is fairly synthetical. What we actually
642 need is a single function, which supports all datagram, stream and
643 packet stream connections, arbitrary protocol families should they
644 be supported by the target system, multicast groups, in both data
645 and control rooted/nonrooted flavors, service quality etc whatever
646 is supported by the underlying network.
648 It must accept a property list describing the connection. The current
649 functions must then go to lisp and provide a suitable list for the
650 generalized connection function.
652 Both UNIX and Win32 support BSD sockets, and there are many extensions
653 available (Sockets 2 spec).
655 A todo is define a consistent set of properties abstracting a
656 network connection. -kkm
660 /* open a TCP network connection to a given HOST/SERVICE. Treated
661 exactly like a normal process when reading and writing. Only
662 differences are in status display and process deletion. A network
663 connection has no PID; you cannot signal it. All you can do is
664 deactivate and close it via delete-process */
666 DEFUN ("open-network-stream-internal", Fopen_network_stream_internal, 4, 5, 0, /*
667 Open a TCP connection for a service to a host.
668 Return a subprocess-object to represent the connection.
669 Input and output work as for subprocesses; `delete-process' closes it.
671 NAME is name for process. It is modified if necessary to make it unique.
672 BUFFER is the buffer (or buffer-name) to associate with the process.
673 Process output goes at end of that buffer, unless you specify
674 an output stream or filter function to handle the output.
675 BUFFER may also be nil, meaning that this process is not associated
677 Third arg is name of the host to connect to, or its IP address.
678 Fourth arg SERVICE is name of the service desired, or an integer
679 specifying a port number to connect to.
680 Fifth argument PROTOCOL is a network protocol. Currently 'tcp
681 (Transmission Control Protocol) and 'udp (User Datagram Protocol) are
682 supported. When omitted, 'tcp is assumed.
684 Ouput via `process-send-string' and input via buffer or filter (see
685 `set-process-filter') are stream-oriented. That means UDP datagrams are
686 not guaranteed to be sent and received in discrete packets. (But small
687 datagrams around 500 bytes that are not truncated by `process-send-string'
688 are usually fine.) Note further that UDP protocol does not guard against
691 (name, buffer, host, service, protocol))
693 /* !!#### This function has not been Mule-ized */
694 /* This function can GC */
695 Lisp_Object proc = Qnil;
696 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, ngcpro1;
699 GCPRO5 (name, buffer, host, service, protocol);
705 CHECK_SYMBOL (protocol);
707 /* Since this code is inside HAVE_SOCKETS, existence of
708 open_network_stream is mandatory */
709 PROCMETH (open_network_stream, (name, host, service, protocol,
713 buffer = Fget_buffer_create (buffer);
714 proc = make_process_internal (name);
717 XPROCESS (proc)->pid = Fcons (service, host);
718 XPROCESS (proc)->buffer = buffer;
719 init_process_io_handles (XPROCESS (proc), (void*)inch, (void*)outch,
720 STREAM_NETWORK_CONNECTION);
722 event_stream_select_process (XPROCESS (proc));
729 #ifdef HAVE_MULTICAST
731 DEFUN ("open-multicast-group-internal", Fopen_multicast_group_internal, 5, 5, 0, /*
732 Open a multicast connection on the specified dest/port/ttl.
733 Return a subprocess-object to represent the connection.
734 Input and output work as for subprocesses; `delete-process' closes it.
736 NAME is name for process. It is modified if necessary to make it unique.
737 BUFFER is the buffer (or buffer-name) to associate with the process.
738 Process output goes at end of that buffer, unless you specify
739 an output stream or filter function to handle the output.
740 BUFFER may also be nil, meaning that this process is not associated
742 Third, fourth and fifth args are the multicast destination group, port and ttl.
743 dest must be an internet address between 224.0.0.0 and 239.255.255.255
744 port is a communication port like in traditional unicast
745 ttl is the time-to-live (15 for site, 63 for region and 127 for world)
747 (name, buffer, dest, port, ttl))
749 /* !!#### This function has not been Mule-ized */
750 /* This function can GC */
751 Lisp_Object proc = Qnil;
757 /* Since this code is inside HAVE_MULTICAST, existence of
758 open_network_stream is mandatory */
759 PROCMETH (open_multicast_group, (name, dest, port, ttl,
763 buffer = Fget_buffer_create (buffer);
765 proc = make_process_internal (name);
768 XPROCESS (proc)->pid = Fcons (port, dest);
769 XPROCESS (proc)->buffer = buffer;
770 init_process_io_handles (XPROCESS (proc), (void*)inch, (void*)outch,
771 STREAM_NETWORK_CONNECTION);
773 event_stream_select_process (XPROCESS (proc));
778 #endif /* HAVE_MULTICAST */
780 #endif /* HAVE_SOCKETS */
783 canonicalize_host_name (Lisp_Object host)
785 return PROCMETH_OR_GIVEN (canonicalize_host_name, (host), host);
789 DEFUN ("set-process-window-size", Fset_process_window_size, 3, 3, 0, /*
790 Tell PROCESS that it has logical window size HEIGHT and WIDTH.
792 (proc, height, width))
794 CHECK_PROCESS (proc);
795 CHECK_NATNUM (height);
796 CHECK_NATNUM (width);
798 MAYBE_INT_PROCMETH (set_window_size, (XPROCESS (proc), XINT (height), XINT (width))) <= 0
803 /************************************************************************/
805 /************************************************************************/
807 /* Read pending output from the process channel,
808 starting with our buffered-ahead character if we have one.
809 Yield number of characters read.
811 This function reads at most 1024 bytes.
812 If you want to read all available subprocess output,
813 you must call it repeatedly until it returns zero. */
816 read_process_output (Lisp_Object proc)
818 /* This function can GC */
819 Bytecount nbytes, nchars;
821 Lisp_Object outstream;
822 Lisp_Process *p = XPROCESS (proc);
824 /* If there is a lot of output from the subprocess, the loop in
825 execute_internal_event() might call read_process_output() more
826 than once. If the filter that was executed from one of these
827 calls set the filter to t, we have to stop now. Return -1 rather
828 than 0 so execute_internal_event() doesn't close the process.
829 Really, the loop in execute_internal_event() should check itself
830 for a process-filter change, like in status_notify(); but the
831 struct Lisp_Process is not exported outside of this file. */
832 if (!PROCESS_LIVE_P (p))
833 return -1; /* already closed */
835 if (!NILP (p->filter) && (p->filter_does_read))
837 Lisp_Object filter_result;
839 /* Some weird FSFmacs crap here with
840 Vdeactivate_mark and current_buffer->keymap */
841 running_asynch_code = 1;
842 filter_result = call2_trapping_errors ("Error in process filter",
843 p->filter, proc, Qnil);
844 running_asynch_code = 0;
845 restore_match_data ();
846 CHECK_INT (filter_result);
847 return XINT (filter_result);
850 nbytes = Lstream_read (XLSTREAM (DATA_INSTREAM(p)), chars, sizeof (chars));
851 if (nbytes <= 0) return nbytes;
853 nchars = bytecount_to_charcount (chars, nbytes);
854 outstream = p->filter;
855 if (!NILP (outstream))
857 /* We used to bind inhibit-quit to t here, but
858 call2_trapping_errors() does that for us. */
859 running_asynch_code = 1;
860 call2_trapping_errors ("Error in process filter",
861 outstream, proc, make_string (chars, nbytes));
862 running_asynch_code = 0;
863 restore_match_data ();
867 /* If no filter, write into buffer if it isn't dead. */
868 if (!NILP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
870 Lisp_Object old_read_only = Qnil;
874 int old_zmacs_region_stays = zmacs_region_stays;
875 struct gcpro gcpro1, gcpro2;
876 struct buffer *buf = XBUFFER (p->buffer);
878 GCPRO2 (proc, old_read_only);
880 old_point = BUF_PT (buf);
881 old_begv = BUF_BEGV (buf);
882 old_zv = BUF_ZV (buf);
883 old_read_only = buf->read_only;
884 buf->read_only = Qnil;
886 /* Insert new output into buffer
887 at the current end-of-output marker,
888 thus preserving logical ordering of input and output. */
889 if (XMARKER (p->mark)->buffer)
891 bufpos_clip_to_bounds (old_begv, marker_position (p->mark),
894 BUF_SET_PT (buf, old_zv);
896 /* If the output marker is outside of the visible region, save
897 the restriction and widen. */
898 if (! (BUF_BEGV (buf) <= BUF_PT (buf) &&
899 BUF_PT (buf) <= BUF_ZV (buf)))
902 /* Make sure opoint floats ahead of any new text, just as point
904 if (BUF_PT (buf) <= old_point)
907 /* Insert after old_begv, but before old_zv. */
908 if (BUF_PT (buf) < old_begv)
910 if (BUF_PT (buf) <= old_zv)
914 /* This screws up initial display of the window. jla */
916 /* Insert before markers in case we are inserting where
917 the buffer's mark is, and the user's next command is Meta-y. */
918 buffer_insert_raw_string_1 (buf, -1, chars,
919 nbytes, INSDEL_BEFORE_MARKERS);
921 buffer_insert_raw_string (buf, chars, nbytes);
924 Fset_marker (p->mark, make_int (BUF_PT (buf)), p->buffer);
926 MARK_MODELINE_CHANGED;
928 /* If the restriction isn't what it should be, set it. */
929 if (old_begv != BUF_BEGV (buf) || old_zv != BUF_ZV (buf))
932 old_begv = bufpos_clip_to_bounds (BUF_BEG (buf),
935 old_zv = bufpos_clip_to_bounds (BUF_BEG (buf),
938 Fnarrow_to_region (make_int (old_begv), make_int (old_zv),
942 /* Handling the process output should not deactivate the mark. */
943 zmacs_region_stays = old_zmacs_region_stays;
944 buf->read_only = old_read_only;
945 old_point = bufpos_clip_to_bounds (BUF_BEGV (buf),
948 BUF_SET_PT (buf, old_point);
955 /* Sending data to subprocess */
957 /* send some data to process PROC. If NONRELOCATABLE is non-NULL, it
958 specifies the address of the data. Otherwise, the data comes from the
959 object RELOCATABLE (either a string or a buffer). START and LEN
960 specify the offset and length of the data to send.
962 Note that START and LEN are in Bufpos's if RELOCATABLE is a buffer,
963 and in Bytecounts otherwise. */
966 send_process (Lisp_Object proc,
967 Lisp_Object relocatable, const Bufbyte *nonrelocatable,
970 /* This function can GC */
971 struct gcpro gcpro1, gcpro2;
972 Lisp_Object lstream = Qnil;
974 GCPRO2 (proc, lstream);
976 if (NILP (DATA_OUTSTREAM (XPROCESS (proc))))
977 signal_simple_error ("Process not open for writing", proc);
981 make_fixed_buffer_input_stream (nonrelocatable + start, len);
982 else if (BUFFERP (relocatable))
983 lstream = make_lisp_buffer_input_stream (XBUFFER (relocatable),
984 start, start + len, 0);
986 lstream = make_lisp_string_input_stream (relocatable, start, len);
988 PROCMETH (send_process, (proc, XLSTREAM (lstream)));
991 Lstream_delete (XLSTREAM (lstream));
994 DEFUN ("process-tty-name", Fprocess_tty_name, 1, 1, 0, /*
995 Return the name of the terminal PROCESS uses, or nil if none.
996 This is the terminal that the process itself reads and writes on,
997 not the name of the pty that Emacs uses to talk with that terminal.
1001 CHECK_PROCESS (proc);
1002 return MAYBE_LISP_PROCMETH (get_tty_name, (XPROCESS (proc)));
1005 DEFUN ("set-process-buffer", Fset_process_buffer, 2, 2, 0, /*
1006 Set buffer associated with PROCESS to BUFFER (a buffer, or nil).
1010 CHECK_PROCESS (proc);
1012 CHECK_BUFFER (buffer);
1013 XPROCESS (proc)->buffer = buffer;
1017 DEFUN ("process-buffer", Fprocess_buffer, 1, 1, 0, /*
1018 Return the buffer PROCESS is associated with.
1019 Output from PROCESS is inserted in this buffer
1020 unless PROCESS has a filter.
1024 CHECK_PROCESS (proc);
1025 return XPROCESS (proc)->buffer;
1028 DEFUN ("process-mark", Fprocess_mark, 1, 1, 0, /*
1029 Return the marker for the end of the last output from PROCESS.
1033 CHECK_PROCESS (proc);
1034 return XPROCESS (proc)->mark;
1038 set_process_filter (Lisp_Object proc, Lisp_Object filter, int filter_does_read)
1040 CHECK_PROCESS (proc);
1041 if (PROCESS_LIVE_P (XPROCESS (proc))) {
1042 if (EQ (filter, Qt))
1043 event_stream_unselect_process (XPROCESS (proc));
1045 event_stream_select_process (XPROCESS (proc));
1048 XPROCESS (proc)->filter = filter;
1049 XPROCESS (proc)->filter_does_read = filter_does_read;
1052 DEFUN ("set-process-filter", Fset_process_filter, 2, 2, 0, /*
1053 Give PROCESS the filter function FILTER; nil means no filter.
1054 t means stop accepting output from the process.
1055 When a process has a filter, each time it does output
1056 the entire string of output is passed to the filter.
1057 The filter gets two arguments: the process and the string of output.
1058 If the process has a filter, its buffer is not used for output.
1062 set_process_filter (proc, filter, 0);
1066 DEFUN ("process-filter", Fprocess_filter, 1, 1, 0, /*
1067 Return the filter function of PROCESS; nil if none.
1068 See `set-process-filter' for more info on filter functions.
1072 CHECK_PROCESS (proc);
1073 return XPROCESS (proc)->filter;
1076 DEFUN ("process-send-region", Fprocess_send_region, 3, 4, 0, /*
1077 Send current contents of the region between START and END as input to PROCESS.
1078 PROCESS may be a process name or an actual process.
1079 BUFFER specifies the buffer to look in; if nil, the current buffer is used.
1080 If the region is more than 500 or so characters long,
1081 it is sent in several bunches. This may happen even for shorter regions.
1082 Output from processes can arrive in between bunches.
1084 (process, start, end, buffer))
1086 /* This function can GC */
1087 Lisp_Object proc = get_process (process);
1089 struct buffer *buf = decode_buffer (buffer, 0);
1091 XSETBUFFER (buffer, buf);
1092 get_buffer_range_char (buf, start, end, &st, &en, 0);
1094 send_process (proc, buffer, 0, st, en - st);
1098 DEFUN ("process-send-string", Fprocess_send_string, 2, 4, 0, /*
1099 Send PROCESS the contents of STRING as input.
1100 PROCESS may be a process name or an actual process.
1101 Optional arguments FROM and TO specify part of STRING, see `substring'.
1102 If STRING is more than 500 or so characters long,
1103 it is sent in several bunches. This may happen even for shorter strings.
1104 Output from processes can arrive in between bunches.
1106 (process, string, from, to))
1108 /* This function can GC */
1113 proc = get_process (process);
1114 CHECK_STRING (string);
1115 get_string_range_byte (string, from, to, &bfr, &bto,
1116 GB_HISTORICAL_STRING_BEHAVIOR);
1119 send_process (proc, string, 0, bfr, len);
1125 DEFUN ("process-input-coding-system", Fprocess_input_coding_system, 1, 1, 0, /*
1126 Return PROCESS's input coding system.
1130 process = get_process (process);
1131 CHECK_LIVE_PROCESS (process);
1132 return decoding_stream_coding_system (XLSTREAM (XPROCESS (process)->coding_instream) );
1135 DEFUN ("process-output-coding-system", Fprocess_output_coding_system, 1, 1, 0, /*
1136 Return PROCESS's output coding system.
1140 process = get_process (process);
1141 CHECK_LIVE_PROCESS (process);
1142 return encoding_stream_coding_system (XLSTREAM (XPROCESS (process)->coding_outstream));
1145 DEFUN ("process-coding-system", Fprocess_coding_system, 1, 1, 0, /*
1146 Return a pair of coding-system for decoding and encoding of PROCESS.
1150 process = get_process (process);
1151 CHECK_LIVE_PROCESS (process);
1152 return Fcons (decoding_stream_coding_system
1153 (XLSTREAM (XPROCESS (process)->coding_instream)),
1154 encoding_stream_coding_system
1155 (XLSTREAM (XPROCESS (process)->coding_outstream)));
1158 DEFUN ("set-process-input-coding-system", Fset_process_input_coding_system,
1160 Set PROCESS's input coding system to CODESYS.
1164 codesys = Fget_coding_system (codesys);
1165 process = get_process (process);
1166 CHECK_LIVE_PROCESS (process);
1168 set_decoding_stream_coding_system
1169 (XLSTREAM (XPROCESS (process)->coding_instream), codesys);
1173 DEFUN ("set-process-output-coding-system", Fset_process_output_coding_system,
1175 Set PROCESS's output coding system to CODESYS.
1179 codesys = Fget_coding_system (codesys);
1180 process = get_process (process);
1181 CHECK_LIVE_PROCESS (process);
1183 set_encoding_stream_coding_system
1184 (XLSTREAM (XPROCESS (process)->coding_outstream), codesys);
1188 DEFUN ("set-process-coding-system", Fset_process_coding_system,
1190 Set coding-systems of PROCESS to DECODING and ENCODING.
1191 DECODING will be used to decode subprocess output and ENCODING to
1192 encode subprocess input.
1194 (process, decoding, encoding))
1196 if (!NILP (decoding))
1197 Fset_process_input_coding_system (process, decoding);
1199 if (!NILP (encoding))
1200 Fset_process_output_coding_system (process, encoding);
1205 #endif /* FILE_CODING */
1207 /************************************************************************/
1208 /* process status */
1209 /************************************************************************/
1212 exec_sentinel_unwind (Lisp_Object datum)
1214 Lisp_Cons *d = XCONS (datum);
1215 XPROCESS (d->car)->sentinel = d->cdr;
1221 exec_sentinel (Lisp_Object proc, Lisp_Object reason)
1223 /* This function can GC */
1224 int speccount = specpdl_depth ();
1225 Lisp_Process *p = XPROCESS (proc);
1226 Lisp_Object sentinel = p->sentinel;
1228 if (NILP (sentinel))
1231 /* Some weird FSFmacs crap here with
1232 Vdeactivate_mark and current_buffer->keymap */
1234 /* Zilch the sentinel while it's running, to avoid recursive invocations;
1235 assure that it gets restored no matter how the sentinel exits. */
1237 record_unwind_protect (exec_sentinel_unwind, noseeum_cons (proc, sentinel));
1238 /* We used to bind inhibit-quit to t here, but call2_trapping_errors()
1239 does that for us. */
1240 running_asynch_code = 1;
1241 call2_trapping_errors ("Error in process sentinel", sentinel, proc, reason);
1242 running_asynch_code = 0;
1243 restore_match_data ();
1244 unbind_to (speccount, Qnil);
1247 DEFUN ("set-process-sentinel", Fset_process_sentinel, 2, 2, 0, /*
1248 Give PROCESS the sentinel SENTINEL; nil for none.
1249 The sentinel is called as a function when the process changes state.
1250 It gets two arguments: the process, and a string describing the change.
1254 CHECK_PROCESS (proc);
1255 XPROCESS (proc)->sentinel = sentinel;
1259 DEFUN ("process-sentinel", Fprocess_sentinel, 1, 1, 0, /*
1260 Return the sentinel of PROCESS; nil if none.
1261 See `set-process-sentinel' for more info on sentinels.
1265 CHECK_PROCESS (proc);
1266 return XPROCESS (proc)->sentinel;
1271 signal_name (int signum)
1273 if (signum >= 0 && signum < NSIG)
1274 return (const char *) sys_siglist[signum];
1276 return (const char *) GETTEXT ("unknown signal");
1280 update_process_status (Lisp_Object p,
1281 Lisp_Object status_symbol,
1285 XPROCESS (p)->tick++;
1287 XPROCESS (p)->status_symbol = status_symbol;
1288 XPROCESS (p)->exit_code = exit_code;
1289 XPROCESS (p)->core_dumped = core_dumped;
1292 /* Return a string describing a process status list. */
1295 status_message (Lisp_Process *p)
1297 Lisp_Object symbol = p->status_symbol;
1298 int code = p->exit_code;
1299 int coredump = p->core_dumped;
1300 Lisp_Object string, string2;
1302 if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
1304 string = build_string (signal_name (code));
1306 string2 = build_translated_string (" (core dumped)\n");
1308 string2 = build_string ("\n");
1309 set_string_char (XSTRING (string), 0,
1310 DOWNCASE (current_buffer,
1311 string_char (XSTRING (string), 0)));
1312 return concat2 (string, string2);
1314 else if (EQ (symbol, Qexit))
1317 return build_translated_string ("finished\n");
1318 string = Fnumber_to_string (make_int (code));
1320 string2 = build_translated_string (" (core dumped)\n");
1322 string2 = build_string ("\n");
1323 return concat2 (build_translated_string ("exited abnormally with code "),
1324 concat2 (string, string2));
1327 return Fcopy_sequence (Fsymbol_name (symbol));
1330 /* Tell status_notify() to check for terminated processes. We do this
1331 because on some systems we sometimes miss SIGCHLD calls. (Not sure
1335 kick_status_notify (void)
1341 /* Report all recent events of a change in process status
1342 (either run the sentinel or output a message).
1343 This is done while Emacs is waiting for keyboard input. */
1346 status_notify (void)
1348 /* This function can GC */
1349 Lisp_Object tail = Qnil;
1350 Lisp_Object symbol = Qnil;
1351 Lisp_Object msg = Qnil;
1352 struct gcpro gcpro1, gcpro2, gcpro3;
1353 /* process_tick is volatile, so we have to remember it now.
1354 Otherwise, we get a race condition is SIGCHLD happens during
1357 (Actually, this is not the case anymore. The code to
1358 update the process structures has been moved out of the
1359 SIGCHLD handler. But for the moment I'm leaving this
1360 stuff in -- it can't hurt.) */
1361 int temp_process_tick;
1363 MAYBE_PROCMETH (reap_exited_processes, ());
1365 temp_process_tick = process_tick;
1367 if (update_tick == temp_process_tick)
1370 /* We need to gcpro tail; if read_process_output calls a filter
1371 which deletes a process and removes the cons to which tail points
1372 from Vprocess_alist, and then causes a GC, tail is an unprotected
1374 GCPRO3 (tail, symbol, msg);
1376 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail))
1378 Lisp_Object proc = XCAR (tail);
1379 Lisp_Process *p = XPROCESS (proc);
1380 /* p->tick is also volatile. Same thing as above applies. */
1381 int this_process_tick;
1383 /* #### extra check for terminated processes, in case a SIGCHLD
1384 got missed (this seems to happen sometimes, I'm not sure why).
1387 MAYBE_PROCMETH (update_status_if_terminated, (p));
1389 this_process_tick = p->tick;
1390 if (this_process_tick != p->update_tick)
1392 p->update_tick = this_process_tick;
1394 /* If process is still active, read any output that remains. */
1395 while (!EQ (p->filter, Qt)
1396 && read_process_output (proc) > 0)
1399 /* Get the text to use for the message. */
1400 msg = status_message (p);
1402 /* If process is terminated, deactivate it or delete it. */
1403 symbol = p->status_symbol;
1405 if (EQ (symbol, Qsignal)
1406 || EQ (symbol, Qexit))
1408 if (delete_exited_processes)
1409 remove_process (proc);
1411 deactivate_process (proc);
1414 /* Now output the message suitably. */
1415 if (!NILP (p->sentinel))
1416 exec_sentinel (proc, msg);
1417 /* Don't bother with a message in the buffer
1418 when a process becomes runnable. */
1419 else if (!EQ (symbol, Qrun) && !NILP (p->buffer))
1421 Lisp_Object old_read_only = Qnil;
1422 Lisp_Object old = Fcurrent_buffer ();
1424 struct gcpro ngcpro1, ngcpro2;
1426 /* Avoid error if buffer is deleted
1427 (probably that's why the process is dead, too) */
1428 if (!BUFFER_LIVE_P (XBUFFER (p->buffer)))
1431 NGCPRO2 (old, old_read_only);
1432 Fset_buffer (p->buffer);
1433 opoint = BUF_PT (current_buffer);
1434 /* Insert new output into buffer
1435 at the current end-of-output marker,
1436 thus preserving logical ordering of input and output. */
1437 if (XMARKER (p->mark)->buffer)
1438 BUF_SET_PT (current_buffer, marker_position (p->mark));
1440 BUF_SET_PT (current_buffer, BUF_ZV (current_buffer));
1441 if (BUF_PT (current_buffer) <= opoint)
1442 opoint += (string_char_length (XSTRING (msg))
1443 + string_char_length (XSTRING (p->name))
1446 old_read_only = current_buffer->read_only;
1447 current_buffer->read_only = Qnil;
1448 buffer_insert_c_string (current_buffer, "\nProcess ");
1449 Finsert (1, &p->name);
1450 buffer_insert_c_string (current_buffer, " ");
1452 current_buffer->read_only = old_read_only;
1453 Fset_marker (p->mark, make_int (BUF_PT (current_buffer)),
1456 opoint = bufpos_clip_to_bounds(BUF_BEGV (XBUFFER (p->buffer)),
1458 BUF_ZV (XBUFFER (p->buffer)));
1459 BUF_SET_PT (current_buffer, opoint);
1466 /* in case buffers use %s in modeline-format */
1467 MARK_MODELINE_CHANGED;
1470 update_tick = temp_process_tick;
1475 DEFUN ("process-status", Fprocess_status, 1, 1, 0, /*
1476 Return the status of PROCESS.
1477 This is a symbol, one of these:
1479 run -- for a process that is running.
1480 stop -- for a process stopped but continuable.
1481 exit -- for a process that has exited.
1482 signal -- for a process that has got a fatal signal.
1483 open -- for a network stream connection that is open.
1484 closed -- for a network stream connection that is closed.
1485 nil -- if arg is a process name and no such process exists.
1487 PROCESS may be a process, a buffer, the name of a process or buffer, or
1488 nil, indicating the current buffer's process.
1492 Lisp_Object status_symbol;
1495 proc = Fget_process (proc);
1497 proc = get_process (proc);
1502 status_symbol = XPROCESS (proc)->status_symbol;
1503 if (network_connection_p (proc))
1505 if (EQ (status_symbol, Qrun))
1506 status_symbol = Qopen;
1507 else if (EQ (status_symbol, Qexit))
1508 status_symbol = Qclosed;
1510 return status_symbol;
1513 DEFUN ("process-exit-status", Fprocess_exit_status, 1, 1, 0, /*
1514 Return the exit status of PROCESS or the signal number that killed it.
1515 If PROCESS has not yet exited or died, return 0.
1519 CHECK_PROCESS (proc);
1520 return make_int (XPROCESS (proc)->exit_code);
1525 /* send a signal number SIGNO to PROCESS.
1526 CURRENT_GROUP means send to the process group that currently owns
1527 the terminal being used to communicate with PROCESS.
1528 This is used for various commands in shell mode.
1529 If NOMSG is zero, insert signal-announcements into process's buffers
1532 If we can, we try to signal PROCESS by sending control characters
1533 down the pty. This allows us to signal inferiors who have changed
1534 their uid, for which killpg would return an EPERM error. */
1537 process_send_signal (Lisp_Object process, int signo,
1538 int current_group, int nomsg)
1540 /* This function can GC */
1541 Lisp_Object proc = get_process (process);
1543 if (network_connection_p (proc))
1544 error ("Network connection %s is not a subprocess",
1545 XSTRING_DATA (XPROCESS(proc)->name));
1546 CHECK_LIVE_PROCESS (proc);
1548 MAYBE_PROCMETH (kill_child_process, (proc, signo, current_group, nomsg));
1551 DEFUN ("interrupt-process", Finterrupt_process, 0, 2, 0, /*
1552 Interrupt process PROCESS. May be process or name of one.
1553 Nil or no arg means current buffer's process.
1554 Second arg CURRENT-GROUP non-nil means send signal to
1555 the current process-group of the process's controlling terminal
1556 rather than to the process's own process group.
1557 If the process is a shell, this means interrupt current subjob
1558 rather than the shell.
1560 (process, current_group))
1562 /* This function can GC */
1563 process_send_signal (process, SIGINT, !NILP (current_group), 0);
1567 DEFUN ("kill-process", Fkill_process, 0, 2, 0, /*
1568 Kill process PROCESS. May be process or name of one.
1569 See function `interrupt-process' for more details on usage.
1571 (process, current_group))
1573 /* This function can GC */
1575 process_send_signal (process, SIGKILL, !NILP (current_group), 0);
1577 error ("kill-process: Not supported on this system");
1582 DEFUN ("quit-process", Fquit_process, 0, 2, 0, /*
1583 Send QUIT signal to process PROCESS. May be process or name of one.
1584 See function `interrupt-process' for more details on usage.
1586 (process, current_group))
1588 /* This function can GC */
1590 process_send_signal (process, SIGQUIT, !NILP (current_group), 0);
1592 error ("quit-process: Not supported on this system");
1597 DEFUN ("stop-process", Fstop_process, 0, 2, 0, /*
1598 Stop process PROCESS. May be process or name of one.
1599 See function `interrupt-process' for more details on usage.
1601 (process, current_group))
1603 /* This function can GC */
1605 process_send_signal (process, SIGTSTP, !NILP (current_group), 0);
1607 error ("stop-process: Not supported on this system");
1612 DEFUN ("continue-process", Fcontinue_process, 0, 2, 0, /*
1613 Continue process PROCESS. May be process or name of one.
1614 See function `interrupt-process' for more details on usage.
1616 (process, current_group))
1618 /* This function can GC */
1620 process_send_signal (process, SIGCONT, !NILP (current_group), 0);
1622 error ("continue-process: Not supported on this system");
1627 DEFUN ("signal-process", Fsignal_process, 2, 2,
1628 "nProcess number: \nnSignal code: ", /*
1629 Send the process with process id PID the signal with code SIGCODE.
1630 PID must be an integer. The process need not be a child of this Emacs.
1631 SIGCODE may be an integer, or a symbol whose name is a signal name.
1643 CHECK_SYMBOL (sigcode);
1644 name = string_data (XSYMBOL (sigcode)->name);
1646 #define handle_signal(signal) \
1647 else if (!strcmp ((const char *) name, #signal)) \
1648 XSETINT (sigcode, signal)
1652 handle_signal (SIGINT); /* ANSI */
1653 handle_signal (SIGILL); /* ANSI */
1654 handle_signal (SIGABRT); /* ANSI */
1655 handle_signal (SIGFPE); /* ANSI */
1656 handle_signal (SIGSEGV); /* ANSI */
1657 handle_signal (SIGTERM); /* ANSI */
1660 handle_signal (SIGHUP); /* POSIX */
1663 handle_signal (SIGQUIT); /* POSIX */
1666 handle_signal (SIGTRAP); /* POSIX */
1669 handle_signal (SIGKILL); /* POSIX */
1672 handle_signal (SIGUSR1); /* POSIX */
1675 handle_signal (SIGUSR2); /* POSIX */
1678 handle_signal (SIGPIPE); /* POSIX */
1681 handle_signal (SIGALRM); /* POSIX */
1684 handle_signal (SIGCHLD); /* POSIX */
1687 handle_signal (SIGCONT); /* POSIX */
1690 handle_signal (SIGSTOP); /* POSIX */
1693 handle_signal (SIGTSTP); /* POSIX */
1696 handle_signal (SIGTTIN); /* POSIX */
1699 handle_signal (SIGTTOU); /* POSIX */
1703 handle_signal (SIGBUS); /* XPG5 */
1706 handle_signal (SIGPOLL); /* XPG5 */
1709 handle_signal (SIGPROF); /* XPG5 */
1712 handle_signal (SIGSYS); /* XPG5 */
1715 handle_signal (SIGURG); /* XPG5 */
1718 handle_signal (SIGXCPU); /* XPG5 */
1721 handle_signal (SIGXFSZ); /* XPG5 */
1724 handle_signal (SIGVTALRM); /* XPG5 */
1728 handle_signal (SIGIO); /* BSD 4.2 */
1731 handle_signal (SIGWINCH); /* BSD 4.3 */
1735 handle_signal (SIGEMT);
1738 handle_signal (SIGINFO);
1741 handle_signal (SIGHWE);
1744 handle_signal (SIGPRE);
1747 handle_signal (SIGUME);
1750 handle_signal (SIGDLK);
1753 handle_signal (SIGCPULIM);
1756 handle_signal (SIGIOT);
1759 handle_signal (SIGLOST);
1762 handle_signal (SIGSTKFLT);
1765 handle_signal (SIGUNUSED);
1768 handle_signal (SIGDANGER); /* AIX */
1771 handle_signal (SIGMSG);
1774 handle_signal (SIGSOUND);
1777 handle_signal (SIGRETRACT);
1780 handle_signal (SIGGRANT);
1783 handle_signal (SIGPWR);
1786 error ("Undefined signal name %s", name);
1789 #undef handle_signal
1791 return make_int (PROCMETH_OR_GIVEN (kill_process_by_pid,
1792 (XINT (pid), XINT (sigcode)), -1));
1795 DEFUN ("process-send-eof", Fprocess_send_eof, 0, 1, 0, /*
1796 Make PROCESS see end-of-file in its input.
1797 PROCESS may be a process, a buffer, the name of a process or buffer, or
1798 nil, indicating the current buffer's process.
1799 If PROCESS is a network connection, or is a process communicating
1800 through a pipe (as opposed to a pty), then you cannot send any more
1801 text to PROCESS after you call this function.
1805 /* This function can GC */
1806 Lisp_Object proc = get_process (process);
1808 /* Make sure the process is really alive. */
1809 if (! EQ (XPROCESS (proc)->status_symbol, Qrun))
1810 error ("Process %s not running", XSTRING_DATA (XPROCESS (proc)->name));
1812 if (!MAYBE_INT_PROCMETH (process_send_eof, (proc)))
1814 if (!NILP (DATA_OUTSTREAM (XPROCESS (proc))))
1816 Lstream_close (XLSTREAM (DATA_OUTSTREAM (XPROCESS (proc))));
1817 event_stream_delete_stream_pair (Qnil, XPROCESS (proc)->pipe_outstream);
1818 XPROCESS (proc)->pipe_outstream = Qnil;
1820 XPROCESS (proc)->coding_outstream = Qnil;
1829 /************************************************************************/
1830 /* deleting a process */
1831 /************************************************************************/
1834 deactivate_process (Lisp_Object proc)
1836 Lisp_Process *p = XPROCESS (proc);
1839 /* It's possible that we got as far in the process-creation
1840 process as creating the descriptors but didn't get so
1841 far as selecting the process for input. In this
1842 case, p->pid is nil: p->pid is set at the same time that
1843 the process is selected for input. */
1844 /* #### The comment does not look correct. event_stream_unselect_process
1845 is guarded by process->selected, so this is not a problem. - kkm*/
1846 /* Must call this before setting the streams to nil */
1847 event_stream_unselect_process (p);
1849 if (!NILP (DATA_OUTSTREAM (p)))
1850 Lstream_close (XLSTREAM (DATA_OUTSTREAM (p)));
1851 if (!NILP (DATA_INSTREAM (p)))
1852 Lstream_close (XLSTREAM (DATA_INSTREAM (p)));
1854 /* Provide minimal implementation for deactivate_process
1855 if there's no process-specific one */
1856 if (HAS_PROCMETH_P (deactivate_process))
1857 usid = PROCMETH (deactivate_process, (p));
1859 usid = event_stream_delete_stream_pair (p->pipe_instream,
1862 if (usid != USID_DONTHASH)
1863 remhash ((const void*)usid, usid_to_process);
1865 p->pipe_instream = Qnil;
1866 p->pipe_outstream = Qnil;
1868 p->coding_instream = Qnil;
1869 p->coding_outstream = Qnil;
1874 remove_process (Lisp_Object proc)
1876 Vprocess_list = delq_no_quit (proc, Vprocess_list);
1877 Fset_marker (XPROCESS (proc)->mark, Qnil, Qnil);
1879 deactivate_process (proc);
1882 DEFUN ("delete-process", Fdelete_process, 1, 1, 0, /*
1883 Delete PROCESS: kill it and forget about it immediately.
1884 PROCESS may be a process or the name of one, or a buffer name.
1888 /* This function can GC */
1890 proc = get_process (proc);
1891 p = XPROCESS (proc);
1892 if (network_connection_p (proc))
1894 p->status_symbol = Qexit;
1900 else if (PROCESS_LIVE_P (p))
1902 Fkill_process (proc, Qnil);
1903 /* Do this now, since remove_process will make sigchld_handler do nothing. */
1904 p->status_symbol = Qsignal;
1905 p->exit_code = SIGKILL;
1911 remove_process (proc);
1915 /* Kill all processes associated with `buffer'.
1916 If `buffer' is nil, kill all processes */
1919 kill_buffer_processes (Lisp_Object buffer)
1923 for (tail = Vprocess_list; CONSP (tail);
1926 Lisp_Object proc = XCAR (tail);
1928 && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer)))
1930 if (network_connection_p (proc))
1931 Fdelete_process (proc);
1932 else if (PROCESS_LIVE_P (XPROCESS (proc)))
1933 process_send_signal (proc, SIGHUP, 0, 1);
1938 DEFUN ("process-kill-without-query", Fprocess_kill_without_query, 1, 2, 0, /*
1939 Say no query needed if PROCESS is running when Emacs is exited.
1940 Optional second argument if non-nil says to require a query.
1941 Value is t if a query was formerly required.
1943 (proc, require_query_p))
1947 CHECK_PROCESS (proc);
1948 tem = XPROCESS (proc)->kill_without_query;
1949 XPROCESS (proc)->kill_without_query = NILP (require_query_p);
1951 return tem ? Qnil : Qt;
1954 DEFUN ("process-kill-without-query-p", Fprocess_kill_without_query_p, 1, 1, 0, /*
1955 Whether PROC will be killed without query if running when emacs is exited.
1959 CHECK_PROCESS (proc);
1960 return XPROCESS (proc)->kill_without_query ? Qt : Qnil;
1964 /* This is not named init_process in order to avoid a conflict with NS 3.3 */
1966 init_xemacs_process (void)
1968 MAYBE_PROCMETH (init_process, ());
1970 Vprocess_list = Qnil;
1972 if (usid_to_process)
1973 clrhash (usid_to_process);
1975 usid_to_process = make_hash_table (32);
1980 xxDEFUN ("process-connection", Fprocess_connection, 0, 1, 0, /*
1981 Return the connection type of `PROCESS'. This can be nil (pipe),
1982 t or pty (pty) or stream (socket connection).
1986 return XPROCESS (process)->type;
1992 syms_of_process (void)
1994 INIT_LRECORD_IMPLEMENTATION (process);
1996 defsymbol (&Qprocessp, "processp");
1997 defsymbol (&Qprocess_live_p, "process-live-p");
1998 defsymbol (&Qrun, "run");
1999 defsymbol (&Qstop, "stop");
2000 defsymbol (&Qopen, "open");
2001 defsymbol (&Qclosed, "closed");
2003 defsymbol (&Qtcp, "tcp");
2004 defsymbol (&Qudp, "udp");
2006 #ifdef HAVE_MULTICAST
2007 defsymbol(&Qmulticast, "multicast"); /* Used for occasional warnings */
2010 DEFSUBR (Fprocessp);
2011 DEFSUBR (Fprocess_live_p);
2012 DEFSUBR (Fget_process);
2013 DEFSUBR (Fget_buffer_process);
2014 DEFSUBR (Fdelete_process);
2015 DEFSUBR (Fprocess_status);
2016 DEFSUBR (Fprocess_exit_status);
2017 DEFSUBR (Fprocess_id);
2018 DEFSUBR (Fprocess_name);
2019 DEFSUBR (Fprocess_tty_name);
2020 DEFSUBR (Fprocess_command);
2021 DEFSUBR (Fset_process_buffer);
2022 DEFSUBR (Fprocess_buffer);
2023 DEFSUBR (Fprocess_mark);
2024 DEFSUBR (Fset_process_filter);
2025 DEFSUBR (Fprocess_filter);
2026 DEFSUBR (Fset_process_window_size);
2027 DEFSUBR (Fset_process_sentinel);
2028 DEFSUBR (Fprocess_sentinel);
2029 DEFSUBR (Fprocess_kill_without_query);
2030 DEFSUBR (Fprocess_kill_without_query_p);
2031 DEFSUBR (Fprocess_list);
2032 DEFSUBR (Fstart_process_internal);
2034 DEFSUBR (Fopen_network_stream_internal);
2035 #ifdef HAVE_MULTICAST
2036 DEFSUBR (Fopen_multicast_group_internal);
2037 #endif /* HAVE_MULTICAST */
2038 #endif /* HAVE_SOCKETS */
2039 DEFSUBR (Fprocess_send_region);
2040 DEFSUBR (Fprocess_send_string);
2041 DEFSUBR (Finterrupt_process);
2042 DEFSUBR (Fkill_process);
2043 DEFSUBR (Fquit_process);
2044 DEFSUBR (Fstop_process);
2045 DEFSUBR (Fcontinue_process);
2046 DEFSUBR (Fprocess_send_eof);
2047 DEFSUBR (Fsignal_process);
2048 /* DEFSUBR (Fprocess_connection); */
2050 DEFSUBR (Fprocess_input_coding_system);
2051 DEFSUBR (Fprocess_output_coding_system);
2052 DEFSUBR (Fset_process_input_coding_system);
2053 DEFSUBR (Fset_process_output_coding_system);
2054 DEFSUBR (Fprocess_coding_system);
2055 DEFSUBR (Fset_process_coding_system);
2056 #endif /* FILE_CODING */
2060 vars_of_process (void)
2062 Fprovide (intern ("subprocesses"));
2064 Fprovide (intern ("network-streams"));
2065 #ifdef HAVE_MULTICAST
2066 Fprovide (intern ("multicast"));
2067 #endif /* HAVE_MULTICAST */
2068 #endif /* HAVE_SOCKETS */
2069 staticpro (&Vprocess_list);
2071 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes /*
2072 *Non-nil means delete processes immediately when they exit.
2073 nil means don't delete them until `list-processes' is run.
2076 delete_exited_processes = 1;
2078 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type /*
2079 Control type of device used to communicate with subprocesses.
2080 Values are nil to use a pipe, or t or `pty' to use a pty.
2081 The value has no effect if the system has no ptys or if all ptys are busy:
2082 then a pipe is used in any case.
2083 The value takes effect when `start-process' is called.
2085 Vprocess_connection_type = Qt;
2087 DEFVAR_BOOL ("windowed-process-io", &windowed_process_io /*
2088 Enables input/output on standard handles of a windowed process.
2089 When this variable is nil (the default), XEmacs does not attempt to read
2090 standard output handle of a windowed process. Instead, the process is
2091 immediately marked as exited immediately upon successful launching. This is
2092 done because normal windowed processes do not use standard I/O, as they are
2093 not connected to any console.
2095 When launching a specially crafted windowed process, which expects to be
2096 launched by XEmacs, or by other program which pipes its standard input and
2097 output, this variable must be set to non-nil, in which case XEmacs will
2098 treat this process just like a console process.
2100 NOTE: You should never set this variable, only bind it.
2102 Only Windows processes can be "windowed" or "console". This variable has no
2103 effect on UNIX processes, because all UNIX processes are "console".
2105 windowed_process_io = 0;
2107 #ifdef PROCESS_IO_BLOCKING
2108 DEFVAR_LISP ("network-stream-blocking-port-list", &network_stream_blocking_port_list /*
2109 List of port numbers or port names to set a blocking I/O mode with connection.
2110 Nil value means to set a default(non-blocking) I/O mode.
2111 The value takes effect when `open-network-stream-internal' is called.
2113 network_stream_blocking_port_list = Qnil;
2114 #endif /* PROCESS_IO_BLOCKING */
2117 #endif /* not NO_SUBPROCESSES */