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. */
26 /* The entire file is within this conditional */
31 #include <sys/types.h> /* some typedefs are used in sys/file.h */
35 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
36 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <arpa/inet.h>
40 #endif /* HAVE_SOCKETS */
42 #if defined(BSD) || defined(STRIDE)
43 #include <sys/ioctl.h>
44 #if !defined (O_NDELAY) && defined (HAVE_PTYS)
46 #endif /* HAVE_PTYS and no O_NDELAY */
47 #endif /* BSD or STRIDE */
66 #include <sys/sysmacros.h> /* for "minor" */
71 #if defined (HPUX) && defined (HAVE_PTYS)
72 #include <sys/ptyio.h>
83 #include <sys/ttyhw.h>
84 #include <sys/stream.h>
90 #undef TIOCGETC /* Avoid confusing some conditionals that test this. */
93 #ifdef BROKEN_TIOCGETC
101 #include "termhooks.h"
102 #include "termopts.h"
103 #include "commands.h"
105 extern int screen_garbaged
;
107 Lisp_Object Qrun
, Qstop
, Qsignal
, Qopen
, Qclosed
;
108 /* Qexit is declared and initialized in eval.c. */
110 /* a process object is a network connection when its childp field is neither
111 Qt nor Qnil but is instead a string (name of foreign host we
112 are connected to + name of port we are connected to) */
115 static Lisp_Object stream_process
;
117 #define NETCONN_P(p) (XGCTYPE (XPROCESS (p)->childp) == Lisp_String)
119 #define NETCONN_P(p) 0
120 #endif /* HAVE_SOCKETS */
122 /* Define first descriptor number available for subprocesses. */
124 #define FIRST_PROC_DESC 1
126 #define FIRST_PROC_DESC 3
129 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
132 #if !defined (SIGCHLD) && defined (SIGCLD)
133 #define SIGCHLD SIGCLD
136 #include "syssignal.h"
138 /* Define the structure that the wait system call stores.
139 On many systems, there is a structure defined for this.
140 But on vanilla-ish USG systems there is not. */
144 #if !defined (BSD) && !defined (UNIPLUS) && !defined (STRIDE) && !(defined (HPUX) && !defined (NOMULTIPLEJOBS)) && !defined (HAVE_WAIT_HEADER)
147 #define WIFSTOPPED(w) ((w&0377) == 0177)
148 #define WIFSIGNALED(w) ((w&0377) != 0177 && (w&~0377) == 0)
149 #define WIFEXITED(w) ((w&0377) == 0)
150 #define WRETCODE(w) (w >> 8)
151 #define WSTOPSIG(w) (w >> 8)
152 #define WCOREDUMP(w) ((w&0200) != 0)
153 #define WTERMSIG(w) (w & 0377)
158 #include <sys/wait.h>
159 #endif /* not BSD 4.1 */
161 #define WAITTYPE union wait
162 #define WRETCODE(w) w.w_retcode
163 #define WCOREDUMP(w) w.w_coredump
166 /* HPUX version 7 has broken definitions of these. */
175 #define WTERMSIG(w) w.w_termsig
178 #define WSTOPSIG(w) w.w_stopsig
181 #define WIFSTOPPED(w) (WTERMSIG (w) == 0177)
184 #define WIFSIGNALED(w) (WTERMSIG (w) != 0177 && (WSTOPSIG (w)) == 0)
187 #define WIFEXITED(w) (WTERMSIG (w) == 0)
189 #endif /* BSD or UNIPLUS or STRIDE */
190 #endif /* no WAITTYPE */
193 /* For the CMU PTY driver + */
194 #define DCL_PROMPT "$ "
204 extern char *sys_errlist
[];
208 extern char *sys_siglist
[];
210 char *sys_siglist
[] =
216 "illegal instruction",
220 "floating point exception",
223 "segmentation violation",
224 "bad argument to system call",
225 "write on a pipe with no one to read it",
227 "software termination signal from kill",
229 "sendable stop signal not from tty",
230 "stop signal from tty",
231 "continue a stopped process",
232 "child status has changed",
233 "background read attempted from control tty",
234 "background write attempted from control tty",
235 "input record available at control tty",
236 "exceeded CPU time limit",
237 "exceeded file size limit"
245 extern int comm_server
;
246 extern int net_listen_address
;
249 /* t means use pty, nil means use a pipe,
250 maybe other values to come. */
251 Lisp_Object Vprocess_connection_type
;
255 #include <sys/socket.h>
259 /* Number of events of change of status of a process. */
262 /* Number of events for which the user or sentinel has been notified. */
266 /* We could get this from param.h, but better not to depend on finding that.
267 And better not to risk that it might define other symbols used in this
270 #define SELECT_TYPE fd_set
271 #else /* no FD_SET */
273 #define SELECT_TYPE int
275 /* Define the macros to access a single-int bitmap of descriptors. */
276 #define FD_SET(n, p) (*(p) |= (1 << (n)))
277 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
278 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
279 #define FD_ZERO(p) (*(p) = 0)
280 #endif /* no FD_SET */
282 /* Mask of bits indicating the descriptors that we wait for input on */
284 SELECT_TYPE input_wait_mask
;
286 int delete_exited_processes
;
288 /* Indexed by descriptor, gives the process (if any) for that descriptor */
289 Lisp_Object chan_process
[MAXDESC
];
291 /* Alist of elements (NAME . PROCESS) */
292 Lisp_Object Vprocess_alist
;
294 Lisp_Object Qprocessp
;
296 Lisp_Object
get_process ();
298 /* Buffered-ahead input char from process, indexed by channel.
299 -1 means empty (no char is buffered).
300 Used on sys V where the only way to tell if there is any
301 output from the process is to read at least one char.
302 Always -1 on systems that support FIONREAD. */
304 int proc_buffered_char
[MAXDESC
];
306 /* Compute the Lisp form of the process status, p->status, from
307 the numeric status that was returned by `wait'. */
310 struct Lisp_Process
*p
;
312 union { int i
; WAITTYPE wt
; } u
;
313 u
.i
= XFASTINT (p
->raw_status_low
) + (XFASTINT (p
->raw_status_high
) << 16);
314 p
->status
= status_convert (u
.wt
);
315 p
->raw_status_low
= Qnil
;
316 p
->raw_status_high
= Qnil
;
319 /* Convert a process status work in Unix format to
320 the list that we use internally. */
327 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
328 else if (WIFEXITED (w
))
329 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
330 WCOREDUMP (w
) ? Qt
: Qnil
));
331 else if (WIFSIGNALED (w
))
332 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
333 WCOREDUMP (w
) ? Qt
: Qnil
));
338 /* Given a status-list, extract the three pieces of information
339 and store them individually through the three pointers. */
342 decode_status (l
, symbol
, code
, coredump
)
350 if (XTYPE (l
) == Lisp_Symbol
)
358 *symbol
= XCONS (l
)->car
;
359 tem
= XCONS (l
)->cdr
;
360 *code
= XFASTINT (XCONS (tem
)->car
);
361 tem
= XFASTINT (XCONS (tem
)->cdr
);
362 *coredump
= !NILP (tem
);
366 /* Return a string describing a process status list. */
369 status_message (status
)
374 Lisp_Object string
, string2
;
376 decode_status (status
, &symbol
, &code
, &coredump
);
378 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
380 string
= build_string (code
< NSIG
? sys_siglist
[code
] : "unknown");
381 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
382 XSTRING (string
)->data
[0] = DOWNCASE (XSTRING (string
)->data
[0]);
383 return concat2 (string
, string2
);
385 else if (EQ (symbol
, Qexit
))
388 return build_string ("finished\n");
389 string
= Fint_to_string (make_number (code
));
390 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
391 return concat2 (build_string ("exited abnormally with code "),
392 concat2 (string
, string2
));
395 return Fcopy_sequence (Fsymbol_name (symbol
));
401 /* Open an available pty, returning a file descriptor.
402 Return -1 on failure.
403 The file name of the terminal corresponding to the pty
404 is left in the variable pty_name. */
418 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
419 for (i
= 0; i
< 16; i
++)
422 #ifdef PTY_NAME_SPRINTF
426 sprintf (pty_name
, "/dev/ptym/pty%c%x", c
, i
);
429 sprintf (pty_name
, "/dev/pty%x", i
);
431 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
433 #endif /* not HPUX */
434 #endif /* no PTY_NAME_SPRINTF */
437 if (stat (pty_name
, &stb
) < 0)
440 fd
= open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
442 fd
= open (pty_name
, O_RDWR
| O_NDELAY
, 0);
444 #else /* Unusual IRIS code */
445 *ptyv
= open ("/dev/ptc", O_RDWR
| O_NDELAY
, 0);
448 if (fstat (fd
, &stb
) < 0)
454 /* check to make certain that both sides are available
455 this avoids a nasty yet stupid bug in rlogins */
456 #ifdef PTY_TTY_NAME_SPRINTF
459 /* TODO: In version 19, make these special cases use the macro above. */
461 sprintf (pty_name
, "/dev/pty/tty%c%x", c
, i
);
464 sprintf (pty_name
, "/dev/ttyp%x", i
);
467 sprintf (pty_name
, "/dev/ttyq%d", minor (stb
.st_rdev
));
469 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
470 #endif /* not IRIS */
472 #endif /* not HPUX */
473 #endif /* no PTY_TTY_NAME_SPRINTF */
475 if (access (pty_name
, 6) != 0)
484 #endif /* not UNIPLUS */
491 #endif /* HAVE_PTYS */
497 register Lisp_Object val
, tem
, name1
;
498 register struct Lisp_Process
*p
;
502 /* size of process structure includes the vector header,
503 so deduct for that. But struct Lisp_Vector includes the first
504 element, thus deducts too much, so add it back. */
505 val
= Fmake_vector (make_number ((sizeof (struct Lisp_Process
)
506 - sizeof (struct Lisp_Vector
)
507 + sizeof (Lisp_Object
))
508 / sizeof (Lisp_Object
)),
510 XSETTYPE (val
, Lisp_Process
);
513 XFASTINT (p
->infd
) = 0;
514 XFASTINT (p
->outfd
) = 0;
515 XFASTINT (p
->pid
) = 0;
516 XFASTINT (p
->tick
) = 0;
517 XFASTINT (p
->update_tick
) = 0;
518 p
->raw_status_low
= Qnil
;
519 p
->raw_status_high
= Qnil
;
521 p
->mark
= Fmake_marker ();
523 /* If name is already in use, modify it until it is unused. */
528 tem
= Fget_process (name1
);
529 if (NILP (tem
)) break;
530 sprintf (suffix
, "<%d>", i
);
531 name1
= concat2 (name
, build_string (suffix
));
535 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
539 remove_process (proc
)
540 register Lisp_Object proc
;
542 register Lisp_Object pair
;
544 pair
= Frassq (proc
, Vprocess_alist
);
545 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
546 Fset_marker (XPROCESS (proc
)->mark
, Qnil
, Qnil
);
548 deactivate_process (proc
);
551 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
552 "Return t if OBJECT is a process.")
556 return XTYPE (obj
) == Lisp_Process
? Qt
: Qnil
;
559 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
560 "Return the process named NAME, or nil if there is none.")
562 register Lisp_Object name
;
564 if (XTYPE (name
) == Lisp_Process
)
566 CHECK_STRING (name
, 0);
567 return Fcdr (Fassoc (name
, Vprocess_alist
));
570 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
571 "Return the (or, a) process associated with BUFFER.\n\
572 BUFFER may be a buffer or the name of one.")
574 register Lisp_Object name
;
576 register Lisp_Object buf
, tail
, proc
;
578 if (NILP (name
)) return Qnil
;
579 buf
= Fget_buffer (name
);
580 if (NILP (buf
)) return Qnil
;
582 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
584 proc
= Fcdr (Fcar (tail
));
585 if (XTYPE (proc
) == Lisp_Process
&& EQ (XPROCESS (proc
)->buffer
, buf
))
591 /* This is how commands for the user decode process arguments */
595 register Lisp_Object name
;
597 register Lisp_Object proc
;
599 proc
= Fget_buffer_process (Fcurrent_buffer ());
602 proc
= Fget_process (name
);
604 proc
= Fget_buffer_process (Fget_buffer (name
));
611 error ("Current buffer has no process");
613 error ("Process %s does not exist", XSTRING (name
)->data
);
617 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
618 "Delete PROCESS: kill it and forget about it immediately.\n\
619 PROCESS may be a process or the name of one, or a buffer name.")
621 register Lisp_Object proc
;
623 proc
= get_process (proc
);
624 XPROCESS (proc
)->raw_status_low
= Qnil
;
625 XPROCESS (proc
)->raw_status_high
= Qnil
;
626 if (NETCONN_P (proc
))
628 XPROCESS (proc
)->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
629 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
631 else if (XFASTINT (XPROCESS (proc
)->infd
))
633 Fkill_process (proc
, Qnil
);
634 /* Do this now, since remove_process will make sigchld_handler do nothing. */
635 XPROCESS (proc
)->status
636 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
637 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
640 remove_process (proc
);
644 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
645 "Return the status of PROCESS: a symbol, one of these:\n\
646 run -- for a process that is running.\n\
647 stop -- for a process stopped but continuable.\n\
648 exit -- for a process that has exited.\n\
649 signal -- for a process that has got a fatal signal.\n\
650 open -- for a network stream connection that is open.\n\
651 closed -- for a network stream connection that is closed.\n\
652 nil -- if arg is a process name and no such process exists.")
653 /* command -- for a command channel opened to Emacs by another process.\n\
654 external -- for an i/o channel opened to Emacs by another process.\n\ */
656 register Lisp_Object proc
;
658 register struct Lisp_Process
*p
;
659 register Lisp_Object status
;
660 proc
= Fget_process (proc
);
664 if (!NILP (p
->raw_status_low
))
667 if (XTYPE (status
) == Lisp_Cons
)
668 status
= XCONS (status
)->car
;
669 if (NETCONN_P (proc
))
671 if (EQ (status
, Qrun
))
673 else if (EQ (status
, Qexit
))
679 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
681 "Return the exit status of PROCESS or the signal number that killed it.\n\
682 If PROCESS has not yet exited or died, return 0.")
684 register Lisp_Object proc
;
686 CHECK_PROCESS (proc
, 0);
687 if (!NILP (XPROCESS (proc
)->raw_status_low
))
688 update_status (XPROCESS (proc
));
689 if (XTYPE (XPROCESS (proc
)->status
) == Lisp_Cons
)
690 return XCONS (XCONS (XPROCESS (proc
)->status
)->cdr
)->car
;
691 return make_number (0);
694 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
695 "Return the process id of PROCESS.\n\
696 This is the pid of the Unix process which PROCESS uses or talks to.\n\
697 For a network connection, this value is nil.")
699 register Lisp_Object proc
;
701 CHECK_PROCESS (proc
, 0);
702 return XPROCESS (proc
)->pid
;
705 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
706 "Return the name of PROCESS, as a string.\n\
707 This is the name of the program invoked in PROCESS,\n\
708 possibly modified to make it unique among process names.")
710 register Lisp_Object proc
;
712 CHECK_PROCESS (proc
, 0);
713 return XPROCESS (proc
)->name
;
716 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
717 "Return the command that was executed to start PROCESS.\n\
718 This is a list of strings, the first string being the program executed\n\
719 and the rest of the strings being the arguments given to it.\n\
720 For a non-child channel, this is nil.")
722 register Lisp_Object proc
;
724 CHECK_PROCESS (proc
, 0);
725 return XPROCESS (proc
)->command
;
728 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
730 "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
732 register Lisp_Object proc
, buffer
;
734 CHECK_PROCESS (proc
, 0);
736 CHECK_BUFFER (buffer
, 1);
737 XPROCESS (proc
)->buffer
= buffer
;
741 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
743 "Return the buffer PROCESS is associated with.\n\
744 Output from PROCESS is inserted in this buffer\n\
745 unless PROCESS has a filter.")
747 register Lisp_Object proc
;
749 CHECK_PROCESS (proc
, 0);
750 return XPROCESS (proc
)->buffer
;
753 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
755 "Return the marker for the end of the last output from PROCESS.")
757 register Lisp_Object proc
;
759 CHECK_PROCESS (proc
, 0);
760 return XPROCESS (proc
)->mark
;
763 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
765 "Give PROCESS the filter function FILTER; nil means no filter.\n\
766 When a process has a filter, each time it does output\n\
767 the entire string of output is passed to the filter.\n\
768 The filter gets two arguments: the process and the string of output.\n\
769 If the process has a filter, its buffer is not used for output.")
771 register Lisp_Object proc
, filter
;
773 CHECK_PROCESS (proc
, 0);
774 XPROCESS (proc
)->filter
= filter
;
778 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
780 "Returns the filter function of PROCESS; nil if none.\n\
781 See `set-process-filter' for more info on filter functions.")
783 register Lisp_Object proc
;
785 CHECK_PROCESS (proc
, 0);
786 return XPROCESS (proc
)->filter
;
789 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
791 "Give PROCESS the sentinel SENTINEL; nil for none.\n\
792 The sentinel is called as a function when the process changes state.\n\
793 It gets two arguments: the process, and a string describing the change.")
795 register Lisp_Object proc
, sentinel
;
797 CHECK_PROCESS (proc
, 0);
798 XPROCESS (proc
)->sentinel
= sentinel
;
802 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
804 "Return the sentinel of PROCESS; nil if none.\n\
805 See `set-process-sentinel' for more info on sentinels.")
807 register Lisp_Object proc
;
809 CHECK_PROCESS (proc
, 0);
810 return XPROCESS (proc
)->sentinel
;
813 DEFUN ("process-kill-without-query", Fprocess_kill_without_query
,
814 Sprocess_kill_without_query
, 1, 2, 0,
815 "Say no query needed if PROCESS is running when Emacs is exited.\n\
816 Optional second argument if non-nill says to require a query.\n\
817 Value is t if a query was formerly required.")
819 register Lisp_Object proc
, value
;
823 CHECK_PROCESS (proc
, 0);
824 tem
= XPROCESS (proc
)->kill_without_query
;
825 XPROCESS (proc
)->kill_without_query
= Fnull (value
);
833 register Lisp_Object tail
, tem
;
834 Lisp_Object proc
, minspace
, tem1
;
835 register struct buffer
*old
= current_buffer
;
836 register struct Lisp_Process
*p
;
840 XFASTINT (minspace
) = 1;
842 set_buffer_internal (XBUFFER (Vstandard_output
));
843 Fbuffer_disable_undo (Vstandard_output
);
845 current_buffer
->truncate_lines
= Qt
;
848 Proc Status Buffer Command\n\
849 ---- ------ ------ -------\n", -1);
851 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
855 proc
= Fcdr (Fcar (tail
));
857 if (NILP (p
->childp
))
860 Finsert (1, &p
->name
);
861 Findent_to (make_number (13), minspace
);
863 if (!NILP (p
->raw_status_low
))
866 if (XTYPE (p
->status
) == Lisp_Cons
)
867 symbol
= XCONS (p
->status
)->car
;
870 if (EQ (symbol
, Qsignal
))
873 tem
= Fcar (Fcdr (p
->status
));
875 if (XINT (tem
) < NSIG
)
876 write_string (sys_siglist
[XINT (tem
)], -1);
879 Fprinc (symbol
, Qnil
);
881 else if (NETCONN_P (proc
))
883 if (EQ (symbol
, Qrun
))
884 write_string ("open", -1);
885 else if (EQ (symbol
, Qexit
))
886 write_string ("closed", -1);
888 Fprinc (symbol
, Qnil
);
891 Fprinc (symbol
, Qnil
);
893 if (EQ (symbol
, Qexit
))
896 tem
= Fcar (Fcdr (p
->status
));
899 sprintf (tembuf
, " %d", XFASTINT (tem
));
900 write_string (tembuf
, -1);
904 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
905 remove_process (proc
);
907 Findent_to (make_number (22), minspace
);
908 if (NILP (p
->buffer
))
909 insert_string ("(none)");
910 else if (NILP (XBUFFER (p
->buffer
)->name
))
911 insert_string ("(Killed)");
913 Finsert (1, &XBUFFER (p
->buffer
)->name
);
915 Findent_to (make_number (37), minspace
);
917 if (NETCONN_P (proc
))
919 sprintf (tembuf
, "(network stream connection to %s)\n",
920 XSTRING (p
->childp
)->data
);
921 insert_string (tembuf
);
935 insert_string ("\n");
941 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 0, "",
942 "Display a list of all processes.\n\
943 \(Any processes listed as Exited or Signaled are actually eliminated\n\
944 after the listing is made.)")
947 internal_with_output_to_temp_buffer ("*Process List*",
948 list_processes_1
, Qnil
);
952 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
953 "Return a list of all processes.")
956 return Fmapcar (Qcdr
, Vprocess_alist
);
959 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
960 "Start a program in a subprocess. Return the process object for it.\n\
961 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
962 NAME is name for process. It is modified if necessary to make it unique.\n\
963 BUFFER is the buffer or (buffer-name) to associate with the process.\n\
964 Process output goes at end of that buffer, unless you specify\n\
965 an output stream or filter function to handle the output.\n\
966 BUFFER may be also nil, meaning that this process is not associated\n\
968 Third arg is program file name. It is searched for as in the shell.\n\
969 Remaining arguments are strings to give program as arguments.")
972 register Lisp_Object
*args
;
974 Lisp_Object buffer
, name
, program
, proc
, tem
;
976 register unsigned char *new_argv
;
979 register unsigned char **new_argv
;
985 buffer
= Fget_buffer_create (buffer
);
988 CHECK_STRING (name
, 0);
992 CHECK_STRING (program
, 2);
995 /* Make a one member argv with all args concatenated
996 together separated by a blank. */
997 len
= XSTRING (program
)->size
+ 2;
998 for (i
= 3; i
< nargs
; i
++)
1001 CHECK_STRING (tem
, i
);
1002 len
+= XSTRING (tem
)->size
+ 1; /* count the blank */
1004 new_argv
= (unsigned char *) alloca (len
);
1005 strcpy (new_argv
, XSTRING (program
)->data
);
1006 for (i
= 3; i
< nargs
; i
++)
1009 CHECK_STRING (tem
, i
);
1010 strcat (new_argv
, " ");
1011 strcat (new_argv
, XSTRING (tem
)->data
);
1014 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1016 for (i
= 3; i
< nargs
; i
++)
1019 CHECK_STRING (tem
, i
);
1020 new_argv
[i
- 2] = XSTRING (tem
)->data
;
1022 new_argv
[i
- 2] = 0;
1023 new_argv
[0] = XSTRING (program
)->data
;
1025 /* If program file name is not absolute, search our path for it */
1026 if (new_argv
[0][0] != '/')
1029 openp (Vexec_path
, program
, "", &tem
, 1);
1031 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1032 new_argv
[0] = XSTRING (tem
)->data
;
1034 #endif /* not VMS */
1036 proc
= make_process (name
);
1038 XPROCESS (proc
)->childp
= Qt
;
1039 XPROCESS (proc
)->command_channel_p
= Qnil
;
1040 XPROCESS (proc
)->buffer
= buffer
;
1041 XPROCESS (proc
)->sentinel
= Qnil
;
1042 XPROCESS (proc
)->filter
= Qnil
;
1043 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1045 create_process (proc
, new_argv
);
1051 create_process_1 (signo
)
1055 /* USG systems forget handlers when they are used;
1056 must reestablish each time */
1057 signal (signo
, create_process_1
);
1061 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1064 /* Mimic blocking of signals on system V, which doesn't really have it. */
1066 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1067 int sigchld_deferred
;
1070 create_process_sigchld ()
1072 signal (SIGCHLD
, create_process_sigchld
);
1074 sigchld_deferred
= 1;
1080 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1081 create_process (process
, new_argv
)
1082 Lisp_Object process
;
1085 int pid
, inchannel
, outchannel
, forkin
, forkout
;
1088 SIGTYPE (*sigchld
)();
1091 Lisp_Object current_dir
;
1093 extern char **environ
;
1097 inchannel
= outchannel
= -1;
1100 if (EQ (Vprocess_connection_type
, Qt
))
1101 outchannel
= inchannel
= allocate_pty ();
1103 /* Make sure that the child will be able to chdir to the current
1104 buffer's current directory. We can't just have the child check
1105 for an error when it does the chdir, since it's in a vfork. */
1106 current_dir
= expand_and_dir_to_file (current_buffer
->directory
, Qnil
);
1107 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1108 report_file_error ("Setting current directory",
1109 Fcons (current_buffer
->directory
, Qnil
));
1114 /* On USG systems it does not work to open the pty's tty here
1115 and then close and reopen it in the child. */
1117 /* Don't let this terminal become our controlling terminal
1118 (in case we don't have one). */
1119 forkout
= forkin
= open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1121 forkout
= forkin
= open (pty_name
, O_RDWR
, 0);
1124 report_file_error ("Opening pty", Qnil
);
1126 forkin
= forkout
= -1;
1127 #endif /* not USG */
1131 #endif /* HAVE_PTYS */
1134 if (socketpair (AF_UNIX
, SOCK_STREAM
, 0, sv
) < 0)
1135 report_file_error ("Opening socketpair", Qnil
);
1136 outchannel
= inchannel
= sv
[0];
1137 forkout
= forkin
= sv
[1];
1139 #else /* not SKTPAIR */
1148 #endif /* not SKTPAIR */
1151 /* Replaced by close_process_descs */
1152 set_exclusive_use (inchannel
);
1153 set_exclusive_use (outchannel
);
1156 /* Stride people say it's a mystery why this is needed
1157 as well as the O_NDELAY, but that it fails without this. */
1158 #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
1161 ioctl (inchannel
, FIONBIO
, &one
);
1166 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1169 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1173 /* Record this as an active process, with its channels.
1174 As a result, child_setup will close Emacs's side of the pipes. */
1175 chan_process
[inchannel
] = process
;
1176 XFASTINT (XPROCESS (process
)->infd
) = inchannel
;
1177 XFASTINT (XPROCESS (process
)->outfd
) = outchannel
;
1178 /* Record the tty descriptor used in the subprocess. */
1180 XPROCESS (process
)->subtty
= Qnil
;
1182 XFASTINT (XPROCESS (process
)->subtty
) = forkin
;
1183 XPROCESS (process
)->pty_flag
= (pty_flag
? Qt
: Qnil
);
1184 XPROCESS (process
)->status
= Qrun
;
1186 /* Delay interrupts until we have a chance to store
1187 the new fork's pid in its process structure */
1191 #else /* not BSD4_1 */
1192 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1193 sigsetmask (sigmask (SIGCHLD
));
1194 #else /* ordinary USG */
1196 sigchld_deferred
= 0;
1197 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
1199 #endif /* ordinary USG */
1200 #endif /* not BSD4_1 */
1201 #endif /* SIGCHLD */
1203 /* Until we store the proper pid, enable sigchld_handler
1204 to recognize an unknown pid as standing for this process.
1205 It is very important not to let this `marker' value stay
1206 in the table after this function has returned; if it does
1207 it might cause call-process to hang and subsequent asynchronous
1208 processes to get their return values scrambled. */
1209 XSETINT (XPROCESS (process
)->pid
, -1);
1212 /* child_setup must clobber environ on systems with true vfork.
1213 Protect it from permanent change. */
1214 char **save_environ
= environ
;
1219 int xforkin
= forkin
;
1220 int xforkout
= forkout
;
1222 #if 0 /* This was probably a mistake--it duplicates code later on,
1223 but fails to handle all the cases. */
1224 /* Make sure SIGCHLD is not blocked in the child. */
1225 sigsetmask (SIGEMPTYMASK
);
1228 /* Make the pty be the controlling terminal of the process. */
1230 /* First, disconnect its current controlling terminal. */
1233 #else /* not HAVE_SETSID */
1235 /* It's very important to call setpgrp() here and no time
1236 afterwards. Otherwise, we lose our controlling tty which
1237 is set when we open the pty. */
1240 #endif /* not HAVE_SETSID */
1242 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1243 can do TIOCSPGRP only to the process's controlling tty. */
1246 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1247 I can't test it since I don't have 4.3. */
1248 int j
= open ("/dev/tty", O_RDWR
, 0);
1249 ioctl (j
, TIOCNOTTY
, 0);
1252 /* In order to get a controlling terminal on some versions
1253 of BSD, it is necessary to put the process in pgrp 0
1254 before it opens the terminal. */
1258 #endif /* TIOCNOTTY */
1260 #if !defined (RTU) && !defined (UNIPLUS)
1261 /*** There is a suggestion that this ought to be a
1262 conditional on TIOCSPGRP. */
1263 /* Now close the pty (if we had it open) and reopen it.
1264 This makes the pty the controlling terminal of the subprocess. */
1267 /* I wonder if close (open (pty_name, ...)) would work? */
1270 xforkout
= xforkin
= open (pty_name
, O_RDWR
, 0);
1275 #endif /* not UNIPLUS and not RTU */
1276 #ifdef SETUP_SLAVE_PTY
1278 #endif /* SETUP_SLAVE_PTY */
1280 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
1281 Now reenable it in the child, so it will die when we want it to. */
1283 signal (SIGHUP
, SIG_DFL
);
1285 #endif /* HAVE_PTYS */
1290 #else /* not BSD4_1 */
1291 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1292 sigsetmask (SIGEMPTYMASK
);
1293 #else /* ordinary USG */
1294 signal (SIGCHLD
, sigchld
);
1295 #endif /* ordinary USG */
1296 #endif /* not BSD4_1 */
1297 #endif /* SIGCHLD */
1299 child_setup_tty (xforkout
);
1300 child_setup (xforkin
, xforkout
, xforkout
,
1301 new_argv
, env
, 1, current_dir
);
1303 environ
= save_environ
;
1308 remove_process (process
);
1309 report_file_error ("Doing vfork", Qnil
);
1312 XFASTINT (XPROCESS (process
)->pid
) = pid
;
1314 FD_SET (inchannel
, &input_wait_mask
);
1316 /* If the subfork execv fails, and it exits,
1317 this close hangs. I don't know why.
1318 So have an interrupt jar it loose. */
1320 signal (SIGALRM
, create_process_1
);
1323 /* OK to close only if it's not a pty. Otherwise we need to leave
1324 it open for ioctl to get pgrp when signals are sent, or to send
1325 the interrupt characters through if that's how we're signalling
1326 subprocesses. Alternately if you are concerned about running out
1327 of file descriptors, you could just save the tty name and open
1328 just to do the ioctl. */
1329 if (NILP (XFASTINT (XPROCESS (process
)->pty_flag
)))
1332 XPROCESS (process
)->subtty
= Qnil
;
1338 if (forkin
!= forkout
&& forkout
>= 0)
1344 #else /* not BSD4_1 */
1345 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1346 sigsetmask (SIGEMPTYMASK
);
1347 #else /* ordinary USG */
1349 signal (SIGCHLD
, sigchld
);
1350 /* Now really handle any of these signals
1351 that came in during this function. */
1352 if (sigchld_deferred
)
1353 kill (getpid (), SIGCHLD
);
1355 #endif /* ordinary USG */
1356 #endif /* not BSD4_1 */
1357 #endif /* SIGCHLD */
1359 #endif /* not VMS */
1363 /* open a TCP network connection to a given HOST/SERVICE. Treated
1364 exactly like a normal process when reading and writing. Only
1365 differences are in status display and process deletion. A network
1366 connection has no PID; you cannot signal it. All you can do is
1367 deactivate and close it via delete-process */
1369 DEFUN ("open-network-stream", Fopen_network_stream
, Sopen_network_stream
,
1371 "Open a TCP connection for a service to a host.\n\
1372 Returns a subprocess-object to represent the connection.\n\
1373 Input and output work as for subprocesses; `delete-process' closes it.\n\
1374 Args are NAME BUFFER HOST SERVICE.\n\
1375 NAME is name for process. It is modified if necessary to make it unique.\n\
1376 BUFFER is the buffer (or buffer-name) to associate with the process.\n\
1377 Process output goes at end of that buffer, unless you specify\n\
1378 an output stream or filter function to handle the output.\n\
1379 BUFFER may be also nil, meaning that this process is not associated\n\
1381 Third arg is name of the host to connect to, or its IP address.\n\
1382 Fourth arg SERVICE is name of the service desired, or an integer\n\
1383 specifying a port number to connect to.")
1384 (name
, buffer
, host
, service
)
1385 Lisp_Object name
, buffer
, host
, service
;
1389 struct sockaddr_in address
;
1390 struct servent
*svc_info
;
1391 struct hostent
*host_info_ptr
, host_info
;
1392 char *(addr_list
[2]);
1393 unsigned long numeric_addr
;
1397 struct hostent host_info_fixed
;
1398 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1400 GCPRO4 (name
, buffer
, host
, service
);
1401 CHECK_STRING (name
, 0);
1402 CHECK_STRING (host
, 0);
1403 if (XTYPE(service
) == Lisp_Int
)
1404 port
= htons ((unsigned short) XINT (service
));
1407 CHECK_STRING (service
, 0);
1408 svc_info
= getservbyname (XSTRING (service
)->data
, "tcp");
1410 error ("Unknown service \"%s\"", XSTRING (service
)->data
);
1411 port
= svc_info
->s_port
;
1414 host_info_ptr
= gethostbyname (XSTRING (host
)->data
);
1415 if (host_info_ptr
== 0)
1416 /* Attempt to interpret host as numeric inet address */
1418 numeric_addr
= inet_addr (XSTRING (host
)->data
);
1419 if (numeric_addr
== -1)
1420 error ("Unknown host \"%s\"", XSTRING (host
)->data
);
1422 host_info_ptr
= &host_info
;
1423 host_info
.h_name
= 0;
1424 host_info
.h_aliases
= 0;
1425 host_info
.h_addrtype
= AF_INET
;
1426 host_info
.h_addr_list
= &(addr_list
[0]);
1427 addr_list
[0] = (char*)(&numeric_addr
);
1429 host_info
.h_length
= strlen (addr_list
[0]);
1432 bzero (&address
, sizeof address
);
1433 bcopy (host_info_ptr
->h_addr
, (char *) &address
.sin_addr
,
1434 host_info_ptr
->h_length
);
1435 address
.sin_family
= host_info_ptr
->h_addrtype
;
1436 address
.sin_port
= port
;
1438 s
= socket (host_info_ptr
->h_addrtype
, SOCK_STREAM
, 0);
1440 report_file_error ("error creating socket", Fcons (name
, Qnil
));
1443 if (connect (s
, &address
, sizeof address
) == -1)
1450 report_file_error ("connection failed",
1451 Fcons (host
, Fcons (name
, Qnil
)));
1457 report_file_error ("error duplicating socket", Fcons (name
, Qnil
));
1460 buffer
= Fget_buffer_create (buffer
);
1461 proc
= make_process (name
);
1463 chan_process
[inch
] = proc
;
1466 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
1469 fcntl (inch
, F_SETFL
, O_NDELAY
);
1473 XPROCESS (proc
)->childp
= host
;
1474 XPROCESS (proc
)->command_channel_p
= Qnil
;
1475 XPROCESS (proc
)->buffer
= buffer
;
1476 XPROCESS (proc
)->sentinel
= Qnil
;
1477 XPROCESS (proc
)->filter
= Qnil
;
1478 XPROCESS (proc
)->command
= Qnil
;
1479 XPROCESS (proc
)->pid
= Qnil
;
1480 XFASTINT (XPROCESS (proc
)->infd
) = s
;
1481 XFASTINT (XPROCESS (proc
)->outfd
) = outch
;
1482 XPROCESS (proc
)->status
= Qrun
;
1483 FD_SET (inch
, &input_wait_mask
);
1488 #endif /* HAVE_SOCKETS */
1490 deactivate_process (proc
)
1493 register int inchannel
, outchannel
;
1494 register struct Lisp_Process
*p
= XPROCESS (proc
);
1496 inchannel
= XFASTINT (p
->infd
);
1497 outchannel
= XFASTINT (p
->outfd
);
1501 /* Beware SIGCHLD hereabouts. */
1502 flush_pending_output (inchannel
);
1505 VMS_PROC_STUFF
*get_vms_process_pointer (), *vs
;
1506 sys$
dassgn (outchannel
);
1507 vs
= get_vms_process_pointer (p
->pid
)
1509 give_back_vms_process_stuff (vs
);
1513 if (outchannel
&& outchannel
!= inchannel
)
1517 XFASTINT (p
->infd
) = 0;
1518 XFASTINT (p
->outfd
) = 0;
1519 chan_process
[inchannel
] = Qnil
;
1520 FD_CLR (inchannel
, &input_wait_mask
);
1524 /* Close all descriptors currently in use for communication
1525 with subprocess. This is used in a newly-forked subprocess
1526 to get rid of irrelevant descriptors. */
1528 close_process_descs ()
1531 for (i
= 0; i
< MAXDESC
; i
++)
1533 Lisp_Object process
;
1534 process
= chan_process
[i
];
1535 if (!NILP (process
))
1537 int in
= XFASTINT (XPROCESS (process
)->infd
);
1538 int out
= XFASTINT (XPROCESS (process
)->outfd
);
1541 if (out
&& in
!= out
)
1547 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
1549 "Allow any pending output from subprocesses to be read by Emacs.\n\
1550 It is read into the process' buffers or given to their filter functions.\n\
1551 Non-nil arg PROCESS means do not return until some output has been received\n\
1553 Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of\n\
1554 seconds and microseconds to wait; return after that much time whether\n\
1555 or not there is input.\n\
1556 Return non-nil iff we received any output before the timeout expired.")
1557 (proc
, timeout
, timeout_msecs
)
1558 register Lisp_Object proc
, timeout
, timeout_msecs
;
1563 if (! NILP (timeout_msecs
))
1565 CHECK_NUMBER (timeout_msecs
, 2);
1566 useconds
= XINT (timeout_msecs
);
1567 if (XTYPE (timeout
) != Lisp_Int
)
1568 XSET (timeout
, Lisp_Int
, 0);
1571 int carry
= useconds
/ 1000000;
1573 XSETINT (timeout
, XINT (timeout
) + carry
);
1574 useconds
-= carry
* 1000000;
1576 /* I think this clause is necessary because C doesn't
1577 guarantee a particular rounding direction for negative
1581 XSETINT (timeout
, XINT (timeout
) - 1);
1582 useconds
+= 1000000;
1587 if (! NILP (timeout
))
1589 CHECK_NUMBER (timeout
, 1);
1590 seconds
= XINT (timeout
);
1603 (wait_reading_process_input (seconds
, useconds
,
1605 ? XPROCESS (get_process (proc
)) : 0), 0)
1609 /* This variable is different from waiting_for_input in keyboard.c.
1610 It is used to communicate to a lisp process-filter/sentinel (via the
1611 function Fwaiting_for_user_input_p below) whether emacs was waiting
1612 for user-input when that process-filter was called.
1613 waiting_for_input cannot be used as that is by definition 0 when
1614 lisp code is being evalled */
1615 static int waiting_for_user_input_p
;
1617 /* Read and dispose of subprocess output while waiting for timeout to
1618 elapse and/or keyboard input to be available.
1621 timeout in seconds, or
1622 zero for no limit, or
1623 -1 means gobble data immediately available but don't wait for any.
1626 0 to ignore keyboard input, or
1627 1 to return when input is available, or
1628 -1 means caller will actually read the input, so don't throw to
1629 the quit handler, or
1630 a pointer to a struct Lisp_Process, meaning wait until something
1631 arrives from that process. The return value is true iff we read
1632 some input from that process.
1634 do_display != 0 means redisplay should be done to show subprocess
1635 output that arrives.
1637 If read_kbd is a pointer to a struct Lisp_Process, then the
1638 function returns true iff we received input from that process
1639 before the timeout elapsed.
1640 Otherwise, return true iff we recieved input from any process. */
1642 wait_reading_process_input (time_limit
, microsecs
, read_kbd
, do_display
)
1643 int time_limit
, microsecs
, read_kbd
, do_display
;
1645 register int channel
, nfds
, m
;
1646 static SELECT_TYPE Available
;
1649 EMACS_TIME timeout
, end_time
, garbage
;
1651 int wait_channel
= 0;
1652 struct Lisp_Process
*wait_proc
= 0;
1653 int got_some_input
= 0;
1655 FD_ZERO (&Available
);
1657 /* Detect when read_kbd is really the address of a Lisp_Process. */
1658 if (read_kbd
> 10 || read_kbd
< -1)
1660 wait_proc
= (struct Lisp_Process
*) read_kbd
;
1661 wait_channel
= XFASTINT (wait_proc
->infd
);
1665 waiting_for_user_input_p
= read_kbd
;
1667 /* Since we may need to wait several times,
1668 compute the absolute time to return at. */
1669 if (time_limit
|| microsecs
)
1671 EMACS_GET_TIME (end_time
);
1672 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
1673 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
1676 /* Turn off periodic alarms (in case they are in use)
1677 because the select emulator uses alarms. */
1682 /* If calling from keyboard input, do not quit
1683 since we want to return C-g as an input character.
1684 Otherwise, do pending quit if requested. */
1688 /* If status of something has changed, and no input is available,
1689 notify the user of the change right away */
1690 if (update_tick
!= process_tick
&& do_display
)
1692 Atemp
= input_wait_mask
;
1693 EMACS_SET_SECS_USECS (timeout
, 0, 0);
1694 if (select (MAXDESC
, &Atemp
, 0, 0, &timeout
) <= 0)
1698 /* Don't wait for output from a non-running process. */
1699 if (wait_proc
!= 0 && !NILP (wait_proc
->raw_status_low
))
1700 update_status (wait_proc
);
1702 && ! EQ (wait_proc
->status
, Qrun
))
1705 /* Compute time from now till when time limit is up */
1706 /* Exit if already run out */
1707 if (time_limit
== -1)
1709 /* -1 specified for timeout means
1710 gobble output available now
1711 but don't wait at all. */
1713 EMACS_SET_SECS_USECS (timeout
, 0, 0);
1715 else if (time_limit
|| microsecs
)
1717 EMACS_GET_TIME (timeout
);
1718 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
1719 if (EMACS_TIME_NEG_P (timeout
))
1724 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
1727 /* Cause C-g and alarm signals to take immediate action,
1728 and cause input available signals to zero out timeout */
1730 set_waiting_for_input (&timeout
);
1732 /* Wait till there is something to do */
1734 Available
= input_wait_mask
;
1736 FD_CLR (0, &Available
);
1738 /* If a screen has been newly mapped and needs updating,
1739 reprocess its display stuff. */
1740 if (screen_garbaged
)
1741 redisplay_preserve_echo_area ();
1743 if (read_kbd
&& detect_input_pending ())
1747 nfds
= select (MAXDESC
, &Available
, 0, 0, &timeout
);
1750 nfds
= select (MAXDESC
, &Available
, 0, 0, &timeout
);
1752 nfds
= select (MAXDESC
, &Available
, 0, 0, &timeout
);
1757 /* Make C-g and alarm signals set flags again */
1758 clear_waiting_for_input ();
1760 /* If we woke up due to SIGWINCH, actually change size now. */
1761 do_pending_window_change ();
1763 if (time_limit
&& nfds
== 0) /* timeout elapsed */
1767 if (xerrno
== EINTR
)
1768 FD_ZERO (&Available
);
1770 /* This happens for no known reason on ALLIANT.
1771 I am guessing that this is the right response. -- RMS. */
1772 else if (xerrno
== EFAULT
)
1773 FD_ZERO (&Available
);
1775 else if (xerrno
== EBADF
)
1778 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
1779 the child's closure of the pts gives the parent a SIGHUP, and
1780 the ptc file descriptor is automatically closed,
1781 yielding EBADF here or at select() call above.
1782 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
1783 in m-ibmrt-aix.h), and here we just ignore the select error.
1784 Cleanup occurs c/o status_notify after SIGCLD. */
1785 FD_ZERO (&Available
); /* Cannot depend on values returned */
1791 error("select error: %s", sys_errlist
[xerrno
]);
1794 else if (nfds
> 0 && FD_ISSET (0, &Available
) && interrupt_input
)
1795 /* System sometimes fails to deliver SIGIO. */
1796 kill (getpid (), SIGIO
);
1799 /* Check for keyboard input */
1800 /* If there is any, return immediately
1801 to give it higher priority than subprocesses */
1803 if (read_kbd
&& detect_input_pending ())
1806 /* If we think we have keyboard input waiting, but didn't get SIGIO
1807 go read it. This can happen with X on BSD after logging out.
1808 In that case, there really is no input and no SIGIO,
1809 but select says there is input. */
1812 if (read_kbd && interrupt_input && (Available & fileno (stdin)))
1814 if (read_kbd
&& interrupt_input
&& (FD_ISSET (fileno (stdin
), &Available
)))
1818 /* Check for connection from other process */
1820 if (Available
& ChannelMask (comm_server
))
1822 Available
&= ~(ChannelMask (comm_server
));
1828 got_some_input
|= nfds
> 0;
1830 /* Check for data from a process or a command channel */
1831 for (channel
= FIRST_PROC_DESC
; channel
< MAXDESC
; channel
++)
1833 if (FD_ISSET (channel
, &Available
))
1837 /* If waiting for this channel, arrange to return as
1838 soon as no more input to be processed. No more
1840 if (wait_channel
== channel
)
1846 proc
= chan_process
[channel
];
1851 /* It's a command channel */
1852 if (!NILP (XPROCESS (proc
)->command_channel_p
))
1854 ProcessCommChan (channel
, proc
);
1855 if (NILP (XPROCESS (proc
)->command_channel_p
))
1857 /* It has ceased to be a command channel! */
1858 int bytes_available
;
1859 if (ioctl (channel
, FIONREAD
, &bytes_available
) < 0)
1860 bytes_available
= 0;
1861 if (bytes_available
)
1862 FD_SET (channel
, &Available
);
1868 /* Read data from the process, starting with our
1869 buffered-ahead character if we have one. */
1871 nread
= read_process_output (proc
, channel
);
1874 /* Since read_process_output can run a filter,
1875 which can call accept-process-output,
1876 don't try to read from any other processes
1877 before doing the select again. */
1878 FD_ZERO (&Available
);
1881 redisplay_preserve_echo_area ();
1884 else if (nread
== -1 && errno
== EWOULDBLOCK
)
1888 else if (nread
== -1 && errno
== EAGAIN
)
1892 else if (nread
== -1 && errno
== EAGAIN
)
1894 /* Note that we cannot distinguish between no input
1895 available now and a closed pipe.
1896 With luck, a closed pipe will be accompanied by
1897 subprocess termination and SIGCHLD. */
1898 else if (nread
== 0 && !NETCONN_P (proc
))
1900 #endif /* O_NDELAY */
1901 #endif /* O_NONBLOCK */
1902 #endif /* EWOULDBLOCK */
1904 /* On some OSs with ptys, when the process on one end of
1905 a pty exits, the other end gets an error reading with
1906 errno = EIO instead of getting an EOF (0 bytes read).
1907 Therefore, if we get an error reading and errno =
1908 EIO, just continue, because the child process has
1909 exited and should clean itself up soon (e.g. when we
1911 else if (nread
== -1 && errno
== EIO
)
1913 #endif /* HAVE_PTYS */
1914 /* If we can detect process termination, don't consider the process
1915 gone just because its pipe is closed. */
1917 else if (nread
== 0 && !NETCONN_P (proc
))
1922 /* Preserve status of processes already terminated. */
1923 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
1924 deactivate_process (proc
);
1925 if (!NILP (XPROCESS (proc
)->raw_status_low
))
1926 update_status (XPROCESS (proc
));
1927 if (EQ (XPROCESS (proc
)->status
, Qrun
))
1928 XPROCESS (proc
)->status
1929 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
1932 } /* end for each file descriptor */
1933 } /* end while exit conditions not met */
1935 /* Resume periodic signals to poll for input, if necessary. */
1938 return got_some_input
;
1941 /* Read pending output from the process channel,
1942 starting with our buffered-ahead character if we have one.
1943 Yield number of characters read.
1945 This function reads at most 1024 characters.
1946 If you want to read all available subprocess output,
1947 you must call it repeatedly until it returns zero. */
1949 read_process_output (proc
, channel
)
1951 register int channel
;
1953 register int nchars
;
1959 register Lisp_Object outstream
;
1960 register struct buffer
*old
= current_buffer
;
1961 register struct Lisp_Process
*p
= XPROCESS (proc
);
1962 register int opoint
;
1965 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
1967 vs
= get_vms_process_pointer (p
->pid
);
1971 return(0); /* Really weird if it does this */
1972 if (!(vs
->iosb
[0] & 1))
1973 return -1; /* I/O error */
1976 error ("Could not get VMS process pointer");
1977 chars
= vs
->inputBuffer
;
1978 nchars
= clean_vms_buffer (chars
, vs
->iosb
[1]);
1981 start_vms_process_read (vs
); /* Crank up the next read on the process */
1982 return 1; /* Nothing worth printing, say we got 1 */
1986 if (proc_buffered_char
[channel
] < 0)
1987 nchars
= read (channel
, chars
, sizeof chars
);
1990 chars
[0] = proc_buffered_char
[channel
];
1991 proc_buffered_char
[channel
] = -1;
1992 nchars
= read (channel
, chars
+ 1, sizeof chars
- 1);
1996 nchars
= nchars
+ 1;
1998 #endif /* not VMS */
2000 if (nchars
<= 0) return nchars
;
2002 outstream
= p
->filter
;
2003 if (!NILP (outstream
))
2005 /* We inhibit quit here instead of just catching it so that
2006 hitting ^G when a filter happens to be running won't screw
2008 int count
= specpdl_ptr
- specpdl
;
2009 specbind (Qinhibit_quit
, Qt
);
2010 call2 (outstream
, proc
, make_string (chars
, nchars
));
2013 start_vms_process_read (vs
);
2019 /* If no filter, write into buffer if it isn't dead. */
2020 if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
2024 Fset_buffer (p
->buffer
);
2027 /* Insert new output into buffer
2028 at the current end-of-output marker,
2029 thus preserving logical ordering of input and output. */
2030 if (XMARKER (p
->mark
)->buffer
)
2031 SET_PT (marker_position (p
->mark
));
2034 if (point
<= opoint
)
2037 tem
= current_buffer
->read_only
;
2038 current_buffer
->read_only
= Qnil
;
2039 /* Insert before markers in case we are inserting where
2040 the buffer's mark is, and the user's next command is Meta-y. */
2041 insert_before_markers (chars
, nchars
);
2042 current_buffer
->read_only
= tem
;
2043 Fset_marker (p
->mark
, make_number (point
), p
->buffer
);
2044 update_mode_lines
++;
2047 set_buffer_internal (old
);
2050 start_vms_process_read (vs
);
2055 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
2057 "Returns non-NIL if emacs is waiting for input from the user.\n\
2058 This is intended for use by asynchronous process output filters and sentinels.")
2061 return ((waiting_for_user_input_p
) ? Qt
: Qnil
);
2064 /* Sending data to subprocess */
2066 jmp_buf send_process_frame
;
2069 send_process_trap ()
2075 longjmp (send_process_frame
, 1);
2078 send_process (proc
, buf
, len
)
2083 /* Don't use register vars; longjmp can lose them. */
2085 unsigned char *procname
= XSTRING (XPROCESS (proc
)->name
)->data
;
2089 struct Lisp_Process
*p
= XPROCESS (proc
);
2090 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
2093 if (! NILP (XPROCESS (proc
)->raw_status_low
))
2094 update_status (XPROCESS (proc
));
2095 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
2096 error ("Process %s not running", procname
);
2099 vs
= get_vms_process_pointer (p
->pid
);
2101 error ("Could not find this process: %x", p
->pid
);
2102 else if (write_to_vms_process (vs
, buf
, len
))
2105 if (!setjmp (send_process_frame
))
2109 /* Don't send more than 500 bytes at a time. */
2112 signal (SIGPIPE
, send_process_trap
);
2113 rv
= write (XFASTINT (XPROCESS (proc
)->outfd
), buf
, this);
2114 signal (SIGPIPE
, SIG_DFL
);
2119 || errno
== EWOULDBLOCK
2126 /* It would be nice to accept process output here,
2127 but that is difficult. For example, it could
2128 garbage what we are sending if that is from a buffer. */
2135 report_file_error ("writing to process", Fcons (proc
, Qnil
));
2139 /* Allow input from processes between bursts of sending.
2140 Otherwise things may get stopped up. */
2142 wait_reading_process_input (-1, 0, 0, 0);
2147 XPROCESS (proc
)->raw_status_low
= Qnil
;
2148 XPROCESS (proc
)->raw_status_high
= Qnil
;
2149 XPROCESS (proc
)->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
2150 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
2151 deactivate_process (proc
);
2153 error ("Error writing to process %s; closed it", procname
);
2155 error ("SIGPIPE raised on process %s; closed it", procname
);
2160 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
2162 "Send current contents of region as input to PROCESS.\n\
2163 PROCESS may be a process name or an actual process.\n\
2164 Called from program, takes three arguments, PROCESS, START and END.\n\
2165 If the region is more than 500 characters long,\n\
2166 it is sent in several bunches. This may happen even for shorter regions.\n\
2167 Output from processes can arrive in between bunches.")
2168 (process
, start
, end
)
2169 Lisp_Object process
, start
, end
;
2174 proc
= get_process (process
);
2175 validate_region (&start
, &end
);
2177 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
2180 start1
= XINT (start
);
2181 send_process (proc
, &FETCH_CHAR (start1
), XINT (end
) - XINT (start
));
2186 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
2188 "Send PROCESS the contents of STRING as input.\n\
2189 PROCESS may be a process name or an actual process.\n\
2190 If STRING is more than 500 characters long,\n\
2191 it is sent in several bunches. This may happen even for shorter strings.\n\
2192 Output from processes can arrive in between bunches.")
2194 Lisp_Object process
, string
;
2197 CHECK_STRING (string
, 1);
2198 proc
= get_process (process
);
2199 send_process (proc
, XSTRING (string
)->data
, XSTRING (string
)->size
);
2203 /* send a signal number SIGNO to PROCESS.
2204 CURRENT_GROUP means send to the process group that currently owns
2205 the terminal being used to communicate with PROCESS.
2206 This is used for various commands in shell mode.
2207 If NOMSG is zero, insert signal-announcements into process's buffers
2210 process_send_signal (process
, signo
, current_group
, nomsg
)
2211 Lisp_Object process
;
2213 Lisp_Object current_group
;
2217 register struct Lisp_Process
*p
;
2221 proc
= get_process (process
);
2222 p
= XPROCESS (proc
);
2224 if (!EQ (p
->childp
, Qt
))
2225 error ("Process %s is not a subprocess",
2226 XSTRING (p
->name
)->data
);
2227 if (!XFASTINT (p
->infd
))
2228 error ("Process %s is not active",
2229 XSTRING (p
->name
)->data
);
2231 if (NILP (p
->pty_flag
))
2232 current_group
= Qnil
;
2234 #ifdef TIOCGPGRP /* Not sure about this! (fnf) */
2235 /* If we are using pgrps, get a pgrp number and make it negative. */
2236 if (!NILP (current_group
))
2238 /* If possible, send signals to the entire pgrp
2239 by sending an input character to it. */
2240 #if defined (TIOCGLTC) && defined (TIOCGETC)
2247 ioctl (XFASTINT (p
->infd
), TIOCGETC
, &c
);
2248 send_process (proc
, &c
.t_intrc
, 1);
2251 ioctl (XFASTINT (p
->infd
), TIOCGETC
, &c
);
2252 send_process (proc
, &c
.t_quitc
, 1);
2255 ioctl (XFASTINT (p
->infd
), TIOCGLTC
, &lc
);
2256 send_process (proc
, &lc
.t_suspc
, 1);
2259 #endif /* have TIOCGLTC and have TIOCGETC */
2260 /* It is possible that the following code would work
2261 on other kinds of USG systems, not just on the IRIS.
2262 This should be tried in Emacs 19. */
2263 #if defined (IRIS) && defined (HAVE_SETSID) /* Check for Irix, not older
2269 ioctl (XFASTINT (p
->infd
), TCGETA
, &t
);
2270 send_process (proc
, &t
.c_cc
[VINTR
], 1);
2273 ioctl (XFASTINT (p
->infd
), TCGETA
, &t
);
2274 send_process (proc
, &t
.c_cc
[VQUIT
], 1);
2277 ioctl (XFASTINT (p
->infd
), TCGETA
, &t
);
2278 send_process (proc
, &t
.c_cc
[VSWTCH
], 1);
2281 #endif /* IRIS and HAVE_SETSID */
2283 /* Get the pgrp using the tty itself, if we have that.
2284 Otherwise, use the pty to get the pgrp.
2285 On pfa systems, saka@pfu.fujitsu.co.JP writes:
2286 "TICGPGRP symbol defined in sys/ioctl.h at E50.
2287 But, TIOCGPGRP donot work on E50 ;-P work fine on E60"
2288 His patch indicates that if TIOCGPGRP returns an error, then
2289 we should just assume that p->pid is also the process group id. */
2293 if (!NILP (p
->subtty
))
2294 err
= ioctl (XFASTINT (p
->subtty
), TIOCGPGRP
, &gid
);
2296 err
= ioctl (XFASTINT (p
->infd
), TIOCGPGRP
, &gid
);
2300 gid
= - XFASTINT (p
->pid
);
2309 gid
= - XFASTINT (p
->pid
);
2310 #else /* not using pgrps */
2311 /* Can't select pgrps on this system, so we know that
2312 the child itself heads the pgrp. */
2313 gid
= - XFASTINT (p
->pid
);
2314 #endif /* not using pgrps */
2320 p
->raw_status_low
= Qnil
;
2321 p
->raw_status_high
= Qnil
;
2323 XSETINT (p
->tick
, ++process_tick
);
2330 send_process (proc
, "\003", 1); /* ^C */
2335 send_process (proc
, "\031", 1); /* ^Y */
2340 sys$
forcex (&(XFASTINT (p
->pid
)), 0, 1);
2343 flush_pending_output (XFASTINT (p
->infd
));
2347 /* If we don't have process groups, send the signal to the immediate
2348 subprocess. That isn't really right, but it's better than any
2349 obvious alternative. */
2352 kill (XFASTINT (p
->pid
), signo
);
2356 /* gid may be a pid, or minus a pgrp's number */
2358 if (!NILP (current_group
))
2359 ioctl (XFASTINT (p
->infd
), TIOCSIGSEND
, signo
);
2362 gid
= - XFASTINT (p
->pid
);
2365 #else /* no TIOCSIGSEND */
2366 EMACS_KILLPG (-gid
, signo
);
2370 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
2371 "Interrupt process PROCESS. May be process or name of one.\n\
2372 Nil or no arg means current buffer's process.\n\
2373 Second arg CURRENT-GROUP non-nil means send signal to\n\
2374 the current process-group of the process's controlling terminal\n\
2375 rather than to the process's own process group.\n\
2376 If the process is a shell, this means interrupt current subjob\n\
2377 rather than the shell.")
2378 (process
, current_group
)
2379 Lisp_Object process
, current_group
;
2381 process_send_signal (process
, SIGINT
, current_group
, 0);
2385 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
2386 "Kill process PROCESS. May be process or name of one.\n\
2387 See function `interrupt-process' for more details on usage.")
2388 (process
, current_group
)
2389 Lisp_Object process
, current_group
;
2391 process_send_signal (process
, SIGKILL
, current_group
, 0);
2395 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
2396 "Send QUIT signal to process PROCESS. May be process or name of one.\n\
2397 See function `interrupt-process' for more details on usage.")
2398 (process
, current_group
)
2399 Lisp_Object process
, current_group
;
2401 process_send_signal (process
, SIGQUIT
, current_group
, 0);
2405 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
2406 "Stop process PROCESS. May be process or name of one.\n\
2407 See function `interrupt-process' for more details on usage.")
2408 (process
, current_group
)
2409 Lisp_Object process
, current_group
;
2412 error ("no SIGTSTP support");
2414 process_send_signal (process
, SIGTSTP
, current_group
, 0);
2419 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
2420 "Continue process PROCESS. May be process or name of one.\n\
2421 See function `interrupt-process' for more details on usage.")
2422 (process
, current_group
)
2423 Lisp_Object process
, current_group
;
2426 process_send_signal (process
, SIGCONT
, current_group
, 0);
2428 error ("no SIGCONT support");
2433 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
2434 2, 2, "nProcess number: \nnSignal code: ",
2435 "Send the process with number PID the signal with code CODE.\n\
2436 Both PID and CODE are integers.")
2438 Lisp_Object pid
, sig
;
2440 CHECK_NUMBER (pid
, 0);
2441 CHECK_NUMBER (sig
, 1);
2442 return make_number (kill (XINT (pid
), XINT (sig
)));
2445 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
2446 "Make PROCESS see end-of-file in its input.\n\
2447 Eof comes after any text already sent to it.\n\
2448 nil or no arg means current buffer's process.")
2450 Lisp_Object process
;
2454 proc
= get_process (process
);
2455 /* Sending a zero-length record is supposed to mean eof
2456 when TIOCREMOTE is turned on. */
2460 write (XFASTINT (XPROCESS (proc
)->outfd
), buf
, 0);
2462 #else /* did not do TOICREMOTE */
2464 send_process (proc
, "\032", 1); /* ^z */
2466 if (!NILP (XPROCESS (proc
)->pty_flag
))
2467 send_process (proc
, "\004", 1);
2470 close (XPROCESS (proc
)->outfd
);
2471 XFASTINT (XPROCESS (proc
)->outfd
) = open ("/dev/null", O_WRONLY
);
2474 #endif /* did not do TOICREMOTE */
2478 /* Kill all processes associated with `buffer'.
2479 If `buffer' is nil, kill all processes */
2481 kill_buffer_processes (buffer
)
2484 Lisp_Object tail
, proc
;
2486 for (tail
= Vprocess_alist
; XGCTYPE (tail
) == Lisp_Cons
;
2487 tail
= XCONS (tail
)->cdr
)
2489 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
2490 if (XGCTYPE (proc
) == Lisp_Process
2491 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
2493 if (NETCONN_P (proc
))
2494 deactivate_process (proc
);
2495 else if (XFASTINT (XPROCESS (proc
)->infd
))
2496 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
2501 /* On receipt of a signal that a child status has changed,
2502 loop asking about children with changed statuses until
2503 the system says there are no more.
2504 All we do is change the status;
2505 we do not run sentinels or print notifications.
2506 That is saved for the next time keyboard input is done,
2507 in order to avoid timing errors. */
2509 /** WARNING: this can be called during garbage collection.
2510 Therefore, it must not be fooled by the presence of mark bits in
2513 /** USG WARNING: Although it is not obvious from the documentation
2514 in signal(2), on a USG system the SIGCLD handler MUST NOT call
2515 signal() before executing at least one wait(), otherwise the handler
2516 will be called again, resulting in an infinite loop. The relevant
2517 portion of the documentation reads "SIGCLD signals will be queued
2518 and the signal-catching function will be continually reentered until
2519 the queue is empty". Invoking signal() causes the kernel to reexamine
2520 the SIGCLD queue. Fred Fish, UniSoft Systems Inc. */
2523 sigchld_handler (signo
)
2526 int old_errno
= errno
;
2528 register struct Lisp_Process
*p
;
2532 sigheld
|= sigbit (SIGCHLD
);
2544 #endif /* no WUNTRACED */
2545 /* Keep trying to get a status until we get a definitive result. */
2549 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
2551 while (pid
<= 0 && errno
== EINTR
);
2555 /* A real failure. We have done all our job, so return. */
2557 /* USG systems forget handlers when they are used;
2558 must reestablish each time */
2560 signal (signo
, sigchld_handler
); /* WARNING - must come after wait3() */
2563 sigheld
&= ~sigbit (SIGCHLD
);
2571 #endif /* no WNOHANG */
2573 /* Find the process that signaled us, and record its status. */
2576 for (tail
= Vprocess_alist
; XSYMBOL (tail
) != XSYMBOL (Qnil
); tail
= XCONS (tail
)->cdr
)
2578 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
2579 p
= XPROCESS (proc
);
2580 if (EQ (p
->childp
, Qt
) && XFASTINT (p
->pid
) == pid
)
2585 /* Look for an asynchronous process whose pid hasn't been filled
2588 for (tail
= Vprocess_alist
; XSYMBOL (tail
) != XSYMBOL (Qnil
); tail
= XCONS (tail
)->cdr
)
2590 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
2591 p
= XPROCESS (proc
);
2592 if (XTYPE (p
->pid
) == Lisp_Int
&& XINT (p
->pid
) == -1)
2597 /* Change the status of the process that was found. */
2600 union { int i
; WAITTYPE wt
; } u
;
2602 XSETINT (p
->tick
, ++process_tick
);
2604 XFASTINT (p
->raw_status_low
) = u
.i
& 0xffff;
2605 XFASTINT (p
->raw_status_high
) = u
.i
>> 16;
2607 /* If process has terminated, stop waiting for its output. */
2608 if (WIFSIGNALED (w
) || WIFEXITED (w
))
2610 FD_CLR (p
->infd
, &input_wait_mask
);
2613 /* There was no asynchronous process found for that id. Check
2614 if we have a synchronous process. */
2617 synch_process_alive
= 0;
2619 /* Report the status of the synchronous process. */
2621 synch_process_retcode
= WRETCODE (w
);
2622 else if (WIFSIGNALED (w
))
2623 synch_process_death
= sys_siglist
[WTERMSIG (w
)];
2626 /* On some systems, we must return right away.
2627 If any more processes want to signal us, we will
2629 Otherwise (on systems that have WNOHANG), loop around
2630 to use up all the processes that have something to tell us. */
2631 #if defined (USG) && ! (defined (HPUX) && defined (WNOHANG))
2633 signal (signo
, sigchld_handler
);
2637 #endif /* USG, but not HPUX with WNOHANG */
2643 exec_sentinel_unwind (data
)
2646 XPROCESS (XCONS (data
)->car
)->sentinel
= XCONS (data
)->cdr
;
2651 exec_sentinel (proc
, reason
)
2652 Lisp_Object proc
, reason
;
2654 Lisp_Object sentinel
;
2655 register struct Lisp_Process
*p
= XPROCESS (proc
);
2656 int count
= specpdl_ptr
- specpdl
;
2658 sentinel
= p
->sentinel
;
2659 if (NILP (sentinel
))
2662 /* Zilch the sentinel while it's running, to avoid recursive invocations;
2663 assure that it gets restored no matter how the sentinel exits. */
2665 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
2666 /* Inhibit quit so that random quits don't screw up a running filter. */
2667 specbind (Qinhibit_quit
, Qt
);
2668 call2 (sentinel
, proc
, reason
);
2672 /* Report all recent events of a change in process status
2673 (either run the sentinel or output a message).
2674 This is done while Emacs is waiting for keyboard input. */
2678 register Lisp_Object proc
, buffer
;
2679 Lisp_Object tail
= Qnil
;
2680 Lisp_Object msg
= Qnil
;
2681 struct gcpro gcpro1
, gcpro2
;
2683 /* We need to gcpro tail; if read_process_output calls a filter
2684 which deletes a process and removes the cons to which tail points
2685 from Vprocess_alist, and then causes a GC, tail is an unprotected
2689 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
2692 register struct Lisp_Process
*p
;
2694 proc
= Fcdr (Fcar (tail
));
2695 p
= XPROCESS (proc
);
2697 if (XINT (p
->tick
) != XINT (p
->update_tick
))
2699 XSETINT (p
->update_tick
, XINT (p
->tick
));
2701 /* If process is still active, read any output that remains. */
2702 if (XFASTINT (p
->infd
))
2703 while (read_process_output (proc
, XFASTINT (p
->infd
)) > 0);
2707 /* Get the text to use for the message. */
2708 if (!NILP (p
->raw_status_low
))
2710 msg
= status_message (p
->status
);
2712 /* If process is terminated, deactivate it or delete it. */
2714 if (XTYPE (p
->status
) == Lisp_Cons
)
2715 symbol
= XCONS (p
->status
)->car
;
2717 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
2718 || EQ (symbol
, Qclosed
))
2720 if (delete_exited_processes
)
2721 remove_process (proc
);
2723 deactivate_process (proc
);
2726 /* Now output the message suitably. */
2727 if (!NILP (p
->sentinel
))
2728 exec_sentinel (proc
, msg
);
2729 /* Don't bother with a message in the buffer
2730 when a process becomes runnable. */
2731 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
2733 Lisp_Object ro
= XBUFFER (buffer
)->read_only
;
2735 struct buffer
*old
= current_buffer
;
2738 /* Avoid error if buffer is deleted
2739 (probably that's why the process is dead, too) */
2740 if (NILP (XBUFFER (buffer
)->name
))
2742 Fset_buffer (buffer
);
2744 /* Insert new output into buffer
2745 at the current end-of-output marker,
2746 thus preserving logical ordering of input and output. */
2747 if (XMARKER (p
->mark
)->buffer
)
2748 SET_PT (marker_position (p
->mark
));
2751 if (point
<= opoint
)
2752 opoint
+= XSTRING (msg
)->size
+ XSTRING (p
->name
)->size
+ 10;
2754 tem
= current_buffer
->read_only
;
2755 current_buffer
->read_only
= Qnil
;
2756 insert_string ("\nProcess ");
2757 Finsert (1, &p
->name
);
2758 insert_string (" ");
2760 current_buffer
->read_only
= tem
;
2761 Fset_marker (p
->mark
, make_number (point
), p
->buffer
);
2764 set_buffer_internal (old
);
2769 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
2770 redisplay_preserve_echo_area ();
2772 update_tick
= process_tick
;
2783 if (! noninteractive
|| initialized
)
2785 signal (SIGCHLD
, sigchld_handler
);
2788 FD_ZERO (&input_wait_mask
);
2789 FD_SET (0, &input_wait_mask
);
2790 Vprocess_alist
= Qnil
;
2791 for (i
= 0; i
< MAXDESC
; i
++)
2793 chan_process
[i
] = Qnil
;
2794 proc_buffered_char
[i
] = -1;
2798 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 0, 1, 0,
2799 "Return the connection type of `PROCESS'. This can be nil (pipe),\n\
2800 t or pty (pty) or stream (socket connection).")
2802 Lisp_Object process
;
2804 return XPROCESS (process
)->type
;
2810 pty_process
= intern ("pty");
2813 stream_process
= intern ("stream");
2815 Qprocessp
= intern ("processp");
2816 staticpro (&Qprocessp
);
2817 Qrun
= intern ("run");
2819 Qstop
= intern ("stop");
2821 Qsignal
= intern ("signal");
2822 staticpro (&Qsignal
);
2824 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
2827 Qexit = intern ("exit");
2828 staticpro (&Qexit); */
2830 Qopen
= intern ("open");
2832 Qclosed
= intern ("closed");
2833 staticpro (&Qclosed
);
2835 staticpro (&Vprocess_alist
);
2837 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
2838 "*Non-nil means delete processes immediately when they exit.\n\
2839 nil means don't delete them until `list-processes' is run.");
2841 delete_exited_processes
= 1;
2843 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
2844 "Control type of device used to communicate with subprocesses.\n\
2845 Values are nil to use a pipe, and t or 'pty for a pty. Note that if\n\
2846 pty's are not available, this variable will be ignored. The value takes\n\
2847 effect when `start-process' is called.");
2848 Vprocess_connection_type
= Qt
;
2850 defsubr (&Sprocessp
);
2851 defsubr (&Sget_process
);
2852 defsubr (&Sget_buffer_process
);
2853 defsubr (&Sdelete_process
);
2854 defsubr (&Sprocess_status
);
2855 defsubr (&Sprocess_exit_status
);
2856 defsubr (&Sprocess_id
);
2857 defsubr (&Sprocess_name
);
2858 defsubr (&Sprocess_command
);
2859 defsubr (&Sset_process_buffer
);
2860 defsubr (&Sprocess_buffer
);
2861 defsubr (&Sprocess_mark
);
2862 defsubr (&Sset_process_filter
);
2863 defsubr (&Sprocess_filter
);
2864 defsubr (&Sset_process_sentinel
);
2865 defsubr (&Sprocess_sentinel
);
2866 defsubr (&Sprocess_kill_without_query
);
2867 defsubr (&Slist_processes
);
2868 defsubr (&Sprocess_list
);
2869 defsubr (&Sstart_process
);
2871 defsubr (&Sopen_network_stream
);
2872 #endif /* HAVE_SOCKETS */
2873 defsubr (&Saccept_process_output
);
2874 defsubr (&Sprocess_send_region
);
2875 defsubr (&Sprocess_send_string
);
2876 defsubr (&Sinterrupt_process
);
2877 defsubr (&Skill_process
);
2878 defsubr (&Squit_process
);
2879 defsubr (&Sstop_process
);
2880 defsubr (&Scontinue_process
);
2881 defsubr (&Sprocess_send_eof
);
2882 defsubr (&Ssignal_process
);
2883 defsubr (&Swaiting_for_user_input_p
);
2884 /* defsubr (&Sprocess_connection); */
2887 #endif /* subprocesses */