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