1 /* Asynchronous subprocess control for GNU Emacs.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 1996
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
27 /* This file is split into two parts by the following preprocessor
28 conditional. The 'then' clause contains all of the support for
29 asynchronous subprocesses. The 'else' clause contains stub
30 versions of some of the asynchronous subprocess routines that are
31 often called elsewhere in Emacs, so we don't have to #ifdef the
32 sections that call them. */
40 #include <sys/types.h> /* some typedefs are used in sys/file.h */
50 #endif /* not WINDOWSNT */
52 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
53 #include <sys/socket.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
57 #ifdef NEED_NET_ERRNO_H
58 #include <net/errno.h>
59 #endif /* NEED_NET_ERRNO_H */
60 #endif /* HAVE_SOCKETS */
62 /* TERM is a poor-man's SLIP, used on Linux. */
67 /* On some systems, e.g. DGUX, inet_addr returns a 'struct in_addr'. */
68 #ifdef HAVE_BROKEN_INET_ADDR
69 #define IN_ADDR struct in_addr
70 #define NUMERIC_ADDR_ERROR (numeric_addr.s_addr == -1)
72 #define IN_ADDR unsigned long
73 #define NUMERIC_ADDR_ERROR (numeric_addr == -1)
76 #if defined(BSD_SYSTEM) || defined(STRIDE)
77 #include <sys/ioctl.h>
78 #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
80 #endif /* HAVE_PTYS and no O_NDELAY */
81 #endif /* BSD_SYSTEM || STRIDE */
83 #ifdef BROKEN_O_NONBLOCK
85 #endif /* BROKEN_O_NONBLOCK */
92 #include <sys/sysmacros.h> /* for "minor" */
102 #include "termhooks.h"
103 #include "termopts.h"
104 #include "commands.h"
106 #include "blockinput.h"
108 Lisp_Object Qprocessp
;
109 Lisp_Object Qrun
, Qstop
, Qsignal
, Qopen
, Qclosed
;
110 Lisp_Object Qlast_nonmenu_event
;
111 /* Qexit is declared and initialized in eval.c. */
113 /* a process object is a network connection when its childp field is neither
114 Qt nor Qnil but is instead a cons cell (HOSTNAME PORTNUM). */
117 #define NETCONN_P(p) (GC_CONSP (XPROCESS (p)->childp))
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"
141 extern char *strerror ();
143 extern char *sys_errlist
[];
150 #ifndef SYS_SIGLIST_DECLARED
155 extern char *sys_siglist
[];
156 #endif /* not LINUX */
158 char *sys_siglist
[] =
164 "illegal instruction",
168 "floating point exception",
171 "segmentation violation",
172 "bad argument to system call",
173 "write on a pipe with no one to read it",
175 "software termination signal from kill",
177 "sendable stop signal not from tty",
178 "stop signal from tty",
179 "continue a stopped process",
180 "child status has changed",
181 "background read attempted from control tty",
182 "background write attempted from control tty",
183 "input record available at control tty",
184 "exceeded CPU time limit",
185 "exceeded file size limit"
187 #endif /* not WINDOWSNT */
190 #endif /* ! SYS_SIGLIST_DECLARED */
192 /* t means use pty, nil means use a pipe,
193 maybe other values to come. */
194 static Lisp_Object Vprocess_connection_type
;
198 #include <sys/socket.h>
202 /* These next two vars are non-static since sysdep.c uses them in the
203 emulation of `select'. */
204 /* Number of events of change of status of a process. */
206 /* Number of events for which the user or sentinel has been notified. */
209 #include "sysselect.h"
211 /* If we support a window system, turn on the code to poll periodically
212 to detect C-g. It isn't actually used when doing interrupt input. */
213 #ifdef HAVE_WINDOW_SYSTEM
214 #define POLL_FOR_INPUT
217 /* Mask of bits indicating the descriptors that we wait for input on. */
219 static SELECT_TYPE input_wait_mask
;
221 /* Mask that excludes keyboard input descriptor (s). */
223 static SELECT_TYPE non_keyboard_wait_mask
;
225 /* The largest descriptor currently in use for a process object. */
226 static int max_process_desc
;
228 /* The largest descriptor currently in use for keyboard input. */
229 static int max_keyboard_desc
;
231 /* Nonzero means delete a process right away if it exits. */
232 static int delete_exited_processes
;
234 /* Indexed by descriptor, gives the process (if any) for that descriptor */
235 Lisp_Object chan_process
[MAXDESC
];
237 /* Alist of elements (NAME . PROCESS) */
238 Lisp_Object Vprocess_alist
;
240 /* Buffered-ahead input char from process, indexed by channel.
241 -1 means empty (no char is buffered).
242 Used on sys V where the only way to tell if there is any
243 output from the process is to read at least one char.
244 Always -1 on systems that support FIONREAD. */
246 /* Don't make static; need to access externally. */
247 int proc_buffered_char
[MAXDESC
];
249 static Lisp_Object
get_process ();
251 extern EMACS_TIME
timer_check ();
252 extern int timers_run
;
254 /* Maximum number of bytes to send to a pty without an eof. */
255 static int pty_max_bytes
;
258 /* The file name of the pty opened by allocate_pty. */
260 static char pty_name
[24];
263 /* Compute the Lisp form of the process status, p->status, from
264 the numeric status that was returned by `wait'. */
266 Lisp_Object
status_convert ();
269 struct Lisp_Process
*p
;
271 union { int i
; WAITTYPE wt
; } u
;
272 u
.i
= XFASTINT (p
->raw_status_low
) + (XFASTINT (p
->raw_status_high
) << 16);
273 p
->status
= status_convert (u
.wt
);
274 p
->raw_status_low
= Qnil
;
275 p
->raw_status_high
= Qnil
;
278 /* Convert a process status word in Unix format to
279 the list that we use internally. */
286 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
287 else if (WIFEXITED (w
))
288 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
289 WCOREDUMP (w
) ? Qt
: Qnil
));
290 else if (WIFSIGNALED (w
))
291 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
292 WCOREDUMP (w
) ? Qt
: Qnil
));
297 /* Given a status-list, extract the three pieces of information
298 and store them individually through the three pointers. */
301 decode_status (l
, symbol
, code
, coredump
)
317 *symbol
= XCONS (l
)->car
;
318 tem
= XCONS (l
)->cdr
;
319 *code
= XFASTINT (XCONS (tem
)->car
);
320 tem
= XCONS (tem
)->cdr
;
321 *coredump
= !NILP (tem
);
325 /* Return a string describing a process status list. */
328 status_message (status
)
333 Lisp_Object string
, string2
;
335 decode_status (status
, &symbol
, &code
, &coredump
);
337 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
343 /* Cast to suppress warning if the table has const char *. */
344 signame
= (char *) sys_siglist
[code
];
346 signame
= sys_errlist
[code
];
351 string
= build_string (signame
);
352 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
353 XSTRING (string
)->data
[0] = DOWNCASE (XSTRING (string
)->data
[0]);
354 return concat2 (string
, string2
);
356 else if (EQ (symbol
, Qexit
))
359 return build_string ("finished\n");
360 string
= Fnumber_to_string (make_number (code
));
361 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
362 return concat2 (build_string ("exited abnormally with code "),
363 concat2 (string
, string2
));
366 return Fcopy_sequence (Fsymbol_name (symbol
));
371 /* Open an available pty, returning a file descriptor.
372 Return -1 on failure.
373 The file name of the terminal corresponding to the pty
374 is left in the variable pty_name. */
383 /* Some systems name their pseudoterminals so that there are gaps in
384 the usual sequence - for example, on HP9000/S700 systems, there
385 are no pseudoterminals with names ending in 'f'. So we wait for
386 three failures in a row before deciding that we've reached the
388 int failed_count
= 0;
393 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
394 for (i
= 0; i
< 16; i
++)
397 #ifdef PTY_NAME_SPRINTF
400 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
401 #endif /* no PTY_NAME_SPRINTF */
405 #else /* no PTY_OPEN */
407 /* Unusual IRIS code */
408 *ptyv
= open ("/dev/ptc", O_RDWR
| O_NDELAY
, 0);
411 if (fstat (fd
, &stb
) < 0)
414 if (stat (pty_name
, &stb
) < 0)
417 if (failed_count
>= 3)
423 fd
= open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
425 fd
= open (pty_name
, O_RDWR
| O_NDELAY
, 0);
427 #endif /* not IRIS */
428 #endif /* no PTY_OPEN */
432 /* check to make certain that both sides are available
433 this avoids a nasty yet stupid bug in rlogins */
434 #ifdef PTY_TTY_NAME_SPRINTF
437 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
438 #endif /* no PTY_TTY_NAME_SPRINTF */
440 if (access (pty_name
, 6) != 0)
443 #if !defined(IRIS) && !defined(__sgi)
449 #endif /* not UNIPLUS */
456 #endif /* HAVE_PTYS */
462 struct Lisp_Vector
*vec
;
463 register Lisp_Object val
, tem
, name1
;
464 register struct Lisp_Process
*p
;
468 vec
= allocate_vectorlike ((EMACS_INT
) VECSIZE (struct Lisp_Process
));
469 for (i
= 0; i
< VECSIZE (struct Lisp_Process
); i
++)
470 vec
->contents
[i
] = Qnil
;
471 vec
->size
= VECSIZE (struct Lisp_Process
);
472 p
= (struct Lisp_Process
*)vec
;
474 XSETINT (p
->infd
, -1);
475 XSETINT (p
->outfd
, -1);
476 XSETFASTINT (p
->pid
, 0);
477 XSETFASTINT (p
->tick
, 0);
478 XSETFASTINT (p
->update_tick
, 0);
479 p
->raw_status_low
= Qnil
;
480 p
->raw_status_high
= Qnil
;
482 p
->mark
= Fmake_marker ();
484 /* If name is already in use, modify it until it is unused. */
489 tem
= Fget_process (name1
);
490 if (NILP (tem
)) break;
491 sprintf (suffix
, "<%d>", i
);
492 name1
= concat2 (name
, build_string (suffix
));
496 XSETPROCESS (val
, p
);
497 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
501 remove_process (proc
)
502 register Lisp_Object proc
;
504 register Lisp_Object pair
;
506 pair
= Frassq (proc
, Vprocess_alist
);
507 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
509 deactivate_process (proc
);
512 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
513 "Return t if OBJECT is a process.")
517 return PROCESSP (object
) ? Qt
: Qnil
;
520 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
521 "Return the process named NAME, or nil if there is none.")
523 register Lisp_Object name
;
527 CHECK_STRING (name
, 0);
528 return Fcdr (Fassoc (name
, Vprocess_alist
));
531 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
532 "Return the (or, a) process associated with BUFFER.\n\
533 BUFFER may be a buffer or the name of one.")
535 register Lisp_Object buffer
;
537 register Lisp_Object buf
, tail
, proc
;
539 if (NILP (buffer
)) return Qnil
;
540 buf
= Fget_buffer (buffer
);
541 if (NILP (buf
)) return Qnil
;
543 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
545 proc
= Fcdr (Fcar (tail
));
546 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
552 /* This is how commands for the user decode process arguments. It
553 accepts a process, a process name, a buffer, a buffer name, or nil.
554 Buffers denote the first process in the buffer, and nil denotes the
559 register Lisp_Object name
;
561 register Lisp_Object proc
, obj
;
564 obj
= Fget_process (name
);
566 obj
= Fget_buffer (name
);
568 error ("Process %s does not exist", XSTRING (name
)->data
);
570 else if (NILP (name
))
571 obj
= Fcurrent_buffer ();
575 /* Now obj should be either a buffer object or a process object.
579 proc
= Fget_buffer_process (obj
);
581 error ("Buffer %s has no process", XSTRING (XBUFFER (obj
)->name
)->data
);
585 CHECK_PROCESS (obj
, 0);
591 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
592 "Delete PROCESS: kill it and forget about it immediately.\n\
593 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
594 nil, indicating the current buffer's process.")
596 register Lisp_Object process
;
598 process
= get_process (process
);
599 XPROCESS (process
)->raw_status_low
= Qnil
;
600 XPROCESS (process
)->raw_status_high
= Qnil
;
601 if (NETCONN_P (process
))
603 XPROCESS (process
)->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
604 XSETINT (XPROCESS (process
)->tick
, ++process_tick
);
606 else if (XINT (XPROCESS (process
)->infd
) >= 0)
608 Fkill_process (process
, Qnil
);
609 /* Do this now, since remove_process will make sigchld_handler do nothing. */
610 XPROCESS (process
)->status
611 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
612 XSETINT (XPROCESS (process
)->tick
, ++process_tick
);
615 remove_process (process
);
619 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
620 "Return the status of PROCESS: a symbol, one of these:\n\
621 run -- for a process that is running.\n\
622 stop -- for a process stopped but continuable.\n\
623 exit -- for a process that has exited.\n\
624 signal -- for a process that has got a fatal signal.\n\
625 open -- for a network stream connection that is open.\n\
626 closed -- for a network stream connection that is closed.\n\
627 nil -- if arg is a process name and no such process exists.\n\
628 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
629 nil, indicating the current buffer's process.")
631 register Lisp_Object process
;
633 register struct Lisp_Process
*p
;
634 register Lisp_Object status
;
636 if (STRINGP (process
))
637 process
= Fget_process (process
);
639 process
= get_process (process
);
644 p
= XPROCESS (process
);
645 if (!NILP (p
->raw_status_low
))
649 status
= XCONS (status
)->car
;
650 if (NETCONN_P (process
))
652 if (EQ (status
, Qrun
))
654 else if (EQ (status
, Qexit
))
660 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
662 "Return the exit status of PROCESS or the signal number that killed it.\n\
663 If PROCESS has not yet exited or died, return 0.")
665 register Lisp_Object process
;
667 CHECK_PROCESS (process
, 0);
668 if (!NILP (XPROCESS (process
)->raw_status_low
))
669 update_status (XPROCESS (process
));
670 if (CONSP (XPROCESS (process
)->status
))
671 return XCONS (XCONS (XPROCESS (process
)->status
)->cdr
)->car
;
672 return make_number (0);
675 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
676 "Return the process id of PROCESS.\n\
677 This is the pid of the Unix process which PROCESS uses or talks to.\n\
678 For a network connection, this value is nil.")
680 register Lisp_Object process
;
682 CHECK_PROCESS (process
, 0);
683 return XPROCESS (process
)->pid
;
686 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
687 "Return the name of PROCESS, as a string.\n\
688 This is the name of the program invoked in PROCESS,\n\
689 possibly modified to make it unique among process names.")
691 register Lisp_Object process
;
693 CHECK_PROCESS (process
, 0);
694 return XPROCESS (process
)->name
;
697 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
698 "Return the command that was executed to start PROCESS.\n\
699 This is a list of strings, the first string being the program executed\n\
700 and the rest of the strings being the arguments given to it.\n\
701 For a non-child channel, this is nil.")
703 register Lisp_Object process
;
705 CHECK_PROCESS (process
, 0);
706 return XPROCESS (process
)->command
;
709 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
710 "Return the name of the terminal PROCESS uses, or nil if none.\n\
711 This is the terminal that the process itself reads and writes on,\n\
712 not the name of the pty that Emacs uses to talk with that terminal.")
714 register Lisp_Object process
;
716 CHECK_PROCESS (process
, 0);
717 return XPROCESS (process
)->tty_name
;
720 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
722 "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
724 register Lisp_Object process
, buffer
;
726 CHECK_PROCESS (process
, 0);
728 CHECK_BUFFER (buffer
, 1);
729 XPROCESS (process
)->buffer
= buffer
;
733 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
735 "Return the buffer PROCESS is associated with.\n\
736 Output from PROCESS is inserted in this buffer\n\
737 unless PROCESS has a filter.")
739 register Lisp_Object process
;
741 CHECK_PROCESS (process
, 0);
742 return XPROCESS (process
)->buffer
;
745 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
747 "Return the marker for the end of the last output from PROCESS.")
749 register Lisp_Object process
;
751 CHECK_PROCESS (process
, 0);
752 return XPROCESS (process
)->mark
;
755 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
757 "Give PROCESS the filter function FILTER; nil means no filter.\n\
758 t means stop accepting output from the process.\n\
759 When a process has a filter, each time it does output\n\
760 the entire string of output is passed to the filter.\n\
761 The filter gets two arguments: the process and the string of output.\n\
762 If the process has a filter, its buffer is not used for output.")
764 register Lisp_Object process
, filter
;
766 CHECK_PROCESS (process
, 0);
769 FD_CLR (XINT (XPROCESS (process
)->infd
), &input_wait_mask
);
770 FD_CLR (XINT (XPROCESS (process
)->infd
), &non_keyboard_wait_mask
);
772 else if (EQ (XPROCESS (process
)->filter
, Qt
))
774 FD_SET (XINT (XPROCESS (process
)->infd
), &input_wait_mask
);
775 FD_SET (XINT (XPROCESS (process
)->infd
), &non_keyboard_wait_mask
);
777 XPROCESS (process
)->filter
= filter
;
781 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
783 "Returns the filter function of PROCESS; nil if none.\n\
784 See `set-process-filter' for more info on filter functions.")
786 register Lisp_Object process
;
788 CHECK_PROCESS (process
, 0);
789 return XPROCESS (process
)->filter
;
792 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
794 "Give PROCESS the sentinel SENTINEL; nil for none.\n\
795 The sentinel is called as a function when the process changes state.\n\
796 It gets two arguments: the process, and a string describing the change.")
798 register Lisp_Object process
, sentinel
;
800 CHECK_PROCESS (process
, 0);
801 XPROCESS (process
)->sentinel
= sentinel
;
805 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
807 "Return the sentinel of PROCESS; nil if none.\n\
808 See `set-process-sentinel' for more info on sentinels.")
810 register Lisp_Object process
;
812 CHECK_PROCESS (process
, 0);
813 return XPROCESS (process
)->sentinel
;
816 DEFUN ("set-process-window-size", Fset_process_window_size
,
817 Sset_process_window_size
, 3, 3, 0,
818 "Tell PROCESS that it has logical window size HEIGHT and WIDTH.")
819 (process
, height
, width
)
820 register Lisp_Object process
, height
, width
;
822 CHECK_PROCESS (process
, 0);
823 CHECK_NATNUM (height
, 0);
824 CHECK_NATNUM (width
, 0);
825 if (set_window_size (XINT (XPROCESS (process
)->infd
),
826 XINT (height
), XINT(width
)) <= 0)
832 DEFUN ("process-kill-without-query", Fprocess_kill_without_query
,
833 Sprocess_kill_without_query
, 1, 2, 0,
834 "Say no query needed if PROCESS is running when Emacs is exited.\n\
835 Optional second argument if non-nil says to require a query.\n\
836 Value is t if a query was formerly required.")
838 register Lisp_Object process
, value
;
842 CHECK_PROCESS (process
, 0);
843 tem
= XPROCESS (process
)->kill_without_query
;
844 XPROCESS (process
)->kill_without_query
= Fnull (value
);
849 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
851 "Return the contact info of PROCESS; t for a real child.\n\
852 For a net connection, the value is a cons cell of the form (HOST SERVICE).")
854 register Lisp_Object process
;
856 CHECK_PROCESS (process
, 0);
857 return XPROCESS (process
)->childp
;
860 #if 0 /* Turned off because we don't currently record this info
861 in the process. Perhaps add it. */
862 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
863 "Return the connection type of `PROCESS'.\n\
864 The value is `nil' for a pipe,\n\
865 `t' or `pty' for a pty, or `stream' for a socket connection.")
869 return XPROCESS (process
)->type
;
876 register Lisp_Object tail
, tem
;
877 Lisp_Object proc
, minspace
, tem1
;
878 register struct buffer
*old
= current_buffer
;
879 register struct Lisp_Process
*p
;
883 XSETFASTINT (minspace
, 1);
885 set_buffer_internal (XBUFFER (Vstandard_output
));
886 Fbuffer_disable_undo (Vstandard_output
);
888 current_buffer
->truncate_lines
= Qt
;
891 Proc Status Buffer Tty Command\n\
892 ---- ------ ------ --- -------\n", -1);
894 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
898 proc
= Fcdr (Fcar (tail
));
900 if (NILP (p
->childp
))
903 Finsert (1, &p
->name
);
904 Findent_to (make_number (13), minspace
);
906 if (!NILP (p
->raw_status_low
))
909 if (CONSP (p
->status
))
910 symbol
= XCONS (p
->status
)->car
;
913 if (EQ (symbol
, Qsignal
))
916 tem
= Fcar (Fcdr (p
->status
));
918 if (XINT (tem
) < NSIG
)
919 write_string (sys_errlist
[XINT (tem
)], -1);
922 Fprinc (symbol
, Qnil
);
924 else if (NETCONN_P (proc
))
926 if (EQ (symbol
, Qrun
))
927 write_string ("open", -1);
928 else if (EQ (symbol
, Qexit
))
929 write_string ("closed", -1);
931 Fprinc (symbol
, Qnil
);
934 Fprinc (symbol
, Qnil
);
936 if (EQ (symbol
, Qexit
))
939 tem
= Fcar (Fcdr (p
->status
));
942 sprintf (tembuf
, " %d", (int) XFASTINT (tem
));
943 write_string (tembuf
, -1);
947 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
948 remove_process (proc
);
950 Findent_to (make_number (22), minspace
);
951 if (NILP (p
->buffer
))
952 insert_string ("(none)");
953 else if (NILP (XBUFFER (p
->buffer
)->name
))
954 insert_string ("(Killed)");
956 Finsert (1, &XBUFFER (p
->buffer
)->name
);
958 Findent_to (make_number (37), minspace
);
960 if (STRINGP (p
->tty_name
))
961 Finsert (1, &p
->tty_name
);
963 insert_string ("(none)");
965 Findent_to (make_number (49), minspace
);
967 if (NETCONN_P (proc
))
969 sprintf (tembuf
, "(network stream connection to %s)\n",
970 XSTRING (XCONS (p
->childp
)->car
)->data
);
971 insert_string (tembuf
);
985 insert_string ("\n");
991 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 0, "",
992 "Display a list of all processes.\n\
993 \(Any processes listed as Exited or Signaled are actually eliminated\n\
994 after the listing is made.)")
997 internal_with_output_to_temp_buffer ("*Process List*",
998 list_processes_1
, Qnil
);
1002 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1003 "Return a list of all processes.")
1006 return Fmapcar (Qcdr
, Vprocess_alist
);
1009 /* Starting asynchronous inferior processes. */
1011 static Lisp_Object
start_process_unwind ();
1013 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1014 "Start a program in a subprocess. Return the process object for it.\n\
1015 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
1016 NAME is name for process. It is modified if necessary to make it unique.\n\
1017 BUFFER is the buffer or (buffer-name) to associate with the process.\n\
1018 Process output goes at end of that buffer, unless you specify\n\
1019 an output stream or filter function to handle the output.\n\
1020 BUFFER may be also nil, meaning that this process is not associated\n\
1022 Third arg is program file name. It is searched for as in the shell.\n\
1023 Remaining arguments are strings to give program as arguments.")
1026 register Lisp_Object
*args
;
1028 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1030 register unsigned char *new_argv
;
1033 register unsigned char **new_argv
;
1036 int count
= specpdl_ptr
- specpdl
;
1040 buffer
= Fget_buffer_create (buffer
);
1042 /* Make sure that the child will be able to chdir to the current
1043 buffer's current directory, or its unhandled equivalent. We
1044 can't just have the child check for an error when it does the
1045 chdir, since it's in a vfork.
1047 We have to GCPRO around this because Fexpand_file_name and
1048 Funhandled_file_name_directory might call a file name handling
1049 function. The argument list is protected by the caller, so all
1050 we really have to worry about is buffer. */
1052 struct gcpro gcpro1
, gcpro2
;
1054 current_dir
= current_buffer
->directory
;
1056 GCPRO2 (buffer
, current_dir
);
1059 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
1061 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1062 report_file_error ("Setting current directory",
1063 Fcons (current_buffer
->directory
, Qnil
));
1069 CHECK_STRING (name
, 0);
1073 CHECK_STRING (program
, 2);
1076 /* Make a one member argv with all args concatenated
1077 together separated by a blank. */
1078 len
= XSTRING (program
)->size
+ 2;
1079 for (i
= 3; i
< nargs
; i
++)
1082 CHECK_STRING (tem
, i
);
1083 len
+= XSTRING (tem
)->size
+ 1; /* count the blank */
1085 new_argv
= (unsigned char *) alloca (len
);
1086 strcpy (new_argv
, XSTRING (program
)->data
);
1087 for (i
= 3; i
< nargs
; i
++)
1090 CHECK_STRING (tem
, i
);
1091 strcat (new_argv
, " ");
1092 strcat (new_argv
, XSTRING (tem
)->data
);
1094 /* Need to add code here to check for program existence on VMS */
1097 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1099 /* If program file name is not absolute, search our path for it */
1100 if (!IS_DIRECTORY_SEP (XSTRING (program
)->data
[0])
1101 && !(XSTRING (program
)->size
> 1
1102 && IS_DEVICE_SEP (XSTRING (program
)->data
[1])))
1104 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1107 GCPRO4 (name
, program
, buffer
, current_dir
);
1108 openp (Vexec_path
, program
, EXEC_SUFFIXES
, &tem
, 1);
1111 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1112 tem
= Fexpand_file_name (tem
, Qnil
);
1113 new_argv
[0] = XSTRING (tem
)->data
;
1117 if (!NILP (Ffile_directory_p (program
)))
1118 error ("Specified program for new process is a directory");
1120 new_argv
[0] = XSTRING (program
)->data
;
1123 for (i
= 3; i
< nargs
; i
++)
1126 CHECK_STRING (tem
, i
);
1127 new_argv
[i
- 2] = XSTRING (tem
)->data
;
1129 new_argv
[i
- 2] = 0;
1130 #endif /* not VMS */
1132 proc
= make_process (name
);
1133 /* If an error occurs and we can't start the process, we want to
1134 remove it from the process list. This means that each error
1135 check in create_process doesn't need to call remove_process
1136 itself; it's all taken care of here. */
1137 record_unwind_protect (start_process_unwind
, proc
);
1139 XPROCESS (proc
)->childp
= Qt
;
1140 XPROCESS (proc
)->command_channel_p
= Qnil
;
1141 XPROCESS (proc
)->buffer
= buffer
;
1142 XPROCESS (proc
)->sentinel
= Qnil
;
1143 XPROCESS (proc
)->filter
= Qnil
;
1144 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1146 /* Make the process marker point into the process buffer (if any). */
1148 Fset_marker (XPROCESS (proc
)->mark
,
1149 make_number (BUF_ZV (XBUFFER (buffer
))), buffer
);
1151 create_process (proc
, new_argv
, current_dir
);
1153 return unbind_to (count
, proc
);
1156 /* This function is the unwind_protect form for Fstart_process. If
1157 PROC doesn't have its pid set, then we know someone has signaled
1158 an error and the process wasn't started successfully, so we should
1159 remove it from the process list. */
1161 start_process_unwind (proc
)
1164 if (!PROCESSP (proc
))
1167 /* Was PROC started successfully? */
1168 if (XINT (XPROCESS (proc
)->pid
) <= 0)
1169 remove_process (proc
);
1176 create_process_1 (signo
)
1179 #if defined (USG) && !defined (POSIX_SIGNALS)
1180 /* USG systems forget handlers when they are used;
1181 must reestablish each time */
1182 signal (signo
, create_process_1
);
1186 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1189 /* Mimic blocking of signals on system V, which doesn't really have it. */
1191 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1192 int sigchld_deferred
;
1195 create_process_sigchld ()
1197 signal (SIGCHLD
, create_process_sigchld
);
1199 sigchld_deferred
= 1;
1205 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1206 create_process (process
, new_argv
, current_dir
)
1207 Lisp_Object process
;
1209 Lisp_Object current_dir
;
1211 int pid
, inchannel
, outchannel
;
1213 #ifdef POSIX_SIGNALS
1216 struct sigaction sigint_action
;
1217 struct sigaction sigquit_action
;
1219 struct sigaction sighup_action
;
1221 #else /* !POSIX_SIGNALS */
1223 SIGTYPE (*sigchld
)();
1225 #endif /* !POSIX_SIGNALS */
1226 /* Use volatile to protect variables from being clobbered by longjmp. */
1227 volatile int forkin
, forkout
;
1228 volatile int pty_flag
= 0;
1229 extern char **environ
;
1231 inchannel
= outchannel
= -1;
1234 if (!NILP (Vprocess_connection_type
))
1235 outchannel
= inchannel
= allocate_pty ();
1240 /* On USG systems it does not work to open the pty's tty here
1241 and then close and reopen it in the child. */
1243 /* Don't let this terminal become our controlling terminal
1244 (in case we don't have one). */
1245 forkout
= forkin
= open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1247 forkout
= forkin
= open (pty_name
, O_RDWR
, 0);
1250 report_file_error ("Opening pty", Qnil
);
1252 forkin
= forkout
= -1;
1253 #endif /* not USG */
1257 #endif /* HAVE_PTYS */
1260 if (socketpair (AF_UNIX
, SOCK_STREAM
, 0, sv
) < 0)
1261 report_file_error ("Opening socketpair", Qnil
);
1262 outchannel
= inchannel
= sv
[0];
1263 forkout
= forkin
= sv
[1];
1265 #else /* not SKTPAIR */
1274 #endif /* not SKTPAIR */
1277 /* Replaced by close_process_descs */
1278 set_exclusive_use (inchannel
);
1279 set_exclusive_use (outchannel
);
1282 /* Stride people say it's a mystery why this is needed
1283 as well as the O_NDELAY, but that it fails without this. */
1284 #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
1287 ioctl (inchannel
, FIONBIO
, &one
);
1292 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1293 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1296 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1297 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
1301 /* Record this as an active process, with its channels.
1302 As a result, child_setup will close Emacs's side of the pipes. */
1303 chan_process
[inchannel
] = process
;
1304 XSETINT (XPROCESS (process
)->infd
, inchannel
);
1305 XSETINT (XPROCESS (process
)->outfd
, outchannel
);
1306 /* Record the tty descriptor used in the subprocess. */
1308 XPROCESS (process
)->subtty
= Qnil
;
1310 XSETFASTINT (XPROCESS (process
)->subtty
, forkin
);
1311 XPROCESS (process
)->pty_flag
= (pty_flag
? Qt
: Qnil
);
1312 XPROCESS (process
)->status
= Qrun
;
1314 /* Delay interrupts until we have a chance to store
1315 the new fork's pid in its process structure */
1316 #ifdef POSIX_SIGNALS
1317 sigemptyset (&blocked
);
1319 sigaddset (&blocked
, SIGCHLD
);
1322 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1323 this sets the parent's signal handlers as well as the child's.
1324 So delay all interrupts whose handlers the child might munge,
1325 and record the current handlers so they can be restored later. */
1326 sigaddset (&blocked
, SIGINT
); sigaction (SIGINT
, 0, &sigint_action
);
1327 sigaddset (&blocked
, SIGQUIT
); sigaction (SIGQUIT
, 0, &sigquit_action
);
1329 sigaddset (&blocked
, SIGHUP
); sigaction (SIGHUP
, 0, &sighup_action
);
1331 #endif /* HAVE_VFORK */
1332 sigprocmask (SIG_BLOCK
, &blocked
, &procmask
);
1333 #else /* !POSIX_SIGNALS */
1337 #else /* not BSD4_1 */
1338 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
1339 sigsetmask (sigmask (SIGCHLD
));
1340 #else /* ordinary USG */
1342 sigchld_deferred
= 0;
1343 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
1345 #endif /* ordinary USG */
1346 #endif /* not BSD4_1 */
1347 #endif /* SIGCHLD */
1348 #endif /* !POSIX_SIGNALS */
1350 FD_SET (inchannel
, &input_wait_mask
);
1351 FD_SET (inchannel
, &non_keyboard_wait_mask
);
1352 if (inchannel
> max_process_desc
)
1353 max_process_desc
= inchannel
;
1355 /* Until we store the proper pid, enable sigchld_handler
1356 to recognize an unknown pid as standing for this process.
1357 It is very important not to let this `marker' value stay
1358 in the table after this function has returned; if it does
1359 it might cause call-process to hang and subsequent asynchronous
1360 processes to get their return values scrambled. */
1361 XSETINT (XPROCESS (process
)->pid
, -1);
1366 /* child_setup must clobber environ on systems with true vfork.
1367 Protect it from permanent change. */
1368 char **save_environ
= environ
;
1373 #endif /* not WINDOWSNT */
1375 int xforkin
= forkin
;
1376 int xforkout
= forkout
;
1378 #if 0 /* This was probably a mistake--it duplicates code later on,
1379 but fails to handle all the cases. */
1380 /* Make sure SIGCHLD is not blocked in the child. */
1381 sigsetmask (SIGEMPTYMASK
);
1384 /* Make the pty be the controlling terminal of the process. */
1386 /* First, disconnect its current controlling terminal. */
1388 /* We tried doing setsid only if pty_flag, but it caused
1389 process_set_signal to fail on SGI when using a pipe. */
1391 /* Make the pty's terminal the controlling terminal. */
1395 /* We ignore the return value
1396 because faith@cs.unc.edu says that is necessary on Linux. */
1397 ioctl (xforkin
, TIOCSCTTY
, 0);
1400 #else /* not HAVE_SETSID */
1402 /* It's very important to call setpgrp here and no time
1403 afterwards. Otherwise, we lose our controlling tty which
1404 is set when we open the pty. */
1407 #endif /* not HAVE_SETSID */
1408 #if defined (HAVE_TERMIOS) && defined (LDISC1)
1409 if (pty_flag
&& xforkin
>= 0)
1412 tcgetattr (xforkin
, &t
);
1414 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
1415 write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
1418 #if defined (NTTYDISC) && defined (TIOCSETD)
1419 if (pty_flag
&& xforkin
>= 0)
1421 /* Use new line discipline. */
1422 int ldisc
= NTTYDISC
;
1423 ioctl (xforkin
, TIOCSETD
, &ldisc
);
1428 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1429 can do TIOCSPGRP only to the process's controlling tty. */
1432 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1433 I can't test it since I don't have 4.3. */
1434 int j
= open ("/dev/tty", O_RDWR
, 0);
1435 ioctl (j
, TIOCNOTTY
, 0);
1438 /* In order to get a controlling terminal on some versions
1439 of BSD, it is necessary to put the process in pgrp 0
1440 before it opens the terminal. */
1448 #endif /* TIOCNOTTY */
1450 #if !defined (RTU) && !defined (UNIPLUS) && !defined (DONT_REOPEN_PTY)
1451 /*** There is a suggestion that this ought to be a
1452 conditional on TIOCSPGRP,
1453 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
1454 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
1455 that system does seem to need this code, even though
1456 both HAVE_SETSID and TIOCSCTTY are defined. */
1457 /* Now close the pty (if we had it open) and reopen it.
1458 This makes the pty the controlling terminal of the subprocess. */
1461 #ifdef SET_CHILD_PTY_PGRP
1462 int pgrp
= getpid ();
1465 /* I wonder if close (open (pty_name, ...)) would work? */
1468 xforkout
= xforkin
= open (pty_name
, O_RDWR
, 0);
1472 write (1, "Couldn't open the pty terminal ", 31);
1473 write (1, pty_name
, strlen (pty_name
));
1478 #ifdef SET_CHILD_PTY_PGRP
1479 ioctl (xforkin
, TIOCSPGRP
, &pgrp
);
1480 ioctl (xforkout
, TIOCSPGRP
, &pgrp
);
1483 #endif /* not UNIPLUS and not RTU and not DONT_REOPEN_PTY */
1485 #ifdef SETUP_SLAVE_PTY
1490 #endif /* SETUP_SLAVE_PTY */
1492 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
1493 Now reenable it in the child, so it will die when we want it to. */
1495 signal (SIGHUP
, SIG_DFL
);
1497 #endif /* HAVE_PTYS */
1499 signal (SIGINT
, SIG_DFL
);
1500 signal (SIGQUIT
, SIG_DFL
);
1502 /* Stop blocking signals in the child. */
1503 #ifdef POSIX_SIGNALS
1504 sigprocmask (SIG_SETMASK
, &procmask
, 0);
1505 #else /* !POSIX_SIGNALS */
1509 #else /* not BSD4_1 */
1510 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
1511 sigsetmask (SIGEMPTYMASK
);
1512 #else /* ordinary USG */
1514 signal (SIGCHLD
, sigchld
);
1516 #endif /* ordinary USG */
1517 #endif /* not BSD4_1 */
1518 #endif /* SIGCHLD */
1519 #endif /* !POSIX_SIGNALS */
1522 child_setup_tty (xforkout
);
1524 pid
= child_setup (xforkin
, xforkout
, xforkout
,
1525 new_argv
, 1, current_dir
);
1526 #else /* not WINDOWSNT */
1527 child_setup (xforkin
, xforkout
, xforkout
,
1528 new_argv
, 1, current_dir
);
1529 #endif /* not WINDOWSNT */
1531 environ
= save_environ
;
1536 /* This runs in the Emacs process. */
1541 if (forkin
!= forkout
&& forkout
>= 0)
1546 /* vfork succeeded. */
1547 XSETFASTINT (XPROCESS (process
)->pid
, pid
);
1550 register_child (pid
, inchannel
);
1551 #endif /* WINDOWSNT */
1553 /* If the subfork execv fails, and it exits,
1554 this close hangs. I don't know why.
1555 So have an interrupt jar it loose. */
1557 signal (SIGALRM
, create_process_1
);
1559 XPROCESS (process
)->subtty
= Qnil
;
1564 if (forkin
!= forkout
&& forkout
>= 0)
1569 XPROCESS (process
)->tty_name
= build_string (pty_name
);
1572 XPROCESS (process
)->tty_name
= Qnil
;
1575 /* Restore the signal state whether vfork succeeded or not.
1576 (We will signal an error, below, if it failed.) */
1577 #ifdef POSIX_SIGNALS
1579 /* Restore the parent's signal handlers. */
1580 sigaction (SIGINT
, &sigint_action
, 0);
1581 sigaction (SIGQUIT
, &sigquit_action
, 0);
1583 sigaction (SIGHUP
, &sighup_action
, 0);
1585 #endif /* HAVE_VFORK */
1586 /* Stop blocking signals in the parent. */
1587 sigprocmask (SIG_SETMASK
, &procmask
, 0);
1588 #else /* !POSIX_SIGNALS */
1592 #else /* not BSD4_1 */
1593 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
1594 sigsetmask (SIGEMPTYMASK
);
1595 #else /* ordinary USG */
1597 signal (SIGCHLD
, sigchld
);
1598 /* Now really handle any of these signals
1599 that came in during this function. */
1600 if (sigchld_deferred
)
1601 kill (getpid (), SIGCHLD
);
1603 #endif /* ordinary USG */
1604 #endif /* not BSD4_1 */
1605 #endif /* SIGCHLD */
1606 #endif /* !POSIX_SIGNALS */
1608 /* Now generate the error if vfork failed. */
1610 report_file_error ("Doing vfork", Qnil
);
1612 #endif /* not VMS */
1616 /* open a TCP network connection to a given HOST/SERVICE. Treated
1617 exactly like a normal process when reading and writing. Only
1618 differences are in status display and process deletion. A network
1619 connection has no PID; you cannot signal it. All you can do is
1620 deactivate and close it via delete-process */
1622 DEFUN ("open-network-stream", Fopen_network_stream
, Sopen_network_stream
,
1624 "Open a TCP connection for a service to a host.\n\
1625 Returns a subprocess-object to represent the connection.\n\
1626 Input and output work as for subprocesses; `delete-process' closes it.\n\
1627 Args are NAME BUFFER HOST SERVICE.\n\
1628 NAME is name for process. It is modified if necessary to make it unique.\n\
1629 BUFFER is the buffer (or buffer-name) to associate with the process.\n\
1630 Process output goes at end of that buffer, unless you specify\n\
1631 an output stream or filter function to handle the output.\n\
1632 BUFFER may be also nil, meaning that this process is not associated\n\
1634 Third arg is name of the host to connect to, or its IP address.\n\
1635 Fourth arg SERVICE is name of the service desired, or an integer\n\
1636 specifying a port number to connect to.")
1637 (name
, buffer
, host
, service
)
1638 Lisp_Object name
, buffer
, host
, service
;
1642 struct sockaddr_in address
;
1643 struct servent
*svc_info
;
1644 struct hostent
*host_info_ptr
, host_info
;
1645 char *(addr_list
[2]);
1646 IN_ADDR numeric_addr
;
1650 struct hostent host_info_fixed
;
1651 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1653 int count
= specpdl_ptr
- specpdl
;
1656 /* Ensure socket support is loaded if available. */
1657 init_winsock (TRUE
);
1660 GCPRO4 (name
, buffer
, host
, service
);
1661 CHECK_STRING (name
, 0);
1662 CHECK_STRING (host
, 0);
1663 if (INTEGERP (service
))
1664 port
= htons ((unsigned short) XINT (service
));
1667 CHECK_STRING (service
, 0);
1668 svc_info
= getservbyname (XSTRING (service
)->data
, "tcp");
1670 error ("Unknown service \"%s\"", XSTRING (service
)->data
);
1671 port
= svc_info
->s_port
;
1674 /* Slow down polling to every ten seconds.
1675 Some kernels have a bug which causes retrying connect to fail
1676 after a connect. Polling can interfere with gethostbyname too. */
1677 #ifdef POLL_FOR_INPUT
1678 bind_polling_period (10);
1689 host_info_ptr
= gethostbyname (XSTRING (host
)->data
);
1692 if (! (host_info_ptr
== 0 && h_errno
== TRY_AGAIN
))
1695 Fsleep_for (make_number (1), Qnil
);
1697 if (host_info_ptr
== 0)
1698 /* Attempt to interpret host as numeric inet address */
1700 numeric_addr
= inet_addr ((char *) XSTRING (host
)->data
);
1701 if (NUMERIC_ADDR_ERROR
)
1702 error ("Unknown host \"%s\"", XSTRING (host
)->data
);
1704 host_info_ptr
= &host_info
;
1705 host_info
.h_name
= 0;
1706 host_info
.h_aliases
= 0;
1707 host_info
.h_addrtype
= AF_INET
;
1709 /* Older machines have only one address slot called h_addr.
1710 Newer machines have h_addr_list, but #define h_addr to
1711 be its first element. */
1712 host_info
.h_addr_list
= &(addr_list
[0]);
1714 host_info
.h_addr
= (char*)(&numeric_addr
);
1716 /* numeric_addr isn't null-terminated; it has fixed length. */
1717 host_info
.h_length
= sizeof (numeric_addr
);
1720 bzero (&address
, sizeof address
);
1721 bcopy (host_info_ptr
->h_addr
, (char *) &address
.sin_addr
,
1722 host_info_ptr
->h_length
);
1723 address
.sin_family
= host_info_ptr
->h_addrtype
;
1724 address
.sin_port
= port
;
1726 s
= socket (host_info_ptr
->h_addrtype
, SOCK_STREAM
, 0);
1728 report_file_error ("error creating socket", Fcons (name
, Qnil
));
1730 /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
1731 when connect is interrupted. So let's not let it get interrupted.
1732 Note we do not turn off polling, because polling is only used
1733 when not interrupt_input, and thus not normally used on the systems
1734 which have this bug. On systems which use polling, there's no way
1735 to quit if polling is turned off. */
1736 if (interrupt_input
)
1744 if (connect (s
, (struct sockaddr
*) &address
, sizeof address
) == -1
1745 && errno
!= EISCONN
)
1753 if (errno
== EADDRINUSE
&& retry
< 20)
1755 /* A delay here is needed on some FreeBSD systems,
1756 and it is harmless, since this retrying takes time anyway
1757 and should be infrequent. */
1758 Fsleep_for (make_number (1), Qnil
);
1765 if (interrupt_input
)
1769 report_file_error ("connection failed",
1770 Fcons (host
, Fcons (name
, Qnil
)));
1775 #ifdef POLL_FOR_INPUT
1776 unbind_to (count
, Qnil
);
1779 if (interrupt_input
)
1783 s
= connect_server (0);
1785 report_file_error ("error creating socket", Fcons (name
, Qnil
));
1786 send_command (s
, C_PORT
, 0, "%s:%d", XSTRING (host
)->data
, ntohs (port
));
1787 send_command (s
, C_DUMB
, 1, 0);
1793 report_file_error ("error duplicating socket", Fcons (name
, Qnil
));
1796 buffer
= Fget_buffer_create (buffer
);
1797 proc
= make_process (name
);
1799 chan_process
[inch
] = proc
;
1802 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
1805 fcntl (inch
, F_SETFL
, O_NDELAY
);
1809 XPROCESS (proc
)->childp
= Fcons (host
, Fcons (service
, Qnil
));
1810 XPROCESS (proc
)->command_channel_p
= Qnil
;
1811 XPROCESS (proc
)->buffer
= buffer
;
1812 XPROCESS (proc
)->sentinel
= Qnil
;
1813 XPROCESS (proc
)->filter
= Qnil
;
1814 XPROCESS (proc
)->command
= Qnil
;
1815 XPROCESS (proc
)->pid
= Qnil
;
1816 XSETINT (XPROCESS (proc
)->infd
, inch
);
1817 XSETINT (XPROCESS (proc
)->outfd
, outch
);
1818 XPROCESS (proc
)->status
= Qrun
;
1819 FD_SET (inch
, &input_wait_mask
);
1820 FD_SET (inch
, &non_keyboard_wait_mask
);
1821 if (inch
> max_process_desc
)
1822 max_process_desc
= inch
;
1827 #endif /* HAVE_SOCKETS */
1829 deactivate_process (proc
)
1832 register int inchannel
, outchannel
;
1833 register struct Lisp_Process
*p
= XPROCESS (proc
);
1835 inchannel
= XINT (p
->infd
);
1836 outchannel
= XINT (p
->outfd
);
1840 /* Beware SIGCHLD hereabouts. */
1841 flush_pending_output (inchannel
);
1844 VMS_PROC_STUFF
*get_vms_process_pointer (), *vs
;
1845 sys$
dassgn (outchannel
);
1846 vs
= get_vms_process_pointer (p
->pid
);
1848 give_back_vms_process_stuff (vs
);
1852 if (outchannel
>= 0 && outchannel
!= inchannel
)
1856 XSETINT (p
->infd
, -1);
1857 XSETINT (p
->outfd
, -1);
1858 chan_process
[inchannel
] = Qnil
;
1859 FD_CLR (inchannel
, &input_wait_mask
);
1860 FD_CLR (inchannel
, &non_keyboard_wait_mask
);
1861 if (inchannel
== max_process_desc
)
1864 /* We just closed the highest-numbered process input descriptor,
1865 so recompute the highest-numbered one now. */
1866 max_process_desc
= 0;
1867 for (i
= 0; i
< MAXDESC
; i
++)
1868 if (!NILP (chan_process
[i
]))
1869 max_process_desc
= i
;
1874 /* Close all descriptors currently in use for communication
1875 with subprocess. This is used in a newly-forked subprocess
1876 to get rid of irrelevant descriptors. */
1878 close_process_descs ()
1882 for (i
= 0; i
< MAXDESC
; i
++)
1884 Lisp_Object process
;
1885 process
= chan_process
[i
];
1886 if (!NILP (process
))
1888 int in
= XINT (XPROCESS (process
)->infd
);
1889 int out
= XINT (XPROCESS (process
)->outfd
);
1892 if (out
>= 0 && in
!= out
)
1899 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
1901 "Allow any pending output from subprocesses to be read by Emacs.\n\
1902 It is read into the process' buffers or given to their filter functions.\n\
1903 Non-nil arg PROCESS means do not return until some output has been received\n\
1905 Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of\n\
1906 seconds and microseconds to wait; return after that much time whether\n\
1907 or not there is input.\n\
1908 Return non-nil iff we received any output before the timeout expired.")
1909 (process
, timeout
, timeout_msecs
)
1910 register Lisp_Object process
, timeout
, timeout_msecs
;
1915 if (! NILP (timeout_msecs
))
1917 CHECK_NUMBER (timeout_msecs
, 2);
1918 useconds
= XINT (timeout_msecs
);
1919 if (!INTEGERP (timeout
))
1920 XSETINT (timeout
, 0);
1923 int carry
= useconds
/ 1000000;
1925 XSETINT (timeout
, XINT (timeout
) + carry
);
1926 useconds
-= carry
* 1000000;
1928 /* I think this clause is necessary because C doesn't
1929 guarantee a particular rounding direction for negative
1933 XSETINT (timeout
, XINT (timeout
) - 1);
1934 useconds
+= 1000000;
1941 if (! NILP (timeout
))
1943 CHECK_NUMBER (timeout
, 1);
1944 seconds
= XINT (timeout
);
1945 if (seconds
< 0 || (seconds
== 0 && useconds
== 0))
1957 XSETFASTINT (process
, 0);
1960 (wait_reading_process_input (seconds
, useconds
, process
, 0)
1964 /* This variable is different from waiting_for_input in keyboard.c.
1965 It is used to communicate to a lisp process-filter/sentinel (via the
1966 function Fwaiting_for_user_input_p below) whether emacs was waiting
1967 for user-input when that process-filter was called.
1968 waiting_for_input cannot be used as that is by definition 0 when
1969 lisp code is being evalled.
1970 This is also used in record_asynch_buffer_change.
1971 For that purpose, this must be 0
1972 when not inside wait_reading_process_input. */
1973 static int waiting_for_user_input_p
;
1975 /* This is here so breakpoints can be put on it. */
1977 wait_reading_process_input_1 ()
1981 /* Read and dispose of subprocess output while waiting for timeout to
1982 elapse and/or keyboard input to be available.
1985 timeout in seconds, or
1986 zero for no limit, or
1987 -1 means gobble data immediately available but don't wait for any.
1990 an additional duration to wait, measured in microseconds.
1991 If this is nonzero and time_limit is 0, then the timeout
1992 consists of MICROSECS only.
1994 READ_KBD is a lisp value:
1995 0 to ignore keyboard input, or
1996 1 to return when input is available, or
1997 -1 meaning caller will actually read the input, so don't throw to
1998 the quit handler, or
1999 a cons cell, meaning wait until its car is non-nil
2000 (and gobble terminal input into the buffer if any arrives), or
2001 a process object, meaning wait until something arrives from that
2002 process. The return value is true iff we read some input from
2005 DO_DISPLAY != 0 means redisplay should be done to show subprocess
2006 output that arrives.
2008 If READ_KBD is a pointer to a struct Lisp_Process, then the
2009 function returns true iff we received input from that process
2010 before the timeout elapsed.
2011 Otherwise, return true iff we received input from any process. */
2013 wait_reading_process_input (time_limit
, microsecs
, read_kbd
, do_display
)
2014 int time_limit
, microsecs
;
2015 Lisp_Object read_kbd
;
2018 register int channel
, nfds
, m
;
2019 static SELECT_TYPE Available
;
2022 EMACS_TIME timeout
, end_time
, garbage
;
2024 int wait_channel
= -1;
2025 struct Lisp_Process
*wait_proc
= 0;
2026 int got_some_input
= 0;
2027 Lisp_Object
*wait_for_cell
= 0;
2029 FD_ZERO (&Available
);
2031 /* If read_kbd is a process to watch, set wait_proc and wait_channel
2033 if (PROCESSP (read_kbd
))
2035 wait_proc
= XPROCESS (read_kbd
);
2036 wait_channel
= XINT (wait_proc
->infd
);
2037 XSETFASTINT (read_kbd
, 0);
2040 /* If waiting for non-nil in a cell, record where. */
2041 if (CONSP (read_kbd
))
2043 wait_for_cell
= &XCONS (read_kbd
)->car
;
2044 XSETFASTINT (read_kbd
, 0);
2047 waiting_for_user_input_p
= XINT (read_kbd
);
2049 /* Since we may need to wait several times,
2050 compute the absolute time to return at. */
2051 if (time_limit
|| microsecs
)
2053 EMACS_GET_TIME (end_time
);
2054 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
2055 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
2058 /* AlainF 5-Jul-1996
2059 HP-UX 10.10 seem to have problems with signals coming in
2060 Causes "poll: interrupted system call" messages when Emacs is run
2062 Turn off periodic alarms (in case they are in use) */
2068 int timeout_reduced_for_timers
= 0;
2070 /* If calling from keyboard input, do not quit
2071 since we want to return C-g as an input character.
2072 Otherwise, do pending quit if requested. */
2073 if (XINT (read_kbd
) >= 0)
2076 /* Exit now if the cell we're waiting for became non-nil. */
2077 if (wait_for_cell
&& ! NILP (*wait_for_cell
))
2080 /* Compute time from now till when time limit is up */
2081 /* Exit if already run out */
2082 if (time_limit
== -1)
2084 /* -1 specified for timeout means
2085 gobble output available now
2086 but don't wait at all. */
2088 EMACS_SET_SECS_USECS (timeout
, 0, 0);
2090 else if (time_limit
|| microsecs
)
2092 EMACS_GET_TIME (timeout
);
2093 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
2094 if (EMACS_TIME_NEG_P (timeout
))
2099 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
2102 /* Normally we run timers here.
2103 But not if wait_for_cell; in those cases,
2104 the wait is supposed to be short,
2105 and those callers cannot handle running arbitrary Lisp code here. */
2106 if (! wait_for_cell
)
2108 EMACS_TIME timer_delay
;
2112 old_timers_run
= timers_run
;
2113 timer_delay
= timer_check (1);
2114 if (timers_run
!= old_timers_run
&& do_display
)
2116 redisplay_preserve_echo_area ();
2117 /* We must retry, since a timer may have requeued itself
2118 and that could alter the time_delay. */
2122 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
2124 EMACS_TIME difference
;
2125 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
2126 if (EMACS_TIME_NEG_P (difference
))
2128 timeout
= timer_delay
;
2129 timeout_reduced_for_timers
= 1;
2132 /* If time_limit is -1, we are not going to wait at all. */
2133 else if (time_limit
!= -1)
2135 /* This is so a breakpoint can be put here. */
2136 wait_reading_process_input_1 ();
2140 /* Cause C-g and alarm signals to take immediate action,
2141 and cause input available signals to zero out timeout.
2143 It is important that we do this before checking for process
2144 activity. If we get a SIGCHLD after the explicit checks for
2145 process activity, timeout is the only way we will know. */
2146 if (XINT (read_kbd
) < 0)
2147 set_waiting_for_input (&timeout
);
2149 /* If status of something has changed, and no input is
2150 available, notify the user of the change right away. After
2151 this explicit check, we'll let the SIGCHLD handler zap
2152 timeout to get our attention. */
2153 if (update_tick
!= process_tick
&& do_display
)
2155 Atemp
= input_wait_mask
;
2156 EMACS_SET_SECS_USECS (timeout
, 0, 0);
2157 if ((select (MAXDESC
, &Atemp
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
2161 /* It's okay for us to do this and then continue with
2162 the loop, since timeout has already been zeroed out. */
2163 clear_waiting_for_input ();
2168 /* Don't wait for output from a non-running process. */
2169 if (wait_proc
!= 0 && !NILP (wait_proc
->raw_status_low
))
2170 update_status (wait_proc
);
2172 && ! EQ (wait_proc
->status
, Qrun
))
2174 clear_waiting_for_input ();
2178 /* Wait till there is something to do */
2180 if (! XINT (read_kbd
) && wait_for_cell
== 0)
2181 Available
= non_keyboard_wait_mask
;
2183 Available
= input_wait_mask
;
2185 /* If frame size has changed or the window is newly mapped,
2186 redisplay now, before we start to wait. There is a race
2187 condition here; if a SIGIO arrives between now and the select
2188 and indicates that a frame is trashed, the select may block
2189 displaying a trashed screen. */
2190 if (frame_garbaged
&& do_display
)
2192 clear_waiting_for_input ();
2193 redisplay_preserve_echo_area ();
2194 if (XINT (read_kbd
) < 0)
2195 set_waiting_for_input (&timeout
);
2198 if (XINT (read_kbd
) && detect_input_pending ())
2201 FD_ZERO (&Available
);
2204 nfds
= select (MAXDESC
, &Available
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
2209 /* Make C-g and alarm signals set flags again */
2210 clear_waiting_for_input ();
2212 /* If we woke up due to SIGWINCH, actually change size now. */
2213 do_pending_window_change ();
2215 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
2216 /* We wanted the full specified time, so return now. */
2220 if (xerrno
== EINTR
)
2221 FD_ZERO (&Available
);
2223 /* Ultrix select seems to return ENOMEM when it is
2224 interrupted. Treat it just like EINTR. Bleah. Note
2225 that we want to test for the "ultrix" CPP symbol, not
2226 "__ultrix__"; the latter is only defined under GCC, but
2227 not by DEC's bundled CC. -JimB */
2228 else if (xerrno
== ENOMEM
)
2229 FD_ZERO (&Available
);
2232 /* This happens for no known reason on ALLIANT.
2233 I am guessing that this is the right response. -- RMS. */
2234 else if (xerrno
== EFAULT
)
2235 FD_ZERO (&Available
);
2237 else if (xerrno
== EBADF
)
2240 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
2241 the child's closure of the pts gives the parent a SIGHUP, and
2242 the ptc file descriptor is automatically closed,
2243 yielding EBADF here or at select() call above.
2244 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
2245 in m/ibmrt-aix.h), and here we just ignore the select error.
2246 Cleanup occurs c/o status_notify after SIGCLD. */
2247 FD_ZERO (&Available
); /* Cannot depend on values returned */
2253 error ("select error: %s", strerror (xerrno
));
2255 #if defined(sun) && !defined(USG5_4)
2256 else if (nfds
> 0 && keyboard_bit_set (&Available
)
2258 /* System sometimes fails to deliver SIGIO.
2260 David J. Mackenzie says that Emacs doesn't compile under
2261 Solaris if this code is enabled, thus the USG5_4 in the CPP
2262 conditional. "I haven't noticed any ill effects so far.
2263 If you find a Solaris expert somewhere, they might know
2265 kill (getpid (), SIGIO
);
2268 /* Check for keyboard input */
2269 /* If there is any, return immediately
2270 to give it higher priority than subprocesses */
2272 if ((XINT (read_kbd
) != 0)
2273 && detect_input_pending_run_timers (do_display
))
2275 swallow_events (do_display
);
2276 if (detect_input_pending_run_timers (do_display
))
2280 /* If wait_for_cell. check for keyboard input
2281 but don't run any timers.
2282 ??? (It seems wrong to me to check for keyboard
2283 input at all when wait_for_cell, but the code
2284 has been this way since July 1994.
2285 Try changing this after version 19.31.) */
2287 && detect_input_pending ())
2289 swallow_events (do_display
);
2290 if (detect_input_pending ())
2294 /* Exit now if the cell we're waiting for became non-nil. */
2295 if (wait_for_cell
&& ! NILP (*wait_for_cell
))
2299 /* If we think we have keyboard input waiting, but didn't get SIGIO
2300 go read it. This can happen with X on BSD after logging out.
2301 In that case, there really is no input and no SIGIO,
2302 but select says there is input. */
2304 if (XINT (read_kbd
) && interrupt_input
2305 && (keyboard_bit_set (&Available
)))
2306 kill (getpid (), SIGIO
);
2310 got_some_input
|= nfds
> 0;
2312 /* If checking input just got us a size-change event from X,
2313 obey it now if we should. */
2314 if (XINT (read_kbd
) || wait_for_cell
)
2315 do_pending_window_change ();
2317 /* Check for data from a process. */
2318 /* Really FIRST_PROC_DESC should be 0 on Unix,
2319 but this is safer in the short run. */
2320 for (channel
= 0; channel
<= max_process_desc
; channel
++)
2322 if (FD_ISSET (channel
, &Available
)
2323 && FD_ISSET (channel
, &non_keyboard_wait_mask
))
2327 /* If waiting for this channel, arrange to return as
2328 soon as no more input to be processed. No more
2330 if (wait_channel
== channel
)
2336 proc
= chan_process
[channel
];
2340 /* Read data from the process, starting with our
2341 buffered-ahead character if we have one. */
2343 nread
= read_process_output (proc
, channel
);
2346 /* Since read_process_output can run a filter,
2347 which can call accept-process-output,
2348 don't try to read from any other processes
2349 before doing the select again. */
2350 FD_ZERO (&Available
);
2353 redisplay_preserve_echo_area ();
2356 else if (nread
== -1 && errno
== EWOULDBLOCK
)
2359 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
2360 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
2362 else if (nread
== -1 && errno
== EAGAIN
)
2366 else if (nread
== -1 && errno
== EAGAIN
)
2368 /* Note that we cannot distinguish between no input
2369 available now and a closed pipe.
2370 With luck, a closed pipe will be accompanied by
2371 subprocess termination and SIGCHLD. */
2372 else if (nread
== 0 && !NETCONN_P (proc
))
2374 #endif /* O_NDELAY */
2375 #endif /* O_NONBLOCK */
2377 /* On some OSs with ptys, when the process on one end of
2378 a pty exits, the other end gets an error reading with
2379 errno = EIO instead of getting an EOF (0 bytes read).
2380 Therefore, if we get an error reading and errno =
2381 EIO, just continue, because the child process has
2382 exited and should clean itself up soon (e.g. when we
2384 else if (nread
== -1 && errno
== EIO
)
2386 #endif /* HAVE_PTYS */
2387 /* If we can detect process termination, don't consider the process
2388 gone just because its pipe is closed. */
2390 else if (nread
== 0 && !NETCONN_P (proc
))
2395 /* Preserve status of processes already terminated. */
2396 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
2397 deactivate_process (proc
);
2398 if (!NILP (XPROCESS (proc
)->raw_status_low
))
2399 update_status (XPROCESS (proc
));
2400 if (EQ (XPROCESS (proc
)->status
, Qrun
))
2401 XPROCESS (proc
)->status
2402 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
2405 } /* end for each file descriptor */
2406 } /* end while exit conditions not met */
2408 waiting_for_user_input_p
= 0;
2410 /* If calling from keyboard input, do not quit
2411 since we want to return C-g as an input character.
2412 Otherwise, do pending quit if requested. */
2413 if (XINT (read_kbd
) >= 0)
2415 /* Prevent input_pending from remaining set if we quit. */
2416 clear_input_pending ();
2420 /* AlainF 5-Jul-1996
2421 HP-UX 10.10 seems to have problems with signals coming in
2422 Causes "poll: interrupted system call" messages when Emacs is run
2424 Turn periodic alarms back on */
2428 return got_some_input
;
2431 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
2434 read_process_output_call (fun_and_args
)
2435 Lisp_Object fun_and_args
;
2437 return apply1 (XCONS (fun_and_args
)->car
, XCONS (fun_and_args
)->cdr
);
2441 read_process_output_error_handler (error
)
2444 cmd_error_internal (error
, "error in process filter: ");
2446 update_echo_area ();
2447 Fsleep_for (make_number (2), Qnil
);
2450 /* Read pending output from the process channel,
2451 starting with our buffered-ahead character if we have one.
2452 Yield number of characters read.
2454 This function reads at most 1024 characters.
2455 If you want to read all available subprocess output,
2456 you must call it repeatedly until it returns zero. */
2458 read_process_output (proc
, channel
)
2460 register int channel
;
2462 register int nchars
;
2468 register Lisp_Object outstream
;
2469 register struct buffer
*old
= current_buffer
;
2470 register struct Lisp_Process
*p
= XPROCESS (proc
);
2471 register int opoint
;
2474 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
2476 vs
= get_vms_process_pointer (p
->pid
);
2480 return(0); /* Really weird if it does this */
2481 if (!(vs
->iosb
[0] & 1))
2482 return -1; /* I/O error */
2485 error ("Could not get VMS process pointer");
2486 chars
= vs
->inputBuffer
;
2487 nchars
= clean_vms_buffer (chars
, vs
->iosb
[1]);
2490 start_vms_process_read (vs
); /* Crank up the next read on the process */
2491 return 1; /* Nothing worth printing, say we got 1 */
2495 if (proc_buffered_char
[channel
] < 0)
2496 nchars
= read (channel
, chars
, sizeof (chars
));
2499 chars
[0] = proc_buffered_char
[channel
];
2500 proc_buffered_char
[channel
] = -1;
2501 nchars
= read (channel
, chars
+ 1, sizeof (chars
) - 1);
2505 nchars
= nchars
+ 1;
2507 #endif /* not VMS */
2509 if (nchars
<= 0) return nchars
;
2511 outstream
= p
->filter
;
2512 if (!NILP (outstream
))
2514 /* We inhibit quit here instead of just catching it so that
2515 hitting ^G when a filter happens to be running won't screw
2517 int count
= specpdl_ptr
- specpdl
;
2518 Lisp_Object odeactivate
;
2519 Lisp_Object obuffer
, okeymap
;
2520 int outer_running_asynch_code
= running_asynch_code
;
2522 /* No need to gcpro these, because all we do with them later
2523 is test them for EQness, and none of them should be a string. */
2524 odeactivate
= Vdeactivate_mark
;
2525 XSETBUFFER (obuffer
, current_buffer
);
2526 okeymap
= current_buffer
->keymap
;
2528 specbind (Qinhibit_quit
, Qt
);
2529 specbind (Qlast_nonmenu_event
, Qt
);
2531 /* In case we get recursively called,
2532 and we already saved the match data nonrecursively,
2533 save the same match data in safely recursive fashion. */
2534 if (outer_running_asynch_code
)
2537 /* Don't clobber the CURRENT match data, either! */
2538 tem
= Fmatch_data (Qnil
, Qnil
);
2539 restore_match_data ();
2540 record_unwind_protect (Fstore_match_data
, Fmatch_data (Qnil
, Qnil
));
2541 Fstore_match_data (tem
);
2544 /* For speed, if a search happens within this code,
2545 save the match data in a special nonrecursive fashion. */
2546 running_asynch_code
= 1;
2548 /* Read and dispose of the process output. */
2549 internal_condition_case_1 (read_process_output_call
,
2552 Fcons (make_string (chars
,
2555 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
2556 read_process_output_error_handler
);
2558 /* If we saved the match data nonrecursively, restore it now. */
2559 restore_match_data ();
2560 running_asynch_code
= outer_running_asynch_code
;
2562 /* Handling the process output should not deactivate the mark. */
2563 Vdeactivate_mark
= odeactivate
;
2565 #if 0 /* Call record_asynch_buffer_change unconditionally,
2566 because we might have changed minor modes or other things
2567 that affect key bindings. */
2568 if (! EQ (Fcurrent_buffer (), obuffer
)
2569 || ! EQ (current_buffer
->keymap
, okeymap
))
2571 /* But do it only if the caller is actually going to read events.
2572 Otherwise there's no need to make him wake up, and it could
2573 cause trouble (for example it would make Fsit_for return). */
2574 if (waiting_for_user_input_p
== -1)
2575 record_asynch_buffer_change ();
2578 start_vms_process_read (vs
);
2580 unbind_to (count
, Qnil
);
2584 /* If no filter, write into buffer if it isn't dead. */
2585 if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
2587 Lisp_Object old_read_only
;
2588 Lisp_Object old_begv
, old_zv
;
2589 Lisp_Object odeactivate
;
2591 odeactivate
= Vdeactivate_mark
;
2593 Fset_buffer (p
->buffer
);
2595 old_read_only
= current_buffer
->read_only
;
2596 XSETFASTINT (old_begv
, BEGV
);
2597 XSETFASTINT (old_zv
, ZV
);
2599 current_buffer
->read_only
= Qnil
;
2601 /* Insert new output into buffer
2602 at the current end-of-output marker,
2603 thus preserving logical ordering of input and output. */
2604 if (XMARKER (p
->mark
)->buffer
)
2605 SET_PT (clip_to_bounds (BEGV
, marker_position (p
->mark
), ZV
));
2609 /* If the output marker is outside of the visible region, save
2610 the restriction and widen. */
2611 if (! (BEGV
<= PT
&& PT
<= ZV
))
2614 /* Make sure opoint floats ahead of any new text, just as point
2619 /* Insert after old_begv, but before old_zv. */
2620 if (PT
< XFASTINT (old_begv
))
2621 XSETFASTINT (old_begv
, XFASTINT (old_begv
) + nchars
);
2622 if (PT
<= XFASTINT (old_zv
))
2623 XSETFASTINT (old_zv
, XFASTINT (old_zv
) + nchars
);
2625 /* Insert before markers in case we are inserting where
2626 the buffer's mark is, and the user's next command is Meta-y. */
2627 insert_before_markers (chars
, nchars
);
2628 Fset_marker (p
->mark
, make_number (PT
), p
->buffer
);
2630 update_mode_lines
++;
2632 /* If the restriction isn't what it should be, set it. */
2633 if (XFASTINT (old_begv
) != BEGV
|| XFASTINT (old_zv
) != ZV
)
2634 Fnarrow_to_region (old_begv
, old_zv
);
2636 /* Handling the process output should not deactivate the mark. */
2637 Vdeactivate_mark
= odeactivate
;
2639 current_buffer
->read_only
= old_read_only
;
2641 set_buffer_internal (old
);
2644 start_vms_process_read (vs
);
2649 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
2651 "Returns non-nil if emacs is waiting for input from the user.\n\
2652 This is intended for use by asynchronous process output filters and sentinels.")
2655 return (waiting_for_user_input_p
? Qt
: Qnil
);
2658 /* Sending data to subprocess */
2660 jmp_buf send_process_frame
;
2663 send_process_trap ()
2669 longjmp (send_process_frame
, 1);
2672 /* Send some data to process PROC.
2673 BUF is the beginning of the data; LEN is the number of characters.
2674 OBJECT is the Lisp object that the data comes from. */
2676 send_process (proc
, buf
, len
, object
)
2677 volatile Lisp_Object proc
;
2682 /* Use volatile to protect variables from being clobbered by longjmp. */
2684 volatile unsigned char *procname
= XSTRING (XPROCESS (proc
)->name
)->data
;
2685 struct gcpro gcpro1
;
2690 struct Lisp_Process
*p
= XPROCESS (proc
);
2691 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
2694 if (! NILP (XPROCESS (proc
)->raw_status_low
))
2695 update_status (XPROCESS (proc
));
2696 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
2697 error ("Process %s not running", procname
);
2700 vs
= get_vms_process_pointer (p
->pid
);
2702 error ("Could not find this process: %x", p
->pid
);
2703 else if (write_to_vms_process (vs
, buf
, len
))
2707 if (pty_max_bytes
== 0)
2709 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
2710 pty_max_bytes
= fpathconf (XFASTINT (XPROCESS (proc
)->outfd
),
2712 if (pty_max_bytes
< 0)
2713 pty_max_bytes
= 250;
2715 pty_max_bytes
= 250;
2717 /* Deduct one, to leave space for the eof. */
2721 if (!setjmp (send_process_frame
))
2725 SIGTYPE (*old_sigpipe
)();
2728 /* Decide how much data we can send in one batch.
2729 Long lines need to be split into multiple batches. */
2730 if (!NILP (XPROCESS (proc
)->pty_flag
))
2732 /* Starting this at zero is always correct when not the first iteration
2733 because the previous iteration ended by sending C-d.
2734 It may not be correct for the first iteration
2735 if a partial line was sent in a separate send_process call.
2736 If that proves worth handling, we need to save linepos
2737 in the process object. */
2740 char *end
= buf
+ len
;
2742 /* Scan through this text for a line that is too long. */
2743 while (ptr
!= end
&& linepos
< pty_max_bytes
)
2751 /* If we found one, break the line there
2752 and put in a C-d to force the buffer through. */
2756 /* Send this batch, using one or more write calls. */
2759 old_sigpipe
= (SIGTYPE (*) ()) signal (SIGPIPE
, send_process_trap
);
2760 rv
= write (XINT (XPROCESS (proc
)->outfd
), buf
, this);
2761 signal (SIGPIPE
, old_sigpipe
);
2767 || errno
== EWOULDBLOCK
2773 /* Buffer is full. Wait, accepting input;
2774 that may allow the program
2775 to finish doing output and read more. */
2780 /* Running filters might relocate buffers or strings.
2781 Arrange to relocate BUF. */
2782 if (BUFFERP (object
))
2783 offset
= BUF_PTR_CHAR_POS (XBUFFER (object
),
2784 (unsigned char *) buf
);
2785 else if (STRINGP (object
))
2786 offset
= buf
- (char *) XSTRING (object
)->data
;
2788 XSETFASTINT (zero
, 0);
2789 #ifdef EMACS_HAS_USECS
2790 wait_reading_process_input (0, 20000, zero
, 0);
2792 wait_reading_process_input (1, 0, zero
, 0);
2795 if (BUFFERP (object
))
2796 buf
= (char *) BUF_CHAR_ADDRESS (XBUFFER (object
), offset
);
2797 else if (STRINGP (object
))
2798 buf
= offset
+ (char *) XSTRING (object
)->data
;
2803 /* This is a real error. */
2804 report_file_error ("writing to process", Fcons (proc
, Qnil
));
2811 /* If we sent just part of the string, put in an EOF
2812 to force it through, before we send the rest. */
2814 Fprocess_send_eof (proc
);
2819 XPROCESS (proc
)->raw_status_low
= Qnil
;
2820 XPROCESS (proc
)->raw_status_high
= Qnil
;
2821 XPROCESS (proc
)->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
2822 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
2823 deactivate_process (proc
);
2825 error ("Error writing to process %s; closed it", procname
);
2827 error ("SIGPIPE raised on process %s; closed it", procname
);
2834 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
2836 "Send current contents of region as input to PROCESS.\n\
2837 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
2838 nil, indicating the current buffer's process.\n\
2839 Called from program, takes three arguments, PROCESS, START and END.\n\
2840 If the region is more than 500 characters long,\n\
2841 it is sent in several bunches. This may happen even for shorter regions.\n\
2842 Output from processes can arrive in between bunches.")
2843 (process
, start
, end
)
2844 Lisp_Object process
, start
, end
;
2849 proc
= get_process (process
);
2850 validate_region (&start
, &end
);
2852 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
2855 start1
= XINT (start
);
2856 send_process (proc
, &FETCH_CHAR (start1
), XINT (end
) - XINT (start
),
2857 Fcurrent_buffer ());
2862 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
2864 "Send PROCESS the contents of STRING as input.\n\
2865 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
2866 nil, indicating the current buffer's process.\n\
2867 If STRING is more than 500 characters long,\n\
2868 it is sent in several bunches. This may happen even for shorter strings.\n\
2869 Output from processes can arrive in between bunches.")
2871 Lisp_Object process
, string
;
2874 CHECK_STRING (string
, 1);
2875 proc
= get_process (process
);
2876 send_process (proc
, XSTRING (string
)->data
, XSTRING (string
)->size
, string
);
2880 /* send a signal number SIGNO to PROCESS.
2881 CURRENT_GROUP means send to the process group that currently owns
2882 the terminal being used to communicate with PROCESS.
2883 This is used for various commands in shell mode.
2884 If NOMSG is zero, insert signal-announcements into process's buffers
2887 If we can, we try to signal PROCESS by sending control characters
2888 down the pty. This allows us to signal inferiors who have changed
2889 their uid, for which killpg would return an EPERM error. */
2892 process_send_signal (process
, signo
, current_group
, nomsg
)
2893 Lisp_Object process
;
2895 Lisp_Object current_group
;
2899 register struct Lisp_Process
*p
;
2903 proc
= get_process (process
);
2904 p
= XPROCESS (proc
);
2906 if (!EQ (p
->childp
, Qt
))
2907 error ("Process %s is not a subprocess",
2908 XSTRING (p
->name
)->data
);
2909 if (XINT (p
->infd
) < 0)
2910 error ("Process %s is not active",
2911 XSTRING (p
->name
)->data
);
2913 if (NILP (p
->pty_flag
))
2914 current_group
= Qnil
;
2916 /* If we are using pgrps, get a pgrp number and make it negative. */
2917 if (!NILP (current_group
))
2919 #ifdef SIGNALS_VIA_CHARACTERS
2920 /* If possible, send signals to the entire pgrp
2921 by sending an input character to it. */
2923 /* TERMIOS is the latest and bestest, and seems most likely to
2924 work. If the system has it, use it. */
2931 tcgetattr (XINT (p
->infd
), &t
);
2932 send_process (proc
, &t
.c_cc
[VINTR
], 1, Qnil
);
2936 tcgetattr (XINT (p
->infd
), &t
);
2937 send_process (proc
, &t
.c_cc
[VQUIT
], 1, Qnil
);
2941 tcgetattr (XINT (p
->infd
), &t
);
2942 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
2943 send_process (proc
, &t
.c_cc
[VSWTCH
], 1, Qnil
);
2945 send_process (proc
, &t
.c_cc
[VSUSP
], 1, Qnil
);
2950 #else /* ! HAVE_TERMIOS */
2952 /* On Berkeley descendants, the following IOCTL's retrieve the
2953 current control characters. */
2954 #if defined (TIOCGLTC) && defined (TIOCGETC)
2962 ioctl (XINT (p
->infd
), TIOCGETC
, &c
);
2963 send_process (proc
, &c
.t_intrc
, 1, Qnil
);
2966 ioctl (XINT (p
->infd
), TIOCGETC
, &c
);
2967 send_process (proc
, &c
.t_quitc
, 1, Qnil
);
2971 ioctl (XINT (p
->infd
), TIOCGLTC
, &lc
);
2972 send_process (proc
, &lc
.t_suspc
, 1, Qnil
);
2974 #endif /* ! defined (SIGTSTP) */
2977 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
2979 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
2986 ioctl (XINT (p
->infd
), TCGETA
, &t
);
2987 send_process (proc
, &t
.c_cc
[VINTR
], 1, Qnil
);
2990 ioctl (XINT (p
->infd
), TCGETA
, &t
);
2991 send_process (proc
, &t
.c_cc
[VQUIT
], 1, Qnil
);
2995 ioctl (XINT (p
->infd
), TCGETA
, &t
);
2996 send_process (proc
, &t
.c_cc
[VSWTCH
], 1, Qnil
);
2998 #endif /* ! defined (SIGTSTP) */
3000 #else /* ! defined (TCGETA) */
3001 Your configuration files are messed up
.
3002 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
3003 you'd better be using one of the alternatives above! */
3004 #endif /* ! defined (TCGETA) */
3005 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
3006 #endif /* ! defined HAVE_TERMIOS */
3007 #endif /* ! defined (SIGNALS_VIA_CHARACTERS) */
3010 /* Get the pgrp using the tty itself, if we have that.
3011 Otherwise, use the pty to get the pgrp.
3012 On pfa systems, saka@pfu.fujitsu.co.JP writes:
3013 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
3014 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
3015 His patch indicates that if TIOCGPGRP returns an error, then
3016 we should just assume that p->pid is also the process group id. */
3020 if (!NILP (p
->subtty
))
3021 err
= ioctl (XFASTINT (p
->subtty
), TIOCGPGRP
, &gid
);
3023 err
= ioctl (XINT (p
->infd
), TIOCGPGRP
, &gid
);
3027 gid
= - XFASTINT (p
->pid
);
3028 #endif /* ! defined (pfa) */
3034 #else /* ! defined (TIOCGPGRP ) */
3035 /* Can't select pgrps on this system, so we know that
3036 the child itself heads the pgrp. */
3037 gid
= - XFASTINT (p
->pid
);
3038 #endif /* ! defined (TIOCGPGRP ) */
3041 gid
= - XFASTINT (p
->pid
);
3047 p
->raw_status_low
= Qnil
;
3048 p
->raw_status_high
= Qnil
;
3050 XSETINT (p
->tick
, ++process_tick
);
3054 #endif /* ! defined (SIGCONT) */
3057 send_process (proc
, "\003", 1, Qnil
); /* ^C */
3062 send_process (proc
, "\031", 1, Qnil
); /* ^Y */
3067 sys$
forcex (&(XFASTINT (p
->pid
)), 0, 1);
3070 flush_pending_output (XINT (p
->infd
));
3074 /* If we don't have process groups, send the signal to the immediate
3075 subprocess. That isn't really right, but it's better than any
3076 obvious alternative. */
3079 kill (XFASTINT (p
->pid
), signo
);
3083 /* gid may be a pid, or minus a pgrp's number */
3085 if (!NILP (current_group
))
3086 ioctl (XINT (p
->infd
), TIOCSIGSEND
, signo
);
3089 gid
= - XFASTINT (p
->pid
);
3092 #else /* ! defined (TIOCSIGSEND) */
3093 EMACS_KILLPG (-gid
, signo
);
3094 #endif /* ! defined (TIOCSIGSEND) */
3097 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
3098 "Interrupt process PROCESS. May be process or name of one.\n\
3099 PROCESS may be a process, a buffer, or the name of a process or buffer.\n\
3100 nil or no arg means current buffer's process.\n\
3101 Second arg CURRENT-GROUP non-nil means send signal to\n\
3102 the current process-group of the process's controlling terminal\n\
3103 rather than to the process's own process group.\n\
3104 If the process is a shell, this means interrupt current subjob\n\
3105 rather than the shell.")
3106 (process
, current_group
)
3107 Lisp_Object process
, current_group
;
3109 process_send_signal (process
, SIGINT
, current_group
, 0);
3113 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
3114 "Kill process PROCESS. May be process or name of one.\n\
3115 See function `interrupt-process' for more details on usage.")
3116 (process
, current_group
)
3117 Lisp_Object process
, current_group
;
3119 process_send_signal (process
, SIGKILL
, current_group
, 0);
3123 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
3124 "Send QUIT signal to process PROCESS. May be process or name of one.\n\
3125 See function `interrupt-process' for more details on usage.")
3126 (process
, current_group
)
3127 Lisp_Object process
, current_group
;
3129 process_send_signal (process
, SIGQUIT
, current_group
, 0);
3133 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
3134 "Stop process PROCESS. May be process or name of one.\n\
3135 See function `interrupt-process' for more details on usage.")
3136 (process
, current_group
)
3137 Lisp_Object process
, current_group
;
3140 error ("no SIGTSTP support");
3142 process_send_signal (process
, SIGTSTP
, current_group
, 0);
3147 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
3148 "Continue process PROCESS. May be process or name of one.\n\
3149 See function `interrupt-process' for more details on usage.")
3150 (process
, current_group
)
3151 Lisp_Object process
, current_group
;
3154 process_send_signal (process
, SIGCONT
, current_group
, 0);
3156 error ("no SIGCONT support");
3161 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
3162 2, 2, "nProcess number: \nnSignal code: ",
3163 "Send the process with process id PID the signal with code SIGCODE.\n\
3164 PID must be an integer. The process need not be a child of this Emacs.\n\
3165 SIGCODE may be an integer, or a symbol whose name is a signal name.")
3167 Lisp_Object pid
, sigcode
;
3169 CHECK_NUMBER (pid
, 0);
3171 #define handle_signal(NAME, VALUE) \
3172 else if (!strcmp (name, NAME)) \
3173 XSETINT (sigcode, VALUE)
3175 if (INTEGERP (sigcode
))
3179 unsigned char *name
;
3181 CHECK_SYMBOL (sigcode
, 1);
3182 name
= XSYMBOL (sigcode
)->name
->data
;
3187 handle_signal ("SIGHUP", SIGHUP
);
3190 handle_signal ("SIGINT", SIGINT
);
3193 handle_signal ("SIGQUIT", SIGQUIT
);
3196 handle_signal ("SIGILL", SIGILL
);
3199 handle_signal ("SIGABRT", SIGABRT
);
3202 handle_signal ("SIGEMT", SIGEMT
);
3205 handle_signal ("SIGKILL", SIGKILL
);
3208 handle_signal ("SIGFPE", SIGFPE
);
3211 handle_signal ("SIGBUS", SIGBUS
);
3214 handle_signal ("SIGSEGV", SIGSEGV
);
3217 handle_signal ("SIGSYS", SIGSYS
);
3220 handle_signal ("SIGPIPE", SIGPIPE
);
3223 handle_signal ("SIGALRM", SIGALRM
);
3226 handle_signal ("SIGTERM", SIGTERM
);
3229 handle_signal ("SIGURG", SIGURG
);
3232 handle_signal ("SIGSTOP", SIGSTOP
);
3235 handle_signal ("SIGTSTP", SIGTSTP
);
3238 handle_signal ("SIGCONT", SIGCONT
);
3241 handle_signal ("SIGCHLD", SIGCHLD
);
3244 handle_signal ("SIGTTIN", SIGTTIN
);
3247 handle_signal ("SIGTTOU", SIGTTOU
);
3250 handle_signal ("SIGIO", SIGIO
);
3253 handle_signal ("SIGXCPU", SIGXCPU
);
3256 handle_signal ("SIGXFSZ", SIGXFSZ
);
3259 handle_signal ("SIGVTALRM", SIGVTALRM
);
3262 handle_signal ("SIGPROF", SIGPROF
);
3265 handle_signal ("SIGWINCH", SIGWINCH
);
3268 handle_signal ("SIGINFO", SIGINFO
);
3271 handle_signal ("SIGUSR1", SIGUSR1
);
3274 handle_signal ("SIGUSR2", SIGUSR2
);
3277 error ("Undefined signal name %s", name
);
3280 #undef handle_signal
3282 return make_number (kill (XINT (pid
), XINT (sigcode
)));
3285 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
3286 "Make PROCESS see end-of-file in its input.\n\
3287 Eof comes after any text already sent to it.\n\
3288 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
3289 nil, indicating the current buffer's process.\n\
3290 If PROCESS is a network connection, or is a process communicating\n\
3291 through a pipe (as opposed to a pty), then you cannot send any more\n\
3292 text to PROCESS after you call this function.")
3294 Lisp_Object process
;
3298 proc
= get_process (process
);
3300 /* Make sure the process is really alive. */
3301 if (! NILP (XPROCESS (proc
)->raw_status_low
))
3302 update_status (XPROCESS (proc
));
3303 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
3304 error ("Process %s not running", XSTRING (XPROCESS (proc
)->name
)->data
);
3307 send_process (proc
, "\032", 1, Qnil
); /* ^z */
3309 if (!NILP (XPROCESS (proc
)->pty_flag
))
3310 send_process (proc
, "\004", 1, Qnil
);
3313 close (XINT (XPROCESS (proc
)->outfd
));
3314 XSETINT (XPROCESS (proc
)->outfd
, open (NULL_DEVICE
, O_WRONLY
));
3320 /* Kill all processes associated with `buffer'.
3321 If `buffer' is nil, kill all processes */
3323 kill_buffer_processes (buffer
)
3326 Lisp_Object tail
, proc
;
3328 for (tail
= Vprocess_alist
; GC_CONSP (tail
); tail
= XCONS (tail
)->cdr
)
3330 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
3331 if (GC_PROCESSP (proc
)
3332 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
3334 if (NETCONN_P (proc
))
3335 Fdelete_process (proc
);
3336 else if (XINT (XPROCESS (proc
)->infd
) >= 0)
3337 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
3342 /* On receipt of a signal that a child status has changed,
3343 loop asking about children with changed statuses until
3344 the system says there are no more.
3345 All we do is change the status;
3346 we do not run sentinels or print notifications.
3347 That is saved for the next time keyboard input is done,
3348 in order to avoid timing errors. */
3350 /** WARNING: this can be called during garbage collection.
3351 Therefore, it must not be fooled by the presence of mark bits in
3354 /** USG WARNING: Although it is not obvious from the documentation
3355 in signal(2), on a USG system the SIGCLD handler MUST NOT call
3356 signal() before executing at least one wait(), otherwise the handler
3357 will be called again, resulting in an infinite loop. The relevant
3358 portion of the documentation reads "SIGCLD signals will be queued
3359 and the signal-catching function will be continually reentered until
3360 the queue is empty". Invoking signal() causes the kernel to reexamine
3361 the SIGCLD queue. Fred Fish, UniSoft Systems Inc. */
3364 sigchld_handler (signo
)
3367 int old_errno
= errno
;
3369 register struct Lisp_Process
*p
;
3370 extern EMACS_TIME
*input_available_clear_time
;
3374 sigheld
|= sigbit (SIGCHLD
);
3386 #endif /* no WUNTRACED */
3387 /* Keep trying to get a status until we get a definitive result. */
3391 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
3393 while (pid
<= 0 && errno
== EINTR
);
3397 /* A real failure. We have done all our job, so return. */
3399 /* USG systems forget handlers when they are used;
3400 must reestablish each time */
3401 #if defined (USG) && !defined (POSIX_SIGNALS)
3402 signal (signo
, sigchld_handler
); /* WARNING - must come after wait3() */
3405 sigheld
&= ~sigbit (SIGCHLD
);
3413 #endif /* no WNOHANG */
3415 /* Find the process that signaled us, and record its status. */
3418 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
3420 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
3421 p
= XPROCESS (proc
);
3422 if (EQ (p
->childp
, Qt
) && XFASTINT (p
->pid
) == pid
)
3427 /* Look for an asynchronous process whose pid hasn't been filled
3430 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
3432 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
3433 p
= XPROCESS (proc
);
3434 if (INTEGERP (p
->pid
) && XINT (p
->pid
) == -1)
3439 /* Change the status of the process that was found. */
3442 union { int i
; WAITTYPE wt
; } u
;
3443 int clear_desc_flag
= 0;
3445 XSETINT (p
->tick
, ++process_tick
);
3447 XSETINT (p
->raw_status_low
, u
.i
& 0xffff);
3448 XSETINT (p
->raw_status_high
, u
.i
>> 16);
3450 /* If process has terminated, stop waiting for its output. */
3451 if ((WIFSIGNALED (w
) || WIFEXITED (w
))
3452 && XINT (p
->infd
) >= 0)
3453 clear_desc_flag
= 1;
3455 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
3456 if (clear_desc_flag
)
3458 FD_CLR (XINT (p
->infd
), &input_wait_mask
);
3459 FD_CLR (XINT (p
->infd
), &non_keyboard_wait_mask
);
3462 /* Tell wait_reading_process_input that it needs to wake up and
3464 if (input_available_clear_time
)
3465 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
3468 /* There was no asynchronous process found for that id. Check
3469 if we have a synchronous process. */
3472 synch_process_alive
= 0;
3474 /* Report the status of the synchronous process. */
3476 synch_process_retcode
= WRETCODE (w
);
3477 else if (WIFSIGNALED (w
))
3479 int code
= WTERMSIG (w
);
3485 /* Suppress warning if the table has const char *. */
3486 signame
= (char *) sys_siglist
[code
];
3488 signame
= sys_errlist
[code
];
3492 signame
= "unknown";
3494 synch_process_death
= signame
;
3497 /* Tell wait_reading_process_input that it needs to wake up and
3499 if (input_available_clear_time
)
3500 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
3503 /* On some systems, we must return right away.
3504 If any more processes want to signal us, we will
3506 Otherwise (on systems that have WNOHANG), loop around
3507 to use up all the processes that have something to tell us. */
3508 #if defined (USG) && ! (defined (HPUX) && defined (WNOHANG)) || defined (WINDOWSNT)
3509 #if defined (USG) && ! defined (POSIX_SIGNALS)
3510 signal (signo
, sigchld_handler
);
3514 #endif /* USG, but not HPUX with WNOHANG */
3520 exec_sentinel_unwind (data
)
3523 XPROCESS (XCONS (data
)->car
)->sentinel
= XCONS (data
)->cdr
;
3528 exec_sentinel_error_handler (error
)
3531 cmd_error_internal (error
, "error in process sentinel: ");
3533 update_echo_area ();
3534 Fsleep_for (make_number (2), Qnil
);
3538 exec_sentinel (proc
, reason
)
3539 Lisp_Object proc
, reason
;
3541 Lisp_Object sentinel
, obuffer
, odeactivate
, okeymap
;
3542 register struct Lisp_Process
*p
= XPROCESS (proc
);
3543 int count
= specpdl_ptr
- specpdl
;
3544 int outer_running_asynch_code
= running_asynch_code
;
3546 /* No need to gcpro these, because all we do with them later
3547 is test them for EQness, and none of them should be a string. */
3548 odeactivate
= Vdeactivate_mark
;
3549 XSETBUFFER (obuffer
, current_buffer
);
3550 okeymap
= current_buffer
->keymap
;
3552 sentinel
= p
->sentinel
;
3553 if (NILP (sentinel
))
3556 /* Zilch the sentinel while it's running, to avoid recursive invocations;
3557 assure that it gets restored no matter how the sentinel exits. */
3559 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
3560 /* Inhibit quit so that random quits don't screw up a running filter. */
3561 specbind (Qinhibit_quit
, Qt
);
3562 specbind (Qlast_nonmenu_event
, Qt
);
3564 /* In case we get recursively called,
3565 and we already saved the match data nonrecursively,
3566 save the same match data in safely recursive fashion. */
3567 if (outer_running_asynch_code
)
3570 tem
= Fmatch_data (Qnil
, Qnil
);
3571 restore_match_data ();
3572 record_unwind_protect (Fstore_match_data
, Fmatch_data (Qnil
, Qnil
));
3573 Fstore_match_data (tem
);
3576 /* For speed, if a search happens within this code,
3577 save the match data in a special nonrecursive fashion. */
3578 running_asynch_code
= 1;
3580 internal_condition_case_1 (read_process_output_call
,
3582 Fcons (proc
, Fcons (reason
, Qnil
))),
3583 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
3584 exec_sentinel_error_handler
);
3586 /* If we saved the match data nonrecursively, restore it now. */
3587 restore_match_data ();
3588 running_asynch_code
= outer_running_asynch_code
;
3590 Vdeactivate_mark
= odeactivate
;
3592 if (! EQ (Fcurrent_buffer (), obuffer
)
3593 || ! EQ (current_buffer
->keymap
, okeymap
))
3595 /* But do it only if the caller is actually going to read events.
3596 Otherwise there's no need to make him wake up, and it could
3597 cause trouble (for example it would make Fsit_for return). */
3598 if (waiting_for_user_input_p
== -1)
3599 record_asynch_buffer_change ();
3601 unbind_to (count
, Qnil
);
3604 /* Report all recent events of a change in process status
3605 (either run the sentinel or output a message).
3606 This is done while Emacs is waiting for keyboard input. */
3610 register Lisp_Object proc
, buffer
;
3611 Lisp_Object tail
, msg
;
3612 struct gcpro gcpro1
, gcpro2
;
3616 /* We need to gcpro tail; if read_process_output calls a filter
3617 which deletes a process and removes the cons to which tail points
3618 from Vprocess_alist, and then causes a GC, tail is an unprotected
3622 /* Set this now, so that if new processes are created by sentinels
3623 that we run, we get called again to handle their status changes. */
3624 update_tick
= process_tick
;
3626 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
3629 register struct Lisp_Process
*p
;
3631 proc
= Fcdr (Fcar (tail
));
3632 p
= XPROCESS (proc
);
3634 if (XINT (p
->tick
) != XINT (p
->update_tick
))
3636 XSETINT (p
->update_tick
, XINT (p
->tick
));
3638 /* If process is still active, read any output that remains. */
3639 while (! EQ (p
->filter
, Qt
)
3640 && XINT (p
->infd
) >= 0
3641 && read_process_output (proc
, XINT (p
->infd
)) > 0);
3645 /* Get the text to use for the message. */
3646 if (!NILP (p
->raw_status_low
))
3648 msg
= status_message (p
->status
);
3650 /* If process is terminated, deactivate it or delete it. */
3652 if (CONSP (p
->status
))
3653 symbol
= XCONS (p
->status
)->car
;
3655 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
3656 || EQ (symbol
, Qclosed
))
3658 if (delete_exited_processes
)
3659 remove_process (proc
);
3661 deactivate_process (proc
);
3664 /* The actions above may have further incremented p->tick.
3665 So set p->update_tick again
3666 so that an error in the sentinel will not cause
3667 this code to be run again. */
3668 XSETINT (p
->update_tick
, XINT (p
->tick
));
3669 /* Now output the message suitably. */
3670 if (!NILP (p
->sentinel
))
3671 exec_sentinel (proc
, msg
);
3672 /* Don't bother with a message in the buffer
3673 when a process becomes runnable. */
3674 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
3676 Lisp_Object ro
, tem
;
3677 struct buffer
*old
= current_buffer
;
3680 ro
= XBUFFER (buffer
)->read_only
;
3682 /* Avoid error if buffer is deleted
3683 (probably that's why the process is dead, too) */
3684 if (NILP (XBUFFER (buffer
)->name
))
3686 Fset_buffer (buffer
);
3688 /* Insert new output into buffer
3689 at the current end-of-output marker,
3690 thus preserving logical ordering of input and output. */
3691 if (XMARKER (p
->mark
)->buffer
)
3692 SET_PT (marker_position (p
->mark
));
3696 opoint
+= XSTRING (msg
)->size
+ XSTRING (p
->name
)->size
+ 10;
3698 tem
= current_buffer
->read_only
;
3699 current_buffer
->read_only
= Qnil
;
3700 insert_string ("\nProcess ");
3701 Finsert (1, &p
->name
);
3702 insert_string (" ");
3704 current_buffer
->read_only
= tem
;
3705 Fset_marker (p
->mark
, make_number (PT
), p
->buffer
);
3708 set_buffer_internal (old
);
3713 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
3714 redisplay_preserve_echo_area ();
3719 /* The first time this is called, assume keyboard input comes from DESC
3720 instead of from where we used to expect it.
3721 Subsequent calls mean assume input keyboard can come from DESC
3722 in addition to other places. */
3724 static int add_keyboard_wait_descriptor_called_flag
;
3727 add_keyboard_wait_descriptor (desc
)
3730 if (! add_keyboard_wait_descriptor_called_flag
)
3731 FD_CLR (0, &input_wait_mask
);
3732 add_keyboard_wait_descriptor_called_flag
= 1;
3733 FD_SET (desc
, &input_wait_mask
);
3734 if (desc
> max_keyboard_desc
)
3735 max_keyboard_desc
= desc
;
3738 /* From now on, do not expect DESC to give keyboard input. */
3741 delete_keyboard_wait_descriptor (desc
)
3745 int lim
= max_keyboard_desc
;
3747 FD_CLR (desc
, &input_wait_mask
);
3749 if (desc
== max_keyboard_desc
)
3750 for (fd
= 0; fd
< lim
; fd
++)
3751 if (FD_ISSET (fd
, &input_wait_mask
)
3752 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
3753 max_keyboard_desc
= fd
;
3756 /* Return nonzero if *MASK has a bit set
3757 that corresponds to one of the keyboard input descriptors. */
3760 keyboard_bit_set (mask
)
3765 for (fd
= 0; fd
<= max_keyboard_desc
; fd
++)
3766 if (FD_ISSET (fd
, mask
) && FD_ISSET (fd
, &input_wait_mask
)
3767 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
3779 if (! noninteractive
|| initialized
)
3781 signal (SIGCHLD
, sigchld_handler
);
3784 FD_ZERO (&input_wait_mask
);
3785 FD_ZERO (&non_keyboard_wait_mask
);
3786 max_process_desc
= 0;
3788 FD_SET (0, &input_wait_mask
);
3790 Vprocess_alist
= Qnil
;
3791 for (i
= 0; i
< MAXDESC
; i
++)
3793 chan_process
[i
] = Qnil
;
3794 proc_buffered_char
[i
] = -1;
3800 Qprocessp
= intern ("processp");
3801 staticpro (&Qprocessp
);
3802 Qrun
= intern ("run");
3804 Qstop
= intern ("stop");
3806 Qsignal
= intern ("signal");
3807 staticpro (&Qsignal
);
3809 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
3812 Qexit = intern ("exit");
3813 staticpro (&Qexit); */
3815 Qopen
= intern ("open");
3817 Qclosed
= intern ("closed");
3818 staticpro (&Qclosed
);
3820 Qlast_nonmenu_event
= intern ("last-nonmenu-event");
3821 staticpro (&Qlast_nonmenu_event
);
3823 staticpro (&Vprocess_alist
);
3825 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
3826 "*Non-nil means delete processes immediately when they exit.\n\
3827 nil means don't delete them until `list-processes' is run.");
3829 delete_exited_processes
= 1;
3831 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
3832 "Control type of device used to communicate with subprocesses.\n\
3833 Values are nil to use a pipe, or t or `pty' to use a pty.\n\
3834 The value has no effect if the system has no ptys or if all ptys are busy:\n\
3835 then a pipe is used in any case.\n\
3836 The value takes effect when `start-process' is called.");
3837 Vprocess_connection_type
= Qt
;
3839 defsubr (&Sprocessp
);
3840 defsubr (&Sget_process
);
3841 defsubr (&Sget_buffer_process
);
3842 defsubr (&Sdelete_process
);
3843 defsubr (&Sprocess_status
);
3844 defsubr (&Sprocess_exit_status
);
3845 defsubr (&Sprocess_id
);
3846 defsubr (&Sprocess_name
);
3847 defsubr (&Sprocess_tty_name
);
3848 defsubr (&Sprocess_command
);
3849 defsubr (&Sset_process_buffer
);
3850 defsubr (&Sprocess_buffer
);
3851 defsubr (&Sprocess_mark
);
3852 defsubr (&Sset_process_filter
);
3853 defsubr (&Sprocess_filter
);
3854 defsubr (&Sset_process_sentinel
);
3855 defsubr (&Sprocess_sentinel
);
3856 defsubr (&Sset_process_window_size
);
3857 defsubr (&Sprocess_kill_without_query
);
3858 defsubr (&Sprocess_contact
);
3859 defsubr (&Slist_processes
);
3860 defsubr (&Sprocess_list
);
3861 defsubr (&Sstart_process
);
3863 defsubr (&Sopen_network_stream
);
3864 #endif /* HAVE_SOCKETS */
3865 defsubr (&Saccept_process_output
);
3866 defsubr (&Sprocess_send_region
);
3867 defsubr (&Sprocess_send_string
);
3868 defsubr (&Sinterrupt_process
);
3869 defsubr (&Skill_process
);
3870 defsubr (&Squit_process
);
3871 defsubr (&Sstop_process
);
3872 defsubr (&Scontinue_process
);
3873 defsubr (&Sprocess_send_eof
);
3874 defsubr (&Ssignal_process
);
3875 defsubr (&Swaiting_for_user_input_p
);
3876 /* defsubr (&Sprocess_connection); */
3880 #else /* not subprocesses */
3882 #include <sys/types.h>
3886 #include "systime.h"
3887 #include "termopts.h"
3888 #include "sysselect.h"
3890 extern int frame_garbaged
;
3892 extern EMACS_TIME
timer_check ();
3893 extern int timers_run
;
3895 /* As described above, except assuming that there are no subprocesses:
3897 Wait for timeout to elapse and/or keyboard input to be available.
3900 timeout in seconds, or
3901 zero for no limit, or
3902 -1 means gobble data immediately available but don't wait for any.
3904 read_kbd is a Lisp_Object:
3905 0 to ignore keyboard input, or
3906 1 to return when input is available, or
3907 -1 means caller will actually read the input, so don't throw to
3909 a cons cell, meaning wait until its car is non-nil
3910 (and gobble terminal input into the buffer if any arrives), or
3911 We know that read_kbd will never be a Lisp_Process, since
3912 `subprocesses' isn't defined.
3914 do_display != 0 means redisplay should be done to show subprocess
3915 output that arrives.
3917 Return true iff we received input from any process. */
3920 wait_reading_process_input (time_limit
, microsecs
, read_kbd
, do_display
)
3921 int time_limit
, microsecs
;
3922 Lisp_Object read_kbd
;
3925 EMACS_TIME end_time
, timeout
;
3926 SELECT_TYPE waitchannels
;
3928 Lisp_Object
*wait_for_cell
= 0;
3930 /* If waiting for non-nil in a cell, record where. */
3931 if (CONSP (read_kbd
))
3933 wait_for_cell
= &XCONS (read_kbd
)->car
;
3934 XSETFASTINT (read_kbd
, 0);
3937 /* What does time_limit really mean? */
3938 if (time_limit
|| microsecs
)
3940 if (time_limit
== -1)
3941 /* In fact, it's zero. */
3942 EMACS_SET_SECS_USECS (timeout
, 0, 0);
3944 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
3946 /* How far in the future is that? */
3947 EMACS_GET_TIME (end_time
);
3948 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
3951 /* It's infinite. */
3952 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
3954 /* Turn off periodic alarms (in case they are in use)
3955 because the select emulator uses alarms. */
3961 int timeout_reduced_for_timers
= 0;
3963 /* If calling from keyboard input, do not quit
3964 since we want to return C-g as an input character.
3965 Otherwise, do pending quit if requested. */
3966 if (XINT (read_kbd
) >= 0)
3969 /* Exit now if the cell we're waiting for became non-nil. */
3970 if (wait_for_cell
&& ! NILP (*wait_for_cell
))
3973 /* Compute time from now till when time limit is up */
3974 /* Exit if already run out */
3975 if (time_limit
> 0 || microsecs
)
3977 EMACS_GET_TIME (timeout
);
3978 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
3979 if (EMACS_TIME_NEG_P (timeout
))
3983 /* If our caller will not immediately handle keyboard events,
3984 run timer events directly.
3985 (Callers that will immediately read keyboard events
3986 call timer_delay on their own.) */
3987 if (! wait_for_cell
)
3989 EMACS_TIME timer_delay
;
3993 old_timers_run
= timers_run
;
3994 timer_delay
= timer_check (1);
3995 if (timers_run
!= old_timers_run
&& do_display
)
3997 redisplay_preserve_echo_area ();
3998 /* We must retry, since a timer may have requeued itself
3999 and that could alter the time delay. */
4003 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
4005 EMACS_TIME difference
;
4006 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
4007 if (EMACS_TIME_NEG_P (difference
))
4009 timeout
= timer_delay
;
4010 timeout_reduced_for_timers
= 1;
4015 /* Cause C-g and alarm signals to take immediate action,
4016 and cause input available signals to zero out timeout. */
4017 if (XINT (read_kbd
) < 0)
4018 set_waiting_for_input (&timeout
);
4020 /* Wait till there is something to do. */
4022 if (! XINT (read_kbd
) && wait_for_cell
== 0)
4023 FD_ZERO (&waitchannels
);
4025 FD_SET (0, &waitchannels
);
4027 /* If a frame has been newly mapped and needs updating,
4028 reprocess its display stuff. */
4029 if (frame_garbaged
&& do_display
)
4031 clear_waiting_for_input ();
4032 redisplay_preserve_echo_area ();
4033 if (XINT (read_kbd
) < 0)
4034 set_waiting_for_input (&timeout
);
4037 if (XINT (read_kbd
) && detect_input_pending ())
4040 FD_ZERO (&waitchannels
);
4043 nfds
= select (1, &waitchannels
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
4048 /* Make C-g and alarm signals set flags again */
4049 clear_waiting_for_input ();
4051 /* If we woke up due to SIGWINCH, actually change size now. */
4052 do_pending_window_change ();
4054 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
4055 /* We waited the full specified time, so return now. */
4060 /* If the system call was interrupted, then go around the
4062 if (xerrno
== EINTR
)
4063 FD_ZERO (&waitchannels
);
4065 error ("select error: %s", strerror (xerrno
));
4068 else if (nfds
> 0 && (waitchannels
& 1) && interrupt_input
)
4069 /* System sometimes fails to deliver SIGIO. */
4070 kill (getpid (), SIGIO
);
4073 if (XINT (read_kbd
) && interrupt_input
&& (waitchannels
& 1))
4074 kill (getpid (), SIGIO
);
4077 /* Check for keyboard input */
4079 if ((XINT (read_kbd
) != 0)
4080 && detect_input_pending_run_timers (do_display
))
4082 swallow_events (do_display
);
4083 if (detect_input_pending_run_timers (do_display
))
4087 /* If wait_for_cell. check for keyboard input
4088 but don't run any timers.
4089 ??? (It seems wrong to me to check for keyboard
4090 input at all when wait_for_cell, but the code
4091 has been this way since July 1994.
4092 Try changing this after version 19.31.) */
4094 && detect_input_pending ())
4096 swallow_events (do_display
);
4097 if (detect_input_pending ())
4101 /* Exit now if the cell we're waiting for became non-nil. */
4102 if (wait_for_cell
&& ! NILP (*wait_for_cell
))
4112 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
4113 /* Don't confuse make-docfile by having two doc strings for this function.
4114 make-docfile does not pay attention to #if, for good reason! */
4117 register Lisp_Object name
;
4122 /* Kill all processes associated with `buffer'.
4123 If `buffer' is nil, kill all processes.
4124 Since we have no subprocesses, this does nothing. */
4126 kill_buffer_processes (buffer
)
4137 defsubr (&Sget_buffer_process
);
4141 #endif /* not subprocesses */