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