(read_process_output): Make sure the current buffer is
[bpt/emacs.git] / src / process.c
CommitLineData
d0d6b7c5 1/* Asynchronous subprocess control for GNU Emacs.
0b5538bd 2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
63136da6 3 1996, 1998, 1999, 2001, 2002, 2003, 2004,
6a1bab0e 4 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
d0d6b7c5
JB
5
6This file is part of GNU Emacs.
7
9ec0b715 8GNU Emacs is free software: you can redistribute it and/or modify
d0d6b7c5 9it under the terms of the GNU General Public License as published by
9ec0b715
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
d0d6b7c5
JB
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9ec0b715 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
d0d6b7c5
JB
20
21
18160b98 22#include <config.h>
68c45bf0
PE
23#include <signal.h>
24
6720a7fb
JB
25/* This file is split into two parts by the following preprocessor
26 conditional. The 'then' clause contains all of the support for
27 asynchronous subprocesses. The 'else' clause contains stub
28 versions of some of the asynchronous subprocess routines that are
29 often called elsewhere in Emacs, so we don't have to #ifdef the
30 sections that call them. */
31
32\f
d0d6b7c5 33#ifdef subprocesses
d0d6b7c5
JB
34
35#include <stdio.h>
36#include <errno.h>
37#include <setjmp.h>
38#include <sys/types.h> /* some typedefs are used in sys/file.h */
39#include <sys/file.h>
40#include <sys/stat.h>
2026418c
YM
41#ifdef HAVE_INTTYPES_H
42#include <inttypes.h>
43#endif
63136da6
AS
44#ifdef STDC_HEADERS
45#include <stdlib.h>
46#endif
934e2a68 47
93b4f699
RS
48#ifdef HAVE_UNISTD_H
49#include <unistd.h>
50#endif
e98d950b 51#include <fcntl.h>
e98d950b 52
d0d6b7c5
JB
53#ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
54#include <sys/socket.h>
55#include <netdb.h>
56#include <netinet/in.h>
57#include <arpa/inet.h>
e690ca94
KS
58
59/* Are local (unix) sockets supported? */
4624371d 60#if defined (HAVE_SYS_UN_H)
e690ca94
KS
61#if !defined (AF_LOCAL) && defined (AF_UNIX)
62#define AF_LOCAL AF_UNIX
63#endif
64#ifdef AF_LOCAL
65#define HAVE_LOCAL_SOCKETS
66#include <sys/un.h>
67#endif
68#endif
d0d6b7c5
JB
69#endif /* HAVE_SOCKETS */
70
e39a993c 71#if defined(BSD_SYSTEM)
d0d6b7c5 72#include <sys/ioctl.h>
0ad77c54 73#if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
d0d6b7c5
JB
74#include <fcntl.h>
75#endif /* HAVE_PTYS and no O_NDELAY */
e39a993c 76#endif /* BSD_SYSTEM */
d0d6b7c5
JB
77
78#ifdef NEED_BSDTTY
79#include <bsdtty.h>
80#endif
81
6b61353c
KH
82/* Can we use SIOCGIFCONF and/or SIOCGIFADDR */
83#ifdef HAVE_SOCKETS
84#if defined(HAVE_SYS_IOCTL_H) && defined(HAVE_NET_IF_H)
85/* sys/ioctl.h may have been included already */
86#ifndef SIOCGIFADDR
87#include <sys/ioctl.h>
88#endif
89#include <net/if.h>
90#endif
91#endif
92
e62cab61
DL
93#ifdef HAVE_SYS_WAIT
94#include <sys/wait.h>
95#endif
d0d6b7c5 96
08116b48
CY
97#ifdef HAVE_RES_INIT
98#include <netinet/in.h>
99#include <arpa/nameser.h>
100#include <resolv.h>
101#endif
102
fa8459a3 103#include "lisp.h"
d0d6b7c5 104#include "systime.h"
36ebaafa 105#include "systty.h"
d0d6b7c5 106
d0d6b7c5
JB
107#include "window.h"
108#include "buffer.h"
5bbf78c1 109#include "character.h"
0fa1789e 110#include "coding.h"
d0d6b7c5 111#include "process.h"
428a555e 112#include "frame.h"
d0d6b7c5
JB
113#include "termhooks.h"
114#include "termopts.h"
115#include "commands.h"
3ec68006 116#include "keyboard.h"
ececcbec 117#include "blockinput.h"
dfcf069d 118#include "dispextern.h"
e0016554 119#include "composite.h"
30904ab7 120#include "atimer.h"
d0d6b7c5 121
dd2281ae 122Lisp_Object Qprocessp;
324e68ba 123Lisp_Object Qrun, Qstop, Qsignal;
e690ca94 124Lisp_Object Qopen, Qclosed, Qconnect, Qfailed, Qlisten;
e1652a86 125Lisp_Object Qlocal, Qipv4, Qdatagram;
d888760c 126Lisp_Object Qreal, Qnetwork, Qserial;
e1652a86
KS
127#ifdef AF_INET6
128Lisp_Object Qipv6;
129#endif
d888760c
GM
130Lisp_Object QCport, QCspeed, QCprocess;
131Lisp_Object QCbytesize, QCstopbits, QCparity, Qodd, Qeven;
132Lisp_Object QCflowcontrol, Qhw, Qsw, QCsummary;
9057ff80 133Lisp_Object QCname, QCbuffer, QChost, QCservice, QCtype;
e690ca94 134Lisp_Object QClocal, QCremote, QCcoding;
9057ff80 135Lisp_Object QCserver, QCnowait, QCnoquery, QCstop;
faa7db08 136Lisp_Object QCsentinel, QClog, QCoptions, QCplist;
6545aada 137Lisp_Object Qlast_nonmenu_event;
3635ecad
JR
138/* QCfamily is declared and initialized in xfaces.c,
139 QCfilter in keyboard.c. */
140extern Lisp_Object QCfamily, QCfilter;
141
d0d6b7c5
JB
142/* Qexit is declared and initialized in eval.c. */
143
e0f712ba
AC
144/* QCfamily is defined in xfaces.c. */
145extern Lisp_Object QCfamily;
146/* QCfilter is defined in keyboard.c. */
147extern Lisp_Object QCfilter;
148
934e2a68
EZ
149Lisp_Object Qeuid, Qegid, Qcomm, Qstate, Qppid, Qpgrp, Qsess, Qttname, Qtpgid;
150Lisp_Object Qminflt, Qmajflt, Qcminflt, Qcmajflt, Qutime, Qstime, Qcstime;
151Lisp_Object Qcutime, Qpri, Qnice, Qthcount, Qstart, Qvsize, Qrss, Qargs;
89c874cf 152Lisp_Object Quser, Qgroup, Qetime, Qpcpu, Qpmem, Qtime, Qctime;
934e2a68 153
d0d6b7c5 154#ifdef HAVE_SOCKETS
d888760c
GM
155#define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
156#define NETCONN1_P(p) (EQ ((p)->type, Qnetwork))
157#define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial))
158#define SERIALCONN1_P(p) (EQ ((p)->type, Qserial))
d0d6b7c5
JB
159#else
160#define NETCONN_P(p) 0
e690ca94 161#define NETCONN1_P(p) 0
d888760c
GM
162#define SERIALCONN_P(p) 0
163#define SERIALCONN1_P(p) 0
d0d6b7c5
JB
164#endif /* HAVE_SOCKETS */
165
166/* Define first descriptor number available for subprocesses. */
d0d6b7c5 167#define FIRST_PROC_DESC 3
d0d6b7c5
JB
168
169/* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
170 testing SIGCHLD. */
171
172#if !defined (SIGCHLD) && defined (SIGCLD)
173#define SIGCHLD SIGCLD
174#endif /* SIGCLD */
175
176#include "syssignal.h"
177
889255b4 178#include "syswait.h"
d0d6b7c5 179
d9e7c622 180extern char *get_operating_system_release ();
41d03b9a 181
d888760c
GM
182/* Serial processes require termios or Windows. */
183#if defined (HAVE_TERMIOS) || defined (WINDOWSNT)
184#define HAVE_SERIAL
185#endif
186
187#ifdef HAVE_SERIAL
188/* From sysdep.c or w32.c */
189extern int serial_open (char *port);
190extern void serial_configure (struct Lisp_Process *p, Lisp_Object contact);
191#endif
192
3ec68006 193#ifndef USE_CRT_DLL
b062d1fe 194extern int errno;
3ec68006 195#endif
d0d6b7c5 196
5f0929a7
RS
197#ifndef HAVE_H_ERRNO
198extern int h_errno;
199#endif
200
d0d6b7c5
JB
201/* t means use pty, nil means use a pipe,
202 maybe other values to come. */
dd2281ae 203static Lisp_Object Vprocess_connection_type;
d0d6b7c5 204
17d02632
KH
205/* These next two vars are non-static since sysdep.c uses them in the
206 emulation of `select'. */
d0d6b7c5 207/* Number of events of change of status of a process. */
17d02632 208int process_tick;
d0d6b7c5 209/* Number of events for which the user or sentinel has been notified. */
17d02632 210int update_tick;
d0d6b7c5 211
dd2a17ab
KS
212/* Define NON_BLOCKING_CONNECT if we can support non-blocking connects. */
213
214#ifdef BROKEN_NON_BLOCKING_CONNECT
215#undef NON_BLOCKING_CONNECT
216#else
217#ifndef NON_BLOCKING_CONNECT
218#ifdef HAVE_SOCKETS
219#ifdef HAVE_SELECT
220#if defined (HAVE_GETPEERNAME) || defined (GNU_LINUX)
221#if defined (O_NONBLOCK) || defined (O_NDELAY)
222#if defined (EWOULDBLOCK) || defined (EINPROGRESS)
223#define NON_BLOCKING_CONNECT
224#endif /* EWOULDBLOCK || EINPROGRESS */
225#endif /* O_NONBLOCK || O_NDELAY */
226#endif /* HAVE_GETPEERNAME || GNU_LINUX */
227#endif /* HAVE_SELECT */
228#endif /* HAVE_SOCKETS */
229#endif /* NON_BLOCKING_CONNECT */
230#endif /* BROKEN_NON_BLOCKING_CONNECT */
231
e690ca94
KS
232/* Define DATAGRAM_SOCKETS if datagrams can be used safely on
233 this system. We need to read full packets, so we need a
234 "non-destructive" select. So we require either native select,
235 or emulation of select using FIONREAD. */
236
e690ca94
KS
237#ifdef BROKEN_DATAGRAM_SOCKETS
238#undef DATAGRAM_SOCKETS
239#else
240#ifndef DATAGRAM_SOCKETS
241#ifdef HAVE_SOCKETS
242#if defined (HAVE_SELECT) || defined (FIONREAD)
243#if defined (HAVE_SENDTO) && defined (HAVE_RECVFROM) && defined (EMSGSIZE)
244#define DATAGRAM_SOCKETS
245#endif /* HAVE_SENDTO && HAVE_RECVFROM && EMSGSIZE */
246#endif /* HAVE_SELECT || FIONREAD */
247#endif /* HAVE_SOCKETS */
248#endif /* DATAGRAM_SOCKETS */
249#endif /* BROKEN_DATAGRAM_SOCKETS */
250
6b61353c
KH
251#if !defined (ADAPTIVE_READ_BUFFERING) && !defined (NO_ADAPTIVE_READ_BUFFERING)
252#ifdef EMACS_HAS_USECS
253#define ADAPTIVE_READ_BUFFERING
254#endif
255#endif
256
257#ifdef ADAPTIVE_READ_BUFFERING
258#define READ_OUTPUT_DELAY_INCREMENT 10000
259#define READ_OUTPUT_DELAY_MAX (READ_OUTPUT_DELAY_INCREMENT * 5)
260#define READ_OUTPUT_DELAY_MAX_MAX (READ_OUTPUT_DELAY_INCREMENT * 7)
261
db853b7a
KS
262/* Number of processes which have a non-zero read_output_delay,
263 and therefore might be delayed for adaptive read buffering. */
6b61353c
KH
264
265static int process_output_delay_count;
266
db853b7a 267/* Non-zero if any process has non-nil read_output_skip. */
6b61353c
KH
268
269static int process_output_skip;
270
271/* Non-nil means to delay reading process output to improve buffering.
272 A value of t means that delay is reset after each send, any other
db853b7a
KS
273 non-nil value does not reset the delay. A value of nil disables
274 adaptive read buffering completely. */
6b61353c
KH
275static Lisp_Object Vprocess_adaptive_read_buffering;
276#else
277#define process_output_delay_count 0
278#endif
279
e690ca94 280
5886acf9 281#include "sysselect.h"
d0d6b7c5 282
ff6daed3
KS
283static int keyboard_bit_set P_ ((SELECT_TYPE *));
284static void deactivate_process P_ ((Lisp_Object));
285static void status_notify P_ ((struct Lisp_Process *));
286static int read_process_output P_ ((Lisp_Object, int));
41d03b9a 287
583dcae4 288/* If we support a window system, turn on the code to poll periodically
44ade2e9 289 to detect C-g. It isn't actually used when doing interrupt input. */
583dcae4 290#ifdef HAVE_WINDOW_SYSTEM
44ade2e9
RS
291#define POLL_FOR_INPUT
292#endif
293
84af9896
RS
294static Lisp_Object get_process ();
295static void exec_sentinel ();
296
297extern EMACS_TIME timer_check ();
298extern int timers_run;
299\f
a69281ff 300/* Mask of bits indicating the descriptors that we wait for input on. */
d0d6b7c5 301
dd2281ae
RS
302static SELECT_TYPE input_wait_mask;
303
994d9841 304/* Mask that excludes keyboard input descriptor(s). */
a69281ff
RS
305
306static SELECT_TYPE non_keyboard_wait_mask;
307
994d9841 308/* Mask that excludes process input descriptor(s). */
b5dc1c83
RS
309
310static SELECT_TYPE non_process_wait_mask;
311
994d9841
NR
312/* Mask for the gpm mouse input descriptor. */
313
314static SELECT_TYPE gpm_wait_mask;
315
bad49fc7 316#ifdef NON_BLOCKING_CONNECT
dd2a17ab
KS
317/* Mask of bits indicating the descriptors that we wait for connect to
318 complete on. Once they complete, they are removed from this mask
319 and added to the input_wait_mask and non_keyboard_wait_mask. */
320
321static SELECT_TYPE connect_wait_mask;
322
323/* Number of bits set in connect_wait_mask. */
324static int num_pending_connects;
325
bad49fc7
KS
326#define IF_NON_BLOCKING_CONNECT(s) s
327#else
328#define IF_NON_BLOCKING_CONNECT(s)
329#endif
330
7d0e672e
RS
331/* The largest descriptor currently in use for a process object. */
332static int max_process_desc;
333
a69281ff
RS
334/* The largest descriptor currently in use for keyboard input. */
335static int max_keyboard_desc;
d0d6b7c5 336
994d9841
NR
337/* The largest descriptor currently in use for gpm mouse input. */
338static int max_gpm_desc;
339
dd2281ae
RS
340/* Nonzero means delete a process right away if it exits. */
341static int delete_exited_processes;
d0d6b7c5
JB
342
343/* Indexed by descriptor, gives the process (if any) for that descriptor */
41f3aa98 344Lisp_Object chan_process[MAXDESC];
d0d6b7c5
JB
345
346/* Alist of elements (NAME . PROCESS) */
41f3aa98 347Lisp_Object Vprocess_alist;
d0d6b7c5
JB
348
349/* Buffered-ahead input char from process, indexed by channel.
350 -1 means empty (no char is buffered).
351 Used on sys V where the only way to tell if there is any
352 output from the process is to read at least one char.
353 Always -1 on systems that support FIONREAD. */
354
e98d950b
RS
355/* Don't make static; need to access externally. */
356int proc_buffered_char[MAXDESC];
dd2281ae 357
0fa1789e 358/* Table of `struct coding-system' for each process. */
c7580538
KH
359static struct coding_system *proc_decode_coding_system[MAXDESC];
360static struct coding_system *proc_encode_coding_system[MAXDESC];
0fa1789e 361
e690ca94
KS
362#ifdef DATAGRAM_SOCKETS
363/* Table of `partner address' for datagram sockets. */
364struct sockaddr_and_len {
365 struct sockaddr *sa;
366 int len;
367} datagram_address[MAXDESC];
368#define DATAGRAM_CHAN_P(chan) (datagram_address[chan].sa != 0)
60f0fb11 369#define DATAGRAM_CONN_P(proc) (PROCESSP (proc) && datagram_address[XPROCESS (proc)->infd].sa != 0)
e690ca94
KS
370#else
371#define DATAGRAM_CHAN_P(chan) (0)
372#define DATAGRAM_CONN_P(proc) (0)
373#endif
374
93b4f699
RS
375/* Maximum number of bytes to send to a pty without an eof. */
376static int pty_max_bytes;
3b9a3dfa 377
84af9896
RS
378/* Nonzero means don't run process sentinels. This is used
379 when exiting. */
380int inhibit_sentinels;
381
875e6b94 382#ifdef HAVE_PTYS
e62cab61
DL
383#ifdef HAVE_PTY_H
384#include <pty.h>
385#endif
875e6b94 386/* The file name of the pty opened by allocate_pty. */
3b9a3dfa
RS
387
388static char pty_name[24];
875e6b94 389#endif
d0d6b7c5
JB
390\f
391/* Compute the Lisp form of the process status, p->status, from
392 the numeric status that was returned by `wait'. */
393
bba104c1 394static Lisp_Object status_convert (int);
f9738840 395
ff6daed3 396static void
d0d6b7c5
JB
397update_status (p)
398 struct Lisp_Process *p;
399{
6bfd98e7 400 eassert (p->raw_status_new);
bba104c1 401 p->status = status_convert (p->raw_status);
6bfd98e7 402 p->raw_status_new = 0;
d0d6b7c5
JB
403}
404
177c0ea7 405/* Convert a process status word in Unix format to
d0d6b7c5
JB
406 the list that we use internally. */
407
ff6daed3 408static Lisp_Object
bba104c1 409status_convert (int w)
d0d6b7c5
JB
410{
411 if (WIFSTOPPED (w))
412 return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil));
413 else if (WIFEXITED (w))
414 return Fcons (Qexit, Fcons (make_number (WRETCODE (w)),
415 WCOREDUMP (w) ? Qt : Qnil));
416 else if (WIFSIGNALED (w))
417 return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)),
418 WCOREDUMP (w) ? Qt : Qnil));
419 else
420 return Qrun;
421}
422
423/* Given a status-list, extract the three pieces of information
424 and store them individually through the three pointers. */
425
ff6daed3 426static void
d0d6b7c5
JB
427decode_status (l, symbol, code, coredump)
428 Lisp_Object l;
429 Lisp_Object *symbol;
430 int *code;
431 int *coredump;
432{
433 Lisp_Object tem;
434
bcd69aea 435 if (SYMBOLP (l))
d0d6b7c5
JB
436 {
437 *symbol = l;
438 *code = 0;
439 *coredump = 0;
440 }
441 else
442 {
70949dac
KR
443 *symbol = XCAR (l);
444 tem = XCDR (l);
445 *code = XFASTINT (XCAR (tem));
446 tem = XCDR (tem);
d0d6b7c5
JB
447 *coredump = !NILP (tem);
448 }
449}
450
451/* Return a string describing a process status list. */
452
116c423c
KS
453static Lisp_Object
454status_message (p)
455 struct Lisp_Process *p;
d0d6b7c5 456{
116c423c 457 Lisp_Object status = p->status;
d0d6b7c5
JB
458 Lisp_Object symbol;
459 int code, coredump;
460 Lisp_Object string, string2;
461
462 decode_status (status, &symbol, &code, &coredump);
463
464 if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
465 {
68c45bf0 466 char *signame;
ca9c0567 467 synchronize_system_messages_locale ();
68c45bf0 468 signame = strsignal (code);
b97ad9ea
RS
469 if (signame == 0)
470 signame = "unknown";
471 string = build_string (signame);
d0d6b7c5 472 string2 = build_string (coredump ? " (core dumped)\n" : "\n");
a814cc69 473 SSET (string, 0, DOWNCASE (SREF (string, 0)));
d0d6b7c5
JB
474 return concat2 (string, string2);
475 }
476 else if (EQ (symbol, Qexit))
477 {
116c423c
KS
478 if (NETCONN1_P (p))
479 return build_string (code == 0 ? "deleted\n" : "connection broken by remote peer\n");
d0d6b7c5
JB
480 if (code == 0)
481 return build_string ("finished\n");
f2980264 482 string = Fnumber_to_string (make_number (code));
d0d6b7c5 483 string2 = build_string (coredump ? " (core dumped)\n" : "\n");
e690ca94
KS
484 return concat3 (build_string ("exited abnormally with code "),
485 string, string2);
d0d6b7c5 486 }
dd2a17ab
KS
487 else if (EQ (symbol, Qfailed))
488 {
489 string = Fnumber_to_string (make_number (code));
490 string2 = build_string ("\n");
e690ca94
KS
491 return concat3 (build_string ("failed with code "),
492 string, string2);
dd2a17ab 493 }
d0d6b7c5
JB
494 else
495 return Fcopy_sequence (Fsymbol_name (symbol));
496}
497\f
498#ifdef HAVE_PTYS
d0d6b7c5 499
875e6b94
KH
500/* Open an available pty, returning a file descriptor.
501 Return -1 on failure.
502 The file name of the terminal corresponding to the pty
503 is left in the variable pty_name. */
504
ff6daed3 505static int
d0d6b7c5
JB
506allocate_pty ()
507{
dfcf069d 508 register int c, i;
d0d6b7c5
JB
509 int fd;
510
511#ifdef PTY_ITERATION
512 PTY_ITERATION
513#else
514 for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
515 for (i = 0; i < 16; i++)
516#endif
517 {
26d7389d 518 struct stat stb; /* Used in some PTY_OPEN. */
d0d6b7c5
JB
519#ifdef PTY_NAME_SPRINTF
520 PTY_NAME_SPRINTF
d0d6b7c5
JB
521#else
522 sprintf (pty_name, "/dev/pty%c%x", c, i);
d0d6b7c5
JB
523#endif /* no PTY_NAME_SPRINTF */
524
4d7c105e
RS
525#ifdef PTY_OPEN
526 PTY_OPEN;
527#else /* no PTY_OPEN */
34967d0f 528 {
34967d0f
SM
529 { /* Some systems name their pseudoterminals so that there are gaps in
530 the usual sequence - for example, on HP9000/S700 systems, there
531 are no pseudoterminals with names ending in 'f'. So we wait for
532 three failures in a row before deciding that we've reached the
533 end of the ptys. */
534 int failed_count = 0;
f3d9532b 535
34967d0f
SM
536 if (stat (pty_name, &stb) < 0)
537 {
538 failed_count++;
539 if (failed_count >= 3)
540 return -1;
541 }
542 else
543 failed_count = 0;
32676c08 544 }
34967d0f
SM
545# ifdef O_NONBLOCK
546 fd = emacs_open (pty_name, O_RDWR | O_NONBLOCK, 0);
547# else
548 fd = emacs_open (pty_name, O_RDWR | O_NDELAY, 0);
549# endif
34967d0f 550 }
4d7c105e 551#endif /* no PTY_OPEN */
d0d6b7c5
JB
552
553 if (fd >= 0)
554 {
555 /* check to make certain that both sides are available
556 this avoids a nasty yet stupid bug in rlogins */
557#ifdef PTY_TTY_NAME_SPRINTF
558 PTY_TTY_NAME_SPRINTF
d0d6b7c5 559#else
63136da6 560 sprintf (pty_name, "/dev/tty%c%x", c, i);
d0d6b7c5 561#endif /* no PTY_TTY_NAME_SPRINTF */
d0d6b7c5
JB
562 if (access (pty_name, 6) != 0)
563 {
68c45bf0 564 emacs_close (fd);
132d0475 565# ifndef __sgi
d0d6b7c5 566 continue;
34967d0f 567# else
d0d6b7c5 568 return -1;
132d0475 569# endif /* __sgi */
d0d6b7c5 570 }
d0d6b7c5
JB
571 setup_pty (fd);
572 return fd;
573 }
574 }
575 return -1;
576}
577#endif /* HAVE_PTYS */
578\f
ff6daed3 579static Lisp_Object
d0d6b7c5
JB
580make_process (name)
581 Lisp_Object name;
582{
583 register Lisp_Object val, tem, name1;
584 register struct Lisp_Process *p;
585 char suffix[10];
586 register int i;
587
98423852 588 p = allocate_process ();
23d6bb9c 589
60f0fb11
SM
590 p->infd = -1;
591 p->outfd = -1;
592 p->tick = 0;
593 p->update_tick = 0;
6bfd98e7 594 p->pid = 0;
fd41ad98 595 p->pty_flag = 0;
6bfd98e7 596 p->raw_status_new = 0;
d0d6b7c5
JB
597 p->status = Qrun;
598 p->mark = Fmake_marker ();
847c048e 599 p->kill_without_query = 0;
d0d6b7c5 600
6b61353c 601#ifdef ADAPTIVE_READ_BUFFERING
60f0fb11
SM
602 p->adaptive_read_buffering = 0;
603 p->read_output_delay = 0;
604 p->read_output_skip = 0;
6b61353c
KH
605#endif
606
d0d6b7c5
JB
607 /* If name is already in use, modify it until it is unused. */
608
609 name1 = name;
610 for (i = 1; ; i++)
611 {
612 tem = Fget_process (name1);
613 if (NILP (tem)) break;
614 sprintf (suffix, "<%d>", i);
615 name1 = concat2 (name, build_string (suffix));
616 }
617 name = name1;
618 p->name = name;
23d6bb9c 619 XSETPROCESS (val, p);
d0d6b7c5
JB
620 Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
621 return val;
622}
623
ff6daed3 624static void
d0d6b7c5
JB
625remove_process (proc)
626 register Lisp_Object proc;
627{
628 register Lisp_Object pair;
629
630 pair = Frassq (proc, Vprocess_alist);
631 Vprocess_alist = Fdelq (pair, Vprocess_alist);
d0d6b7c5
JB
632
633 deactivate_process (proc);
634}
03f04413
KH
635
636/* Setup coding systems of PROCESS. */
637
638void
639setup_process_coding_systems (process)
640 Lisp_Object process;
641{
642 struct Lisp_Process *p = XPROCESS (process);
60f0fb11
SM
643 int inch = p->infd;
644 int outch = p->outfd;
8f924df7 645 Lisp_Object coding_system;
03f04413 646
20f1ef2e
KH
647 if (inch < 0 || outch < 0)
648 return;
649
03f04413
KH
650 if (!proc_decode_coding_system[inch])
651 proc_decode_coding_system[inch]
652 = (struct coding_system *) xmalloc (sizeof (struct coding_system));
8f924df7 653 coding_system = p->decode_coding_system;
03f04413 654 if (! NILP (p->filter))
5431bd6a 655 ;
03f04413
KH
656 else if (BUFFERP (p->buffer))
657 {
658 if (NILP (XBUFFER (p->buffer)->enable_multibyte_characters))
8f924df7 659 coding_system = raw_text_coding_system (coding_system);
03f04413 660 }
8f924df7 661 setup_coding_system (coding_system, proc_decode_coding_system[inch]);
03f04413
KH
662
663 if (!proc_encode_coding_system[outch])
664 proc_encode_coding_system[outch]
665 = (struct coding_system *) xmalloc (sizeof (struct coding_system));
666 setup_coding_system (p->encode_coding_system,
667 proc_encode_coding_system[outch]);
668}
d0d6b7c5
JB
669\f
670DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0,
fdb82f93
PJ
671 doc: /* Return t if OBJECT is a process. */)
672 (object)
4ee3e309 673 Lisp_Object object;
d0d6b7c5 674{
4ee3e309 675 return PROCESSP (object) ? Qt : Qnil;
d0d6b7c5
JB
676}
677
678DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0,
fdb82f93
PJ
679 doc: /* Return the process named NAME, or nil if there is none. */)
680 (name)
d0d6b7c5
JB
681 register Lisp_Object name;
682{
bcd69aea 683 if (PROCESSP (name))
d0d6b7c5 684 return name;
b7826503 685 CHECK_STRING (name);
d0d6b7c5
JB
686 return Fcdr (Fassoc (name, Vprocess_alist));
687}
688
689DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
fdb82f93
PJ
690 doc: /* Return the (or a) process associated with BUFFER.
691BUFFER may be a buffer or the name of one. */)
692 (buffer)
4ee3e309 693 register Lisp_Object buffer;
d0d6b7c5
JB
694{
695 register Lisp_Object buf, tail, proc;
696
4ee3e309
EN
697 if (NILP (buffer)) return Qnil;
698 buf = Fget_buffer (buffer);
d0d6b7c5
JB
699 if (NILP (buf)) return Qnil;
700
99784d63 701 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
d0d6b7c5 702 {
99784d63 703 proc = Fcdr (XCAR (tail));
bcd69aea 704 if (PROCESSP (proc) && EQ (XPROCESS (proc)->buffer, buf))
d0d6b7c5
JB
705 return proc;
706 }
707 return Qnil;
708}
709
ebb9e16f
JB
710/* This is how commands for the user decode process arguments. It
711 accepts a process, a process name, a buffer, a buffer name, or nil.
712 Buffers denote the first process in the buffer, and nil denotes the
713 current buffer. */
d0d6b7c5 714
77b221b1 715static Lisp_Object
d0d6b7c5
JB
716get_process (name)
717 register Lisp_Object name;
718{
1619761d
KH
719 register Lisp_Object proc, obj;
720 if (STRINGP (name))
721 {
722 obj = Fget_process (name);
723 if (NILP (obj))
724 obj = Fget_buffer (name);
725 if (NILP (obj))
d5db4077 726 error ("Process %s does not exist", SDATA (name));
1619761d
KH
727 }
728 else if (NILP (name))
729 obj = Fcurrent_buffer ();
d0d6b7c5 730 else
1619761d
KH
731 obj = name;
732
733 /* Now obj should be either a buffer object or a process object.
734 */
735 if (BUFFERP (obj))
d0d6b7c5 736 {
1619761d 737 proc = Fget_buffer_process (obj);
d0d6b7c5 738 if (NILP (proc))
d5db4077 739 error ("Buffer %s has no process", SDATA (XBUFFER (obj)->name));
d0d6b7c5 740 }
d0d6b7c5 741 else
1619761d 742 {
b7826503 743 CHECK_PROCESS (obj);
1619761d
KH
744 proc = obj;
745 }
746 return proc;
d0d6b7c5
JB
747}
748
a5cfdda8
KS
749
750#ifdef SIGCHLD
751/* Fdelete_process promises to immediately forget about the process, but in
752 reality, Emacs needs to remember those processes until they have been
753 treated by sigchld_handler; otherwise this handler would consider the
754 process as being synchronous and say that the synchronous process is
755 dead. */
756static Lisp_Object deleted_pid_list;
757#endif
758
d0d6b7c5 759DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0,
fdb82f93
PJ
760 doc: /* Delete PROCESS: kill it and forget about it immediately.
761PROCESS may be a process, a buffer, the name of a process or buffer, or
762nil, indicating the current buffer's process. */)
763 (process)
4ee3e309 764 register Lisp_Object process;
d0d6b7c5 765{
ff6daed3
KS
766 register struct Lisp_Process *p;
767
4ee3e309 768 process = get_process (process);
ff6daed3
KS
769 p = XPROCESS (process);
770
6bfd98e7 771 p->raw_status_new = 0;
d888760c 772 if (NETCONN1_P (p) || SERIALCONN1_P (p))
d0d6b7c5 773 {
ff6daed3 774 p->status = Fcons (Qexit, Fcons (make_number (0), Qnil));
60f0fb11 775 p->tick = ++process_tick;
ff6daed3 776 status_notify (p);
d0d6b7c5 777 }
60f0fb11 778 else if (p->infd >= 0)
d0d6b7c5 779 {
a5cfdda8
KS
780#ifdef SIGCHLD
781 Lisp_Object symbol;
d30fe779
EZ
782 /* Assignment to EMACS_INT stops GCC whining about limited range
783 of data type. */
29ed4d51 784 EMACS_INT pid = p->pid;
a5cfdda8
KS
785
786 /* No problem storing the pid here, as it is still in Vprocess_alist. */
d30fe779 787 deleted_pid_list = Fcons (make_fixnum_or_float (pid),
a5cfdda8
KS
788 /* GC treated elements set to nil. */
789 Fdelq (Qnil, deleted_pid_list));
790 /* If the process has already signaled, remove it from the list. */
791 if (p->raw_status_new)
792 update_status (p);
793 symbol = p->status;
794 if (CONSP (p->status))
795 symbol = XCAR (p->status);
796 if (EQ (symbol, Qsignal) || EQ (symbol, Qexit))
29ed4d51
CY
797 deleted_pid_list
798 = Fdelete (make_fixnum_or_float (pid), deleted_pid_list);
a5cfdda8
KS
799 else
800#endif
801 {
802 Fkill_process (process, Qnil);
803 /* Do this now, since remove_process will make sigchld_handler do nothing. */
804 p->status
805 = Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil));
60f0fb11 806 p->tick = ++process_tick;
a5cfdda8
KS
807 status_notify (p);
808 }
d0d6b7c5 809 }
4ee3e309 810 remove_process (process);
d0d6b7c5
JB
811 return Qnil;
812}
813\f
814DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0,
fdb82f93
PJ
815 doc: /* Return the status of PROCESS.
816The returned value is one of the following symbols:
817run -- for a process that is running.
818stop -- for a process stopped but continuable.
819exit -- for a process that has exited.
820signal -- for a process that has got a fatal signal.
821open -- for a network stream connection that is open.
e690ca94 822listen -- for a network stream server that is listening.
fdb82f93 823closed -- for a network stream connection that is closed.
e4984112
KS
824connect -- when waiting for a non-blocking connection to complete.
825failed -- when a non-blocking connection has failed.
fdb82f93
PJ
826nil -- if arg is a process name and no such process exists.
827PROCESS may be a process, a buffer, the name of a process, or
828nil, indicating the current buffer's process. */)
829 (process)
4ee3e309 830 register Lisp_Object process;
d0d6b7c5
JB
831{
832 register struct Lisp_Process *p;
833 register Lisp_Object status;
343f4114 834
4ee3e309
EN
835 if (STRINGP (process))
836 process = Fget_process (process);
343f4114 837 else
4ee3e309 838 process = get_process (process);
343f4114 839
4ee3e309
EN
840 if (NILP (process))
841 return process;
343f4114 842
4ee3e309 843 p = XPROCESS (process);
6bfd98e7 844 if (p->raw_status_new)
d0d6b7c5
JB
845 update_status (p);
846 status = p->status;
bcd69aea 847 if (CONSP (status))
70949dac 848 status = XCAR (status);
d888760c 849 if (NETCONN1_P (p) || SERIALCONN1_P (p))
d0d6b7c5 850 {
e690ca94 851 if (EQ (status, Qexit))
d0d6b7c5 852 status = Qclosed;
e690ca94
KS
853 else if (EQ (p->command, Qt))
854 status = Qstop;
855 else if (EQ (status, Qrun))
856 status = Qopen;
d0d6b7c5
JB
857 }
858 return status;
859}
860
861DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status,
862 1, 1, 0,
fdb82f93
PJ
863 doc: /* Return the exit status of PROCESS or the signal number that killed it.
864If PROCESS has not yet exited or died, return 0. */)
865 (process)
4ee3e309 866 register Lisp_Object process;
d0d6b7c5 867{
b7826503 868 CHECK_PROCESS (process);
6bfd98e7 869 if (XPROCESS (process)->raw_status_new)
4ee3e309
EN
870 update_status (XPROCESS (process));
871 if (CONSP (XPROCESS (process)->status))
70949dac 872 return XCAR (XCDR (XPROCESS (process)->status));
d0d6b7c5
JB
873 return make_number (0);
874}
875
876DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0,
fdb82f93 877 doc: /* Return the process id of PROCESS.
f3d9532b 878This is the pid of the external process which PROCESS uses or talks to.
fdb82f93
PJ
879For a network connection, this value is nil. */)
880 (process)
4ee3e309 881 register Lisp_Object process;
d0d6b7c5 882{
d30fe779
EZ
883 /* Assignment to EMACS_INT stops GCC whining about limited range of
884 data type. */
885 EMACS_INT pid;
886
b7826503 887 CHECK_PROCESS (process);
d30fe779
EZ
888 pid = XPROCESS (process)->pid;
889 return (pid ? make_fixnum_or_float (pid) : Qnil);
d0d6b7c5
JB
890}
891
892DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0,
fdb82f93
PJ
893 doc: /* Return the name of PROCESS, as a string.
894This is the name of the program invoked in PROCESS,
895possibly modified to make it unique among process names. */)
896 (process)
4ee3e309 897 register Lisp_Object process;
d0d6b7c5 898{
b7826503 899 CHECK_PROCESS (process);
4ee3e309 900 return XPROCESS (process)->name;
d0d6b7c5
JB
901}
902
903DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0,
fdb82f93
PJ
904 doc: /* Return the command that was executed to start PROCESS.
905This is a list of strings, the first string being the program executed
906and the rest of the strings being the arguments given to it.
d888760c
GM
907For a network or serial process, this is nil (process is running) or t
908\(process is stopped). */)
fdb82f93 909 (process)
4ee3e309 910 register Lisp_Object process;
d0d6b7c5 911{
b7826503 912 CHECK_PROCESS (process);
4ee3e309 913 return XPROCESS (process)->command;
d0d6b7c5
JB
914}
915
3b9a3dfa 916DEFUN ("process-tty-name", Fprocess_tty_name, Sprocess_tty_name, 1, 1, 0,
fdb82f93
PJ
917 doc: /* Return the name of the terminal PROCESS uses, or nil if none.
918This is the terminal that the process itself reads and writes on,
919not the name of the pty that Emacs uses to talk with that terminal. */)
920 (process)
4ee3e309 921 register Lisp_Object process;
3b9a3dfa 922{
b7826503 923 CHECK_PROCESS (process);
4ee3e309 924 return XPROCESS (process)->tty_name;
3b9a3dfa
RS
925}
926
d0d6b7c5 927DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer,
fdb82f93
PJ
928 2, 2, 0,
929 doc: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil). */)
930 (process, buffer)
4ee3e309 931 register Lisp_Object process, buffer;
d0d6b7c5 932{
e690ca94
KS
933 struct Lisp_Process *p;
934
b7826503 935 CHECK_PROCESS (process);
d0d6b7c5 936 if (!NILP (buffer))
b7826503 937 CHECK_BUFFER (buffer);
e690ca94
KS
938 p = XPROCESS (process);
939 p->buffer = buffer;
d888760c 940 if (NETCONN1_P (p) || SERIALCONN1_P (p))
e690ca94 941 p->childp = Fplist_put (p->childp, QCbuffer, buffer);
03f04413 942 setup_process_coding_systems (process);
d0d6b7c5
JB
943 return buffer;
944}
945
946DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer,
fdb82f93
PJ
947 1, 1, 0,
948 doc: /* Return the buffer PROCESS is associated with.
949Output from PROCESS is inserted in this buffer unless PROCESS has a filter. */)
950 (process)
4ee3e309 951 register Lisp_Object process;
d0d6b7c5 952{
b7826503 953 CHECK_PROCESS (process);
4ee3e309 954 return XPROCESS (process)->buffer;
d0d6b7c5
JB
955}
956
957DEFUN ("process-mark", Fprocess_mark, Sprocess_mark,
fdb82f93
PJ
958 1, 1, 0,
959 doc: /* Return the marker for the end of the last output from PROCESS. */)
960 (process)
4ee3e309 961 register Lisp_Object process;
d0d6b7c5 962{
b7826503 963 CHECK_PROCESS (process);
4ee3e309 964 return XPROCESS (process)->mark;
d0d6b7c5
JB
965}
966
967DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter,
fdb82f93
PJ
968 2, 2, 0,
969 doc: /* Give PROCESS the filter function FILTER; nil means no filter.
7b82c3b6 970A value of t means stop accepting output from the process.
3452df9e
KS
971
972When a process has a filter, its buffer is not used for output.
973Instead, each time it does output, the entire string of output is
177c0ea7 974passed to the filter.
3452df9e 975
fdb82f93 976The filter gets two arguments: the process and the string of output.
3452df9e
KS
977The string argument is normally a multibyte string, except:
978- if the process' input coding system is no-conversion or raw-text,
979 it is a unibyte string (the non-converted input), or else
980- if `default-enable-multibyte-characters' is nil, it is a unibyte
981 string (the result of converting the decoded input multibyte
982 string to unibyte with `string-make-unibyte'). */)
fdb82f93 983 (process, filter)
4ee3e309 984 register Lisp_Object process, filter;
d0d6b7c5 985{
471f86b9 986 struct Lisp_Process *p;
177c0ea7 987
b7826503 988 CHECK_PROCESS (process);
471f86b9
GM
989 p = XPROCESS (process);
990
991 /* Don't signal an error if the process' input file descriptor
992 is closed. This could make debugging Lisp more difficult,
993 for example when doing something like
994
995 (setq process (start-process ...))
996 (debug)
997 (set-process-filter process ...) */
177c0ea7 998
60f0fb11 999 if (p->infd >= 0)
a69281ff 1000 {
e690ca94 1001 if (EQ (filter, Qt) && !EQ (p->status, Qlisten))
471f86b9 1002 {
60f0fb11
SM
1003 FD_CLR (p->infd, &input_wait_mask);
1004 FD_CLR (p->infd, &non_keyboard_wait_mask);
471f86b9 1005 }
e690ca94 1006 else if (EQ (p->filter, Qt)
d888760c
GM
1007 /* Network or serial process not stopped: */
1008 && !EQ (p->command, Qt))
471f86b9 1009 {
60f0fb11
SM
1010 FD_SET (p->infd, &input_wait_mask);
1011 FD_SET (p->infd, &non_keyboard_wait_mask);
471f86b9 1012 }
a69281ff 1013 }
177c0ea7 1014
471f86b9 1015 p->filter = filter;
d888760c 1016 if (NETCONN1_P (p) || SERIALCONN1_P (p))
e690ca94 1017 p->childp = Fplist_put (p->childp, QCfilter, filter);
03f04413 1018 setup_process_coding_systems (process);
d0d6b7c5
JB
1019 return filter;
1020}
1021
1022DEFUN ("process-filter", Fprocess_filter, Sprocess_filter,
fdb82f93
PJ
1023 1, 1, 0,
1024 doc: /* Returns the filter function of PROCESS; nil if none.
1025See `set-process-filter' for more info on filter functions. */)
1026 (process)
4ee3e309 1027 register Lisp_Object process;
d0d6b7c5 1028{
b7826503 1029 CHECK_PROCESS (process);
4ee3e309 1030 return XPROCESS (process)->filter;
d0d6b7c5
JB
1031}
1032
1033DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel,
fdb82f93
PJ
1034 2, 2, 0,
1035 doc: /* Give PROCESS the sentinel SENTINEL; nil for none.
1036The sentinel is called as a function when the process changes state.
1037It gets two arguments: the process, and a string describing the change. */)
1038 (process, sentinel)
4ee3e309 1039 register Lisp_Object process, sentinel;
d0d6b7c5 1040{
6b61353c
KH
1041 struct Lisp_Process *p;
1042
b7826503 1043 CHECK_PROCESS (process);
6b61353c
KH
1044 p = XPROCESS (process);
1045
1046 p->sentinel = sentinel;
d888760c 1047 if (NETCONN1_P (p) || SERIALCONN1_P (p))
6b61353c 1048 p->childp = Fplist_put (p->childp, QCsentinel, sentinel);
d0d6b7c5
JB
1049 return sentinel;
1050}
1051
1052DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
fdb82f93
PJ
1053 1, 1, 0,
1054 doc: /* Return the sentinel of PROCESS; nil if none.
1055See `set-process-sentinel' for more info on sentinels. */)
1056 (process)
4ee3e309 1057 register Lisp_Object process;
d0d6b7c5 1058{
b7826503 1059 CHECK_PROCESS (process);
4ee3e309 1060 return XPROCESS (process)->sentinel;
d0d6b7c5
JB
1061}
1062
396df322 1063DEFUN ("set-process-window-size", Fset_process_window_size,
fdb82f93
PJ
1064 Sset_process_window_size, 3, 3, 0,
1065 doc: /* Tell PROCESS that it has logical window size HEIGHT and WIDTH. */)
1066 (process, height, width)
4ee3e309 1067 register Lisp_Object process, height, width;
396df322 1068{
b7826503
PJ
1069 CHECK_PROCESS (process);
1070 CHECK_NATNUM (height);
1071 CHECK_NATNUM (width);
177c0ea7 1072
60f0fb11
SM
1073 if (XPROCESS (process)->infd < 0
1074 || set_window_size (XPROCESS (process)->infd,
471f86b9 1075 XINT (height), XINT (width)) <= 0)
396df322
RS
1076 return Qnil;
1077 else
1078 return Qt;
1079}
1080
52a1b894 1081DEFUN ("set-process-inherit-coding-system-flag",
fdb82f93
PJ
1082 Fset_process_inherit_coding_system_flag,
1083 Sset_process_inherit_coding_system_flag, 2, 2, 0,
1084 doc: /* Determine whether buffer of PROCESS will inherit coding-system.
1085If the second argument FLAG is non-nil, then the variable
1086`buffer-file-coding-system' of the buffer associated with PROCESS
1087will be bound to the value of the coding system used to decode
1088the process output.
1089
1090This is useful when the coding system specified for the process buffer
1091leaves either the character code conversion or the end-of-line conversion
1092unspecified, or if the coding system used to decode the process output
1093is more appropriate for saving the process buffer.
1094
1095Binding the variable `inherit-process-coding-system' to non-nil before
1096starting the process is an alternative way of setting the inherit flag
1097for the process which will run. */)
1098 (process, flag)
52a1b894
EZ
1099 register Lisp_Object process, flag;
1100{
b7826503 1101 CHECK_PROCESS (process);
60f0fb11 1102 XPROCESS (process)->inherit_coding_system_flag = !NILP (flag);
52a1b894
EZ
1103 return flag;
1104}
1105
1106DEFUN ("process-inherit-coding-system-flag",
fdb82f93
PJ
1107 Fprocess_inherit_coding_system_flag, Sprocess_inherit_coding_system_flag,
1108 1, 1, 0,
1109 doc: /* Return the value of inherit-coding-system flag for PROCESS.
1110If this flag is t, `buffer-file-coding-system' of the buffer
1111associated with PROCESS will inherit the coding system used to decode
1112the process output. */)
1113 (process)
52a1b894
EZ
1114 register Lisp_Object process;
1115{
b7826503 1116 CHECK_PROCESS (process);
60f0fb11 1117 return XPROCESS (process)->inherit_coding_system_flag ? Qt : Qnil;
52a1b894
EZ
1118}
1119
e690ca94
KS
1120DEFUN ("set-process-query-on-exit-flag",
1121 Fset_process_query_on_exit_flag, Sset_process_query_on_exit_flag,
1122 2, 2, 0,
1123 doc: /* Specify if query is needed for PROCESS when Emacs is exited.
f3d9532b 1124If the second argument FLAG is non-nil, Emacs will query the user before
e690ca94
KS
1125exiting if PROCESS is running. */)
1126 (process, flag)
1127 register Lisp_Object process, flag;
d0d6b7c5 1128{
b7826503 1129 CHECK_PROCESS (process);
60f0fb11 1130 XPROCESS (process)->kill_without_query = NILP (flag);
e690ca94 1131 return flag;
d0d6b7c5 1132}
312c9964 1133
e690ca94
KS
1134DEFUN ("process-query-on-exit-flag",
1135 Fprocess_query_on_exit_flag, Sprocess_query_on_exit_flag,
fdb82f93 1136 1, 1, 0,
f3d9532b 1137 doc: /* Return the current value of query-on-exit flag for PROCESS. */)
fdb82f93 1138 (process)
de282a05
RS
1139 register Lisp_Object process;
1140{
b7826503 1141 CHECK_PROCESS (process);
60f0fb11 1142 return (XPROCESS (process)->kill_without_query ? Qnil : Qt);
e690ca94
KS
1143}
1144
1145#ifdef DATAGRAM_SOCKETS
1146Lisp_Object Fprocess_datagram_address ();
1147#endif
1148
1149DEFUN ("process-contact", Fprocess_contact, Sprocess_contact,
1150 1, 2, 0,
1151 doc: /* Return the contact info of PROCESS; t for a real child.
d888760c
GM
1152For a network or serial connection, the value depends on the optional
1153KEY arg. If KEY is nil, value is a cons cell of the form (HOST
1154SERVICE) for a network connection or (PORT SPEED) for a serial
1155connection. If KEY is t, the complete contact information for the
1156connection is returned, else the specific value for the keyword KEY is
1157returned. See `make-network-process' or `make-serial-process' for a
1158list of keywords. */)
e690ca94
KS
1159 (process, key)
1160 register Lisp_Object process, key;
1161{
1162 Lisp_Object contact;
1163
1164 CHECK_PROCESS (process);
1165 contact = XPROCESS (process)->childp;
1166
1167#ifdef DATAGRAM_SOCKETS
1168 if (DATAGRAM_CONN_P (process)
1169 && (EQ (key, Qt) || EQ (key, QCremote)))
177c0ea7 1170 contact = Fplist_put (contact, QCremote,
e690ca94
KS
1171 Fprocess_datagram_address (process));
1172#endif
1173
d888760c 1174 if ((!NETCONN_P (process) && !SERIALCONN_P (process)) || EQ (key, Qt))
e690ca94 1175 return contact;
d888760c 1176 if (NILP (key) && NETCONN_P (process))
e690ca94
KS
1177 return Fcons (Fplist_get (contact, QChost),
1178 Fcons (Fplist_get (contact, QCservice), Qnil));
d888760c
GM
1179 if (NILP (key) && SERIALCONN_P (process))
1180 return Fcons (Fplist_get (contact, QCport),
1181 Fcons (Fplist_get (contact, QCspeed), Qnil));
e690ca94 1182 return Fplist_get (contact, key);
de282a05
RS
1183}
1184
faa7db08
KS
1185DEFUN ("process-plist", Fprocess_plist, Sprocess_plist,
1186 1, 1, 0,
1187 doc: /* Return the plist of PROCESS. */)
1188 (process)
1189 register Lisp_Object process;
365aee82 1190{
ac4a7584 1191 CHECK_PROCESS (process);
faa7db08 1192 return XPROCESS (process)->plist;
ac4a7584 1193}
365aee82 1194
faa7db08
KS
1195DEFUN ("set-process-plist", Fset_process_plist, Sset_process_plist,
1196 2, 2, 0,
26a086c6 1197 doc: /* Replace the plist of PROCESS with PLIST. Returns PLIST. */)
faa7db08
KS
1198 (process, plist)
1199 register Lisp_Object process, plist;
ac4a7584 1200{
365aee82 1201 CHECK_PROCESS (process);
faa7db08 1202 CHECK_LIST (plist);
365aee82 1203
faa7db08 1204 XPROCESS (process)->plist = plist;
26a086c6 1205 return plist;
365aee82
KS
1206}
1207
312c9964
RS
1208#if 0 /* Turned off because we don't currently record this info
1209 in the process. Perhaps add it. */
1210DEFUN ("process-connection", Fprocess_connection, Sprocess_connection, 1, 1, 0,
fdb82f93
PJ
1211 doc: /* Return the connection type of PROCESS.
1212The value is nil for a pipe, t or `pty' for a pty, or `stream' for
1213a socket connection. */)
1214 (process)
312c9964
RS
1215 Lisp_Object process;
1216{
1217 return XPROCESS (process)->type;
1218}
1219#endif
2a7bb882 1220
d888760c
GM
1221DEFUN ("process-type", Fprocess_type, Sprocess_type, 1, 1, 0,
1222 doc: /* Return the connection type of PROCESS.
1223The value is either the symbol `real', `network', or `serial'.
1224PROCESS may be a process, a buffer, the name of a process or buffer, or
1225nil, indicating the current buffer's process. */)
1226 (process)
1227 Lisp_Object process;
1228{
1229 Lisp_Object proc;
1230 proc = get_process (process);
1231 return XPROCESS (proc)->type;
1232}
991234f0
KS
1233
1234#ifdef HAVE_SOCKETS
1235DEFUN ("format-network-address", Fformat_network_address, Sformat_network_address,
0dccee41 1236 1, 2, 0,
991234f0 1237 doc: /* Convert network ADDRESS from internal format to a string.
e1652a86
KS
1238A 4 or 5 element vector represents an IPv4 address (with port number).
1239An 8 or 9 element vector represents an IPv6 address (with port number).
0dccee41 1240If optional second argument OMIT-PORT is non-nil, don't include a port
e1652a86
KS
1241number in the string, even when present in ADDRESS.
1242Returns nil if format of ADDRESS is invalid. */)
0dccee41
KS
1243 (address, omit_port)
1244 Lisp_Object address, omit_port;
991234f0 1245{
991234f0
KS
1246 if (NILP (address))
1247 return Qnil;
1248
1249 if (STRINGP (address)) /* AF_LOCAL */
1250 return address;
1251
e1652a86 1252 if (VECTORP (address)) /* AF_INET or AF_INET6 */
991234f0
KS
1253 {
1254 register struct Lisp_Vector *p = XVECTOR (address);
ded83665 1255 Lisp_Object args[10];
0dccee41 1256 int nargs, i;
991234f0 1257
e1652a86 1258 if (p->size == 4 || (p->size == 5 && !NILP (omit_port)))
0dccee41
KS
1259 {
1260 args[0] = build_string ("%d.%d.%d.%d");
1261 nargs = 4;
1262 }
1263 else if (p->size == 5)
1264 {
1265 args[0] = build_string ("%d.%d.%d.%d:%d");
1266 nargs = 5;
1267 }
e1652a86
KS
1268 else if (p->size == 8 || (p->size == 9 && !NILP (omit_port)))
1269 {
1270 args[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
1271 nargs = 8;
1272 }
1273 else if (p->size == 9)
1274 {
1275 args[0] = build_string ("[%x:%x:%x:%x:%x:%x:%x:%x]:%d");
1276 nargs = 9;
1277 }
0dccee41 1278 else
991234f0
KS
1279 return Qnil;
1280
0dccee41 1281 for (i = 0; i < nargs; i++)
cbf23adb
JB
1282 {
1283 EMACS_INT element = XINT (p->contents[i]);
1284
1285 if (element < 0 || element > 65535)
1286 return Qnil;
1287
1288 if (nargs <= 5 /* IPv4 */
1289 && i < 4 /* host, not port */
1290 && element > 255)
1291 return Qnil;
1292
1293 args[i+1] = p->contents[i];
1294 }
1295
0dccee41 1296 return Fformat (nargs+1, args);
991234f0
KS
1297 }
1298
1299 if (CONSP (address))
1300 {
1301 Lisp_Object args[2];
1302 args[0] = build_string ("<Family %d>");
8d2ff840 1303 args[1] = Fcar (address);
991234f0 1304 return Fformat (2, args);
991234f0
KS
1305 }
1306
1307 return Qnil;
1308}
1309#endif
d0d6b7c5 1310\f
ff6daed3 1311static Lisp_Object
e690ca94
KS
1312list_processes_1 (query_only)
1313 Lisp_Object query_only;
d0d6b7c5
JB
1314{
1315 register Lisp_Object tail, tem;
1316 Lisp_Object proc, minspace, tem1;
d0d6b7c5 1317 register struct Lisp_Process *p;
e690ca94
KS
1318 char tembuf[300];
1319 int w_proc, w_buffer, w_tty;
1eed3bdc 1320 int exited = 0;
e690ca94
KS
1321 Lisp_Object i_status, i_buffer, i_tty, i_command;
1322
1323 w_proc = 4; /* Proc */
1324 w_buffer = 6; /* Buffer */
1325 w_tty = 0; /* Omit if no ttys */
1326
99784d63 1327 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
e690ca94
KS
1328 {
1329 int i;
1330
99784d63 1331 proc = Fcdr (XCAR (tail));
e690ca94 1332 p = XPROCESS (proc);
d888760c 1333 if (NILP (p->type))
e690ca94 1334 continue;
60f0fb11 1335 if (!NILP (query_only) && p->kill_without_query)
e690ca94
KS
1336 continue;
1337 if (STRINGP (p->name)
d5db4077 1338 && ( i = SCHARS (p->name), (i > w_proc)))
e690ca94
KS
1339 w_proc = i;
1340 if (!NILP (p->buffer))
1341 {
e69c4cae
SM
1342 if (NILP (XBUFFER (p->buffer)->name))
1343 {
1344 if (w_buffer < 8)
1345 w_buffer = 8; /* (Killed) */
1346 }
d5db4077 1347 else if ((i = SCHARS (XBUFFER (p->buffer)->name), (i > w_buffer)))
e690ca94
KS
1348 w_buffer = i;
1349 }
1350 if (STRINGP (p->tty_name)
d5db4077 1351 && (i = SCHARS (p->tty_name), (i > w_tty)))
e690ca94
KS
1352 w_tty = i;
1353 }
1354
1355 XSETFASTINT (i_status, w_proc + 1);
1356 XSETFASTINT (i_buffer, XFASTINT (i_status) + 9);
1357 if (w_tty)
1358 {
1359 XSETFASTINT (i_tty, XFASTINT (i_buffer) + w_buffer + 1);
d3c0009d
AS
1360 XSETFASTINT (i_command, XFASTINT (i_tty) + w_tty + 1);
1361 }
1362 else
1363 {
e690ca94
KS
1364 i_tty = Qnil;
1365 XSETFASTINT (i_command, XFASTINT (i_buffer) + w_buffer + 1);
1366 }
d0d6b7c5 1367
22719df2 1368 XSETFASTINT (minspace, 1);
d0d6b7c5
JB
1369
1370 set_buffer_internal (XBUFFER (Vstandard_output));
ec2258fa 1371 current_buffer->undo_list = Qt;
d0d6b7c5
JB
1372
1373 current_buffer->truncate_lines = Qt;
1374
e690ca94
KS
1375 write_string ("Proc", -1);
1376 Findent_to (i_status, minspace); write_string ("Status", -1);
1377 Findent_to (i_buffer, minspace); write_string ("Buffer", -1);
1378 if (!NILP (i_tty))
1379 {
1380 Findent_to (i_tty, minspace); write_string ("Tty", -1);
1381 }
1382 Findent_to (i_command, minspace); write_string ("Command", -1);
1383 write_string ("\n", -1);
1384
1385 write_string ("----", -1);
1386 Findent_to (i_status, minspace); write_string ("------", -1);
1387 Findent_to (i_buffer, minspace); write_string ("------", -1);
1388 if (!NILP (i_tty))
1389 {
1390 Findent_to (i_tty, minspace); write_string ("---", -1);
1391 }
1392 Findent_to (i_command, minspace); write_string ("-------", -1);
1393 write_string ("\n", -1);
d0d6b7c5 1394
99784d63 1395 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
d0d6b7c5
JB
1396 {
1397 Lisp_Object symbol;
1398
99784d63 1399 proc = Fcdr (XCAR (tail));
d0d6b7c5 1400 p = XPROCESS (proc);
d888760c 1401 if (NILP (p->type))
d0d6b7c5 1402 continue;
60f0fb11 1403 if (!NILP (query_only) && p->kill_without_query)
e690ca94 1404 continue;
d0d6b7c5
JB
1405
1406 Finsert (1, &p->name);
e690ca94 1407 Findent_to (i_status, minspace);
d0d6b7c5 1408
6bfd98e7 1409 if (p->raw_status_new)
d0d6b7c5
JB
1410 update_status (p);
1411 symbol = p->status;
bcd69aea 1412 if (CONSP (p->status))
70949dac 1413 symbol = XCAR (p->status);
d0d6b7c5 1414
d0d6b7c5
JB
1415 if (EQ (symbol, Qsignal))
1416 {
1417 Lisp_Object tem;
1418 tem = Fcar (Fcdr (p->status));
7c2fb837 1419 Fprinc (symbol, Qnil);
d0d6b7c5 1420 }
d888760c 1421 else if (NETCONN1_P (p) || SERIALCONN1_P (p))
d0d6b7c5 1422 {
e690ca94 1423 if (EQ (symbol, Qexit))
d0d6b7c5 1424 write_string ("closed", -1);
e690ca94
KS
1425 else if (EQ (p->command, Qt))
1426 write_string ("stopped", -1);
1427 else if (EQ (symbol, Qrun))
1428 write_string ("open", -1);
d0d6b7c5
JB
1429 else
1430 Fprinc (symbol, Qnil);
1431 }
d888760c
GM
1432 else if (SERIALCONN1_P (p))
1433 {
1434 write_string ("running", -1);
1435 }
d0d6b7c5
JB
1436 else
1437 Fprinc (symbol, Qnil);
1438
1439 if (EQ (symbol, Qexit))
1440 {
1441 Lisp_Object tem;
1442 tem = Fcar (Fcdr (p->status));
1443 if (XFASTINT (tem))
1444 {
3162bafa 1445 sprintf (tembuf, " %d", (int) XFASTINT (tem));
d0d6b7c5
JB
1446 write_string (tembuf, -1);
1447 }
1448 }
1449
1eed3bdc
KS
1450 if (EQ (symbol, Qsignal) || EQ (symbol, Qexit) || EQ (symbol, Qclosed))
1451 exited++;
d0d6b7c5 1452
e690ca94 1453 Findent_to (i_buffer, minspace);
d0d6b7c5
JB
1454 if (NILP (p->buffer))
1455 insert_string ("(none)");
1456 else if (NILP (XBUFFER (p->buffer)->name))
1457 insert_string ("(Killed)");
1458 else
1459 Finsert (1, &XBUFFER (p->buffer)->name);
1460
e690ca94
KS
1461 if (!NILP (i_tty))
1462 {
1463 Findent_to (i_tty, minspace);
1464 if (STRINGP (p->tty_name))
1465 Finsert (1, &p->tty_name);
1466 }
a9fde32e 1467
e690ca94 1468 Findent_to (i_command, minspace);
a9fde32e 1469
e690ca94
KS
1470 if (EQ (p->status, Qlisten))
1471 {
1472 Lisp_Object port = Fplist_get (p->childp, QCservice);
1473 if (INTEGERP (port))
1474 port = Fnumber_to_string (port);
991234f0 1475 if (NILP (port))
0dccee41 1476 port = Fformat_network_address (Fplist_get (p->childp, QClocal), Qnil);
e690ca94 1477 sprintf (tembuf, "(network %s server on %s)\n",
60f0fb11 1478 (DATAGRAM_CHAN_P (p->infd) ? "datagram" : "stream"),
991234f0 1479 (STRINGP (port) ? (char *)SDATA (port) : "?"));
e690ca94
KS
1480 insert_string (tembuf);
1481 }
1482 else if (NETCONN1_P (p))
63136da6 1483 {
e690ca94
KS
1484 /* For a local socket, there is no host name,
1485 so display service instead. */
1486 Lisp_Object host = Fplist_get (p->childp, QChost);
1487 if (!STRINGP (host))
1488 {
1489 host = Fplist_get (p->childp, QCservice);
1490 if (INTEGERP (host))
1491 host = Fnumber_to_string (host);
1492 }
991234f0 1493 if (NILP (host))
0dccee41 1494 host = Fformat_network_address (Fplist_get (p->childp, QCremote), Qnil);
e690ca94 1495 sprintf (tembuf, "(network %s connection to %s)\n",
60f0fb11 1496 (DATAGRAM_CHAN_P (p->infd) ? "datagram" : "stream"),
991234f0 1497 (STRINGP (host) ? (char *)SDATA (host) : "?"));
d0d6b7c5 1498 insert_string (tembuf);
63136da6 1499 }
d888760c
GM
1500 else if (SERIALCONN1_P (p))
1501 {
1502 Lisp_Object port = Fplist_get (p->childp, QCport);
1503 Lisp_Object speed = Fplist_get (p->childp, QCspeed);
1504 insert_string ("(serial port ");
1505 if (STRINGP (port))
1506 insert_string (SDATA (port));
1507 else
1508 insert_string ("?");
1509 if (INTEGERP (speed))
1510 {
1511 sprintf (tembuf, " at %d b/s", XINT (speed));
1512 insert_string (tembuf);
1513 }
1514 insert_string (")\n");
1515 }
177c0ea7 1516 else
d0d6b7c5
JB
1517 {
1518 tem = p->command;
1519 while (1)
1520 {
1521 tem1 = Fcar (tem);
1522 Finsert (1, &tem1);
1523 tem = Fcdr (tem);
1524 if (NILP (tem))
1525 break;
1526 insert_string (" ");
1527 }
1528 insert_string ("\n");
1529 }
1530 }
1eed3bdc
KS
1531 if (exited)
1532 status_notify (NULL);
d0d6b7c5
JB
1533 return Qnil;
1534}
1535
e690ca94 1536DEFUN ("list-processes", Flist_processes, Slist_processes, 0, 1, "P",
fdb82f93 1537 doc: /* Display a list of all processes.
e690ca94
KS
1538If optional argument QUERY-ONLY is non-nil, only processes with
1539the query-on-exit flag set will be listed.
fdb82f93
PJ
1540Any process listed as exited or signaled is actually eliminated
1541after the listing is made. */)
e690ca94
KS
1542 (query_only)
1543 Lisp_Object query_only;
d0d6b7c5
JB
1544{
1545 internal_with_output_to_temp_buffer ("*Process List*",
e690ca94 1546 list_processes_1, query_only);
d0d6b7c5
JB
1547 return Qnil;
1548}
1549
1550DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
fdb82f93
PJ
1551 doc: /* Return a list of all processes. */)
1552 ()
d0d6b7c5
JB
1553{
1554 return Fmapcar (Qcdr, Vprocess_alist);
1555}
1556\f
b0310da4
JB
1557/* Starting asynchronous inferior processes. */
1558
1559static Lisp_Object start_process_unwind ();
1560
d0d6b7c5 1561DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0,
fdb82f93
PJ
1562 doc: /* Start a program in a subprocess. Return the process object for it.
1563NAME is name for process. It is modified if necessary to make it unique.
553acde5 1564BUFFER is the buffer (or buffer name) to associate with the process.
4b6c5eee
EZ
1565
1566Process output (both standard output and standard error streams) goes
1567at end of BUFFER, unless you specify an output stream or filter
1568function to handle the output. BUFFER may also be nil, meaning that
16fdeb17
RS
1569this process is not associated with any buffer.
1570
553acde5 1571PROGRAM is the program file name. It is searched for in PATH.
3ecdf100 1572Remaining arguments are strings to give program as arguments.
320aebc9 1573
4b6c5eee
EZ
1574If you want to separate standard output from standard error, invoke
1575the command through a shell and redirect one of them using the shell
1576syntax.
320aebc9 1577
3ecdf100 1578usage: (start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS) */)
fdb82f93 1579 (nargs, args)
d0d6b7c5
JB
1580 int nargs;
1581 register Lisp_Object *args;
1582{
1e30af70 1583 Lisp_Object buffer, name, program, proc, current_dir, tem;
d0d6b7c5 1584 register unsigned char **new_argv;
d0d6b7c5 1585 register int i;
aed13378 1586 int count = SPECPDL_INDEX ();
d0d6b7c5
JB
1587
1588 buffer = args[1];
1589 if (!NILP (buffer))
1590 buffer = Fget_buffer_create (buffer);
1591
1e30af70
JB
1592 /* Make sure that the child will be able to chdir to the current
1593 buffer's current directory, or its unhandled equivalent. We
1594 can't just have the child check for an error when it does the
1595 chdir, since it's in a vfork.
1596
1597 We have to GCPRO around this because Fexpand_file_name and
1598 Funhandled_file_name_directory might call a file name handling
1599 function. The argument list is protected by the caller, so all
1600 we really have to worry about is buffer. */
1601 {
1602 struct gcpro gcpro1, gcpro2;
1603
1604 current_dir = current_buffer->directory;
1605
1606 GCPRO2 (buffer, current_dir);
1607
ca319910
SM
1608 current_dir = Funhandled_file_name_directory (current_dir);
1609 if (NILP (current_dir))
1610 /* If the file name handler says that current_dir is unreachable, use
1611 a sensible default. */
1612 current_dir = build_string ("~/");
1613 current_dir = expand_and_dir_to_file (current_dir, Qnil);
1e30af70
JB
1614 if (NILP (Ffile_accessible_directory_p (current_dir)))
1615 report_file_error ("Setting current directory",
1616 Fcons (current_buffer->directory, Qnil));
1617
1618 UNGCPRO;
1619 }
1620
d0d6b7c5 1621 name = args[0];
b7826503 1622 CHECK_STRING (name);
d0d6b7c5
JB
1623
1624 program = args[2];
1625
b7826503 1626 CHECK_STRING (program);
d0d6b7c5 1627
d0d6b7c5 1628 proc = make_process (name);
b0310da4
JB
1629 /* If an error occurs and we can't start the process, we want to
1630 remove it from the process list. This means that each error
1631 check in create_process doesn't need to call remove_process
1632 itself; it's all taken care of here. */
1633 record_unwind_protect (start_process_unwind, proc);
d0d6b7c5
JB
1634
1635 XPROCESS (proc)->childp = Qt;
faa7db08 1636 XPROCESS (proc)->plist = Qnil;
d888760c 1637 XPROCESS (proc)->type = Qreal;
d0d6b7c5
JB
1638 XPROCESS (proc)->buffer = buffer;
1639 XPROCESS (proc)->sentinel = Qnil;
1640 XPROCESS (proc)->filter = Qnil;
1641 XPROCESS (proc)->command = Flist (nargs - 2, args + 2);
1642
6b61353c 1643#ifdef ADAPTIVE_READ_BUFFERING
60f0fb11
SM
1644 XPROCESS (proc)->adaptive_read_buffering
1645 = (NILP (Vprocess_adaptive_read_buffering) ? 0
1646 : EQ (Vprocess_adaptive_read_buffering, Qt) ? 1 : 2);
6b61353c
KH
1647#endif
1648
7af71e17 1649 /* Make the process marker point into the process buffer (if any). */
f523e0c3 1650 if (BUFFERP (buffer))
d8a2934e
RS
1651 set_marker_both (XPROCESS (proc)->mark, buffer,
1652 BUF_ZV (XBUFFER (buffer)),
1653 BUF_ZV_BYTE (XBUFFER (buffer)));
7af71e17 1654
67918941 1655 {
d5d4ae71
KH
1656 /* Decide coding systems for communicating with the process. Here
1657 we don't setup the structure coding_system nor pay attention to
1658 unibyte mode. They are done in create_process. */
1659
67918941
RS
1660 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1661 Lisp_Object coding_systems = Qt;
1662 Lisp_Object val, *args2;
a4a37e65 1663 struct gcpro gcpro1, gcpro2;
67918941 1664
d5d4ae71
KH
1665 val = Vcoding_system_for_read;
1666 if (NILP (val))
67918941
RS
1667 {
1668 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
1669 args2[0] = Qstart_process;
1670 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
a4a37e65 1671 GCPRO2 (proc, current_dir);
67918941
RS
1672 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
1673 UNGCPRO;
1674 if (CONSP (coding_systems))
70949dac 1675 val = XCAR (coding_systems);
67918941 1676 else if (CONSP (Vdefault_process_coding_system))
70949dac 1677 val = XCAR (Vdefault_process_coding_system);
67918941
RS
1678 }
1679 XPROCESS (proc)->decode_coding_system = val;
1680
d5d4ae71
KH
1681 val = Vcoding_system_for_write;
1682 if (NILP (val))
67918941
RS
1683 {
1684 if (EQ (coding_systems, Qt))
1685 {
1686 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof args2);
1687 args2[0] = Qstart_process;
1688 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
a4a37e65 1689 GCPRO2 (proc, current_dir);
67918941
RS
1690 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
1691 UNGCPRO;
1692 }
1693 if (CONSP (coding_systems))
70949dac 1694 val = XCDR (coding_systems);
67918941 1695 else if (CONSP (Vdefault_process_coding_system))
70949dac 1696 val = XCDR (Vdefault_process_coding_system);
67918941
RS
1697 }
1698 XPROCESS (proc)->encode_coding_system = val;
1699 }
0fa1789e 1700
ed30cf85
RS
1701 /* If program file name is not absolute, search our path for it.
1702 Put the name we will really use in TEM. */
d5db4077
KR
1703 if (!IS_DIRECTORY_SEP (SREF (program, 0))
1704 && !(SCHARS (program) > 1
1705 && IS_DEVICE_SEP (SREF (program, 1))))
a4a37e65
KH
1706 {
1707 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1708
1709 tem = Qnil;
1710 GCPRO4 (name, program, buffer, current_dir);
cc4db0c7 1711 openp (Vexec_path, program, Vexec_suffixes, &tem, make_number (X_OK));
a4a37e65
KH
1712 UNGCPRO;
1713 if (NILP (tem))
1714 report_file_error ("Searching for program", Fcons (program, Qnil));
1715 tem = Fexpand_file_name (tem, Qnil);
a4a37e65
KH
1716 }
1717 else
1718 {
1719 if (!NILP (Ffile_directory_p (program)))
1720 error ("Specified program for new process is a directory");
ed30cf85 1721 tem = program;
a4a37e65
KH
1722 }
1723
ed30cf85
RS
1724 /* If program file name starts with /: for quoting a magic name,
1725 discard that. */
1726 if (SBYTES (tem) > 2 && SREF (tem, 0) == '/'
1727 && SREF (tem, 1) == ':')
1728 tem = Fsubstring (tem, make_number (2), Qnil);
1729
77bf07e1
AS
1730 {
1731 struct gcpro gcpro1;
1732 GCPRO1 (tem);
1733
1734 /* Encode the file name and put it in NEW_ARGV.
1735 That's where the child will use it to execute the program. */
1736 tem = Fcons (ENCODE_FILE (tem), Qnil);
ed30cf85 1737
77bf07e1
AS
1738 /* Here we encode arguments by the coding system used for sending
1739 data to the process. We don't support using different coding
1740 systems for encoding arguments and for encoding data sent to the
1741 process. */
1742
1743 for (i = 3; i < nargs; i++)
1744 {
1745 tem = Fcons (args[i], tem);
1746 CHECK_STRING (XCAR (tem));
1747 if (STRING_MULTIBYTE (XCAR (tem)))
1748 XSETCAR (tem,
1749 code_convert_string_norecord
1750 (XCAR (tem), XPROCESS (proc)->encode_coding_system, 1));
1751 }
a4a37e65 1752
77bf07e1
AS
1753 UNGCPRO;
1754 }
1755
1756 /* Now that everything is encoded we can collect the strings into
1757 NEW_ARGV. */
1758 new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
1759 new_argv[nargs - 2] = 0;
1760
1761 for (i = nargs - 3; i >= 0; i--)
a4a37e65 1762 {
77bf07e1
AS
1763 new_argv[i] = SDATA (XCAR (tem));
1764 tem = XCDR (tem);
a4a37e65 1765 }
a4a37e65 1766
0fa1789e 1767 XPROCESS (proc)->decoding_buf = make_uninit_string (0);
60f0fb11 1768 XPROCESS (proc)->decoding_carryover = 0;
0fa1789e
KH
1769 XPROCESS (proc)->encoding_buf = make_uninit_string (0);
1770
52a1b894 1771 XPROCESS (proc)->inherit_coding_system_flag
e6fca843 1772 = !(NILP (buffer) || !inherit_process_coding_system);
52a1b894 1773
6b53bb85 1774 create_process (proc, (char **) new_argv, current_dir);
d0d6b7c5 1775
b0310da4 1776 return unbind_to (count, proc);
d0d6b7c5
JB
1777}
1778
b0310da4 1779/* This function is the unwind_protect form for Fstart_process. If
8e6208c5 1780 PROC doesn't have its pid set, then we know someone has signaled
b0310da4
JB
1781 an error and the process wasn't started successfully, so we should
1782 remove it from the process list. */
1783static Lisp_Object
1784start_process_unwind (proc)
1785 Lisp_Object proc;
1786{
bcd69aea 1787 if (!PROCESSP (proc))
b0310da4
JB
1788 abort ();
1789
1790 /* Was PROC started successfully? */
6bfd98e7 1791 if (XPROCESS (proc)->pid <= 0)
b0310da4
JB
1792 remove_process (proc);
1793
1794 return Qnil;
1795}
1796
ff6daed3 1797static void
30904ab7
GM
1798create_process_1 (timer)
1799 struct atimer *timer;
d0d6b7c5 1800{
30904ab7 1801 /* Nothing to do. */
d0d6b7c5
JB
1802}
1803
30904ab7 1804
d0d6b7c5
JB
1805#if 0 /* This doesn't work; see the note before sigchld_handler. */
1806#ifdef USG
1807#ifdef SIGCHLD
1808/* Mimic blocking of signals on system V, which doesn't really have it. */
1809
1810/* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1811int sigchld_deferred;
1812
1813SIGTYPE
1814create_process_sigchld ()
1815{
1816 signal (SIGCHLD, create_process_sigchld);
1817
1818 sigchld_deferred = 1;
1819}
1820#endif
1821#endif
1822#endif
1823
6b53bb85 1824void
1e30af70 1825create_process (process, new_argv, current_dir)
d0d6b7c5
JB
1826 Lisp_Object process;
1827 char **new_argv;
1e30af70 1828 Lisp_Object current_dir;
d0d6b7c5 1829{
29ed4d51
CY
1830 int inchannel, outchannel;
1831 pid_t pid;
d0d6b7c5 1832 int sv[2];
2c4df143
YM
1833#if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1834 int wait_child_setup[2];
1835#endif
0dc70c33
KH
1836#ifdef POSIX_SIGNALS
1837 sigset_t procmask;
1838 sigset_t blocked;
1839 struct sigaction sigint_action;
1840 struct sigaction sigquit_action;
1841#ifdef AIX
1842 struct sigaction sighup_action;
1843#endif
1844#else /* !POSIX_SIGNALS */
41d03b9a 1845#if 0
d0d6b7c5
JB
1846#ifdef SIGCHLD
1847 SIGTYPE (*sigchld)();
1848#endif
41d03b9a 1849#endif /* 0 */
0dc70c33 1850#endif /* !POSIX_SIGNALS */
ecd1f654
KH
1851 /* Use volatile to protect variables from being clobbered by longjmp. */
1852 volatile int forkin, forkout;
1853 volatile int pty_flag = 0;
3ec68006 1854#ifndef USE_CRT_DLL
d0d6b7c5 1855 extern char **environ;
3ec68006 1856#endif
d0d6b7c5 1857
d0d6b7c5
JB
1858 inchannel = outchannel = -1;
1859
1860#ifdef HAVE_PTYS
fe45da4e 1861 if (!NILP (Vprocess_connection_type))
d0d6b7c5
JB
1862 outchannel = inchannel = allocate_pty ();
1863
d0d6b7c5
JB
1864 if (inchannel >= 0)
1865 {
2af70a0c
RS
1866#if ! defined (USG) || defined (USG_SUBTTY_WORKS)
1867 /* On most USG systems it does not work to open the pty's tty here,
1868 then close it and reopen it in the child. */
d0d6b7c5
JB
1869#ifdef O_NOCTTY
1870 /* Don't let this terminal become our controlling terminal
1871 (in case we don't have one). */
68c45bf0 1872 forkout = forkin = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0);
d0d6b7c5 1873#else
68c45bf0 1874 forkout = forkin = emacs_open (pty_name, O_RDWR, 0);
d0d6b7c5
JB
1875#endif
1876 if (forkin < 0)
1877 report_file_error ("Opening pty", Qnil);
1878#else
1879 forkin = forkout = -1;
2af70a0c 1880#endif /* not USG, or USG_SUBTTY_WORKS */
d0d6b7c5
JB
1881 pty_flag = 1;
1882 }
1883 else
1884#endif /* HAVE_PTYS */
d0d6b7c5 1885 {
fc14013c
KH
1886 int tem;
1887 tem = pipe (sv);
1888 if (tem < 0)
1889 report_file_error ("Creating pipe", Qnil);
d0d6b7c5
JB
1890 inchannel = sv[0];
1891 forkout = sv[1];
fc14013c
KH
1892 tem = pipe (sv);
1893 if (tem < 0)
1894 {
68c45bf0
PE
1895 emacs_close (inchannel);
1896 emacs_close (forkout);
fc14013c
KH
1897 report_file_error ("Creating pipe", Qnil);
1898 }
d0d6b7c5
JB
1899 outchannel = sv[1];
1900 forkin = sv[0];
1901 }
d0d6b7c5 1902
2c4df143
YM
1903#if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
1904 {
1905 int tem;
1906
1907 tem = pipe (wait_child_setup);
1908 if (tem < 0)
1909 report_file_error ("Creating pipe", Qnil);
1910 tem = fcntl (wait_child_setup[1], F_GETFD, 0);
1911 if (tem >= 0)
1912 tem = fcntl (wait_child_setup[1], F_SETFD, tem | FD_CLOEXEC);
1913 if (tem < 0)
1914 {
1915 emacs_close (wait_child_setup[0]);
1916 emacs_close (wait_child_setup[1]);
1917 report_file_error ("Setting file descriptor flags", Qnil);
1918 }
1919 }
1920#endif
1921
d0d6b7c5
JB
1922#if 0
1923 /* Replaced by close_process_descs */
1924 set_exclusive_use (inchannel);
1925 set_exclusive_use (outchannel);
1926#endif
1927
d0d6b7c5
JB
1928#ifdef O_NONBLOCK
1929 fcntl (inchannel, F_SETFL, O_NONBLOCK);
03832893 1930 fcntl (outchannel, F_SETFL, O_NONBLOCK);
d0d6b7c5
JB
1931#else
1932#ifdef O_NDELAY
1933 fcntl (inchannel, F_SETFL, O_NDELAY);
03832893 1934 fcntl (outchannel, F_SETFL, O_NDELAY);
d0d6b7c5
JB
1935#endif
1936#endif
1937
1938 /* Record this as an active process, with its channels.
1939 As a result, child_setup will close Emacs's side of the pipes. */
1940 chan_process[inchannel] = process;
60f0fb11
SM
1941 XPROCESS (process)->infd = inchannel;
1942 XPROCESS (process)->outfd = outchannel;
16782258
JD
1943
1944 /* Previously we recorded the tty descriptor used in the subprocess.
1945 It was only used for getting the foreground tty process, so now
1946 we just reopen the device (see emacs_get_tty_pgrp) as this is
1947 more portable (see USG_SUBTTY_WORKS above). */
1948
60f0fb11 1949 XPROCESS (process)->pty_flag = pty_flag;
d0d6b7c5 1950 XPROCESS (process)->status = Qrun;
03f04413 1951 setup_process_coding_systems (process);
d0d6b7c5
JB
1952
1953 /* Delay interrupts until we have a chance to store
1954 the new fork's pid in its process structure */
0dc70c33
KH
1955#ifdef POSIX_SIGNALS
1956 sigemptyset (&blocked);
1957#ifdef SIGCHLD
1958 sigaddset (&blocked, SIGCHLD);
1959#endif
351e611f 1960#ifdef HAVE_WORKING_VFORK
0dc70c33
KH
1961 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
1962 this sets the parent's signal handlers as well as the child's.
1963 So delay all interrupts whose handlers the child might munge,
1964 and record the current handlers so they can be restored later. */
1965 sigaddset (&blocked, SIGINT ); sigaction (SIGINT , 0, &sigint_action );
1966 sigaddset (&blocked, SIGQUIT); sigaction (SIGQUIT, 0, &sigquit_action);
1967#ifdef AIX
1968 sigaddset (&blocked, SIGHUP ); sigaction (SIGHUP , 0, &sighup_action );
1969#endif
351e611f 1970#endif /* HAVE_WORKING_VFORK */
0dc70c33
KH
1971 sigprocmask (SIG_BLOCK, &blocked, &procmask);
1972#else /* !POSIX_SIGNALS */
d0d6b7c5 1973#ifdef SIGCHLD
e39a993c 1974#if defined (BSD_SYSTEM) || defined (HPUX)
d0d6b7c5
JB
1975 sigsetmask (sigmask (SIGCHLD));
1976#else /* ordinary USG */
1977#if 0
1978 sigchld_deferred = 0;
1979 sigchld = signal (SIGCHLD, create_process_sigchld);
1980#endif
1981#endif /* ordinary USG */
d0d6b7c5 1982#endif /* SIGCHLD */
0dc70c33 1983#endif /* !POSIX_SIGNALS */
d0d6b7c5 1984
3081bf8d 1985 FD_SET (inchannel, &input_wait_mask);
a69281ff 1986 FD_SET (inchannel, &non_keyboard_wait_mask);
3081bf8d
KH
1987 if (inchannel > max_process_desc)
1988 max_process_desc = inchannel;
1989
d0d6b7c5
JB
1990 /* Until we store the proper pid, enable sigchld_handler
1991 to recognize an unknown pid as standing for this process.
1992 It is very important not to let this `marker' value stay
1993 in the table after this function has returned; if it does
1994 it might cause call-process to hang and subsequent asynchronous
1995 processes to get their return values scrambled. */
6bfd98e7 1996 XPROCESS (process)->pid = -1;
d0d6b7c5 1997
ececcbec 1998 BLOCK_INPUT;
177c0ea7 1999
d0d6b7c5
JB
2000 {
2001 /* child_setup must clobber environ on systems with true vfork.
2002 Protect it from permanent change. */
2003 char **save_environ = environ;
2004
14dc6093 2005 current_dir = ENCODE_FILE (current_dir);
a932f187 2006
e98d950b 2007#ifndef WINDOWSNT
d0d6b7c5
JB
2008 pid = vfork ();
2009 if (pid == 0)
e98d950b 2010#endif /* not WINDOWSNT */
d0d6b7c5
JB
2011 {
2012 int xforkin = forkin;
2013 int xforkout = forkout;
2014
2015#if 0 /* This was probably a mistake--it duplicates code later on,
2016 but fails to handle all the cases. */
2017 /* Make sure SIGCHLD is not blocked in the child. */
2018 sigsetmask (SIGEMPTYMASK);
2019#endif
2020
2021 /* Make the pty be the controlling terminal of the process. */
2022#ifdef HAVE_PTYS
2023 /* First, disconnect its current controlling terminal. */
2024#ifdef HAVE_SETSID
7ce48618
RS
2025 /* We tried doing setsid only if pty_flag, but it caused
2026 process_set_signal to fail on SGI when using a pipe. */
2027 setsid ();
ce4c9c90 2028 /* Make the pty's terminal the controlling terminal. */
084fd64a 2029 if (pty_flag)
39e9ebcd 2030 {
39e9ebcd
RS
2031#ifdef TIOCSCTTY
2032 /* We ignore the return value
2033 because faith@cs.unc.edu says that is necessary on Linux. */
2034 ioctl (xforkin, TIOCSCTTY, 0);
ce4c9c90 2035#endif
39e9ebcd 2036 }
d0d6b7c5 2037#else /* not HAVE_SETSID */
c14e53a4 2038#ifdef USG
000ab717 2039 /* It's very important to call setpgrp here and no time
d0d6b7c5
JB
2040 afterwards. Otherwise, we lose our controlling tty which
2041 is set when we open the pty. */
2042 setpgrp ();
2043#endif /* USG */
2044#endif /* not HAVE_SETSID */
9bcf8ec6
KH
2045#if defined (HAVE_TERMIOS) && defined (LDISC1)
2046 if (pty_flag && xforkin >= 0)
2047 {
2048 struct termios t;
2049 tcgetattr (xforkin, &t);
2050 t.c_lflag = LDISC1;
2051 if (tcsetattr (xforkin, TCSANOW, &t) < 0)
68c45bf0 2052 emacs_write (1, "create_process/tcsetattr LDISC1 failed\n", 39);
9bcf8ec6
KH
2053 }
2054#else
aafadd9f 2055#if defined (NTTYDISC) && defined (TIOCSETD)
ff773a4e 2056 if (pty_flag && xforkin >= 0)
afc549fd
RS
2057 {
2058 /* Use new line discipline. */
2059 int ldisc = NTTYDISC;
4458f555 2060 ioctl (xforkin, TIOCSETD, &ldisc);
afc549fd 2061 }
000ab717 2062#endif
9bcf8ec6 2063#endif
177c0ea7 2064#ifdef TIOCNOTTY
d0d6b7c5
JB
2065 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
2066 can do TIOCSPGRP only to the process's controlling tty. */
2067 if (pty_flag)
2068 {
177c0ea7 2069 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
d0d6b7c5 2070 I can't test it since I don't have 4.3. */
68c45bf0 2071 int j = emacs_open ("/dev/tty", O_RDWR, 0);
d0d6b7c5 2072 ioctl (j, TIOCNOTTY, 0);
68c45bf0 2073 emacs_close (j);
5a570e37 2074#ifndef USG
d0d6b7c5
JB
2075 /* In order to get a controlling terminal on some versions
2076 of BSD, it is necessary to put the process in pgrp 0
2077 before it opens the terminal. */
99c1aeca 2078#ifdef HAVE_SETPGID
3ea1d291
RS
2079 setpgid (0, 0);
2080#else
d0d6b7c5 2081 setpgrp (0, 0);
3ea1d291 2082#endif
d0d6b7c5
JB
2083#endif
2084 }
2085#endif /* TIOCNOTTY */
2086
e39a993c 2087#if !defined (DONT_REOPEN_PTY)
d0d6b7c5 2088/*** There is a suggestion that this ought to be a
99153b9e
RS
2089 conditional on TIOCSPGRP,
2090 or !(defined (HAVE_SETSID) && defined (TIOCSCTTY)).
2091 Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
2092 that system does seem to need this code, even though
2093 both HAVE_SETSID and TIOCSCTTY are defined. */
d0d6b7c5
JB
2094 /* Now close the pty (if we had it open) and reopen it.
2095 This makes the pty the controlling terminal of the subprocess. */
2096 if (pty_flag)
2097 {
99e3d726 2098
68c45bf0
PE
2099 /* I wonder if emacs_close (emacs_open (pty_name, ...))
2100 would work? */
d0d6b7c5 2101 if (xforkin >= 0)
68c45bf0
PE
2102 emacs_close (xforkin);
2103 xforkout = xforkin = emacs_open (pty_name, O_RDWR, 0);
d0d6b7c5 2104
4aa54ba8
RS
2105 if (xforkin < 0)
2106 {
68c45bf0
PE
2107 emacs_write (1, "Couldn't open the pty terminal ", 31);
2108 emacs_write (1, pty_name, strlen (pty_name));
2109 emacs_write (1, "\n", 1);
4aa54ba8
RS
2110 _exit (1);
2111 }
2112
d0d6b7c5 2113 }
e39a993c 2114#endif /* not DONT_REOPEN_PTY */
e9bf058b 2115
d0d6b7c5 2116#ifdef SETUP_SLAVE_PTY
13a72104
RS
2117 if (pty_flag)
2118 {
2119 SETUP_SLAVE_PTY;
2120 }
d0d6b7c5
JB
2121#endif /* SETUP_SLAVE_PTY */
2122#ifdef AIX
2123 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
2124 Now reenable it in the child, so it will die when we want it to. */
2125 if (pty_flag)
2126 signal (SIGHUP, SIG_DFL);
2127#endif
2128#endif /* HAVE_PTYS */
2129
0dc70c33
KH
2130 signal (SIGINT, SIG_DFL);
2131 signal (SIGQUIT, SIG_DFL);
2132
2133 /* Stop blocking signals in the child. */
2134#ifdef POSIX_SIGNALS
2135 sigprocmask (SIG_SETMASK, &procmask, 0);
2136#else /* !POSIX_SIGNALS */
d0d6b7c5 2137#ifdef SIGCHLD
e39a993c 2138#if defined (BSD_SYSTEM) || defined (HPUX)
d0d6b7c5
JB
2139 sigsetmask (SIGEMPTYMASK);
2140#else /* ordinary USG */
63528b78 2141#if 0
d0d6b7c5 2142 signal (SIGCHLD, sigchld);
63528b78 2143#endif
d0d6b7c5 2144#endif /* ordinary USG */
d0d6b7c5 2145#endif /* SIGCHLD */
0dc70c33 2146#endif /* !POSIX_SIGNALS */
5e7e1da2 2147
ab01d0a8
RS
2148 if (pty_flag)
2149 child_setup_tty (xforkout);
e98d950b
RS
2150#ifdef WINDOWSNT
2151 pid = child_setup (xforkin, xforkout, xforkout,
2152 new_argv, 1, current_dir);
177c0ea7 2153#else /* not WINDOWSNT */
2c4df143
YM
2154#ifdef FD_CLOEXEC
2155 emacs_close (wait_child_setup[0]);
2156#endif
d0d6b7c5 2157 child_setup (xforkin, xforkout, xforkout,
e065a56e 2158 new_argv, 1, current_dir);
e98d950b 2159#endif /* not WINDOWSNT */
d0d6b7c5
JB
2160 }
2161 environ = save_environ;
2162 }
2163
ececcbec
RS
2164 UNBLOCK_INPUT;
2165
4a127b3b 2166 /* This runs in the Emacs process. */
d0d6b7c5 2167 if (pid < 0)
6311cf58
RS
2168 {
2169 if (forkin >= 0)
68c45bf0 2170 emacs_close (forkin);
6311cf58 2171 if (forkin != forkout && forkout >= 0)
68c45bf0 2172 emacs_close (forkout);
6311cf58 2173 }
4a127b3b
KH
2174 else
2175 {
2176 /* vfork succeeded. */
6bfd98e7 2177 XPROCESS (process)->pid = pid;
d0d6b7c5 2178
e98d950b 2179#ifdef WINDOWSNT
4a127b3b 2180 register_child (pid, inchannel);
e98d950b
RS
2181#endif /* WINDOWSNT */
2182
4a127b3b
KH
2183 /* If the subfork execv fails, and it exits,
2184 this close hangs. I don't know why.
2185 So have an interrupt jar it loose. */
30904ab7
GM
2186 {
2187 struct atimer *timer;
2188 EMACS_TIME offset;
177c0ea7 2189
30904ab7
GM
2190 stop_polling ();
2191 EMACS_SET_SECS_USECS (offset, 1, 0);
2192 timer = start_atimer (ATIMER_RELATIVE, offset, create_process_1, 0);
177c0ea7 2193
30904ab7
GM
2194 if (forkin >= 0)
2195 emacs_close (forkin);
2196
2197 cancel_atimer (timer);
2198 start_polling ();
2199 }
177c0ea7 2200
4a127b3b 2201 if (forkin != forkout && forkout >= 0)
68c45bf0 2202 emacs_close (forkout);
d0d6b7c5 2203
875e6b94 2204#ifdef HAVE_PTYS
4a127b3b
KH
2205 if (pty_flag)
2206 XPROCESS (process)->tty_name = build_string (pty_name);
2207 else
875e6b94 2208#endif
4a127b3b 2209 XPROCESS (process)->tty_name = Qnil;
2c4df143
YM
2210
2211#if !defined (WINDOWSNT) && defined (FD_CLOEXEC)
2212 /* Wait for child_setup to complete in case that vfork is
2213 actually defined as fork. The descriptor wait_child_setup[1]
2214 of a pipe is closed at the child side either by close-on-exec
2215 on successful execvp or the _exit call in child_setup. */
2216 {
2217 char dummy;
2218
2219 emacs_close (wait_child_setup[1]);
2220 emacs_read (wait_child_setup[0], &dummy, 1);
2221 emacs_close (wait_child_setup[0]);
2222 }
2223#endif
4a127b3b 2224 }
3b9a3dfa 2225
4a127b3b
KH
2226 /* Restore the signal state whether vfork succeeded or not.
2227 (We will signal an error, below, if it failed.) */
0dc70c33 2228#ifdef POSIX_SIGNALS
351e611f 2229#ifdef HAVE_WORKING_VFORK
0dc70c33
KH
2230 /* Restore the parent's signal handlers. */
2231 sigaction (SIGINT, &sigint_action, 0);
2232 sigaction (SIGQUIT, &sigquit_action, 0);
2233#ifdef AIX
2234 sigaction (SIGHUP, &sighup_action, 0);
2235#endif
351e611f 2236#endif /* HAVE_WORKING_VFORK */
0dc70c33
KH
2237 /* Stop blocking signals in the parent. */
2238 sigprocmask (SIG_SETMASK, &procmask, 0);
2239#else /* !POSIX_SIGNALS */
d0d6b7c5 2240#ifdef SIGCHLD
e39a993c 2241#if defined (BSD_SYSTEM) || defined (HPUX)
d0d6b7c5
JB
2242 sigsetmask (SIGEMPTYMASK);
2243#else /* ordinary USG */
2244#if 0
2245 signal (SIGCHLD, sigchld);
2246 /* Now really handle any of these signals
2247 that came in during this function. */
2248 if (sigchld_deferred)
2249 kill (getpid (), SIGCHLD);
2250#endif
2251#endif /* ordinary USG */
d0d6b7c5 2252#endif /* SIGCHLD */
0dc70c33 2253#endif /* !POSIX_SIGNALS */
4a127b3b
KH
2254
2255 /* Now generate the error if vfork failed. */
2256 if (pid < 0)
2257 report_file_error ("Doing vfork", Qnil);
d0d6b7c5 2258}
d0d6b7c5 2259
e690ca94 2260\f
d0d6b7c5
JB
2261#ifdef HAVE_SOCKETS
2262
e690ca94
KS
2263/* Convert an internal struct sockaddr to a lisp object (vector or string).
2264 The address family of sa is not included in the result. */
2265
2266static Lisp_Object
2267conv_sockaddr_to_lisp (sa, len)
2268 struct sockaddr *sa;
2269 int len;
2270{
2271 Lisp_Object address;
2272 int i;
2273 unsigned char *cp;
2274 register struct Lisp_Vector *p;
2275
63136da6
AS
2276 /* Workaround for a bug in getsockname on BSD: Names bound to
2277 sockets in the UNIX domain are inaccessible; getsockname returns
2278 a zero length name. */
2279 if (len < OFFSETOF (struct sockaddr, sa_family) + sizeof (sa->sa_family))
fff4e459 2280 return empty_unibyte_string;
63136da6 2281
e690ca94
KS
2282 switch (sa->sa_family)
2283 {
2284 case AF_INET:
2285 {
2286 struct sockaddr_in *sin = (struct sockaddr_in *) sa;
2287 len = sizeof (sin->sin_addr) + 1;
2288 address = Fmake_vector (make_number (len), Qnil);
2289 p = XVECTOR (address);
2290 p->contents[--len] = make_number (ntohs (sin->sin_port));
63136da6 2291 cp = (unsigned char *) &sin->sin_addr;
e690ca94
KS
2292 break;
2293 }
e1652a86
KS
2294#ifdef AF_INET6
2295 case AF_INET6:
2296 {
2297 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
63136da6 2298 uint16_t *ip6 = (uint16_t *) &sin6->sin6_addr;
e1652a86
KS
2299 len = sizeof (sin6->sin6_addr)/2 + 1;
2300 address = Fmake_vector (make_number (len), Qnil);
2301 p = XVECTOR (address);
2302 p->contents[--len] = make_number (ntohs (sin6->sin6_port));
2303 for (i = 0; i < len; i++)
2304 p->contents[i] = make_number (ntohs (ip6[i]));
2305 return address;
2306 }
2307#endif
e690ca94
KS
2308#ifdef HAVE_LOCAL_SOCKETS
2309 case AF_LOCAL:
2310 {
fb23673a
EZ
2311 struct sockaddr_un *sockun = (struct sockaddr_un *) sa;
2312 for (i = 0; i < sizeof (sockun->sun_path); i++)
2313 if (sockun->sun_path[i] == 0)
e690ca94 2314 break;
fb23673a 2315 return make_unibyte_string (sockun->sun_path, i);
e690ca94
KS
2316 }
2317#endif
2318 default:
63136da6 2319 len -= OFFSETOF (struct sockaddr, sa_family) + sizeof (sa->sa_family);
e690ca94
KS
2320 address = Fcons (make_number (sa->sa_family),
2321 Fmake_vector (make_number (len), Qnil));
2322 p = XVECTOR (XCDR (address));
63136da6 2323 cp = (unsigned char *) &sa->sa_family + sizeof (sa->sa_family);
e690ca94
KS
2324 break;
2325 }
2326
2327 i = 0;
2328 while (i < len)
2329 p->contents[i++] = make_number (*cp++);
2330
2331 return address;
2332}
2333
2334
2335/* Get family and required size for sockaddr structure to hold ADDRESS. */
2336
2337static int
2338get_lisp_to_sockaddr_size (address, familyp)
2339 Lisp_Object address;
2340 int *familyp;
2341{
2342 register struct Lisp_Vector *p;
2343
2344 if (VECTORP (address))
2345 {
2346 p = XVECTOR (address);
2347 if (p->size == 5)
2348 {
2349 *familyp = AF_INET;
2350 return sizeof (struct sockaddr_in);
2351 }
e1652a86
KS
2352#ifdef AF_INET6
2353 else if (p->size == 9)
2354 {
2355 *familyp = AF_INET6;
2356 return sizeof (struct sockaddr_in6);
2357 }
2358#endif
e690ca94
KS
2359 }
2360#ifdef HAVE_LOCAL_SOCKETS
2361 else if (STRINGP (address))
2362 {
2363 *familyp = AF_LOCAL;
2364 return sizeof (struct sockaddr_un);
2365 }
2366#endif
2367 else if (CONSP (address) && INTEGERP (XCAR (address)) && VECTORP (XCDR (address)))
2368 {
2369 struct sockaddr *sa;
2370 *familyp = XINT (XCAR (address));
2371 p = XVECTOR (XCDR (address));
2372 return p->size + sizeof (sa->sa_family);
2373 }
2374 return 0;
2375}
2376
2377/* Convert an address object (vector or string) to an internal sockaddr.
9d1a8e5d
RS
2378
2379 The address format has been basically validated by
2380 get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
2381 it could have come from user data. So if FAMILY is not valid,
2382 we return after zeroing *SA. */
e690ca94
KS
2383
2384static void
2385conv_lisp_to_sockaddr (family, address, sa, len)
2386 int family;
2387 Lisp_Object address;
2388 struct sockaddr *sa;
2389 int len;
2390{
2391 register struct Lisp_Vector *p;
34967d0f 2392 register unsigned char *cp = NULL;
e690ca94
KS
2393 register int i;
2394
2395 bzero (sa, len);
e690ca94
KS
2396
2397 if (VECTORP (address))
2398 {
2399 p = XVECTOR (address);
2400 if (family == AF_INET)
2401 {
2402 struct sockaddr_in *sin = (struct sockaddr_in *) sa;
2403 len = sizeof (sin->sin_addr) + 1;
2404 i = XINT (p->contents[--len]);
2405 sin->sin_port = htons (i);
2406 cp = (unsigned char *)&sin->sin_addr;
9d1a8e5d 2407 sa->sa_family = family;
e690ca94 2408 }
e1652a86
KS
2409#ifdef AF_INET6
2410 else if (family == AF_INET6)
2411 {
2412 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa;
2413 uint16_t *ip6 = (uint16_t *)&sin6->sin6_addr;
2414 len = sizeof (sin6->sin6_addr) + 1;
2415 i = XINT (p->contents[--len]);
2416 sin6->sin6_port = htons (i);
2417 for (i = 0; i < len; i++)
2418 if (INTEGERP (p->contents[i]))
2419 {
2420 int j = XFASTINT (p->contents[i]) & 0xffff;
2421 ip6[i] = ntohs (j);
2422 }
9d1a8e5d 2423 sa->sa_family = family;
e1652a86
KS
2424 }
2425#endif
6bf890d7 2426 return;
e690ca94
KS
2427 }
2428 else if (STRINGP (address))
2429 {
2430#ifdef HAVE_LOCAL_SOCKETS
2431 if (family == AF_LOCAL)
2432 {
fb23673a 2433 struct sockaddr_un *sockun = (struct sockaddr_un *) sa;
d5db4077 2434 cp = SDATA (address);
fb23673a
EZ
2435 for (i = 0; i < sizeof (sockun->sun_path) && *cp; i++)
2436 sockun->sun_path[i] = *cp++;
9d1a8e5d 2437 sa->sa_family = family;
e690ca94
KS
2438 }
2439#endif
2440 return;
2441 }
2442 else
2443 {
2444 p = XVECTOR (XCDR (address));
2445 cp = (unsigned char *)sa + sizeof (sa->sa_family);
2446 }
2447
2448 for (i = 0; i < len; i++)
2449 if (INTEGERP (p->contents[i]))
2450 *cp++ = XFASTINT (p->contents[i]) & 0xff;
2451}
2452
2453#ifdef DATAGRAM_SOCKETS
2454DEFUN ("process-datagram-address", Fprocess_datagram_address, Sprocess_datagram_address,
2455 1, 1, 0,
2456 doc: /* Get the current datagram address associated with PROCESS. */)
2457 (process)
2458 Lisp_Object process;
2459{
2460 int channel;
2461
2462 CHECK_PROCESS (process);
2463
2464 if (!DATAGRAM_CONN_P (process))
2465 return Qnil;
2466
60f0fb11 2467 channel = XPROCESS (process)->infd;
e690ca94
KS
2468 return conv_sockaddr_to_lisp (datagram_address[channel].sa,
2469 datagram_address[channel].len);
2470}
2471
2472DEFUN ("set-process-datagram-address", Fset_process_datagram_address, Sset_process_datagram_address,
2473 2, 2, 0,
2474 doc: /* Set the datagram address for PROCESS to ADDRESS.
2475Returns nil upon error setting address, ADDRESS otherwise. */)
2476 (process, address)
2477 Lisp_Object process, address;
2478{
2479 int channel;
2480 int family, len;
2481
2482 CHECK_PROCESS (process);
2483
2484 if (!DATAGRAM_CONN_P (process))
2485 return Qnil;
2486
60f0fb11 2487 channel = XPROCESS (process)->infd;
e690ca94
KS
2488
2489 len = get_lisp_to_sockaddr_size (address, &family);
2490 if (datagram_address[channel].len != len)
2491 return Qnil;
2492 conv_lisp_to_sockaddr (family, address, datagram_address[channel].sa, len);
2493 return address;
2494}
2495#endif
2496\f
2497
2498static struct socket_options {
2499 /* The name of this option. Should be lowercase version of option
177c0ea7 2500 name without SO_ prefix. */
e690ca94 2501 char *name;
e690ca94
KS
2502 /* Option level SOL_... */
2503 int optlevel;
2504 /* Option number SO_... */
2505 int optnum;
6b61353c
KH
2506 enum { SOPT_UNKNOWN, SOPT_BOOL, SOPT_INT, SOPT_IFNAME, SOPT_LINGER } opttype;
2507 enum { OPIX_NONE=0, OPIX_MISC=1, OPIX_REUSEADDR=2 } optbit;
e690ca94
KS
2508} socket_options[] =
2509 {
2510#ifdef SO_BINDTODEVICE
6b61353c 2511 { ":bindtodevice", SOL_SOCKET, SO_BINDTODEVICE, SOPT_IFNAME, OPIX_MISC },
e690ca94
KS
2512#endif
2513#ifdef SO_BROADCAST
6b61353c 2514 { ":broadcast", SOL_SOCKET, SO_BROADCAST, SOPT_BOOL, OPIX_MISC },
e690ca94
KS
2515#endif
2516#ifdef SO_DONTROUTE
6b61353c 2517 { ":dontroute", SOL_SOCKET, SO_DONTROUTE, SOPT_BOOL, OPIX_MISC },
e690ca94
KS
2518#endif
2519#ifdef SO_KEEPALIVE
6b61353c 2520 { ":keepalive", SOL_SOCKET, SO_KEEPALIVE, SOPT_BOOL, OPIX_MISC },
e690ca94
KS
2521#endif
2522#ifdef SO_LINGER
6b61353c 2523 { ":linger", SOL_SOCKET, SO_LINGER, SOPT_LINGER, OPIX_MISC },
e690ca94
KS
2524#endif
2525#ifdef SO_OOBINLINE
6b61353c 2526 { ":oobinline", SOL_SOCKET, SO_OOBINLINE, SOPT_BOOL, OPIX_MISC },
e690ca94
KS
2527#endif
2528#ifdef SO_PRIORITY
6b61353c 2529 { ":priority", SOL_SOCKET, SO_PRIORITY, SOPT_INT, OPIX_MISC },
e690ca94
KS
2530#endif
2531#ifdef SO_REUSEADDR
6b61353c 2532 { ":reuseaddr", SOL_SOCKET, SO_REUSEADDR, SOPT_BOOL, OPIX_REUSEADDR },
e690ca94 2533#endif
6b61353c 2534 { 0, 0, 0, SOPT_UNKNOWN, OPIX_NONE }
e690ca94
KS
2535 };
2536
6b61353c 2537/* Set option OPT to value VAL on socket S.
e690ca94 2538
6b61353c
KH
2539 Returns (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
2540 Signals an error if setting a known option fails.
2541*/
e690ca94
KS
2542
2543static int
6b61353c 2544set_socket_option (s, opt, val)
e690ca94 2545 int s;
6b61353c 2546 Lisp_Object opt, val;
e690ca94 2547{
6b61353c
KH
2548 char *name;
2549 struct socket_options *sopt;
2550 int ret = 0;
e690ca94 2551
6b61353c 2552 CHECK_SYMBOL (opt);
e690ca94 2553
6b61353c
KH
2554 name = (char *) SDATA (SYMBOL_NAME (opt));
2555 for (sopt = socket_options; sopt->name; sopt++)
2556 if (strcmp (name, sopt->name) == 0)
2557 break;
e690ca94 2558
6b61353c
KH
2559 switch (sopt->opttype)
2560 {
2561 case SOPT_BOOL:
2562 {
2563 int optval;
2564 optval = NILP (val) ? 0 : 1;
2565 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2566 &optval, sizeof (optval));
2567 break;
2568 }
e690ca94 2569
6b61353c
KH
2570 case SOPT_INT:
2571 {
2572 int optval;
2573 if (INTEGERP (val))
2574 optval = XINT (val);
2575 else
2576 error ("Bad option value for %s", name);
2577 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2578 &optval, sizeof (optval));
2579 break;
2580 }
e690ca94 2581
6b61353c
KH
2582#ifdef SO_BINDTODEVICE
2583 case SOPT_IFNAME:
2584 {
2585 char devname[IFNAMSIZ+1];
e690ca94 2586
6b61353c
KH
2587 /* This is broken, at least in the Linux 2.4 kernel.
2588 To unbind, the arg must be a zero integer, not the empty string.
2589 This should work on all systems. KFS. 2003-09-23. */
2590 bzero (devname, sizeof devname);
2591 if (STRINGP (val))
e690ca94 2592 {
6b61353c
KH
2593 char *arg = (char *) SDATA (val);
2594 int len = min (strlen (arg), IFNAMSIZ);
2595 bcopy (arg, devname, len);
e690ca94 2596 }
6b61353c
KH
2597 else if (!NILP (val))
2598 error ("Bad option value for %s", name);
2599 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2600 devname, IFNAMSIZ);
2601 break;
2602 }
2603#endif
e690ca94 2604
177c0ea7 2605#ifdef SO_LINGER
6b61353c
KH
2606 case SOPT_LINGER:
2607 {
2608 struct linger linger;
e690ca94 2609
6b61353c
KH
2610 linger.l_onoff = 1;
2611 linger.l_linger = 0;
2612 if (INTEGERP (val))
2613 linger.l_linger = XINT (val);
2614 else
2615 linger.l_onoff = NILP (val) ? 0 : 1;
2616 ret = setsockopt (s, sopt->optlevel, sopt->optnum,
2617 &linger, sizeof (linger));
2618 break;
2619 }
e690ca94 2620#endif
6b61353c
KH
2621
2622 default:
2623 return 0;
e690ca94 2624 }
6b61353c
KH
2625
2626 if (ret < 0)
2627 report_file_error ("Cannot set network option",
2628 Fcons (opt, Fcons (val, Qnil)));
2629 return (1 << sopt->optbit);
e690ca94
KS
2630}
2631
6b61353c
KH
2632
2633DEFUN ("set-network-process-option",
2634 Fset_network_process_option, Sset_network_process_option,
2635 3, 4, 0,
2636 doc: /* For network process PROCESS set option OPTION to value VALUE.
2637See `make-network-process' for a list of options and values.
2638If optional fourth arg NO-ERROR is non-nil, don't signal an error if
2639OPTION is not a supported option, return nil instead; otherwise return t. */)
2640 (process, option, value, no_error)
2641 Lisp_Object process, option, value;
2642 Lisp_Object no_error;
e690ca94 2643{
6b61353c
KH
2644 int s;
2645 struct Lisp_Process *p;
e690ca94 2646
e690ca94 2647 CHECK_PROCESS (process);
6b61353c
KH
2648 p = XPROCESS (process);
2649 if (!NETCONN1_P (p))
2650 error ("Process is not a network process");
2651
60f0fb11 2652 s = p->infd;
6b61353c
KH
2653 if (s < 0)
2654 error ("Process is not running");
2655
2656 if (set_socket_option (s, option, value))
e690ca94 2657 {
6b61353c
KH
2658 p->childp = Fplist_put (p->childp, option, value);
2659 return Qt;
e690ca94 2660 }
6b61353c
KH
2661
2662 if (NILP (no_error))
2663 error ("Unknown or unsupported option");
2664
2665 return Qnil;
e690ca94 2666}
6b61353c 2667
e690ca94 2668\f
d888760c
GM
2669#ifdef HAVE_SERIAL
2670DEFUN ("serial-process-configure",
2671 Fserial_process_configure,
2672 Sserial_process_configure,
2673 0, MANY, 0,
2674 doc: /* Configure speed, bytesize, etc. of a serial process.
2675
2676Arguments are specified as keyword/argument pairs. Attributes that
2677are not given are re-initialized from the process's current
2678configuration (available via the function `process-contact') or set to
2679reasonable default values. The following arguments are defined:
2680
2681:process PROCESS
2682:name NAME
2683:buffer BUFFER
2684:port PORT
2685-- Any of these arguments can be given to identify the process that is
2686to be configured. If none of these arguments is given, the current
2687buffer's process is used.
2688
2689:speed SPEED -- SPEED is the speed of the serial port in bits per
2690second, also called baud rate. Any value can be given for SPEED, but
2691most serial ports work only at a few defined values between 1200 and
2692115200, with 9600 being the most common value. If SPEED is nil, the
2693serial port is not configured any further, i.e., all other arguments
2694are ignored. This may be useful for special serial ports such as
2695Bluetooth-to-serial converters which can only be configured through AT
2696commands. A value of nil for SPEED can be used only when passed
2697through `make-serial-process' or `serial-term'.
2698
2699:bytesize BYTESIZE -- BYTESIZE is the number of bits per byte, which
2700can be 7 or 8. If BYTESIZE is not given or nil, a value of 8 is used.
2701
2702:parity PARITY -- PARITY can be nil (don't use parity), the symbol
2703`odd' (use odd parity), or the symbol `even' (use even parity). If
2704PARITY is not given, no parity is used.
2705
2706:stopbits STOPBITS -- STOPBITS is the number of stopbits used to
2707terminate a byte transmission. STOPBITS can be 1 or 2. If STOPBITS
2708is not given or nil, 1 stopbit is used.
2709
2710:flowcontrol FLOWCONTROL -- FLOWCONTROL determines the type of
2711flowcontrol to be used, which is either nil (don't use flowcontrol),
2712the symbol `hw' (use RTS/CTS hardware flowcontrol), or the symbol `sw'
2713\(use XON/XOFF software flowcontrol). If FLOWCONTROL is not given, no
2714flowcontrol is used.
2715
2716`serial-process-configure' is called by `make-serial-process' for the
2717initial configuration of the serial port.
2718
2719Examples:
2720
2721\(serial-process-configure :process "/dev/ttyS0" :speed 1200)
2722
2723\(serial-process-configure
2724 :buffer "COM1" :stopbits 1 :parity 'odd :flowcontrol 'hw)
2725
2726\(serial-process-configure :port "\\\\.\\COM13" :bytesize 7)
2727
2728usage: (serial-process-configure &rest ARGS) */)
2729 (nargs, args)
2730 int nargs;
2731 Lisp_Object *args;
2732{
2733 struct Lisp_Process *p;
2734 Lisp_Object contact = Qnil;
2735 Lisp_Object proc = Qnil;
2736 struct gcpro gcpro1;
2737
2738 contact = Flist (nargs, args);
2739 GCPRO1 (contact);
2740
2741 proc = Fplist_get (contact, QCprocess);
2742 if (NILP (proc))
2743 proc = Fplist_get (contact, QCname);
2744 if (NILP (proc))
2745 proc = Fplist_get (contact, QCbuffer);
2746 if (NILP (proc))
2747 proc = Fplist_get (contact, QCport);
2748 proc = get_process (proc);
2749 p = XPROCESS (proc);
a879ee6f 2750 if (!EQ (p->type, Qserial))
d888760c
GM
2751 error ("Not a serial process");
2752
2753 if (NILP (Fplist_get (p->childp, QCspeed)))
2754 {
2755 UNGCPRO;
2756 return Qnil;
2757 }
2758
2759 serial_configure (p, contact);
2760
2761 UNGCPRO;
2762 return Qnil;
2763}
d888760c 2764
d888760c
GM
2765/* Used by make-serial-process to recover from errors. */
2766Lisp_Object make_serial_process_unwind (Lisp_Object proc)
2767{
2768 if (!PROCESSP (proc))
2769 abort ();
2770 remove_process (proc);
2771 return Qnil;
2772}
d888760c 2773
d888760c
GM
2774DEFUN ("make-serial-process", Fmake_serial_process, Smake_serial_process,
2775 0, MANY, 0,
2776 doc: /* Create and return a serial port process.
2777
2778In Emacs, serial port connections are represented by process objects,
2779so input and output work as for subprocesses, and `delete-process'
2780closes a serial port connection. However, a serial process has no
2781process id, it cannot be signaled, and the status codes are different
2782from normal processes.
2783
2784`make-serial-process' creates a process and a buffer, on which you
2785probably want to use `process-send-string'. Try \\[serial-term] for
2786an interactive terminal. See below for examples.
2787
2788Arguments are specified as keyword/argument pairs. The following
2789arguments are defined:
2790
2791:port PORT -- (mandatory) PORT is the path or name of the serial port.
2792For example, this could be "/dev/ttyS0" on Unix. On Windows, this
2793could be "COM1", or "\\\\.\\COM10" for ports higher than COM9 (double
2794the backslashes in strings).
2795
2796:speed SPEED -- (mandatory) is handled by `serial-process-configure',
2797which is called by `make-serial-process'.
2798
2799:name NAME -- NAME is the name of the process. If NAME is not given,
2800the value of PORT is used.
2801
2802:buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2803with the process. Process output goes at the end of that buffer,
2804unless you specify an output stream or filter function to handle the
2805output. If BUFFER is not given, the value of NAME is used.
2806
2807:coding CODING -- If CODING is a symbol, it specifies the coding
2808system used for both reading and writing for this process. If CODING
2809is a cons (DECODING . ENCODING), DECODING is used for reading, and
2810ENCODING is used for writing.
2811
2812:noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
2813the process is running. If BOOL is not given, query before exiting.
2814
2815:stop BOOL -- Start process in the `stopped' state if BOOL is non-nil.
2816In the stopped state, a serial process does not accept incoming data,
2817but you can send outgoing data. The stopped state is cleared by
2818`continue-process' and set by `stop-process'.
2819
2820:filter FILTER -- Install FILTER as the process filter.
2821
2822:sentinel SENTINEL -- Install SENTINEL as the process sentinel.
2823
2824:plist PLIST -- Install PLIST as the initial plist of the process.
2825
2826:speed
2827:bytesize
2828:parity
2829:stopbits
2830:flowcontrol
2831-- These arguments are handled by `serial-process-configure', which is
2832called by `make-serial-process'.
2833
2834The original argument list, possibly modified by later configuration,
2835is available via the function `process-contact'.
2836
2837Examples:
2838
2839\(make-serial-process :port "/dev/ttyS0" :speed 9600)
2840
2841\(make-serial-process :port "COM1" :speed 115200 :stopbits 2)
2842
2843\(make-serial-process :port "\\\\.\\COM13" :speed 1200 :bytesize 7 :parity 'odd)
2844
2845\(make-serial-process :port "/dev/tty.BlueConsole-SPP-1" :speed nil)
2846
2847usage: (make-serial-process &rest ARGS) */)
2848 (nargs, args)
2849 int nargs;
2850 Lisp_Object *args;
2851{
2852 int fd = -1;
2853 Lisp_Object proc, contact, port;
2854 struct Lisp_Process *p;
2855 struct gcpro gcpro1;
2856 Lisp_Object name, buffer;
2857 Lisp_Object tem, val;
2858 int specpdl_count = -1;
2859
2860 if (nargs == 0)
2861 return Qnil;
2862
2863 contact = Flist (nargs, args);
2864 GCPRO1 (contact);
2865
2866 port = Fplist_get (contact, QCport);
2867 if (NILP (port))
2868 error ("No port specified");
2869 CHECK_STRING (port);
2870
2871 if (NILP (Fplist_member (contact, QCspeed)))
2872 error (":speed not specified");
2873 if (!NILP (Fplist_get (contact, QCspeed)))
2874 CHECK_NUMBER (Fplist_get (contact, QCspeed));
2875
2876 name = Fplist_get (contact, QCname);
2877 if (NILP (name))
2878 name = port;
2879 CHECK_STRING (name);
2880 proc = make_process (name);
2881 specpdl_count = SPECPDL_INDEX ();
2882 record_unwind_protect (make_serial_process_unwind, proc);
2883 p = XPROCESS (proc);
2884
2885 fd = serial_open ((char*) SDATA (port));
2886 p->infd = fd;
2887 p->outfd = fd;
2888 if (fd > max_process_desc)
2889 max_process_desc = fd;
2890 chan_process[fd] = proc;
2891
2892 buffer = Fplist_get (contact, QCbuffer);
2893 if (NILP (buffer))
2894 buffer = name;
2895 buffer = Fget_buffer_create (buffer);
2896 p->buffer = buffer;
2897
2898 p->childp = contact;
2899 p->plist = Fcopy_sequence (Fplist_get (contact, QCplist));
2900 p->type = Qserial;
2901 p->sentinel = Fplist_get (contact, QCsentinel);
2902 p->filter = Fplist_get (contact, QCfilter);
2903 p->log = Qnil;
2904 if (tem = Fplist_get (contact, QCnoquery), !NILP (tem))
2905 p->kill_without_query = 1;
2906 if (tem = Fplist_get (contact, QCstop), !NILP (tem))
2907 p->command = Qt;
2908 p->pty_flag = 0;
2909
2910 if (!EQ (p->command, Qt))
2911 {
2912 FD_SET (fd, &input_wait_mask);
2913 FD_SET (fd, &non_keyboard_wait_mask);
2914 }
2915
2916 if (BUFFERP (buffer))
2917 {
2918 set_marker_both (p->mark, buffer,
2919 BUF_ZV (XBUFFER (buffer)),
2920 BUF_ZV_BYTE (XBUFFER (buffer)));
2921 }
2922
2923 tem = Fplist_member (contact, QCcoding);
2924 if (!NILP (tem) && (!CONSP (tem) || !CONSP (XCDR (tem))))
2925 tem = Qnil;
2926
2927 val = Qnil;
2928 if (!NILP (tem))
2929 {
2930 val = XCAR (XCDR (tem));
2931 if (CONSP (val))
2932 val = XCAR (val);
2933 }
2934 else if (!NILP (Vcoding_system_for_read))
2935 val = Vcoding_system_for_read;
2936 else if ((!NILP (buffer) && NILP (XBUFFER (buffer)->enable_multibyte_characters))
2937 || (NILP (buffer) && NILP (buffer_defaults.enable_multibyte_characters)))
2938 val = Qnil;
2939 p->decode_coding_system = val;
2940
2941 val = Qnil;
2942 if (!NILP (tem))
2943 {
2944 val = XCAR (XCDR (tem));
2945 if (CONSP (val))
2946 val = XCDR (val);
2947 }
2948 else if (!NILP (Vcoding_system_for_write))
2949 val = Vcoding_system_for_write;
2950 else if ((!NILP (buffer) && NILP (XBUFFER (buffer)->enable_multibyte_characters))
2951 || (NILP (buffer) && NILP (buffer_defaults.enable_multibyte_characters)))
2952 val = Qnil;
2953 p->encode_coding_system = val;
2954
2955 setup_process_coding_systems (proc);
2956 p->decoding_buf = make_uninit_string (0);
2957 p->decoding_carryover = 0;
2958 p->encoding_buf = make_uninit_string (0);
2959 p->inherit_coding_system_flag
2960 = !(!NILP (tem) || NILP (buffer) || !inherit_process_coding_system);
2961
2962 Fserial_process_configure(nargs, args);
2963
2964 specpdl_ptr = specpdl + specpdl_count;
2965
2966 UNGCPRO;
2967 return proc;
2968}
2969#endif /* HAVE_SERIAL */
2970
e690ca94
KS
2971/* Create a network stream/datagram client/server process. Treated
2972 exactly like a normal process when reading and writing. Primary
d0d6b7c5
JB
2973 differences are in status display and process deletion. A network
2974 connection has no PID; you cannot signal it. All you can do is
e690ca94
KS
2975 stop/continue it and deactivate/close it via delete-process */
2976
177c0ea7
JB
2977DEFUN ("make-network-process", Fmake_network_process, Smake_network_process,
2978 0, MANY, 0,
e690ca94
KS
2979 doc: /* Create and return a network server or client process.
2980
fa9d4315 2981In Emacs, network connections are represented by process objects, so
e690ca94
KS
2982input and output work as for subprocesses and `delete-process' closes
2983a network connection. However, a network process has no process id,
d22426e1 2984it cannot be signaled, and the status codes are different from normal
e690ca94
KS
2985processes.
2986
2987Arguments are specified as keyword/argument pairs. The following
2988arguments are defined:
2989
2990:name NAME -- NAME is name for process. It is modified if necessary
2991to make it unique.
2992
2993:buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
2994with the process. Process output goes at end of that buffer, unless
2995you specify an output stream or filter function to handle the output.
2996BUFFER may be also nil, meaning that this process is not associated
2997with any buffer.
2998
2999:host HOST -- HOST is name of the host to connect to, or its IP
3000address. The symbol `local' specifies the local host. If specified
3001for a server process, it must be a valid name or address for the local
3002host, and only clients connecting to that address will be accepted.
3003
3004:service SERVICE -- SERVICE is name of the service desired, or an
3005integer specifying a port number to connect to. If SERVICE is t,
eba25e7c
GM
3006a random port number is selected for the server. (If Emacs was
3007compiled with getaddrinfo, a port number can also be specified as a
3008string, e.g. "80", as well as an integer. This is not portable.)
e690ca94 3009
9057ff80
KS
3010:type TYPE -- TYPE is the type of connection. The default (nil) is a
3011stream type connection, `datagram' creates a datagram type connection.
3012
e690ca94 3013:family FAMILY -- FAMILY is the address (and protocol) family for the
e1652a86
KS
3014service specified by HOST and SERVICE. The default (nil) is to use
3015whatever address family (IPv4 or IPv6) that is defined for the host
3016and port number specified by HOST and SERVICE. Other address families
3017supported are:
e690ca94 3018 local -- for a local (i.e. UNIX) address specified by SERVICE.
e1652a86
KS
3019 ipv4 -- use IPv4 address family only.
3020 ipv6 -- use IPv6 address family only.
e690ca94
KS
3021
3022:local ADDRESS -- ADDRESS is the local address used for the connection.
3023This parameter is ignored when opening a client process. When specified
3024for a server process, the FAMILY, HOST and SERVICE args are ignored.
3025
3026:remote ADDRESS -- ADDRESS is the remote partner's address for the
3027connection. This parameter is ignored when opening a stream server
3028process. For a datagram server process, it specifies the initial
3029setting of the remote datagram address. When specified for a client
3030process, the FAMILY, HOST, and SERVICE args are ignored.
3031
3032The format of ADDRESS depends on the address family:
3033- An IPv4 address is represented as an vector of integers [A B C D P]
3034corresponding to numeric IP address A.B.C.D and port number P.
3035- A local address is represented as a string with the address in the
3036local address space.
3037- An "unsupported family" address is represented by a cons (F . AV)
3038where F is the family number and AV is a vector containing the socket
3039address data with one element per address data byte. Do not rely on
3040this format in portable code, as it may depend on implementation
3041defined constants, data sizes, and data structure alignment.
3042
6b61353c
KH
3043:coding CODING -- If CODING is a symbol, it specifies the coding
3044system used for both reading and writing for this process. If CODING
3045is a cons (DECODING . ENCODING), DECODING is used for reading, and
3046ENCODING is used for writing.
e690ca94
KS
3047
3048:nowait BOOL -- If BOOL is non-nil for a stream type client process,
3049return without waiting for the connection to complete; instead, the
3050sentinel function will be called with second arg matching "open" (if
3051successful) or "failed" when the connect completes. Default is to use
3052a blocking connect (i.e. wait) for stream type connections.
3053
3054:noquery BOOL -- Query the user unless BOOL is non-nil, and process is
f3d9532b 3055running when Emacs is exited.
e690ca94
KS
3056
3057:stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
3058In the stopped state, a server process does not accept new
3059connections, and a client process does not handle incoming traffic.
3060The stopped state is cleared by `continue-process' and set by
3061`stop-process'.
3062
3063:filter FILTER -- Install FILTER as the process filter.
3064
7392e23c
KS
3065:filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
3066process filter are multibyte, otherwise they are unibyte.
e0f24100 3067If this keyword is not specified, the strings are multibyte if
03f04413
KH
3068`default-enable-multibyte-characters' is non-nil.
3069
e690ca94
KS
3070:sentinel SENTINEL -- Install SENTINEL as the process sentinel.
3071
3072:log LOG -- Install LOG as the server process log function. This
991234f0 3073function is called when the server accepts a network connection from a
e690ca94
KS
3074client. The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
3075is the server process, CLIENT is the new process for the connection,
3076and MESSAGE is a string.
3077
faa7db08 3078:plist PLIST -- Install PLIST as the new process' initial plist.
177c0ea7 3079
6b61353c 3080:server QLEN -- if QLEN is non-nil, create a server process for the
e690ca94 3081specified FAMILY, SERVICE, and connection type (stream or datagram).
6b61353c
KH
3082If QLEN is an integer, it is used as the max. length of the server's
3083pending connection queue (also known as the backlog); the default
3084queue length is 5. Default is to create a client process.
3085
3086The following network options can be specified for this connection:
3087
3088:broadcast BOOL -- Allow send and receive of datagram broadcasts.
3089:dontroute BOOL -- Only send to directly connected hosts.
3090:keepalive BOOL -- Send keep-alive messages on network stream.
3091:linger BOOL or TIMEOUT -- Send queued messages before closing.
3092:oobinline BOOL -- Place out-of-band data in receive data stream.
3093:priority INT -- Set protocol defined priority for sent packets.
3094:reuseaddr BOOL -- Allow reusing a recently used local address
3095 (this is allowed by default for a server process).
3096:bindtodevice NAME -- bind to interface NAME. Using this may require
3097 special privileges on some systems.
3098
3099Consult the relevant system programmer's manual pages for more
3100information on using these options.
3101
3102
3103A server process will listen for and accept connections from clients.
3104When a client connection is accepted, a new network process is created
3105for the connection with the following parameters:
e690ca94 3106
e690ca94
KS
3107- The client's process name is constructed by concatenating the server
3108process' NAME and a client identification string.
3109- If the FILTER argument is non-nil, the client process will not get a
3110separate process buffer; otherwise, the client's process buffer is a newly
3111created buffer named after the server process' BUFFER name or process
177c0ea7 3112NAME concatenated with the client identification string.
e690ca94
KS
3113- The connection type and the process filter and sentinel parameters are
3114inherited from the server process' TYPE, FILTER and SENTINEL.
3115- The client process' contact info is set according to the client's
3116addressing information (typically an IP address and a port number).
faa7db08 3117- The client process' plist is initialized from the server's plist.
e690ca94
KS
3118
3119Notice that the FILTER and SENTINEL args are never used directly by
3120the server process. Also, the BUFFER argument is not used directly by
9e9a5792
KS
3121the server process, but via the optional :log function, accepted (and
3122failed) connections may be logged in the server process' buffer.
e690ca94 3123
365aee82
KS
3124The original argument list, modified with the actual connection
3125information, is available via the `process-contact' function.
365aee82 3126
fa9d4315 3127usage: (make-network-process &rest ARGS) */)
e690ca94
KS
3128 (nargs, args)
3129 int nargs;
3130 Lisp_Object *args;
d0d6b7c5
JB
3131{
3132 Lisp_Object proc;
e690ca94
KS
3133 Lisp_Object contact;
3134 struct Lisp_Process *p;
70dbdb36 3135#ifdef HAVE_GETADDRINFO
e690ca94 3136 struct addrinfo ai, *res, *lres;
e1652a86
KS
3137 struct addrinfo hints;
3138 char *portstring, portbuf[128];
70dbdb36 3139#else /* HAVE_GETADDRINFO */
dd2a17ab
KS
3140 struct _emacs_addrinfo
3141 {
3142 int ai_family;
3143 int ai_socktype;
3144 int ai_protocol;
3145 int ai_addrlen;
3146 struct sockaddr *ai_addr;
3147 struct _emacs_addrinfo *ai_next;
3148 } ai, *res, *lres;
418b48fd 3149#endif /* HAVE_GETADDRINFO */
e690ca94
KS
3150 struct sockaddr_in address_in;
3151#ifdef HAVE_LOCAL_SOCKETS
3152 struct sockaddr_un address_un;
3153#endif
3154 int port;
dd2a17ab
KS
3155 int ret = 0;
3156 int xerrno = 0;
418b48fd 3157 int s = -1, outch, inch;
e690ca94 3158 struct gcpro gcpro1;
aed13378 3159 int count = SPECPDL_INDEX ();
5684cd6e 3160 int count1;
e690ca94
KS
3161 Lisp_Object QCaddress; /* one of QClocal or QCremote */
3162 Lisp_Object tem;
3163 Lisp_Object name, buffer, host, service, address;
3164 Lisp_Object filter, sentinel;
3165 int is_non_blocking_client = 0;
6b61353c 3166 int is_server = 0, backlog = 5;
9057ff80 3167 int socktype;
e690ca94
KS
3168 int family = -1;
3169
3170 if (nargs == 0)
3171 return Qnil;
dd2a17ab 3172
e690ca94
KS
3173 /* Save arguments for process-contact and clone-process. */
3174 contact = Flist (nargs, args);
3175 GCPRO1 (contact);
3176
bff3ed0a
RS
3177#ifdef WINDOWSNT
3178 /* Ensure socket support is loaded if available. */
3179 init_winsock (TRUE);
3180#endif
3181
9057ff80
KS
3182 /* :type TYPE (nil: stream, datagram */
3183 tem = Fplist_get (contact, QCtype);
3184 if (NILP (tem))
3185 socktype = SOCK_STREAM;
3186#ifdef DATAGRAM_SOCKETS
3187 else if (EQ (tem, Qdatagram))
3188 socktype = SOCK_DGRAM;
e690ca94 3189#endif
9057ff80
KS
3190 else
3191 error ("Unsupported connection type");
e690ca94
KS
3192
3193 /* :server BOOL */
3194 tem = Fplist_get (contact, QCserver);
3195 if (!NILP (tem))
3196 {
75728599
JR
3197 /* Don't support network sockets when non-blocking mode is
3198 not available, since a blocked Emacs is not useful. */
132d0475 3199#if !defined(O_NONBLOCK) && !defined(O_NDELAY)
e690ca94
KS
3200 error ("Network servers not supported");
3201#else
3202 is_server = 1;
6b61353c
KH
3203 if (INTEGERP (tem))
3204 backlog = XINT (tem);
e690ca94
KS
3205#endif
3206 }
3207
3208 /* Make QCaddress an alias for :local (server) or :remote (client). */
3209 QCaddress = is_server ? QClocal : QCremote;
3210
d7e344cd 3211 /* :nowait BOOL */
e690ca94
KS
3212 if (!is_server && socktype == SOCK_STREAM
3213 && (tem = Fplist_get (contact, QCnowait), !NILP (tem)))
3214 {
3215#ifndef NON_BLOCKING_CONNECT
3216 error ("Non-blocking connect not supported");
3217#else
3218 is_non_blocking_client = 1;
3219#endif
3220 }
3221
3222 name = Fplist_get (contact, QCname);
3223 buffer = Fplist_get (contact, QCbuffer);
3224 filter = Fplist_get (contact, QCfilter);
3225 sentinel = Fplist_get (contact, QCsentinel);
3226
b7826503 3227 CHECK_STRING (name);
e690ca94 3228
e690ca94
KS
3229 /* Initialize addrinfo structure in case we don't use getaddrinfo. */
3230 ai.ai_socktype = socktype;
3231 ai.ai_protocol = 0;
3232 ai.ai_next = NULL;
3233 res = &ai;
a319f7c1 3234
e690ca94
KS
3235 /* :local ADDRESS or :remote ADDRESS */
3236 address = Fplist_get (contact, QCaddress);
3237 if (!NILP (address))
a319f7c1 3238 {
e690ca94
KS
3239 host = service = Qnil;
3240
3241 if (!(ai.ai_addrlen = get_lisp_to_sockaddr_size (address, &family)))
3242 error ("Malformed :address");
3243 ai.ai_family = family;
3244 ai.ai_addr = alloca (ai.ai_addrlen);
3245 conv_lisp_to_sockaddr (family, address, ai.ai_addr, ai.ai_addrlen);
3246 goto open_socket;
a319f7c1 3247 }
e690ca94
KS
3248
3249 /* :family FAMILY -- nil (for Inet), local, or integer. */
3250 tem = Fplist_get (contact, QCfamily);
e1652a86 3251 if (NILP (tem))
a319f7c1 3252 {
87b6ca8d 3253#if defined(HAVE_GETADDRINFO) && defined(AF_INET6)
e1652a86
KS
3254 family = AF_UNSPEC;
3255#else
3256 family = AF_INET;
e690ca94 3257#endif
a319f7c1 3258 }
e1652a86
KS
3259#ifdef HAVE_LOCAL_SOCKETS
3260 else if (EQ (tem, Qlocal))
3261 family = AF_LOCAL;
3262#endif
3263#ifdef AF_INET6
3264 else if (EQ (tem, Qipv6))
3265 family = AF_INET6;
3266#endif
3267 else if (EQ (tem, Qipv4))
3268 family = AF_INET;
3269 else if (INTEGERP (tem))
3270 family = XINT (tem);
3271 else
e690ca94 3272 error ("Unknown address family");
e1652a86 3273
e690ca94
KS
3274 ai.ai_family = family;
3275
3276 /* :service SERVICE -- string, integer (port number), or t (random port). */
3277 service = Fplist_get (contact, QCservice);
3278
3279#ifdef HAVE_LOCAL_SOCKETS
3280 if (family == AF_LOCAL)
d0d6b7c5 3281 {
e690ca94
KS
3282 /* Host is not used. */
3283 host = Qnil;
b7826503 3284 CHECK_STRING (service);
e690ca94
KS
3285 bzero (&address_un, sizeof address_un);
3286 address_un.sun_family = AF_LOCAL;
d5db4077 3287 strncpy (address_un.sun_path, SDATA (service), sizeof address_un.sun_path);
e690ca94
KS
3288 ai.ai_addr = (struct sockaddr *) &address_un;
3289 ai.ai_addrlen = sizeof address_un;
3290 goto open_socket;
d0d6b7c5 3291 }
e690ca94 3292#endif
a319f7c1 3293
e690ca94
KS
3294 /* :host HOST -- hostname, ip address, or 'local for localhost. */
3295 host = Fplist_get (contact, QChost);
3296 if (!NILP (host))
3297 {
3298 if (EQ (host, Qlocal))
3299 host = build_string ("localhost");
3300 CHECK_STRING (host);
3301 }
d0d6b7c5 3302
798b64bb
KH
3303 /* Slow down polling to every ten seconds.
3304 Some kernels have a bug which causes retrying connect to fail
3305 after a connect. Polling can interfere with gethostbyname too. */
3306#ifdef POLL_FOR_INPUT
e690ca94
KS
3307 if (socktype == SOCK_STREAM)
3308 {
3309 record_unwind_protect (unwind_stop_other_atimers, Qnil);
3310 bind_polling_period (10);
3311 }
798b64bb
KH
3312#endif
3313
a319f7c1 3314#ifdef HAVE_GETADDRINFO
e690ca94
KS
3315 /* If we have a host, use getaddrinfo to resolve both host and service.
3316 Otherwise, use getservbyname to lookup the service. */
3317 if (!NILP (host))
3318 {
3319
3320 /* SERVICE can either be a string or int.
3321 Convert to a C string for later use by getaddrinfo. */
3322 if (EQ (service, Qt))
3323 portstring = "0";
3324 else if (INTEGERP (service))
3325 {
3326 sprintf (portbuf, "%ld", (long) XINT (service));
3327 portstring = portbuf;
3328 }
3329 else
3330 {
3331 CHECK_STRING (service);
d5db4077 3332 portstring = SDATA (service);
e690ca94
KS
3333 }
3334
3335 immediate_quit = 1;
3336 QUIT;
3337 memset (&hints, 0, sizeof (hints));
3338 hints.ai_flags = 0;
e1652a86 3339 hints.ai_family = family;
e690ca94
KS
3340 hints.ai_socktype = socktype;
3341 hints.ai_protocol = 0;
08116b48
CY
3342
3343#ifdef HAVE_RES_INIT
3344 res_init ();
3345#endif
3346
d5db4077 3347 ret = getaddrinfo (SDATA (host), portstring, &hints, &res);
e690ca94 3348 if (ret)
f6270f62 3349#ifdef HAVE_GAI_STRERROR
d5db4077 3350 error ("%s/%s %s", SDATA (host), portstring, gai_strerror(ret));
f6270f62 3351#else
63136da6 3352 error ("%s/%s getaddrinfo error %d", SDATA (host), portstring, ret);
f6270f62 3353#endif
e690ca94 3354 immediate_quit = 0;
a319f7c1 3355
e690ca94
KS
3356 goto open_socket;
3357 }
3358#endif /* HAVE_GETADDRINFO */
a319f7c1 3359
e690ca94
KS
3360 /* We end up here if getaddrinfo is not defined, or in case no hostname
3361 has been specified (e.g. for a local server process). */
3362
3363 if (EQ (service, Qt))
3364 port = 0;
3365 else if (INTEGERP (service))
3366 port = htons ((unsigned short) XINT (service));
3367 else
616da37c 3368 {
e690ca94
KS
3369 struct servent *svc_info;
3370 CHECK_STRING (service);
177c0ea7 3371 svc_info = getservbyname (SDATA (service),
e690ca94
KS
3372 (socktype == SOCK_DGRAM ? "udp" : "tcp"));
3373 if (svc_info == 0)
d5db4077 3374 error ("Unknown service: %s", SDATA (service));
e690ca94
KS
3375 port = svc_info->s_port;
3376 }
3377
3378 bzero (&address_in, sizeof address_in);
3379 address_in.sin_family = family;
3380 address_in.sin_addr.s_addr = INADDR_ANY;
3381 address_in.sin_port = port;
3382
3383#ifndef HAVE_GETADDRINFO
3384 if (!NILP (host))
3385 {
3386 struct hostent *host_info_ptr;
3387
3388 /* gethostbyname may fail with TRY_AGAIN, but we don't honour that,
f3d9532b 3389 as it may `hang' Emacs for a very long time. */
5d6c2aa3
RS
3390 immediate_quit = 1;
3391 QUIT;
08116b48
CY
3392
3393#ifdef HAVE_RES_INIT
3394 res_init ();
3395#endif
3396
d5db4077 3397 host_info_ptr = gethostbyname (SDATA (host));
5d6c2aa3 3398 immediate_quit = 0;
177c0ea7 3399
e690ca94
KS
3400 if (host_info_ptr)
3401 {
3402 bcopy (host_info_ptr->h_addr, (char *) &address_in.sin_addr,
3403 host_info_ptr->h_length);
3404 family = host_info_ptr->h_addrtype;
3405 address_in.sin_family = family;
3406 }
3407 else
3408 /* Attempt to interpret host as numeric inet address */
3409 {
4624371d 3410 unsigned long numeric_addr;
d5db4077 3411 numeric_addr = inet_addr ((char *) SDATA (host));
4624371d 3412 if (numeric_addr == -1)
d5db4077 3413 error ("Unknown host \"%s\"", SDATA (host));
d0d6b7c5 3414
e690ca94
KS
3415 bcopy ((char *)&numeric_addr, (char *) &address_in.sin_addr,
3416 sizeof (address_in.sin_addr));
3417 }
d0d6b7c5 3418
e690ca94 3419 }
dd2a17ab 3420#endif /* not HAVE_GETADDRINFO */
d0d6b7c5 3421
e690ca94
KS
3422 ai.ai_family = family;
3423 ai.ai_addr = (struct sockaddr *) &address_in;
3424 ai.ai_addrlen = sizeof address_in;
3425
3426 open_socket:
3427
dd2a17ab 3428 /* Do this in case we never enter the for-loop below. */
aed13378 3429 count1 = SPECPDL_INDEX ();
dd2a17ab 3430 s = -1;
457a9bee 3431
dd2a17ab
KS
3432 for (lres = res; lres; lres = lres->ai_next)
3433 {
6b61353c
KH
3434 int optn, optbits;
3435
4e9dd03b
KS
3436 retry_connect:
3437
dd2a17ab
KS
3438 s = socket (lres->ai_family, lres->ai_socktype, lres->ai_protocol);
3439 if (s < 0)
3440 {
3441 xerrno = errno;
3442 continue;
3443 }
0f2ee0c1 3444
e690ca94
KS
3445#ifdef DATAGRAM_SOCKETS
3446 if (!is_server && socktype == SOCK_DGRAM)
3447 break;
3448#endif /* DATAGRAM_SOCKETS */
3449
dd2a17ab 3450#ifdef NON_BLOCKING_CONNECT
e690ca94 3451 if (is_non_blocking_client)
dd2a17ab
KS
3452 {
3453#ifdef O_NONBLOCK
3454 ret = fcntl (s, F_SETFL, O_NONBLOCK);
3455#else
3456 ret = fcntl (s, F_SETFL, O_NDELAY);
3457#endif
3458 if (ret < 0)
3459 {
3460 xerrno = errno;
3461 emacs_close (s);
3462 s = -1;
3463 continue;
3464 }
3465 }
3466#endif
177c0ea7 3467
dd2a17ab 3468 /* Make us close S if quit. */
dd2a17ab
KS
3469 record_unwind_protect (close_file_unwind, make_number (s));
3470
6b61353c
KH
3471 /* Parse network options in the arg list.
3472 We simply ignore anything which isn't a known option (including other keywords).
63136da6 3473 An error is signaled if setting a known option fails. */
6b61353c
KH
3474 for (optn = optbits = 0; optn < nargs-1; optn += 2)
3475 optbits |= set_socket_option (s, args[optn], args[optn+1]);
3476
e690ca94
KS
3477 if (is_server)
3478 {
3479 /* Configure as a server socket. */
6b61353c
KH
3480
3481 /* SO_REUSEADDR = 1 is default for server sockets; must specify
3482 explicit :reuseaddr key to override this. */
e690ca94
KS
3483#ifdef HAVE_LOCAL_SOCKETS
3484 if (family != AF_LOCAL)
3485#endif
6b61353c
KH
3486 if (!(optbits & (1 << OPIX_REUSEADDR)))
3487 {
3488 int optval = 1;
3489 if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval))
3490 report_file_error ("Cannot set reuse option on server socket", Qnil);
3491 }
177c0ea7 3492
e690ca94
KS
3493 if (bind (s, lres->ai_addr, lres->ai_addrlen))
3494 report_file_error ("Cannot bind server socket", Qnil);
3495
3496#ifdef HAVE_GETSOCKNAME
3497 if (EQ (service, Qt))
3498 {
3499 struct sockaddr_in sa1;
3500 int len1 = sizeof (sa1);
3501 if (getsockname (s, (struct sockaddr *)&sa1, &len1) == 0)
3502 {
3503 ((struct sockaddr_in *)(lres->ai_addr))->sin_port = sa1.sin_port;
21bd170d 3504 service = make_number (ntohs (sa1.sin_port));
e690ca94
KS
3505 contact = Fplist_put (contact, QCservice, service);
3506 }
3507 }
3508#endif
3509
6b61353c 3510 if (socktype == SOCK_STREAM && listen (s, backlog))
e690ca94
KS
3511 report_file_error ("Cannot listen on server socket", Qnil);
3512
3513 break;
3514 }
3515
dd2a17ab
KS
3516 immediate_quit = 1;
3517 QUIT;
3518
3519 /* This turns off all alarm-based interrupts; the
3520 bind_polling_period call above doesn't always turn all the
3521 short-interval ones off, especially if interrupt_input is
3522 set.
3523
3524 It'd be nice to be able to control the connect timeout
177c0ea7 3525 though. Would non-blocking connect calls be portable?
dd2a17ab
KS
3526
3527 This used to be conditioned by HAVE_GETADDRINFO. Why? */
3528
f40f9848 3529 turn_on_atimers (0);
dd2a17ab
KS
3530
3531 ret = connect (s, lres->ai_addr, lres->ai_addrlen);
3532 xerrno = errno;
3533
f40f9848 3534 turn_on_atimers (1);
dd2a17ab
KS
3535
3536 if (ret == 0 || xerrno == EISCONN)
3537 {
dd2a17ab
KS
3538 /* The unwind-protect will be discarded afterwards.
3539 Likewise for immediate_quit. */
3540 break;
3541 }
3542
3543#ifdef NON_BLOCKING_CONNECT
3544#ifdef EINPROGRESS
e690ca94 3545 if (is_non_blocking_client && xerrno == EINPROGRESS)
dd2a17ab
KS
3546 break;
3547#else
3548#ifdef EWOULDBLOCK
e690ca94 3549 if (is_non_blocking_client && xerrno == EWOULDBLOCK)
dd2a17ab
KS
3550 break;
3551#endif
3552#endif
3553#endif
e333e864 3554
0f2ee0c1
RS
3555 immediate_quit = 0;
3556
dd2a17ab 3557 /* Discard the unwind protect closing S. */
5684cd6e 3558 specpdl_ptr = specpdl + count1;
68c45bf0 3559 emacs_close (s);
dd2a17ab 3560 s = -1;
4e9dd03b
KS
3561
3562 if (xerrno == EINTR)
3563 goto retry_connect;
dd2a17ab 3564 }
457a9bee 3565
e690ca94
KS
3566 if (s >= 0)
3567 {
3568#ifdef DATAGRAM_SOCKETS
3569 if (socktype == SOCK_DGRAM)
3570 {
3571 if (datagram_address[s].sa)
3572 abort ();
3573 datagram_address[s].sa = (struct sockaddr *) xmalloc (lres->ai_addrlen);
3574 datagram_address[s].len = lres->ai_addrlen;
3575 if (is_server)
3576 {
3577 Lisp_Object remote;
3578 bzero (datagram_address[s].sa, lres->ai_addrlen);
3579 if (remote = Fplist_get (contact, QCremote), !NILP (remote))
3580 {
3581 int rfamily, rlen;
3582 rlen = get_lisp_to_sockaddr_size (remote, &rfamily);
3583 if (rfamily == lres->ai_family && rlen == lres->ai_addrlen)
3584 conv_lisp_to_sockaddr (rfamily, remote,
3585 datagram_address[s].sa, rlen);
3586 }
3587 }
3588 else
3589 bcopy (lres->ai_addr, datagram_address[s].sa, lres->ai_addrlen);
3590 }
3591#endif
177c0ea7 3592 contact = Fplist_put (contact, QCaddress,
e690ca94 3593 conv_sockaddr_to_lisp (lres->ai_addr, lres->ai_addrlen));
2185db04
KS
3594#ifdef HAVE_GETSOCKNAME
3595 if (!is_server)
3596 {
3597 struct sockaddr_in sa1;
3598 int len1 = sizeof (sa1);
3599 if (getsockname (s, (struct sockaddr *)&sa1, &len1) == 0)
3600 contact = Fplist_put (contact, QClocal,
3601 conv_sockaddr_to_lisp (&sa1, len1));
3602 }
3603#endif
e690ca94
KS
3604 }
3605
54b3b62f
YM
3606 immediate_quit = 0;
3607
dd2a17ab 3608#ifdef HAVE_GETADDRINFO
e690ca94 3609 if (res != &ai)
54b3b62f
YM
3610 {
3611 BLOCK_INPUT;
3612 freeaddrinfo (res);
3613 UNBLOCK_INPUT;
3614 }
dd2a17ab
KS
3615#endif
3616
e690ca94
KS
3617 /* Discard the unwind protect for closing S, if any. */
3618 specpdl_ptr = specpdl + count1;
3619
3620 /* Unwind bind_polling_period and request_sigio. */
3621 unbind_to (count, Qnil);
3622
dd2a17ab
KS
3623 if (s < 0)
3624 {
dd2a17ab
KS
3625 /* If non-blocking got this far - and failed - assume non-blocking is
3626 not supported after all. This is probably a wrong assumption, but
e690ca94
KS
3627 the normal blocking calls to open-network-stream handles this error
3628 better. */
3629 if (is_non_blocking_client)
dd2a17ab 3630 return Qnil;
dd2a17ab 3631
d0d6b7c5 3632 errno = xerrno;
e690ca94
KS
3633 if (is_server)
3634 report_file_error ("make server process failed", contact);
3635 else
3636 report_file_error ("make client process failed", contact);
d0d6b7c5 3637 }
44ade2e9 3638
d0d6b7c5 3639 inch = s;
59f23005 3640 outch = s;
d0d6b7c5
JB
3641
3642 if (!NILP (buffer))
3643 buffer = Fget_buffer_create (buffer);
3644 proc = make_process (name);
3645
3646 chan_process[inch] = proc;
3647
3648#ifdef O_NONBLOCK
3649 fcntl (inch, F_SETFL, O_NONBLOCK);
3650#else
3651#ifdef O_NDELAY
3652 fcntl (inch, F_SETFL, O_NDELAY);
3653#endif
3654#endif
3655
e690ca94
KS
3656 p = XPROCESS (proc);
3657
3658 p->childp = contact;
faa7db08 3659 p->plist = Fcopy_sequence (Fplist_get (contact, QCplist));
d888760c 3660 p->type = Qnetwork;
177c0ea7 3661
e690ca94
KS
3662 p->buffer = buffer;
3663 p->sentinel = sentinel;
3664 p->filter = filter;
3665 p->log = Fplist_get (contact, QClog);
3666 if (tem = Fplist_get (contact, QCnoquery), !NILP (tem))
60f0fb11 3667 p->kill_without_query = 1;
e690ca94
KS
3668 if ((tem = Fplist_get (contact, QCstop), !NILP (tem)))
3669 p->command = Qt;
6bfd98e7 3670 p->pid = 0;
60f0fb11
SM
3671 p->infd = inch;
3672 p->outfd = outch;
e690ca94
KS
3673 if (is_server && socktype == SOCK_STREAM)
3674 p->status = Qlisten;
dd2a17ab 3675
f523e0c3
SM
3676 /* Make the process marker point into the process buffer (if any). */
3677 if (BUFFERP (buffer))
3678 set_marker_both (p->mark, buffer,
3679 BUF_ZV (XBUFFER (buffer)),
3680 BUF_ZV_BYTE (XBUFFER (buffer)));
3681
dd2a17ab 3682#ifdef NON_BLOCKING_CONNECT
e690ca94 3683 if (is_non_blocking_client)
dd2a17ab
KS
3684 {
3685 /* We may get here if connect did succeed immediately. However,
3686 in that case, we still need to signal this like a non-blocking
3687 connection. */
e690ca94 3688 p->status = Qconnect;
dd2a17ab
KS
3689 if (!FD_ISSET (inch, &connect_wait_mask))
3690 {
3691 FD_SET (inch, &connect_wait_mask);
3692 num_pending_connects++;
3693 }
3694 }
3695 else
3696#endif
e690ca94
KS
3697 /* A server may have a client filter setting of Qt, but it must
3698 still listen for incoming connects unless it is stopped. */
3699 if ((!EQ (p->filter, Qt) && !EQ (p->command, Qt))
3700 || (EQ (p->status, Qlisten) && NILP (p->command)))
dd2a17ab
KS
3701 {
3702 FD_SET (inch, &input_wait_mask);
3703 FD_SET (inch, &non_keyboard_wait_mask);
3704 }
3705
7d0e672e
RS
3706 if (inch > max_process_desc)
3707 max_process_desc = inch;
d0d6b7c5 3708
e690ca94
KS
3709 tem = Fplist_member (contact, QCcoding);
3710 if (!NILP (tem) && (!CONSP (tem) || !CONSP (XCDR (tem))))
3711 tem = Qnil; /* No error message (too late!). */
3712
67918941
RS
3713 {
3714 /* Setup coding systems for communicating with the network stream. */
3715 struct gcpro gcpro1;
3716 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
3717 Lisp_Object coding_systems = Qt;
3718 Lisp_Object args[5], val;
3719
e690ca94 3720 if (!NILP (tem))
6b61353c
KH
3721 {
3722 val = XCAR (XCDR (tem));
3723 if (CONSP (val))
3724 val = XCAR (val);
3725 }
e690ca94 3726 else if (!NILP (Vcoding_system_for_read))
67918941 3727 val = Vcoding_system_for_read;
41d03b9a
GM
3728 else if ((!NILP (buffer) && NILP (XBUFFER (buffer)->enable_multibyte_characters))
3729 || (NILP (buffer) && NILP (buffer_defaults.enable_multibyte_characters)))
67918941
RS
3730 /* We dare not decode end-of-line format by setting VAL to
3731 Qraw_text, because the existing Emacs Lisp libraries
3732 assume that they receive bare code including a sequene of
3733 CR LF. */
3734 val = Qnil;
3735 else
3736 {
991234f0
KS
3737 if (NILP (host) || NILP (service))
3738 coding_systems = Qnil;
3739 else
3740 {
3741 args[0] = Qopen_network_stream, args[1] = name,
3742 args[2] = buffer, args[3] = host, args[4] = service;
3743 GCPRO1 (proc);
3744 coding_systems = Ffind_operation_coding_system (5, args);
3745 UNGCPRO;
3746 }
67918941 3747 if (CONSP (coding_systems))
70949dac 3748 val = XCAR (coding_systems);
67918941 3749 else if (CONSP (Vdefault_process_coding_system))
70949dac 3750 val = XCAR (Vdefault_process_coding_system);
67918941
RS
3751 else
3752 val = Qnil;
3753 }
e690ca94 3754 p->decode_coding_system = val;
0fa1789e 3755
e690ca94 3756 if (!NILP (tem))
6b61353c
KH
3757 {
3758 val = XCAR (XCDR (tem));
3759 if (CONSP (val))
3760 val = XCDR (val);
3761 }
e690ca94 3762 else if (!NILP (Vcoding_system_for_write))
67918941
RS
3763 val = Vcoding_system_for_write;
3764 else if (NILP (current_buffer->enable_multibyte_characters))
3765 val = Qnil;
3766 else
3767 {
3768 if (EQ (coding_systems, Qt))
3769 {
991234f0
KS
3770 if (NILP (host) || NILP (service))
3771 coding_systems = Qnil;
3772 else
3773 {
3774 args[0] = Qopen_network_stream, args[1] = name,
3775 args[2] = buffer, args[3] = host, args[4] = service;
3776 GCPRO1 (proc);
3777 coding_systems = Ffind_operation_coding_system (5, args);
3778 UNGCPRO;
3779 }
67918941
RS
3780 }
3781 if (CONSP (coding_systems))
70949dac 3782 val = XCDR (coding_systems);
67918941 3783 else if (CONSP (Vdefault_process_coding_system))
70949dac 3784 val = XCDR (Vdefault_process_coding_system);
67918941
RS
3785 else
3786 val = Qnil;
3787 }
e690ca94 3788 p->encode_coding_system = val;
67918941 3789 }
03f04413 3790 setup_process_coding_systems (proc);
0fa1789e 3791
e690ca94 3792 p->decoding_buf = make_uninit_string (0);
60f0fb11 3793 p->decoding_carryover = 0;
e690ca94 3794 p->encoding_buf = make_uninit_string (0);
0fa1789e 3795
e690ca94 3796 p->inherit_coding_system_flag
e6fca843 3797 = !(!NILP (tem) || NILP (buffer) || !inherit_process_coding_system);
52a1b894 3798
d0d6b7c5
JB
3799 UNGCPRO;
3800 return proc;
3801}
3802#endif /* HAVE_SOCKETS */
3803
6b61353c
KH
3804\f
3805#if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
3806
3807#ifdef SIOCGIFCONF
3808DEFUN ("network-interface-list", Fnetwork_interface_list, Snetwork_interface_list, 0, 0, 0,
3809 doc: /* Return an alist of all network interfaces and their network address.
3810Each element is a cons, the car of which is a string containing the
3811interface name, and the cdr is the network address in internal
3812format; see the description of ADDRESS in `make-network-process'. */)
3813 ()
3814{
3815 struct ifconf ifconf;
3816 struct ifreq *ifreqs = NULL;
3817 int ifaces = 0;
3818 int buf_size, s;
3819 Lisp_Object res;
3820
3821 s = socket (AF_INET, SOCK_STREAM, 0);
3822 if (s < 0)
3823 return Qnil;
3824
3825 again:
3826 ifaces += 25;
3827 buf_size = ifaces * sizeof(ifreqs[0]);
3828 ifreqs = (struct ifreq *)xrealloc(ifreqs, buf_size);
3829 if (!ifreqs)
3830 {
3831 close (s);
3832 return Qnil;
3833 }
3834
3835 ifconf.ifc_len = buf_size;
3836 ifconf.ifc_req = ifreqs;
3837 if (ioctl (s, SIOCGIFCONF, &ifconf))
3838 {
3839 close (s);
3840 return Qnil;
3841 }
3842
3843 if (ifconf.ifc_len == buf_size)
3844 goto again;
3845
3846 close (s);
3847 ifaces = ifconf.ifc_len / sizeof (ifreqs[0]);
3848
3849 res = Qnil;
3850 while (--ifaces >= 0)
3851 {
3852 struct ifreq *ifq = &ifreqs[ifaces];
3853 char namebuf[sizeof (ifq->ifr_name) + 1];
3854 if (ifq->ifr_addr.sa_family != AF_INET)
3855 continue;
3856 bcopy (ifq->ifr_name, namebuf, sizeof (ifq->ifr_name));
3857 namebuf[sizeof (ifq->ifr_name)] = 0;
3858 res = Fcons (Fcons (build_string (namebuf),
3859 conv_sockaddr_to_lisp (&ifq->ifr_addr,
3860 sizeof (struct sockaddr))),
3861 res);
3862 }
3863
3864 return res;
3865}
3866#endif /* SIOCGIFCONF */
3867
3868#if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
3869
3870struct ifflag_def {
3871 int flag_bit;
3872 char *flag_sym;
3873};
3874
3875static struct ifflag_def ifflag_table[] = {
3876#ifdef IFF_UP
3877 { IFF_UP, "up" },
3878#endif
3879#ifdef IFF_BROADCAST
3880 { IFF_BROADCAST, "broadcast" },
3881#endif
3882#ifdef IFF_DEBUG
3883 { IFF_DEBUG, "debug" },
3884#endif
3885#ifdef IFF_LOOPBACK
3886 { IFF_LOOPBACK, "loopback" },
3887#endif
3888#ifdef IFF_POINTOPOINT
3889 { IFF_POINTOPOINT, "pointopoint" },
3890#endif
3891#ifdef IFF_RUNNING
3892 { IFF_RUNNING, "running" },
3893#endif
3894#ifdef IFF_NOARP
3895 { IFF_NOARP, "noarp" },
3896#endif
3897#ifdef IFF_PROMISC
3898 { IFF_PROMISC, "promisc" },
3899#endif
3900#ifdef IFF_NOTRAILERS
3901 { IFF_NOTRAILERS, "notrailers" },
3902#endif
3903#ifdef IFF_ALLMULTI
3904 { IFF_ALLMULTI, "allmulti" },
3905#endif
3906#ifdef IFF_MASTER
3907 { IFF_MASTER, "master" },
3908#endif
3909#ifdef IFF_SLAVE
3910 { IFF_SLAVE, "slave" },
3911#endif
3912#ifdef IFF_MULTICAST
3913 { IFF_MULTICAST, "multicast" },
3914#endif
3915#ifdef IFF_PORTSEL
3916 { IFF_PORTSEL, "portsel" },
3917#endif
3918#ifdef IFF_AUTOMEDIA
3919 { IFF_AUTOMEDIA, "automedia" },
3920#endif
3921#ifdef IFF_DYNAMIC
3922 { IFF_DYNAMIC, "dynamic" },
e1652a86 3923#endif
2026418c
YM
3924#ifdef IFF_OACTIVE
3925 { IFF_OACTIVE, "oactive" }, /* OpenBSD: transmission in progress */
e1652a86
KS
3926#endif
3927#ifdef IFF_SIMPLEX
3928 { IFF_SIMPLEX, "simplex" }, /* OpenBSD: can't hear own transmissions */
3929#endif
3930#ifdef IFF_LINK0
3931 { IFF_LINK0, "link0" }, /* OpenBSD: per link layer defined bit */
3932#endif
3933#ifdef IFF_LINK1
3934 { IFF_LINK1, "link1" }, /* OpenBSD: per link layer defined bit */
3935#endif
3936#ifdef IFF_LINK2
3937 { IFF_LINK2, "link2" }, /* OpenBSD: per link layer defined bit */
6b61353c
KH
3938#endif
3939 { 0, 0 }
3940};
3941
3942DEFUN ("network-interface-info", Fnetwork_interface_info, Snetwork_interface_info, 1, 1, 0,
3943 doc: /* Return information about network interface named IFNAME.
3944The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
3945where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
3946NETMASK is the layer 3 network mask, HWADDR is the layer 2 addres, and
3947FLAGS is the current flags of the interface. */)
3948 (ifname)
3949 Lisp_Object ifname;
3950{
3951 struct ifreq rq;
3952 Lisp_Object res = Qnil;
3953 Lisp_Object elt;
3954 int s;
3955 int any = 0;
3956
3957 CHECK_STRING (ifname);
3958
3959 bzero (rq.ifr_name, sizeof rq.ifr_name);
3960 strncpy (rq.ifr_name, SDATA (ifname), sizeof (rq.ifr_name));
3961
3962 s = socket (AF_INET, SOCK_STREAM, 0);
3963 if (s < 0)
3964 return Qnil;
3965
3966 elt = Qnil;
3967#if defined(SIOCGIFFLAGS) && defined(HAVE_STRUCT_IFREQ_IFR_FLAGS)
3968 if (ioctl (s, SIOCGIFFLAGS, &rq) == 0)
3969 {
3970 int flags = rq.ifr_flags;
3971 struct ifflag_def *fp;
3972 int fnum;
3973
3974 any++;
82a80058 3975 for (fp = ifflag_table; flags != 0 && fp->flag_sym; fp++)
6b61353c
KH
3976 {
3977 if (flags & fp->flag_bit)
3978 {
3979 elt = Fcons (intern (fp->flag_sym), elt);
3980 flags -= fp->flag_bit;
3981 }
3982 }
3983 for (fnum = 0; flags && fnum < 32; fnum++)
3984 {
3985 if (flags & (1 << fnum))
3986 {
3987 elt = Fcons (make_number (fnum), elt);
3988 }
3989 }
3990 }
3991#endif
3992 res = Fcons (elt, res);
3993
3994 elt = Qnil;
3995#if defined(SIOCGIFHWADDR) && defined(HAVE_STRUCT_IFREQ_IFR_HWADDR)
3996 if (ioctl (s, SIOCGIFHWADDR, &rq) == 0)
3997 {
3998 Lisp_Object hwaddr = Fmake_vector (make_number (6), Qnil);
3999 register struct Lisp_Vector *p = XVECTOR (hwaddr);
4000 int n;
4001
4002 any++;
4003 for (n = 0; n < 6; n++)
4004 p->contents[n] = make_number (((unsigned char *)&rq.ifr_hwaddr.sa_data[0])[n]);
4005 elt = Fcons (make_number (rq.ifr_hwaddr.sa_family), hwaddr);
4006 }
4007#endif
4008 res = Fcons (elt, res);
4009
4010 elt = Qnil;
2026418c 4011#if defined(SIOCGIFNETMASK) && (defined(HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined(HAVE_STRUCT_IFREQ_IFR_ADDR))
6b61353c
KH
4012 if (ioctl (s, SIOCGIFNETMASK, &rq) == 0)
4013 {
4014 any++;
2026418c 4015#ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
6b61353c 4016 elt = conv_sockaddr_to_lisp (&rq.ifr_netmask, sizeof (rq.ifr_netmask));
2026418c
YM
4017#else
4018 elt = conv_sockaddr_to_lisp (&rq.ifr_addr, sizeof (rq.ifr_addr));
4019#endif
6b61353c
KH
4020 }
4021#endif
4022 res = Fcons (elt, res);
4023
4024 elt = Qnil;
4025#if defined(SIOCGIFBRDADDR) && defined(HAVE_STRUCT_IFREQ_IFR_BROADADDR)
4026 if (ioctl (s, SIOCGIFBRDADDR, &rq) == 0)
4027 {
4028 any++;
4029 elt = conv_sockaddr_to_lisp (&rq.ifr_broadaddr, sizeof (rq.ifr_broadaddr));
4030 }
4031#endif
4032 res = Fcons (elt, res);
4033
4034 elt = Qnil;
4035#if defined(SIOCGIFADDR) && defined(HAVE_STRUCT_IFREQ_IFR_ADDR)
4036 if (ioctl (s, SIOCGIFADDR, &rq) == 0)
4037 {
4038 any++;
4039 elt = conv_sockaddr_to_lisp (&rq.ifr_addr, sizeof (rq.ifr_addr));
4040 }
4041#endif
4042 res = Fcons (elt, res);
4043
4044 close (s);
4045
4046 return any ? res : Qnil;
4047}
4048#endif
4049#endif /* HAVE_SOCKETS */
4050
04391069
RS
4051/* Turn off input and output for process PROC. */
4052
6b53bb85 4053void
d0d6b7c5
JB
4054deactivate_process (proc)
4055 Lisp_Object proc;
4056{
4057 register int inchannel, outchannel;
4058 register struct Lisp_Process *p = XPROCESS (proc);
4059
60f0fb11
SM
4060 inchannel = p->infd;
4061 outchannel = p->outfd;
d0d6b7c5 4062
6b61353c 4063#ifdef ADAPTIVE_READ_BUFFERING
60f0fb11 4064 if (p->read_output_delay > 0)
6b61353c
KH
4065 {
4066 if (--process_output_delay_count < 0)
4067 process_output_delay_count = 0;
60f0fb11
SM
4068 p->read_output_delay = 0;
4069 p->read_output_skip = 0;
6b61353c
KH
4070 }
4071#endif
f3d9532b 4072
a9f2c884 4073 if (inchannel >= 0)
d0d6b7c5
JB
4074 {
4075 /* Beware SIGCHLD hereabouts. */
4076 flush_pending_output (inchannel);
68c45bf0 4077 emacs_close (inchannel);
a9f2c884 4078 if (outchannel >= 0 && outchannel != inchannel)
68c45bf0 4079 emacs_close (outchannel);
d0d6b7c5 4080
60f0fb11
SM
4081 p->infd = -1;
4082 p->outfd = -1;
e690ca94
KS
4083#ifdef DATAGRAM_SOCKETS
4084 if (DATAGRAM_CHAN_P (inchannel))
4085 {
4086 xfree (datagram_address[inchannel].sa);
4087 datagram_address[inchannel].sa = 0;
4088 datagram_address[inchannel].len = 0;
4089 }
4090#endif
d0d6b7c5
JB
4091 chan_process[inchannel] = Qnil;
4092 FD_CLR (inchannel, &input_wait_mask);
a69281ff 4093 FD_CLR (inchannel, &non_keyboard_wait_mask);
bad49fc7 4094#ifdef NON_BLOCKING_CONNECT
dd2a17ab
KS
4095 if (FD_ISSET (inchannel, &connect_wait_mask))
4096 {
4097 FD_CLR (inchannel, &connect_wait_mask);
4098 if (--num_pending_connects < 0)
4099 abort ();
4100 }
bad49fc7 4101#endif
7d0e672e
RS
4102 if (inchannel == max_process_desc)
4103 {
4104 int i;
4105 /* We just closed the highest-numbered process input descriptor,
4106 so recompute the highest-numbered one now. */
4107 max_process_desc = 0;
4108 for (i = 0; i < MAXDESC; i++)
4109 if (!NILP (chan_process[i]))
4110 max_process_desc = i;
4111 }
d0d6b7c5
JB
4112 }
4113}
4114
4115/* Close all descriptors currently in use for communication
4116 with subprocess. This is used in a newly-forked subprocess
4117 to get rid of irrelevant descriptors. */
4118
6b53bb85 4119void
d0d6b7c5
JB
4120close_process_descs ()
4121{
e98d950b 4122#ifndef WINDOWSNT
d0d6b7c5
JB
4123 int i;
4124 for (i = 0; i < MAXDESC; i++)
4125 {
4126 Lisp_Object process;
4127 process = chan_process[i];
4128 if (!NILP (process))
4129 {
60f0fb11
SM
4130 int in = XPROCESS (process)->infd;
4131 int out = XPROCESS (process)->outfd;
a9f2c884 4132 if (in >= 0)
68c45bf0 4133 emacs_close (in);
a9f2c884 4134 if (out >= 0 && in != out)
68c45bf0 4135 emacs_close (out);
d0d6b7c5
JB
4136 }
4137 }
e98d950b 4138#endif
d0d6b7c5
JB
4139}
4140\f
4141DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output,
107ed38d 4142 0, 4, 0,
fdb82f93
PJ
4143 doc: /* Allow any pending output from subprocesses to be read by Emacs.
4144It is read into the process' buffers or given to their filter functions.
4145Non-nil arg PROCESS means do not return until some output has been received
4146from PROCESS.
5088da42 4147
2a7bb882
MR
4148Non-nil second arg SECONDS and third arg MILLISEC are number of seconds
4149and milliseconds to wait; return after that much time whether or not
4150there is any subprocess output. If SECONDS is a floating point number,
5088da42 4151it specifies a fractional number of seconds to wait.
891bd142 4152The MILLISEC argument is obsolete and should be avoided.
5088da42 4153
107ed38d
KS
4154If optional fourth arg JUST-THIS-ONE is non-nil, only accept output
4155from PROCESS, suspending reading output from other processes.
4156If JUST-THIS-ONE is an integer, don't run any timers either.
e0f24100 4157Return non-nil if we received any output before the timeout expired. */)
5088da42
KS
4158 (process, seconds, millisec, just_this_one)
4159 register Lisp_Object process, seconds, millisec, just_this_one;
d0d6b7c5 4160{
5088da42 4161 int secs, usecs = 0;
d0d6b7c5 4162
0748d150 4163 if (! NILP (process))
b7826503 4164 CHECK_PROCESS (process);
107ed38d
KS
4165 else
4166 just_this_one = Qnil;
0748d150 4167
891bd142
SM
4168 if (!NILP (millisec))
4169 { /* Obsolete calling convention using integers rather than floats. */
4170 CHECK_NUMBER (millisec);
4171 if (NILP (seconds))
4172 seconds = make_float (XINT (millisec) / 1000.0);
4173 else
4174 {
4175 CHECK_NUMBER (seconds);
4176 seconds = make_float (XINT (millisec) / 1000.0 + XINT (seconds));
4177 }
4178 }
4179
5088da42 4180 if (!NILP (seconds))
d0d6b7c5 4181 {
5088da42
KS
4182 if (INTEGERP (seconds))
4183 secs = XINT (seconds);
4184 else if (FLOATP (seconds))
4185 {
4186 double timeout = XFLOAT_DATA (seconds);
4187 secs = (int) timeout;
4188 usecs = (int) ((timeout - (double) secs) * 1000000);
4189 }
4190 else
4191 wrong_type_argument (Qnumberp, seconds);
d0d6b7c5 4192
5088da42
KS
4193 if (secs < 0 || (secs == 0 && usecs == 0))
4194 secs = -1, usecs = 0;
d0d6b7c5
JB
4195 }
4196 else
5088da42 4197 secs = NILP (process) ? -1 : 0;
d0d6b7c5
JB
4198
4199 return
5088da42 4200 (wait_reading_process_output (secs, usecs, 0, 0,
214d6069
KS
4201 Qnil,
4202 !NILP (process) ? XPROCESS (process) : NULL,
4203 NILP (just_this_one) ? 0 :
4204 !INTEGERP (just_this_one) ? 1 : -1)
d0d6b7c5
JB
4205 ? Qt : Qnil);
4206}
4207
e690ca94
KS
4208/* Accept a connection for server process SERVER on CHANNEL. */
4209
4210static int connect_counter = 0;
4211
4212static void
4213server_accept_connection (server, channel)
4214 Lisp_Object server;
4215 int channel;
4216{
4217 Lisp_Object proc, caller, name, buffer;
4218 Lisp_Object contact, host, service;
4219 struct Lisp_Process *ps= XPROCESS (server);
4220 struct Lisp_Process *p;
4221 int s;
4222 union u_sockaddr {
4223 struct sockaddr sa;
4224 struct sockaddr_in in;
e1652a86
KS
4225#ifdef AF_INET6
4226 struct sockaddr_in6 in6;
4227#endif
e690ca94
KS
4228#ifdef HAVE_LOCAL_SOCKETS
4229 struct sockaddr_un un;
4230#endif
4231 } saddr;
4232 int len = sizeof saddr;
4233
4234 s = accept (channel, &saddr.sa, &len);
4235
4236 if (s < 0)
4237 {
4238 int code = errno;
4239
4240 if (code == EAGAIN)
4241 return;
4242#ifdef EWOULDBLOCK
4243 if (code == EWOULDBLOCK)
4244 return;
4245#endif
4246
4247 if (!NILP (ps->log))
4248 call3 (ps->log, server, Qnil,
4249 concat3 (build_string ("accept failed with code"),
4250 Fnumber_to_string (make_number (code)),
4251 build_string ("\n")));
4252 return;
4253 }
4254
4255 connect_counter++;
4256
4257 /* Setup a new process to handle the connection. */
4258
4259 /* Generate a unique identification of the caller, and build contact
4260 information for this process. */
4261 host = Qt;
4262 service = Qnil;
4263 switch (saddr.sa.sa_family)
4264 {
4265 case AF_INET:
4266 {
4267 Lisp_Object args[5];
4268 unsigned char *ip = (unsigned char *)&saddr.in.sin_addr.s_addr;
4269 args[0] = build_string ("%d.%d.%d.%d");
4270 args[1] = make_number (*ip++);
4271 args[2] = make_number (*ip++);
4272 args[3] = make_number (*ip++);
4273 args[4] = make_number (*ip++);
4274 host = Fformat (5, args);
4275 service = make_number (ntohs (saddr.in.sin_port));
4276
4277 args[0] = build_string (" <%s:%d>");
4278 args[1] = host;
4279 args[2] = service;
4280 caller = Fformat (3, args);
4281 }
4282 break;
4283
e1652a86
KS
4284#ifdef AF_INET6
4285 case AF_INET6:
4286 {
4287 Lisp_Object args[9];
4288 uint16_t *ip6 = (uint16_t *)&saddr.in6.sin6_addr;
4289 int i;
4290 args[0] = build_string ("%x:%x:%x:%x:%x:%x:%x:%x");
4291 for (i = 0; i < 8; i++)
4292 args[i+1] = make_number (ntohs(ip6[i]));
4293 host = Fformat (9, args);
4294 service = make_number (ntohs (saddr.in.sin_port));
4295
4296 args[0] = build_string (" <[%s]:%d>");
4297 args[1] = host;
4298 args[2] = service;
4299 caller = Fformat (3, args);
4300 }
4301 break;
4302#endif
4303
e690ca94
KS
4304#ifdef HAVE_LOCAL_SOCKETS
4305 case AF_LOCAL:
4306#endif
4307 default:
4308 caller = Fnumber_to_string (make_number (connect_counter));
eef60308 4309 caller = concat3 (build_string (" <"), caller, build_string (">"));
e690ca94
KS
4310 break;
4311 }
4312
4313 /* Create a new buffer name for this process if it doesn't have a
4314 filter. The new buffer name is based on the buffer name or
4315 process name of the server process concatenated with the caller
4316 identification. */
4317
4318 if (!NILP (ps->filter) && !EQ (ps->filter, Qt))
4319 buffer = Qnil;
4320 else
4321 {
4322 buffer = ps->buffer;
4323 if (!NILP (buffer))
4324 buffer = Fbuffer_name (buffer);
4325 else
4326 buffer = ps->name;
4327 if (!NILP (buffer))
4328 {
4329 buffer = concat2 (buffer, caller);
4330 buffer = Fget_buffer_create (buffer);
4331 }
4332 }
4333
4334 /* Generate a unique name for the new server process. Combine the
4335 server process name with the caller identification. */
4336
4337 name = concat2 (ps->name, caller);
4338 proc = make_process (name);
4339
4340 chan_process[s] = proc;
4341
4342#ifdef O_NONBLOCK
4343 fcntl (s, F_SETFL, O_NONBLOCK);
4344#else
4345#ifdef O_NDELAY
4346 fcntl (s, F_SETFL, O_NDELAY);
4347#endif
4348#endif
4349
4350 p = XPROCESS (proc);
4351
4352 /* Build new contact information for this setup. */
4353 contact = Fcopy_sequence (ps->childp);
4354 contact = Fplist_put (contact, QCserver, Qnil);
4355 contact = Fplist_put (contact, QChost, host);
4356 if (!NILP (service))
4357 contact = Fplist_put (contact, QCservice, service);
177c0ea7 4358 contact = Fplist_put (contact, QCremote,
e690ca94
KS
4359 conv_sockaddr_to_lisp (&saddr.sa, len));
4360#ifdef HAVE_GETSOCKNAME
4361 len = sizeof saddr;
2185db04 4362 if (getsockname (s, &saddr.sa, &len) == 0)
177c0ea7 4363 contact = Fplist_put (contact, QClocal,
e690ca94
KS
4364 conv_sockaddr_to_lisp (&saddr.sa, len));
4365#endif
4366
4367 p->childp = contact;
faa7db08 4368 p->plist = Fcopy_sequence (ps->plist);
d888760c 4369 p->type = Qnetwork;
ac4a7584 4370
e690ca94
KS
4371 p->buffer = buffer;
4372 p->sentinel = ps->sentinel;
4373 p->filter = ps->filter;
4374 p->command = Qnil;
6bfd98e7 4375 p->pid = 0;
60f0fb11
SM
4376 p->infd = s;
4377 p->outfd = s;
e690ca94
KS
4378 p->status = Qrun;
4379
4380 /* Client processes for accepted connections are not stopped initially. */
4381 if (!EQ (p->filter, Qt))
4382 {
4383 FD_SET (s, &input_wait_mask);
4384 FD_SET (s, &non_keyboard_wait_mask);
4385 }
4386
4387 if (s > max_process_desc)
4388 max_process_desc = s;
4389
177c0ea7 4390 /* Setup coding system for new process based on server process.
e690ca94
KS
4391 This seems to be the proper thing to do, as the coding system
4392 of the new process should reflect the settings at the time the
4393 server socket was opened; not the current settings. */
4394
4395 p->decode_coding_system = ps->decode_coding_system;
4396 p->encode_coding_system = ps->encode_coding_system;
03f04413 4397 setup_process_coding_systems (proc);
e690ca94
KS
4398
4399 p->decoding_buf = make_uninit_string (0);
60f0fb11 4400 p->decoding_carryover = 0;
e690ca94 4401 p->encoding_buf = make_uninit_string (0);
e690ca94
KS
4402
4403 p->inherit_coding_system_flag
60f0fb11 4404 = (NILP (buffer) ? 0 : ps->inherit_coding_system_flag);
e690ca94
KS
4405
4406 if (!NILP (ps->log))
4407 call3 (ps->log, server, proc,
4408 concat3 (build_string ("accept from "),
4409 (STRINGP (host) ? host : build_string ("-")),
4410 build_string ("\n")));
4411
bed9664a 4412 if (!NILP (p->sentinel))
177c0ea7 4413 exec_sentinel (proc,
e690ca94
KS
4414 concat3 (build_string ("open from "),
4415 (STRINGP (host) ? host : build_string ("-")),
4416 build_string ("\n")));
4417}
4418
d0d6b7c5
JB
4419/* This variable is different from waiting_for_input in keyboard.c.
4420 It is used to communicate to a lisp process-filter/sentinel (via the
f3d9532b 4421 function Fwaiting_for_user_input_p below) whether Emacs was waiting
d0d6b7c5
JB
4422 for user-input when that process-filter was called.
4423 waiting_for_input cannot be used as that is by definition 0 when
d430ee71
RS
4424 lisp code is being evalled.
4425 This is also used in record_asynch_buffer_change.
4426 For that purpose, this must be 0
214d6069 4427 when not inside wait_reading_process_output. */
d0d6b7c5
JB
4428static int waiting_for_user_input_p;
4429
2beb96f9
KS
4430static Lisp_Object
4431wait_reading_process_output_unwind (data)
4432 Lisp_Object data;
4433{
4434 waiting_for_user_input_p = XINT (data);
4435 return Qnil;
4436}
4437
c573ae8e 4438/* This is here so breakpoints can be put on it. */
dfcf069d 4439static void
214d6069 4440wait_reading_process_output_1 ()
c573ae8e
RS
4441{
4442}
4443
e1e541bd
KS
4444/* Use a wrapper around select to work around a bug in gdb 5.3.
4445 Normally, the wrapper is optimzed away by inlining.
4446
4447 If emacs is stopped inside select, the gdb backtrace doesn't
4448 show the function which called select, so it is practically
4449 impossible to step through wait_reading_process_output. */
4450
4451#ifndef select
4452static INLINE int
4453select_wrapper (n, rfd, wfd, xfd, tmo)
4454 int n;
4455 SELECT_TYPE *rfd, *wfd, *xfd;
4456 EMACS_TIME *tmo;
4457{
4458 return select (n, rfd, wfd, xfd, tmo);
4459}
4460#define select select_wrapper
4461#endif
4462
d0d6b7c5
JB
4463/* Read and dispose of subprocess output while waiting for timeout to
4464 elapse and/or keyboard input to be available.
4465
de6fd4b9 4466 TIME_LIMIT is:
d0d6b7c5
JB
4467 timeout in seconds, or
4468 zero for no limit, or
4469 -1 means gobble data immediately available but don't wait for any.
4470
de6fd4b9
RS
4471 MICROSECS is:
4472 an additional duration to wait, measured in microseconds.
4473 If this is nonzero and time_limit is 0, then the timeout
4474 consists of MICROSECS only.
6e4f3667 4475
de6fd4b9 4476 READ_KBD is a lisp value:
d0d6b7c5
JB
4477 0 to ignore keyboard input, or
4478 1 to return when input is available, or
84aa3ace 4479 -1 meaning caller will actually read the input, so don't throw to
d0d6b7c5 4480 the quit handler, or
d0d6b7c5 4481
de6fd4b9 4482 DO_DISPLAY != 0 means redisplay should be done to show subprocess
107ed38d 4483 output that arrives.
d0d6b7c5 4484
b89a415a
KS
4485 If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
4486 (and gobble terminal input into the buffer if any arrives).
d0d6b7c5 4487
b89a415a 4488 If WAIT_PROC is specified, wait until something arrives from that
e0f24100 4489 process. The return value is true if we read some input from
b89a415a
KS
4490 that process.
4491
4492 If JUST_WAIT_PROC is non-nil, handle only output from WAIT_PROC
4493 (suspending output from other processes). A negative value
4494 means don't run any timers either.
4495
e0f24100 4496 If WAIT_PROC is specified, then the function returns true if we
b89a415a 4497 received input from that process before the timeout elapsed.
e0f24100 4498 Otherwise, return true if we received input from any process. */
d0d6b7c5 4499
dfcf069d 4500int
214d6069
KS
4501wait_reading_process_output (time_limit, microsecs, read_kbd, do_display,
4502 wait_for_cell, wait_proc, just_wait_proc)
b89a415a
KS
4503 int time_limit, microsecs, read_kbd, do_display;
4504 Lisp_Object wait_for_cell;
4505 struct Lisp_Process *wait_proc;
4506 int just_wait_proc;
d0d6b7c5 4507{
41d03b9a 4508 register int channel, nfds;
855448dc 4509 SELECT_TYPE Available;
bad49fc7 4510#ifdef NON_BLOCKING_CONNECT
855448dc 4511 SELECT_TYPE Connecting;
bad49fc7
KS
4512 int check_connect;
4513#endif
4514 int check_delay, no_avail;
d0d6b7c5
JB
4515 int xerrno;
4516 Lisp_Object proc;
41d03b9a 4517 EMACS_TIME timeout, end_time;
a9f2c884 4518 int wait_channel = -1;
d0d6b7c5 4519 int got_some_input = 0;
2beb96f9 4520 int count = SPECPDL_INDEX ();
d0d6b7c5
JB
4521
4522 FD_ZERO (&Available);
bad49fc7 4523#ifdef NON_BLOCKING_CONNECT
dd2a17ab 4524 FD_ZERO (&Connecting);
bad49fc7 4525#endif
d0d6b7c5 4526
b89a415a
KS
4527 /* If wait_proc is a process to watch, set wait_channel accordingly. */
4528 if (wait_proc != NULL)
60f0fb11 4529 wait_channel = wait_proc->infd;
84aa3ace 4530
2beb96f9
KS
4531 record_unwind_protect (wait_reading_process_output_unwind,
4532 make_number (waiting_for_user_input_p));
b89a415a 4533 waiting_for_user_input_p = read_kbd;
d0d6b7c5
JB
4534
4535 /* Since we may need to wait several times,
4536 compute the absolute time to return at. */
4537 if (time_limit || microsecs)
4538 {
4539 EMACS_GET_TIME (end_time);
4540 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
4541 EMACS_ADD_TIME (end_time, end_time, timeout);
4542 }
4543
d0d6b7c5
JB
4544 while (1)
4545 {
c0239a0b
RS
4546 int timeout_reduced_for_timers = 0;
4547
d0d6b7c5
JB
4548 /* If calling from keyboard input, do not quit
4549 since we want to return C-g as an input character.
4550 Otherwise, do pending quit if requested. */
b89a415a 4551 if (read_kbd >= 0)
d0d6b7c5 4552 QUIT;
6b61353c 4553#ifdef SYNC_INPUT
ef5eb925 4554 else
c3eff1b0 4555 process_pending_signals ();
6b61353c 4556#endif
d0d6b7c5 4557
889255b4 4558 /* Exit now if the cell we're waiting for became non-nil. */
f3fbd155 4559 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
889255b4
RS
4560 break;
4561
d0d6b7c5
JB
4562 /* Compute time from now till when time limit is up */
4563 /* Exit if already run out */
4564 if (time_limit == -1)
4565 {
4566 /* -1 specified for timeout means
4567 gobble output available now
4568 but don't wait at all. */
4569
4570 EMACS_SET_SECS_USECS (timeout, 0, 0);
4571 }
4572 else if (time_limit || microsecs)
4573 {
4574 EMACS_GET_TIME (timeout);
4575 EMACS_SUB_TIME (timeout, end_time, timeout);
4576 if (EMACS_TIME_NEG_P (timeout))
4577 break;
4578 }
4579 else
4580 {
4581 EMACS_SET_SECS_USECS (timeout, 100000, 0);
4582 }
4583
f854a00b
RS
4584 /* Normally we run timers here.
4585 But not if wait_for_cell; in those cases,
4586 the wait is supposed to be short,
4587 and those callers cannot handle running arbitrary Lisp code here. */
107ed38d 4588 if (NILP (wait_for_cell)
b89a415a 4589 && just_wait_proc >= 0)
fb4c3627 4590 {
c0239a0b 4591 EMACS_TIME timer_delay;
c573ae8e 4592
9baacf76 4593 do
c573ae8e 4594 {
9baacf76 4595 int old_timers_run = timers_run;
c88164fe 4596 struct buffer *old_buffer = current_buffer;
70bb1e5f 4597 Lisp_Object old_window = selected_window;
177c0ea7 4598
9baacf76 4599 timer_delay = timer_check (1);
a2fab450
GM
4600
4601 /* If a timer has run, this might have changed buffers
4602 an alike. Make read_key_sequence aware of that. */
4603 if (timers_run != old_timers_run
70bb1e5f
CY
4604 && (old_buffer != current_buffer
4605 || !EQ (old_window, selected_window))
a2fab450
GM
4606 && waiting_for_user_input_p == -1)
4607 record_asynch_buffer_change ();
177c0ea7 4608
9baacf76
GM
4609 if (timers_run != old_timers_run && do_display)
4610 /* We must retry, since a timer may have requeued itself
4611 and that could alter the time_delay. */
3007ebfb 4612 redisplay_preserve_echo_area (9);
9baacf76
GM
4613 else
4614 break;
c573ae8e 4615 }
9baacf76 4616 while (!detect_input_pending ());
c573ae8e 4617
69645afc 4618 /* If there is unread keyboard input, also return. */
b89a415a 4619 if (read_kbd != 0
69645afc
RS
4620 && requeued_events_pending_p ())
4621 break;
4622
c0239a0b 4623 if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1)
fb4c3627
RS
4624 {
4625 EMACS_TIME difference;
4626 EMACS_SUB_TIME (difference, timer_delay, timeout);
4627 if (EMACS_TIME_NEG_P (difference))
c0239a0b
RS
4628 {
4629 timeout = timer_delay;
4630 timeout_reduced_for_timers = 1;
4631 }
fb4c3627 4632 }
4abca5e7
RS
4633 /* If time_limit is -1, we are not going to wait at all. */
4634 else if (time_limit != -1)
c573ae8e
RS
4635 {
4636 /* This is so a breakpoint can be put here. */
214d6069 4637 wait_reading_process_output_1 ();
c573ae8e 4638 }
fb4c3627
RS
4639 }
4640
90ab1a81
JB
4641 /* Cause C-g and alarm signals to take immediate action,
4642 and cause input available signals to zero out timeout.
4643
4644 It is important that we do this before checking for process
4645 activity. If we get a SIGCHLD after the explicit checks for
4646 process activity, timeout is the only way we will know. */
b89a415a 4647 if (read_kbd < 0)
90ab1a81
JB
4648 set_waiting_for_input (&timeout);
4649
6be429b1
JB
4650 /* If status of something has changed, and no input is
4651 available, notify the user of the change right away. After
4652 this explicit check, we'll let the SIGCHLD handler zap
61bd39a3
CY
4653 timeout to get our attention. When Emacs is run
4654 interactively, only do this with a nonzero DO_DISPLAY
4655 argument, because status_notify triggers redisplay. */
4656 if (update_tick != process_tick
4657 && (do_display || noninteractive))
6be429b1 4658 {
bad49fc7
KS
4659 SELECT_TYPE Atemp;
4660#ifdef NON_BLOCKING_CONNECT
4661 SELECT_TYPE Ctemp;
4662#endif
dd2a17ab 4663
6be429b1 4664 Atemp = input_wait_mask;
bad49fc7
KS
4665 IF_NON_BLOCKING_CONNECT (Ctemp = connect_wait_mask);
4666
6be429b1 4667 EMACS_SET_SECS_USECS (timeout, 0, 0);
994d9841
NR
4668 if ((select (max (max (max_process_desc, max_keyboard_desc),
4669 max_gpm_desc) + 1,
177c0ea7 4670 &Atemp,
bad49fc7 4671#ifdef NON_BLOCKING_CONNECT
dd2a17ab 4672 (num_pending_connects > 0 ? &Ctemp : (SELECT_TYPE *)0),
bad49fc7
KS
4673#else
4674 (SELECT_TYPE *)0,
4675#endif
dd2a17ab 4676 (SELECT_TYPE *)0, &timeout)
ecd1f654 4677 <= 0))
90ab1a81
JB
4678 {
4679 /* It's okay for us to do this and then continue with
a0e4d3f3 4680 the loop, since timeout has already been zeroed out. */
90ab1a81 4681 clear_waiting_for_input ();
ff6daed3 4682 status_notify (NULL);
90ab1a81 4683 }
6be429b1
JB
4684 }
4685
dd2a17ab 4686 /* Don't wait for output from a non-running process. Just
63136da6 4687 read whatever data has already been received. */
6bfd98e7 4688 if (wait_proc && wait_proc->raw_status_new)
6be429b1 4689 update_status (wait_proc);
6bfd98e7 4690 if (wait_proc
dd2a17ab
KS
4691 && ! EQ (wait_proc->status, Qrun)
4692 && ! EQ (wait_proc->status, Qconnect))
9aa2a7f4 4693 {
215b45e9 4694 int nread, total_nread = 0;
7ce63188 4695
9aa2a7f4 4696 clear_waiting_for_input ();
7ce63188
RS
4697 XSETPROCESS (proc, wait_proc);
4698
4699 /* Read data from the process, until we exhaust it. */
60f0fb11 4700 while (wait_proc->infd >= 0)
215b45e9 4701 {
60f0fb11 4702 nread = read_process_output (proc, wait_proc->infd);
e1b37c34
GM
4703
4704 if (nread == 0)
4705 break;
4706
63136da6
AS
4707 if (0 < nread)
4708 {
891bd142
SM
4709 total_nread += nread;
4710 got_some_input = 1;
4711 }
215b45e9
RS
4712#ifdef EIO
4713 else if (nread == -1 && EIO == errno)
63136da6 4714 break;
e1b37c34
GM
4715#endif
4716#ifdef EAGAIN
4717 else if (nread == -1 && EAGAIN == errno)
63136da6 4718 break;
e1b37c34
GM
4719#endif
4720#ifdef EWOULDBLOCK
4721 else if (nread == -1 && EWOULDBLOCK == errno)
63136da6 4722 break;
215b45e9
RS
4723#endif
4724 }
7ce63188 4725 if (total_nread > 0 && do_display)
3007ebfb 4726 redisplay_preserve_echo_area (10);
7ce63188 4727
9aa2a7f4
JB
4728 break;
4729 }
6be429b1 4730
d0d6b7c5
JB
4731 /* Wait till there is something to do */
4732
b89a415a 4733 if (wait_proc && just_wait_proc)
107ed38d 4734 {
60f0fb11 4735 if (wait_proc->infd < 0) /* Terminated */
b89a415a 4736 break;
60f0fb11 4737 FD_SET (wait_proc->infd, &Available);
bad49fc7
KS
4738 check_delay = 0;
4739 IF_NON_BLOCKING_CONNECT (check_connect = 0);
107ed38d
KS
4740 }
4741 else if (!NILP (wait_for_cell))
dd2a17ab
KS
4742 {
4743 Available = non_process_wait_mask;
bad49fc7
KS
4744 check_delay = 0;
4745 IF_NON_BLOCKING_CONNECT (check_connect = 0);
dd2a17ab 4746 }
a69281ff 4747 else
dd2a17ab 4748 {
b89a415a 4749 if (! read_kbd)
dd2a17ab
KS
4750 Available = non_keyboard_wait_mask;
4751 else
4752 Available = input_wait_mask;
bad49fc7 4753 IF_NON_BLOCKING_CONNECT (check_connect = (num_pending_connects > 0));
6b61353c 4754 check_delay = wait_channel >= 0 ? 0 : process_output_delay_count;
dd2a17ab 4755 }
d0d6b7c5 4756
ff11dfa1 4757 /* If frame size has changed or the window is newly mapped,
ffd56f97
JB
4758 redisplay now, before we start to wait. There is a race
4759 condition here; if a SIGIO arrives between now and the select
016899c0
JB
4760 and indicates that a frame is trashed, the select may block
4761 displaying a trashed screen. */
5164ee8e 4762 if (frame_garbaged && do_display)
7286affd
RS
4763 {
4764 clear_waiting_for_input ();
3007ebfb 4765 redisplay_preserve_echo_area (11);
b89a415a 4766 if (read_kbd < 0)
7efe788e 4767 set_waiting_for_input (&timeout);
7286affd 4768 }
ffd56f97 4769
dd2a17ab 4770 no_avail = 0;
b89a415a 4771 if (read_kbd && detect_input_pending ())
0a65b032
RS
4772 {
4773 nfds = 0;
dd2a17ab 4774 no_avail = 1;
0a65b032
RS
4775 }
4776 else
dd2a17ab 4777 {
bad49fc7 4778#ifdef NON_BLOCKING_CONNECT
dd2a17ab
KS
4779 if (check_connect)
4780 Connecting = connect_wait_mask;
bad49fc7 4781#endif
6b61353c
KH
4782
4783#ifdef ADAPTIVE_READ_BUFFERING
db853b7a 4784 /* Set the timeout for adaptive read buffering if any
60f0fb11 4785 process has non-zero read_output_skip and non-zero
db853b7a
KS
4786 read_output_delay, and we are not reading output for a
4787 specific wait_channel. It is not executed if
4788 Vprocess_adaptive_read_buffering is nil. */
6b61353c
KH
4789 if (process_output_skip && check_delay > 0)
4790 {
4791 int usecs = EMACS_USECS (timeout);
4792 if (EMACS_SECS (timeout) > 0 || usecs > READ_OUTPUT_DELAY_MAX)
4793 usecs = READ_OUTPUT_DELAY_MAX;
4794 for (channel = 0; check_delay > 0 && channel <= max_process_desc; channel++)
4795 {
4796 proc = chan_process[channel];
4797 if (NILP (proc))
4798 continue;
db853b7a 4799 /* Find minimum non-zero read_output_delay among the
60f0fb11
SM
4800 processes with non-zero read_output_skip. */
4801 if (XPROCESS (proc)->read_output_delay > 0)
6b61353c
KH
4802 {
4803 check_delay--;
60f0fb11 4804 if (!XPROCESS (proc)->read_output_skip)
6b61353c
KH
4805 continue;
4806 FD_CLR (channel, &Available);
60f0fb11
SM
4807 XPROCESS (proc)->read_output_skip = 0;
4808 if (XPROCESS (proc)->read_output_delay < usecs)
4809 usecs = XPROCESS (proc)->read_output_delay;
6b61353c
KH
4810 }
4811 }
4812 EMACS_SET_SECS_USECS (timeout, 0, usecs);
4813 process_output_skip = 0;
4814 }
4815#endif
edfda783 4816#ifdef HAVE_NS
63136da6 4817 nfds = ns_select
edfda783 4818#else
63136da6 4819 nfds = select
edfda783 4820#endif
63136da6 4821 (max (max (max_process_desc, max_keyboard_desc),
994d9841 4822 max_gpm_desc) + 1,
177c0ea7 4823 &Available,
bad49fc7 4824#ifdef NON_BLOCKING_CONNECT
dd2a17ab 4825 (check_connect ? &Connecting : (SELECT_TYPE *)0),
bad49fc7
KS
4826#else
4827 (SELECT_TYPE *)0,
4828#endif
dd2a17ab
KS
4829 (SELECT_TYPE *)0, &timeout);
4830 }
6720a7fb 4831
d0d6b7c5
JB
4832 xerrno = errno;
4833
4834 /* Make C-g and alarm signals set flags again */
4835 clear_waiting_for_input ();
4836
4837 /* If we woke up due to SIGWINCH, actually change size now. */
2b653806 4838 do_pending_window_change (0);
d0d6b7c5 4839
c0239a0b
RS
4840 if (time_limit && nfds == 0 && ! timeout_reduced_for_timers)
4841 /* We wanted the full specified time, so return now. */
d0d6b7c5
JB
4842 break;
4843 if (nfds < 0)
4844 {
4845 if (xerrno == EINTR)
dd2a17ab 4846 no_avail = 1;
d0d6b7c5
JB
4847 else if (xerrno == EBADF)
4848 {
4849#ifdef AIX
4850 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
4851 the child's closure of the pts gives the parent a SIGHUP, and
4852 the ptc file descriptor is automatically closed,
4853 yielding EBADF here or at select() call above.
4854 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
a0e4d3f3 4855 in m/ibmrt-aix.h), and here we just ignore the select error.
d0d6b7c5 4856 Cleanup occurs c/o status_notify after SIGCLD. */
dd2a17ab 4857 no_avail = 1; /* Cannot depend on values returned */
d0d6b7c5
JB
4858#else
4859 abort ();
4860#endif
4861 }
4862 else
68c45bf0 4863 error ("select error: %s", emacs_strerror (xerrno));
d0d6b7c5 4864 }
dd2a17ab
KS
4865
4866 if (no_avail)
4867 {
4868 FD_ZERO (&Available);
bad49fc7 4869 IF_NON_BLOCKING_CONNECT (check_connect = 0);
dd2a17ab
KS
4870 }
4871
5d5beb62
RS
4872#if 0 /* When polling is used, interrupt_input is 0,
4873 so get_input_pending should read the input.
4874 So this should not be needed. */
4875 /* If we are using polling for input,
4876 and we see input available, make it get read now.
4877 Otherwise it might not actually get read for a second.
214d6069 4878 And on hpux, since we turn off polling in wait_reading_process_output,
5d5beb62 4879 it might never get read at all if we don't spend much time
214d6069 4880 outside of wait_reading_process_output. */
b89a415a 4881 if (read_kbd && interrupt_input
5d5beb62
RS
4882 && keyboard_bit_set (&Available)
4883 && input_polling_used ())
4884 kill (getpid (), SIGALRM);
4885#endif
4886
d0d6b7c5
JB
4887 /* Check for keyboard input */
4888 /* If there is any, return immediately
4889 to give it higher priority than subprocesses */
4890
b89a415a 4891 if (read_kbd != 0)
6ed6233b 4892 {
a2fab450 4893 int old_timers_run = timers_run;
c88164fe 4894 struct buffer *old_buffer = current_buffer;
c0323133 4895 Lisp_Object old_window = selected_window;
a2fab450 4896 int leave = 0;
177c0ea7 4897
5d6c2aa3 4898 if (detect_input_pending_run_timers (do_display))
a2fab450
GM
4899 {
4900 swallow_events (do_display);
4901 if (detect_input_pending_run_timers (do_display))
4902 leave = 1;
4903 }
6ed6233b 4904
a2fab450
GM
4905 /* If a timer has run, this might have changed buffers
4906 an alike. Make read_key_sequence aware of that. */
4907 if (timers_run != old_timers_run
c88164fe 4908 && waiting_for_user_input_p == -1
c0323133
GM
4909 && (old_buffer != current_buffer
4910 || !EQ (old_window, selected_window)))
a2fab450
GM
4911 record_asynch_buffer_change ();
4912
4913 if (leave)
4914 break;
177c0ea7
JB
4915 }
4916
69645afc 4917 /* If there is unread keyboard input, also return. */
b89a415a 4918 if (read_kbd != 0
69645afc
RS
4919 && requeued_events_pending_p ())
4920 break;
4921
77e1b3d4
RS
4922 /* If we are not checking for keyboard input now,
4923 do process events (but don't run any timers).
4924 This is so that X events will be processed.
0c9960e9 4925 Otherwise they may have to wait until polling takes place.
77e1b3d4
RS
4926 That would causes delays in pasting selections, for example.
4927
4928 (We used to do this only if wait_for_cell.) */
b89a415a 4929 if (read_kbd == 0 && detect_input_pending ())
f854a00b
RS
4930 {
4931 swallow_events (do_display);
0c9960e9 4932#if 0 /* Exiting when read_kbd doesn't request that seems wrong, though. */
f854a00b
RS
4933 if (detect_input_pending ())
4934 break;
5d5beb62 4935#endif
0c9960e9 4936 }
f854a00b 4937
84aa3ace 4938 /* Exit now if the cell we're waiting for became non-nil. */
f3fbd155 4939 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
84aa3ace
RS
4940 break;
4941
4746118a 4942#ifdef SIGIO
5d5beb62 4943 /* If we think we have keyboard input waiting, but didn't get SIGIO,
d0d6b7c5
JB
4944 go read it. This can happen with X on BSD after logging out.
4945 In that case, there really is no input and no SIGIO,
4946 but select says there is input. */
4947
b89a415a 4948 if (read_kbd && interrupt_input
2601f59e 4949 && keyboard_bit_set (&Available) && ! noninteractive)
e643c5be 4950 kill (getpid (), SIGIO);
4746118a 4951#endif
d0d6b7c5 4952
d0d6b7c5
JB
4953 if (! wait_proc)
4954 got_some_input |= nfds > 0;
4955
32676c08
JB
4956 /* If checking input just got us a size-change event from X,
4957 obey it now if we should. */
b89a415a 4958 if (read_kbd || ! NILP (wait_for_cell))
2b653806 4959 do_pending_window_change (0);
32676c08 4960
a9f2c884 4961 /* Check for data from a process. */
dd2a17ab
KS
4962 if (no_avail || nfds == 0)
4963 continue;
4964
a9f2c884
RS
4965 /* Really FIRST_PROC_DESC should be 0 on Unix,
4966 but this is safer in the short run. */
a69281ff 4967 for (channel = 0; channel <= max_process_desc; channel++)
d0d6b7c5 4968 {
a69281ff
RS
4969 if (FD_ISSET (channel, &Available)
4970 && FD_ISSET (channel, &non_keyboard_wait_mask))
d0d6b7c5
JB
4971 {
4972 int nread;
4973
4974 /* If waiting for this channel, arrange to return as
4975 soon as no more input to be processed. No more
4976 waiting. */
4977 if (wait_channel == channel)
4978 {
a9f2c884 4979 wait_channel = -1;
d0d6b7c5
JB
4980 time_limit = -1;
4981 got_some_input = 1;
4982 }
4983 proc = chan_process[channel];
4984 if (NILP (proc))
4985 continue;
4986
e690ca94
KS
4987 /* If this is a server stream socket, accept connection. */
4988 if (EQ (XPROCESS (proc)->status, Qlisten))
4989 {
4990 server_accept_connection (proc, channel);
4991 continue;
4992 }
4993
d0d6b7c5
JB
4994 /* Read data from the process, starting with our
4995 buffered-ahead character if we have one. */
4996
4997 nread = read_process_output (proc, channel);
4998 if (nread > 0)
4999 {
5000 /* Since read_process_output can run a filter,
5001 which can call accept-process-output,
5002 don't try to read from any other processes
5003 before doing the select again. */
5004 FD_ZERO (&Available);
5005
5006 if (do_display)
3007ebfb 5007 redisplay_preserve_echo_area (12);
d0d6b7c5
JB
5008 }
5009#ifdef EWOULDBLOCK
5010 else if (nread == -1 && errno == EWOULDBLOCK)
5011 ;
0b75e9a4 5012#endif
89d7280d
RS
5013 /* ISC 4.1 defines both EWOULDBLOCK and O_NONBLOCK,
5014 and Emacs uses O_NONBLOCK, so what we get is EAGAIN. */
d0d6b7c5
JB
5015#ifdef O_NONBLOCK
5016 else if (nread == -1 && errno == EAGAIN)
5017 ;
5018#else
5019#ifdef O_NDELAY
5020 else if (nread == -1 && errno == EAGAIN)
5021 ;
5022 /* Note that we cannot distinguish between no input
5023 available now and a closed pipe.
5024 With luck, a closed pipe will be accompanied by
5025 subprocess termination and SIGCHLD. */
d888760c 5026 else if (nread == 0 && !NETCONN_P (proc) && !SERIALCONN_P (proc))
d0d6b7c5 5027 ;
4740825c
KS
5028#endif /* O_NDELAY */
5029#endif /* O_NONBLOCK */
d0d6b7c5
JB
5030#ifdef HAVE_PTYS
5031 /* On some OSs with ptys, when the process on one end of
5032 a pty exits, the other end gets an error reading with
5033 errno = EIO instead of getting an EOF (0 bytes read).
5034 Therefore, if we get an error reading and errno =
5035 EIO, just continue, because the child process has
5036 exited and should clean itself up soon (e.g. when we
5651af6d
RS
5037 get a SIGCHLD).
5038
5039 However, it has been known to happen that the SIGCHLD
4740825c 5040 got lost. So raise the signal again just in case.
5651af6d 5041 It can't hurt. */
d0d6b7c5 5042 else if (nread == -1 && errno == EIO)
4740825c
KS
5043 {
5044 /* Clear the descriptor now, so we only raise the signal once. */
5045 FD_CLR (channel, &input_wait_mask);
5046 FD_CLR (channel, &non_keyboard_wait_mask);
5047
5048 kill (getpid (), SIGCHLD);
5049 }
5050#endif /* HAVE_PTYS */
ffd56f97
JB
5051 /* If we can detect process termination, don't consider the process
5052 gone just because its pipe is closed. */
d0d6b7c5 5053#ifdef SIGCHLD
d888760c 5054 else if (nread == 0 && !NETCONN_P (proc) && !SERIALCONN_P (proc))
d0d6b7c5
JB
5055 ;
5056#endif
5057 else
5058 {
5059 /* Preserve status of processes already terminated. */
60f0fb11 5060 XPROCESS (proc)->tick = ++process_tick;
d0d6b7c5 5061 deactivate_process (proc);
6bfd98e7 5062 if (XPROCESS (proc)->raw_status_new)
d0d6b7c5
JB
5063 update_status (XPROCESS (proc));
5064 if (EQ (XPROCESS (proc)->status, Qrun))
5065 XPROCESS (proc)->status
5066 = Fcons (Qexit, Fcons (make_number (256), Qnil));
5067 }
5068 }
177c0ea7 5069#ifdef NON_BLOCKING_CONNECT
1566f511
KS
5070 if (check_connect && FD_ISSET (channel, &Connecting)
5071 && FD_ISSET (channel, &connect_wait_mask))
dd2a17ab
KS
5072 {
5073 struct Lisp_Process *p;
dd2a17ab
KS
5074
5075 FD_CLR (channel, &connect_wait_mask);
5076 if (--num_pending_connects < 0)
5077 abort ();
5078
5079 proc = chan_process[channel];
5080 if (NILP (proc))
5081 continue;
5082
5083 p = XPROCESS (proc);
5084
5085#ifdef GNU_LINUX
5086 /* getsockopt(,,SO_ERROR,,) is said to hang on some systems.
63136da6 5087 So only use it on systems where it is known to work. */
dd2a17ab 5088 {
e690ca94 5089 int xlen = sizeof(xerrno);
dd2a17ab
KS
5090 if (getsockopt(channel, SOL_SOCKET, SO_ERROR, &xerrno, &xlen))
5091 xerrno = errno;
5092 }
5093#else
44c887be
PJ
5094 {
5095 struct sockaddr pname;
5096 int pnamelen = sizeof(pname);
5097
5098 /* If connection failed, getpeername will fail. */
5099 xerrno = 0;
5100 if (getpeername(channel, &pname, &pnamelen) < 0)
5101 {
5102 /* Obtain connect failure code through error slippage. */
5103 char dummy;
dd2a17ab 5104 xerrno = errno;
44c887be
PJ
5105 if (errno == ENOTCONN && read(channel, &dummy, 1) < 0)
5106 xerrno = errno;
5107 }
5108 }
dd2a17ab
KS
5109#endif
5110 if (xerrno)
5111 {
60f0fb11 5112 p->tick = ++process_tick;
dd2a17ab
KS
5113 p->status = Fcons (Qfailed, Fcons (make_number (xerrno), Qnil));
5114 deactivate_process (proc);
5115 }
5116 else
5117 {
5118 p->status = Qrun;
5119 /* Execute the sentinel here. If we had relied on
5120 status_notify to do it later, it will read input
5121 from the process before calling the sentinel. */
5122 exec_sentinel (proc, build_string ("open\n"));
e690ca94 5123 if (!EQ (p->filter, Qt) && !EQ (p->command, Qt))
dd2a17ab 5124 {
60f0fb11
SM
5125 FD_SET (p->infd, &input_wait_mask);
5126 FD_SET (p->infd, &non_keyboard_wait_mask);
dd2a17ab
KS
5127 }
5128 }
5129 }
5130#endif /* NON_BLOCKING_CONNECT */
ffd56f97
JB
5131 } /* end for each file descriptor */
5132 } /* end while exit conditions not met */
d0d6b7c5 5133
2beb96f9 5134 unbind_to (count, Qnil);
d430ee71 5135
ffd56f97
JB
5136 /* If calling from keyboard input, do not quit
5137 since we want to return C-g as an input character.
5138 Otherwise, do pending quit if requested. */
b89a415a 5139 if (read_kbd >= 0)
ffd56f97
JB
5140 {
5141 /* Prevent input_pending from remaining set if we quit. */
5142 clear_input_pending ();
5143 QUIT;
5144 }
efa2a55c 5145
d0d6b7c5
JB
5146 return got_some_input;
5147}
5148\f
3b9a3dfa
RS
5149/* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS. */
5150
5151static Lisp_Object
5152read_process_output_call (fun_and_args)
5153 Lisp_Object fun_and_args;
5154{
70949dac 5155 return apply1 (XCAR (fun_and_args), XCDR (fun_and_args));
3b9a3dfa
RS
5156}
5157
5158static Lisp_Object
5159read_process_output_error_handler (error)
5160 Lisp_Object error;
5161{
5162 cmd_error_internal (error, "error in process filter: ");
5163 Vinhibit_quit = Qt;
5164 update_echo_area ();
833ba342 5165 Fsleep_for (make_number (2), Qnil);
8c983bf2 5166 return Qt;
3b9a3dfa
RS
5167}
5168
d0d6b7c5
JB
5169/* Read pending output from the process channel,
5170 starting with our buffered-ahead character if we have one.
0fa1789e 5171 Yield number of decoded characters read.
d0d6b7c5 5172
116c423c 5173 This function reads at most 4096 characters.
d0d6b7c5 5174 If you want to read all available subprocess output,
0fa1789e
KH
5175 you must call it repeatedly until it returns zero.
5176
5177 The characters read are decoded according to PROC's coding-system
5178 for decoding. */
d0d6b7c5 5179
ff6daed3 5180static int
d0d6b7c5
JB
5181read_process_output (proc, channel)
5182 Lisp_Object proc;
5183 register int channel;
5184{
8f924df7 5185 register int nbytes;
d0d6b7c5 5186 char *chars;
d0d6b7c5 5187 register Lisp_Object outstream;
d0d6b7c5
JB
5188 register struct Lisp_Process *p = XPROCESS (proc);
5189 register int opoint;
c7580538 5190 struct coding_system *coding = proc_decode_coding_system[channel];
60f0fb11 5191 int carryover = p->decoding_carryover;
116c423c 5192 int readmax = 4096;
d0d6b7c5 5193
e690ca94 5194 chars = (char *) alloca (carryover + readmax);
e7fbaa65
KH
5195 if (carryover)
5196 /* See the comment above. */
d5db4077 5197 bcopy (SDATA (p->decoding_buf), chars, carryover);
0fa1789e 5198
e690ca94
KS
5199#ifdef DATAGRAM_SOCKETS
5200 /* We have a working select, so proc_buffered_char is always -1. */
5201 if (DATAGRAM_CHAN_P (channel))
5202 {
5203 int len = datagram_address[channel].len;
39b1da20 5204 nbytes = recvfrom (channel, chars + carryover, readmax,
e690ca94
KS
5205 0, datagram_address[channel].sa, &len);
5206 }
5207 else
5208#endif
d0d6b7c5 5209 if (proc_buffered_char[channel] < 0)
6b61353c 5210 {
39b1da20 5211 nbytes = emacs_read (channel, chars + carryover, readmax);
6b61353c 5212#ifdef ADAPTIVE_READ_BUFFERING
60f0fb11 5213 if (nbytes > 0 && p->adaptive_read_buffering)
6b61353c 5214 {
60f0fb11 5215 int delay = p->read_output_delay;
6b61353c
KH
5216 if (nbytes < 256)
5217 {
5218 if (delay < READ_OUTPUT_DELAY_MAX_MAX)
5219 {
5220 if (delay == 0)
5221 process_output_delay_count++;
5222 delay += READ_OUTPUT_DELAY_INCREMENT * 2;
5223 }
5224 }
39b1da20 5225 else if (delay > 0 && (nbytes == readmax))
6b61353c
KH
5226 {
5227 delay -= READ_OUTPUT_DELAY_INCREMENT;
5228 if (delay == 0)
5229 process_output_delay_count--;
5230 }
60f0fb11 5231 p->read_output_delay = delay;
6b61353c
KH
5232 if (delay)
5233 {
60f0fb11 5234 p->read_output_skip = 1;
6b61353c
KH
5235 process_output_skip = 1;
5236 }
5237 }
5238#endif
5239 }
d0d6b7c5
JB
5240 else
5241 {
ed7a4b2d 5242 chars[carryover] = proc_buffered_char[channel];
d0d6b7c5 5243 proc_buffered_char[channel] = -1;
39b1da20 5244 nbytes = emacs_read (channel, chars + carryover + 1, readmax - 1);
1d2fc612
RS
5245 if (nbytes < 0)
5246 nbytes = 1;
d0d6b7c5 5247 else
1d2fc612 5248 nbytes = nbytes + 1;
d0d6b7c5 5249 }
d0d6b7c5 5250
60f0fb11 5251 p->decoding_carryover = 0;
ca65341e 5252
ed7a4b2d 5253 /* At this point, NBYTES holds number of bytes just received
0fa1789e 5254 (including the one in proc_buffered_char[channel]). */
de7fbd09
KH
5255 if (nbytes <= 0)
5256 {
5257 if (nbytes < 0 || coding->mode & CODING_MODE_LAST_BLOCK)
5258 return nbytes;
5259 coding->mode |= CODING_MODE_LAST_BLOCK;
5260 }
d0d6b7c5 5261
1d2fc612 5262 /* Now set NBYTES how many bytes we must decode. */
e7fbaa65 5263 nbytes += carryover;
0fa1789e 5264
1d2fc612 5265 /* Read and dispose of the process output. */
d0d6b7c5
JB
5266 outstream = p->filter;
5267 if (!NILP (outstream))
5268 {
177c0ea7 5269 /* We inhibit quit here instead of just catching it so that
d0d6b7c5
JB
5270 hitting ^G when a filter happens to be running won't screw
5271 it up. */
aed13378 5272 int count = SPECPDL_INDEX ();
30c78175 5273 Lisp_Object odeactivate;
dfc21838 5274 Lisp_Object obuffer, okeymap;
1d2fc612 5275 Lisp_Object text;
4da2f5be 5276 int outer_running_asynch_code = running_asynch_code;
bbce7d72 5277 int waiting = waiting_for_user_input_p;
30c78175 5278
dfc21838
RS
5279 /* No need to gcpro these, because all we do with them later
5280 is test them for EQness, and none of them should be a string. */
30c78175 5281 odeactivate = Vdeactivate_mark;
dfc21838
RS
5282 XSETBUFFER (obuffer, current_buffer);
5283 okeymap = current_buffer->keymap;
30c78175 5284
d0d6b7c5 5285 specbind (Qinhibit_quit, Qt);
6545aada 5286 specbind (Qlast_nonmenu_event, Qt);
3b9a3dfa 5287
4da2f5be
RS
5288 /* In case we get recursively called,
5289 and we already saved the match data nonrecursively,
5290 save the same match data in safely recursive fashion. */
5291 if (outer_running_asynch_code)
5292 {
5293 Lisp_Object tem;
5294 /* Don't clobber the CURRENT match data, either! */
89f2614d
KS
5295 tem = Fmatch_data (Qnil, Qnil, Qnil);
5296 restore_search_regs ();
5297 record_unwind_save_match_data ();
5298 Fset_match_data (tem, Qt);
4da2f5be
RS
5299 }
5300
5301 /* For speed, if a search happens within this code,
5302 save the match data in a special nonrecursive fashion. */
7074fde6 5303 running_asynch_code = 1;
4da2f5be 5304
5bbf78c1
KH
5305 decode_coding_c_string (coding, chars, nbytes, Qt);
5306 text = coding->dst_object;
5bbf78c1 5307 Vlast_coding_system_used = CODING_ID_NAME (coding->id);
ed7a4b2d 5308 /* A new coding system might be found. */
5bbf78c1 5309 if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
ed7a4b2d 5310 {
5bbf78c1 5311 p->decode_coding_system = Vlast_coding_system_used;
ed7a4b2d
KH
5312
5313 /* Don't call setup_coding_system for
5314 proc_decode_coding_system[channel] here. It is done in
5315 detect_coding called via decode_coding above. */
5316
5317 /* If a coding system for encoding is not yet decided, we set
5318 it as the same as coding-system for decoding.
5319
5320 But, before doing that we must check if
5321 proc_encode_coding_system[p->outfd] surely points to a
5322 valid memory because p->outfd will be changed once EOF is
5323 sent to the process. */
5324 if (NILP (p->encode_coding_system)
60f0fb11 5325 && proc_encode_coding_system[p->outfd])
ed7a4b2d 5326 {
76d812bb
KH
5327 p->encode_coding_system
5328 = coding_inherit_eol_type (Vlast_coding_system_used, Qnil);
5bbf78c1 5329 setup_coding_system (p->encode_coding_system,
60f0fb11 5330 proc_encode_coding_system[p->outfd]);
ed7a4b2d
KH
5331 }
5332 }
51c6067d 5333
dd8c7a53
KH
5334 if (coding->carryover_bytes > 0)
5335 {
04961398
KH
5336 if (SCHARS (p->decoding_buf) < coding->carryover_bytes)
5337 p->decoding_buf = make_uninit_string (coding->carryover_bytes);
8f924df7 5338 bcopy (coding->carryover, SDATA (p->decoding_buf),
dd8c7a53 5339 coding->carryover_bytes);
8c406a9b 5340 p->decoding_carryover = coding->carryover_bytes;
dd8c7a53 5341 }
e430e5ba 5342 if (SBYTES (text) > 0)
dd97db06
KH
5343 internal_condition_case_1 (read_process_output_call,
5344 Fcons (outstream,
5345 Fcons (proc, Fcons (text, Qnil))),
5346 !NILP (Vdebug_on_error) ? Qnil : Qerror,
5347 read_process_output_error_handler);
4da2f5be
RS
5348
5349 /* If we saved the match data nonrecursively, restore it now. */
89f2614d 5350 restore_search_regs ();
4da2f5be 5351 running_asynch_code = outer_running_asynch_code;
d0d6b7c5 5352
592ce97f 5353 /* Handling the process output should not deactivate the mark. */
30c78175
RS
5354 Vdeactivate_mark = odeactivate;
5355
bbce7d72
RS
5356 /* Restore waiting_for_user_input_p as it was
5357 when we were called, in case the filter clobbered it. */
5358 waiting_for_user_input_p = waiting;
5359
7973cfa8
RS
5360#if 0 /* Call record_asynch_buffer_change unconditionally,
5361 because we might have changed minor modes or other things
5362 that affect key bindings. */
dfc21838
RS
5363 if (! EQ (Fcurrent_buffer (), obuffer)
5364 || ! EQ (current_buffer->keymap, okeymap))
7973cfa8 5365#endif
927e08be
RS
5366 /* But do it only if the caller is actually going to read events.
5367 Otherwise there's no need to make him wake up, and it could
7157b8fe 5368 cause trouble (for example it would make sit_for return). */
927e08be
RS
5369 if (waiting_for_user_input_p == -1)
5370 record_asynch_buffer_change ();
d72534ba 5371
2ea6d561 5372 unbind_to (count, Qnil);
e430e5ba 5373 return nbytes;
d0d6b7c5
JB
5374 }
5375
5376 /* If no filter, write into buffer if it isn't dead. */
5377 if (!NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name))
5378 {
b0310da4 5379 Lisp_Object old_read_only;
12ca5cdf 5380 int old_begv, old_zv;
d8a2934e 5381 int old_begv_byte, old_zv_byte;
30c78175 5382 Lisp_Object odeactivate;
d8a2934e
RS
5383 int before, before_byte;
5384 int opoint_byte;
ed7a4b2d 5385 Lisp_Object text;
926b7e5e 5386 struct buffer *b;
59c4c60f 5387 int count = SPECPDL_INDEX ();
30c78175
RS
5388
5389 odeactivate = Vdeactivate_mark;
d0d6b7c5 5390
59c4c60f 5391 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
d0d6b7c5 5392 Fset_buffer (p->buffer);
6ec8bbd2 5393 opoint = PT;
d8a2934e 5394 opoint_byte = PT_BYTE;
b0310da4 5395 old_read_only = current_buffer->read_only;
12ca5cdf
RS
5396 old_begv = BEGV;
5397 old_zv = ZV;
d8a2934e
RS
5398 old_begv_byte = BEGV_BYTE;
5399 old_zv_byte = ZV_BYTE;
b0310da4
JB
5400
5401 current_buffer->read_only = Qnil;
d0d6b7c5
JB
5402
5403 /* Insert new output into buffer
5404 at the current end-of-output marker,
5405 thus preserving logical ordering of input and output. */
5406 if (XMARKER (p->mark)->buffer)
d8a2934e
RS
5407 SET_PT_BOTH (clip_to_bounds (BEGV, marker_position (p->mark), ZV),
5408 clip_to_bounds (BEGV_BYTE, marker_byte_position (p->mark),
5409 ZV_BYTE));
d0d6b7c5 5410 else
d8a2934e 5411 SET_PT_BOTH (ZV, ZV_BYTE);
12ca5cdf 5412 before = PT;
d8a2934e 5413 before_byte = PT_BYTE;
b0310da4
JB
5414
5415 /* If the output marker is outside of the visible region, save
5416 the restriction and widen. */
6ec8bbd2 5417 if (! (BEGV <= PT && PT <= ZV))
b0310da4
JB
5418 Fwiden ();
5419
5bbf78c1
KH
5420 decode_coding_c_string (coding, chars, nbytes, Qt);
5421 text = coding->dst_object;
5422 Vlast_coding_system_used = CODING_ID_NAME (coding->id);
ed7a4b2d
KH
5423 /* A new coding system might be found. See the comment in the
5424 similar code in the previous `if' block. */
5bbf78c1 5425 if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
ed7a4b2d 5426 {
5bbf78c1 5427 p->decode_coding_system = Vlast_coding_system_used;
ed7a4b2d 5428 if (NILP (p->encode_coding_system)
60f0fb11 5429 && proc_encode_coding_system[p->outfd])
ed7a4b2d 5430 {
76d812bb
KH
5431 p->encode_coding_system
5432 = coding_inherit_eol_type (Vlast_coding_system_used, Qnil);
5bbf78c1 5433 setup_coding_system (p->encode_coding_system,
60f0fb11 5434 proc_encode_coding_system[p->outfd]);
ed7a4b2d
KH
5435 }
5436 }
dd8c7a53
KH
5437 if (coding->carryover_bytes > 0)
5438 {
04961398
KH
5439 if (SCHARS (p->decoding_buf) < coding->carryover_bytes)
5440 p->decoding_buf = make_uninit_string (coding->carryover_bytes);
8f924df7 5441 bcopy (coding->carryover, SDATA (p->decoding_buf),
dd8c7a53 5442 coding->carryover_bytes);
8c406a9b 5443 p->decoding_carryover = coding->carryover_bytes;
dd8c7a53 5444 }
d69864bf
KH
5445 /* Adjust the multibyteness of TEXT to that of the buffer. */
5446 if (NILP (current_buffer->enable_multibyte_characters)
5447 != ! STRING_MULTIBYTE (text))
5448 text = (STRING_MULTIBYTE (text)
57bb5c37 5449 ? Fstring_as_unibyte (text)
03f04413 5450 : Fstring_to_multibyte (text));
57bb5c37
KH
5451 /* Insert before markers in case we are inserting where
5452 the buffer's mark is, and the user's next command is Meta-y. */
e430e5ba
KH
5453 insert_from_string_before_markers (text, 0, 0,
5454 SCHARS (text), SBYTES (text), 0);
0d023da1 5455
926b7e5e
GM
5456 /* Make sure the process marker's position is valid when the
5457 process buffer is changed in the signal_after_change above.
5458 W3 is known to do that. */
5459 if (BUFFERP (p->buffer)
5460 && (b = XBUFFER (p->buffer), b != current_buffer))
5461 set_marker_both (p->mark, p->buffer, BUF_PT (b), BUF_PT_BYTE (b));
5462 else
5463 set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
b0310da4 5464
d0d6b7c5
JB
5465 update_mode_lines++;
5466
12ca5cdf
RS
5467 /* Make sure opoint and the old restrictions
5468 float ahead of any new text just as point would. */
5469 if (opoint >= before)
d8a2934e
RS
5470 {
5471 opoint += PT - before;
5472 opoint_byte += PT_BYTE - before_byte;
5473 }
12ca5cdf 5474 if (old_begv > before)
d8a2934e
RS
5475 {
5476 old_begv += PT - before;
5477 old_begv_byte += PT_BYTE - before_byte;
5478 }
12ca5cdf 5479 if (old_zv >= before)
d8a2934e
RS
5480 {
5481 old_zv += PT - before;
5482 old_zv_byte += PT_BYTE - before_byte;
5483 }
12ca5cdf 5484
b0310da4 5485 /* If the restriction isn't what it should be, set it. */
12ca5cdf
RS
5486 if (old_begv != BEGV || old_zv != ZV)
5487 Fnarrow_to_region (make_number (old_begv), make_number (old_zv));
b0310da4 5488
592ce97f 5489 /* Handling the process output should not deactivate the mark. */
30c78175
RS
5490 Vdeactivate_mark = odeactivate;
5491
b0310da4 5492 current_buffer->read_only = old_read_only;
d8a2934e 5493 SET_PT_BOTH (opoint, opoint_byte);
59c4c60f 5494 unbind_to (count, Qnil);
d0d6b7c5 5495 }
1d2fc612 5496 return nbytes;
d0d6b7c5
JB
5497}
5498
5499DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, Swaiting_for_user_input_p,
5500 0, 0, 0,
f3d9532b 5501 doc: /* Returns non-nil if Emacs is waiting for input from the user.
fdb82f93
PJ
5502This is intended for use by asynchronous process output filters and sentinels. */)
5503 ()
d0d6b7c5 5504{
8b4d685f 5505 return (waiting_for_user_input_p ? Qt : Qnil);
d0d6b7c5
JB
5506}
5507\f
5508/* Sending data to subprocess */
5509
5510jmp_buf send_process_frame;
0daad115 5511Lisp_Object process_sent_to;
d0d6b7c5
JB
5512
5513SIGTYPE
5514send_process_trap ()
5515{
333f1b6f 5516 SIGNAL_THREAD_CHECK (SIGPIPE);
c8f44005 5517 sigunblock (sigmask (SIGPIPE));
d0d6b7c5
JB
5518 longjmp (send_process_frame, 1);
5519}
5520
4556b700
RS
5521/* Send some data to process PROC.
5522 BUF is the beginning of the data; LEN is the number of characters.
a92e4183
KH
5523 OBJECT is the Lisp object that the data comes from. If OBJECT is
5524 nil or t, it means that the data comes from C string.
0fa1789e 5525
a92e4183
KH
5526 If OBJECT is not nil, the data is encoded by PROC's coding-system
5527 for encoding before it is sent.
1fb0098c
GM
5528
5529 This function can evaluate Lisp code and can garbage collect. */
4556b700 5530
ff6daed3 5531static void
4556b700 5532send_process (proc, buf, len, object)
ecd1f654 5533 volatile Lisp_Object proc;
0daad115
GM
5534 unsigned char *volatile buf;
5535 volatile int len;
5536 volatile Lisp_Object object;
d0d6b7c5 5537{
ecd1f654 5538 /* Use volatile to protect variables from being clobbered by longjmp. */
6b61353c 5539 struct Lisp_Process *p = XPROCESS (proc);
d0d6b7c5 5540 int rv;
0fa1789e 5541 struct coding_system *coding;
6044e593 5542 struct gcpro gcpro1;
41c4895c 5543 SIGTYPE (*volatile old_sigpipe) ();
6044e593
RS
5544
5545 GCPRO1 (object);
d0d6b7c5 5546
6bfd98e7 5547 if (p->raw_status_new)
6b61353c
KH
5548 update_status (p);
5549 if (! EQ (p->status, Qrun))
5550 error ("Process %s not running", SDATA (p->name));
60f0fb11 5551 if (p->outfd < 0)
6b61353c 5552 error ("Output file descriptor of %s is closed", SDATA (p->name));
0fa1789e 5553
60f0fb11 5554 coding = proc_encode_coding_system[p->outfd];
5bbf78c1 5555 Vlast_coding_system_used = CODING_ID_NAME (coding->id);
486b111b 5556
ed7a4b2d
KH
5557 if ((STRINGP (object) && STRING_MULTIBYTE (object))
5558 || (BUFFERP (object)
a92e4183
KH
5559 && !NILP (XBUFFER (object)->enable_multibyte_characters))
5560 || EQ (object, Qt))
278bfdd6 5561 {
6b61353c 5562 if (!EQ (Vlast_coding_system_used, p->encode_coding_system))
278bfdd6
KH
5563 /* The coding system for encoding was changed to raw-text
5564 because we sent a unibyte text previously. Now we are
5565 sending a multibyte text, thus we must encode it by the
6b61353c
KH
5566 original coding system specified for the current process. */
5567 setup_coding_system (p->encode_coding_system, coding);
e08bbb84 5568 coding->src_multibyte = 1;
278bfdd6
KH
5569 }
5570 else
5571 {
a92e4183
KH
5572 /* For sending a unibyte text, character code conversion should
5573 not take place but EOL conversion should. So, setup raw-text
5574 or one of the subsidiary if we have not yet done it. */
5bbf78c1 5575 if (CODING_REQUIRE_ENCODING (coding))
a92e4183
KH
5576 {
5577 if (CODING_REQUIRE_FLUSHING (coding))
5578 {
5579 /* But, before changing the coding, we must flush out data. */
5580 coding->mode |= CODING_MODE_LAST_BLOCK;
5581 send_process (proc, "", 0, Qt);
8f924df7 5582 coding->mode &= CODING_MODE_LAST_BLOCK;
a92e4183 5583 }
5bbf78c1
KH
5584 setup_coding_system (raw_text_coding_system
5585 (Vlast_coding_system_used),
5586 coding);
e08bbb84 5587 coding->src_multibyte = 0;
a92e4183 5588 }
278bfdd6 5589 }
a4a37e65
KH
5590 coding->dst_multibyte = 0;
5591
ed7a4b2d 5592 if (CODING_REQUIRE_ENCODING (coding))
0fa1789e 5593 {
5bbf78c1 5594 coding->dst_object = Qt;
ed7a4b2d 5595 if (BUFFERP (object))
0fa1789e 5596 {
5bbf78c1
KH
5597 int from_byte, from, to;
5598 int save_pt, save_pt_byte;
5599 struct buffer *cur = current_buffer;
5600
5601 set_buffer_internal (XBUFFER (object));
5602 save_pt = PT, save_pt_byte = PT_BYTE;
5603
5604 from_byte = PTR_BYTE_POS (buf);
5605 from = BYTE_TO_CHAR (from_byte);
5606 to = BYTE_TO_CHAR (from_byte + len);
5607 TEMP_SET_PT_BOTH (from, from_byte);
5608 encode_coding_object (coding, object, from, from_byte,
5609 to, from_byte + len, Qt);
5610 TEMP_SET_PT_BOTH (save_pt, save_pt_byte);
5611 set_buffer_internal (cur);
ed7a4b2d
KH
5612 }
5613 else if (STRINGP (object))
5614 {
5bbf78c1 5615 encode_coding_string (coding, object, 1);
0fa1789e 5616 }
5bbf78c1 5617 else
452294c2 5618 {
5bbf78c1
KH
5619 coding->dst_object = make_unibyte_string (buf, len);
5620 coding->produced = len;
452294c2 5621 }
ed7a4b2d 5622
e7fbaa65 5623 len = coding->produced;
8f924df7 5624 buf = SDATA (coding->dst_object);
0fa1789e 5625 }
d0d6b7c5 5626
4556b700
RS
5627 if (pty_max_bytes == 0)
5628 {
5629#if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON)
60f0fb11 5630 pty_max_bytes = fpathconf (p->outfd, _PC_MAX_CANON);
4556b700
RS
5631 if (pty_max_bytes < 0)
5632 pty_max_bytes = 250;
5633#else
5634 pty_max_bytes = 250;
5635#endif
5636 /* Deduct one, to leave space for the eof. */
5637 pty_max_bytes--;
5638 }
5639
0daad115
GM
5640 /* 2000-09-21: Emacs 20.7, sparc-sun-solaris-2.6, GCC 2.95.2,
5641 CFLAGS="-g -O": The value of the parameter `proc' is clobbered
5642 when returning with longjmp despite being declared volatile. */
d0d6b7c5 5643 if (!setjmp (send_process_frame))
0daad115
GM
5644 {
5645 process_sent_to = proc;
5646 while (len > 0)
5647 {
5648 int this = len;
93b4f699 5649
0daad115
GM
5650 /* Decide how much data we can send in one batch.
5651 Long lines need to be split into multiple batches. */
60f0fb11 5652 if (p->pty_flag)
0daad115 5653 {
c0ec53ad 5654 /* Starting this at zero is always correct when not the first
63136da6 5655 iteration because the previous iteration ended by sending C-d.
0daad115
GM
5656 It may not be correct for the first iteration
5657 if a partial line was sent in a separate send_process call.
5658 If that proves worth handling, we need to save linepos
5659 in the process object. */
5660 int linepos = 0;
5661 unsigned char *ptr = (unsigned char *) buf;
5662 unsigned char *end = (unsigned char *) buf + len;
5663
5664 /* Scan through this text for a line that is too long. */
5665 while (ptr != end && linepos < pty_max_bytes)
5666 {
5667 if (*ptr == '\n')
5668 linepos = 0;
5669 else
5670 linepos++;
5671 ptr++;
5672 }
5673 /* If we found one, break the line there
5674 and put in a C-d to force the buffer through. */
5675 this = ptr - buf;
5676 }
93b4f699 5677
0daad115
GM
5678 /* Send this batch, using one or more write calls. */
5679 while (this > 0)
5680 {
60f0fb11 5681 int outfd = p->outfd;
0daad115 5682 old_sigpipe = (SIGTYPE (*) ()) signal (SIGPIPE, send_process_trap);
e690ca94
KS
5683#ifdef DATAGRAM_SOCKETS
5684 if (DATAGRAM_CHAN_P (outfd))
5685 {
5686 rv = sendto (outfd, (char *) buf, this,
5687 0, datagram_address[outfd].sa,
5688 datagram_address[outfd].len);
5689 if (rv < 0 && errno == EMSGSIZE)
c8f44005
RS
5690 {
5691 signal (SIGPIPE, old_sigpipe);
5692 report_file_error ("sending datagram",
5693 Fcons (proc, Qnil));
5694 }
e690ca94
KS
5695 }
5696 else
5697#endif
6b61353c
KH
5698 {
5699 rv = emacs_write (outfd, (char *) buf, this);
5700#ifdef ADAPTIVE_READ_BUFFERING
60f0fb11
SM
5701 if (p->read_output_delay > 0
5702 && p->adaptive_read_buffering == 1)
6b61353c 5703 {
60f0fb11 5704 p->read_output_delay = 0;
6b61353c 5705 process_output_delay_count--;
60f0fb11 5706 p->read_output_skip = 0;
6b61353c
KH
5707 }
5708#endif
5709 }
0daad115 5710 signal (SIGPIPE, old_sigpipe);
4556b700 5711
0daad115
GM
5712 if (rv < 0)
5713 {
5714 if (0
d0d6b7c5 5715#ifdef EWOULDBLOCK
0daad115 5716 || errno == EWOULDBLOCK
d0d6b7c5
JB
5717#endif
5718#ifdef EAGAIN
0daad115 5719 || errno == EAGAIN
d0d6b7c5 5720#endif
0daad115 5721 )
177c0ea7 5722 /* Buffer is full. Wait, accepting input;
0daad115
GM
5723 that may allow the program
5724 to finish doing output and read more. */
5725 {
0daad115 5726 int offset = 0;
4556b700 5727
3433b6bd 5728#ifdef BROKEN_PTY_READ_AFTER_EAGAIN
0daad115
GM
5729 /* A gross hack to work around a bug in FreeBSD.
5730 In the following sequence, read(2) returns
5731 bogus data:
5732
5733 write(2) 1022 bytes
5734 write(2) 954 bytes, get EAGAIN
5735 read(2) 1024 bytes in process_read_output
5736 read(2) 11 bytes in process_read_output
5737
5738 That is, read(2) returns more bytes than have
5739 ever been written successfully. The 1033 bytes
5740 read are the 1022 bytes written successfully
5741 after processing (for example with CRs added if
5742 the terminal is set up that way which it is
5743 here). The same bytes will be seen again in a
5744 later read(2), without the CRs. */
177c0ea7 5745
0daad115
GM
5746 if (errno == EAGAIN)
5747 {
5748 int flags = FWRITE;
60f0fb11 5749 ioctl (p->outfd, TIOCFLUSH, &flags);
0daad115 5750 }
3433b6bd 5751#endif /* BROKEN_PTY_READ_AFTER_EAGAIN */
177c0ea7 5752
0daad115
GM
5753 /* Running filters might relocate buffers or strings.
5754 Arrange to relocate BUF. */
b5bfebec
AS
5755 if (CODING_REQUIRE_ENCODING (coding))
5756 offset = buf - SDATA (coding->dst_object);
5757 else if (BUFFERP (object))
0daad115
GM
5758 offset = BUF_PTR_BYTE_POS (XBUFFER (object), buf);
5759 else if (STRINGP (object))
d5db4077 5760 offset = buf - SDATA (object);
0daad115 5761
f3e6605c 5762#ifdef EMACS_HAS_USECS
214d6069 5763 wait_reading_process_output (0, 20000, 0, 0, Qnil, NULL, 0);
f3e6605c 5764#else
214d6069 5765 wait_reading_process_output (1, 0, 0, 0, Qnil, NULL, 0);
f3e6605c 5766#endif
4556b700 5767
b5bfebec
AS
5768 if (CODING_REQUIRE_ENCODING (coding))
5769 buf = offset + SDATA (coding->dst_object);
5770 else if (BUFFERP (object))
0daad115
GM
5771 buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
5772 else if (STRINGP (object))
d5db4077 5773 buf = offset + SDATA (object);
4556b700 5774
0daad115
GM
5775 rv = 0;
5776 }
5777 else
5778 /* This is a real error. */
5779 report_file_error ("writing to process", Fcons (proc, Qnil));
5780 }
5781 buf += rv;
5782 len -= rv;
5783 this -= rv;
5784 }
f76475ad 5785
d888760c 5786 /* If we sent just part of the string, put in an EOF (C-d)
0daad115
GM
5787 to force it through, before we send the rest. */
5788 if (len > 0)
5789 Fprocess_send_eof (proc);
5790 }
5791 }
d0d6b7c5
JB
5792 else
5793 {
0ff69270 5794 signal (SIGPIPE, old_sigpipe);
0daad115 5795 proc = process_sent_to;
6b61353c 5796 p = XPROCESS (proc);
6bfd98e7 5797 p->raw_status_new = 0;
6b61353c 5798 p->status = Fcons (Qexit, Fcons (make_number (256), Qnil));
60f0fb11 5799 p->tick = ++process_tick;
d0d6b7c5 5800 deactivate_process (proc);
6b61353c 5801 error ("SIGPIPE raised on process %s; closed it", SDATA (p->name));
d0d6b7c5 5802 }
6044e593
RS
5803
5804 UNGCPRO;
d0d6b7c5
JB
5805}
5806
5807DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region,
fdb82f93
PJ
5808 3, 3, 0,
5809 doc: /* Send current contents of region as input to PROCESS.
5810PROCESS may be a process, a buffer, the name of a process or buffer, or
5811nil, indicating the current buffer's process.
5812Called from program, takes three arguments, PROCESS, START and END.
5813If the region is more than 500 characters long,
5814it is sent in several bunches. This may happen even for shorter regions.
5815Output from processes can arrive in between bunches. */)
5816 (process, start, end)
d0d6b7c5
JB
5817 Lisp_Object process, start, end;
5818{
5819 Lisp_Object proc;
1b234f64 5820 int start1, end1;
d0d6b7c5
JB
5821
5822 proc = get_process (process);
5823 validate_region (&start, &end);
1b234f64
KH
5824
5825 if (XINT (start) < GPT && XINT (end) > GPT)
5826 move_gap (XINT (start));
5827
5828 start1 = CHAR_TO_BYTE (XINT (start));
5829 end1 = CHAR_TO_BYTE (XINT (end));
5830 send_process (proc, BYTE_POS_ADDR (start1), end1 - start1,
5831 Fcurrent_buffer ());
5832
d0d6b7c5
JB
5833 return Qnil;
5834}
5835
5836DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string,
fdb82f93
PJ
5837 2, 2, 0,
5838 doc: /* Send PROCESS the contents of STRING as input.
5839PROCESS may be a process, a buffer, the name of a process or buffer, or
5840nil, indicating the current buffer's process.
5841If STRING is more than 500 characters long,
5842it is sent in several bunches. This may happen even for shorter strings.
5843Output from processes can arrive in between bunches. */)
5844 (process, string)
d0d6b7c5
JB
5845 Lisp_Object process, string;
5846{
5847 Lisp_Object proc;
b7826503 5848 CHECK_STRING (string);
d0d6b7c5 5849 proc = get_process (process);
1b234f64
KH
5850 send_process (proc, SDATA (string),
5851 SBYTES (string), string);
d0d6b7c5
JB
5852 return Qnil;
5853}
5854\f
16782258
JD
5855/* Return the foreground process group for the tty/pty that
5856 the process P uses. */
5857static int
5858emacs_get_tty_pgrp (p)
5859 struct Lisp_Process *p;
5860{
5861 int gid = -1;
5862
f3d9532b 5863#ifdef TIOCGPGRP
60f0fb11 5864 if (ioctl (p->infd, TIOCGPGRP, &gid) == -1 && ! NILP (p->tty_name))
16782258
JD
5865 {
5866 int fd;
5867 /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
5868 master side. Try the slave side. */
eef60308 5869 fd = emacs_open (SDATA (p->tty_name), O_RDONLY, 0);
16782258
JD
5870
5871 if (fd != -1)
5872 {
5873 ioctl (fd, TIOCGPGRP, &gid);
5874 emacs_close (fd);
5875 }
5876 }
5877#endif /* defined (TIOCGPGRP ) */
5878
5879 return gid;
5880}
5881
b81ea5ef
RS
5882DEFUN ("process-running-child-p", Fprocess_running_child_p,
5883 Sprocess_running_child_p, 0, 1, 0,
fdb82f93
PJ
5884 doc: /* Return t if PROCESS has given the terminal to a child.
5885If the operating system does not make it possible to find out,
5886return t unconditionally. */)
5887 (process)
b81ea5ef
RS
5888 Lisp_Object process;
5889{
5890 /* Initialize in case ioctl doesn't exist or gives an error,
5891 in a way that will cause returning t. */
16782258 5892 int gid;
b81ea5ef
RS
5893 Lisp_Object proc;
5894 struct Lisp_Process *p;
5895
5896 proc = get_process (process);
5897 p = XPROCESS (proc);
5898
d888760c 5899 if (!EQ (p->type, Qreal))
b81ea5ef 5900 error ("Process %s is not a subprocess",
d5db4077 5901 SDATA (p->name));
60f0fb11 5902 if (p->infd < 0)
b81ea5ef 5903 error ("Process %s is not active",
d5db4077 5904 SDATA (p->name));
b81ea5ef 5905
16782258 5906 gid = emacs_get_tty_pgrp (p);
b81ea5ef 5907
6bfd98e7 5908 if (gid == p->pid)
b81ea5ef
RS
5909 return Qnil;
5910 return Qt;
5911}
5912\f
d0d6b7c5 5913/* send a signal number SIGNO to PROCESS.
b81ea5ef
RS
5914 If CURRENT_GROUP is t, that means send to the process group
5915 that currently owns the terminal being used to communicate with PROCESS.
d0d6b7c5 5916 This is used for various commands in shell mode.
b81ea5ef
RS
5917 If CURRENT_GROUP is lambda, that means send to the process group
5918 that currently owns the terminal, but only if it is NOT the shell itself.
5919
d0d6b7c5 5920 If NOMSG is zero, insert signal-announcements into process's buffers
b0310da4
JB
5921 right away.
5922
5923 If we can, we try to signal PROCESS by sending control characters
e333e864 5924 down the pty. This allows us to signal inferiors who have changed
b0310da4 5925 their uid, for which killpg would return an EPERM error. */
d0d6b7c5 5926
f9738840 5927static void
d0d6b7c5
JB
5928process_send_signal (process, signo, current_group, nomsg)
5929 Lisp_Object process;
5930 int signo;
5931 Lisp_Object current_group;
5932 int nomsg;
5933{
5934 Lisp_Object proc;
5935 register struct Lisp_Process *p;
5936 int gid;
5937 int no_pgrp = 0;
5938
5939 proc = get_process (process);
5940 p = XPROCESS (proc);
5941
d888760c 5942 if (!EQ (p->type, Qreal))
d0d6b7c5 5943 error ("Process %s is not a subprocess",
d5db4077 5944 SDATA (p->name));
60f0fb11 5945 if (p->infd < 0)
d0d6b7c5 5946 error ("Process %s is not active",
d5db4077 5947 SDATA (p->name));
d0d6b7c5 5948
60f0fb11 5949 if (!p->pty_flag)
d0d6b7c5
JB
5950 current_group = Qnil;
5951
d0d6b7c5 5952 /* If we are using pgrps, get a pgrp number and make it negative. */
2af70a0c
RS
5953 if (NILP (current_group))
5954 /* Send the signal to the shell's process group. */
6bfd98e7 5955 gid = p->pid;
2af70a0c 5956 else
d0d6b7c5 5957 {
b0310da4 5958#ifdef SIGNALS_VIA_CHARACTERS
d0d6b7c5
JB
5959 /* If possible, send signals to the entire pgrp
5960 by sending an input character to it. */
b0310da4 5961
6be429b1 5962 /* TERMIOS is the latest and bestest, and seems most likely to
63136da6 5963 work. If the system has it, use it. */
6be429b1
JB
5964#ifdef HAVE_TERMIOS
5965 struct termios t;
7f916a36
RS
5966 cc_t *sig_char = NULL;
5967
60f0fb11 5968 tcgetattr (p->infd, &t);
6be429b1
JB
5969
5970 switch (signo)
5971 {
5972 case SIGINT:
7f916a36
RS
5973 sig_char = &t.c_cc[VINTR];
5974 break;
6be429b1
JB
5975
5976 case SIGQUIT:
7f916a36
RS
5977 sig_char = &t.c_cc[VQUIT];
5978 break;
6be429b1
JB
5979
5980 case SIGTSTP:
d0adf46f 5981#if defined (VSWTCH) && !defined (PREFER_VSUSP)
7f916a36 5982 sig_char = &t.c_cc[VSWTCH];
6be429b1 5983#else
7f916a36 5984 sig_char = &t.c_cc[VSUSP];
6be429b1 5985#endif
7f916a36 5986 break;
6be429b1
JB
5987 }
5988
ca4313d5 5989 if (sig_char && *sig_char != CDISABLE)
f1c206fc
RS
5990 {
5991 send_process (proc, sig_char, 1, Qnil);
5992 return;
5993 }
5994 /* If we can't send the signal with a character,
5995 fall through and send it another way. */
6be429b1
JB
5996#else /* ! HAVE_TERMIOS */
5997
b0310da4
JB
5998 /* On Berkeley descendants, the following IOCTL's retrieve the
5999 current control characters. */
d0d6b7c5 6000#if defined (TIOCGLTC) && defined (TIOCGETC)
b0310da4 6001
d0d6b7c5
JB
6002 struct tchars c;
6003 struct ltchars lc;
6004
6005 switch (signo)
6006 {
6007 case SIGINT:
60f0fb11 6008 ioctl (p->infd, TIOCGETC, &c);
4556b700 6009 send_process (proc, &c.t_intrc, 1, Qnil);
f9738840 6010 return;
d0d6b7c5 6011 case SIGQUIT:
60f0fb11 6012 ioctl (p->infd, TIOCGETC, &c);
4556b700 6013 send_process (proc, &c.t_quitc, 1, Qnil);
f9738840 6014 return;
0ad77c54 6015#ifdef SIGTSTP
d0d6b7c5 6016 case SIGTSTP:
60f0fb11 6017 ioctl (p->infd, TIOCGLTC, &lc);
4556b700 6018 send_process (proc, &lc.t_suspc, 1, Qnil);
f9738840 6019 return;
b0310da4 6020#endif /* ! defined (SIGTSTP) */
d0d6b7c5 6021 }
b0310da4
JB
6022
6023#else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
6024
6025 /* On SYSV descendants, the TCGETA ioctl retrieves the current control
6026 characters. */
6027#ifdef TCGETA
d0d6b7c5
JB
6028 struct termio t;
6029 switch (signo)
6030 {
6031 case SIGINT:
60f0fb11 6032 ioctl (p->infd, TCGETA, &t);
4556b700 6033 send_process (proc, &t.c_cc[VINTR], 1, Qnil);
f9738840 6034 return;
d0d6b7c5 6035 case SIGQUIT:
60f0fb11 6036 ioctl (p->infd, TCGETA, &t);
4556b700 6037 send_process (proc, &t.c_cc[VQUIT], 1, Qnil);
f9738840 6038 return;
7d79e3b4 6039#ifdef SIGTSTP
d0d6b7c5 6040 case SIGTSTP:
60f0fb11 6041 ioctl (p->infd, TCGETA, &t);
4556b700 6042 send_process (proc, &t.c_cc[VSWTCH], 1, Qnil);
f9738840 6043 return;
b0310da4 6044#endif /* ! defined (SIGTSTP) */
d0d6b7c5 6045 }
b0310da4
JB
6046#else /* ! defined (TCGETA) */
6047 Your configuration files are messed up.
6048 /* If your system configuration files define SIGNALS_VIA_CHARACTERS,
6049 you'd better be using one of the alternatives above! */
6050#endif /* ! defined (TCGETA) */
6051#endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
eef60308 6052 /* In this case, the code above should alway return. */
20505336 6053 abort ();
f1c206fc
RS
6054#endif /* ! defined HAVE_TERMIOS */
6055
6056 /* The code above may fall through if it can't
6057 handle the signal. */
20505336 6058#endif /* defined (SIGNALS_VIA_CHARACTERS) */
d0d6b7c5 6059
177c0ea7 6060#ifdef TIOCGPGRP
2af70a0c 6061 /* Get the current pgrp using the tty itself, if we have that.
d0d6b7c5
JB
6062 Otherwise, use the pty to get the pgrp.
6063 On pfa systems, saka@pfu.fujitsu.co.JP writes:
b0310da4
JB
6064 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
6065 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
d0d6b7c5
JB
6066 His patch indicates that if TIOCGPGRP returns an error, then
6067 we should just assume that p->pid is also the process group id. */
d0d6b7c5 6068
16782258 6069 gid = emacs_get_tty_pgrp (p);
f3d9532b 6070
16782258
JD
6071 if (gid == -1)
6072 /* If we can't get the information, assume
6073 the shell owns the tty. */
6bfd98e7 6074 gid = p->pid;
2af70a0c
RS
6075
6076 /* It is not clear whether anything really can set GID to -1.
6077 Perhaps on some system one of those ioctls can or could do so.
6078 Or perhaps this is vestigial. */
d0d6b7c5
JB
6079 if (gid == -1)
6080 no_pgrp = 1;
b0310da4 6081#else /* ! defined (TIOCGPGRP ) */
301c3fe4
JB
6082 /* Can't select pgrps on this system, so we know that
6083 the child itself heads the pgrp. */
6bfd98e7 6084 gid = p->pid;
301c3fe4 6085#endif /* ! defined (TIOCGPGRP ) */
b81ea5ef
RS
6086
6087 /* If current_group is lambda, and the shell owns the terminal,
6088 don't send any signal. */
6bfd98e7 6089 if (EQ (current_group, Qlambda) && gid == p->pid)
b81ea5ef 6090 return;
d0d6b7c5 6091 }
d0d6b7c5
JB
6092
6093 switch (signo)
6094 {
6095#ifdef SIGCONT
6096 case SIGCONT:
6bfd98e7 6097 p->raw_status_new = 0;
d0d6b7c5 6098 p->status = Qrun;
60f0fb11 6099 p->tick = ++process_tick;
d0d6b7c5 6100 if (!nomsg)
ff6daed3 6101 status_notify (NULL);
d0d6b7c5 6102 break;
301c3fe4 6103#endif /* ! defined (SIGCONT) */
d0d6b7c5 6104 case SIGINT:
d0d6b7c5 6105 case SIGQUIT:
d0d6b7c5 6106 case SIGKILL:
60f0fb11 6107 flush_pending_output (p->infd);
d0d6b7c5
JB
6108 break;
6109 }
6110
6111 /* If we don't have process groups, send the signal to the immediate
6112 subprocess. That isn't really right, but it's better than any
6113 obvious alternative. */
6114 if (no_pgrp)
6115 {
6bfd98e7 6116 kill (p->pid, signo);
d0d6b7c5
JB
6117 return;
6118 }
6119
6120 /* gid may be a pid, or minus a pgrp's number */
6121#ifdef TIOCSIGSEND
6122 if (!NILP (current_group))
16782258 6123 {
60f0fb11 6124 if (ioctl (p->infd, TIOCSIGSEND, signo) == -1)
16782258
JD
6125 EMACS_KILLPG (gid, signo);
6126 }
d0d6b7c5
JB
6127 else
6128 {
6bfd98e7 6129 gid = - p->pid;
d0d6b7c5
JB
6130 kill (gid, signo);
6131 }
301c3fe4 6132#else /* ! defined (TIOCSIGSEND) */
2af70a0c 6133 EMACS_KILLPG (gid, signo);
301c3fe4 6134#endif /* ! defined (TIOCSIGSEND) */
d0d6b7c5
JB
6135}
6136
6137DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0,
fdb82f93
PJ
6138 doc: /* Interrupt process PROCESS.
6139PROCESS may be a process, a buffer, or the name of a process or buffer.
f3d9532b 6140No arg or nil means current buffer's process.
fdb82f93
PJ
6141Second arg CURRENT-GROUP non-nil means send signal to
6142the current process-group of the process's controlling terminal
6143rather than to the process's own process group.
6144If the process is a shell, this means interrupt current subjob
6145rather than the shell.
6146
6147If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
6148don't send the signal. */)
6149 (process, current_group)
d0d6b7c5
JB
6150 Lisp_Object process, current_group;
6151{
6152 process_send_signal (process, SIGINT, current_group, 0);
6153 return process;
6154}
6155
6156DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0,
fdb82f93
PJ
6157 doc: /* Kill process PROCESS. May be process or name of one.
6158See function `interrupt-process' for more details on usage. */)
6159 (process, current_group)
d0d6b7c5
JB
6160 Lisp_Object process, current_group;
6161{
6162 process_send_signal (process, SIGKILL, current_group, 0);
6163 return process;
6164}
6165
6166DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0,
fdb82f93
PJ
6167 doc: /* Send QUIT signal to process PROCESS. May be process or name of one.
6168See function `interrupt-process' for more details on usage. */)
6169 (process, current_group)
d0d6b7c5
JB
6170 Lisp_Object process, current_group;
6171{
6172 process_send_signal (process, SIGQUIT, current_group, 0);
6173 return process;
6174}
6175
6176DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0,
fdb82f93 6177 doc: /* Stop process PROCESS. May be process or name of one.
177c0ea7 6178See function `interrupt-process' for more details on usage.
d888760c
GM
6179If PROCESS is a network or serial process, inhibit handling of incoming
6180traffic. */)
fdb82f93 6181 (process, current_group)
d0d6b7c5
JB
6182 Lisp_Object process, current_group;
6183{
e690ca94 6184#ifdef HAVE_SOCKETS
d888760c 6185 if (PROCESSP (process) && (NETCONN_P (process) || SERIALCONN_P (process)))
e690ca94
KS
6186 {
6187 struct Lisp_Process *p;
177c0ea7 6188
e690ca94
KS
6189 p = XPROCESS (process);
6190 if (NILP (p->command)
60f0fb11 6191 && p->infd >= 0)
e690ca94 6192 {
60f0fb11
SM
6193 FD_CLR (p->infd, &input_wait_mask);
6194 FD_CLR (p->infd, &non_keyboard_wait_mask);
e690ca94
KS
6195 }
6196 p->command = Qt;
6197 return process;
6198 }
6199#endif
d0d6b7c5 6200#ifndef SIGTSTP
a9a73d6c 6201 error ("No SIGTSTP support");
d0d6b7c5
JB
6202#else
6203 process_send_signal (process, SIGTSTP, current_group, 0);
6204#endif
6205 return process;
6206}
6207
6208DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0,
fdb82f93 6209 doc: /* Continue process PROCESS. May be process or name of one.
177c0ea7 6210See function `interrupt-process' for more details on usage.
d888760c
GM
6211If PROCESS is a network or serial process, resume handling of incoming
6212traffic. */)
fdb82f93 6213 (process, current_group)
d0d6b7c5
JB
6214 Lisp_Object process, current_group;
6215{
e690ca94 6216#ifdef HAVE_SOCKETS
d888760c 6217 if (PROCESSP (process) && (NETCONN_P (process) || SERIALCONN_P (process)))
e690ca94
KS
6218 {
6219 struct Lisp_Process *p;
6220
6221 p = XPROCESS (process);
6222 if (EQ (p->command, Qt)
60f0fb11 6223 && p->infd >= 0
e690ca94
KS
6224 && (!EQ (p->filter, Qt) || EQ (p->status, Qlisten)))
6225 {
60f0fb11
SM
6226 FD_SET (p->infd, &input_wait_mask);
6227 FD_SET (p->infd, &non_keyboard_wait_mask);
d888760c
GM
6228#ifdef WINDOWSNT
6229 if (fd_info[ p->infd ].flags & FILE_SERIAL)
6230 PurgeComm (fd_info[ p->infd ].hnd, PURGE_RXABORT | PURGE_RXCLEAR);
6231#endif
6232#ifdef HAVE_TERMIOS
6233 tcflush (p->infd, TCIFLUSH);
6234#endif
e690ca94
KS
6235 }
6236 p->command = Qnil;
6237 return process;
6238 }
6239#endif
d0d6b7c5
JB
6240#ifdef SIGCONT
6241 process_send_signal (process, SIGCONT, current_group, 0);
6242#else
a9a73d6c 6243 error ("No SIGCONT support");
d0d6b7c5
JB
6244#endif
6245 return process;
6246}
6247
6248DEFUN ("signal-process", Fsignal_process, Ssignal_process,
cdd5ea86
KS
6249 2, 2, "sProcess (name or number): \nnSignal code: ",
6250 doc: /* Send PROCESS the signal with code SIGCODE.
648f6edf 6251PROCESS may also be a number specifying the process id of the
cdd5ea86
KS
6252process to signal; in this case, the process need not be a child of
6253this Emacs.
fdb82f93 6254SIGCODE may be an integer, or a symbol whose name is a signal name. */)
cdd5ea86
KS
6255 (process, sigcode)
6256 Lisp_Object process, sigcode;
d0d6b7c5 6257{
6bfd98e7 6258 pid_t pid;
cdd5ea86
KS
6259
6260 if (INTEGERP (process))
6261 {
6bfd98e7
SM
6262 pid = XINT (process);
6263 goto got_it;
6264 }
6265
6266 if (FLOATP (process))
6267 {
648f6edf 6268 pid = (pid_t) XFLOAT_DATA (process);
cdd5ea86
KS
6269 goto got_it;
6270 }
6271
6272 if (STRINGP (process))
6273 {
6274 Lisp_Object tem;
6275 if (tem = Fget_process (process), NILP (tem))
6276 {
6bfd98e7
SM
6277 pid = XINT (Fstring_to_number (process, make_number (10)));
6278 if (pid > 0)
cdd5ea86
KS
6279 goto got_it;
6280 }
6281 process = tem;
6282 }
6283 else
6284 process = get_process (process);
177c0ea7 6285
cdd5ea86
KS
6286 if (NILP (process))
6287 return process;
6288
6289 CHECK_PROCESS (process);
6290 pid = XPROCESS (process)->pid;
6bfd98e7 6291 if (pid <= 0)
cdd5ea86
KS
6292 error ("Cannot signal process %s", SDATA (XPROCESS (process)->name));
6293
6294 got_it:
4766242d 6295
59324b1d 6296#define parse_signal(NAME, VALUE) \
05131107 6297 else if (!xstrcasecmp (name, NAME)) \
4766242d
RS
6298 XSETINT (sigcode, VALUE)
6299
6300 if (INTEGERP (sigcode))
6301 ;
6302 else
6303 {
6304 unsigned char *name;
6305
b7826503 6306 CHECK_SYMBOL (sigcode);
d5db4077 6307 name = SDATA (SYMBOL_NAME (sigcode));
4766242d 6308
ec0f21f4 6309 if (!strncmp(name, "SIG", 3) || !strncmp(name, "sig", 3))
675984ed
KS
6310 name += 3;
6311
4766242d
RS
6312 if (0)
6313 ;
59324b1d
KS
6314#ifdef SIGUSR1
6315 parse_signal ("usr1", SIGUSR1);
6316#endif
6317#ifdef SIGUSR2
6318 parse_signal ("usr2", SIGUSR2);
6319#endif
6320#ifdef SIGTERM
6321 parse_signal ("term", SIGTERM);
6322#endif
4766242d 6323#ifdef SIGHUP
59324b1d 6324 parse_signal ("hup", SIGHUP);
4766242d
RS
6325#endif
6326#ifdef SIGINT
59324b1d 6327 parse_signal ("int", SIGINT);
4766242d
RS
6328#endif
6329#ifdef SIGQUIT
59324b1d 6330 parse_signal ("quit", SIGQUIT);
4766242d
RS
6331#endif
6332#ifdef SIGILL
59324b1d 6333 parse_signal ("ill", SIGILL);
4766242d
RS
6334#endif
6335#ifdef SIGABRT
59324b1d 6336 parse_signal ("abrt", SIGABRT);
4766242d
RS
6337#endif
6338#ifdef SIGEMT
59324b1d 6339 parse_signal ("emt", SIGEMT);
4766242d
RS
6340#endif
6341#ifdef SIGKILL
59324b1d 6342 parse_signal ("kill", SIGKILL);
4766242d
RS
6343#endif
6344#ifdef SIGFPE
59324b1d 6345 parse_signal ("fpe", SIGFPE);
4766242d
RS
6346#endif
6347#ifdef SIGBUS
59324b1d 6348 parse_signal ("bus", SIGBUS);
4766242d
RS
6349#endif
6350#ifdef SIGSEGV
59324b1d 6351 parse_signal ("segv", SIGSEGV);
4766242d
RS
6352#endif
6353#ifdef SIGSYS
59324b1d 6354 parse_signal ("sys", SIGSYS);
4766242d
RS
6355#endif
6356#ifdef SIGPIPE
59324b1d 6357 parse_signal ("pipe", SIGPIPE);
4766242d
RS
6358#endif
6359#ifdef SIGALRM
59324b1d 6360 parse_signal ("alrm", SIGALRM);
4766242d
RS
6361#endif
6362#ifdef SIGURG
59324b1d 6363 parse_signal ("urg", SIGURG);
4766242d
RS
6364#endif
6365#ifdef SIGSTOP
59324b1d 6366 parse_signal ("stop", SIGSTOP);
4766242d
RS
6367#endif
6368#ifdef SIGTSTP
59324b1d 6369 parse_signal ("tstp", SIGTSTP);
4766242d
RS
6370#endif
6371#ifdef SIGCONT
59324b1d 6372 parse_signal ("cont", SIGCONT);
4766242d
RS
6373#endif
6374#ifdef SIGCHLD
59324b1d 6375 parse_signal ("chld", SIGCHLD);
4766242d
RS
6376#endif
6377#ifdef SIGTTIN
59324b1d 6378 parse_signal ("ttin", SIGTTIN);
4766242d
RS
6379#endif
6380#ifdef SIGTTOU
59324b1d 6381 parse_signal ("ttou", SIGTTOU);
4766242d
RS
6382#endif
6383#ifdef SIGIO
59324b1d 6384 parse_signal ("io", SIGIO);
4766242d
RS
6385#endif
6386#ifdef SIGXCPU
59324b1d 6387 parse_signal ("xcpu", SIGXCPU);
4766242d
RS
6388#endif
6389#ifdef SIGXFSZ
59324b1d 6390 parse_signal ("xfsz", SIGXFSZ);
4766242d
RS
6391#endif
6392#ifdef SIGVTALRM
59324b1d 6393 parse_signal ("vtalrm", SIGVTALRM);
4766242d
RS
6394#endif
6395#ifdef SIGPROF
59324b1d 6396 parse_signal ("prof", SIGPROF);
4766242d
RS
6397#endif
6398#ifdef SIGWINCH
59324b1d 6399 parse_signal ("winch", SIGWINCH);
4766242d
RS
6400#endif
6401#ifdef SIGINFO
59324b1d 6402 parse_signal ("info", SIGINFO);
4766242d
RS
6403#endif
6404 else
9fa195a2 6405 error ("Undefined signal name %s", name);
4766242d
RS
6406 }
6407
59324b1d 6408#undef parse_signal
4766242d 6409
6bfd98e7 6410 return make_number (kill (pid, XINT (sigcode)));
d0d6b7c5
JB
6411}
6412
6413DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0,
fdb82f93
PJ
6414 doc: /* Make PROCESS see end-of-file in its input.
6415EOF comes after any text already sent to it.
6416PROCESS may be a process, a buffer, the name of a process or buffer, or
6417nil, indicating the current buffer's process.
6418If PROCESS is a network connection, or is a process communicating
6419through a pipe (as opposed to a pty), then you cannot send any more
d888760c
GM
6420text to PROCESS after you call this function.
6421If PROCESS is a serial process, wait until all output written to the
6422process has been transmitted to the serial port. */)
fdb82f93 6423 (process)
d0d6b7c5
JB
6424 Lisp_Object process;
6425{
6426 Lisp_Object proc;
de7fbd09 6427 struct coding_system *coding;
d0d6b7c5 6428
e690ca94
KS
6429 if (DATAGRAM_CONN_P (process))
6430 return process;
6431
d0d6b7c5 6432 proc = get_process (process);
60f0fb11 6433 coding = proc_encode_coding_system[XPROCESS (proc)->outfd];
577d03d5
RS
6434
6435 /* Make sure the process is really alive. */
6bfd98e7 6436 if (XPROCESS (proc)->raw_status_new)
577d03d5
RS
6437 update_status (XPROCESS (proc));
6438 if (! EQ (XPROCESS (proc)->status, Qrun))
d5db4077 6439 error ("Process %s not running", SDATA (XPROCESS (proc)->name));
577d03d5 6440
de7fbd09
KH
6441 if (CODING_REQUIRE_FLUSHING (coding))
6442 {
6443 coding->mode |= CODING_MODE_LAST_BLOCK;
6444 send_process (proc, "", 0, Qnil);
6445 }
6446
60f0fb11 6447 if (XPROCESS (proc)->pty_flag)
4556b700 6448 send_process (proc, "\004", 1, Qnil);
a879ee6f 6449 else if (EQ (XPROCESS (proc)->type, Qserial))
d888760c
GM
6450 {
6451#ifdef HAVE_TERMIOS
6452 if (tcdrain (XPROCESS (proc)->outfd) != 0)
6453 error ("tcdrain() failed: %s", emacs_strerror (errno));
6454#endif
6455 /* Do nothing on Windows because writes are blocking. */
6456 }
d0d6b7c5
JB
6457 else
6458 {
4525f571
RS
6459 int old_outfd, new_outfd;
6460
93853f3d 6461#ifdef HAVE_SHUTDOWN
02f55c4b
RS
6462 /* If this is a network connection, or socketpair is used
6463 for communication with the subprocess, call shutdown to cause EOF.
6464 (In some old system, shutdown to socketpair doesn't work.
6465 Then we just can't win.) */
a879ee6f 6466 if (EQ (XPROCESS (proc)->type, Qnetwork)
60f0fb11
SM
6467 || XPROCESS (proc)->outfd == XPROCESS (proc)->infd)
6468 shutdown (XPROCESS (proc)->outfd, 1);
02f55c4b 6469 /* In case of socketpair, outfd == infd, so don't close it. */
60f0fb11
SM
6470 if (XPROCESS (proc)->outfd != XPROCESS (proc)->infd)
6471 emacs_close (XPROCESS (proc)->outfd);
93853f3d 6472#else /* not HAVE_SHUTDOWN */
60f0fb11 6473 emacs_close (XPROCESS (proc)->outfd);
93853f3d 6474#endif /* not HAVE_SHUTDOWN */
68c45bf0 6475 new_outfd = emacs_open (NULL_DEVICE, O_WRONLY, 0);
27448380
RS
6476 if (new_outfd < 0)
6477 abort ();
60f0fb11 6478 old_outfd = XPROCESS (proc)->outfd;
4525f571
RS
6479
6480 if (!proc_encode_coding_system[new_outfd])
6481 proc_encode_coding_system[new_outfd]
6482 = (struct coding_system *) xmalloc (sizeof (struct coding_system));
6483 bcopy (proc_encode_coding_system[old_outfd],
6484 proc_encode_coding_system[new_outfd],
6485 sizeof (struct coding_system));
6486 bzero (proc_encode_coding_system[old_outfd],
6487 sizeof (struct coding_system));
6488
60f0fb11 6489 XPROCESS (proc)->outfd = new_outfd;
d0d6b7c5 6490 }
d0d6b7c5
JB
6491 return process;
6492}
6493
6494/* Kill all processes associated with `buffer'.
3fed8ad5 6495 If `buffer' is nil, kill all processes */
d0d6b7c5 6496
6b53bb85 6497void
d0d6b7c5
JB
6498kill_buffer_processes (buffer)
6499 Lisp_Object buffer;
6500{
6501 Lisp_Object tail, proc;
6502
8e50cc2d 6503 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
d0d6b7c5 6504 {
70949dac 6505 proc = XCDR (XCAR (tail));
8e50cc2d 6506 if (PROCESSP (proc)
d0d6b7c5
JB
6507 && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer)))
6508 {
d888760c 6509 if (NETCONN_P (proc) || SERIALCONN_P (proc))
e1ab4959 6510 Fdelete_process (proc);
60f0fb11 6511 else if (XPROCESS (proc)->infd >= 0)
d0d6b7c5
JB
6512 process_send_signal (proc, SIGHUP, Qnil, 1);
6513 }
6514 }
6515}
6516\f
3fed8ad5
GM
6517/* On receipt of a signal that a child status has changed, loop asking
6518 about children with changed statuses until the system says there
6519 are no more.
177c0ea7 6520
3fed8ad5
GM
6521 All we do is change the status; we do not run sentinels or print
6522 notifications. That is saved for the next time keyboard input is
6523 done, in order to avoid timing errors.
6524
6525 ** WARNING: this can be called during garbage collection.
6526 Therefore, it must not be fooled by the presence of mark bits in
6527 Lisp objects.
6528
6529 ** USG WARNING: Although it is not obvious from the documentation
6530 in signal(2), on a USG system the SIGCLD handler MUST NOT call
6531 signal() before executing at least one wait(), otherwise the
6532 handler will be called again, resulting in an infinite loop. The
6533 relevant portion of the documentation reads "SIGCLD signals will be
6534 queued and the signal-catching function will be continually
6535 reentered until the queue is empty". Invoking signal() causes the
6536 kernel to reexamine the SIGCLD queue. Fred Fish, UniSoft Systems
6b61353c
KH
6537 Inc.
6538
6539 ** Malloc WARNING: This should never call malloc either directly or
6540 indirectly; if it does, that is a bug */
d0d6b7c5 6541
a5cfdda8 6542#ifdef SIGCHLD
d0d6b7c5
JB
6543SIGTYPE
6544sigchld_handler (signo)
6545 int signo;
6546{
6547 int old_errno = errno;
6548 Lisp_Object proc;
6549 register struct Lisp_Process *p;
6be429b1 6550 extern EMACS_TIME *input_available_clear_time;
d0d6b7c5 6551
333f1b6f
JD
6552 SIGNAL_THREAD_CHECK (signo);
6553
d0d6b7c5
JB
6554 while (1)
6555 {
8e050977 6556 pid_t pid;
bba104c1 6557 int w;
d0d6b7c5
JB
6558 Lisp_Object tail;
6559
6560#ifdef WNOHANG
6561#ifndef WUNTRACED
6562#define WUNTRACED 0
6563#endif /* no WUNTRACED */
6564 /* Keep trying to get a status until we get a definitive result. */
e7d4a904 6565 do
63136da6 6566 {
29ed4d51
CY
6567 errno = 0;
6568 pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
29ed4d51 6569 }
e7d4a904 6570 while (pid < 0 && errno == EINTR);
d0d6b7c5
JB
6571
6572 if (pid <= 0)
6573 {
3fed8ad5
GM
6574 /* PID == 0 means no processes found, PID == -1 means a real
6575 failure. We have done all our job, so return. */
d0d6b7c5
JB
6576
6577 /* USG systems forget handlers when they are used;
6578 must reestablish each time */
3c0ee47b 6579#if defined (USG) && !defined (POSIX_SIGNALS)
d0d6b7c5 6580 signal (signo, sigchld_handler); /* WARNING - must come after wait3() */
d0d6b7c5
JB
6581#endif
6582 errno = old_errno;
6583 return;
6584 }
6585#else
6586 pid = wait (&w);
6587#endif /* no WNOHANG */
6588
6589 /* Find the process that signaled us, and record its status. */
6590
a5cfdda8 6591 /* The process can have been deleted by Fdelete_process. */
8e50cc2d 6592 for (tail = deleted_pid_list; CONSP (tail); tail = XCDR (tail))
a5cfdda8 6593 {
8e050977 6594 Lisp_Object xpid = XCAR (tail);
8e50cc2d
SM
6595 if ((INTEGERP (xpid) && pid == (pid_t) XINT (xpid))
6596 || (FLOATP (xpid) && pid == (pid_t) XFLOAT_DATA (xpid)))
8e050977
CY
6597 {
6598 XSETCAR (tail, Qnil);
6599 goto sigchld_end_of_loop;
6600 }
a5cfdda8
KS
6601 }
6602
6603 /* Otherwise, if it is asynchronous, it is in Vprocess_alist. */
d0d6b7c5 6604 p = 0;
8e50cc2d 6605 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
d0d6b7c5 6606 {
70949dac 6607 proc = XCDR (XCAR (tail));
d0d6b7c5 6608 p = XPROCESS (proc);
d888760c 6609 if (EQ (p->type, Qreal) && p->pid == pid)
d0d6b7c5
JB
6610 break;
6611 p = 0;
6612 }
6613
6614 /* Look for an asynchronous process whose pid hasn't been filled
6615 in yet. */
6616 if (p == 0)
8e50cc2d 6617 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
d0d6b7c5 6618 {
70949dac 6619 proc = XCDR (XCAR (tail));
d0d6b7c5 6620 p = XPROCESS (proc);
6bfd98e7 6621 if (p->pid == -1)
d0d6b7c5
JB
6622 break;
6623 p = 0;
6624 }
177c0ea7 6625
d0d6b7c5
JB
6626 /* Change the status of the process that was found. */
6627 if (p != 0)
6628 {
e98d950b 6629 int clear_desc_flag = 0;
177c0ea7 6630
60f0fb11 6631 p->tick = ++process_tick;
bba104c1 6632 p->raw_status = w;
6bfd98e7 6633 p->raw_status_new = 1;
177c0ea7 6634
d0d6b7c5 6635 /* If process has terminated, stop waiting for its output. */
e98d950b 6636 if ((WIFSIGNALED (w) || WIFEXITED (w))
60f0fb11 6637 && p->infd >= 0)
e98d950b
RS
6638 clear_desc_flag = 1;
6639
6640 /* We use clear_desc_flag to avoid a compiler bug in Microsoft C. */
6641 if (clear_desc_flag)
6642 {
60f0fb11
SM
6643 FD_CLR (p->infd, &input_wait_mask);
6644 FD_CLR (p->infd, &non_keyboard_wait_mask);
e98d950b 6645 }
6be429b1 6646
214d6069 6647 /* Tell wait_reading_process_output that it needs to wake up and
6be429b1
JB
6648 look around. */
6649 if (input_available_clear_time)
6650 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
d0d6b7c5
JB
6651 }
6652
a5cfdda8
KS
6653 /* There was no asynchronous process found for that pid: we have
6654 a synchronous process. */
d0d6b7c5
JB
6655 else
6656 {
6657 synch_process_alive = 0;
6658
6659 /* Report the status of the synchronous process. */
6660 if (WIFEXITED (w))
6661 synch_process_retcode = WRETCODE (w);
6662 else if (WIFSIGNALED (w))
63136da6 6663 synch_process_termsig = WTERMSIG (w);
6be429b1 6664
214d6069 6665 /* Tell wait_reading_process_output that it needs to wake up and
6be429b1
JB
6666 look around. */
6667 if (input_available_clear_time)
6668 EMACS_SET_SECS_USECS (*input_available_clear_time, 0, 0);
d0d6b7c5
JB
6669 }
6670
a5cfdda8
KS
6671 sigchld_end_of_loop:
6672 ;
6673
d0d6b7c5
JB
6674 /* On some systems, we must return right away.
6675 If any more processes want to signal us, we will
6676 get another signal.
6677 Otherwise (on systems that have WNOHANG), loop around
6678 to use up all the processes that have something to tell us. */
4e6277d8 6679#if (defined WINDOWSNT \
8a2a6032 6680 || (defined USG && !defined GNU_LINUX \
63136da6 6681 && !(defined HPUX && defined WNOHANG)))
3c0ee47b 6682#if defined (USG) && ! defined (POSIX_SIGNALS)
d0d6b7c5
JB
6683 signal (signo, sigchld_handler);
6684#endif
6685 errno = old_errno;
6686 return;
6687#endif /* USG, but not HPUX with WNOHANG */
6688 }
6689}
a5cfdda8 6690#endif /* SIGCHLD */
d0d6b7c5
JB
6691\f
6692
6693static Lisp_Object
6694exec_sentinel_unwind (data)
6695 Lisp_Object data;
6696{
70949dac 6697 XPROCESS (XCAR (data))->sentinel = XCDR (data);
d0d6b7c5
JB
6698 return Qnil;
6699}
6700
3b9a3dfa
RS
6701static Lisp_Object
6702exec_sentinel_error_handler (error)
6703 Lisp_Object error;
6704{
6705 cmd_error_internal (error, "error in process sentinel: ");
6706 Vinhibit_quit = Qt;
6707 update_echo_area ();
833ba342 6708 Fsleep_for (make_number (2), Qnil);
8c983bf2 6709 return Qt;
3b9a3dfa
RS
6710}
6711
d0d6b7c5
JB
6712static void
6713exec_sentinel (proc, reason)
6714 Lisp_Object proc, reason;
6715{
dfc21838 6716 Lisp_Object sentinel, obuffer, odeactivate, okeymap;
d0d6b7c5 6717 register struct Lisp_Process *p = XPROCESS (proc);
aed13378 6718 int count = SPECPDL_INDEX ();
4da2f5be 6719 int outer_running_asynch_code = running_asynch_code;
bbce7d72 6720 int waiting = waiting_for_user_input_p;
d0d6b7c5 6721
84af9896
RS
6722 if (inhibit_sentinels)
6723 return;
6724
dfc21838
RS
6725 /* No need to gcpro these, because all we do with them later
6726 is test them for EQness, and none of them should be a string. */
8fb3cf64 6727 odeactivate = Vdeactivate_mark;
dfc21838
RS
6728 XSETBUFFER (obuffer, current_buffer);
6729 okeymap = current_buffer->keymap;
6730
d0d6b7c5
JB
6731 sentinel = p->sentinel;
6732 if (NILP (sentinel))
6733 return;
6734
6735 /* Zilch the sentinel while it's running, to avoid recursive invocations;
6736 assure that it gets restored no matter how the sentinel exits. */
6737 p->sentinel = Qnil;
6738 record_unwind_protect (exec_sentinel_unwind, Fcons (proc, sentinel));
6739 /* Inhibit quit so that random quits don't screw up a running filter. */
6740 specbind (Qinhibit_quit, Qt);
6545aada 6741 specbind (Qlast_nonmenu_event, Qt);
3b9a3dfa 6742
4da2f5be
RS
6743 /* In case we get recursively called,
6744 and we already saved the match data nonrecursively,
6745 save the same match data in safely recursive fashion. */
6746 if (outer_running_asynch_code)
6747 {
6748 Lisp_Object tem;
89f2614d
KS
6749 tem = Fmatch_data (Qnil, Qnil, Qnil);
6750 restore_search_regs ();
6751 record_unwind_save_match_data ();
6752 Fset_match_data (tem, Qt);
4da2f5be
RS
6753 }
6754
6755 /* For speed, if a search happens within this code,
6756 save the match data in a special nonrecursive fashion. */
7074fde6 6757 running_asynch_code = 1;
4da2f5be 6758
3b9a3dfa
RS
6759 internal_condition_case_1 (read_process_output_call,
6760 Fcons (sentinel,
6761 Fcons (proc, Fcons (reason, Qnil))),
6762 !NILP (Vdebug_on_error) ? Qnil : Qerror,
6763 exec_sentinel_error_handler);
4da2f5be
RS
6764
6765 /* If we saved the match data nonrecursively, restore it now. */
89f2614d 6766 restore_search_regs ();
4da2f5be 6767 running_asynch_code = outer_running_asynch_code;
8fb3cf64
KH
6768
6769 Vdeactivate_mark = odeactivate;
bbce7d72
RS
6770
6771 /* Restore waiting_for_user_input_p as it was
6772 when we were called, in case the filter clobbered it. */
6773 waiting_for_user_input_p = waiting;
6774
7973cfa8 6775#if 0
dfc21838
RS
6776 if (! EQ (Fcurrent_buffer (), obuffer)
6777 || ! EQ (current_buffer->keymap, okeymap))
7973cfa8 6778#endif
927e08be
RS
6779 /* But do it only if the caller is actually going to read events.
6780 Otherwise there's no need to make him wake up, and it could
7157b8fe 6781 cause trouble (for example it would make sit_for return). */
927e08be
RS
6782 if (waiting_for_user_input_p == -1)
6783 record_asynch_buffer_change ();
8fb3cf64 6784
2ea6d561 6785 unbind_to (count, Qnil);
d0d6b7c5
JB
6786}
6787
6788/* Report all recent events of a change in process status
6789 (either run the sentinel or output a message).
b50fe468
RS
6790 This is usually done while Emacs is waiting for keyboard input
6791 but can be done at other times. */
d0d6b7c5 6792
ff6daed3
KS
6793static void
6794status_notify (deleting_process)
6795 struct Lisp_Process *deleting_process;
d0d6b7c5
JB
6796{
6797 register Lisp_Object proc, buffer;
2e4149a8 6798 Lisp_Object tail, msg;
d0d6b7c5
JB
6799 struct gcpro gcpro1, gcpro2;
6800
2e4149a8
KH
6801 tail = Qnil;
6802 msg = Qnil;
d0d6b7c5
JB
6803 /* We need to gcpro tail; if read_process_output calls a filter
6804 which deletes a process and removes the cons to which tail points
6805 from Vprocess_alist, and then causes a GC, tail is an unprotected
6806 reference. */
6807 GCPRO2 (tail, msg);
6808
30623085
RS
6809 /* Set this now, so that if new processes are created by sentinels
6810 that we run, we get called again to handle their status changes. */
6811 update_tick = process_tick;
6812
99784d63 6813 for (tail = Vprocess_alist; CONSP (tail); tail = XCDR (tail))
d0d6b7c5 6814 {
30623085
RS
6815 Lisp_Object symbol;
6816 register struct Lisp_Process *p;
6817
99784d63 6818 proc = Fcdr (XCAR (tail));
30623085
RS
6819 p = XPROCESS (proc);
6820
60f0fb11 6821 if (p->tick != p->update_tick)
d0d6b7c5 6822 {
60f0fb11 6823 p->update_tick = p->tick;
d0d6b7c5 6824
30623085 6825 /* If process is still active, read any output that remains. */
4da2f5be 6826 while (! EQ (p->filter, Qt)
dd2a17ab 6827 && ! EQ (p->status, Qconnect)
e690ca94 6828 && ! EQ (p->status, Qlisten)
d888760c
GM
6829 /* Network or serial process not stopped: */
6830 && ! EQ (p->command, Qt)
60f0fb11 6831 && p->infd >= 0
ff6daed3 6832 && p != deleting_process
60f0fb11 6833 && read_process_output (proc, p->infd) > 0);
d0d6b7c5 6834
30623085 6835 buffer = p->buffer;
d0d6b7c5 6836
30623085 6837 /* Get the text to use for the message. */
6bfd98e7 6838 if (p->raw_status_new)
30623085 6839 update_status (p);
116c423c 6840 msg = status_message (p);
d0d6b7c5 6841
30623085
RS
6842 /* If process is terminated, deactivate it or delete it. */
6843 symbol = p->status;
6844 if (CONSP (p->status))
70949dac 6845 symbol = XCAR (p->status);
d0d6b7c5 6846
30623085
RS
6847 if (EQ (symbol, Qsignal) || EQ (symbol, Qexit)
6848 || EQ (symbol, Qclosed))
6849 {
6850 if (delete_exited_processes)
6851 remove_process (proc);
6852 else
6853 deactivate_process (proc);
6854 }
d0d6b7c5 6855
0ad61fe7
RS
6856 /* The actions above may have further incremented p->tick.
6857 So set p->update_tick again
6858 so that an error in the sentinel will not cause
6859 this code to be run again. */
60f0fb11 6860 p->update_tick = p->tick;
30623085
RS
6861 /* Now output the message suitably. */
6862 if (!NILP (p->sentinel))
6863 exec_sentinel (proc, msg);
6864 /* Don't bother with a message in the buffer
6865 when a process becomes runnable. */
6866 else if (!EQ (symbol, Qrun) && !NILP (buffer))
6867 {
6868 Lisp_Object ro, tem;
6869 struct buffer *old = current_buffer;
d8a2934e
RS
6870 int opoint, opoint_byte;
6871 int before, before_byte;
2e4149a8 6872
30623085 6873 ro = XBUFFER (buffer)->read_only;
d0d6b7c5 6874
30623085
RS
6875 /* Avoid error if buffer is deleted
6876 (probably that's why the process is dead, too) */
6877 if (NILP (XBUFFER (buffer)->name))
6878 continue;
6879 Fset_buffer (buffer);
12ca5cdf 6880
6ec8bbd2 6881 opoint = PT;
d8a2934e 6882 opoint_byte = PT_BYTE;
30623085
RS
6883 /* Insert new output into buffer
6884 at the current end-of-output marker,
6885 thus preserving logical ordering of input and output. */
6886 if (XMARKER (p->mark)->buffer)
d8a2934e 6887 Fgoto_char (p->mark);
30623085 6888 else
d8a2934e 6889 SET_PT_BOTH (ZV, ZV_BYTE);
12ca5cdf
RS
6890
6891 before = PT;
d8a2934e 6892 before_byte = PT_BYTE;
30623085
RS
6893
6894 tem = current_buffer->read_only;
6895 current_buffer->read_only = Qnil;
6896 insert_string ("\nProcess ");
6897 Finsert (1, &p->name);
6898 insert_string (" ");
6899 Finsert (1, &msg);
6900 current_buffer->read_only = tem;
d8a2934e 6901 set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
30623085 6902
12ca5cdf 6903 if (opoint >= before)
d8a2934e
RS
6904 SET_PT_BOTH (opoint + (PT - before),
6905 opoint_byte + (PT_BYTE - before_byte));
12ca5cdf 6906 else
d8a2934e 6907 SET_PT_BOTH (opoint, opoint_byte);
12ca5cdf 6908
30623085 6909 set_buffer_internal (old);
d0d6b7c5 6910 }
30623085
RS
6911 }
6912 } /* end for */
d0d6b7c5
JB
6913
6914 update_mode_lines++; /* in case buffers use %s in mode-line-format */
3007ebfb 6915 redisplay_preserve_echo_area (13);
d0d6b7c5 6916
d0d6b7c5
JB
6917 UNGCPRO;
6918}
0fa1789e
KH
6919
6920\f
6921DEFUN ("set-process-coding-system", Fset_process_coding_system,
6922 Sset_process_coding_system, 1, 3, 0,
fdb82f93
PJ
6923 doc: /* Set coding systems of PROCESS to DECODING and ENCODING.
6924DECODING will be used to decode subprocess output and ENCODING to
6925encode subprocess input. */)
f3d9532b
JB
6926 (process, decoding, encoding)
6927 register Lisp_Object process, decoding, encoding;
0fa1789e
KH
6928{
6929 register struct Lisp_Process *p;
6930
f3d9532b
JB
6931 CHECK_PROCESS (process);
6932 p = XPROCESS (process);
60f0fb11 6933 if (p->infd < 0)
d5db4077 6934 error ("Input file descriptor of %s closed", SDATA (p->name));
60f0fb11 6935 if (p->outfd < 0)
d5db4077 6936 error ("Output file descriptor of %s closed", SDATA (p->name));
03f04413
KH
6937 Fcheck_coding_system (decoding);
6938 Fcheck_coding_system (encoding);
76d812bb 6939 encoding = coding_inherit_eol_type (encoding, Qnil);
03f04413
KH
6940 p->decode_coding_system = decoding;
6941 p->encode_coding_system = encoding;
f3d9532b 6942 setup_process_coding_systems (process);
0fa1789e
KH
6943
6944 return Qnil;
6945}
6946
6947DEFUN ("process-coding-system",
6948 Fprocess_coding_system, Sprocess_coding_system, 1, 1, 0,
fdb82f93 6949 doc: /* Return a cons of coding systems for decoding and encoding of PROCESS. */)
f3d9532b
JB
6950 (process)
6951 register Lisp_Object process;
0fa1789e 6952{
f3d9532b
JB
6953 CHECK_PROCESS (process);
6954 return Fcons (XPROCESS (process)->decode_coding_system,
6955 XPROCESS (process)->encode_coding_system);
0fa1789e 6956}
03f04413
KH
6957
6958DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte,
6959 Sset_process_filter_multibyte, 2, 2, 0,
7392e23c
KS
6960 doc: /* Set multibyteness of the strings given to PROCESS's filter.
6961If FLAG is non-nil, the filter is given multibyte strings.
6962If FLAG is nil, the filter is given unibyte strings. In this case,
03f04413
KH
6963all character code conversion except for end-of-line conversion is
6964suppressed. */)
f3d9532b
JB
6965 (process, flag)
6966 Lisp_Object process, flag;
03f04413
KH
6967{
6968 register struct Lisp_Process *p;
6969
f3d9532b
JB
6970 CHECK_PROCESS (process);
6971 p = XPROCESS (process);
5431bd6a
SM
6972 if (NILP (flag))
6973 p->decode_coding_system = raw_text_coding_system (p->decode_coding_system);
f3d9532b 6974 setup_process_coding_systems (process);
03f04413
KH
6975
6976 return Qnil;
6977}
6978
6979DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p,
6980 Sprocess_filter_multibyte_p, 1, 1, 0,
6981 doc: /* Return t if a multibyte string is given to PROCESS's filter.*/)
f3d9532b
JB
6982 (process)
6983 Lisp_Object process;
03f04413
KH
6984{
6985 register struct Lisp_Process *p;
5431bd6a 6986 struct coding_system *coding;
03f04413 6987
f3d9532b
JB
6988 CHECK_PROCESS (process);
6989 p = XPROCESS (process);
5431bd6a
SM
6990 coding = proc_decode_coding_system[p->infd];
6991 return (CODING_FOR_UNIBYTE (coding) ? Qnil : Qt);
03f04413
KH
6992}
6993
6994
d0d6b7c5 6995\f
da8e1115 6996/* Add DESC to the set of keyboard input descriptors. */
a69281ff
RS
6997
6998void
6999add_keyboard_wait_descriptor (desc)
7000 int desc;
7001{
a69281ff 7002 FD_SET (desc, &input_wait_mask);
b5dc1c83 7003 FD_SET (desc, &non_process_wait_mask);
a69281ff
RS
7004 if (desc > max_keyboard_desc)
7005 max_keyboard_desc = desc;
7006}
7007
994d9841
NR
7008static int add_gpm_wait_descriptor_called_flag;
7009
7010void
7011add_gpm_wait_descriptor (desc)
7012 int desc;
7013{
7014 if (! add_gpm_wait_descriptor_called_flag)
7015 FD_CLR (0, &input_wait_mask);
7016 add_gpm_wait_descriptor_called_flag = 1;
7017 FD_SET (desc, &input_wait_mask);
7018 FD_SET (desc, &gpm_wait_mask);
7019 if (desc > max_gpm_desc)
7020 max_gpm_desc = desc;
7021}
7022
a69281ff
RS
7023/* From now on, do not expect DESC to give keyboard input. */
7024
7025void
7026delete_keyboard_wait_descriptor (desc)
7027 int desc;
7028{
7029 int fd;
7030 int lim = max_keyboard_desc;
7031
7032 FD_CLR (desc, &input_wait_mask);
b5dc1c83 7033 FD_CLR (desc, &non_process_wait_mask);
a69281ff
RS
7034
7035 if (desc == max_keyboard_desc)
7036 for (fd = 0; fd < lim; fd++)
7037 if (FD_ISSET (fd, &input_wait_mask)
994d9841
NR
7038 && !FD_ISSET (fd, &non_keyboard_wait_mask)
7039 && !FD_ISSET (fd, &gpm_wait_mask))
a69281ff
RS
7040 max_keyboard_desc = fd;
7041}
7042
994d9841
NR
7043void
7044delete_gpm_wait_descriptor (desc)
7045 int desc;
7046{
7047 int fd;
7048 int lim = max_gpm_desc;
7049
7050 FD_CLR (desc, &input_wait_mask);
7051 FD_CLR (desc, &non_process_wait_mask);
7052
7053 if (desc == max_gpm_desc)
7054 for (fd = 0; fd < lim; fd++)
7055 if (FD_ISSET (fd, &input_wait_mask)
7056 && !FD_ISSET (fd, &non_keyboard_wait_mask)
7057 && !FD_ISSET (fd, &non_process_wait_mask))
7058 max_gpm_desc = fd;
7059}
7060
a69281ff
RS
7061/* Return nonzero if *MASK has a bit set
7062 that corresponds to one of the keyboard input descriptors. */
7063
ff6daed3 7064static int
a69281ff
RS
7065keyboard_bit_set (mask)
7066 SELECT_TYPE *mask;
7067{
7068 int fd;
7069
ee8e09af 7070 for (fd = 0; fd <= max_keyboard_desc; fd++)
a69281ff
RS
7071 if (FD_ISSET (fd, mask) && FD_ISSET (fd, &input_wait_mask)
7072 && !FD_ISSET (fd, &non_keyboard_wait_mask))
7073 return 1;
7074
7075 return 0;
7076}
7077\f
934e2a68
EZ
7078/* Enumeration of and access to system processes a-la ps(1). */
7079
934e2a68
EZ
7080DEFUN ("list-system-processes", Flist_system_processes, Slist_system_processes,
7081 0, 0, 0,
7082 doc: /* Return a list of numerical process IDs of all running processes.
7083If this functionality is unsupported, return nil.
7084
a20878b6 7085See `process-attributes' for getting attributes of a process given its ID. */)
934e2a68
EZ
7086 ()
7087{
06e111a6 7088 return list_system_processes ();
934e2a68
EZ
7089}
7090
a20878b6
MB
7091DEFUN ("process-attributes", Fprocess_attributes,
7092 Sprocess_attributes, 1, 1, 0,
934e2a68
EZ
7093 doc: /* Return attributes of the process given by its PID, a number.
7094
7095Value is an alist where each element is a cons cell of the form
7096
ea92add1 7097 \(KEY . VALUE)
934e2a68
EZ
7098
7099If this functionality is unsupported, the value is nil.
7100
7101See `list-system-processes' for getting a list of all process IDs.
7102
ea92add1
RW
7103The KEYs of the attributes that this function may return are listed
7104below, together with the type of the associated VALUE (in parentheses).
8f8b082a
EZ
7105Not all platforms support all of these attributes; unsupported
7106attributes will not appear in the returned alist.
934e2a68
EZ
7107Unless explicitly indicated otherwise, numbers can have either
7108integer or floating point values.
7109
7110 euid -- Effective user User ID of the process (number)
7111 user -- User name corresponding to euid (string)
7112 egid -- Effective user Group ID of the process (number)
7113 group -- Group name corresponding to egid (string)
7114 comm -- Command name (executable name only) (string)
7115 state -- Process state code, such as "S", "R", or "T" (string)
7116 ppid -- Parent process ID (number)
7117 pgrp -- Process group ID (number)
7118 sess -- Session ID, i.e. process ID of session leader (number)
7119 ttname -- Controlling tty name (string)
7120 tpgid -- ID of foreground process group on the process's tty (number)
7121 minflt -- number of minor page faults (number)
7122 majflt -- number of major page faults (number)
7123 cminflt -- cumulative number of minor page faults (number)
7124 cmajflt -- cumulative number of major page faults (number)
7125 utime -- user time used by the process, in the (HIGH LOW USEC) format
7126 stime -- system time used by the process, in the (HIGH LOW USEC) format
89c874cf 7127 time -- sum of utime and stime, in the (HIGH LOW USEC) format
934e2a68
EZ
7128 cutime -- user time used by the process and its children, (HIGH LOW USEC)
7129 cstime -- system time used by the process and its children, (HIGH LOW USEC)
89c874cf 7130 ctime -- sum of cutime and cstime, in the (HIGH LOW USEC) format
934e2a68
EZ
7131 pri -- priority of the process (number)
7132 nice -- nice value of the process (number)
7133 thcount -- process thread count (number)
7134 start -- time the process started, in the (HIGH LOW USEC) format
7135 vsize -- virtual memory size of the process in KB's (number)
7136 rss -- resident set size of the process in KB's (number)
7137 etime -- elapsed time the process is running, in (HIGH LOW USEC) format
7138 pcpu -- percents of CPU time used by the process (floating-point number)
7139 pmem -- percents of total physical memory used by process's resident set
7140 (floating-point number)
7141 args -- command line which invoked the process (string). */)
7142 (pid)
7143
7144 Lisp_Object pid;
7145{
06e111a6 7146 return system_process_attributes (pid);
934e2a68
EZ
7147}
7148\f
dfcf069d 7149void
d0d6b7c5
JB
7150init_process ()
7151{
7152 register int i;
7153
84af9896
RS
7154 inhibit_sentinels = 0;
7155
d0d6b7c5
JB
7156#ifdef SIGCHLD
7157#ifndef CANNOT_DUMP
7158 if (! noninteractive || initialized)
7159#endif
7160 signal (SIGCHLD, sigchld_handler);
7161#endif
7162
7163 FD_ZERO (&input_wait_mask);
a69281ff 7164 FD_ZERO (&non_keyboard_wait_mask);
b5dc1c83 7165 FD_ZERO (&non_process_wait_mask);
7d0e672e 7166 max_process_desc = 0;
dd2281ae 7167
bad49fc7
KS
7168#ifdef NON_BLOCKING_CONNECT
7169 FD_ZERO (&connect_wait_mask);
7170 num_pending_connects = 0;
7171#endif
7172
6b61353c
KH
7173#ifdef ADAPTIVE_READ_BUFFERING
7174 process_output_delay_count = 0;
7175 process_output_skip = 0;
7176#endif
7177
c0707982
KL
7178 /* Don't do this, it caused infinite select loops. The display
7179 method should call add_keyboard_wait_descriptor on stdin if it
7180 needs that. */
7181#if 0
a69281ff 7182 FD_SET (0, &input_wait_mask);
c0707982 7183#endif
dd2281ae 7184
d0d6b7c5 7185 Vprocess_alist = Qnil;
a5cfdda8
KS
7186#ifdef SIGCHLD
7187 deleted_pid_list = Qnil;
7188#endif
d0d6b7c5
JB
7189 for (i = 0; i < MAXDESC; i++)
7190 {
7191 chan_process[i] = Qnil;
7192 proc_buffered_char[i] = -1;
7193 }
c7580538
KH
7194 bzero (proc_decode_coding_system, sizeof proc_decode_coding_system);
7195 bzero (proc_encode_coding_system, sizeof proc_encode_coding_system);
e690ca94
KS
7196#ifdef DATAGRAM_SOCKETS
7197 bzero (datagram_address, sizeof datagram_address);
7198#endif
9057ff80 7199
c2bd2c26
KS
7200#ifdef HAVE_SOCKETS
7201 {
7202 Lisp_Object subfeatures = Qnil;
6b61353c
KH
7203 struct socket_options *sopt;
7204
9057ff80
KS
7205#define ADD_SUBFEATURE(key, val) \
7206 subfeatures = Fcons (Fcons (key, Fcons (val, Qnil)), subfeatures)
7207
9057ff80 7208#ifdef NON_BLOCKING_CONNECT
c2bd2c26 7209 ADD_SUBFEATURE (QCnowait, Qt);
9057ff80
KS
7210#endif
7211#ifdef DATAGRAM_SOCKETS
c2bd2c26 7212 ADD_SUBFEATURE (QCtype, Qdatagram);
9057ff80
KS
7213#endif
7214#ifdef HAVE_LOCAL_SOCKETS
c2bd2c26 7215 ADD_SUBFEATURE (QCfamily, Qlocal);
e1652a86
KS
7216#endif
7217 ADD_SUBFEATURE (QCfamily, Qipv4);
7218#ifdef AF_INET6
7219 ADD_SUBFEATURE (QCfamily, Qipv6);
9057ff80
KS
7220#endif
7221#ifdef HAVE_GETSOCKNAME
c2bd2c26 7222 ADD_SUBFEATURE (QCservice, Qt);
9057ff80 7223#endif
132d0475 7224#if defined(O_NONBLOCK) || defined(O_NDELAY)
c2bd2c26 7225 ADD_SUBFEATURE (QCserver, Qt);
9057ff80 7226#endif
6b61353c
KH
7227
7228 for (sopt = socket_options; sopt->name; sopt++)
7229 subfeatures = Fcons (intern (sopt->name), subfeatures);
7230
c2bd2c26
KS
7231 Fprovide (intern ("make-network-process"), subfeatures);
7232 }
7233#endif /* HAVE_SOCKETS */
d9e7c622 7234
14145fa3 7235#if defined (DARWIN_OS)
db853b7a 7236 /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
d9e7c622
ST
7237 processes. As such, we only change the default value. */
7238 if (initialized)
7239 {
7240 char *release = get_operating_system_release();
7241 if (!release || !release[0] || (release[0] < MIN_PTY_KERNEL_VERSION
7242 && release[1] == '.')) {
7243 Vprocess_connection_type = Qnil;
7244 }
7245 }
7246#endif
d0d6b7c5 7247}
312c9964 7248
dfcf069d 7249void
d0d6b7c5
JB
7250syms_of_process ()
7251{
d0d6b7c5
JB
7252 Qprocessp = intern ("processp");
7253 staticpro (&Qprocessp);
7254 Qrun = intern ("run");
7255 staticpro (&Qrun);
7256 Qstop = intern ("stop");
7257 staticpro (&Qstop);
324e68ba
KS
7258 Qsignal = intern ("signal");
7259 staticpro (&Qsignal);
d0d6b7c5
JB
7260
7261 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
7262 here again.
7263
7264 Qexit = intern ("exit");
7265 staticpro (&Qexit); */
7266
7267 Qopen = intern ("open");
7268 staticpro (&Qopen);
7269 Qclosed = intern ("closed");
7270 staticpro (&Qclosed);
dd2a17ab
KS
7271 Qconnect = intern ("connect");
7272 staticpro (&Qconnect);
7273 Qfailed = intern ("failed");
7274 staticpro (&Qfailed);
e690ca94
KS
7275 Qlisten = intern ("listen");
7276 staticpro (&Qlisten);
7277 Qlocal = intern ("local");
7278 staticpro (&Qlocal);
e1652a86
KS
7279 Qipv4 = intern ("ipv4");
7280 staticpro (&Qipv4);
7281#ifdef AF_INET6
7282 Qipv6 = intern ("ipv6");
7283 staticpro (&Qipv6);
7284#endif
9057ff80
KS
7285 Qdatagram = intern ("datagram");
7286 staticpro (&Qdatagram);
e690ca94 7287
d888760c
GM
7288 QCport = intern (":port");
7289 staticpro (&QCport);
7290 QCspeed = intern (":speed");
7291 staticpro (&QCspeed);
7292 QCprocess = intern (":process");
7293 staticpro (&QCprocess);
7294
7295 QCbytesize = intern (":bytesize");
7296 staticpro (&QCbytesize);
7297 QCstopbits = intern (":stopbits");
7298 staticpro (&QCstopbits);
7299 QCparity = intern (":parity");
7300 staticpro (&QCparity);
7301 Qodd = intern ("odd");
7302 staticpro (&Qodd);
7303 Qeven = intern ("even");
7304 staticpro (&Qeven);
7305 QCflowcontrol = intern (":flowcontrol");
7306 staticpro (&QCflowcontrol);
7307 Qhw = intern ("hw");
7308 staticpro (&Qhw);
7309 Qsw = intern ("sw");
7310 staticpro (&Qsw);
7311 QCsummary = intern (":summary");
7312 staticpro (&QCsummary);
7313
7314 Qreal = intern ("real");
7315 staticpro (&Qreal);
7316 Qnetwork = intern ("network");
7317 staticpro (&Qnetwork);
7318 Qserial = intern ("serial");
7319 staticpro (&Qserial);
7320
e690ca94
KS
7321 QCname = intern (":name");
7322 staticpro (&QCname);
7323 QCbuffer = intern (":buffer");
7324 staticpro (&QCbuffer);
7325 QChost = intern (":host");
7326 staticpro (&QChost);
7327 QCservice = intern (":service");
7328 staticpro (&QCservice);
9057ff80
KS
7329 QCtype = intern (":type");
7330 staticpro (&QCtype);
e690ca94
KS
7331 QClocal = intern (":local");
7332 staticpro (&QClocal);
7333 QCremote = intern (":remote");
7334 staticpro (&QCremote);
7335 QCcoding = intern (":coding");
7336 staticpro (&QCcoding);
7337 QCserver = intern (":server");
7338 staticpro (&QCserver);
e690ca94
KS
7339 QCnowait = intern (":nowait");
7340 staticpro (&QCnowait);
e690ca94
KS
7341 QCsentinel = intern (":sentinel");
7342 staticpro (&QCsentinel);
7343 QClog = intern (":log");
7344 staticpro (&QClog);
7345 QCnoquery = intern (":noquery");
7346 staticpro (&QCnoquery);
7347 QCstop = intern (":stop");
7348 staticpro (&QCstop);
7349 QCoptions = intern (":options");
7350 staticpro (&QCoptions);
faa7db08
KS
7351 QCplist = intern (":plist");
7352 staticpro (&QCplist);
177c0ea7 7353
6545aada
RS
7354 Qlast_nonmenu_event = intern ("last-nonmenu-event");
7355 staticpro (&Qlast_nonmenu_event);
7356
d0d6b7c5 7357 staticpro (&Vprocess_alist);
a5cfdda8
KS
7358#ifdef SIGCHLD
7359 staticpro (&deleted_pid_list);
7360#endif
d0d6b7c5 7361
934e2a68
EZ
7362 Qeuid = intern ("euid");
7363 staticpro (&Qeuid);
7364 Qegid = intern ("egid");
7365 staticpro (&Qegid);
7366 Quser = intern ("user");
7367 staticpro (&Quser);
7368 Qgroup = intern ("group");
7369 staticpro (&Qgroup);
7370 Qcomm = intern ("comm");
7371 staticpro (&Qcomm);
7372 Qstate = intern ("state");
7373 staticpro (&Qstate);
7374 Qppid = intern ("ppid");
7375 staticpro (&Qppid);
7376 Qpgrp = intern ("pgrp");
7377 staticpro (&Qpgrp);
7378 Qsess = intern ("sess");
7379 staticpro (&Qsess);
7380 Qttname = intern ("ttname");
7381 staticpro (&Qttname);
7382 Qtpgid = intern ("tpgid");
7383 staticpro (&Qtpgid);
7384 Qminflt = intern ("minflt");
7385 staticpro (&Qminflt);
7386 Qmajflt = intern ("majflt");
7387 staticpro (&Qmajflt);
7388 Qcminflt = intern ("cminflt");
7389 staticpro (&Qcminflt);
7390 Qcmajflt = intern ("cmajflt");
7391 staticpro (&Qcmajflt);
7392 Qutime = intern ("utime");
7393 staticpro (&Qutime);
7394 Qstime = intern ("stime");
7395 staticpro (&Qstime);
89c874cf
EZ
7396 Qtime = intern ("time");
7397 staticpro (&Qtime);
934e2a68
EZ
7398 Qcutime = intern ("cutime");
7399 staticpro (&Qcutime);
7400 Qcstime = intern ("cstime");
7401 staticpro (&Qcstime);
89c874cf
EZ
7402 Qctime = intern ("ctime");
7403 staticpro (&Qctime);
934e2a68
EZ
7404 Qpri = intern ("pri");
7405 staticpro (&Qpri);
7406 Qnice = intern ("nice");
7407 staticpro (&Qnice);
7408 Qthcount = intern ("thcount");
7409 staticpro (&Qthcount);
7410 Qstart = intern ("start");
7411 staticpro (&Qstart);
7412 Qvsize = intern ("vsize");
7413 staticpro (&Qvsize);
7414 Qrss = intern ("rss");
7415 staticpro (&Qrss);
7416 Qetime = intern ("etime");
7417 staticpro (&Qetime);
7418 Qpcpu = intern ("pcpu");
7419 staticpro (&Qpcpu);
7420 Qpmem = intern ("pmem");
7421 staticpro (&Qpmem);
7422 Qargs = intern ("args");
7423 staticpro (&Qargs);
7424
d0d6b7c5 7425 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
fdb82f93 7426 doc: /* *Non-nil means delete processes immediately when they exit.
d4e83d5c 7427A value of nil means don't delete them until `list-processes' is run. */);
d0d6b7c5
JB
7428
7429 delete_exited_processes = 1;
7430
7431 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type,
fdb82f93
PJ
7432 doc: /* Control type of device used to communicate with subprocesses.
7433Values are nil to use a pipe, or t or `pty' to use a pty.
7434The value has no effect if the system has no ptys or if all ptys are busy:
7435then a pipe is used in any case.
7436The value takes effect when `start-process' is called. */);
d0d6b7c5
JB
7437 Vprocess_connection_type = Qt;
7438
6b61353c
KH
7439#ifdef ADAPTIVE_READ_BUFFERING
7440 DEFVAR_LISP ("process-adaptive-read-buffering", &Vprocess_adaptive_read_buffering,
7441 doc: /* If non-nil, improve receive buffering by delaying after short reads.
f3d9532b 7442On some systems, when Emacs reads the output from a subprocess, the output data
6b61353c 7443is read in very small blocks, potentially resulting in very poor performance.
2de35a25 7444This behavior can be remedied to some extent by setting this variable to a
6b61353c 7445non-nil value, as it will automatically delay reading from such processes, to
635a3496 7446allow them to produce more output before Emacs tries to read it.
6b61353c
KH
7447If the value is t, the delay is reset after each write to the process; any other
7448non-nil value means that the delay is not reset on write.
7449The variable takes effect when `start-process' is called. */);
7450 Vprocess_adaptive_read_buffering = Qt;
7451#endif
7452
d0d6b7c5
JB
7453 defsubr (&Sprocessp);
7454 defsubr (&Sget_process);
7455 defsubr (&Sget_buffer_process);
7456 defsubr (&Sdelete_process);
7457 defsubr (&Sprocess_status);
7458 defsubr (&Sprocess_exit_status);
7459 defsubr (&Sprocess_id);
7460 defsubr (&Sprocess_name);
3b9a3dfa 7461 defsubr (&Sprocess_tty_name);
d0d6b7c5
JB
7462 defsubr (&Sprocess_command);
7463 defsubr (&Sset_process_buffer);
7464 defsubr (&Sprocess_buffer);
7465 defsubr (&Sprocess_mark);
7466 defsubr (&Sset_process_filter);
7467 defsubr (&Sprocess_filter);
7468 defsubr (&Sset_process_sentinel);
7469 defsubr (&Sprocess_sentinel);
de282a05 7470 defsubr (&Sset_process_window_size);
52a1b894
EZ
7471 defsubr (&Sset_process_inherit_coding_system_flag);
7472 defsubr (&Sprocess_inherit_coding_system_flag);
e690ca94
KS
7473 defsubr (&Sset_process_query_on_exit_flag);
7474 defsubr (&Sprocess_query_on_exit_flag);
de282a05 7475 defsubr (&Sprocess_contact);
faa7db08
KS
7476 defsubr (&Sprocess_plist);
7477 defsubr (&Sset_process_plist);
d0d6b7c5
JB
7478 defsubr (&Slist_processes);
7479 defsubr (&Sprocess_list);
7480 defsubr (&Sstart_process);
d888760c
GM
7481#ifdef HAVE_SERIAL
7482 defsubr (&Sserial_process_configure);
7483 defsubr (&Smake_serial_process);
7484#endif /* HAVE_SERIAL */
d0d6b7c5 7485#ifdef HAVE_SOCKETS
6b61353c 7486 defsubr (&Sset_network_process_option);
e690ca94 7487 defsubr (&Smake_network_process);
991234f0 7488 defsubr (&Sformat_network_address);
d0d6b7c5 7489#endif /* HAVE_SOCKETS */
6b61353c
KH
7490#if defined(HAVE_SOCKETS) && defined(HAVE_NET_IF_H) && defined(HAVE_SYS_IOCTL_H)
7491#ifdef SIOCGIFCONF
7492 defsubr (&Snetwork_interface_list);
7493#endif
7494#if defined(SIOCGIFADDR) || defined(SIOCGIFHWADDR) || defined(SIOCGIFFLAGS)
7495 defsubr (&Snetwork_interface_info);
7496#endif
7497#endif /* HAVE_SOCKETS ... */
e690ca94
KS
7498#ifdef DATAGRAM_SOCKETS
7499 defsubr (&Sprocess_datagram_address);
7500 defsubr (&Sset_process_datagram_address);
7501#endif
d0d6b7c5
JB
7502 defsubr (&Saccept_process_output);
7503 defsubr (&Sprocess_send_region);
7504 defsubr (&Sprocess_send_string);
7505 defsubr (&Sinterrupt_process);
7506 defsubr (&Skill_process);
7507 defsubr (&Squit_process);
7508 defsubr (&Sstop_process);
7509 defsubr (&Scontinue_process);
b81ea5ef 7510 defsubr (&Sprocess_running_child_p);
d0d6b7c5
JB
7511 defsubr (&Sprocess_send_eof);
7512 defsubr (&Ssignal_process);
7513 defsubr (&Swaiting_for_user_input_p);
d888760c 7514 defsubr (&Sprocess_type);
0fa1789e
KH
7515 defsubr (&Sset_process_coding_system);
7516 defsubr (&Sprocess_coding_system);
03f04413
KH
7517 defsubr (&Sset_process_filter_multibyte);
7518 defsubr (&Sprocess_filter_multibyte_p);
934e2a68 7519 defsubr (&Slist_system_processes);
a20878b6 7520 defsubr (&Sprocess_attributes);
d0d6b7c5
JB
7521}
7522
6720a7fb
JB
7523\f
7524#else /* not subprocesses */
7525
7526#include <sys/types.h>
7527#include <errno.h>
934e2a68
EZ
7528#include <sys/stat.h>
7529#include <stdlib.h>
7530#include <fcntl.h>
7531#ifdef HAVE_UNISTD_H
7532#include <unistd.h>
7533#endif
6720a7fb
JB
7534
7535#include "lisp.h"
7536#include "systime.h"
5bbf78c1 7537#include "character.h"
52a1b894 7538#include "coding.h"
6720a7fb 7539#include "termopts.h"
81afb6d1 7540#include "sysselect.h"
6720a7fb 7541
ff11dfa1 7542extern int frame_garbaged;
6720a7fb 7543
f694e5d2
KH
7544extern EMACS_TIME timer_check ();
7545extern int timers_run;
6720a7fb 7546
8c57b2e5 7547Lisp_Object QCtype, QCname;
9057ff80 7548
6a1bab0e
EZ
7549Lisp_Object Qeuid, Qegid, Qcomm, Qstate, Qppid, Qpgrp, Qsess, Qttname, Qtpgid;
7550Lisp_Object Qminflt, Qmajflt, Qcminflt, Qcmajflt, Qutime, Qstime, Qcstime;
7551Lisp_Object Qcutime, Qpri, Qnice, Qthcount, Qstart, Qvsize, Qrss, Qargs;
7552Lisp_Object Quser, Qgroup, Qetime, Qpcpu, Qpmem, Qtime, Qctime;
7553
6720a7fb
JB
7554/* As described above, except assuming that there are no subprocesses:
7555
7556 Wait for timeout to elapse and/or keyboard input to be available.
7557
7558 time_limit is:
7559 timeout in seconds, or
7560 zero for no limit, or
7561 -1 means gobble data immediately available but don't wait for any.
7562
f76475ad 7563 read_kbd is a Lisp_Object:
6720a7fb
JB
7564 0 to ignore keyboard input, or
7565 1 to return when input is available, or
7566 -1 means caller will actually read the input, so don't throw to
7567 the quit handler.
b89a415a
KS
7568
7569 see full version for other parameters. We know that wait_proc will
7570 always be NULL, since `subprocesses' isn't defined.
6720a7fb
JB
7571
7572 do_display != 0 means redisplay should be done to show subprocess
5164ee8e 7573 output that arrives.
6720a7fb 7574
e0f24100 7575 Return true if we received input from any process. */
6720a7fb
JB
7576
7577int
214d6069
KS
7578wait_reading_process_output (time_limit, microsecs, read_kbd, do_display,
7579 wait_for_cell, wait_proc, just_wait_proc)
b89a415a
KS
7580 int time_limit, microsecs, read_kbd, do_display;
7581 Lisp_Object wait_for_cell;
7582 struct Lisp_Process *wait_proc;
7583 int just_wait_proc;
6720a7fb 7584{
52fd88d3 7585 register int nfds;
f694e5d2 7586 EMACS_TIME end_time, timeout;
8db121c4 7587 SELECT_TYPE waitchannels;
f694e5d2 7588 int xerrno;
6720a7fb
JB
7589
7590 /* What does time_limit really mean? */
7591 if (time_limit || microsecs)
7592 {
6720a7fb 7593 EMACS_GET_TIME (end_time);
52fd88d3 7594 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
6720a7fb
JB
7595 EMACS_ADD_TIME (end_time, end_time, timeout);
7596 }
6720a7fb
JB
7597
7598 /* Turn off periodic alarms (in case they are in use)
75eb23f1 7599 and then turn off any other atimers,
6720a7fb 7600 because the select emulator uses alarms. */
75eb23f1 7601 stop_polling ();
30904ab7 7602 turn_on_atimers (0);
6720a7fb 7603
52fd88d3 7604 while (1)
6720a7fb 7605 {
bae8d137 7606 int timeout_reduced_for_timers = 0;
6720a7fb 7607
6720a7fb
JB
7608 /* If calling from keyboard input, do not quit
7609 since we want to return C-g as an input character.
7610 Otherwise, do pending quit if requested. */
b89a415a 7611 if (read_kbd >= 0)
6720a7fb
JB
7612 QUIT;
7613
0a65b032 7614 /* Exit now if the cell we're waiting for became non-nil. */
f3fbd155 7615 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
0a65b032
RS
7616 break;
7617
bae8d137
RS
7618 /* Compute time from now till when time limit is up */
7619 /* Exit if already run out */
52fd88d3
EZ
7620 if (time_limit == -1)
7621 {
7622 /* -1 specified for timeout means
7623 gobble output available now
7624 but don't wait at all. */
7625
7626 EMACS_SET_SECS_USECS (timeout, 0, 0);
7627 }
7628 else if (time_limit || microsecs)
6720a7fb 7629 {
f694e5d2
KH
7630 EMACS_GET_TIME (timeout);
7631 EMACS_SUB_TIME (timeout, end_time, timeout);
7632 if (EMACS_TIME_NEG_P (timeout))
6720a7fb
JB
7633 break;
7634 }
52fd88d3
EZ
7635 else
7636 {
7637 EMACS_SET_SECS_USECS (timeout, 100000, 0);
7638 }
6720a7fb 7639
bae8d137
RS
7640 /* If our caller will not immediately handle keyboard events,
7641 run timer events directly.
7642 (Callers that will immediately read keyboard events
7643 call timer_delay on their own.) */
f3fbd155 7644 if (NILP (wait_for_cell))
bae8d137
RS
7645 {
7646 EMACS_TIME timer_delay;
0a65b032 7647
9baacf76 7648 do
0a65b032 7649 {
9baacf76
GM
7650 int old_timers_run = timers_run;
7651 timer_delay = timer_check (1);
7652 if (timers_run != old_timers_run && do_display)
7653 /* We must retry, since a timer may have requeued itself
7654 and that could alter the time delay. */
3007ebfb 7655 redisplay_preserve_echo_area (14);
9baacf76
GM
7656 else
7657 break;
0a65b032 7658 }
9baacf76 7659 while (!detect_input_pending ());
0a65b032 7660
52fd88d3 7661 /* If there is unread keyboard input, also return. */
b89a415a 7662 if (read_kbd != 0
52fd88d3
EZ
7663 && requeued_events_pending_p ())
7664 break;
7665
f694e5d2 7666 if (! EMACS_TIME_NEG_P (timer_delay) && time_limit != -1)
bae8d137
RS
7667 {
7668 EMACS_TIME difference;
f694e5d2 7669 EMACS_SUB_TIME (difference, timer_delay, timeout);
bae8d137
RS
7670 if (EMACS_TIME_NEG_P (difference))
7671 {
f694e5d2 7672 timeout = timer_delay;
bae8d137
RS
7673 timeout_reduced_for_timers = 1;
7674 }
7675 }
7676 }
7677
6720a7fb
JB
7678 /* Cause C-g and alarm signals to take immediate action,
7679 and cause input available signals to zero out timeout. */
b89a415a 7680 if (read_kbd < 0)
6720a7fb
JB
7681 set_waiting_for_input (&timeout);
7682
0a65b032
RS
7683 /* Wait till there is something to do. */
7684
b89a415a 7685 if (! read_kbd && NILP (wait_for_cell))
0a65b032
RS
7686 FD_ZERO (&waitchannels);
7687 else
7688 FD_SET (0, &waitchannels);
7689
ff11dfa1 7690 /* If a frame has been newly mapped and needs updating,
6720a7fb 7691 reprocess its display stuff. */
5164ee8e 7692 if (frame_garbaged && do_display)
0a65b032
RS
7693 {
7694 clear_waiting_for_input ();
3007ebfb 7695 redisplay_preserve_echo_area (15);
b89a415a 7696 if (read_kbd < 0)
0a65b032
RS
7697 set_waiting_for_input (&timeout);
7698 }
6720a7fb 7699
b89a415a 7700 if (read_kbd && detect_input_pending ())
1861b214
RS
7701 {
7702 nfds = 0;
7703 FD_ZERO (&waitchannels);
7704 }
7705 else
7706 nfds = select (1, &waitchannels, (SELECT_TYPE *)0, (SELECT_TYPE *)0,
7707 &timeout);
f694e5d2
KH
7708
7709 xerrno = errno;
6720a7fb
JB
7710
7711 /* Make C-g and alarm signals set flags again */
7712 clear_waiting_for_input ();
7713
7714 /* If we woke up due to SIGWINCH, actually change size now. */
2b653806 7715 do_pending_window_change (0);
6720a7fb 7716
f694e5d2
KH
7717 if (time_limit && nfds == 0 && ! timeout_reduced_for_timers)
7718 /* We waited the full specified time, so return now. */
7719 break;
7720
6720a7fb
JB
7721 if (nfds == -1)
7722 {
7723 /* If the system call was interrupted, then go around the
7724 loop again. */
f694e5d2 7725 if (xerrno == EINTR)
8db121c4 7726 FD_ZERO (&waitchannels);
f694e5d2 7727 else
68c45bf0 7728 error ("select error: %s", emacs_strerror (xerrno));
6720a7fb 7729 }
89f1e540 7730#ifdef SOLARIS2
6720a7fb
JB
7731 else if (nfds > 0 && (waitchannels & 1) && interrupt_input)
7732 /* System sometimes fails to deliver SIGIO. */
7733 kill (getpid (), SIGIO);
7734#endif
7324d660 7735#ifdef SIGIO
b89a415a 7736 if (read_kbd && interrupt_input && (waitchannels & 1))
e643c5be 7737 kill (getpid (), SIGIO);
7324d660 7738#endif
6720a7fb 7739
f694e5d2
KH
7740 /* Check for keyboard input */
7741
b89a415a 7742 if (read_kbd
78c1afb6 7743 && detect_input_pending_run_timers (do_display))
f694e5d2 7744 {
78c1afb6 7745 swallow_events (do_display);
f694e5d2 7746 if (detect_input_pending_run_timers (do_display))
a2fab450 7747 break;
78c1afb6
EZ
7748 }
7749
52fd88d3 7750 /* If there is unread keyboard input, also return. */
b89a415a 7751 if (read_kbd
52fd88d3
EZ
7752 && requeued_events_pending_p ())
7753 break;
7754
f854a00b
RS
7755 /* If wait_for_cell. check for keyboard input
7756 but don't run any timers.
7757 ??? (It seems wrong to me to check for keyboard
7758 input at all when wait_for_cell, but the code
7759 has been this way since July 1994.
7760 Try changing this after version 19.31.) */
f3fbd155 7761 if (! NILP (wait_for_cell)
f854a00b
RS
7762 && detect_input_pending ())
7763 {
7764 swallow_events (do_display);
7765 if (detect_input_pending ())
7766 break;
7767 }
7768
0a65b032 7769 /* Exit now if the cell we're waiting for became non-nil. */
f3fbd155 7770 if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
0a65b032 7771 break;
6720a7fb
JB
7772 }
7773
a87b802f
JB
7774 start_polling ();
7775
6720a7fb
JB
7776 return 0;
7777}
7778
7779
e2ba787b
PJ
7780/* Don't confuse make-docfile by having two doc strings for this function.
7781 make-docfile does not pay attention to #if, for good reason! */
6720a7fb 7782DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
fdb82f93
PJ
7783 0)
7784 (name)
6720a7fb
JB
7785 register Lisp_Object name;
7786{
7787 return Qnil;
7788}
7789
e2ba787b
PJ
7790 /* Don't confuse make-docfile by having two doc strings for this function.
7791 make-docfile does not pay attention to #if, for good reason! */
52a1b894 7792DEFUN ("process-inherit-coding-system-flag",
fdb82f93
PJ
7793 Fprocess_inherit_coding_system_flag, Sprocess_inherit_coding_system_flag,
7794 1, 1, 0,
7795 0)
7796 (process)
52a1b894
EZ
7797 register Lisp_Object process;
7798{
7799 /* Ignore the argument and return the value of
7800 inherit-process-coding-system. */
7801 return inherit_process_coding_system ? Qt : Qnil;
7802}
7803
6720a7fb
JB
7804/* Kill all processes associated with `buffer'.
7805 If `buffer' is nil, kill all processes.
7806 Since we have no subprocesses, this does nothing. */
7807
d9bb0c32 7808void
6720a7fb
JB
7809kill_buffer_processes (buffer)
7810 Lisp_Object buffer;
7811{
7812}
7813
934e2a68
EZ
7814DEFUN ("list-system-processes", Flist_system_processes, Slist_system_processes,
7815 0, 0, 0,
7816 doc: /* Return a list of numerical process IDs of all running processes.
7817If this functionality is unsupported, return nil.
7818
a20878b6 7819See `process-attributes' for getting attributes of a process given its ID. */)
934e2a68
EZ
7820 ()
7821{
6a1bab0e 7822 return list_system_processes ();
934e2a68
EZ
7823}
7824
a20878b6
MB
7825DEFUN ("process-attributes", Fprocess_attributes,
7826 Sprocess_attributes, 1, 1, 0,
934e2a68
EZ
7827 doc: /* Return attributes of the process given by its PID, a number.
7828
7829Value is an alist where each element is a cons cell of the form
7830
a7713d66 7831 \(KEY . VALUE)
934e2a68
EZ
7832
7833If this functionality is unsupported, the value is nil.
7834
7835See `list-system-processes' for getting a list of all process IDs.
7836
a7713d66
RW
7837The KEYs of the attributes that this function may return are listed
7838below, together with the type of the associated VALUE (in parentheses).
8f8b082a
EZ
7839Not all platforms support all of these attributes; unsupported
7840attributes will not appear in the returned alist.
934e2a68
EZ
7841Unless explicitly indicated otherwise, numbers can have either
7842integer or floating point values.
7843
7844 euid -- Effective user User ID of the process (number)
7845 user -- User name corresponding to euid (string)
7846 egid -- Effective user Group ID of the process (number)
7847 group -- Group name corresponding to egid (string)
7848 comm -- Command name (executable name only) (string)
7849 state -- Process state code, such as "S", "R", or "T" (string)
7850 ppid -- Parent process ID (number)
7851 pgrp -- Process group ID (number)
7852 sess -- Session ID, i.e. process ID of session leader (number)
7853 ttname -- Controlling tty name (string)
7854 tpgid -- ID of foreground process group on the process's tty (number)
7855 minflt -- number of minor page faults (number)
7856 majflt -- number of major page faults (number)
7857 cminflt -- cumulative number of minor page faults (number)
7858 cmajflt -- cumulative number of major page faults (number)
7859 utime -- user time used by the process, in the (HIGH LOW USEC) format
7860 stime -- system time used by the process, in the (HIGH LOW USEC) format
89c874cf 7861 time -- sum of utime and stime, in the (HIGH LOW USEC) format
934e2a68
EZ
7862 cutime -- user time used by the process and its children, (HIGH LOW USEC)
7863 cstime -- system time used by the process and its children, (HIGH LOW USEC)
89c874cf 7864 ctime -- sum of cutime and cstime, in the (HIGH LOW USEC) format
934e2a68
EZ
7865 pri -- priority of the process (number)
7866 nice -- nice value of the process (number)
7867 thcount -- process thread count (number)
7868 start -- time the process started, in the (HIGH LOW USEC) format
7869 vsize -- virtual memory size of the process in KB's (number)
7870 rss -- resident set size of the process in KB's (number)
7871 etime -- elapsed time the process is running, in (HIGH LOW USEC) format
7872 pcpu -- percents of CPU time used by the process (floating-point number)
7873 pmem -- percents of total physical memory used by process's resident set
7874 (floating-point number)
7875 args -- command line which invoked the process (string). */)
7876 (pid)
7877
7878 Lisp_Object pid;
7879{
6a1bab0e 7880 return system_process_attributes (pid);
934e2a68
EZ
7881}
7882
02b9b4fd 7883void
6720a7fb
JB
7884init_process ()
7885{
7886}
7887
02b9b4fd 7888void
6720a7fb
JB
7889syms_of_process ()
7890{
9057ff80
KS
7891 QCtype = intern (":type");
7892 staticpro (&QCtype);
8c57b2e5
EZ
7893 QCname = intern (":name");
7894 staticpro (&QCname);
6a1bab0e
EZ
7895 QCtype = intern (":type");
7896 staticpro (&QCtype);
7897 QCname = intern (":name");
7898 staticpro (&QCname);
7899 Qeuid = intern ("euid");
7900 staticpro (&Qeuid);
7901 Qegid = intern ("egid");
7902 staticpro (&Qegid);
7903 Quser = intern ("user");
7904 staticpro (&Quser);
7905 Qgroup = intern ("group");
7906 staticpro (&Qgroup);
7907 Qcomm = intern ("comm");
7908 staticpro (&Qcomm);
7909 Qstate = intern ("state");
7910 staticpro (&Qstate);
7911 Qppid = intern ("ppid");
7912 staticpro (&Qppid);
7913 Qpgrp = intern ("pgrp");
7914 staticpro (&Qpgrp);
7915 Qsess = intern ("sess");
7916 staticpro (&Qsess);
7917 Qttname = intern ("ttname");
7918 staticpro (&Qttname);
7919 Qtpgid = intern ("tpgid");
7920 staticpro (&Qtpgid);
7921 Qminflt = intern ("minflt");
7922 staticpro (&Qminflt);
7923 Qmajflt = intern ("majflt");
7924 staticpro (&Qmajflt);
7925 Qcminflt = intern ("cminflt");
7926 staticpro (&Qcminflt);
7927 Qcmajflt = intern ("cmajflt");
7928 staticpro (&Qcmajflt);
7929 Qutime = intern ("utime");
7930 staticpro (&Qutime);
7931 Qstime = intern ("stime");
7932 staticpro (&Qstime);
7933 Qtime = intern ("time");
7934 staticpro (&Qtime);
7935 Qcutime = intern ("cutime");
7936 staticpro (&Qcutime);
7937 Qcstime = intern ("cstime");
7938 staticpro (&Qcstime);
7939 Qctime = intern ("ctime");
7940 staticpro (&Qctime);
7941 Qpri = intern ("pri");
7942 staticpro (&Qpri);
7943 Qnice = intern ("nice");
7944 staticpro (&Qnice);
7945 Qthcount = intern ("thcount");
7946 staticpro (&Qthcount);
7947 Qstart = intern ("start");
7948 staticpro (&Qstart);
7949 Qvsize = intern ("vsize");
7950 staticpro (&Qvsize);
7951 Qrss = intern ("rss");
7952 staticpro (&Qrss);
7953 Qetime = intern ("etime");
7954 staticpro (&Qetime);
7955 Qpcpu = intern ("pcpu");
7956 staticpro (&Qpcpu);
7957 Qpmem = intern ("pmem");
7958 staticpro (&Qpmem);
7959 Qargs = intern ("args");
7960 staticpro (&Qargs);
9057ff80 7961
6720a7fb 7962 defsubr (&Sget_buffer_process);
52a1b894 7963 defsubr (&Sprocess_inherit_coding_system_flag);
934e2a68 7964 defsubr (&Slist_system_processes);
a20878b6 7965 defsubr (&Sprocess_attributes);
6720a7fb
JB
7966}
7967
7968\f
7969#endif /* not subprocesses */
6b61353c
KH
7970
7971/* arch-tag: 3706c011-7b9a-4117-bd4f-59e7f701a4c4
7972 (do not change this comment) */