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