* process.c [SYSV]: Don't include <termios.h>, <termio.h>, or
[bpt/emacs.git] / src / process.c
1 /* Asynchronous subprocess control for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 #include <signal.h>
22
23 #include "config.h"
24
25 /* This file is split into two parts by the following preprocessor
26 conditional. The 'then' clause contains all of the support for
27 asynchronous subprocesses. The 'else' clause contains stub
28 versions of some of the asynchronous subprocess routines that are
29 often called elsewhere in Emacs, so we don't have to #ifdef the
30 sections that call them. */
31
32 \f
33 #ifdef subprocesses
34
35 #include <stdio.h>
36 #include <errno.h>
37 #include <setjmp.h>
38 #include <sys/types.h> /* some typedefs are used in sys/file.h */
39 #include <sys/file.h>
40 #include <sys/stat.h>
41
42 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
43 #include <sys/socket.h>
44 #include <netdb.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #endif /* HAVE_SOCKETS */
48
49 #if defined(BSD) || defined(STRIDE)
50 #include <sys/ioctl.h>
51 #if !defined (O_NDELAY) && defined (HAVE_PTYS) && !defined(USG5)
52 #include <fcntl.h>
53 #endif /* HAVE_PTYS and no O_NDELAY */
54 #endif /* BSD or STRIDE */
55
56 #ifdef NEED_BSDTTY
57 #include <bsdtty.h>
58 #endif
59
60 #ifdef IRIS
61 #include <sys/sysmacros.h> /* for "minor" */
62 #endif /* not IRIS */
63
64 #include "systime.h"
65 #include "systty.h"
66
67 #include "lisp.h"
68 #include "window.h"
69 #include "buffer.h"
70 #include "process.h"
71 #include "termhooks.h"
72 #include "termopts.h"
73 #include "commands.h"
74 #include "dispextern.h"
75
76 Lisp_Object Qrun, Qstop, Qsignal, Qopen, Qclosed;
77 /* Qexit is declared and initialized in eval.c. */
78
79 /* a process object is a network connection when its childp field is neither
80 Qt nor Qnil but is instead a string (name of foreign host we
81 are connected to + name of port we are connected to) */
82
83 #ifdef HAVE_SOCKETS
84 static Lisp_Object stream_process;
85
86 #define NETCONN_P(p) (XGCTYPE (XPROCESS (p)->childp) == Lisp_String)
87 #else
88 #define NETCONN_P(p) 0
89 #endif /* HAVE_SOCKETS */
90
91 /* Define first descriptor number available for subprocesses. */
92 #ifdef VMS
93 #define FIRST_PROC_DESC 1
94 #else /* Not VMS */
95 #define FIRST_PROC_DESC 3
96 #endif
97
98 /* Define SIGCHLD as an alias for SIGCLD. There are many conditionals
99 testing SIGCHLD. */
100
101 #if !defined (SIGCHLD) && defined (SIGCLD)
102 #define SIGCHLD SIGCLD
103 #endif /* SIGCLD */
104
105 #include "syssignal.h"
106
107 /* Define the structure that the wait system call stores.
108 On many systems, there is a structure defined for this.
109 But on vanilla-ish USG systems there is not. */
110
111 #ifndef VMS
112 #ifndef WAITTYPE
113 #if !defined (BSD) && !defined (UNIPLUS) && !defined (STRIDE) && !(defined (HPUX) && !defined (NOMULTIPLEJOBS)) && !defined (HAVE_WAIT_HEADER)
114 #define WAITTYPE int
115 #define WIFSTOPPED(w) ((w&0377) == 0177)
116 #define WIFSIGNALED(w) ((w&0377) != 0177 && (w&~0377) == 0)
117 #define WIFEXITED(w) ((w&0377) == 0)
118 #define WRETCODE(w) (w >> 8)
119 #define WSTOPSIG(w) (w >> 8)
120 #define WTERMSIG(w) (w & 0377)
121 #ifndef WCOREDUMP
122 #define WCOREDUMP(w) ((w&0200) != 0)
123 #endif
124 #else
125 #ifdef BSD4_1
126 #include <wait.h>
127 #else
128 #include <sys/wait.h>
129 #endif /* not BSD 4.1 */
130
131 #define WAITTYPE union wait
132 #define WRETCODE(w) w.w_retcode
133 #define WCOREDUMP(w) w.w_coredump
134
135 #ifdef HPUX
136 /* HPUX version 7 has broken definitions of these. */
137 #undef WTERMSIG
138 #undef WSTOPSIG
139 #undef WIFSTOPPED
140 #undef WIFSIGNALED
141 #undef WIFEXITED
142 #endif
143
144 #ifndef WTERMSIG
145 #define WTERMSIG(w) w.w_termsig
146 #endif
147 #ifndef WSTOPSIG
148 #define WSTOPSIG(w) w.w_stopsig
149 #endif
150 #ifndef WIFSTOPPED
151 #define WIFSTOPPED(w) (WTERMSIG (w) == 0177)
152 #endif
153 #ifndef WIFSIGNALED
154 #define WIFSIGNALED(w) (WTERMSIG (w) != 0177 && (WSTOPSIG (w)) == 0)
155 #endif
156 #ifndef WIFEXITED
157 #define WIFEXITED(w) (WTERMSIG (w) == 0)
158 #endif
159 #endif /* BSD or UNIPLUS or STRIDE */
160 #endif /* no WAITTYPE */
161 #else /* VMS */
162
163 /* For the CMU PTY driver + */
164 #define DCL_PROMPT "$ "
165
166 #include <ssdef.h>
167 #include <iodef.h>
168 #include <clidef.h>
169 #include "vmsproc.h"
170 #endif /* VMS */
171
172 extern errno;
173 extern sys_nerr;
174 extern char *sys_errlist[];
175
176 #ifndef VMS
177 #ifndef BSD4_1
178 extern char *sys_siglist[];
179 #else
180 char *sys_siglist[] =
181 {
182 "bum signal!!",
183 "hangup",
184 "interrupt",
185 "quit",
186 "illegal instruction",
187 "trace trap",
188 "iot instruction",
189 "emt instruction",
190 "floating point exception",
191 "kill",
192 "bus error",
193 "segmentation violation",
194 "bad argument to system call",
195 "write on a pipe with no one to read it",
196 "alarm clock",
197 "software termination signal from kill",
198 "status signal",
199 "sendable stop signal not from tty",
200 "stop signal from tty",
201 "continue a stopped process",
202 "child status has changed",
203 "background read attempted from control tty",
204 "background write attempted from control tty",
205 "input record available at control tty",
206 "exceeded CPU time limit",
207 "exceeded file size limit"
208 };
209 #endif
210 #endif /* VMS */
211
212 #ifdef vipc
213
214 #include "vipc.h"
215 extern int comm_server;
216 extern int net_listen_address;
217 #endif /* vipc */
218
219 /* t means use pty, nil means use a pipe,
220 maybe other values to come. */
221 Lisp_Object Vprocess_connection_type;
222
223 #ifdef SKTPAIR
224 #ifndef HAVE_SOCKETS
225 #include <sys/socket.h>
226 #endif
227 #endif /* SKTPAIR */
228
229 /* Number of events of change of status of a process. */
230 int process_tick;
231
232 /* Number of events for which the user or sentinel has been notified. */
233 int update_tick;
234
235 #ifdef FD_SET
236 /* We could get this from param.h, but better not to depend on finding that.
237 And better not to risk that it might define other symbols used in this
238 file. */
239 #define MAXDESC 64
240 #define SELECT_TYPE fd_set
241 #else /* no FD_SET */
242 #define MAXDESC 32
243 #define SELECT_TYPE int
244
245 /* Define the macros to access a single-int bitmap of descriptors. */
246 #define FD_SET(n, p) (*(p) |= (1 << (n)))
247 #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
248 #define FD_ISSET(n, p) (*(p) & (1 << (n)))
249 #define FD_ZERO(p) (*(p) = 0)
250 #endif /* no FD_SET */
251
252 /* Mask of bits indicating the descriptors that we wait for input on */
253
254 SELECT_TYPE input_wait_mask;
255
256 int delete_exited_processes;
257
258 /* Indexed by descriptor, gives the process (if any) for that descriptor */
259 Lisp_Object chan_process[MAXDESC];
260
261 /* Alist of elements (NAME . PROCESS) */
262 Lisp_Object Vprocess_alist;
263
264 Lisp_Object Qprocessp;
265
266 Lisp_Object get_process ();
267
268 /* Buffered-ahead input char from process, indexed by channel.
269 -1 means empty (no char is buffered).
270 Used on sys V where the only way to tell if there is any
271 output from the process is to read at least one char.
272 Always -1 on systems that support FIONREAD. */
273
274 int proc_buffered_char[MAXDESC];
275 \f
276 /* Compute the Lisp form of the process status, p->status, from
277 the numeric status that was returned by `wait'. */
278
279 Lisp_Object status_convert ();
280
281 update_status (p)
282 struct Lisp_Process *p;
283 {
284 union { int i; WAITTYPE wt; } u;
285 u.i = XFASTINT (p->raw_status_low) + (XFASTINT (p->raw_status_high) << 16);
286 p->status = status_convert (u.wt);
287 p->raw_status_low = Qnil;
288 p->raw_status_high = Qnil;
289 }
290
291 /* Convert a process status work in Unix format to
292 the list that we use internally. */
293
294 Lisp_Object
295 status_convert (w)
296 WAITTYPE w;
297 {
298 if (WIFSTOPPED (w))
299 return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil));
300 else if (WIFEXITED (w))
301 return Fcons (Qexit, Fcons (make_number (WRETCODE (w)),
302 WCOREDUMP (w) ? Qt : Qnil));
303 else if (WIFSIGNALED (w))
304 return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)),
305 WCOREDUMP (w) ? Qt : Qnil));
306 else
307 return Qrun;
308 }
309
310 /* Given a status-list, extract the three pieces of information
311 and store them individually through the three pointers. */
312
313 void
314 decode_status (l, symbol, code, coredump)
315 Lisp_Object l;
316 Lisp_Object *symbol;
317 int *code;
318 int *coredump;
319 {
320 Lisp_Object tem;
321
322 if (XTYPE (l) == Lisp_Symbol)
323 {
324 *symbol = l;
325 *code = 0;
326 *coredump = 0;
327 }
328 else
329 {
330 *symbol = XCONS (l)->car;
331 tem = XCONS (l)->cdr;
332 *code = XFASTINT (XCONS (tem)->car);
333 tem = XCONS (tem)->cdr;
334 *coredump = !NILP (tem);
335 }
336 }
337
338 /* Return a string describing a process status list. */
339
340 Lisp_Object
341 status_message (status)
342 Lisp_Object status;
343 {
344 Lisp_Object symbol;
345 int code, coredump;
346 Lisp_Object string, string2;
347
348 decode_status (status, &symbol, &code, &coredump);
349
350 if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
351 {
352 string = build_string (code < NSIG ? sys_siglist[code] : "unknown");
353 string2 = build_string (coredump ? " (core dumped)\n" : "\n");
354 XSTRING (string)->data[0] = DOWNCASE (XSTRING (string)->data[0]);
355 return concat2 (string, string2);
356 }
357 else if (EQ (symbol, Qexit))
358 {
359 if (code == 0)
360 return build_string ("finished\n");
361 string = Fint_to_string (make_number (code));
362 string2 = build_string (coredump ? " (core dumped)\n" : "\n");
363 return concat2 (build_string ("exited abnormally with code "),
364 concat2 (string, string2));
365 }
366 else
367 return Fcopy_sequence (Fsymbol_name (symbol));
368 }
369 \f
370 #ifdef HAVE_PTYS
371
372 /* Open an available pty, returning a file descriptor.
373 Return -1 on failure.
374 The file name of the terminal corresponding to the pty
375 is left in the variable pty_name. */
376
377 char pty_name[24];
378
379 int
380 allocate_pty ()
381 {
382 struct stat stb;
383 register c, i;
384 int fd;
385
386 /* Some systems name their pseudoterminals so that there are gaps in
387 the usual sequence - for example, on HP9000/S700 systems, there
388 are no pseudoterminals with names ending in 'f'. So we wait for
389 three failures in a row before deciding that we've reached the
390 end of the ptys. */
391 int failed_count = 0;
392
393 #ifdef PTY_ITERATION
394 PTY_ITERATION
395 #else
396 for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
397 for (i = 0; i < 16; i++)
398 #endif
399 {
400 #ifdef PTY_NAME_SPRINTF
401 PTY_NAME_SPRINTF
402 #else
403 sprintf (pty_name, "/dev/pty%c%x", c, i);
404 #endif /* no PTY_NAME_SPRINTF */
405
406 #ifdef PTY_OPEN
407 PTY_OPEN;
408 #else /* no PTY_OPEN */
409 #ifdef IRIS
410 /* Unusual IRIS code */
411 *ptyv = open ("/dev/ptc", O_RDWR | O_NDELAY, 0);
412 if (fd < 0)
413 return -1;
414 if (fstat (fd, &stb) < 0)
415 return -1;
416 #else /* not IRIS */
417 if (stat (pty_name, &stb) < 0)
418 {
419 failed_count++;
420 if (failed_count >= 3)
421 return -1;
422 }
423 else
424 failed_count = 0;
425 #ifdef O_NONBLOCK
426 fd = open (pty_name, O_RDWR | O_NONBLOCK, 0);
427 #else
428 fd = open (pty_name, O_RDWR | O_NDELAY, 0);
429 #endif
430 #endif /* not IRIS */
431 #endif /* no PTY_OPEN */
432
433 if (fd >= 0)
434 {
435 /* check to make certain that both sides are available
436 this avoids a nasty yet stupid bug in rlogins */
437 #ifdef PTY_TTY_NAME_SPRINTF
438 PTY_TTY_NAME_SPRINTF
439 #else
440 sprintf (pty_name, "/dev/tty%c%x", c, i);
441 #endif /* no PTY_TTY_NAME_SPRINTF */
442 #ifndef UNIPLUS
443 if (access (pty_name, 6) != 0)
444 {
445 close (fd);
446 #ifndef IRIS
447 continue;
448 #else
449 return -1;
450 #endif /* IRIS */
451 }
452 #endif /* not UNIPLUS */
453 setup_pty (fd);
454 return fd;
455 }
456 }
457 return -1;
458 }
459 #endif /* HAVE_PTYS */
460 \f
461 Lisp_Object
462 make_process (name)
463 Lisp_Object name;
464 {
465 register Lisp_Object val, tem, name1;
466 register struct Lisp_Process *p;
467 char suffix[10];
468 register int i;
469
470 /* size of process structure includes the vector header,
471 so deduct for that. But struct Lisp_Vector includes the first
472 element, thus deducts too much, so add it back. */
473 val = Fmake_vector (make_number ((sizeof (struct Lisp_Process)
474 - sizeof (struct Lisp_Vector)
475 + sizeof (Lisp_Object))
476 / sizeof (Lisp_Object)),
477 Qnil);
478 XSETTYPE (val, Lisp_Process);
479
480 p = XPROCESS (val);
481 XFASTINT (p->infd) = 0;
482 XFASTINT (p->outfd) = 0;
483 XFASTINT (p->pid) = 0;
484 XFASTINT (p->tick) = 0;
485 XFASTINT (p->update_tick) = 0;
486 p->raw_status_low = Qnil;
487 p->raw_status_high = Qnil;
488 p->status = Qrun;
489 p->mark = Fmake_marker ();
490
491 /* If name is already in use, modify it until it is unused. */
492
493 name1 = name;
494 for (i = 1; ; i++)
495 {
496 tem = Fget_process (name1);
497 if (NILP (tem)) break;
498 sprintf (suffix, "<%d>", i);
499 name1 = concat2 (name, build_string (suffix));
500 }
501 name = name1;
502 p->name = name;
503 Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
504 return val;
505 }
506
507 remove_process (proc)
508 register Lisp_Object proc;
509 {
510 register Lisp_Object pair;
511
512 pair = Frassq (proc, Vprocess_alist);
513 Vprocess_alist = Fdelq (pair, Vprocess_alist);
514 Fset_marker (XPROCESS (proc)->mark, Qnil, Qnil);
515
516 deactivate_process (proc);
517 }
518 \f
519 DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0,
520 "Return t if OBJECT is a process.")
521 (obj)
522 Lisp_Object obj;
523 {
524 return XTYPE (obj) == Lisp_Process ? Qt : Qnil;
525 }
526
527 DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0,
528 "Return the process named NAME, or nil if there is none.")
529 (name)
530 register Lisp_Object name;
531 {
532 if (XTYPE (name) == Lisp_Process)
533 return name;
534 CHECK_STRING (name, 0);
535 return Fcdr (Fassoc (name, Vprocess_alist));
536 }
537
538 DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
539 "Return the (or, a) process associated with BUFFER.\n\
540 BUFFER may be a buffer or the name of one.")
541 (name)
542 register Lisp_Object name;
543 {
544 register Lisp_Object buf, tail, proc;
545
546 if (NILP (name)) return Qnil;
547 buf = Fget_buffer (name);
548 if (NILP (buf)) return Qnil;
549
550 for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
551 {
552 proc = Fcdr (Fcar (tail));
553 if (XTYPE (proc) == Lisp_Process && EQ (XPROCESS (proc)->buffer, buf))
554 return proc;
555 }
556 return Qnil;
557 }
558
559 /* This is how commands for the user decode process arguments. It
560 accepts a process, a process name, a buffer, a buffer name, or nil.
561 Buffers denote the first process in the buffer, and nil denotes the
562 current buffer. */
563
564 Lisp_Object
565 get_process (name)
566 register Lisp_Object name;
567 {
568 register Lisp_Object proc;
569 if (NILP (name))
570 proc = Fget_buffer_process (Fcurrent_buffer ());
571 else
572 {
573 proc = Fget_process (name);
574 if (NILP (proc))
575 proc = Fget_buffer_process (Fget_buffer (name));
576 }
577
578 if (!NILP (proc))
579 return proc;
580
581 if (NILP (name))
582 error ("Current buffer has no process");
583 else
584 error ("Process %s does not exist", XSTRING (name)->data);
585 /* NOTREACHED */
586 }
587
588 DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0,
589 "Delete PROCESS: kill it and forget about it immediately.\n\
590 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
591 nil, indicating the current buffer's process.")
592 (proc)
593 register Lisp_Object proc;
594 {
595 proc = get_process (proc);
596 XPROCESS (proc)->raw_status_low = Qnil;
597 XPROCESS (proc)->raw_status_high = Qnil;
598 if (NETCONN_P (proc))
599 {
600 XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (0), Qnil));
601 XSETINT (XPROCESS (proc)->tick, ++process_tick);
602 }
603 else if (XFASTINT (XPROCESS (proc)->infd))
604 {
605 Fkill_process (proc, Qnil);
606 /* Do this now, since remove_process will make sigchld_handler do nothing. */
607 XPROCESS (proc)->status
608 = Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil));
609 XSETINT (XPROCESS (proc)->tick, ++process_tick);
610 status_notify ();
611 }
612 remove_process (proc);
613 return Qnil;
614 }
615 \f
616 DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0,
617 "Return the status of PROCESS: a symbol, one of these:\n\
618 run -- for a process that is running.\n\
619 stop -- for a process stopped but continuable.\n\
620 exit -- for a process that has exited.\n\
621 signal -- for a process that has got a fatal signal.\n\
622 open -- for a network stream connection that is open.\n\
623 closed -- for a network stream connection that is closed.\n\
624 nil -- if arg is a process name and no such process exists.\n\
625 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
626 nil, indicating the current buffer's process.")
627 /* command -- for a command channel opened to Emacs by another process.\n\
628 external -- for an i/o channel opened to Emacs by another process.\n\ */
629 (proc)
630 register Lisp_Object proc;
631 {
632 register struct Lisp_Process *p;
633 register Lisp_Object status;
634 proc = get_process (proc);
635 if (NILP (proc))
636 return proc;
637 p = XPROCESS (proc);
638 if (!NILP (p->raw_status_low))
639 update_status (p);
640 status = p->status;
641 if (XTYPE (status) == Lisp_Cons)
642 status = XCONS (status)->car;
643 if (NETCONN_P (proc))
644 {
645 if (EQ (status, Qrun))
646 status = Qopen;
647 else if (EQ (status, Qexit))
648 status = Qclosed;
649 }
650 return status;
651 }
652
653 DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status,
654 1, 1, 0,
655 "Return the exit status of PROCESS or the signal number that killed it.\n\
656 If PROCESS has not yet exited or died, return 0.")
657 (proc)
658 register Lisp_Object proc;
659 {
660 CHECK_PROCESS (proc, 0);
661 if (!NILP (XPROCESS (proc)->raw_status_low))
662 update_status (XPROCESS (proc));
663 if (XTYPE (XPROCESS (proc)->status) == Lisp_Cons)
664 return XCONS (XCONS (XPROCESS (proc)->status)->cdr)->car;
665 return make_number (0);
666 }
667
668 DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0,
669 "Return the process id of PROCESS.\n\
670 This is the pid of the Unix process which PROCESS uses or talks to.\n\
671 For a network connection, this value is nil.")
672 (proc)
673 register Lisp_Object proc;
674 {
675 CHECK_PROCESS (proc, 0);
676 return XPROCESS (proc)->pid;
677 }
678
679 DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0,
680 "Return the name of PROCESS, as a string.\n\
681 This is the name of the program invoked in PROCESS,\n\
682 possibly modified to make it unique among process names.")
683 (proc)
684 register Lisp_Object proc;
685 {
686 CHECK_PROCESS (proc, 0);
687 return XPROCESS (proc)->name;
688 }
689
690 DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0,
691 "Return the command that was executed to start PROCESS.\n\
692 This is a list of strings, the first string being the program executed\n\
693 and the rest of the strings being the arguments given to it.\n\
694 For a non-child channel, this is nil.")
695 (proc)
696 register Lisp_Object proc;
697 {
698 CHECK_PROCESS (proc, 0);
699 return XPROCESS (proc)->command;
700 }
701
702 DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer,
703 2, 2, 0,
704 "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
705 (proc, buffer)
706 register Lisp_Object proc, buffer;
707 {
708 CHECK_PROCESS (proc, 0);
709 if (!NILP (buffer))
710 CHECK_BUFFER (buffer, 1);
711 XPROCESS (proc)->buffer = buffer;
712 return buffer;
713 }
714
715 DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer,
716 1, 1, 0,
717 "Return the buffer PROCESS is associated with.\n\
718 Output from PROCESS is inserted in this buffer\n\
719 unless PROCESS has a filter.")
720 (proc)
721 register Lisp_Object proc;
722 {
723 CHECK_PROCESS (proc, 0);
724 return XPROCESS (proc)->buffer;
725 }
726
727 DEFUN ("process-mark", Fprocess_mark, Sprocess_mark,
728 1, 1, 0,
729 "Return the marker for the end of the last output from PROCESS.")
730 (proc)
731 register Lisp_Object proc;
732 {
733 CHECK_PROCESS (proc, 0);
734 return XPROCESS (proc)->mark;
735 }
736
737 DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter,
738 2, 2, 0,
739 "Give PROCESS the filter function FILTER; nil means no filter.\n\
740 When a process has a filter, each time it does output\n\
741 the entire string of output is passed to the filter.\n\
742 The filter gets two arguments: the process and the string of output.\n\
743 If the process has a filter, its buffer is not used for output.")
744 (proc, filter)
745 register Lisp_Object proc, filter;
746 {
747 CHECK_PROCESS (proc, 0);
748 XPROCESS (proc)->filter = filter;
749 return filter;
750 }
751
752 DEFUN ("process-filter", Fprocess_filter, Sprocess_filter,
753 1, 1, 0,
754 "Returns the filter function of PROCESS; nil if none.\n\
755 See `set-process-filter' for more info on filter functions.")
756 (proc)
757 register Lisp_Object proc;
758 {
759 CHECK_PROCESS (proc, 0);
760 return XPROCESS (proc)->filter;
761 }
762
763 DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel,
764 2, 2, 0,
765 "Give PROCESS the sentinel SENTINEL; nil for none.\n\
766 The sentinel is called as a function when the process changes state.\n\
767 It gets two arguments: the process, and a string describing the change.")
768 (proc, sentinel)
769 register Lisp_Object proc, sentinel;
770 {
771 CHECK_PROCESS (proc, 0);
772 XPROCESS (proc)->sentinel = sentinel;
773 return sentinel;
774 }
775
776 DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
777 1, 1, 0,
778 "Return the sentinel of PROCESS; nil if none.\n\
779 See `set-process-sentinel' for more info on sentinels.")
780 (proc)
781 register Lisp_Object proc;
782 {
783 CHECK_PROCESS (proc, 0);
784 return XPROCESS (proc)->sentinel;
785 }
786
787 DEFUN ("process-kill-without-query", Fprocess_kill_without_query,
788 Sprocess_kill_without_query, 1, 2, 0,
789 "Say no query needed if PROCESS is running when Emacs is exited.\n\
790 Optional second argument if non-nill says to require a query.\n\
791 Value is t if a query was formerly required.")
792 (proc, value)
793 register Lisp_Object proc, value;
794 {
795 Lisp_Object tem;
796
797 CHECK_PROCESS (proc, 0);
798 tem = XPROCESS (proc)->kill_without_query;
799 XPROCESS (proc)->kill_without_query = Fnull (value);
800
801 return Fnull (tem);
802 }
803 \f
804 Lisp_Object
805 list_processes_1 ()
806 {
807 register Lisp_Object tail, tem;
808 Lisp_Object proc, minspace, tem1;
809 register struct buffer *old = current_buffer;
810 register struct Lisp_Process *p;
811 register int state;
812 char tembuf[80];
813
814 XFASTINT (minspace) = 1;
815
816 set_buffer_internal (XBUFFER (Vstandard_output));
817 Fbuffer_disable_undo (Vstandard_output);
818
819 current_buffer->truncate_lines = Qt;
820
821 write_string ("\
822 Proc Status Buffer Command\n\
823 ---- ------ ------ -------\n", -1);
824
825 for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
826 {
827 Lisp_Object symbol;
828
829 proc = Fcdr (Fcar (tail));
830 p = XPROCESS (proc);
831 if (NILP (p->childp))
832 continue;
833
834 Finsert (1, &p->name);
835 Findent_to (make_number (13), minspace);
836
837 if (!NILP (p->raw_status_low))
838 update_status (p);
839 symbol = p->status;
840 if (XTYPE (p->status) == Lisp_Cons)
841 symbol = XCONS (p->status)->car;
842
843
844 if (EQ (symbol, Qsignal))
845 {
846 Lisp_Object tem;
847 tem = Fcar (Fcdr (p->status));
848 #ifdef VMS
849 if (XINT (tem) < NSIG)
850 write_string (sys_siglist [XINT (tem)], -1);
851 else
852 #endif
853 Fprinc (symbol, Qnil);
854 }
855 else if (NETCONN_P (proc))
856 {
857 if (EQ (symbol, Qrun))
858 write_string ("open", -1);
859 else if (EQ (symbol, Qexit))
860 write_string ("closed", -1);
861 else
862 Fprinc (symbol, Qnil);
863 }
864 else
865 Fprinc (symbol, Qnil);
866
867 if (EQ (symbol, Qexit))
868 {
869 Lisp_Object tem;
870 tem = Fcar (Fcdr (p->status));
871 if (XFASTINT (tem))
872 {
873 sprintf (tembuf, " %d", XFASTINT (tem));
874 write_string (tembuf, -1);
875 }
876 }
877
878 if (EQ (symbol, Qsignal) || EQ (symbol, Qexit))
879 remove_process (proc);
880
881 Findent_to (make_number (22), minspace);
882 if (NILP (p->buffer))
883 insert_string ("(none)");
884 else if (NILP (XBUFFER (p->buffer)->name))
885 insert_string ("(Killed)");
886 else
887 Finsert (1, &XBUFFER (p->buffer)->name);
888
889 Findent_to (make_number (37), minspace);
890
891 if (NETCONN_P (proc))
892 {
893 sprintf (tembuf, "(network stream connection to %s)\n",
894 XSTRING (p->childp)->data);
895 insert_string (tembuf);
896 }
897 else
898 {
899 tem = p->command;
900 while (1)
901 {
902 tem1 = Fcar (tem);
903 Finsert (1, &tem1);
904 tem = Fcdr (tem);
905 if (NILP (tem))
906 break;
907 insert_string (" ");
908 }
909 insert_string ("\n");
910 }
911 }
912 return Qnil;
913 }
914
915 DEFUN ("list-processes", Flist_processes, Slist_processes, 0, 0, "",
916 "Display a list of all processes.\n\
917 \(Any processes listed as Exited or Signaled are actually eliminated\n\
918 after the listing is made.)")
919 ()
920 {
921 internal_with_output_to_temp_buffer ("*Process List*",
922 list_processes_1, Qnil);
923 return Qnil;
924 }
925
926 DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
927 "Return a list of all processes.")
928 ()
929 {
930 return Fmapcar (Qcdr, Vprocess_alist);
931 }
932 \f
933 DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0,
934 "Start a program in a subprocess. Return the process object for it.\n\
935 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
936 NAME is name for process. It is modified if necessary to make it unique.\n\
937 BUFFER is the buffer or (buffer-name) to associate with the process.\n\
938 Process output goes at end of that buffer, unless you specify\n\
939 an output stream or filter function to handle the output.\n\
940 BUFFER may be also nil, meaning that this process is not associated\n\
941 with any buffer\n\
942 Third arg is program file name. It is searched for as in the shell.\n\
943 Remaining arguments are strings to give program as arguments.")
944 (nargs, args)
945 int nargs;
946 register Lisp_Object *args;
947 {
948 Lisp_Object buffer, name, program, proc, tem;
949 #ifdef VMS
950 register unsigned char *new_argv;
951 int len;
952 #else
953 register unsigned char **new_argv;
954 #endif
955 register int i;
956
957 buffer = args[1];
958 if (!NILP (buffer))
959 buffer = Fget_buffer_create (buffer);
960
961 name = args[0];
962 CHECK_STRING (name, 0);
963
964 program = args[2];
965
966 CHECK_STRING (program, 2);
967
968 #ifdef VMS
969 /* Make a one member argv with all args concatenated
970 together separated by a blank. */
971 len = XSTRING (program)->size + 2;
972 for (i = 3; i < nargs; i++)
973 {
974 tem = args[i];
975 CHECK_STRING (tem, i);
976 len += XSTRING (tem)->size + 1; /* count the blank */
977 }
978 new_argv = (unsigned char *) alloca (len);
979 strcpy (new_argv, XSTRING (program)->data);
980 for (i = 3; i < nargs; i++)
981 {
982 tem = args[i];
983 CHECK_STRING (tem, i);
984 strcat (new_argv, " ");
985 strcat (new_argv, XSTRING (tem)->data);
986 }
987 #else /* not VMS */
988 new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
989
990 for (i = 3; i < nargs; i++)
991 {
992 tem = args[i];
993 CHECK_STRING (tem, i);
994 new_argv[i - 2] = XSTRING (tem)->data;
995 }
996 new_argv[i - 2] = 0;
997 new_argv[0] = XSTRING (program)->data;
998
999 /* If program file name is not absolute, search our path for it */
1000 if (new_argv[0][0] != '/')
1001 {
1002 tem = Qnil;
1003 openp (Vexec_path, program, "", &tem, 1);
1004 if (NILP (tem))
1005 report_file_error ("Searching for program", Fcons (program, Qnil));
1006 new_argv[0] = XSTRING (tem)->data;
1007 }
1008 #endif /* not VMS */
1009
1010 proc = make_process (name);
1011
1012 XPROCESS (proc)->childp = Qt;
1013 XPROCESS (proc)->command_channel_p = Qnil;
1014 XPROCESS (proc)->buffer = buffer;
1015 XPROCESS (proc)->sentinel = Qnil;
1016 XPROCESS (proc)->filter = Qnil;
1017 XPROCESS (proc)->command = Flist (nargs - 2, args + 2);
1018
1019 create_process (proc, new_argv);
1020
1021 return proc;
1022 }
1023
1024 SIGTYPE
1025 create_process_1 (signo)
1026 int signo;
1027 {
1028 #ifdef USG
1029 /* USG systems forget handlers when they are used;
1030 must reestablish each time */
1031 signal (signo, create_process_1);
1032 #endif /* USG */
1033 }
1034
1035 #if 0 /* This doesn't work; see the note before sigchld_handler. */
1036 #ifdef USG
1037 #ifdef SIGCHLD
1038 /* Mimic blocking of signals on system V, which doesn't really have it. */
1039
1040 /* Nonzero means we got a SIGCHLD when it was supposed to be blocked. */
1041 int sigchld_deferred;
1042
1043 SIGTYPE
1044 create_process_sigchld ()
1045 {
1046 signal (SIGCHLD, create_process_sigchld);
1047
1048 sigchld_deferred = 1;
1049 }
1050 #endif
1051 #endif
1052 #endif
1053
1054 #ifndef VMS /* VMS version of this function is in vmsproc.c. */
1055 create_process (process, new_argv)
1056 Lisp_Object process;
1057 char **new_argv;
1058 {
1059 int pid, inchannel, outchannel, forkin, forkout;
1060 int sv[2];
1061 #ifdef SIGCHLD
1062 SIGTYPE (*sigchld)();
1063 #endif
1064 int pty_flag = 0;
1065 Lisp_Object current_dir;
1066 extern char **environ;
1067
1068 inchannel = outchannel = -1;
1069
1070 #ifdef HAVE_PTYS
1071 if (EQ (Vprocess_connection_type, Qt))
1072 outchannel = inchannel = allocate_pty ();
1073
1074 /* Make sure that the child will be able to chdir to the current
1075 buffer's current directory. We can't just have the child check
1076 for an error when it does the chdir, since it's in a vfork. */
1077 current_dir = expand_and_dir_to_file (current_buffer->directory, Qnil);
1078 if (NILP (Ffile_accessible_directory_p (current_dir)))
1079 report_file_error ("Setting current directory",
1080 Fcons (current_buffer->directory, Qnil));
1081
1082 if (inchannel >= 0)
1083 {
1084 #ifndef USG
1085 /* On USG systems it does not work to open the pty's tty here
1086 and then close and reopen it in the child. */
1087 #ifdef O_NOCTTY
1088 /* Don't let this terminal become our controlling terminal
1089 (in case we don't have one). */
1090 forkout = forkin = open (pty_name, O_RDWR | O_NOCTTY, 0);
1091 #else
1092 forkout = forkin = open (pty_name, O_RDWR, 0);
1093 #endif
1094 if (forkin < 0)
1095 report_file_error ("Opening pty", Qnil);
1096 #else
1097 forkin = forkout = -1;
1098 #endif /* not USG */
1099 pty_flag = 1;
1100 }
1101 else
1102 #endif /* HAVE_PTYS */
1103 #ifdef SKTPAIR
1104 {
1105 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) < 0)
1106 report_file_error ("Opening socketpair", Qnil);
1107 outchannel = inchannel = sv[0];
1108 forkout = forkin = sv[1];
1109 }
1110 #else /* not SKTPAIR */
1111 {
1112 pipe (sv);
1113 inchannel = sv[0];
1114 forkout = sv[1];
1115 pipe (sv);
1116 outchannel = sv[1];
1117 forkin = sv[0];
1118 }
1119 #endif /* not SKTPAIR */
1120
1121 #if 0
1122 /* Replaced by close_process_descs */
1123 set_exclusive_use (inchannel);
1124 set_exclusive_use (outchannel);
1125 #endif
1126
1127 /* Stride people say it's a mystery why this is needed
1128 as well as the O_NDELAY, but that it fails without this. */
1129 #if defined (STRIDE) || (defined (pfa) && defined (HAVE_PTYS))
1130 {
1131 int one = 1;
1132 ioctl (inchannel, FIONBIO, &one);
1133 }
1134 #endif
1135
1136 #ifdef O_NONBLOCK
1137 fcntl (inchannel, F_SETFL, O_NONBLOCK);
1138 #else
1139 #ifdef O_NDELAY
1140 fcntl (inchannel, F_SETFL, O_NDELAY);
1141 #endif
1142 #endif
1143
1144 /* Record this as an active process, with its channels.
1145 As a result, child_setup will close Emacs's side of the pipes. */
1146 chan_process[inchannel] = process;
1147 XFASTINT (XPROCESS (process)->infd) = inchannel;
1148 XFASTINT (XPROCESS (process)->outfd) = outchannel;
1149 /* Record the tty descriptor used in the subprocess. */
1150 if (forkin < 0)
1151 XPROCESS (process)->subtty = Qnil;
1152 else
1153 XFASTINT (XPROCESS (process)->subtty) = forkin;
1154 XPROCESS (process)->pty_flag = (pty_flag ? Qt : Qnil);
1155 XPROCESS (process)->status = Qrun;
1156
1157 /* Delay interrupts until we have a chance to store
1158 the new fork's pid in its process structure */
1159 #ifdef SIGCHLD
1160 #ifdef BSD4_1
1161 sighold (SIGCHLD);
1162 #else /* not BSD4_1 */
1163 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1164 sigsetmask (sigmask (SIGCHLD));
1165 #else /* ordinary USG */
1166 #if 0
1167 sigchld_deferred = 0;
1168 sigchld = signal (SIGCHLD, create_process_sigchld);
1169 #endif
1170 #endif /* ordinary USG */
1171 #endif /* not BSD4_1 */
1172 #endif /* SIGCHLD */
1173
1174 /* Until we store the proper pid, enable sigchld_handler
1175 to recognize an unknown pid as standing for this process.
1176 It is very important not to let this `marker' value stay
1177 in the table after this function has returned; if it does
1178 it might cause call-process to hang and subsequent asynchronous
1179 processes to get their return values scrambled. */
1180 XSETINT (XPROCESS (process)->pid, -1);
1181
1182 {
1183 /* child_setup must clobber environ on systems with true vfork.
1184 Protect it from permanent change. */
1185 char **save_environ = environ;
1186
1187 pid = vfork ();
1188 if (pid == 0)
1189 {
1190 int xforkin = forkin;
1191 int xforkout = forkout;
1192
1193 #if 0 /* This was probably a mistake--it duplicates code later on,
1194 but fails to handle all the cases. */
1195 /* Make sure SIGCHLD is not blocked in the child. */
1196 sigsetmask (SIGEMPTYMASK);
1197 #endif
1198
1199 /* Make the pty be the controlling terminal of the process. */
1200 #ifdef HAVE_PTYS
1201 /* First, disconnect its current controlling terminal. */
1202 #ifdef HAVE_SETSID
1203 setsid ();
1204 #ifdef TIOCSCTTY
1205 /* Make the pty's terminal the controlling terminal. */
1206 if (pty_flag && (ioctl (xforkin, TIOCSCTTY, 0) < 0))
1207 abort ();
1208 #endif
1209 #else /* not HAVE_SETSID */
1210 #ifdef USG
1211 /* It's very important to call setpgrp() here and no time
1212 afterwards. Otherwise, we lose our controlling tty which
1213 is set when we open the pty. */
1214 setpgrp ();
1215 #endif /* USG */
1216 #endif /* not HAVE_SETSID */
1217 #ifdef TIOCNOTTY
1218 /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
1219 can do TIOCSPGRP only to the process's controlling tty. */
1220 if (pty_flag)
1221 {
1222 /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
1223 I can't test it since I don't have 4.3. */
1224 int j = open ("/dev/tty", O_RDWR, 0);
1225 ioctl (j, TIOCNOTTY, 0);
1226 close (j);
1227 #ifndef USG
1228 /* In order to get a controlling terminal on some versions
1229 of BSD, it is necessary to put the process in pgrp 0
1230 before it opens the terminal. */
1231 setpgrp (0, 0);
1232 #endif
1233 }
1234 #endif /* TIOCNOTTY */
1235
1236 #if !defined (RTU) && !defined (UNIPLUS)
1237 /*** There is a suggestion that this ought to be a
1238 conditional on TIOCSPGRP. */
1239 /* Now close the pty (if we had it open) and reopen it.
1240 This makes the pty the controlling terminal of the subprocess. */
1241 if (pty_flag)
1242 {
1243 /* I wonder if close (open (pty_name, ...)) would work? */
1244 if (xforkin >= 0)
1245 close (xforkin);
1246 xforkout = xforkin = open (pty_name, O_RDWR, 0);
1247
1248 if (xforkin < 0)
1249 abort ();
1250 }
1251 #endif /* not UNIPLUS and not RTU */
1252 #ifdef SETUP_SLAVE_PTY
1253 SETUP_SLAVE_PTY;
1254 #endif /* SETUP_SLAVE_PTY */
1255 #ifdef AIX
1256 /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
1257 Now reenable it in the child, so it will die when we want it to. */
1258 if (pty_flag)
1259 signal (SIGHUP, SIG_DFL);
1260 #endif
1261 #endif /* HAVE_PTYS */
1262
1263 #ifdef SIGCHLD
1264 #ifdef BSD4_1
1265 sigrelse (SIGCHLD);
1266 #else /* not BSD4_1 */
1267 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1268 sigsetmask (SIGEMPTYMASK);
1269 #else /* ordinary USG */
1270 #if 0
1271 signal (SIGCHLD, sigchld);
1272 #endif
1273 #endif /* ordinary USG */
1274 #endif /* not BSD4_1 */
1275 #endif /* SIGCHLD */
1276
1277 child_setup_tty (xforkout);
1278 child_setup (xforkin, xforkout, xforkout,
1279 new_argv, 1, current_dir);
1280 }
1281 environ = save_environ;
1282 }
1283
1284 if (pid < 0)
1285 {
1286 remove_process (process);
1287 report_file_error ("Doing vfork", Qnil);
1288 }
1289
1290 XFASTINT (XPROCESS (process)->pid) = pid;
1291
1292 FD_SET (inchannel, &input_wait_mask);
1293
1294 /* If the subfork execv fails, and it exits,
1295 this close hangs. I don't know why.
1296 So have an interrupt jar it loose. */
1297 stop_polling ();
1298 signal (SIGALRM, create_process_1);
1299 alarm (1);
1300 #ifdef SYSV4_PTYS
1301 /* OK to close only if it's not a pty. Otherwise we need to leave
1302 it open for ioctl to get pgrp when signals are sent, or to send
1303 the interrupt characters through if that's how we're signalling
1304 subprocesses. Alternately if you are concerned about running out
1305 of file descriptors, you could just save the tty name and open
1306 just to do the ioctl. */
1307 if (NILP (XFASTINT (XPROCESS (process)->pty_flag)))
1308 #endif
1309 {
1310 XPROCESS (process)->subtty = Qnil;
1311 if (forkin >= 0)
1312 close (forkin);
1313 }
1314 alarm (0);
1315 start_polling ();
1316 if (forkin != forkout && forkout >= 0)
1317 close (forkout);
1318
1319 #ifdef SIGCHLD
1320 #ifdef BSD4_1
1321 sigrelse (SIGCHLD);
1322 #else /* not BSD4_1 */
1323 #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
1324 sigsetmask (SIGEMPTYMASK);
1325 #else /* ordinary USG */
1326 #if 0
1327 signal (SIGCHLD, sigchld);
1328 /* Now really handle any of these signals
1329 that came in during this function. */
1330 if (sigchld_deferred)
1331 kill (getpid (), SIGCHLD);
1332 #endif
1333 #endif /* ordinary USG */
1334 #endif /* not BSD4_1 */
1335 #endif /* SIGCHLD */
1336 }
1337 #endif /* not VMS */
1338
1339 #ifdef HAVE_SOCKETS
1340
1341 /* open a TCP network connection to a given HOST/SERVICE. Treated
1342 exactly like a normal process when reading and writing. Only
1343 differences are in status display and process deletion. A network
1344 connection has no PID; you cannot signal it. All you can do is
1345 deactivate and close it via delete-process */
1346
1347 DEFUN ("open-network-stream", Fopen_network_stream, Sopen_network_stream,
1348 4, 4, 0,
1349 "Open a TCP connection for a service to a host.\n\
1350 Returns a subprocess-object to represent the connection.\n\
1351 Input and output work as for subprocesses; `delete-process' closes it.\n\
1352 Args are NAME BUFFER HOST SERVICE.\n\
1353 NAME is name for process. It is modified if necessary to make it unique.\n\
1354 BUFFER is the buffer (or buffer-name) to associate with the process.\n\
1355 Process output goes at end of that buffer, unless you specify\n\
1356 an output stream or filter function to handle the output.\n\
1357 BUFFER may be also nil, meaning that this process is not associated\n\
1358 with any buffer\n\
1359 Third arg is name of the host to connect to, or its IP address.\n\
1360 Fourth arg SERVICE is name of the service desired, or an integer\n\
1361 specifying a port number to connect to.")
1362 (name, buffer, host, service)
1363 Lisp_Object name, buffer, host, service;
1364 {
1365 Lisp_Object proc;
1366 register int i;
1367 struct sockaddr_in address;
1368 struct servent *svc_info;
1369 struct hostent *host_info_ptr, host_info;
1370 char *(addr_list[2]);
1371 unsigned long numeric_addr;
1372 int s, outch, inch;
1373 char errstring[80];
1374 int port;
1375 struct hostent host_info_fixed;
1376 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
1377
1378 GCPRO4 (name, buffer, host, service);
1379 CHECK_STRING (name, 0);
1380 CHECK_STRING (host, 0);
1381 if (XTYPE(service) == Lisp_Int)
1382 port = htons ((unsigned short) XINT (service));
1383 else
1384 {
1385 CHECK_STRING (service, 0);
1386 svc_info = getservbyname (XSTRING (service)->data, "tcp");
1387 if (svc_info == 0)
1388 error ("Unknown service \"%s\"", XSTRING (service)->data);
1389 port = svc_info->s_port;
1390 }
1391
1392 host_info_ptr = gethostbyname (XSTRING (host)->data);
1393 if (host_info_ptr == 0)
1394 /* Attempt to interpret host as numeric inet address */
1395 {
1396 numeric_addr = inet_addr (XSTRING (host)->data);
1397 if (numeric_addr == -1)
1398 error ("Unknown host \"%s\"", XSTRING (host)->data);
1399
1400 host_info_ptr = &host_info;
1401 host_info.h_name = 0;
1402 host_info.h_aliases = 0;
1403 host_info.h_addrtype = AF_INET;
1404 host_info.h_addr_list = &(addr_list[0]);
1405 addr_list[0] = (char*)(&numeric_addr);
1406 addr_list[1] = 0;
1407 host_info.h_length = strlen (addr_list[0]);
1408 }
1409
1410 bzero (&address, sizeof address);
1411 bcopy (host_info_ptr->h_addr, (char *) &address.sin_addr,
1412 host_info_ptr->h_length);
1413 address.sin_family = host_info_ptr->h_addrtype;
1414 address.sin_port = port;
1415
1416 s = socket (host_info_ptr->h_addrtype, SOCK_STREAM, 0);
1417 if (s < 0)
1418 report_file_error ("error creating socket", Fcons (name, Qnil));
1419
1420 loop:
1421 if (connect (s, &address, sizeof address) == -1)
1422 {
1423 int xerrno = errno;
1424 if (errno == EINTR)
1425 goto loop;
1426 close (s);
1427 errno = xerrno;
1428 report_file_error ("connection failed",
1429 Fcons (host, Fcons (name, Qnil)));
1430 }
1431
1432 inch = s;
1433 outch = dup (s);
1434 if (outch < 0)
1435 report_file_error ("error duplicating socket", Fcons (name, Qnil));
1436
1437 if (!NILP (buffer))
1438 buffer = Fget_buffer_create (buffer);
1439 proc = make_process (name);
1440
1441 chan_process[inch] = proc;
1442
1443 #ifdef O_NONBLOCK
1444 fcntl (inch, F_SETFL, O_NONBLOCK);
1445 #else
1446 #ifdef O_NDELAY
1447 fcntl (inch, F_SETFL, O_NDELAY);
1448 #endif
1449 #endif
1450
1451 XPROCESS (proc)->childp = host;
1452 XPROCESS (proc)->command_channel_p = Qnil;
1453 XPROCESS (proc)->buffer = buffer;
1454 XPROCESS (proc)->sentinel = Qnil;
1455 XPROCESS (proc)->filter = Qnil;
1456 XPROCESS (proc)->command = Qnil;
1457 XPROCESS (proc)->pid = Qnil;
1458 XFASTINT (XPROCESS (proc)->infd) = s;
1459 XFASTINT (XPROCESS (proc)->outfd) = outch;
1460 XPROCESS (proc)->status = Qrun;
1461 FD_SET (inch, &input_wait_mask);
1462
1463 UNGCPRO;
1464 return proc;
1465 }
1466 #endif /* HAVE_SOCKETS */
1467
1468 deactivate_process (proc)
1469 Lisp_Object proc;
1470 {
1471 register int inchannel, outchannel;
1472 register struct Lisp_Process *p = XPROCESS (proc);
1473
1474 inchannel = XFASTINT (p->infd);
1475 outchannel = XFASTINT (p->outfd);
1476
1477 if (inchannel)
1478 {
1479 /* Beware SIGCHLD hereabouts. */
1480 flush_pending_output (inchannel);
1481 #ifdef VMS
1482 {
1483 VMS_PROC_STUFF *get_vms_process_pointer (), *vs;
1484 sys$dassgn (outchannel);
1485 vs = get_vms_process_pointer (p->pid)
1486 if (vs)
1487 give_back_vms_process_stuff (vs);
1488 }
1489 #else
1490 close (inchannel);
1491 if (outchannel && outchannel != inchannel)
1492 close (outchannel);
1493 #endif
1494
1495 XFASTINT (p->infd) = 0;
1496 XFASTINT (p->outfd) = 0;
1497 chan_process[inchannel] = Qnil;
1498 FD_CLR (inchannel, &input_wait_mask);
1499 }
1500 }
1501
1502 /* Close all descriptors currently in use for communication
1503 with subprocess. This is used in a newly-forked subprocess
1504 to get rid of irrelevant descriptors. */
1505
1506 close_process_descs ()
1507 {
1508 int i;
1509 for (i = 0; i < MAXDESC; i++)
1510 {
1511 Lisp_Object process;
1512 process = chan_process[i];
1513 if (!NILP (process))
1514 {
1515 int in = XFASTINT (XPROCESS (process)->infd);
1516 int out = XFASTINT (XPROCESS (process)->outfd);
1517 if (in)
1518 close (in);
1519 if (out && in != out)
1520 close (out);
1521 }
1522 }
1523 }
1524 \f
1525 DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output,
1526 0, 3, 0,
1527 "Allow any pending output from subprocesses to be read by Emacs.\n\
1528 It is read into the process' buffers or given to their filter functions.\n\
1529 Non-nil arg PROCESS means do not return until some output has been received\n\
1530 from PROCESS.\n\
1531 Non-nil second arg TIMEOUT and third arg TIMEOUT-MSECS are number of\n\
1532 seconds and microseconds to wait; return after that much time whether\n\
1533 or not there is input.\n\
1534 Return non-nil iff we received any output before the timeout expired.")
1535 (proc, timeout, timeout_msecs)
1536 register Lisp_Object proc, timeout, timeout_msecs;
1537 {
1538 int seconds;
1539 int useconds;
1540
1541 if (! NILP (timeout_msecs))
1542 {
1543 CHECK_NUMBER (timeout_msecs, 2);
1544 useconds = XINT (timeout_msecs);
1545 if (XTYPE (timeout) != Lisp_Int)
1546 XSET (timeout, Lisp_Int, 0);
1547
1548 {
1549 int carry = useconds / 1000000;
1550
1551 XSETINT (timeout, XINT (timeout) + carry);
1552 useconds -= carry * 1000000;
1553
1554 /* I think this clause is necessary because C doesn't
1555 guarantee a particular rounding direction for negative
1556 integers. */
1557 if (useconds < 0)
1558 {
1559 XSETINT (timeout, XINT (timeout) - 1);
1560 useconds += 1000000;
1561 }
1562 }
1563 }
1564 else
1565 useconds = 0;
1566
1567 if (! NILP (timeout))
1568 {
1569 CHECK_NUMBER (timeout, 1);
1570 seconds = XINT (timeout);
1571 if (seconds <= 0)
1572 seconds = -1;
1573 }
1574 else
1575 {
1576 if (NILP (proc))
1577 seconds = -1;
1578 else
1579 seconds = 0;
1580 }
1581
1582 if (NILP (proc))
1583 XFASTINT (proc) = 0;
1584
1585 return
1586 (wait_reading_process_input (seconds, useconds, proc, 0)
1587 ? Qt : Qnil);
1588 }
1589
1590 /* This variable is different from waiting_for_input in keyboard.c.
1591 It is used to communicate to a lisp process-filter/sentinel (via the
1592 function Fwaiting_for_user_input_p below) whether emacs was waiting
1593 for user-input when that process-filter was called.
1594 waiting_for_input cannot be used as that is by definition 0 when
1595 lisp code is being evalled */
1596 static int waiting_for_user_input_p;
1597
1598 /* Read and dispose of subprocess output while waiting for timeout to
1599 elapse and/or keyboard input to be available.
1600
1601 time_limit is:
1602 timeout in seconds, or
1603 zero for no limit, or
1604 -1 means gobble data immediately available but don't wait for any.
1605
1606 read_kbd is a lisp value:
1607 0 to ignore keyboard input, or
1608 1 to return when input is available, or
1609 -1 means caller will actually read the input, so don't throw to
1610 the quit handler, or
1611 a process object, meaning wait until something arrives from that
1612 process. The return value is true iff we read some input from
1613 that process.
1614
1615 do_display != 0 means redisplay should be done to show subprocess
1616 output that arrives.
1617
1618 If read_kbd is a pointer to a struct Lisp_Process, then the
1619 function returns true iff we received input from that process
1620 before the timeout elapsed.
1621 Otherwise, return true iff we recieved input from any process. */
1622
1623 wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
1624 int time_limit, microsecs;
1625 Lisp_Object read_kbd;
1626 int do_display;
1627 {
1628 register int channel, nfds, m;
1629 static SELECT_TYPE Available;
1630 int xerrno;
1631 Lisp_Object proc;
1632 EMACS_TIME timeout, end_time, garbage;
1633 SELECT_TYPE Atemp;
1634 int wait_channel = 0;
1635 struct Lisp_Process *wait_proc = 0;
1636 int got_some_input = 0;
1637
1638 FD_ZERO (&Available);
1639
1640 /* If read_kbd is a process to watch, set wait_proc and wait_channel
1641 accordingly. */
1642 if (XTYPE (read_kbd) == Lisp_Process)
1643 {
1644 wait_proc = XPROCESS (read_kbd);
1645 wait_channel = XFASTINT (wait_proc->infd);
1646 XFASTINT (read_kbd) = 0;
1647 }
1648
1649 waiting_for_user_input_p = XINT (read_kbd);
1650
1651 /* Since we may need to wait several times,
1652 compute the absolute time to return at. */
1653 if (time_limit || microsecs)
1654 {
1655 EMACS_GET_TIME (end_time);
1656 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
1657 EMACS_ADD_TIME (end_time, end_time, timeout);
1658 }
1659
1660 while (1)
1661 {
1662 /* If calling from keyboard input, do not quit
1663 since we want to return C-g as an input character.
1664 Otherwise, do pending quit if requested. */
1665 if (XINT (read_kbd) >= 0)
1666 QUIT;
1667
1668 /* If status of something has changed, and no input is available,
1669 notify the user of the change right away */
1670 if (update_tick != process_tick && do_display)
1671 {
1672 Atemp = input_wait_mask;
1673 EMACS_SET_SECS_USECS (timeout, 0, 0);
1674 if (select (MAXDESC, &Atemp, 0, 0, &timeout) <= 0)
1675 status_notify ();
1676 }
1677
1678 /* Don't wait for output from a non-running process. */
1679 if (wait_proc != 0 && !NILP (wait_proc->raw_status_low))
1680 update_status (wait_proc);
1681 if (wait_proc != 0
1682 && ! EQ (wait_proc->status, Qrun))
1683 break;
1684
1685 /* Compute time from now till when time limit is up */
1686 /* Exit if already run out */
1687 if (time_limit == -1)
1688 {
1689 /* -1 specified for timeout means
1690 gobble output available now
1691 but don't wait at all. */
1692
1693 EMACS_SET_SECS_USECS (timeout, 0, 0);
1694 }
1695 else if (time_limit || microsecs)
1696 {
1697 EMACS_GET_TIME (timeout);
1698 EMACS_SUB_TIME (timeout, end_time, timeout);
1699 if (EMACS_TIME_NEG_P (timeout))
1700 break;
1701 }
1702 else
1703 {
1704 EMACS_SET_SECS_USECS (timeout, 100000, 0);
1705 }
1706
1707 /* Cause C-g and alarm signals to take immediate action,
1708 and cause input available signals to zero out timeout */
1709 if (XINT (read_kbd) < 0)
1710 set_waiting_for_input (&timeout);
1711
1712 /* Wait till there is something to do */
1713
1714 Available = input_wait_mask;
1715 if (! XINT (read_kbd))
1716 FD_CLR (0, &Available);
1717
1718 /* If frame size has changed or the window is newly mapped,
1719 redisplay now, before we start to wait. There is a race
1720 condition here; if a SIGIO arrives between now and the select
1721 and indicates that a frame is trashed, we lose. */
1722 if (frame_garbaged)
1723 redisplay_preserve_echo_area ();
1724
1725 if (XINT (read_kbd) && detect_input_pending ())
1726 nfds = 0;
1727 else
1728 nfds = select (MAXDESC, &Available, 0, 0, &timeout);
1729
1730 xerrno = errno;
1731
1732 /* Make C-g and alarm signals set flags again */
1733 clear_waiting_for_input ();
1734
1735 /* If we woke up due to SIGWINCH, actually change size now. */
1736 do_pending_window_change ();
1737
1738 if (time_limit && nfds == 0) /* timeout elapsed */
1739 break;
1740 if (nfds < 0)
1741 {
1742 if (xerrno == EINTR)
1743 FD_ZERO (&Available);
1744 #ifdef __ultrix__
1745 /* Ultrix select seems to return ENOMEM when it is interrupted.
1746 Treat it just like EINTR. Bleah. -JimB */
1747 else if (xerrno == ENOMEM)
1748 FD_ZERO (&Available);
1749 #endif
1750 #ifdef ALLIANT
1751 /* This happens for no known reason on ALLIANT.
1752 I am guessing that this is the right response. -- RMS. */
1753 else if (xerrno == EFAULT)
1754 FD_ZERO (&Available);
1755 #endif
1756 else if (xerrno == EBADF)
1757 {
1758 #ifdef AIX
1759 /* AIX doesn't handle PTY closure the same way BSD does. On AIX,
1760 the child's closure of the pts gives the parent a SIGHUP, and
1761 the ptc file descriptor is automatically closed,
1762 yielding EBADF here or at select() call above.
1763 So, SIGHUP is ignored (see def of PTY_TTY_NAME_SPRINTF
1764 in m-ibmrt-aix.h), and here we just ignore the select error.
1765 Cleanup occurs c/o status_notify after SIGCLD. */
1766 FD_ZERO (&Available); /* Cannot depend on values returned */
1767 #else
1768 abort ();
1769 #endif
1770 }
1771 else
1772 error("select error: %s", sys_errlist[xerrno]);
1773 }
1774 #ifdef sun
1775 else if (nfds > 0 && FD_ISSET (0, &Available) && interrupt_input)
1776 /* System sometimes fails to deliver SIGIO. */
1777 kill (getpid (), SIGIO);
1778 #endif
1779
1780 /* Check for keyboard input */
1781 /* If there is any, return immediately
1782 to give it higher priority than subprocesses */
1783
1784 if (XINT (read_kbd) && detect_input_pending ())
1785 break;
1786
1787 #ifdef SIGIO
1788 /* If we think we have keyboard input waiting, but didn't get SIGIO
1789 go read it. This can happen with X on BSD after logging out.
1790 In that case, there really is no input and no SIGIO,
1791 but select says there is input. */
1792
1793 /*
1794 if (XINT (read_kbd) && interrupt_input && (Available & fileno (stdin)))
1795 */
1796 if (XINT (read_kbd) && interrupt_input && (FD_ISSET (fileno (stdin), &Available)))
1797 kill (0, SIGIO);
1798 #endif
1799
1800 #ifdef vipc
1801 /* Check for connection from other process */
1802
1803 if (Available & ChannelMask (comm_server))
1804 {
1805 Available &= ~(ChannelMask (comm_server));
1806 create_commchan ();
1807 }
1808 #endif /* vipc */
1809
1810 if (! wait_proc)
1811 got_some_input |= nfds > 0;
1812
1813 /* If checking input just got us a size-change event from X,
1814 obey it now if we should. */
1815 if (XINT (read_kbd))
1816 do_pending_window_change ();
1817
1818 /* Check for data from a process or a command channel */
1819 for (channel = FIRST_PROC_DESC; channel < MAXDESC; channel++)
1820 {
1821 if (FD_ISSET (channel, &Available))
1822 {
1823 int nread;
1824
1825 /* If waiting for this channel, arrange to return as
1826 soon as no more input to be processed. No more
1827 waiting. */
1828 if (wait_channel == channel)
1829 {
1830 wait_channel = 0;
1831 time_limit = -1;
1832 got_some_input = 1;
1833 }
1834 proc = chan_process[channel];
1835 if (NILP (proc))
1836 continue;
1837
1838 #ifdef vipc
1839 /* It's a command channel */
1840 if (!NILP (XPROCESS (proc)->command_channel_p))
1841 {
1842 ProcessCommChan (channel, proc);
1843 if (NILP (XPROCESS (proc)->command_channel_p))
1844 {
1845 /* It has ceased to be a command channel! */
1846 int bytes_available;
1847 if (ioctl (channel, FIONREAD, &bytes_available) < 0)
1848 bytes_available = 0;
1849 if (bytes_available)
1850 FD_SET (channel, &Available);
1851 }
1852 continue;
1853 }
1854 #endif /* vipc */
1855
1856 /* Read data from the process, starting with our
1857 buffered-ahead character if we have one. */
1858
1859 nread = read_process_output (proc, channel);
1860 if (nread > 0)
1861 {
1862 /* Since read_process_output can run a filter,
1863 which can call accept-process-output,
1864 don't try to read from any other processes
1865 before doing the select again. */
1866 FD_ZERO (&Available);
1867
1868 if (do_display)
1869 redisplay_preserve_echo_area ();
1870 }
1871 #ifdef EWOULDBLOCK
1872 else if (nread == -1 && errno == EWOULDBLOCK)
1873 ;
1874 #else
1875 #ifdef O_NONBLOCK
1876 else if (nread == -1 && errno == EAGAIN)
1877 ;
1878 #else
1879 #ifdef O_NDELAY
1880 else if (nread == -1 && errno == EAGAIN)
1881 ;
1882 /* Note that we cannot distinguish between no input
1883 available now and a closed pipe.
1884 With luck, a closed pipe will be accompanied by
1885 subprocess termination and SIGCHLD. */
1886 else if (nread == 0 && !NETCONN_P (proc))
1887 ;
1888 #endif /* O_NDELAY */
1889 #endif /* O_NONBLOCK */
1890 #endif /* EWOULDBLOCK */
1891 #ifdef HAVE_PTYS
1892 /* On some OSs with ptys, when the process on one end of
1893 a pty exits, the other end gets an error reading with
1894 errno = EIO instead of getting an EOF (0 bytes read).
1895 Therefore, if we get an error reading and errno =
1896 EIO, just continue, because the child process has
1897 exited and should clean itself up soon (e.g. when we
1898 get a SIGCHLD). */
1899 else if (nread == -1 && errno == EIO)
1900 ;
1901 #endif /* HAVE_PTYS */
1902 /* If we can detect process termination, don't consider the process
1903 gone just because its pipe is closed. */
1904 #ifdef SIGCHLD
1905 else if (nread == 0 && !NETCONN_P (proc))
1906 ;
1907 #endif
1908 else
1909 {
1910 /* Preserve status of processes already terminated. */
1911 XSETINT (XPROCESS (proc)->tick, ++process_tick);
1912 deactivate_process (proc);
1913 if (!NILP (XPROCESS (proc)->raw_status_low))
1914 update_status (XPROCESS (proc));
1915 if (EQ (XPROCESS (proc)->status, Qrun))
1916 XPROCESS (proc)->status
1917 = Fcons (Qexit, Fcons (make_number (256), Qnil));
1918 }
1919 }
1920 } /* end for each file descriptor */
1921 } /* end while exit conditions not met */
1922
1923 /* If calling from keyboard input, do not quit
1924 since we want to return C-g as an input character.
1925 Otherwise, do pending quit if requested. */
1926 if (XINT (read_kbd) >= 0)
1927 {
1928 /* Prevent input_pending from remaining set if we quit. */
1929 clear_input_pending ();
1930 QUIT;
1931 }
1932
1933 return got_some_input;
1934 }
1935 \f
1936 /* Read pending output from the process channel,
1937 starting with our buffered-ahead character if we have one.
1938 Yield number of characters read.
1939
1940 This function reads at most 1024 characters.
1941 If you want to read all available subprocess output,
1942 you must call it repeatedly until it returns zero. */
1943
1944 read_process_output (proc, channel)
1945 Lisp_Object proc;
1946 register int channel;
1947 {
1948 register int nchars;
1949 #ifdef VMS
1950 char *chars;
1951 #else
1952 char chars[1024];
1953 #endif
1954 register Lisp_Object outstream;
1955 register struct buffer *old = current_buffer;
1956 register struct Lisp_Process *p = XPROCESS (proc);
1957 register int opoint;
1958
1959 #ifdef VMS
1960 VMS_PROC_STUFF *vs, *get_vms_process_pointer();
1961
1962 vs = get_vms_process_pointer (p->pid);
1963 if (vs)
1964 {
1965 if (!vs->iosb[0])
1966 return(0); /* Really weird if it does this */
1967 if (!(vs->iosb[0] & 1))
1968 return -1; /* I/O error */
1969 }
1970 else
1971 error ("Could not get VMS process pointer");
1972 chars = vs->inputBuffer;
1973 nchars = clean_vms_buffer (chars, vs->iosb[1]);
1974 if (nchars <= 0)
1975 {
1976 start_vms_process_read (vs); /* Crank up the next read on the process */
1977 return 1; /* Nothing worth printing, say we got 1 */
1978 }
1979 #else /* not VMS */
1980
1981 if (proc_buffered_char[channel] < 0)
1982 nchars = read (channel, chars, sizeof chars);
1983 else
1984 {
1985 chars[0] = proc_buffered_char[channel];
1986 proc_buffered_char[channel] = -1;
1987 nchars = read (channel, chars + 1, sizeof chars - 1);
1988 if (nchars < 0)
1989 nchars = 1;
1990 else
1991 nchars = nchars + 1;
1992 }
1993 #endif /* not VMS */
1994
1995 if (nchars <= 0) return nchars;
1996
1997 outstream = p->filter;
1998 if (!NILP (outstream))
1999 {
2000 /* We inhibit quit here instead of just catching it so that
2001 hitting ^G when a filter happens to be running won't screw
2002 it up. */
2003 int count = specpdl_ptr - specpdl;
2004 specbind (Qinhibit_quit, Qt);
2005 call2 (outstream, proc, make_string (chars, nchars));
2006
2007 #ifdef VMS
2008 start_vms_process_read (vs);
2009 #endif
2010 unbind_to (count);
2011 return nchars;
2012 }
2013
2014 /* If no filter, write into buffer if it isn't dead. */
2015 if (!NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name))
2016 {
2017 Lisp_Object tem;
2018
2019 Fset_buffer (p->buffer);
2020 opoint = point;
2021
2022 /* Insert new output into buffer
2023 at the current end-of-output marker,
2024 thus preserving logical ordering of input and output. */
2025 if (XMARKER (p->mark)->buffer)
2026 SET_PT (marker_position (p->mark));
2027 else
2028 SET_PT (ZV);
2029 if (point <= opoint)
2030 opoint += nchars;
2031
2032 tem = current_buffer->read_only;
2033 current_buffer->read_only = Qnil;
2034 /* Insert before markers in case we are inserting where
2035 the buffer's mark is, and the user's next command is Meta-y. */
2036 insert_before_markers (chars, nchars);
2037 current_buffer->read_only = tem;
2038 Fset_marker (p->mark, make_number (point), p->buffer);
2039 update_mode_lines++;
2040
2041 SET_PT (opoint);
2042 set_buffer_internal (old);
2043 }
2044 #ifdef VMS
2045 start_vms_process_read (vs);
2046 #endif
2047 return nchars;
2048 }
2049
2050 DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, Swaiting_for_user_input_p,
2051 0, 0, 0,
2052 "Returns non-NIL if emacs is waiting for input from the user.\n\
2053 This is intended for use by asynchronous process output filters and sentinels.")
2054 ()
2055 {
2056 return ((waiting_for_user_input_p) ? Qt : Qnil);
2057 }
2058 \f
2059 /* Sending data to subprocess */
2060
2061 jmp_buf send_process_frame;
2062
2063 SIGTYPE
2064 send_process_trap ()
2065 {
2066 #ifdef BSD4_1
2067 sigrelse (SIGPIPE);
2068 sigrelse (SIGALRM);
2069 #endif /* BSD4_1 */
2070 longjmp (send_process_frame, 1);
2071 }
2072
2073 send_process (proc, buf, len)
2074 Lisp_Object proc;
2075 char *buf;
2076 int len;
2077 {
2078 /* Don't use register vars; longjmp can lose them. */
2079 int rv;
2080 unsigned char *procname = XSTRING (XPROCESS (proc)->name)->data;
2081
2082
2083 #ifdef VMS
2084 struct Lisp_Process *p = XPROCESS (proc);
2085 VMS_PROC_STUFF *vs, *get_vms_process_pointer();
2086 #endif /* VMS */
2087
2088 if (! NILP (XPROCESS (proc)->raw_status_low))
2089 update_status (XPROCESS (proc));
2090 if (! EQ (XPROCESS (proc)->status, Qrun))
2091 error ("Process %s not running", procname);
2092
2093 #ifdef VMS
2094 vs = get_vms_process_pointer (p->pid);
2095 if (vs == 0)
2096 error ("Could not find this process: %x", p->pid);
2097 else if (write_to_vms_process (vs, buf, len))
2098 ;
2099 #else
2100 if (!setjmp (send_process_frame))
2101 while (len > 0)
2102 {
2103 int this = len;
2104 SIGTYPE (*old_sigpipe)();
2105
2106 /* Don't send more than 500 bytes at a time. */
2107 if (this > 500)
2108 this = 500;
2109 old_sigpipe = (SIGTYPE (*) ()) signal (SIGPIPE, send_process_trap);
2110 rv = write (XFASTINT (XPROCESS (proc)->outfd), buf, this);
2111 signal (SIGPIPE, old_sigpipe);
2112 if (rv < 0)
2113 {
2114 if (0
2115 #ifdef EWOULDBLOCK
2116 || errno == EWOULDBLOCK
2117 #endif
2118 #ifdef EAGAIN
2119 || errno == EAGAIN
2120 #endif
2121 )
2122 {
2123 /* It would be nice to accept process output here,
2124 but that is difficult. For example, it could
2125 garbage what we are sending if that is from a buffer. */
2126 immediate_quit = 1;
2127 QUIT;
2128 sleep (1);
2129 immediate_quit = 0;
2130 continue;
2131 }
2132 report_file_error ("writing to process", Fcons (proc, Qnil));
2133 }
2134 buf += rv;
2135 len -= rv;
2136 /* Allow input from processes between bursts of sending.
2137 Otherwise things may get stopped up. */
2138 if (len > 0)
2139 {
2140 Lisp_Object zero;
2141
2142 XFASTINT (zero) = 0;
2143 wait_reading_process_input (-1, 0, zero, 0);
2144 }
2145 }
2146 #endif
2147 else
2148 {
2149 XPROCESS (proc)->raw_status_low = Qnil;
2150 XPROCESS (proc)->raw_status_high = Qnil;
2151 XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (256), Qnil));
2152 XSETINT (XPROCESS (proc)->tick, ++process_tick);
2153 deactivate_process (proc);
2154 #ifdef VMS
2155 error ("Error writing to process %s; closed it", procname);
2156 #else
2157 error ("SIGPIPE raised on process %s; closed it", procname);
2158 #endif
2159 }
2160 }
2161
2162 DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region,
2163 3, 3, 0,
2164 "Send current contents of region as input to PROCESS.\n\
2165 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
2166 nil, indicating the current buffer's process.\n\
2167 Called from program, takes three arguments, PROCESS, START and END.\n\
2168 If the region is more than 500 characters long,\n\
2169 it is sent in several bunches. This may happen even for shorter regions.\n\
2170 Output from processes can arrive in between bunches.")
2171 (process, start, end)
2172 Lisp_Object process, start, end;
2173 {
2174 Lisp_Object proc;
2175 int start1;
2176
2177 proc = get_process (process);
2178 validate_region (&start, &end);
2179
2180 if (XINT (start) < GPT && XINT (end) > GPT)
2181 move_gap (start);
2182
2183 start1 = XINT (start);
2184 send_process (proc, &FETCH_CHAR (start1), XINT (end) - XINT (start));
2185
2186 return Qnil;
2187 }
2188
2189 DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string,
2190 2, 2, 0,
2191 "Send PROCESS the contents of STRING as input.\n\
2192 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
2193 nil, indicating the current buffer's process.\n\
2194 If STRING is more than 500 characters long,\n\
2195 it is sent in several bunches. This may happen even for shorter strings.\n\
2196 Output from processes can arrive in between bunches.")
2197 (process, string)
2198 Lisp_Object process, string;
2199 {
2200 Lisp_Object proc;
2201 CHECK_STRING (string, 1);
2202 proc = get_process (process);
2203 send_process (proc, XSTRING (string)->data, XSTRING (string)->size);
2204 return Qnil;
2205 }
2206 \f
2207 /* send a signal number SIGNO to PROCESS.
2208 CURRENT_GROUP means send to the process group that currently owns
2209 the terminal being used to communicate with PROCESS.
2210 This is used for various commands in shell mode.
2211 If NOMSG is zero, insert signal-announcements into process's buffers
2212 right away. */
2213
2214 static void
2215 process_send_signal (process, signo, current_group, nomsg)
2216 Lisp_Object process;
2217 int signo;
2218 Lisp_Object current_group;
2219 int nomsg;
2220 {
2221 Lisp_Object proc;
2222 register struct Lisp_Process *p;
2223 int gid;
2224 int no_pgrp = 0;
2225
2226 proc = get_process (process);
2227 p = XPROCESS (proc);
2228
2229 if (!EQ (p->childp, Qt))
2230 error ("Process %s is not a subprocess",
2231 XSTRING (p->name)->data);
2232 if (!XFASTINT (p->infd))
2233 error ("Process %s is not active",
2234 XSTRING (p->name)->data);
2235
2236 if (NILP (p->pty_flag))
2237 current_group = Qnil;
2238
2239 /* If we are using pgrps, get a pgrp number and make it negative. */
2240 if (!NILP (current_group))
2241 {
2242 /* If possible, send signals to the entire pgrp
2243 by sending an input character to it. */
2244 #if defined (TIOCGLTC) && defined (TIOCGETC)
2245 struct tchars c;
2246 struct ltchars lc;
2247
2248 switch (signo)
2249 {
2250 case SIGINT:
2251 ioctl (XFASTINT (p->infd), TIOCGETC, &c);
2252 send_process (proc, &c.t_intrc, 1);
2253 return;
2254 case SIGQUIT:
2255 ioctl (XFASTINT (p->infd), TIOCGETC, &c);
2256 send_process (proc, &c.t_quitc, 1);
2257 return;
2258 #ifdef SIGTSTP
2259 case SIGTSTP:
2260 ioctl (XFASTINT (p->infd), TIOCGLTC, &lc);
2261 send_process (proc, &lc.t_suspc, 1);
2262 return;
2263 #endif /* SIGTSTP */
2264 }
2265 #endif /* ! defined (TIOCGLTC) && defined (TIOCGETC) */
2266 /* It is possible that the following code would work
2267 on other kinds of USG systems, not just on the IRIS.
2268 This should be tried in Emacs 19. */
2269 #if defined (USG)
2270 struct termio t;
2271 switch (signo)
2272 {
2273 case SIGINT:
2274 ioctl (XFASTINT (p->infd), TCGETA, &t);
2275 send_process (proc, &t.c_cc[VINTR], 1);
2276 return;
2277 case SIGQUIT:
2278 ioctl (XFASTINT (p->infd), TCGETA, &t);
2279 send_process (proc, &t.c_cc[VQUIT], 1);
2280 return;
2281 #ifdef SIGTSTP
2282 case SIGTSTP:
2283 ioctl (XFASTINT (p->infd), TCGETA, &t);
2284 send_process (proc, &t.c_cc[VSWTCH], 1);
2285 return;
2286 #endif
2287 }
2288 #endif /* ! defined (USG) */
2289
2290 #ifdef TIOCGPGRP
2291 /* Get the pgrp using the tty itself, if we have that.
2292 Otherwise, use the pty to get the pgrp.
2293 On pfa systems, saka@pfu.fujitsu.co.JP writes:
2294 "TICGPGRP symbol defined in sys/ioctl.h at E50.
2295 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
2296 His patch indicates that if TIOCGPGRP returns an error, then
2297 we should just assume that p->pid is also the process group id. */
2298 {
2299 int err;
2300
2301 if (!NILP (p->subtty))
2302 err = ioctl (XFASTINT (p->subtty), TIOCGPGRP, &gid);
2303 else
2304 err = ioctl (XFASTINT (p->infd), TIOCGPGRP, &gid);
2305
2306 #ifdef pfa
2307 if (err == -1)
2308 gid = - XFASTINT (p->pid);
2309 #endif /* ! defined (pfa) */
2310 }
2311 if (gid == -1)
2312 no_pgrp = 1;
2313 else
2314 gid = - gid;
2315 #else /* ! defined (TIOCGPGRP ) */
2316 /* Can't select pgrps on this system, so we know that
2317 the child itself heads the pgrp. */
2318 gid = - XFASTINT (p->pid);
2319 #endif /* ! defined (TIOCGPGRP ) */
2320 }
2321 else
2322 gid = - XFASTINT (p->pid);
2323
2324 switch (signo)
2325 {
2326 #ifdef SIGCONT
2327 case SIGCONT:
2328 p->raw_status_low = Qnil;
2329 p->raw_status_high = Qnil;
2330 p->status = Qrun;
2331 XSETINT (p->tick, ++process_tick);
2332 if (!nomsg)
2333 status_notify ();
2334 break;
2335 #endif /* ! defined (SIGCONT) */
2336 case SIGINT:
2337 #ifdef VMS
2338 send_process (proc, "\003", 1); /* ^C */
2339 goto whoosh;
2340 #endif
2341 case SIGQUIT:
2342 #ifdef VMS
2343 send_process (proc, "\031", 1); /* ^Y */
2344 goto whoosh;
2345 #endif
2346 case SIGKILL:
2347 #ifdef VMS
2348 sys$forcex (&(XFASTINT (p->pid)), 0, 1);
2349 whoosh:
2350 #endif
2351 flush_pending_output (XFASTINT (p->infd));
2352 break;
2353 }
2354
2355 /* If we don't have process groups, send the signal to the immediate
2356 subprocess. That isn't really right, but it's better than any
2357 obvious alternative. */
2358 if (no_pgrp)
2359 {
2360 kill (XFASTINT (p->pid), signo);
2361 return;
2362 }
2363
2364 /* gid may be a pid, or minus a pgrp's number */
2365 #ifdef TIOCSIGSEND
2366 if (!NILP (current_group))
2367 ioctl (XFASTINT (p->infd), TIOCSIGSEND, signo);
2368 else
2369 {
2370 gid = - XFASTINT (p->pid);
2371 kill (gid, signo);
2372 }
2373 #else /* ! defined (TIOCSIGSEND) */
2374 EMACS_KILLPG (-gid, signo);
2375 #endif /* ! defined (TIOCSIGSEND) */
2376 }
2377
2378 DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0,
2379 "Interrupt process PROCESS. May be process or name of one.\n\
2380 PROCESS may be a process, a buffer, or the name of a process or buffer.\n\
2381 Nil or no arg means current buffer's process.\n\
2382 Second arg CURRENT-GROUP non-nil means send signal to\n\
2383 the current process-group of the process's controlling terminal\n\
2384 rather than to the process's own process group.\n\
2385 If the process is a shell, this means interrupt current subjob\n\
2386 rather than the shell.")
2387 (process, current_group)
2388 Lisp_Object process, current_group;
2389 {
2390 process_send_signal (process, SIGINT, current_group, 0);
2391 return process;
2392 }
2393
2394 DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0,
2395 "Kill process PROCESS. May be process or name of one.\n\
2396 See function `interrupt-process' for more details on usage.")
2397 (process, current_group)
2398 Lisp_Object process, current_group;
2399 {
2400 process_send_signal (process, SIGKILL, current_group, 0);
2401 return process;
2402 }
2403
2404 DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0,
2405 "Send QUIT signal to process PROCESS. May be process or name of one.\n\
2406 See function `interrupt-process' for more details on usage.")
2407 (process, current_group)
2408 Lisp_Object process, current_group;
2409 {
2410 process_send_signal (process, SIGQUIT, current_group, 0);
2411 return process;
2412 }
2413
2414 DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0,
2415 "Stop process PROCESS. May be process or name of one.\n\
2416 See function `interrupt-process' for more details on usage.")
2417 (process, current_group)
2418 Lisp_Object process, current_group;
2419 {
2420 #ifndef SIGTSTP
2421 error ("no SIGTSTP support");
2422 #else
2423 process_send_signal (process, SIGTSTP, current_group, 0);
2424 #endif
2425 return process;
2426 }
2427
2428 DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0,
2429 "Continue process PROCESS. May be process or name of one.\n\
2430 See function `interrupt-process' for more details on usage.")
2431 (process, current_group)
2432 Lisp_Object process, current_group;
2433 {
2434 #ifdef SIGCONT
2435 process_send_signal (process, SIGCONT, current_group, 0);
2436 #else
2437 error ("no SIGCONT support");
2438 #endif
2439 return process;
2440 }
2441
2442 DEFUN ("signal-process", Fsignal_process, Ssignal_process,
2443 2, 2, "nProcess number: \nnSignal code: ",
2444 "Send the process with number PID the signal with code CODE.\n\
2445 Both PID and CODE are integers.")
2446 (pid, sig)
2447 Lisp_Object pid, sig;
2448 {
2449 CHECK_NUMBER (pid, 0);
2450 CHECK_NUMBER (sig, 1);
2451 return make_number (kill (XINT (pid), XINT (sig)));
2452 }
2453
2454 DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0,
2455 "Make PROCESS see end-of-file in its input.\n\
2456 Eof comes after any text already sent to it.\n\
2457 PROCESS may be a process, a buffer, the name of a process or buffer, or\n\
2458 nil, indicating the current buffer's process.")
2459 (process)
2460 Lisp_Object process;
2461 {
2462 Lisp_Object proc;
2463
2464 proc = get_process (process);
2465 /* Sending a zero-length record is supposed to mean eof
2466 when TIOCREMOTE is turned on. */
2467 #ifdef DID_REMOTE
2468 {
2469 char buf[1];
2470 write (XFASTINT (XPROCESS (proc)->outfd), buf, 0);
2471 }
2472 #else /* did not do TOICREMOTE */
2473 #ifdef VMS
2474 send_process (proc, "\032", 1); /* ^z */
2475 #else
2476 if (!NILP (XPROCESS (proc)->pty_flag))
2477 send_process (proc, "\004", 1);
2478 else
2479 {
2480 close (XPROCESS (proc)->outfd);
2481 XFASTINT (XPROCESS (proc)->outfd) = open ("/dev/null", O_WRONLY);
2482 }
2483 #endif /* VMS */
2484 #endif /* did not do TOICREMOTE */
2485 return process;
2486 }
2487
2488 /* Kill all processes associated with `buffer'.
2489 If `buffer' is nil, kill all processes */
2490
2491 kill_buffer_processes (buffer)
2492 Lisp_Object buffer;
2493 {
2494 Lisp_Object tail, proc;
2495
2496 for (tail = Vprocess_alist; XGCTYPE (tail) == Lisp_Cons;
2497 tail = XCONS (tail)->cdr)
2498 {
2499 proc = XCONS (XCONS (tail)->car)->cdr;
2500 if (XGCTYPE (proc) == Lisp_Process
2501 && (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer)))
2502 {
2503 if (NETCONN_P (proc))
2504 deactivate_process (proc);
2505 else if (XFASTINT (XPROCESS (proc)->infd))
2506 process_send_signal (proc, SIGHUP, Qnil, 1);
2507 }
2508 }
2509 }
2510 \f
2511 /* On receipt of a signal that a child status has changed,
2512 loop asking about children with changed statuses until
2513 the system says there are no more.
2514 All we do is change the status;
2515 we do not run sentinels or print notifications.
2516 That is saved for the next time keyboard input is done,
2517 in order to avoid timing errors. */
2518
2519 /** WARNING: this can be called during garbage collection.
2520 Therefore, it must not be fooled by the presence of mark bits in
2521 Lisp objects. */
2522
2523 /** USG WARNING: Although it is not obvious from the documentation
2524 in signal(2), on a USG system the SIGCLD handler MUST NOT call
2525 signal() before executing at least one wait(), otherwise the handler
2526 will be called again, resulting in an infinite loop. The relevant
2527 portion of the documentation reads "SIGCLD signals will be queued
2528 and the signal-catching function will be continually reentered until
2529 the queue is empty". Invoking signal() causes the kernel to reexamine
2530 the SIGCLD queue. Fred Fish, UniSoft Systems Inc. */
2531
2532 SIGTYPE
2533 sigchld_handler (signo)
2534 int signo;
2535 {
2536 int old_errno = errno;
2537 Lisp_Object proc;
2538 register struct Lisp_Process *p;
2539
2540 #ifdef BSD4_1
2541 extern int sigheld;
2542 sigheld |= sigbit (SIGCHLD);
2543 #endif
2544
2545 while (1)
2546 {
2547 register int pid;
2548 WAITTYPE w;
2549 Lisp_Object tail;
2550
2551 #ifdef WNOHANG
2552 #ifndef WUNTRACED
2553 #define WUNTRACED 0
2554 #endif /* no WUNTRACED */
2555 /* Keep trying to get a status until we get a definitive result. */
2556 do
2557 {
2558 errno = 0;
2559 pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
2560 }
2561 while (pid <= 0 && errno == EINTR);
2562
2563 if (pid <= 0)
2564 {
2565 /* A real failure. We have done all our job, so return. */
2566
2567 /* USG systems forget handlers when they are used;
2568 must reestablish each time */
2569 #ifdef USG
2570 signal (signo, sigchld_handler); /* WARNING - must come after wait3() */
2571 #endif
2572 #ifdef BSD4_1
2573 sigheld &= ~sigbit (SIGCHLD);
2574 sigrelse (SIGCHLD);
2575 #endif
2576 errno = old_errno;
2577 return;
2578 }
2579 #else
2580 pid = wait (&w);
2581 #endif /* no WNOHANG */
2582
2583 /* Find the process that signaled us, and record its status. */
2584
2585 p = 0;
2586 for (tail = Vprocess_alist; XSYMBOL (tail) != XSYMBOL (Qnil); tail = XCONS (tail)->cdr)
2587 {
2588 proc = XCONS (XCONS (tail)->car)->cdr;
2589 p = XPROCESS (proc);
2590 if (EQ (p->childp, Qt) && XFASTINT (p->pid) == pid)
2591 break;
2592 p = 0;
2593 }
2594
2595 /* Look for an asynchronous process whose pid hasn't been filled
2596 in yet. */
2597 if (p == 0)
2598 for (tail = Vprocess_alist; XSYMBOL (tail) != XSYMBOL (Qnil); tail = XCONS (tail)->cdr)
2599 {
2600 proc = XCONS (XCONS (tail)->car)->cdr;
2601 p = XPROCESS (proc);
2602 if (XTYPE (p->pid) == Lisp_Int && XINT (p->pid) == -1)
2603 break;
2604 p = 0;
2605 }
2606
2607 /* Change the status of the process that was found. */
2608 if (p != 0)
2609 {
2610 union { int i; WAITTYPE wt; } u;
2611
2612 XSETINT (p->tick, ++process_tick);
2613 u.wt = w;
2614 XFASTINT (p->raw_status_low) = u.i & 0xffff;
2615 XFASTINT (p->raw_status_high) = u.i >> 16;
2616
2617 /* If process has terminated, stop waiting for its output. */
2618 if (WIFSIGNALED (w) || WIFEXITED (w))
2619 if (XFASTINT (p->infd))
2620 FD_CLR (XFASTINT (p->infd), &input_wait_mask);
2621 }
2622
2623 /* There was no asynchronous process found for that id. Check
2624 if we have a synchronous process. */
2625 else
2626 {
2627 synch_process_alive = 0;
2628
2629 /* Report the status of the synchronous process. */
2630 if (WIFEXITED (w))
2631 synch_process_retcode = WRETCODE (w);
2632 else if (WIFSIGNALED (w))
2633 synch_process_death = sys_siglist[WTERMSIG (w)];
2634 }
2635
2636 /* On some systems, we must return right away.
2637 If any more processes want to signal us, we will
2638 get another signal.
2639 Otherwise (on systems that have WNOHANG), loop around
2640 to use up all the processes that have something to tell us. */
2641 #if defined (USG) && ! (defined (HPUX) && defined (WNOHANG))
2642 #ifdef USG
2643 signal (signo, sigchld_handler);
2644 #endif
2645 errno = old_errno;
2646 return;
2647 #endif /* USG, but not HPUX with WNOHANG */
2648 }
2649 }
2650 \f
2651
2652 static Lisp_Object
2653 exec_sentinel_unwind (data)
2654 Lisp_Object data;
2655 {
2656 XPROCESS (XCONS (data)->car)->sentinel = XCONS (data)->cdr;
2657 return Qnil;
2658 }
2659
2660 static void
2661 exec_sentinel (proc, reason)
2662 Lisp_Object proc, reason;
2663 {
2664 Lisp_Object sentinel;
2665 register struct Lisp_Process *p = XPROCESS (proc);
2666 int count = specpdl_ptr - specpdl;
2667
2668 sentinel = p->sentinel;
2669 if (NILP (sentinel))
2670 return;
2671
2672 /* Zilch the sentinel while it's running, to avoid recursive invocations;
2673 assure that it gets restored no matter how the sentinel exits. */
2674 p->sentinel = Qnil;
2675 record_unwind_protect (exec_sentinel_unwind, Fcons (proc, sentinel));
2676 /* Inhibit quit so that random quits don't screw up a running filter. */
2677 specbind (Qinhibit_quit, Qt);
2678 call2 (sentinel, proc, reason);
2679 unbind_to (count);
2680 }
2681
2682 /* Report all recent events of a change in process status
2683 (either run the sentinel or output a message).
2684 This is done while Emacs is waiting for keyboard input. */
2685
2686 status_notify ()
2687 {
2688 register Lisp_Object proc, buffer;
2689 Lisp_Object tail = Qnil;
2690 Lisp_Object msg = Qnil;
2691 struct gcpro gcpro1, gcpro2;
2692
2693 /* We need to gcpro tail; if read_process_output calls a filter
2694 which deletes a process and removes the cons to which tail points
2695 from Vprocess_alist, and then causes a GC, tail is an unprotected
2696 reference. */
2697 GCPRO2 (tail, msg);
2698
2699 for (tail = Vprocess_alist; !NILP (tail); tail = Fcdr (tail))
2700 {
2701 Lisp_Object symbol;
2702 register struct Lisp_Process *p;
2703
2704 proc = Fcdr (Fcar (tail));
2705 p = XPROCESS (proc);
2706
2707 if (XINT (p->tick) != XINT (p->update_tick))
2708 {
2709 XSETINT (p->update_tick, XINT (p->tick));
2710
2711 /* If process is still active, read any output that remains. */
2712 if (XFASTINT (p->infd))
2713 while (read_process_output (proc, XFASTINT (p->infd)) > 0);
2714
2715 buffer = p->buffer;
2716
2717 /* Get the text to use for the message. */
2718 if (!NILP (p->raw_status_low))
2719 update_status (p);
2720 msg = status_message (p->status);
2721
2722 /* If process is terminated, deactivate it or delete it. */
2723 symbol = p->status;
2724 if (XTYPE (p->status) == Lisp_Cons)
2725 symbol = XCONS (p->status)->car;
2726
2727 if (EQ (symbol, Qsignal) || EQ (symbol, Qexit)
2728 || EQ (symbol, Qclosed))
2729 {
2730 if (delete_exited_processes)
2731 remove_process (proc);
2732 else
2733 deactivate_process (proc);
2734 }
2735
2736 /* Now output the message suitably. */
2737 if (!NILP (p->sentinel))
2738 exec_sentinel (proc, msg);
2739 /* Don't bother with a message in the buffer
2740 when a process becomes runnable. */
2741 else if (!EQ (symbol, Qrun) && !NILP (buffer))
2742 {
2743 Lisp_Object ro = XBUFFER (buffer)->read_only;
2744 Lisp_Object tem;
2745 struct buffer *old = current_buffer;
2746 int opoint;
2747
2748 /* Avoid error if buffer is deleted
2749 (probably that's why the process is dead, too) */
2750 if (NILP (XBUFFER (buffer)->name))
2751 continue;
2752 Fset_buffer (buffer);
2753 opoint = point;
2754 /* Insert new output into buffer
2755 at the current end-of-output marker,
2756 thus preserving logical ordering of input and output. */
2757 if (XMARKER (p->mark)->buffer)
2758 SET_PT (marker_position (p->mark));
2759 else
2760 SET_PT (ZV);
2761 if (point <= opoint)
2762 opoint += XSTRING (msg)->size + XSTRING (p->name)->size + 10;
2763
2764 tem = current_buffer->read_only;
2765 current_buffer->read_only = Qnil;
2766 insert_string ("\nProcess ");
2767 Finsert (1, &p->name);
2768 insert_string (" ");
2769 Finsert (1, &msg);
2770 current_buffer->read_only = tem;
2771 Fset_marker (p->mark, make_number (point), p->buffer);
2772
2773 SET_PT (opoint);
2774 set_buffer_internal (old);
2775 }
2776 }
2777 } /* end for */
2778
2779 update_mode_lines++; /* in case buffers use %s in mode-line-format */
2780 redisplay_preserve_echo_area ();
2781
2782 update_tick = process_tick;
2783
2784 UNGCPRO;
2785 }
2786 \f
2787 init_process ()
2788 {
2789 register int i;
2790
2791 #ifdef SIGCHLD
2792 #ifndef CANNOT_DUMP
2793 if (! noninteractive || initialized)
2794 #endif
2795 signal (SIGCHLD, sigchld_handler);
2796 #endif
2797
2798 FD_ZERO (&input_wait_mask);
2799 FD_SET (0, &input_wait_mask);
2800 Vprocess_alist = Qnil;
2801 for (i = 0; i < MAXDESC; i++)
2802 {
2803 chan_process[i] = Qnil;
2804 proc_buffered_char[i] = -1;
2805 }
2806 }
2807 #if 0
2808 DEFUN ("process-connection", Fprocess_connection, Sprocess_connection, 0, 1, 0,
2809 "Return the connection type of `PROCESS'. This can be nil (pipe),\n\
2810 t or pty (pty) or stream (socket connection).")
2811 (process)
2812 Lisp_Object process;
2813 {
2814 return XPROCESS (process)->type;
2815 }
2816 #endif
2817 syms_of_process ()
2818 {
2819 #ifdef HAVE_SOCKETS
2820 stream_process = intern ("stream");
2821 #endif
2822 Qprocessp = intern ("processp");
2823 staticpro (&Qprocessp);
2824 Qrun = intern ("run");
2825 staticpro (&Qrun);
2826 Qstop = intern ("stop");
2827 staticpro (&Qstop);
2828 Qsignal = intern ("signal");
2829 staticpro (&Qsignal);
2830
2831 /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
2832 here again.
2833
2834 Qexit = intern ("exit");
2835 staticpro (&Qexit); */
2836
2837 Qopen = intern ("open");
2838 staticpro (&Qopen);
2839 Qclosed = intern ("closed");
2840 staticpro (&Qclosed);
2841
2842 staticpro (&Vprocess_alist);
2843
2844 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
2845 "*Non-nil means delete processes immediately when they exit.\n\
2846 nil means don't delete them until `list-processes' is run.");
2847
2848 delete_exited_processes = 1;
2849
2850 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type,
2851 "Control type of device used to communicate with subprocesses.\n\
2852 Values are nil to use a pipe, and t or 'pty for a pty. Note that if\n\
2853 pty's are not available, this variable will be ignored. The value takes\n\
2854 effect when `start-process' is called.");
2855 Vprocess_connection_type = Qt;
2856
2857 defsubr (&Sprocessp);
2858 defsubr (&Sget_process);
2859 defsubr (&Sget_buffer_process);
2860 defsubr (&Sdelete_process);
2861 defsubr (&Sprocess_status);
2862 defsubr (&Sprocess_exit_status);
2863 defsubr (&Sprocess_id);
2864 defsubr (&Sprocess_name);
2865 defsubr (&Sprocess_command);
2866 defsubr (&Sset_process_buffer);
2867 defsubr (&Sprocess_buffer);
2868 defsubr (&Sprocess_mark);
2869 defsubr (&Sset_process_filter);
2870 defsubr (&Sprocess_filter);
2871 defsubr (&Sset_process_sentinel);
2872 defsubr (&Sprocess_sentinel);
2873 defsubr (&Sprocess_kill_without_query);
2874 defsubr (&Slist_processes);
2875 defsubr (&Sprocess_list);
2876 defsubr (&Sstart_process);
2877 #ifdef HAVE_SOCKETS
2878 defsubr (&Sopen_network_stream);
2879 #endif /* HAVE_SOCKETS */
2880 defsubr (&Saccept_process_output);
2881 defsubr (&Sprocess_send_region);
2882 defsubr (&Sprocess_send_string);
2883 defsubr (&Sinterrupt_process);
2884 defsubr (&Skill_process);
2885 defsubr (&Squit_process);
2886 defsubr (&Sstop_process);
2887 defsubr (&Scontinue_process);
2888 defsubr (&Sprocess_send_eof);
2889 defsubr (&Ssignal_process);
2890 defsubr (&Swaiting_for_user_input_p);
2891 /* defsubr (&Sprocess_connection); */
2892 }
2893
2894 \f
2895 #else /* not subprocesses */
2896
2897 #include <sys/types.h>
2898 #include <errno.h>
2899
2900 #include "lisp.h"
2901 #include "systime.h"
2902 #include "termopts.h"
2903
2904 extern int frame_garbaged;
2905
2906
2907 /* As described above, except assuming that there are no subprocesses:
2908
2909 Wait for timeout to elapse and/or keyboard input to be available.
2910
2911 time_limit is:
2912 timeout in seconds, or
2913 zero for no limit, or
2914 -1 means gobble data immediately available but don't wait for any.
2915
2916 read_kbd is a Lisp_Object:
2917 0 to ignore keyboard input, or
2918 1 to return when input is available, or
2919 -1 means caller will actually read the input, so don't throw to
2920 the quit handler.
2921 We know that read_kbd will never be a Lisp_Process, since
2922 `subprocesses' isn't defined.
2923
2924 do_display != 0 means redisplay should be done to show subprocess
2925 output that arrives. This version of the function ignores it.
2926
2927 Return true iff we recieved input from any process. */
2928
2929 int
2930 wait_reading_process_input (time_limit, microsecs, read_kbd, do_display)
2931 int time_limit, microsecs;
2932 Lisp_Object read_kbd;
2933 int do_display;
2934 {
2935 EMACS_TIME end_time, timeout, *timeout_p;
2936 int waitchannels;
2937
2938 /* What does time_limit really mean? */
2939 if (time_limit || microsecs)
2940 {
2941 /* It's not infinite. */
2942 timeout_p = &timeout;
2943
2944 if (time_limit == -1)
2945 /* In fact, it's zero. */
2946 EMACS_SET_SECS_USECS (timeout, 0, 0);
2947 else
2948 EMACS_SET_SECS_USECS (timeout, time_limit, microsecs);
2949
2950 /* How far in the future is that? */
2951 EMACS_GET_TIME (end_time);
2952 EMACS_ADD_TIME (end_time, end_time, timeout);
2953 }
2954 else
2955 /* It's infinite. */
2956 timeout_p = 0;
2957
2958 /* Turn off periodic alarms (in case they are in use)
2959 because the select emulator uses alarms. */
2960 stop_polling ();
2961
2962 for (;;)
2963 {
2964 int nfds;
2965
2966 waitchannels = XINT (read_kbd) ? 1 : 0;
2967
2968 /* If calling from keyboard input, do not quit
2969 since we want to return C-g as an input character.
2970 Otherwise, do pending quit if requested. */
2971 if (XINT (read_kbd) >= 0)
2972 QUIT;
2973
2974 if (timeout_p)
2975 {
2976 EMACS_GET_TIME (*timeout_p);
2977 EMACS_SUB_TIME (*timeout_p, end_time, *timeout_p);
2978 if (EMACS_TIME_NEG_P (*timeout_p))
2979 break;
2980 }
2981
2982 /* Cause C-g and alarm signals to take immediate action,
2983 and cause input available signals to zero out timeout. */
2984 if (XINT (read_kbd) < 0)
2985 set_waiting_for_input (&timeout);
2986
2987 /* If a frame has been newly mapped and needs updating,
2988 reprocess its display stuff. */
2989 if (frame_garbaged)
2990 redisplay_preserve_echo_area ();
2991
2992 if (XINT (read_kbd) && detect_input_pending ())
2993 nfds = 0;
2994 else
2995 nfds = select (1, &waitchannels, 0, 0, timeout_p);
2996
2997 /* Make C-g and alarm signals set flags again */
2998 clear_waiting_for_input ();
2999
3000 /* If we woke up due to SIGWINCH, actually change size now. */
3001 do_pending_window_change ();
3002
3003 if (nfds == -1)
3004 {
3005 /* If the system call was interrupted, then go around the
3006 loop again. */
3007 if (errno == EINTR)
3008 waitchannels = 0;
3009 }
3010 #ifdef sun
3011 else if (nfds > 0 && (waitchannels & 1) && interrupt_input)
3012 /* System sometimes fails to deliver SIGIO. */
3013 kill (getpid (), SIGIO);
3014 #endif
3015 if (XINT (read_kbd) && interrupt_input && (waitchannels & 1))
3016 kill (0, SIGIO);
3017
3018 /* If we have timed out (nfds == 0) or found some input (nfds > 0),
3019 we should exit. */
3020 if (nfds >= 0)
3021 break;
3022 }
3023
3024 return 0;
3025 }
3026
3027
3028 DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
3029 "Return the (or, a) process associated with BUFFER.\n\
3030 This copy of Emacs has not been built to support subprocesses, so this\n\
3031 function always returns nil.")
3032 (name)
3033 register Lisp_Object name;
3034 {
3035 return Qnil;
3036 }
3037
3038 /* Kill all processes associated with `buffer'.
3039 If `buffer' is nil, kill all processes.
3040 Since we have no subprocesses, this does nothing. */
3041
3042 kill_buffer_processes (buffer)
3043 Lisp_Object buffer;
3044 {
3045 }
3046
3047 init_process ()
3048 {
3049 }
3050
3051 syms_of_process ()
3052 {
3053 defsubr (&Sget_buffer_process);
3054 }
3055
3056 \f
3057 #endif /* not subprocesses */