1 /* Asynchronous subprocess control for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1992 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 1, 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 #if defined(BSD) || defined(STRIDE)
50 #include <sys/ioctl.h>
51 #if !defined (O_NDELAY) && defined (HAVE_PTYS)
53 #endif /* HAVE_PTYS and no O_NDELAY */
54 #endif /* BSD or STRIDE */
73 #include <sys/sysmacros.h> /* for "minor" */
78 #if defined (HPUX) && defined (HAVE_PTYS)
79 #include <sys/ptyio.h>
90 #include <sys/ttyhw.h>
91 #include <sys/stream.h>
97 #undef TIOCGETC /* Avoid confusing some conditionals that test this. */
100 #ifdef BROKEN_TIOCGETC
108 #include "termhooks.h"
109 #include "termopts.h"
110 #include "commands.h"
111 #include "dispextern.h"
113 Lisp_Object Qrun
, Qstop
, Qsignal
, Qopen
, Qclosed
;
114 /* Qexit is declared and initialized in eval.c. */
116 /* a process object is a network connection when its childp field is neither
117 Qt nor Qnil but is instead a string (name of foreign host we
118 are connected to + name of port we are connected to) */
121 static Lisp_Object stream_process
;
123 #define NETCONN_P(p) (XGCTYPE (XPROCESS (p)->childp) == Lisp_String)
125 #define NETCONN_P(p) 0
126 #endif /* HAVE_SOCKETS */
128 /* Define first descriptor number available for subprocesses. */
130 #define FIRST_PROC_DESC 1
132 #define FIRST_PROC_DESC 3
135 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
138 #if !defined (SIGCHLD) && defined (SIGCLD)
139 #define SIGCHLD SIGCLD
142 #include "syssignal.h"
144 /* Define the structure that the wait system call stores.
145 On many systems, there is a structure defined for this.
146 But on vanilla-ish USG systems there is not. */
150 #if !defined (BSD) && !defined (UNIPLUS) && !defined (STRIDE) && !(defined (HPUX) && !defined (NOMULTIPLEJOBS)) && !defined (HAVE_WAIT_HEADER)
153 #define WIFSTOPPED(w) ((w&0377) == 0177)
154 #define WIFSIGNALED(w) ((w&0377) != 0177 && (w&~0377) == 0)
155 #define WIFEXITED(w) ((w&0377) == 0)
156 #define WRETCODE(w) (w >> 8)
157 #define WSTOPSIG(w) (w >> 8)
158 #define WCOREDUMP(w) ((w&0200) != 0)
159 #define WTERMSIG(w) (w & 0377)
164 #include <sys/wait.h>
165 #endif /* not BSD 4.1 */
167 #define WAITTYPE union wait
168 #define WRETCODE(w) w.w_retcode
169 #define WCOREDUMP(w) w.w_coredump
172 /* HPUX version 7 has broken definitions of these. */
181 #define WTERMSIG(w) w.w_termsig
184 #define WSTOPSIG(w) w.w_stopsig
187 #define WIFSTOPPED(w) (WTERMSIG (w) == 0177)
190 #define WIFSIGNALED(w) (WTERMSIG (w) != 0177 && (WSTOPSIG (w)) == 0)
193 #define WIFEXITED(w) (WTERMSIG (w) == 0)
195 #endif /* BSD or UNIPLUS or STRIDE */
196 #endif /* no WAITTYPE */
199 /* For the CMU PTY driver + */
200 #define DCL_PROMPT "$ "
210 extern char *sys_errlist
[];
214 extern char *sys_siglist
[];
216 char *sys_siglist
[] =
222 "illegal instruction",
226 "floating point exception",
229 "segmentation violation",
230 "bad argument to system call",
231 "write on a pipe with no one to read it",
233 "software termination signal from kill",
235 "sendable stop signal not from tty",
236 "stop signal from tty",
237 "continue a stopped process",
238 "child status has changed",
239 "background read attempted from control tty",
240 "background write attempted from control tty",
241 "input record available at control tty",
242 "exceeded CPU time limit",
243 "exceeded file size limit"
251 extern int comm_server
;
252 extern int net_listen_address
;
255 /* t means use pty, nil means use a pipe,
256 maybe other values to come. */
257 Lisp_Object Vprocess_connection_type
;
261 #include <sys/socket.h>
265 /* Number of events of change of status of a process. */
268 /* Number of events for which the user or sentinel has been notified. */
272 /* We could get this from param.h, but better not to depend on finding that.
273 And better not to risk that it might define other symbols used in this
276 #define SELECT_TYPE fd_set
277 #else /* no FD_SET */
279 #define SELECT_TYPE int
281 /* Define the macros to access a single-int bitmap of descriptors. */
282 #define FD_SET(n, p) (*(p) |= (1 << (n)))
283 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
284 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
285 #define FD_ZERO(p) (*(p) = 0)
286 #endif /* no FD_SET */
288 /* Mask of bits indicating the descriptors that we wait for input on */
290 SELECT_TYPE input_wait_mask
;
292 int delete_exited_processes
;
294 /* Indexed by descriptor, gives the process (if any) for that descriptor */
295 Lisp_Object chan_process
[MAXDESC
];
297 /* Alist of elements (NAME . PROCESS) */
298 Lisp_Object Vprocess_alist
;
300 Lisp_Object Qprocessp
;
302 Lisp_Object
get_process ();
304 /* Buffered-ahead input char from process, indexed by channel.
305 -1 means empty (no char is buffered).
306 Used on sys V where the only way to tell if there is any
307 output from the process is to read at least one char.
308 Always -1 on systems that support FIONREAD. */
310 int proc_buffered_char
[MAXDESC
];
312 /* Compute the Lisp form of the process status, p->status, from
313 the numeric status that was returned by `wait'. */
316 struct Lisp_Process
*p
;
318 union { int i
; WAITTYPE wt
; } u
;
319 u
.i
= XFASTINT (p
->raw_status_low
) + (XFASTINT (p
->raw_status_high
) << 16);
320 p
->status
= status_convert (u
.wt
);
321 p
->raw_status_low
= Qnil
;
322 p
->raw_status_high
= Qnil
;
325 /* Convert a process status work in Unix format to
326 the list that we use internally. */
333 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
334 else if (WIFEXITED (w
))
335 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
336 WCOREDUMP (w
) ? Qt
: Qnil
));
337 else if (WIFSIGNALED (w
))
338 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
339 WCOREDUMP (w
) ? Qt
: Qnil
));
344 /* Given a status-list, extract the three pieces of information
345 and store them individually through the three pointers. */
348 decode_status (l
, symbol
, code
, coredump
)
356 if (XTYPE (l
) == Lisp_Symbol
)
364 *symbol
= XCONS (l
)->car
;
365 tem
= XCONS (l
)->cdr
;
366 *code
= XFASTINT (XCONS (tem
)->car
);
367 tem
= XFASTINT (XCONS (tem
)->cdr
);
368 *coredump
= !NILP (tem
);
372 /* Return a string describing a process status list. */
375 status_message (status
)
380 Lisp_Object string
, string2
;
382 decode_status (status
, &symbol
, &code
, &coredump
);
384 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
386 string
= build_string (code
< NSIG
? sys_siglist
[code
] : "unknown");
387 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
388 XSTRING (string
)->data
[0] = DOWNCASE (XSTRING (string
)->data
[0]);
389 return concat2 (string
, string2
);
391 else if (EQ (symbol
, Qexit
))
394 return build_string ("finished\n");
395 string
= Fint_to_string (make_number (code
));
396 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
397 return concat2 (build_string ("exited abnormally with code "),
398 concat2 (string
, string2
));
401 return Fcopy_sequence (Fsymbol_name (symbol
));
405 static int pty_process
;
407 /* Open an available pty, returning a file descriptor.
408 Return -1 on failure.
409 The file name of the terminal corresponding to the pty
410 is left in the variable pty_name. */
421 /* Some systems name their pseudoterminals so that there are gaps in
422 the usual sequence - for example, on HP9000/S700 systems, there
423 are no pseudoterminals with names ending in 'f'. So we wait for
424 three failures in a row before deciding that we've reached the
426 int failed_count
= 0;
431 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
432 for (i
= 0; i
< 16; i
++)
435 #ifdef PTY_NAME_SPRINTF
439 sprintf (pty_name
, "/dev/ptym/pty%c%x", c
, i
);
442 sprintf (pty_name
, "/dev/pty%x", i
);
444 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
446 #endif /* not HPUX */
447 #endif /* no PTY_NAME_SPRINTF */
450 /* Unusual IRIS code */
451 *ptyv
= open ("/dev/ptc", O_RDWR
| O_NDELAY
, 0);
454 if (fstat (fd
, &stb
) < 0)
457 if (stat (pty_name
, &stb
) < 0)
460 if (failed_count
>= 3)
466 fd
= open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
468 fd
= open (pty_name
, O_RDWR
| O_NDELAY
, 0);
474 /* check to make certain that both sides are available
475 this avoids a nasty yet stupid bug in rlogins */
476 #ifdef PTY_TTY_NAME_SPRINTF
479 /* TODO: In version 19, make these special cases use the macro above. */
481 sprintf (pty_name
, "/dev/pty/tty%c%x", c
, i
);
484 sprintf (pty_name
, "/dev/ttyp%x", i
);
487 sprintf (pty_name
, "/dev/ttyq%d", minor (stb
.st_rdev
));
489 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
490 #endif /* not IRIS */
492 #endif /* not HPUX */
493 #endif /* no PTY_TTY_NAME_SPRINTF */
495 if (access (pty_name
, 6) != 0)
504 #endif /* not UNIPLUS */
511 #endif /* HAVE_PTYS */
517 register Lisp_Object val
, tem
, name1
;
518 register struct Lisp_Process
*p
;
522 /* size of process structure includes the vector header,
523 so deduct for that. But struct Lisp_Vector includes the first
524 element, thus deducts too much, so add it back. */
525 val
= Fmake_vector (make_number ((sizeof (struct Lisp_Process
)
526 - sizeof (struct Lisp_Vector
)
527 + sizeof (Lisp_Object
))
528 / sizeof (Lisp_Object
)),
530 XSETTYPE (val
, Lisp_Process
);
533 XFASTINT (p
->infd
) = 0;
534 XFASTINT (p
->outfd
) = 0;
535 XFASTINT (p
->pid
) = 0;
536 XFASTINT (p
->tick
) = 0;
537 XFASTINT (p
->update_tick
) = 0;
538 p
->raw_status_low
= Qnil
;
539 p
->raw_status_high
= Qnil
;
541 p
->mark
= Fmake_marker ();
543 /* If name is already in use, modify it until it is unused. */
548 tem
= Fget_process (name1
);
549 if (NILP (tem
)) break;
550 sprintf (suffix
, "<%d>", i
);
551 name1
= concat2 (name
, build_string (suffix
));
555 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
559 remove_process (proc
)
560 register Lisp_Object proc
;
562 register Lisp_Object pair
;
564 pair
= Frassq (proc
, Vprocess_alist
);
565 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
566 Fset_marker (XPROCESS (proc
)->mark
, Qnil
, Qnil
);
568 deactivate_process (proc
);
571 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
572 "Return t if OBJECT is a process.")
576 return XTYPE (obj
) == Lisp_Process
? Qt
: Qnil
;
579 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
580 "Return the process named NAME, or nil if there is none.")
582 register Lisp_Object name
;
584 if (XTYPE (name
) == Lisp_Process
)
586 CHECK_STRING (name
, 0);
587 return Fcdr (Fassoc (name
, Vprocess_alist
));
590 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
591 "Return the (or, a) process associated with BUFFER.\n\
592 BUFFER may be a buffer or the name of one.")
594 register Lisp_Object name
;
596 register Lisp_Object buf
, tail
, proc
;
598 if (NILP (name
)) return Qnil
;
599 buf
= Fget_buffer (name
);
600 if (NILP (buf
)) return Qnil
;
602 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
604 proc
= Fcdr (Fcar (tail
));
605 if (XTYPE (proc
) == Lisp_Process
&& EQ (XPROCESS (proc
)->buffer
, buf
))
611 /* This is how commands for the user decode process arguments */
615 register Lisp_Object name
;
617 register Lisp_Object proc
;
619 proc
= Fget_buffer_process (Fcurrent_buffer ());
622 proc
= Fget_process (name
);
624 proc
= Fget_buffer_process (Fget_buffer (name
));
631 error ("Current buffer has no process");
633 error ("Process %s does not exist", XSTRING (name
)->data
);
637 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
638 "Delete PROCESS: kill it and forget about it immediately.\n\
639 PROCESS may be a process or the name of one, or a buffer name.")
641 register Lisp_Object proc
;
643 proc
= get_process (proc
);
644 XPROCESS (proc
)->raw_status_low
= Qnil
;
645 XPROCESS (proc
)->raw_status_high
= Qnil
;
646 if (NETCONN_P (proc
))
648 XPROCESS (proc
)->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
649 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
651 else if (XFASTINT (XPROCESS (proc
)->infd
))
653 Fkill_process (proc
, Qnil
);
654 /* Do this now, since remove_process will make sigchld_handler do nothing. */
655 XPROCESS (proc
)->status
656 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
657 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
660 remove_process (proc
);
664 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
665 "Return the status of PROCESS: a symbol, one of these:\n\
666 run -- for a process that is running.\n\
667 stop -- for a process stopped but continuable.\n\
668 exit -- for a process that has exited.\n\
669 signal -- for a process that has got a fatal signal.\n\
670 open -- for a network stream connection that is open.\n\
671 closed -- for a network stream connection that is closed.\n\
672 nil -- if arg is a process name and no such process exists.")
673 /* command -- for a command channel opened to Emacs by another process.\n\
674 external -- for an i/o channel opened to Emacs by another process.\n\ */
676 register Lisp_Object proc
;
678 register struct Lisp_Process
*p
;
679 register Lisp_Object status
;
680 proc
= Fget_process (proc
);
684 if (!NILP (p
->raw_status_low
))
687 if (XTYPE (status
) == Lisp_Cons
)
688 status
= XCONS (status
)->car
;
689 if (NETCONN_P (proc
))
691 if (EQ (status
, Qrun
))
693 else if (EQ (status
, Qexit
))
699 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
701 "Return the exit status of PROCESS or the signal number that killed it.\n\
702 If PROCESS has not yet exited or died, return 0.")
704 register Lisp_Object proc
;
706 CHECK_PROCESS (proc
, 0);
707 if (!NILP (XPROCESS (proc
)->raw_status_low
))
708 update_status (XPROCESS (proc
));
709 if (XTYPE (XPROCESS (proc
)->status
) == Lisp_Cons
)
710 return XCONS (XCONS (XPROCESS (proc
)->status
)->cdr
)->car
;
711 return make_number (0);
714 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
715 "Return the process id of PROCESS.\n\
716 This is the pid of the Unix process which PROCESS uses or talks to.\n\
717 For a network connection, this value is nil.")
719 register Lisp_Object proc
;
721 CHECK_PROCESS (proc
, 0);
722 return XPROCESS (proc
)->pid
;
725 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
726 "Return the name of PROCESS, as a string.\n\
727 This is the name of the program invoked in PROCESS,\n\
728 possibly modified to make it unique among process names.")
730 register Lisp_Object proc
;
732 CHECK_PROCESS (proc
, 0);
733 return XPROCESS (proc
)->name
;
736 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
737 "Return the command that was executed to start PROCESS.\n\
738 This is a list of strings, the first string being the program executed\n\
739 and the rest of the strings being the arguments given to it.\n\
740 For a non-child channel, this is nil.")
742 register Lisp_Object proc
;
744 CHECK_PROCESS (proc
, 0);
745 return XPROCESS (proc
)->command
;
748 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
750 "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
752 register Lisp_Object proc
, buffer
;
754 CHECK_PROCESS (proc
, 0);
756 CHECK_BUFFER (buffer
, 1);
757 XPROCESS (proc
)->buffer
= buffer
;
761 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
763 "Return the buffer PROCESS is associated with.\n\
764 Output from PROCESS is inserted in this buffer\n\
765 unless PROCESS has a filter.")
767 register Lisp_Object proc
;
769 CHECK_PROCESS (proc
, 0);
770 return XPROCESS (proc
)->buffer
;
773 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
775 "Return the marker for the end of the last output from PROCESS.")
777 register Lisp_Object proc
;
779 CHECK_PROCESS (proc
, 0);
780 return XPROCESS (proc
)->mark
;
783 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
785 "Give PROCESS the filter function FILTER; nil means no filter.\n\
786 When a process has a filter, each time it does output\n\
787 the entire string of output is passed to the filter.\n\
788 The filter gets two arguments: the process and the string of output.\n\
789 If the process has a filter, its buffer is not used for output.")
791 register Lisp_Object proc
, filter
;
793 CHECK_PROCESS (proc
, 0);
794 XPROCESS (proc
)->filter
= filter
;
798 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
800 "Returns the filter function of PROCESS; nil if none.\n\
801 See `set-process-filter' for more info on filter functions.")
803 register Lisp_Object proc
;
805 CHECK_PROCESS (proc
, 0);
806 return XPROCESS (proc
)->filter
;
809 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
811 "Give PROCESS the sentinel SENTINEL; nil for none.\n\
812 The sentinel is called as a function when the process changes state.\n\
813 It gets two arguments: the process, and a string describing the change.")
815 register Lisp_Object proc
, sentinel
;
817 CHECK_PROCESS (proc
, 0);
818 XPROCESS (proc
)->sentinel
= sentinel
;
822 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
824 "Return the sentinel of PROCESS; nil if none.\n\
825 See `set-process-sentinel' for more info on sentinels.")
827 register Lisp_Object proc
;
829 CHECK_PROCESS (proc
, 0);
830 return XPROCESS (proc
)->sentinel
;
833 DEFUN ("process-kill-without-query", Fprocess_kill_without_query
,
834 Sprocess_kill_without_query
, 1, 2, 0,
835 "Say no query needed if PROCESS is running when Emacs is exited.\n\
836 Optional second argument if non-nill says to require a query.\n\
837 Value is t if a query was formerly required.")
839 register Lisp_Object proc
, value
;
843 CHECK_PROCESS (proc
, 0);
844 tem
= XPROCESS (proc
)->kill_without_query
;
845 XPROCESS (proc
)->kill_without_query
= Fnull (value
);
853 register Lisp_Object tail
, tem
;
854 Lisp_Object proc
, minspace
, tem1
;
855 register struct buffer
*old
= current_buffer
;
856 register struct Lisp_Process
*p
;
860 XFASTINT (minspace
) = 1;
862 set_buffer_internal (XBUFFER (Vstandard_output
));
863 Fbuffer_disable_undo (Vstandard_output
);
865 current_buffer
->truncate_lines
= Qt
;
868 Proc Status Buffer Command\n\
869 ---- ------ ------ -------\n", -1);
871 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
875 proc
= Fcdr (Fcar (tail
));
877 if (NILP (p
->childp
))
880 Finsert (1, &p
->name
);
881 Findent_to (make_number (13), minspace
);
883 if (!NILP (p
->raw_status_low
))
886 if (XTYPE (p
->status
) == Lisp_Cons
)
887 symbol
= XCONS (p
->status
)->car
;
890 if (EQ (symbol
, Qsignal
))
893 tem
= Fcar (Fcdr (p
->status
));
895 if (XINT (tem
) < NSIG
)
896 write_string (sys_siglist
[XINT (tem
)], -1);
899 Fprinc (symbol
, Qnil
);
901 else if (NETCONN_P (proc
))
903 if (EQ (symbol
, Qrun
))
904 write_string ("open", -1);
905 else if (EQ (symbol
, Qexit
))
906 write_string ("closed", -1);
908 Fprinc (symbol
, Qnil
);
911 Fprinc (symbol
, Qnil
);
913 if (EQ (symbol
, Qexit
))
916 tem
= Fcar (Fcdr (p
->status
));
919 sprintf (tembuf
, " %d", XFASTINT (tem
));
920 write_string (tembuf
, -1);
924 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
925 remove_process (proc
);
927 Findent_to (make_number (22), minspace
);
928 if (NILP (p
->buffer
))
929 insert_string ("(none)");
930 else if (NILP (XBUFFER (p
->buffer
)->name
))
931 insert_string ("(Killed)");
933 Finsert (1, &XBUFFER (p
->buffer
)->name
);
935 Findent_to (make_number (37), minspace
);
937 if (NETCONN_P (proc
))
939 sprintf (tembuf
, "(network stream connection to %s)\n",
940 XSTRING (p
->childp
)->data
);
941 insert_string (tembuf
);
955 insert_string ("\n");
961 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 0, "",
962 "Display a list of all processes.\n\
963 \(Any processes listed as Exited or Signaled are actually eliminated\n\
964 after the listing is made.)")
967 internal_with_output_to_temp_buffer ("*Process List*",
968 list_processes_1
, Qnil
);
972 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
973 "Return a list of all processes.")
976 return Fmapcar (Qcdr
, Vprocess_alist
);
979 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
980 "Start a program in a subprocess. Return the process object for it.\n\
981 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
982 NAME is name for process. It is modified if necessary to make it unique.\n\
983 BUFFER is the buffer or (buffer-name) to associate with the process.\n\
984 Process output goes at end of that buffer, unless you specify\n\
985 an output stream or filter function to handle the output.\n\
986 BUFFER may be also nil, meaning that this process is not associated\n\
988 Third arg is program file name. It is searched for as in the shell.\n\
989 Remaining arguments are strings to give program as arguments.")
992 register Lisp_Object
*args
;
994 Lisp_Object buffer
, name
, program
, proc
, tem
;
996 register unsigned char *new_argv
;
999 register unsigned char **new_argv
;
1005 buffer
= Fget_buffer_create (buffer
);
1008 CHECK_STRING (name
, 0);
1012 CHECK_STRING (program
, 2);
1015 /* Make a one member argv with all args concatenated
1016 together separated by a blank. */
1017 len
= XSTRING (program
)->size
+ 2;
1018 for (i
= 3; i
< nargs
; i
++)
1021 CHECK_STRING (tem
, i
);
1022 len
+= XSTRING (tem
)->size
+ 1; /* count the blank */
1024 new_argv
= (unsigned char *) alloca (len
);
1025 strcpy (new_argv
, XSTRING (program
)->data
);
1026 for (i
= 3; i
< nargs
; i
++)
1029 CHECK_STRING (tem
, i
);
1030 strcat (new_argv
, " ");
1031 strcat (new_argv
, XSTRING (tem
)->data
);
1034 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1036 for (i
= 3; i
< nargs
; i
++)
1039 CHECK_STRING (tem
, i
);
1040 new_argv
[i
- 2] = XSTRING (tem
)->data
;
1042 new_argv
[i
- 2] = 0;
1043 new_argv
[0] = XSTRING (program
)->data
;
1045 /* If program file name is not absolute, search our path for it */
1046 if (new_argv
[0][0] != '/')
1049 openp (Vexec_path
, program
, "", &tem
, 1);
1051 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1052 new_argv
[0] = XSTRING (tem
)->data
;
1054 #endif /* not VMS */
1056 proc
= make_process (name
);
1058 XPROCESS (proc
)->childp
= Qt
;
1059 XPROCESS (proc
)->command_channel_p
= Qnil
;
1060 XPROCESS (proc
)->buffer
= buffer
;
1061 XPROCESS (proc
)->sentinel
= Qnil
;
1062 XPROCESS (proc
)->filter
= Qnil
;
1063 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1065 create_process (proc
, new_argv
);
1071 create_process_1 (signo
)
1075 /* USG systems forget handlers when they are used;
1076 must reestablish each time */
1077 signal (signo
, create_process_1
);
1081 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1084 /* Mimic blocking of signals on system V, which doesn't really have it. */
1086 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1087 int sigchld_deferred
;
1090 create_process_sigchld ()
1092 signal (SIGCHLD
, create_process_sigchld
);
1094 sigchld_deferred
= 1;
1100 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1101 create_process (process
, new_argv
)
1102 Lisp_Object process
;
1105 int pid
, inchannel
, outchannel
, forkin
, forkout
;
1108 SIGTYPE (*sigchld
)();
1111 Lisp_Object current_dir
;
1112 extern char **environ
;
1114 inchannel
= outchannel
= -1;
1117 if (EQ (Vprocess_connection_type
, Qt
))
1118 outchannel
= inchannel
= allocate_pty ();
1120 /* Make sure that the child will be able to chdir to the current
1121 buffer's current directory. We can't just have the child check
1122 for an error when it does the chdir, since it's in a vfork. */
1123 current_dir
= expand_and_dir_to_file (current_buffer
->directory
, Qnil
);
1124 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1125 report_file_error ("Setting current directory",
1126 Fcons (current_buffer
->directory
, Qnil
));
1131 /* On USG systems it does not work to open the pty's tty here
1132 and then close and reopen it in the child. */
1134 /* Don't let this terminal become our controlling terminal
1135 (in case we don't have one). */
1136 forkout
= forkin
= open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1138 forkout
= forkin
= open (pty_name
, O_RDWR
, 0);
1141 report_file_error ("Opening pty", Qnil
);
1143 forkin
= forkout
= -1;
1144 #endif /* not USG */
1148 #endif /* HAVE_PTYS */
1151 if (socketpair (AF_UNIX
, SOCK_STREAM
, 0, sv
) < 0)
1152 report_file_error ("Opening socketpair", Qnil
);
1153 outchannel
= inchannel
= sv
[0];
1154 forkout
= forkin
= sv
[1];
1156 #else /* not SKTPAIR */
1165 #endif /* not SKTPAIR */
1168 /* Replaced by close_process_descs */
1169 set_exclusive_use (inchannel
);
1170 set_exclusive_use (outchannel
);
1173 /* Stride people say it's a mystery why this is needed
1174 as well as the O_NDELAY, but that it fails without this. */
1175 #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
1178 ioctl (inchannel
, FIONBIO
, &one
);
1183 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1186 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1190 /* Record this as an active process, with its channels.
1191 As a result, child_setup will close Emacs's side of the pipes. */
1192 chan_process
[inchannel
] = process
;
1193 XFASTINT (XPROCESS (process
)->infd
) = inchannel
;
1194 XFASTINT (XPROCESS (process
)->outfd
) = outchannel
;
1195 /* Record the tty descriptor used in the subprocess. */
1197 XPROCESS (process
)->subtty
= Qnil
;
1199 XFASTINT (XPROCESS (process
)->subtty
) = forkin
;
1200 XPROCESS (process
)->pty_flag
= (pty_flag
? Qt
: Qnil
);
1201 XPROCESS (process
)->status
= Qrun
;
1203 /* Delay interrupts until we have a chance to store
1204 the new fork's pid in its process structure */
1208 #else /* not BSD4_1 */
1209 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1210 sigsetmask (sigmask (SIGCHLD
));
1211 #else /* ordinary USG */
1213 sigchld_deferred
= 0;
1214 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
1216 #endif /* ordinary USG */
1217 #endif /* not BSD4_1 */
1218 #endif /* SIGCHLD */
1220 /* Until we store the proper pid, enable sigchld_handler
1221 to recognize an unknown pid as standing for this process.
1222 It is very important not to let this `marker' value stay
1223 in the table after this function has returned; if it does
1224 it might cause call-process to hang and subsequent asynchronous
1225 processes to get their return values scrambled. */
1226 XSETINT (XPROCESS (process
)->pid
, -1);
1229 /* child_setup must clobber environ on systems with true vfork.
1230 Protect it from permanent change. */
1231 char **save_environ
= environ
;
1236 int xforkin
= forkin
;
1237 int xforkout
= forkout
;
1239 #if 0 /* This was probably a mistake--it duplicates code later on,
1240 but fails to handle all the cases. */
1241 /* Make sure SIGCHLD is not blocked in the child. */
1242 sigsetmask (SIGEMPTYMASK
);
1245 /* Make the pty be the controlling terminal of the process. */
1247 /* First, disconnect its current controlling terminal. */
1250 #else /* not HAVE_SETSID */
1252 /* It's very important to call setpgrp() here and no time
1253 afterwards. Otherwise, we lose our controlling tty which
1254 is set when we open the pty. */
1257 #endif /* not HAVE_SETSID */
1259 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1260 can do TIOCSPGRP only to the process's controlling tty. */
1263 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1264 I can't test it since I don't have 4.3. */
1265 int j
= open ("/dev/tty", O_RDWR
, 0);
1266 ioctl (j
, TIOCNOTTY
, 0);
1269 /* In order to get a controlling terminal on some versions
1270 of BSD, it is necessary to put the process in pgrp 0
1271 before it opens the terminal. */
1275 #endif /* TIOCNOTTY */
1277 #if !defined (RTU) && !defined (UNIPLUS)
1278 /*** There is a suggestion that this ought to be a
1279 conditional on TIOCSPGRP. */
1280 /* Now close the pty (if we had it open) and reopen it.
1281 This makes the pty the controlling terminal of the subprocess. */
1284 /* I wonder if close (open (pty_name, ...)) would work? */
1287 xforkout
= xforkin
= open (pty_name
, O_RDWR
, 0);
1292 #endif /* not UNIPLUS and not RTU */
1293 #ifdef SETUP_SLAVE_PTY
1295 #endif /* SETUP_SLAVE_PTY */
1297 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
1298 Now reenable it in the child, so it will die when we want it to. */
1300 signal (SIGHUP
, SIG_DFL
);
1302 #endif /* HAVE_PTYS */
1307 #else /* not BSD4_1 */
1308 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1309 sigsetmask (SIGEMPTYMASK
);
1310 #else /* ordinary USG */
1311 signal (SIGCHLD
, sigchld
);
1312 #endif /* ordinary USG */
1313 #endif /* not BSD4_1 */
1314 #endif /* SIGCHLD */
1316 child_setup_tty (xforkout
);
1317 child_setup (xforkin
, xforkout
, xforkout
,
1318 new_argv
, 1, current_dir
);
1320 environ
= save_environ
;
1325 remove_process (process
);
1326 report_file_error ("Doing vfork", Qnil
);
1329 XFASTINT (XPROCESS (process
)->pid
) = pid
;
1331 FD_SET (inchannel
, &input_wait_mask
);
1333 /* If the subfork execv fails, and it exits,
1334 this close hangs. I don't know why.
1335 So have an interrupt jar it loose. */
1337 signal (SIGALRM
, create_process_1
);
1340 /* OK to close only if it's not a pty. Otherwise we need to leave
1341 it open for ioctl to get pgrp when signals are sent, or to send
1342 the interrupt characters through if that's how we're signalling
1343 subprocesses. Alternately if you are concerned about running out
1344 of file descriptors, you could just save the tty name and open
1345 just to do the ioctl. */
1346 if (NILP (XFASTINT (XPROCESS (process
)->pty_flag
)))
1349 XPROCESS (process
)->subtty
= Qnil
;
1355 if (forkin
!= forkout
&& forkout
>= 0)
1361 #else /* not BSD4_1 */
1362 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1363 sigsetmask (SIGEMPTYMASK
);
1364 #else /* ordinary USG */
1366 signal (SIGCHLD
, sigchld
);
1367 /* Now really handle any of these signals
1368 that came in during this function. */
1369 if (sigchld_deferred
)
1370 kill (getpid (), SIGCHLD
);
1372 #endif /* ordinary USG */
1373 #endif /* not BSD4_1 */
1374 #endif /* SIGCHLD */
1376 #endif /* not VMS */
1380 /* open a TCP network connection to a given HOST/SERVICE. Treated
1381 exactly like a normal process when reading and writing. Only
1382 differences are in status display and process deletion. A network
1383 connection has no PID; you cannot signal it. All you can do is
1384 deactivate and close it via delete-process */
1386 DEFUN ("open-network-stream", Fopen_network_stream
, Sopen_network_stream
,
1388 "Open a TCP connection for a service to a host.\n\
1389 Returns a subprocess-object to represent the connection.\n\
1390 Input and output work as for subprocesses; `delete-process' closes it.\n\
1391 Args are NAME BUFFER HOST SERVICE.\n\
1392 NAME is name for process. It is modified if necessary to make it unique.\n\
1393 BUFFER is the buffer (or buffer-name) to associate with the process.\n\
1394 Process output goes at end of that buffer, unless you specify\n\
1395 an output stream or filter function to handle the output.\n\
1396 BUFFER may be also nil, meaning that this process is not associated\n\
1398 Third arg is name of the host to connect to, or its IP address.\n\
1399 Fourth arg SERVICE is name of the service desired, or an integer\n\
1400 specifying a port number to connect to.")
1401 (name
, buffer
, host
, service
)
1402 Lisp_Object name
, buffer
, host
, service
;
1406 struct sockaddr_in address
;
1407 struct servent
*svc_info
;
1408 struct hostent
*host_info_ptr
, host_info
;
1409 char *(addr_list
[2]);
1410 unsigned long numeric_addr
;
1414 struct hostent host_info_fixed
;
1415 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1417 GCPRO4 (name
, buffer
, host
, service
);
1418 CHECK_STRING (name
, 0);
1419 CHECK_STRING (host
, 0);
1420 if (XTYPE(service
) == Lisp_Int
)
1421 port
= htons ((unsigned short) XINT (service
));
1424 CHECK_STRING (service
, 0);
1425 svc_info
= getservbyname (XSTRING (service
)->data
, "tcp");
1427 error ("Unknown service \"%s\"", XSTRING (service
)->data
);
1428 port
= svc_info
->s_port
;
1431 host_info_ptr
= gethostbyname (XSTRING (host
)->data
);
1432 if (host_info_ptr
== 0)
1433 /* Attempt to interpret host as numeric inet address */
1435 numeric_addr
= inet_addr (XSTRING (host
)->data
);
1436 if (numeric_addr
== -1)
1437 error ("Unknown host \"%s\"", XSTRING (host
)->data
);
1439 host_info_ptr
= &host_info
;
1440 host_info
.h_name
= 0;
1441 host_info
.h_aliases
= 0;
1442 host_info
.h_addrtype
= AF_INET
;
1443 host_info
.h_addr_list
= &(addr_list
[0]);
1444 addr_list
[0] = (char*)(&numeric_addr
);
1446 host_info
.h_length
= strlen (addr_list
[0]);
1449 bzero (&address
, sizeof address
);
1450 bcopy (host_info_ptr
->h_addr
, (char *) &address
.sin_addr
,
1451 host_info_ptr
->h_length
);
1452 address
.sin_family
= host_info_ptr
->h_addrtype
;
1453 address
.sin_port
= port
;
1455 s
= socket (host_info_ptr
->h_addrtype
, SOCK_STREAM
, 0);
1457 report_file_error ("error creating socket", Fcons (name
, Qnil
));
1460 if (connect (s
, &address
, sizeof address
) == -1)
1467 report_file_error ("connection failed",
1468 Fcons (host
, Fcons (name
, Qnil
)));
1474 report_file_error ("error duplicating socket", Fcons (name
, Qnil
));
1477 buffer
= Fget_buffer_create (buffer
);
1478 proc
= make_process (name
);
1480 chan_process
[inch
] = proc
;
1483 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
1486 fcntl (inch
, F_SETFL
, O_NDELAY
);
1490 XPROCESS (proc
)->childp
= host
;
1491 XPROCESS (proc
)->command_channel_p
= Qnil
;
1492 XPROCESS (proc
)->buffer
= buffer
;
1493 XPROCESS (proc
)->sentinel
= Qnil
;
1494 XPROCESS (proc
)->filter
= Qnil
;
1495 XPROCESS (proc
)->command
= Qnil
;
1496 XPROCESS (proc
)->pid
= Qnil
;
1497 XFASTINT (XPROCESS (proc
)->infd
) = s
;
1498 XFASTINT (XPROCESS (proc
)->outfd
) = outch
;
1499 XPROCESS (proc
)->status
= Qrun
;
1500 FD_SET (inch
, &input_wait_mask
);
1505 #endif /* HAVE_SOCKETS */
1507 deactivate_process (proc
)
1510 register int inchannel
, outchannel
;
1511 register struct Lisp_Process
*p
= XPROCESS (proc
);
1513 inchannel
= XFASTINT (p
->infd
);
1514 outchannel
= XFASTINT (p
->outfd
);
1518 /* Beware SIGCHLD hereabouts. */
1519 flush_pending_output (inchannel
);
1522 VMS_PROC_STUFF
*get_vms_process_pointer (), *vs
;
1523 sys$
dassgn (outchannel
);
1524 vs
= get_vms_process_pointer (p
->pid
)
1526 give_back_vms_process_stuff (vs
);
1530 if (outchannel
&& outchannel
!= inchannel
)
1534 XFASTINT (p
->infd
) = 0;
1535 XFASTINT (p
->outfd
) = 0;
1536 chan_process
[inchannel
] = Qnil
;
1537 FD_CLR (inchannel
, &input_wait_mask
);
1541 /* Close all descriptors currently in use for communication
1542 with subprocess. This is used in a newly-forked subprocess
1543 to get rid of irrelevant descriptors. */
1545 close_process_descs ()
1548 for (i
= 0; i
< MAXDESC
; i
++)
1550 Lisp_Object process
;
1551 process
= chan_process
[i
];
1552 if (!NILP (process
))
1554 int in
= XFASTINT (XPROCESS (process
)->infd
);
1555 int out
= XFASTINT (XPROCESS (process
)->outfd
);
1558 if (out
&& in
!= out
)
1564 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
1566 "Allow any pending output from subprocesses to be read by Emacs.\n\
1567 It is read into the process' buffers or given to their filter functions.\n\
1568 Non-nil arg PROCESS means do not return until some output has been received\n\
1570 Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of\n\
1571 seconds and microseconds to wait; return after that much time whether\n\
1572 or not there is input.\n\
1573 Return non-nil iff we received any output before the timeout expired.")
1574 (proc
, timeout
, timeout_msecs
)
1575 register Lisp_Object proc
, timeout
, timeout_msecs
;
1580 if (! NILP (timeout_msecs
))
1582 CHECK_NUMBER (timeout_msecs
, 2);
1583 useconds
= XINT (timeout_msecs
);
1584 if (XTYPE (timeout
) != Lisp_Int
)
1585 XSET (timeout
, Lisp_Int
, 0);
1588 int carry
= useconds
/ 1000000;
1590 XSETINT (timeout
, XINT (timeout
) + carry
);
1591 useconds
-= carry
* 1000000;
1593 /* I think this clause is necessary because C doesn't
1594 guarantee a particular rounding direction for negative
1598 XSETINT (timeout
, XINT (timeout
) - 1);
1599 useconds
+= 1000000;
1604 if (! NILP (timeout
))
1606 CHECK_NUMBER (timeout
, 1);
1607 seconds
= XINT (timeout
);
1620 (wait_reading_process_input (seconds
, useconds
,
1622 ? XPROCESS (get_process (proc
)) : 0), 0)
1626 /* This variable is different from waiting_for_input in keyboard.c.
1627 It is used to communicate to a lisp process-filter/sentinel (via the
1628 function Fwaiting_for_user_input_p below) whether emacs was waiting
1629 for user-input when that process-filter was called.
1630 waiting_for_input cannot be used as that is by definition 0 when
1631 lisp code is being evalled */
1632 static int waiting_for_user_input_p
;
1634 /* Read and dispose of subprocess output while waiting for timeout to
1635 elapse and/or keyboard input to be available.
1638 timeout in seconds, or
1639 zero for no limit, or
1640 -1 means gobble data immediately available but don't wait for any.
1643 0 to ignore keyboard input, or
1644 1 to return when input is available, or
1645 -1 means caller will actually read the input, so don't throw to
1646 the quit handler, or
1647 a pointer to a struct Lisp_Process, meaning wait until something
1648 arrives from that process. The return value is true iff we read
1649 some input from that process.
1651 do_display != 0 means redisplay should be done to show subprocess
1652 output that arrives.
1654 If read_kbd is a pointer to a struct Lisp_Process, then the
1655 function returns true iff we received input from that process
1656 before the timeout elapsed.
1657 Otherwise, return true iff we recieved input from any process. */
1659 wait_reading_process_input (time_limit
, microsecs
, read_kbd
, do_display
)
1660 int time_limit
, microsecs
, read_kbd
, do_display
;
1662 register int channel
, nfds
, m
;
1663 static SELECT_TYPE Available
;
1666 EMACS_TIME timeout
, end_time
, garbage
;
1668 int wait_channel
= 0;
1669 struct Lisp_Process
*wait_proc
= 0;
1670 int got_some_input
= 0;
1672 FD_ZERO (&Available
);
1674 /* Detect when read_kbd is really the address of a Lisp_Process. */
1675 if (read_kbd
> 10 || read_kbd
< -1)
1677 wait_proc
= (struct Lisp_Process
*) read_kbd
;
1678 wait_channel
= XFASTINT (wait_proc
->infd
);
1682 waiting_for_user_input_p
= read_kbd
;
1684 /* Since we may need to wait several times,
1685 compute the absolute time to return at. */
1686 if (time_limit
|| microsecs
)
1688 EMACS_GET_TIME (end_time
);
1689 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
1690 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
1693 /* Turn off periodic alarms (in case they are in use)
1694 because the select emulator uses alarms. */
1699 /* If calling from keyboard input, do not quit
1700 since we want to return C-g as an input character.
1701 Otherwise, do pending quit if requested. */
1705 /* If status of something has changed, and no input is available,
1706 notify the user of the change right away */
1707 if (update_tick
!= process_tick
&& do_display
)
1709 Atemp
= input_wait_mask
;
1710 EMACS_SET_SECS_USECS (timeout
, 0, 0);
1711 if (select (MAXDESC
, &Atemp
, 0, 0, &timeout
) <= 0)
1715 /* Don't wait for output from a non-running process. */
1716 if (wait_proc
!= 0 && !NILP (wait_proc
->raw_status_low
))
1717 update_status (wait_proc
);
1719 && ! EQ (wait_proc
->status
, Qrun
))
1722 /* Compute time from now till when time limit is up */
1723 /* Exit if already run out */
1724 if (time_limit
== -1)
1726 /* -1 specified for timeout means
1727 gobble output available now
1728 but don't wait at all. */
1730 EMACS_SET_SECS_USECS (timeout
, 0, 0);
1732 else if (time_limit
|| microsecs
)
1734 EMACS_GET_TIME (timeout
);
1735 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
1736 if (EMACS_TIME_NEG_P (timeout
))
1741 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
1744 /* Cause C-g and alarm signals to take immediate action,
1745 and cause input available signals to zero out timeout */
1747 set_waiting_for_input (&timeout
);
1749 /* Wait till there is something to do */
1751 Available
= input_wait_mask
;
1753 FD_CLR (0, &Available
);
1755 if (read_kbd
&& detect_input_pending ())
1758 nfds
= select (MAXDESC
, &Available
, 0, 0, &timeout
);
1762 /* Make C-g and alarm signals set flags again */
1763 clear_waiting_for_input ();
1765 /* If we woke up due to SIGWINCH, actually change size now. */
1766 do_pending_window_change ();
1768 if (time_limit
&& nfds
== 0) /* timeout elapsed */
1772 if (xerrno
== EINTR
)
1773 FD_ZERO (&Available
);
1775 /* This happens for no known reason on ALLIANT.
1776 I am guessing that this is the right response. -- RMS. */
1777 else if (xerrno
== EFAULT
)
1778 FD_ZERO (&Available
);
1780 else if (xerrno
== EBADF
)
1783 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
1784 the child's closure of the pts gives the parent a SIGHUP, and
1785 the ptc file descriptor is automatically closed,
1786 yielding EBADF here or at select() call above.
1787 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
1788 in m-ibmrt-aix.h), and here we just ignore the select error.
1789 Cleanup occurs c/o status_notify after SIGCLD. */
1790 FD_ZERO (&Available
); /* Cannot depend on values returned */
1796 error("select error: %s", sys_errlist
[xerrno
]);
1799 else if (nfds
> 0 && FD_ISSET (0, &Available
) && interrupt_input
)
1800 /* System sometimes fails to deliver SIGIO. */
1801 kill (getpid (), SIGIO
);
1804 /* Check for keyboard input */
1805 /* If there is any, return immediately
1806 to give it higher priority than subprocesses */
1808 if (read_kbd
&& detect_input_pending ())
1812 /* If we think we have keyboard input waiting, but didn't get SIGIO
1813 go read it. This can happen with X on BSD after logging out.
1814 In that case, there really is no input and no SIGIO,
1815 but select says there is input. */
1818 if (read_kbd && interrupt_input && (Available & fileno (stdin)))
1820 if (read_kbd
&& interrupt_input
&& (FD_ISSET (fileno (stdin
), &Available
)))
1825 /* Check for connection from other process */
1827 if (Available
& ChannelMask (comm_server
))
1829 Available
&= ~(ChannelMask (comm_server
));
1835 got_some_input
|= nfds
> 0;
1837 /* If checking input just got us a size-change event from X,
1838 obey it now if we should. */
1840 do_pending_window_change ();
1842 /* If screen size has changed, redisplay now
1843 for either sit-for or keyboard input. */
1844 if (read_kbd
&& screen_garbaged
)
1845 redisplay_preserve_echo_area ();
1847 /* Check for data from a process or a command channel */
1848 for (channel
= FIRST_PROC_DESC
; channel
< MAXDESC
; channel
++)
1850 if (FD_ISSET (channel
, &Available
))
1854 /* If waiting for this channel, arrange to return as
1855 soon as no more input to be processed. No more
1857 if (wait_channel
== channel
)
1863 proc
= chan_process
[channel
];
1868 /* It's a command channel */
1869 if (!NILP (XPROCESS (proc
)->command_channel_p
))
1871 ProcessCommChan (channel
, proc
);
1872 if (NILP (XPROCESS (proc
)->command_channel_p
))
1874 /* It has ceased to be a command channel! */
1875 int bytes_available
;
1876 if (ioctl (channel
, FIONREAD
, &bytes_available
) < 0)
1877 bytes_available
= 0;
1878 if (bytes_available
)
1879 FD_SET (channel
, &Available
);
1885 /* Read data from the process, starting with our
1886 buffered-ahead character if we have one. */
1888 nread
= read_process_output (proc
, channel
);
1891 /* Since read_process_output can run a filter,
1892 which can call accept-process-output,
1893 don't try to read from any other processes
1894 before doing the select again. */
1895 FD_ZERO (&Available
);
1898 redisplay_preserve_echo_area ();
1901 else if (nread
== -1 && errno
== EWOULDBLOCK
)
1905 else if (nread
== -1 && errno
== EAGAIN
)
1909 else if (nread
== -1 && errno
== EAGAIN
)
1911 /* Note that we cannot distinguish between no input
1912 available now and a closed pipe.
1913 With luck, a closed pipe will be accompanied by
1914 subprocess termination and SIGCHLD. */
1915 else if (nread
== 0 && !NETCONN_P (proc
))
1917 #endif /* O_NDELAY */
1918 #endif /* O_NONBLOCK */
1919 #endif /* EWOULDBLOCK */
1921 /* On some OSs with ptys, when the process on one end of
1922 a pty exits, the other end gets an error reading with
1923 errno = EIO instead of getting an EOF (0 bytes read).
1924 Therefore, if we get an error reading and errno =
1925 EIO, just continue, because the child process has
1926 exited and should clean itself up soon (e.g. when we
1928 else if (nread
== -1 && errno
== EIO
)
1930 #endif /* HAVE_PTYS */
1931 /* If we can detect process termination, don't consider the process
1932 gone just because its pipe is closed. */
1934 else if (nread
== 0 && !NETCONN_P (proc
))
1939 /* Preserve status of processes already terminated. */
1940 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
1941 deactivate_process (proc
);
1942 if (!NILP (XPROCESS (proc
)->raw_status_low
))
1943 update_status (XPROCESS (proc
));
1944 if (EQ (XPROCESS (proc
)->status
, Qrun
))
1945 XPROCESS (proc
)->status
1946 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
1949 } /* end for each file descriptor */
1950 } /* end while exit conditions not met */
1952 /* Resume periodic signals to poll for input, if necessary. */
1955 return got_some_input
;
1958 /* Read pending output from the process channel,
1959 starting with our buffered-ahead character if we have one.
1960 Yield number of characters read.
1962 This function reads at most 1024 characters.
1963 If you want to read all available subprocess output,
1964 you must call it repeatedly until it returns zero. */
1966 read_process_output (proc
, channel
)
1968 register int channel
;
1970 register int nchars
;
1976 register Lisp_Object outstream
;
1977 register struct buffer
*old
= current_buffer
;
1978 register struct Lisp_Process
*p
= XPROCESS (proc
);
1979 register int opoint
;
1982 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
1984 vs
= get_vms_process_pointer (p
->pid
);
1988 return(0); /* Really weird if it does this */
1989 if (!(vs
->iosb
[0] & 1))
1990 return -1; /* I/O error */
1993 error ("Could not get VMS process pointer");
1994 chars
= vs
->inputBuffer
;
1995 nchars
= clean_vms_buffer (chars
, vs
->iosb
[1]);
1998 start_vms_process_read (vs
); /* Crank up the next read on the process */
1999 return 1; /* Nothing worth printing, say we got 1 */
2003 if (proc_buffered_char
[channel
] < 0)
2004 nchars
= read (channel
, chars
, sizeof chars
);
2007 chars
[0] = proc_buffered_char
[channel
];
2008 proc_buffered_char
[channel
] = -1;
2009 nchars
= read (channel
, chars
+ 1, sizeof chars
- 1);
2013 nchars
= nchars
+ 1;
2015 #endif /* not VMS */
2017 if (nchars
<= 0) return nchars
;
2019 outstream
= p
->filter
;
2020 if (!NILP (outstream
))
2022 /* We inhibit quit here instead of just catching it so that
2023 hitting ^G when a filter happens to be running won't screw
2025 int count
= specpdl_ptr
- specpdl
;
2026 specbind (Qinhibit_quit
, Qt
);
2027 call2 (outstream
, proc
, make_string (chars
, nchars
));
2030 start_vms_process_read (vs
);
2036 /* If no filter, write into buffer if it isn't dead. */
2037 if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
2041 Fset_buffer (p
->buffer
);
2044 /* Insert new output into buffer
2045 at the current end-of-output marker,
2046 thus preserving logical ordering of input and output. */
2047 if (XMARKER (p
->mark
)->buffer
)
2048 SET_PT (marker_position (p
->mark
));
2051 if (point
<= opoint
)
2054 tem
= current_buffer
->read_only
;
2055 current_buffer
->read_only
= Qnil
;
2056 /* Insert before markers in case we are inserting where
2057 the buffer's mark is, and the user's next command is Meta-y. */
2058 insert_before_markers (chars
, nchars
);
2059 current_buffer
->read_only
= tem
;
2060 Fset_marker (p
->mark
, make_number (point
), p
->buffer
);
2061 update_mode_lines
++;
2064 set_buffer_internal (old
);
2067 start_vms_process_read (vs
);
2072 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
2074 "Returns non-NIL if emacs is waiting for input from the user.\n\
2075 This is intended for use by asynchronous process output filters and sentinels.")
2078 return ((waiting_for_user_input_p
) ? Qt
: Qnil
);
2081 /* Sending data to subprocess */
2083 jmp_buf send_process_frame
;
2086 send_process_trap ()
2092 longjmp (send_process_frame
, 1);
2095 send_process (proc
, buf
, len
)
2100 /* Don't use register vars; longjmp can lose them. */
2102 unsigned char *procname
= XSTRING (XPROCESS (proc
)->name
)->data
;
2106 struct Lisp_Process
*p
= XPROCESS (proc
);
2107 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
2110 if (! NILP (XPROCESS (proc
)->raw_status_low
))
2111 update_status (XPROCESS (proc
));
2112 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
2113 error ("Process %s not running", procname
);
2116 vs
= get_vms_process_pointer (p
->pid
);
2118 error ("Could not find this process: %x", p
->pid
);
2119 else if (write_to_vms_process (vs
, buf
, len
))
2122 if (!setjmp (send_process_frame
))
2126 SIGTYPE (*old_sigpipe
)();
2128 /* Don't send more than 500 bytes at a time. */
2131 old_sigpipe
= signal (SIGPIPE
, send_process_trap
);
2132 rv
= write (XFASTINT (XPROCESS (proc
)->outfd
), buf
, this);
2133 signal (SIGPIPE
, old_sigpipe
);
2138 || errno
== EWOULDBLOCK
2145 /* It would be nice to accept process output here,
2146 but that is difficult. For example, it could
2147 garbage what we are sending if that is from a buffer. */
2154 report_file_error ("writing to process", Fcons (proc
, Qnil
));
2158 /* Allow input from processes between bursts of sending.
2159 Otherwise things may get stopped up. */
2161 wait_reading_process_input (-1, 0, 0, 0);
2166 XPROCESS (proc
)->raw_status_low
= Qnil
;
2167 XPROCESS (proc
)->raw_status_high
= Qnil
;
2168 XPROCESS (proc
)->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
2169 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
2170 deactivate_process (proc
);
2172 error ("Error writing to process %s; closed it", procname
);
2174 error ("SIGPIPE raised on process %s; closed it", procname
);
2179 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
2181 "Send current contents of region as input to PROCESS.\n\
2182 PROCESS may be a process name or an actual process.\n\
2183 Called from program, takes three arguments, PROCESS, START and END.\n\
2184 If the region is more than 500 characters long,\n\
2185 it is sent in several bunches. This may happen even for shorter regions.\n\
2186 Output from processes can arrive in between bunches.")
2187 (process
, start
, end
)
2188 Lisp_Object process
, start
, end
;
2193 proc
= get_process (process
);
2194 validate_region (&start
, &end
);
2196 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
2199 start1
= XINT (start
);
2200 send_process (proc
, &FETCH_CHAR (start1
), XINT (end
) - XINT (start
));
2205 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
2207 "Send PROCESS the contents of STRING as input.\n\
2208 PROCESS may be a process name or an actual process.\n\
2209 If STRING is more than 500 characters long,\n\
2210 it is sent in several bunches. This may happen even for shorter strings.\n\
2211 Output from processes can arrive in between bunches.")
2213 Lisp_Object process
, string
;
2216 CHECK_STRING (string
, 1);
2217 proc
= get_process (process
);
2218 send_process (proc
, XSTRING (string
)->data
, XSTRING (string
)->size
);
2222 /* send a signal number SIGNO to PROCESS.
2223 CURRENT_GROUP means send to the process group that currently owns
2224 the terminal being used to communicate with PROCESS.
2225 This is used for various commands in shell mode.
2226 If NOMSG is zero, insert signal-announcements into process's buffers
2229 process_send_signal (process
, signo
, current_group
, nomsg
)
2230 Lisp_Object process
;
2232 Lisp_Object current_group
;
2236 register struct Lisp_Process
*p
;
2240 proc
= get_process (process
);
2241 p
= XPROCESS (proc
);
2243 if (!EQ (p
->childp
, Qt
))
2244 error ("Process %s is not a subprocess",
2245 XSTRING (p
->name
)->data
);
2246 if (!XFASTINT (p
->infd
))
2247 error ("Process %s is not active",
2248 XSTRING (p
->name
)->data
);
2250 if (NILP (p
->pty_flag
))
2251 current_group
= Qnil
;
2253 #ifdef TIOCGPGRP /* Not sure about this! (fnf) */
2254 /* If we are using pgrps, get a pgrp number and make it negative. */
2255 if (!NILP (current_group
))
2257 /* If possible, send signals to the entire pgrp
2258 by sending an input character to it. */
2259 #if defined (TIOCGLTC) && defined (TIOCGETC)
2266 ioctl (XFASTINT (p
->infd
), TIOCGETC
, &c
);
2267 send_process (proc
, &c
.t_intrc
, 1);
2270 ioctl (XFASTINT (p
->infd
), TIOCGETC
, &c
);
2271 send_process (proc
, &c
.t_quitc
, 1);
2274 ioctl (XFASTINT (p
->infd
), TIOCGLTC
, &lc
);
2275 send_process (proc
, &lc
.t_suspc
, 1);
2278 #endif /* have TIOCGLTC and have TIOCGETC */
2279 /* It is possible that the following code would work
2280 on other kinds of USG systems, not just on the IRIS.
2281 This should be tried in Emacs 19. */
2282 #if defined (IRIS) && defined (HAVE_SETSID) /* Check for Irix, not older
2288 ioctl (XFASTINT (p
->infd
), TCGETA
, &t
);
2289 send_process (proc
, &t
.c_cc
[VINTR
], 1);
2292 ioctl (XFASTINT (p
->infd
), TCGETA
, &t
);
2293 send_process (proc
, &t
.c_cc
[VQUIT
], 1);
2296 ioctl (XFASTINT (p
->infd
), TCGETA
, &t
);
2297 send_process (proc
, &t
.c_cc
[VSWTCH
], 1);
2300 #endif /* IRIS and HAVE_SETSID */
2302 /* Get the pgrp using the tty itself, if we have that.
2303 Otherwise, use the pty to get the pgrp.
2304 On pfa systems, saka@pfu.fujitsu.co.JP writes:
2305 "TICGPGRP symbol defined in sys/ioctl.h at E50.
2306 But, TIOCGPGRP donot work on E50 ;-P work fine on E60"
2307 His patch indicates that if TIOCGPGRP returns an error, then
2308 we should just assume that p->pid is also the process group id. */
2312 if (!NILP (p
->subtty
))
2313 err
= ioctl (XFASTINT (p
->subtty
), TIOCGPGRP
, &gid
);
2315 err
= ioctl (XFASTINT (p
->infd
), TIOCGPGRP
, &gid
);
2319 gid
= - XFASTINT (p
->pid
);
2328 gid
= - XFASTINT (p
->pid
);
2329 #else /* not using pgrps */
2330 /* Can't select pgrps on this system, so we know that
2331 the child itself heads the pgrp. */
2332 gid
= - XFASTINT (p
->pid
);
2333 #endif /* not using pgrps */
2339 p
->raw_status_low
= Qnil
;
2340 p
->raw_status_high
= Qnil
;
2342 XSETINT (p
->tick
, ++process_tick
);
2349 send_process (proc
, "\003", 1); /* ^C */
2354 send_process (proc
, "\031", 1); /* ^Y */
2359 sys$
forcex (&(XFASTINT (p
->pid
)), 0, 1);
2362 flush_pending_output (XFASTINT (p
->infd
));
2366 /* If we don't have process groups, send the signal to the immediate
2367 subprocess. That isn't really right, but it's better than any
2368 obvious alternative. */
2371 kill (XFASTINT (p
->pid
), signo
);
2375 /* gid may be a pid, or minus a pgrp's number */
2377 if (!NILP (current_group
))
2378 ioctl (XFASTINT (p
->infd
), TIOCSIGSEND
, signo
);
2381 gid
= - XFASTINT (p
->pid
);
2384 #else /* no TIOCSIGSEND */
2385 EMACS_KILLPG (-gid
, signo
);
2389 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
2390 "Interrupt process PROCESS. May be process or name of one.\n\
2391 Nil or no arg means current buffer's process.\n\
2392 Second arg CURRENT-GROUP non-nil means send signal to\n\
2393 the current process-group of the process's controlling terminal\n\
2394 rather than to the process's own process group.\n\
2395 If the process is a shell, this means interrupt current subjob\n\
2396 rather than the shell.")
2397 (process
, current_group
)
2398 Lisp_Object process
, current_group
;
2400 process_send_signal (process
, SIGINT
, current_group
, 0);
2404 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
2405 "Kill process PROCESS. May be process or name of one.\n\
2406 See function `interrupt-process' for more details on usage.")
2407 (process
, current_group
)
2408 Lisp_Object process
, current_group
;
2410 process_send_signal (process
, SIGKILL
, current_group
, 0);
2414 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
2415 "Send QUIT signal to process PROCESS. May be process or name of one.\n\
2416 See function `interrupt-process' for more details on usage.")
2417 (process
, current_group
)
2418 Lisp_Object process
, current_group
;
2420 process_send_signal (process
, SIGQUIT
, current_group
, 0);
2424 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
2425 "Stop process PROCESS. May be process or name of one.\n\
2426 See function `interrupt-process' for more details on usage.")
2427 (process
, current_group
)
2428 Lisp_Object process
, current_group
;
2431 error ("no SIGTSTP support");
2433 process_send_signal (process
, SIGTSTP
, current_group
, 0);
2438 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
2439 "Continue process PROCESS. May be process or name of one.\n\
2440 See function `interrupt-process' for more details on usage.")
2441 (process
, current_group
)
2442 Lisp_Object process
, current_group
;
2445 process_send_signal (process
, SIGCONT
, current_group
, 0);
2447 error ("no SIGCONT support");
2452 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
2453 2, 2, "nProcess number: \nnSignal code: ",
2454 "Send the process with number PID the signal with code CODE.\n\
2455 Both PID and CODE are integers.")
2457 Lisp_Object pid
, sig
;
2459 CHECK_NUMBER (pid
, 0);
2460 CHECK_NUMBER (sig
, 1);
2461 return make_number (kill (XINT (pid
), XINT (sig
)));
2464 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
2465 "Make PROCESS see end-of-file in its input.\n\
2466 Eof comes after any text already sent to it.\n\
2467 nil or no arg means current buffer's process.")
2469 Lisp_Object process
;
2473 proc
= get_process (process
);
2474 /* Sending a zero-length record is supposed to mean eof
2475 when TIOCREMOTE is turned on. */
2479 write (XFASTINT (XPROCESS (proc
)->outfd
), buf
, 0);
2481 #else /* did not do TOICREMOTE */
2483 send_process (proc
, "\032", 1); /* ^z */
2485 if (!NILP (XPROCESS (proc
)->pty_flag
))
2486 send_process (proc
, "\004", 1);
2489 close (XPROCESS (proc
)->outfd
);
2490 XFASTINT (XPROCESS (proc
)->outfd
) = open ("/dev/null", O_WRONLY
);
2493 #endif /* did not do TOICREMOTE */
2497 /* Kill all processes associated with `buffer'.
2498 If `buffer' is nil, kill all processes */
2500 kill_buffer_processes (buffer
)
2503 Lisp_Object tail
, proc
;
2505 for (tail
= Vprocess_alist
; XGCTYPE (tail
) == Lisp_Cons
;
2506 tail
= XCONS (tail
)->cdr
)
2508 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
2509 if (XGCTYPE (proc
) == Lisp_Process
2510 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
2512 if (NETCONN_P (proc
))
2513 deactivate_process (proc
);
2514 else if (XFASTINT (XPROCESS (proc
)->infd
))
2515 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
2520 /* On receipt of a signal that a child status has changed,
2521 loop asking about children with changed statuses until
2522 the system says there are no more.
2523 All we do is change the status;
2524 we do not run sentinels or print notifications.
2525 That is saved for the next time keyboard input is done,
2526 in order to avoid timing errors. */
2528 /** WARNING: this can be called during garbage collection.
2529 Therefore, it must not be fooled by the presence of mark bits in
2532 /** USG WARNING: Although it is not obvious from the documentation
2533 in signal(2), on a USG system the SIGCLD handler MUST NOT call
2534 signal() before executing at least one wait(), otherwise the handler
2535 will be called again, resulting in an infinite loop. The relevant
2536 portion of the documentation reads "SIGCLD signals will be queued
2537 and the signal-catching function will be continually reentered until
2538 the queue is empty". Invoking signal() causes the kernel to reexamine
2539 the SIGCLD queue. Fred Fish, UniSoft Systems Inc. */
2542 sigchld_handler (signo
)
2545 int old_errno
= errno
;
2547 register struct Lisp_Process
*p
;
2551 sigheld
|= sigbit (SIGCHLD
);
2563 #endif /* no WUNTRACED */
2564 /* Keep trying to get a status until we get a definitive result. */
2568 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
2570 while (pid
<= 0 && errno
== EINTR
);
2574 /* A real failure. We have done all our job, so return. */
2576 /* USG systems forget handlers when they are used;
2577 must reestablish each time */
2579 signal (signo
, sigchld_handler
); /* WARNING - must come after wait3() */
2582 sigheld
&= ~sigbit (SIGCHLD
);
2590 #endif /* no WNOHANG */
2592 /* Find the process that signaled us, and record its status. */
2595 for (tail
= Vprocess_alist
; XSYMBOL (tail
) != XSYMBOL (Qnil
); tail
= XCONS (tail
)->cdr
)
2597 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
2598 p
= XPROCESS (proc
);
2599 if (EQ (p
->childp
, Qt
) && XFASTINT (p
->pid
) == pid
)
2604 /* Look for an asynchronous process whose pid hasn't been filled
2607 for (tail
= Vprocess_alist
; XSYMBOL (tail
) != XSYMBOL (Qnil
); tail
= XCONS (tail
)->cdr
)
2609 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
2610 p
= XPROCESS (proc
);
2611 if (XTYPE (p
->pid
) == Lisp_Int
&& XINT (p
->pid
) == -1)
2616 /* Change the status of the process that was found. */
2619 union { int i
; WAITTYPE wt
; } u
;
2621 XSETINT (p
->tick
, ++process_tick
);
2623 XFASTINT (p
->raw_status_low
) = u
.i
& 0xffff;
2624 XFASTINT (p
->raw_status_high
) = u
.i
>> 16;
2626 /* If process has terminated, stop waiting for its output. */
2627 if (WIFSIGNALED (w
) || WIFEXITED (w
))
2629 FD_CLR (p
->infd
, &input_wait_mask
);
2632 /* There was no asynchronous process found for that id. Check
2633 if we have a synchronous process. */
2636 synch_process_alive
= 0;
2638 /* Report the status of the synchronous process. */
2640 synch_process_retcode
= WRETCODE (w
);
2641 else if (WIFSIGNALED (w
))
2642 synch_process_death
= sys_siglist
[WTERMSIG (w
)];
2645 /* On some systems, we must return right away.
2646 If any more processes want to signal us, we will
2648 Otherwise (on systems that have WNOHANG), loop around
2649 to use up all the processes that have something to tell us. */
2650 #if defined (USG) && ! (defined (HPUX) && defined (WNOHANG))
2652 signal (signo
, sigchld_handler
);
2656 #endif /* USG, but not HPUX with WNOHANG */
2662 exec_sentinel_unwind (data
)
2665 XPROCESS (XCONS (data
)->car
)->sentinel
= XCONS (data
)->cdr
;
2670 exec_sentinel (proc
, reason
)
2671 Lisp_Object proc
, reason
;
2673 Lisp_Object sentinel
;
2674 register struct Lisp_Process
*p
= XPROCESS (proc
);
2675 int count
= specpdl_ptr
- specpdl
;
2677 sentinel
= p
->sentinel
;
2678 if (NILP (sentinel
))
2681 /* Zilch the sentinel while it's running, to avoid recursive invocations;
2682 assure that it gets restored no matter how the sentinel exits. */
2684 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
2685 /* Inhibit quit so that random quits don't screw up a running filter. */
2686 specbind (Qinhibit_quit
, Qt
);
2687 call2 (sentinel
, proc
, reason
);
2691 /* Report all recent events of a change in process status
2692 (either run the sentinel or output a message).
2693 This is done while Emacs is waiting for keyboard input. */
2697 register Lisp_Object proc
, buffer
;
2698 Lisp_Object tail
= Qnil
;
2699 Lisp_Object msg
= Qnil
;
2700 struct gcpro gcpro1
, gcpro2
;
2702 /* We need to gcpro tail; if read_process_output calls a filter
2703 which deletes a process and removes the cons to which tail points
2704 from Vprocess_alist, and then causes a GC, tail is an unprotected
2708 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
2711 register struct Lisp_Process
*p
;
2713 proc
= Fcdr (Fcar (tail
));
2714 p
= XPROCESS (proc
);
2716 if (XINT (p
->tick
) != XINT (p
->update_tick
))
2718 XSETINT (p
->update_tick
, XINT (p
->tick
));
2720 /* If process is still active, read any output that remains. */
2721 if (XFASTINT (p
->infd
))
2722 while (read_process_output (proc
, XFASTINT (p
->infd
)) > 0);
2726 /* Get the text to use for the message. */
2727 if (!NILP (p
->raw_status_low
))
2729 msg
= status_message (p
->status
);
2731 /* If process is terminated, deactivate it or delete it. */
2733 if (XTYPE (p
->status
) == Lisp_Cons
)
2734 symbol
= XCONS (p
->status
)->car
;
2736 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
2737 || EQ (symbol
, Qclosed
))
2739 if (delete_exited_processes
)
2740 remove_process (proc
);
2742 deactivate_process (proc
);
2745 /* Now output the message suitably. */
2746 if (!NILP (p
->sentinel
))
2747 exec_sentinel (proc
, msg
);
2748 /* Don't bother with a message in the buffer
2749 when a process becomes runnable. */
2750 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
2752 Lisp_Object ro
= XBUFFER (buffer
)->read_only
;
2754 struct buffer
*old
= current_buffer
;
2757 /* Avoid error if buffer is deleted
2758 (probably that's why the process is dead, too) */
2759 if (NILP (XBUFFER (buffer
)->name
))
2761 Fset_buffer (buffer
);
2763 /* Insert new output into buffer
2764 at the current end-of-output marker,
2765 thus preserving logical ordering of input and output. */
2766 if (XMARKER (p
->mark
)->buffer
)
2767 SET_PT (marker_position (p
->mark
));
2770 if (point
<= opoint
)
2771 opoint
+= XSTRING (msg
)->size
+ XSTRING (p
->name
)->size
+ 10;
2773 tem
= current_buffer
->read_only
;
2774 current_buffer
->read_only
= Qnil
;
2775 insert_string ("\nProcess ");
2776 Finsert (1, &p
->name
);
2777 insert_string (" ");
2779 current_buffer
->read_only
= tem
;
2780 Fset_marker (p
->mark
, make_number (point
), p
->buffer
);
2783 set_buffer_internal (old
);
2788 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
2789 redisplay_preserve_echo_area ();
2791 update_tick
= process_tick
;
2802 if (! noninteractive
|| initialized
)
2804 signal (SIGCHLD
, sigchld_handler
);
2807 FD_ZERO (&input_wait_mask
);
2808 FD_SET (0, &input_wait_mask
);
2809 Vprocess_alist
= Qnil
;
2810 for (i
= 0; i
< MAXDESC
; i
++)
2812 chan_process
[i
] = Qnil
;
2813 proc_buffered_char
[i
] = -1;
2817 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 0, 1, 0,
2818 "Return the connection type of `PROCESS'. This can be nil (pipe),\n\
2819 t or pty (pty) or stream (socket connection).")
2821 Lisp_Object process
;
2823 return XPROCESS (process
)->type
;
2829 pty_process
= intern ("pty");
2832 stream_process
= intern ("stream");
2834 Qprocessp
= intern ("processp");
2835 staticpro (&Qprocessp
);
2836 Qrun
= intern ("run");
2838 Qstop
= intern ("stop");
2840 Qsignal
= intern ("signal");
2841 staticpro (&Qsignal
);
2843 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
2846 Qexit = intern ("exit");
2847 staticpro (&Qexit); */
2849 Qopen
= intern ("open");
2851 Qclosed
= intern ("closed");
2852 staticpro (&Qclosed
);
2854 staticpro (&Vprocess_alist
);
2856 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
2857 "*Non-nil means delete processes immediately when they exit.\n\
2858 nil means don't delete them until `list-processes' is run.");
2860 delete_exited_processes
= 1;
2862 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
2863 "Control type of device used to communicate with subprocesses.\n\
2864 Values are nil to use a pipe, and t or 'pty for a pty. Note that if\n\
2865 pty's are not available, this variable will be ignored. The value takes\n\
2866 effect when `start-process' is called.");
2867 Vprocess_connection_type
= Qt
;
2869 defsubr (&Sprocessp
);
2870 defsubr (&Sget_process
);
2871 defsubr (&Sget_buffer_process
);
2872 defsubr (&Sdelete_process
);
2873 defsubr (&Sprocess_status
);
2874 defsubr (&Sprocess_exit_status
);
2875 defsubr (&Sprocess_id
);
2876 defsubr (&Sprocess_name
);
2877 defsubr (&Sprocess_command
);
2878 defsubr (&Sset_process_buffer
);
2879 defsubr (&Sprocess_buffer
);
2880 defsubr (&Sprocess_mark
);
2881 defsubr (&Sset_process_filter
);
2882 defsubr (&Sprocess_filter
);
2883 defsubr (&Sset_process_sentinel
);
2884 defsubr (&Sprocess_sentinel
);
2885 defsubr (&Sprocess_kill_without_query
);
2886 defsubr (&Slist_processes
);
2887 defsubr (&Sprocess_list
);
2888 defsubr (&Sstart_process
);
2890 defsubr (&Sopen_network_stream
);
2891 #endif /* HAVE_SOCKETS */
2892 defsubr (&Saccept_process_output
);
2893 defsubr (&Sprocess_send_region
);
2894 defsubr (&Sprocess_send_string
);
2895 defsubr (&Sinterrupt_process
);
2896 defsubr (&Skill_process
);
2897 defsubr (&Squit_process
);
2898 defsubr (&Sstop_process
);
2899 defsubr (&Scontinue_process
);
2900 defsubr (&Sprocess_send_eof
);
2901 defsubr (&Ssignal_process
);
2902 defsubr (&Swaiting_for_user_input_p
);
2903 /* defsubr (&Sprocess_connection); */
2907 #else /* not subprocesses */
2909 #include <sys/types.h>
2913 #include "systime.h"
2914 #include "termopts.h"
2916 extern int screen_garbaged
;
2919 /* As described above, except assuming that there are no subprocesses:
2921 Wait for timeout to elapse and/or keyboard input to be available.
2924 timeout in seconds, or
2925 zero for no limit, or
2926 -1 means gobble data immediately available but don't wait for any.
2929 0 to ignore keyboard input, or
2930 1 to return when input is available, or
2931 -1 means caller will actually read the input, so don't throw to
2933 We know that read_kbd will never be a Lisp_Process, since
2934 `subprocesses' isn't defined.
2936 do_display != 0 means redisplay should be done to show subprocess
2937 output that arrives. This version of the function ignores it.
2939 If read_kbd is a pointer to a struct Lisp_Process, then the
2940 function returns true iff we received input from that process
2941 before the timeout elapsed.
2942 Otherwise, return true iff we recieved input from any process. */
2945 wait_reading_process_input (time_limit
, microsecs
, read_kbd
, do_display
)
2946 int time_limit
, microsecs
, read_kbd
, do_display
;
2948 EMACS_TIME end_time
, timeout
, *timeout_p
;
2951 /* What does time_limit really mean? */
2952 if (time_limit
|| microsecs
)
2954 /* It's not infinite. */
2955 timeout_p
= &timeout
;
2957 if (time_limit
== -1)
2958 /* In fact, it's zero. */
2959 EMACS_SET_SECS_USECS (timeout
, 0, 0);
2961 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
2963 /* How far in the future is that? */
2964 EMACS_GET_TIME (end_time
);
2965 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
2968 /* It's infinite. */
2971 /* Turn off periodic alarms (in case they are in use)
2972 because the select emulator uses alarms. */
2979 waitchannels
= read_kbd
? 1 : 0;
2981 /* If calling from keyboard input, do not quit
2982 since we want to return C-g as an input character.
2983 Otherwise, do pending quit if requested. */
2989 EMACS_GET_TIME (*timeout_p
);
2990 EMACS_SUB_TIME (*timeout_p
, end_time
, *timeout_p
);
2991 if (EMACS_TIME_NEG_P (*timeout_p
))
2995 /* Cause C-g and alarm signals to take immediate action,
2996 and cause input available signals to zero out timeout. */
2998 set_waiting_for_input (&timeout
);
3000 /* If a screen has been newly mapped and needs updating,
3001 reprocess its display stuff. */
3002 if (screen_garbaged
)
3003 redisplay_preserve_echo_area ();
3005 if (read_kbd
&& detect_input_pending ())
3008 nfds
= select (1, &waitchannels
, 0, 0, timeout_p
);
3010 /* Make C-g and alarm signals set flags again */
3011 clear_waiting_for_input ();
3013 /* If we woke up due to SIGWINCH, actually change size now. */
3014 do_pending_window_change ();
3018 /* If the system call was interrupted, then go around the
3024 else if (nfds
> 0 && (waitchannels
& 1) && interrupt_input
)
3025 /* System sometimes fails to deliver SIGIO. */
3026 kill (getpid (), SIGIO
);
3028 if (read_kbd
&& interrupt_input
&& (waitchannels
& 1))
3031 /* If we have timed out (nfds == 0) or found some input (nfds > 0),
3041 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
3042 "Return the (or, a) process associated with BUFFER.\n\
3043 This copy of Emacs has not been built to support subprocesses, so this\n\
3044 function always returns nil.")
3046 register Lisp_Object name
;
3051 /* Kill all processes associated with `buffer'.
3052 If `buffer' is nil, kill all processes.
3053 Since we have no subprocesses, this does nothing. */
3055 kill_buffer_processes (buffer
)
3066 defsubr (&Sget_buffer_process
);
3070 #endif /* not subprocesses */