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