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) && !defined(USG5)
53 #endif /* HAVE_PTYS and no O_NDELAY */
54 #endif /* BSD or STRIDE */
61 #include <sys/sysmacros.h> /* for "minor" */
71 #include "termhooks.h"
74 #include "dispextern.h"
76 Lisp_Object Qrun
, Qstop
, Qsignal
, Qopen
, Qclosed
;
77 /* Qexit is declared and initialized in eval.c. */
79 /* a process object is a network connection when its childp field is neither
80 Qt nor Qnil but is instead a string (name of foreign host we
81 are connected to + name of port we are connected to) */
84 static Lisp_Object stream_process
;
86 #define NETCONN_P(p) (XGCTYPE (XPROCESS (p)->childp) == Lisp_String)
88 #define NETCONN_P(p) 0
89 #endif /* HAVE_SOCKETS */
91 /* Define first descriptor number available for subprocesses. */
93 #define FIRST_PROC_DESC 1
95 #define FIRST_PROC_DESC 3
98 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
101 #if !defined (SIGCHLD) && defined (SIGCLD)
102 #define SIGCHLD SIGCLD
105 #include "syssignal.h"
107 /* Define the structure that the wait system call stores.
108 On many systems, there is a structure defined for this.
109 But on vanilla-ish USG systems there is not. */
113 #if !defined (BSD) && !defined (UNIPLUS) && !defined (STRIDE) && !(defined (HPUX) && !defined (NOMULTIPLEJOBS)) && !defined (HAVE_WAIT_HEADER)
115 #define WIFSTOPPED(w) ((w&0377) == 0177)
116 #define WIFSIGNALED(w) ((w&0377) != 0177 && (w&~0377) == 0)
117 #define WIFEXITED(w) ((w&0377) == 0)
118 #define WRETCODE(w) (w >> 8)
119 #define WSTOPSIG(w) (w >> 8)
120 #define WTERMSIG(w) (w & 0377)
122 #define WCOREDUMP(w) ((w&0200) != 0)
128 #include <sys/wait.h>
129 #endif /* not BSD 4.1 */
131 #define WAITTYPE union wait
132 #define WRETCODE(w) w.w_retcode
133 #define WCOREDUMP(w) w.w_coredump
136 /* HPUX version 7 has broken definitions of these. */
145 #define WTERMSIG(w) w.w_termsig
148 #define WSTOPSIG(w) w.w_stopsig
151 #define WIFSTOPPED(w) (WTERMSIG (w) == 0177)
154 #define WIFSIGNALED(w) (WTERMSIG (w) != 0177 && (WSTOPSIG (w)) == 0)
157 #define WIFEXITED(w) (WTERMSIG (w) == 0)
159 #endif /* BSD or UNIPLUS or STRIDE */
160 #endif /* no WAITTYPE */
163 /* For the CMU PTY driver + */
164 #define DCL_PROMPT "$ "
174 extern char *sys_errlist
[];
178 extern char *sys_siglist
[];
180 char *sys_siglist
[] =
186 "illegal instruction",
190 "floating point exception",
193 "segmentation violation",
194 "bad argument to system call",
195 "write on a pipe with no one to read it",
197 "software termination signal from kill",
199 "sendable stop signal not from tty",
200 "stop signal from tty",
201 "continue a stopped process",
202 "child status has changed",
203 "background read attempted from control tty",
204 "background write attempted from control tty",
205 "input record available at control tty",
206 "exceeded CPU time limit",
207 "exceeded file size limit"
215 extern int comm_server
;
216 extern int net_listen_address
;
219 /* t means use pty, nil means use a pipe,
220 maybe other values to come. */
221 Lisp_Object Vprocess_connection_type
;
225 #include <sys/socket.h>
229 /* Number of events of change of status of a process. */
232 /* Number of events for which the user or sentinel has been notified. */
236 /* We could get this from param.h, but better not to depend on finding that.
237 And better not to risk that it might define other symbols used in this
240 #define SELECT_TYPE fd_set
241 #else /* no FD_SET */
243 #define SELECT_TYPE int
245 /* Define the macros to access a single-int bitmap of descriptors. */
246 #define FD_SET(n, p) (*(p) |= (1 << (n)))
247 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
248 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
249 #define FD_ZERO(p) (*(p) = 0)
250 #endif /* no FD_SET */
252 /* Mask of bits indicating the descriptors that we wait for input on */
254 SELECT_TYPE input_wait_mask
;
256 int delete_exited_processes
;
258 /* Indexed by descriptor, gives the process (if any) for that descriptor */
259 Lisp_Object chan_process
[MAXDESC
];
261 /* Alist of elements (NAME . PROCESS) */
262 Lisp_Object Vprocess_alist
;
264 Lisp_Object Qprocessp
;
266 Lisp_Object
get_process ();
268 /* Buffered-ahead input char from process, indexed by channel.
269 -1 means empty (no char is buffered).
270 Used on sys V where the only way to tell if there is any
271 output from the process is to read at least one char.
272 Always -1 on systems that support FIONREAD. */
274 int proc_buffered_char
[MAXDESC
];
276 /* Compute the Lisp form of the process status, p->status, from
277 the numeric status that was returned by `wait'. */
279 Lisp_Object
status_convert ();
282 struct Lisp_Process
*p
;
284 union { int i
; WAITTYPE wt
; } u
;
285 u
.i
= XFASTINT (p
->raw_status_low
) + (XFASTINT (p
->raw_status_high
) << 16);
286 p
->status
= status_convert (u
.wt
);
287 p
->raw_status_low
= Qnil
;
288 p
->raw_status_high
= Qnil
;
291 /* Convert a process status work in Unix format to
292 the list that we use internally. */
299 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
300 else if (WIFEXITED (w
))
301 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
302 WCOREDUMP (w
) ? Qt
: Qnil
));
303 else if (WIFSIGNALED (w
))
304 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
305 WCOREDUMP (w
) ? Qt
: Qnil
));
310 /* Given a status-list, extract the three pieces of information
311 and store them individually through the three pointers. */
314 decode_status (l
, symbol
, code
, coredump
)
322 if (XTYPE (l
) == Lisp_Symbol
)
330 *symbol
= XCONS (l
)->car
;
331 tem
= XCONS (l
)->cdr
;
332 *code
= XFASTINT (XCONS (tem
)->car
);
333 tem
= XCONS (tem
)->cdr
;
334 *coredump
= !NILP (tem
);
338 /* Return a string describing a process status list. */
341 status_message (status
)
346 Lisp_Object string
, string2
;
348 decode_status (status
, &symbol
, &code
, &coredump
);
350 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
352 string
= build_string (code
< NSIG
? sys_siglist
[code
] : "unknown");
353 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
354 XSTRING (string
)->data
[0] = DOWNCASE (XSTRING (string
)->data
[0]);
355 return concat2 (string
, string2
);
357 else if (EQ (symbol
, Qexit
))
360 return build_string ("finished\n");
361 string
= Fint_to_string (make_number (code
));
362 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
363 return concat2 (build_string ("exited abnormally with code "),
364 concat2 (string
, string2
));
367 return Fcopy_sequence (Fsymbol_name (symbol
));
372 /* Open an available pty, returning a file descriptor.
373 Return -1 on failure.
374 The file name of the terminal corresponding to the pty
375 is left in the variable pty_name. */
386 /* Some systems name their pseudoterminals so that there are gaps in
387 the usual sequence - for example, on HP9000/S700 systems, there
388 are no pseudoterminals with names ending in 'f'. So we wait for
389 three failures in a row before deciding that we've reached the
391 int failed_count
= 0;
396 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
397 for (i
= 0; i
< 16; i
++)
400 #ifdef PTY_NAME_SPRINTF
403 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
404 #endif /* no PTY_NAME_SPRINTF */
408 #else /* no PTY_OPEN */
410 /* Unusual IRIS code */
411 *ptyv
= open ("/dev/ptc", O_RDWR
| O_NDELAY
, 0);
414 if (fstat (fd
, &stb
) < 0)
417 if (stat (pty_name
, &stb
) < 0)
420 if (failed_count
>= 3)
426 fd
= open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
428 fd
= open (pty_name
, O_RDWR
| O_NDELAY
, 0);
430 #endif /* not IRIS */
431 #endif /* no PTY_OPEN */
435 /* check to make certain that both sides are available
436 this avoids a nasty yet stupid bug in rlogins */
437 #ifdef PTY_TTY_NAME_SPRINTF
440 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
441 #endif /* no PTY_TTY_NAME_SPRINTF */
443 if (access (pty_name
, 6) != 0)
452 #endif /* not UNIPLUS */
459 #endif /* HAVE_PTYS */
465 register Lisp_Object val
, tem
, name1
;
466 register struct Lisp_Process
*p
;
470 /* size of process structure includes the vector header,
471 so deduct for that. But struct Lisp_Vector includes the first
472 element, thus deducts too much, so add it back. */
473 val
= Fmake_vector (make_number ((sizeof (struct Lisp_Process
)
474 - sizeof (struct Lisp_Vector
)
475 + sizeof (Lisp_Object
))
476 / sizeof (Lisp_Object
)),
478 XSETTYPE (val
, Lisp_Process
);
481 XFASTINT (p
->infd
) = 0;
482 XFASTINT (p
->outfd
) = 0;
483 XFASTINT (p
->pid
) = 0;
484 XFASTINT (p
->tick
) = 0;
485 XFASTINT (p
->update_tick
) = 0;
486 p
->raw_status_low
= Qnil
;
487 p
->raw_status_high
= Qnil
;
489 p
->mark
= Fmake_marker ();
491 /* If name is already in use, modify it until it is unused. */
496 tem
= Fget_process (name1
);
497 if (NILP (tem
)) break;
498 sprintf (suffix
, "<%d>", i
);
499 name1
= concat2 (name
, build_string (suffix
));
503 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
507 remove_process (proc
)
508 register Lisp_Object proc
;
510 register Lisp_Object pair
;
512 pair
= Frassq (proc
, Vprocess_alist
);
513 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
514 Fset_marker (XPROCESS (proc
)->mark
, Qnil
, Qnil
);
516 deactivate_process (proc
);
519 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
520 "Return t if OBJECT is a process.")
524 return XTYPE (obj
) == Lisp_Process
? Qt
: Qnil
;
527 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
528 "Return the process named NAME, or nil if there is none.")
530 register Lisp_Object name
;
532 if (XTYPE (name
) == Lisp_Process
)
534 CHECK_STRING (name
, 0);
535 return Fcdr (Fassoc (name
, Vprocess_alist
));
538 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
539 "Return the (or, a) process associated with BUFFER.\n\
540 BUFFER may be a buffer or the name of one.")
542 register Lisp_Object name
;
544 register Lisp_Object buf
, tail
, proc
;
546 if (NILP (name
)) return Qnil
;
547 buf
= Fget_buffer (name
);
548 if (NILP (buf
)) return Qnil
;
550 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
552 proc
= Fcdr (Fcar (tail
));
553 if (XTYPE (proc
) == Lisp_Process
&& EQ (XPROCESS (proc
)->buffer
, buf
))
559 /* This is how commands for the user decode process arguments. It
560 accepts a process, a process name, a buffer, a buffer name, or nil.
561 Buffers denote the first process in the buffer, and nil denotes the
566 register Lisp_Object name
;
568 register Lisp_Object proc
;
570 proc
= Fget_buffer_process (Fcurrent_buffer ());
573 proc
= Fget_process (name
);
575 proc
= Fget_buffer_process (Fget_buffer (name
));
582 error ("Current buffer has no process");
584 error ("Process %s does not exist", XSTRING (name
)->data
);
588 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
589 "Delete PROCESS: kill it and forget about it immediately.\n\
590 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
591 nil, indicating the current buffer's process.")
593 register Lisp_Object proc
;
595 proc
= get_process (proc
);
596 XPROCESS (proc
)->raw_status_low
= Qnil
;
597 XPROCESS (proc
)->raw_status_high
= Qnil
;
598 if (NETCONN_P (proc
))
600 XPROCESS (proc
)->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
601 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
603 else if (XFASTINT (XPROCESS (proc
)->infd
))
605 Fkill_process (proc
, Qnil
);
606 /* Do this now, since remove_process will make sigchld_handler do nothing. */
607 XPROCESS (proc
)->status
608 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
609 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
612 remove_process (proc
);
616 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
617 "Return the status of PROCESS: a symbol, one of these:\n\
618 run -- for a process that is running.\n\
619 stop -- for a process stopped but continuable.\n\
620 exit -- for a process that has exited.\n\
621 signal -- for a process that has got a fatal signal.\n\
622 open -- for a network stream connection that is open.\n\
623 closed -- for a network stream connection that is closed.\n\
624 nil -- if arg is a process name and no such process exists.\n\
625 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
626 nil, indicating the current buffer's process.")
627 /* command -- for a command channel opened to Emacs by another process.\n\
628 external -- for an i/o channel opened to Emacs by another process.\n\ */
630 register Lisp_Object proc
;
632 register struct Lisp_Process
*p
;
633 register Lisp_Object status
;
634 proc
= get_process (proc
);
638 if (!NILP (p
->raw_status_low
))
641 if (XTYPE (status
) == Lisp_Cons
)
642 status
= XCONS (status
)->car
;
643 if (NETCONN_P (proc
))
645 if (EQ (status
, Qrun
))
647 else if (EQ (status
, Qexit
))
653 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
655 "Return the exit status of PROCESS or the signal number that killed it.\n\
656 If PROCESS has not yet exited or died, return 0.")
658 register Lisp_Object proc
;
660 CHECK_PROCESS (proc
, 0);
661 if (!NILP (XPROCESS (proc
)->raw_status_low
))
662 update_status (XPROCESS (proc
));
663 if (XTYPE (XPROCESS (proc
)->status
) == Lisp_Cons
)
664 return XCONS (XCONS (XPROCESS (proc
)->status
)->cdr
)->car
;
665 return make_number (0);
668 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
669 "Return the process id of PROCESS.\n\
670 This is the pid of the Unix process which PROCESS uses or talks to.\n\
671 For a network connection, this value is nil.")
673 register Lisp_Object proc
;
675 CHECK_PROCESS (proc
, 0);
676 return XPROCESS (proc
)->pid
;
679 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
680 "Return the name of PROCESS, as a string.\n\
681 This is the name of the program invoked in PROCESS,\n\
682 possibly modified to make it unique among process names.")
684 register Lisp_Object proc
;
686 CHECK_PROCESS (proc
, 0);
687 return XPROCESS (proc
)->name
;
690 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
691 "Return the command that was executed to start PROCESS.\n\
692 This is a list of strings, the first string being the program executed\n\
693 and the rest of the strings being the arguments given to it.\n\
694 For a non-child channel, this is nil.")
696 register Lisp_Object proc
;
698 CHECK_PROCESS (proc
, 0);
699 return XPROCESS (proc
)->command
;
702 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
704 "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
706 register Lisp_Object proc
, buffer
;
708 CHECK_PROCESS (proc
, 0);
710 CHECK_BUFFER (buffer
, 1);
711 XPROCESS (proc
)->buffer
= buffer
;
715 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
717 "Return the buffer PROCESS is associated with.\n\
718 Output from PROCESS is inserted in this buffer\n\
719 unless PROCESS has a filter.")
721 register Lisp_Object proc
;
723 CHECK_PROCESS (proc
, 0);
724 return XPROCESS (proc
)->buffer
;
727 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
729 "Return the marker for the end of the last output from PROCESS.")
731 register Lisp_Object proc
;
733 CHECK_PROCESS (proc
, 0);
734 return XPROCESS (proc
)->mark
;
737 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
739 "Give PROCESS the filter function FILTER; nil means no filter.\n\
740 When a process has a filter, each time it does output\n\
741 the entire string of output is passed to the filter.\n\
742 The filter gets two arguments: the process and the string of output.\n\
743 If the process has a filter, its buffer is not used for output.")
745 register Lisp_Object proc
, filter
;
747 CHECK_PROCESS (proc
, 0);
748 XPROCESS (proc
)->filter
= filter
;
752 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
754 "Returns the filter function of PROCESS; nil if none.\n\
755 See `set-process-filter' for more info on filter functions.")
757 register Lisp_Object proc
;
759 CHECK_PROCESS (proc
, 0);
760 return XPROCESS (proc
)->filter
;
763 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
765 "Give PROCESS the sentinel SENTINEL; nil for none.\n\
766 The sentinel is called as a function when the process changes state.\n\
767 It gets two arguments: the process, and a string describing the change.")
769 register Lisp_Object proc
, sentinel
;
771 CHECK_PROCESS (proc
, 0);
772 XPROCESS (proc
)->sentinel
= sentinel
;
776 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
778 "Return the sentinel of PROCESS; nil if none.\n\
779 See `set-process-sentinel' for more info on sentinels.")
781 register Lisp_Object proc
;
783 CHECK_PROCESS (proc
, 0);
784 return XPROCESS (proc
)->sentinel
;
787 DEFUN ("process-kill-without-query", Fprocess_kill_without_query
,
788 Sprocess_kill_without_query
, 1, 2, 0,
789 "Say no query needed if PROCESS is running when Emacs is exited.\n\
790 Optional second argument if non-nill says to require a query.\n\
791 Value is t if a query was formerly required.")
793 register Lisp_Object proc
, value
;
797 CHECK_PROCESS (proc
, 0);
798 tem
= XPROCESS (proc
)->kill_without_query
;
799 XPROCESS (proc
)->kill_without_query
= Fnull (value
);
807 register Lisp_Object tail
, tem
;
808 Lisp_Object proc
, minspace
, tem1
;
809 register struct buffer
*old
= current_buffer
;
810 register struct Lisp_Process
*p
;
814 XFASTINT (minspace
) = 1;
816 set_buffer_internal (XBUFFER (Vstandard_output
));
817 Fbuffer_disable_undo (Vstandard_output
);
819 current_buffer
->truncate_lines
= Qt
;
822 Proc Status Buffer Command\n\
823 ---- ------ ------ -------\n", -1);
825 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
829 proc
= Fcdr (Fcar (tail
));
831 if (NILP (p
->childp
))
834 Finsert (1, &p
->name
);
835 Findent_to (make_number (13), minspace
);
837 if (!NILP (p
->raw_status_low
))
840 if (XTYPE (p
->status
) == Lisp_Cons
)
841 symbol
= XCONS (p
->status
)->car
;
844 if (EQ (symbol
, Qsignal
))
847 tem
= Fcar (Fcdr (p
->status
));
849 if (XINT (tem
) < NSIG
)
850 write_string (sys_siglist
[XINT (tem
)], -1);
853 Fprinc (symbol
, Qnil
);
855 else if (NETCONN_P (proc
))
857 if (EQ (symbol
, Qrun
))
858 write_string ("open", -1);
859 else if (EQ (symbol
, Qexit
))
860 write_string ("closed", -1);
862 Fprinc (symbol
, Qnil
);
865 Fprinc (symbol
, Qnil
);
867 if (EQ (symbol
, Qexit
))
870 tem
= Fcar (Fcdr (p
->status
));
873 sprintf (tembuf
, " %d", XFASTINT (tem
));
874 write_string (tembuf
, -1);
878 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
879 remove_process (proc
);
881 Findent_to (make_number (22), minspace
);
882 if (NILP (p
->buffer
))
883 insert_string ("(none)");
884 else if (NILP (XBUFFER (p
->buffer
)->name
))
885 insert_string ("(Killed)");
887 Finsert (1, &XBUFFER (p
->buffer
)->name
);
889 Findent_to (make_number (37), minspace
);
891 if (NETCONN_P (proc
))
893 sprintf (tembuf
, "(network stream connection to %s)\n",
894 XSTRING (p
->childp
)->data
);
895 insert_string (tembuf
);
909 insert_string ("\n");
915 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 0, "",
916 "Display a list of all processes.\n\
917 \(Any processes listed as Exited or Signaled are actually eliminated\n\
918 after the listing is made.)")
921 internal_with_output_to_temp_buffer ("*Process List*",
922 list_processes_1
, Qnil
);
926 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
927 "Return a list of all processes.")
930 return Fmapcar (Qcdr
, Vprocess_alist
);
933 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
934 "Start a program in a subprocess. Return the process object for it.\n\
935 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
936 NAME is name for process. It is modified if necessary to make it unique.\n\
937 BUFFER is the buffer or (buffer-name) to associate with the process.\n\
938 Process output goes at end of that buffer, unless you specify\n\
939 an output stream or filter function to handle the output.\n\
940 BUFFER may be also nil, meaning that this process is not associated\n\
942 Third arg is program file name. It is searched for as in the shell.\n\
943 Remaining arguments are strings to give program as arguments.")
946 register Lisp_Object
*args
;
948 Lisp_Object buffer
, name
, program
, proc
, tem
;
950 register unsigned char *new_argv
;
953 register unsigned char **new_argv
;
959 buffer
= Fget_buffer_create (buffer
);
962 CHECK_STRING (name
, 0);
966 CHECK_STRING (program
, 2);
969 /* Make a one member argv with all args concatenated
970 together separated by a blank. */
971 len
= XSTRING (program
)->size
+ 2;
972 for (i
= 3; i
< nargs
; i
++)
975 CHECK_STRING (tem
, i
);
976 len
+= XSTRING (tem
)->size
+ 1; /* count the blank */
978 new_argv
= (unsigned char *) alloca (len
);
979 strcpy (new_argv
, XSTRING (program
)->data
);
980 for (i
= 3; i
< nargs
; i
++)
983 CHECK_STRING (tem
, i
);
984 strcat (new_argv
, " ");
985 strcat (new_argv
, XSTRING (tem
)->data
);
988 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
990 for (i
= 3; i
< nargs
; i
++)
993 CHECK_STRING (tem
, i
);
994 new_argv
[i
- 2] = XSTRING (tem
)->data
;
997 new_argv
[0] = XSTRING (program
)->data
;
999 /* If program file name is not absolute, search our path for it */
1000 if (new_argv
[0][0] != '/')
1003 openp (Vexec_path
, program
, "", &tem
, 1);
1005 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1006 new_argv
[0] = XSTRING (tem
)->data
;
1008 #endif /* not VMS */
1010 proc
= make_process (name
);
1012 XPROCESS (proc
)->childp
= Qt
;
1013 XPROCESS (proc
)->command_channel_p
= Qnil
;
1014 XPROCESS (proc
)->buffer
= buffer
;
1015 XPROCESS (proc
)->sentinel
= Qnil
;
1016 XPROCESS (proc
)->filter
= Qnil
;
1017 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1019 create_process (proc
, new_argv
);
1025 create_process_1 (signo
)
1029 /* USG systems forget handlers when they are used;
1030 must reestablish each time */
1031 signal (signo
, create_process_1
);
1035 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1038 /* Mimic blocking of signals on system V, which doesn't really have it. */
1040 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1041 int sigchld_deferred
;
1044 create_process_sigchld ()
1046 signal (SIGCHLD
, create_process_sigchld
);
1048 sigchld_deferred
= 1;
1054 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1055 create_process (process
, new_argv
)
1056 Lisp_Object process
;
1059 int pid
, inchannel
, outchannel
, forkin
, forkout
;
1062 SIGTYPE (*sigchld
)();
1065 Lisp_Object current_dir
;
1066 extern char **environ
;
1068 inchannel
= outchannel
= -1;
1071 if (EQ (Vprocess_connection_type
, Qt
))
1072 outchannel
= inchannel
= allocate_pty ();
1074 /* Make sure that the child will be able to chdir to the current
1075 buffer's current directory. We can't just have the child check
1076 for an error when it does the chdir, since it's in a vfork. */
1077 current_dir
= expand_and_dir_to_file (current_buffer
->directory
, Qnil
);
1078 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1079 report_file_error ("Setting current directory",
1080 Fcons (current_buffer
->directory
, Qnil
));
1085 /* On USG systems it does not work to open the pty's tty here
1086 and then close and reopen it in the child. */
1088 /* Don't let this terminal become our controlling terminal
1089 (in case we don't have one). */
1090 forkout
= forkin
= open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1092 forkout
= forkin
= open (pty_name
, O_RDWR
, 0);
1095 report_file_error ("Opening pty", Qnil
);
1097 forkin
= forkout
= -1;
1098 #endif /* not USG */
1102 #endif /* HAVE_PTYS */
1105 if (socketpair (AF_UNIX
, SOCK_STREAM
, 0, sv
) < 0)
1106 report_file_error ("Opening socketpair", Qnil
);
1107 outchannel
= inchannel
= sv
[0];
1108 forkout
= forkin
= sv
[1];
1110 #else /* not SKTPAIR */
1119 #endif /* not SKTPAIR */
1122 /* Replaced by close_process_descs */
1123 set_exclusive_use (inchannel
);
1124 set_exclusive_use (outchannel
);
1127 /* Stride people say it's a mystery why this is needed
1128 as well as the O_NDELAY, but that it fails without this. */
1129 #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
1132 ioctl (inchannel
, FIONBIO
, &one
);
1137 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1140 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1144 /* Record this as an active process, with its channels.
1145 As a result, child_setup will close Emacs's side of the pipes. */
1146 chan_process
[inchannel
] = process
;
1147 XFASTINT (XPROCESS (process
)->infd
) = inchannel
;
1148 XFASTINT (XPROCESS (process
)->outfd
) = outchannel
;
1149 /* Record the tty descriptor used in the subprocess. */
1151 XPROCESS (process
)->subtty
= Qnil
;
1153 XFASTINT (XPROCESS (process
)->subtty
) = forkin
;
1154 XPROCESS (process
)->pty_flag
= (pty_flag
? Qt
: Qnil
);
1155 XPROCESS (process
)->status
= Qrun
;
1157 /* Delay interrupts until we have a chance to store
1158 the new fork's pid in its process structure */
1162 #else /* not BSD4_1 */
1163 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1164 sigsetmask (sigmask (SIGCHLD
));
1165 #else /* ordinary USG */
1167 sigchld_deferred
= 0;
1168 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
1170 #endif /* ordinary USG */
1171 #endif /* not BSD4_1 */
1172 #endif /* SIGCHLD */
1174 /* Until we store the proper pid, enable sigchld_handler
1175 to recognize an unknown pid as standing for this process.
1176 It is very important not to let this `marker' value stay
1177 in the table after this function has returned; if it does
1178 it might cause call-process to hang and subsequent asynchronous
1179 processes to get their return values scrambled. */
1180 XSETINT (XPROCESS (process
)->pid
, -1);
1183 /* child_setup must clobber environ on systems with true vfork.
1184 Protect it from permanent change. */
1185 char **save_environ
= environ
;
1190 int xforkin
= forkin
;
1191 int xforkout
= forkout
;
1193 #if 0 /* This was probably a mistake--it duplicates code later on,
1194 but fails to handle all the cases. */
1195 /* Make sure SIGCHLD is not blocked in the child. */
1196 sigsetmask (SIGEMPTYMASK
);
1199 /* Make the pty be the controlling terminal of the process. */
1201 /* First, disconnect its current controlling terminal. */
1205 /* Make the pty's terminal the controlling terminal. */
1206 if (pty_flag
&& (ioctl (xforkin
, TIOCSCTTY
, 0) < 0))
1209 #else /* not HAVE_SETSID */
1211 /* It's very important to call setpgrp() here and no time
1212 afterwards. Otherwise, we lose our controlling tty which
1213 is set when we open the pty. */
1216 #endif /* not HAVE_SETSID */
1218 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1219 can do TIOCSPGRP only to the process's controlling tty. */
1222 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1223 I can't test it since I don't have 4.3. */
1224 int j
= open ("/dev/tty", O_RDWR
, 0);
1225 ioctl (j
, TIOCNOTTY
, 0);
1228 /* In order to get a controlling terminal on some versions
1229 of BSD, it is necessary to put the process in pgrp 0
1230 before it opens the terminal. */
1234 #endif /* TIOCNOTTY */
1236 #if !defined (RTU) && !defined (UNIPLUS)
1237 /*** There is a suggestion that this ought to be a
1238 conditional on TIOCSPGRP. */
1239 /* Now close the pty (if we had it open) and reopen it.
1240 This makes the pty the controlling terminal of the subprocess. */
1243 /* I wonder if close (open (pty_name, ...)) would work? */
1246 xforkout
= xforkin
= open (pty_name
, O_RDWR
, 0);
1251 #endif /* not UNIPLUS and not RTU */
1252 #ifdef SETUP_SLAVE_PTY
1254 #endif /* SETUP_SLAVE_PTY */
1256 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
1257 Now reenable it in the child, so it will die when we want it to. */
1259 signal (SIGHUP
, SIG_DFL
);
1261 #endif /* HAVE_PTYS */
1266 #else /* not BSD4_1 */
1267 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1268 sigsetmask (SIGEMPTYMASK
);
1269 #else /* ordinary USG */
1271 signal (SIGCHLD
, sigchld
);
1273 #endif /* ordinary USG */
1274 #endif /* not BSD4_1 */
1275 #endif /* SIGCHLD */
1277 child_setup_tty (xforkout
);
1278 child_setup (xforkin
, xforkout
, xforkout
,
1279 new_argv
, 1, current_dir
);
1281 environ
= save_environ
;
1286 remove_process (process
);
1287 report_file_error ("Doing vfork", Qnil
);
1290 XFASTINT (XPROCESS (process
)->pid
) = pid
;
1292 FD_SET (inchannel
, &input_wait_mask
);
1294 /* If the subfork execv fails, and it exits,
1295 this close hangs. I don't know why.
1296 So have an interrupt jar it loose. */
1298 signal (SIGALRM
, create_process_1
);
1301 /* OK to close only if it's not a pty. Otherwise we need to leave
1302 it open for ioctl to get pgrp when signals are sent, or to send
1303 the interrupt characters through if that's how we're signalling
1304 subprocesses. Alternately if you are concerned about running out
1305 of file descriptors, you could just save the tty name and open
1306 just to do the ioctl. */
1307 if (NILP (XFASTINT (XPROCESS (process
)->pty_flag
)))
1310 XPROCESS (process
)->subtty
= Qnil
;
1316 if (forkin
!= forkout
&& forkout
>= 0)
1322 #else /* not BSD4_1 */
1323 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1324 sigsetmask (SIGEMPTYMASK
);
1325 #else /* ordinary USG */
1327 signal (SIGCHLD
, sigchld
);
1328 /* Now really handle any of these signals
1329 that came in during this function. */
1330 if (sigchld_deferred
)
1331 kill (getpid (), SIGCHLD
);
1333 #endif /* ordinary USG */
1334 #endif /* not BSD4_1 */
1335 #endif /* SIGCHLD */
1337 #endif /* not VMS */
1341 /* open a TCP network connection to a given HOST/SERVICE. Treated
1342 exactly like a normal process when reading and writing. Only
1343 differences are in status display and process deletion. A network
1344 connection has no PID; you cannot signal it. All you can do is
1345 deactivate and close it via delete-process */
1347 DEFUN ("open-network-stream", Fopen_network_stream
, Sopen_network_stream
,
1349 "Open a TCP connection for a service to a host.\n\
1350 Returns a subprocess-object to represent the connection.\n\
1351 Input and output work as for subprocesses; `delete-process' closes it.\n\
1352 Args are NAME BUFFER HOST SERVICE.\n\
1353 NAME is name for process. It is modified if necessary to make it unique.\n\
1354 BUFFER is the buffer (or buffer-name) to associate with the process.\n\
1355 Process output goes at end of that buffer, unless you specify\n\
1356 an output stream or filter function to handle the output.\n\
1357 BUFFER may be also nil, meaning that this process is not associated\n\
1359 Third arg is name of the host to connect to, or its IP address.\n\
1360 Fourth arg SERVICE is name of the service desired, or an integer\n\
1361 specifying a port number to connect to.")
1362 (name
, buffer
, host
, service
)
1363 Lisp_Object name
, buffer
, host
, service
;
1367 struct sockaddr_in address
;
1368 struct servent
*svc_info
;
1369 struct hostent
*host_info_ptr
, host_info
;
1370 char *(addr_list
[2]);
1371 unsigned long numeric_addr
;
1375 struct hostent host_info_fixed
;
1376 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1378 GCPRO4 (name
, buffer
, host
, service
);
1379 CHECK_STRING (name
, 0);
1380 CHECK_STRING (host
, 0);
1381 if (XTYPE(service
) == Lisp_Int
)
1382 port
= htons ((unsigned short) XINT (service
));
1385 CHECK_STRING (service
, 0);
1386 svc_info
= getservbyname (XSTRING (service
)->data
, "tcp");
1388 error ("Unknown service \"%s\"", XSTRING (service
)->data
);
1389 port
= svc_info
->s_port
;
1392 host_info_ptr
= gethostbyname (XSTRING (host
)->data
);
1393 if (host_info_ptr
== 0)
1394 /* Attempt to interpret host as numeric inet address */
1396 numeric_addr
= inet_addr (XSTRING (host
)->data
);
1397 if (numeric_addr
== -1)
1398 error ("Unknown host \"%s\"", XSTRING (host
)->data
);
1400 host_info_ptr
= &host_info
;
1401 host_info
.h_name
= 0;
1402 host_info
.h_aliases
= 0;
1403 host_info
.h_addrtype
= AF_INET
;
1404 host_info
.h_addr_list
= &(addr_list
[0]);
1405 addr_list
[0] = (char*)(&numeric_addr
);
1407 host_info
.h_length
= strlen (addr_list
[0]);
1410 bzero (&address
, sizeof address
);
1411 bcopy (host_info_ptr
->h_addr
, (char *) &address
.sin_addr
,
1412 host_info_ptr
->h_length
);
1413 address
.sin_family
= host_info_ptr
->h_addrtype
;
1414 address
.sin_port
= port
;
1416 s
= socket (host_info_ptr
->h_addrtype
, SOCK_STREAM
, 0);
1418 report_file_error ("error creating socket", Fcons (name
, Qnil
));
1421 if (connect (s
, &address
, sizeof address
) == -1)
1428 report_file_error ("connection failed",
1429 Fcons (host
, Fcons (name
, Qnil
)));
1435 report_file_error ("error duplicating socket", Fcons (name
, Qnil
));
1438 buffer
= Fget_buffer_create (buffer
);
1439 proc
= make_process (name
);
1441 chan_process
[inch
] = proc
;
1444 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
1447 fcntl (inch
, F_SETFL
, O_NDELAY
);
1451 XPROCESS (proc
)->childp
= host
;
1452 XPROCESS (proc
)->command_channel_p
= Qnil
;
1453 XPROCESS (proc
)->buffer
= buffer
;
1454 XPROCESS (proc
)->sentinel
= Qnil
;
1455 XPROCESS (proc
)->filter
= Qnil
;
1456 XPROCESS (proc
)->command
= Qnil
;
1457 XPROCESS (proc
)->pid
= Qnil
;
1458 XFASTINT (XPROCESS (proc
)->infd
) = s
;
1459 XFASTINT (XPROCESS (proc
)->outfd
) = outch
;
1460 XPROCESS (proc
)->status
= Qrun
;
1461 FD_SET (inch
, &input_wait_mask
);
1466 #endif /* HAVE_SOCKETS */
1468 deactivate_process (proc
)
1471 register int inchannel
, outchannel
;
1472 register struct Lisp_Process
*p
= XPROCESS (proc
);
1474 inchannel
= XFASTINT (p
->infd
);
1475 outchannel
= XFASTINT (p
->outfd
);
1479 /* Beware SIGCHLD hereabouts. */
1480 flush_pending_output (inchannel
);
1483 VMS_PROC_STUFF
*get_vms_process_pointer (), *vs
;
1484 sys$
dassgn (outchannel
);
1485 vs
= get_vms_process_pointer (p
->pid
)
1487 give_back_vms_process_stuff (vs
);
1491 if (outchannel
&& outchannel
!= inchannel
)
1495 XFASTINT (p
->infd
) = 0;
1496 XFASTINT (p
->outfd
) = 0;
1497 chan_process
[inchannel
] = Qnil
;
1498 FD_CLR (inchannel
, &input_wait_mask
);
1502 /* Close all descriptors currently in use for communication
1503 with subprocess. This is used in a newly-forked subprocess
1504 to get rid of irrelevant descriptors. */
1506 close_process_descs ()
1509 for (i
= 0; i
< MAXDESC
; i
++)
1511 Lisp_Object process
;
1512 process
= chan_process
[i
];
1513 if (!NILP (process
))
1515 int in
= XFASTINT (XPROCESS (process
)->infd
);
1516 int out
= XFASTINT (XPROCESS (process
)->outfd
);
1519 if (out
&& in
!= out
)
1525 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
1527 "Allow any pending output from subprocesses to be read by Emacs.\n\
1528 It is read into the process' buffers or given to their filter functions.\n\
1529 Non-nil arg PROCESS means do not return until some output has been received\n\
1531 Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of\n\
1532 seconds and microseconds to wait; return after that much time whether\n\
1533 or not there is input.\n\
1534 Return non-nil iff we received any output before the timeout expired.")
1535 (proc
, timeout
, timeout_msecs
)
1536 register Lisp_Object proc
, timeout
, timeout_msecs
;
1541 if (! NILP (timeout_msecs
))
1543 CHECK_NUMBER (timeout_msecs
, 2);
1544 useconds
= XINT (timeout_msecs
);
1545 if (XTYPE (timeout
) != Lisp_Int
)
1546 XSET (timeout
, Lisp_Int
, 0);
1549 int carry
= useconds
/ 1000000;
1551 XSETINT (timeout
, XINT (timeout
) + carry
);
1552 useconds
-= carry
* 1000000;
1554 /* I think this clause is necessary because C doesn't
1555 guarantee a particular rounding direction for negative
1559 XSETINT (timeout
, XINT (timeout
) - 1);
1560 useconds
+= 1000000;
1567 if (! NILP (timeout
))
1569 CHECK_NUMBER (timeout
, 1);
1570 seconds
= XINT (timeout
);
1583 XFASTINT (proc
) = 0;
1586 (wait_reading_process_input (seconds
, useconds
, proc
, 0)
1590 /* This variable is different from waiting_for_input in keyboard.c.
1591 It is used to communicate to a lisp process-filter/sentinel (via the
1592 function Fwaiting_for_user_input_p below) whether emacs was waiting
1593 for user-input when that process-filter was called.
1594 waiting_for_input cannot be used as that is by definition 0 when
1595 lisp code is being evalled */
1596 static int waiting_for_user_input_p
;
1598 /* Read and dispose of subprocess output while waiting for timeout to
1599 elapse and/or keyboard input to be available.
1602 timeout in seconds, or
1603 zero for no limit, or
1604 -1 means gobble data immediately available but don't wait for any.
1606 read_kbd is a lisp value:
1607 0 to ignore keyboard input, or
1608 1 to return when input is available, or
1609 -1 means caller will actually read the input, so don't throw to
1610 the quit handler, or
1611 a process object, meaning wait until something arrives from that
1612 process. The return value is true iff we read some input from
1615 do_display != 0 means redisplay should be done to show subprocess
1616 output that arrives.
1618 If read_kbd is a pointer to a struct Lisp_Process, then the
1619 function returns true iff we received input from that process
1620 before the timeout elapsed.
1621 Otherwise, return true iff we recieved input from any process. */
1623 wait_reading_process_input (time_limit
, microsecs
, read_kbd
, do_display
)
1624 int time_limit
, microsecs
;
1625 Lisp_Object read_kbd
;
1628 register int channel
, nfds
, m
;
1629 static SELECT_TYPE Available
;
1632 EMACS_TIME timeout
, end_time
, garbage
;
1634 int wait_channel
= 0;
1635 struct Lisp_Process
*wait_proc
= 0;
1636 int got_some_input
= 0;
1638 FD_ZERO (&Available
);
1640 /* If read_kbd is a process to watch, set wait_proc and wait_channel
1642 if (XTYPE (read_kbd
) == Lisp_Process
)
1644 wait_proc
= XPROCESS (read_kbd
);
1645 wait_channel
= XFASTINT (wait_proc
->infd
);
1646 XFASTINT (read_kbd
) = 0;
1649 waiting_for_user_input_p
= XINT (read_kbd
);
1651 /* Since we may need to wait several times,
1652 compute the absolute time to return at. */
1653 if (time_limit
|| microsecs
)
1655 EMACS_GET_TIME (end_time
);
1656 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
1657 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
1662 /* If calling from keyboard input, do not quit
1663 since we want to return C-g as an input character.
1664 Otherwise, do pending quit if requested. */
1665 if (XINT (read_kbd
) >= 0)
1668 /* If status of something has changed, and no input is available,
1669 notify the user of the change right away */
1670 if (update_tick
!= process_tick
&& do_display
)
1672 Atemp
= input_wait_mask
;
1673 EMACS_SET_SECS_USECS (timeout
, 0, 0);
1674 if (select (MAXDESC
, &Atemp
, 0, 0, &timeout
) <= 0)
1678 /* Don't wait for output from a non-running process. */
1679 if (wait_proc
!= 0 && !NILP (wait_proc
->raw_status_low
))
1680 update_status (wait_proc
);
1682 && ! EQ (wait_proc
->status
, Qrun
))
1685 /* Compute time from now till when time limit is up */
1686 /* Exit if already run out */
1687 if (time_limit
== -1)
1689 /* -1 specified for timeout means
1690 gobble output available now
1691 but don't wait at all. */
1693 EMACS_SET_SECS_USECS (timeout
, 0, 0);
1695 else if (time_limit
|| microsecs
)
1697 EMACS_GET_TIME (timeout
);
1698 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
1699 if (EMACS_TIME_NEG_P (timeout
))
1704 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
1707 /* Cause C-g and alarm signals to take immediate action,
1708 and cause input available signals to zero out timeout */
1709 if (XINT (read_kbd
) < 0)
1710 set_waiting_for_input (&timeout
);
1712 /* Wait till there is something to do */
1714 Available
= input_wait_mask
;
1715 if (! XINT (read_kbd
))
1716 FD_CLR (0, &Available
);
1718 /* If frame size has changed or the window is newly mapped,
1719 redisplay now, before we start to wait. There is a race
1720 condition here; if a SIGIO arrives between now and the select
1721 and indicates that a frame is trashed, we lose. */
1723 redisplay_preserve_echo_area ();
1725 if (XINT (read_kbd
) && detect_input_pending ())
1728 nfds
= select (MAXDESC
, &Available
, 0, 0, &timeout
);
1732 /* Make C-g and alarm signals set flags again */
1733 clear_waiting_for_input ();
1735 /* If we woke up due to SIGWINCH, actually change size now. */
1736 do_pending_window_change ();
1738 if (time_limit
&& nfds
== 0) /* timeout elapsed */
1742 if (xerrno
== EINTR
)
1743 FD_ZERO (&Available
);
1745 /* Ultrix select seems to return ENOMEM when it is interrupted.
1746 Treat it just like EINTR. Bleah. -JimB */
1747 else if (xerrno
== ENOMEM
)
1748 FD_ZERO (&Available
);
1751 /* This happens for no known reason on ALLIANT.
1752 I am guessing that this is the right response. -- RMS. */
1753 else if (xerrno
== EFAULT
)
1754 FD_ZERO (&Available
);
1756 else if (xerrno
== EBADF
)
1759 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
1760 the child's closure of the pts gives the parent a SIGHUP, and
1761 the ptc file descriptor is automatically closed,
1762 yielding EBADF here or at select() call above.
1763 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
1764 in m-ibmrt-aix.h), and here we just ignore the select error.
1765 Cleanup occurs c/o status_notify after SIGCLD. */
1766 FD_ZERO (&Available
); /* Cannot depend on values returned */
1772 error("select error: %s", sys_errlist
[xerrno
]);
1775 else if (nfds
> 0 && FD_ISSET (0, &Available
) && interrupt_input
)
1776 /* System sometimes fails to deliver SIGIO. */
1777 kill (getpid (), SIGIO
);
1780 /* Check for keyboard input */
1781 /* If there is any, return immediately
1782 to give it higher priority than subprocesses */
1784 if (XINT (read_kbd
) && detect_input_pending ())
1788 /* If we think we have keyboard input waiting, but didn't get SIGIO
1789 go read it. This can happen with X on BSD after logging out.
1790 In that case, there really is no input and no SIGIO,
1791 but select says there is input. */
1794 if (XINT (read_kbd) && interrupt_input && (Available & fileno (stdin)))
1796 if (XINT (read_kbd
) && interrupt_input
&& (FD_ISSET (fileno (stdin
), &Available
)))
1801 /* Check for connection from other process */
1803 if (Available
& ChannelMask (comm_server
))
1805 Available
&= ~(ChannelMask (comm_server
));
1811 got_some_input
|= nfds
> 0;
1813 /* If checking input just got us a size-change event from X,
1814 obey it now if we should. */
1815 if (XINT (read_kbd
))
1816 do_pending_window_change ();
1818 /* Check for data from a process or a command channel */
1819 for (channel
= FIRST_PROC_DESC
; channel
< MAXDESC
; channel
++)
1821 if (FD_ISSET (channel
, &Available
))
1825 /* If waiting for this channel, arrange to return as
1826 soon as no more input to be processed. No more
1828 if (wait_channel
== channel
)
1834 proc
= chan_process
[channel
];
1839 /* It's a command channel */
1840 if (!NILP (XPROCESS (proc
)->command_channel_p
))
1842 ProcessCommChan (channel
, proc
);
1843 if (NILP (XPROCESS (proc
)->command_channel_p
))
1845 /* It has ceased to be a command channel! */
1846 int bytes_available
;
1847 if (ioctl (channel
, FIONREAD
, &bytes_available
) < 0)
1848 bytes_available
= 0;
1849 if (bytes_available
)
1850 FD_SET (channel
, &Available
);
1856 /* Read data from the process, starting with our
1857 buffered-ahead character if we have one. */
1859 nread
= read_process_output (proc
, channel
);
1862 /* Since read_process_output can run a filter,
1863 which can call accept-process-output,
1864 don't try to read from any other processes
1865 before doing the select again. */
1866 FD_ZERO (&Available
);
1869 redisplay_preserve_echo_area ();
1872 else if (nread
== -1 && errno
== EWOULDBLOCK
)
1876 else if (nread
== -1 && errno
== EAGAIN
)
1880 else if (nread
== -1 && errno
== EAGAIN
)
1882 /* Note that we cannot distinguish between no input
1883 available now and a closed pipe.
1884 With luck, a closed pipe will be accompanied by
1885 subprocess termination and SIGCHLD. */
1886 else if (nread
== 0 && !NETCONN_P (proc
))
1888 #endif /* O_NDELAY */
1889 #endif /* O_NONBLOCK */
1890 #endif /* EWOULDBLOCK */
1892 /* On some OSs with ptys, when the process on one end of
1893 a pty exits, the other end gets an error reading with
1894 errno = EIO instead of getting an EOF (0 bytes read).
1895 Therefore, if we get an error reading and errno =
1896 EIO, just continue, because the child process has
1897 exited and should clean itself up soon (e.g. when we
1899 else if (nread
== -1 && errno
== EIO
)
1901 #endif /* HAVE_PTYS */
1902 /* If we can detect process termination, don't consider the process
1903 gone just because its pipe is closed. */
1905 else if (nread
== 0 && !NETCONN_P (proc
))
1910 /* Preserve status of processes already terminated. */
1911 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
1912 deactivate_process (proc
);
1913 if (!NILP (XPROCESS (proc
)->raw_status_low
))
1914 update_status (XPROCESS (proc
));
1915 if (EQ (XPROCESS (proc
)->status
, Qrun
))
1916 XPROCESS (proc
)->status
1917 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
1920 } /* end for each file descriptor */
1921 } /* end while exit conditions not met */
1923 /* If calling from keyboard input, do not quit
1924 since we want to return C-g as an input character.
1925 Otherwise, do pending quit if requested. */
1926 if (XINT (read_kbd
) >= 0)
1928 /* Prevent input_pending from remaining set if we quit. */
1929 clear_input_pending ();
1933 return got_some_input
;
1936 /* Read pending output from the process channel,
1937 starting with our buffered-ahead character if we have one.
1938 Yield number of characters read.
1940 This function reads at most 1024 characters.
1941 If you want to read all available subprocess output,
1942 you must call it repeatedly until it returns zero. */
1944 read_process_output (proc
, channel
)
1946 register int channel
;
1948 register int nchars
;
1954 register Lisp_Object outstream
;
1955 register struct buffer
*old
= current_buffer
;
1956 register struct Lisp_Process
*p
= XPROCESS (proc
);
1957 register int opoint
;
1960 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
1962 vs
= get_vms_process_pointer (p
->pid
);
1966 return(0); /* Really weird if it does this */
1967 if (!(vs
->iosb
[0] & 1))
1968 return -1; /* I/O error */
1971 error ("Could not get VMS process pointer");
1972 chars
= vs
->inputBuffer
;
1973 nchars
= clean_vms_buffer (chars
, vs
->iosb
[1]);
1976 start_vms_process_read (vs
); /* Crank up the next read on the process */
1977 return 1; /* Nothing worth printing, say we got 1 */
1981 if (proc_buffered_char
[channel
] < 0)
1982 nchars
= read (channel
, chars
, sizeof chars
);
1985 chars
[0] = proc_buffered_char
[channel
];
1986 proc_buffered_char
[channel
] = -1;
1987 nchars
= read (channel
, chars
+ 1, sizeof chars
- 1);
1991 nchars
= nchars
+ 1;
1993 #endif /* not VMS */
1995 if (nchars
<= 0) return nchars
;
1997 outstream
= p
->filter
;
1998 if (!NILP (outstream
))
2000 /* We inhibit quit here instead of just catching it so that
2001 hitting ^G when a filter happens to be running won't screw
2003 int count
= specpdl_ptr
- specpdl
;
2004 specbind (Qinhibit_quit
, Qt
);
2005 call2 (outstream
, proc
, make_string (chars
, nchars
));
2008 start_vms_process_read (vs
);
2014 /* If no filter, write into buffer if it isn't dead. */
2015 if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
2019 Fset_buffer (p
->buffer
);
2022 /* Insert new output into buffer
2023 at the current end-of-output marker,
2024 thus preserving logical ordering of input and output. */
2025 if (XMARKER (p
->mark
)->buffer
)
2026 SET_PT (marker_position (p
->mark
));
2029 if (point
<= opoint
)
2032 tem
= current_buffer
->read_only
;
2033 current_buffer
->read_only
= Qnil
;
2034 /* Insert before markers in case we are inserting where
2035 the buffer's mark is, and the user's next command is Meta-y. */
2036 insert_before_markers (chars
, nchars
);
2037 current_buffer
->read_only
= tem
;
2038 Fset_marker (p
->mark
, make_number (point
), p
->buffer
);
2039 update_mode_lines
++;
2042 set_buffer_internal (old
);
2045 start_vms_process_read (vs
);
2050 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
2052 "Returns non-NIL if emacs is waiting for input from the user.\n\
2053 This is intended for use by asynchronous process output filters and sentinels.")
2056 return ((waiting_for_user_input_p
) ? Qt
: Qnil
);
2059 /* Sending data to subprocess */
2061 jmp_buf send_process_frame
;
2064 send_process_trap ()
2070 longjmp (send_process_frame
, 1);
2073 send_process (proc
, buf
, len
)
2078 /* Don't use register vars; longjmp can lose them. */
2080 unsigned char *procname
= XSTRING (XPROCESS (proc
)->name
)->data
;
2084 struct Lisp_Process
*p
= XPROCESS (proc
);
2085 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
2088 if (! NILP (XPROCESS (proc
)->raw_status_low
))
2089 update_status (XPROCESS (proc
));
2090 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
2091 error ("Process %s not running", procname
);
2094 vs
= get_vms_process_pointer (p
->pid
);
2096 error ("Could not find this process: %x", p
->pid
);
2097 else if (write_to_vms_process (vs
, buf
, len
))
2100 if (!setjmp (send_process_frame
))
2104 SIGTYPE (*old_sigpipe
)();
2106 /* Don't send more than 500 bytes at a time. */
2109 old_sigpipe
= (SIGTYPE (*) ()) signal (SIGPIPE
, send_process_trap
);
2110 rv
= write (XFASTINT (XPROCESS (proc
)->outfd
), buf
, this);
2111 signal (SIGPIPE
, old_sigpipe
);
2116 || errno
== EWOULDBLOCK
2123 /* It would be nice to accept process output here,
2124 but that is difficult. For example, it could
2125 garbage what we are sending if that is from a buffer. */
2132 report_file_error ("writing to process", Fcons (proc
, Qnil
));
2136 /* Allow input from processes between bursts of sending.
2137 Otherwise things may get stopped up. */
2142 XFASTINT (zero
) = 0;
2143 wait_reading_process_input (-1, 0, zero
, 0);
2149 XPROCESS (proc
)->raw_status_low
= Qnil
;
2150 XPROCESS (proc
)->raw_status_high
= Qnil
;
2151 XPROCESS (proc
)->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
2152 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
2153 deactivate_process (proc
);
2155 error ("Error writing to process %s; closed it", procname
);
2157 error ("SIGPIPE raised on process %s; closed it", procname
);
2162 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
2164 "Send current contents of region as input to PROCESS.\n\
2165 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
2166 nil, indicating the current buffer's process.\n\
2167 Called from program, takes three arguments, PROCESS, START and END.\n\
2168 If the region is more than 500 characters long,\n\
2169 it is sent in several bunches. This may happen even for shorter regions.\n\
2170 Output from processes can arrive in between bunches.")
2171 (process
, start
, end
)
2172 Lisp_Object process
, start
, end
;
2177 proc
= get_process (process
);
2178 validate_region (&start
, &end
);
2180 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
2183 start1
= XINT (start
);
2184 send_process (proc
, &FETCH_CHAR (start1
), XINT (end
) - XINT (start
));
2189 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
2191 "Send PROCESS the contents of STRING as input.\n\
2192 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
2193 nil, indicating the current buffer's process.\n\
2194 If STRING is more than 500 characters long,\n\
2195 it is sent in several bunches. This may happen even for shorter strings.\n\
2196 Output from processes can arrive in between bunches.")
2198 Lisp_Object process
, string
;
2201 CHECK_STRING (string
, 1);
2202 proc
= get_process (process
);
2203 send_process (proc
, XSTRING (string
)->data
, XSTRING (string
)->size
);
2207 /* send a signal number SIGNO to PROCESS.
2208 CURRENT_GROUP means send to the process group that currently owns
2209 the terminal being used to communicate with PROCESS.
2210 This is used for various commands in shell mode.
2211 If NOMSG is zero, insert signal-announcements into process's buffers
2215 process_send_signal (process
, signo
, current_group
, nomsg
)
2216 Lisp_Object process
;
2218 Lisp_Object current_group
;
2222 register struct Lisp_Process
*p
;
2226 proc
= get_process (process
);
2227 p
= XPROCESS (proc
);
2229 if (!EQ (p
->childp
, Qt
))
2230 error ("Process %s is not a subprocess",
2231 XSTRING (p
->name
)->data
);
2232 if (!XFASTINT (p
->infd
))
2233 error ("Process %s is not active",
2234 XSTRING (p
->name
)->data
);
2236 if (NILP (p
->pty_flag
))
2237 current_group
= Qnil
;
2239 /* If we are using pgrps, get a pgrp number and make it negative. */
2240 if (!NILP (current_group
))
2242 /* If possible, send signals to the entire pgrp
2243 by sending an input character to it. */
2244 #if defined (TIOCGLTC) && defined (TIOCGETC)
2251 ioctl (XFASTINT (p
->infd
), TIOCGETC
, &c
);
2252 send_process (proc
, &c
.t_intrc
, 1);
2255 ioctl (XFASTINT (p
->infd
), TIOCGETC
, &c
);
2256 send_process (proc
, &c
.t_quitc
, 1);
2260 ioctl (XFASTINT (p
->infd
), TIOCGLTC
, &lc
);
2261 send_process (proc
, &lc
.t_suspc
, 1);
2263 #endif /* SIGTSTP */
2265 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
2266 /* It is possible that the following code would work
2267 on other kinds of USG systems, not just on the IRIS.
2268 This should be tried in Emacs 19. */
2274 ioctl (XFASTINT (p
->infd
), TCGETA
, &t
);
2275 send_process (proc
, &t
.c_cc
[VINTR
], 1);
2278 ioctl (XFASTINT (p
->infd
), TCGETA
, &t
);
2279 send_process (proc
, &t
.c_cc
[VQUIT
], 1);
2283 ioctl (XFASTINT (p
->infd
), TCGETA
, &t
);
2284 send_process (proc
, &t
.c_cc
[VSWTCH
], 1);
2288 #endif /* ! defined (USG) */
2291 /* Get the pgrp using the tty itself, if we have that.
2292 Otherwise, use the pty to get the pgrp.
2293 On pfa systems, saka@pfu.fujitsu.co.JP writes:
2294 "TICGPGRP symbol defined in sys/ioctl.h at E50.
2295 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
2296 His patch indicates that if TIOCGPGRP returns an error, then
2297 we should just assume that p->pid is also the process group id. */
2301 if (!NILP (p
->subtty
))
2302 err
= ioctl (XFASTINT (p
->subtty
), TIOCGPGRP
, &gid
);
2304 err
= ioctl (XFASTINT (p
->infd
), TIOCGPGRP
, &gid
);
2308 gid
= - XFASTINT (p
->pid
);
2309 #endif /* ! defined (pfa) */
2315 #else /* ! defined (TIOCGPGRP ) */
2316 /* Can't select pgrps on this system, so we know that
2317 the child itself heads the pgrp. */
2318 gid
= - XFASTINT (p
->pid
);
2319 #endif /* ! defined (TIOCGPGRP ) */
2322 gid
= - XFASTINT (p
->pid
);
2328 p
->raw_status_low
= Qnil
;
2329 p
->raw_status_high
= Qnil
;
2331 XSETINT (p
->tick
, ++process_tick
);
2335 #endif /* ! defined (SIGCONT) */
2338 send_process (proc
, "\003", 1); /* ^C */
2343 send_process (proc
, "\031", 1); /* ^Y */
2348 sys$
forcex (&(XFASTINT (p
->pid
)), 0, 1);
2351 flush_pending_output (XFASTINT (p
->infd
));
2355 /* If we don't have process groups, send the signal to the immediate
2356 subprocess. That isn't really right, but it's better than any
2357 obvious alternative. */
2360 kill (XFASTINT (p
->pid
), signo
);
2364 /* gid may be a pid, or minus a pgrp's number */
2366 if (!NILP (current_group
))
2367 ioctl (XFASTINT (p
->infd
), TIOCSIGSEND
, signo
);
2370 gid
= - XFASTINT (p
->pid
);
2373 #else /* ! defined (TIOCSIGSEND) */
2374 EMACS_KILLPG (-gid
, signo
);
2375 #endif /* ! defined (TIOCSIGSEND) */
2378 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
2379 "Interrupt process PROCESS. May be process or name of one.\n\
2380 PROCESS may be a process, a buffer, or the name of a process or buffer.\n\
2381 Nil or no arg means current buffer's process.\n\
2382 Second arg CURRENT-GROUP non-nil means send signal to\n\
2383 the current process-group of the process's controlling terminal\n\
2384 rather than to the process's own process group.\n\
2385 If the process is a shell, this means interrupt current subjob\n\
2386 rather than the shell.")
2387 (process
, current_group
)
2388 Lisp_Object process
, current_group
;
2390 process_send_signal (process
, SIGINT
, current_group
, 0);
2394 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
2395 "Kill process PROCESS. May be process or name of one.\n\
2396 See function `interrupt-process' for more details on usage.")
2397 (process
, current_group
)
2398 Lisp_Object process
, current_group
;
2400 process_send_signal (process
, SIGKILL
, current_group
, 0);
2404 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
2405 "Send QUIT signal to 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
, SIGQUIT
, current_group
, 0);
2414 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
2415 "Stop 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
;
2421 error ("no SIGTSTP support");
2423 process_send_signal (process
, SIGTSTP
, current_group
, 0);
2428 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
2429 "Continue process PROCESS. May be process or name of one.\n\
2430 See function `interrupt-process' for more details on usage.")
2431 (process
, current_group
)
2432 Lisp_Object process
, current_group
;
2435 process_send_signal (process
, SIGCONT
, current_group
, 0);
2437 error ("no SIGCONT support");
2442 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
2443 2, 2, "nProcess number: \nnSignal code: ",
2444 "Send the process with number PID the signal with code CODE.\n\
2445 Both PID and CODE are integers.")
2447 Lisp_Object pid
, sig
;
2449 CHECK_NUMBER (pid
, 0);
2450 CHECK_NUMBER (sig
, 1);
2451 return make_number (kill (XINT (pid
), XINT (sig
)));
2454 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
2455 "Make PROCESS see end-of-file in its input.\n\
2456 Eof comes after any text already sent to it.\n\
2457 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
2458 nil, indicating the current buffer's process.")
2460 Lisp_Object process
;
2464 proc
= get_process (process
);
2465 /* Sending a zero-length record is supposed to mean eof
2466 when TIOCREMOTE is turned on. */
2470 write (XFASTINT (XPROCESS (proc
)->outfd
), buf
, 0);
2472 #else /* did not do TOICREMOTE */
2474 send_process (proc
, "\032", 1); /* ^z */
2476 if (!NILP (XPROCESS (proc
)->pty_flag
))
2477 send_process (proc
, "\004", 1);
2480 close (XPROCESS (proc
)->outfd
);
2481 XFASTINT (XPROCESS (proc
)->outfd
) = open ("/dev/null", O_WRONLY
);
2484 #endif /* did not do TOICREMOTE */
2488 /* Kill all processes associated with `buffer'.
2489 If `buffer' is nil, kill all processes */
2491 kill_buffer_processes (buffer
)
2494 Lisp_Object tail
, proc
;
2496 for (tail
= Vprocess_alist
; XGCTYPE (tail
) == Lisp_Cons
;
2497 tail
= XCONS (tail
)->cdr
)
2499 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
2500 if (XGCTYPE (proc
) == Lisp_Process
2501 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
2503 if (NETCONN_P (proc
))
2504 deactivate_process (proc
);
2505 else if (XFASTINT (XPROCESS (proc
)->infd
))
2506 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
2511 /* On receipt of a signal that a child status has changed,
2512 loop asking about children with changed statuses until
2513 the system says there are no more.
2514 All we do is change the status;
2515 we do not run sentinels or print notifications.
2516 That is saved for the next time keyboard input is done,
2517 in order to avoid timing errors. */
2519 /** WARNING: this can be called during garbage collection.
2520 Therefore, it must not be fooled by the presence of mark bits in
2523 /** USG WARNING: Although it is not obvious from the documentation
2524 in signal(2), on a USG system the SIGCLD handler MUST NOT call
2525 signal() before executing at least one wait(), otherwise the handler
2526 will be called again, resulting in an infinite loop. The relevant
2527 portion of the documentation reads "SIGCLD signals will be queued
2528 and the signal-catching function will be continually reentered until
2529 the queue is empty". Invoking signal() causes the kernel to reexamine
2530 the SIGCLD queue. Fred Fish, UniSoft Systems Inc. */
2533 sigchld_handler (signo
)
2536 int old_errno
= errno
;
2538 register struct Lisp_Process
*p
;
2542 sigheld
|= sigbit (SIGCHLD
);
2554 #endif /* no WUNTRACED */
2555 /* Keep trying to get a status until we get a definitive result. */
2559 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
2561 while (pid
<= 0 && errno
== EINTR
);
2565 /* A real failure. We have done all our job, so return. */
2567 /* USG systems forget handlers when they are used;
2568 must reestablish each time */
2570 signal (signo
, sigchld_handler
); /* WARNING - must come after wait3() */
2573 sigheld
&= ~sigbit (SIGCHLD
);
2581 #endif /* no WNOHANG */
2583 /* Find the process that signaled us, and record its status. */
2586 for (tail
= Vprocess_alist
; XSYMBOL (tail
) != XSYMBOL (Qnil
); tail
= XCONS (tail
)->cdr
)
2588 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
2589 p
= XPROCESS (proc
);
2590 if (EQ (p
->childp
, Qt
) && XFASTINT (p
->pid
) == pid
)
2595 /* Look for an asynchronous process whose pid hasn't been filled
2598 for (tail
= Vprocess_alist
; XSYMBOL (tail
) != XSYMBOL (Qnil
); tail
= XCONS (tail
)->cdr
)
2600 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
2601 p
= XPROCESS (proc
);
2602 if (XTYPE (p
->pid
) == Lisp_Int
&& XINT (p
->pid
) == -1)
2607 /* Change the status of the process that was found. */
2610 union { int i
; WAITTYPE wt
; } u
;
2612 XSETINT (p
->tick
, ++process_tick
);
2614 XFASTINT (p
->raw_status_low
) = u
.i
& 0xffff;
2615 XFASTINT (p
->raw_status_high
) = u
.i
>> 16;
2617 /* If process has terminated, stop waiting for its output. */
2618 if (WIFSIGNALED (w
) || WIFEXITED (w
))
2619 if (XFASTINT (p
->infd
))
2620 FD_CLR (XFASTINT (p
->infd
), &input_wait_mask
);
2623 /* There was no asynchronous process found for that id. Check
2624 if we have a synchronous process. */
2627 synch_process_alive
= 0;
2629 /* Report the status of the synchronous process. */
2631 synch_process_retcode
= WRETCODE (w
);
2632 else if (WIFSIGNALED (w
))
2633 synch_process_death
= sys_siglist
[WTERMSIG (w
)];
2636 /* On some systems, we must return right away.
2637 If any more processes want to signal us, we will
2639 Otherwise (on systems that have WNOHANG), loop around
2640 to use up all the processes that have something to tell us. */
2641 #if defined (USG) && ! (defined (HPUX) && defined (WNOHANG))
2643 signal (signo
, sigchld_handler
);
2647 #endif /* USG, but not HPUX with WNOHANG */
2653 exec_sentinel_unwind (data
)
2656 XPROCESS (XCONS (data
)->car
)->sentinel
= XCONS (data
)->cdr
;
2661 exec_sentinel (proc
, reason
)
2662 Lisp_Object proc
, reason
;
2664 Lisp_Object sentinel
;
2665 register struct Lisp_Process
*p
= XPROCESS (proc
);
2666 int count
= specpdl_ptr
- specpdl
;
2668 sentinel
= p
->sentinel
;
2669 if (NILP (sentinel
))
2672 /* Zilch the sentinel while it's running, to avoid recursive invocations;
2673 assure that it gets restored no matter how the sentinel exits. */
2675 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
2676 /* Inhibit quit so that random quits don't screw up a running filter. */
2677 specbind (Qinhibit_quit
, Qt
);
2678 call2 (sentinel
, proc
, reason
);
2682 /* Report all recent events of a change in process status
2683 (either run the sentinel or output a message).
2684 This is done while Emacs is waiting for keyboard input. */
2688 register Lisp_Object proc
, buffer
;
2689 Lisp_Object tail
= Qnil
;
2690 Lisp_Object msg
= Qnil
;
2691 struct gcpro gcpro1
, gcpro2
;
2693 /* We need to gcpro tail; if read_process_output calls a filter
2694 which deletes a process and removes the cons to which tail points
2695 from Vprocess_alist, and then causes a GC, tail is an unprotected
2699 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
2702 register struct Lisp_Process
*p
;
2704 proc
= Fcdr (Fcar (tail
));
2705 p
= XPROCESS (proc
);
2707 if (XINT (p
->tick
) != XINT (p
->update_tick
))
2709 XSETINT (p
->update_tick
, XINT (p
->tick
));
2711 /* If process is still active, read any output that remains. */
2712 if (XFASTINT (p
->infd
))
2713 while (read_process_output (proc
, XFASTINT (p
->infd
)) > 0);
2717 /* Get the text to use for the message. */
2718 if (!NILP (p
->raw_status_low
))
2720 msg
= status_message (p
->status
);
2722 /* If process is terminated, deactivate it or delete it. */
2724 if (XTYPE (p
->status
) == Lisp_Cons
)
2725 symbol
= XCONS (p
->status
)->car
;
2727 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
2728 || EQ (symbol
, Qclosed
))
2730 if (delete_exited_processes
)
2731 remove_process (proc
);
2733 deactivate_process (proc
);
2736 /* Now output the message suitably. */
2737 if (!NILP (p
->sentinel
))
2738 exec_sentinel (proc
, msg
);
2739 /* Don't bother with a message in the buffer
2740 when a process becomes runnable. */
2741 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
2743 Lisp_Object ro
= XBUFFER (buffer
)->read_only
;
2745 struct buffer
*old
= current_buffer
;
2748 /* Avoid error if buffer is deleted
2749 (probably that's why the process is dead, too) */
2750 if (NILP (XBUFFER (buffer
)->name
))
2752 Fset_buffer (buffer
);
2754 /* Insert new output into buffer
2755 at the current end-of-output marker,
2756 thus preserving logical ordering of input and output. */
2757 if (XMARKER (p
->mark
)->buffer
)
2758 SET_PT (marker_position (p
->mark
));
2761 if (point
<= opoint
)
2762 opoint
+= XSTRING (msg
)->size
+ XSTRING (p
->name
)->size
+ 10;
2764 tem
= current_buffer
->read_only
;
2765 current_buffer
->read_only
= Qnil
;
2766 insert_string ("\nProcess ");
2767 Finsert (1, &p
->name
);
2768 insert_string (" ");
2770 current_buffer
->read_only
= tem
;
2771 Fset_marker (p
->mark
, make_number (point
), p
->buffer
);
2774 set_buffer_internal (old
);
2779 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
2780 redisplay_preserve_echo_area ();
2782 update_tick
= process_tick
;
2793 if (! noninteractive
|| initialized
)
2795 signal (SIGCHLD
, sigchld_handler
);
2798 FD_ZERO (&input_wait_mask
);
2799 FD_SET (0, &input_wait_mask
);
2800 Vprocess_alist
= Qnil
;
2801 for (i
= 0; i
< MAXDESC
; i
++)
2803 chan_process
[i
] = Qnil
;
2804 proc_buffered_char
[i
] = -1;
2808 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 0, 1, 0,
2809 "Return the connection type of `PROCESS'. This can be nil (pipe),\n\
2810 t or pty (pty) or stream (socket connection).")
2812 Lisp_Object process
;
2814 return XPROCESS (process
)->type
;
2820 stream_process
= intern ("stream");
2822 Qprocessp
= intern ("processp");
2823 staticpro (&Qprocessp
);
2824 Qrun
= intern ("run");
2826 Qstop
= intern ("stop");
2828 Qsignal
= intern ("signal");
2829 staticpro (&Qsignal
);
2831 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
2834 Qexit = intern ("exit");
2835 staticpro (&Qexit); */
2837 Qopen
= intern ("open");
2839 Qclosed
= intern ("closed");
2840 staticpro (&Qclosed
);
2842 staticpro (&Vprocess_alist
);
2844 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
2845 "*Non-nil means delete processes immediately when they exit.\n\
2846 nil means don't delete them until `list-processes' is run.");
2848 delete_exited_processes
= 1;
2850 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
2851 "Control type of device used to communicate with subprocesses.\n\
2852 Values are nil to use a pipe, and t or 'pty for a pty. Note that if\n\
2853 pty's are not available, this variable will be ignored. The value takes\n\
2854 effect when `start-process' is called.");
2855 Vprocess_connection_type
= Qt
;
2857 defsubr (&Sprocessp
);
2858 defsubr (&Sget_process
);
2859 defsubr (&Sget_buffer_process
);
2860 defsubr (&Sdelete_process
);
2861 defsubr (&Sprocess_status
);
2862 defsubr (&Sprocess_exit_status
);
2863 defsubr (&Sprocess_id
);
2864 defsubr (&Sprocess_name
);
2865 defsubr (&Sprocess_command
);
2866 defsubr (&Sset_process_buffer
);
2867 defsubr (&Sprocess_buffer
);
2868 defsubr (&Sprocess_mark
);
2869 defsubr (&Sset_process_filter
);
2870 defsubr (&Sprocess_filter
);
2871 defsubr (&Sset_process_sentinel
);
2872 defsubr (&Sprocess_sentinel
);
2873 defsubr (&Sprocess_kill_without_query
);
2874 defsubr (&Slist_processes
);
2875 defsubr (&Sprocess_list
);
2876 defsubr (&Sstart_process
);
2878 defsubr (&Sopen_network_stream
);
2879 #endif /* HAVE_SOCKETS */
2880 defsubr (&Saccept_process_output
);
2881 defsubr (&Sprocess_send_region
);
2882 defsubr (&Sprocess_send_string
);
2883 defsubr (&Sinterrupt_process
);
2884 defsubr (&Skill_process
);
2885 defsubr (&Squit_process
);
2886 defsubr (&Sstop_process
);
2887 defsubr (&Scontinue_process
);
2888 defsubr (&Sprocess_send_eof
);
2889 defsubr (&Ssignal_process
);
2890 defsubr (&Swaiting_for_user_input_p
);
2891 /* defsubr (&Sprocess_connection); */
2895 #else /* not subprocesses */
2897 #include <sys/types.h>
2901 #include "systime.h"
2902 #include "termopts.h"
2904 extern int frame_garbaged
;
2907 /* As described above, except assuming that there are no subprocesses:
2909 Wait for timeout to elapse and/or keyboard input to be available.
2912 timeout in seconds, or
2913 zero for no limit, or
2914 -1 means gobble data immediately available but don't wait for any.
2916 read_kbd is a Lisp_Object:
2917 0 to ignore keyboard input, or
2918 1 to return when input is available, or
2919 -1 means caller will actually read the input, so don't throw to
2921 We know that read_kbd will never be a Lisp_Process, since
2922 `subprocesses' isn't defined.
2924 do_display != 0 means redisplay should be done to show subprocess
2925 output that arrives. This version of the function ignores it.
2927 Return true iff we recieved input from any process. */
2930 wait_reading_process_input (time_limit
, microsecs
, read_kbd
, do_display
)
2931 int time_limit
, microsecs
;
2932 Lisp_Object read_kbd
;
2935 EMACS_TIME end_time
, timeout
, *timeout_p
;
2938 /* What does time_limit really mean? */
2939 if (time_limit
|| microsecs
)
2941 /* It's not infinite. */
2942 timeout_p
= &timeout
;
2944 if (time_limit
== -1)
2945 /* In fact, it's zero. */
2946 EMACS_SET_SECS_USECS (timeout
, 0, 0);
2948 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
2950 /* How far in the future is that? */
2951 EMACS_GET_TIME (end_time
);
2952 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
2955 /* It's infinite. */
2958 /* Turn off periodic alarms (in case they are in use)
2959 because the select emulator uses alarms. */
2966 waitchannels
= XINT (read_kbd
) ? 1 : 0;
2968 /* If calling from keyboard input, do not quit
2969 since we want to return C-g as an input character.
2970 Otherwise, do pending quit if requested. */
2971 if (XINT (read_kbd
) >= 0)
2976 EMACS_GET_TIME (*timeout_p
);
2977 EMACS_SUB_TIME (*timeout_p
, end_time
, *timeout_p
);
2978 if (EMACS_TIME_NEG_P (*timeout_p
))
2982 /* Cause C-g and alarm signals to take immediate action,
2983 and cause input available signals to zero out timeout. */
2984 if (XINT (read_kbd
) < 0)
2985 set_waiting_for_input (&timeout
);
2987 /* If a frame has been newly mapped and needs updating,
2988 reprocess its display stuff. */
2990 redisplay_preserve_echo_area ();
2992 if (XINT (read_kbd
) && detect_input_pending ())
2995 nfds
= select (1, &waitchannels
, 0, 0, timeout_p
);
2997 /* Make C-g and alarm signals set flags again */
2998 clear_waiting_for_input ();
3000 /* If we woke up due to SIGWINCH, actually change size now. */
3001 do_pending_window_change ();
3005 /* If the system call was interrupted, then go around the
3011 else if (nfds
> 0 && (waitchannels
& 1) && interrupt_input
)
3012 /* System sometimes fails to deliver SIGIO. */
3013 kill (getpid (), SIGIO
);
3015 if (XINT (read_kbd
) && interrupt_input
&& (waitchannels
& 1))
3018 /* If we have timed out (nfds == 0) or found some input (nfds > 0),
3028 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
3029 "Return the (or, a) process associated with BUFFER.\n\
3030 This copy of Emacs has not been built to support subprocesses, so this\n\
3031 function always returns nil.")
3033 register Lisp_Object name
;
3038 /* Kill all processes associated with `buffer'.
3039 If `buffer' is nil, kill all processes.
3040 Since we have no subprocesses, this does nothing. */
3042 kill_buffer_processes (buffer
)
3053 defsubr (&Sget_buffer_process
);
3057 #endif /* not subprocesses */