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