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