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