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