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