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