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