Include blockinput.h.
[bpt/emacs.git] / src / process.c
1 /* Asynchronous subprocess control for GNU Emacs.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95, 1996
3 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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)
10 any later version.
11
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.
16
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. */
21
22
23 #include <signal.h>
24
25 #include <config.h>
26
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. */
33
34 \f
35 #ifdef subprocesses
36
37 #include <stdio.h>
38 #include <errno.h>
39 #include <setjmp.h>
40 #include <sys/types.h> /* some typedefs are used in sys/file.h */
41 #include <sys/file.h>
42 #include <sys/stat.h>
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46
47 #ifdef WINDOWSNT
48 #include <stdlib.h>
49 #include <fcntl.h>
50 #endif /* not WINDOWSNT */
51
52 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
53 #include <sys/socket.h>
54 #include <netdb.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 */
61
62 /* TERM is a poor-man's SLIP, used on Linux. */
63 #ifdef TERM
64 #include <client.h>
65 #endif
66
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)
71 #else
72 #define IN_ADDR unsigned long
73 #define NUMERIC_ADDR_ERROR (numeric_addr == -1)
74 #endif
75
76 #if defined(BSD_SYSTEM) || defined(STRIDE)
77 #include <sys/ioctl.h>
78 #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
79 #include <fcntl.h>
80 #endif /* HAVE_PTYS and no O_NDELAY */
81 #endif /* BSD_SYSTEM || STRIDE */
82
83 #ifdef BROKEN_O_NONBLOCK
84 #undef O_NONBLOCK
85 #endif /* BROKEN_O_NONBLOCK */
86
87 #ifdef NEED_BSDTTY
88 #include <bsdtty.h>
89 #endif
90
91 #ifdef IRIS
92 #include <sys/sysmacros.h> /* for "minor" */
93 #endif /* not IRIS */
94
95 #include "systime.h"
96 #include "systty.h"
97
98 #include "lisp.h"
99 #include "window.h"
100 #include "buffer.h"
101 #include "process.h"
102 #include "termhooks.h"
103 #include "termopts.h"
104 #include "commands.h"
105 #include "frame.h"
106 #include "blockinput.h"
107
108 Lisp_Object Qprocessp;
109 Lisp_Object Qrun, Qstop, Qsignal, Qopen, Qclosed;
110 Lisp_Object Qlast_nonmenu_event;
111 /* Qexit is declared and initialized in eval.c. */
112
113 /* a process object is a network connection when its childp field is neither
114 Qt nor Qnil but is instead a cons cell (HOSTNAME PORTNUM). */
115
116 #ifdef HAVE_SOCKETS
117 #define NETCONN_P(p) (GC_CONSP (XPROCESS (p)->childp))
118 #else
119 #define NETCONN_P(p) 0
120 #endif /* HAVE_SOCKETS */
121
122 /* Define first descriptor number available for subprocesses. */
123 #ifdef VMS
124 #define FIRST_PROC_DESC 1
125 #else /* Not VMS */
126 #define FIRST_PROC_DESC 3
127 #endif
128
129 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
130 testing SIGCHLD. */
131
132 #if !defined (SIGCHLD) && defined (SIGCLD)
133 #define SIGCHLD SIGCLD
134 #endif /* SIGCLD */
135
136 #include "syssignal.h"
137
138 #include "syswait.h"
139
140 extern int errno;
141 extern char *strerror ();
142 #ifdef VMS
143 extern char *sys_errlist[];
144 #endif
145
146 #ifndef HAVE_H_ERRNO
147 extern int h_errno;
148 #endif
149
150 #ifndef SYS_SIGLIST_DECLARED
151 #ifndef VMS
152 #ifndef BSD4_1
153 #ifndef WINDOWSNT
154 #ifndef LINUX
155 extern char *sys_siglist[];
156 #endif /* not LINUX */
157 #else /* BSD4_1 */
158 char *sys_siglist[] =
159 {
160 "bum signal!!",
161 "hangup",
162 "interrupt",
163 "quit",
164 "illegal instruction",
165 "trace trap",
166 "iot instruction",
167 "emt instruction",
168 "floating point exception",
169 "kill",
170 "bus error",
171 "segmentation violation",
172 "bad argument to system call",
173 "write on a pipe with no one to read it",
174 "alarm clock",
175 "software termination signal from kill",
176 "status signal",
177 "sendable stop signal not from tty",
178 "stop signal from tty",
179 "continue a stopped process",
180 "child status has changed",
181 "background read attempted from control tty",
182 "background write attempted from control tty",
183 "input record available at control tty",
184 "exceeded CPU time limit",
185 "exceeded file size limit"
186 };
187 #endif /* not WINDOWSNT */
188 #endif
189 #endif /* VMS */
190 #endif /* ! SYS_SIGLIST_DECLARED */
191
192 /* t means use pty, nil means use a pipe,
193 maybe other values to come. */
194 static Lisp_Object Vprocess_connection_type;
195
196 #ifdef SKTPAIR
197 #ifndef HAVE_SOCKETS
198 #include <sys/socket.h>
199 #endif
200 #endif /* SKTPAIR */
201
202 /* These next two vars are non-static since sysdep.c uses them in the
203 emulation of `select'. */
204 /* Number of events of change of status of a process. */
205 int process_tick;
206 /* Number of events for which the user or sentinel has been notified. */
207 int update_tick;
208
209 #include "sysselect.h"
210
211 /* If we support a window system, turn on the code to poll periodically
212 to detect C-g. It isn't actually used when doing interrupt input. */
213 #ifdef HAVE_WINDOW_SYSTEM
214 #define POLL_FOR_INPUT
215 #endif
216
217 /* Mask of bits indicating the descriptors that we wait for input on. */
218
219 static SELECT_TYPE input_wait_mask;
220
221 /* Mask that excludes keyboard input descriptor (s). */
222
223 static SELECT_TYPE non_keyboard_wait_mask;
224
225 /* The largest descriptor currently in use for a process object. */
226 static int max_process_desc;
227
228 /* The largest descriptor currently in use for keyboard input. */
229 static int max_keyboard_desc;
230
231 /* Nonzero means delete a process right away if it exits. */
232 static int delete_exited_processes;
233
234 /* Indexed by descriptor, gives the process (if any) for that descriptor */
235 Lisp_Object chan_process[MAXDESC];
236
237 /* Alist of elements (NAME . PROCESS) */
238 Lisp_Object Vprocess_alist;
239
240 /* Buffered-ahead input char from process, indexed by channel.
241 -1 means empty (no char is buffered).
242 Used on sys V where the only way to tell if there is any
243 output from the process is to read at least one char.
244 Always -1 on systems that support FIONREAD. */
245
246 /* Don't make static; need to access externally. */
247 int proc_buffered_char[MAXDESC];
248
249 static Lisp_Object get_process ();
250
251 extern EMACS_TIME timer_check ();
252 extern int timers_run;
253
254 /* Maximum number of bytes to send to a pty without an eof. */
255 static int pty_max_bytes;
256
257 #ifdef HAVE_PTYS
258 /* The file name of the pty opened by allocate_pty. */
259
260 static char pty_name[24];
261 #endif
262 \f
263 /* Compute the Lisp form of the process status, p->status, from
264 the numeric status that was returned by `wait'. */
265
266 Lisp_Object status_convert ();
267
268 update_status (p)
269 struct Lisp_Process *p;
270 {
271 union { int i; WAITTYPE wt; } u;
272 u.i = XFASTINT (p->raw_status_low) + (XFASTINT (p->raw_status_high) << 16);
273 p->status = status_convert (u.wt);
274 p->raw_status_low = Qnil;
275 p->raw_status_high = Qnil;
276 }
277
278 /* Convert a process status word in Unix format to
279 the list that we use internally. */
280
281 Lisp_Object
282 status_convert (w)
283 WAITTYPE w;
284 {
285 if (WIFSTOPPED (w))
286 return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil));
287 else if (WIFEXITED (w))
288 return Fcons (Qexit, Fcons (make_number (WRETCODE (w)),
289 WCOREDUMP (w) ? Qt : Qnil));
290 else if (WIFSIGNALED (w))
291 return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)),
292 WCOREDUMP (w) ? Qt : Qnil));
293 else
294 return Qrun;
295 }
296
297 /* Given a status-list, extract the three pieces of information
298 and store them individually through the three pointers. */
299
300 void
301 decode_status (l, symbol, code, coredump)
302 Lisp_Object l;
303 Lisp_Object *symbol;
304 int *code;
305 int *coredump;
306 {
307 Lisp_Object tem;
308
309 if (SYMBOLP (l))
310 {
311 *symbol = l;
312 *code = 0;
313 *coredump = 0;
314 }
315 else
316 {
317 *symbol = XCONS (l)->car;
318 tem = XCONS (l)->cdr;
319 *code = XFASTINT (XCONS (tem)->car);
320 tem = XCONS (tem)->cdr;
321 *coredump = !NILP (tem);
322 }
323 }
324
325 /* Return a string describing a process status list. */
326
327 Lisp_Object
328 status_message (status)
329 Lisp_Object status;
330 {
331 Lisp_Object symbol;
332 int code, coredump;
333 Lisp_Object string, string2;
334
335 decode_status (status, &symbol, &code, &coredump);
336
337 if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
338 {
339 char *signame = 0;
340 if (code < NSIG)
341 {
342 #ifndef VMS
343 /* Cast to suppress warning if the table has const char *. */
344 signame = (char *) sys_siglist[code];
345 #else
346 signame = sys_errlist[code];
347 #endif
348 }
349 if (signame == 0)
350 signame = "unknown";
351 string = build_string (signame);
352 string2 = build_string (coredump ? " (core dumped)\n" : "\n");
353 XSTRING (string)->data[0] = DOWNCASE (XSTRING (string)->data[0]);
354 return concat2 (string, string2);
355 }
356 else if (EQ (symbol, Qexit))
357 {
358 if (code == 0)
359 return build_string ("finished\n");
360 string = Fnumber_to_string (make_number (code));
361 string2 = build_string (coredump ? " (core dumped)\n" : "\n");
362 return concat2 (build_string ("exited abnormally with code "),
363 concat2 (string, string2));
364 }
365 else
366 return Fcopy_sequence (Fsymbol_name (symbol));
367 }
368 \f
369 #ifdef HAVE_PTYS
370
371 /* Open an available pty, returning a file descriptor.
372 Return -1 on failure.
373 The file name of the terminal corresponding to the pty
374 is left in the variable pty_name. */
375
376 int
377 allocate_pty ()
378 {
379 struct stat stb;
380 register c, i;
381 int fd;
382
383 /* Some systems name their pseudoterminals so that there are gaps in
384 the usual sequence - for example, on HP9000/S700 systems, there
385 are no pseudoterminals with names ending in 'f'. So we wait for
386 three failures in a row before deciding that we've reached the
387 end of the ptys. */
388 int failed_count = 0;
389
390 #ifdef PTY_ITERATION
391 PTY_ITERATION
392 #else
393 for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
394 for (i = 0; i < 16; i++)
395 #endif
396 {
397 #ifdef PTY_NAME_SPRINTF
398 PTY_NAME_SPRINTF
399 #else
400 sprintf (pty_name, "/dev/pty%c%x", c, i);
401 #endif /* no PTY_NAME_SPRINTF */
402
403 #ifdef PTY_OPEN
404 PTY_OPEN;
405 #else /* no PTY_OPEN */
406 #ifdef IRIS
407 /* Unusual IRIS code */
408 *ptyv = open ("/dev/ptc", O_RDWR | O_NDELAY, 0);
409 if (fd < 0)
410 return -1;
411 if (fstat (fd, &stb) < 0)
412 return -1;
413 #else /* not IRIS */
414 if (stat (pty_name, &stb) < 0)
415 {
416 failed_count++;
417 if (failed_count >= 3)
418 return -1;
419 }
420 else
421 failed_count = 0;
422 #ifdef O_NONBLOCK
423 fd = open (pty_name, O_RDWR | O_NONBLOCK, 0);
424 #else
425 fd = open (pty_name, O_RDWR | O_NDELAY, 0);
426 #endif
427 #endif /* not IRIS */
428 #endif /* no PTY_OPEN */
429
430 if (fd >= 0)
431 {
432 /* check to make certain that both sides are available
433 this avoids a nasty yet stupid bug in rlogins */
434 #ifdef PTY_TTY_NAME_SPRINTF
435 PTY_TTY_NAME_SPRINTF
436 #else
437 sprintf (pty_name, "/dev/tty%c%x", c, i);
438 #endif /* no PTY_TTY_NAME_SPRINTF */
439 #ifndef UNIPLUS
440 if (access (pty_name, 6) != 0)
441 {
442 close (fd);
443 #if !defined(IRIS) && !defined(__sgi)
444 continue;
445 #else
446 return -1;
447 #endif /* IRIS */
448 }
449 #endif /* not UNIPLUS */
450 setup_pty (fd);
451 return fd;
452 }
453 }
454 return -1;
455 }
456 #endif /* HAVE_PTYS */
457 \f
458 Lisp_Object
459 make_process (name)
460 Lisp_Object name;
461 {
462 struct Lisp_Vector *vec;
463 register Lisp_Object val, tem, name1;
464 register struct Lisp_Process *p;
465 char suffix[10];
466 register int i;
467
468 vec = allocate_vectorlike ((EMACS_INT) VECSIZE (struct Lisp_Process));
469 for (i = 0; i < VECSIZE (struct Lisp_Process); i++)
470 vec->contents[i] = Qnil;
471 vec->size = VECSIZE (struct Lisp_Process);
472 p = (struct Lisp_Process *)vec;
473
474 XSETINT (p->infd, -1);
475 XSETINT (p->outfd, -1);
476 XSETFASTINT (p->pid, 0);
477 XSETFASTINT (p->tick, 0);
478 XSETFASTINT (p->update_tick, 0);
479 p->raw_status_low = Qnil;
480 p->raw_status_high = Qnil;
481 p->status = Qrun;
482 p->mark = Fmake_marker ();
483
484 /* If name is already in use, modify it until it is unused. */
485
486 name1 = name;
487 for (i = 1; ; i++)
488 {
489 tem = Fget_process (name1);
490 if (NILP (tem)) break;
491 sprintf (suffix, "<%d>", i);
492 name1 = concat2 (name, build_string (suffix));
493 }
494 name = name1;
495 p->name = name;
496 XSETPROCESS (val, p);
497 Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
498 return val;
499 }
500
501 remove_process (proc)
502 register Lisp_Object proc;
503 {
504 register Lisp_Object pair;
505
506 pair = Frassq (proc, Vprocess_alist);
507 Vprocess_alist = Fdelq (pair, Vprocess_alist);
508
509 deactivate_process (proc);
510 }
511 \f
512 DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0,
513 "Return t if OBJECT is a process.")
514 (object)
515 Lisp_Object object;
516 {
517 return PROCESSP (object) ? Qt : Qnil;
518 }
519
520 DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0,
521 "Return the process named NAME, or nil if there is none.")
522 (name)
523 register Lisp_Object name;
524 {
525 if (PROCESSP (name))
526 return name;
527 CHECK_STRING (name, 0);
528 return Fcdr (Fassoc (name, Vprocess_alist));
529 }
530
531 DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
532 "Return the (or, a) process associated with BUFFER.\n\
533 BUFFER may be a buffer or the name of one.")
534 (buffer)
535 register Lisp_Object buffer;
536 {
537 register Lisp_Object buf, tail, proc;
538
539 if (NILP (buffer)) return Qnil;
540 buf = Fget_buffer (buffer);
541 if (NILP (buf)) return Qnil;
542
543 for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
544 {
545 proc = Fcdr (Fcar (tail));
546 if (PROCESSP (proc) && EQ (XPROCESS (proc)->buffer, buf))
547 return proc;
548 }
549 return Qnil;
550 }
551
552 /* This is how commands for the user decode process arguments. It
553 accepts a process, a process name, a buffer, a buffer name, or nil.
554 Buffers denote the first process in the buffer, and nil denotes the
555 current buffer. */
556
557 static Lisp_Object
558 get_process (name)
559 register Lisp_Object name;
560 {
561 register Lisp_Object proc, obj;
562 if (STRINGP (name))
563 {
564 obj = Fget_process (name);
565 if (NILP (obj))
566 obj = Fget_buffer (name);
567 if (NILP (obj))
568 error ("Process %s does not exist", XSTRING (name)->data);
569 }
570 else if (NILP (name))
571 obj = Fcurrent_buffer ();
572 else
573 obj = name;
574
575 /* Now obj should be either a buffer object or a process object.
576 */
577 if (BUFFERP (obj))
578 {
579 proc = Fget_buffer_process (obj);
580 if (NILP (proc))
581 error ("Buffer %s has no process", XSTRING (XBUFFER (obj)->name)->data);
582 }
583 else
584 {
585 CHECK_PROCESS (obj, 0);
586 proc = obj;
587 }
588 return proc;
589 }
590
591 DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0,
592 "Delete PROCESS: kill it and forget about it immediately.\n\
593 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
594 nil, indicating the current buffer's process.")
595 (process)
596 register Lisp_Object process;
597 {
598 process = get_process (process);
599 XPROCESS (process)->raw_status_low = Qnil;
600 XPROCESS (process)->raw_status_high = Qnil;
601 if (NETCONN_P (process))
602 {
603 XPROCESS (process)->status = Fcons (Qexit, Fcons (make_number (0), Qnil));
604 XSETINT (XPROCESS (process)->tick, ++process_tick);
605 }
606 else if (XINT (XPROCESS (process)->infd) >= 0)
607 {
608 Fkill_process (process, Qnil);
609 /* Do this now, since remove_process will make sigchld_handler do nothing. */
610 XPROCESS (process)->status
611 = Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil));
612 XSETINT (XPROCESS (process)->tick, ++process_tick);
613 status_notify ();
614 }
615 remove_process (process);
616 return Qnil;
617 }
618 \f
619 DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0,
620 "Return the status of PROCESS: a symbol, one of these:\n\
621 run -- for a process that is running.\n\
622 stop -- for a process stopped but continuable.\n\
623 exit -- for a process that has exited.\n\
624 signal -- for a process that has got a fatal signal.\n\
625 open -- for a network stream connection that is open.\n\
626 closed -- for a network stream connection that is closed.\n\
627 nil -- if arg is a process name and no such process exists.\n\
628 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
629 nil, indicating the current buffer's process.")
630 (process)
631 register Lisp_Object process;
632 {
633 register struct Lisp_Process *p;
634 register Lisp_Object status;
635
636 if (STRINGP (process))
637 process = Fget_process (process);
638 else
639 process = get_process (process);
640
641 if (NILP (process))
642 return process;
643
644 p = XPROCESS (process);
645 if (!NILP (p->raw_status_low))
646 update_status (p);
647 status = p->status;
648 if (CONSP (status))
649 status = XCONS (status)->car;
650 if (NETCONN_P (process))
651 {
652 if (EQ (status, Qrun))
653 status = Qopen;
654 else if (EQ (status, Qexit))
655 status = Qclosed;
656 }
657 return status;
658 }
659
660 DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status,
661 1, 1, 0,
662 "Return the exit status of PROCESS or the signal number that killed it.\n\
663 If PROCESS has not yet exited or died, return 0.")
664 (process)
665 register Lisp_Object process;
666 {
667 CHECK_PROCESS (process, 0);
668 if (!NILP (XPROCESS (process)->raw_status_low))
669 update_status (XPROCESS (process));
670 if (CONSP (XPROCESS (process)->status))
671 return XCONS (XCONS (XPROCESS (process)->status)->cdr)->car;
672 return make_number (0);
673 }
674
675 DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0,
676 "Return the process id of PROCESS.\n\
677 This is the pid of the Unix process which PROCESS uses or talks to.\n\
678 For a network connection, this value is nil.")
679 (process)
680 register Lisp_Object process;
681 {
682 CHECK_PROCESS (process, 0);
683 return XPROCESS (process)->pid;
684 }
685
686 DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0,
687 "Return the name of PROCESS, as a string.\n\
688 This is the name of the program invoked in PROCESS,\n\
689 possibly modified to make it unique among process names.")
690 (process)
691 register Lisp_Object process;
692 {
693 CHECK_PROCESS (process, 0);
694 return XPROCESS (process)->name;
695 }
696
697 DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0,
698 "Return the command that was executed to start PROCESS.\n\
699 This is a list of strings, the first string being the program executed\n\
700 and the rest of the strings being the arguments given to it.\n\
701 For a non-child channel, this is nil.")
702 (process)
703 register Lisp_Object process;
704 {
705 CHECK_PROCESS (process, 0);
706 return XPROCESS (process)->command;
707 }
708
709 DEFUN ("process-tty-name", Fprocess_tty_name, Sprocess_tty_name, 1, 1, 0,
710 "Return the name of the terminal PROCESS uses, or nil if none.\n\
711 This is the terminal that the process itself reads and writes on,\n\
712 not the name of the pty that Emacs uses to talk with that terminal.")
713 (process)
714 register Lisp_Object process;
715 {
716 CHECK_PROCESS (process, 0);
717 return XPROCESS (process)->tty_name;
718 }
719
720 DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer,
721 2, 2, 0,
722 "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
723 (process, buffer)
724 register Lisp_Object process, buffer;
725 {
726 CHECK_PROCESS (process, 0);
727 if (!NILP (buffer))
728 CHECK_BUFFER (buffer, 1);
729 XPROCESS (process)->buffer = buffer;
730 return buffer;
731 }
732
733 DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer,
734 1, 1, 0,
735 "Return the buffer PROCESS is associated with.\n\
736 Output from PROCESS is inserted in this buffer\n\
737 unless PROCESS has a filter.")
738 (process)
739 register Lisp_Object process;
740 {
741 CHECK_PROCESS (process, 0);
742 return XPROCESS (process)->buffer;
743 }
744
745 DEFUN ("process-mark", Fprocess_mark, Sprocess_mark,
746 1, 1, 0,
747 "Return the marker for the end of the last output from PROCESS.")
748 (process)
749 register Lisp_Object process;
750 {
751 CHECK_PROCESS (process, 0);
752 return XPROCESS (process)->mark;
753 }
754
755 DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter,
756 2, 2, 0,
757 "Give PROCESS the filter function FILTER; nil means no filter.\n\
758 t means stop accepting output from the process.\n\
759 When a process has a filter, each time it does output\n\
760 the entire string of output is passed to the filter.\n\
761 The filter gets two arguments: the process and the string of output.\n\
762 If the process has a filter, its buffer is not used for output.")
763 (process, filter)
764 register Lisp_Object process, filter;
765 {
766 CHECK_PROCESS (process, 0);
767 if (EQ (filter, Qt))
768 {
769 FD_CLR (XINT (XPROCESS (process)->infd), &input_wait_mask);
770 FD_CLR (XINT (XPROCESS (process)->infd), &non_keyboard_wait_mask);
771 }
772 else if (EQ (XPROCESS (process)->filter, Qt))
773 {
774 FD_SET (XINT (XPROCESS (process)->infd), &input_wait_mask);
775 FD_SET (XINT (XPROCESS (process)->infd), &non_keyboard_wait_mask);
776 }
777 XPROCESS (process)->filter = filter;
778 return filter;
779 }
780
781 DEFUN ("process-filter", Fprocess_filter, Sprocess_filter,
782 1, 1, 0,
783 "Returns the filter function of PROCESS; nil if none.\n\
784 See `set-process-filter' for more info on filter functions.")
785 (process)
786 register Lisp_Object process;
787 {
788 CHECK_PROCESS (process, 0);
789 return XPROCESS (process)->filter;
790 }
791
792 DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel,
793 2, 2, 0,
794 "Give PROCESS the sentinel SENTINEL; nil for none.\n\
795 The sentinel is called as a function when the process changes state.\n\
796 It gets two arguments: the process, and a string describing the change.")
797 (process, sentinel)
798 register Lisp_Object process, sentinel;
799 {
800 CHECK_PROCESS (process, 0);
801 XPROCESS (process)->sentinel = sentinel;
802 return sentinel;
803 }
804
805 DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
806 1, 1, 0,
807 "Return the sentinel of PROCESS; nil if none.\n\
808 See `set-process-sentinel' for more info on sentinels.")
809 (process)
810 register Lisp_Object process;
811 {
812 CHECK_PROCESS (process, 0);
813 return XPROCESS (process)->sentinel;
814 }
815
816 DEFUN ("set-process-window-size", Fset_process_window_size,
817 Sset_process_window_size, 3, 3, 0,
818 "Tell PROCESS that it has logical window size HEIGHT and WIDTH.")
819 (process, height, width)
820 register Lisp_Object process, height, width;
821 {
822 CHECK_PROCESS (process, 0);
823 CHECK_NATNUM (height, 0);
824 CHECK_NATNUM (width, 0);
825 if (set_window_size (XINT (XPROCESS (process)->infd),
826 XINT (height), XINT(width)) <= 0)
827 return Qnil;
828 else
829 return Qt;
830 }
831
832 DEFUN ("process-kill-without-query", Fprocess_kill_without_query,
833 Sprocess_kill_without_query, 1, 2, 0,
834 "Say no query needed if PROCESS is running when Emacs is exited.\n\
835 Optional second argument if non-nil says to require a query.\n\
836 Value is t if a query was formerly required.")
837 (process, value)
838 register Lisp_Object process, value;
839 {
840 Lisp_Object tem;
841
842 CHECK_PROCESS (process, 0);
843 tem = XPROCESS (process)->kill_without_query;
844 XPROCESS (process)->kill_without_query = Fnull (value);
845
846 return Fnull (tem);
847 }
848
849 DEFUN ("process-contact", Fprocess_contact, Sprocess_contact,
850 1, 1, 0,
851 "Return the contact info of PROCESS; t for a real child.\n\
852 For a net connection, the value is a cons cell of the form (HOST SERVICE).")
853 (process)
854 register Lisp_Object process;
855 {
856 CHECK_PROCESS (process, 0);
857 return XPROCESS (process)->childp;
858 }
859
860 #if 0 /* Turned off because we don't currently record this info
861 in the process. Perhaps add it. */
862 DEFUN ("process-connection", Fprocess_connection, Sprocess_connection, 1, 1, 0,
863 "Return the connection type of `PROCESS'.\n\
864 The value is `nil' for a pipe,\n\
865 `t' or `pty' for a pty, or `stream' for a socket connection.")
866 (process)
867 Lisp_Object process;
868 {
869 return XPROCESS (process)->type;
870 }
871 #endif
872 \f
873 Lisp_Object
874 list_processes_1 ()
875 {
876 register Lisp_Object tail, tem;
877 Lisp_Object proc, minspace, tem1;
878 register struct buffer *old = current_buffer;
879 register struct Lisp_Process *p;
880 register int state;
881 char tembuf[80];
882
883 XSETFASTINT (minspace, 1);
884
885 set_buffer_internal (XBUFFER (Vstandard_output));
886 Fbuffer_disable_undo (Vstandard_output);
887
888 current_buffer->truncate_lines = Qt;
889
890 write_string ("\
891 Proc Status Buffer Tty Command\n\
892 ---- ------ ------ --- -------\n", -1);
893
894 for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
895 {
896 Lisp_Object symbol;
897
898 proc = Fcdr (Fcar (tail));
899 p = XPROCESS (proc);
900 if (NILP (p->childp))
901 continue;
902
903 Finsert (1, &p->name);
904 Findent_to (make_number (13), minspace);
905
906 if (!NILP (p->raw_status_low))
907 update_status (p);
908 symbol = p->status;
909 if (CONSP (p->status))
910 symbol = XCONS (p->status)->car;
911
912
913 if (EQ (symbol, Qsignal))
914 {
915 Lisp_Object tem;
916 tem = Fcar (Fcdr (p->status));
917 #ifdef VMS
918 if (XINT (tem) < NSIG)
919 write_string (sys_errlist [XINT (tem)], -1);
920 else
921 #endif
922 Fprinc (symbol, Qnil);
923 }
924 else if (NETCONN_P (proc))
925 {
926 if (EQ (symbol, Qrun))
927 write_string ("open", -1);
928 else if (EQ (symbol, Qexit))
929 write_string ("closed", -1);
930 else
931 Fprinc (symbol, Qnil);
932 }
933 else
934 Fprinc (symbol, Qnil);
935
936 if (EQ (symbol, Qexit))
937 {
938 Lisp_Object tem;
939 tem = Fcar (Fcdr (p->status));
940 if (XFASTINT (tem))
941 {
942 sprintf (tembuf, " %d", (int) XFASTINT (tem));
943 write_string (tembuf, -1);
944 }
945 }
946
947 if (EQ (symbol, Qsignal) || EQ (symbol, Qexit))
948 remove_process (proc);
949
950 Findent_to (make_number (22), minspace);
951 if (NILP (p->buffer))
952 insert_string ("(none)");
953 else if (NILP (XBUFFER (p->buffer)->name))
954 insert_string ("(Killed)");
955 else
956 Finsert (1, &XBUFFER (p->buffer)->name);
957
958 Findent_to (make_number (37), minspace);
959
960 if (STRINGP (p->tty_name))
961 Finsert (1, &p->tty_name);
962 else
963 insert_string ("(none)");
964
965 Findent_to (make_number (49), minspace);
966
967 if (NETCONN_P (proc))
968 {
969 sprintf (tembuf, "(network stream connection to %s)\n",
970 XSTRING (XCONS (p->childp)->car)->data);
971 insert_string (tembuf);
972 }
973 else
974 {
975 tem = p->command;
976 while (1)
977 {
978 tem1 = Fcar (tem);
979 Finsert (1, &tem1);
980 tem = Fcdr (tem);
981 if (NILP (tem))
982 break;
983 insert_string (" ");
984 }
985 insert_string ("\n");
986 }
987 }
988 return Qnil;
989 }
990
991 DEFUN ("list-processes", Flist_processes, Slist_processes, 0, 0, "",
992 "Display a list of all processes.\n\
993 \(Any processes listed as Exited or Signaled are actually eliminated\n\
994 after the listing is made.)")
995 ()
996 {
997 internal_with_output_to_temp_buffer ("*Process List*",
998 list_processes_1, Qnil);
999 return Qnil;
1000 }
1001
1002 DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
1003 "Return a list of all processes.")
1004 ()
1005 {
1006 return Fmapcar (Qcdr, Vprocess_alist);
1007 }
1008 \f
1009 /* Starting asynchronous inferior processes. */
1010
1011 static Lisp_Object start_process_unwind ();
1012
1013 DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0,
1014 "Start a program in a subprocess. Return the process object for it.\n\
1015 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
1016 NAME is name for process. It is modified if necessary to make it unique.\n\
1017 BUFFER is the buffer or (buffer-name) to associate with the process.\n\
1018 Process output goes at end of that buffer, unless you specify\n\
1019 an output stream or filter function to handle the output.\n\
1020 BUFFER may be also nil, meaning that this process is not associated\n\
1021 with any buffer\n\
1022 Third arg is program file name. It is searched for as in the shell.\n\
1023 Remaining arguments are strings to give program as arguments.")
1024 (nargs, args)
1025 int nargs;
1026 register Lisp_Object *args;
1027 {
1028 Lisp_Object buffer, name, program, proc, current_dir, tem;
1029 #ifdef VMS
1030 register unsigned char *new_argv;
1031 int len;
1032 #else
1033 register unsigned char **new_argv;
1034 #endif
1035 register int i;
1036 int count = specpdl_ptr - specpdl;
1037
1038 buffer = args[1];
1039 if (!NILP (buffer))
1040 buffer = Fget_buffer_create (buffer);
1041
1042 /* Make sure that the child will be able to chdir to the current
1043 buffer's current directory, or its unhandled equivalent. We
1044 can't just have the child check for an error when it does the
1045 chdir, since it's in a vfork.
1046
1047 We have to GCPRO around this because Fexpand_file_name and
1048 Funhandled_file_name_directory might call a file name handling
1049 function. The argument list is protected by the caller, so all
1050 we really have to worry about is buffer. */
1051 {
1052 struct gcpro gcpro1, gcpro2;
1053
1054 current_dir = current_buffer->directory;
1055
1056 GCPRO2 (buffer, current_dir);
1057
1058 current_dir
1059 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
1060 Qnil);
1061 if (NILP (Ffile_accessible_directory_p (current_dir)))
1062 report_file_error ("Setting current directory",
1063 Fcons (current_buffer->directory, Qnil));
1064
1065 UNGCPRO;
1066 }
1067
1068 name = args[0];
1069 CHECK_STRING (name, 0);
1070
1071 program = args[2];
1072
1073 CHECK_STRING (program, 2);
1074
1075 #ifdef VMS
1076 /* Make a one member argv with all args concatenated
1077 together separated by a blank. */
1078 len = XSTRING (program)->size + 2;
1079 for (i = 3; i < nargs; i++)
1080 {
1081 tem = args[i];
1082 CHECK_STRING (tem, i);
1083 len += XSTRING (tem)->size + 1; /* count the blank */
1084 }
1085 new_argv = (unsigned char *) alloca (len);
1086 strcpy (new_argv, XSTRING (program)->data);
1087 for (i = 3; i < nargs; i++)
1088 {
1089 tem = args[i];
1090 CHECK_STRING (tem, i);
1091 strcat (new_argv, " ");
1092 strcat (new_argv, XSTRING (tem)->data);
1093 }
1094 /* Need to add code here to check for program existence on VMS */
1095
1096 #else /* not VMS */
1097 new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
1098
1099 /* If program file name is not absolute, search our path for it */
1100 if (!IS_DIRECTORY_SEP (XSTRING (program)->data[0])
1101 && !(XSTRING (program)->size > 1
1102 && IS_DEVICE_SEP (XSTRING (program)->data[1])))
1103 {
1104 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1105
1106 tem = Qnil;
1107 GCPRO4 (name, program, buffer, current_dir);
1108 openp (Vexec_path, program, EXEC_SUFFIXES, &tem, 1);
1109 UNGCPRO;
1110 if (NILP (tem))
1111 report_file_error ("Searching for program", Fcons (program, Qnil));
1112 tem = Fexpand_file_name (tem, Qnil);
1113 new_argv[0] = XSTRING (tem)->data;
1114 }
1115 else
1116 {
1117 if (!NILP (Ffile_directory_p (program)))
1118 error ("Specified program for new process is a directory");
1119
1120 new_argv[0] = XSTRING (program)->data;
1121 }
1122
1123 for (i = 3; i < nargs; i++)
1124 {
1125 tem = args[i];
1126 CHECK_STRING (tem, i);
1127 new_argv[i - 2] = XSTRING (tem)->data;
1128 }
1129 new_argv[i - 2] = 0;
1130 #endif /* not VMS */
1131
1132 proc = make_process (name);
1133 /* If an error occurs and we can't start the process, we want to
1134 remove it from the process list. This means that each error
1135 check in create_process doesn't need to call remove_process
1136 itself; it's all taken care of here. */
1137 record_unwind_protect (start_process_unwind, proc);
1138
1139 XPROCESS (proc)->childp = Qt;
1140 XPROCESS (proc)->command_channel_p = Qnil;
1141 XPROCESS (proc)->buffer = buffer;
1142 XPROCESS (proc)->sentinel = Qnil;
1143 XPROCESS (proc)->filter = Qnil;
1144 XPROCESS (proc)->command = Flist (nargs - 2, args + 2);
1145
1146 /* Make the process marker point into the process buffer (if any). */
1147 if (!NILP (buffer))
1148 Fset_marker (XPROCESS (proc)->mark,
1149 make_number (BUF_ZV (XBUFFER (buffer))), buffer);
1150
1151 create_process (proc, new_argv, current_dir);
1152
1153 return unbind_to (count, proc);
1154 }
1155
1156 /* This function is the unwind_protect form for Fstart_process. If
1157 PROC doesn't have its pid set, then we know someone has signaled
1158 an error and the process wasn't started successfully, so we should
1159 remove it from the process list. */
1160 static Lisp_Object
1161 start_process_unwind (proc)
1162 Lisp_Object proc;
1163 {
1164 if (!PROCESSP (proc))
1165 abort ();
1166
1167 /* Was PROC started successfully? */
1168 if (XINT (XPROCESS (proc)->pid) <= 0)
1169 remove_process (proc);
1170
1171 return Qnil;
1172 }
1173
1174
1175 SIGTYPE
1176 create_process_1 (signo)
1177 int signo;
1178 {
1179 #if defined (USG) && !defined (POSIX_SIGNALS)
1180 /* USG systems forget handlers when they are used;
1181 must reestablish each time */
1182 signal (signo, create_process_1);
1183 #endif /* USG */
1184 }
1185
1186 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1187 #ifdef USG
1188 #ifdef SIGCHLD
1189 /* Mimic blocking of signals on system V, which doesn't really have it. */
1190
1191 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1192 int sigchld_deferred;
1193
1194 SIGTYPE
1195 create_process_sigchld ()
1196 {
1197 signal (SIGCHLD, create_process_sigchld);
1198
1199 sigchld_deferred = 1;
1200 }
1201 #endif
1202 #endif
1203 #endif
1204
1205 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1206 create_process (process, new_argv, current_dir)
1207 Lisp_Object process;
1208 char **new_argv;
1209 Lisp_Object current_dir;
1210 {
1211 int pid, inchannel, outchannel;
1212 int sv[2];
1213 #ifdef POSIX_SIGNALS
1214 sigset_t procmask;
1215 sigset_t blocked;
1216 struct sigaction sigint_action;
1217 struct sigaction sigquit_action;
1218 #ifdef AIX
1219 struct sigaction sighup_action;
1220 #endif
1221 #else /* !POSIX_SIGNALS */
1222 #ifdef SIGCHLD
1223 SIGTYPE (*sigchld)();
1224 #endif
1225 #endif /* !POSIX_SIGNALS */
1226 /* Use volatile to protect variables from being clobbered by longjmp. */
1227 volatile int forkin, forkout;
1228 volatile int pty_flag = 0;
1229 extern char **environ;
1230
1231 inchannel = outchannel = -1;
1232
1233 #ifdef HAVE_PTYS
1234 if (!NILP (Vprocess_connection_type))
1235 outchannel = inchannel = allocate_pty ();
1236
1237 if (inchannel >= 0)
1238 {
1239 #ifndef USG
1240 /* On USG systems it does not work to open the pty's tty here
1241 and then close and reopen it in the child. */
1242 #ifdef O_NOCTTY
1243 /* Don't let this terminal become our controlling terminal
1244 (in case we don't have one). */
1245 forkout = forkin = open (pty_name, O_RDWR | O_NOCTTY, 0);
1246 #else
1247 forkout = forkin = open (pty_name, O_RDWR, 0);
1248 #endif
1249 if (forkin < 0)
1250 report_file_error ("Opening pty", Qnil);
1251 #else
1252 forkin = forkout = -1;
1253 #endif /* not USG */
1254 pty_flag = 1;
1255 }
1256 else
1257 #endif /* HAVE_PTYS */
1258 #ifdef SKTPAIR
1259 {
1260 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) < 0)
1261 report_file_error ("Opening socketpair", Qnil);
1262 outchannel = inchannel = sv[0];
1263 forkout = forkin = sv[1];
1264 }
1265 #else /* not SKTPAIR */
1266 {
1267 pipe (sv);
1268 inchannel = sv[0];
1269 forkout = sv[1];
1270 pipe (sv);
1271 outchannel = sv[1];
1272 forkin = sv[0];
1273 }
1274 #endif /* not SKTPAIR */
1275
1276 #if 0
1277 /* Replaced by close_process_descs */
1278 set_exclusive_use (inchannel);
1279 set_exclusive_use (outchannel);
1280 #endif
1281
1282 /* Stride people say it's a mystery why this is needed
1283 as well as the O_NDELAY, but that it fails without this. */
1284 #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
1285 {
1286 int one = 1;
1287 ioctl (inchannel, FIONBIO, &one);
1288 }
1289 #endif
1290
1291 #ifdef O_NONBLOCK
1292 fcntl (inchannel, F_SETFL, O_NONBLOCK);
1293 fcntl (outchannel, F_SETFL, O_NONBLOCK);
1294 #else
1295 #ifdef O_NDELAY
1296 fcntl (inchannel, F_SETFL, O_NDELAY);
1297 fcntl (outchannel, F_SETFL, O_NDELAY);
1298 #endif
1299 #endif
1300
1301 /* Record this as an active process, with its channels.
1302 As a result, child_setup will close Emacs's side of the pipes. */
1303 chan_process[inchannel] = process;
1304 XSETINT (XPROCESS (process)->infd, inchannel);
1305 XSETINT (XPROCESS (process)->outfd, outchannel);
1306 /* Record the tty descriptor used in the subprocess. */
1307 if (forkin < 0)
1308 XPROCESS (process)->subtty = Qnil;
1309 else
1310 XSETFASTINT (XPROCESS (process)->subtty, forkin);
1311 XPROCESS (process)->pty_flag = (pty_flag ? Qt : Qnil);
1312 XPROCESS (process)->status = Qrun;
1313
1314 /* Delay interrupts until we have a chance to store
1315 the new fork's pid in its process structure */
1316 #ifdef POSIX_SIGNALS
1317 sigemptyset (&blocked);
1318 #ifdef SIGCHLD
1319 sigaddset (&blocked, SIGCHLD);
1320 #endif
1321 #ifdef HAVE_VFORK
1322 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1323 this sets the parent's signal handlers as well as the child's.
1324 So delay all interrupts whose handlers the child might munge,
1325 and record the current handlers so they can be restored later. */
1326 sigaddset (&blocked, SIGINT ); sigaction (SIGINT , 0, &sigint_action );
1327 sigaddset (&blocked, SIGQUIT); sigaction (SIGQUIT, 0, &sigquit_action);
1328 #ifdef AIX
1329 sigaddset (&blocked, SIGHUP ); sigaction (SIGHUP , 0, &sighup_action );
1330 #endif
1331 #endif /* HAVE_VFORK */
1332 sigprocmask (SIG_BLOCK, &blocked, &procmask);
1333 #else /* !POSIX_SIGNALS */
1334 #ifdef SIGCHLD
1335 #ifdef BSD4_1
1336 sighold (SIGCHLD);
1337 #else /* not BSD4_1 */
1338 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
1339 sigsetmask (sigmask (SIGCHLD));
1340 #else /* ordinary USG */
1341 #if 0
1342 sigchld_deferred = 0;
1343 sigchld = signal (SIGCHLD, create_process_sigchld);
1344 #endif
1345 #endif /* ordinary USG */
1346 #endif /* not BSD4_1 */
1347 #endif /* SIGCHLD */
1348 #endif /* !POSIX_SIGNALS */
1349
1350 FD_SET (inchannel, &input_wait_mask);
1351 FD_SET (inchannel, &non_keyboard_wait_mask);
1352 if (inchannel > max_process_desc)
1353 max_process_desc = inchannel;
1354
1355 /* Until we store the proper pid, enable sigchld_handler
1356 to recognize an unknown pid as standing for this process.
1357 It is very important not to let this `marker' value stay
1358 in the table after this function has returned; if it does
1359 it might cause call-process to hang and subsequent asynchronous
1360 processes to get their return values scrambled. */
1361 XSETINT (XPROCESS (process)->pid, -1);
1362
1363 BLOCK_INPUT;
1364
1365 {
1366 /* child_setup must clobber environ on systems with true vfork.
1367 Protect it from permanent change. */
1368 char **save_environ = environ;
1369
1370 #ifndef WINDOWSNT
1371 pid = vfork ();
1372 if (pid == 0)
1373 #endif /* not WINDOWSNT */
1374 {
1375 int xforkin = forkin;
1376 int xforkout = forkout;
1377
1378 #if 0 /* This was probably a mistake--it duplicates code later on,
1379 but fails to handle all the cases. */
1380 /* Make sure SIGCHLD is not blocked in the child. */
1381 sigsetmask (SIGEMPTYMASK);
1382 #endif
1383
1384 /* Make the pty be the controlling terminal of the process. */
1385 #ifdef HAVE_PTYS
1386 /* First, disconnect its current controlling terminal. */
1387 #ifdef HAVE_SETSID
1388 /* We tried doing setsid only if pty_flag, but it caused
1389 process_set_signal to fail on SGI when using a pipe. */
1390 setsid ();
1391 /* Make the pty's terminal the controlling terminal. */
1392 if (pty_flag)
1393 {
1394 #ifdef TIOCSCTTY
1395 /* We ignore the return value
1396 because faith@cs.unc.edu says that is necessary on Linux. */
1397 ioctl (xforkin, TIOCSCTTY, 0);
1398 #endif
1399 }
1400 #else /* not HAVE_SETSID */
1401 #ifdef USG
1402 /* It's very important to call setpgrp here and no time
1403 afterwards. Otherwise, we lose our controlling tty which
1404 is set when we open the pty. */
1405 setpgrp ();
1406 #endif /* USG */
1407 #endif /* not HAVE_SETSID */
1408 #if defined (HAVE_TERMIOS) && defined (LDISC1)
1409 if (pty_flag && xforkin >= 0)
1410 {
1411 struct termios t;
1412 tcgetattr (xforkin, &t);
1413 t.c_lflag = LDISC1;
1414 if (tcsetattr (xforkin, TCSANOW, &t) < 0)
1415 write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
1416 }
1417 #else
1418 #if defined (NTTYDISC) && defined (TIOCSETD)
1419 if (pty_flag && xforkin >= 0)
1420 {
1421 /* Use new line discipline. */
1422 int ldisc = NTTYDISC;
1423 ioctl (xforkin, TIOCSETD, &ldisc);
1424 }
1425 #endif
1426 #endif
1427 #ifdef TIOCNOTTY
1428 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1429 can do TIOCSPGRP only to the process's controlling tty. */
1430 if (pty_flag)
1431 {
1432 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1433 I can't test it since I don't have 4.3. */
1434 int j = open ("/dev/tty", O_RDWR, 0);
1435 ioctl (j, TIOCNOTTY, 0);
1436 close (j);
1437 #ifndef USG
1438 /* In order to get a controlling terminal on some versions
1439 of BSD, it is necessary to put the process in pgrp 0
1440 before it opens the terminal. */
1441 #ifdef HAVE_SETPGID
1442 setpgid (0, 0);
1443 #else
1444 setpgrp (0, 0);
1445 #endif
1446 #endif
1447 }
1448 #endif /* TIOCNOTTY */
1449
1450 #if !defined (RTU) && !defined (UNIPLUS) && !defined (DONT_REOPEN_PTY)
1451 /*** There is a suggestion that this ought to be a
1452 conditional on TIOCSPGRP,
1453 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
1454 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
1455 that system does seem to need this code, even though
1456 both HAVE_SETSID and TIOCSCTTY are defined. */
1457 /* Now close the pty (if we had it open) and reopen it.
1458 This makes the pty the controlling terminal of the subprocess. */
1459 if (pty_flag)
1460 {
1461 #ifdef SET_CHILD_PTY_PGRP
1462 int pgrp = getpid ();
1463 #endif
1464
1465 /* I wonder if close (open (pty_name, ...)) would work? */
1466 if (xforkin >= 0)
1467 close (xforkin);
1468 xforkout = xforkin = open (pty_name, O_RDWR, 0);
1469
1470 if (xforkin < 0)
1471 {
1472 write (1, "Couldn't open the pty terminal ", 31);
1473 write (1, pty_name, strlen (pty_name));
1474 write (1, "\n", 1);
1475 _exit (1);
1476 }
1477
1478 #ifdef SET_CHILD_PTY_PGRP
1479 ioctl (xforkin, TIOCSPGRP, &pgrp);
1480 ioctl (xforkout, TIOCSPGRP, &pgrp);
1481 #endif
1482 }
1483 #endif /* not UNIPLUS and not RTU and not DONT_REOPEN_PTY */
1484
1485 #ifdef SETUP_SLAVE_PTY
1486 if (pty_flag)
1487 {
1488 SETUP_SLAVE_PTY;
1489 }
1490 #endif /* SETUP_SLAVE_PTY */
1491 #ifdef AIX
1492 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
1493 Now reenable it in the child, so it will die when we want it to. */
1494 if (pty_flag)
1495 signal (SIGHUP, SIG_DFL);
1496 #endif
1497 #endif /* HAVE_PTYS */
1498
1499 signal (SIGINT, SIG_DFL);
1500 signal (SIGQUIT, SIG_DFL);
1501
1502 /* Stop blocking signals in the child. */
1503 #ifdef POSIX_SIGNALS
1504 sigprocmask (SIG_SETMASK, &procmask, 0);
1505 #else /* !POSIX_SIGNALS */
1506 #ifdef SIGCHLD
1507 #ifdef BSD4_1
1508 sigrelse (SIGCHLD);
1509 #else /* not BSD4_1 */
1510 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
1511 sigsetmask (SIGEMPTYMASK);
1512 #else /* ordinary USG */
1513 #if 0
1514 signal (SIGCHLD, sigchld);
1515 #endif
1516 #endif /* ordinary USG */
1517 #endif /* not BSD4_1 */
1518 #endif /* SIGCHLD */
1519 #endif /* !POSIX_SIGNALS */
1520
1521 if (pty_flag)
1522 child_setup_tty (xforkout);
1523 #ifdef WINDOWSNT
1524 pid = child_setup (xforkin, xforkout, xforkout,
1525 new_argv, 1, current_dir);
1526 #else /* not WINDOWSNT */
1527 child_setup (xforkin, xforkout, xforkout,
1528 new_argv, 1, current_dir);
1529 #endif /* not WINDOWSNT */
1530 }
1531 environ = save_environ;
1532 }
1533
1534 UNBLOCK_INPUT;
1535
1536 /* This runs in the Emacs process. */
1537 if (pid < 0)
1538 {
1539 if (forkin >= 0)
1540 close (forkin);
1541 if (forkin != forkout && forkout >= 0)
1542 close (forkout);
1543 }
1544 else
1545 {
1546 /* vfork succeeded. */
1547 XSETFASTINT (XPROCESS (process)->pid, pid);
1548
1549 #ifdef WINDOWSNT
1550 register_child (pid, inchannel);
1551 #endif /* WINDOWSNT */
1552
1553 /* If the subfork execv fails, and it exits,
1554 this close hangs. I don't know why.
1555 So have an interrupt jar it loose. */
1556 stop_polling ();
1557 signal (SIGALRM, create_process_1);
1558 alarm (1);
1559 XPROCESS (process)->subtty = Qnil;
1560 if (forkin >= 0)
1561 close (forkin);
1562 alarm (0);
1563 start_polling ();
1564 if (forkin != forkout && forkout >= 0)
1565 close (forkout);
1566
1567 #ifdef HAVE_PTYS
1568 if (pty_flag)
1569 XPROCESS (process)->tty_name = build_string (pty_name);
1570 else
1571 #endif
1572 XPROCESS (process)->tty_name = Qnil;
1573 }
1574
1575 /* Restore the signal state whether vfork succeeded or not.
1576 (We will signal an error, below, if it failed.) */
1577 #ifdef POSIX_SIGNALS
1578 #ifdef HAVE_VFORK
1579 /* Restore the parent's signal handlers. */
1580 sigaction (SIGINT, &sigint_action, 0);
1581 sigaction (SIGQUIT, &sigquit_action, 0);
1582 #ifdef AIX
1583 sigaction (SIGHUP, &sighup_action, 0);
1584 #endif
1585 #endif /* HAVE_VFORK */
1586 /* Stop blocking signals in the parent. */
1587 sigprocmask (SIG_SETMASK, &procmask, 0);
1588 #else /* !POSIX_SIGNALS */
1589 #ifdef SIGCHLD
1590 #ifdef BSD4_1
1591 sigrelse (SIGCHLD);
1592 #else /* not BSD4_1 */
1593 #if defined (BSD_SYSTEM) || defined (UNIPLUS) || defined (HPUX)
1594 sigsetmask (SIGEMPTYMASK);
1595 #else /* ordinary USG */
1596 #if 0
1597 signal (SIGCHLD, sigchld);
1598 /* Now really handle any of these signals
1599 that came in during this function. */
1600 if (sigchld_deferred)
1601 kill (getpid (), SIGCHLD);
1602 #endif
1603 #endif /* ordinary USG */
1604 #endif /* not BSD4_1 */
1605 #endif /* SIGCHLD */
1606 #endif /* !POSIX_SIGNALS */
1607
1608 /* Now generate the error if vfork failed. */
1609 if (pid < 0)
1610 report_file_error ("Doing vfork", Qnil);
1611 }
1612 #endif /* not VMS */
1613
1614 #ifdef HAVE_SOCKETS
1615
1616 /* open a TCP network connection to a given HOST/SERVICE. Treated
1617 exactly like a normal process when reading and writing. Only
1618 differences are in status display and process deletion. A network
1619 connection has no PID; you cannot signal it. All you can do is
1620 deactivate and close it via delete-process */
1621
1622 DEFUN ("open-network-stream", Fopen_network_stream, Sopen_network_stream,
1623 4, 4, 0,
1624 "Open a TCP connection for a service to a host.\n\
1625 Returns a subprocess-object to represent the connection.\n\
1626 Input and output work as for subprocesses; `delete-process' closes it.\n\
1627 Args are NAME BUFFER HOST SERVICE.\n\
1628 NAME is name for process. It is modified if necessary to make it unique.\n\
1629 BUFFER is the buffer (or buffer-name) to associate with the process.\n\
1630 Process output goes at end of that buffer, unless you specify\n\
1631 an output stream or filter function to handle the output.\n\
1632 BUFFER may be also nil, meaning that this process is not associated\n\
1633 with any buffer\n\
1634 Third arg is name of the host to connect to, or its IP address.\n\
1635 Fourth arg SERVICE is name of the service desired, or an integer\n\
1636 specifying a port number to connect to.")
1637 (name, buffer, host, service)
1638 Lisp_Object name, buffer, host, service;
1639 {
1640 Lisp_Object proc;
1641 register int i;
1642 struct sockaddr_in address;
1643 struct servent *svc_info;
1644 struct hostent *host_info_ptr, host_info;
1645 char *(addr_list[2]);
1646 IN_ADDR numeric_addr;
1647 int s, outch, inch;
1648 char errstring[80];
1649 int port;
1650 struct hostent host_info_fixed;
1651 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1652 int retry = 0;
1653 int count = specpdl_ptr - specpdl;
1654
1655 #ifdef WINDOWSNT
1656 /* Ensure socket support is loaded if available. */
1657 init_winsock (TRUE);
1658 #endif
1659
1660 GCPRO4 (name, buffer, host, service);
1661 CHECK_STRING (name, 0);
1662 CHECK_STRING (host, 0);
1663 if (INTEGERP (service))
1664 port = htons ((unsigned short) XINT (service));
1665 else
1666 {
1667 CHECK_STRING (service, 0);
1668 svc_info = getservbyname (XSTRING (service)->data, "tcp");
1669 if (svc_info == 0)
1670 error ("Unknown service \"%s\"", XSTRING (service)->data);
1671 port = svc_info->s_port;
1672 }
1673
1674 /* Slow down polling to every ten seconds.
1675 Some kernels have a bug which causes retrying connect to fail
1676 after a connect. Polling can interfere with gethostbyname too. */
1677 #ifdef POLL_FOR_INPUT
1678 bind_polling_period (10);
1679 #endif
1680
1681 #ifndef TERM
1682 while (1)
1683 {
1684 #ifdef TRY_AGAIN
1685 h_errno = 0;
1686 #endif
1687 immediate_quit = 1;
1688 QUIT;
1689 host_info_ptr = gethostbyname (XSTRING (host)->data);
1690 immediate_quit = 0;
1691 #ifdef TRY_AGAIN
1692 if (! (host_info_ptr == 0 && h_errno == TRY_AGAIN))
1693 #endif
1694 break;
1695 Fsleep_for (make_number (1), Qnil);
1696 }
1697 if (host_info_ptr == 0)
1698 /* Attempt to interpret host as numeric inet address */
1699 {
1700 numeric_addr = inet_addr ((char *) XSTRING (host)->data);
1701 if (NUMERIC_ADDR_ERROR)
1702 error ("Unknown host \"%s\"", XSTRING (host)->data);
1703
1704 host_info_ptr = &host_info;
1705 host_info.h_name = 0;
1706 host_info.h_aliases = 0;
1707 host_info.h_addrtype = AF_INET;
1708 #ifdef h_addr
1709 /* Older machines have only one address slot called h_addr.
1710 Newer machines have h_addr_list, but #define h_addr to
1711 be its first element. */
1712 host_info.h_addr_list = &(addr_list[0]);
1713 #endif
1714 host_info.h_addr = (char*)(&numeric_addr);
1715 addr_list[1] = 0;
1716 /* numeric_addr isn't null-terminated; it has fixed length. */
1717 host_info.h_length = sizeof (numeric_addr);
1718 }
1719
1720 bzero (&address, sizeof address);
1721 bcopy (host_info_ptr->h_addr, (char *) &address.sin_addr,
1722 host_info_ptr->h_length);
1723 address.sin_family = host_info_ptr->h_addrtype;
1724 address.sin_port = port;
1725
1726 s = socket (host_info_ptr->h_addrtype, SOCK_STREAM, 0);
1727 if (s < 0)
1728 report_file_error ("error creating socket", Fcons (name, Qnil));
1729
1730 /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
1731 when connect is interrupted. So let's not let it get interrupted.
1732 Note we do not turn off polling, because polling is only used
1733 when not interrupt_input, and thus not normally used on the systems
1734 which have this bug. On systems which use polling, there's no way
1735 to quit if polling is turned off. */
1736 if (interrupt_input)
1737 unrequest_sigio ();
1738
1739 loop:
1740
1741 immediate_quit = 1;
1742 QUIT;
1743
1744 if (connect (s, (struct sockaddr *) &address, sizeof address) == -1
1745 && errno != EISCONN)
1746 {
1747 int xerrno = errno;
1748
1749 immediate_quit = 0;
1750
1751 if (errno == EINTR)
1752 goto loop;
1753 if (errno == EADDRINUSE && retry < 20)
1754 {
1755 /* A delay here is needed on some FreeBSD systems,
1756 and it is harmless, since this retrying takes time anyway
1757 and should be infrequent. */
1758 Fsleep_for (make_number (1), Qnil);
1759 retry++;
1760 goto loop;
1761 }
1762
1763 close (s);
1764
1765 if (interrupt_input)
1766 request_sigio ();
1767
1768 errno = xerrno;
1769 report_file_error ("connection failed",
1770 Fcons (host, Fcons (name, Qnil)));
1771 }
1772
1773 immediate_quit = 0;
1774
1775 #ifdef POLL_FOR_INPUT
1776 unbind_to (count, Qnil);
1777 #endif
1778
1779 if (interrupt_input)
1780 request_sigio ();
1781
1782 #else /* TERM */
1783 s = connect_server (0);
1784 if (s < 0)
1785 report_file_error ("error creating socket", Fcons (name, Qnil));
1786 send_command (s, C_PORT, 0, "%s:%d", XSTRING (host)->data, ntohs (port));
1787 send_command (s, C_DUMB, 1, 0);
1788 #endif /* TERM */
1789
1790 inch = s;
1791 outch = dup (s);
1792 if (outch < 0)
1793 report_file_error ("error duplicating socket", Fcons (name, Qnil));
1794
1795 if (!NILP (buffer))
1796 buffer = Fget_buffer_create (buffer);
1797 proc = make_process (name);
1798
1799 chan_process[inch] = proc;
1800
1801 #ifdef O_NONBLOCK
1802 fcntl (inch, F_SETFL, O_NONBLOCK);
1803 #else
1804 #ifdef O_NDELAY
1805 fcntl (inch, F_SETFL, O_NDELAY);
1806 #endif
1807 #endif
1808
1809 XPROCESS (proc)->childp = Fcons (host, Fcons (service, Qnil));
1810 XPROCESS (proc)->command_channel_p = Qnil;
1811 XPROCESS (proc)->buffer = buffer;
1812 XPROCESS (proc)->sentinel = Qnil;
1813 XPROCESS (proc)->filter = Qnil;
1814 XPROCESS (proc)->command = Qnil;
1815 XPROCESS (proc)->pid = Qnil;
1816 XSETINT (XPROCESS (proc)->infd, inch);
1817 XSETINT (XPROCESS (proc)->outfd, outch);
1818 XPROCESS (proc)->status = Qrun;
1819 FD_SET (inch, &input_wait_mask);
1820 FD_SET (inch, &non_keyboard_wait_mask);
1821 if (inch > max_process_desc)
1822 max_process_desc = inch;
1823
1824 UNGCPRO;
1825 return proc;
1826 }
1827 #endif /* HAVE_SOCKETS */
1828
1829 deactivate_process (proc)
1830 Lisp_Object proc;
1831 {
1832 register int inchannel, outchannel;
1833 register struct Lisp_Process *p = XPROCESS (proc);
1834
1835 inchannel = XINT (p->infd);
1836 outchannel = XINT (p->outfd);
1837
1838 if (inchannel >= 0)
1839 {
1840 /* Beware SIGCHLD hereabouts. */
1841 flush_pending_output (inchannel);
1842 #ifdef VMS
1843 {
1844 VMS_PROC_STUFF *get_vms_process_pointer (), *vs;
1845 sys$dassgn (outchannel);
1846 vs = get_vms_process_pointer (p->pid);
1847 if (vs)
1848 give_back_vms_process_stuff (vs);
1849 }
1850 #else
1851 close (inchannel);
1852 if (outchannel >= 0 && outchannel != inchannel)
1853 close (outchannel);
1854 #endif
1855
1856 XSETINT (p->infd, -1);
1857 XSETINT (p->outfd, -1);
1858 chan_process[inchannel] = Qnil;
1859 FD_CLR (inchannel, &input_wait_mask);
1860 FD_CLR (inchannel, &non_keyboard_wait_mask);
1861 if (inchannel == max_process_desc)
1862 {
1863 int i;
1864 /* We just closed the highest-numbered process input descriptor,
1865 so recompute the highest-numbered one now. */
1866 max_process_desc = 0;
1867 for (i = 0; i < MAXDESC; i++)
1868 if (!NILP (chan_process[i]))
1869 max_process_desc = i;
1870 }
1871 }
1872 }
1873
1874 /* Close all descriptors currently in use for communication
1875 with subprocess. This is used in a newly-forked subprocess
1876 to get rid of irrelevant descriptors. */
1877
1878 close_process_descs ()
1879 {
1880 #ifndef WINDOWSNT
1881 int i;
1882 for (i = 0; i < MAXDESC; i++)
1883 {
1884 Lisp_Object process;
1885 process = chan_process[i];
1886 if (!NILP (process))
1887 {
1888 int in = XINT (XPROCESS (process)->infd);
1889 int out = XINT (XPROCESS (process)->outfd);
1890 if (in >= 0)
1891 close (in);
1892 if (out >= 0 && in != out)
1893 close (out);
1894 }
1895 }
1896 #endif
1897 }
1898 \f
1899 DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output,
1900 0, 3, 0,
1901 "Allow any pending output from subprocesses to be read by Emacs.\n\
1902 It is read into the process' buffers or given to their filter functions.\n\
1903 Non-nil arg PROCESS means do not return until some output has been received\n\
1904 from PROCESS.\n\
1905 Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of\n\
1906 seconds and microseconds to wait; return after that much time whether\n\
1907 or not there is input.\n\
1908 Return non-nil iff we received any output before the timeout expired.")
1909 (process, timeout, timeout_msecs)
1910 register Lisp_Object process, timeout, timeout_msecs;
1911 {
1912 int seconds;
1913 int useconds;
1914
1915 if (! NILP (timeout_msecs))
1916 {
1917 CHECK_NUMBER (timeout_msecs, 2);
1918 useconds = XINT (timeout_msecs);
1919 if (!INTEGERP (timeout))
1920 XSETINT (timeout, 0);
1921
1922 {
1923 int carry = useconds / 1000000;
1924
1925 XSETINT (timeout, XINT (timeout) + carry);
1926 useconds -= carry * 1000000;
1927
1928 /* I think this clause is necessary because C doesn't
1929 guarantee a particular rounding direction for negative
1930 integers. */
1931 if (useconds < 0)
1932 {
1933 XSETINT (timeout, XINT (timeout) - 1);
1934 useconds += 1000000;
1935 }
1936 }
1937 }
1938 else
1939 useconds = 0;
1940
1941 if (! NILP (timeout))
1942 {
1943 CHECK_NUMBER (timeout, 1);
1944 seconds = XINT (timeout);
1945 if (seconds < 0 || (seconds == 0 && useconds == 0))
1946 seconds = -1;
1947 }
1948 else
1949 {
1950 if (NILP (process))
1951 seconds = -1;
1952 else
1953 seconds = 0;
1954 }
1955
1956 if (NILP (process))
1957 XSETFASTINT (process, 0);
1958
1959 return
1960 (wait_reading_process_input (seconds, useconds, process, 0)
1961 ? Qt : Qnil);
1962 }
1963
1964 /* This variable is different from waiting_for_input in keyboard.c.
1965 It is used to communicate to a lisp process-filter/sentinel (via the
1966 function Fwaiting_for_user_input_p below) whether emacs was waiting
1967 for user-input when that process-filter was called.
1968 waiting_for_input cannot be used as that is by definition 0 when
1969 lisp code is being evalled.
1970 This is also used in record_asynch_buffer_change.
1971 For that purpose, this must be 0
1972 when not inside wait_reading_process_input. */
1973 static int waiting_for_user_input_p;
1974
1975 /* This is here so breakpoints can be put on it. */
1976 static
1977 wait_reading_process_input_1 ()
1978 {
1979 }
1980
1981 /* Read and dispose of subprocess output while waiting for timeout to
1982 elapse and/or keyboard input to be available.
1983
1984 TIME_LIMIT is:
1985 timeout in seconds, or
1986 zero for no limit, or
1987 -1 means gobble data immediately available but don't wait for any.
1988
1989 MICROSECS is:
1990 an additional duration to wait, measured in microseconds.
1991 If this is nonzero and time_limit is 0, then the timeout
1992 consists of MICROSECS only.
1993
1994 READ_KBD is a lisp value:
1995 0 to ignore keyboard input, or
1996 1 to return when input is available, or
1997 -1 meaning caller will actually read the input, so don't throw to
1998 the quit handler, or
1999 a cons cell, meaning wait until its car is non-nil
2000 (and gobble terminal input into the buffer if any arrives), or
2001 a process object, meaning wait until something arrives from that
2002 process. The return value is true iff we read some input from
2003 that process.
2004
2005 DO_DISPLAY != 0 means redisplay should be done to show subprocess
2006 output that arrives.
2007
2008 If READ_KBD is a pointer to a struct Lisp_Process, then the
2009 function returns true iff we received input from that process
2010 before the timeout elapsed.
2011 Otherwise, return true iff we received input from any process. */
2012
2013 wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
2014 int time_limit, microsecs;
2015 Lisp_Object read_kbd;
2016 int do_display;
2017 {
2018 register int channel, nfds, m;
2019 static SELECT_TYPE Available;
2020 int xerrno;
2021 Lisp_Object proc;
2022 EMACS_TIME timeout, end_time, garbage;
2023 SELECT_TYPE Atemp;
2024 int wait_channel = -1;
2025 struct Lisp_Process *wait_proc = 0;
2026 int got_some_input = 0;
2027 Lisp_Object *wait_for_cell = 0;
2028
2029 FD_ZERO (&Available);
2030
2031 /* If read_kbd is a process to watch, set wait_proc and wait_channel
2032 accordingly. */
2033 if (PROCESSP (read_kbd))
2034 {
2035 wait_proc = XPROCESS (read_kbd);
2036 wait_channel = XINT (wait_proc->infd);
2037 XSETFASTINT (read_kbd, 0);
2038 }
2039
2040 /* If waiting for non-nil in a cell, record where. */
2041 if (CONSP (read_kbd))
2042 {
2043 wait_for_cell = &XCONS (read_kbd)->car;
2044 XSETFASTINT (read_kbd, 0);
2045 }
2046
2047 waiting_for_user_input_p = XINT (read_kbd);
2048
2049 /* Since we may need to wait several times,
2050 compute the absolute time to return at. */
2051 if (time_limit || microsecs)
2052 {
2053 EMACS_GET_TIME (end_time);
2054 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
2055 EMACS_ADD_TIME (end_time, end_time, timeout);
2056 }
2057 #ifdef hpux
2058 /* AlainF 5-Jul-1996
2059 HP-UX 10.10 seem to have problems with signals coming in
2060 Causes "poll: interrupted system call" messages when Emacs is run
2061 in an X window
2062 Turn off periodic alarms (in case they are in use) */
2063 stop_polling ();
2064 #endif
2065
2066 while (1)
2067 {
2068 int timeout_reduced_for_timers = 0;
2069
2070 /* If calling from keyboard input, do not quit
2071 since we want to return C-g as an input character.
2072 Otherwise, do pending quit if requested. */
2073 if (XINT (read_kbd) >= 0)
2074 QUIT;
2075
2076 /* Exit now if the cell we're waiting for became non-nil. */
2077 if (wait_for_cell && ! NILP (*wait_for_cell))
2078 break;
2079
2080 /* Compute time from now till when time limit is up */
2081 /* Exit if already run out */
2082 if (time_limit == -1)
2083 {
2084 /* -1 specified for timeout means
2085 gobble output available now
2086 but don't wait at all. */
2087
2088 EMACS_SET_SECS_USECS (timeout, 0, 0);
2089 }
2090 else if (time_limit || microsecs)
2091 {
2092 EMACS_GET_TIME (timeout);
2093 EMACS_SUB_TIME (timeout, end_time, timeout);
2094 if (EMACS_TIME_NEG_P (timeout))
2095 break;
2096 }
2097 else
2098 {
2099 EMACS_SET_SECS_USECS (timeout, 100000, 0);
2100 }
2101
2102 /* Normally we run timers here.
2103 But not if wait_for_cell; in those cases,
2104 the wait is supposed to be short,
2105 and those callers cannot handle running arbitrary Lisp code here. */
2106 if (! wait_for_cell)
2107 {
2108 EMACS_TIME timer_delay;
2109 int old_timers_run;
2110
2111 retry:
2112 old_timers_run = timers_run;
2113 timer_delay = timer_check (1);
2114 if (timers_run != old_timers_run && do_display)
2115 {
2116 redisplay_preserve_echo_area ();
2117 /* We must retry, since a timer may have requeued itself
2118 and that could alter the time_delay. */
2119 goto retry;
2120 }
2121
2122 if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1)
2123 {
2124 EMACS_TIME difference;
2125 EMACS_SUB_TIME (difference, timer_delay, timeout);
2126 if (EMACS_TIME_NEG_P (difference))
2127 {
2128 timeout = timer_delay;
2129 timeout_reduced_for_timers = 1;
2130 }
2131 }
2132 /* If time_limit is -1, we are not going to wait at all. */
2133 else if (time_limit != -1)
2134 {
2135 /* This is so a breakpoint can be put here. */
2136 wait_reading_process_input_1 ();
2137 }
2138 }
2139
2140 /* Cause C-g and alarm signals to take immediate action,
2141 and cause input available signals to zero out timeout.
2142
2143 It is important that we do this before checking for process
2144 activity. If we get a SIGCHLD after the explicit checks for
2145 process activity, timeout is the only way we will know. */
2146 if (XINT (read_kbd) < 0)
2147 set_waiting_for_input (&timeout);
2148
2149 /* If status of something has changed, and no input is
2150 available, notify the user of the change right away. After
2151 this explicit check, we'll let the SIGCHLD handler zap
2152 timeout to get our attention. */
2153 if (update_tick != process_tick && do_display)
2154 {
2155 Atemp = input_wait_mask;
2156 EMACS_SET_SECS_USECS (timeout, 0, 0);
2157 if ((select (MAXDESC, &Atemp, (SELECT_TYPE *)0, (SELECT_TYPE *)0,
2158 &timeout)
2159 <= 0))
2160 {
2161 /* It's okay for us to do this and then continue with
2162 the loop, since timeout has already been zeroed out. */
2163 clear_waiting_for_input ();
2164 status_notify ();
2165 }
2166 }
2167
2168 /* Don't wait for output from a non-running process. */
2169 if (wait_proc != 0 && !NILP (wait_proc->raw_status_low))
2170 update_status (wait_proc);
2171 if (wait_proc != 0
2172 && ! EQ (wait_proc->status, Qrun))
2173 {
2174 clear_waiting_for_input ();
2175 break;
2176 }
2177
2178 /* Wait till there is something to do */
2179
2180 if (! XINT (read_kbd) && wait_for_cell == 0)
2181 Available = non_keyboard_wait_mask;
2182 else
2183 Available = input_wait_mask;
2184
2185 /* If frame size has changed or the window is newly mapped,
2186 redisplay now, before we start to wait. There is a race
2187 condition here; if a SIGIO arrives between now and the select
2188 and indicates that a frame is trashed, the select may block
2189 displaying a trashed screen. */
2190 if (frame_garbaged && do_display)
2191 {
2192 clear_waiting_for_input ();
2193 redisplay_preserve_echo_area ();
2194 if (XINT (read_kbd) < 0)
2195 set_waiting_for_input (&timeout);
2196 }
2197
2198 if (XINT (read_kbd) && detect_input_pending ())
2199 {
2200 nfds = 0;
2201 FD_ZERO (&Available);
2202 }
2203 else
2204 nfds = select (MAXDESC, &Available, (SELECT_TYPE *)0, (SELECT_TYPE *)0,
2205 &timeout);
2206
2207 xerrno = errno;
2208
2209 /* Make C-g and alarm signals set flags again */
2210 clear_waiting_for_input ();
2211
2212 /* If we woke up due to SIGWINCH, actually change size now. */
2213 do_pending_window_change ();
2214
2215 if (time_limit && nfds == 0 && ! timeout_reduced_for_timers)
2216 /* We wanted the full specified time, so return now. */
2217 break;
2218 if (nfds < 0)
2219 {
2220 if (xerrno == EINTR)
2221 FD_ZERO (&Available);
2222 #ifdef ultrix
2223 /* Ultrix select seems to return ENOMEM when it is
2224 interrupted. Treat it just like EINTR. Bleah. Note
2225 that we want to test for the "ultrix" CPP symbol, not
2226 "__ultrix__"; the latter is only defined under GCC, but
2227 not by DEC's bundled CC. -JimB */
2228 else if (xerrno == ENOMEM)
2229 FD_ZERO (&Available);
2230 #endif
2231 #ifdef ALLIANT
2232 /* This happens for no known reason on ALLIANT.
2233 I am guessing that this is the right response. -- RMS. */
2234 else if (xerrno == EFAULT)
2235 FD_ZERO (&Available);
2236 #endif
2237 else if (xerrno == EBADF)
2238 {
2239 #ifdef AIX
2240 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
2241 the child's closure of the pts gives the parent a SIGHUP, and
2242 the ptc file descriptor is automatically closed,
2243 yielding EBADF here or at select() call above.
2244 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
2245 in m/ibmrt-aix.h), and here we just ignore the select error.
2246 Cleanup occurs c/o status_notify after SIGCLD. */
2247 FD_ZERO (&Available); /* Cannot depend on values returned */
2248 #else
2249 abort ();
2250 #endif
2251 }
2252 else
2253 error ("select error: %s", strerror (xerrno));
2254 }
2255 #if defined(sun) && !defined(USG5_4)
2256 else if (nfds > 0 && keyboard_bit_set (&Available)
2257 && interrupt_input)
2258 /* System sometimes fails to deliver SIGIO.
2259
2260 David J. Mackenzie says that Emacs doesn't compile under
2261 Solaris if this code is enabled, thus the USG5_4 in the CPP
2262 conditional. "I haven't noticed any ill effects so far.
2263 If you find a Solaris expert somewhere, they might know
2264 better." */
2265 kill (getpid (), SIGIO);
2266 #endif
2267
2268 /* Check for keyboard input */
2269 /* If there is any, return immediately
2270 to give it higher priority than subprocesses */
2271
2272 if ((XINT (read_kbd) != 0)
2273 && detect_input_pending_run_timers (do_display))
2274 {
2275 swallow_events (do_display);
2276 if (detect_input_pending_run_timers (do_display))
2277 break;
2278 }
2279
2280 /* If wait_for_cell. check for keyboard input
2281 but don't run any timers.
2282 ??? (It seems wrong to me to check for keyboard
2283 input at all when wait_for_cell, but the code
2284 has been this way since July 1994.
2285 Try changing this after version 19.31.) */
2286 if (wait_for_cell
2287 && detect_input_pending ())
2288 {
2289 swallow_events (do_display);
2290 if (detect_input_pending ())
2291 break;
2292 }
2293
2294 /* Exit now if the cell we're waiting for became non-nil. */
2295 if (wait_for_cell && ! NILP (*wait_for_cell))
2296 break;
2297
2298 #ifdef SIGIO
2299 /* If we think we have keyboard input waiting, but didn't get SIGIO
2300 go read it. This can happen with X on BSD after logging out.
2301 In that case, there really is no input and no SIGIO,
2302 but select says there is input. */
2303
2304 if (XINT (read_kbd) && interrupt_input
2305 && (keyboard_bit_set (&Available)))
2306 kill (getpid (), SIGIO);
2307 #endif
2308
2309 if (! wait_proc)
2310 got_some_input |= nfds > 0;
2311
2312 /* If checking input just got us a size-change event from X,
2313 obey it now if we should. */
2314 if (XINT (read_kbd) || wait_for_cell)
2315 do_pending_window_change ();
2316
2317 /* Check for data from a process. */
2318 /* Really FIRST_PROC_DESC should be 0 on Unix,
2319 but this is safer in the short run. */
2320 for (channel = 0; channel <= max_process_desc; channel++)
2321 {
2322 if (FD_ISSET (channel, &Available)
2323 && FD_ISSET (channel, &non_keyboard_wait_mask))
2324 {
2325 int nread;
2326
2327 /* If waiting for this channel, arrange to return as
2328 soon as no more input to be processed. No more
2329 waiting. */
2330 if (wait_channel == channel)
2331 {
2332 wait_channel = -1;
2333 time_limit = -1;
2334 got_some_input = 1;
2335 }
2336 proc = chan_process[channel];
2337 if (NILP (proc))
2338 continue;
2339
2340 /* Read data from the process, starting with our
2341 buffered-ahead character if we have one. */
2342
2343 nread = read_process_output (proc, channel);
2344 if (nread > 0)
2345 {
2346 /* Since read_process_output can run a filter,
2347 which can call accept-process-output,
2348 don't try to read from any other processes
2349 before doing the select again. */
2350 FD_ZERO (&Available);
2351
2352 if (do_display)
2353 redisplay_preserve_echo_area ();
2354 }
2355 #ifdef EWOULDBLOCK
2356 else if (nread == -1 && errno == EWOULDBLOCK)
2357 ;
2358 #endif
2359 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
2360 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
2361 #ifdef O_NONBLOCK
2362 else if (nread == -1 && errno == EAGAIN)
2363 ;
2364 #else
2365 #ifdef O_NDELAY
2366 else if (nread == -1 && errno == EAGAIN)
2367 ;
2368 /* Note that we cannot distinguish between no input
2369 available now and a closed pipe.
2370 With luck, a closed pipe will be accompanied by
2371 subprocess termination and SIGCHLD. */
2372 else if (nread == 0 && !NETCONN_P (proc))
2373 ;
2374 #endif /* O_NDELAY */
2375 #endif /* O_NONBLOCK */
2376 #ifdef HAVE_PTYS
2377 /* On some OSs with ptys, when the process on one end of
2378 a pty exits, the other end gets an error reading with
2379 errno = EIO instead of getting an EOF (0 bytes read).
2380 Therefore, if we get an error reading and errno =
2381 EIO, just continue, because the child process has
2382 exited and should clean itself up soon (e.g. when we
2383 get a SIGCHLD). */
2384 else if (nread == -1 && errno == EIO)
2385 ;
2386 #endif /* HAVE_PTYS */
2387 /* If we can detect process termination, don't consider the process
2388 gone just because its pipe is closed. */
2389 #ifdef SIGCHLD
2390 else if (nread == 0 && !NETCONN_P (proc))
2391 ;
2392 #endif
2393 else
2394 {
2395 /* Preserve status of processes already terminated. */
2396 XSETINT (XPROCESS (proc)->tick, ++process_tick);
2397 deactivate_process (proc);
2398 if (!NILP (XPROCESS (proc)->raw_status_low))
2399 update_status (XPROCESS (proc));
2400 if (EQ (XPROCESS (proc)->status, Qrun))
2401 XPROCESS (proc)->status
2402 = Fcons (Qexit, Fcons (make_number (256), Qnil));
2403 }
2404 }
2405 } /* end for each file descriptor */
2406 } /* end while exit conditions not met */
2407
2408 waiting_for_user_input_p = 0;
2409
2410 /* If calling from keyboard input, do not quit
2411 since we want to return C-g as an input character.
2412 Otherwise, do pending quit if requested. */
2413 if (XINT (read_kbd) >= 0)
2414 {
2415 /* Prevent input_pending from remaining set if we quit. */
2416 clear_input_pending ();
2417 QUIT;
2418 }
2419 #ifdef hpux
2420 /* AlainF 5-Jul-1996
2421 HP-UX 10.10 seems to have problems with signals coming in
2422 Causes "poll: interrupted system call" messages when Emacs is run
2423 in an X window
2424 Turn periodic alarms back on */
2425 start_polling();
2426 #endif
2427
2428 return got_some_input;
2429 }
2430 \f
2431 /* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
2432
2433 static Lisp_Object
2434 read_process_output_call (fun_and_args)
2435 Lisp_Object fun_and_args;
2436 {
2437 return apply1 (XCONS (fun_and_args)->car, XCONS (fun_and_args)->cdr);
2438 }
2439
2440 static Lisp_Object
2441 read_process_output_error_handler (error)
2442 Lisp_Object error;
2443 {
2444 cmd_error_internal (error, "error in process filter: ");
2445 Vinhibit_quit = Qt;
2446 update_echo_area ();
2447 Fsleep_for (make_number (2), Qnil);
2448 }
2449
2450 /* Read pending output from the process channel,
2451 starting with our buffered-ahead character if we have one.
2452 Yield number of characters read.
2453
2454 This function reads at most 1024 characters.
2455 If you want to read all available subprocess output,
2456 you must call it repeatedly until it returns zero. */
2457
2458 read_process_output (proc, channel)
2459 Lisp_Object proc;
2460 register int channel;
2461 {
2462 register int nchars;
2463 #ifdef VMS
2464 char *chars;
2465 #else
2466 char chars[1024];
2467 #endif
2468 register Lisp_Object outstream;
2469 register struct buffer *old = current_buffer;
2470 register struct Lisp_Process *p = XPROCESS (proc);
2471 register int opoint;
2472
2473 #ifdef VMS
2474 VMS_PROC_STUFF *vs, *get_vms_process_pointer();
2475
2476 vs = get_vms_process_pointer (p->pid);
2477 if (vs)
2478 {
2479 if (!vs->iosb[0])
2480 return(0); /* Really weird if it does this */
2481 if (!(vs->iosb[0] & 1))
2482 return -1; /* I/O error */
2483 }
2484 else
2485 error ("Could not get VMS process pointer");
2486 chars = vs->inputBuffer;
2487 nchars = clean_vms_buffer (chars, vs->iosb[1]);
2488 if (nchars <= 0)
2489 {
2490 start_vms_process_read (vs); /* Crank up the next read on the process */
2491 return 1; /* Nothing worth printing, say we got 1 */
2492 }
2493 #else /* not VMS */
2494
2495 if (proc_buffered_char[channel] < 0)
2496 nchars = read (channel, chars, sizeof (chars));
2497 else
2498 {
2499 chars[0] = proc_buffered_char[channel];
2500 proc_buffered_char[channel] = -1;
2501 nchars = read (channel, chars + 1, sizeof (chars) - 1);
2502 if (nchars < 0)
2503 nchars = 1;
2504 else
2505 nchars = nchars + 1;
2506 }
2507 #endif /* not VMS */
2508
2509 if (nchars <= 0) return nchars;
2510
2511 outstream = p->filter;
2512 if (!NILP (outstream))
2513 {
2514 /* We inhibit quit here instead of just catching it so that
2515 hitting ^G when a filter happens to be running won't screw
2516 it up. */
2517 int count = specpdl_ptr - specpdl;
2518 Lisp_Object odeactivate;
2519 Lisp_Object obuffer, okeymap;
2520 int outer_running_asynch_code = running_asynch_code;
2521
2522 /* No need to gcpro these, because all we do with them later
2523 is test them for EQness, and none of them should be a string. */
2524 odeactivate = Vdeactivate_mark;
2525 XSETBUFFER (obuffer, current_buffer);
2526 okeymap = current_buffer->keymap;
2527
2528 specbind (Qinhibit_quit, Qt);
2529 specbind (Qlast_nonmenu_event, Qt);
2530
2531 /* In case we get recursively called,
2532 and we already saved the match data nonrecursively,
2533 save the same match data in safely recursive fashion. */
2534 if (outer_running_asynch_code)
2535 {
2536 Lisp_Object tem;
2537 /* Don't clobber the CURRENT match data, either! */
2538 tem = Fmatch_data (Qnil, Qnil);
2539 restore_match_data ();
2540 record_unwind_protect (Fstore_match_data, Fmatch_data (Qnil, Qnil));
2541 Fstore_match_data (tem);
2542 }
2543
2544 /* For speed, if a search happens within this code,
2545 save the match data in a special nonrecursive fashion. */
2546 running_asynch_code = 1;
2547
2548 /* Read and dispose of the process output. */
2549 internal_condition_case_1 (read_process_output_call,
2550 Fcons (outstream,
2551 Fcons (proc,
2552 Fcons (make_string (chars,
2553 nchars),
2554 Qnil))),
2555 !NILP (Vdebug_on_error) ? Qnil : Qerror,
2556 read_process_output_error_handler);
2557
2558 /* If we saved the match data nonrecursively, restore it now. */
2559 restore_match_data ();
2560 running_asynch_code = outer_running_asynch_code;
2561
2562 /* Handling the process output should not deactivate the mark. */
2563 Vdeactivate_mark = odeactivate;
2564
2565 #if 0 /* Call record_asynch_buffer_change unconditionally,
2566 because we might have changed minor modes or other things
2567 that affect key bindings. */
2568 if (! EQ (Fcurrent_buffer (), obuffer)
2569 || ! EQ (current_buffer->keymap, okeymap))
2570 #endif
2571 /* But do it only if the caller is actually going to read events.
2572 Otherwise there's no need to make him wake up, and it could
2573 cause trouble (for example it would make Fsit_for return). */
2574 if (waiting_for_user_input_p == -1)
2575 record_asynch_buffer_change ();
2576
2577 #ifdef VMS
2578 start_vms_process_read (vs);
2579 #endif
2580 unbind_to (count, Qnil);
2581 return nchars;
2582 }
2583
2584 /* If no filter, write into buffer if it isn't dead. */
2585 if (!NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name))
2586 {
2587 Lisp_Object old_read_only;
2588 Lisp_Object old_begv, old_zv;
2589 Lisp_Object odeactivate;
2590
2591 odeactivate = Vdeactivate_mark;
2592
2593 Fset_buffer (p->buffer);
2594 opoint = PT;
2595 old_read_only = current_buffer->read_only;
2596 XSETFASTINT (old_begv, BEGV);
2597 XSETFASTINT (old_zv, ZV);
2598
2599 current_buffer->read_only = Qnil;
2600
2601 /* Insert new output into buffer
2602 at the current end-of-output marker,
2603 thus preserving logical ordering of input and output. */
2604 if (XMARKER (p->mark)->buffer)
2605 SET_PT (clip_to_bounds (BEGV, marker_position (p->mark), ZV));
2606 else
2607 SET_PT (ZV);
2608
2609 /* If the output marker is outside of the visible region, save
2610 the restriction and widen. */
2611 if (! (BEGV <= PT && PT <= ZV))
2612 Fwiden ();
2613
2614 /* Make sure opoint floats ahead of any new text, just as point
2615 would. */
2616 if (PT <= opoint)
2617 opoint += nchars;
2618
2619 /* Insert after old_begv, but before old_zv. */
2620 if (PT < XFASTINT (old_begv))
2621 XSETFASTINT (old_begv, XFASTINT (old_begv) + nchars);
2622 if (PT <= XFASTINT (old_zv))
2623 XSETFASTINT (old_zv, XFASTINT (old_zv) + nchars);
2624
2625 /* Insert before markers in case we are inserting where
2626 the buffer's mark is, and the user's next command is Meta-y. */
2627 insert_before_markers (chars, nchars);
2628 Fset_marker (p->mark, make_number (PT), p->buffer);
2629
2630 update_mode_lines++;
2631
2632 /* If the restriction isn't what it should be, set it. */
2633 if (XFASTINT (old_begv) != BEGV || XFASTINT (old_zv) != ZV)
2634 Fnarrow_to_region (old_begv, old_zv);
2635
2636 /* Handling the process output should not deactivate the mark. */
2637 Vdeactivate_mark = odeactivate;
2638
2639 current_buffer->read_only = old_read_only;
2640 SET_PT (opoint);
2641 set_buffer_internal (old);
2642 }
2643 #ifdef VMS
2644 start_vms_process_read (vs);
2645 #endif
2646 return nchars;
2647 }
2648
2649 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, Swaiting_for_user_input_p,
2650 0, 0, 0,
2651 "Returns non-nil if emacs is waiting for input from the user.\n\
2652 This is intended for use by asynchronous process output filters and sentinels.")
2653 ()
2654 {
2655 return (waiting_for_user_input_p ? Qt : Qnil);
2656 }
2657 \f
2658 /* Sending data to subprocess */
2659
2660 jmp_buf send_process_frame;
2661
2662 SIGTYPE
2663 send_process_trap ()
2664 {
2665 #ifdef BSD4_1
2666 sigrelse (SIGPIPE);
2667 sigrelse (SIGALRM);
2668 #endif /* BSD4_1 */
2669 longjmp (send_process_frame, 1);
2670 }
2671
2672 /* Send some data to process PROC.
2673 BUF is the beginning of the data; LEN is the number of characters.
2674 OBJECT is the Lisp object that the data comes from. */
2675
2676 send_process (proc, buf, len, object)
2677 volatile Lisp_Object proc;
2678 char *buf;
2679 int len;
2680 Lisp_Object object;
2681 {
2682 /* Use volatile to protect variables from being clobbered by longjmp. */
2683 int rv;
2684 volatile unsigned char *procname = XSTRING (XPROCESS (proc)->name)->data;
2685 struct gcpro gcpro1;
2686
2687 GCPRO1 (object);
2688
2689 #ifdef VMS
2690 struct Lisp_Process *p = XPROCESS (proc);
2691 VMS_PROC_STUFF *vs, *get_vms_process_pointer();
2692 #endif /* VMS */
2693
2694 if (! NILP (XPROCESS (proc)->raw_status_low))
2695 update_status (XPROCESS (proc));
2696 if (! EQ (XPROCESS (proc)->status, Qrun))
2697 error ("Process %s not running", procname);
2698
2699 #ifdef VMS
2700 vs = get_vms_process_pointer (p->pid);
2701 if (vs == 0)
2702 error ("Could not find this process: %x", p->pid);
2703 else if (write_to_vms_process (vs, buf, len))
2704 ;
2705 #else
2706
2707 if (pty_max_bytes == 0)
2708 {
2709 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
2710 pty_max_bytes = fpathconf (XFASTINT (XPROCESS (proc)->outfd),
2711 _PC_MAX_CANON);
2712 if (pty_max_bytes < 0)
2713 pty_max_bytes = 250;
2714 #else
2715 pty_max_bytes = 250;
2716 #endif
2717 /* Deduct one, to leave space for the eof. */
2718 pty_max_bytes--;
2719 }
2720
2721 if (!setjmp (send_process_frame))
2722 while (len > 0)
2723 {
2724 int this = len;
2725 SIGTYPE (*old_sigpipe)();
2726 int flush_pty = 0;
2727
2728 /* Decide how much data we can send in one batch.
2729 Long lines need to be split into multiple batches. */
2730 if (!NILP (XPROCESS (proc)->pty_flag))
2731 {
2732 /* Starting this at zero is always correct when not the first iteration
2733 because the previous iteration ended by sending C-d.
2734 It may not be correct for the first iteration
2735 if a partial line was sent in a separate send_process call.
2736 If that proves worth handling, we need to save linepos
2737 in the process object. */
2738 int linepos = 0;
2739 char *ptr = buf;
2740 char *end = buf + len;
2741
2742 /* Scan through this text for a line that is too long. */
2743 while (ptr != end && linepos < pty_max_bytes)
2744 {
2745 if (*ptr == '\n')
2746 linepos = 0;
2747 else
2748 linepos++;
2749 ptr++;
2750 }
2751 /* If we found one, break the line there
2752 and put in a C-d to force the buffer through. */
2753 this = ptr - buf;
2754 }
2755
2756 /* Send this batch, using one or more write calls. */
2757 while (this > 0)
2758 {
2759 old_sigpipe = (SIGTYPE (*) ()) signal (SIGPIPE, send_process_trap);
2760 rv = write (XINT (XPROCESS (proc)->outfd), buf, this);
2761 signal (SIGPIPE, old_sigpipe);
2762
2763 if (rv < 0)
2764 {
2765 if (0
2766 #ifdef EWOULDBLOCK
2767 || errno == EWOULDBLOCK
2768 #endif
2769 #ifdef EAGAIN
2770 || errno == EAGAIN
2771 #endif
2772 )
2773 /* Buffer is full. Wait, accepting input;
2774 that may allow the program
2775 to finish doing output and read more. */
2776 {
2777 Lisp_Object zero;
2778 int offset;
2779
2780 /* Running filters might relocate buffers or strings.
2781 Arrange to relocate BUF. */
2782 if (BUFFERP (object))
2783 offset = BUF_PTR_CHAR_POS (XBUFFER (object),
2784 (unsigned char *) buf);
2785 else if (STRINGP (object))
2786 offset = buf - (char *) XSTRING (object)->data;
2787
2788 XSETFASTINT (zero, 0);
2789 #ifdef EMACS_HAS_USECS
2790 wait_reading_process_input (0, 20000, zero, 0);
2791 #else
2792 wait_reading_process_input (1, 0, zero, 0);
2793 #endif
2794
2795 if (BUFFERP (object))
2796 buf = (char *) BUF_CHAR_ADDRESS (XBUFFER (object), offset);
2797 else if (STRINGP (object))
2798 buf = offset + (char *) XSTRING (object)->data;
2799
2800 rv = 0;
2801 }
2802 else
2803 /* This is a real error. */
2804 report_file_error ("writing to process", Fcons (proc, Qnil));
2805 }
2806 buf += rv;
2807 len -= rv;
2808 this -= rv;
2809 }
2810
2811 /* If we sent just part of the string, put in an EOF
2812 to force it through, before we send the rest. */
2813 if (len > 0)
2814 Fprocess_send_eof (proc);
2815 }
2816 #endif
2817 else
2818 {
2819 XPROCESS (proc)->raw_status_low = Qnil;
2820 XPROCESS (proc)->raw_status_high = Qnil;
2821 XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (256), Qnil));
2822 XSETINT (XPROCESS (proc)->tick, ++process_tick);
2823 deactivate_process (proc);
2824 #ifdef VMS
2825 error ("Error writing to process %s; closed it", procname);
2826 #else
2827 error ("SIGPIPE raised on process %s; closed it", procname);
2828 #endif
2829 }
2830
2831 UNGCPRO;
2832 }
2833
2834 DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region,
2835 3, 3, 0,
2836 "Send current contents of region as input to PROCESS.\n\
2837 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
2838 nil, indicating the current buffer's process.\n\
2839 Called from program, takes three arguments, PROCESS, START and END.\n\
2840 If the region is more than 500 characters long,\n\
2841 it is sent in several bunches. This may happen even for shorter regions.\n\
2842 Output from processes can arrive in between bunches.")
2843 (process, start, end)
2844 Lisp_Object process, start, end;
2845 {
2846 Lisp_Object proc;
2847 int start1;
2848
2849 proc = get_process (process);
2850 validate_region (&start, &end);
2851
2852 if (XINT (start) < GPT && XINT (end) > GPT)
2853 move_gap (start);
2854
2855 start1 = XINT (start);
2856 send_process (proc, &FETCH_CHAR (start1), XINT (end) - XINT (start),
2857 Fcurrent_buffer ());
2858
2859 return Qnil;
2860 }
2861
2862 DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string,
2863 2, 2, 0,
2864 "Send PROCESS the contents of STRING as input.\n\
2865 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
2866 nil, indicating the current buffer's process.\n\
2867 If STRING is more than 500 characters long,\n\
2868 it is sent in several bunches. This may happen even for shorter strings.\n\
2869 Output from processes can arrive in between bunches.")
2870 (process, string)
2871 Lisp_Object process, string;
2872 {
2873 Lisp_Object proc;
2874 CHECK_STRING (string, 1);
2875 proc = get_process (process);
2876 send_process (proc, XSTRING (string)->data, XSTRING (string)->size, string);
2877 return Qnil;
2878 }
2879 \f
2880 /* send a signal number SIGNO to PROCESS.
2881 CURRENT_GROUP means send to the process group that currently owns
2882 the terminal being used to communicate with PROCESS.
2883 This is used for various commands in shell mode.
2884 If NOMSG is zero, insert signal-announcements into process's buffers
2885 right away.
2886
2887 If we can, we try to signal PROCESS by sending control characters
2888 down the pty. This allows us to signal inferiors who have changed
2889 their uid, for which killpg would return an EPERM error. */
2890
2891 static void
2892 process_send_signal (process, signo, current_group, nomsg)
2893 Lisp_Object process;
2894 int signo;
2895 Lisp_Object current_group;
2896 int nomsg;
2897 {
2898 Lisp_Object proc;
2899 register struct Lisp_Process *p;
2900 int gid;
2901 int no_pgrp = 0;
2902
2903 proc = get_process (process);
2904 p = XPROCESS (proc);
2905
2906 if (!EQ (p->childp, Qt))
2907 error ("Process %s is not a subprocess",
2908 XSTRING (p->name)->data);
2909 if (XINT (p->infd) < 0)
2910 error ("Process %s is not active",
2911 XSTRING (p->name)->data);
2912
2913 if (NILP (p->pty_flag))
2914 current_group = Qnil;
2915
2916 /* If we are using pgrps, get a pgrp number and make it negative. */
2917 if (!NILP (current_group))
2918 {
2919 #ifdef SIGNALS_VIA_CHARACTERS
2920 /* If possible, send signals to the entire pgrp
2921 by sending an input character to it. */
2922
2923 /* TERMIOS is the latest and bestest, and seems most likely to
2924 work. If the system has it, use it. */
2925 #ifdef HAVE_TERMIOS
2926 struct termios t;
2927
2928 switch (signo)
2929 {
2930 case SIGINT:
2931 tcgetattr (XINT (p->infd), &t);
2932 send_process (proc, &t.c_cc[VINTR], 1, Qnil);
2933 return;
2934
2935 case SIGQUIT:
2936 tcgetattr (XINT (p->infd), &t);
2937 send_process (proc, &t.c_cc[VQUIT], 1, Qnil);
2938 return;
2939
2940 case SIGTSTP:
2941 tcgetattr (XINT (p->infd), &t);
2942 #if defined (VSWTCH) && !defined (PREFER_VSUSP)
2943 send_process (proc, &t.c_cc[VSWTCH], 1, Qnil);
2944 #else
2945 send_process (proc, &t.c_cc[VSUSP], 1, Qnil);
2946 #endif
2947 return;
2948 }
2949
2950 #else /* ! HAVE_TERMIOS */
2951
2952 /* On Berkeley descendants, the following IOCTL's retrieve the
2953 current control characters. */
2954 #if defined (TIOCGLTC) && defined (TIOCGETC)
2955
2956 struct tchars c;
2957 struct ltchars lc;
2958
2959 switch (signo)
2960 {
2961 case SIGINT:
2962 ioctl (XINT (p->infd), TIOCGETC, &c);
2963 send_process (proc, &c.t_intrc, 1, Qnil);
2964 return;
2965 case SIGQUIT:
2966 ioctl (XINT (p->infd), TIOCGETC, &c);
2967 send_process (proc, &c.t_quitc, 1, Qnil);
2968 return;
2969 #ifdef SIGTSTP
2970 case SIGTSTP:
2971 ioctl (XINT (p->infd), TIOCGLTC, &lc);
2972 send_process (proc, &lc.t_suspc, 1, Qnil);
2973 return;
2974 #endif /* ! defined (SIGTSTP) */
2975 }
2976
2977 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
2978
2979 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
2980 characters. */
2981 #ifdef TCGETA
2982 struct termio t;
2983 switch (signo)
2984 {
2985 case SIGINT:
2986 ioctl (XINT (p->infd), TCGETA, &t);
2987 send_process (proc, &t.c_cc[VINTR], 1, Qnil);
2988 return;
2989 case SIGQUIT:
2990 ioctl (XINT (p->infd), TCGETA, &t);
2991 send_process (proc, &t.c_cc[VQUIT], 1, Qnil);
2992 return;
2993 #ifdef SIGTSTP
2994 case SIGTSTP:
2995 ioctl (XINT (p->infd), TCGETA, &t);
2996 send_process (proc, &t.c_cc[VSWTCH], 1, Qnil);
2997 return;
2998 #endif /* ! defined (SIGTSTP) */
2999 }
3000 #else /* ! defined (TCGETA) */
3001 Your configuration files are messed up.
3002 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
3003 you'd better be using one of the alternatives above! */
3004 #endif /* ! defined (TCGETA) */
3005 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
3006 #endif /* ! defined HAVE_TERMIOS */
3007 #endif /* ! defined (SIGNALS_VIA_CHARACTERS) */
3008
3009 #ifdef TIOCGPGRP
3010 /* Get the pgrp using the tty itself, if we have that.
3011 Otherwise, use the pty to get the pgrp.
3012 On pfa systems, saka@pfu.fujitsu.co.JP writes:
3013 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
3014 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
3015 His patch indicates that if TIOCGPGRP returns an error, then
3016 we should just assume that p->pid is also the process group id. */
3017 {
3018 int err;
3019
3020 if (!NILP (p->subtty))
3021 err = ioctl (XFASTINT (p->subtty), TIOCGPGRP, &gid);
3022 else
3023 err = ioctl (XINT (p->infd), TIOCGPGRP, &gid);
3024
3025 #ifdef pfa
3026 if (err == -1)
3027 gid = - XFASTINT (p->pid);
3028 #endif /* ! defined (pfa) */
3029 }
3030 if (gid == -1)
3031 no_pgrp = 1;
3032 else
3033 gid = - gid;
3034 #else /* ! defined (TIOCGPGRP ) */
3035 /* Can't select pgrps on this system, so we know that
3036 the child itself heads the pgrp. */
3037 gid = - XFASTINT (p->pid);
3038 #endif /* ! defined (TIOCGPGRP ) */
3039 }
3040 else
3041 gid = - XFASTINT (p->pid);
3042
3043 switch (signo)
3044 {
3045 #ifdef SIGCONT
3046 case SIGCONT:
3047 p->raw_status_low = Qnil;
3048 p->raw_status_high = Qnil;
3049 p->status = Qrun;
3050 XSETINT (p->tick, ++process_tick);
3051 if (!nomsg)
3052 status_notify ();
3053 break;
3054 #endif /* ! defined (SIGCONT) */
3055 case SIGINT:
3056 #ifdef VMS
3057 send_process (proc, "\003", 1, Qnil); /* ^C */
3058 goto whoosh;
3059 #endif
3060 case SIGQUIT:
3061 #ifdef VMS
3062 send_process (proc, "\031", 1, Qnil); /* ^Y */
3063 goto whoosh;
3064 #endif
3065 case SIGKILL:
3066 #ifdef VMS
3067 sys$forcex (&(XFASTINT (p->pid)), 0, 1);
3068 whoosh:
3069 #endif
3070 flush_pending_output (XINT (p->infd));
3071 break;
3072 }
3073
3074 /* If we don't have process groups, send the signal to the immediate
3075 subprocess. That isn't really right, but it's better than any
3076 obvious alternative. */
3077 if (no_pgrp)
3078 {
3079 kill (XFASTINT (p->pid), signo);
3080 return;
3081 }
3082
3083 /* gid may be a pid, or minus a pgrp's number */
3084 #ifdef TIOCSIGSEND
3085 if (!NILP (current_group))
3086 ioctl (XINT (p->infd), TIOCSIGSEND, signo);
3087 else
3088 {
3089 gid = - XFASTINT (p->pid);
3090 kill (gid, signo);
3091 }
3092 #else /* ! defined (TIOCSIGSEND) */
3093 EMACS_KILLPG (-gid, signo);
3094 #endif /* ! defined (TIOCSIGSEND) */
3095 }
3096
3097 DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0,
3098 "Interrupt process PROCESS. May be process or name of one.\n\
3099 PROCESS may be a process, a buffer, or the name of a process or buffer.\n\
3100 nil or no arg means current buffer's process.\n\
3101 Second arg CURRENT-GROUP non-nil means send signal to\n\
3102 the current process-group of the process's controlling terminal\n\
3103 rather than to the process's own process group.\n\
3104 If the process is a shell, this means interrupt current subjob\n\
3105 rather than the shell.")
3106 (process, current_group)
3107 Lisp_Object process, current_group;
3108 {
3109 process_send_signal (process, SIGINT, current_group, 0);
3110 return process;
3111 }
3112
3113 DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0,
3114 "Kill process PROCESS. May be process or name of one.\n\
3115 See function `interrupt-process' for more details on usage.")
3116 (process, current_group)
3117 Lisp_Object process, current_group;
3118 {
3119 process_send_signal (process, SIGKILL, current_group, 0);
3120 return process;
3121 }
3122
3123 DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0,
3124 "Send QUIT signal to process PROCESS. May be process or name of one.\n\
3125 See function `interrupt-process' for more details on usage.")
3126 (process, current_group)
3127 Lisp_Object process, current_group;
3128 {
3129 process_send_signal (process, SIGQUIT, current_group, 0);
3130 return process;
3131 }
3132
3133 DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0,
3134 "Stop process PROCESS. May be process or name of one.\n\
3135 See function `interrupt-process' for more details on usage.")
3136 (process, current_group)
3137 Lisp_Object process, current_group;
3138 {
3139 #ifndef SIGTSTP
3140 error ("no SIGTSTP support");
3141 #else
3142 process_send_signal (process, SIGTSTP, current_group, 0);
3143 #endif
3144 return process;
3145 }
3146
3147 DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0,
3148 "Continue process PROCESS. May be process or name of one.\n\
3149 See function `interrupt-process' for more details on usage.")
3150 (process, current_group)
3151 Lisp_Object process, current_group;
3152 {
3153 #ifdef SIGCONT
3154 process_send_signal (process, SIGCONT, current_group, 0);
3155 #else
3156 error ("no SIGCONT support");
3157 #endif
3158 return process;
3159 }
3160
3161 DEFUN ("signal-process", Fsignal_process, Ssignal_process,
3162 2, 2, "nProcess number: \nnSignal code: ",
3163 "Send the process with process id PID the signal with code SIGCODE.\n\
3164 PID must be an integer. The process need not be a child of this Emacs.\n\
3165 SIGCODE may be an integer, or a symbol whose name is a signal name.")
3166 (pid, sigcode)
3167 Lisp_Object pid, sigcode;
3168 {
3169 CHECK_NUMBER (pid, 0);
3170
3171 #define handle_signal(NAME, VALUE) \
3172 else if (!strcmp (name, NAME)) \
3173 XSETINT (sigcode, VALUE)
3174
3175 if (INTEGERP (sigcode))
3176 ;
3177 else
3178 {
3179 unsigned char *name;
3180
3181 CHECK_SYMBOL (sigcode, 1);
3182 name = XSYMBOL (sigcode)->name->data;
3183
3184 if (0)
3185 ;
3186 #ifdef SIGHUP
3187 handle_signal ("SIGHUP", SIGHUP);
3188 #endif
3189 #ifdef SIGINT
3190 handle_signal ("SIGINT", SIGINT);
3191 #endif
3192 #ifdef SIGQUIT
3193 handle_signal ("SIGQUIT", SIGQUIT);
3194 #endif
3195 #ifdef SIGILL
3196 handle_signal ("SIGILL", SIGILL);
3197 #endif
3198 #ifdef SIGABRT
3199 handle_signal ("SIGABRT", SIGABRT);
3200 #endif
3201 #ifdef SIGEMT
3202 handle_signal ("SIGEMT", SIGEMT);
3203 #endif
3204 #ifdef SIGKILL
3205 handle_signal ("SIGKILL", SIGKILL);
3206 #endif
3207 #ifdef SIGFPE
3208 handle_signal ("SIGFPE", SIGFPE);
3209 #endif
3210 #ifdef SIGBUS
3211 handle_signal ("SIGBUS", SIGBUS);
3212 #endif
3213 #ifdef SIGSEGV
3214 handle_signal ("SIGSEGV", SIGSEGV);
3215 #endif
3216 #ifdef SIGSYS
3217 handle_signal ("SIGSYS", SIGSYS);
3218 #endif
3219 #ifdef SIGPIPE
3220 handle_signal ("SIGPIPE", SIGPIPE);
3221 #endif
3222 #ifdef SIGALRM
3223 handle_signal ("SIGALRM", SIGALRM);
3224 #endif
3225 #ifdef SIGTERM
3226 handle_signal ("SIGTERM", SIGTERM);
3227 #endif
3228 #ifdef SIGURG
3229 handle_signal ("SIGURG", SIGURG);
3230 #endif
3231 #ifdef SIGSTOP
3232 handle_signal ("SIGSTOP", SIGSTOP);
3233 #endif
3234 #ifdef SIGTSTP
3235 handle_signal ("SIGTSTP", SIGTSTP);
3236 #endif
3237 #ifdef SIGCONT
3238 handle_signal ("SIGCONT", SIGCONT);
3239 #endif
3240 #ifdef SIGCHLD
3241 handle_signal ("SIGCHLD", SIGCHLD);
3242 #endif
3243 #ifdef SIGTTIN
3244 handle_signal ("SIGTTIN", SIGTTIN);
3245 #endif
3246 #ifdef SIGTTOU
3247 handle_signal ("SIGTTOU", SIGTTOU);
3248 #endif
3249 #ifdef SIGIO
3250 handle_signal ("SIGIO", SIGIO);
3251 #endif
3252 #ifdef SIGXCPU
3253 handle_signal ("SIGXCPU", SIGXCPU);
3254 #endif
3255 #ifdef SIGXFSZ
3256 handle_signal ("SIGXFSZ", SIGXFSZ);
3257 #endif
3258 #ifdef SIGVTALRM
3259 handle_signal ("SIGVTALRM", SIGVTALRM);
3260 #endif
3261 #ifdef SIGPROF
3262 handle_signal ("SIGPROF", SIGPROF);
3263 #endif
3264 #ifdef SIGWINCH
3265 handle_signal ("SIGWINCH", SIGWINCH);
3266 #endif
3267 #ifdef SIGINFO
3268 handle_signal ("SIGINFO", SIGINFO);
3269 #endif
3270 #ifdef SIGUSR1
3271 handle_signal ("SIGUSR1", SIGUSR1);
3272 #endif
3273 #ifdef SIGUSR2
3274 handle_signal ("SIGUSR2", SIGUSR2);
3275 #endif
3276 else
3277 error ("Undefined signal name %s", name);
3278 }
3279
3280 #undef handle_signal
3281
3282 return make_number (kill (XINT (pid), XINT (sigcode)));
3283 }
3284
3285 DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0,
3286 "Make PROCESS see end-of-file in its input.\n\
3287 Eof comes after any text already sent to it.\n\
3288 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
3289 nil, indicating the current buffer's process.\n\
3290 If PROCESS is a network connection, or is a process communicating\n\
3291 through a pipe (as opposed to a pty), then you cannot send any more\n\
3292 text to PROCESS after you call this function.")
3293 (process)
3294 Lisp_Object process;
3295 {
3296 Lisp_Object proc;
3297
3298 proc = get_process (process);
3299
3300 /* Make sure the process is really alive. */
3301 if (! NILP (XPROCESS (proc)->raw_status_low))
3302 update_status (XPROCESS (proc));
3303 if (! EQ (XPROCESS (proc)->status, Qrun))
3304 error ("Process %s not running", XSTRING (XPROCESS (proc)->name)->data);
3305
3306 #ifdef VMS
3307 send_process (proc, "\032", 1, Qnil); /* ^z */
3308 #else
3309 if (!NILP (XPROCESS (proc)->pty_flag))
3310 send_process (proc, "\004", 1, Qnil);
3311 else
3312 {
3313 close (XINT (XPROCESS (proc)->outfd));
3314 XSETINT (XPROCESS (proc)->outfd, open (NULL_DEVICE, O_WRONLY));
3315 }
3316 #endif /* VMS */
3317 return process;
3318 }
3319
3320 /* Kill all processes associated with `buffer'.
3321 If `buffer' is nil, kill all processes */
3322
3323 kill_buffer_processes (buffer)
3324 Lisp_Object buffer;
3325 {
3326 Lisp_Object tail, proc;
3327
3328 for (tail = Vprocess_alist; GC_CONSP (tail); tail = XCONS (tail)->cdr)
3329 {
3330 proc = XCONS (XCONS (tail)->car)->cdr;
3331 if (GC_PROCESSP (proc)
3332 && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer)))
3333 {
3334 if (NETCONN_P (proc))
3335 Fdelete_process (proc);
3336 else if (XINT (XPROCESS (proc)->infd) >= 0)
3337 process_send_signal (proc, SIGHUP, Qnil, 1);
3338 }
3339 }
3340 }
3341 \f
3342 /* On receipt of a signal that a child status has changed,
3343 loop asking about children with changed statuses until
3344 the system says there are no more.
3345 All we do is change the status;
3346 we do not run sentinels or print notifications.
3347 That is saved for the next time keyboard input is done,
3348 in order to avoid timing errors. */
3349
3350 /** WARNING: this can be called during garbage collection.
3351 Therefore, it must not be fooled by the presence of mark bits in
3352 Lisp objects. */
3353
3354 /** USG WARNING: Although it is not obvious from the documentation
3355 in signal(2), on a USG system the SIGCLD handler MUST NOT call
3356 signal() before executing at least one wait(), otherwise the handler
3357 will be called again, resulting in an infinite loop. The relevant
3358 portion of the documentation reads "SIGCLD signals will be queued
3359 and the signal-catching function will be continually reentered until
3360 the queue is empty". Invoking signal() causes the kernel to reexamine
3361 the SIGCLD queue. Fred Fish, UniSoft Systems Inc. */
3362
3363 SIGTYPE
3364 sigchld_handler (signo)
3365 int signo;
3366 {
3367 int old_errno = errno;
3368 Lisp_Object proc;
3369 register struct Lisp_Process *p;
3370 extern EMACS_TIME *input_available_clear_time;
3371
3372 #ifdef BSD4_1
3373 extern int sigheld;
3374 sigheld |= sigbit (SIGCHLD);
3375 #endif
3376
3377 while (1)
3378 {
3379 register int pid;
3380 WAITTYPE w;
3381 Lisp_Object tail;
3382
3383 #ifdef WNOHANG
3384 #ifndef WUNTRACED
3385 #define WUNTRACED 0
3386 #endif /* no WUNTRACED */
3387 /* Keep trying to get a status until we get a definitive result. */
3388 do
3389 {
3390 errno = 0;
3391 pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
3392 }
3393 while (pid <= 0 && errno == EINTR);
3394
3395 if (pid <= 0)
3396 {
3397 /* A real failure. We have done all our job, so return. */
3398
3399 /* USG systems forget handlers when they are used;
3400 must reestablish each time */
3401 #if defined (USG) && !defined (POSIX_SIGNALS)
3402 signal (signo, sigchld_handler); /* WARNING - must come after wait3() */
3403 #endif
3404 #ifdef BSD4_1
3405 sigheld &= ~sigbit (SIGCHLD);
3406 sigrelse (SIGCHLD);
3407 #endif
3408 errno = old_errno;
3409 return;
3410 }
3411 #else
3412 pid = wait (&w);
3413 #endif /* no WNOHANG */
3414
3415 /* Find the process that signaled us, and record its status. */
3416
3417 p = 0;
3418 for (tail = Vprocess_alist; CONSP (tail); tail = XCONS (tail)->cdr)
3419 {
3420 proc = XCONS (XCONS (tail)->car)->cdr;
3421 p = XPROCESS (proc);
3422 if (EQ (p->childp, Qt) && XFASTINT (p->pid) == pid)
3423 break;
3424 p = 0;
3425 }
3426
3427 /* Look for an asynchronous process whose pid hasn't been filled
3428 in yet. */
3429 if (p == 0)
3430 for (tail = Vprocess_alist; CONSP (tail); tail = XCONS (tail)->cdr)
3431 {
3432 proc = XCONS (XCONS (tail)->car)->cdr;
3433 p = XPROCESS (proc);
3434 if (INTEGERP (p->pid) && XINT (p->pid) == -1)
3435 break;
3436 p = 0;
3437 }
3438
3439 /* Change the status of the process that was found. */
3440 if (p != 0)
3441 {
3442 union { int i; WAITTYPE wt; } u;
3443 int clear_desc_flag = 0;
3444
3445 XSETINT (p->tick, ++process_tick);
3446 u.wt = w;
3447 XSETINT (p->raw_status_low, u.i & 0xffff);
3448 XSETINT (p->raw_status_high, u.i >> 16);
3449
3450 /* If process has terminated, stop waiting for its output. */
3451 if ((WIFSIGNALED (w) || WIFEXITED (w))
3452 && XINT (p->infd) >= 0)
3453 clear_desc_flag = 1;
3454
3455 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
3456 if (clear_desc_flag)
3457 {
3458 FD_CLR (XINT (p->infd), &input_wait_mask);
3459 FD_CLR (XINT (p->infd), &non_keyboard_wait_mask);
3460 }
3461
3462 /* Tell wait_reading_process_input that it needs to wake up and
3463 look around. */
3464 if (input_available_clear_time)
3465 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
3466 }
3467
3468 /* There was no asynchronous process found for that id. Check
3469 if we have a synchronous process. */
3470 else
3471 {
3472 synch_process_alive = 0;
3473
3474 /* Report the status of the synchronous process. */
3475 if (WIFEXITED (w))
3476 synch_process_retcode = WRETCODE (w);
3477 else if (WIFSIGNALED (w))
3478 {
3479 int code = WTERMSIG (w);
3480 char *signame = 0;
3481
3482 if (code < NSIG)
3483 {
3484 #ifndef VMS
3485 /* Suppress warning if the table has const char *. */
3486 signame = (char *) sys_siglist[code];
3487 #else
3488 signame = sys_errlist[code];
3489 #endif
3490 }
3491 if (signame == 0)
3492 signame = "unknown";
3493
3494 synch_process_death = signame;
3495 }
3496
3497 /* Tell wait_reading_process_input that it needs to wake up and
3498 look around. */
3499 if (input_available_clear_time)
3500 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
3501 }
3502
3503 /* On some systems, we must return right away.
3504 If any more processes want to signal us, we will
3505 get another signal.
3506 Otherwise (on systems that have WNOHANG), loop around
3507 to use up all the processes that have something to tell us. */
3508 #if defined (USG) && ! (defined (HPUX) && defined (WNOHANG)) || defined (WINDOWSNT)
3509 #if defined (USG) && ! defined (POSIX_SIGNALS)
3510 signal (signo, sigchld_handler);
3511 #endif
3512 errno = old_errno;
3513 return;
3514 #endif /* USG, but not HPUX with WNOHANG */
3515 }
3516 }
3517 \f
3518
3519 static Lisp_Object
3520 exec_sentinel_unwind (data)
3521 Lisp_Object data;
3522 {
3523 XPROCESS (XCONS (data)->car)->sentinel = XCONS (data)->cdr;
3524 return Qnil;
3525 }
3526
3527 static Lisp_Object
3528 exec_sentinel_error_handler (error)
3529 Lisp_Object error;
3530 {
3531 cmd_error_internal (error, "error in process sentinel: ");
3532 Vinhibit_quit = Qt;
3533 update_echo_area ();
3534 Fsleep_for (make_number (2), Qnil);
3535 }
3536
3537 static void
3538 exec_sentinel (proc, reason)
3539 Lisp_Object proc, reason;
3540 {
3541 Lisp_Object sentinel, obuffer, odeactivate, okeymap;
3542 register struct Lisp_Process *p = XPROCESS (proc);
3543 int count = specpdl_ptr - specpdl;
3544 int outer_running_asynch_code = running_asynch_code;
3545
3546 /* No need to gcpro these, because all we do with them later
3547 is test them for EQness, and none of them should be a string. */
3548 odeactivate = Vdeactivate_mark;
3549 XSETBUFFER (obuffer, current_buffer);
3550 okeymap = current_buffer->keymap;
3551
3552 sentinel = p->sentinel;
3553 if (NILP (sentinel))
3554 return;
3555
3556 /* Zilch the sentinel while it's running, to avoid recursive invocations;
3557 assure that it gets restored no matter how the sentinel exits. */
3558 p->sentinel = Qnil;
3559 record_unwind_protect (exec_sentinel_unwind, Fcons (proc, sentinel));
3560 /* Inhibit quit so that random quits don't screw up a running filter. */
3561 specbind (Qinhibit_quit, Qt);
3562 specbind (Qlast_nonmenu_event, Qt);
3563
3564 /* In case we get recursively called,
3565 and we already saved the match data nonrecursively,
3566 save the same match data in safely recursive fashion. */
3567 if (outer_running_asynch_code)
3568 {
3569 Lisp_Object tem;
3570 tem = Fmatch_data (Qnil, Qnil);
3571 restore_match_data ();
3572 record_unwind_protect (Fstore_match_data, Fmatch_data (Qnil, Qnil));
3573 Fstore_match_data (tem);
3574 }
3575
3576 /* For speed, if a search happens within this code,
3577 save the match data in a special nonrecursive fashion. */
3578 running_asynch_code = 1;
3579
3580 internal_condition_case_1 (read_process_output_call,
3581 Fcons (sentinel,
3582 Fcons (proc, Fcons (reason, Qnil))),
3583 !NILP (Vdebug_on_error) ? Qnil : Qerror,
3584 exec_sentinel_error_handler);
3585
3586 /* If we saved the match data nonrecursively, restore it now. */
3587 restore_match_data ();
3588 running_asynch_code = outer_running_asynch_code;
3589
3590 Vdeactivate_mark = odeactivate;
3591 #if 0
3592 if (! EQ (Fcurrent_buffer (), obuffer)
3593 || ! EQ (current_buffer->keymap, okeymap))
3594 #endif
3595 /* But do it only if the caller is actually going to read events.
3596 Otherwise there's no need to make him wake up, and it could
3597 cause trouble (for example it would make Fsit_for return). */
3598 if (waiting_for_user_input_p == -1)
3599 record_asynch_buffer_change ();
3600
3601 unbind_to (count, Qnil);
3602 }
3603
3604 /* Report all recent events of a change in process status
3605 (either run the sentinel or output a message).
3606 This is done while Emacs is waiting for keyboard input. */
3607
3608 status_notify ()
3609 {
3610 register Lisp_Object proc, buffer;
3611 Lisp_Object tail, msg;
3612 struct gcpro gcpro1, gcpro2;
3613
3614 tail = Qnil;
3615 msg = Qnil;
3616 /* We need to gcpro tail; if read_process_output calls a filter
3617 which deletes a process and removes the cons to which tail points
3618 from Vprocess_alist, and then causes a GC, tail is an unprotected
3619 reference. */
3620 GCPRO2 (tail, msg);
3621
3622 /* Set this now, so that if new processes are created by sentinels
3623 that we run, we get called again to handle their status changes. */
3624 update_tick = process_tick;
3625
3626 for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
3627 {
3628 Lisp_Object symbol;
3629 register struct Lisp_Process *p;
3630
3631 proc = Fcdr (Fcar (tail));
3632 p = XPROCESS (proc);
3633
3634 if (XINT (p->tick) != XINT (p->update_tick))
3635 {
3636 XSETINT (p->update_tick, XINT (p->tick));
3637
3638 /* If process is still active, read any output that remains. */
3639 while (! EQ (p->filter, Qt)
3640 && XINT (p->infd) >= 0
3641 && read_process_output (proc, XINT (p->infd)) > 0);
3642
3643 buffer = p->buffer;
3644
3645 /* Get the text to use for the message. */
3646 if (!NILP (p->raw_status_low))
3647 update_status (p);
3648 msg = status_message (p->status);
3649
3650 /* If process is terminated, deactivate it or delete it. */
3651 symbol = p->status;
3652 if (CONSP (p->status))
3653 symbol = XCONS (p->status)->car;
3654
3655 if (EQ (symbol, Qsignal) || EQ (symbol, Qexit)
3656 || EQ (symbol, Qclosed))
3657 {
3658 if (delete_exited_processes)
3659 remove_process (proc);
3660 else
3661 deactivate_process (proc);
3662 }
3663
3664 /* The actions above may have further incremented p->tick.
3665 So set p->update_tick again
3666 so that an error in the sentinel will not cause
3667 this code to be run again. */
3668 XSETINT (p->update_tick, XINT (p->tick));
3669 /* Now output the message suitably. */
3670 if (!NILP (p->sentinel))
3671 exec_sentinel (proc, msg);
3672 /* Don't bother with a message in the buffer
3673 when a process becomes runnable. */
3674 else if (!EQ (symbol, Qrun) && !NILP (buffer))
3675 {
3676 Lisp_Object ro, tem;
3677 struct buffer *old = current_buffer;
3678 int opoint;
3679
3680 ro = XBUFFER (buffer)->read_only;
3681
3682 /* Avoid error if buffer is deleted
3683 (probably that's why the process is dead, too) */
3684 if (NILP (XBUFFER (buffer)->name))
3685 continue;
3686 Fset_buffer (buffer);
3687 opoint = PT;
3688 /* Insert new output into buffer
3689 at the current end-of-output marker,
3690 thus preserving logical ordering of input and output. */
3691 if (XMARKER (p->mark)->buffer)
3692 SET_PT (marker_position (p->mark));
3693 else
3694 SET_PT (ZV);
3695 if (PT <= opoint)
3696 opoint += XSTRING (msg)->size + XSTRING (p->name)->size + 10;
3697
3698 tem = current_buffer->read_only;
3699 current_buffer->read_only = Qnil;
3700 insert_string ("\nProcess ");
3701 Finsert (1, &p->name);
3702 insert_string (" ");
3703 Finsert (1, &msg);
3704 current_buffer->read_only = tem;
3705 Fset_marker (p->mark, make_number (PT), p->buffer);
3706
3707 SET_PT (opoint);
3708 set_buffer_internal (old);
3709 }
3710 }
3711 } /* end for */
3712
3713 update_mode_lines++; /* in case buffers use %s in mode-line-format */
3714 redisplay_preserve_echo_area ();
3715
3716 UNGCPRO;
3717 }
3718 \f
3719 /* The first time this is called, assume keyboard input comes from DESC
3720 instead of from where we used to expect it.
3721 Subsequent calls mean assume input keyboard can come from DESC
3722 in addition to other places. */
3723
3724 static int add_keyboard_wait_descriptor_called_flag;
3725
3726 void
3727 add_keyboard_wait_descriptor (desc)
3728 int desc;
3729 {
3730 if (! add_keyboard_wait_descriptor_called_flag)
3731 FD_CLR (0, &input_wait_mask);
3732 add_keyboard_wait_descriptor_called_flag = 1;
3733 FD_SET (desc, &input_wait_mask);
3734 if (desc > max_keyboard_desc)
3735 max_keyboard_desc = desc;
3736 }
3737
3738 /* From now on, do not expect DESC to give keyboard input. */
3739
3740 void
3741 delete_keyboard_wait_descriptor (desc)
3742 int desc;
3743 {
3744 int fd;
3745 int lim = max_keyboard_desc;
3746
3747 FD_CLR (desc, &input_wait_mask);
3748
3749 if (desc == max_keyboard_desc)
3750 for (fd = 0; fd < lim; fd++)
3751 if (FD_ISSET (fd, &input_wait_mask)
3752 && !FD_ISSET (fd, &non_keyboard_wait_mask))
3753 max_keyboard_desc = fd;
3754 }
3755
3756 /* Return nonzero if *MASK has a bit set
3757 that corresponds to one of the keyboard input descriptors. */
3758
3759 int
3760 keyboard_bit_set (mask)
3761 SELECT_TYPE *mask;
3762 {
3763 int fd;
3764
3765 for (fd = 0; fd <= max_keyboard_desc; fd++)
3766 if (FD_ISSET (fd, mask) && FD_ISSET (fd, &input_wait_mask)
3767 && !FD_ISSET (fd, &non_keyboard_wait_mask))
3768 return 1;
3769
3770 return 0;
3771 }
3772 \f
3773 init_process ()
3774 {
3775 register int i;
3776
3777 #ifdef SIGCHLD
3778 #ifndef CANNOT_DUMP
3779 if (! noninteractive || initialized)
3780 #endif
3781 signal (SIGCHLD, sigchld_handler);
3782 #endif
3783
3784 FD_ZERO (&input_wait_mask);
3785 FD_ZERO (&non_keyboard_wait_mask);
3786 max_process_desc = 0;
3787
3788 FD_SET (0, &input_wait_mask);
3789
3790 Vprocess_alist = Qnil;
3791 for (i = 0; i < MAXDESC; i++)
3792 {
3793 chan_process[i] = Qnil;
3794 proc_buffered_char[i] = -1;
3795 }
3796 }
3797
3798 syms_of_process ()
3799 {
3800 Qprocessp = intern ("processp");
3801 staticpro (&Qprocessp);
3802 Qrun = intern ("run");
3803 staticpro (&Qrun);
3804 Qstop = intern ("stop");
3805 staticpro (&Qstop);
3806 Qsignal = intern ("signal");
3807 staticpro (&Qsignal);
3808
3809 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
3810 here again.
3811
3812 Qexit = intern ("exit");
3813 staticpro (&Qexit); */
3814
3815 Qopen = intern ("open");
3816 staticpro (&Qopen);
3817 Qclosed = intern ("closed");
3818 staticpro (&Qclosed);
3819
3820 Qlast_nonmenu_event = intern ("last-nonmenu-event");
3821 staticpro (&Qlast_nonmenu_event);
3822
3823 staticpro (&Vprocess_alist);
3824
3825 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
3826 "*Non-nil means delete processes immediately when they exit.\n\
3827 nil means don't delete them until `list-processes' is run.");
3828
3829 delete_exited_processes = 1;
3830
3831 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type,
3832 "Control type of device used to communicate with subprocesses.\n\
3833 Values are nil to use a pipe, or t or `pty' to use a pty.\n\
3834 The value has no effect if the system has no ptys or if all ptys are busy:\n\
3835 then a pipe is used in any case.\n\
3836 The value takes effect when `start-process' is called.");
3837 Vprocess_connection_type = Qt;
3838
3839 defsubr (&Sprocessp);
3840 defsubr (&Sget_process);
3841 defsubr (&Sget_buffer_process);
3842 defsubr (&Sdelete_process);
3843 defsubr (&Sprocess_status);
3844 defsubr (&Sprocess_exit_status);
3845 defsubr (&Sprocess_id);
3846 defsubr (&Sprocess_name);
3847 defsubr (&Sprocess_tty_name);
3848 defsubr (&Sprocess_command);
3849 defsubr (&Sset_process_buffer);
3850 defsubr (&Sprocess_buffer);
3851 defsubr (&Sprocess_mark);
3852 defsubr (&Sset_process_filter);
3853 defsubr (&Sprocess_filter);
3854 defsubr (&Sset_process_sentinel);
3855 defsubr (&Sprocess_sentinel);
3856 defsubr (&Sset_process_window_size);
3857 defsubr (&Sprocess_kill_without_query);
3858 defsubr (&Sprocess_contact);
3859 defsubr (&Slist_processes);
3860 defsubr (&Sprocess_list);
3861 defsubr (&Sstart_process);
3862 #ifdef HAVE_SOCKETS
3863 defsubr (&Sopen_network_stream);
3864 #endif /* HAVE_SOCKETS */
3865 defsubr (&Saccept_process_output);
3866 defsubr (&Sprocess_send_region);
3867 defsubr (&Sprocess_send_string);
3868 defsubr (&Sinterrupt_process);
3869 defsubr (&Skill_process);
3870 defsubr (&Squit_process);
3871 defsubr (&Sstop_process);
3872 defsubr (&Scontinue_process);
3873 defsubr (&Sprocess_send_eof);
3874 defsubr (&Ssignal_process);
3875 defsubr (&Swaiting_for_user_input_p);
3876 /* defsubr (&Sprocess_connection); */
3877 }
3878
3879 \f
3880 #else /* not subprocesses */
3881
3882 #include <sys/types.h>
3883 #include <errno.h>
3884
3885 #include "lisp.h"
3886 #include "systime.h"
3887 #include "termopts.h"
3888 #include "sysselect.h"
3889
3890 extern int frame_garbaged;
3891
3892 extern EMACS_TIME timer_check ();
3893 extern int timers_run;
3894
3895 /* As described above, except assuming that there are no subprocesses:
3896
3897 Wait for timeout to elapse and/or keyboard input to be available.
3898
3899 time_limit is:
3900 timeout in seconds, or
3901 zero for no limit, or
3902 -1 means gobble data immediately available but don't wait for any.
3903
3904 read_kbd is a Lisp_Object:
3905 0 to ignore keyboard input, or
3906 1 to return when input is available, or
3907 -1 means caller will actually read the input, so don't throw to
3908 the quit handler.
3909 a cons cell, meaning wait until its car is non-nil
3910 (and gobble terminal input into the buffer if any arrives), or
3911 We know that read_kbd will never be a Lisp_Process, since
3912 `subprocesses' isn't defined.
3913
3914 do_display != 0 means redisplay should be done to show subprocess
3915 output that arrives.
3916
3917 Return true iff we received input from any process. */
3918
3919 int
3920 wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
3921 int time_limit, microsecs;
3922 Lisp_Object read_kbd;
3923 int do_display;
3924 {
3925 EMACS_TIME end_time, timeout;
3926 SELECT_TYPE waitchannels;
3927 int xerrno;
3928 Lisp_Object *wait_for_cell = 0;
3929
3930 /* If waiting for non-nil in a cell, record where. */
3931 if (CONSP (read_kbd))
3932 {
3933 wait_for_cell = &XCONS (read_kbd)->car;
3934 XSETFASTINT (read_kbd, 0);
3935 }
3936
3937 /* What does time_limit really mean? */
3938 if (time_limit || microsecs)
3939 {
3940 if (time_limit == -1)
3941 /* In fact, it's zero. */
3942 EMACS_SET_SECS_USECS (timeout, 0, 0);
3943 else
3944 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
3945
3946 /* How far in the future is that? */
3947 EMACS_GET_TIME (end_time);
3948 EMACS_ADD_TIME (end_time, end_time, timeout);
3949 }
3950 else
3951 /* It's infinite. */
3952 EMACS_SET_SECS_USECS (timeout, 100000, 0);
3953
3954 /* Turn off periodic alarms (in case they are in use)
3955 because the select emulator uses alarms. */
3956 stop_polling ();
3957
3958 for (;;)
3959 {
3960 int nfds;
3961 int timeout_reduced_for_timers = 0;
3962
3963 /* If calling from keyboard input, do not quit
3964 since we want to return C-g as an input character.
3965 Otherwise, do pending quit if requested. */
3966 if (XINT (read_kbd) >= 0)
3967 QUIT;
3968
3969 /* Exit now if the cell we're waiting for became non-nil. */
3970 if (wait_for_cell && ! NILP (*wait_for_cell))
3971 break;
3972
3973 /* Compute time from now till when time limit is up */
3974 /* Exit if already run out */
3975 if (time_limit > 0 || microsecs)
3976 {
3977 EMACS_GET_TIME (timeout);
3978 EMACS_SUB_TIME (timeout, end_time, timeout);
3979 if (EMACS_TIME_NEG_P (timeout))
3980 break;
3981 }
3982
3983 /* If our caller will not immediately handle keyboard events,
3984 run timer events directly.
3985 (Callers that will immediately read keyboard events
3986 call timer_delay on their own.) */
3987 if (! wait_for_cell)
3988 {
3989 EMACS_TIME timer_delay;
3990 int old_timers_run;
3991
3992 retry:
3993 old_timers_run = timers_run;
3994 timer_delay = timer_check (1);
3995 if (timers_run != old_timers_run && do_display)
3996 {
3997 redisplay_preserve_echo_area ();
3998 /* We must retry, since a timer may have requeued itself
3999 and that could alter the time delay. */
4000 goto retry;
4001 }
4002
4003 if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1)
4004 {
4005 EMACS_TIME difference;
4006 EMACS_SUB_TIME (difference, timer_delay, timeout);
4007 if (EMACS_TIME_NEG_P (difference))
4008 {
4009 timeout = timer_delay;
4010 timeout_reduced_for_timers = 1;
4011 }
4012 }
4013 }
4014
4015 /* Cause C-g and alarm signals to take immediate action,
4016 and cause input available signals to zero out timeout. */
4017 if (XINT (read_kbd) < 0)
4018 set_waiting_for_input (&timeout);
4019
4020 /* Wait till there is something to do. */
4021
4022 if (! XINT (read_kbd) && wait_for_cell == 0)
4023 FD_ZERO (&waitchannels);
4024 else
4025 FD_SET (0, &waitchannels);
4026
4027 /* If a frame has been newly mapped and needs updating,
4028 reprocess its display stuff. */
4029 if (frame_garbaged && do_display)
4030 {
4031 clear_waiting_for_input ();
4032 redisplay_preserve_echo_area ();
4033 if (XINT (read_kbd) < 0)
4034 set_waiting_for_input (&timeout);
4035 }
4036
4037 if (XINT (read_kbd) && detect_input_pending ())
4038 {
4039 nfds = 0;
4040 FD_ZERO (&waitchannels);
4041 }
4042 else
4043 nfds = select (1, &waitchannels, (SELECT_TYPE *)0, (SELECT_TYPE *)0,
4044 &timeout);
4045
4046 xerrno = errno;
4047
4048 /* Make C-g and alarm signals set flags again */
4049 clear_waiting_for_input ();
4050
4051 /* If we woke up due to SIGWINCH, actually change size now. */
4052 do_pending_window_change ();
4053
4054 if (time_limit && nfds == 0 && ! timeout_reduced_for_timers)
4055 /* We waited the full specified time, so return now. */
4056 break;
4057
4058 if (nfds == -1)
4059 {
4060 /* If the system call was interrupted, then go around the
4061 loop again. */
4062 if (xerrno == EINTR)
4063 FD_ZERO (&waitchannels);
4064 else
4065 error ("select error: %s", strerror (xerrno));
4066 }
4067 #ifdef sun
4068 else if (nfds > 0 && (waitchannels & 1) && interrupt_input)
4069 /* System sometimes fails to deliver SIGIO. */
4070 kill (getpid (), SIGIO);
4071 #endif
4072 #ifdef SIGIO
4073 if (XINT (read_kbd) && interrupt_input && (waitchannels & 1))
4074 kill (getpid (), SIGIO);
4075 #endif
4076
4077 /* Check for keyboard input */
4078
4079 if ((XINT (read_kbd) != 0)
4080 && detect_input_pending_run_timers (do_display))
4081 {
4082 swallow_events (do_display);
4083 if (detect_input_pending_run_timers (do_display))
4084 break;
4085 }
4086
4087 /* If wait_for_cell. check for keyboard input
4088 but don't run any timers.
4089 ??? (It seems wrong to me to check for keyboard
4090 input at all when wait_for_cell, but the code
4091 has been this way since July 1994.
4092 Try changing this after version 19.31.) */
4093 if (wait_for_cell
4094 && detect_input_pending ())
4095 {
4096 swallow_events (do_display);
4097 if (detect_input_pending ())
4098 break;
4099 }
4100
4101 /* Exit now if the cell we're waiting for became non-nil. */
4102 if (wait_for_cell && ! NILP (*wait_for_cell))
4103 break;
4104 }
4105
4106 start_polling ();
4107
4108 return 0;
4109 }
4110
4111
4112 DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
4113 /* Don't confuse make-docfile by having two doc strings for this function.
4114 make-docfile does not pay attention to #if, for good reason! */
4115 0)
4116 (name)
4117 register Lisp_Object name;
4118 {
4119 return Qnil;
4120 }
4121
4122 /* Kill all processes associated with `buffer'.
4123 If `buffer' is nil, kill all processes.
4124 Since we have no subprocesses, this does nothing. */
4125
4126 kill_buffer_processes (buffer)
4127 Lisp_Object buffer;
4128 {
4129 }
4130
4131 init_process ()
4132 {
4133 }
4134
4135 syms_of_process ()
4136 {
4137 defsubr (&Sget_buffer_process);
4138 }
4139
4140 \f
4141 #endif /* not subprocesses */