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