*** empty log message ***
[emacs.git] / src / process.c
blob442cae6d1590b39af23914354c8914448ad7d807
1 /* Asynchronous subprocess control for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1992 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include <signal.h>
23 #include "config.h"
25 /* This file is split into two parts by the following preprocessor
26 conditional. The 'then' clause contains all of the support for
27 asynchronous subprocesses. The 'else' clause contains stub
28 versions of some of the asynchronous subprocess routines that are
29 often called elsewhere in Emacs, so we don't have to #ifdef the
30 sections that call them. */
33 #ifdef subprocesses
35 #include <stdio.h>
36 #include <errno.h>
37 #include <setjmp.h>
38 #include <sys/types.h> /* some typedefs are used in sys/file.h */
39 #include <sys/file.h>
40 #include <sys/stat.h>
42 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
43 #include <sys/socket.h>
44 #include <netdb.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #endif /* HAVE_SOCKETS */
49 #if defined(BSD) || defined(STRIDE)
50 #include <sys/ioctl.h>
51 #if !defined (O_NDELAY) && defined (HAVE_PTYS)
52 #include <fcntl.h>
53 #endif /* HAVE_PTYS and no O_NDELAY */
54 #endif /* BSD or STRIDE */
55 #ifdef USG
56 #ifdef HAVE_TERMIOS
57 #include <termios.h>
58 #else
59 #include <termio.h>
60 #endif
61 #include <fcntl.h>
62 #endif /* USG */
64 #ifdef NEED_BSDTTY
65 #include <bsdtty.h>
66 #endif
68 #ifdef HPUX
69 #undef TIOCGPGRP
70 #endif
72 #ifdef IRIS
73 #include <sys/sysmacros.h> /* for "minor" */
74 #endif /* not IRIS */
76 #include "systime.h"
78 #if defined (HPUX) && defined (HAVE_PTYS)
79 #include <sys/ptyio.h>
80 #endif
82 #ifdef AIX
83 #include <sys/pty.h>
84 #include <unistd.h>
85 #endif
87 #ifdef SYSV_PTYS
88 #include <sys/tty.h>
89 #ifdef titan
90 #include <sys/ttyhw.h>
91 #include <sys/stream.h>
92 #endif
93 #include <sys/pty.h>
94 #endif
96 #ifdef XENIX
97 #undef TIOCGETC /* Avoid confusing some conditionals that test this. */
98 #endif
100 #ifdef BROKEN_TIOCGETC
101 #undef TIOCGETC
102 #endif
104 #include "lisp.h"
105 #include "window.h"
106 #include "buffer.h"
107 #include "process.h"
108 #include "termhooks.h"
109 #include "termopts.h"
110 #include "commands.h"
112 extern int screen_garbaged;
114 Lisp_Object Qrun, Qstop, Qsignal, Qopen, Qclosed;
115 /* Qexit is declared and initialized in eval.c. */
117 /* a process object is a network connection when its childp field is neither
118 Qt nor Qnil but is instead a string (name of foreign host we
119 are connected to + name of port we are connected to) */
121 #ifdef HAVE_SOCKETS
122 static Lisp_Object stream_process;
124 #define NETCONN_P(p) (XGCTYPE (XPROCESS (p)->childp) == Lisp_String)
125 #else
126 #define NETCONN_P(p) 0
127 #endif /* HAVE_SOCKETS */
129 /* Define first descriptor number available for subprocesses. */
130 #ifdef VMS
131 #define FIRST_PROC_DESC 1
132 #else /* Not VMS */
133 #define FIRST_PROC_DESC 3
134 #endif
136 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
137 testing SIGCHLD. */
139 #if !defined (SIGCHLD) && defined (SIGCLD)
140 #define SIGCHLD SIGCLD
141 #endif /* SIGCLD */
143 #include "syssignal.h"
145 /* Define the structure that the wait system call stores.
146 On many systems, there is a structure defined for this.
147 But on vanilla-ish USG systems there is not. */
149 #ifndef VMS
150 #ifndef WAITTYPE
151 #if !defined (BSD) && !defined (UNIPLUS) && !defined (STRIDE) && !(defined (HPUX) && !defined (NOMULTIPLEJOBS)) && !defined (HAVE_WAIT_HEADER)
152 mis;tak-+;;:
153 #define WAITTYPE int
154 #define WIFSTOPPED(w) ((w&0377) == 0177)
155 #define WIFSIGNALED(w) ((w&0377) != 0177 && (w&~0377) == 0)
156 #define WIFEXITED(w) ((w&0377) == 0)
157 #define WRETCODE(w) (w >> 8)
158 #define WSTOPSIG(w) (w >> 8)
159 #define WCOREDUMP(w) ((w&0200) != 0)
160 #define WTERMSIG(w) (w & 0377)
161 #else
162 #ifdef BSD4_1
163 #include <wait.h>
164 #else
165 #include <sys/wait.h>
166 #endif /* not BSD 4.1 */
168 #define WAITTYPE union wait
169 #define WRETCODE(w) w.w_retcode
170 #define WCOREDUMP(w) w.w_coredump
172 #ifdef HPUX
173 /* HPUX version 7 has broken definitions of these. */
174 #undef WTERMSIG
175 #undef WSTOPSIG
176 #undef WIFSTOPPED
177 #undef WIFSIGNALED
178 #undef WIFEXITED
179 #endif
181 #ifndef WTERMSIG
182 #define WTERMSIG(w) w.w_termsig
183 #endif
184 #ifndef WSTOPSIG
185 #define WSTOPSIG(w) w.w_stopsig
186 #endif
187 #ifndef WIFSTOPPED
188 #define WIFSTOPPED(w) (WTERMSIG (w) == 0177)
189 #endif
190 #ifndef WIFSIGNALED
191 #define WIFSIGNALED(w) (WTERMSIG (w) != 0177 && (WSTOPSIG (w)) == 0)
192 #endif
193 #ifndef WIFEXITED
194 #define WIFEXITED(w) (WTERMSIG (w) == 0)
195 #endif
196 #endif /* BSD or UNIPLUS or STRIDE */
197 #endif /* no WAITTYPE */
198 #else /* VMS */
200 /* For the CMU PTY driver + */
201 #define DCL_PROMPT "$ "
203 #include <ssdef.h>
204 #include <iodef.h>
205 #include <clidef.h>
206 #include "vmsproc.h"
207 #endif /* VMS */
209 extern errno;
210 extern sys_nerr;
211 extern char *sys_errlist[];
213 #ifndef VMS
214 #ifndef BSD4_1
215 extern char *sys_siglist[];
216 #else
217 char *sys_siglist[] =
219 "bum signal!!",
220 "hangup",
221 "interrupt",
222 "quit",
223 "illegal instruction",
224 "trace trap",
225 "iot instruction",
226 "emt instruction",
227 "floating point exception",
228 "kill",
229 "bus error",
230 "segmentation violation",
231 "bad argument to system call",
232 "write on a pipe with no one to read it",
233 "alarm clock",
234 "software termination signal from kill",
235 "status signal",
236 "sendable stop signal not from tty",
237 "stop signal from tty",
238 "continue a stopped process",
239 "child status has changed",
240 "background read attempted from control tty",
241 "background write attempted from control tty",
242 "input record available at control tty",
243 "exceeded CPU time limit",
244 "exceeded file size limit"
246 #endif
247 #endif /* VMS */
249 #ifdef vipc
251 #include "vipc.h"
252 extern int comm_server;
253 extern int net_listen_address;
254 #endif /* vipc */
256 /* t means use pty, nil means use a pipe,
257 maybe other values to come. */
258 Lisp_Object Vprocess_connection_type;
260 #ifdef SKTPAIR
261 #ifndef HAVE_SOCKETS
262 #include <sys/socket.h>
263 #endif
264 #endif /* SKTPAIR */
266 /* Number of events of change of status of a process. */
267 int process_tick;
269 /* Number of events for which the user or sentinel has been notified. */
270 int update_tick;
272 #ifdef FD_SET
273 /* We could get this from param.h, but better not to depend on finding that.
274 And better not to risk that it might define other symbols used in this
275 file. */
276 #define MAXDESC 64
277 #define SELECT_TYPE fd_set
278 #else /* no FD_SET */
279 #define MAXDESC 32
280 #define SELECT_TYPE int
282 /* Define the macros to access a single-int bitmap of descriptors. */
283 #define FD_SET(n, p) (*(p) |= (1 << (n)))
284 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
285 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
286 #define FD_ZERO(p) (*(p) = 0)
287 #endif /* no FD_SET */
289 /* Mask of bits indicating the descriptors that we wait for input on */
291 SELECT_TYPE input_wait_mask;
293 int delete_exited_processes;
295 /* Indexed by descriptor, gives the process (if any) for that descriptor */
296 Lisp_Object chan_process[MAXDESC];
298 /* Alist of elements (NAME . PROCESS) */
299 Lisp_Object Vprocess_alist;
301 Lisp_Object Qprocessp;
303 Lisp_Object get_process ();
305 /* Buffered-ahead input char from process, indexed by channel.
306 -1 means empty (no char is buffered).
307 Used on sys V where the only way to tell if there is any
308 output from the process is to read at least one char.
309 Always -1 on systems that support FIONREAD. */
311 int proc_buffered_char[MAXDESC];
313 /* Compute the Lisp form of the process status, p->status, from
314 the numeric status that was returned by `wait'. */
316 update_status (p)
317 struct Lisp_Process *p;
319 union { int i; WAITTYPE wt; } u;
320 u.i = XFASTINT (p->raw_status_low) + (XFASTINT (p->raw_status_high) << 16);
321 p->status = status_convert (u.wt);
322 p->raw_status_low = Qnil;
323 p->raw_status_high = Qnil;
326 /* Convert a process status work in Unix format to
327 the list that we use internally. */
329 Lisp_Object
330 status_convert (w)
331 WAITTYPE w;
333 if (WIFSTOPPED (w))
334 return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil));
335 else if (WIFEXITED (w))
336 return Fcons (Qexit, Fcons (make_number (WRETCODE (w)),
337 WCOREDUMP (w) ? Qt : Qnil));
338 else if (WIFSIGNALED (w))
339 return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)),
340 WCOREDUMP (w) ? Qt : Qnil));
341 else
342 return Qrun;
345 /* Given a status-list, extract the three pieces of information
346 and store them individually through the three pointers. */
348 void
349 decode_status (l, symbol, code, coredump)
350 Lisp_Object l;
351 Lisp_Object *symbol;
352 int *code;
353 int *coredump;
355 Lisp_Object tem;
357 if (XTYPE (l) == Lisp_Symbol)
359 *symbol = l;
360 *code = 0;
361 *coredump = 0;
363 else
365 *symbol = XCONS (l)->car;
366 tem = XCONS (l)->cdr;
367 *code = XFASTINT (XCONS (tem)->car);
368 tem = XFASTINT (XCONS (tem)->cdr);
369 *coredump = !NILP (tem);
373 /* Return a string describing a process status list. */
375 Lisp_Object
376 status_message (status)
377 Lisp_Object status;
379 Lisp_Object symbol;
380 int code, coredump;
381 Lisp_Object string, string2;
383 decode_status (status, &symbol, &code, &coredump);
385 if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
387 string = build_string (code < NSIG ? sys_siglist[code] : "unknown");
388 string2 = build_string (coredump ? " (core dumped)\n" : "\n");
389 XSTRING (string)->data[0] = DOWNCASE (XSTRING (string)->data[0]);
390 return concat2 (string, string2);
392 else if (EQ (symbol, Qexit))
394 if (code == 0)
395 return build_string ("finished\n");
396 string = Fint_to_string (make_number (code));
397 string2 = build_string (coredump ? " (core dumped)\n" : "\n");
398 return concat2 (build_string ("exited abnormally with code "),
399 concat2 (string, string2));
401 else
402 return Fcopy_sequence (Fsymbol_name (symbol));
405 #ifdef HAVE_PTYS
406 static int pty_process;
408 /* Open an available pty, returning a file descriptor.
409 Return -1 on failure.
410 The file name of the terminal corresponding to the pty
411 is left in the variable pty_name. */
413 char pty_name[24];
416 allocate_pty ()
418 struct stat stb;
419 register c, i;
420 int fd;
422 #ifdef PTY_ITERATION
423 PTY_ITERATION
424 #else
425 for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
426 for (i = 0; i < 16; i++)
427 #endif
429 #ifdef PTY_NAME_SPRINTF
430 PTY_NAME_SPRINTF
431 #else
432 #ifdef HPUX
433 sprintf (pty_name, "/dev/ptym/pty%c%x", c, i);
434 #else
435 #ifdef RTU
436 sprintf (pty_name, "/dev/pty%x", i);
437 #else
438 sprintf (pty_name, "/dev/pty%c%x", c, i);
439 #endif /* not RTU */
440 #endif /* not HPUX */
441 #endif /* no PTY_NAME_SPRINTF */
443 #ifndef IRIS
444 if (stat (pty_name, &stb) < 0)
445 return -1;
446 #ifdef O_NONBLOCK
447 fd = open (pty_name, O_RDWR | O_NONBLOCK, 0);
448 #else
449 fd = open (pty_name, O_RDWR | O_NDELAY, 0);
450 #endif
451 #else /* Unusual IRIS code */
452 *ptyv = open ("/dev/ptc", O_RDWR | O_NDELAY, 0);
453 if (fd < 0)
454 return -1;
455 if (fstat (fd, &stb) < 0)
456 return -1;
457 #endif /* IRIS */
459 if (fd >= 0)
461 /* check to make certain that both sides are available
462 this avoids a nasty yet stupid bug in rlogins */
463 #ifdef PTY_TTY_NAME_SPRINTF
464 PTY_TTY_NAME_SPRINTF
465 #else
466 /* TODO: In version 19, make these special cases use the macro above. */
467 #ifdef HPUX
468 sprintf (pty_name, "/dev/pty/tty%c%x", c, i);
469 #else
470 #ifdef RTU
471 sprintf (pty_name, "/dev/ttyp%x", i);
472 #else
473 #ifdef IRIS
474 sprintf (pty_name, "/dev/ttyq%d", minor (stb.st_rdev));
475 #else
476 sprintf (pty_name, "/dev/tty%c%x", c, i);
477 #endif /* not IRIS */
478 #endif /* not RTU */
479 #endif /* not HPUX */
480 #endif /* no PTY_TTY_NAME_SPRINTF */
481 #ifndef UNIPLUS
482 if (access (pty_name, 6) != 0)
484 close (fd);
485 #ifndef IRIS
486 continue;
487 #else
488 return -1;
489 #endif /* IRIS */
491 #endif /* not UNIPLUS */
492 setup_pty (fd);
493 return fd;
496 return -1;
498 #endif /* HAVE_PTYS */
500 Lisp_Object
501 make_process (name)
502 Lisp_Object name;
504 register Lisp_Object val, tem, name1;
505 register struct Lisp_Process *p;
506 char suffix[10];
507 register int i;
509 /* size of process structure includes the vector header,
510 so deduct for that. But struct Lisp_Vector includes the first
511 element, thus deducts too much, so add it back. */
512 val = Fmake_vector (make_number ((sizeof (struct Lisp_Process)
513 - sizeof (struct Lisp_Vector)
514 + sizeof (Lisp_Object))
515 / sizeof (Lisp_Object)),
516 Qnil);
517 XSETTYPE (val, Lisp_Process);
519 p = XPROCESS (val);
520 XFASTINT (p->infd) = 0;
521 XFASTINT (p->outfd) = 0;
522 XFASTINT (p->pid) = 0;
523 XFASTINT (p->tick) = 0;
524 XFASTINT (p->update_tick) = 0;
525 p->raw_status_low = Qnil;
526 p->raw_status_high = Qnil;
527 p->status = Qrun;
528 p->mark = Fmake_marker ();
530 /* If name is already in use, modify it until it is unused. */
532 name1 = name;
533 for (i = 1; ; i++)
535 tem = Fget_process (name1);
536 if (NILP (tem)) break;
537 sprintf (suffix, "<%d>", i);
538 name1 = concat2 (name, build_string (suffix));
540 name = name1;
541 p->name = name;
542 Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
543 return val;
546 remove_process (proc)
547 register Lisp_Object proc;
549 register Lisp_Object pair;
551 pair = Frassq (proc, Vprocess_alist);
552 Vprocess_alist = Fdelq (pair, Vprocess_alist);
553 Fset_marker (XPROCESS (proc)->mark, Qnil, Qnil);
555 deactivate_process (proc);
558 DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0,
559 "Return t if OBJECT is a process.")
560 (obj)
561 Lisp_Object obj;
563 return XTYPE (obj) == Lisp_Process ? Qt : Qnil;
566 DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0,
567 "Return the process named NAME, or nil if there is none.")
568 (name)
569 register Lisp_Object name;
571 if (XTYPE (name) == Lisp_Process)
572 return name;
573 CHECK_STRING (name, 0);
574 return Fcdr (Fassoc (name, Vprocess_alist));
577 DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
578 "Return the (or, a) process associated with BUFFER.\n\
579 BUFFER may be a buffer or the name of one.")
580 (name)
581 register Lisp_Object name;
583 register Lisp_Object buf, tail, proc;
585 if (NILP (name)) return Qnil;
586 buf = Fget_buffer (name);
587 if (NILP (buf)) return Qnil;
589 for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
591 proc = Fcdr (Fcar (tail));
592 if (XTYPE (proc) == Lisp_Process && EQ (XPROCESS (proc)->buffer, buf))
593 return proc;
595 return Qnil;
598 /* This is how commands for the user decode process arguments */
600 Lisp_Object
601 get_process (name)
602 register Lisp_Object name;
604 register Lisp_Object proc;
605 if (NILP (name))
606 proc = Fget_buffer_process (Fcurrent_buffer ());
607 else
609 proc = Fget_process (name);
610 if (NILP (proc))
611 proc = Fget_buffer_process (Fget_buffer (name));
614 if (!NILP (proc))
615 return proc;
617 if (NILP (name))
618 error ("Current buffer has no process");
619 else
620 error ("Process %s does not exist", XSTRING (name)->data);
621 /* NOTREACHED */
624 DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0,
625 "Delete PROCESS: kill it and forget about it immediately.\n\
626 PROCESS may be a process or the name of one, or a buffer name.")
627 (proc)
628 register Lisp_Object proc;
630 proc = get_process (proc);
631 XPROCESS (proc)->raw_status_low = Qnil;
632 XPROCESS (proc)->raw_status_high = Qnil;
633 if (NETCONN_P (proc))
635 XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (0), Qnil));
636 XSETINT (XPROCESS (proc)->tick, ++process_tick);
638 else if (XFASTINT (XPROCESS (proc)->infd))
640 Fkill_process (proc, Qnil);
641 /* Do this now, since remove_process will make sigchld_handler do nothing. */
642 XPROCESS (proc)->status
643 = Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil));
644 XSETINT (XPROCESS (proc)->tick, ++process_tick);
645 status_notify ();
647 remove_process (proc);
648 return Qnil;
651 DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0,
652 "Return the status of PROCESS: a symbol, one of these:\n\
653 run -- for a process that is running.\n\
654 stop -- for a process stopped but continuable.\n\
655 exit -- for a process that has exited.\n\
656 signal -- for a process that has got a fatal signal.\n\
657 open -- for a network stream connection that is open.\n\
658 closed -- for a network stream connection that is closed.\n\
659 nil -- if arg is a process name and no such process exists.")
660 /* command -- for a command channel opened to Emacs by another process.\n\
661 external -- for an i/o channel opened to Emacs by another process.\n\ */
662 (proc)
663 register Lisp_Object proc;
665 register struct Lisp_Process *p;
666 register Lisp_Object status;
667 proc = Fget_process (proc);
668 if (NILP (proc))
669 return proc;
670 p = XPROCESS (proc);
671 if (!NILP (p->raw_status_low))
672 update_status (p);
673 status = p->status;
674 if (XTYPE (status) == Lisp_Cons)
675 status = XCONS (status)->car;
676 if (NETCONN_P (proc))
678 if (EQ (status, Qrun))
679 status = Qopen;
680 else if (EQ (status, Qexit))
681 status = Qclosed;
683 return status;
686 DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status,
687 1, 1, 0,
688 "Return the exit status of PROCESS or the signal number that killed it.\n\
689 If PROCESS has not yet exited or died, return 0.")
690 (proc)
691 register Lisp_Object proc;
693 CHECK_PROCESS (proc, 0);
694 if (!NILP (XPROCESS (proc)->raw_status_low))
695 update_status (XPROCESS (proc));
696 if (XTYPE (XPROCESS (proc)->status) == Lisp_Cons)
697 return XCONS (XCONS (XPROCESS (proc)->status)->cdr)->car;
698 return make_number (0);
701 DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0,
702 "Return the process id of PROCESS.\n\
703 This is the pid of the Unix process which PROCESS uses or talks to.\n\
704 For a network connection, this value is nil.")
705 (proc)
706 register Lisp_Object proc;
708 CHECK_PROCESS (proc, 0);
709 return XPROCESS (proc)->pid;
712 DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0,
713 "Return the name of PROCESS, as a string.\n\
714 This is the name of the program invoked in PROCESS,\n\
715 possibly modified to make it unique among process names.")
716 (proc)
717 register Lisp_Object proc;
719 CHECK_PROCESS (proc, 0);
720 return XPROCESS (proc)->name;
723 DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0,
724 "Return the command that was executed to start PROCESS.\n\
725 This is a list of strings, the first string being the program executed\n\
726 and the rest of the strings being the arguments given to it.\n\
727 For a non-child channel, this is nil.")
728 (proc)
729 register Lisp_Object proc;
731 CHECK_PROCESS (proc, 0);
732 return XPROCESS (proc)->command;
735 DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer,
736 2, 2, 0,
737 "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
738 (proc, buffer)
739 register Lisp_Object proc, buffer;
741 CHECK_PROCESS (proc, 0);
742 if (!NILP (buffer))
743 CHECK_BUFFER (buffer, 1);
744 XPROCESS (proc)->buffer = buffer;
745 return buffer;
748 DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer,
749 1, 1, 0,
750 "Return the buffer PROCESS is associated with.\n\
751 Output from PROCESS is inserted in this buffer\n\
752 unless PROCESS has a filter.")
753 (proc)
754 register Lisp_Object proc;
756 CHECK_PROCESS (proc, 0);
757 return XPROCESS (proc)->buffer;
760 DEFUN ("process-mark", Fprocess_mark, Sprocess_mark,
761 1, 1, 0,
762 "Return the marker for the end of the last output from PROCESS.")
763 (proc)
764 register Lisp_Object proc;
766 CHECK_PROCESS (proc, 0);
767 return XPROCESS (proc)->mark;
770 DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter,
771 2, 2, 0,
772 "Give PROCESS the filter function FILTER; nil means no filter.\n\
773 When a process has a filter, each time it does output\n\
774 the entire string of output is passed to the filter.\n\
775 The filter gets two arguments: the process and the string of output.\n\
776 If the process has a filter, its buffer is not used for output.")
777 (proc, filter)
778 register Lisp_Object proc, filter;
780 CHECK_PROCESS (proc, 0);
781 XPROCESS (proc)->filter = filter;
782 return filter;
785 DEFUN ("process-filter", Fprocess_filter, Sprocess_filter,
786 1, 1, 0,
787 "Returns the filter function of PROCESS; nil if none.\n\
788 See `set-process-filter' for more info on filter functions.")
789 (proc)
790 register Lisp_Object proc;
792 CHECK_PROCESS (proc, 0);
793 return XPROCESS (proc)->filter;
796 DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel,
797 2, 2, 0,
798 "Give PROCESS the sentinel SENTINEL; nil for none.\n\
799 The sentinel is called as a function when the process changes state.\n\
800 It gets two arguments: the process, and a string describing the change.")
801 (proc, sentinel)
802 register Lisp_Object proc, sentinel;
804 CHECK_PROCESS (proc, 0);
805 XPROCESS (proc)->sentinel = sentinel;
806 return sentinel;
809 DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
810 1, 1, 0,
811 "Return the sentinel of PROCESS; nil if none.\n\
812 See `set-process-sentinel' for more info on sentinels.")
813 (proc)
814 register Lisp_Object proc;
816 CHECK_PROCESS (proc, 0);
817 return XPROCESS (proc)->sentinel;
820 DEFUN ("process-kill-without-query", Fprocess_kill_without_query,
821 Sprocess_kill_without_query, 1, 2, 0,
822 "Say no query needed if PROCESS is running when Emacs is exited.\n\
823 Optional second argument if non-nill says to require a query.\n\
824 Value is t if a query was formerly required.")
825 (proc, value)
826 register Lisp_Object proc, value;
828 Lisp_Object tem;
830 CHECK_PROCESS (proc, 0);
831 tem = XPROCESS (proc)->kill_without_query;
832 XPROCESS (proc)->kill_without_query = Fnull (value);
834 return Fnull (tem);
837 Lisp_Object
838 list_processes_1 ()
840 register Lisp_Object tail, tem;
841 Lisp_Object proc, minspace, tem1;
842 register struct buffer *old = current_buffer;
843 register struct Lisp_Process *p;
844 register int state;
845 char tembuf[80];
847 XFASTINT (minspace) = 1;
849 set_buffer_internal (XBUFFER (Vstandard_output));
850 Fbuffer_disable_undo (Vstandard_output);
852 current_buffer->truncate_lines = Qt;
854 write_string ("\
855 Proc Status Buffer Command\n\
856 ---- ------ ------ -------\n", -1);
858 for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
860 Lisp_Object symbol;
862 proc = Fcdr (Fcar (tail));
863 p = XPROCESS (proc);
864 if (NILP (p->childp))
865 continue;
867 Finsert (1, &p->name);
868 Findent_to (make_number (13), minspace);
870 if (!NILP (p->raw_status_low))
871 update_status (p);
872 symbol = p->status;
873 if (XTYPE (p->status) == Lisp_Cons)
874 symbol = XCONS (p->status)->car;
877 if (EQ (symbol, Qsignal))
879 Lisp_Object tem;
880 tem = Fcar (Fcdr (p->status));
881 #ifdef VMS
882 if (XINT (tem) < NSIG)
883 write_string (sys_siglist [XINT (tem)], -1);
884 else
885 #endif
886 Fprinc (symbol, Qnil);
888 else if (NETCONN_P (proc))
890 if (EQ (symbol, Qrun))
891 write_string ("open", -1);
892 else if (EQ (symbol, Qexit))
893 write_string ("closed", -1);
894 else
895 Fprinc (symbol, Qnil);
897 else
898 Fprinc (symbol, Qnil);
900 if (EQ (symbol, Qexit))
902 Lisp_Object tem;
903 tem = Fcar (Fcdr (p->status));
904 if (XFASTINT (tem))
906 sprintf (tembuf, " %d", XFASTINT (tem));
907 write_string (tembuf, -1);
911 if (EQ (symbol, Qsignal) || EQ (symbol, Qexit))
912 remove_process (proc);
914 Findent_to (make_number (22), minspace);
915 if (NILP (p->buffer))
916 insert_string ("(none)");
917 else if (NILP (XBUFFER (p->buffer)->name))
918 insert_string ("(Killed)");
919 else
920 Finsert (1, &XBUFFER (p->buffer)->name);
922 Findent_to (make_number (37), minspace);
924 if (NETCONN_P (proc))
926 sprintf (tembuf, "(network stream connection to %s)\n",
927 XSTRING (p->childp)->data);
928 insert_string (tembuf);
930 else
932 tem = p->command;
933 while (1)
935 tem1 = Fcar (tem);
936 Finsert (1, &tem1);
937 tem = Fcdr (tem);
938 if (NILP (tem))
939 break;
940 insert_string (" ");
942 insert_string ("\n");
945 return Qnil;
948 DEFUN ("list-processes", Flist_processes, Slist_processes, 0, 0, "",
949 "Display a list of all processes.\n\
950 \(Any processes listed as Exited or Signaled are actually eliminated\n\
951 after the listing is made.)")
954 internal_with_output_to_temp_buffer ("*Process List*",
955 list_processes_1, Qnil);
956 return Qnil;
959 DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
960 "Return a list of all processes.")
963 return Fmapcar (Qcdr, Vprocess_alist);
966 DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0,
967 "Start a program in a subprocess. Return the process object for it.\n\
968 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
969 NAME is name for process. It is modified if necessary to make it unique.\n\
970 BUFFER is the buffer or (buffer-name) to associate with the process.\n\
971 Process output goes at end of that buffer, unless you specify\n\
972 an output stream or filter function to handle the output.\n\
973 BUFFER may be also nil, meaning that this process is not associated\n\
974 with any buffer\n\
975 Third arg is program file name. It is searched for as in the shell.\n\
976 Remaining arguments are strings to give program as arguments.")
977 (nargs, args)
978 int nargs;
979 register Lisp_Object *args;
981 Lisp_Object buffer, name, program, proc, tem;
982 #ifdef VMS
983 register unsigned char *new_argv;
984 int len;
985 #else
986 register unsigned char **new_argv;
987 #endif
988 register int i;
990 buffer = args[1];
991 if (!NILP (buffer))
992 buffer = Fget_buffer_create (buffer);
994 name = args[0];
995 CHECK_STRING (name, 0);
997 program = args[2];
999 CHECK_STRING (program, 2);
1001 #ifdef VMS
1002 /* Make a one member argv with all args concatenated
1003 together separated by a blank. */
1004 len = XSTRING (program)->size + 2;
1005 for (i = 3; i < nargs; i++)
1007 tem = args[i];
1008 CHECK_STRING (tem, i);
1009 len += XSTRING (tem)->size + 1; /* count the blank */
1011 new_argv = (unsigned char *) alloca (len);
1012 strcpy (new_argv, XSTRING (program)->data);
1013 for (i = 3; i < nargs; i++)
1015 tem = args[i];
1016 CHECK_STRING (tem, i);
1017 strcat (new_argv, " ");
1018 strcat (new_argv, XSTRING (tem)->data);
1020 #else /* not VMS */
1021 new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
1023 for (i = 3; i < nargs; i++)
1025 tem = args[i];
1026 CHECK_STRING (tem, i);
1027 new_argv[i - 2] = XSTRING (tem)->data;
1029 new_argv[i - 2] = 0;
1030 new_argv[0] = XSTRING (program)->data;
1032 /* If program file name is not absolute, search our path for it */
1033 if (new_argv[0][0] != '/')
1035 tem = Qnil;
1036 openp (Vexec_path, program, "", &tem, 1);
1037 if (NILP (tem))
1038 report_file_error ("Searching for program", Fcons (program, Qnil));
1039 new_argv[0] = XSTRING (tem)->data;
1041 #endif /* not VMS */
1043 proc = make_process (name);
1045 XPROCESS (proc)->childp = Qt;
1046 XPROCESS (proc)->command_channel_p = Qnil;
1047 XPROCESS (proc)->buffer = buffer;
1048 XPROCESS (proc)->sentinel = Qnil;
1049 XPROCESS (proc)->filter = Qnil;
1050 XPROCESS (proc)->command = Flist (nargs - 2, args + 2);
1052 create_process (proc, new_argv);
1054 return proc;
1057 SIGTYPE
1058 create_process_1 (signo)
1059 int signo;
1061 #ifdef USG
1062 /* USG systems forget handlers when they are used;
1063 must reestablish each time */
1064 signal (signo, create_process_1);
1065 #endif /* USG */
1068 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1069 #ifdef USG
1070 #ifdef SIGCHLD
1071 /* Mimic blocking of signals on system V, which doesn't really have it. */
1073 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1074 int sigchld_deferred;
1076 SIGTYPE
1077 create_process_sigchld ()
1079 signal (SIGCHLD, create_process_sigchld);
1081 sigchld_deferred = 1;
1083 #endif
1084 #endif
1085 #endif
1087 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1088 create_process (process, new_argv)
1089 Lisp_Object process;
1090 char **new_argv;
1092 int pid, inchannel, outchannel, forkin, forkout;
1093 int sv[2];
1094 #ifdef SIGCHLD
1095 SIGTYPE (*sigchld)();
1096 #endif
1097 int pty_flag = 0;
1098 Lisp_Object current_dir;
1099 char **env;
1100 extern char **environ;
1102 env = environ;
1104 inchannel = outchannel = -1;
1106 #ifdef HAVE_PTYS
1107 if (EQ (Vprocess_connection_type, Qt))
1108 outchannel = inchannel = allocate_pty ();
1110 /* Make sure that the child will be able to chdir to the current
1111 buffer's current directory. We can't just have the child check
1112 for an error when it does the chdir, since it's in a vfork. */
1113 current_dir = expand_and_dir_to_file (current_buffer->directory, Qnil);
1114 if (NILP (Ffile_accessible_directory_p (current_dir)))
1115 report_file_error ("Setting current directory",
1116 Fcons (current_buffer->directory, Qnil));
1118 if (inchannel >= 0)
1120 #ifndef USG
1121 /* On USG systems it does not work to open the pty's tty here
1122 and then close and reopen it in the child. */
1123 #ifdef O_NOCTTY
1124 /* Don't let this terminal become our controlling terminal
1125 (in case we don't have one). */
1126 forkout = forkin = open (pty_name, O_RDWR | O_NOCTTY, 0);
1127 #else
1128 forkout = forkin = open (pty_name, O_RDWR, 0);
1129 #endif
1130 if (forkin < 0)
1131 report_file_error ("Opening pty", Qnil);
1132 #else
1133 forkin = forkout = -1;
1134 #endif /* not USG */
1135 pty_flag = 1;
1137 else
1138 #endif /* HAVE_PTYS */
1139 #ifdef SKTPAIR
1141 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) < 0)
1142 report_file_error ("Opening socketpair", Qnil);
1143 outchannel = inchannel = sv[0];
1144 forkout = forkin = sv[1];
1146 #else /* not SKTPAIR */
1148 pipe (sv);
1149 inchannel = sv[0];
1150 forkout = sv[1];
1151 pipe (sv);
1152 outchannel = sv[1];
1153 forkin = sv[0];
1155 #endif /* not SKTPAIR */
1157 #if 0
1158 /* Replaced by close_process_descs */
1159 set_exclusive_use (inchannel);
1160 set_exclusive_use (outchannel);
1161 #endif
1163 /* Stride people say it's a mystery why this is needed
1164 as well as the O_NDELAY, but that it fails without this. */
1165 #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
1167 int one = 1;
1168 ioctl (inchannel, FIONBIO, &one);
1170 #endif
1172 #ifdef O_NONBLOCK
1173 fcntl (inchannel, F_SETFL, O_NONBLOCK);
1174 #else
1175 #ifdef O_NDELAY
1176 fcntl (inchannel, F_SETFL, O_NDELAY);
1177 #endif
1178 #endif
1180 /* Record this as an active process, with its channels.
1181 As a result, child_setup will close Emacs's side of the pipes. */
1182 chan_process[inchannel] = process;
1183 XFASTINT (XPROCESS (process)->infd) = inchannel;
1184 XFASTINT (XPROCESS (process)->outfd) = outchannel;
1185 /* Record the tty descriptor used in the subprocess. */
1186 if (forkin < 0)
1187 XPROCESS (process)->subtty = Qnil;
1188 else
1189 XFASTINT (XPROCESS (process)->subtty) = forkin;
1190 XPROCESS (process)->pty_flag = (pty_flag ? Qt : Qnil);
1191 XPROCESS (process)->status = Qrun;
1193 /* Delay interrupts until we have a chance to store
1194 the new fork's pid in its process structure */
1195 #ifdef SIGCHLD
1196 #ifdef BSD4_1
1197 sighold (SIGCHLD);
1198 #else /* not BSD4_1 */
1199 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1200 sigsetmask (sigmask (SIGCHLD));
1201 #else /* ordinary USG */
1202 #if 0
1203 sigchld_deferred = 0;
1204 sigchld = signal (SIGCHLD, create_process_sigchld);
1205 #endif
1206 #endif /* ordinary USG */
1207 #endif /* not BSD4_1 */
1208 #endif /* SIGCHLD */
1210 /* Until we store the proper pid, enable sigchld_handler
1211 to recognize an unknown pid as standing for this process.
1212 It is very important not to let this `marker' value stay
1213 in the table after this function has returned; if it does
1214 it might cause call-process to hang and subsequent asynchronous
1215 processes to get their return values scrambled. */
1216 XSETINT (XPROCESS (process)->pid, -1);
1219 /* child_setup must clobber environ on systems with true vfork.
1220 Protect it from permanent change. */
1221 char **save_environ = environ;
1223 pid = vfork ();
1224 if (pid == 0)
1226 int xforkin = forkin;
1227 int xforkout = forkout;
1229 #if 0 /* This was probably a mistake--it duplicates code later on,
1230 but fails to handle all the cases. */
1231 /* Make sure SIGCHLD is not blocked in the child. */
1232 sigsetmask (SIGEMPTYMASK);
1233 #endif
1235 /* Make the pty be the controlling terminal of the process. */
1236 #ifdef HAVE_PTYS
1237 /* First, disconnect its current controlling terminal. */
1238 #ifdef HAVE_SETSID
1239 setsid ();
1240 #else /* not HAVE_SETSID */
1241 #ifdef USG
1242 /* It's very important to call setpgrp() here and no time
1243 afterwards. Otherwise, we lose our controlling tty which
1244 is set when we open the pty. */
1245 setpgrp ();
1246 #endif /* USG */
1247 #endif /* not HAVE_SETSID */
1248 #ifdef TIOCNOTTY
1249 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1250 can do TIOCSPGRP only to the process's controlling tty. */
1251 if (pty_flag)
1253 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1254 I can't test it since I don't have 4.3. */
1255 int j = open ("/dev/tty", O_RDWR, 0);
1256 ioctl (j, TIOCNOTTY, 0);
1257 close (j);
1258 #ifndef USG
1259 /* In order to get a controlling terminal on some versions
1260 of BSD, it is necessary to put the process in pgrp 0
1261 before it opens the terminal. */
1262 setpgrp (0, 0);
1263 #endif
1265 #endif /* TIOCNOTTY */
1267 #if !defined (RTU) && !defined (UNIPLUS)
1268 /*** There is a suggestion that this ought to be a
1269 conditional on TIOCSPGRP. */
1270 /* Now close the pty (if we had it open) and reopen it.
1271 This makes the pty the controlling terminal of the subprocess. */
1272 if (pty_flag)
1274 /* I wonder if close (open (pty_name, ...)) would work? */
1275 if (xforkin >= 0)
1276 close (xforkin);
1277 xforkout = xforkin = open (pty_name, O_RDWR, 0);
1279 if (xforkin < 0)
1280 abort ();
1282 #endif /* not UNIPLUS and not RTU */
1283 #ifdef SETUP_SLAVE_PTY
1284 SETUP_SLAVE_PTY;
1285 #endif /* SETUP_SLAVE_PTY */
1286 #ifdef AIX
1287 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
1288 Now reenable it in the child, so it will die when we want it to. */
1289 if (pty_flag)
1290 signal (SIGHUP, SIG_DFL);
1291 #endif
1292 #endif /* HAVE_PTYS */
1294 #ifdef SIGCHLD
1295 #ifdef BSD4_1
1296 sigrelse (SIGCHLD);
1297 #else /* not BSD4_1 */
1298 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1299 sigsetmask (SIGEMPTYMASK);
1300 #else /* ordinary USG */
1301 signal (SIGCHLD, sigchld);
1302 #endif /* ordinary USG */
1303 #endif /* not BSD4_1 */
1304 #endif /* SIGCHLD */
1306 child_setup_tty (xforkout);
1307 child_setup (xforkin, xforkout, xforkout,
1308 new_argv, env, 1, current_dir);
1310 environ = save_environ;
1313 if (pid < 0)
1315 remove_process (process);
1316 report_file_error ("Doing vfork", Qnil);
1319 XFASTINT (XPROCESS (process)->pid) = pid;
1321 FD_SET (inchannel, &input_wait_mask);
1323 /* If the subfork execv fails, and it exits,
1324 this close hangs. I don't know why.
1325 So have an interrupt jar it loose. */
1326 stop_polling ();
1327 signal (SIGALRM, create_process_1);
1328 alarm (1);
1329 #ifdef SYSV4_PTYS
1330 /* OK to close only if it's not a pty. Otherwise we need to leave
1331 it open for ioctl to get pgrp when signals are sent, or to send
1332 the interrupt characters through if that's how we're signalling
1333 subprocesses. Alternately if you are concerned about running out
1334 of file descriptors, you could just save the tty name and open
1335 just to do the ioctl. */
1336 if (NILP (XFASTINT (XPROCESS (process)->pty_flag)))
1337 #endif
1339 XPROCESS (process)->subtty = Qnil;
1340 if (forkin >= 0)
1341 close (forkin);
1343 alarm (0);
1344 start_polling ();
1345 if (forkin != forkout && forkout >= 0)
1346 close (forkout);
1348 #ifdef SIGCHLD
1349 #ifdef BSD4_1
1350 sigrelse (SIGCHLD);
1351 #else /* not BSD4_1 */
1352 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1353 sigsetmask (SIGEMPTYMASK);
1354 #else /* ordinary USG */
1355 #if 0
1356 signal (SIGCHLD, sigchld);
1357 /* Now really handle any of these signals
1358 that came in during this function. */
1359 if (sigchld_deferred)
1360 kill (getpid (), SIGCHLD);
1361 #endif
1362 #endif /* ordinary USG */
1363 #endif /* not BSD4_1 */
1364 #endif /* SIGCHLD */
1366 #endif /* not VMS */
1368 #ifdef HAVE_SOCKETS
1370 /* open a TCP network connection to a given HOST/SERVICE. Treated
1371 exactly like a normal process when reading and writing. Only
1372 differences are in status display and process deletion. A network
1373 connection has no PID; you cannot signal it. All you can do is
1374 deactivate and close it via delete-process */
1376 DEFUN ("open-network-stream", Fopen_network_stream, Sopen_network_stream,
1377 4, 4, 0,
1378 "Open a TCP connection for a service to a host.\n\
1379 Returns a subprocess-object to represent the connection.\n\
1380 Input and output work as for subprocesses; `delete-process' closes it.\n\
1381 Args are NAME BUFFER HOST SERVICE.\n\
1382 NAME is name for process. It is modified if necessary to make it unique.\n\
1383 BUFFER is the buffer (or buffer-name) to associate with the process.\n\
1384 Process output goes at end of that buffer, unless you specify\n\
1385 an output stream or filter function to handle the output.\n\
1386 BUFFER may be also nil, meaning that this process is not associated\n\
1387 with any buffer\n\
1388 Third arg is name of the host to connect to, or its IP address.\n\
1389 Fourth arg SERVICE is name of the service desired, or an integer\n\
1390 specifying a port number to connect to.")
1391 (name, buffer, host, service)
1392 Lisp_Object name, buffer, host, service;
1394 Lisp_Object proc;
1395 register int i;
1396 struct sockaddr_in address;
1397 struct servent *svc_info;
1398 struct hostent *host_info_ptr, host_info;
1399 char *(addr_list[2]);
1400 unsigned long numeric_addr;
1401 int s, outch, inch;
1402 char errstring[80];
1403 int port;
1404 struct hostent host_info_fixed;
1405 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1407 GCPRO4 (name, buffer, host, service);
1408 CHECK_STRING (name, 0);
1409 CHECK_STRING (host, 0);
1410 if (XTYPE(service) == Lisp_Int)
1411 port = htons ((unsigned short) XINT (service));
1412 else
1414 CHECK_STRING (service, 0);
1415 svc_info = getservbyname (XSTRING (service)->data, "tcp");
1416 if (svc_info == 0)
1417 error ("Unknown service \"%s\"", XSTRING (service)->data);
1418 port = svc_info->s_port;
1421 host_info_ptr = gethostbyname (XSTRING (host)->data);
1422 if (host_info_ptr == 0)
1423 /* Attempt to interpret host as numeric inet address */
1425 numeric_addr = inet_addr (XSTRING (host)->data);
1426 if (numeric_addr == -1)
1427 error ("Unknown host \"%s\"", XSTRING (host)->data);
1429 host_info_ptr = &host_info;
1430 host_info.h_name = 0;
1431 host_info.h_aliases = 0;
1432 host_info.h_addrtype = AF_INET;
1433 host_info.h_addr_list = &(addr_list[0]);
1434 addr_list[0] = (char*)(&numeric_addr);
1435 addr_list[1] = 0;
1436 host_info.h_length = strlen (addr_list[0]);
1439 bzero (&address, sizeof address);
1440 bcopy (host_info_ptr->h_addr, (char *) &address.sin_addr,
1441 host_info_ptr->h_length);
1442 address.sin_family = host_info_ptr->h_addrtype;
1443 address.sin_port = port;
1445 s = socket (host_info_ptr->h_addrtype, SOCK_STREAM, 0);
1446 if (s < 0)
1447 report_file_error ("error creating socket", Fcons (name, Qnil));
1449 loop:
1450 if (connect (s, &address, sizeof address) == -1)
1452 int xerrno = errno;
1453 if (errno == EINTR)
1454 goto loop;
1455 close (s);
1456 errno = xerrno;
1457 report_file_error ("connection failed",
1458 Fcons (host, Fcons (name, Qnil)));
1461 inch = s;
1462 outch = dup (s);
1463 if (outch < 0)
1464 report_file_error ("error duplicating socket", Fcons (name, Qnil));
1466 if (!NILP (buffer))
1467 buffer = Fget_buffer_create (buffer);
1468 proc = make_process (name);
1470 chan_process[inch] = proc;
1472 #ifdef O_NONBLOCK
1473 fcntl (inch, F_SETFL, O_NONBLOCK);
1474 #else
1475 #ifdef O_NDELAY
1476 fcntl (inch, F_SETFL, O_NDELAY);
1477 #endif
1478 #endif
1480 XPROCESS (proc)->childp = host;
1481 XPROCESS (proc)->command_channel_p = Qnil;
1482 XPROCESS (proc)->buffer = buffer;
1483 XPROCESS (proc)->sentinel = Qnil;
1484 XPROCESS (proc)->filter = Qnil;
1485 XPROCESS (proc)->command = Qnil;
1486 XPROCESS (proc)->pid = Qnil;
1487 XFASTINT (XPROCESS (proc)->infd) = s;
1488 XFASTINT (XPROCESS (proc)->outfd) = outch;
1489 XPROCESS (proc)->status = Qrun;
1490 FD_SET (inch, &input_wait_mask);
1492 UNGCPRO;
1493 return proc;
1495 #endif /* HAVE_SOCKETS */
1497 deactivate_process (proc)
1498 Lisp_Object proc;
1500 register int inchannel, outchannel;
1501 register struct Lisp_Process *p = XPROCESS (proc);
1503 inchannel = XFASTINT (p->infd);
1504 outchannel = XFASTINT (p->outfd);
1506 if (inchannel)
1508 /* Beware SIGCHLD hereabouts. */
1509 flush_pending_output (inchannel);
1510 #ifdef VMS
1512 VMS_PROC_STUFF *get_vms_process_pointer (), *vs;
1513 sys$dassgn (outchannel);
1514 vs = get_vms_process_pointer (p->pid)
1515 if (vs)
1516 give_back_vms_process_stuff (vs);
1518 #else
1519 close (inchannel);
1520 if (outchannel && outchannel != inchannel)
1521 close (outchannel);
1522 #endif
1524 XFASTINT (p->infd) = 0;
1525 XFASTINT (p->outfd) = 0;
1526 chan_process[inchannel] = Qnil;
1527 FD_CLR (inchannel, &input_wait_mask);
1531 /* Close all descriptors currently in use for communication
1532 with subprocess. This is used in a newly-forked subprocess
1533 to get rid of irrelevant descriptors. */
1535 close_process_descs ()
1537 int i;
1538 for (i = 0; i < MAXDESC; i++)
1540 Lisp_Object process;
1541 process = chan_process[i];
1542 if (!NILP (process))
1544 int in = XFASTINT (XPROCESS (process)->infd);
1545 int out = XFASTINT (XPROCESS (process)->outfd);
1546 if (in)
1547 close (in);
1548 if (out && in != out)
1549 close (out);
1554 DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output,
1555 0, 3, 0,
1556 "Allow any pending output from subprocesses to be read by Emacs.\n\
1557 It is read into the process' buffers or given to their filter functions.\n\
1558 Non-nil arg PROCESS means do not return until some output has been received\n\
1559 from PROCESS.\n\
1560 Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of\n\
1561 seconds and microseconds to wait; return after that much time whether\n\
1562 or not there is input.\n\
1563 Return non-nil iff we received any output before the timeout expired.")
1564 (proc, timeout, timeout_msecs)
1565 register Lisp_Object proc, timeout, timeout_msecs;
1567 int seconds;
1568 int useconds;
1570 if (! NILP (timeout_msecs))
1572 CHECK_NUMBER (timeout_msecs, 2);
1573 useconds = XINT (timeout_msecs);
1574 if (XTYPE (timeout) != Lisp_Int)
1575 XSET (timeout, Lisp_Int, 0);
1578 int carry = useconds / 1000000;
1580 XSETINT (timeout, XINT (timeout) + carry);
1581 useconds -= carry * 1000000;
1583 /* I think this clause is necessary because C doesn't
1584 guarantee a particular rounding direction for negative
1585 integers. */
1586 if (useconds < 0)
1588 XSETINT (timeout, XINT (timeout) - 1);
1589 useconds += 1000000;
1594 if (! NILP (timeout))
1596 CHECK_NUMBER (timeout, 1);
1597 seconds = XINT (timeout);
1598 if (seconds <= 0)
1599 seconds = -1;
1601 else
1603 if (NILP (proc))
1604 seconds = -1;
1605 else
1606 seconds = 0;
1609 return
1610 (wait_reading_process_input (seconds, useconds,
1611 (NILP (proc)
1612 ? XPROCESS (get_process (proc)) : 0), 0)
1613 ? Qt : Qnil);
1616 /* This variable is different from waiting_for_input in keyboard.c.
1617 It is used to communicate to a lisp process-filter/sentinel (via the
1618 function Fwaiting_for_user_input_p below) whether emacs was waiting
1619 for user-input when that process-filter was called.
1620 waiting_for_input cannot be used as that is by definition 0 when
1621 lisp code is being evalled */
1622 static int waiting_for_user_input_p;
1624 /* Read and dispose of subprocess output while waiting for timeout to
1625 elapse and/or keyboard input to be available.
1627 time_limit is:
1628 timeout in seconds, or
1629 zero for no limit, or
1630 -1 means gobble data immediately available but don't wait for any.
1632 read_kbd is:
1633 0 to ignore keyboard input, or
1634 1 to return when input is available, or
1635 -1 means caller will actually read the input, so don't throw to
1636 the quit handler, or
1637 a pointer to a struct Lisp_Process, meaning wait until something
1638 arrives from that process. The return value is true iff we read
1639 some input from that process.
1641 do_display != 0 means redisplay should be done to show subprocess
1642 output that arrives.
1644 If read_kbd is a pointer to a struct Lisp_Process, then the
1645 function returns true iff we received input from that process
1646 before the timeout elapsed.
1647 Otherwise, return true iff we recieved input from any process. */
1649 wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
1650 int time_limit, microsecs, read_kbd, do_display;
1652 register int channel, nfds, m;
1653 static SELECT_TYPE Available;
1654 int xerrno;
1655 Lisp_Object proc;
1656 EMACS_TIME timeout, end_time, garbage;
1657 SELECT_TYPE Atemp;
1658 int wait_channel = 0;
1659 struct Lisp_Process *wait_proc = 0;
1660 int got_some_input = 0;
1662 FD_ZERO (&Available);
1664 /* Detect when read_kbd is really the address of a Lisp_Process. */
1665 if (read_kbd > 10 || read_kbd < -1)
1667 wait_proc = (struct Lisp_Process *) read_kbd;
1668 wait_channel = XFASTINT (wait_proc->infd);
1669 read_kbd = 0;
1672 waiting_for_user_input_p = read_kbd;
1674 /* Since we may need to wait several times,
1675 compute the absolute time to return at. */
1676 if (time_limit || microsecs)
1678 EMACS_GET_TIME (end_time);
1679 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
1680 EMACS_ADD_TIME (end_time, end_time, timeout);
1683 /* Turn off periodic alarms (in case they are in use)
1684 because the select emulator uses alarms. */
1685 stop_polling ();
1687 while (1)
1689 /* If calling from keyboard input, do not quit
1690 since we want to return C-g as an input character.
1691 Otherwise, do pending quit if requested. */
1692 if (read_kbd >= 0)
1693 QUIT;
1695 /* If status of something has changed, and no input is available,
1696 notify the user of the change right away */
1697 if (update_tick != process_tick && do_display)
1699 Atemp = input_wait_mask;
1700 EMACS_SET_SECS_USECS (timeout, 0, 0);
1701 if (select (MAXDESC, &Atemp, 0, 0, &timeout) <= 0)
1702 status_notify ();
1705 /* Don't wait for output from a non-running process. */
1706 if (wait_proc != 0 && !NILP (wait_proc->raw_status_low))
1707 update_status (wait_proc);
1708 if (wait_proc != 0
1709 && ! EQ (wait_proc->status, Qrun))
1710 break;
1712 /* Compute time from now till when time limit is up */
1713 /* Exit if already run out */
1714 if (time_limit == -1)
1716 /* -1 specified for timeout means
1717 gobble output available now
1718 but don't wait at all. */
1720 EMACS_SET_SECS_USECS (timeout, 0, 0);
1722 else if (time_limit || microsecs)
1724 EMACS_GET_TIME (timeout);
1725 EMACS_SUB_TIME (timeout, end_time, timeout);
1726 if (EMACS_TIME_NEG_P (timeout))
1727 break;
1729 else
1731 EMACS_SET_SECS_USECS (timeout, 100000, 0);
1734 /* Cause C-g and alarm signals to take immediate action,
1735 and cause input available signals to zero out timeout */
1736 if (read_kbd < 0)
1737 set_waiting_for_input (&timeout);
1739 /* Wait till there is something to do */
1741 Available = input_wait_mask;
1742 if (!read_kbd)
1743 FD_CLR (0, &Available);
1745 /* If a screen has been newly mapped and needs updating,
1746 reprocess its display stuff. */
1747 if (screen_garbaged)
1748 redisplay_preserve_echo_area ();
1750 if (read_kbd && detect_input_pending ())
1751 nfds = 0;
1752 else
1753 nfds = select (MAXDESC, &Available, 0, 0, &timeout);
1755 xerrno = errno;
1757 /* Make C-g and alarm signals set flags again */
1758 clear_waiting_for_input ();
1760 /* If we woke up due to SIGWINCH, actually change size now. */
1761 do_pending_window_change ();
1763 if (time_limit && nfds == 0) /* timeout elapsed */
1764 break;
1765 if (nfds < 0)
1767 if (xerrno == EINTR)
1768 FD_ZERO (&Available);
1769 #ifdef ALLIANT
1770 /* This happens for no known reason on ALLIANT.
1771 I am guessing that this is the right response. -- RMS. */
1772 else if (xerrno == EFAULT)
1773 FD_ZERO (&Available);
1774 #endif
1775 else if (xerrno == EBADF)
1777 #ifdef AIX
1778 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
1779 the child's closure of the pts gives the parent a SIGHUP, and
1780 the ptc file descriptor is automatically closed,
1781 yielding EBADF here or at select() call above.
1782 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
1783 in m-ibmrt-aix.h), and here we just ignore the select error.
1784 Cleanup occurs c/o status_notify after SIGCLD. */
1785 FD_ZERO (&Available); /* Cannot depend on values returned */
1786 #else
1787 abort ();
1788 #endif
1790 else
1791 error("select error: %s", sys_errlist[xerrno]);
1793 #ifdef sun
1794 else if (nfds > 0 && FD_ISSET (0, &Available) && interrupt_input)
1795 /* System sometimes fails to deliver SIGIO. */
1796 kill (getpid (), SIGIO);
1797 #endif
1799 /* Check for keyboard input */
1800 /* If there is any, return immediately
1801 to give it higher priority than subprocesses */
1803 if (read_kbd && detect_input_pending ())
1804 break;
1806 #ifdef SIGIO
1807 /* If we think we have keyboard input waiting, but didn't get SIGIO
1808 go read it. This can happen with X on BSD after logging out.
1809 In that case, there really is no input and no SIGIO,
1810 but select says there is input. */
1813 if (read_kbd && interrupt_input && (Available & fileno (stdin)))
1815 if (read_kbd && interrupt_input && (FD_ISSET (fileno (stdin), &Available)))
1816 kill (0, SIGIO);
1817 #endif
1819 #ifdef vipc
1820 /* Check for connection from other process */
1822 if (Available & ChannelMask (comm_server))
1824 Available &= ~(ChannelMask (comm_server));
1825 create_commchan ();
1827 #endif vipc
1829 if (! wait_proc)
1830 got_some_input |= nfds > 0;
1832 /* Check for data from a process or a command channel */
1833 for (channel = FIRST_PROC_DESC; channel < MAXDESC; channel++)
1835 if (FD_ISSET (channel, &Available))
1837 int nread;
1839 /* If waiting for this channel, arrange to return as
1840 soon as no more input to be processed. No more
1841 waiting. */
1842 if (wait_channel == channel)
1844 wait_channel = 0;
1845 time_limit = -1;
1846 got_some_input = 1;
1848 proc = chan_process[channel];
1849 if (NILP (proc))
1850 continue;
1852 #ifdef vipc
1853 /* It's a command channel */
1854 if (!NILP (XPROCESS (proc)->command_channel_p))
1856 ProcessCommChan (channel, proc);
1857 if (NILP (XPROCESS (proc)->command_channel_p))
1859 /* It has ceased to be a command channel! */
1860 int bytes_available;
1861 if (ioctl (channel, FIONREAD, &bytes_available) < 0)
1862 bytes_available = 0;
1863 if (bytes_available)
1864 FD_SET (channel, &Available);
1866 continue;
1868 #endif /* vipc */
1870 /* Read data from the process, starting with our
1871 buffered-ahead character if we have one. */
1873 nread = read_process_output (proc, channel);
1874 if (nread > 0)
1876 /* Since read_process_output can run a filter,
1877 which can call accept-process-output,
1878 don't try to read from any other processes
1879 before doing the select again. */
1880 FD_ZERO (&Available);
1882 if (do_display)
1883 redisplay_preserve_echo_area ();
1885 #ifdef EWOULDBLOCK
1886 else if (nread == -1 && errno == EWOULDBLOCK)
1888 #else
1889 #ifdef O_NONBLOCK
1890 else if (nread == -1 && errno == EAGAIN)
1892 #else
1893 #ifdef O_NDELAY
1894 else if (nread == -1 && errno == EAGAIN)
1896 /* Note that we cannot distinguish between no input
1897 available now and a closed pipe.
1898 With luck, a closed pipe will be accompanied by
1899 subprocess termination and SIGCHLD. */
1900 else if (nread == 0 && !NETCONN_P (proc))
1902 #endif /* O_NDELAY */
1903 #endif /* O_NONBLOCK */
1904 #endif /* EWOULDBLOCK */
1905 #ifdef HAVE_PTYS
1906 /* On some OSs with ptys, when the process on one end of
1907 a pty exits, the other end gets an error reading with
1908 errno = EIO instead of getting an EOF (0 bytes read).
1909 Therefore, if we get an error reading and errno =
1910 EIO, just continue, because the child process has
1911 exited and should clean itself up soon (e.g. when we
1912 get a SIGCHLD). */
1913 else if (nread == -1 && errno == EIO)
1915 #endif /* HAVE_PTYS */
1916 /* If we can detect process termination, don't consider the process
1917 gone just because its pipe is closed. */
1918 #ifdef SIGCHLD
1919 else if (nread == 0 && !NETCONN_P (proc))
1921 #endif
1922 else
1924 /* Preserve status of processes already terminated. */
1925 XSETINT (XPROCESS (proc)->tick, ++process_tick);
1926 deactivate_process (proc);
1927 if (!NILP (XPROCESS (proc)->raw_status_low))
1928 update_status (XPROCESS (proc));
1929 if (EQ (XPROCESS (proc)->status, Qrun))
1930 XPROCESS (proc)->status
1931 = Fcons (Qexit, Fcons (make_number (256), Qnil));
1934 } /* end for each file descriptor */
1935 } /* end while exit conditions not met */
1937 /* Resume periodic signals to poll for input, if necessary. */
1938 start_polling ();
1940 return got_some_input;
1943 /* Read pending output from the process channel,
1944 starting with our buffered-ahead character if we have one.
1945 Yield number of characters read.
1947 This function reads at most 1024 characters.
1948 If you want to read all available subprocess output,
1949 you must call it repeatedly until it returns zero. */
1951 read_process_output (proc, channel)
1952 Lisp_Object proc;
1953 register int channel;
1955 register int nchars;
1956 #ifdef VMS
1957 char *chars;
1958 #else
1959 char chars[1024];
1960 #endif
1961 register Lisp_Object outstream;
1962 register struct buffer *old = current_buffer;
1963 register struct Lisp_Process *p = XPROCESS (proc);
1964 register int opoint;
1966 #ifdef VMS
1967 VMS_PROC_STUFF *vs, *get_vms_process_pointer();
1969 vs = get_vms_process_pointer (p->pid);
1970 if (vs)
1972 if (!vs->iosb[0])
1973 return(0); /* Really weird if it does this */
1974 if (!(vs->iosb[0] & 1))
1975 return -1; /* I/O error */
1977 else
1978 error ("Could not get VMS process pointer");
1979 chars = vs->inputBuffer;
1980 nchars = clean_vms_buffer (chars, vs->iosb[1]);
1981 if (nchars <= 0)
1983 start_vms_process_read (vs); /* Crank up the next read on the process */
1984 return 1; /* Nothing worth printing, say we got 1 */
1986 #else /* not VMS */
1988 if (proc_buffered_char[channel] < 0)
1989 nchars = read (channel, chars, sizeof chars);
1990 else
1992 chars[0] = proc_buffered_char[channel];
1993 proc_buffered_char[channel] = -1;
1994 nchars = read (channel, chars + 1, sizeof chars - 1);
1995 if (nchars < 0)
1996 nchars = 1;
1997 else
1998 nchars = nchars + 1;
2000 #endif /* not VMS */
2002 if (nchars <= 0) return nchars;
2004 outstream = p->filter;
2005 if (!NILP (outstream))
2007 /* We inhibit quit here instead of just catching it so that
2008 hitting ^G when a filter happens to be running won't screw
2009 it up. */
2010 int count = specpdl_ptr - specpdl;
2011 specbind (Qinhibit_quit, Qt);
2012 call2 (outstream, proc, make_string (chars, nchars));
2014 #ifdef VMS
2015 start_vms_process_read (vs);
2016 #endif
2017 unbind_to (count);
2018 return nchars;
2021 /* If no filter, write into buffer if it isn't dead. */
2022 if (!NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name))
2024 Lisp_Object tem;
2026 Fset_buffer (p->buffer);
2027 opoint = point;
2029 /* Insert new output into buffer
2030 at the current end-of-output marker,
2031 thus preserving logical ordering of input and output. */
2032 if (XMARKER (p->mark)->buffer)
2033 SET_PT (marker_position (p->mark));
2034 else
2035 SET_PT (ZV);
2036 if (point <= opoint)
2037 opoint += nchars;
2039 tem = current_buffer->read_only;
2040 current_buffer->read_only = Qnil;
2041 /* Insert before markers in case we are inserting where
2042 the buffer's mark is, and the user's next command is Meta-y. */
2043 insert_before_markers (chars, nchars);
2044 current_buffer->read_only = tem;
2045 Fset_marker (p->mark, make_number (point), p->buffer);
2046 update_mode_lines++;
2048 SET_PT (opoint);
2049 set_buffer_internal (old);
2051 #ifdef VMS
2052 start_vms_process_read (vs);
2053 #endif
2054 return nchars;
2057 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, Swaiting_for_user_input_p,
2058 0, 0, 0,
2059 "Returns non-NIL if emacs is waiting for input from the user.\n\
2060 This is intended for use by asynchronous process output filters and sentinels.")
2063 return ((waiting_for_user_input_p) ? Qt : Qnil);
2066 /* Sending data to subprocess */
2068 jmp_buf send_process_frame;
2070 SIGTYPE
2071 send_process_trap ()
2073 #ifdef BSD4_1
2074 sigrelse (SIGPIPE);
2075 sigrelse (SIGALRM);
2076 #endif /* BSD4_1 */
2077 longjmp (send_process_frame, 1);
2080 send_process (proc, buf, len)
2081 Lisp_Object proc;
2082 char *buf;
2083 int len;
2085 /* Don't use register vars; longjmp can lose them. */
2086 int rv;
2087 unsigned char *procname = XSTRING (XPROCESS (proc)->name)->data;
2090 #ifdef VMS
2091 struct Lisp_Process *p = XPROCESS (proc);
2092 VMS_PROC_STUFF *vs, *get_vms_process_pointer();
2093 #endif /* VMS */
2095 if (! NILP (XPROCESS (proc)->raw_status_low))
2096 update_status (XPROCESS (proc));
2097 if (! EQ (XPROCESS (proc)->status, Qrun))
2098 error ("Process %s not running", procname);
2100 #ifdef VMS
2101 vs = get_vms_process_pointer (p->pid);
2102 if (vs == 0)
2103 error ("Could not find this process: %x", p->pid);
2104 else if (write_to_vms_process (vs, buf, len))
2106 #else
2107 if (!setjmp (send_process_frame))
2108 while (len > 0)
2110 int this = len;
2111 SIGTYPE (*old_sigpipe)();
2113 /* Don't send more than 500 bytes at a time. */
2114 if (this > 500)
2115 this = 500;
2116 old_sigpipe = signal (SIGPIPE, send_process_trap);
2117 rv = write (XFASTINT (XPROCESS (proc)->outfd), buf, this);
2118 signal (SIGPIPE, old_sigpipe);
2119 if (rv < 0)
2121 if (0
2122 #ifdef EWOULDBLOCK
2123 || errno == EWOULDBLOCK
2124 #endif
2125 #ifdef EAGAIN
2126 || errno == EAGAIN
2127 #endif
2130 /* It would be nice to accept process output here,
2131 but that is difficult. For example, it could
2132 garbage what we are sending if that is from a buffer. */
2133 immediate_quit = 1;
2134 QUIT;
2135 sleep (1);
2136 immediate_quit = 0;
2137 continue;
2139 report_file_error ("writing to process", Fcons (proc, Qnil));
2141 buf += rv;
2142 len -= rv;
2143 /* Allow input from processes between bursts of sending.
2144 Otherwise things may get stopped up. */
2145 if (len > 0)
2146 wait_reading_process_input (-1, 0, 0, 0);
2148 #endif
2149 else
2151 XPROCESS (proc)->raw_status_low = Qnil;
2152 XPROCESS (proc)->raw_status_high = Qnil;
2153 XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (256), Qnil));
2154 XSETINT (XPROCESS (proc)->tick, ++process_tick);
2155 deactivate_process (proc);
2156 #ifdef VMS
2157 error ("Error writing to process %s; closed it", procname);
2158 #else
2159 error ("SIGPIPE raised on process %s; closed it", procname);
2160 #endif
2164 DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region,
2165 3, 3, 0,
2166 "Send current contents of region as input to PROCESS.\n\
2167 PROCESS may be a process name or an actual process.\n\
2168 Called from program, takes three arguments, PROCESS, START and END.\n\
2169 If the region is more than 500 characters long,\n\
2170 it is sent in several bunches. This may happen even for shorter regions.\n\
2171 Output from processes can arrive in between bunches.")
2172 (process, start, end)
2173 Lisp_Object process, start, end;
2175 Lisp_Object proc;
2176 int start1;
2178 proc = get_process (process);
2179 validate_region (&start, &end);
2181 if (XINT (start) < GPT && XINT (end) > GPT)
2182 move_gap (start);
2184 start1 = XINT (start);
2185 send_process (proc, &FETCH_CHAR (start1), XINT (end) - XINT (start));
2187 return Qnil;
2190 DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string,
2191 2, 2, 0,
2192 "Send PROCESS the contents of STRING as input.\n\
2193 PROCESS may be a process name or an actual process.\n\
2194 If STRING is more than 500 characters long,\n\
2195 it is sent in several bunches. This may happen even for shorter strings.\n\
2196 Output from processes can arrive in between bunches.")
2197 (process, string)
2198 Lisp_Object process, string;
2200 Lisp_Object proc;
2201 CHECK_STRING (string, 1);
2202 proc = get_process (process);
2203 send_process (proc, XSTRING (string)->data, XSTRING (string)->size);
2204 return Qnil;
2207 /* send a signal number SIGNO to PROCESS.
2208 CURRENT_GROUP means send to the process group that currently owns
2209 the terminal being used to communicate with PROCESS.
2210 This is used for various commands in shell mode.
2211 If NOMSG is zero, insert signal-announcements into process's buffers
2212 right away. */
2214 process_send_signal (process, signo, current_group, nomsg)
2215 Lisp_Object process;
2216 int signo;
2217 Lisp_Object current_group;
2218 int nomsg;
2220 Lisp_Object proc;
2221 register struct Lisp_Process *p;
2222 int gid;
2223 int no_pgrp = 0;
2225 proc = get_process (process);
2226 p = XPROCESS (proc);
2228 if (!EQ (p->childp, Qt))
2229 error ("Process %s is not a subprocess",
2230 XSTRING (p->name)->data);
2231 if (!XFASTINT (p->infd))
2232 error ("Process %s is not active",
2233 XSTRING (p->name)->data);
2235 if (NILP (p->pty_flag))
2236 current_group = Qnil;
2238 #ifdef TIOCGPGRP /* Not sure about this! (fnf) */
2239 /* If we are using pgrps, get a pgrp number and make it negative. */
2240 if (!NILP (current_group))
2242 /* If possible, send signals to the entire pgrp
2243 by sending an input character to it. */
2244 #if defined (TIOCGLTC) && defined (TIOCGETC)
2245 struct tchars c;
2246 struct ltchars lc;
2248 switch (signo)
2250 case SIGINT:
2251 ioctl (XFASTINT (p->infd), TIOCGETC, &c);
2252 send_process (proc, &c.t_intrc, 1);
2253 return Qnil;
2254 case SIGQUIT:
2255 ioctl (XFASTINT (p->infd), TIOCGETC, &c);
2256 send_process (proc, &c.t_quitc, 1);
2257 return Qnil;
2258 case SIGTSTP:
2259 ioctl (XFASTINT (p->infd), TIOCGLTC, &lc);
2260 send_process (proc, &lc.t_suspc, 1);
2261 return Qnil;
2263 #endif /* have TIOCGLTC and have TIOCGETC */
2264 /* It is possible that the following code would work
2265 on other kinds of USG systems, not just on the IRIS.
2266 This should be tried in Emacs 19. */
2267 #if defined (IRIS) && defined (HAVE_SETSID) /* Check for Irix, not older
2268 systems. */
2269 struct termio t;
2270 switch (signo)
2272 case SIGINT:
2273 ioctl (XFASTINT (p->infd), TCGETA, &t);
2274 send_process (proc, &t.c_cc[VINTR], 1);
2275 return Qnil;
2276 case SIGQUIT:
2277 ioctl (XFASTINT (p->infd), TCGETA, &t);
2278 send_process (proc, &t.c_cc[VQUIT], 1);
2279 return Qnil;
2280 case SIGTSTP:
2281 ioctl (XFASTINT (p->infd), TCGETA, &t);
2282 send_process (proc, &t.c_cc[VSWTCH], 1);
2283 return Qnil;
2285 #endif /* IRIS and HAVE_SETSID */
2287 /* Get the pgrp using the tty itself, if we have that.
2288 Otherwise, use the pty to get the pgrp.
2289 On pfa systems, saka@pfu.fujitsu.co.JP writes:
2290 "TICGPGRP symbol defined in sys/ioctl.h at E50.
2291 But, TIOCGPGRP donot work on E50 ;-P work fine on E60"
2292 His patch indicates that if TIOCGPGRP returns an error, then
2293 we should just assume that p->pid is also the process group id. */
2295 int err;
2297 if (!NILP (p->subtty))
2298 err = ioctl (XFASTINT (p->subtty), TIOCGPGRP, &gid);
2299 else
2300 err = ioctl (XFASTINT (p->infd), TIOCGPGRP, &gid);
2302 #ifdef pfa
2303 if (err == -1)
2304 gid = - XFASTINT (p->pid);
2305 #endif
2307 if (gid == -1)
2308 no_pgrp = 1;
2309 else
2310 gid = - gid;
2312 else
2313 gid = - XFASTINT (p->pid);
2314 #else /* not using pgrps */
2315 /* Can't select pgrps on this system, so we know that
2316 the child itself heads the pgrp. */
2317 gid = - XFASTINT (p->pid);
2318 #endif /* not using pgrps */
2320 switch (signo)
2322 #ifdef SIGCONT
2323 case SIGCONT:
2324 p->raw_status_low = Qnil;
2325 p->raw_status_high = Qnil;
2326 p->status = Qrun;
2327 XSETINT (p->tick, ++process_tick);
2328 if (!nomsg)
2329 status_notify ();
2330 break;
2331 #endif
2332 case SIGINT:
2333 #ifdef VMS
2334 send_process (proc, "\003", 1); /* ^C */
2335 goto whoosh;
2336 #endif
2337 case SIGQUIT:
2338 #ifdef VMS
2339 send_process (proc, "\031", 1); /* ^Y */
2340 goto whoosh;
2341 #endif
2342 case SIGKILL:
2343 #ifdef VMS
2344 sys$forcex (&(XFASTINT (p->pid)), 0, 1);
2345 whoosh:
2346 #endif
2347 flush_pending_output (XFASTINT (p->infd));
2348 break;
2351 /* If we don't have process groups, send the signal to the immediate
2352 subprocess. That isn't really right, but it's better than any
2353 obvious alternative. */
2354 if (no_pgrp)
2356 kill (XFASTINT (p->pid), signo);
2357 return;
2360 /* gid may be a pid, or minus a pgrp's number */
2361 #ifdef TIOCSIGSEND
2362 if (!NILP (current_group))
2363 ioctl (XFASTINT (p->infd), TIOCSIGSEND, signo);
2364 else
2366 gid = - XFASTINT (p->pid);
2367 kill (gid, signo);
2369 #else /* no TIOCSIGSEND */
2370 EMACS_KILLPG (-gid, signo);
2371 #endif
2374 DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0,
2375 "Interrupt process PROCESS. May be process or name of one.\n\
2376 Nil or no arg means current buffer's process.\n\
2377 Second arg CURRENT-GROUP non-nil means send signal to\n\
2378 the current process-group of the process's controlling terminal\n\
2379 rather than to the process's own process group.\n\
2380 If the process is a shell, this means interrupt current subjob\n\
2381 rather than the shell.")
2382 (process, current_group)
2383 Lisp_Object process, current_group;
2385 process_send_signal (process, SIGINT, current_group, 0);
2386 return process;
2389 DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0,
2390 "Kill process PROCESS. May be process or name of one.\n\
2391 See function `interrupt-process' for more details on usage.")
2392 (process, current_group)
2393 Lisp_Object process, current_group;
2395 process_send_signal (process, SIGKILL, current_group, 0);
2396 return process;
2399 DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0,
2400 "Send QUIT signal to process PROCESS. May be process or name of one.\n\
2401 See function `interrupt-process' for more details on usage.")
2402 (process, current_group)
2403 Lisp_Object process, current_group;
2405 process_send_signal (process, SIGQUIT, current_group, 0);
2406 return process;
2409 DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0,
2410 "Stop process PROCESS. May be process or name of one.\n\
2411 See function `interrupt-process' for more details on usage.")
2412 (process, current_group)
2413 Lisp_Object process, current_group;
2415 #ifndef SIGTSTP
2416 error ("no SIGTSTP support");
2417 #else
2418 process_send_signal (process, SIGTSTP, current_group, 0);
2419 #endif
2420 return process;
2423 DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0,
2424 "Continue process PROCESS. May be process or name of one.\n\
2425 See function `interrupt-process' for more details on usage.")
2426 (process, current_group)
2427 Lisp_Object process, current_group;
2429 #ifdef SIGCONT
2430 process_send_signal (process, SIGCONT, current_group, 0);
2431 #else
2432 error ("no SIGCONT support");
2433 #endif
2434 return process;
2437 DEFUN ("signal-process", Fsignal_process, Ssignal_process,
2438 2, 2, "nProcess number: \nnSignal code: ",
2439 "Send the process with number PID the signal with code CODE.\n\
2440 Both PID and CODE are integers.")
2441 (pid, sig)
2442 Lisp_Object pid, sig;
2444 CHECK_NUMBER (pid, 0);
2445 CHECK_NUMBER (sig, 1);
2446 return make_number (kill (XINT (pid), XINT (sig)));
2449 DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0,
2450 "Make PROCESS see end-of-file in its input.\n\
2451 Eof comes after any text already sent to it.\n\
2452 nil or no arg means current buffer's process.")
2453 (process)
2454 Lisp_Object process;
2456 Lisp_Object proc;
2458 proc = get_process (process);
2459 /* Sending a zero-length record is supposed to mean eof
2460 when TIOCREMOTE is turned on. */
2461 #ifdef DID_REMOTE
2463 char buf[1];
2464 write (XFASTINT (XPROCESS (proc)->outfd), buf, 0);
2466 #else /* did not do TOICREMOTE */
2467 #ifdef VMS
2468 send_process (proc, "\032", 1); /* ^z */
2469 #else
2470 if (!NILP (XPROCESS (proc)->pty_flag))
2471 send_process (proc, "\004", 1);
2472 else
2474 close (XPROCESS (proc)->outfd);
2475 XFASTINT (XPROCESS (proc)->outfd) = open ("/dev/null", O_WRONLY);
2477 #endif /* VMS */
2478 #endif /* did not do TOICREMOTE */
2479 return process;
2482 /* Kill all processes associated with `buffer'.
2483 If `buffer' is nil, kill all processes */
2485 kill_buffer_processes (buffer)
2486 Lisp_Object buffer;
2488 Lisp_Object tail, proc;
2490 for (tail = Vprocess_alist; XGCTYPE (tail) == Lisp_Cons;
2491 tail = XCONS (tail)->cdr)
2493 proc = XCONS (XCONS (tail)->car)->cdr;
2494 if (XGCTYPE (proc) == Lisp_Process
2495 && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer)))
2497 if (NETCONN_P (proc))
2498 deactivate_process (proc);
2499 else if (XFASTINT (XPROCESS (proc)->infd))
2500 process_send_signal (proc, SIGHUP, Qnil, 1);
2505 /* On receipt of a signal that a child status has changed,
2506 loop asking about children with changed statuses until
2507 the system says there are no more.
2508 All we do is change the status;
2509 we do not run sentinels or print notifications.
2510 That is saved for the next time keyboard input is done,
2511 in order to avoid timing errors. */
2513 /** WARNING: this can be called during garbage collection.
2514 Therefore, it must not be fooled by the presence of mark bits in
2515 Lisp objects. */
2517 /** USG WARNING: Although it is not obvious from the documentation
2518 in signal(2), on a USG system the SIGCLD handler MUST NOT call
2519 signal() before executing at least one wait(), otherwise the handler
2520 will be called again, resulting in an infinite loop. The relevant
2521 portion of the documentation reads "SIGCLD signals will be queued
2522 and the signal-catching function will be continually reentered until
2523 the queue is empty". Invoking signal() causes the kernel to reexamine
2524 the SIGCLD queue. Fred Fish, UniSoft Systems Inc. */
2526 SIGTYPE
2527 sigchld_handler (signo)
2528 int signo;
2530 int old_errno = errno;
2531 Lisp_Object proc;
2532 register struct Lisp_Process *p;
2534 #ifdef BSD4_1
2535 extern int sigheld;
2536 sigheld |= sigbit (SIGCHLD);
2537 #endif
2539 while (1)
2541 register int pid;
2542 WAITTYPE w;
2543 Lisp_Object tail;
2545 #ifdef WNOHANG
2546 #ifndef WUNTRACED
2547 #define WUNTRACED 0
2548 #endif /* no WUNTRACED */
2549 /* Keep trying to get a status until we get a definitive result. */
2552 errno = 0;
2553 pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
2555 while (pid <= 0 && errno == EINTR);
2557 if (pid <= 0)
2559 /* A real failure. We have done all our job, so return. */
2561 /* USG systems forget handlers when they are used;
2562 must reestablish each time */
2563 #ifdef USG
2564 signal (signo, sigchld_handler); /* WARNING - must come after wait3() */
2565 #endif
2566 #ifdef BSD4_1
2567 sigheld &= ~sigbit (SIGCHLD);
2568 sigrelse (SIGCHLD);
2569 #endif
2570 errno = old_errno;
2571 return;
2573 #else
2574 pid = wait (&w);
2575 #endif /* no WNOHANG */
2577 /* Find the process that signaled us, and record its status. */
2579 p = 0;
2580 for (tail = Vprocess_alist; XSYMBOL (tail) != XSYMBOL (Qnil); tail = XCONS (tail)->cdr)
2582 proc = XCONS (XCONS (tail)->car)->cdr;
2583 p = XPROCESS (proc);
2584 if (EQ (p->childp, Qt) && XFASTINT (p->pid) == pid)
2585 break;
2586 p = 0;
2589 /* Look for an asynchronous process whose pid hasn't been filled
2590 in yet. */
2591 if (p == 0)
2592 for (tail = Vprocess_alist; XSYMBOL (tail) != XSYMBOL (Qnil); tail = XCONS (tail)->cdr)
2594 proc = XCONS (XCONS (tail)->car)->cdr;
2595 p = XPROCESS (proc);
2596 if (XTYPE (p->pid) == Lisp_Int && XINT (p->pid) == -1)
2597 break;
2598 p = 0;
2601 /* Change the status of the process that was found. */
2602 if (p != 0)
2604 union { int i; WAITTYPE wt; } u;
2606 XSETINT (p->tick, ++process_tick);
2607 u.wt = w;
2608 XFASTINT (p->raw_status_low) = u.i & 0xffff;
2609 XFASTINT (p->raw_status_high) = u.i >> 16;
2611 /* If process has terminated, stop waiting for its output. */
2612 if (WIFSIGNALED (w) || WIFEXITED (w))
2613 if (p->infd)
2614 FD_CLR (p->infd, &input_wait_mask);
2617 /* There was no asynchronous process found for that id. Check
2618 if we have a synchronous process. */
2619 else
2621 synch_process_alive = 0;
2623 /* Report the status of the synchronous process. */
2624 if (WIFEXITED (w))
2625 synch_process_retcode = WRETCODE (w);
2626 else if (WIFSIGNALED (w))
2627 synch_process_death = sys_siglist[WTERMSIG (w)];
2630 /* On some systems, we must return right away.
2631 If any more processes want to signal us, we will
2632 get another signal.
2633 Otherwise (on systems that have WNOHANG), loop around
2634 to use up all the processes that have something to tell us. */
2635 #if defined (USG) && ! (defined (HPUX) && defined (WNOHANG))
2636 #ifdef USG
2637 signal (signo, sigchld_handler);
2638 #endif
2639 errno = old_errno;
2640 return;
2641 #endif /* USG, but not HPUX with WNOHANG */
2646 static Lisp_Object
2647 exec_sentinel_unwind (data)
2648 Lisp_Object data;
2650 XPROCESS (XCONS (data)->car)->sentinel = XCONS (data)->cdr;
2651 return Qnil;
2654 static void
2655 exec_sentinel (proc, reason)
2656 Lisp_Object proc, reason;
2658 Lisp_Object sentinel;
2659 register struct Lisp_Process *p = XPROCESS (proc);
2660 int count = specpdl_ptr - specpdl;
2662 sentinel = p->sentinel;
2663 if (NILP (sentinel))
2664 return;
2666 /* Zilch the sentinel while it's running, to avoid recursive invocations;
2667 assure that it gets restored no matter how the sentinel exits. */
2668 p->sentinel = Qnil;
2669 record_unwind_protect (exec_sentinel_unwind, Fcons (proc, sentinel));
2670 /* Inhibit quit so that random quits don't screw up a running filter. */
2671 specbind (Qinhibit_quit, Qt);
2672 call2 (sentinel, proc, reason);
2673 unbind_to (count);
2676 /* Report all recent events of a change in process status
2677 (either run the sentinel or output a message).
2678 This is done while Emacs is waiting for keyboard input. */
2680 status_notify ()
2682 register Lisp_Object proc, buffer;
2683 Lisp_Object tail = Qnil;
2684 Lisp_Object msg = Qnil;
2685 struct gcpro gcpro1, gcpro2;
2687 /* We need to gcpro tail; if read_process_output calls a filter
2688 which deletes a process and removes the cons to which tail points
2689 from Vprocess_alist, and then causes a GC, tail is an unprotected
2690 reference. */
2691 GCPRO2 (tail, msg);
2693 for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
2695 Lisp_Object symbol;
2696 register struct Lisp_Process *p;
2698 proc = Fcdr (Fcar (tail));
2699 p = XPROCESS (proc);
2701 if (XINT (p->tick) != XINT (p->update_tick))
2703 XSETINT (p->update_tick, XINT (p->tick));
2705 /* If process is still active, read any output that remains. */
2706 if (XFASTINT (p->infd))
2707 while (read_process_output (proc, XFASTINT (p->infd)) > 0);
2709 buffer = p->buffer;
2711 /* Get the text to use for the message. */
2712 if (!NILP (p->raw_status_low))
2713 update_status (p);
2714 msg = status_message (p->status);
2716 /* If process is terminated, deactivate it or delete it. */
2717 symbol = p->status;
2718 if (XTYPE (p->status) == Lisp_Cons)
2719 symbol = XCONS (p->status)->car;
2721 if (EQ (symbol, Qsignal) || EQ (symbol, Qexit)
2722 || EQ (symbol, Qclosed))
2724 if (delete_exited_processes)
2725 remove_process (proc);
2726 else
2727 deactivate_process (proc);
2730 /* Now output the message suitably. */
2731 if (!NILP (p->sentinel))
2732 exec_sentinel (proc, msg);
2733 /* Don't bother with a message in the buffer
2734 when a process becomes runnable. */
2735 else if (!EQ (symbol, Qrun) && !NILP (buffer))
2737 Lisp_Object ro = XBUFFER (buffer)->read_only;
2738 Lisp_Object tem;
2739 struct buffer *old = current_buffer;
2740 int opoint;
2742 /* Avoid error if buffer is deleted
2743 (probably that's why the process is dead, too) */
2744 if (NILP (XBUFFER (buffer)->name))
2745 continue;
2746 Fset_buffer (buffer);
2747 opoint = point;
2748 /* Insert new output into buffer
2749 at the current end-of-output marker,
2750 thus preserving logical ordering of input and output. */
2751 if (XMARKER (p->mark)->buffer)
2752 SET_PT (marker_position (p->mark));
2753 else
2754 SET_PT (ZV);
2755 if (point <= opoint)
2756 opoint += XSTRING (msg)->size + XSTRING (p->name)->size + 10;
2758 tem = current_buffer->read_only;
2759 current_buffer->read_only = Qnil;
2760 insert_string ("\nProcess ");
2761 Finsert (1, &p->name);
2762 insert_string (" ");
2763 Finsert (1, &msg);
2764 current_buffer->read_only = tem;
2765 Fset_marker (p->mark, make_number (point), p->buffer);
2767 SET_PT (opoint);
2768 set_buffer_internal (old);
2771 } /* end for */
2773 update_mode_lines++; /* in case buffers use %s in mode-line-format */
2774 redisplay_preserve_echo_area ();
2776 update_tick = process_tick;
2778 UNGCPRO;
2781 init_process ()
2783 register int i;
2785 #ifdef SIGCHLD
2786 #ifndef CANNOT_DUMP
2787 if (! noninteractive || initialized)
2788 #endif
2789 signal (SIGCHLD, sigchld_handler);
2790 #endif
2792 FD_ZERO (&input_wait_mask);
2793 FD_SET (0, &input_wait_mask);
2794 Vprocess_alist = Qnil;
2795 for (i = 0; i < MAXDESC; i++)
2797 chan_process[i] = Qnil;
2798 proc_buffered_char[i] = -1;
2801 #if 0
2802 DEFUN ("process-connection", Fprocess_connection, Sprocess_connection, 0, 1, 0,
2803 "Return the connection type of `PROCESS'. This can be nil (pipe),\n\
2804 t or pty (pty) or stream (socket connection).")
2805 (process)
2806 Lisp_Object process;
2808 return XPROCESS (process)->type;
2810 #endif
2811 syms_of_process ()
2813 #ifdef HAVE_PTYS
2814 pty_process = intern ("pty");
2815 #endif
2816 #ifdef HAVE_SOCKETS
2817 stream_process = intern ("stream");
2818 #endif
2819 Qprocessp = intern ("processp");
2820 staticpro (&Qprocessp);
2821 Qrun = intern ("run");
2822 staticpro (&Qrun);
2823 Qstop = intern ("stop");
2824 staticpro (&Qstop);
2825 Qsignal = intern ("signal");
2826 staticpro (&Qsignal);
2828 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
2829 here again.
2831 Qexit = intern ("exit");
2832 staticpro (&Qexit); */
2834 Qopen = intern ("open");
2835 staticpro (&Qopen);
2836 Qclosed = intern ("closed");
2837 staticpro (&Qclosed);
2839 staticpro (&Vprocess_alist);
2841 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
2842 "*Non-nil means delete processes immediately when they exit.\n\
2843 nil means don't delete them until `list-processes' is run.");
2845 delete_exited_processes = 1;
2847 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type,
2848 "Control type of device used to communicate with subprocesses.\n\
2849 Values are nil to use a pipe, and t or 'pty for a pty. Note that if\n\
2850 pty's are not available, this variable will be ignored. The value takes\n\
2851 effect when `start-process' is called.");
2852 Vprocess_connection_type = Qt;
2854 defsubr (&Sprocessp);
2855 defsubr (&Sget_process);
2856 defsubr (&Sget_buffer_process);
2857 defsubr (&Sdelete_process);
2858 defsubr (&Sprocess_status);
2859 defsubr (&Sprocess_exit_status);
2860 defsubr (&Sprocess_id);
2861 defsubr (&Sprocess_name);
2862 defsubr (&Sprocess_command);
2863 defsubr (&Sset_process_buffer);
2864 defsubr (&Sprocess_buffer);
2865 defsubr (&Sprocess_mark);
2866 defsubr (&Sset_process_filter);
2867 defsubr (&Sprocess_filter);
2868 defsubr (&Sset_process_sentinel);
2869 defsubr (&Sprocess_sentinel);
2870 defsubr (&Sprocess_kill_without_query);
2871 defsubr (&Slist_processes);
2872 defsubr (&Sprocess_list);
2873 defsubr (&Sstart_process);
2874 #ifdef HAVE_SOCKETS
2875 defsubr (&Sopen_network_stream);
2876 #endif /* HAVE_SOCKETS */
2877 defsubr (&Saccept_process_output);
2878 defsubr (&Sprocess_send_region);
2879 defsubr (&Sprocess_send_string);
2880 defsubr (&Sinterrupt_process);
2881 defsubr (&Skill_process);
2882 defsubr (&Squit_process);
2883 defsubr (&Sstop_process);
2884 defsubr (&Scontinue_process);
2885 defsubr (&Sprocess_send_eof);
2886 defsubr (&Ssignal_process);
2887 defsubr (&Swaiting_for_user_input_p);
2888 /* defsubr (&Sprocess_connection); */
2892 #else /* not subprocesses */
2894 #include <sys/types.h>
2895 #include <errno.h>
2897 #include "lisp.h"
2898 #include "systime.h"
2899 #include "termopts.h"
2901 extern int screen_garbaged;
2904 /* As described above, except assuming that there are no subprocesses:
2906 Wait for timeout to elapse and/or keyboard input to be available.
2908 time_limit is:
2909 timeout in seconds, or
2910 zero for no limit, or
2911 -1 means gobble data immediately available but don't wait for any.
2913 read_kbd is:
2914 0 to ignore keyboard input, or
2915 1 to return when input is available, or
2916 -1 means caller will actually read the input, so don't throw to
2917 the quit handler.
2918 We know that read_kbd will never be a Lisp_Process, since
2919 `subprocesses' isn't defined.
2921 do_display != 0 means redisplay should be done to show subprocess
2922 output that arrives. This version of the function ignores it.
2924 If read_kbd is a pointer to a struct Lisp_Process, then the
2925 function returns true iff we received input from that process
2926 before the timeout elapsed.
2927 Otherwise, return true iff we recieved input from any process. */
2930 wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
2931 int time_limit, microsecs, read_kbd, do_display;
2933 EMACS_TIME end_time, timeout, *timeout_p;
2934 int waitchannels;
2936 /* What does time_limit really mean? */
2937 if (time_limit || microsecs)
2939 /* It's not infinite. */
2940 timeout_p = &timeout;
2942 if (time_limit == -1)
2943 /* In fact, it's zero. */
2944 EMACS_SET_SECS_USECS (timeout, 0, 0);
2945 else
2946 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
2948 /* How far in the future is that? */
2949 EMACS_GET_TIME (end_time);
2950 EMACS_ADD_TIME (end_time, end_time, timeout);
2952 else
2953 /* It's infinite. */
2954 timeout_p = 0;
2956 /* Turn off periodic alarms (in case they are in use)
2957 because the select emulator uses alarms. */
2958 stop_polling ();
2960 for (;;)
2962 int nfds;
2964 waitchannels = read_kbd ? 1 : 0;
2966 /* If calling from keyboard input, do not quit
2967 since we want to return C-g as an input character.
2968 Otherwise, do pending quit if requested. */
2969 if (read_kbd >= 0)
2970 QUIT;
2972 if (timeout_p)
2974 EMACS_GET_TIME (*timeout_p);
2975 EMACS_SUB_TIME (*timeout_p, end_time, *timeout_p);
2976 if (EMACS_TIME_NEG_P (*timeout_p))
2977 break;
2980 /* Cause C-g and alarm signals to take immediate action,
2981 and cause input available signals to zero out timeout. */
2982 if (read_kbd < 0)
2983 set_waiting_for_input (&timeout);
2985 /* If a screen has been newly mapped and needs updating,
2986 reprocess its display stuff. */
2987 if (screen_garbaged)
2988 redisplay_preserve_echo_area ();
2990 if (read_kbd && detect_input_pending ())
2991 nfds = 0;
2992 else
2993 nfds = select (1, &waitchannels, 0, 0, timeout_p);
2995 /* Make C-g and alarm signals set flags again */
2996 clear_waiting_for_input ();
2998 /* If we woke up due to SIGWINCH, actually change size now. */
2999 do_pending_window_change ();
3001 if (nfds == -1)
3003 /* If the system call was interrupted, then go around the
3004 loop again. */
3005 if (errno == EINTR)
3006 waitchannels = 0;
3008 #ifdef sun
3009 else if (nfds > 0 && (waitchannels & 1) && interrupt_input)
3010 /* System sometimes fails to deliver SIGIO. */
3011 kill (getpid (), SIGIO);
3012 #endif
3013 if (read_kbd && interrupt_input && (waitchannels & 1))
3014 kill (0, SIGIO);
3016 /* If we have timed out (nfds == 0) or found some input (nfds > 0),
3017 we should exit. */
3018 if (nfds >= 0)
3019 break;
3022 return 0;
3026 DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
3027 "Return the (or, a) process associated with BUFFER.\n\
3028 This copy of Emacs has not been built to support subprocesses, so this\n\
3029 function always returns nil.")
3030 (name)
3031 register Lisp_Object name;
3033 return Qnil;
3036 /* Kill all processes associated with `buffer'.
3037 If `buffer' is nil, kill all processes.
3038 Since we have no subprocesses, this does nothing. */
3040 kill_buffer_processes (buffer)
3041 Lisp_Object buffer;
3045 init_process ()
3049 syms_of_process ()
3051 defsubr (&Sget_buffer_process);
3055 #endif /* not subprocesses */