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