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