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" */
104 #include "termhooks.h"
105 #include "termopts.h"
106 #include "commands.h"
108 #include "blockinput.h"
110 #define max(a, b) ((a) > (b) ? (a) : (b))
112 Lisp_Object Qprocessp
;
113 Lisp_Object Qrun
, Qstop
, Qsignal
, Qopen
, Qclosed
;
114 Lisp_Object Qlast_nonmenu_event
;
115 /* Qexit is declared and initialized in eval.c. */
117 /* a process object is a network connection when its childp field is neither
118 Qt nor Qnil but is instead a cons cell (HOSTNAME PORTNUM). */
121 #define NETCONN_P(p) (GC_CONSP (XPROCESS (p)->childp))
123 #define NETCONN_P(p) 0
124 #endif /* HAVE_SOCKETS */
126 /* Define first descriptor number available for subprocesses. */
128 #define FIRST_PROC_DESC 1
130 #define FIRST_PROC_DESC 3
133 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
136 #if !defined (SIGCHLD) && defined (SIGCLD)
137 #define SIGCHLD SIGCLD
140 #include "syssignal.h"
145 extern char *strerror ();
147 extern char *sys_errlist
[];
154 #ifndef SYS_SIGLIST_DECLARED
159 extern char *sys_siglist
[];
160 #endif /* not LINUX */
162 char *sys_siglist
[] =
168 "illegal instruction",
172 "floating point exception",
175 "segmentation violation",
176 "bad argument to system call",
177 "write on a pipe with no one to read it",
179 "software termination signal from kill",
181 "sendable stop signal not from tty",
182 "stop signal from tty",
183 "continue a stopped process",
184 "child status has changed",
185 "background read attempted from control tty",
186 "background write attempted from control tty",
187 "input record available at control tty",
188 "exceeded CPU time limit",
189 "exceeded file size limit"
191 #endif /* not WINDOWSNT */
194 #endif /* ! SYS_SIGLIST_DECLARED */
196 /* t means use pty, nil means use a pipe,
197 maybe other values to come. */
198 static Lisp_Object Vprocess_connection_type
;
202 #include <sys/socket.h>
206 /* These next two vars are non-static since sysdep.c uses them in the
207 emulation of `select'. */
208 /* Number of events of change of status of a process. */
210 /* Number of events for which the user or sentinel has been notified. */
213 #include "sysselect.h"
215 /* If we support a window system, turn on the code to poll periodically
216 to detect C-g. It isn't actually used when doing interrupt input. */
217 #ifdef HAVE_WINDOW_SYSTEM
218 #define POLL_FOR_INPUT
221 /* Mask of bits indicating the descriptors that we wait for input on. */
223 static SELECT_TYPE input_wait_mask
;
225 /* Mask that excludes keyboard input descriptor (s). */
227 static SELECT_TYPE non_keyboard_wait_mask
;
229 /* Mask that excludes process input descriptor (s). */
231 static SELECT_TYPE non_process_wait_mask
;
233 /* The largest descriptor currently in use for a process object. */
234 static int max_process_desc
;
236 /* The largest descriptor currently in use for keyboard input. */
237 static int max_keyboard_desc
;
239 /* Nonzero means delete a process right away if it exits. */
240 static int delete_exited_processes
;
242 /* Indexed by descriptor, gives the process (if any) for that descriptor */
243 Lisp_Object chan_process
[MAXDESC
];
245 /* Alist of elements (NAME . PROCESS) */
246 Lisp_Object Vprocess_alist
;
248 /* Buffered-ahead input char from process, indexed by channel.
249 -1 means empty (no char is buffered).
250 Used on sys V where the only way to tell if there is any
251 output from the process is to read at least one char.
252 Always -1 on systems that support FIONREAD. */
254 /* Don't make static; need to access externally. */
255 int proc_buffered_char
[MAXDESC
];
257 /* Table of `struct coding-system' for each process. */
258 static struct coding_system
*proc_decode_coding_system
[MAXDESC
];
259 static struct coding_system
*proc_encode_coding_system
[MAXDESC
];
261 static Lisp_Object
get_process ();
263 extern EMACS_TIME
timer_check ();
264 extern int timers_run
;
266 /* Maximum number of bytes to send to a pty without an eof. */
267 static int pty_max_bytes
;
270 /* The file name of the pty opened by allocate_pty. */
272 static char pty_name
[24];
275 /* Compute the Lisp form of the process status, p->status, from
276 the numeric status that was returned by `wait'. */
278 Lisp_Object
status_convert ();
281 struct Lisp_Process
*p
;
283 union { int i
; WAITTYPE wt
; } u
;
284 u
.i
= XFASTINT (p
->raw_status_low
) + (XFASTINT (p
->raw_status_high
) << 16);
285 p
->status
= status_convert (u
.wt
);
286 p
->raw_status_low
= Qnil
;
287 p
->raw_status_high
= Qnil
;
290 /* Convert a process status word in Unix format to
291 the list that we use internally. */
298 return Fcons (Qstop
, Fcons (make_number (WSTOPSIG (w
)), Qnil
));
299 else if (WIFEXITED (w
))
300 return Fcons (Qexit
, Fcons (make_number (WRETCODE (w
)),
301 WCOREDUMP (w
) ? Qt
: Qnil
));
302 else if (WIFSIGNALED (w
))
303 return Fcons (Qsignal
, Fcons (make_number (WTERMSIG (w
)),
304 WCOREDUMP (w
) ? Qt
: Qnil
));
309 /* Given a status-list, extract the three pieces of information
310 and store them individually through the three pointers. */
313 decode_status (l
, symbol
, code
, coredump
)
329 *symbol
= XCONS (l
)->car
;
330 tem
= XCONS (l
)->cdr
;
331 *code
= XFASTINT (XCONS (tem
)->car
);
332 tem
= XCONS (tem
)->cdr
;
333 *coredump
= !NILP (tem
);
337 /* Return a string describing a process status list. */
340 status_message (status
)
345 Lisp_Object string
, string2
;
347 decode_status (status
, &symbol
, &code
, &coredump
);
349 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qstop
))
355 /* Cast to suppress warning if the table has const char *. */
356 signame
= (char *) sys_siglist
[code
];
358 signame
= sys_errlist
[code
];
363 string
= build_string (signame
);
364 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
365 XSTRING (string
)->data
[0] = DOWNCASE (XSTRING (string
)->data
[0]);
366 return concat2 (string
, string2
);
368 else if (EQ (symbol
, Qexit
))
371 return build_string ("finished\n");
372 string
= Fnumber_to_string (make_number (code
));
373 string2
= build_string (coredump
? " (core dumped)\n" : "\n");
374 return concat2 (build_string ("exited abnormally with code "),
375 concat2 (string
, string2
));
378 return Fcopy_sequence (Fsymbol_name (symbol
));
383 /* Open an available pty, returning a file descriptor.
384 Return -1 on failure.
385 The file name of the terminal corresponding to the pty
386 is left in the variable pty_name. */
395 /* Some systems name their pseudoterminals so that there are gaps in
396 the usual sequence - for example, on HP9000/S700 systems, there
397 are no pseudoterminals with names ending in 'f'. So we wait for
398 three failures in a row before deciding that we've reached the
400 int failed_count
= 0;
405 for (c
= FIRST_PTY_LETTER
; c
<= 'z'; c
++)
406 for (i
= 0; i
< 16; i
++)
409 #ifdef PTY_NAME_SPRINTF
412 sprintf (pty_name
, "/dev/pty%c%x", c
, i
);
413 #endif /* no PTY_NAME_SPRINTF */
417 #else /* no PTY_OPEN */
419 /* Unusual IRIS code */
420 *ptyv
= open ("/dev/ptc", O_RDWR
| O_NDELAY
, 0);
423 if (fstat (fd
, &stb
) < 0)
426 if (stat (pty_name
, &stb
) < 0)
429 if (failed_count
>= 3)
435 fd
= open (pty_name
, O_RDWR
| O_NONBLOCK
, 0);
437 fd
= open (pty_name
, O_RDWR
| O_NDELAY
, 0);
439 #endif /* not IRIS */
440 #endif /* no PTY_OPEN */
444 /* check to make certain that both sides are available
445 this avoids a nasty yet stupid bug in rlogins */
446 #ifdef PTY_TTY_NAME_SPRINTF
449 sprintf (pty_name
, "/dev/tty%c%x", c
, i
);
450 #endif /* no PTY_TTY_NAME_SPRINTF */
452 if (access (pty_name
, 6) != 0)
455 #if !defined(IRIS) && !defined(__sgi)
461 #endif /* not UNIPLUS */
468 #endif /* HAVE_PTYS */
474 struct Lisp_Vector
*vec
;
475 register Lisp_Object val
, tem
, name1
;
476 register struct Lisp_Process
*p
;
480 vec
= allocate_vectorlike ((EMACS_INT
) VECSIZE (struct Lisp_Process
));
481 for (i
= 0; i
< VECSIZE (struct Lisp_Process
); i
++)
482 vec
->contents
[i
] = Qnil
;
483 vec
->size
= VECSIZE (struct Lisp_Process
);
484 p
= (struct Lisp_Process
*)vec
;
486 XSETINT (p
->infd
, -1);
487 XSETINT (p
->outfd
, -1);
488 XSETFASTINT (p
->pid
, 0);
489 XSETFASTINT (p
->tick
, 0);
490 XSETFASTINT (p
->update_tick
, 0);
491 p
->raw_status_low
= Qnil
;
492 p
->raw_status_high
= Qnil
;
494 p
->mark
= Fmake_marker ();
496 /* If name is already in use, modify it until it is unused. */
501 tem
= Fget_process (name1
);
502 if (NILP (tem
)) break;
503 sprintf (suffix
, "<%d>", i
);
504 name1
= concat2 (name
, build_string (suffix
));
508 XSETPROCESS (val
, p
);
509 Vprocess_alist
= Fcons (Fcons (name
, val
), Vprocess_alist
);
513 remove_process (proc
)
514 register Lisp_Object proc
;
516 register Lisp_Object pair
;
518 pair
= Frassq (proc
, Vprocess_alist
);
519 Vprocess_alist
= Fdelq (pair
, Vprocess_alist
);
521 deactivate_process (proc
);
524 DEFUN ("processp", Fprocessp
, Sprocessp
, 1, 1, 0,
525 "Return t if OBJECT is a process.")
529 return PROCESSP (object
) ? Qt
: Qnil
;
532 DEFUN ("get-process", Fget_process
, Sget_process
, 1, 1, 0,
533 "Return the process named NAME, or nil if there is none.")
535 register Lisp_Object name
;
539 CHECK_STRING (name
, 0);
540 return Fcdr (Fassoc (name
, Vprocess_alist
));
543 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
544 "Return the (or, a) process associated with BUFFER.\n\
545 BUFFER may be a buffer or the name of one.")
547 register Lisp_Object buffer
;
549 register Lisp_Object buf
, tail
, proc
;
551 if (NILP (buffer
)) return Qnil
;
552 buf
= Fget_buffer (buffer
);
553 if (NILP (buf
)) return Qnil
;
555 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
557 proc
= Fcdr (Fcar (tail
));
558 if (PROCESSP (proc
) && EQ (XPROCESS (proc
)->buffer
, buf
))
564 /* This is how commands for the user decode process arguments. It
565 accepts a process, a process name, a buffer, a buffer name, or nil.
566 Buffers denote the first process in the buffer, and nil denotes the
571 register Lisp_Object name
;
573 register Lisp_Object proc
, obj
;
576 obj
= Fget_process (name
);
578 obj
= Fget_buffer (name
);
580 error ("Process %s does not exist", XSTRING (name
)->data
);
582 else if (NILP (name
))
583 obj
= Fcurrent_buffer ();
587 /* Now obj should be either a buffer object or a process object.
591 proc
= Fget_buffer_process (obj
);
593 error ("Buffer %s has no process", XSTRING (XBUFFER (obj
)->name
)->data
);
597 CHECK_PROCESS (obj
, 0);
603 DEFUN ("delete-process", Fdelete_process
, Sdelete_process
, 1, 1, 0,
604 "Delete PROCESS: kill it and forget about it immediately.\n\
605 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
606 nil, indicating the current buffer's process.")
608 register Lisp_Object process
;
610 process
= get_process (process
);
611 XPROCESS (process
)->raw_status_low
= Qnil
;
612 XPROCESS (process
)->raw_status_high
= Qnil
;
613 if (NETCONN_P (process
))
615 XPROCESS (process
)->status
= Fcons (Qexit
, Fcons (make_number (0), Qnil
));
616 XSETINT (XPROCESS (process
)->tick
, ++process_tick
);
618 else if (XINT (XPROCESS (process
)->infd
) >= 0)
620 Fkill_process (process
, Qnil
);
621 /* Do this now, since remove_process will make sigchld_handler do nothing. */
622 XPROCESS (process
)->status
623 = Fcons (Qsignal
, Fcons (make_number (SIGKILL
), Qnil
));
624 XSETINT (XPROCESS (process
)->tick
, ++process_tick
);
627 remove_process (process
);
631 DEFUN ("process-status", Fprocess_status
, Sprocess_status
, 1, 1, 0,
632 "Return the status of PROCESS: a symbol, one of these:\n\
633 run -- for a process that is running.\n\
634 stop -- for a process stopped but continuable.\n\
635 exit -- for a process that has exited.\n\
636 signal -- for a process that has got a fatal signal.\n\
637 open -- for a network stream connection that is open.\n\
638 closed -- for a network stream connection that is closed.\n\
639 nil -- if arg is a process name and no such process exists.\n\
640 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
641 nil, indicating the current buffer's process.")
643 register Lisp_Object process
;
645 register struct Lisp_Process
*p
;
646 register Lisp_Object status
;
648 if (STRINGP (process
))
649 process
= Fget_process (process
);
651 process
= get_process (process
);
656 p
= XPROCESS (process
);
657 if (!NILP (p
->raw_status_low
))
661 status
= XCONS (status
)->car
;
662 if (NETCONN_P (process
))
664 if (EQ (status
, Qrun
))
666 else if (EQ (status
, Qexit
))
672 DEFUN ("process-exit-status", Fprocess_exit_status
, Sprocess_exit_status
,
674 "Return the exit status of PROCESS or the signal number that killed it.\n\
675 If PROCESS has not yet exited or died, return 0.")
677 register Lisp_Object process
;
679 CHECK_PROCESS (process
, 0);
680 if (!NILP (XPROCESS (process
)->raw_status_low
))
681 update_status (XPROCESS (process
));
682 if (CONSP (XPROCESS (process
)->status
))
683 return XCONS (XCONS (XPROCESS (process
)->status
)->cdr
)->car
;
684 return make_number (0);
687 DEFUN ("process-id", Fprocess_id
, Sprocess_id
, 1, 1, 0,
688 "Return the process id of PROCESS.\n\
689 This is the pid of the Unix process which PROCESS uses or talks to.\n\
690 For a network connection, this value is nil.")
692 register Lisp_Object process
;
694 CHECK_PROCESS (process
, 0);
695 return XPROCESS (process
)->pid
;
698 DEFUN ("process-name", Fprocess_name
, Sprocess_name
, 1, 1, 0,
699 "Return the name of PROCESS, as a string.\n\
700 This is the name of the program invoked in PROCESS,\n\
701 possibly modified to make it unique among process names.")
703 register Lisp_Object process
;
705 CHECK_PROCESS (process
, 0);
706 return XPROCESS (process
)->name
;
709 DEFUN ("process-command", Fprocess_command
, Sprocess_command
, 1, 1, 0,
710 "Return the command that was executed to start PROCESS.\n\
711 This is a list of strings, the first string being the program executed\n\
712 and the rest of the strings being the arguments given to it.\n\
713 For a non-child channel, this is nil.")
715 register Lisp_Object process
;
717 CHECK_PROCESS (process
, 0);
718 return XPROCESS (process
)->command
;
721 DEFUN ("process-tty-name", Fprocess_tty_name
, Sprocess_tty_name
, 1, 1, 0,
722 "Return the name of the terminal PROCESS uses, or nil if none.\n\
723 This is the terminal that the process itself reads and writes on,\n\
724 not the name of the pty that Emacs uses to talk with that terminal.")
726 register Lisp_Object process
;
728 CHECK_PROCESS (process
, 0);
729 return XPROCESS (process
)->tty_name
;
732 DEFUN ("set-process-buffer", Fset_process_buffer
, Sset_process_buffer
,
734 "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
736 register Lisp_Object process
, buffer
;
738 CHECK_PROCESS (process
, 0);
740 CHECK_BUFFER (buffer
, 1);
741 XPROCESS (process
)->buffer
= buffer
;
745 DEFUN ("process-buffer", Fprocess_buffer
, Sprocess_buffer
,
747 "Return the buffer PROCESS is associated with.\n\
748 Output from PROCESS is inserted in this buffer\n\
749 unless PROCESS has a filter.")
751 register Lisp_Object process
;
753 CHECK_PROCESS (process
, 0);
754 return XPROCESS (process
)->buffer
;
757 DEFUN ("process-mark", Fprocess_mark
, Sprocess_mark
,
759 "Return the marker for the end of the last output from PROCESS.")
761 register Lisp_Object process
;
763 CHECK_PROCESS (process
, 0);
764 return XPROCESS (process
)->mark
;
767 DEFUN ("set-process-filter", Fset_process_filter
, Sset_process_filter
,
769 "Give PROCESS the filter function FILTER; nil means no filter.\n\
770 t means stop accepting output from the process.\n\
771 When a process has a filter, each time it does output\n\
772 the entire string of output is passed to the filter.\n\
773 The filter gets two arguments: the process and the string of output.\n\
774 If the process has a filter, its buffer is not used for output.")
776 register Lisp_Object process
, filter
;
778 CHECK_PROCESS (process
, 0);
781 FD_CLR (XINT (XPROCESS (process
)->infd
), &input_wait_mask
);
782 FD_CLR (XINT (XPROCESS (process
)->infd
), &non_keyboard_wait_mask
);
784 else if (EQ (XPROCESS (process
)->filter
, Qt
))
786 FD_SET (XINT (XPROCESS (process
)->infd
), &input_wait_mask
);
787 FD_SET (XINT (XPROCESS (process
)->infd
), &non_keyboard_wait_mask
);
789 XPROCESS (process
)->filter
= filter
;
793 DEFUN ("process-filter", Fprocess_filter
, Sprocess_filter
,
795 "Returns the filter function of PROCESS; nil if none.\n\
796 See `set-process-filter' for more info on filter functions.")
798 register Lisp_Object process
;
800 CHECK_PROCESS (process
, 0);
801 return XPROCESS (process
)->filter
;
804 DEFUN ("set-process-sentinel", Fset_process_sentinel
, Sset_process_sentinel
,
806 "Give PROCESS the sentinel SENTINEL; nil for none.\n\
807 The sentinel is called as a function when the process changes state.\n\
808 It gets two arguments: the process, and a string describing the change.")
810 register Lisp_Object process
, sentinel
;
812 CHECK_PROCESS (process
, 0);
813 XPROCESS (process
)->sentinel
= sentinel
;
817 DEFUN ("process-sentinel", Fprocess_sentinel
, Sprocess_sentinel
,
819 "Return the sentinel of PROCESS; nil if none.\n\
820 See `set-process-sentinel' for more info on sentinels.")
822 register Lisp_Object process
;
824 CHECK_PROCESS (process
, 0);
825 return XPROCESS (process
)->sentinel
;
828 DEFUN ("set-process-window-size", Fset_process_window_size
,
829 Sset_process_window_size
, 3, 3, 0,
830 "Tell PROCESS that it has logical window size HEIGHT and WIDTH.")
831 (process
, height
, width
)
832 register Lisp_Object process
, height
, width
;
834 CHECK_PROCESS (process
, 0);
835 CHECK_NATNUM (height
, 0);
836 CHECK_NATNUM (width
, 0);
837 if (set_window_size (XINT (XPROCESS (process
)->infd
),
838 XINT (height
), XINT(width
)) <= 0)
844 DEFUN ("process-kill-without-query", Fprocess_kill_without_query
,
845 Sprocess_kill_without_query
, 1, 2, 0,
846 "Say no query needed if PROCESS is running when Emacs is exited.\n\
847 Optional second argument if non-nil says to require a query.\n\
848 Value is t if a query was formerly required.")
850 register Lisp_Object process
, value
;
854 CHECK_PROCESS (process
, 0);
855 tem
= XPROCESS (process
)->kill_without_query
;
856 XPROCESS (process
)->kill_without_query
= Fnull (value
);
861 DEFUN ("process-contact", Fprocess_contact
, Sprocess_contact
,
863 "Return the contact info of PROCESS; t for a real child.\n\
864 For a net connection, the value is a cons cell of the form (HOST SERVICE).")
866 register Lisp_Object process
;
868 CHECK_PROCESS (process
, 0);
869 return XPROCESS (process
)->childp
;
872 #if 0 /* Turned off because we don't currently record this info
873 in the process. Perhaps add it. */
874 DEFUN ("process-connection", Fprocess_connection
, Sprocess_connection
, 1, 1, 0,
875 "Return the connection type of `PROCESS'.\n\
876 The value is `nil' for a pipe,\n\
877 `t' or `pty' for a pty, or `stream' for a socket connection.")
881 return XPROCESS (process
)->type
;
888 register Lisp_Object tail
, tem
;
889 Lisp_Object proc
, minspace
, tem1
;
890 register struct buffer
*old
= current_buffer
;
891 register struct Lisp_Process
*p
;
895 XSETFASTINT (minspace
, 1);
897 set_buffer_internal (XBUFFER (Vstandard_output
));
898 Fbuffer_disable_undo (Vstandard_output
);
900 current_buffer
->truncate_lines
= Qt
;
903 Proc Status Buffer Tty Command\n\
904 ---- ------ ------ --- -------\n", -1);
906 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
910 proc
= Fcdr (Fcar (tail
));
912 if (NILP (p
->childp
))
915 Finsert (1, &p
->name
);
916 Findent_to (make_number (13), minspace
);
918 if (!NILP (p
->raw_status_low
))
921 if (CONSP (p
->status
))
922 symbol
= XCONS (p
->status
)->car
;
925 if (EQ (symbol
, Qsignal
))
928 tem
= Fcar (Fcdr (p
->status
));
930 if (XINT (tem
) < NSIG
)
931 write_string (sys_errlist
[XINT (tem
)], -1);
934 Fprinc (symbol
, Qnil
);
936 else if (NETCONN_P (proc
))
938 if (EQ (symbol
, Qrun
))
939 write_string ("open", -1);
940 else if (EQ (symbol
, Qexit
))
941 write_string ("closed", -1);
943 Fprinc (symbol
, Qnil
);
946 Fprinc (symbol
, Qnil
);
948 if (EQ (symbol
, Qexit
))
951 tem
= Fcar (Fcdr (p
->status
));
954 sprintf (tembuf
, " %d", (int) XFASTINT (tem
));
955 write_string (tembuf
, -1);
959 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
))
960 remove_process (proc
);
962 Findent_to (make_number (22), minspace
);
963 if (NILP (p
->buffer
))
964 insert_string ("(none)");
965 else if (NILP (XBUFFER (p
->buffer
)->name
))
966 insert_string ("(Killed)");
968 Finsert (1, &XBUFFER (p
->buffer
)->name
);
970 Findent_to (make_number (37), minspace
);
972 if (STRINGP (p
->tty_name
))
973 Finsert (1, &p
->tty_name
);
975 insert_string ("(none)");
977 Findent_to (make_number (49), minspace
);
979 if (NETCONN_P (proc
))
981 sprintf (tembuf
, "(network stream connection to %s)\n",
982 XSTRING (XCONS (p
->childp
)->car
)->data
);
983 insert_string (tembuf
);
997 insert_string ("\n");
1003 DEFUN ("list-processes", Flist_processes
, Slist_processes
, 0, 0, "",
1004 "Display a list of all processes.\n\
1005 \(Any processes listed as Exited or Signaled are actually eliminated\n\
1006 after the listing is made.)")
1009 internal_with_output_to_temp_buffer ("*Process List*",
1010 list_processes_1
, Qnil
);
1014 DEFUN ("process-list", Fprocess_list
, Sprocess_list
, 0, 0, 0,
1015 "Return a list of all processes.")
1018 return Fmapcar (Qcdr
, Vprocess_alist
);
1021 /* Starting asynchronous inferior processes. */
1023 static Lisp_Object
start_process_unwind ();
1025 DEFUN ("start-process", Fstart_process
, Sstart_process
, 3, MANY
, 0,
1026 "Start a program in a subprocess. Return the process object for it.\n\
1027 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
1028 NAME is name for process. It is modified if necessary to make it unique.\n\
1029 BUFFER is the buffer or (buffer-name) to associate with the process.\n\
1030 Process output goes at end of that buffer, unless you specify\n\
1031 an output stream or filter function to handle the output.\n\
1032 BUFFER may be also nil, meaning that this process is not associated\n\
1034 Third arg is program file name. It is searched for as in the shell.\n\
1035 Remaining arguments are strings to give program as arguments.")
1038 register Lisp_Object
*args
;
1040 Lisp_Object buffer
, name
, program
, proc
, current_dir
, tem
;
1042 register unsigned char *new_argv
;
1045 register unsigned char **new_argv
;
1048 int count
= specpdl_ptr
- specpdl
;
1052 buffer
= Fget_buffer_create (buffer
);
1054 /* Make sure that the child will be able to chdir to the current
1055 buffer's current directory, or its unhandled equivalent. We
1056 can't just have the child check for an error when it does the
1057 chdir, since it's in a vfork.
1059 We have to GCPRO around this because Fexpand_file_name and
1060 Funhandled_file_name_directory might call a file name handling
1061 function. The argument list is protected by the caller, so all
1062 we really have to worry about is buffer. */
1064 struct gcpro gcpro1
, gcpro2
;
1066 current_dir
= current_buffer
->directory
;
1068 GCPRO2 (buffer
, current_dir
);
1071 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
1073 if (NILP (Ffile_accessible_directory_p (current_dir
)))
1074 report_file_error ("Setting current directory",
1075 Fcons (current_buffer
->directory
, Qnil
));
1081 CHECK_STRING (name
, 0);
1085 CHECK_STRING (program
, 2);
1088 /* Make a one member argv with all args concatenated
1089 together separated by a blank. */
1090 len
= XSTRING (program
)->size
+ 2;
1091 for (i
= 3; i
< nargs
; i
++)
1094 CHECK_STRING (tem
, i
);
1095 len
+= XSTRING (tem
)->size
+ 1; /* count the blank */
1097 new_argv
= (unsigned char *) alloca (len
);
1098 strcpy (new_argv
, XSTRING (program
)->data
);
1099 for (i
= 3; i
< nargs
; i
++)
1102 CHECK_STRING (tem
, i
);
1103 strcat (new_argv
, " ");
1104 strcat (new_argv
, XSTRING (tem
)->data
);
1106 /* Need to add code here to check for program existence on VMS */
1109 new_argv
= (unsigned char **) alloca ((nargs
- 1) * sizeof (char *));
1111 /* If program file name is not absolute, search our path for it */
1112 if (!IS_DIRECTORY_SEP (XSTRING (program
)->data
[0])
1113 && !(XSTRING (program
)->size
> 1
1114 && IS_DEVICE_SEP (XSTRING (program
)->data
[1])))
1116 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1119 GCPRO4 (name
, program
, buffer
, current_dir
);
1120 openp (Vexec_path
, program
, EXEC_SUFFIXES
, &tem
, 1);
1123 report_file_error ("Searching for program", Fcons (program
, Qnil
));
1124 tem
= Fexpand_file_name (tem
, Qnil
);
1125 new_argv
[0] = XSTRING (tem
)->data
;
1129 if (!NILP (Ffile_directory_p (program
)))
1130 error ("Specified program for new process is a directory");
1132 new_argv
[0] = XSTRING (program
)->data
;
1135 for (i
= 3; i
< nargs
; i
++)
1138 CHECK_STRING (tem
, i
);
1139 new_argv
[i
- 2] = XSTRING (tem
)->data
;
1141 new_argv
[i
- 2] = 0;
1142 #endif /* not VMS */
1144 proc
= make_process (name
);
1145 /* If an error occurs and we can't start the process, we want to
1146 remove it from the process list. This means that each error
1147 check in create_process doesn't need to call remove_process
1148 itself; it's all taken care of here. */
1149 record_unwind_protect (start_process_unwind
, proc
);
1151 XPROCESS (proc
)->childp
= Qt
;
1152 XPROCESS (proc
)->command_channel_p
= Qnil
;
1153 XPROCESS (proc
)->buffer
= buffer
;
1154 XPROCESS (proc
)->sentinel
= Qnil
;
1155 XPROCESS (proc
)->filter
= Qnil
;
1156 XPROCESS (proc
)->command
= Flist (nargs
- 2, args
+ 2);
1158 /* Make the process marker point into the process buffer (if any). */
1160 Fset_marker (XPROCESS (proc
)->mark
,
1161 make_number (BUF_ZV (XBUFFER (buffer
))), buffer
);
1163 if (!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
)
1164 || NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
))
1166 XPROCESS (proc
)->decode_coding_system
= Qnil
;
1167 XPROCESS (proc
)->encode_coding_system
= Qnil
;
1171 /* Setup coding systems for communicating with the process. */
1172 /* Qt denotes that we have not yet called Ffind_coding_system. */
1173 Lisp_Object coding_systems
= Qt
;
1174 Lisp_Object val
, *args2
;
1175 struct gcpro gcpro1
;
1177 if (NILP (val
= Vcoding_system_for_read
))
1179 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1180 args2
[0] = Qstart_process
;
1181 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1183 coding_systems
= Ffind_coding_system (nargs
+ 1, args2
);
1185 if (CONSP (coding_systems
))
1186 val
= XCONS (coding_systems
)->car
;
1187 else if (CONSP (Vdefault_process_coding_system
))
1188 val
= XCONS (Vdefault_process_coding_system
)->car
;
1190 XPROCESS (proc
)->decode_coding_system
= val
;
1192 if (NILP (val
= Vcoding_system_for_write
))
1194 if (EQ (coding_systems
, Qt
))
1196 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof args2
);
1197 args2
[0] = Qstart_process
;
1198 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1200 coding_systems
= Ffind_coding_system (nargs
+ 1, args2
);
1203 if (CONSP (coding_systems
))
1204 val
= XCONS (coding_systems
)->cdr
;
1205 else if (CONSP (Vdefault_process_coding_system
))
1206 val
= XCONS (Vdefault_process_coding_system
)->cdr
;
1208 XPROCESS (proc
)->encode_coding_system
= val
;
1211 XPROCESS (proc
)->decoding_buf
= make_uninit_string (0);
1212 XPROCESS (proc
)->encoding_buf
= make_uninit_string (0);
1214 create_process (proc
, new_argv
, current_dir
);
1216 return unbind_to (count
, proc
);
1219 /* This function is the unwind_protect form for Fstart_process. If
1220 PROC doesn't have its pid set, then we know someone has signaled
1221 an error and the process wasn't started successfully, so we should
1222 remove it from the process list. */
1224 start_process_unwind (proc
)
1227 if (!PROCESSP (proc
))
1230 /* Was PROC started successfully? */
1231 if (XINT (XPROCESS (proc
)->pid
) <= 0)
1232 remove_process (proc
);
1239 create_process_1 (signo
)
1242 #if defined (USG) && !defined (POSIX_SIGNALS)
1243 /* USG systems forget handlers when they are used;
1244 must reestablish each time */
1245 signal (signo
, create_process_1
);
1249 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1252 /* Mimic blocking of signals on system V, which doesn't really have it. */
1254 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1255 int sigchld_deferred
;
1258 create_process_sigchld ()
1260 signal (SIGCHLD
, create_process_sigchld
);
1262 sigchld_deferred
= 1;
1268 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1269 create_process (process
, new_argv
, current_dir
)
1270 Lisp_Object process
;
1272 Lisp_Object current_dir
;
1274 int pid
, inchannel
, outchannel
;
1276 #ifdef POSIX_SIGNALS
1279 struct sigaction sigint_action
;
1280 struct sigaction sigquit_action
;
1282 struct sigaction sighup_action
;
1284 #else /* !POSIX_SIGNALS */
1286 SIGTYPE (*sigchld
)();
1288 #endif /* !POSIX_SIGNALS */
1289 /* Use volatile to protect variables from being clobbered by longjmp. */
1290 volatile int forkin
, forkout
;
1291 volatile int pty_flag
= 0;
1292 extern char **environ
;
1294 inchannel
= outchannel
= -1;
1297 if (!NILP (Vprocess_connection_type
))
1298 outchannel
= inchannel
= allocate_pty ();
1303 /* On USG systems it does not work to open the pty's tty here
1304 and then close and reopen it in the child. */
1306 /* Don't let this terminal become our controlling terminal
1307 (in case we don't have one). */
1308 forkout
= forkin
= open (pty_name
, O_RDWR
| O_NOCTTY
, 0);
1310 forkout
= forkin
= open (pty_name
, O_RDWR
, 0);
1313 report_file_error ("Opening pty", Qnil
);
1315 forkin
= forkout
= -1;
1316 #endif /* not USG */
1320 #endif /* HAVE_PTYS */
1323 if (socketpair (AF_UNIX
, SOCK_STREAM
, 0, sv
) < 0)
1324 report_file_error ("Opening socketpair", Qnil
);
1325 outchannel
= inchannel
= sv
[0];
1326 forkout
= forkin
= sv
[1];
1328 #else /* not SKTPAIR */
1337 #endif /* not SKTPAIR */
1340 /* Replaced by close_process_descs */
1341 set_exclusive_use (inchannel
);
1342 set_exclusive_use (outchannel
);
1345 /* Stride people say it's a mystery why this is needed
1346 as well as the O_NDELAY, but that it fails without this. */
1347 #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
1350 ioctl (inchannel
, FIONBIO
, &one
);
1355 fcntl (inchannel
, F_SETFL
, O_NONBLOCK
);
1356 fcntl (outchannel
, F_SETFL
, O_NONBLOCK
);
1359 fcntl (inchannel
, F_SETFL
, O_NDELAY
);
1360 fcntl (outchannel
, F_SETFL
, O_NDELAY
);
1364 /* Record this as an active process, with its channels.
1365 As a result, child_setup will close Emacs's side of the pipes. */
1366 chan_process
[inchannel
] = process
;
1367 XSETINT (XPROCESS (process
)->infd
, inchannel
);
1368 XSETINT (XPROCESS (process
)->outfd
, outchannel
);
1369 /* Record the tty descriptor used in the subprocess. */
1371 XPROCESS (process
)->subtty
= Qnil
;
1373 XSETFASTINT (XPROCESS (process
)->subtty
, forkin
);
1374 XPROCESS (process
)->pty_flag
= (pty_flag
? Qt
: Qnil
);
1375 XPROCESS (process
)->status
= Qrun
;
1376 if (!proc_decode_coding_system
[inchannel
])
1377 proc_decode_coding_system
[inchannel
]
1378 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
1379 setup_coding_system (XPROCESS (process
)->decode_coding_system
,
1380 proc_decode_coding_system
[inchannel
]);
1381 if (!proc_encode_coding_system
[outchannel
])
1382 proc_encode_coding_system
[outchannel
]
1383 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
1384 setup_coding_system (XPROCESS (process
)->encode_coding_system
,
1385 proc_encode_coding_system
[outchannel
]);
1387 /* Delay interrupts until we have a chance to store
1388 the new fork's pid in its process structure */
1389 #ifdef POSIX_SIGNALS
1390 sigemptyset (&blocked
);
1392 sigaddset (&blocked
, SIGCHLD
);
1395 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1396 this sets the parent's signal handlers as well as the child's.
1397 So delay all interrupts whose handlers the child might munge,
1398 and record the current handlers so they can be restored later. */
1399 sigaddset (&blocked
, SIGINT
); sigaction (SIGINT
, 0, &sigint_action
);
1400 sigaddset (&blocked
, SIGQUIT
); sigaction (SIGQUIT
, 0, &sigquit_action
);
1402 sigaddset (&blocked
, SIGHUP
); sigaction (SIGHUP
, 0, &sighup_action
);
1404 #endif /* HAVE_VFORK */
1405 sigprocmask (SIG_BLOCK
, &blocked
, &procmask
);
1406 #else /* !POSIX_SIGNALS */
1410 #else /* not BSD4_1 */
1411 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
1412 sigsetmask (sigmask (SIGCHLD
));
1413 #else /* ordinary USG */
1415 sigchld_deferred
= 0;
1416 sigchld
= signal (SIGCHLD
, create_process_sigchld
);
1418 #endif /* ordinary USG */
1419 #endif /* not BSD4_1 */
1420 #endif /* SIGCHLD */
1421 #endif /* !POSIX_SIGNALS */
1423 FD_SET (inchannel
, &input_wait_mask
);
1424 FD_SET (inchannel
, &non_keyboard_wait_mask
);
1425 if (inchannel
> max_process_desc
)
1426 max_process_desc
= inchannel
;
1428 /* Until we store the proper pid, enable sigchld_handler
1429 to recognize an unknown pid as standing for this process.
1430 It is very important not to let this `marker' value stay
1431 in the table after this function has returned; if it does
1432 it might cause call-process to hang and subsequent asynchronous
1433 processes to get their return values scrambled. */
1434 XSETINT (XPROCESS (process
)->pid
, -1);
1439 /* child_setup must clobber environ on systems with true vfork.
1440 Protect it from permanent change. */
1441 char **save_environ
= environ
;
1446 #endif /* not WINDOWSNT */
1448 int xforkin
= forkin
;
1449 int xforkout
= forkout
;
1451 #if 0 /* This was probably a mistake--it duplicates code later on,
1452 but fails to handle all the cases. */
1453 /* Make sure SIGCHLD is not blocked in the child. */
1454 sigsetmask (SIGEMPTYMASK
);
1457 /* Make the pty be the controlling terminal of the process. */
1459 /* First, disconnect its current controlling terminal. */
1461 /* We tried doing setsid only if pty_flag, but it caused
1462 process_set_signal to fail on SGI when using a pipe. */
1464 /* Make the pty's terminal the controlling terminal. */
1468 /* We ignore the return value
1469 because faith@cs.unc.edu says that is necessary on Linux. */
1470 ioctl (xforkin
, TIOCSCTTY
, 0);
1473 #else /* not HAVE_SETSID */
1475 /* It's very important to call setpgrp here and no time
1476 afterwards. Otherwise, we lose our controlling tty which
1477 is set when we open the pty. */
1480 #endif /* not HAVE_SETSID */
1481 #if defined (HAVE_TERMIOS) && defined (LDISC1)
1482 if (pty_flag
&& xforkin
>= 0)
1485 tcgetattr (xforkin
, &t
);
1487 if (tcsetattr (xforkin
, TCSANOW
, &t
) < 0)
1488 write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
1491 #if defined (NTTYDISC) && defined (TIOCSETD)
1492 if (pty_flag
&& xforkin
>= 0)
1494 /* Use new line discipline. */
1495 int ldisc
= NTTYDISC
;
1496 ioctl (xforkin
, TIOCSETD
, &ldisc
);
1501 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1502 can do TIOCSPGRP only to the process's controlling tty. */
1505 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1506 I can't test it since I don't have 4.3. */
1507 int j
= open ("/dev/tty", O_RDWR
, 0);
1508 ioctl (j
, TIOCNOTTY
, 0);
1511 /* In order to get a controlling terminal on some versions
1512 of BSD, it is necessary to put the process in pgrp 0
1513 before it opens the terminal. */
1521 #endif /* TIOCNOTTY */
1523 #if !defined (RTU) && !defined (UNIPLUS) && !defined (DONT_REOPEN_PTY)
1524 /*** There is a suggestion that this ought to be a
1525 conditional on TIOCSPGRP,
1526 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
1527 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
1528 that system does seem to need this code, even though
1529 both HAVE_SETSID and TIOCSCTTY are defined. */
1530 /* Now close the pty (if we had it open) and reopen it.
1531 This makes the pty the controlling terminal of the subprocess. */
1534 #ifdef SET_CHILD_PTY_PGRP
1535 int pgrp
= getpid ();
1538 /* I wonder if close (open (pty_name, ...)) would work? */
1541 xforkout
= xforkin
= open (pty_name
, O_RDWR
, 0);
1545 write (1, "Couldn't open the pty terminal ", 31);
1546 write (1, pty_name
, strlen (pty_name
));
1551 #ifdef SET_CHILD_PTY_PGRP
1552 ioctl (xforkin
, TIOCSPGRP
, &pgrp
);
1553 ioctl (xforkout
, TIOCSPGRP
, &pgrp
);
1556 #endif /* not UNIPLUS and not RTU and not DONT_REOPEN_PTY */
1558 #ifdef SETUP_SLAVE_PTY
1563 #endif /* SETUP_SLAVE_PTY */
1565 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
1566 Now reenable it in the child, so it will die when we want it to. */
1568 signal (SIGHUP
, SIG_DFL
);
1570 #endif /* HAVE_PTYS */
1572 signal (SIGINT
, SIG_DFL
);
1573 signal (SIGQUIT
, SIG_DFL
);
1575 /* Stop blocking signals in the child. */
1576 #ifdef POSIX_SIGNALS
1577 sigprocmask (SIG_SETMASK
, &procmask
, 0);
1578 #else /* !POSIX_SIGNALS */
1582 #else /* not BSD4_1 */
1583 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
1584 sigsetmask (SIGEMPTYMASK
);
1585 #else /* ordinary USG */
1587 signal (SIGCHLD
, sigchld
);
1589 #endif /* ordinary USG */
1590 #endif /* not BSD4_1 */
1591 #endif /* SIGCHLD */
1592 #endif /* !POSIX_SIGNALS */
1595 child_setup_tty (xforkout
);
1597 pid
= child_setup (xforkin
, xforkout
, xforkout
,
1598 new_argv
, 1, current_dir
);
1599 #else /* not WINDOWSNT */
1600 child_setup (xforkin
, xforkout
, xforkout
,
1601 new_argv
, 1, current_dir
);
1602 #endif /* not WINDOWSNT */
1604 environ
= save_environ
;
1609 /* This runs in the Emacs process. */
1614 if (forkin
!= forkout
&& forkout
>= 0)
1619 /* vfork succeeded. */
1620 XSETFASTINT (XPROCESS (process
)->pid
, pid
);
1623 register_child (pid
, inchannel
);
1624 #endif /* WINDOWSNT */
1626 /* If the subfork execv fails, and it exits,
1627 this close hangs. I don't know why.
1628 So have an interrupt jar it loose. */
1630 signal (SIGALRM
, create_process_1
);
1632 XPROCESS (process
)->subtty
= Qnil
;
1637 if (forkin
!= forkout
&& forkout
>= 0)
1642 XPROCESS (process
)->tty_name
= build_string (pty_name
);
1645 XPROCESS (process
)->tty_name
= Qnil
;
1648 /* Restore the signal state whether vfork succeeded or not.
1649 (We will signal an error, below, if it failed.) */
1650 #ifdef POSIX_SIGNALS
1652 /* Restore the parent's signal handlers. */
1653 sigaction (SIGINT
, &sigint_action
, 0);
1654 sigaction (SIGQUIT
, &sigquit_action
, 0);
1656 sigaction (SIGHUP
, &sighup_action
, 0);
1658 #endif /* HAVE_VFORK */
1659 /* Stop blocking signals in the parent. */
1660 sigprocmask (SIG_SETMASK
, &procmask
, 0);
1661 #else /* !POSIX_SIGNALS */
1665 #else /* not BSD4_1 */
1666 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
1667 sigsetmask (SIGEMPTYMASK
);
1668 #else /* ordinary USG */
1670 signal (SIGCHLD
, sigchld
);
1671 /* Now really handle any of these signals
1672 that came in during this function. */
1673 if (sigchld_deferred
)
1674 kill (getpid (), SIGCHLD
);
1676 #endif /* ordinary USG */
1677 #endif /* not BSD4_1 */
1678 #endif /* SIGCHLD */
1679 #endif /* !POSIX_SIGNALS */
1681 /* Now generate the error if vfork failed. */
1683 report_file_error ("Doing vfork", Qnil
);
1685 #endif /* not VMS */
1689 /* open a TCP network connection to a given HOST/SERVICE. Treated
1690 exactly like a normal process when reading and writing. Only
1691 differences are in status display and process deletion. A network
1692 connection has no PID; you cannot signal it. All you can do is
1693 deactivate and close it via delete-process */
1695 DEFUN ("open-network-stream", Fopen_network_stream
, Sopen_network_stream
,
1697 "Open a TCP connection for a service to a host.\n\
1698 Returns a subprocess-object to represent the connection.\n\
1699 Input and output work as for subprocesses; `delete-process' closes it.\n\
1700 Args are NAME BUFFER HOST SERVICE.\n\
1701 NAME is name for process. It is modified if necessary to make it unique.\n\
1702 BUFFER is the buffer (or buffer-name) to associate with the process.\n\
1703 Process output goes at end of that buffer, unless you specify\n\
1704 an output stream or filter function to handle the output.\n\
1705 BUFFER may be also nil, meaning that this process is not associated\n\
1707 Third arg is name of the host to connect to, or its IP address.\n\
1708 Fourth arg SERVICE is name of the service desired, or an integer\n\
1709 specifying a port number to connect to.")
1710 (name
, buffer
, host
, service
)
1711 Lisp_Object name
, buffer
, host
, service
;
1715 struct sockaddr_in address
;
1716 struct servent
*svc_info
;
1717 struct hostent
*host_info_ptr
, host_info
;
1718 char *(addr_list
[2]);
1719 IN_ADDR numeric_addr
;
1723 struct hostent host_info_fixed
;
1724 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
1726 int count
= specpdl_ptr
- specpdl
;
1729 /* Ensure socket support is loaded if available. */
1730 init_winsock (TRUE
);
1733 GCPRO4 (name
, buffer
, host
, service
);
1734 CHECK_STRING (name
, 0);
1735 CHECK_STRING (host
, 0);
1736 if (INTEGERP (service
))
1737 port
= htons ((unsigned short) XINT (service
));
1740 CHECK_STRING (service
, 0);
1741 svc_info
= getservbyname (XSTRING (service
)->data
, "tcp");
1743 error ("Unknown service \"%s\"", XSTRING (service
)->data
);
1744 port
= svc_info
->s_port
;
1747 /* Slow down polling to every ten seconds.
1748 Some kernels have a bug which causes retrying connect to fail
1749 after a connect. Polling can interfere with gethostbyname too. */
1750 #ifdef POLL_FOR_INPUT
1751 bind_polling_period (10);
1762 host_info_ptr
= gethostbyname (XSTRING (host
)->data
);
1765 if (! (host_info_ptr
== 0 && h_errno
== TRY_AGAIN
))
1768 Fsleep_for (make_number (1), Qnil
);
1770 if (host_info_ptr
== 0)
1771 /* Attempt to interpret host as numeric inet address */
1773 numeric_addr
= inet_addr ((char *) XSTRING (host
)->data
);
1774 if (NUMERIC_ADDR_ERROR
)
1775 error ("Unknown host \"%s\"", XSTRING (host
)->data
);
1777 host_info_ptr
= &host_info
;
1778 host_info
.h_name
= 0;
1779 host_info
.h_aliases
= 0;
1780 host_info
.h_addrtype
= AF_INET
;
1782 /* Older machines have only one address slot called h_addr.
1783 Newer machines have h_addr_list, but #define h_addr to
1784 be its first element. */
1785 host_info
.h_addr_list
= &(addr_list
[0]);
1787 host_info
.h_addr
= (char*)(&numeric_addr
);
1789 /* numeric_addr isn't null-terminated; it has fixed length. */
1790 host_info
.h_length
= sizeof (numeric_addr
);
1793 bzero (&address
, sizeof address
);
1794 bcopy (host_info_ptr
->h_addr
, (char *) &address
.sin_addr
,
1795 host_info_ptr
->h_length
);
1796 address
.sin_family
= host_info_ptr
->h_addrtype
;
1797 address
.sin_port
= port
;
1799 s
= socket (host_info_ptr
->h_addrtype
, SOCK_STREAM
, 0);
1801 report_file_error ("error creating socket", Fcons (name
, Qnil
));
1803 /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
1804 when connect is interrupted. So let's not let it get interrupted.
1805 Note we do not turn off polling, because polling is only used
1806 when not interrupt_input, and thus not normally used on the systems
1807 which have this bug. On systems which use polling, there's no way
1808 to quit if polling is turned off. */
1809 if (interrupt_input
)
1817 if (connect (s
, (struct sockaddr
*) &address
, sizeof address
) == -1
1818 && errno
!= EISCONN
)
1826 if (errno
== EADDRINUSE
&& retry
< 20)
1828 /* A delay here is needed on some FreeBSD systems,
1829 and it is harmless, since this retrying takes time anyway
1830 and should be infrequent. */
1831 Fsleep_for (make_number (1), Qnil
);
1838 if (interrupt_input
)
1842 report_file_error ("connection failed",
1843 Fcons (host
, Fcons (name
, Qnil
)));
1848 #ifdef POLL_FOR_INPUT
1849 unbind_to (count
, Qnil
);
1852 if (interrupt_input
)
1856 s
= connect_server (0);
1858 report_file_error ("error creating socket", Fcons (name
, Qnil
));
1859 send_command (s
, C_PORT
, 0, "%s:%d", XSTRING (host
)->data
, ntohs (port
));
1860 send_command (s
, C_DUMB
, 1, 0);
1867 buffer
= Fget_buffer_create (buffer
);
1868 proc
= make_process (name
);
1870 chan_process
[inch
] = proc
;
1873 fcntl (inch
, F_SETFL
, O_NONBLOCK
);
1876 fcntl (inch
, F_SETFL
, O_NDELAY
);
1880 XPROCESS (proc
)->childp
= Fcons (host
, Fcons (service
, Qnil
));
1881 XPROCESS (proc
)->command_channel_p
= Qnil
;
1882 XPROCESS (proc
)->buffer
= buffer
;
1883 XPROCESS (proc
)->sentinel
= Qnil
;
1884 XPROCESS (proc
)->filter
= Qnil
;
1885 XPROCESS (proc
)->command
= Qnil
;
1886 XPROCESS (proc
)->pid
= Qnil
;
1887 XSETINT (XPROCESS (proc
)->infd
, inch
);
1888 XSETINT (XPROCESS (proc
)->outfd
, outch
);
1889 XPROCESS (proc
)->status
= Qrun
;
1890 FD_SET (inch
, &input_wait_mask
);
1891 FD_SET (inch
, &non_keyboard_wait_mask
);
1892 if (inch
> max_process_desc
)
1893 max_process_desc
= inch
;
1895 if (!NILP (buffer
) && NILP (XBUFFER (buffer
)->enable_multibyte_characters
)
1896 || NILP (buffer
) && NILP (buffer_defaults
.enable_multibyte_characters
))
1898 XPROCESS (proc
)->decode_coding_system
= Qnil
;
1899 XPROCESS (proc
)->encode_coding_system
= Qnil
;
1903 /* Setup coding systems for communicating with the network stream. */
1904 struct gcpro gcpro1
;
1905 /* Qt denotes that we have not yet called Ffind_coding_system. */
1906 Lisp_Object coding_systems
= Qt
;
1907 Lisp_Object args
[5], val
;
1909 if (NILP (val
= Vcoding_system_for_read
))
1911 args
[0] = Qopen_network_stream
, args
[1] = name
,
1912 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
1914 coding_systems
= Ffind_coding_system (5, args
);
1916 if (CONSP (coding_systems
))
1917 val
= XCONS (coding_systems
)->car
;
1918 else if (CONSP (Vdefault_process_coding_system
))
1919 val
= XCONS (Vdefault_process_coding_system
)->car
;
1921 XPROCESS (proc
)->decode_coding_system
= val
;
1923 if (NILP (val
= Vcoding_system_for_write
))
1925 if (EQ (coding_systems
, Qt
))
1927 args
[0] = Qopen_network_stream
, args
[1] = name
,
1928 args
[2] = buffer
, args
[3] = host
, args
[4] = service
;
1930 coding_systems
= Ffind_coding_system (5, args
);
1933 if (CONSP (coding_systems
))
1934 val
= XCONS (coding_systems
)->cdr
;
1935 else if (CONSP (Vdefault_process_coding_system
))
1936 val
= XCONS (Vdefault_process_coding_system
)->cdr
;
1938 XPROCESS (proc
)->encode_coding_system
= val
;
1941 if (!proc_decode_coding_system
[inch
])
1942 proc_decode_coding_system
[inch
]
1943 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
1944 setup_coding_system (XPROCESS (proc
)->decode_coding_system
,
1945 proc_decode_coding_system
[inch
]);
1946 if (!proc_encode_coding_system
[outch
])
1947 proc_encode_coding_system
[outch
]
1948 = (struct coding_system
*) xmalloc (sizeof (struct coding_system
));
1949 setup_coding_system (XPROCESS (proc
)->encode_coding_system
,
1950 proc_encode_coding_system
[outch
]);
1952 XPROCESS (proc
)->decoding_buf
= make_uninit_string (0);
1953 XPROCESS (proc
)->encoding_buf
= make_uninit_string (0);
1958 #endif /* HAVE_SOCKETS */
1960 deactivate_process (proc
)
1963 register int inchannel
, outchannel
;
1964 register struct Lisp_Process
*p
= XPROCESS (proc
);
1966 inchannel
= XINT (p
->infd
);
1967 outchannel
= XINT (p
->outfd
);
1971 /* Beware SIGCHLD hereabouts. */
1972 flush_pending_output (inchannel
);
1975 VMS_PROC_STUFF
*get_vms_process_pointer (), *vs
;
1976 sys$
dassgn (outchannel
);
1977 vs
= get_vms_process_pointer (p
->pid
);
1979 give_back_vms_process_stuff (vs
);
1983 if (outchannel
>= 0 && outchannel
!= inchannel
)
1987 XSETINT (p
->infd
, -1);
1988 XSETINT (p
->outfd
, -1);
1989 chan_process
[inchannel
] = Qnil
;
1990 FD_CLR (inchannel
, &input_wait_mask
);
1991 FD_CLR (inchannel
, &non_keyboard_wait_mask
);
1992 if (inchannel
== max_process_desc
)
1995 /* We just closed the highest-numbered process input descriptor,
1996 so recompute the highest-numbered one now. */
1997 max_process_desc
= 0;
1998 for (i
= 0; i
< MAXDESC
; i
++)
1999 if (!NILP (chan_process
[i
]))
2000 max_process_desc
= i
;
2005 /* Close all descriptors currently in use for communication
2006 with subprocess. This is used in a newly-forked subprocess
2007 to get rid of irrelevant descriptors. */
2009 close_process_descs ()
2013 for (i
= 0; i
< MAXDESC
; i
++)
2015 Lisp_Object process
;
2016 process
= chan_process
[i
];
2017 if (!NILP (process
))
2019 int in
= XINT (XPROCESS (process
)->infd
);
2020 int out
= XINT (XPROCESS (process
)->outfd
);
2023 if (out
>= 0 && in
!= out
)
2030 DEFUN ("accept-process-output", Faccept_process_output
, Saccept_process_output
,
2032 "Allow any pending output from subprocesses to be read by Emacs.\n\
2033 It is read into the process' buffers or given to their filter functions.\n\
2034 Non-nil arg PROCESS means do not return until some output has been received\n\
2036 Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of\n\
2037 seconds and microseconds to wait; return after that much time whether\n\
2038 or not there is input.\n\
2039 Return non-nil iff we received any output before the timeout expired.")
2040 (process
, timeout
, timeout_msecs
)
2041 register Lisp_Object process
, timeout
, timeout_msecs
;
2046 if (! NILP (timeout_msecs
))
2048 CHECK_NUMBER (timeout_msecs
, 2);
2049 useconds
= XINT (timeout_msecs
);
2050 if (!INTEGERP (timeout
))
2051 XSETINT (timeout
, 0);
2054 int carry
= useconds
/ 1000000;
2056 XSETINT (timeout
, XINT (timeout
) + carry
);
2057 useconds
-= carry
* 1000000;
2059 /* I think this clause is necessary because C doesn't
2060 guarantee a particular rounding direction for negative
2064 XSETINT (timeout
, XINT (timeout
) - 1);
2065 useconds
+= 1000000;
2072 if (! NILP (timeout
))
2074 CHECK_NUMBER (timeout
, 1);
2075 seconds
= XINT (timeout
);
2076 if (seconds
< 0 || (seconds
== 0 && useconds
== 0))
2088 XSETFASTINT (process
, 0);
2091 (wait_reading_process_input (seconds
, useconds
, process
, 0)
2095 /* This variable is different from waiting_for_input in keyboard.c.
2096 It is used to communicate to a lisp process-filter/sentinel (via the
2097 function Fwaiting_for_user_input_p below) whether emacs was waiting
2098 for user-input when that process-filter was called.
2099 waiting_for_input cannot be used as that is by definition 0 when
2100 lisp code is being evalled.
2101 This is also used in record_asynch_buffer_change.
2102 For that purpose, this must be 0
2103 when not inside wait_reading_process_input. */
2104 static int waiting_for_user_input_p
;
2106 /* This is here so breakpoints can be put on it. */
2108 wait_reading_process_input_1 ()
2112 /* Read and dispose of subprocess output while waiting for timeout to
2113 elapse and/or keyboard input to be available.
2116 timeout in seconds, or
2117 zero for no limit, or
2118 -1 means gobble data immediately available but don't wait for any.
2121 an additional duration to wait, measured in microseconds.
2122 If this is nonzero and time_limit is 0, then the timeout
2123 consists of MICROSECS only.
2125 READ_KBD is a lisp value:
2126 0 to ignore keyboard input, or
2127 1 to return when input is available, or
2128 -1 meaning caller will actually read the input, so don't throw to
2129 the quit handler, or
2130 a cons cell, meaning wait until its car is non-nil
2131 (and gobble terminal input into the buffer if any arrives), or
2132 a process object, meaning wait until something arrives from that
2133 process. The return value is true iff we read some input from
2136 DO_DISPLAY != 0 means redisplay should be done to show subprocess
2137 output that arrives.
2139 If READ_KBD is a pointer to a struct Lisp_Process, then the
2140 function returns true iff we received input from that process
2141 before the timeout elapsed.
2142 Otherwise, return true iff we received input from any process. */
2144 wait_reading_process_input (time_limit
, microsecs
, read_kbd
, do_display
)
2145 int time_limit
, microsecs
;
2146 Lisp_Object read_kbd
;
2149 register int channel
, nfds
, m
;
2150 static SELECT_TYPE Available
;
2153 EMACS_TIME timeout
, end_time
, garbage
;
2155 int wait_channel
= -1;
2156 struct Lisp_Process
*wait_proc
= 0;
2157 int got_some_input
= 0;
2158 Lisp_Object
*wait_for_cell
= 0;
2160 FD_ZERO (&Available
);
2162 /* If read_kbd is a process to watch, set wait_proc and wait_channel
2164 if (PROCESSP (read_kbd
))
2166 wait_proc
= XPROCESS (read_kbd
);
2167 wait_channel
= XINT (wait_proc
->infd
);
2168 XSETFASTINT (read_kbd
, 0);
2171 /* If waiting for non-nil in a cell, record where. */
2172 if (CONSP (read_kbd
))
2174 wait_for_cell
= &XCONS (read_kbd
)->car
;
2175 XSETFASTINT (read_kbd
, 0);
2178 waiting_for_user_input_p
= XINT (read_kbd
);
2180 /* Since we may need to wait several times,
2181 compute the absolute time to return at. */
2182 if (time_limit
|| microsecs
)
2184 EMACS_GET_TIME (end_time
);
2185 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
2186 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
2189 /* AlainF 5-Jul-1996
2190 HP-UX 10.10 seem to have problems with signals coming in
2191 Causes "poll: interrupted system call" messages when Emacs is run
2193 Turn off periodic alarms (in case they are in use) */
2199 int timeout_reduced_for_timers
= 0;
2201 /* If calling from keyboard input, do not quit
2202 since we want to return C-g as an input character.
2203 Otherwise, do pending quit if requested. */
2204 if (XINT (read_kbd
) >= 0)
2207 /* Exit now if the cell we're waiting for became non-nil. */
2208 if (wait_for_cell
&& ! NILP (*wait_for_cell
))
2211 /* Compute time from now till when time limit is up */
2212 /* Exit if already run out */
2213 if (time_limit
== -1)
2215 /* -1 specified for timeout means
2216 gobble output available now
2217 but don't wait at all. */
2219 EMACS_SET_SECS_USECS (timeout
, 0, 0);
2221 else if (time_limit
|| microsecs
)
2223 EMACS_GET_TIME (timeout
);
2224 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
2225 if (EMACS_TIME_NEG_P (timeout
))
2230 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
2233 /* Normally we run timers here.
2234 But not if wait_for_cell; in those cases,
2235 the wait is supposed to be short,
2236 and those callers cannot handle running arbitrary Lisp code here. */
2237 if (! wait_for_cell
)
2239 EMACS_TIME timer_delay
;
2243 old_timers_run
= timers_run
;
2244 timer_delay
= timer_check (1);
2245 if (timers_run
!= old_timers_run
&& do_display
)
2247 redisplay_preserve_echo_area ();
2248 /* We must retry, since a timer may have requeued itself
2249 and that could alter the time_delay. */
2253 /* If there is unread keyboard input, also return. */
2254 if (XINT (read_kbd
) != 0
2255 && requeued_events_pending_p ())
2258 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
2260 EMACS_TIME difference
;
2261 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
2262 if (EMACS_TIME_NEG_P (difference
))
2264 timeout
= timer_delay
;
2265 timeout_reduced_for_timers
= 1;
2268 /* If time_limit is -1, we are not going to wait at all. */
2269 else if (time_limit
!= -1)
2271 /* This is so a breakpoint can be put here. */
2272 wait_reading_process_input_1 ();
2276 /* Cause C-g and alarm signals to take immediate action,
2277 and cause input available signals to zero out timeout.
2279 It is important that we do this before checking for process
2280 activity. If we get a SIGCHLD after the explicit checks for
2281 process activity, timeout is the only way we will know. */
2282 if (XINT (read_kbd
) < 0)
2283 set_waiting_for_input (&timeout
);
2285 /* If status of something has changed, and no input is
2286 available, notify the user of the change right away. After
2287 this explicit check, we'll let the SIGCHLD handler zap
2288 timeout to get our attention. */
2289 if (update_tick
!= process_tick
&& do_display
)
2291 Atemp
= input_wait_mask
;
2292 EMACS_SET_SECS_USECS (timeout
, 0, 0);
2293 if ((select (max (max_process_desc
, max_keyboard_desc
) + 1,
2294 &Atemp
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
2298 /* It's okay for us to do this and then continue with
2299 the loop, since timeout has already been zeroed out. */
2300 clear_waiting_for_input ();
2305 /* Don't wait for output from a non-running process. */
2306 if (wait_proc
!= 0 && !NILP (wait_proc
->raw_status_low
))
2307 update_status (wait_proc
);
2309 && ! EQ (wait_proc
->status
, Qrun
))
2311 int nread
, total_nread
;
2313 clear_waiting_for_input ();
2314 XSETPROCESS (proc
, wait_proc
);
2316 /* Read data from the process, until we exhaust it. */
2317 while (XINT (wait_proc
->infd
) >= 0
2319 = read_process_output (proc
, XINT (wait_proc
->infd
))))
2320 total_nread
+= nread
;
2321 if (total_nread
> 0 && do_display
)
2322 redisplay_preserve_echo_area ();
2327 /* Wait till there is something to do */
2330 Available
= non_process_wait_mask
;
2331 else if (! XINT (read_kbd
))
2332 Available
= non_keyboard_wait_mask
;
2334 Available
= input_wait_mask
;
2336 /* If frame size has changed or the window is newly mapped,
2337 redisplay now, before we start to wait. There is a race
2338 condition here; if a SIGIO arrives between now and the select
2339 and indicates that a frame is trashed, the select may block
2340 displaying a trashed screen. */
2341 if (frame_garbaged
&& do_display
)
2343 clear_waiting_for_input ();
2344 redisplay_preserve_echo_area ();
2345 if (XINT (read_kbd
) < 0)
2346 set_waiting_for_input (&timeout
);
2349 if (XINT (read_kbd
) && detect_input_pending ())
2352 FD_ZERO (&Available
);
2355 nfds
= select (max (max_process_desc
, max_keyboard_desc
) + 1,
2356 &Available
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
2361 /* Make C-g and alarm signals set flags again */
2362 clear_waiting_for_input ();
2364 /* If we woke up due to SIGWINCH, actually change size now. */
2365 do_pending_window_change ();
2367 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
2368 /* We wanted the full specified time, so return now. */
2372 if (xerrno
== EINTR
)
2373 FD_ZERO (&Available
);
2375 /* Ultrix select seems to return ENOMEM when it is
2376 interrupted. Treat it just like EINTR. Bleah. Note
2377 that we want to test for the "ultrix" CPP symbol, not
2378 "__ultrix__"; the latter is only defined under GCC, but
2379 not by DEC's bundled CC. -JimB */
2380 else if (xerrno
== ENOMEM
)
2381 FD_ZERO (&Available
);
2384 /* This happens for no known reason on ALLIANT.
2385 I am guessing that this is the right response. -- RMS. */
2386 else if (xerrno
== EFAULT
)
2387 FD_ZERO (&Available
);
2389 else if (xerrno
== EBADF
)
2392 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
2393 the child's closure of the pts gives the parent a SIGHUP, and
2394 the ptc file descriptor is automatically closed,
2395 yielding EBADF here or at select() call above.
2396 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
2397 in m/ibmrt-aix.h), and here we just ignore the select error.
2398 Cleanup occurs c/o status_notify after SIGCLD. */
2399 FD_ZERO (&Available
); /* Cannot depend on values returned */
2405 error ("select error: %s", strerror (xerrno
));
2407 #if defined(sun) && !defined(USG5_4)
2408 else if (nfds
> 0 && keyboard_bit_set (&Available
)
2410 /* System sometimes fails to deliver SIGIO.
2412 David J. Mackenzie says that Emacs doesn't compile under
2413 Solaris if this code is enabled, thus the USG5_4 in the CPP
2414 conditional. "I haven't noticed any ill effects so far.
2415 If you find a Solaris expert somewhere, they might know
2417 kill (getpid (), SIGIO
);
2420 #if 0 /* When polling is used, interrupt_input is 0,
2421 so get_input_pending should read the input.
2422 So this should not be needed. */
2423 /* If we are using polling for input,
2424 and we see input available, make it get read now.
2425 Otherwise it might not actually get read for a second.
2426 And on hpux, since we turn off polling in wait_reading_process_input,
2427 it might never get read at all if we don't spend much time
2428 outside of wait_reading_process_input. */
2429 if (XINT (read_kbd
) && interrupt_input
2430 && keyboard_bit_set (&Available
)
2431 && input_polling_used ())
2432 kill (getpid (), SIGALRM
);
2435 /* Check for keyboard input */
2436 /* If there is any, return immediately
2437 to give it higher priority than subprocesses */
2439 if ((XINT (read_kbd
) != 0)
2440 && detect_input_pending_run_timers (do_display
))
2442 swallow_events (do_display
);
2443 if (detect_input_pending_run_timers (do_display
))
2447 /* If there is unread keyboard input, also return. */
2448 if (XINT (read_kbd
) != 0
2449 && requeued_events_pending_p ())
2452 /* If wait_for_cell. check for keyboard input
2453 but don't run any timers.
2454 The reason for this is so that X events will be processed.
2455 Otherwise they may have to wait until polling takes place.
2456 That would causes delays in pasting selections, for example. */
2458 && detect_input_pending ())
2460 swallow_events (do_display
);
2461 #if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
2462 if (detect_input_pending ())
2467 /* Exit now if the cell we're waiting for became non-nil. */
2468 if (wait_for_cell
&& ! NILP (*wait_for_cell
))
2472 /* If we think we have keyboard input waiting, but didn't get SIGIO,
2473 go read it. This can happen with X on BSD after logging out.
2474 In that case, there really is no input and no SIGIO,
2475 but select says there is input. */
2477 if (XINT (read_kbd
) && interrupt_input
2478 && keyboard_bit_set (&Available
))
2479 kill (getpid (), SIGIO
);
2483 got_some_input
|= nfds
> 0;
2485 /* If checking input just got us a size-change event from X,
2486 obey it now if we should. */
2487 if (XINT (read_kbd
) || wait_for_cell
)
2488 do_pending_window_change ();
2490 /* Check for data from a process. */
2491 /* Really FIRST_PROC_DESC should be 0 on Unix,
2492 but this is safer in the short run. */
2493 for (channel
= 0; channel
<= max_process_desc
; channel
++)
2495 if (FD_ISSET (channel
, &Available
)
2496 && FD_ISSET (channel
, &non_keyboard_wait_mask
))
2500 /* If waiting for this channel, arrange to return as
2501 soon as no more input to be processed. No more
2503 if (wait_channel
== channel
)
2509 proc
= chan_process
[channel
];
2513 /* Read data from the process, starting with our
2514 buffered-ahead character if we have one. */
2516 nread
= read_process_output (proc
, channel
);
2519 /* Since read_process_output can run a filter,
2520 which can call accept-process-output,
2521 don't try to read from any other processes
2522 before doing the select again. */
2523 FD_ZERO (&Available
);
2526 redisplay_preserve_echo_area ();
2529 else if (nread
== -1 && errno
== EWOULDBLOCK
)
2532 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
2533 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
2535 else if (nread
== -1 && errno
== EAGAIN
)
2539 else if (nread
== -1 && errno
== EAGAIN
)
2541 /* Note that we cannot distinguish between no input
2542 available now and a closed pipe.
2543 With luck, a closed pipe will be accompanied by
2544 subprocess termination and SIGCHLD. */
2545 else if (nread
== 0 && !NETCONN_P (proc
))
2547 #endif /* O_NDELAY */
2548 #endif /* O_NONBLOCK */
2550 /* On some OSs with ptys, when the process on one end of
2551 a pty exits, the other end gets an error reading with
2552 errno = EIO instead of getting an EOF (0 bytes read).
2553 Therefore, if we get an error reading and errno =
2554 EIO, just continue, because the child process has
2555 exited and should clean itself up soon (e.g. when we
2557 else if (nread
== -1 && errno
== EIO
)
2559 #endif /* HAVE_PTYS */
2560 /* If we can detect process termination, don't consider the process
2561 gone just because its pipe is closed. */
2563 else if (nread
== 0 && !NETCONN_P (proc
))
2568 /* Preserve status of processes already terminated. */
2569 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
2570 deactivate_process (proc
);
2571 if (!NILP (XPROCESS (proc
)->raw_status_low
))
2572 update_status (XPROCESS (proc
));
2573 if (EQ (XPROCESS (proc
)->status
, Qrun
))
2574 XPROCESS (proc
)->status
2575 = Fcons (Qexit
, Fcons (make_number (256), Qnil
));
2578 } /* end for each file descriptor */
2579 } /* end while exit conditions not met */
2581 waiting_for_user_input_p
= 0;
2583 /* If calling from keyboard input, do not quit
2584 since we want to return C-g as an input character.
2585 Otherwise, do pending quit if requested. */
2586 if (XINT (read_kbd
) >= 0)
2588 /* Prevent input_pending from remaining set if we quit. */
2589 clear_input_pending ();
2593 /* AlainF 5-Jul-1996
2594 HP-UX 10.10 seems to have problems with signals coming in
2595 Causes "poll: interrupted system call" messages when Emacs is run
2597 Turn periodic alarms back on */
2601 return got_some_input
;
2604 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
2607 read_process_output_call (fun_and_args
)
2608 Lisp_Object fun_and_args
;
2610 return apply1 (XCONS (fun_and_args
)->car
, XCONS (fun_and_args
)->cdr
);
2614 read_process_output_error_handler (error
)
2617 cmd_error_internal (error
, "error in process filter: ");
2619 update_echo_area ();
2620 Fsleep_for (make_number (2), Qnil
);
2623 /* Read pending output from the process channel,
2624 starting with our buffered-ahead character if we have one.
2625 Yield number of decoded characters read.
2627 This function reads at most 1024 characters.
2628 If you want to read all available subprocess output,
2629 you must call it repeatedly until it returns zero.
2631 The characters read are decoded according to PROC's coding-system
2634 read_process_output (proc
, channel
)
2636 register int channel
;
2638 register int nchars
;
2641 int chars_allocated
= 0; /* If 1, `chars' should be freed later. */
2645 register Lisp_Object outstream
;
2646 register struct buffer
*old
= current_buffer
;
2647 register struct Lisp_Process
*p
= XPROCESS (proc
);
2648 register int opoint
;
2649 struct coding_system
*coding
= proc_decode_coding_system
[channel
];
2650 int chars_in_decoding_buf
= 0; /* If 1, `chars' points
2651 XSTRING (p->decoding_buf)->data. */
2654 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
2656 vs
= get_vms_process_pointer (p
->pid
);
2660 return(0); /* Really weird if it does this */
2661 if (!(vs
->iosb
[0] & 1))
2662 return -1; /* I/O error */
2665 error ("Could not get VMS process pointer");
2666 chars
= vs
->inputBuffer
;
2667 nchars
= clean_vms_buffer (chars
, vs
->iosb
[1]);
2670 start_vms_process_read (vs
); /* Crank up the next read on the process */
2671 return 1; /* Nothing worth printing, say we got 1 */
2673 if (coding
->carryover_size
)
2675 /* The data carried over in the previous decoding should be
2676 prepended to the new data read to decode all together. */
2677 char *buf
= (char *) xmalloc (nchars
+ coding
->carryover_size
);
2679 bcopy (coding
->carryover
, buf
, coding
->carryover_size
);
2680 bcopy (chars
, buf
+ coding
->carryover_size
, nchars
);
2682 chars_allocated
= 1;
2686 if (coding
->carryover_size
)
2687 /* The data carried over in the previous decoding should be
2688 prepended to the new data read to decode all together. */
2689 bcopy (coding
->carryover
, buf
, coding
->carryover_size
);
2691 if (proc_buffered_char
[channel
] < 0)
2692 nchars
= read (channel
, buf
+ coding
->carryover_size
,
2693 (sizeof buf
) - coding
->carryover_size
);
2696 buf
[coding
->carryover_size
] = proc_buffered_char
[channel
];
2697 proc_buffered_char
[channel
] = -1;
2698 nchars
= read (channel
, buf
+ coding
->carryover_size
+ 1,
2699 (sizeof buf
) - coding
->carryover_size
- 1);
2703 nchars
= nchars
+ 1;
2706 #endif /* not VMS */
2708 /* At this point, NCHARS holds number of characters just received
2709 (including the one in proc_buffered_char[channel]). */
2710 if (nchars
<= 0) return nchars
;
2712 /* Now set NCHARS how many bytes we must decode. */
2713 nchars
+= coding
->carryover_size
;
2715 if (CODING_REQUIRE_CONVERSION (coding
))
2717 int require
= decoding_buffer_size (coding
, nchars
);
2718 int consumed
, produced
;
2720 if (XSTRING (p
->decoding_buf
)->size
< require
)
2721 p
->decoding_buf
= make_uninit_string (require
);
2722 produced
= decode_coding (coding
, chars
, XSTRING (p
->decoding_buf
)->data
,
2723 nchars
, XSTRING (p
->decoding_buf
)->size
,
2726 /* New coding-system might be found by `decode_coding'. */
2727 if (!EQ (p
->decode_coding_system
, coding
->symbol
))
2729 p
->decode_coding_system
= coding
->symbol
;
2730 setup_coding_system (coding
->symbol
,
2731 proc_decode_coding_system
[channel
]);
2732 /* If coding-system for encoding is not yet decided, we set it
2733 as the same as coding-system for decoding. */
2734 if (NILP (p
->encode_coding_system
))
2736 p
->encode_coding_system
= coding
->symbol
;
2737 setup_coding_system (coding
->symbol
,
2738 proc_encode_coding_system
[channel
]);
2742 /* Now we don't need the contents of `chars'. */
2743 if (chars_allocated
)
2748 chars
= XSTRING (p
->decoding_buf
)->data
;
2750 chars_in_decoding_buf
= 1;
2753 else if (chars_allocated
)
2755 /* Although we don't have to decode the received data, we must
2756 move it to an area which we don't have to free. */
2757 if (! STRINGP (p
->decoding_buf
)
2758 || XSTRING (p
->decoding_buf
)->size
< nchars
)
2759 p
->decoding_buf
= make_uninit_string (nchars
);
2760 bcopy (chars
, XSTRING (p
->decoding_buf
)->data
, nchars
);
2762 chars
= XSTRING (p
->decoding_buf
)->data
;
2763 chars_in_decoding_buf
= 1;
2767 outstream
= p
->filter
;
2768 if (!NILP (outstream
))
2770 /* We inhibit quit here instead of just catching it so that
2771 hitting ^G when a filter happens to be running won't screw
2773 int count
= specpdl_ptr
- specpdl
;
2774 Lisp_Object odeactivate
;
2775 Lisp_Object obuffer
, okeymap
;
2776 int outer_running_asynch_code
= running_asynch_code
;
2778 /* No need to gcpro these, because all we do with them later
2779 is test them for EQness, and none of them should be a string. */
2780 odeactivate
= Vdeactivate_mark
;
2781 XSETBUFFER (obuffer
, current_buffer
);
2782 okeymap
= current_buffer
->keymap
;
2784 specbind (Qinhibit_quit
, Qt
);
2785 specbind (Qlast_nonmenu_event
, Qt
);
2787 /* In case we get recursively called,
2788 and we already saved the match data nonrecursively,
2789 save the same match data in safely recursive fashion. */
2790 if (outer_running_asynch_code
)
2793 /* Don't clobber the CURRENT match data, either! */
2794 tem
= Fmatch_data (Qnil
, Qnil
);
2795 restore_match_data ();
2796 record_unwind_protect (Fstore_match_data
, Fmatch_data (Qnil
, Qnil
));
2797 Fstore_match_data (tem
);
2800 /* For speed, if a search happens within this code,
2801 save the match data in a special nonrecursive fashion. */
2802 running_asynch_code
= 1;
2804 /* Read and dispose of the process output. */
2805 internal_condition_case_1 (read_process_output_call
,
2808 Fcons (make_string (chars
,
2811 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
2812 read_process_output_error_handler
);
2814 /* If we saved the match data nonrecursively, restore it now. */
2815 restore_match_data ();
2816 running_asynch_code
= outer_running_asynch_code
;
2818 /* Handling the process output should not deactivate the mark. */
2819 Vdeactivate_mark
= odeactivate
;
2821 #if 0 /* Call record_asynch_buffer_change unconditionally,
2822 because we might have changed minor modes or other things
2823 that affect key bindings. */
2824 if (! EQ (Fcurrent_buffer (), obuffer
)
2825 || ! EQ (current_buffer
->keymap
, okeymap
))
2827 /* But do it only if the caller is actually going to read events.
2828 Otherwise there's no need to make him wake up, and it could
2829 cause trouble (for example it would make Fsit_for return). */
2830 if (waiting_for_user_input_p
== -1)
2831 record_asynch_buffer_change ();
2834 start_vms_process_read (vs
);
2836 unbind_to (count
, Qnil
);
2840 /* If no filter, write into buffer if it isn't dead. */
2841 if (!NILP (p
->buffer
) && !NILP (XBUFFER (p
->buffer
)->name
))
2843 Lisp_Object old_read_only
;
2844 int old_begv
, old_zv
;
2845 Lisp_Object odeactivate
;
2848 odeactivate
= Vdeactivate_mark
;
2850 Fset_buffer (p
->buffer
);
2852 old_read_only
= current_buffer
->read_only
;
2856 current_buffer
->read_only
= Qnil
;
2858 /* Insert new output into buffer
2859 at the current end-of-output marker,
2860 thus preserving logical ordering of input and output. */
2861 if (XMARKER (p
->mark
)->buffer
)
2862 SET_PT (clip_to_bounds (BEGV
, marker_position (p
->mark
), ZV
));
2867 /* If the output marker is outside of the visible region, save
2868 the restriction and widen. */
2869 if (! (BEGV
<= PT
&& PT
<= ZV
))
2872 /* Insert before markers in case we are inserting where
2873 the buffer's mark is, and the user's next command is Meta-y. */
2874 if (chars_in_decoding_buf
)
2875 insert_from_string_before_markers (p
->decoding_buf
, 0, nchars
, 0);
2877 insert_before_markers (chars
, nchars
);
2878 Fset_marker (p
->mark
, make_number (PT
), p
->buffer
);
2880 update_mode_lines
++;
2882 /* Make sure opoint and the old restrictions
2883 float ahead of any new text just as point would. */
2884 if (opoint
>= before
)
2885 opoint
+= PT
- before
;
2886 if (old_begv
> before
)
2887 old_begv
+= PT
- before
;
2888 if (old_zv
>= before
)
2889 old_zv
+= PT
- before
;
2891 /* If the restriction isn't what it should be, set it. */
2892 if (old_begv
!= BEGV
|| old_zv
!= ZV
)
2893 Fnarrow_to_region (make_number (old_begv
), make_number (old_zv
));
2895 /* Handling the process output should not deactivate the mark. */
2896 Vdeactivate_mark
= odeactivate
;
2898 current_buffer
->read_only
= old_read_only
;
2900 set_buffer_internal (old
);
2903 start_vms_process_read (vs
);
2908 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p
, Swaiting_for_user_input_p
,
2910 "Returns non-nil if emacs is waiting for input from the user.\n\
2911 This is intended for use by asynchronous process output filters and sentinels.")
2914 return (waiting_for_user_input_p
? Qt
: Qnil
);
2917 /* Sending data to subprocess */
2919 jmp_buf send_process_frame
;
2922 send_process_trap ()
2928 longjmp (send_process_frame
, 1);
2931 /* Send some data to process PROC.
2932 BUF is the beginning of the data; LEN is the number of characters.
2933 OBJECT is the Lisp object that the data comes from.
2935 The data is encoded by PROC's coding-system for encoding before it
2936 is sent. But if the data ends at the middle of multi-byte
2937 representation, that incomplete sequence of bytes are sent without
2938 being encoded. Should we store them in a buffer to prepend them to
2939 the data send later? */
2941 send_process (proc
, buf
, len
, object
)
2942 volatile Lisp_Object proc
;
2947 /* Use volatile to protect variables from being clobbered by longjmp. */
2949 volatile unsigned char *procname
= XSTRING (XPROCESS (proc
)->name
)->data
;
2950 struct coding_system
*coding
;
2951 struct gcpro gcpro1
;
2956 struct Lisp_Process
*p
= XPROCESS (proc
);
2957 VMS_PROC_STUFF
*vs
, *get_vms_process_pointer();
2960 if (! NILP (XPROCESS (proc
)->raw_status_low
))
2961 update_status (XPROCESS (proc
));
2962 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
2963 error ("Process %s not running", procname
);
2964 if (XINT (XPROCESS (proc
)->outfd
) < 0)
2965 error ("Output file descriptor of %s is closed", procname
);
2967 coding
= proc_encode_coding_system
[XINT (XPROCESS (proc
)->outfd
)];
2968 if (CODING_REQUIRE_CONVERSION (coding
))
2970 int require
= encoding_buffer_size (coding
, len
);
2972 char *temp_buf
= NULL
;
2974 /* Remember the offset of data because a string or a buffer may
2975 be relocated. Setting OFFSET to -1 means we don't have to
2977 offset
= (BUFFERP (object
)
2978 ? BUF_PTR_CHAR_POS (XBUFFER (object
), (unsigned char *) buf
)
2980 ? offset
= buf
- (char *) XSTRING (object
)->data
2983 if (coding
->carryover_size
> 0)
2985 temp_buf
= (char *) xmalloc (len
+ coding
->carryover_size
);
2989 if (BUFFERP (object
))
2990 buf
= (char *) BUF_CHAR_ADDRESS (XBUFFER (object
), offset
);
2991 else if (STRINGP (object
))
2992 buf
= offset
+ (char *) XSTRING (object
)->data
;
2993 /* Now we don't have to care relocation. */
2996 bcopy (coding
->carryover
, temp_buf
, coding
->carryover_size
);
2997 bcopy (buf
, temp_buf
+ coding
->carryover_size
, len
);
3001 if (XSTRING (XPROCESS (proc
)->encoding_buf
)->size
< require
)
3003 XPROCESS (proc
)->encoding_buf
= make_uninit_string (require
);
3007 if (BUFFERP (object
))
3008 buf
= (char *) BUF_CHAR_ADDRESS (XBUFFER (object
), offset
);
3009 else if (STRINGP (object
))
3010 buf
= offset
+ (char *) XSTRING (object
)->data
;
3013 object
= XPROCESS (proc
)->encoding_buf
;
3014 len
= encode_coding (coding
, buf
, XSTRING (object
)->data
,
3015 len
, XSTRING (object
)->size
, &dummy
);
3016 buf
= XSTRING (object
)->data
;
3022 vs
= get_vms_process_pointer (p
->pid
);
3024 error ("Could not find this process: %x", p
->pid
);
3025 else if (write_to_vms_process (vs
, buf
, len
))
3029 if (pty_max_bytes
== 0)
3031 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
3032 pty_max_bytes
= fpathconf (XFASTINT (XPROCESS (proc
)->outfd
),
3034 if (pty_max_bytes
< 0)
3035 pty_max_bytes
= 250;
3037 pty_max_bytes
= 250;
3039 /* Deduct one, to leave space for the eof. */
3043 if (!setjmp (send_process_frame
))
3047 SIGTYPE (*old_sigpipe
)();
3050 /* Decide how much data we can send in one batch.
3051 Long lines need to be split into multiple batches. */
3052 if (!NILP (XPROCESS (proc
)->pty_flag
))
3054 /* Starting this at zero is always correct when not the first iteration
3055 because the previous iteration ended by sending C-d.
3056 It may not be correct for the first iteration
3057 if a partial line was sent in a separate send_process call.
3058 If that proves worth handling, we need to save linepos
3059 in the process object. */
3062 char *end
= buf
+ len
;
3064 /* Scan through this text for a line that is too long. */
3065 while (ptr
!= end
&& linepos
< pty_max_bytes
)
3073 /* If we found one, break the line there
3074 and put in a C-d to force the buffer through. */
3078 /* Send this batch, using one or more write calls. */
3081 old_sigpipe
= (SIGTYPE (*) ()) signal (SIGPIPE
, send_process_trap
);
3082 rv
= write (XINT (XPROCESS (proc
)->outfd
), buf
, this);
3083 signal (SIGPIPE
, old_sigpipe
);
3089 || errno
== EWOULDBLOCK
3095 /* Buffer is full. Wait, accepting input;
3096 that may allow the program
3097 to finish doing output and read more. */
3102 /* Running filters might relocate buffers or strings.
3103 Arrange to relocate BUF. */
3104 if (BUFFERP (object
))
3105 offset
= BUF_PTR_CHAR_POS (XBUFFER (object
),
3106 (unsigned char *) buf
);
3107 else if (STRINGP (object
))
3108 offset
= buf
- (char *) XSTRING (object
)->data
;
3110 XSETFASTINT (zero
, 0);
3111 #ifdef EMACS_HAS_USECS
3112 wait_reading_process_input (0, 20000, zero
, 0);
3114 wait_reading_process_input (1, 0, zero
, 0);
3117 if (BUFFERP (object
))
3118 buf
= (char *) BUF_CHAR_ADDRESS (XBUFFER (object
), offset
);
3119 else if (STRINGP (object
))
3120 buf
= offset
+ (char *) XSTRING (object
)->data
;
3125 /* This is a real error. */
3126 report_file_error ("writing to process", Fcons (proc
, Qnil
));
3133 /* If we sent just part of the string, put in an EOF
3134 to force it through, before we send the rest. */
3136 Fprocess_send_eof (proc
);
3141 XPROCESS (proc
)->raw_status_low
= Qnil
;
3142 XPROCESS (proc
)->raw_status_high
= Qnil
;
3143 XPROCESS (proc
)->status
= Fcons (Qexit
, Fcons (make_number (256), Qnil
));
3144 XSETINT (XPROCESS (proc
)->tick
, ++process_tick
);
3145 deactivate_process (proc
);
3147 error ("Error writing to process %s; closed it", procname
);
3149 error ("SIGPIPE raised on process %s; closed it", procname
);
3156 DEFUN ("process-send-region", Fprocess_send_region
, Sprocess_send_region
,
3158 "Send current contents of region as input to PROCESS.\n\
3159 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
3160 nil, indicating the current buffer's process.\n\
3161 Called from program, takes three arguments, PROCESS, START and END.\n\
3162 If the region is more than 500 characters long,\n\
3163 it is sent in several bunches. This may happen even for shorter regions.\n\
3164 Output from processes can arrive in between bunches.")
3165 (process
, start
, end
)
3166 Lisp_Object process
, start
, end
;
3171 proc
= get_process (process
);
3172 validate_region (&start
, &end
);
3174 if (XINT (start
) < GPT
&& XINT (end
) > GPT
)
3177 start1
= XINT (start
);
3178 send_process (proc
, POS_ADDR (start1
), XINT (end
) - XINT (start
),
3179 Fcurrent_buffer ());
3184 DEFUN ("process-send-string", Fprocess_send_string
, Sprocess_send_string
,
3186 "Send PROCESS the contents of STRING as input.\n\
3187 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
3188 nil, indicating the current buffer's process.\n\
3189 If STRING is more than 500 characters long,\n\
3190 it is sent in several bunches. This may happen even for shorter strings.\n\
3191 Output from processes can arrive in between bunches.")
3193 Lisp_Object process
, string
;
3196 CHECK_STRING (string
, 1);
3197 proc
= get_process (process
);
3198 send_process (proc
, XSTRING (string
)->data
, XSTRING (string
)->size
, string
);
3202 /* send a signal number SIGNO to PROCESS.
3203 CURRENT_GROUP means send to the process group that currently owns
3204 the terminal being used to communicate with PROCESS.
3205 This is used for various commands in shell mode.
3206 If NOMSG is zero, insert signal-announcements into process's buffers
3209 If we can, we try to signal PROCESS by sending control characters
3210 down the pty. This allows us to signal inferiors who have changed
3211 their uid, for which killpg would return an EPERM error. */
3214 process_send_signal (process
, signo
, current_group
, nomsg
)
3215 Lisp_Object process
;
3217 Lisp_Object current_group
;
3221 register struct Lisp_Process
*p
;
3225 proc
= get_process (process
);
3226 p
= XPROCESS (proc
);
3228 if (!EQ (p
->childp
, Qt
))
3229 error ("Process %s is not a subprocess",
3230 XSTRING (p
->name
)->data
);
3231 if (XINT (p
->infd
) < 0)
3232 error ("Process %s is not active",
3233 XSTRING (p
->name
)->data
);
3235 if (NILP (p
->pty_flag
))
3236 current_group
= Qnil
;
3238 /* If we are using pgrps, get a pgrp number and make it negative. */
3239 if (!NILP (current_group
))
3241 #ifdef SIGNALS_VIA_CHARACTERS
3242 /* If possible, send signals to the entire pgrp
3243 by sending an input character to it. */
3245 /* TERMIOS is the latest and bestest, and seems most likely to
3246 work. If the system has it, use it. */
3253 tcgetattr (XINT (p
->infd
), &t
);
3254 send_process (proc
, &t
.c_cc
[VINTR
], 1, Qnil
);
3258 tcgetattr (XINT (p
->infd
), &t
);
3259 send_process (proc
, &t
.c_cc
[VQUIT
], 1, Qnil
);
3263 tcgetattr (XINT (p
->infd
), &t
);
3264 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
3265 send_process (proc
, &t
.c_cc
[VSWTCH
], 1, Qnil
);
3267 send_process (proc
, &t
.c_cc
[VSUSP
], 1, Qnil
);
3272 #else /* ! HAVE_TERMIOS */
3274 /* On Berkeley descendants, the following IOCTL's retrieve the
3275 current control characters. */
3276 #if defined (TIOCGLTC) && defined (TIOCGETC)
3284 ioctl (XINT (p
->infd
), TIOCGETC
, &c
);
3285 send_process (proc
, &c
.t_intrc
, 1, Qnil
);
3288 ioctl (XINT (p
->infd
), TIOCGETC
, &c
);
3289 send_process (proc
, &c
.t_quitc
, 1, Qnil
);
3293 ioctl (XINT (p
->infd
), TIOCGLTC
, &lc
);
3294 send_process (proc
, &lc
.t_suspc
, 1, Qnil
);
3296 #endif /* ! defined (SIGTSTP) */
3299 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
3301 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
3308 ioctl (XINT (p
->infd
), TCGETA
, &t
);
3309 send_process (proc
, &t
.c_cc
[VINTR
], 1, Qnil
);
3312 ioctl (XINT (p
->infd
), TCGETA
, &t
);
3313 send_process (proc
, &t
.c_cc
[VQUIT
], 1, Qnil
);
3317 ioctl (XINT (p
->infd
), TCGETA
, &t
);
3318 send_process (proc
, &t
.c_cc
[VSWTCH
], 1, Qnil
);
3320 #endif /* ! defined (SIGTSTP) */
3322 #else /* ! defined (TCGETA) */
3323 Your configuration files are messed up
.
3324 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
3325 you'd better be using one of the alternatives above! */
3326 #endif /* ! defined (TCGETA) */
3327 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
3328 #endif /* ! defined HAVE_TERMIOS */
3329 #endif /* ! defined (SIGNALS_VIA_CHARACTERS) */
3332 /* Get the pgrp using the tty itself, if we have that.
3333 Otherwise, use the pty to get the pgrp.
3334 On pfa systems, saka@pfu.fujitsu.co.JP writes:
3335 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
3336 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
3337 His patch indicates that if TIOCGPGRP returns an error, then
3338 we should just assume that p->pid is also the process group id. */
3342 if (!NILP (p
->subtty
))
3343 err
= ioctl (XFASTINT (p
->subtty
), TIOCGPGRP
, &gid
);
3345 err
= ioctl (XINT (p
->infd
), TIOCGPGRP
, &gid
);
3349 gid
= - XFASTINT (p
->pid
);
3350 #endif /* ! defined (pfa) */
3356 #else /* ! defined (TIOCGPGRP ) */
3357 /* Can't select pgrps on this system, so we know that
3358 the child itself heads the pgrp. */
3359 gid
= - XFASTINT (p
->pid
);
3360 #endif /* ! defined (TIOCGPGRP ) */
3363 gid
= - XFASTINT (p
->pid
);
3369 p
->raw_status_low
= Qnil
;
3370 p
->raw_status_high
= Qnil
;
3372 XSETINT (p
->tick
, ++process_tick
);
3376 #endif /* ! defined (SIGCONT) */
3379 send_process (proc
, "\003", 1, Qnil
); /* ^C */
3384 send_process (proc
, "\031", 1, Qnil
); /* ^Y */
3389 sys$
forcex (&(XFASTINT (p
->pid
)), 0, 1);
3392 flush_pending_output (XINT (p
->infd
));
3396 /* If we don't have process groups, send the signal to the immediate
3397 subprocess. That isn't really right, but it's better than any
3398 obvious alternative. */
3401 kill (XFASTINT (p
->pid
), signo
);
3405 /* gid may be a pid, or minus a pgrp's number */
3407 if (!NILP (current_group
))
3408 ioctl (XINT (p
->infd
), TIOCSIGSEND
, signo
);
3411 gid
= - XFASTINT (p
->pid
);
3414 #else /* ! defined (TIOCSIGSEND) */
3415 EMACS_KILLPG (-gid
, signo
);
3416 #endif /* ! defined (TIOCSIGSEND) */
3419 DEFUN ("interrupt-process", Finterrupt_process
, Sinterrupt_process
, 0, 2, 0,
3420 "Interrupt process PROCESS. May be process or name of one.\n\
3421 PROCESS may be a process, a buffer, or the name of a process or buffer.\n\
3422 nil or no arg means current buffer's process.\n\
3423 Second arg CURRENT-GROUP non-nil means send signal to\n\
3424 the current process-group of the process's controlling terminal\n\
3425 rather than to the process's own process group.\n\
3426 If the process is a shell, this means interrupt current subjob\n\
3427 rather than the shell.")
3428 (process
, current_group
)
3429 Lisp_Object process
, current_group
;
3431 process_send_signal (process
, SIGINT
, current_group
, 0);
3435 DEFUN ("kill-process", Fkill_process
, Skill_process
, 0, 2, 0,
3436 "Kill process PROCESS. May be process or name of one.\n\
3437 See function `interrupt-process' for more details on usage.")
3438 (process
, current_group
)
3439 Lisp_Object process
, current_group
;
3441 process_send_signal (process
, SIGKILL
, current_group
, 0);
3445 DEFUN ("quit-process", Fquit_process
, Squit_process
, 0, 2, 0,
3446 "Send QUIT signal to process PROCESS. May be process or name of one.\n\
3447 See function `interrupt-process' for more details on usage.")
3448 (process
, current_group
)
3449 Lisp_Object process
, current_group
;
3451 process_send_signal (process
, SIGQUIT
, current_group
, 0);
3455 DEFUN ("stop-process", Fstop_process
, Sstop_process
, 0, 2, 0,
3456 "Stop process PROCESS. May be process or name of one.\n\
3457 See function `interrupt-process' for more details on usage.")
3458 (process
, current_group
)
3459 Lisp_Object process
, current_group
;
3462 error ("no SIGTSTP support");
3464 process_send_signal (process
, SIGTSTP
, current_group
, 0);
3469 DEFUN ("continue-process", Fcontinue_process
, Scontinue_process
, 0, 2, 0,
3470 "Continue process PROCESS. May be process or name of one.\n\
3471 See function `interrupt-process' for more details on usage.")
3472 (process
, current_group
)
3473 Lisp_Object process
, current_group
;
3476 process_send_signal (process
, SIGCONT
, current_group
, 0);
3478 error ("no SIGCONT support");
3483 DEFUN ("signal-process", Fsignal_process
, Ssignal_process
,
3484 2, 2, "nProcess number: \nnSignal code: ",
3485 "Send the process with process id PID the signal with code SIGCODE.\n\
3486 PID must be an integer. The process need not be a child of this Emacs.\n\
3487 SIGCODE may be an integer, or a symbol whose name is a signal name.")
3489 Lisp_Object pid
, sigcode
;
3491 CHECK_NUMBER (pid
, 0);
3493 #define handle_signal(NAME, VALUE) \
3494 else if (!strcmp (name, NAME)) \
3495 XSETINT (sigcode, VALUE)
3497 if (INTEGERP (sigcode
))
3501 unsigned char *name
;
3503 CHECK_SYMBOL (sigcode
, 1);
3504 name
= XSYMBOL (sigcode
)->name
->data
;
3509 handle_signal ("SIGHUP", SIGHUP
);
3512 handle_signal ("SIGINT", SIGINT
);
3515 handle_signal ("SIGQUIT", SIGQUIT
);
3518 handle_signal ("SIGILL", SIGILL
);
3521 handle_signal ("SIGABRT", SIGABRT
);
3524 handle_signal ("SIGEMT", SIGEMT
);
3527 handle_signal ("SIGKILL", SIGKILL
);
3530 handle_signal ("SIGFPE", SIGFPE
);
3533 handle_signal ("SIGBUS", SIGBUS
);
3536 handle_signal ("SIGSEGV", SIGSEGV
);
3539 handle_signal ("SIGSYS", SIGSYS
);
3542 handle_signal ("SIGPIPE", SIGPIPE
);
3545 handle_signal ("SIGALRM", SIGALRM
);
3548 handle_signal ("SIGTERM", SIGTERM
);
3551 handle_signal ("SIGURG", SIGURG
);
3554 handle_signal ("SIGSTOP", SIGSTOP
);
3557 handle_signal ("SIGTSTP", SIGTSTP
);
3560 handle_signal ("SIGCONT", SIGCONT
);
3563 handle_signal ("SIGCHLD", SIGCHLD
);
3566 handle_signal ("SIGTTIN", SIGTTIN
);
3569 handle_signal ("SIGTTOU", SIGTTOU
);
3572 handle_signal ("SIGIO", SIGIO
);
3575 handle_signal ("SIGXCPU", SIGXCPU
);
3578 handle_signal ("SIGXFSZ", SIGXFSZ
);
3581 handle_signal ("SIGVTALRM", SIGVTALRM
);
3584 handle_signal ("SIGPROF", SIGPROF
);
3587 handle_signal ("SIGWINCH", SIGWINCH
);
3590 handle_signal ("SIGINFO", SIGINFO
);
3593 handle_signal ("SIGUSR1", SIGUSR1
);
3596 handle_signal ("SIGUSR2", SIGUSR2
);
3599 error ("Undefined signal name %s", name
);
3602 #undef handle_signal
3604 return make_number (kill (XINT (pid
), XINT (sigcode
)));
3607 DEFUN ("process-send-eof", Fprocess_send_eof
, Sprocess_send_eof
, 0, 1, 0,
3608 "Make PROCESS see end-of-file in its input.\n\
3609 Eof comes after any text already sent to it.\n\
3610 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
3611 nil, indicating the current buffer's process.\n\
3612 If PROCESS is a network connection, or is a process communicating\n\
3613 through a pipe (as opposed to a pty), then you cannot send any more\n\
3614 text to PROCESS after you call this function.")
3616 Lisp_Object process
;
3620 proc
= get_process (process
);
3622 /* Make sure the process is really alive. */
3623 if (! NILP (XPROCESS (proc
)->raw_status_low
))
3624 update_status (XPROCESS (proc
));
3625 if (! EQ (XPROCESS (proc
)->status
, Qrun
))
3626 error ("Process %s not running", XSTRING (XPROCESS (proc
)->name
)->data
);
3629 send_process (proc
, "\032", 1, Qnil
); /* ^z */
3631 if (!NILP (XPROCESS (proc
)->pty_flag
))
3632 send_process (proc
, "\004", 1, Qnil
);
3635 #ifdef HAVE_SHUTDOWN
3636 /* If this is a network connection, or socketpair is used
3637 for communication with the subprocess, call shutdown to cause EOF.
3638 (In some old system, shutdown to socketpair doesn't work.
3639 Then we just can't win.) */
3640 if (NILP (XPROCESS (proc
)->pid
)
3641 || XINT (XPROCESS (proc
)->outfd
) == XINT (XPROCESS (proc
)->infd
))
3642 shutdown (XINT (XPROCESS (proc
)->outfd
), 1);
3643 /* In case of socketpair, outfd == infd, so don't close it. */
3644 if (XINT (XPROCESS (proc
)->outfd
) != XINT (XPROCESS (proc
)->infd
))
3645 close (XINT (XPROCESS (proc
)->outfd
));
3646 #else /* not HAVE_SHUTDOWN */
3647 close (XINT (XPROCESS (proc
)->outfd
));
3648 #endif /* not HAVE_SHUTDOWN */
3649 XSETINT (XPROCESS (proc
)->outfd
, open (NULL_DEVICE
, O_WRONLY
));
3655 /* Kill all processes associated with `buffer'.
3656 If `buffer' is nil, kill all processes */
3658 kill_buffer_processes (buffer
)
3661 Lisp_Object tail
, proc
;
3663 for (tail
= Vprocess_alist
; GC_CONSP (tail
); tail
= XCONS (tail
)->cdr
)
3665 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
3666 if (GC_PROCESSP (proc
)
3667 && (NILP (buffer
) || EQ (XPROCESS (proc
)->buffer
, buffer
)))
3669 if (NETCONN_P (proc
))
3670 Fdelete_process (proc
);
3671 else if (XINT (XPROCESS (proc
)->infd
) >= 0)
3672 process_send_signal (proc
, SIGHUP
, Qnil
, 1);
3677 /* On receipt of a signal that a child status has changed,
3678 loop asking about children with changed statuses until
3679 the system says there are no more.
3680 All we do is change the status;
3681 we do not run sentinels or print notifications.
3682 That is saved for the next time keyboard input is done,
3683 in order to avoid timing errors. */
3685 /** WARNING: this can be called during garbage collection.
3686 Therefore, it must not be fooled by the presence of mark bits in
3689 /** USG WARNING: Although it is not obvious from the documentation
3690 in signal(2), on a USG system the SIGCLD handler MUST NOT call
3691 signal() before executing at least one wait(), otherwise the handler
3692 will be called again, resulting in an infinite loop. The relevant
3693 portion of the documentation reads "SIGCLD signals will be queued
3694 and the signal-catching function will be continually reentered until
3695 the queue is empty". Invoking signal() causes the kernel to reexamine
3696 the SIGCLD queue. Fred Fish, UniSoft Systems Inc. */
3699 sigchld_handler (signo
)
3702 int old_errno
= errno
;
3704 register struct Lisp_Process
*p
;
3705 extern EMACS_TIME
*input_available_clear_time
;
3709 sigheld
|= sigbit (SIGCHLD
);
3721 #endif /* no WUNTRACED */
3722 /* Keep trying to get a status until we get a definitive result. */
3726 pid
= wait3 (&w
, WNOHANG
| WUNTRACED
, 0);
3728 while (pid
<= 0 && errno
== EINTR
);
3732 /* A real failure. We have done all our job, so return. */
3734 /* USG systems forget handlers when they are used;
3735 must reestablish each time */
3736 #if defined (USG) && !defined (POSIX_SIGNALS)
3737 signal (signo
, sigchld_handler
); /* WARNING - must come after wait3() */
3740 sigheld
&= ~sigbit (SIGCHLD
);
3748 #endif /* no WNOHANG */
3750 /* Find the process that signaled us, and record its status. */
3753 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
3755 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
3756 p
= XPROCESS (proc
);
3757 if (EQ (p
->childp
, Qt
) && XFASTINT (p
->pid
) == pid
)
3762 /* Look for an asynchronous process whose pid hasn't been filled
3765 for (tail
= Vprocess_alist
; CONSP (tail
); tail
= XCONS (tail
)->cdr
)
3767 proc
= XCONS (XCONS (tail
)->car
)->cdr
;
3768 p
= XPROCESS (proc
);
3769 if (INTEGERP (p
->pid
) && XINT (p
->pid
) == -1)
3774 /* Change the status of the process that was found. */
3777 union { int i
; WAITTYPE wt
; } u
;
3778 int clear_desc_flag
= 0;
3780 XSETINT (p
->tick
, ++process_tick
);
3782 XSETINT (p
->raw_status_low
, u
.i
& 0xffff);
3783 XSETINT (p
->raw_status_high
, u
.i
>> 16);
3785 /* If process has terminated, stop waiting for its output. */
3786 if ((WIFSIGNALED (w
) || WIFEXITED (w
))
3787 && XINT (p
->infd
) >= 0)
3788 clear_desc_flag
= 1;
3790 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
3791 if (clear_desc_flag
)
3793 FD_CLR (XINT (p
->infd
), &input_wait_mask
);
3794 FD_CLR (XINT (p
->infd
), &non_keyboard_wait_mask
);
3797 /* Tell wait_reading_process_input that it needs to wake up and
3799 if (input_available_clear_time
)
3800 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
3803 /* There was no asynchronous process found for that id. Check
3804 if we have a synchronous process. */
3807 synch_process_alive
= 0;
3809 /* Report the status of the synchronous process. */
3811 synch_process_retcode
= WRETCODE (w
);
3812 else if (WIFSIGNALED (w
))
3814 int code
= WTERMSIG (w
);
3820 /* Suppress warning if the table has const char *. */
3821 signame
= (char *) sys_siglist
[code
];
3823 signame
= sys_errlist
[code
];
3827 signame
= "unknown";
3829 synch_process_death
= signame
;
3832 /* Tell wait_reading_process_input that it needs to wake up and
3834 if (input_available_clear_time
)
3835 EMACS_SET_SECS_USECS (*input_available_clear_time
, 0, 0);
3838 /* On some systems, we must return right away.
3839 If any more processes want to signal us, we will
3841 Otherwise (on systems that have WNOHANG), loop around
3842 to use up all the processes that have something to tell us. */
3843 #if defined (USG) && ! (defined (HPUX) && defined (WNOHANG)) || defined (WINDOWSNT)
3844 #if defined (USG) && ! defined (POSIX_SIGNALS)
3845 signal (signo
, sigchld_handler
);
3849 #endif /* USG, but not HPUX with WNOHANG */
3855 exec_sentinel_unwind (data
)
3858 XPROCESS (XCONS (data
)->car
)->sentinel
= XCONS (data
)->cdr
;
3863 exec_sentinel_error_handler (error
)
3866 cmd_error_internal (error
, "error in process sentinel: ");
3868 update_echo_area ();
3869 Fsleep_for (make_number (2), Qnil
);
3873 exec_sentinel (proc
, reason
)
3874 Lisp_Object proc
, reason
;
3876 Lisp_Object sentinel
, obuffer
, odeactivate
, okeymap
;
3877 register struct Lisp_Process
*p
= XPROCESS (proc
);
3878 int count
= specpdl_ptr
- specpdl
;
3879 int outer_running_asynch_code
= running_asynch_code
;
3881 /* No need to gcpro these, because all we do with them later
3882 is test them for EQness, and none of them should be a string. */
3883 odeactivate
= Vdeactivate_mark
;
3884 XSETBUFFER (obuffer
, current_buffer
);
3885 okeymap
= current_buffer
->keymap
;
3887 sentinel
= p
->sentinel
;
3888 if (NILP (sentinel
))
3891 /* Zilch the sentinel while it's running, to avoid recursive invocations;
3892 assure that it gets restored no matter how the sentinel exits. */
3894 record_unwind_protect (exec_sentinel_unwind
, Fcons (proc
, sentinel
));
3895 /* Inhibit quit so that random quits don't screw up a running filter. */
3896 specbind (Qinhibit_quit
, Qt
);
3897 specbind (Qlast_nonmenu_event
, Qt
);
3899 /* In case we get recursively called,
3900 and we already saved the match data nonrecursively,
3901 save the same match data in safely recursive fashion. */
3902 if (outer_running_asynch_code
)
3905 tem
= Fmatch_data (Qnil
, Qnil
);
3906 restore_match_data ();
3907 record_unwind_protect (Fstore_match_data
, Fmatch_data (Qnil
, Qnil
));
3908 Fstore_match_data (tem
);
3911 /* For speed, if a search happens within this code,
3912 save the match data in a special nonrecursive fashion. */
3913 running_asynch_code
= 1;
3915 internal_condition_case_1 (read_process_output_call
,
3917 Fcons (proc
, Fcons (reason
, Qnil
))),
3918 !NILP (Vdebug_on_error
) ? Qnil
: Qerror
,
3919 exec_sentinel_error_handler
);
3921 /* If we saved the match data nonrecursively, restore it now. */
3922 restore_match_data ();
3923 running_asynch_code
= outer_running_asynch_code
;
3925 Vdeactivate_mark
= odeactivate
;
3927 if (! EQ (Fcurrent_buffer (), obuffer
)
3928 || ! EQ (current_buffer
->keymap
, okeymap
))
3930 /* But do it only if the caller is actually going to read events.
3931 Otherwise there's no need to make him wake up, and it could
3932 cause trouble (for example it would make Fsit_for return). */
3933 if (waiting_for_user_input_p
== -1)
3934 record_asynch_buffer_change ();
3936 unbind_to (count
, Qnil
);
3939 /* Report all recent events of a change in process status
3940 (either run the sentinel or output a message).
3941 This is done while Emacs is waiting for keyboard input. */
3945 register Lisp_Object proc
, buffer
;
3946 Lisp_Object tail
, msg
;
3947 struct gcpro gcpro1
, gcpro2
;
3951 /* We need to gcpro tail; if read_process_output calls a filter
3952 which deletes a process and removes the cons to which tail points
3953 from Vprocess_alist, and then causes a GC, tail is an unprotected
3957 /* Set this now, so that if new processes are created by sentinels
3958 that we run, we get called again to handle their status changes. */
3959 update_tick
= process_tick
;
3961 for (tail
= Vprocess_alist
; !NILP (tail
); tail
= Fcdr (tail
))
3964 register struct Lisp_Process
*p
;
3966 proc
= Fcdr (Fcar (tail
));
3967 p
= XPROCESS (proc
);
3969 if (XINT (p
->tick
) != XINT (p
->update_tick
))
3971 XSETINT (p
->update_tick
, XINT (p
->tick
));
3973 /* If process is still active, read any output that remains. */
3974 while (! EQ (p
->filter
, Qt
)
3975 && XINT (p
->infd
) >= 0
3976 && read_process_output (proc
, XINT (p
->infd
)) > 0);
3980 /* Get the text to use for the message. */
3981 if (!NILP (p
->raw_status_low
))
3983 msg
= status_message (p
->status
);
3985 /* If process is terminated, deactivate it or delete it. */
3987 if (CONSP (p
->status
))
3988 symbol
= XCONS (p
->status
)->car
;
3990 if (EQ (symbol
, Qsignal
) || EQ (symbol
, Qexit
)
3991 || EQ (symbol
, Qclosed
))
3993 if (delete_exited_processes
)
3994 remove_process (proc
);
3996 deactivate_process (proc
);
3999 /* The actions above may have further incremented p->tick.
4000 So set p->update_tick again
4001 so that an error in the sentinel will not cause
4002 this code to be run again. */
4003 XSETINT (p
->update_tick
, XINT (p
->tick
));
4004 /* Now output the message suitably. */
4005 if (!NILP (p
->sentinel
))
4006 exec_sentinel (proc
, msg
);
4007 /* Don't bother with a message in the buffer
4008 when a process becomes runnable. */
4009 else if (!EQ (symbol
, Qrun
) && !NILP (buffer
))
4011 Lisp_Object ro
, tem
;
4012 struct buffer
*old
= current_buffer
;
4016 ro
= XBUFFER (buffer
)->read_only
;
4018 /* Avoid error if buffer is deleted
4019 (probably that's why the process is dead, too) */
4020 if (NILP (XBUFFER (buffer
)->name
))
4022 Fset_buffer (buffer
);
4025 /* Insert new output into buffer
4026 at the current end-of-output marker,
4027 thus preserving logical ordering of input and output. */
4028 if (XMARKER (p
->mark
)->buffer
)
4029 SET_PT (marker_position (p
->mark
));
4035 tem
= current_buffer
->read_only
;
4036 current_buffer
->read_only
= Qnil
;
4037 insert_string ("\nProcess ");
4038 Finsert (1, &p
->name
);
4039 insert_string (" ");
4041 current_buffer
->read_only
= tem
;
4042 Fset_marker (p
->mark
, make_number (PT
), p
->buffer
);
4044 if (opoint
>= before
)
4045 SET_PT (opoint
+ (PT
- before
));
4049 set_buffer_internal (old
);
4054 update_mode_lines
++; /* in case buffers use %s in mode-line-format */
4055 redisplay_preserve_echo_area ();
4061 DEFUN ("set-process-coding-system", Fset_process_coding_system
,
4062 Sset_process_coding_system
, 1, 3, 0,
4063 "Set coding-systems of PROCESS to DECODING (input from the process) and\n\
4064 ENCODING (output to the process).")
4065 (proc
, decoding
, encoding
)
4066 register Lisp_Object proc
, decoding
, encoding
;
4068 register struct Lisp_Process
*p
;
4070 CHECK_PROCESS (proc
, 0);
4071 p
= XPROCESS (proc
);
4072 if (XINT (p
->infd
) < 0)
4073 error ("Input file descriptor of %s closed", XSTRING (p
->name
)->data
);
4074 if (XINT (p
->outfd
) < 0)
4075 error ("Output file descriptor of %s closed", XSTRING (p
->name
)->data
);
4077 p
->decode_coding_system
= Fcheck_coding_system (decoding
);
4078 p
->encode_coding_system
= Fcheck_coding_system (encoding
);
4079 setup_coding_system (decoding
,
4080 proc_decode_coding_system
[XINT (p
->infd
)]);
4081 setup_coding_system (encoding
,
4082 proc_encode_coding_system
[XINT (p
->outfd
)]);
4087 DEFUN ("process-coding-system",
4088 Fprocess_coding_system
, Sprocess_coding_system
, 1, 1, 0,
4089 "Return a cons of coding-system for decoding and encoding of PROCESS.")
4091 register Lisp_Object proc
;
4093 CHECK_PROCESS (proc
, 0);
4094 return Fcons (XPROCESS (proc
)->decode_coding_system
,
4095 XPROCESS (proc
)->encode_coding_system
);
4098 /* The first time this is called, assume keyboard input comes from DESC
4099 instead of from where we used to expect it.
4100 Subsequent calls mean assume input keyboard can come from DESC
4101 in addition to other places. */
4103 static int add_keyboard_wait_descriptor_called_flag
;
4106 add_keyboard_wait_descriptor (desc
)
4109 if (! add_keyboard_wait_descriptor_called_flag
)
4110 FD_CLR (0, &input_wait_mask
);
4111 add_keyboard_wait_descriptor_called_flag
= 1;
4112 FD_SET (desc
, &input_wait_mask
);
4113 FD_SET (desc
, &non_process_wait_mask
);
4114 if (desc
> max_keyboard_desc
)
4115 max_keyboard_desc
= desc
;
4118 /* From now on, do not expect DESC to give keyboard input. */
4121 delete_keyboard_wait_descriptor (desc
)
4125 int lim
= max_keyboard_desc
;
4127 FD_CLR (desc
, &input_wait_mask
);
4128 FD_CLR (desc
, &non_process_wait_mask
);
4130 if (desc
== max_keyboard_desc
)
4131 for (fd
= 0; fd
< lim
; fd
++)
4132 if (FD_ISSET (fd
, &input_wait_mask
)
4133 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
4134 max_keyboard_desc
= fd
;
4137 /* Return nonzero if *MASK has a bit set
4138 that corresponds to one of the keyboard input descriptors. */
4141 keyboard_bit_set (mask
)
4146 for (fd
= 0; fd
<= max_keyboard_desc
; fd
++)
4147 if (FD_ISSET (fd
, mask
) && FD_ISSET (fd
, &input_wait_mask
)
4148 && !FD_ISSET (fd
, &non_keyboard_wait_mask
))
4160 if (! noninteractive
|| initialized
)
4162 signal (SIGCHLD
, sigchld_handler
);
4165 FD_ZERO (&input_wait_mask
);
4166 FD_ZERO (&non_keyboard_wait_mask
);
4167 FD_ZERO (&non_process_wait_mask
);
4168 max_process_desc
= 0;
4170 FD_SET (0, &input_wait_mask
);
4172 Vprocess_alist
= Qnil
;
4173 for (i
= 0; i
< MAXDESC
; i
++)
4175 chan_process
[i
] = Qnil
;
4176 proc_buffered_char
[i
] = -1;
4178 bzero (proc_decode_coding_system
, sizeof proc_decode_coding_system
);
4179 bzero (proc_encode_coding_system
, sizeof proc_encode_coding_system
);
4184 Qprocessp
= intern ("processp");
4185 staticpro (&Qprocessp
);
4186 Qrun
= intern ("run");
4188 Qstop
= intern ("stop");
4190 Qsignal
= intern ("signal");
4191 staticpro (&Qsignal
);
4193 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
4196 Qexit = intern ("exit");
4197 staticpro (&Qexit); */
4199 Qopen
= intern ("open");
4201 Qclosed
= intern ("closed");
4202 staticpro (&Qclosed
);
4204 Qlast_nonmenu_event
= intern ("last-nonmenu-event");
4205 staticpro (&Qlast_nonmenu_event
);
4207 staticpro (&Vprocess_alist
);
4209 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes
,
4210 "*Non-nil means delete processes immediately when they exit.\n\
4211 nil means don't delete them until `list-processes' is run.");
4213 delete_exited_processes
= 1;
4215 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type
,
4216 "Control type of device used to communicate with subprocesses.\n\
4217 Values are nil to use a pipe, or t or `pty' to use a pty.\n\
4218 The value has no effect if the system has no ptys or if all ptys are busy:\n\
4219 then a pipe is used in any case.\n\
4220 The value takes effect when `start-process' is called.");
4221 Vprocess_connection_type
= Qt
;
4223 defsubr (&Sprocessp
);
4224 defsubr (&Sget_process
);
4225 defsubr (&Sget_buffer_process
);
4226 defsubr (&Sdelete_process
);
4227 defsubr (&Sprocess_status
);
4228 defsubr (&Sprocess_exit_status
);
4229 defsubr (&Sprocess_id
);
4230 defsubr (&Sprocess_name
);
4231 defsubr (&Sprocess_tty_name
);
4232 defsubr (&Sprocess_command
);
4233 defsubr (&Sset_process_buffer
);
4234 defsubr (&Sprocess_buffer
);
4235 defsubr (&Sprocess_mark
);
4236 defsubr (&Sset_process_filter
);
4237 defsubr (&Sprocess_filter
);
4238 defsubr (&Sset_process_sentinel
);
4239 defsubr (&Sprocess_sentinel
);
4240 defsubr (&Sset_process_window_size
);
4241 defsubr (&Sprocess_kill_without_query
);
4242 defsubr (&Sprocess_contact
);
4243 defsubr (&Slist_processes
);
4244 defsubr (&Sprocess_list
);
4245 defsubr (&Sstart_process
);
4247 defsubr (&Sopen_network_stream
);
4248 #endif /* HAVE_SOCKETS */
4249 defsubr (&Saccept_process_output
);
4250 defsubr (&Sprocess_send_region
);
4251 defsubr (&Sprocess_send_string
);
4252 defsubr (&Sinterrupt_process
);
4253 defsubr (&Skill_process
);
4254 defsubr (&Squit_process
);
4255 defsubr (&Sstop_process
);
4256 defsubr (&Scontinue_process
);
4257 defsubr (&Sprocess_send_eof
);
4258 defsubr (&Ssignal_process
);
4259 defsubr (&Swaiting_for_user_input_p
);
4260 /* defsubr (&Sprocess_connection); */
4261 defsubr (&Sset_process_coding_system
);
4262 defsubr (&Sprocess_coding_system
);
4266 #else /* not subprocesses */
4268 #include <sys/types.h>
4272 #include "systime.h"
4273 #include "termopts.h"
4274 #include "sysselect.h"
4276 extern int frame_garbaged
;
4278 extern EMACS_TIME
timer_check ();
4279 extern int timers_run
;
4281 /* As described above, except assuming that there are no subprocesses:
4283 Wait for timeout to elapse and/or keyboard input to be available.
4286 timeout in seconds, or
4287 zero for no limit, or
4288 -1 means gobble data immediately available but don't wait for any.
4290 read_kbd is a Lisp_Object:
4291 0 to ignore keyboard input, or
4292 1 to return when input is available, or
4293 -1 means caller will actually read the input, so don't throw to
4295 a cons cell, meaning wait until its car is non-nil
4296 (and gobble terminal input into the buffer if any arrives), or
4297 We know that read_kbd will never be a Lisp_Process, since
4298 `subprocesses' isn't defined.
4300 do_display != 0 means redisplay should be done to show subprocess
4301 output that arrives.
4303 Return true iff we received input from any process. */
4306 wait_reading_process_input (time_limit
, microsecs
, read_kbd
, do_display
)
4307 int time_limit
, microsecs
;
4308 Lisp_Object read_kbd
;
4311 EMACS_TIME end_time
, timeout
;
4312 SELECT_TYPE waitchannels
;
4314 Lisp_Object
*wait_for_cell
= 0;
4316 /* If waiting for non-nil in a cell, record where. */
4317 if (CONSP (read_kbd
))
4319 wait_for_cell
= &XCONS (read_kbd
)->car
;
4320 XSETFASTINT (read_kbd
, 0);
4323 /* What does time_limit really mean? */
4324 if (time_limit
|| microsecs
)
4326 if (time_limit
== -1)
4327 /* In fact, it's zero. */
4328 EMACS_SET_SECS_USECS (timeout
, 0, 0);
4330 EMACS_SET_SECS_USECS (timeout
, time_limit
, microsecs
);
4332 /* How far in the future is that? */
4333 EMACS_GET_TIME (end_time
);
4334 EMACS_ADD_TIME (end_time
, end_time
, timeout
);
4337 /* It's infinite. */
4338 EMACS_SET_SECS_USECS (timeout
, 100000, 0);
4340 /* Turn off periodic alarms (in case they are in use)
4341 because the select emulator uses alarms. */
4347 int timeout_reduced_for_timers
= 0;
4349 /* If calling from keyboard input, do not quit
4350 since we want to return C-g as an input character.
4351 Otherwise, do pending quit if requested. */
4352 if (XINT (read_kbd
) >= 0)
4355 /* Exit now if the cell we're waiting for became non-nil. */
4356 if (wait_for_cell
&& ! NILP (*wait_for_cell
))
4359 /* Compute time from now till when time limit is up */
4360 /* Exit if already run out */
4361 if (time_limit
> 0 || microsecs
)
4363 EMACS_GET_TIME (timeout
);
4364 EMACS_SUB_TIME (timeout
, end_time
, timeout
);
4365 if (EMACS_TIME_NEG_P (timeout
))
4369 /* If our caller will not immediately handle keyboard events,
4370 run timer events directly.
4371 (Callers that will immediately read keyboard events
4372 call timer_delay on their own.) */
4373 if (! wait_for_cell
)
4375 EMACS_TIME timer_delay
;
4379 old_timers_run
= timers_run
;
4380 timer_delay
= timer_check (1);
4381 if (timers_run
!= old_timers_run
&& do_display
)
4383 redisplay_preserve_echo_area ();
4384 /* We must retry, since a timer may have requeued itself
4385 and that could alter the time delay. */
4389 if (! EMACS_TIME_NEG_P (timer_delay
) && time_limit
!= -1)
4391 EMACS_TIME difference
;
4392 EMACS_SUB_TIME (difference
, timer_delay
, timeout
);
4393 if (EMACS_TIME_NEG_P (difference
))
4395 timeout
= timer_delay
;
4396 timeout_reduced_for_timers
= 1;
4401 /* Cause C-g and alarm signals to take immediate action,
4402 and cause input available signals to zero out timeout. */
4403 if (XINT (read_kbd
) < 0)
4404 set_waiting_for_input (&timeout
);
4406 /* Wait till there is something to do. */
4408 if (! XINT (read_kbd
) && wait_for_cell
== 0)
4409 FD_ZERO (&waitchannels
);
4411 FD_SET (0, &waitchannels
);
4413 /* If a frame has been newly mapped and needs updating,
4414 reprocess its display stuff. */
4415 if (frame_garbaged
&& do_display
)
4417 clear_waiting_for_input ();
4418 redisplay_preserve_echo_area ();
4419 if (XINT (read_kbd
) < 0)
4420 set_waiting_for_input (&timeout
);
4423 if (XINT (read_kbd
) && detect_input_pending ())
4426 FD_ZERO (&waitchannels
);
4429 nfds
= select (1, &waitchannels
, (SELECT_TYPE
*)0, (SELECT_TYPE
*)0,
4434 /* Make C-g and alarm signals set flags again */
4435 clear_waiting_for_input ();
4437 /* If we woke up due to SIGWINCH, actually change size now. */
4438 do_pending_window_change ();
4440 if (time_limit
&& nfds
== 0 && ! timeout_reduced_for_timers
)
4441 /* We waited the full specified time, so return now. */
4446 /* If the system call was interrupted, then go around the
4448 if (xerrno
== EINTR
)
4449 FD_ZERO (&waitchannels
);
4451 error ("select error: %s", strerror (xerrno
));
4454 else if (nfds
> 0 && (waitchannels
& 1) && interrupt_input
)
4455 /* System sometimes fails to deliver SIGIO. */
4456 kill (getpid (), SIGIO
);
4459 if (XINT (read_kbd
) && interrupt_input
&& (waitchannels
& 1))
4460 kill (getpid (), SIGIO
);
4463 /* Check for keyboard input */
4465 if ((XINT (read_kbd
) != 0)
4466 && detect_input_pending_run_timers (do_display
))
4468 swallow_events (do_display
);
4469 if (detect_input_pending_run_timers (do_display
))
4473 /* If wait_for_cell. check for keyboard input
4474 but don't run any timers.
4475 ??? (It seems wrong to me to check for keyboard
4476 input at all when wait_for_cell, but the code
4477 has been this way since July 1994.
4478 Try changing this after version 19.31.) */
4480 && detect_input_pending ())
4482 swallow_events (do_display
);
4483 if (detect_input_pending ())
4487 /* Exit now if the cell we're waiting for became non-nil. */
4488 if (wait_for_cell
&& ! NILP (*wait_for_cell
))
4498 DEFUN ("get-buffer-process", Fget_buffer_process
, Sget_buffer_process
, 1, 1, 0,
4499 /* Don't confuse make-docfile by having two doc strings for this function.
4500 make-docfile does not pay attention to #if, for good reason! */
4503 register Lisp_Object name
;
4508 /* Kill all processes associated with `buffer'.
4509 If `buffer' is nil, kill all processes.
4510 Since we have no subprocesses, this does nothing. */
4512 kill_buffer_processes (buffer
)
4523 defsubr (&Sget_buffer_process
);
4527 #endif /* not subprocesses */