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