1 /* Asynchronous subprocess control for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
25 /* This file is split into two parts by the following preprocessor
26 conditional. The 'then' clause contains all of the support for
27 asynchronous subprocesses. The 'else' clause contains stub
28 versions of some of the asynchronous subprocess routines that are
29 often called elsewhere in Emacs, so we don't have to #ifdef the
30 sections that call them. */
38 #include <sys/types.h> /* some typedefs are used in sys/file.h */
42 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
43 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #endif /* HAVE_SOCKETS */
49 /* TERM is a poor-man's SLIP, used on Linux. */
54 /* DGUX inet_addr returns a 'struct in_addr'. */
56 #define IN_ADDR struct in_addr
57 #define NUMERIC_ADDR_ERROR (numeric_addr.s_addr == -1)
59 #define IN_ADDR unsigned long
60 #define NUMERIC_ADDR_ERROR (numeric_addr == -1)
63 #if defined(BSD) || defined(STRIDE)
64 #include <sys/ioctl.h>
65 #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
67 #endif /* HAVE_PTYS and no O_NDELAY */
68 #endif /* BSD or STRIDE */
75 #include <sys/sysmacros.h> /* for "minor" */
85 #include "termhooks.h"
90 Lisp_Object Qprocessp
;
91 Lisp_Object Qrun
, Qstop
, Qsignal
, Qopen
, Qclosed
;
92 /* Qexit is declared and initialized in eval.c. */
94 /* a process object is a network connection when its childp field is neither
95 Qt nor Qnil but is instead a string (name of foreign host we
96 are connected to + name of port we are connected to) */
99 static Lisp_Object stream_process
;
101 #define NETCONN_P(p) (XGCTYPE (XPROCESS (p)->childp) == Lisp_String)
103 #define NETCONN_P(p) 0
104 #endif /* HAVE_SOCKETS */
106 /* Define first descriptor number available for subprocesses. */
108 #define FIRST_PROC_DESC 1
110 #define FIRST_PROC_DESC 3
113 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
116 #if !defined (SIGCHLD) && defined (SIGCLD)
117 #define SIGCHLD SIGCLD
120 #include "syssignal.h"
126 extern char *sys_errlist
[];
131 extern char *sys_siglist
[];
134 char *sys_siglist
[] =
140 "illegal instruction",
144 "floating point exception",
147 "segmentation violation",
148 "bad argument to system call",
149 "write on a pipe with no one to read it",
151 "software termination signal from kill",
153 "sendable stop signal not from tty",
154 "stop signal from tty",
155 "continue a stopped process",
156 "child status has changed",
157 "background read attempted from control tty",
158 "background write attempted from control tty",
159 "input record available at control tty",
160 "exceeded CPU time limit",
161 "exceeded file size limit"
166 /* t means use pty, nil means use a pipe,
167 maybe other values to come. */
168 static Lisp_Object Vprocess_connection_type
;
172 #include <sys/socket.h>
176 /* Number of events of change of status of a process. */
177 static int process_tick
;
179 /* Number of events for which the user or sentinel has been notified. */
180 static int update_tick
;
183 /* We could get this from param.h, but better not to depend on finding that.
184 And better not to risk that it might define other symbols used in this
187 #define MAXDESC FD_SETSIZE
191 #define SELECT_TYPE fd_set
192 #else /* no FD_SET */
194 #define SELECT_TYPE int
196 /* Define the macros to access a single-int bitmap of descriptors. */
197 #define FD_SET(n, p) (*(p) |= (1 << (n)))
198 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
199 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
200 #define FD_ZERO(p) (*(p) = 0)
201 #endif /* no FD_SET */
203 /* Mask of bits indicating the descriptors that we wait for input on */
205 static SELECT_TYPE input_wait_mask
;
207 /* Descriptor to use for keyboard input. */
208 static int keyboard_descriptor
;
210 /* Nonzero means delete a process right away if it exits. */
211 static int delete_exited_processes
;
213 /* Indexed by descriptor, gives the process (if any) for that descriptor */
214 static Lisp_Object chan_process
[MAXDESC
];
216 /* Alist of elements (NAME . PROCESS) */
217 static Lisp_Object Vprocess_alist
;
219 /* Buffered-ahead input char from process, indexed by channel.
220 -1 means empty (no char is buffered).
221 Used on sys V where the only way to tell if there is any
222 output from the process is to read at least one char.
223 Always -1 on systems that support FIONREAD. */
225 static int proc_buffered_char
[MAXDESC
];
227 static Lisp_Object
get_process ();
229 /* Compute the Lisp form of the process status, p->status, from
230 the numeric status that was returned by `wait'. */
232 Lisp_Object
status_convert ();
235 struct Lisp_Process
*p
;
237 union { int i
; WAITTYPE wt
; } u
;
238 u
.i
= XFASTINT (p
->raw_status_low
) + (XFASTINT (p
->raw_status_high
) << 16);
239 p
->status
= status_convert (u
.wt
);
240 p
->raw_status_low
= Qnil
;
241 p
->raw_status_high
= Qnil
;
244 /* Convert a process status word in Unix format to
245 the list that we use internally. */
252 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
253 else if (WIFEXITED (w
))
254 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
255 WCOREDUMP (w
) ? Qt
: Qnil
));
256 else if (WIFSIGNALED (w
))
257 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
258 WCOREDUMP (w
) ? Qt
: Qnil
));
263 /* Given a status-list, extract the three pieces of information
264 and store them individually through the three pointers. */
267 decode_status (l
, symbol
, code
, coredump
)
275 if (XTYPE (l
) == Lisp_Symbol
)
283 *symbol
= XCONS (l
)->car
;
284 tem
= XCONS (l
)->cdr
;
285 *code
= XFASTINT (XCONS (tem
)->car
);
286 tem
= XCONS (tem
)->cdr
;
287 *coredump
= !NILP (tem
);
291 /* Return a string describing a process status list. */
294 status_message (status
)
299 Lisp_Object string
, string2
;
301 decode_status (status
, &symbol
, &code
, &coredump
);
303 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
306 string
= build_string (code
< NSIG
? sys_siglist
[code
] : "unknown");
308 string
= build_string (code
< NSIG
? sys_errlist
[code
] : "unknown");
310 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
311 XSTRING (string
)->data
[0] = DOWNCASE (XSTRING (string
)->data
[0]);
312 return concat2 (string
, string2
);
314 else if (EQ (symbol
, Qexit
))
317 return build_string ("finished\n");
318 string
= Fnumber_to_string (make_number (code
));
319 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
320 return concat2 (build_string ("exited abnormally with code "),
321 concat2 (string
, string2
));
324 return Fcopy_sequence (Fsymbol_name (symbol
));
329 /* Open an available pty, returning a file descriptor.
330 Return -1 on failure.
331 The file name of the terminal corresponding to the pty
332 is left in the variable pty_name. */
343 /* Some systems name their pseudoterminals so that there are gaps in
344 the usual sequence - for example, on HP9000/S700 systems, there
345 are no pseudoterminals with names ending in 'f'. So we wait for
346 three failures in a row before deciding that we've reached the
348 int failed_count
= 0;
353 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
354 for (i
= 0; i
< 16; i
++)
357 #ifdef PTY_NAME_SPRINTF
360 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
361 #endif /* no PTY_NAME_SPRINTF */
365 #else /* no PTY_OPEN */
367 /* Unusual IRIS code */
368 *ptyv
= open ("/dev/ptc", O_RDWR
| O_NDELAY
, 0);
371 if (fstat (fd
, &stb
) < 0)
374 if (stat (pty_name
, &stb
) < 0)
377 if (failed_count
>= 3)
383 fd
= open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
385 fd
= open (pty_name
, O_RDWR
| O_NDELAY
, 0);
387 #endif /* not IRIS */
388 #endif /* no PTY_OPEN */
392 /* check to make certain that both sides are available
393 this avoids a nasty yet stupid bug in rlogins */
394 #ifdef PTY_TTY_NAME_SPRINTF
397 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
398 #endif /* no PTY_TTY_NAME_SPRINTF */
400 if (access (pty_name
, 6) != 0)
403 #if !defined(IRIS) && !defined(__sgi)
409 #endif /* not UNIPLUS */
416 #endif /* HAVE_PTYS */
422 register Lisp_Object val
, tem
, name1
;
423 register struct Lisp_Process
*p
;
427 /* size of process structure includes the vector header,
428 so deduct for that. But struct Lisp_Vector includes the first
429 element, thus deducts too much, so add it back. */
430 val
= Fmake_vector (make_number ((sizeof (struct Lisp_Process
)
431 - sizeof (struct Lisp_Vector
)
432 + sizeof (Lisp_Object
))
433 / sizeof (Lisp_Object
)),
435 XSETTYPE (val
, Lisp_Process
);
438 XFASTINT (p
->infd
) = 0;
439 XFASTINT (p
->outfd
) = 0;
440 XFASTINT (p
->pid
) = 0;
441 XFASTINT (p
->tick
) = 0;
442 XFASTINT (p
->update_tick
) = 0;
443 p
->raw_status_low
= Qnil
;
444 p
->raw_status_high
= Qnil
;
446 p
->mark
= Fmake_marker ();
448 /* If name is already in use, modify it until it is unused. */
453 tem
= Fget_process (name1
);
454 if (NILP (tem
)) break;
455 sprintf (suffix
, "<%d>", i
);
456 name1
= concat2 (name
, build_string (suffix
));
460 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
464 remove_process (proc
)
465 register Lisp_Object proc
;
467 register Lisp_Object pair
;
469 pair
= Frassq (proc
, Vprocess_alist
);
470 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
471 Fset_marker (XPROCESS (proc
)->mark
, Qnil
, Qnil
);
473 deactivate_process (proc
);
476 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
477 "Return t if OBJECT is a process.")
481 return XTYPE (obj
) == Lisp_Process
? Qt
: Qnil
;
484 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
485 "Return the process named NAME, or nil if there is none.")
487 register Lisp_Object name
;
489 if (XTYPE (name
) == Lisp_Process
)
491 CHECK_STRING (name
, 0);
492 return Fcdr (Fassoc (name
, Vprocess_alist
));
495 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
496 "Return the (or, a) process associated with BUFFER.\n\
497 BUFFER may be a buffer or the name of one.")
499 register Lisp_Object name
;
501 register Lisp_Object buf
, tail
, proc
;
503 if (NILP (name
)) return Qnil
;
504 buf
= Fget_buffer (name
);
505 if (NILP (buf
)) return Qnil
;
507 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
509 proc
= Fcdr (Fcar (tail
));
510 if (XTYPE (proc
) == Lisp_Process
&& EQ (XPROCESS (proc
)->buffer
, buf
))
516 /* This is how commands for the user decode process arguments. It
517 accepts a process, a process name, a buffer, a buffer name, or nil.
518 Buffers denote the first process in the buffer, and nil denotes the
523 register Lisp_Object name
;
525 register Lisp_Object proc
;
527 proc
= Fget_buffer_process (Fcurrent_buffer ());
530 proc
= Fget_process (name
);
532 proc
= Fget_buffer_process (Fget_buffer (name
));
539 error ("Current buffer has no process");
541 error ("Process %s does not exist", XSTRING (name
)->data
);
545 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
546 "Delete PROCESS: kill it and forget about it immediately.\n\
547 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
548 nil, indicating the current buffer's process.")
550 register Lisp_Object proc
;
552 proc
= get_process (proc
);
553 XPROCESS (proc
)->raw_status_low
= Qnil
;
554 XPROCESS (proc
)->raw_status_high
= Qnil
;
555 if (NETCONN_P (proc
))
557 XPROCESS (proc
)->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
558 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
560 else if (XFASTINT (XPROCESS (proc
)->infd
))
562 Fkill_process (proc
, Qnil
);
563 /* Do this now, since remove_process will make sigchld_handler do nothing. */
564 XPROCESS (proc
)->status
565 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
566 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
569 remove_process (proc
);
573 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
574 "Return the status of PROCESS: a symbol, one of these:\n\
575 run -- for a process that is running.\n\
576 stop -- for a process stopped but continuable.\n\
577 exit -- for a process that has exited.\n\
578 signal -- for a process that has got a fatal signal.\n\
579 open -- for a network stream connection that is open.\n\
580 closed -- for a network stream connection that is closed.\n\
581 nil -- if arg is a process name and no such process exists.\n\
582 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
583 nil, indicating the current buffer's process.")
585 register Lisp_Object proc
;
587 register struct Lisp_Process
*p
;
588 register Lisp_Object status
;
589 proc
= Fget_process (proc
);
593 if (!NILP (p
->raw_status_low
))
596 if (XTYPE (status
) == Lisp_Cons
)
597 status
= XCONS (status
)->car
;
598 if (NETCONN_P (proc
))
600 if (EQ (status
, Qrun
))
602 else if (EQ (status
, Qexit
))
608 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
610 "Return the exit status of PROCESS or the signal number that killed it.\n\
611 If PROCESS has not yet exited or died, return 0.")
613 register Lisp_Object proc
;
615 CHECK_PROCESS (proc
, 0);
616 if (!NILP (XPROCESS (proc
)->raw_status_low
))
617 update_status (XPROCESS (proc
));
618 if (XTYPE (XPROCESS (proc
)->status
) == Lisp_Cons
)
619 return XCONS (XCONS (XPROCESS (proc
)->status
)->cdr
)->car
;
620 return make_number (0);
623 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
624 "Return the process id of PROCESS.\n\
625 This is the pid of the Unix process which PROCESS uses or talks to.\n\
626 For a network connection, this value is nil.")
628 register Lisp_Object proc
;
630 CHECK_PROCESS (proc
, 0);
631 return XPROCESS (proc
)->pid
;
634 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
635 "Return the name of PROCESS, as a string.\n\
636 This is the name of the program invoked in PROCESS,\n\
637 possibly modified to make it unique among process names.")
639 register Lisp_Object proc
;
641 CHECK_PROCESS (proc
, 0);
642 return XPROCESS (proc
)->name
;
645 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
646 "Return the command that was executed to start PROCESS.\n\
647 This is a list of strings, the first string being the program executed\n\
648 and the rest of the strings being the arguments given to it.\n\
649 For a non-child channel, this is nil.")
651 register Lisp_Object proc
;
653 CHECK_PROCESS (proc
, 0);
654 return XPROCESS (proc
)->command
;
657 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
659 "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
661 register Lisp_Object proc
, buffer
;
663 CHECK_PROCESS (proc
, 0);
665 CHECK_BUFFER (buffer
, 1);
666 XPROCESS (proc
)->buffer
= buffer
;
670 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
672 "Return the buffer PROCESS is associated with.\n\
673 Output from PROCESS is inserted in this buffer\n\
674 unless PROCESS has a filter.")
676 register Lisp_Object proc
;
678 CHECK_PROCESS (proc
, 0);
679 return XPROCESS (proc
)->buffer
;
682 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
684 "Return the marker for the end of the last output from PROCESS.")
686 register Lisp_Object proc
;
688 CHECK_PROCESS (proc
, 0);
689 return XPROCESS (proc
)->mark
;
692 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
694 "Give PROCESS the filter function FILTER; nil means no filter.\n\
695 t means stop accepting output from the process.\n\
696 When a process has a filter, each time it does output\n\
697 the entire string of output is passed to the filter.\n\
698 The filter gets two arguments: the process and the string of output.\n\
699 If the process has a filter, its buffer is not used for output.")
701 register Lisp_Object proc
, filter
;
703 CHECK_PROCESS (proc
, 0);
705 FD_CLR (XPROCESS (proc
)->infd
, &input_wait_mask
);
706 else if (EQ (XPROCESS (proc
)->filter
, Qt
))
707 FD_SET (XPROCESS (proc
)->infd
, &input_wait_mask
);
708 XPROCESS (proc
)->filter
= filter
;
712 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
714 "Returns the filter function of PROCESS; nil if none.\n\
715 See `set-process-filter' for more info on filter functions.")
717 register Lisp_Object proc
;
719 CHECK_PROCESS (proc
, 0);
720 return XPROCESS (proc
)->filter
;
723 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
725 "Give PROCESS the sentinel SENTINEL; nil for none.\n\
726 The sentinel is called as a function when the process changes state.\n\
727 It gets two arguments: the process, and a string describing the change.")
729 register Lisp_Object proc
, sentinel
;
731 CHECK_PROCESS (proc
, 0);
732 XPROCESS (proc
)->sentinel
= sentinel
;
736 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
738 "Return the sentinel of PROCESS; nil if none.\n\
739 See `set-process-sentinel' for more info on sentinels.")
741 register Lisp_Object proc
;
743 CHECK_PROCESS (proc
, 0);
744 return XPROCESS (proc
)->sentinel
;
747 DEFUN ("process-kill-without-query", Fprocess_kill_without_query
,
748 Sprocess_kill_without_query
, 1, 2, 0,
749 "Say no query needed if PROCESS is running when Emacs is exited.\n\
750 Optional second argument if non-nill says to require a query.\n\
751 Value is t if a query was formerly required.")
753 register Lisp_Object proc
, value
;
757 CHECK_PROCESS (proc
, 0);
758 tem
= XPROCESS (proc
)->kill_without_query
;
759 XPROCESS (proc
)->kill_without_query
= Fnull (value
);
764 #if 0 /* Turned off because we don't currently record this info
765 in the process. Perhaps add it. */
766 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
767 "Return the connection type of `PROCESS'.\n\
768 The value is `nil' for a pipe,\n\
769 `t' or `pty' for a pty, or `stream' for a socket connection.")
773 return XPROCESS (process
)->type
;
780 register Lisp_Object tail
, tem
;
781 Lisp_Object proc
, minspace
, tem1
;
782 register struct buffer
*old
= current_buffer
;
783 register struct Lisp_Process
*p
;
787 XFASTINT (minspace
) = 1;
789 set_buffer_internal (XBUFFER (Vstandard_output
));
790 Fbuffer_disable_undo (Vstandard_output
);
792 current_buffer
->truncate_lines
= Qt
;
795 Proc Status Buffer Command\n\
796 ---- ------ ------ -------\n", -1);
798 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
802 proc
= Fcdr (Fcar (tail
));
804 if (NILP (p
->childp
))
807 Finsert (1, &p
->name
);
808 Findent_to (make_number (13), minspace
);
810 if (!NILP (p
->raw_status_low
))
813 if (XTYPE (p
->status
) == Lisp_Cons
)
814 symbol
= XCONS (p
->status
)->car
;
817 if (EQ (symbol
, Qsignal
))
820 tem
= Fcar (Fcdr (p
->status
));
822 if (XINT (tem
) < NSIG
)
823 write_string (sys_errlist
[XINT (tem
)], -1);
826 Fprinc (symbol
, Qnil
);
828 else if (NETCONN_P (proc
))
830 if (EQ (symbol
, Qrun
))
831 write_string ("open", -1);
832 else if (EQ (symbol
, Qexit
))
833 write_string ("closed", -1);
835 Fprinc (symbol
, Qnil
);
838 Fprinc (symbol
, Qnil
);
840 if (EQ (symbol
, Qexit
))
843 tem
= Fcar (Fcdr (p
->status
));
846 sprintf (tembuf
, " %d", XFASTINT (tem
));
847 write_string (tembuf
, -1);
851 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
852 remove_process (proc
);
854 Findent_to (make_number (22), minspace
);
855 if (NILP (p
->buffer
))
856 insert_string ("(none)");
857 else if (NILP (XBUFFER (p
->buffer
)->name
))
858 insert_string ("(Killed)");
860 Finsert (1, &XBUFFER (p
->buffer
)->name
);
862 Findent_to (make_number (37), minspace
);
864 if (NETCONN_P (proc
))
866 sprintf (tembuf
, "(network stream connection to %s)\n",
867 XSTRING (p
->childp
)->data
);
868 insert_string (tembuf
);
882 insert_string ("\n");
888 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 0, "",
889 "Display a list of all processes.\n\
890 \(Any processes listed as Exited or Signaled are actually eliminated\n\
891 after the listing is made.)")
894 internal_with_output_to_temp_buffer ("*Process List*",
895 list_processes_1
, Qnil
);
899 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
900 "Return a list of all processes.")
903 return Fmapcar (Qcdr
, Vprocess_alist
);
906 /* Starting asynchronous inferior processes. */
908 static Lisp_Object
start_process_unwind ();
910 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
911 "Start a program in a subprocess. Return the process object for it.\n\
912 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
913 NAME is name for process. It is modified if necessary to make it unique.\n\
914 BUFFER is the buffer or (buffer-name) to associate with the process.\n\
915 Process output goes at end of that buffer, unless you specify\n\
916 an output stream or filter function to handle the output.\n\
917 BUFFER may be also nil, meaning that this process is not associated\n\
919 Third arg is program file name. It is searched for as in the shell.\n\
920 Remaining arguments are strings to give program as arguments.")
923 register Lisp_Object
*args
;
925 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
927 register unsigned char *new_argv
;
930 register unsigned char **new_argv
;
933 int count
= specpdl_ptr
- specpdl
;
937 buffer
= Fget_buffer_create (buffer
);
939 /* Make sure that the child will be able to chdir to the current
940 buffer's current directory, or its unhandled equivalent. We
941 can't just have the child check for an error when it does the
942 chdir, since it's in a vfork.
944 We have to GCPRO around this because Fexpand_file_name and
945 Funhandled_file_name_directory might call a file name handling
946 function. The argument list is protected by the caller, so all
947 we really have to worry about is buffer. */
949 struct gcpro gcpro1
, gcpro2
;
951 current_dir
= current_buffer
->directory
;
953 GCPRO2 (buffer
, current_dir
);
956 expand_and_dir_to_file
957 (Funhandled_file_name_directory (current_dir
), Qnil
);
958 if (NILP (Ffile_accessible_directory_p (current_dir
)))
959 report_file_error ("Setting current directory",
960 Fcons (current_buffer
->directory
, Qnil
));
966 CHECK_STRING (name
, 0);
970 CHECK_STRING (program
, 2);
973 /* Make a one member argv with all args concatenated
974 together separated by a blank. */
975 len
= XSTRING (program
)->size
+ 2;
976 for (i
= 3; i
< nargs
; i
++)
979 CHECK_STRING (tem
, i
);
980 len
+= XSTRING (tem
)->size
+ 1; /* count the blank */
982 new_argv
= (unsigned char *) alloca (len
);
983 strcpy (new_argv
, XSTRING (program
)->data
);
984 for (i
= 3; i
< nargs
; i
++)
987 CHECK_STRING (tem
, i
);
988 strcat (new_argv
, " ");
989 strcat (new_argv
, XSTRING (tem
)->data
);
991 /* Need to add code here to check for program existence on VMS */
994 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
996 for (i
= 3; i
< nargs
; i
++)
999 CHECK_STRING (tem
, i
);
1000 new_argv
[i
- 2] = XSTRING (tem
)->data
;
1002 new_argv
[i
- 2] = 0;
1003 new_argv
[0] = XSTRING (program
)->data
;
1005 /* If program file name is not absolute, search our path for it */
1006 if (new_argv
[0][0] != '/')
1009 openp (Vexec_path
, program
, EXEC_SUFFIXES
, &tem
, 1);
1011 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1012 new_argv
[0] = XSTRING (tem
)->data
;
1014 #endif /* not VMS */
1016 proc
= make_process (name
);
1017 /* If an error occurs and we can't start the process, we want to
1018 remove it from the process list. This means that each error
1019 check in create_process doesn't need to call remove_process
1020 itself; it's all taken care of here. */
1021 record_unwind_protect (start_process_unwind
, proc
);
1023 XPROCESS (proc
)->childp
= Qt
;
1024 XPROCESS (proc
)->command_channel_p
= Qnil
;
1025 XPROCESS (proc
)->buffer
= buffer
;
1026 XPROCESS (proc
)->sentinel
= Qnil
;
1027 XPROCESS (proc
)->filter
= Qnil
;
1028 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1030 create_process (proc
, new_argv
, current_dir
);
1032 return unbind_to (count
, proc
);
1035 /* This function is the unwind_protect form for Fstart_process. If
1036 PROC doesn't have its pid set, then we know someone has signalled
1037 an error and the process wasn't started successfully, so we should
1038 remove it from the process list. */
1040 start_process_unwind (proc
)
1043 if (XTYPE (proc
) != Lisp_Process
)
1046 /* Was PROC started successfully? */
1047 if (XINT (XPROCESS (proc
)->pid
) <= 0)
1048 remove_process (proc
);
1055 create_process_1 (signo
)
1059 /* USG systems forget handlers when they are used;
1060 must reestablish each time */
1061 signal (signo
, create_process_1
);
1065 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1068 /* Mimic blocking of signals on system V, which doesn't really have it. */
1070 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1071 int sigchld_deferred
;
1074 create_process_sigchld ()
1076 signal (SIGCHLD
, create_process_sigchld
);
1078 sigchld_deferred
= 1;
1084 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1085 create_process (process
, new_argv
, current_dir
)
1086 Lisp_Object process
;
1088 Lisp_Object current_dir
;
1090 int pid
, inchannel
, outchannel
, forkin
, forkout
;
1093 SIGTYPE (*sigchld
)();
1096 extern char **environ
;
1098 inchannel
= outchannel
= -1;
1101 if (EQ (Vprocess_connection_type
, Qt
))
1102 outchannel
= inchannel
= allocate_pty ();
1107 /* On USG systems it does not work to open the pty's tty here
1108 and then close and reopen it in the child. */
1110 /* Don't let this terminal become our controlling terminal
1111 (in case we don't have one). */
1112 forkout
= forkin
= open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1114 forkout
= forkin
= open (pty_name
, O_RDWR
, 0);
1117 report_file_error ("Opening pty", Qnil
);
1119 forkin
= forkout
= -1;
1120 #endif /* not USG */
1124 #endif /* HAVE_PTYS */
1127 if (socketpair (AF_UNIX
, SOCK_STREAM
, 0, sv
) < 0)
1128 report_file_error ("Opening socketpair", Qnil
);
1129 outchannel
= inchannel
= sv
[0];
1130 forkout
= forkin
= sv
[1];
1132 #else /* not SKTPAIR */
1141 #endif /* not SKTPAIR */
1144 /* Replaced by close_process_descs */
1145 set_exclusive_use (inchannel
);
1146 set_exclusive_use (outchannel
);
1149 /* Stride people say it's a mystery why this is needed
1150 as well as the O_NDELAY, but that it fails without this. */
1151 #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
1154 ioctl (inchannel
, FIONBIO
, &one
);
1159 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1162 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1166 /* Record this as an active process, with its channels.
1167 As a result, child_setup will close Emacs's side of the pipes. */
1168 chan_process
[inchannel
] = process
;
1169 XFASTINT (XPROCESS (process
)->infd
) = inchannel
;
1170 XFASTINT (XPROCESS (process
)->outfd
) = outchannel
;
1171 /* Record the tty descriptor used in the subprocess. */
1173 XPROCESS (process
)->subtty
= Qnil
;
1175 XFASTINT (XPROCESS (process
)->subtty
) = forkin
;
1176 XPROCESS (process
)->pty_flag
= (pty_flag
? Qt
: Qnil
);
1177 XPROCESS (process
)->status
= Qrun
;
1179 /* Delay interrupts until we have a chance to store
1180 the new fork's pid in its process structure */
1184 #else /* not BSD4_1 */
1185 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1186 sigsetmask (sigmask (SIGCHLD
));
1187 #else /* ordinary USG */
1189 sigchld_deferred
= 0;
1190 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
1192 #endif /* ordinary USG */
1193 #endif /* not BSD4_1 */
1194 #endif /* SIGCHLD */
1196 /* Until we store the proper pid, enable sigchld_handler
1197 to recognize an unknown pid as standing for this process.
1198 It is very important not to let this `marker' value stay
1199 in the table after this function has returned; if it does
1200 it might cause call-process to hang and subsequent asynchronous
1201 processes to get their return values scrambled. */
1202 XSETINT (XPROCESS (process
)->pid
, -1);
1205 /* child_setup must clobber environ on systems with true vfork.
1206 Protect it from permanent change. */
1207 char **save_environ
= environ
;
1212 int xforkin
= forkin
;
1213 int xforkout
= forkout
;
1215 #if 0 /* This was probably a mistake--it duplicates code later on,
1216 but fails to handle all the cases. */
1217 /* Make sure SIGCHLD is not blocked in the child. */
1218 sigsetmask (SIGEMPTYMASK
);
1221 /* Make the pty be the controlling terminal of the process. */
1223 /* First, disconnect its current controlling terminal. */
1227 /* Make the pty's terminal the controlling terminal. */
1229 /* We ignore the return value
1230 because faith@cs.unc.edu says that is necessary on Linux. */
1231 ioctl (xforkin
, TIOCSCTTY
, 0);
1233 #else /* not HAVE_SETSID */
1234 #if defined (USG) && !defined (IRIX)
1235 /* It's very important to call setpgrp() here and no time
1236 afterwards. Otherwise, we lose our controlling tty which
1237 is set when we open the pty. */
1240 #endif /* not HAVE_SETSID */
1242 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1243 can do TIOCSPGRP only to the process's controlling tty. */
1246 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1247 I can't test it since I don't have 4.3. */
1248 int j
= open ("/dev/tty", O_RDWR
, 0);
1249 ioctl (j
, TIOCNOTTY
, 0);
1252 /* In order to get a controlling terminal on some versions
1253 of BSD, it is necessary to put the process in pgrp 0
1254 before it opens the terminal. */
1258 #endif /* TIOCNOTTY */
1260 #if !defined (RTU) && !defined (UNIPLUS)
1261 /*** There is a suggestion that this ought to be a
1262 conditional on TIOCSPGRP. */
1263 /* Now close the pty (if we had it open) and reopen it.
1264 This makes the pty the controlling terminal of the subprocess. */
1267 /* I wonder if close (open (pty_name, ...)) would work? */
1270 xforkout
= xforkin
= open (pty_name
, O_RDWR
, 0);
1275 #endif /* not UNIPLUS and not RTU */
1276 #ifdef SETUP_SLAVE_PTY
1278 #endif /* SETUP_SLAVE_PTY */
1280 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
1281 Now reenable it in the child, so it will die when we want it to. */
1283 signal (SIGHUP
, SIG_DFL
);
1285 #endif /* HAVE_PTYS */
1290 #else /* not BSD4_1 */
1291 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1292 sigsetmask (SIGEMPTYMASK
);
1293 #else /* ordinary USG */
1295 signal (SIGCHLD
, sigchld
);
1297 #endif /* ordinary USG */
1298 #endif /* not BSD4_1 */
1299 #endif /* SIGCHLD */
1301 child_setup_tty (xforkout
);
1302 child_setup (xforkin
, xforkout
, xforkout
,
1303 new_argv
, 1, current_dir
);
1305 environ
= save_environ
;
1309 report_file_error ("Doing vfork", Qnil
);
1311 XFASTINT (XPROCESS (process
)->pid
) = pid
;
1313 FD_SET (inchannel
, &input_wait_mask
);
1315 /* If the subfork execv fails, and it exits,
1316 this close hangs. I don't know why.
1317 So have an interrupt jar it loose. */
1319 signal (SIGALRM
, create_process_1
);
1322 /* OK to close only if it's not a pty. Otherwise we need to leave
1323 it open for ioctl to get pgrp when signals are sent, or to send
1324 the interrupt characters through if that's how we're signalling
1325 subprocesses. Alternately if you are concerned about running out
1326 of file descriptors, you could just save the tty name and open
1327 just to do the ioctl. */
1328 if (NILP (XFASTINT (XPROCESS (process
)->pty_flag
)))
1331 XPROCESS (process
)->subtty
= Qnil
;
1337 if (forkin
!= forkout
&& forkout
>= 0)
1343 #else /* not BSD4_1 */
1344 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1345 sigsetmask (SIGEMPTYMASK
);
1346 #else /* ordinary USG */
1348 signal (SIGCHLD
, sigchld
);
1349 /* Now really handle any of these signals
1350 that came in during this function. */
1351 if (sigchld_deferred
)
1352 kill (getpid (), SIGCHLD
);
1354 #endif /* ordinary USG */
1355 #endif /* not BSD4_1 */
1356 #endif /* SIGCHLD */
1358 #endif /* not VMS */
1362 /* open a TCP network connection to a given HOST/SERVICE. Treated
1363 exactly like a normal process when reading and writing. Only
1364 differences are in status display and process deletion. A network
1365 connection has no PID; you cannot signal it. All you can do is
1366 deactivate and close it via delete-process */
1368 DEFUN ("open-network-stream", Fopen_network_stream
, Sopen_network_stream
,
1370 "Open a TCP connection for a service to a host.\n\
1371 Returns a subprocess-object to represent the connection.\n\
1372 Input and output work as for subprocesses; `delete-process' closes it.\n\
1373 Args are NAME BUFFER HOST SERVICE.\n\
1374 NAME is name for process. It is modified if necessary to make it unique.\n\
1375 BUFFER is the buffer (or buffer-name) to associate with the process.\n\
1376 Process output goes at end of that buffer, unless you specify\n\
1377 an output stream or filter function to handle the output.\n\
1378 BUFFER may be also nil, meaning that this process is not associated\n\
1380 Third arg is name of the host to connect to, or its IP address.\n\
1381 Fourth arg SERVICE is name of the service desired, or an integer\n\
1382 specifying a port number to connect to.")
1383 (name
, buffer
, host
, service
)
1384 Lisp_Object name
, buffer
, host
, service
;
1388 struct sockaddr_in address
;
1389 struct servent
*svc_info
;
1390 struct hostent
*host_info_ptr
, host_info
;
1391 char *(addr_list
[2]);
1392 IN_ADDR numeric_addr
;
1396 struct hostent host_info_fixed
;
1397 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1399 GCPRO4 (name
, buffer
, host
, service
);
1400 CHECK_STRING (name
, 0);
1401 CHECK_STRING (host
, 0);
1402 if (XTYPE(service
) == Lisp_Int
)
1403 port
= htons ((unsigned short) XINT (service
));
1406 CHECK_STRING (service
, 0);
1407 svc_info
= getservbyname (XSTRING (service
)->data
, "tcp");
1409 error ("Unknown service \"%s\"", XSTRING (service
)->data
);
1410 port
= svc_info
->s_port
;
1414 host_info_ptr
= gethostbyname (XSTRING (host
)->data
);
1415 if (host_info_ptr
== 0)
1416 /* Attempt to interpret host as numeric inet address */
1418 numeric_addr
= inet_addr ((char *) XSTRING (host
)->data
);
1419 if (NUMERIC_ADDR_ERROR
)
1420 error ("Unknown host \"%s\"", XSTRING (host
)->data
);
1422 host_info_ptr
= &host_info
;
1423 host_info
.h_name
= 0;
1424 host_info
.h_aliases
= 0;
1425 host_info
.h_addrtype
= AF_INET
;
1427 /* Older machines have only one address slot called h_addr.
1428 Newer machines have h_addr_list, but #define h_addr to
1429 be its first element. */
1430 host_info
.h_addr_list
= &(addr_list
[0]);
1432 host_info
.h_addr
= (char*)(&numeric_addr
);
1434 host_info
.h_length
= strlen (addr_list
[0]);
1437 bzero (&address
, sizeof address
);
1438 bcopy (host_info_ptr
->h_addr
, (char *) &address
.sin_addr
,
1439 host_info_ptr
->h_length
);
1440 address
.sin_family
= host_info_ptr
->h_addrtype
;
1441 address
.sin_port
= port
;
1443 s
= socket (host_info_ptr
->h_addrtype
, SOCK_STREAM
, 0);
1445 report_file_error ("error creating socket", Fcons (name
, Qnil
));
1448 if (connect (s
, (struct sockaddr
*) &address
, sizeof address
) == -1)
1455 report_file_error ("connection failed",
1456 Fcons (host
, Fcons (name
, Qnil
)));
1459 s
= connect_server (0);
1461 report_file_error ("error creating socket", Fcons (name
, Qnil
));
1462 send_command (s
, C_PORT
, 0, "%s:%d", XSTRING (host
)->data
, ntohs (port
));
1463 send_command (s
, C_DUMB
, 1, 0);
1469 report_file_error ("error duplicating socket", Fcons (name
, Qnil
));
1472 buffer
= Fget_buffer_create (buffer
);
1473 proc
= make_process (name
);
1475 chan_process
[inch
] = proc
;
1478 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
1481 fcntl (inch
, F_SETFL
, O_NDELAY
);
1485 XPROCESS (proc
)->childp
= host
;
1486 XPROCESS (proc
)->command_channel_p
= Qnil
;
1487 XPROCESS (proc
)->buffer
= buffer
;
1488 XPROCESS (proc
)->sentinel
= Qnil
;
1489 XPROCESS (proc
)->filter
= Qnil
;
1490 XPROCESS (proc
)->command
= Qnil
;
1491 XPROCESS (proc
)->pid
= Qnil
;
1492 XFASTINT (XPROCESS (proc
)->infd
) = s
;
1493 XFASTINT (XPROCESS (proc
)->outfd
) = outch
;
1494 XPROCESS (proc
)->status
= Qrun
;
1495 FD_SET (inch
, &input_wait_mask
);
1500 #endif /* HAVE_SOCKETS */
1502 deactivate_process (proc
)
1505 register int inchannel
, outchannel
;
1506 register struct Lisp_Process
*p
= XPROCESS (proc
);
1508 inchannel
= XFASTINT (p
->infd
);
1509 outchannel
= XFASTINT (p
->outfd
);
1513 /* Beware SIGCHLD hereabouts. */
1514 flush_pending_output (inchannel
);
1517 VMS_PROC_STUFF
*get_vms_process_pointer (), *vs
;
1518 sys$
dassgn (outchannel
);
1519 vs
= get_vms_process_pointer (p
->pid
);
1521 give_back_vms_process_stuff (vs
);
1525 if (outchannel
&& outchannel
!= inchannel
)
1529 XFASTINT (p
->infd
) = 0;
1530 XFASTINT (p
->outfd
) = 0;
1531 chan_process
[inchannel
] = Qnil
;
1532 FD_CLR (inchannel
, &input_wait_mask
);
1536 /* Close all descriptors currently in use for communication
1537 with subprocess. This is used in a newly-forked subprocess
1538 to get rid of irrelevant descriptors. */
1540 close_process_descs ()
1543 for (i
= 0; i
< MAXDESC
; i
++)
1545 Lisp_Object process
;
1546 process
= chan_process
[i
];
1547 if (!NILP (process
))
1549 int in
= XFASTINT (XPROCESS (process
)->infd
);
1550 int out
= XFASTINT (XPROCESS (process
)->outfd
);
1553 if (out
&& in
!= out
)
1559 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
1561 "Allow any pending output from subprocesses to be read by Emacs.\n\
1562 It is read into the process' buffers or given to their filter functions.\n\
1563 Non-nil arg PROCESS means do not return until some output has been received\n\
1565 Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of\n\
1566 seconds and microseconds to wait; return after that much time whether\n\
1567 or not there is input.\n\
1568 Return non-nil iff we received any output before the timeout expired.")
1569 (proc
, timeout
, timeout_msecs
)
1570 register Lisp_Object proc
, timeout
, timeout_msecs
;
1575 if (! NILP (timeout_msecs
))
1577 CHECK_NUMBER (timeout_msecs
, 2);
1578 useconds
= XINT (timeout_msecs
);
1579 if (XTYPE (timeout
) != Lisp_Int
)
1580 XSET (timeout
, Lisp_Int
, 0);
1583 int carry
= useconds
/ 1000000;
1585 XSETINT (timeout
, XINT (timeout
) + carry
);
1586 useconds
-= carry
* 1000000;
1588 /* I think this clause is necessary because C doesn't
1589 guarantee a particular rounding direction for negative
1593 XSETINT (timeout
, XINT (timeout
) - 1);
1594 useconds
+= 1000000;
1601 if (! NILP (timeout
))
1603 CHECK_NUMBER (timeout
, 1);
1604 seconds
= XINT (timeout
);
1617 XFASTINT (proc
) = 0;
1620 (wait_reading_process_input (seconds
, useconds
, proc
, 0)
1624 /* This variable is different from waiting_for_input in keyboard.c.
1625 It is used to communicate to a lisp process-filter/sentinel (via the
1626 function Fwaiting_for_user_input_p below) whether emacs was waiting
1627 for user-input when that process-filter was called.
1628 waiting_for_input cannot be used as that is by definition 0 when
1629 lisp code is being evalled */
1630 static int waiting_for_user_input_p
;
1632 /* Read and dispose of subprocess output while waiting for timeout to
1633 elapse and/or keyboard input to be available.
1636 timeout in seconds, or
1637 zero for no limit, or
1638 -1 means gobble data immediately available but don't wait for any.
1641 an additional duration to wait, measured in microseconds.
1642 If this is nonzero and time_limit is 0, then the timeout
1643 consists of MICROSECS only.
1645 READ_KBD is a lisp value:
1646 0 to ignore keyboard input, or
1647 1 to return when input is available, or
1648 -1 meaning caller will actually read the input, so don't throw to
1649 the quit handler, or
1650 a cons cell, meaning wait wait until its car is non-nil
1651 (and gobble terminal input into the buffer if any arrives), or
1652 a process object, meaning wait until something arrives from that
1653 process. The return value is true iff we read some input from
1656 DO_DISPLAY != 0 means redisplay should be done to show subprocess
1657 output that arrives.
1659 If READ_KBD is a pointer to a struct Lisp_Process, then the
1660 function returns true iff we received input from that process
1661 before the timeout elapsed.
1662 Otherwise, return true iff we received input from any process. */
1664 wait_reading_process_input (time_limit
, microsecs
, read_kbd
, do_display
)
1665 int time_limit
, microsecs
;
1666 Lisp_Object read_kbd
;
1669 register int channel
, nfds
, m
;
1670 static SELECT_TYPE Available
;
1673 EMACS_TIME timeout
, end_time
, garbage
;
1675 int wait_channel
= 0;
1676 struct Lisp_Process
*wait_proc
= 0;
1677 int got_some_input
= 0;
1678 Lisp_Object
*wait_for_cell
= 0;
1680 FD_ZERO (&Available
);
1682 /* If read_kbd is a process to watch, set wait_proc and wait_channel
1684 if (XTYPE (read_kbd
) == Lisp_Process
)
1686 wait_proc
= XPROCESS (read_kbd
);
1687 wait_channel
= XFASTINT (wait_proc
->infd
);
1688 XFASTINT (read_kbd
) = 0;
1691 /* If waiting for non-nil in a cell, record where. */
1692 if (XTYPE (read_kbd
) == Lisp_Cons
)
1694 wait_for_cell
= &XCONS (read_kbd
)->car
;
1695 XFASTINT (read_kbd
) = 0;
1698 waiting_for_user_input_p
= XINT (read_kbd
);
1700 /* Since we may need to wait several times,
1701 compute the absolute time to return at. */
1702 if (time_limit
|| microsecs
)
1704 EMACS_GET_TIME (end_time
);
1705 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
1706 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
1711 /* If calling from keyboard input, do not quit
1712 since we want to return C-g as an input character.
1713 Otherwise, do pending quit if requested. */
1714 if (XINT (read_kbd
) >= 0)
1717 /* Exit now if the cell we're waiting for became non-nil. */
1718 if (wait_for_cell
&& ! NILP (*wait_for_cell
))
1721 /* Compute time from now till when time limit is up */
1722 /* Exit if already run out */
1723 if (time_limit
== -1)
1725 /* -1 specified for timeout means
1726 gobble output available now
1727 but don't wait at all. */
1729 EMACS_SET_SECS_USECS (timeout
, 0, 0);
1731 else if (time_limit
|| microsecs
)
1733 EMACS_GET_TIME (timeout
);
1734 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
1735 if (EMACS_TIME_NEG_P (timeout
))
1740 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
1743 /* Cause C-g and alarm signals to take immediate action,
1744 and cause input available signals to zero out timeout.
1746 It is important that we do this before checking for process
1747 activity. If we get a SIGCHLD after the explicit checks for
1748 process activity, timeout is the only way we will know. */
1749 if (XINT (read_kbd
) < 0)
1750 set_waiting_for_input (&timeout
);
1752 /* If status of something has changed, and no input is
1753 available, notify the user of the change right away. After
1754 this explicit check, we'll let the SIGCHLD handler zap
1755 timeout to get our attention. */
1756 if (update_tick
!= process_tick
&& do_display
)
1758 Atemp
= input_wait_mask
;
1759 EMACS_SET_SECS_USECS (timeout
, 0, 0);
1760 if (select (MAXDESC
, &Atemp
, 0, 0, &timeout
) <= 0)
1762 /* It's okay for us to do this and then continue with
1763 the loop, since timeout has already been zeroed out. */
1764 clear_waiting_for_input ();
1769 /* Don't wait for output from a non-running process. */
1770 if (wait_proc
!= 0 && !NILP (wait_proc
->raw_status_low
))
1771 update_status (wait_proc
);
1773 && ! EQ (wait_proc
->status
, Qrun
))
1775 clear_waiting_for_input ();
1779 /* Wait till there is something to do */
1781 Available
= input_wait_mask
;
1782 /* We used to have && wait_for_cell == 0
1783 but that led to lossage handling selection_request events:
1784 within one, we would start to handle another. */
1785 if (! XINT (read_kbd
))
1786 FD_CLR (keyboard_descriptor
, &Available
);
1788 /* If frame size has changed or the window is newly mapped,
1789 redisplay now, before we start to wait. There is a race
1790 condition here; if a SIGIO arrives between now and the select
1791 and indicates that a frame is trashed, the select may block
1792 displaying a trashed screen. */
1794 redisplay_preserve_echo_area ();
1796 if (XINT (read_kbd
) && detect_input_pending ())
1799 nfds
= select (MAXDESC
, &Available
, 0, 0, &timeout
);
1803 /* Make C-g and alarm signals set flags again */
1804 clear_waiting_for_input ();
1806 /* If we woke up due to SIGWINCH, actually change size now. */
1807 do_pending_window_change ();
1809 if (time_limit
&& nfds
== 0) /* timeout elapsed */
1813 if (xerrno
== EINTR
)
1814 FD_ZERO (&Available
);
1816 /* Ultrix select seems to return ENOMEM when it is
1817 interrupted. Treat it just like EINTR. Bleah. Note
1818 that we want to test for the "ultrix" CPP symbol, not
1819 "__ultrix__"; the latter is only defined under GCC, but
1820 not by DEC's bundled CC. -JimB */
1821 else if (xerrno
== ENOMEM
)
1822 FD_ZERO (&Available
);
1825 /* This happens for no known reason on ALLIANT.
1826 I am guessing that this is the right response. -- RMS. */
1827 else if (xerrno
== EFAULT
)
1828 FD_ZERO (&Available
);
1830 else if (xerrno
== EBADF
)
1833 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
1834 the child's closure of the pts gives the parent a SIGHUP, and
1835 the ptc file descriptor is automatically closed,
1836 yielding EBADF here or at select() call above.
1837 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
1838 in m-ibmrt-aix.h), and here we just ignore the select error.
1839 Cleanup occurs c/o status_notify after SIGCLD. */
1840 FD_ZERO (&Available
); /* Cannot depend on values returned */
1846 error("select error: %s", sys_errlist
[xerrno
]);
1848 #if defined(sun) && !defined(USG5_4)
1849 else if (nfds
> 0 && FD_ISSET (keyboard_descriptor
, &Available
)
1851 /* System sometimes fails to deliver SIGIO.
1853 David J. Mackenzie says that Emacs doesn't compile under
1854 Solaris if this code is enabled, thus the USG5_4 in the CPP
1855 conditional. "I haven't noticed any ill effects so far.
1856 If you find a Solaris expert somewhere, they might know
1858 kill (getpid (), SIGIO
);
1861 /* Check for keyboard input */
1862 /* If there is any, return immediately
1863 to give it higher priority than subprocesses */
1865 /* We used to do his if wait_for_cell,
1866 but that caused infinite recursion in selection request events. */
1867 if ((XINT (read_kbd
))
1868 && detect_input_pending ())
1871 if (detect_input_pending ())
1875 /* Exit now if the cell we're waiting for became non-nil. */
1876 if (wait_for_cell
&& ! NILP (*wait_for_cell
))
1880 /* If we think we have keyboard input waiting, but didn't get SIGIO
1881 go read it. This can happen with X on BSD after logging out.
1882 In that case, there really is no input and no SIGIO,
1883 but select says there is input. */
1885 if (XINT (read_kbd
) && interrupt_input
1886 && (FD_ISSET (keyboard_descriptor
, &Available
)))
1891 got_some_input
|= nfds
> 0;
1893 /* If checking input just got us a size-change event from X,
1894 obey it now if we should. */
1895 if (XINT (read_kbd
) || wait_for_cell
)
1896 do_pending_window_change ();
1898 /* Check for data from a process or a command channel */
1899 for (channel
= FIRST_PROC_DESC
; channel
< MAXDESC
; channel
++)
1901 if (FD_ISSET (channel
, &Available
))
1905 /* If waiting for this channel, arrange to return as
1906 soon as no more input to be processed. No more
1908 if (wait_channel
== channel
)
1914 proc
= chan_process
[channel
];
1918 /* Read data from the process, starting with our
1919 buffered-ahead character if we have one. */
1921 nread
= read_process_output (proc
, channel
);
1924 /* Since read_process_output can run a filter,
1925 which can call accept-process-output,
1926 don't try to read from any other processes
1927 before doing the select again. */
1928 FD_ZERO (&Available
);
1931 redisplay_preserve_echo_area ();
1934 else if (nread
== -1 && errno
== EWOULDBLOCK
)
1938 else if (nread
== -1 && errno
== EAGAIN
)
1942 else if (nread
== -1 && errno
== EAGAIN
)
1944 /* Note that we cannot distinguish between no input
1945 available now and a closed pipe.
1946 With luck, a closed pipe will be accompanied by
1947 subprocess termination and SIGCHLD. */
1948 else if (nread
== 0 && !NETCONN_P (proc
))
1950 #endif /* O_NDELAY */
1951 #endif /* O_NONBLOCK */
1952 #endif /* EWOULDBLOCK */
1954 /* On some OSs with ptys, when the process on one end of
1955 a pty exits, the other end gets an error reading with
1956 errno = EIO instead of getting an EOF (0 bytes read).
1957 Therefore, if we get an error reading and errno =
1958 EIO, just continue, because the child process has
1959 exited and should clean itself up soon (e.g. when we
1961 else if (nread
== -1 && errno
== EIO
)
1963 #endif /* HAVE_PTYS */
1964 /* If we can detect process termination, don't consider the process
1965 gone just because its pipe is closed. */
1967 else if (nread
== 0 && !NETCONN_P (proc
))
1972 /* Preserve status of processes already terminated. */
1973 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
1974 deactivate_process (proc
);
1975 if (!NILP (XPROCESS (proc
)->raw_status_low
))
1976 update_status (XPROCESS (proc
));
1977 if (EQ (XPROCESS (proc
)->status
, Qrun
))
1978 XPROCESS (proc
)->status
1979 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
1982 } /* end for each file descriptor */
1983 } /* end while exit conditions not met */
1985 /* If calling from keyboard input, do not quit
1986 since we want to return C-g as an input character.
1987 Otherwise, do pending quit if requested. */
1988 if (XINT (read_kbd
) >= 0)
1990 /* Prevent input_pending from remaining set if we quit. */
1991 clear_input_pending ();
1995 return got_some_input
;
1998 /* Read pending output from the process channel,
1999 starting with our buffered-ahead character if we have one.
2000 Yield number of characters read.
2002 This function reads at most 1024 characters.
2003 If you want to read all available subprocess output,
2004 you must call it repeatedly until it returns zero. */
2006 read_process_output (proc
, channel
)
2008 register int channel
;
2010 register int nchars
;
2016 register Lisp_Object outstream
;
2017 register struct buffer
*old
= current_buffer
;
2018 register struct Lisp_Process
*p
= XPROCESS (proc
);
2019 register int opoint
;
2022 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
2024 vs
= get_vms_process_pointer (p
->pid
);
2028 return(0); /* Really weird if it does this */
2029 if (!(vs
->iosb
[0] & 1))
2030 return -1; /* I/O error */
2033 error ("Could not get VMS process pointer");
2034 chars
= vs
->inputBuffer
;
2035 nchars
= clean_vms_buffer (chars
, vs
->iosb
[1]);
2038 start_vms_process_read (vs
); /* Crank up the next read on the process */
2039 return 1; /* Nothing worth printing, say we got 1 */
2043 if (proc_buffered_char
[channel
] < 0)
2044 nchars
= read (channel
, chars
, sizeof chars
);
2047 chars
[0] = proc_buffered_char
[channel
];
2048 proc_buffered_char
[channel
] = -1;
2049 nchars
= read (channel
, chars
+ 1, sizeof chars
- 1);
2053 nchars
= nchars
+ 1;
2055 #endif /* not VMS */
2057 if (nchars
<= 0) return nchars
;
2059 outstream
= p
->filter
;
2060 if (!NILP (outstream
))
2062 /* We inhibit quit here instead of just catching it so that
2063 hitting ^G when a filter happens to be running won't screw
2065 int count
= specpdl_ptr
- specpdl
;
2066 Lisp_Object odeactivate
;
2068 odeactivate
= Vdeactivate_mark
;
2070 specbind (Qinhibit_quit
, Qt
);
2071 call2 (outstream
, proc
, make_string (chars
, nchars
));
2073 /* Handling the process output should not deactivate the mark. */
2074 Vdeactivate_mark
= odeactivate
;
2077 start_vms_process_read (vs
);
2083 /* If no filter, write into buffer if it isn't dead. */
2084 if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
2086 Lisp_Object old_read_only
;
2087 Lisp_Object old_begv
, old_zv
;
2088 Lisp_Object odeactivate
;
2090 odeactivate
= Vdeactivate_mark
;
2092 Fset_buffer (p
->buffer
);
2094 old_read_only
= current_buffer
->read_only
;
2095 XFASTINT (old_begv
) = BEGV
;
2096 XFASTINT (old_zv
) = ZV
;
2098 current_buffer
->read_only
= Qnil
;
2100 /* Insert new output into buffer
2101 at the current end-of-output marker,
2102 thus preserving logical ordering of input and output. */
2103 if (XMARKER (p
->mark
)->buffer
)
2104 SET_PT (marker_position (p
->mark
));
2108 /* If the output marker is outside of the visible region, save
2109 the restriction and widen. */
2110 if (! (BEGV
<= point
&& point
<= ZV
))
2113 /* Make sure opoint floats ahead of any new text, just as point
2115 if (point
<= opoint
)
2118 /* Insert after old_begv, but before old_zv. */
2119 if (point
< XFASTINT (old_begv
))
2120 XFASTINT (old_begv
) += nchars
;
2121 if (point
<= XFASTINT (old_zv
))
2122 XFASTINT (old_zv
) += nchars
;
2124 /* Insert before markers in case we are inserting where
2125 the buffer's mark is, and the user's next command is Meta-y. */
2126 insert_before_markers (chars
, nchars
);
2127 Fset_marker (p
->mark
, make_number (point
), p
->buffer
);
2129 update_mode_lines
++;
2131 /* If the restriction isn't what it should be, set it. */
2132 if (XFASTINT (old_begv
) != BEGV
|| XFASTINT (old_zv
) != ZV
)
2133 Fnarrow_to_region (old_begv
, old_zv
);
2135 /* Handling the process output should not deactivate the mark. */
2136 Vdeactivate_mark
= odeactivate
;
2138 current_buffer
->read_only
= old_read_only
;
2140 set_buffer_internal (old
);
2143 start_vms_process_read (vs
);
2148 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
2150 "Returns non-NIL if emacs is waiting for input from the user.\n\
2151 This is intended for use by asynchronous process output filters and sentinels.")
2154 return ((waiting_for_user_input_p
) ? Qt
: Qnil
);
2157 /* Sending data to subprocess */
2159 jmp_buf send_process_frame
;
2162 send_process_trap ()
2168 longjmp (send_process_frame
, 1);
2171 send_process (proc
, buf
, len
)
2176 /* Don't use register vars; longjmp can lose them. */
2178 unsigned char *procname
= XSTRING (XPROCESS (proc
)->name
)->data
;
2182 struct Lisp_Process
*p
= XPROCESS (proc
);
2183 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
2186 if (! NILP (XPROCESS (proc
)->raw_status_low
))
2187 update_status (XPROCESS (proc
));
2188 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
2189 error ("Process %s not running", procname
);
2192 vs
= get_vms_process_pointer (p
->pid
);
2194 error ("Could not find this process: %x", p
->pid
);
2195 else if (write_to_vms_process (vs
, buf
, len
))
2198 if (!setjmp (send_process_frame
))
2202 SIGTYPE (*old_sigpipe
)();
2204 /* Don't send more than 500 bytes at a time. */
2207 old_sigpipe
= (SIGTYPE (*) ()) signal (SIGPIPE
, send_process_trap
);
2208 rv
= write (XFASTINT (XPROCESS (proc
)->outfd
), buf
, this);
2209 signal (SIGPIPE
, old_sigpipe
);
2214 || errno
== EWOULDBLOCK
2221 /* It would be nice to accept process output here,
2222 but that is difficult. For example, it could
2223 garbage what we are sending if that is from a buffer. */
2230 report_file_error ("writing to process", Fcons (proc
, Qnil
));
2234 /* Allow input from processes between bursts of sending.
2235 Otherwise things may get stopped up. */
2240 XFASTINT (zero
) = 0;
2241 wait_reading_process_input (-1, 0, zero
, 0);
2247 XPROCESS (proc
)->raw_status_low
= Qnil
;
2248 XPROCESS (proc
)->raw_status_high
= Qnil
;
2249 XPROCESS (proc
)->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
2250 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
2251 deactivate_process (proc
);
2253 error ("Error writing to process %s; closed it", procname
);
2255 error ("SIGPIPE raised on process %s; closed it", procname
);
2260 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
2262 "Send current contents of region as input to PROCESS.\n\
2263 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
2264 nil, indicating the current buffer's process.\n\
2265 Called from program, takes three arguments, PROCESS, START and END.\n\
2266 If the region is more than 500 characters long,\n\
2267 it is sent in several bunches. This may happen even for shorter regions.\n\
2268 Output from processes can arrive in between bunches.")
2269 (process
, start
, end
)
2270 Lisp_Object process
, start
, end
;
2275 proc
= get_process (process
);
2276 validate_region (&start
, &end
);
2278 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
2281 start1
= XINT (start
);
2282 send_process (proc
, &FETCH_CHAR (start1
), XINT (end
) - XINT (start
));
2287 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
2289 "Send PROCESS the contents of STRING as input.\n\
2290 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
2291 nil, indicating the current buffer's process.\n\
2292 If STRING is more than 500 characters long,\n\
2293 it is sent in several bunches. This may happen even for shorter strings.\n\
2294 Output from processes can arrive in between bunches.")
2296 Lisp_Object process
, string
;
2299 CHECK_STRING (string
, 1);
2300 proc
= get_process (process
);
2301 send_process (proc
, XSTRING (string
)->data
, XSTRING (string
)->size
);
2305 /* send a signal number SIGNO to PROCESS.
2306 CURRENT_GROUP means send to the process group that currently owns
2307 the terminal being used to communicate with PROCESS.
2308 This is used for various commands in shell mode.
2309 If NOMSG is zero, insert signal-announcements into process's buffers
2312 If we can, we try to signal PROCESS by sending control characters
2313 down the pipe. This allows us to signal inferiors who have changed
2314 their uid, for which killpg would return an EPERM error. */
2317 process_send_signal (process
, signo
, current_group
, nomsg
)
2318 Lisp_Object process
;
2320 Lisp_Object current_group
;
2324 register struct Lisp_Process
*p
;
2328 proc
= get_process (process
);
2329 p
= XPROCESS (proc
);
2331 if (!EQ (p
->childp
, Qt
))
2332 error ("Process %s is not a subprocess",
2333 XSTRING (p
->name
)->data
);
2334 if (!XFASTINT (p
->infd
))
2335 error ("Process %s is not active",
2336 XSTRING (p
->name
)->data
);
2338 if (NILP (p
->pty_flag
))
2339 current_group
= Qnil
;
2341 /* If we are using pgrps, get a pgrp number and make it negative. */
2342 if (!NILP (current_group
))
2344 #ifdef SIGNALS_VIA_CHARACTERS
2345 /* If possible, send signals to the entire pgrp
2346 by sending an input character to it. */
2348 /* TERMIOS is the latest and bestest, and seems most likely to
2349 work. If the system has it, use it. */
2356 tcgetattr (XFASTINT (p
->infd
), &t
);
2357 send_process (proc
, &t
.c_cc
[VINTR
], 1);
2361 tcgetattr (XFASTINT (p
->infd
), &t
);
2362 send_process (proc
, &t
.c_cc
[VQUIT
], 1);
2366 tcgetattr (XFASTINT (p
->infd
), &t
);
2368 send_process (proc
, &t
.c_cc
[VSWTCH
], 1);
2370 send_process (proc
, &t
.c_cc
[VSUSP
], 1);
2375 #else /* ! HAVE_TERMIOS */
2377 /* On Berkeley descendants, the following IOCTL's retrieve the
2378 current control characters. */
2379 #if defined (TIOCGLTC) && defined (TIOCGETC)
2387 ioctl (XFASTINT (p
->infd
), TIOCGETC
, &c
);
2388 send_process (proc
, &c
.t_intrc
, 1);
2391 ioctl (XFASTINT (p
->infd
), TIOCGETC
, &c
);
2392 send_process (proc
, &c
.t_quitc
, 1);
2396 ioctl (XFASTINT (p
->infd
), TIOCGLTC
, &lc
);
2397 send_process (proc
, &lc
.t_suspc
, 1);
2399 #endif /* ! defined (SIGTSTP) */
2402 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
2404 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
2411 ioctl (XFASTINT (p
->infd
), TCGETA
, &t
);
2412 send_process (proc
, &t
.c_cc
[VINTR
], 1);
2415 ioctl (XFASTINT (p
->infd
), TCGETA
, &t
);
2416 send_process (proc
, &t
.c_cc
[VQUIT
], 1);
2420 ioctl (XFASTINT (p
->infd
), TCGETA
, &t
);
2421 send_process (proc
, &t
.c_cc
[VSWTCH
], 1);
2423 #endif /* ! defined (SIGTSTP) */
2425 #else /* ! defined (TCGETA) */
2426 Your configuration files are messed up
.
2427 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
2428 you'd better be using one of the alternatives above! */
2429 #endif /* ! defined (TCGETA) */
2430 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
2431 #endif /* ! defined HAVE_TERMIOS */
2432 #endif /* ! defined (SIGNALS_VIA_CHARACTERS) */
2435 /* Get the pgrp using the tty itself, if we have that.
2436 Otherwise, use the pty to get the pgrp.
2437 On pfa systems, saka@pfu.fujitsu.co.JP writes:
2438 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
2439 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
2440 His patch indicates that if TIOCGPGRP returns an error, then
2441 we should just assume that p->pid is also the process group id. */
2445 if (!NILP (p
->subtty
))
2446 err
= ioctl (XFASTINT (p
->subtty
), TIOCGPGRP
, &gid
);
2448 err
= ioctl (XFASTINT (p
->infd
), TIOCGPGRP
, &gid
);
2452 gid
= - XFASTINT (p
->pid
);
2453 #endif /* ! defined (pfa) */
2459 #else /* ! defined (TIOCGPGRP ) */
2460 /* Can't select pgrps on this system, so we know that
2461 the child itself heads the pgrp. */
2462 gid
= - XFASTINT (p
->pid
);
2463 #endif /* ! defined (TIOCGPGRP ) */
2466 gid
= - XFASTINT (p
->pid
);
2472 p
->raw_status_low
= Qnil
;
2473 p
->raw_status_high
= Qnil
;
2475 XSETINT (p
->tick
, ++process_tick
);
2479 #endif /* ! defined (SIGCONT) */
2482 send_process (proc
, "\003", 1); /* ^C */
2487 send_process (proc
, "\031", 1); /* ^Y */
2492 sys$
forcex (&(XFASTINT (p
->pid
)), 0, 1);
2495 flush_pending_output (XFASTINT (p
->infd
));
2499 /* If we don't have process groups, send the signal to the immediate
2500 subprocess. That isn't really right, but it's better than any
2501 obvious alternative. */
2504 kill (XFASTINT (p
->pid
), signo
);
2508 /* gid may be a pid, or minus a pgrp's number */
2510 if (!NILP (current_group
))
2511 ioctl (XFASTINT (p
->infd
), TIOCSIGSEND
, signo
);
2514 gid
= - XFASTINT (p
->pid
);
2517 #else /* ! defined (TIOCSIGSEND) */
2518 EMACS_KILLPG (-gid
, signo
);
2519 #endif /* ! defined (TIOCSIGSEND) */
2522 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
2523 "Interrupt process PROCESS. May be process or name of one.\n\
2524 PROCESS may be a process, a buffer, or the name of a process or buffer.\n\
2525 Nil or no arg means current buffer's process.\n\
2526 Second arg CURRENT-GROUP non-nil means send signal to\n\
2527 the current process-group of the process's controlling terminal\n\
2528 rather than to the process's own process group.\n\
2529 If the process is a shell, this means interrupt current subjob\n\
2530 rather than the shell.")
2531 (process
, current_group
)
2532 Lisp_Object process
, current_group
;
2534 process_send_signal (process
, SIGINT
, current_group
, 0);
2538 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
2539 "Kill process PROCESS. May be process or name of one.\n\
2540 See function `interrupt-process' for more details on usage.")
2541 (process
, current_group
)
2542 Lisp_Object process
, current_group
;
2544 process_send_signal (process
, SIGKILL
, current_group
, 0);
2548 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
2549 "Send QUIT signal to process PROCESS. May be process or name of one.\n\
2550 See function `interrupt-process' for more details on usage.")
2551 (process
, current_group
)
2552 Lisp_Object process
, current_group
;
2554 process_send_signal (process
, SIGQUIT
, current_group
, 0);
2558 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
2559 "Stop process PROCESS. May be process or name of one.\n\
2560 See function `interrupt-process' for more details on usage.")
2561 (process
, current_group
)
2562 Lisp_Object process
, current_group
;
2565 error ("no SIGTSTP support");
2567 process_send_signal (process
, SIGTSTP
, current_group
, 0);
2572 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
2573 "Continue process PROCESS. May be process or name of one.\n\
2574 See function `interrupt-process' for more details on usage.")
2575 (process
, current_group
)
2576 Lisp_Object process
, current_group
;
2579 process_send_signal (process
, SIGCONT
, current_group
, 0);
2581 error ("no SIGCONT support");
2586 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
2587 2, 2, "nProcess number: \nnSignal code: ",
2588 "Send the process with number PID the signal with code CODE.\n\
2589 Both PID and CODE are integers.")
2591 Lisp_Object pid
, sig
;
2593 CHECK_NUMBER (pid
, 0);
2594 CHECK_NUMBER (sig
, 1);
2595 return make_number (kill (XINT (pid
), XINT (sig
)));
2598 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
2599 "Make PROCESS see end-of-file in its input.\n\
2600 Eof comes after any text already sent to it.\n\
2601 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
2602 nil, indicating the current buffer's process.")
2604 Lisp_Object process
;
2608 proc
= get_process (process
);
2610 /* Make sure the process is really alive. */
2611 if (! NILP (XPROCESS (proc
)->raw_status_low
))
2612 update_status (XPROCESS (proc
));
2613 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
2614 error ("Process %s not running", XSTRING (XPROCESS (proc
)->name
)->data
);
2616 /* Sending a zero-length record is supposed to mean eof
2617 when TIOCREMOTE is turned on. */
2621 write (XFASTINT (XPROCESS (proc
)->outfd
), buf
, 0);
2623 #else /* did not do TOICREMOTE */
2625 send_process (proc
, "\032", 1); /* ^z */
2627 if (!NILP (XPROCESS (proc
)->pty_flag
))
2628 send_process (proc
, "\004", 1);
2631 close (XPROCESS (proc
)->outfd
);
2632 XFASTINT (XPROCESS (proc
)->outfd
) = open (NULL_DEVICE
, O_WRONLY
);
2635 #endif /* did not do TOICREMOTE */
2639 /* Kill all processes associated with `buffer'.
2640 If `buffer' is nil, kill all processes */
2642 kill_buffer_processes (buffer
)
2645 Lisp_Object tail
, proc
;
2647 for (tail
= Vprocess_alist
; XGCTYPE (tail
) == Lisp_Cons
;
2648 tail
= XCONS (tail
)->cdr
)
2650 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
2651 if (XGCTYPE (proc
) == Lisp_Process
2652 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
2654 if (NETCONN_P (proc
))
2655 deactivate_process (proc
);
2656 else if (XFASTINT (XPROCESS (proc
)->infd
))
2657 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
2662 /* On receipt of a signal that a child status has changed,
2663 loop asking about children with changed statuses until
2664 the system says there are no more.
2665 All we do is change the status;
2666 we do not run sentinels or print notifications.
2667 That is saved for the next time keyboard input is done,
2668 in order to avoid timing errors. */
2670 /** WARNING: this can be called during garbage collection.
2671 Therefore, it must not be fooled by the presence of mark bits in
2674 /** USG WARNING: Although it is not obvious from the documentation
2675 in signal(2), on a USG system the SIGCLD handler MUST NOT call
2676 signal() before executing at least one wait(), otherwise the handler
2677 will be called again, resulting in an infinite loop. The relevant
2678 portion of the documentation reads "SIGCLD signals will be queued
2679 and the signal-catching function will be continually reentered until
2680 the queue is empty". Invoking signal() causes the kernel to reexamine
2681 the SIGCLD queue. Fred Fish, UniSoft Systems Inc. */
2684 sigchld_handler (signo
)
2687 int old_errno
= errno
;
2689 register struct Lisp_Process
*p
;
2690 extern EMACS_TIME
*input_available_clear_time
;
2694 sigheld
|= sigbit (SIGCHLD
);
2706 #endif /* no WUNTRACED */
2707 /* Keep trying to get a status until we get a definitive result. */
2711 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
2713 while (pid
<= 0 && errno
== EINTR
);
2717 /* A real failure. We have done all our job, so return. */
2719 /* USG systems forget handlers when they are used;
2720 must reestablish each time */
2722 signal (signo
, sigchld_handler
); /* WARNING - must come after wait3() */
2725 sigheld
&= ~sigbit (SIGCHLD
);
2733 #endif /* no WNOHANG */
2735 /* Find the process that signaled us, and record its status. */
2738 for (tail
= Vprocess_alist
; XSYMBOL (tail
) != XSYMBOL (Qnil
); tail
= XCONS (tail
)->cdr
)
2740 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
2741 p
= XPROCESS (proc
);
2742 if (EQ (p
->childp
, Qt
) && XFASTINT (p
->pid
) == pid
)
2747 /* Look for an asynchronous process whose pid hasn't been filled
2750 for (tail
= Vprocess_alist
; XSYMBOL (tail
) != XSYMBOL (Qnil
); tail
= XCONS (tail
)->cdr
)
2752 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
2753 p
= XPROCESS (proc
);
2754 if (XTYPE (p
->pid
) == Lisp_Int
&& XINT (p
->pid
) == -1)
2759 /* Change the status of the process that was found. */
2762 union { int i
; WAITTYPE wt
; } u
;
2764 XSETINT (p
->tick
, ++process_tick
);
2766 XFASTINT (p
->raw_status_low
) = u
.i
& 0xffff;
2767 XFASTINT (p
->raw_status_high
) = u
.i
>> 16;
2769 /* If process has terminated, stop waiting for its output. */
2770 if (WIFSIGNALED (w
) || WIFEXITED (w
))
2771 if (XFASTINT (p
->infd
))
2772 FD_CLR (XFASTINT (p
->infd
), &input_wait_mask
);
2774 /* Tell wait_reading_process_input that it needs to wake up and
2776 if (input_available_clear_time
)
2777 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
2780 /* There was no asynchronous process found for that id. Check
2781 if we have a synchronous process. */
2784 synch_process_alive
= 0;
2786 /* Report the status of the synchronous process. */
2788 synch_process_retcode
= WRETCODE (w
);
2789 else if (WIFSIGNALED (w
))
2791 synch_process_death
= (char *) sys_siglist
[WTERMSIG (w
)];
2793 synch_process_death
= sys_errlist
[WTERMSIG (w
)];
2796 /* Tell wait_reading_process_input that it needs to wake up and
2798 if (input_available_clear_time
)
2799 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
2802 /* On some systems, we must return right away.
2803 If any more processes want to signal us, we will
2805 Otherwise (on systems that have WNOHANG), loop around
2806 to use up all the processes that have something to tell us. */
2807 #if defined (USG) && ! (defined (HPUX) && defined (WNOHANG))
2809 signal (signo
, sigchld_handler
);
2813 #endif /* USG, but not HPUX with WNOHANG */
2819 exec_sentinel_unwind (data
)
2822 XPROCESS (XCONS (data
)->car
)->sentinel
= XCONS (data
)->cdr
;
2827 exec_sentinel (proc
, reason
)
2828 Lisp_Object proc
, reason
;
2830 Lisp_Object sentinel
;
2831 register struct Lisp_Process
*p
= XPROCESS (proc
);
2832 int count
= specpdl_ptr
- specpdl
;
2834 sentinel
= p
->sentinel
;
2835 if (NILP (sentinel
))
2838 /* Zilch the sentinel while it's running, to avoid recursive invocations;
2839 assure that it gets restored no matter how the sentinel exits. */
2841 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
2842 /* Inhibit quit so that random quits don't screw up a running filter. */
2843 specbind (Qinhibit_quit
, Qt
);
2844 call2 (sentinel
, proc
, reason
);
2848 /* Report all recent events of a change in process status
2849 (either run the sentinel or output a message).
2850 This is done while Emacs is waiting for keyboard input. */
2854 register Lisp_Object proc
, buffer
;
2855 Lisp_Object tail
= Qnil
;
2856 Lisp_Object msg
= Qnil
;
2857 struct gcpro gcpro1
, gcpro2
;
2859 /* We need to gcpro tail; if read_process_output calls a filter
2860 which deletes a process and removes the cons to which tail points
2861 from Vprocess_alist, and then causes a GC, tail is an unprotected
2865 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
2868 register struct Lisp_Process
*p
;
2870 proc
= Fcdr (Fcar (tail
));
2871 p
= XPROCESS (proc
);
2873 if (XINT (p
->tick
) != XINT (p
->update_tick
))
2875 XSETINT (p
->update_tick
, XINT (p
->tick
));
2877 /* If process is still active, read any output that remains. */
2878 if (XFASTINT (p
->infd
))
2879 while (! EQ (p
->filter
, Qt
)
2880 && read_process_output (proc
, XFASTINT (p
->infd
)) > 0);
2884 /* Get the text to use for the message. */
2885 if (!NILP (p
->raw_status_low
))
2887 msg
= status_message (p
->status
);
2889 /* If process is terminated, deactivate it or delete it. */
2891 if (XTYPE (p
->status
) == Lisp_Cons
)
2892 symbol
= XCONS (p
->status
)->car
;
2894 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
2895 || EQ (symbol
, Qclosed
))
2897 if (delete_exited_processes
)
2898 remove_process (proc
);
2900 deactivate_process (proc
);
2903 /* Now output the message suitably. */
2904 if (!NILP (p
->sentinel
))
2905 exec_sentinel (proc
, msg
);
2906 /* Don't bother with a message in the buffer
2907 when a process becomes runnable. */
2908 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
2910 Lisp_Object ro
= XBUFFER (buffer
)->read_only
;
2912 struct buffer
*old
= current_buffer
;
2915 /* Avoid error if buffer is deleted
2916 (probably that's why the process is dead, too) */
2917 if (NILP (XBUFFER (buffer
)->name
))
2919 Fset_buffer (buffer
);
2921 /* Insert new output into buffer
2922 at the current end-of-output marker,
2923 thus preserving logical ordering of input and output. */
2924 if (XMARKER (p
->mark
)->buffer
)
2925 SET_PT (marker_position (p
->mark
));
2928 if (point
<= opoint
)
2929 opoint
+= XSTRING (msg
)->size
+ XSTRING (p
->name
)->size
+ 10;
2931 tem
= current_buffer
->read_only
;
2932 current_buffer
->read_only
= Qnil
;
2933 insert_string ("\nProcess ");
2934 Finsert (1, &p
->name
);
2935 insert_string (" ");
2937 current_buffer
->read_only
= tem
;
2938 Fset_marker (p
->mark
, make_number (point
), p
->buffer
);
2941 set_buffer_internal (old
);
2946 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
2947 redisplay_preserve_echo_area ();
2949 update_tick
= process_tick
;
2960 if (! noninteractive
|| initialized
)
2962 signal (SIGCHLD
, sigchld_handler
);
2965 FD_ZERO (&input_wait_mask
);
2967 keyboard_descriptor
= 0;
2968 FD_SET (keyboard_descriptor
, &input_wait_mask
);
2970 Vprocess_alist
= Qnil
;
2971 for (i
= 0; i
< MAXDESC
; i
++)
2973 chan_process
[i
] = Qnil
;
2974 proc_buffered_char
[i
] = -1;
2978 /* From now on, assume keyboard input comes from descriptor DESC. */
2981 change_keyboard_wait_descriptor (desc
)
2984 FD_CLR (keyboard_descriptor
, &input_wait_mask
);
2985 keyboard_descriptor
= desc
;
2986 FD_SET (keyboard_descriptor
, &input_wait_mask
);
2992 stream_process
= intern ("stream");
2994 Qprocessp
= intern ("processp");
2995 staticpro (&Qprocessp
);
2996 Qrun
= intern ("run");
2998 Qstop
= intern ("stop");
3000 Qsignal
= intern ("signal");
3001 staticpro (&Qsignal
);
3003 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
3006 Qexit = intern ("exit");
3007 staticpro (&Qexit); */
3009 Qopen
= intern ("open");
3011 Qclosed
= intern ("closed");
3012 staticpro (&Qclosed
);
3014 staticpro (&Vprocess_alist
);
3016 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
3017 "*Non-nil means delete processes immediately when they exit.\n\
3018 nil means don't delete them until `list-processes' is run.");
3020 delete_exited_processes
= 1;
3022 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
3023 "Control type of device used to communicate with subprocesses.\n\
3024 Values are nil to use a pipe, and t or 'pty for a pty. Note that if\n\
3025 pty's are not available, this variable will be ignored. The value takes\n\
3026 effect when `start-process' is called.");
3027 Vprocess_connection_type
= Qt
;
3029 defsubr (&Sprocessp
);
3030 defsubr (&Sget_process
);
3031 defsubr (&Sget_buffer_process
);
3032 defsubr (&Sdelete_process
);
3033 defsubr (&Sprocess_status
);
3034 defsubr (&Sprocess_exit_status
);
3035 defsubr (&Sprocess_id
);
3036 defsubr (&Sprocess_name
);
3037 defsubr (&Sprocess_command
);
3038 defsubr (&Sset_process_buffer
);
3039 defsubr (&Sprocess_buffer
);
3040 defsubr (&Sprocess_mark
);
3041 defsubr (&Sset_process_filter
);
3042 defsubr (&Sprocess_filter
);
3043 defsubr (&Sset_process_sentinel
);
3044 defsubr (&Sprocess_sentinel
);
3045 defsubr (&Sprocess_kill_without_query
);
3046 defsubr (&Slist_processes
);
3047 defsubr (&Sprocess_list
);
3048 defsubr (&Sstart_process
);
3050 defsubr (&Sopen_network_stream
);
3051 #endif /* HAVE_SOCKETS */
3052 defsubr (&Saccept_process_output
);
3053 defsubr (&Sprocess_send_region
);
3054 defsubr (&Sprocess_send_string
);
3055 defsubr (&Sinterrupt_process
);
3056 defsubr (&Skill_process
);
3057 defsubr (&Squit_process
);
3058 defsubr (&Sstop_process
);
3059 defsubr (&Scontinue_process
);
3060 defsubr (&Sprocess_send_eof
);
3061 defsubr (&Ssignal_process
);
3062 defsubr (&Swaiting_for_user_input_p
);
3063 /* defsubr (&Sprocess_connection); */
3067 #else /* not subprocesses */
3069 #include <sys/types.h>
3073 #include "systime.h"
3074 #include "termopts.h"
3076 extern int frame_garbaged
;
3079 /* As described above, except assuming that there are no subprocesses:
3081 Wait for timeout to elapse and/or keyboard input to be available.
3084 timeout in seconds, or
3085 zero for no limit, or
3086 -1 means gobble data immediately available but don't wait for any.
3088 read_kbd is a Lisp_Object:
3089 0 to ignore keyboard input, or
3090 1 to return when input is available, or
3091 -1 means caller will actually read the input, so don't throw to
3093 We know that read_kbd will never be a Lisp_Process, since
3094 `subprocesses' isn't defined.
3096 do_display != 0 means redisplay should be done to show subprocess
3097 output that arrives. This version of the function ignores it.
3099 Return true iff we received input from any process. */
3102 wait_reading_process_input (time_limit
, microsecs
, read_kbd
, do_display
)
3103 int time_limit
, microsecs
;
3104 Lisp_Object read_kbd
;
3107 EMACS_TIME end_time
, timeout
, *timeout_p
;
3110 /* What does time_limit really mean? */
3111 if (time_limit
|| microsecs
)
3113 /* It's not infinite. */
3114 timeout_p
= &timeout
;
3116 if (time_limit
== -1)
3117 /* In fact, it's zero. */
3118 EMACS_SET_SECS_USECS (timeout
, 0, 0);
3120 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
3122 /* How far in the future is that? */
3123 EMACS_GET_TIME (end_time
);
3124 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
3127 /* It's infinite. */
3130 /* Turn off periodic alarms (in case they are in use)
3131 because the select emulator uses alarms. */
3138 waitchannels
= XINT (read_kbd
) ? 1 : 0;
3140 /* If calling from keyboard input, do not quit
3141 since we want to return C-g as an input character.
3142 Otherwise, do pending quit if requested. */
3143 if (XINT (read_kbd
) >= 0)
3148 EMACS_GET_TIME (*timeout_p
);
3149 EMACS_SUB_TIME (*timeout_p
, end_time
, *timeout_p
);
3150 if (EMACS_TIME_NEG_P (*timeout_p
))
3154 /* Cause C-g and alarm signals to take immediate action,
3155 and cause input available signals to zero out timeout. */
3156 if (XINT (read_kbd
) < 0)
3157 set_waiting_for_input (&timeout
);
3159 /* If a frame has been newly mapped and needs updating,
3160 reprocess its display stuff. */
3162 redisplay_preserve_echo_area ();
3164 if (XINT (read_kbd
) && detect_input_pending ())
3167 nfds
= select (1, &waitchannels
, 0, 0, timeout_p
);
3169 /* Make C-g and alarm signals set flags again */
3170 clear_waiting_for_input ();
3172 /* If we woke up due to SIGWINCH, actually change size now. */
3173 do_pending_window_change ();
3177 /* If the system call was interrupted, then go around the
3183 else if (nfds
> 0 && (waitchannels
& 1) && interrupt_input
)
3184 /* System sometimes fails to deliver SIGIO. */
3185 kill (getpid (), SIGIO
);
3188 if (XINT (read_kbd
) && interrupt_input
&& (waitchannels
& 1))
3192 /* If we have timed out (nfds == 0) or found some input (nfds > 0),
3204 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
3205 /* Don't confused make-docfile by having two doc strings for this function.
3206 make-docfile does not pay attention to #if, for good reason! */
3209 register Lisp_Object name
;
3214 /* Kill all processes associated with `buffer'.
3215 If `buffer' is nil, kill all processes.
3216 Since we have no subprocesses, this does nothing. */
3218 kill_buffer_processes (buffer
)
3229 defsubr (&Sget_buffer_process
);
3233 #endif /* not subprocesses */