1 /* Asynchronous subprocess control for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1992 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
25 /* This file is split into two parts by the following preprocessor
26 conditional. The 'then' clause contains all of the support for
27 asynchronous subprocesses. The 'else' clause contains stub
28 versions of some of the asynchronous subprocess routines that are
29 often called elsewhere in Emacs, so we don't have to #ifdef the
30 sections that call them. */
38 #include <sys/types.h> /* some typedefs are used in sys/file.h */
42 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
43 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #endif /* HAVE_SOCKETS */
49 #if defined(BSD) || defined(STRIDE)
50 #include <sys/ioctl.h>
51 #if !defined (O_NDELAY) && defined (HAVE_PTYS)
53 #endif /* HAVE_PTYS and no O_NDELAY */
54 #endif /* BSD or STRIDE */
73 #include <sys/sysmacros.h> /* for "minor" */
78 #if defined (HPUX) && defined (HAVE_PTYS)
79 #include <sys/ptyio.h>
90 #include <sys/ttyhw.h>
91 #include <sys/stream.h>
97 #undef TIOCGETC /* Avoid confusing some conditionals that test this. */
100 #ifdef BROKEN_TIOCGETC
108 #include "termhooks.h"
109 #include "termopts.h"
110 #include "commands.h"
112 extern int screen_garbaged
;
114 Lisp_Object Qrun
, Qstop
, Qsignal
, Qopen
, Qclosed
;
115 /* Qexit is declared and initialized in eval.c. */
117 /* a process object is a network connection when its childp field is neither
118 Qt nor Qnil but is instead a string (name of foreign host we
119 are connected to + name of port we are connected to) */
122 static Lisp_Object stream_process
;
124 #define NETCONN_P(p) (XGCTYPE (XPROCESS (p)->childp) == Lisp_String)
126 #define NETCONN_P(p) 0
127 #endif /* HAVE_SOCKETS */
129 /* Define first descriptor number available for subprocesses. */
131 #define FIRST_PROC_DESC 1
133 #define FIRST_PROC_DESC 3
136 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
139 #if !defined (SIGCHLD) && defined (SIGCLD)
140 #define SIGCHLD SIGCLD
143 #include "syssignal.h"
145 /* Define the structure that the wait system call stores.
146 On many systems, there is a structure defined for this.
147 But on vanilla-ish USG systems there is not. */
151 #if !defined (BSD) && !defined (UNIPLUS) && !defined (STRIDE) && !(defined (HPUX) && !defined (NOMULTIPLEJOBS)) && !defined (HAVE_WAIT_HEADER)
154 #define WIFSTOPPED(w) ((w&0377) == 0177)
155 #define WIFSIGNALED(w) ((w&0377) != 0177 && (w&~0377) == 0)
156 #define WIFEXITED(w) ((w&0377) == 0)
157 #define WRETCODE(w) (w >> 8)
158 #define WSTOPSIG(w) (w >> 8)
159 #define WCOREDUMP(w) ((w&0200) != 0)
160 #define WTERMSIG(w) (w & 0377)
165 #include <sys/wait.h>
166 #endif /* not BSD 4.1 */
168 #define WAITTYPE union wait
169 #define WRETCODE(w) w.w_retcode
170 #define WCOREDUMP(w) w.w_coredump
173 /* HPUX version 7 has broken definitions of these. */
182 #define WTERMSIG(w) w.w_termsig
185 #define WSTOPSIG(w) w.w_stopsig
188 #define WIFSTOPPED(w) (WTERMSIG (w) == 0177)
191 #define WIFSIGNALED(w) (WTERMSIG (w) != 0177 && (WSTOPSIG (w)) == 0)
194 #define WIFEXITED(w) (WTERMSIG (w) == 0)
196 #endif /* BSD or UNIPLUS or STRIDE */
197 #endif /* no WAITTYPE */
200 /* For the CMU PTY driver + */
201 #define DCL_PROMPT "$ "
211 extern char *sys_errlist
[];
215 extern char *sys_siglist
[];
217 char *sys_siglist
[] =
223 "illegal instruction",
227 "floating point exception",
230 "segmentation violation",
231 "bad argument to system call",
232 "write on a pipe with no one to read it",
234 "software termination signal from kill",
236 "sendable stop signal not from tty",
237 "stop signal from tty",
238 "continue a stopped process",
239 "child status has changed",
240 "background read attempted from control tty",
241 "background write attempted from control tty",
242 "input record available at control tty",
243 "exceeded CPU time limit",
244 "exceeded file size limit"
252 extern int comm_server
;
253 extern int net_listen_address
;
256 /* t means use pty, nil means use a pipe,
257 maybe other values to come. */
258 Lisp_Object Vprocess_connection_type
;
262 #include <sys/socket.h>
266 /* Number of events of change of status of a process. */
269 /* Number of events for which the user or sentinel has been notified. */
273 /* We could get this from param.h, but better not to depend on finding that.
274 And better not to risk that it might define other symbols used in this
277 #define SELECT_TYPE fd_set
278 #else /* no FD_SET */
280 #define SELECT_TYPE int
282 /* Define the macros to access a single-int bitmap of descriptors. */
283 #define FD_SET(n, p) (*(p) |= (1 << (n)))
284 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
285 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
286 #define FD_ZERO(p) (*(p) = 0)
287 #endif /* no FD_SET */
289 /* Mask of bits indicating the descriptors that we wait for input on */
291 SELECT_TYPE input_wait_mask
;
293 int delete_exited_processes
;
295 /* Indexed by descriptor, gives the process (if any) for that descriptor */
296 Lisp_Object chan_process
[MAXDESC
];
298 /* Alist of elements (NAME . PROCESS) */
299 Lisp_Object Vprocess_alist
;
301 Lisp_Object Qprocessp
;
303 Lisp_Object
get_process ();
305 /* Buffered-ahead input char from process, indexed by channel.
306 -1 means empty (no char is buffered).
307 Used on sys V where the only way to tell if there is any
308 output from the process is to read at least one char.
309 Always -1 on systems that support FIONREAD. */
311 int proc_buffered_char
[MAXDESC
];
313 /* Compute the Lisp form of the process status, p->status, from
314 the numeric status that was returned by `wait'. */
317 struct Lisp_Process
*p
;
319 union { int i
; WAITTYPE wt
; } u
;
320 u
.i
= XFASTINT (p
->raw_status_low
) + (XFASTINT (p
->raw_status_high
) << 16);
321 p
->status
= status_convert (u
.wt
);
322 p
->raw_status_low
= Qnil
;
323 p
->raw_status_high
= Qnil
;
326 /* Convert a process status work in Unix format to
327 the list that we use internally. */
334 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
335 else if (WIFEXITED (w
))
336 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
337 WCOREDUMP (w
) ? Qt
: Qnil
));
338 else if (WIFSIGNALED (w
))
339 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
340 WCOREDUMP (w
) ? Qt
: Qnil
));
345 /* Given a status-list, extract the three pieces of information
346 and store them individually through the three pointers. */
349 decode_status (l
, symbol
, code
, coredump
)
357 if (XTYPE (l
) == Lisp_Symbol
)
365 *symbol
= XCONS (l
)->car
;
366 tem
= XCONS (l
)->cdr
;
367 *code
= XFASTINT (XCONS (tem
)->car
);
368 tem
= XFASTINT (XCONS (tem
)->cdr
);
369 *coredump
= !NILP (tem
);
373 /* Return a string describing a process status list. */
376 status_message (status
)
381 Lisp_Object string
, string2
;
383 decode_status (status
, &symbol
, &code
, &coredump
);
385 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
387 string
= build_string (code
< NSIG
? sys_siglist
[code
] : "unknown");
388 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
389 XSTRING (string
)->data
[0] = DOWNCASE (XSTRING (string
)->data
[0]);
390 return concat2 (string
, string2
);
392 else if (EQ (symbol
, Qexit
))
395 return build_string ("finished\n");
396 string
= Fint_to_string (make_number (code
));
397 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
398 return concat2 (build_string ("exited abnormally with code "),
399 concat2 (string
, string2
));
402 return Fcopy_sequence (Fsymbol_name (symbol
));
408 /* Open an available pty, returning a file descriptor.
409 Return -1 on failure.
410 The file name of the terminal corresponding to the pty
411 is left in the variable pty_name. */
425 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
426 for (i
= 0; i
< 16; i
++)
429 #ifdef PTY_NAME_SPRINTF
433 sprintf (pty_name
, "/dev/ptym/pty%c%x", c
, i
);
436 sprintf (pty_name
, "/dev/pty%x", i
);
438 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
440 #endif /* not HPUX */
441 #endif /* no PTY_NAME_SPRINTF */
444 if (stat (pty_name
, &stb
) < 0)
447 fd
= open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
449 fd
= open (pty_name
, O_RDWR
| O_NDELAY
, 0);
451 #else /* Unusual IRIS code */
452 *ptyv
= open ("/dev/ptc", O_RDWR
| O_NDELAY
, 0);
455 if (fstat (fd
, &stb
) < 0)
461 /* check to make certain that both sides are available
462 this avoids a nasty yet stupid bug in rlogins */
463 #ifdef PTY_TTY_NAME_SPRINTF
466 /* TODO: In version 19, make these special cases use the macro above. */
468 sprintf (pty_name
, "/dev/pty/tty%c%x", c
, i
);
471 sprintf (pty_name
, "/dev/ttyp%x", i
);
474 sprintf (pty_name
, "/dev/ttyq%d", minor (stb
.st_rdev
));
476 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
477 #endif /* not IRIS */
479 #endif /* not HPUX */
480 #endif /* no PTY_TTY_NAME_SPRINTF */
482 if (access (pty_name
, 6) != 0)
491 #endif /* not UNIPLUS */
498 #endif /* HAVE_PTYS */
504 register Lisp_Object val
, tem
, name1
;
505 register struct Lisp_Process
*p
;
509 /* size of process structure includes the vector header,
510 so deduct for that. But struct Lisp_Vector includes the first
511 element, thus deducts too much, so add it back. */
512 val
= Fmake_vector (make_number ((sizeof (struct Lisp_Process
)
513 - sizeof (struct Lisp_Vector
)
514 + sizeof (Lisp_Object
))
515 / sizeof (Lisp_Object
)),
517 XSETTYPE (val
, Lisp_Process
);
520 XFASTINT (p
->infd
) = 0;
521 XFASTINT (p
->outfd
) = 0;
522 XFASTINT (p
->pid
) = 0;
523 XFASTINT (p
->tick
) = 0;
524 XFASTINT (p
->update_tick
) = 0;
525 p
->raw_status_low
= Qnil
;
526 p
->raw_status_high
= Qnil
;
528 p
->mark
= Fmake_marker ();
530 /* If name is already in use, modify it until it is unused. */
535 tem
= Fget_process (name1
);
536 if (NILP (tem
)) break;
537 sprintf (suffix
, "<%d>", i
);
538 name1
= concat2 (name
, build_string (suffix
));
542 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
546 remove_process (proc
)
547 register Lisp_Object proc
;
549 register Lisp_Object pair
;
551 pair
= Frassq (proc
, Vprocess_alist
);
552 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
553 Fset_marker (XPROCESS (proc
)->mark
, Qnil
, Qnil
);
555 deactivate_process (proc
);
558 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
559 "Return t if OBJECT is a process.")
563 return XTYPE (obj
) == Lisp_Process
? Qt
: Qnil
;
566 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
567 "Return the process named NAME, or nil if there is none.")
569 register Lisp_Object name
;
571 if (XTYPE (name
) == Lisp_Process
)
573 CHECK_STRING (name
, 0);
574 return Fcdr (Fassoc (name
, Vprocess_alist
));
577 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
578 "Return the (or, a) process associated with BUFFER.\n\
579 BUFFER may be a buffer or the name of one.")
581 register Lisp_Object name
;
583 register Lisp_Object buf
, tail
, proc
;
585 if (NILP (name
)) return Qnil
;
586 buf
= Fget_buffer (name
);
587 if (NILP (buf
)) return Qnil
;
589 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
591 proc
= Fcdr (Fcar (tail
));
592 if (XTYPE (proc
) == Lisp_Process
&& EQ (XPROCESS (proc
)->buffer
, buf
))
598 /* This is how commands for the user decode process arguments */
602 register Lisp_Object name
;
604 register Lisp_Object proc
;
606 proc
= Fget_buffer_process (Fcurrent_buffer ());
609 proc
= Fget_process (name
);
611 proc
= Fget_buffer_process (Fget_buffer (name
));
618 error ("Current buffer has no process");
620 error ("Process %s does not exist", XSTRING (name
)->data
);
624 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
625 "Delete PROCESS: kill it and forget about it immediately.\n\
626 PROCESS may be a process or the name of one, or a buffer name.")
628 register Lisp_Object proc
;
630 proc
= get_process (proc
);
631 XPROCESS (proc
)->raw_status_low
= Qnil
;
632 XPROCESS (proc
)->raw_status_high
= Qnil
;
633 if (NETCONN_P (proc
))
635 XPROCESS (proc
)->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
636 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
638 else if (XFASTINT (XPROCESS (proc
)->infd
))
640 Fkill_process (proc
, Qnil
);
641 /* Do this now, since remove_process will make sigchld_handler do nothing. */
642 XPROCESS (proc
)->status
643 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
644 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
647 remove_process (proc
);
651 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
652 "Return the status of PROCESS: a symbol, one of these:\n\
653 run -- for a process that is running.\n\
654 stop -- for a process stopped but continuable.\n\
655 exit -- for a process that has exited.\n\
656 signal -- for a process that has got a fatal signal.\n\
657 open -- for a network stream connection that is open.\n\
658 closed -- for a network stream connection that is closed.\n\
659 nil -- if arg is a process name and no such process exists.")
660 /* command -- for a command channel opened to Emacs by another process.\n\
661 external -- for an i/o channel opened to Emacs by another process.\n\ */
663 register Lisp_Object proc
;
665 register struct Lisp_Process
*p
;
666 register Lisp_Object status
;
667 proc
= Fget_process (proc
);
671 if (!NILP (p
->raw_status_low
))
674 if (XTYPE (status
) == Lisp_Cons
)
675 status
= XCONS (status
)->car
;
676 if (NETCONN_P (proc
))
678 if (EQ (status
, Qrun
))
680 else if (EQ (status
, Qexit
))
686 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
688 "Return the exit status of PROCESS or the signal number that killed it.\n\
689 If PROCESS has not yet exited or died, return 0.")
691 register Lisp_Object proc
;
693 CHECK_PROCESS (proc
, 0);
694 if (!NILP (XPROCESS (proc
)->raw_status_low
))
695 update_status (XPROCESS (proc
));
696 if (XTYPE (XPROCESS (proc
)->status
) == Lisp_Cons
)
697 return XCONS (XCONS (XPROCESS (proc
)->status
)->cdr
)->car
;
698 return make_number (0);
701 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
702 "Return the process id of PROCESS.\n\
703 This is the pid of the Unix process which PROCESS uses or talks to.\n\
704 For a network connection, this value is nil.")
706 register Lisp_Object proc
;
708 CHECK_PROCESS (proc
, 0);
709 return XPROCESS (proc
)->pid
;
712 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
713 "Return the name of PROCESS, as a string.\n\
714 This is the name of the program invoked in PROCESS,\n\
715 possibly modified to make it unique among process names.")
717 register Lisp_Object proc
;
719 CHECK_PROCESS (proc
, 0);
720 return XPROCESS (proc
)->name
;
723 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
724 "Return the command that was executed to start PROCESS.\n\
725 This is a list of strings, the first string being the program executed\n\
726 and the rest of the strings being the arguments given to it.\n\
727 For a non-child channel, this is nil.")
729 register Lisp_Object proc
;
731 CHECK_PROCESS (proc
, 0);
732 return XPROCESS (proc
)->command
;
735 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
737 "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
739 register Lisp_Object proc
, buffer
;
741 CHECK_PROCESS (proc
, 0);
743 CHECK_BUFFER (buffer
, 1);
744 XPROCESS (proc
)->buffer
= buffer
;
748 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
750 "Return the buffer PROCESS is associated with.\n\
751 Output from PROCESS is inserted in this buffer\n\
752 unless PROCESS has a filter.")
754 register Lisp_Object proc
;
756 CHECK_PROCESS (proc
, 0);
757 return XPROCESS (proc
)->buffer
;
760 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
762 "Return the marker for the end of the last output from PROCESS.")
764 register Lisp_Object proc
;
766 CHECK_PROCESS (proc
, 0);
767 return XPROCESS (proc
)->mark
;
770 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
772 "Give PROCESS the filter function FILTER; nil means no filter.\n\
773 When a process has a filter, each time it does output\n\
774 the entire string of output is passed to the filter.\n\
775 The filter gets two arguments: the process and the string of output.\n\
776 If the process has a filter, its buffer is not used for output.")
778 register Lisp_Object proc
, filter
;
780 CHECK_PROCESS (proc
, 0);
781 XPROCESS (proc
)->filter
= filter
;
785 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
787 "Returns the filter function of PROCESS; nil if none.\n\
788 See `set-process-filter' for more info on filter functions.")
790 register Lisp_Object proc
;
792 CHECK_PROCESS (proc
, 0);
793 return XPROCESS (proc
)->filter
;
796 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
798 "Give PROCESS the sentinel SENTINEL; nil for none.\n\
799 The sentinel is called as a function when the process changes state.\n\
800 It gets two arguments: the process, and a string describing the change.")
802 register Lisp_Object proc
, sentinel
;
804 CHECK_PROCESS (proc
, 0);
805 XPROCESS (proc
)->sentinel
= sentinel
;
809 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
811 "Return the sentinel of PROCESS; nil if none.\n\
812 See `set-process-sentinel' for more info on sentinels.")
814 register Lisp_Object proc
;
816 CHECK_PROCESS (proc
, 0);
817 return XPROCESS (proc
)->sentinel
;
820 DEFUN ("process-kill-without-query", Fprocess_kill_without_query
,
821 Sprocess_kill_without_query
, 1, 2, 0,
822 "Say no query needed if PROCESS is running when Emacs is exited.\n\
823 Optional second argument if non-nill says to require a query.\n\
824 Value is t if a query was formerly required.")
826 register Lisp_Object proc
, value
;
830 CHECK_PROCESS (proc
, 0);
831 tem
= XPROCESS (proc
)->kill_without_query
;
832 XPROCESS (proc
)->kill_without_query
= Fnull (value
);
840 register Lisp_Object tail
, tem
;
841 Lisp_Object proc
, minspace
, tem1
;
842 register struct buffer
*old
= current_buffer
;
843 register struct Lisp_Process
*p
;
847 XFASTINT (minspace
) = 1;
849 set_buffer_internal (XBUFFER (Vstandard_output
));
850 Fbuffer_disable_undo (Vstandard_output
);
852 current_buffer
->truncate_lines
= Qt
;
855 Proc Status Buffer Command\n\
856 ---- ------ ------ -------\n", -1);
858 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
862 proc
= Fcdr (Fcar (tail
));
864 if (NILP (p
->childp
))
867 Finsert (1, &p
->name
);
868 Findent_to (make_number (13), minspace
);
870 if (!NILP (p
->raw_status_low
))
873 if (XTYPE (p
->status
) == Lisp_Cons
)
874 symbol
= XCONS (p
->status
)->car
;
877 if (EQ (symbol
, Qsignal
))
880 tem
= Fcar (Fcdr (p
->status
));
882 if (XINT (tem
) < NSIG
)
883 write_string (sys_siglist
[XINT (tem
)], -1);
886 Fprinc (symbol
, Qnil
);
888 else if (NETCONN_P (proc
))
890 if (EQ (symbol
, Qrun
))
891 write_string ("open", -1);
892 else if (EQ (symbol
, Qexit
))
893 write_string ("closed", -1);
895 Fprinc (symbol
, Qnil
);
898 Fprinc (symbol
, Qnil
);
900 if (EQ (symbol
, Qexit
))
903 tem
= Fcar (Fcdr (p
->status
));
906 sprintf (tembuf
, " %d", XFASTINT (tem
));
907 write_string (tembuf
, -1);
911 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
912 remove_process (proc
);
914 Findent_to (make_number (22), minspace
);
915 if (NILP (p
->buffer
))
916 insert_string ("(none)");
917 else if (NILP (XBUFFER (p
->buffer
)->name
))
918 insert_string ("(Killed)");
920 Finsert (1, &XBUFFER (p
->buffer
)->name
);
922 Findent_to (make_number (37), minspace
);
924 if (NETCONN_P (proc
))
926 sprintf (tembuf
, "(network stream connection to %s)\n",
927 XSTRING (p
->childp
)->data
);
928 insert_string (tembuf
);
942 insert_string ("\n");
948 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 0, "",
949 "Display a list of all processes.\n\
950 \(Any processes listed as Exited or Signaled are actually eliminated\n\
951 after the listing is made.)")
954 internal_with_output_to_temp_buffer ("*Process List*",
955 list_processes_1
, Qnil
);
959 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
960 "Return a list of all processes.")
963 return Fmapcar (Qcdr
, Vprocess_alist
);
966 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
967 "Start a program in a subprocess. Return the process object for it.\n\
968 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
969 NAME is name for process. It is modified if necessary to make it unique.\n\
970 BUFFER is the buffer or (buffer-name) to associate with the process.\n\
971 Process output goes at end of that buffer, unless you specify\n\
972 an output stream or filter function to handle the output.\n\
973 BUFFER may be also nil, meaning that this process is not associated\n\
975 Third arg is program file name. It is searched for as in the shell.\n\
976 Remaining arguments are strings to give program as arguments.")
979 register Lisp_Object
*args
;
981 Lisp_Object buffer
, name
, program
, proc
, tem
;
983 register unsigned char *new_argv
;
986 register unsigned char **new_argv
;
992 buffer
= Fget_buffer_create (buffer
);
995 CHECK_STRING (name
, 0);
999 CHECK_STRING (program
, 2);
1002 /* Make a one member argv with all args concatenated
1003 together separated by a blank. */
1004 len
= XSTRING (program
)->size
+ 2;
1005 for (i
= 3; i
< nargs
; i
++)
1008 CHECK_STRING (tem
, i
);
1009 len
+= XSTRING (tem
)->size
+ 1; /* count the blank */
1011 new_argv
= (unsigned char *) alloca (len
);
1012 strcpy (new_argv
, XSTRING (program
)->data
);
1013 for (i
= 3; i
< nargs
; i
++)
1016 CHECK_STRING (tem
, i
);
1017 strcat (new_argv
, " ");
1018 strcat (new_argv
, XSTRING (tem
)->data
);
1021 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1023 for (i
= 3; i
< nargs
; i
++)
1026 CHECK_STRING (tem
, i
);
1027 new_argv
[i
- 2] = XSTRING (tem
)->data
;
1029 new_argv
[i
- 2] = 0;
1030 new_argv
[0] = XSTRING (program
)->data
;
1032 /* If program file name is not absolute, search our path for it */
1033 if (new_argv
[0][0] != '/')
1036 openp (Vexec_path
, program
, "", &tem
, 1);
1038 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1039 new_argv
[0] = XSTRING (tem
)->data
;
1041 #endif /* not VMS */
1043 proc
= make_process (name
);
1045 XPROCESS (proc
)->childp
= Qt
;
1046 XPROCESS (proc
)->command_channel_p
= Qnil
;
1047 XPROCESS (proc
)->buffer
= buffer
;
1048 XPROCESS (proc
)->sentinel
= Qnil
;
1049 XPROCESS (proc
)->filter
= Qnil
;
1050 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1052 create_process (proc
, new_argv
);
1058 create_process_1 (signo
)
1062 /* USG systems forget handlers when they are used;
1063 must reestablish each time */
1064 signal (signo
, create_process_1
);
1068 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1071 /* Mimic blocking of signals on system V, which doesn't really have it. */
1073 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1074 int sigchld_deferred
;
1077 create_process_sigchld ()
1079 signal (SIGCHLD
, create_process_sigchld
);
1081 sigchld_deferred
= 1;
1087 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1088 create_process (process
, new_argv
)
1089 Lisp_Object process
;
1092 int pid
, inchannel
, outchannel
, forkin
, forkout
;
1095 SIGTYPE (*sigchld
)();
1098 Lisp_Object current_dir
;
1100 extern char **environ
;
1104 inchannel
= outchannel
= -1;
1107 if (EQ (Vprocess_connection_type
, Qt
))
1108 outchannel
= inchannel
= allocate_pty ();
1110 /* Make sure that the child will be able to chdir to the current
1111 buffer's current directory. We can't just have the child check
1112 for an error when it does the chdir, since it's in a vfork. */
1113 current_dir
= expand_and_dir_to_file (current_buffer
->directory
, Qnil
);
1114 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1115 report_file_error ("Setting current directory",
1116 Fcons (current_buffer
->directory
, Qnil
));
1121 /* On USG systems it does not work to open the pty's tty here
1122 and then close and reopen it in the child. */
1124 /* Don't let this terminal become our controlling terminal
1125 (in case we don't have one). */
1126 forkout
= forkin
= open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1128 forkout
= forkin
= open (pty_name
, O_RDWR
, 0);
1131 report_file_error ("Opening pty", Qnil
);
1133 forkin
= forkout
= -1;
1134 #endif /* not USG */
1138 #endif /* HAVE_PTYS */
1141 if (socketpair (AF_UNIX
, SOCK_STREAM
, 0, sv
) < 0)
1142 report_file_error ("Opening socketpair", Qnil
);
1143 outchannel
= inchannel
= sv
[0];
1144 forkout
= forkin
= sv
[1];
1146 #else /* not SKTPAIR */
1155 #endif /* not SKTPAIR */
1158 /* Replaced by close_process_descs */
1159 set_exclusive_use (inchannel
);
1160 set_exclusive_use (outchannel
);
1163 /* Stride people say it's a mystery why this is needed
1164 as well as the O_NDELAY, but that it fails without this. */
1165 #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
1168 ioctl (inchannel
, FIONBIO
, &one
);
1173 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1176 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1180 /* Record this as an active process, with its channels.
1181 As a result, child_setup will close Emacs's side of the pipes. */
1182 chan_process
[inchannel
] = process
;
1183 XFASTINT (XPROCESS (process
)->infd
) = inchannel
;
1184 XFASTINT (XPROCESS (process
)->outfd
) = outchannel
;
1185 /* Record the tty descriptor used in the subprocess. */
1187 XPROCESS (process
)->subtty
= Qnil
;
1189 XFASTINT (XPROCESS (process
)->subtty
) = forkin
;
1190 XPROCESS (process
)->pty_flag
= (pty_flag
? Qt
: Qnil
);
1191 XPROCESS (process
)->status
= Qrun
;
1193 /* Delay interrupts until we have a chance to store
1194 the new fork's pid in its process structure */
1198 #else /* not BSD4_1 */
1199 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1200 sigsetmask (sigmask (SIGCHLD
));
1201 #else /* ordinary USG */
1203 sigchld_deferred
= 0;
1204 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
1206 #endif /* ordinary USG */
1207 #endif /* not BSD4_1 */
1208 #endif /* SIGCHLD */
1210 /* Until we store the proper pid, enable sigchld_handler
1211 to recognize an unknown pid as standing for this process.
1212 It is very important not to let this `marker' value stay
1213 in the table after this function has returned; if it does
1214 it might cause call-process to hang and subsequent asynchronous
1215 processes to get their return values scrambled. */
1216 XSETINT (XPROCESS (process
)->pid
, -1);
1219 /* child_setup must clobber environ on systems with true vfork.
1220 Protect it from permanent change. */
1221 char **save_environ
= environ
;
1226 int xforkin
= forkin
;
1227 int xforkout
= forkout
;
1229 #if 0 /* This was probably a mistake--it duplicates code later on,
1230 but fails to handle all the cases. */
1231 /* Make sure SIGCHLD is not blocked in the child. */
1232 sigsetmask (SIGEMPTYMASK
);
1235 /* Make the pty be the controlling terminal of the process. */
1237 /* First, disconnect its current controlling terminal. */
1240 #else /* not HAVE_SETSID */
1242 /* It's very important to call setpgrp() here and no time
1243 afterwards. Otherwise, we lose our controlling tty which
1244 is set when we open the pty. */
1247 #endif /* not HAVE_SETSID */
1249 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1250 can do TIOCSPGRP only to the process's controlling tty. */
1253 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1254 I can't test it since I don't have 4.3. */
1255 int j
= open ("/dev/tty", O_RDWR
, 0);
1256 ioctl (j
, TIOCNOTTY
, 0);
1259 /* In order to get a controlling terminal on some versions
1260 of BSD, it is necessary to put the process in pgrp 0
1261 before it opens the terminal. */
1265 #endif /* TIOCNOTTY */
1267 #if !defined (RTU) && !defined (UNIPLUS)
1268 /*** There is a suggestion that this ought to be a
1269 conditional on TIOCSPGRP. */
1270 /* Now close the pty (if we had it open) and reopen it.
1271 This makes the pty the controlling terminal of the subprocess. */
1274 /* I wonder if close (open (pty_name, ...)) would work? */
1277 xforkout
= xforkin
= open (pty_name
, O_RDWR
, 0);
1282 #endif /* not UNIPLUS and not RTU */
1283 #ifdef SETUP_SLAVE_PTY
1285 #endif /* SETUP_SLAVE_PTY */
1287 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
1288 Now reenable it in the child, so it will die when we want it to. */
1290 signal (SIGHUP
, SIG_DFL
);
1292 #endif /* HAVE_PTYS */
1297 #else /* not BSD4_1 */
1298 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1299 sigsetmask (SIGEMPTYMASK
);
1300 #else /* ordinary USG */
1301 signal (SIGCHLD
, sigchld
);
1302 #endif /* ordinary USG */
1303 #endif /* not BSD4_1 */
1304 #endif /* SIGCHLD */
1306 child_setup_tty (xforkout
);
1307 child_setup (xforkin
, xforkout
, xforkout
,
1308 new_argv
, env
, 1, current_dir
);
1310 environ
= save_environ
;
1315 remove_process (process
);
1316 report_file_error ("Doing vfork", Qnil
);
1319 XFASTINT (XPROCESS (process
)->pid
) = pid
;
1321 FD_SET (inchannel
, &input_wait_mask
);
1323 /* If the subfork execv fails, and it exits,
1324 this close hangs. I don't know why.
1325 So have an interrupt jar it loose. */
1327 signal (SIGALRM
, create_process_1
);
1330 /* OK to close only if it's not a pty. Otherwise we need to leave
1331 it open for ioctl to get pgrp when signals are sent, or to send
1332 the interrupt characters through if that's how we're signalling
1333 subprocesses. Alternately if you are concerned about running out
1334 of file descriptors, you could just save the tty name and open
1335 just to do the ioctl. */
1336 if (NILP (XFASTINT (XPROCESS (process
)->pty_flag
)))
1339 XPROCESS (process
)->subtty
= Qnil
;
1345 if (forkin
!= forkout
&& forkout
>= 0)
1351 #else /* not BSD4_1 */
1352 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1353 sigsetmask (SIGEMPTYMASK
);
1354 #else /* ordinary USG */
1356 signal (SIGCHLD
, sigchld
);
1357 /* Now really handle any of these signals
1358 that came in during this function. */
1359 if (sigchld_deferred
)
1360 kill (getpid (), SIGCHLD
);
1362 #endif /* ordinary USG */
1363 #endif /* not BSD4_1 */
1364 #endif /* SIGCHLD */
1366 #endif /* not VMS */
1370 /* open a TCP network connection to a given HOST/SERVICE. Treated
1371 exactly like a normal process when reading and writing. Only
1372 differences are in status display and process deletion. A network
1373 connection has no PID; you cannot signal it. All you can do is
1374 deactivate and close it via delete-process */
1376 DEFUN ("open-network-stream", Fopen_network_stream
, Sopen_network_stream
,
1378 "Open a TCP connection for a service to a host.\n\
1379 Returns a subprocess-object to represent the connection.\n\
1380 Input and output work as for subprocesses; `delete-process' closes it.\n\
1381 Args are NAME BUFFER HOST SERVICE.\n\
1382 NAME is name for process. It is modified if necessary to make it unique.\n\
1383 BUFFER is the buffer (or buffer-name) to associate with the process.\n\
1384 Process output goes at end of that buffer, unless you specify\n\
1385 an output stream or filter function to handle the output.\n\
1386 BUFFER may be also nil, meaning that this process is not associated\n\
1388 Third arg is name of the host to connect to, or its IP address.\n\
1389 Fourth arg SERVICE is name of the service desired, or an integer\n\
1390 specifying a port number to connect to.")
1391 (name
, buffer
, host
, service
)
1392 Lisp_Object name
, buffer
, host
, service
;
1396 struct sockaddr_in address
;
1397 struct servent
*svc_info
;
1398 struct hostent
*host_info_ptr
, host_info
;
1399 char *(addr_list
[2]);
1400 unsigned long numeric_addr
;
1404 struct hostent host_info_fixed
;
1405 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1407 GCPRO4 (name
, buffer
, host
, service
);
1408 CHECK_STRING (name
, 0);
1409 CHECK_STRING (host
, 0);
1410 if (XTYPE(service
) == Lisp_Int
)
1411 port
= htons ((unsigned short) XINT (service
));
1414 CHECK_STRING (service
, 0);
1415 svc_info
= getservbyname (XSTRING (service
)->data
, "tcp");
1417 error ("Unknown service \"%s\"", XSTRING (service
)->data
);
1418 port
= svc_info
->s_port
;
1421 host_info_ptr
= gethostbyname (XSTRING (host
)->data
);
1422 if (host_info_ptr
== 0)
1423 /* Attempt to interpret host as numeric inet address */
1425 numeric_addr
= inet_addr (XSTRING (host
)->data
);
1426 if (numeric_addr
== -1)
1427 error ("Unknown host \"%s\"", XSTRING (host
)->data
);
1429 host_info_ptr
= &host_info
;
1430 host_info
.h_name
= 0;
1431 host_info
.h_aliases
= 0;
1432 host_info
.h_addrtype
= AF_INET
;
1433 host_info
.h_addr_list
= &(addr_list
[0]);
1434 addr_list
[0] = (char*)(&numeric_addr
);
1436 host_info
.h_length
= strlen (addr_list
[0]);
1439 bzero (&address
, sizeof address
);
1440 bcopy (host_info_ptr
->h_addr
, (char *) &address
.sin_addr
,
1441 host_info_ptr
->h_length
);
1442 address
.sin_family
= host_info_ptr
->h_addrtype
;
1443 address
.sin_port
= port
;
1445 s
= socket (host_info_ptr
->h_addrtype
, SOCK_STREAM
, 0);
1447 report_file_error ("error creating socket", Fcons (name
, Qnil
));
1450 if (connect (s
, &address
, sizeof address
) == -1)
1457 report_file_error ("connection failed",
1458 Fcons (host
, Fcons (name
, Qnil
)));
1464 report_file_error ("error duplicating socket", Fcons (name
, Qnil
));
1467 buffer
= Fget_buffer_create (buffer
);
1468 proc
= make_process (name
);
1470 chan_process
[inch
] = proc
;
1473 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
1476 fcntl (inch
, F_SETFL
, O_NDELAY
);
1480 XPROCESS (proc
)->childp
= host
;
1481 XPROCESS (proc
)->command_channel_p
= Qnil
;
1482 XPROCESS (proc
)->buffer
= buffer
;
1483 XPROCESS (proc
)->sentinel
= Qnil
;
1484 XPROCESS (proc
)->filter
= Qnil
;
1485 XPROCESS (proc
)->command
= Qnil
;
1486 XPROCESS (proc
)->pid
= Qnil
;
1487 XFASTINT (XPROCESS (proc
)->infd
) = s
;
1488 XFASTINT (XPROCESS (proc
)->outfd
) = outch
;
1489 XPROCESS (proc
)->status
= Qrun
;
1490 FD_SET (inch
, &input_wait_mask
);
1495 #endif /* HAVE_SOCKETS */
1497 deactivate_process (proc
)
1500 register int inchannel
, outchannel
;
1501 register struct Lisp_Process
*p
= XPROCESS (proc
);
1503 inchannel
= XFASTINT (p
->infd
);
1504 outchannel
= XFASTINT (p
->outfd
);
1508 /* Beware SIGCHLD hereabouts. */
1509 flush_pending_output (inchannel
);
1512 VMS_PROC_STUFF
*get_vms_process_pointer (), *vs
;
1513 sys$
dassgn (outchannel
);
1514 vs
= get_vms_process_pointer (p
->pid
)
1516 give_back_vms_process_stuff (vs
);
1520 if (outchannel
&& outchannel
!= inchannel
)
1524 XFASTINT (p
->infd
) = 0;
1525 XFASTINT (p
->outfd
) = 0;
1526 chan_process
[inchannel
] = Qnil
;
1527 FD_CLR (inchannel
, &input_wait_mask
);
1531 /* Close all descriptors currently in use for communication
1532 with subprocess. This is used in a newly-forked subprocess
1533 to get rid of irrelevant descriptors. */
1535 close_process_descs ()
1538 for (i
= 0; i
< MAXDESC
; i
++)
1540 Lisp_Object process
;
1541 process
= chan_process
[i
];
1542 if (!NILP (process
))
1544 int in
= XFASTINT (XPROCESS (process
)->infd
);
1545 int out
= XFASTINT (XPROCESS (process
)->outfd
);
1548 if (out
&& in
!= out
)
1554 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
1556 "Allow any pending output from subprocesses to be read by Emacs.\n\
1557 It is read into the process' buffers or given to their filter functions.\n\
1558 Non-nil arg PROCESS means do not return until some output has been received\n\
1560 Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of\n\
1561 seconds and microseconds to wait; return after that much time whether\n\
1562 or not there is input.\n\
1563 Return non-nil iff we received any output before the timeout expired.")
1564 (proc
, timeout
, timeout_msecs
)
1565 register Lisp_Object proc
, timeout
, timeout_msecs
;
1570 if (! NILP (timeout_msecs
))
1572 CHECK_NUMBER (timeout_msecs
, 2);
1573 useconds
= XINT (timeout_msecs
);
1574 if (XTYPE (timeout
) != Lisp_Int
)
1575 XSET (timeout
, Lisp_Int
, 0);
1578 int carry
= useconds
/ 1000000;
1580 XSETINT (timeout
, XINT (timeout
) + carry
);
1581 useconds
-= carry
* 1000000;
1583 /* I think this clause is necessary because C doesn't
1584 guarantee a particular rounding direction for negative
1588 XSETINT (timeout
, XINT (timeout
) - 1);
1589 useconds
+= 1000000;
1594 if (! NILP (timeout
))
1596 CHECK_NUMBER (timeout
, 1);
1597 seconds
= XINT (timeout
);
1610 (wait_reading_process_input (seconds
, useconds
,
1612 ? XPROCESS (get_process (proc
)) : 0), 0)
1616 /* This variable is different from waiting_for_input in keyboard.c.
1617 It is used to communicate to a lisp process-filter/sentinel (via the
1618 function Fwaiting_for_user_input_p below) whether emacs was waiting
1619 for user-input when that process-filter was called.
1620 waiting_for_input cannot be used as that is by definition 0 when
1621 lisp code is being evalled */
1622 static int waiting_for_user_input_p
;
1624 /* Read and dispose of subprocess output while waiting for timeout to
1625 elapse and/or keyboard input to be available.
1628 timeout in seconds, or
1629 zero for no limit, or
1630 -1 means gobble data immediately available but don't wait for any.
1633 0 to ignore keyboard input, or
1634 1 to return when input is available, or
1635 -1 means caller will actually read the input, so don't throw to
1636 the quit handler, or
1637 a pointer to a struct Lisp_Process, meaning wait until something
1638 arrives from that process. The return value is true iff we read
1639 some input from that process.
1641 do_display != 0 means redisplay should be done to show subprocess
1642 output that arrives.
1644 If read_kbd is a pointer to a struct Lisp_Process, then the
1645 function returns true iff we received input from that process
1646 before the timeout elapsed.
1647 Otherwise, return true iff we recieved input from any process. */
1649 wait_reading_process_input (time_limit
, microsecs
, read_kbd
, do_display
)
1650 int time_limit
, microsecs
, read_kbd
, do_display
;
1652 register int channel
, nfds
, m
;
1653 static SELECT_TYPE Available
;
1656 EMACS_TIME timeout
, end_time
, garbage
;
1658 int wait_channel
= 0;
1659 struct Lisp_Process
*wait_proc
= 0;
1660 int got_some_input
= 0;
1662 FD_ZERO (&Available
);
1664 /* Detect when read_kbd is really the address of a Lisp_Process. */
1665 if (read_kbd
> 10 || read_kbd
< -1)
1667 wait_proc
= (struct Lisp_Process
*) read_kbd
;
1668 wait_channel
= XFASTINT (wait_proc
->infd
);
1672 waiting_for_user_input_p
= read_kbd
;
1674 /* Since we may need to wait several times,
1675 compute the absolute time to return at. */
1676 if (time_limit
|| microsecs
)
1678 EMACS_GET_TIME (end_time
);
1679 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
1680 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
1683 /* Turn off periodic alarms (in case they are in use)
1684 because the select emulator uses alarms. */
1689 /* If calling from keyboard input, do not quit
1690 since we want to return C-g as an input character.
1691 Otherwise, do pending quit if requested. */
1695 /* If status of something has changed, and no input is available,
1696 notify the user of the change right away */
1697 if (update_tick
!= process_tick
&& do_display
)
1699 Atemp
= input_wait_mask
;
1700 EMACS_SET_SECS_USECS (timeout
, 0, 0);
1701 if (select (MAXDESC
, &Atemp
, 0, 0, &timeout
) <= 0)
1705 /* Don't wait for output from a non-running process. */
1706 if (wait_proc
!= 0 && !NILP (wait_proc
->raw_status_low
))
1707 update_status (wait_proc
);
1709 && ! EQ (wait_proc
->status
, Qrun
))
1712 /* Compute time from now till when time limit is up */
1713 /* Exit if already run out */
1714 if (time_limit
== -1)
1716 /* -1 specified for timeout means
1717 gobble output available now
1718 but don't wait at all. */
1720 EMACS_SET_SECS_USECS (timeout
, 0, 0);
1722 else if (time_limit
|| microsecs
)
1724 EMACS_GET_TIME (timeout
);
1725 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
1726 if (EMACS_TIME_NEG_P (timeout
))
1731 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
1734 /* Cause C-g and alarm signals to take immediate action,
1735 and cause input available signals to zero out timeout */
1737 set_waiting_for_input (&timeout
);
1739 /* Wait till there is something to do */
1741 Available
= input_wait_mask
;
1743 FD_CLR (0, &Available
);
1745 /* If a screen has been newly mapped and needs updating,
1746 reprocess its display stuff. */
1747 if (screen_garbaged
)
1748 redisplay_preserve_echo_area ();
1750 if (read_kbd
&& detect_input_pending ())
1753 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); */
2888 #else /* not subprocesses */
2890 #include <sys/types.h>
2894 #include "systime.h"
2895 #include "termopts.h"
2897 extern int screen_garbaged
;
2900 /* As described above, except assuming that there are no subprocesses:
2902 Wait for timeout to elapse and/or keyboard input to be available.
2905 timeout in seconds, or
2906 zero for no limit, or
2907 -1 means gobble data immediately available but don't wait for any.
2910 0 to ignore keyboard input, or
2911 1 to return when input is available, or
2912 -1 means caller will actually read the input, so don't throw to
2914 We know that read_kbd will never be a Lisp_Process, since
2915 `subprocesses' isn't defined.
2917 do_display != 0 means redisplay should be done to show subprocess
2918 output that arrives. This version of the function ignores it.
2920 If read_kbd is a pointer to a struct Lisp_Process, then the
2921 function returns true iff we received input from that process
2922 before the timeout elapsed.
2923 Otherwise, return true iff we recieved input from any process. */
2926 wait_reading_process_input (time_limit
, microsecs
, read_kbd
, do_display
)
2927 int time_limit
, microsecs
, read_kbd
, do_display
;
2929 EMACS_TIME end_time
, timeout
, *timeout_p
;
2932 /* What does time_limit really mean? */
2933 if (time_limit
|| microsecs
)
2935 /* It's not infinite. */
2936 timeout_p
= &timeout
;
2938 if (time_limit
== -1)
2939 /* In fact, it's zero. */
2940 EMACS_SET_SECS_USECS (timeout
, 0, 0);
2942 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
2944 /* How far in the future is that? */
2945 EMACS_GET_TIME (end_time
);
2946 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
2949 /* It's infinite. */
2952 /* Turn off periodic alarms (in case they are in use)
2953 because the select emulator uses alarms. */
2960 waitchannels
= read_kbd
? 1 : 0;
2962 /* If calling from keyboard input, do not quit
2963 since we want to return C-g as an input character.
2964 Otherwise, do pending quit if requested. */
2970 EMACS_GET_TIME (*timeout_p
);
2971 EMACS_SUB_TIME (*timeout_p
, end_time
, *timeout_p
);
2972 if (EMACS_TIME_NEG_P (*timeout_p
))
2976 /* Cause C-g and alarm signals to take immediate action,
2977 and cause input available signals to zero out timeout. */
2979 set_waiting_for_input (&timeout
);
2981 /* If a screen has been newly mapped and needs updating,
2982 reprocess its display stuff. */
2983 if (screen_garbaged
)
2984 redisplay_preserve_echo_area ();
2986 if (read_kbd
&& detect_input_pending ())
2989 nfds
= select (1, &waitchannels
, 0, 0, timeout_p
);
2991 /* Make C-g and alarm signals set flags again */
2992 clear_waiting_for_input ();
2994 /* If we woke up due to SIGWINCH, actually change size now. */
2995 do_pending_window_change ();
2999 /* If the system call was interrupted, then go around the
3005 else if (nfds
> 0 && (waitchannels
& 1) && interrupt_input
)
3006 /* System sometimes fails to deliver SIGIO. */
3007 kill (getpid (), SIGIO
);
3009 if (read_kbd
&& interrupt_input
&& (waitchannels
& 1))
3012 /* If we have timed out (nfds == 0) or found some input (nfds > 0),
3022 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
3023 "Return the (or, a) process associated with BUFFER.\n\
3024 This copy of Emacs has not been built to support subprocesses, so this\n\
3025 function always returns nil.")
3027 register Lisp_Object name
;
3032 /* Kill all processes associated with `buffer'.
3033 If `buffer' is nil, kill all processes.
3034 Since we have no subprocesses, this does nothing. */
3036 kill_buffer_processes (buffer
)
3047 defsubr (&Sget_buffer_process
);
3051 #endif /* not subprocesses */