Mercurial > hg > xemacs-beta
comparison src/process.c @ 0:376386a54a3c r19-14
Import from CVS: tag r19-14
author | cvs |
---|---|
date | Mon, 13 Aug 2007 08:45:50 +0200 |
parents | |
children | 0293115a14e9 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:376386a54a3c |
---|---|
1 /* Asynchronous subprocess control for XEmacs. | |
2 Copyright (C) 1985, 1986, 1987, 1988, 1992, 1993, 1994, 1995 | |
3 Free Software Foundation, Inc. | |
4 Copyright (C) 1995 Sun Microsystems, Inc. | |
5 Copyright (C) 1995, 1996 Ben Wing. | |
6 | |
7 This file is part of XEmacs. | |
8 | |
9 XEmacs is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
11 Free Software Foundation; either version 2, or (at your option) any | |
12 later version. | |
13 | |
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
20 along with XEmacs; see the file COPYING. If not, write to | |
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 Boston, MA 02111-1307, USA. */ | |
23 | |
24 /* Synched up with: Mule 2.0, FSF 19.30. */ | |
25 | |
26 /* This file has been Mule-ized except for `start-process-internal' | |
27 and `open-network-stream-internal'. */ | |
28 | |
29 #include <config.h> | |
30 | |
31 #if !defined (NO_SUBPROCESSES) | |
32 | |
33 /* The entire file is within this conditional */ | |
34 | |
35 #include "lisp.h" | |
36 | |
37 #include "buffer.h" | |
38 #include "commands.h" | |
39 #include "events.h" | |
40 #include "frame.h" | |
41 #include "insdel.h" | |
42 #include "lstream.h" | |
43 #include "opaque.h" | |
44 #include "process.h" | |
45 #include "sysdep.h" | |
46 #include "window.h" | |
47 | |
48 #include <setjmp.h> | |
49 #include "sysfile.h" | |
50 #include "sysproc.h" | |
51 #include "systime.h" | |
52 #include "syssignal.h" /* Always include before systty.h */ | |
53 | |
54 #include "systty.h" | |
55 #include "syswait.h" | |
56 | |
57 /* a process object is a network connection when its pid field a cons | |
58 (name of name of port we are connected to . foreign host name) */ | |
59 | |
60 /* Valid values of process->status_symbol */ | |
61 Lisp_Object Qrun, Qstop; /* Qexit from eval.c, Qsignal from data.c. */ | |
62 /* Qrun => Qopen, Qexit => Qclosed for "network connection" processes */ | |
63 Lisp_Object Qopen, Qclosed; | |
64 | |
65 /* t means use pty, nil means use a pipe, | |
66 maybe other values to come. */ | |
67 static Lisp_Object Vprocess_connection_type; | |
68 | |
69 /* FSFmacs says: | |
70 | |
71 These next two vars are non-static since sysdep.c uses them in the | |
72 emulation of `select'. */ | |
73 /* Number of events of change of status of a process. */ | |
74 static volatile int process_tick; | |
75 | |
76 /* Number of events for which the user or sentinel has been notified. */ | |
77 static int update_tick; | |
78 | |
79 /* Nonzero means delete a process right away if it exits. */ | |
80 int delete_exited_processes; | |
81 | |
82 /* Indexed by descriptor, gives the process (if any) for that descriptor */ | |
83 Lisp_Object descriptor_to_process[MAXDESC]; | |
84 | |
85 /* List of process objects. */ | |
86 Lisp_Object Vprocess_list; | |
87 | |
88 Lisp_Object Qprocessp; | |
89 | |
90 /* Buffered-ahead input char from process, indexed by channel. | |
91 -1 means empty (no char is buffered). | |
92 Used on sys V where the only way to tell if there is any | |
93 output from the process is to read at least one char. | |
94 Always -1 on systems that support FIONREAD. */ | |
95 | |
96 /* FSFmacs says: | |
97 Don't make static; need to access externally. */ | |
98 static int proc_buffered_char[MAXDESC]; | |
99 | |
100 #ifdef HAVE_PTYS | |
101 /* The file name of the pty opened by allocate_pty. */ | |
102 | |
103 static char pty_name[24]; | |
104 #endif | |
105 | |
106 | |
107 /************************************************************************/ | |
108 /* the process Lisp object */ | |
109 /************************************************************************/ | |
110 | |
111 /* | |
112 * Structure records pertinent information about open channels. | |
113 * There is one channel associated with each process. | |
114 */ | |
115 | |
116 struct Lisp_Process | |
117 { | |
118 struct lcrecord_header header; | |
119 /* Name of this process */ | |
120 Lisp_Object name; | |
121 /* List of command arguments that this process was run with */ | |
122 Lisp_Object command; | |
123 /* (funcall FILTER PROC STRING) (if FILTER is non-nil) | |
124 to dispose of a bunch of chars from the process all at once */ | |
125 Lisp_Object filter; | |
126 /* (funcall SENTINEL PROCESS) when process state changes */ | |
127 Lisp_Object sentinel; | |
128 /* Buffer that output is going to */ | |
129 Lisp_Object buffer; | |
130 /* Marker set to end of last buffer-inserted output from this process */ | |
131 Lisp_Object mark; | |
132 /* Lisp_Int of subprocess' PID, or a cons of | |
133 service/host if this is really a network connection */ | |
134 Lisp_Object pid; | |
135 /* Non-0 if this is really a ToolTalk channel. */ | |
136 int connected_via_filedesc_p; | |
137 #if 0 /* FSFmacs */ | |
138 /* Perhaps it's cleaner this way, but FSFmacs | |
139 provides no way of retrieving this value, so I'll | |
140 leave this info with PID. */ | |
141 /* Non-nil if this is really a child process */ | |
142 Lisp_Object childp; | |
143 #endif | |
144 | |
145 /* Symbol indicating status of process. | |
146 This may be a symbol: run, stop, exit, signal */ | |
147 Lisp_Object status_symbol; | |
148 | |
149 | |
150 /* Exit code if process has terminated, | |
151 signal which stopped/interrupted process | |
152 or 0 if process is running */ | |
153 int exit_code; | |
154 /* Number of this process */ | |
155 /* Non-false if process has exited and "dumped core" on its way down */ | |
156 char core_dumped; | |
157 /* Descriptor by which we read from this process. -1 for dead process */ | |
158 int infd; | |
159 /* Descriptor by which we write to this process. -1 for dead process */ | |
160 int outfd; | |
161 /* Descriptor for the tty which this process is using. | |
162 -1 if we didn't record it (on some systems, there's no need). */ | |
163 int subtty; | |
164 /* Name of subprocess terminal. */ | |
165 Lisp_Object tty_name; | |
166 /* Non-false if communicating through a pty. */ | |
167 char pty_flag; | |
168 /* This next field is only actually used #ifdef ENERGIZE */ | |
169 /* if this flag is not NIL, then filter will do the read on the | |
170 channel, rather than having a call to make_string. | |
171 This only works if the filter is a subr. */ | |
172 char filter_does_read; | |
173 /* Non-nil means kill silently if Emacs is exited. */ | |
174 char kill_without_query; | |
175 char selected; | |
176 /* Event-count of last event in which this process changed status. */ | |
177 volatile int tick; | |
178 /* Event-count of last such event reported. */ | |
179 int update_tick; | |
180 /* streams used in input and output */ | |
181 Lisp_Object instream; | |
182 Lisp_Object outstream; | |
183 /* The actual filedesc stream used for output; may be different | |
184 than OUTSTREAM under Mule */ | |
185 Lisp_Object filedesc_stream; | |
186 }; | |
187 | |
188 static Lisp_Object mark_process (Lisp_Object, void (*) (Lisp_Object)); | |
189 static void print_process (Lisp_Object, Lisp_Object, int); | |
190 static void finalize_process (void *, int); | |
191 DEFINE_LRECORD_IMPLEMENTATION ("process", process, | |
192 mark_process, print_process, finalize_process, | |
193 0, 0, struct Lisp_Process); | |
194 | |
195 static Lisp_Object | |
196 mark_process (Lisp_Object obj, void (*markobj) (Lisp_Object)) | |
197 { | |
198 struct Lisp_Process *proc = XPROCESS (obj); | |
199 ((markobj) (proc->name)); | |
200 ((markobj) (proc->command)); | |
201 ((markobj) (proc->filter)); | |
202 ((markobj) (proc->sentinel)); | |
203 ((markobj) (proc->buffer)); | |
204 ((markobj) (proc->mark)); | |
205 ((markobj) (proc->pid)); | |
206 ((markobj) (proc->tty_name)); | |
207 ((markobj) (proc->instream)); | |
208 ((markobj) (proc->outstream)); | |
209 ((markobj) (proc->filedesc_stream)); | |
210 return (proc->status_symbol); | |
211 } | |
212 | |
213 static void | |
214 print_process (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag) | |
215 { | |
216 struct Lisp_Process *proc = XPROCESS (obj); | |
217 | |
218 if (print_readably) | |
219 error ("printing unreadable object #<process %s>", | |
220 string_data (XSTRING (proc->name))); | |
221 | |
222 if (!escapeflag) | |
223 { | |
224 print_internal (proc->name, printcharfun, 0); | |
225 } | |
226 else | |
227 { | |
228 int netp = network_connection_p (obj); | |
229 write_c_string (((netp) ? GETTEXT ("#<network connection ") : | |
230 GETTEXT ("#<process ")), printcharfun); | |
231 print_internal (proc->name, printcharfun, 1); | |
232 write_c_string (((netp) ? " " : " pid "), printcharfun); | |
233 print_internal (proc->pid, printcharfun, 1); | |
234 write_c_string (" state:", printcharfun); | |
235 print_internal (proc->status_symbol, printcharfun, 1); | |
236 write_c_string (">", printcharfun); | |
237 } | |
238 } | |
239 | |
240 #ifdef HAVE_WINDOW_SYSTEM | |
241 extern void debug_process_finalization (struct Lisp_Process *p); | |
242 #endif /* HAVE_WINDOW_SYSTEM */ | |
243 | |
244 static void | |
245 finalize_process (void *header, int for_disksave) | |
246 { | |
247 if (for_disksave) return; /* hmm, what would this do anyway? */ | |
248 /* #### this probably needs to be tied into the tty event loop */ | |
249 /* #### when there is one */ | |
250 #ifdef HAVE_WINDOW_SYSTEM | |
251 { | |
252 struct Lisp_Process *p = (struct Lisp_Process *) header; | |
253 debug_process_finalization (p); | |
254 } | |
255 #endif /* HAVE_WINDOW_SYSTEM */ | |
256 } | |
257 | |
258 | |
259 /************************************************************************/ | |
260 /* basic process accessors */ | |
261 /************************************************************************/ | |
262 | |
263 static SIGTYPE | |
264 close_safely_handler (int signo) | |
265 { | |
266 EMACS_REESTABLISH_SIGNAL (signo, close_safely_handler); | |
267 SIGRETURN; | |
268 } | |
269 | |
270 static void | |
271 close_safely (int fd) | |
272 { | |
273 stop_interrupts (); | |
274 signal (SIGALRM, close_safely_handler); | |
275 alarm (1); | |
276 close (fd); | |
277 alarm (0); | |
278 start_interrupts (); | |
279 } | |
280 | |
281 static void | |
282 close_descriptor_pair (int in, int out) | |
283 { | |
284 if (in >= 0) | |
285 close (in); | |
286 if (out != in && out >= 0) | |
287 close (out); | |
288 } | |
289 | |
290 /* Close all descriptors currently in use for communication | |
291 with subprocess. This is used in a newly-forked subprocess | |
292 to get rid of irrelevant descriptors. */ | |
293 | |
294 void | |
295 close_process_descs (void) | |
296 { | |
297 #ifndef WINDOWSNT | |
298 int i; | |
299 for (i = 0; i < MAXDESC; i++) | |
300 { | |
301 Lisp_Object process; | |
302 process = descriptor_to_process[i]; | |
303 if (!NILP (process)) | |
304 { | |
305 close_descriptor_pair (XPROCESS (process)->infd, | |
306 XPROCESS (process)->outfd); | |
307 } | |
308 } | |
309 #endif | |
310 } | |
311 | |
312 void | |
313 get_process_file_descriptors (struct Lisp_Process *p, int *infd, | |
314 int *outfd) | |
315 { | |
316 if (! p) abort (); | |
317 /* the cast of MAXDESC is needed for some versions of Linux */ | |
318 assert (p->infd >= -1 && p->infd < ((int) (MAXDESC))); | |
319 assert (p->outfd >= -1 && p->outfd < ((int) (MAXDESC))); | |
320 *infd = p->infd; | |
321 *outfd = p->outfd; | |
322 } | |
323 | |
324 struct Lisp_Process * | |
325 get_process_from_input_descriptor (int infd) | |
326 { | |
327 Lisp_Object proc; | |
328 | |
329 if ((infd < 0) || (infd >= ((int) (MAXDESC)))) abort (); | |
330 proc = descriptor_to_process[infd]; | |
331 if (NILP (proc)) | |
332 return 0; | |
333 else | |
334 return XPROCESS (proc); | |
335 } | |
336 | |
337 int | |
338 get_process_selected_p (struct Lisp_Process *p) | |
339 { | |
340 return p->selected; | |
341 } | |
342 | |
343 void | |
344 set_process_selected_p (struct Lisp_Process *p, int selected_p) | |
345 { | |
346 p->selected = !!selected_p; | |
347 } | |
348 | |
349 #ifdef HAVE_SOCKETS | |
350 int | |
351 network_connection_p (Lisp_Object process) | |
352 { | |
353 return (GC_CONSP (XPROCESS (process)->pid)); | |
354 } | |
355 #endif | |
356 | |
357 int | |
358 connected_via_filedesc_p (struct Lisp_Process *p) | |
359 { | |
360 return p->connected_via_filedesc_p; | |
361 } | |
362 | |
363 DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0 /* | |
364 Return t if OBJECT is a process. | |
365 */ ) | |
366 (obj) | |
367 Lisp_Object obj; | |
368 { | |
369 return ((PROCESSP (obj)) ? Qt : Qnil); | |
370 } | |
371 | |
372 DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0 /* | |
373 Return a list of all processes. | |
374 */ ) | |
375 () | |
376 { | |
377 return Fcopy_sequence (Vprocess_list); | |
378 } | |
379 | |
380 DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0 /* | |
381 Return the process named NAME, or nil if there is none. | |
382 */ ) | |
383 (name) | |
384 Lisp_Object name; | |
385 { | |
386 Lisp_Object tail; | |
387 | |
388 if (GC_PROCESSP (name)) | |
389 return (name); | |
390 | |
391 if (!gc_in_progress) | |
392 /* this only gets called during GC when emacs is going away as a result | |
393 of a signal or crash. */ | |
394 CHECK_STRING (name); | |
395 | |
396 for (tail = Vprocess_list; GC_CONSP (tail); tail = XCDR (tail)) | |
397 { | |
398 Lisp_Object proc = XCAR (tail); | |
399 QUIT; | |
400 if (!NILP (Fequal (name, XPROCESS (proc)->name))) | |
401 return (XCAR (tail)); | |
402 } | |
403 return Qnil; | |
404 } | |
405 | |
406 DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0 /* | |
407 Return the (or, a) process associated with BUFFER. | |
408 BUFFER may be a buffer or the name of one. | |
409 */ ) | |
410 (name) | |
411 Lisp_Object name; | |
412 { | |
413 Lisp_Object buf, tail, proc; | |
414 | |
415 if (GC_NILP (name)) return Qnil; | |
416 buf = Fget_buffer (name); | |
417 if (GC_NILP (buf)) return Qnil; | |
418 | |
419 #ifdef ENERGIZE | |
420 { | |
421 Lisp_Object p = energize_get_buffer_process (buf); | |
422 if (!GC_NILP (p)) return p; | |
423 } | |
424 #endif | |
425 | |
426 for (tail = Vprocess_list; GC_CONSP (tail); tail = XCDR (tail)) | |
427 { | |
428 /* jwz: do not quit here - it isn't necessary, as there is no way for | |
429 Vprocess_list to get circular or overwhelmingly long, and this | |
430 function is called from layout_mode_element under redisplay. */ | |
431 /* QUIT; */ | |
432 proc = XCAR (tail); | |
433 if (GC_PROCESSP (proc) && EQ (XPROCESS (proc)->buffer, buf)) | |
434 return proc; | |
435 } | |
436 return Qnil; | |
437 } | |
438 | |
439 /* This is how commands for the user decode process arguments. It | |
440 accepts a process, a process name, a buffer, a buffer name, or nil. | |
441 Buffers denote the first process in the buffer, and nil denotes the | |
442 current buffer. */ | |
443 | |
444 static Lisp_Object | |
445 get_process (Lisp_Object name) | |
446 { | |
447 Lisp_Object proc; | |
448 | |
449 #ifdef I18N3 | |
450 /* #### Look more closely into translating process names. */ | |
451 #endif | |
452 | |
453 /* This may be called during a GC from process_send_signal() from | |
454 kill_buffer_processes() if emacs decides to abort(). */ | |
455 if (GC_PROCESSP (name)) | |
456 return name; | |
457 | |
458 if (GC_NILP (name)) | |
459 proc = Fget_buffer_process (Fcurrent_buffer ()); | |
460 else | |
461 { | |
462 proc = Fget_process (name); | |
463 if (GC_NILP (proc)) | |
464 proc = Fget_buffer_process (Fget_buffer (name)); | |
465 } | |
466 | |
467 if (!GC_NILP (proc)) | |
468 return proc; | |
469 | |
470 if (GC_NILP (name)) | |
471 error ("Current buffer has no process"); | |
472 else | |
473 error ("Process %s does not exist", string_data (XSTRING (name))); | |
474 /* NOTREACHED */ | |
475 return Qnil; /* warning suppression */ | |
476 } | |
477 | |
478 DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0 /* | |
479 Return the process id of PROCESS. | |
480 This is the pid of the Unix process which PROCESS uses or talks to. | |
481 For a network connection, this value is a cons of | |
482 (foreign-network-port . foreign-host-name). | |
483 */ ) | |
484 (proc) | |
485 Lisp_Object proc; | |
486 { | |
487 Lisp_Object pid; | |
488 CHECK_PROCESS (proc); | |
489 | |
490 pid = XPROCESS (proc)->pid; | |
491 if (network_connection_p (proc)) | |
492 /* return (Qnil); */ | |
493 return (Fcons (Fcar (pid), Fcdr (pid))); | |
494 else | |
495 return (pid); | |
496 } | |
497 | |
498 DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0 /* | |
499 Return the name of PROCESS, as a string. | |
500 This is the name of the program invoked in PROCESS, | |
501 possibly modified to make it unique among process names. | |
502 */ ) | |
503 (proc) | |
504 Lisp_Object proc; | |
505 { | |
506 CHECK_PROCESS (proc); | |
507 return XPROCESS (proc)->name; | |
508 } | |
509 | |
510 DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0 /* | |
511 Return the command that was executed to start PROCESS. | |
512 This is a list of strings, the first string being the program executed | |
513 and the rest of the strings being the arguments given to it. | |
514 */ ) | |
515 (proc) | |
516 Lisp_Object proc; | |
517 { | |
518 CHECK_PROCESS (proc); | |
519 return XPROCESS (proc)->command; | |
520 } | |
521 | |
522 | |
523 /************************************************************************/ | |
524 /* creating a process */ | |
525 /************************************************************************/ | |
526 | |
527 static Lisp_Object | |
528 make_process_internal (Lisp_Object name) | |
529 { | |
530 Lisp_Object val, name1; | |
531 int i; | |
532 struct Lisp_Process *p | |
533 = alloc_lcrecord (sizeof (struct Lisp_Process), lrecord_process); | |
534 | |
535 /* If name is already in use, modify it until it is unused. */ | |
536 name1 = name; | |
537 for (i = 1; ; i++) | |
538 { | |
539 char suffix[10]; | |
540 Lisp_Object tem = Fget_process (name1); | |
541 if (NILP (tem)) | |
542 break; | |
543 sprintf (suffix, "<%d>", i); | |
544 name1 = concat2 (name, build_string (suffix)); | |
545 } | |
546 name = name1; | |
547 p->name = name; | |
548 | |
549 p->command = Qnil; | |
550 p->filter = Qnil; | |
551 p->sentinel = Qnil; | |
552 p->buffer = Qnil; | |
553 p->mark = Fmake_marker (); | |
554 p->pid = Qnil; | |
555 p->status_symbol = Qrun; | |
556 p->connected_via_filedesc_p = 0; | |
557 p->exit_code = 0; | |
558 p->core_dumped = 0; | |
559 p->infd = -1; | |
560 p->outfd = -1; | |
561 p->subtty = -1; | |
562 p->tty_name = Qnil; | |
563 p->pty_flag = 0; | |
564 p->filter_does_read = 0; | |
565 p->kill_without_query = 0; | |
566 p->selected = 0; | |
567 p->tick = 0; | |
568 p->update_tick = 0; | |
569 p->instream = Qnil; | |
570 p->outstream = Qnil; | |
571 | |
572 XSETPROCESS (val, p); | |
573 | |
574 Vprocess_list = Fcons (val, Vprocess_list); | |
575 return (val); | |
576 } | |
577 | |
578 #ifdef HAVE_PTYS | |
579 | |
580 /* Open an available pty, returning a file descriptor. | |
581 Return -1 on failure. | |
582 The file name of the terminal corresponding to the pty | |
583 is left in the variable pty_name. */ | |
584 | |
585 static int | |
586 allocate_pty (void) | |
587 { | |
588 struct stat stb; | |
589 int c, i; | |
590 int fd; | |
591 | |
592 /* Some systems name their pseudoterminals so that there are gaps in | |
593 the usual sequence - for example, on HP9000/S700 systems, there | |
594 are no pseudoterminals with names ending in 'f'. So we wait for | |
595 three failures in a row before deciding that we've reached the | |
596 end of the ptys. */ | |
597 int failed_count = 0; | |
598 | |
599 #ifdef PTY_ITERATION | |
600 PTY_ITERATION | |
601 #else | |
602 for (c = FIRST_PTY_LETTER; c <= 'z'; c++) | |
603 for (i = 0; i < 16; i++) | |
604 #endif | |
605 { | |
606 #ifdef PTY_NAME_SPRINTF | |
607 PTY_NAME_SPRINTF | |
608 #else | |
609 sprintf (pty_name, "/dev/pty%c%x", c, i); | |
610 #endif /* no PTY_NAME_SPRINTF */ | |
611 | |
612 #ifdef PTY_OPEN | |
613 PTY_OPEN; | |
614 #else /* no PTY_OPEN */ | |
615 #ifdef IRIS | |
616 /* Unusual IRIS code */ | |
617 *ptyv = open ("/dev/ptc", O_RDWR | O_NDELAY, 0); | |
618 if (fd < 0) | |
619 return -1; | |
620 if (fstat (fd, &stb) < 0) | |
621 return -1; | |
622 #else /* not IRIS */ | |
623 if (stat (pty_name, &stb) < 0) | |
624 { | |
625 failed_count++; | |
626 if (failed_count >= 3) | |
627 return -1; | |
628 } | |
629 else | |
630 failed_count = 0; | |
631 #ifdef O_NONBLOCK | |
632 fd = open (pty_name, O_RDWR | O_NONBLOCK, 0); | |
633 #else | |
634 fd = open (pty_name, O_RDWR | O_NDELAY, 0); | |
635 #endif | |
636 #endif /* not IRIS */ | |
637 #endif /* no PTY_OPEN */ | |
638 | |
639 if (fd >= 0) | |
640 { | |
641 /* check to make certain that both sides are available | |
642 this avoids a nasty yet stupid bug in rlogins */ | |
643 #ifdef PTY_TTY_NAME_SPRINTF | |
644 PTY_TTY_NAME_SPRINTF | |
645 #else | |
646 sprintf (pty_name, "/dev/tty%c%x", c, i); | |
647 #endif /* no PTY_TTY_NAME_SPRINTF */ | |
648 #ifndef UNIPLUS | |
649 if (access (pty_name, 6) != 0) | |
650 { | |
651 close (fd); | |
652 #if !defined(IRIS) && !defined(__sgi) | |
653 continue; | |
654 #else | |
655 return -1; | |
656 #endif /* IRIS */ | |
657 } | |
658 #endif /* not UNIPLUS */ | |
659 setup_pty (fd); | |
660 return fd; | |
661 } | |
662 } | |
663 return -1; | |
664 } | |
665 #endif /* HAVE_PTYS */ | |
666 | |
667 static int | |
668 create_bidirectional_pipe (int *inchannel, int *outchannel, | |
669 volatile int *forkin, volatile int *forkout) | |
670 { | |
671 int sv[2]; | |
672 | |
673 #ifdef SKTPAIR | |
674 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) < 0) | |
675 return -1; | |
676 *outchannel = *inchannel = sv[0]; | |
677 *forkout = *forkin = sv[1]; | |
678 #else /* not SKTPAIR */ | |
679 #ifdef WINDOWSNT | |
680 pipe_with_inherited_out (sv); | |
681 *inchannel = sv[0]; | |
682 *forkout = sv[1]; | |
683 | |
684 pipe_with_inherited_in (sv); | |
685 *forkin = sv[0]; | |
686 *outchannel = sv[1]; | |
687 #else /* not WINDOWSNT */ | |
688 int temp; | |
689 temp = pipe (sv); | |
690 if (temp < 0) return -1; | |
691 *inchannel = sv[0]; | |
692 *forkout = sv[1]; | |
693 temp = pipe (sv); | |
694 if (temp < 0) return -1; | |
695 *outchannel = sv[1]; | |
696 *forkin = sv[0]; | |
697 #endif /* not WINDOWSNT */ | |
698 #endif /* not SKTPAIR */ | |
699 return 0; | |
700 } | |
701 | |
702 #ifndef VMS /* VMS version of this function is in vmsproc.c. */ | |
703 | |
704 static Bufbyte | |
705 get_eof_char (struct Lisp_Process *p) | |
706 { | |
707 /* Figure out the eof character for the outfd of the given process. | |
708 * The following code is similar to that in process_send_signal, and | |
709 * should probably be merged with that code somehow. */ | |
710 | |
711 #ifdef HAVE_TERMIOS | |
712 struct termios t; | |
713 tcgetattr (p->outfd, &t); | |
714 if (strlen ((CONST char *) t.c_cc) < (unsigned int) (VEOF + 1)) | |
715 return (Bufbyte) '\004'; | |
716 else | |
717 return (Bufbyte) t.c_cc[VEOF]; | |
718 #else /* ! HAVE_TERMIOS */ | |
719 /* On Berkeley descendants, the following IOCTL's retrieve the | |
720 current control characters. */ | |
721 #if defined (TIOCGETC) | |
722 struct tchars c; | |
723 ioctl (p->outfd, TIOCGETC, &c); | |
724 return (Bufbyte) c.t_eofc; | |
725 #else /* ! defined (TIOCGLTC) && defined (TIOCGETC) */ | |
726 /* On SYSV descendants, the TCGETA ioctl retrieves the current control | |
727 characters. */ | |
728 #ifdef TCGETA | |
729 struct termio t; | |
730 ioctl (p->outfd, TCGETA, &t); | |
731 if (strlen ((CONST char *) t.c_cc) < (unsigned int) (VINTR + 1)) | |
732 return (Bufbyte) '\004'; | |
733 else | |
734 return (Bufbyte) t.c_cc[VINTR]; | |
735 #else /* ! defined (TCGETA) */ | |
736 /* Rather than complain, we'll just guess ^D, which is what | |
737 * earlier emacsen always used. */ | |
738 return (Bufbyte) '\004'; | |
739 #endif /* ! defined (TCGETA) */ | |
740 #endif /* ! defined (TIOCGETC) */ | |
741 #endif /* ! defined (HAVE_TERMIOS) */ | |
742 } | |
743 | |
744 static int | |
745 get_pty_max_bytes (struct Lisp_Process *p) | |
746 { | |
747 int pty_max_bytes; | |
748 | |
749 #if defined (HAVE_FPATHCONF) && defined (_PC_MAX_CANON) | |
750 pty_max_bytes = fpathconf (p->outfd, _PC_MAX_CANON); | |
751 if (pty_max_bytes < 0) | |
752 pty_max_bytes = 250; | |
753 #else | |
754 pty_max_bytes = 250; | |
755 #endif | |
756 /* Deduct one, to leave space for the eof. */ | |
757 pty_max_bytes--; | |
758 | |
759 return pty_max_bytes; | |
760 } | |
761 | |
762 static void | |
763 init_process_fds (struct Lisp_Process *p, int in, int out) | |
764 { | |
765 p->infd = in; | |
766 p->outfd = out; | |
767 p->instream = make_filedesc_input_stream (in, 0, -1, 0); | |
768 p->outstream = make_filedesc_output_stream (out, 0, -1, | |
769 LSTR_BLOCKED_OK | |
770 | (p->pty_flag ? | |
771 LSTR_PTY_FLUSHING : 0)); | |
772 p->filedesc_stream = p->outstream; | |
773 if (p->pty_flag) | |
774 { | |
775 Bufbyte eof_char = get_eof_char (p); | |
776 int pty_max_bytes = get_pty_max_bytes (p); | |
777 filedesc_stream_set_pty_flushing (XLSTREAM (p->outstream), | |
778 pty_max_bytes, eof_char); | |
779 } | |
780 } | |
781 | |
782 static void | |
783 create_process (Lisp_Object process, | |
784 char **new_argv, CONST char *current_dir) | |
785 { | |
786 /* This function rewritten by wing@666.com. */ | |
787 | |
788 int pid, inchannel, outchannel; | |
789 /* Use volatile to protect variables from being clobbered by longjmp. */ | |
790 volatile int forkin, forkout; | |
791 volatile int pty_flag = 0; | |
792 char **env; | |
793 struct Lisp_Process *p = XPROCESS (process); | |
794 | |
795 env = environ; | |
796 | |
797 inchannel = outchannel = forkin = forkout = -1; | |
798 | |
799 #ifdef HAVE_PTYS | |
800 if (!NILP (Vprocess_connection_type)) | |
801 { | |
802 /* find a new pty, open the master side, return the opened | |
803 file handle, and store the name of the corresponding slave | |
804 side in global variable pty_name. */ | |
805 outchannel = inchannel = allocate_pty (); | |
806 } | |
807 | |
808 if (inchannel >= 0) | |
809 { | |
810 /* You're "supposed" to now open the slave in the child. | |
811 On some systems, we can open it here; this allows for | |
812 better error checking. */ | |
813 #ifndef USG | |
814 /* On USG systems it does not work to open the pty's tty here | |
815 and then close and reopen it in the child. */ | |
816 #ifdef O_NOCTTY | |
817 /* Don't let this terminal become our controlling terminal | |
818 (in case we don't have one). */ | |
819 forkout = forkin = open (pty_name, O_RDWR | O_NOCTTY, 0); | |
820 #else | |
821 forkout = forkin = open (pty_name, O_RDWR, 0); | |
822 #endif | |
823 if (forkin < 0) | |
824 goto io_failure; | |
825 #endif /* not USG */ | |
826 p->pty_flag = pty_flag = 1; | |
827 } | |
828 else | |
829 #endif /* HAVE_PTYS */ | |
830 if (create_bidirectional_pipe (&inchannel, &outchannel, | |
831 &forkin, &forkout) < 0) | |
832 goto io_failure; | |
833 | |
834 #if 0 | |
835 /* Replaced by close_process_descs */ | |
836 set_exclusive_use (inchannel); | |
837 set_exclusive_use (outchannel); | |
838 #endif | |
839 | |
840 set_descriptor_non_blocking (inchannel); | |
841 | |
842 /* Record this as an active process, with its channels. | |
843 As a result, child_setup will close Emacs's side of the pipes. */ | |
844 descriptor_to_process[inchannel] = process; | |
845 init_process_fds (p, inchannel, outchannel); | |
846 /* Record the tty descriptor used in the subprocess. */ | |
847 p->subtty = forkin; | |
848 p->status_symbol = Qrun; | |
849 p->exit_code = 0; | |
850 | |
851 { | |
852 /* child_setup must clobber environ on systems with true vfork. | |
853 Protect it from permanent change. */ | |
854 char **save_environ = environ; | |
855 | |
856 #ifdef EMACS_BTL | |
857 /* when performance monitoring is on, turn it off before the vfork(), | |
858 as the child has no handler for the signal -- when back in the | |
859 parent process, turn it back on if it was really on when you "turned | |
860 it off" */ | |
861 int logging_on = cadillac_stop_logging (); /* #### rename me */ | |
862 #endif | |
863 | |
864 #ifndef WINDOWSNT | |
865 pid = vfork (); | |
866 if (pid == 0) | |
867 #endif /* not WINDOWSNT */ | |
868 { | |
869 /**** Now we're in the child process ****/ | |
870 int xforkin = forkin; | |
871 int xforkout = forkout; | |
872 | |
873 if (!pty_flag) | |
874 EMACS_SEPARATE_PROCESS_GROUP (); | |
875 #ifdef HAVE_PTYS | |
876 else | |
877 { | |
878 /* Disconnect the current controlling terminal, pursuant to | |
879 making the pty be the controlling terminal of the process. | |
880 Also put us in our own process group. */ | |
881 | |
882 disconnect_controlling_terminal (); | |
883 | |
884 /* Open the pty connection and make the pty's terminal | |
885 our controlling terminal. | |
886 | |
887 On systems with TIOCSCTTY, we just use it to set | |
888 the controlling terminal. On other systems, the | |
889 first TTY we open becomes the controlling terminal. | |
890 So, we end up with four possibilities: | |
891 | |
892 (1) on USG and TIOCSCTTY systems, we open the pty | |
893 and use TIOCSCTTY. | |
894 (2) on other USG systems, we just open the pty. | |
895 (3) on non-USG systems with TIOCSCTTY, we | |
896 just use TIOCSCTTY. (On non-USG systems, we | |
897 already opened the pty in the parent process.) | |
898 (4) on non-USG systems without TIOCSCTTY, we | |
899 close the pty and reopen it. | |
900 | |
901 This would be cleaner if we didn't open the pty | |
902 in the parent process, but doing it that way | |
903 makes it possible to trap error conditions. | |
904 It's harder to convey an error from the child | |
905 process, and I don't feel like messing with | |
906 this now. */ | |
907 | |
908 /* There was some weirdo, probably wrong, | |
909 conditionalization on RTU and UNIPLUS here. | |
910 I deleted it. So sue me. */ | |
911 | |
912 /* SunOS has TIOCSCTTY but the close/open method | |
913 also works. */ | |
914 | |
915 # if defined (USG) || !defined (TIOCSCTTY) | |
916 /* Now close the pty (if we had it open) and reopen it. | |
917 This makes the pty the controlling terminal of the | |
918 subprocess. */ | |
919 /* I wonder if close (open (pty_name, ...)) would work? */ | |
920 if (xforkin >= 0) | |
921 close (xforkin); | |
922 xforkout = xforkin = open (pty_name, O_RDWR, 0); | |
923 if (xforkin < 0) | |
924 { | |
925 write (1, "Couldn't open the pty terminal ", 31); | |
926 write (1, pty_name, strlen (pty_name)); | |
927 write (1, "\n", 1); | |
928 _exit (1); | |
929 } | |
930 # endif /* USG or not TIOCSCTTY */ | |
931 | |
932 /* Miscellaneous setup required for some systems. | |
933 Must be done before using tc* functions on xforkin. | |
934 This guarantees that isatty(xforkin) is true. */ | |
935 | |
936 # ifdef SETUP_SLAVE_PTY | |
937 SETUP_SLAVE_PTY; | |
938 # endif /* SETUP_SLAVE_PTY */ | |
939 | |
940 # ifdef TIOCSCTTY | |
941 /* We ignore the return value | |
942 because faith@cs.unc.edu says that is necessary on Linux. */ | |
943 assert (isatty (xforkin)); | |
944 ioctl (xforkin, TIOCSCTTY, 0); | |
945 # endif /* TIOCSCTTY */ | |
946 | |
947 /* Change the line discipline. */ | |
948 | |
949 # if defined (HAVE_TERMIOS) && defined (LDISC1) | |
950 { | |
951 struct termios t; | |
952 assert (isatty (xforkin)); | |
953 tcgetattr (xforkin, &t); | |
954 t.c_lflag = LDISC1; | |
955 if (tcsetattr (xforkin, TCSANOW, &t) < 0) | |
956 perror ("create_process/tcsetattr LDISC1 failed\n"); | |
957 } | |
958 # elif defined (NTTYDISC) && defined (TIOCSETD) | |
959 { | |
960 /* Use new line discipline. TIOCSETD is accepted and | |
961 ignored on Sys5.4 systems with ttcompat. */ | |
962 int ldisc = NTTYDISC; | |
963 assert (isatty (xforkin)); | |
964 ioctl (xforkin, TIOCSETD, &ldisc); | |
965 } | |
966 # endif /* TIOCSETD & NTTYDISC */ | |
967 | |
968 /* Make our process group be the foreground group | |
969 of our new controlling terminal. */ | |
970 | |
971 { | |
972 int piddly = EMACS_GET_PROCESS_GROUP (); | |
973 EMACS_SET_TTY_PROCESS_GROUP (xforkin, &piddly); | |
974 } | |
975 | |
976 # ifdef AIX | |
977 /* On AIX, we've disabled SIGHUP above once we start a | |
978 child on a pty. Now reenable it in the child, so it | |
979 will die when we want it to. */ | |
980 signal (SIGHUP, SIG_DFL); | |
981 # endif /* AIX */ | |
982 } | |
983 #endif /* HAVE_PTYS */ | |
984 | |
985 signal (SIGINT, SIG_DFL); | |
986 signal (SIGQUIT, SIG_DFL); | |
987 | |
988 #ifndef MSDOS | |
989 if (pty_flag) | |
990 { | |
991 /* Set up the terminal characteristics of the pty. */ | |
992 child_setup_tty (xforkout); | |
993 } | |
994 | |
995 #ifdef WINDOWSNT | |
996 pid = child_setup (xforkin, xforkout, xforkout, | |
997 new_argv, current_dir); | |
998 #else /* not WINDOWSNT */ | |
999 child_setup (xforkin, xforkout, xforkout, new_argv, current_dir); | |
1000 #endif /* not WINDOWSNT */ | |
1001 #endif /* not MSDOS */ | |
1002 } | |
1003 #ifdef EMACS_BTL | |
1004 else if (logging_on) | |
1005 cadillac_start_logging (); /* #### rename me */ | |
1006 #endif | |
1007 | |
1008 environ = save_environ; | |
1009 } | |
1010 | |
1011 if (pid < 0) | |
1012 { | |
1013 close_descriptor_pair (forkin, forkout); | |
1014 report_file_error ("Doing vfork", Qnil); | |
1015 } | |
1016 | |
1017 p->pid = make_int (pid); | |
1018 /* p->subtty = -1; */ | |
1019 | |
1020 #ifdef WINDOWSNT | |
1021 register_child (pid, inchannel); | |
1022 #endif /* WINDOWSNT */ | |
1023 | |
1024 /* If the subfork execv fails, and it exits, | |
1025 this close hangs. I don't know why. | |
1026 So have an interrupt jar it loose. */ | |
1027 if (forkin >= 0) | |
1028 close_safely (forkin); | |
1029 if (forkin != forkout && forkout >= 0) | |
1030 close (forkout); | |
1031 | |
1032 #ifdef HAVE_PTYS | |
1033 if (pty_flag) | |
1034 XPROCESS (process)->tty_name = build_string (pty_name); | |
1035 else | |
1036 #endif | |
1037 XPROCESS (process)->tty_name = Qnil; | |
1038 | |
1039 /* Notice that SIGCHLD was not blocked. (This is not possible on | |
1040 some systems.) No biggie if SIGCHLD occurs right around the | |
1041 time that this call happens, because SIGCHLD() does not actually | |
1042 deselect the process (that doesn't occur until the next time | |
1043 we're waiting for an event, when status_notify() is called). */ | |
1044 event_stream_select_process (XPROCESS (process)); | |
1045 | |
1046 return; | |
1047 | |
1048 io_failure: | |
1049 { | |
1050 int temp = errno; | |
1051 close_descriptor_pair (forkin, forkout); | |
1052 close_descriptor_pair (inchannel, outchannel); | |
1053 errno = temp; | |
1054 report_file_error ("Opening pty or pipe", Qnil); | |
1055 } | |
1056 } | |
1057 #endif /* not VMS */ | |
1058 | |
1059 /* This function is the unwind_protect form for Fstart_process_internal. If | |
1060 PROC doesn't have its pid set, then we know someone has signalled | |
1061 an error and the process wasn't started successfully, so we should | |
1062 remove it from the process list. */ | |
1063 static void remove_process (Lisp_Object proc); | |
1064 static Lisp_Object | |
1065 start_process_unwind (Lisp_Object proc) | |
1066 { | |
1067 /* Was PROC started successfully? */ | |
1068 if (EQ (XPROCESS (proc)->pid, Qnil)) | |
1069 remove_process (proc); | |
1070 return Qnil; | |
1071 } | |
1072 | |
1073 DEFUN ("start-process-internal", Fstart_process_internal, | |
1074 Sstart_process_internal, 3, MANY, 0 /* | |
1075 Start a program in a subprocess. Return the process object for it. | |
1076 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS | |
1077 NAME is name for process. It is modified if necessary to make it unique. | |
1078 BUFFER is the buffer or (buffer-name) to associate with the process. | |
1079 Process output goes at end of that buffer, unless you specify | |
1080 an output stream or filter function to handle the output. | |
1081 BUFFER may be also nil, meaning that this process is not associated | |
1082 with any buffer | |
1083 Third arg is program file name. It is searched for as in the shell. | |
1084 Remaining arguments are strings to give program as arguments. | |
1085 INCODE and OUTCODE specify the coding-system objects used in input/output | |
1086 from/to the process. | |
1087 */ ) | |
1088 (nargs, args) | |
1089 int nargs; | |
1090 Lisp_Object *args; | |
1091 { | |
1092 /* !!#### This function has not been Mule-ized */ | |
1093 /* This function can GC */ | |
1094 Lisp_Object buffer, name, program, proc, current_dir; | |
1095 Lisp_Object tem; | |
1096 int speccount = specpdl_depth (); | |
1097 #ifdef VMS | |
1098 char *new_argv; | |
1099 int len; | |
1100 #else | |
1101 char **new_argv; | |
1102 #endif | |
1103 int i; | |
1104 | |
1105 buffer = args[1]; | |
1106 if (!NILP (buffer)) | |
1107 buffer = Fget_buffer_create (buffer); | |
1108 | |
1109 CHECK_STRING (args[0]); /* name */ | |
1110 CHECK_STRING (args[2]); /* program */ | |
1111 | |
1112 /* Make sure that the child will be able to chdir to the current | |
1113 buffer's current directory, or its unhandled equivalent. We | |
1114 can't just have the child check for an error when it does the | |
1115 chdir, since it's in a vfork. | |
1116 | |
1117 We have to GCPRO around this because Fexpand_file_name and | |
1118 Funhandled_file_name_directory might call a file name handling | |
1119 function. The argument list is protected by the caller, so all | |
1120 we really have to worry about is buffer. */ | |
1121 { | |
1122 struct gcpro gcpro1, gcpro2; /* Caller gc-protects args[] */ | |
1123 | |
1124 current_dir = current_buffer->directory; | |
1125 | |
1126 GCPRO2 (buffer, current_dir); | |
1127 | |
1128 current_dir = | |
1129 expand_and_dir_to_file (Funhandled_file_name_directory (current_dir), | |
1130 Qnil); | |
1131 #if 0 /* This loser breaks ange-ftp */ | |
1132 if (NILP (Ffile_accessible_directory_p (current_dir))) | |
1133 report_file_error ("Setting current directory", | |
1134 list1 (current_buffer->directory)); | |
1135 #endif /* 0 */ | |
1136 | |
1137 UNGCPRO; | |
1138 } | |
1139 | |
1140 name = args[0]; | |
1141 program = args[2]; | |
1142 | |
1143 #ifdef VMS | |
1144 /* Make a one member argv with all args concatenated | |
1145 together separated by a blank. */ | |
1146 len = string_length (XSTRING (program)) + 2; | |
1147 for (i = 3; i < nargs; i++) | |
1148 { | |
1149 tem = args[i]; | |
1150 CHECK_STRING (tem); | |
1151 len += string_length (XSTRING (tem)) + 1; /* count the blank */ | |
1152 } | |
1153 new_argv = (char *) alloca (len); | |
1154 strcpy (new_argv, string_data (XSTRING (program))); | |
1155 for (i = 3; i < nargs; i++) | |
1156 { | |
1157 tem = args[i]; | |
1158 CHECK_STRING (tem); | |
1159 strcat (new_argv, " "); | |
1160 strcat (new_argv, string_data (XSTRING (tem))); | |
1161 } | |
1162 /* Need to add code here to check for program existence on VMS */ | |
1163 | |
1164 #else /* not VMS */ | |
1165 new_argv = (char **) | |
1166 alloca ((nargs - 1) * sizeof (char *)); | |
1167 | |
1168 new_argv[0] = (char *) string_data (XSTRING (program)); | |
1169 | |
1170 /* If program file name is not absolute, search our path for it */ | |
1171 if (!IS_DIRECTORY_SEP (string_byte (XSTRING (program), 0)) | |
1172 && !(string_length (XSTRING (program)) > 1 | |
1173 && IS_DEVICE_SEP (string_byte (XSTRING (program), 1)))) | |
1174 { | |
1175 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; /* Caller protects args[] */ | |
1176 GCPRO4 (buffer, current_dir, name, program); | |
1177 | |
1178 tem = Qnil; | |
1179 locate_file (Vexec_path, program, EXEC_SUFFIXES, &tem, | |
1180 X_OK); | |
1181 UNGCPRO; | |
1182 if (NILP (tem)) | |
1183 report_file_error ("Searching for program", list1 (program)); | |
1184 tem = Fexpand_file_name (tem, Qnil); | |
1185 new_argv[0] = (char *) string_data (XSTRING (tem)); | |
1186 } | |
1187 else | |
1188 { | |
1189 if (!NILP (Ffile_directory_p (program))) | |
1190 error ("Specified program for new process is a directory"); | |
1191 } | |
1192 | |
1193 for (i = 3; i < nargs; i++) | |
1194 { | |
1195 tem = args[i]; | |
1196 CHECK_STRING (tem); | |
1197 new_argv[i - 2] = | |
1198 (char *) string_data (XSTRING (tem)); | |
1199 } | |
1200 new_argv[i - 2] = 0; | |
1201 | |
1202 #endif /* not VMS */ | |
1203 | |
1204 proc = make_process_internal (name); | |
1205 | |
1206 XPROCESS (proc)->buffer = buffer; | |
1207 XPROCESS (proc)->command = Flist (nargs - 2, | |
1208 args + 2); | |
1209 | |
1210 /* Make the process marker point into the process buffer (if any). */ | |
1211 if (!NILP (buffer)) | |
1212 Fset_marker (XPROCESS (proc)->mark, | |
1213 make_int (BUF_ZV (XBUFFER (buffer))), buffer); | |
1214 | |
1215 /* If an error occurs and we can't start the process, we want to | |
1216 remove it from the process list. This means that each error | |
1217 check in create_process doesn't need to call remove_process | |
1218 itself; it's all taken care of here. */ | |
1219 record_unwind_protect (start_process_unwind, proc); | |
1220 | |
1221 create_process (proc, new_argv, | |
1222 (char *) string_data (XSTRING (current_dir))); | |
1223 | |
1224 return unbind_to (speccount, proc); | |
1225 } | |
1226 | |
1227 | |
1228 /* connect to an existing file descriptor. This is very similar to | |
1229 open-network-stream except that it assumes that the connection has | |
1230 already been initialized. It is currently used for ToolTalk | |
1231 communication. */ | |
1232 | |
1233 /* This function used to be visible on the Lisp level, but there is no | |
1234 real point in doing that. Here is the doc string: | |
1235 | |
1236 "Connect to an existing file descriptor.\n\ | |
1237 Returns a subprocess-object to represent the connection.\n\ | |
1238 Input and output work as for subprocesses; `delete-process' closes it.\n\ | |
1239 Args are NAME BUFFER INFD OUTFD.\n\ | |
1240 NAME is name for process. It is modified if necessary to make it unique.\n\ | |
1241 BUFFER is the buffer (or buffer-name) to associate with the process.\n\ | |
1242 Process output goes at end of that buffer, unless you specify\n\ | |
1243 an output stream or filter function to handle the output.\n\ | |
1244 BUFFER may be also nil, meaning that this process is not associated\n\ | |
1245 with any buffer\n\ | |
1246 INFD and OUTFD specify the file descriptors to use for input and\n\ | |
1247 output, respectively." | |
1248 */ | |
1249 | |
1250 Lisp_Object | |
1251 connect_to_file_descriptor (Lisp_Object name, Lisp_Object buffer, | |
1252 Lisp_Object infd, Lisp_Object outfd) | |
1253 { | |
1254 /* This function can GC */ | |
1255 Lisp_Object proc; | |
1256 int inch; | |
1257 | |
1258 CHECK_STRING (name); | |
1259 CHECK_INT (infd); | |
1260 CHECK_INT (outfd); | |
1261 | |
1262 inch = XINT (infd); | |
1263 if (!NILP (descriptor_to_process[inch])) | |
1264 error ("There is already a process connected to fd %d", inch); | |
1265 if (!NILP (buffer)) | |
1266 buffer = Fget_buffer_create (buffer); | |
1267 proc = make_process_internal (name); | |
1268 | |
1269 descriptor_to_process[inch] = proc; | |
1270 | |
1271 XPROCESS (proc)->pid = Fcons (infd, name); | |
1272 XPROCESS (proc)->buffer = buffer; | |
1273 init_process_fds (XPROCESS (proc), inch, XINT (outfd)); | |
1274 XPROCESS (proc)->connected_via_filedesc_p = 1; | |
1275 | |
1276 event_stream_select_process (XPROCESS (proc)); | |
1277 | |
1278 return proc; | |
1279 } | |
1280 | |
1281 | |
1282 #ifdef HAVE_SOCKETS | |
1283 | |
1284 static int | |
1285 get_internet_address (Lisp_Object host, struct sockaddr_in *address, | |
1286 Error_behavior errb) | |
1287 { | |
1288 struct hostent *host_info_ptr; | |
1289 | |
1290 #ifndef HAVE_TERM | |
1291 memset (address, 0, sizeof (*address)); | |
1292 | |
1293 while (1) | |
1294 { | |
1295 #ifdef TRY_AGAIN | |
1296 h_errno = 0; | |
1297 #endif | |
1298 /* Some systems can't handle SIGIO/SIGALARM in gethostbyname. */ | |
1299 slow_down_interrupts (); | |
1300 host_info_ptr = gethostbyname ((char *) string_data (XSTRING (host))); | |
1301 speed_up_interrupts (); | |
1302 #ifdef TRY_AGAIN | |
1303 if (! (host_info_ptr == 0 && h_errno == TRY_AGAIN)) | |
1304 #endif | |
1305 break; | |
1306 Fsleep_for (make_int (1)); | |
1307 } | |
1308 if (host_info_ptr) | |
1309 { | |
1310 address->sin_family = host_info_ptr->h_addrtype; | |
1311 memcpy (&address->sin_addr, host_info_ptr->h_addr, host_info_ptr->h_length); | |
1312 } | |
1313 else | |
1314 { | |
1315 IN_ADDR numeric_addr; | |
1316 /* Attempt to interpret host as numeric inet address */ | |
1317 numeric_addr = inet_addr ((char *) string_data (XSTRING (host))); | |
1318 if (NUMERIC_ADDR_ERROR) | |
1319 { | |
1320 maybe_error (Qprocess, errb, | |
1321 "Unknown host \"%s\"", string_data (XSTRING (host))); | |
1322 return 0; | |
1323 } | |
1324 | |
1325 /* There was some broken code here that called strlen() here | |
1326 on (char *) &numeric_addr and even sometimes accessed | |
1327 uninitialized data. */ | |
1328 address->sin_family = AF_INET; | |
1329 * (IN_ADDR *) &address->sin_addr = numeric_addr; | |
1330 } | |
1331 | |
1332 return 1; | |
1333 } | |
1334 | |
1335 /* open a TCP network connection to a given HOST/SERVICE. Treated | |
1336 exactly like a normal process when reading and writing. Only | |
1337 differences are in status display and process deletion. A network | |
1338 connection has no PID; you cannot signal it. All you can do is | |
1339 deactivate and close it via delete-process */ | |
1340 | |
1341 DEFUN ("open-network-stream-internal", Fopen_network_stream_internal, | |
1342 Sopen_network_stream_internal, | |
1343 4, 4, 0 /* | |
1344 Open a TCP connection for a service to a host. | |
1345 Returns a subprocess-object to represent the connection. | |
1346 Input and output work as for subprocesses; `delete-process' closes it. | |
1347 Args are NAME BUFFER HOST SERVICE. | |
1348 NAME is name for process. It is modified if necessary to make it unique. | |
1349 BUFFER is the buffer (or buffer-name) to associate with the process. | |
1350 Process output goes at end of that buffer, unless you specify | |
1351 an output stream or filter function to handle the output. | |
1352 BUFFER may be also nil, meaning that this process is not associated | |
1353 with any buffer | |
1354 Third arg is name of the host to connect to, or its IP address. | |
1355 Fourth arg SERVICE is name of the service desired, or an integer | |
1356 specifying a port number to connect to. | |
1357 */ ) | |
1358 (name, buffer, host, service) | |
1359 Lisp_Object name, buffer, host, service; | |
1360 { | |
1361 /* !!#### This function has not been Mule-ized */ | |
1362 /* This function can GC */ | |
1363 Lisp_Object proc; | |
1364 struct sockaddr_in address; | |
1365 int s, outch, inch; | |
1366 int port; | |
1367 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
1368 int retry = 0; | |
1369 int retval; | |
1370 | |
1371 GCPRO4 (name, buffer, host, service); | |
1372 CHECK_STRING (name); | |
1373 CHECK_STRING (host); | |
1374 if (INTP (service)) | |
1375 port = htons ((unsigned short) XINT (service)); | |
1376 else | |
1377 { | |
1378 struct servent *svc_info; | |
1379 CHECK_STRING (service); | |
1380 svc_info = getservbyname ((char *) string_data (XSTRING (service)), | |
1381 "tcp"); | |
1382 if (svc_info == 0) | |
1383 #ifdef WIN32 | |
1384 error ("Unknown service \"%s\" (%d)", | |
1385 string_data (XSTRING (service)), WSAGetLastError ()); | |
1386 #else | |
1387 error ("Unknown service \"%s\"", string_data (XSTRING (service))); | |
1388 #endif | |
1389 port = svc_info->s_port; | |
1390 } | |
1391 | |
1392 get_internet_address (host, &address, ERROR_ME); | |
1393 address.sin_port = port; | |
1394 | |
1395 s = socket (address.sin_family, SOCK_STREAM, 0); | |
1396 if (s < 0) | |
1397 report_file_error ("error creating socket", list1 (name)); | |
1398 | |
1399 /* Turn off interrupts here -- see comments below. There used to | |
1400 be code which called bind_polling_period() to slow the polling | |
1401 period down rather than turn it off, but that seems rather | |
1402 bogus to me. Best thing here is to use a non-blocking connect | |
1403 or something, to check for QUIT. */ | |
1404 | |
1405 /* Comments that are not quite valid: */ | |
1406 | |
1407 /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR) | |
1408 when connect is interrupted. So let's not let it get interrupted. | |
1409 Note we do not turn off polling, because polling is only used | |
1410 when not interrupt_input, and thus not normally used on the systems | |
1411 which have this bug. On systems which use polling, there's no way | |
1412 to quit if polling is turned off. */ | |
1413 | |
1414 /* Slow down polling. Some kernels have a bug which causes retrying | |
1415 connect to fail after a connect. */ | |
1416 | |
1417 slow_down_interrupts (); | |
1418 | |
1419 loop: | |
1420 | |
1421 /* A system call interrupted with a SIGALRM or SIGIO comes back | |
1422 here, with can_break_system_calls reset to 0. */ | |
1423 SETJMP (break_system_call_jump); | |
1424 if (QUITP) | |
1425 { | |
1426 speed_up_interrupts (); | |
1427 REALLY_QUIT; | |
1428 /* In case something really weird happens ... */ | |
1429 slow_down_interrupts (); | |
1430 } | |
1431 | |
1432 /* Break out of connect with a signal (it isn't otherwise possible). | |
1433 Thus you don't get screwed with a hung network. */ | |
1434 can_break_system_calls = 1; | |
1435 retval = connect (s, (struct sockaddr *) &address, sizeof (address)); | |
1436 can_break_system_calls = 0; | |
1437 if (retval == -1 && errno != EISCONN) | |
1438 { | |
1439 int xerrno = errno; | |
1440 if (errno == EINTR) | |
1441 goto loop; | |
1442 if (errno == EADDRINUSE && retry < 20) | |
1443 { | |
1444 /* A delay here is needed on some FreeBSD systems, | |
1445 and it is harmless, since this retrying takes time anyway | |
1446 and should be infrequent. */ | |
1447 Fsleep_for (make_int (1)); | |
1448 retry++; | |
1449 goto loop; | |
1450 } | |
1451 | |
1452 close (s); | |
1453 | |
1454 speed_up_interrupts (); | |
1455 | |
1456 errno = xerrno; | |
1457 report_file_error ("connection failed", list2 (host, name)); | |
1458 } | |
1459 | |
1460 speed_up_interrupts (); | |
1461 | |
1462 #else /* HAVE_TERM */ | |
1463 s = connect_server (0); | |
1464 if (s < 0) | |
1465 report_file_error ("error creating socket", Fcons (name, Qnil)); | |
1466 send_command (s, C_PORT, 0, "%s:%d", string_data (XSTRING (host)), ntohs (port)); | |
1467 send_command (s, C_DUMB, 1, 0); | |
1468 #endif /* HAVE_TERM */ | |
1469 | |
1470 inch = s; | |
1471 outch = dup (s); | |
1472 if (outch < 0) | |
1473 { | |
1474 close (s); /* this used to be leaked; from Kyle Jones */ | |
1475 report_file_error ("error duplicating socket", list1 (name)); | |
1476 } | |
1477 | |
1478 if (!NILP (buffer)) | |
1479 buffer = Fget_buffer_create (buffer); | |
1480 proc = make_process_internal (name); | |
1481 | |
1482 descriptor_to_process[inch] = proc; | |
1483 | |
1484 set_descriptor_non_blocking (inch); | |
1485 | |
1486 XPROCESS (proc)->pid = Fcons (service, host); | |
1487 XPROCESS (proc)->buffer = buffer; | |
1488 init_process_fds (XPROCESS (proc), inch, outch); | |
1489 XPROCESS (proc)->connected_via_filedesc_p = 0; | |
1490 | |
1491 event_stream_select_process (XPROCESS (proc)); | |
1492 | |
1493 UNGCPRO; | |
1494 return proc; | |
1495 } | |
1496 | |
1497 #endif /* HAVE_SOCKETS */ | |
1498 | |
1499 Lisp_Object | |
1500 canonicalize_host_name (Lisp_Object host) | |
1501 { | |
1502 #ifdef HAVE_SOCKETS | |
1503 /* #### for HAVE_TERM, you probably have to do something else. */ | |
1504 struct sockaddr_in address; | |
1505 | |
1506 if (!get_internet_address (host, &address, ERROR_ME_NOT)) | |
1507 return host; | |
1508 | |
1509 if (address.sin_family == AF_INET) | |
1510 return build_string (inet_ntoa (address.sin_addr)); | |
1511 else | |
1512 /* #### any clue what to do here? */ | |
1513 return host; | |
1514 #else | |
1515 return host; | |
1516 #endif | |
1517 } | |
1518 | |
1519 | |
1520 DEFUN ("set-process-window-size", Fset_process_window_size, | |
1521 Sset_process_window_size, 3, 3, 0 /* | |
1522 Tell PROCESS that it has logical window size HEIGHT and WIDTH. | |
1523 */ ) | |
1524 (proc, height, width) | |
1525 Lisp_Object proc, height, width; | |
1526 { | |
1527 CHECK_PROCESS (proc); | |
1528 CHECK_NATNUM (height); | |
1529 CHECK_NATNUM (width); | |
1530 if (set_window_size (XPROCESS (proc)->infd, XINT (height), XINT (width)) | |
1531 <= 0) | |
1532 return Qnil; | |
1533 else | |
1534 return Qt; | |
1535 } | |
1536 | |
1537 | |
1538 /************************************************************************/ | |
1539 /* Process I/O */ | |
1540 /************************************************************************/ | |
1541 | |
1542 /* (Faccept_process_output is now in event-stream.c) */ | |
1543 | |
1544 /* Some FSFmacs error handlers here. We handle this | |
1545 in call2_trapping_errors(). */ | |
1546 | |
1547 /* Read pending output from the process channel, | |
1548 starting with our buffered-ahead character if we have one. | |
1549 Yield number of characters read. | |
1550 | |
1551 This function reads at most 1024 bytes. | |
1552 If you want to read all available subprocess output, | |
1553 you must call it repeatedly until it returns zero. */ | |
1554 | |
1555 Charcount | |
1556 read_process_output (Lisp_Object proc) | |
1557 { | |
1558 /* This function can GC */ | |
1559 Bytecount nbytes, nchars; | |
1560 #ifdef VMS | |
1561 char *chars; | |
1562 #else | |
1563 Bufbyte chars[1024]; | |
1564 #endif | |
1565 Lisp_Object outstream; | |
1566 struct Lisp_Process *p = XPROCESS (proc); | |
1567 | |
1568 /* If there is a lot of output from the subprocess, the loop in | |
1569 execute_internal_event() might call read_process_output() more | |
1570 than once. If the filter that was executed from one of these | |
1571 calls set the filter to t, we have to stop now. Return -1 rather | |
1572 than 0 so execute_internal_event() doesn't close the process. | |
1573 Really, the loop in execute_internal_event() should check itself | |
1574 for a process-filter change, like in status_notify(); but the | |
1575 struct Lisp_Process is not exported outside of this file. */ | |
1576 if (p->infd < 0) | |
1577 return -1; /* already closed */ | |
1578 | |
1579 if (!NILP (p->filter) && (p->filter_does_read)) | |
1580 { | |
1581 Lisp_Object filter_result; | |
1582 | |
1583 /* Some weird FSFmacs crap here with | |
1584 Vdeactivate_mark and current_buffer->keymap */ | |
1585 running_asynch_code = 1; | |
1586 filter_result = call2_trapping_errors ("Error in process filter", | |
1587 p->filter, proc, Qnil); | |
1588 running_asynch_code = 0; | |
1589 restore_match_data (); | |
1590 CHECK_INT (filter_result); | |
1591 return XINT (filter_result); | |
1592 } | |
1593 | |
1594 #ifdef VMS | |
1595 VMS_PROC_STUFF *vs, *get_vms_process_pointer(); | |
1596 | |
1597 vs = get_vms_process_pointer (XINT (p->pid)); | |
1598 if (vs) | |
1599 { | |
1600 if (!vs->iosb[0]) | |
1601 return(0); /* Really weird if it does this */ | |
1602 if (!(vs->iosb[0] & 1)) | |
1603 return -1; /* I/O error */ | |
1604 } | |
1605 else | |
1606 error ("Could not get VMS process pointer"); | |
1607 chars = vs->inputBuffer; | |
1608 nbytes = clean_vms_buffer (chars, vs->iosb[1]); | |
1609 if (nbytes <= 0) | |
1610 { | |
1611 start_vms_process_read (vs); /* Crank up the next read on the process */ | |
1612 return 1; /* Nothing worth printing, say we got 1 */ | |
1613 } | |
1614 #else /* not VMS */ | |
1615 | |
1616 #if 0 /* FSFmacs */ | |
1617 /* #### equivalent code from FSFmacs. Would need some porting | |
1618 for Windows NT. */ | |
1619 if (proc_buffered_char[channel] < 0) | |
1620 #ifdef WINDOWSNT | |
1621 nchars = read_child_output (channel, chars, sizeof (chars)); | |
1622 #else | |
1623 nchars = read (channel, chars, sizeof chars); | |
1624 #endif | |
1625 else | |
1626 { | |
1627 chars[0] = proc_buffered_char[channel]; | |
1628 proc_buffered_char[channel] = -1; | |
1629 #ifdef WINDOWSNT | |
1630 nchars = read_child_output (channel, chars + 1, sizeof (chars) - 1); | |
1631 #else | |
1632 nchars = read (channel, chars + 1, sizeof chars - 1); | |
1633 #endif | |
1634 if (nchars < 0) | |
1635 nchars = 1; | |
1636 else | |
1637 nchars = nchars + 1; | |
1638 } | |
1639 #endif /* FSFmacs */ | |
1640 | |
1641 nbytes = Lstream_read (XLSTREAM (p->instream), chars, sizeof (chars)); | |
1642 if (nbytes <= 0) return nbytes; | |
1643 #endif /* not VMS */ | |
1644 | |
1645 nchars = bytecount_to_charcount (chars, nbytes); | |
1646 outstream = p->filter; | |
1647 if (!NILP (outstream)) | |
1648 { | |
1649 /* We used to bind inhibit-quit to t here, but | |
1650 call2_trapping_errors() does that for us. */ | |
1651 running_asynch_code = 1; | |
1652 call2_trapping_errors ("Error in process filter", | |
1653 outstream, proc, make_string (chars, nbytes)); | |
1654 running_asynch_code = 0; | |
1655 restore_match_data (); | |
1656 #ifdef VMS | |
1657 start_vms_process_read (vs); | |
1658 #endif | |
1659 return (nchars); | |
1660 } | |
1661 | |
1662 /* If no filter, write into buffer if it isn't dead. */ | |
1663 if (!NILP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer))) | |
1664 { | |
1665 Lisp_Object old_read_only = Qnil; | |
1666 Bufpos old_point; | |
1667 Bufpos old_begv; | |
1668 Bufpos old_zv; | |
1669 int old_zmacs_region_stays = zmacs_region_stays; | |
1670 struct gcpro gcpro1, gcpro2; | |
1671 struct buffer *buf = XBUFFER (p->buffer); | |
1672 | |
1673 GCPRO2 (proc, old_read_only); | |
1674 | |
1675 old_point = BUF_PT (buf); | |
1676 old_begv = BUF_BEGV (buf); | |
1677 old_zv = BUF_ZV (buf); | |
1678 old_read_only = buf->read_only; | |
1679 buf->read_only = Qnil; | |
1680 | |
1681 /* Insert new output into buffer | |
1682 at the current end-of-output marker, | |
1683 thus preserving logical ordering of input and output. */ | |
1684 if (XMARKER (p->mark)->buffer) | |
1685 BUF_SET_PT (buf, | |
1686 bufpos_clip_to_bounds (old_begv, marker_position (p->mark), | |
1687 old_zv)); | |
1688 else | |
1689 BUF_SET_PT (buf, old_zv); | |
1690 | |
1691 /* If the output marker is outside of the visible region, save | |
1692 the restriction and widen. */ | |
1693 if (! (BUF_BEGV (buf) <= BUF_PT (buf) && | |
1694 BUF_PT (buf) <= BUF_ZV (buf))) | |
1695 Fwiden (p->buffer); | |
1696 | |
1697 /* Make sure opoint floats ahead of any new text, just as point | |
1698 would. */ | |
1699 if (BUF_PT (buf) <= old_point) | |
1700 old_point += nchars; | |
1701 | |
1702 /* Insert after old_begv, but before old_zv. */ | |
1703 if (BUF_PT (buf) < old_begv) | |
1704 old_begv += nchars; | |
1705 if (BUF_PT (buf) <= old_zv) | |
1706 old_zv += nchars; | |
1707 | |
1708 #if 0 | |
1709 /* This screws up intial display of the window. jla */ | |
1710 | |
1711 /* Insert before markers in case we are inserting where | |
1712 the buffer's mark is, and the user's next command is Meta-y. */ | |
1713 buffer_insert_raw_string_1 (buf, -1, chars, | |
1714 nbytes, INSDEL_BEFORE_MARKERS); | |
1715 #else | |
1716 buffer_insert_raw_string (buf, chars, nbytes); | |
1717 #endif | |
1718 | |
1719 Fset_marker (p->mark, make_int (BUF_PT (buf)), p->buffer); | |
1720 | |
1721 MARK_MODELINE_CHANGED; | |
1722 | |
1723 /* If the restriction isn't what it should be, set it. */ | |
1724 if (old_begv != BUF_BEGV (buf) || old_zv != BUF_ZV (buf)) | |
1725 Fnarrow_to_region (make_int (old_begv), make_int (old_zv), | |
1726 p->buffer); | |
1727 | |
1728 /* Handling the process output should not deactivate the mark. */ | |
1729 zmacs_region_stays = old_zmacs_region_stays; | |
1730 buf->read_only = old_read_only; | |
1731 BUF_SET_PT (buf, old_point); | |
1732 | |
1733 UNGCPRO; | |
1734 } | |
1735 #ifdef VMS | |
1736 start_vms_process_read (vs); | |
1737 #endif | |
1738 return (nchars); | |
1739 } | |
1740 | |
1741 /* Sending data to subprocess */ | |
1742 | |
1743 static JMP_BUF send_process_frame; | |
1744 | |
1745 static SIGTYPE | |
1746 send_process_trap (int signum) | |
1747 { | |
1748 EMACS_REESTABLISH_SIGNAL (signum, send_process_trap); | |
1749 EMACS_UNBLOCK_SIGNAL (signum); | |
1750 LONGJMP (send_process_frame, 1); | |
1751 } | |
1752 | |
1753 /* send some data to process PROC. If NONRELOCATABLE is non-NULL, it | |
1754 specifies the address of the data. Otherwise, the data comes from the | |
1755 object RELOCATABLE (either a string or a buffer). START and LEN | |
1756 specify the offset and length of the data to send. | |
1757 | |
1758 Note that START and LEN are in Bufpos's if RELOCATABLE is a buffer, | |
1759 and in Bytecounts otherwise. */ | |
1760 | |
1761 static void | |
1762 send_process (volatile Lisp_Object proc, | |
1763 Lisp_Object relocatable, CONST Bufbyte *nonrelocatable, | |
1764 int start, int len) | |
1765 { | |
1766 /* This function can GC */ | |
1767 /* Use volatile to protect variables from being clobbered by longjmp. */ | |
1768 struct gcpro gcpro1, gcpro2; | |
1769 SIGTYPE (*old_sigpipe) (int) = 0; | |
1770 Lisp_Object lstream = Qnil; | |
1771 volatile struct Lisp_Process *p = XPROCESS (proc); | |
1772 #if defined (NO_UNION_TYPE) /* || !defined (__GNUC__) GCC bug only??? */ | |
1773 /* #### ugh! There must be a better solution. */ | |
1774 Lisp_Object defeat_volatile_kludge = (Lisp_Object) proc; | |
1775 #else | |
1776 Lisp_Object defeat_volatile_kludge = proc; | |
1777 #endif | |
1778 | |
1779 #ifdef VMS | |
1780 VMS_PROC_STUFF *vs, *get_vms_process_pointer (int); | |
1781 #endif /* VMS */ | |
1782 | |
1783 GCPRO2 (defeat_volatile_kludge, lstream); | |
1784 | |
1785 if (p->outfd < 0) | |
1786 signal_simple_error ("Process not open for writing", proc); | |
1787 | |
1788 #ifdef VMS | |
1789 vs = get_vms_process_pointer (XINT (p->pid)); | |
1790 if (vs == 0) | |
1791 error ("Could not find this process: %x", | |
1792 XINT (p->pid)); | |
1793 else if (write_to_vms_process (vs, buf, len)) | |
1794 ; | |
1795 #else | |
1796 | |
1797 if (nonrelocatable) | |
1798 lstream = | |
1799 make_fixed_buffer_input_stream (nonrelocatable + start, len); | |
1800 else if (GC_BUFFERP (relocatable)) | |
1801 lstream = make_lisp_buffer_input_stream (XBUFFER (relocatable), | |
1802 start, start + len, 0); | |
1803 else | |
1804 lstream = make_lisp_string_input_stream (relocatable, start, len); | |
1805 | |
1806 if (!SETJMP (send_process_frame)) | |
1807 { | |
1808 /* use a reasonable-sized buffer (somewhere around the size of the | |
1809 stream buffer) so as to avoid inundating the stream with blocked | |
1810 data. */ | |
1811 Bufbyte chunkbuf[512]; | |
1812 Bytecount chunklen; | |
1813 | |
1814 while (1) | |
1815 { | |
1816 int writeret; | |
1817 | |
1818 chunklen = Lstream_read (XLSTREAM (lstream), chunkbuf, 512); | |
1819 if (chunklen <= 0) | |
1820 break; /* perhaps should abort() if < 0? | |
1821 This should never happen. */ | |
1822 old_sigpipe = | |
1823 (SIGTYPE (*) (int)) signal (SIGPIPE, send_process_trap); | |
1824 /* Lstream_write() will never successfully write less than | |
1825 the amount sent in. In the worst case, it just buffers | |
1826 the unwritten data. */ | |
1827 writeret = Lstream_write (XLSTREAM (p->outstream), chunkbuf, | |
1828 chunklen); | |
1829 signal (SIGPIPE, old_sigpipe); | |
1830 if (writeret < 0) | |
1831 /* This is a real error. Blocking errors are handled | |
1832 specially inside of the filedesc stream. */ | |
1833 report_file_error ("writing to process", | |
1834 list1 (proc)); | |
1835 while (filedesc_stream_was_blocked (XLSTREAM (p->filedesc_stream))) | |
1836 { | |
1837 /* Buffer is full. Wait, accepting input; | |
1838 that may allow the program | |
1839 to finish doing output and read more. */ | |
1840 Faccept_process_output (Qnil, make_int (1), Qnil); | |
1841 old_sigpipe = | |
1842 (SIGTYPE (*) (int)) signal (SIGPIPE, send_process_trap); | |
1843 Lstream_flush (XLSTREAM (p->filedesc_stream)); | |
1844 signal (SIGPIPE, old_sigpipe); | |
1845 } | |
1846 } | |
1847 } | |
1848 #endif /* !VMS */ | |
1849 else | |
1850 { /* We got here from a longjmp() from the SIGPIPE handler */ | |
1851 signal (SIGPIPE, old_sigpipe); | |
1852 p->status_symbol = Qexit; | |
1853 p->exit_code = 256; /* #### SIGPIPE ??? */ | |
1854 p->core_dumped = 0; | |
1855 p->tick++; | |
1856 process_tick++; | |
1857 deactivate_process (proc); | |
1858 #ifdef VMS | |
1859 error ("Error writing to process %s; closed it", | |
1860 string_data (XSTRING (p->name))); | |
1861 #else | |
1862 error ("SIGPIPE raised on process %s; closed it", | |
1863 string_data (XSTRING (p->name))); | |
1864 #endif | |
1865 } | |
1866 Lstream_flush (XLSTREAM (p->outstream)); | |
1867 UNGCPRO; | |
1868 } | |
1869 | |
1870 DEFUN ("process-tty-name", Fprocess_tty_name, Sprocess_tty_name, 1, 1, 0 /* | |
1871 Return the name of the terminal PROCESS uses, or nil if none. | |
1872 This is the terminal that the process itself reads and writes on, | |
1873 not the name of the pty that Emacs uses to talk with that terminal. | |
1874 */ ) | |
1875 (proc) | |
1876 Lisp_Object proc; | |
1877 { | |
1878 CHECK_PROCESS (proc); | |
1879 return XPROCESS (proc)->tty_name; | |
1880 } | |
1881 | |
1882 DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer, | |
1883 2, 2, 0 /* | |
1884 Set buffer associated with PROCESS to BUFFER (a buffer, or nil). | |
1885 */ ) | |
1886 (proc, buffer) | |
1887 Lisp_Object proc, buffer; | |
1888 { | |
1889 CHECK_PROCESS (proc); | |
1890 if (!NILP (buffer)) | |
1891 CHECK_BUFFER (buffer); | |
1892 XPROCESS (proc)->buffer = buffer; | |
1893 return buffer; | |
1894 } | |
1895 | |
1896 DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer, | |
1897 1, 1, 0 /* | |
1898 Return the buffer PROCESS is associated with. | |
1899 Output from PROCESS is inserted in this buffer | |
1900 unless PROCESS has a filter. | |
1901 */ ) | |
1902 (proc) | |
1903 Lisp_Object proc; | |
1904 { | |
1905 CHECK_PROCESS (proc); | |
1906 return XPROCESS (proc)->buffer; | |
1907 } | |
1908 | |
1909 DEFUN ("process-mark", Fprocess_mark, Sprocess_mark, | |
1910 1, 1, 0 /* | |
1911 Return the marker for the end of the last output from PROCESS. | |
1912 */ ) | |
1913 (proc) | |
1914 Lisp_Object proc; | |
1915 { | |
1916 CHECK_PROCESS (proc); | |
1917 #ifdef ENERGIZE | |
1918 if (EQ (proc, Venergize_process)) /* per buffer rather than per process */ | |
1919 return Fenergize_user_input_buffer_mark (Qnil); /* ## current_buffer ok? */ | |
1920 #endif | |
1921 return XPROCESS (proc)->mark; | |
1922 } | |
1923 | |
1924 void | |
1925 set_process_filter (Lisp_Object proc, Lisp_Object filter, int filter_does_read) | |
1926 { | |
1927 CHECK_PROCESS (proc); | |
1928 if (PROCESS_LIVE_P (proc)) | |
1929 if (EQ (filter, Qt)) | |
1930 event_stream_unselect_process (XPROCESS (proc)); | |
1931 else | |
1932 event_stream_select_process (XPROCESS (proc)); | |
1933 | |
1934 XPROCESS (proc)->filter = filter; | |
1935 XPROCESS (proc)->filter_does_read = filter_does_read; | |
1936 } | |
1937 | |
1938 DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter, | |
1939 2, 2, 0 /* | |
1940 Give PROCESS the filter function FILTER; nil means no filter. | |
1941 t means stop accepting output from the process. | |
1942 When a process has a filter, each time it does output | |
1943 the entire string of output is passed to the filter. | |
1944 The filter gets two arguments: the process and the string of output. | |
1945 If the process has a filter, its buffer is not used for output. | |
1946 */ ) | |
1947 (proc, filter) | |
1948 Lisp_Object proc, filter; | |
1949 { | |
1950 set_process_filter (proc, filter, 0); | |
1951 return filter; | |
1952 } | |
1953 | |
1954 DEFUN ("process-filter", Fprocess_filter, Sprocess_filter, | |
1955 1, 1, 0 /* | |
1956 Return the filter function of PROCESS; nil if none. | |
1957 See `set-process-filter' for more info on filter functions. | |
1958 */ ) | |
1959 (proc) | |
1960 Lisp_Object proc; | |
1961 { | |
1962 CHECK_PROCESS (proc); | |
1963 return XPROCESS (proc)->filter; | |
1964 } | |
1965 | |
1966 DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region, | |
1967 3, 3, 0 /* | |
1968 Send current contents of region as input to PROCESS. | |
1969 PROCESS may be a process name or an actual process. | |
1970 Called from program, takes three arguments, PROCESS, START and END. | |
1971 If the region is more than 500 or so characters long, | |
1972 it is sent in several bunches. This may happen even for shorter regions. | |
1973 Output from processes can arrive in between bunches. | |
1974 */ ) | |
1975 (process, start, end) | |
1976 Lisp_Object process, start, end; | |
1977 { | |
1978 /* This function can GC */ | |
1979 Lisp_Object proc = get_process (process); | |
1980 Bufpos st, en; | |
1981 | |
1982 get_buffer_range_char (current_buffer, start, end, &st, &en, 0); | |
1983 | |
1984 send_process (proc, Fcurrent_buffer (), 0, | |
1985 st, en - st); | |
1986 return (Qnil); | |
1987 } | |
1988 | |
1989 DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string, | |
1990 2, 4, 0 /* | |
1991 Send PROCESS the contents of STRING as input. | |
1992 PROCESS may be a process name or an actual process. | |
1993 Optional arguments FROM and TO specify part of STRING, see `substring'. | |
1994 If STRING is more than 500 or so characters long, | |
1995 it is sent in several bunches. This may happen even for shorter strings. | |
1996 Output from processes can arrive in between bunches. | |
1997 */ ) | |
1998 (process, string, from, to) | |
1999 Lisp_Object process, string, from, to; | |
2000 { | |
2001 /* This function can GC */ | |
2002 Lisp_Object proc; | |
2003 Bytecount len; | |
2004 Bytecount bfr, bto; | |
2005 | |
2006 proc = get_process (process); | |
2007 CHECK_STRING (string); | |
2008 get_string_range_byte (string, from, to, &bfr, &bto, | |
2009 GB_HISTORICAL_STRING_BEHAVIOR); | |
2010 len = bto - bfr; | |
2011 | |
2012 send_process (proc, string, 0, bfr, len); | |
2013 return (Qnil); | |
2014 } | |
2015 | |
2016 | |
2017 /************************************************************************/ | |
2018 /* process status */ | |
2019 /************************************************************************/ | |
2020 | |
2021 /* Some FSFmacs error handlers here. We handle this | |
2022 in call2_trapping_errors(). */ | |
2023 | |
2024 static Lisp_Object | |
2025 exec_sentinel_unwind (Lisp_Object datum) | |
2026 { | |
2027 struct Lisp_Cons *d = XCONS (datum); | |
2028 XPROCESS (d->car)->sentinel = d->cdr; | |
2029 free_cons (d); | |
2030 return Qnil; | |
2031 } | |
2032 | |
2033 static void | |
2034 exec_sentinel (Lisp_Object proc, Lisp_Object reason) | |
2035 { | |
2036 /* This function can GC */ | |
2037 Lisp_Object sentinel; | |
2038 struct Lisp_Process *p = XPROCESS (proc); | |
2039 int speccount = specpdl_depth (); | |
2040 | |
2041 sentinel = p->sentinel; | |
2042 if (NILP (sentinel)) | |
2043 return; | |
2044 | |
2045 /* Some weird FSFmacs crap here with | |
2046 Vdeactivate_mark and current_buffer->keymap */ | |
2047 | |
2048 /* Zilch the sentinel while it's running, to avoid recursive invocations; | |
2049 assure that it gets restored no matter how the sentinel exits. */ | |
2050 p->sentinel = Qnil; | |
2051 record_unwind_protect (exec_sentinel_unwind, noseeum_cons (proc, sentinel)); | |
2052 /* We used to bind inhibit-quit to t here, but call2_trapping_errors() | |
2053 does that for us. */ | |
2054 running_asynch_code = 1; | |
2055 call2_trapping_errors ("Error in process sentinel", | |
2056 sentinel, proc, reason); | |
2057 running_asynch_code = 0; | |
2058 restore_match_data (); | |
2059 unbind_to (speccount, Qnil); | |
2060 } | |
2061 | |
2062 DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel, | |
2063 2, 2, 0 /* | |
2064 Give PROCESS the sentinel SENTINEL; nil for none. | |
2065 The sentinel is called as a function when the process changes state. | |
2066 It gets two arguments: the process, and a string describing the change. | |
2067 */ ) | |
2068 (proc, sentinel) | |
2069 Lisp_Object proc, sentinel; | |
2070 { | |
2071 CHECK_PROCESS (proc); | |
2072 XPROCESS (proc)->sentinel = sentinel; | |
2073 return sentinel; | |
2074 } | |
2075 | |
2076 DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel, | |
2077 1, 1, 0 /* | |
2078 Return the sentinel of PROCESS; nil if none. | |
2079 See `set-process-sentinel' for more info on sentinels. | |
2080 */ ) | |
2081 (proc) | |
2082 Lisp_Object proc; | |
2083 { | |
2084 CHECK_PROCESS (proc); | |
2085 return XPROCESS (proc)->sentinel; | |
2086 } | |
2087 | |
2088 | |
2089 CONST char * | |
2090 signal_name (int signum) | |
2091 { | |
2092 if (signum >= 0 && signum < NSIG) | |
2093 #ifndef VMS | |
2094 return ((CONST char *) sys_siglist[signum]); | |
2095 #else | |
2096 return ((CONST char *) sys_errlist[signum]); | |
2097 #endif | |
2098 return ((CONST char *) GETTEXT ("unknown signal")); | |
2099 } | |
2100 | |
2101 /* Compute the Lisp form of the process status from | |
2102 the numeric status that was returned by `wait'. */ | |
2103 | |
2104 static void | |
2105 update_status_from_wait_code (struct Lisp_Process *p, WAITTYPE *w_fmh) | |
2106 { | |
2107 /* C compiler lossage when attempting to pass w directly */ | |
2108 WAITTYPE w = *w_fmh; | |
2109 | |
2110 if (WIFSTOPPED (w)) | |
2111 { | |
2112 p->status_symbol = Qstop; | |
2113 p->exit_code = WSTOPSIG (w); | |
2114 p->core_dumped = 0; | |
2115 } | |
2116 else if (WIFEXITED (w)) | |
2117 { | |
2118 p->status_symbol = Qexit; | |
2119 p->exit_code = WRETCODE (w); | |
2120 p->core_dumped = ((WCOREDUMP (w)) ? 1 : 0); | |
2121 } | |
2122 else if (WIFSIGNALED (w)) | |
2123 { | |
2124 p->status_symbol = Qsignal; | |
2125 p->exit_code = (int) WTERMSIG (w); | |
2126 p->core_dumped = ((WCOREDUMP (w)) ? 1 : 0); | |
2127 } | |
2128 else | |
2129 { | |
2130 p->status_symbol = Qrun; | |
2131 p->exit_code = 0; | |
2132 } | |
2133 } | |
2134 | |
2135 void | |
2136 update_process_status (Lisp_Object p, | |
2137 Lisp_Object status_symbol, | |
2138 int exit_code, | |
2139 int core_dumped) | |
2140 { | |
2141 XPROCESS (p)->tick++; | |
2142 process_tick++; | |
2143 XPROCESS (p)->status_symbol = status_symbol; | |
2144 XPROCESS (p)->exit_code = exit_code; | |
2145 XPROCESS (p)->core_dumped = core_dumped; | |
2146 } | |
2147 | |
2148 #ifdef SIGCHLD | |
2149 | |
2150 #define MAX_EXITED_PROCESSES 1000 | |
2151 static volatile pid_t exited_processes[MAX_EXITED_PROCESSES]; | |
2152 static volatile WAITTYPE exited_processes_status[MAX_EXITED_PROCESSES]; | |
2153 static volatile int exited_processes_index; | |
2154 | |
2155 static volatile int sigchld_happened; | |
2156 | |
2157 /* For any processes that have changed status and are recorded | |
2158 and such, update the corresponding struct Lisp_Process. | |
2159 We separate this from record_exited_processes() so that | |
2160 we never have to call this function from within a signal | |
2161 handler. We block SIGCHLD in case record_exited_processes() | |
2162 is called from a signal handler. */ | |
2163 | |
2164 static void | |
2165 reap_exited_processes (void) | |
2166 { | |
2167 int i; | |
2168 struct Lisp_Process *p; | |
2169 | |
2170 EMACS_BLOCK_SIGNAL (SIGCHLD); | |
2171 for (i = 0; i < exited_processes_index; i++) | |
2172 { | |
2173 int pid = exited_processes[i]; | |
2174 WAITTYPE w = exited_processes_status[i]; | |
2175 | |
2176 /* Find the process that signaled us, and record its status. */ | |
2177 | |
2178 p = 0; | |
2179 { | |
2180 Lisp_Object tail; | |
2181 LIST_LOOP (tail, Vprocess_list) | |
2182 { | |
2183 Lisp_Object proc = XCAR (tail); | |
2184 p = XPROCESS (proc); | |
2185 if (INTP (p->pid) && XINT (p->pid) == pid) | |
2186 break; | |
2187 p = 0; | |
2188 } | |
2189 } | |
2190 | |
2191 if (p) | |
2192 { | |
2193 /* Change the status of the process that was found. */ | |
2194 p->tick++; | |
2195 process_tick++; | |
2196 update_status_from_wait_code (p, &w); | |
2197 | |
2198 /* If process has terminated, stop waiting for its output. */ | |
2199 if (WIFSIGNALED (w) || WIFEXITED (w)) | |
2200 { | |
2201 if (p->infd >= 0) | |
2202 { | |
2203 /* We can't just call event_stream->unselect_process_cb (p) | |
2204 here, because that calls XtRemoveInput, which is not | |
2205 necessarily reentrant, so we can't call this at interrupt | |
2206 level. | |
2207 */ | |
2208 } | |
2209 } | |
2210 } | |
2211 else | |
2212 { | |
2213 /* There was no asynchronous process found for that id. Check | |
2214 if we have a synchronous process. Only set sync process status | |
2215 if there is one, so we work OK with the waitpid() call in | |
2216 wait_for_termination(). */ | |
2217 if (synch_process_alive != 0) | |
2218 { /* Set the global sync process status variables. */ | |
2219 synch_process_alive = 0; | |
2220 | |
2221 /* Report the status of the synchronous process. */ | |
2222 if (WIFEXITED (w)) | |
2223 synch_process_retcode = WRETCODE (w); | |
2224 else if (WIFSIGNALED (w)) | |
2225 synch_process_death = signal_name (WTERMSIG (w)); | |
2226 } | |
2227 } | |
2228 } | |
2229 | |
2230 exited_processes_index = 0; | |
2231 | |
2232 EMACS_UNBLOCK_SIGNAL (SIGCHLD); | |
2233 } | |
2234 | |
2235 /* On receipt of a signal that a child status has changed, | |
2236 loop asking about children with changed statuses until | |
2237 the system says there are no more. All we do is record | |
2238 the processes and wait status. | |
2239 | |
2240 This function could be called from within the SIGCHLD | |
2241 handler, so it must be completely reentrant. When | |
2242 not called from a SIGCHLD handler, BLOCK_SIGCHLD should | |
2243 be non-zero so that SIGCHLD is blocked while this | |
2244 function is running. (This is necessary so avoid | |
2245 race conditions with the SIGCHLD_HAPPENED flag). */ | |
2246 | |
2247 static void | |
2248 record_exited_processes (int block_sigchld) | |
2249 { | |
2250 if (block_sigchld) | |
2251 EMACS_BLOCK_SIGNAL (SIGCHLD); | |
2252 | |
2253 while (sigchld_happened) | |
2254 { | |
2255 int pid; | |
2256 WAITTYPE w; | |
2257 | |
2258 /* Keep trying to get a status until we get a definitive result. */ | |
2259 do | |
2260 { | |
2261 errno = 0; | |
2262 #ifdef WNOHANG | |
2263 # ifndef WUNTRACED | |
2264 # define WUNTRACED 0 | |
2265 # endif /* not WUNTRACED */ | |
2266 # ifdef HAVE_WAITPID | |
2267 pid = waitpid ((pid_t) -1, &w, WNOHANG | WUNTRACED); | |
2268 # else | |
2269 pid = wait3 (&w, WNOHANG | WUNTRACED, 0); | |
2270 # endif | |
2271 #else /* not WNOHANG */ | |
2272 pid = wait (&w); | |
2273 #endif /* not WNOHANG */ | |
2274 } | |
2275 while (pid <= 0 && errno == EINTR); | |
2276 | |
2277 if (pid <= 0) | |
2278 break; | |
2279 | |
2280 if (exited_processes_index < MAX_EXITED_PROCESSES) | |
2281 { | |
2282 exited_processes[exited_processes_index] = pid; | |
2283 exited_processes_status[exited_processes_index] = w; | |
2284 exited_processes_index++; | |
2285 } | |
2286 | |
2287 /* On systems with WNOHANG, we just ignore the number | |
2288 of times that SIGCHLD was signalled, and keep looping | |
2289 until there are no more processes to wait on. If we | |
2290 don't have WNOHANG, we have to rely on the count in | |
2291 SIGCHLD_HAPPENED. */ | |
2292 #ifndef WNOHANG | |
2293 sigchld_happened--; | |
2294 #endif /* not WNOHANG */ | |
2295 } | |
2296 | |
2297 sigchld_happened = 0; | |
2298 | |
2299 if (block_sigchld) | |
2300 EMACS_UNBLOCK_SIGNAL (SIGCHLD); | |
2301 } | |
2302 | |
2303 /** USG WARNING: Although it is not obvious from the documentation | |
2304 in signal(2), on a USG system the SIGCLD handler MUST NOT call | |
2305 signal() before executing at least one wait(), otherwise the handler | |
2306 will be called again, resulting in an infinite loop. The relevant | |
2307 portion of the documentation reads "SIGCLD signals will be queued | |
2308 and the signal-catching function will be continually reentered until | |
2309 the queue is empty". Invoking signal() causes the kernel to reexamine | |
2310 the SIGCLD queue. Fred Fish, UniSoft Systems Inc. | |
2311 | |
2312 (Note that now this only applies in SYS V Release 2 and before. | |
2313 On SYS V Release 3, we use sigset() to set the signal handler for | |
2314 the first time, and so we don't have to reestablish the signal handler | |
2315 in the handler below. On SYS V Release 4, we don't get this weirdo | |
2316 behavior when we use sigaction(), which we do use.) */ | |
2317 | |
2318 static SIGTYPE | |
2319 sigchld_handler (int signo) | |
2320 { | |
2321 #ifdef OBNOXIOUS_SYSV_SIGCLD_BEHAVIOR | |
2322 int old_errno = errno; | |
2323 | |
2324 sigchld_happened++; | |
2325 record_exited_processes (0); | |
2326 errno = old_errno; | |
2327 #else | |
2328 sigchld_happened++; | |
2329 #endif | |
2330 signal_fake_event (); | |
2331 /* WARNING - must come after wait3() for USG systems */ | |
2332 EMACS_REESTABLISH_SIGNAL (signo, sigchld_handler); | |
2333 SIGRETURN; | |
2334 } | |
2335 | |
2336 #endif /* SIGCHLD */ | |
2337 | |
2338 /* Return a string describing a process status list. */ | |
2339 | |
2340 static Lisp_Object | |
2341 status_message (struct Lisp_Process *p) | |
2342 { | |
2343 Lisp_Object symbol = p->status_symbol; | |
2344 int code = p->exit_code; | |
2345 int coredump = p->core_dumped; | |
2346 Lisp_Object string, string2; | |
2347 | |
2348 if (EQ (symbol, Qsignal) || EQ (symbol, Qstop)) | |
2349 { | |
2350 string = build_string (signal_name (code)); | |
2351 if (coredump) | |
2352 string2 = build_translated_string (" (core dumped)\n"); | |
2353 else | |
2354 string2 = build_string ("\n"); | |
2355 set_string_char (XSTRING (string), 0, | |
2356 DOWNCASE (current_buffer, | |
2357 string_char (XSTRING (string), 0))); | |
2358 return concat2 (string, string2); | |
2359 } | |
2360 else if (EQ (symbol, Qexit)) | |
2361 { | |
2362 if (code == 0) | |
2363 return build_translated_string ("finished\n"); | |
2364 string = Fnumber_to_string (make_int (code)); | |
2365 if (coredump) | |
2366 string2 = build_translated_string (" (core dumped)\n"); | |
2367 else | |
2368 string2 = build_string ("\n"); | |
2369 return concat2 (build_translated_string ("exited abnormally with code "), | |
2370 concat2 (string, string2)); | |
2371 } | |
2372 else | |
2373 return Fcopy_sequence (Fsymbol_name (symbol)); | |
2374 } | |
2375 | |
2376 /* Tell status_notify() to check for terminated processes. We do this | |
2377 because on some systems we sometimes miss SIGCHLD calls. (Not sure | |
2378 why.) */ | |
2379 | |
2380 void | |
2381 kick_status_notify (void) | |
2382 { | |
2383 process_tick++; | |
2384 } | |
2385 | |
2386 /* Report all recent events of a change in process status | |
2387 (either run the sentinel or output a message). | |
2388 This is done while Emacs is waiting for keyboard input. */ | |
2389 | |
2390 void | |
2391 status_notify (void) | |
2392 { | |
2393 /* This function can GC */ | |
2394 Lisp_Object tail = Qnil; | |
2395 Lisp_Object symbol = Qnil; | |
2396 Lisp_Object msg = Qnil; | |
2397 struct gcpro gcpro1, gcpro2, gcpro3; | |
2398 /* process_tick is volatile, so we have to remember it now. | |
2399 Otherwise, we get a race condition is SIGCHLD happens during | |
2400 this function. | |
2401 | |
2402 (Actually, this is not the case anymore. The code to | |
2403 update the process structures has been moved out of the | |
2404 SIGCHLD handler. But for the moment I'm leaving this | |
2405 stuff in -- it can't hurt.) */ | |
2406 int temp_process_tick; | |
2407 | |
2408 #ifdef SIGCHLD | |
2409 #ifndef OBNOXIOUS_SYSV_SIGCLD_BEHAVIOR | |
2410 record_exited_processes (1); | |
2411 #endif | |
2412 reap_exited_processes (); | |
2413 #endif | |
2414 | |
2415 temp_process_tick = process_tick; | |
2416 | |
2417 if (update_tick == temp_process_tick) | |
2418 return; | |
2419 | |
2420 /* We need to gcpro tail; if read_process_output calls a filter | |
2421 which deletes a process and removes the cons to which tail points | |
2422 from Vprocess_alist, and then causes a GC, tail is an unprotected | |
2423 reference. */ | |
2424 GCPRO3 (tail, symbol, msg); | |
2425 | |
2426 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail)) | |
2427 { | |
2428 Lisp_Object proc = XCAR (tail); | |
2429 struct Lisp_Process *p = XPROCESS (proc); | |
2430 /* p->tick is also volatile. Same thing as above applies. */ | |
2431 int this_process_tick; | |
2432 | |
2433 #ifdef HAVE_WAITPID | |
2434 /* #### extra check for terminated processes, in case a SIGCHLD | |
2435 got missed (this seems to happen sometimes, I'm not sure why). | |
2436 */ | |
2437 { | |
2438 WAITTYPE w; | |
2439 #ifdef SIGCHLD | |
2440 EMACS_BLOCK_SIGNAL (SIGCHLD); | |
2441 #endif | |
2442 if (INTP (p->pid) && | |
2443 waitpid (XINT (p->pid), &w, WNOHANG) == XINT (p->pid)) | |
2444 { | |
2445 p->tick++; | |
2446 update_status_from_wait_code (p, &w); | |
2447 } | |
2448 #ifdef SIGCHLD | |
2449 EMACS_UNBLOCK_SIGNAL (SIGCHLD); | |
2450 #endif | |
2451 } | |
2452 #endif | |
2453 this_process_tick = p->tick; | |
2454 if (this_process_tick != p->update_tick) | |
2455 { | |
2456 p->update_tick = this_process_tick; | |
2457 | |
2458 /* If process is still active, read any output that remains. */ | |
2459 while (!EQ (p->filter, Qt) | |
2460 && read_process_output (proc) > 0) | |
2461 ; | |
2462 | |
2463 /* Get the text to use for the message. */ | |
2464 msg = status_message (p); | |
2465 | |
2466 /* If process is terminated, deactivate it or delete it. */ | |
2467 symbol = p->status_symbol; | |
2468 | |
2469 if (EQ (symbol, Qsignal) | |
2470 || EQ (symbol, Qexit)) | |
2471 { | |
2472 if (delete_exited_processes) | |
2473 remove_process (proc); | |
2474 else | |
2475 deactivate_process (proc); | |
2476 } | |
2477 | |
2478 /* Now output the message suitably. */ | |
2479 if (!NILP (p->sentinel)) | |
2480 exec_sentinel (proc, msg); | |
2481 /* Don't bother with a message in the buffer | |
2482 when a process becomes runnable. */ | |
2483 else if (!EQ (symbol, Qrun) && !NILP (p->buffer)) | |
2484 { | |
2485 Lisp_Object old_read_only = Qnil; | |
2486 Lisp_Object old = Fcurrent_buffer (); | |
2487 Bufpos opoint; | |
2488 struct gcpro ngcpro1, ngcpro2; | |
2489 | |
2490 /* Avoid error if buffer is deleted | |
2491 (probably that's why the process is dead, too) */ | |
2492 if (!BUFFER_LIVE_P (XBUFFER (p->buffer))) | |
2493 continue; | |
2494 | |
2495 NGCPRO2 (old, old_read_only); | |
2496 Fset_buffer (p->buffer); | |
2497 opoint = BUF_PT (current_buffer); | |
2498 /* Insert new output into buffer | |
2499 at the current end-of-output marker, | |
2500 thus preserving logical ordering of input and output. */ | |
2501 if (XMARKER (p->mark)->buffer) | |
2502 BUF_SET_PT (current_buffer, marker_position (p->mark)); | |
2503 else | |
2504 BUF_SET_PT (current_buffer, BUF_ZV (current_buffer)); | |
2505 if (BUF_PT (current_buffer) <= opoint) | |
2506 opoint += (string_char_length (XSTRING (msg)) | |
2507 + string_char_length (XSTRING (p->name)) | |
2508 + 10); | |
2509 | |
2510 old_read_only = current_buffer->read_only; | |
2511 current_buffer->read_only = Qnil; | |
2512 buffer_insert_c_string (current_buffer, "\nProcess "); | |
2513 Finsert (1, &p->name); | |
2514 buffer_insert_c_string (current_buffer, " "); | |
2515 Finsert (1, &msg); | |
2516 current_buffer->read_only = old_read_only; | |
2517 Fset_marker (p->mark, make_int (BUF_PT (current_buffer)), | |
2518 p->buffer); | |
2519 | |
2520 BUF_SET_PT (current_buffer, opoint); | |
2521 Fset_buffer (old); | |
2522 NUNGCPRO; | |
2523 } | |
2524 } | |
2525 } /* end for */ | |
2526 | |
2527 /* in case buffers use %s in modeline-format */ | |
2528 MARK_MODELINE_CHANGED; | |
2529 redisplay (); | |
2530 | |
2531 update_tick = temp_process_tick; | |
2532 | |
2533 UNGCPRO; | |
2534 } | |
2535 | |
2536 DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0 /* | |
2537 Return the status of PROCESS. | |
2538 This is a symbol, one of these: | |
2539 | |
2540 run -- for a process that is running. | |
2541 stop -- for a process stopped but continuable. | |
2542 exit -- for a process that has exited. | |
2543 signal -- for a process that has got a fatal signal. | |
2544 open -- for a network stream connection that is open. | |
2545 closed -- for a network stream connection that is closed. | |
2546 nil -- if arg is a process name and no such process exists. | |
2547 PROCESS may be a process, a buffer, the name of a process or buffer, or | |
2548 nil, indicating the current buffer's process. | |
2549 */ ) | |
2550 (proc) | |
2551 Lisp_Object proc; | |
2552 { | |
2553 Lisp_Object status; | |
2554 | |
2555 if (STRINGP (proc)) | |
2556 proc = Fget_process (proc); | |
2557 else | |
2558 proc = get_process (proc); | |
2559 | |
2560 if (NILP (proc)) | |
2561 return proc; | |
2562 | |
2563 status = XPROCESS (proc)->status_symbol; | |
2564 if (network_connection_p (proc)) | |
2565 { | |
2566 if (EQ (status, Qrun)) | |
2567 status = Qopen; | |
2568 else if (EQ (status, Qexit)) | |
2569 status = Qclosed; | |
2570 } | |
2571 return (status); | |
2572 } | |
2573 | |
2574 DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status, | |
2575 1, 1, 0 /* | |
2576 Return the exit status of PROCESS or the signal number that killed it. | |
2577 If PROCESS has not yet exited or died, return 0. | |
2578 */ ) | |
2579 (proc) | |
2580 Lisp_Object proc; | |
2581 { | |
2582 CHECK_PROCESS (proc); | |
2583 return (make_int (XPROCESS (proc)->exit_code)); | |
2584 } | |
2585 | |
2586 | |
2587 #ifdef SIGNALS_VIA_CHARACTERS | |
2588 /* Get signal character to send to process if SIGNALS_VIA_CHARACTERS */ | |
2589 | |
2590 static int | |
2591 process_signal_char (int tty_fd, int signo) | |
2592 { | |
2593 /* If it's not a tty, pray that these default values work */ | |
2594 if (!isatty(tty_fd)) { | |
2595 #define CNTL(ch) (037 & (ch)) | |
2596 switch (signo) | |
2597 { | |
2598 case SIGINT: return CNTL('C'); | |
2599 case SIGQUIT: return CNTL('\\'); | |
2600 #ifdef SIGTSTP | |
2601 case SIGTSTP: return CNTL('Z'); | |
2602 #endif | |
2603 } | |
2604 } | |
2605 | |
2606 #ifdef HAVE_TERMIOS | |
2607 /* TERMIOS is the latest and bestest, and seems most likely to work. | |
2608 If the system has it, use it. */ | |
2609 { | |
2610 struct termios t; | |
2611 tcgetattr (tty_fd, &t); | |
2612 switch (signo) | |
2613 { | |
2614 case SIGINT: return t.c_cc[VINTR]; | |
2615 case SIGQUIT: return t.c_cc[VQUIT]; | |
2616 # if defined (VSWTCH) && !defined (PREFER_VSUSP) | |
2617 case SIGTSTP: return t.c_cc[VSWTCH]; | |
2618 # else | |
2619 case SIGTSTP: return t.c_cc[VSUSP]; | |
2620 # endif | |
2621 } | |
2622 } | |
2623 | |
2624 # elif defined (TIOCGLTC) && defined (TIOCGETC) /* not HAVE_TERMIOS */ | |
2625 { | |
2626 /* On Berkeley descendants, the following IOCTL's retrieve the | |
2627 current control characters. */ | |
2628 struct tchars c; | |
2629 struct ltchars lc; | |
2630 switch (signo) | |
2631 { | |
2632 case SIGINT: ioctl (tty_fd, TIOCGETC, &c); return c.t_intrc; | |
2633 case SIGQUIT: ioctl (tty_fd, TIOCGETC, &c); return c.t_quitc; | |
2634 # ifdef SIGTSTP | |
2635 case SIGTSTP: ioctl (tty_fd, TIOCGLTC, &lc); return lc.t_suspc; | |
2636 # endif /* SIGTSTP */ | |
2637 } | |
2638 } | |
2639 | |
2640 # elif defined (TCGETA) /* ! defined (TIOCGLTC) && defined (TIOCGETC) */ | |
2641 { | |
2642 /* On SYSV descendants, the TCGETA ioctl retrieves the current | |
2643 control characters. */ | |
2644 struct termio t; | |
2645 ioctl (tty_fd, TCGETA, &t); | |
2646 switch (signo) { | |
2647 case SIGINT: return t.c_cc[VINTR]; | |
2648 case SIGQUIT: return t.c_cc[VQUIT]; | |
2649 # ifdef SIGTSTP | |
2650 case SIGTSTP: return t.c_cc[VSWTCH]; | |
2651 # endif /* SIGTSTP */ | |
2652 } | |
2653 } | |
2654 # else /* ! defined (TCGETA) */ | |
2655 #error ERROR! Using SIGNALS_VIA_CHARACTERS, but not (TIOCGLTC && TIOCGETC) || TCGETA | |
2656 /* If your system configuration files define SIGNALS_VIA_CHARACTERS, | |
2657 you'd better be using one of the alternatives above! */ | |
2658 # endif /* ! defined (TCGETA) */ | |
2659 return '\0'; | |
2660 } | |
2661 #endif /* SIGNALS_VIA_CHARACTERS */ | |
2662 | |
2663 | |
2664 /* send a signal number SIGNO to PROCESS. | |
2665 CURRENT_GROUP means send to the process group that currently owns | |
2666 the terminal being used to communicate with PROCESS. | |
2667 This is used for various commands in shell mode. | |
2668 If NOMSG is zero, insert signal-announcements into process's buffers | |
2669 right away. | |
2670 | |
2671 If we can, we try to signal PROCESS by sending control characters | |
2672 down the pty. This allows us to signal inferiors who have changed | |
2673 their uid, for which killpg would return an EPERM error. */ | |
2674 | |
2675 static void | |
2676 process_send_signal (Lisp_Object process0, int signo, | |
2677 int current_group, int nomsg) | |
2678 { | |
2679 /* This function can GC */ | |
2680 Lisp_Object proc = get_process (process0); | |
2681 struct Lisp_Process *p = XPROCESS (proc); | |
2682 int gid; | |
2683 int no_pgrp = 0; | |
2684 | |
2685 if (network_connection_p (proc)) | |
2686 error ("Network connection %s is not a subprocess", | |
2687 string_data (XSTRING (p->name))); | |
2688 if (p->infd < 0) | |
2689 error ("Process %s is not active", | |
2690 string_data (XSTRING (p->name))); | |
2691 | |
2692 if (!p->pty_flag) | |
2693 current_group = 0; | |
2694 | |
2695 /* If we are using pgrps, get a pgrp number and make it negative. */ | |
2696 if (current_group) | |
2697 { | |
2698 #ifdef SIGNALS_VIA_CHARACTERS | |
2699 /* If possible, send signals to the entire pgrp | |
2700 by sending an input character to it. */ | |
2701 { | |
2702 char sigchar = process_signal_char(p->subtty, signo); | |
2703 if (sigchar) { | |
2704 send_process (proc, Qnil, (Bufbyte *) &sigchar, 0, 1); | |
2705 return; | |
2706 } | |
2707 } | |
2708 #endif /* ! defined (SIGNALS_VIA_CHARACTERS) */ | |
2709 | |
2710 #ifdef TIOCGPGRP | |
2711 /* Get the pgrp using the tty itself, if we have that. | |
2712 Otherwise, use the pty to get the pgrp. | |
2713 On pfa systems, saka@pfu.fujitsu.co.JP writes: | |
2714 "TIOCGPGRP symbol defined in sys/ioctl.h at E50. | |
2715 But, TIOCGPGRP does not work on E50 ;-P works fine on E60" | |
2716 His patch indicates that if TIOCGPGRP returns an error, then | |
2717 we should just assume that p->pid is also the process group id. */ | |
2718 { | |
2719 int err; | |
2720 | |
2721 err = ioctl ( (p->subtty != -1 ? p->subtty : p->infd), TIOCGPGRP, &gid); | |
2722 | |
2723 #ifdef pfa | |
2724 if (err == -1) | |
2725 gid = - XINT (p->pid); | |
2726 #endif /* ! defined (pfa) */ | |
2727 } | |
2728 if (gid == -1) | |
2729 no_pgrp = 1; | |
2730 else | |
2731 gid = - gid; | |
2732 #else /* ! defined (TIOCGPGRP ) */ | |
2733 /* Can't select pgrps on this system, so we know that | |
2734 the child itself heads the pgrp. */ | |
2735 gid = - XINT (p->pid); | |
2736 #endif /* ! defined (TIOCGPGRP ) */ | |
2737 } | |
2738 else | |
2739 gid = - XINT (p->pid); | |
2740 | |
2741 switch (signo) | |
2742 { | |
2743 #ifdef SIGCONT | |
2744 case SIGCONT: | |
2745 p->status_symbol = Qrun; | |
2746 p->exit_code = 0; | |
2747 p->tick++; | |
2748 process_tick++; | |
2749 if (!nomsg) | |
2750 status_notify (); | |
2751 break; | |
2752 #endif /* ! defined (SIGCONT) */ | |
2753 case SIGINT: | |
2754 #ifdef VMS | |
2755 send_process (proc, Qnil, (Bufbyte *) "\003", 0, | |
2756 1); /* ^C */ | |
2757 goto whoosh; | |
2758 #endif | |
2759 case SIGQUIT: | |
2760 #ifdef VMS | |
2761 send_process (proc, Qnil, (Bufbyte *) "\031", 0, | |
2762 1); /* ^Y */ | |
2763 goto whoosh; | |
2764 #endif | |
2765 case SIGKILL: | |
2766 #ifdef VMS | |
2767 sys$forcex (&(XINT (p->pid)), 0, 1); | |
2768 whoosh: | |
2769 #endif | |
2770 flush_pending_output (p->infd); | |
2771 break; | |
2772 } | |
2773 | |
2774 /* If we don't have process groups, send the signal to the immediate | |
2775 subprocess. That isn't really right, but it's better than any | |
2776 obvious alternative. */ | |
2777 if (no_pgrp) | |
2778 { | |
2779 kill (XINT (p->pid), signo); | |
2780 return; | |
2781 } | |
2782 | |
2783 /* gid may be a pid, or minus a pgrp's number */ | |
2784 #ifdef TIOCSIGSEND | |
2785 if (current_group) | |
2786 ioctl (p->infd, TIOCSIGSEND, signo); | |
2787 else | |
2788 { | |
2789 gid = - XINT (p->pid); | |
2790 kill (gid, signo); | |
2791 } | |
2792 #else /* ! defined (TIOCSIGSEND) */ | |
2793 EMACS_KILLPG (-gid, signo); | |
2794 #endif /* ! defined (TIOCSIGSEND) */ | |
2795 } | |
2796 | |
2797 DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0 /* | |
2798 Interrupt process PROCESS. May be process or name of one. | |
2799 Nil or no arg means current buffer's process. | |
2800 Second arg CURRENT-GROUP non-nil means send signal to | |
2801 the current process-group of the process's controlling terminal | |
2802 rather than to the process's own process group. | |
2803 If the process is a shell, this means interrupt current subjob | |
2804 rather than the shell. | |
2805 */ ) | |
2806 (process, current_group) | |
2807 Lisp_Object process, current_group; | |
2808 { | |
2809 /* This function can GC */ | |
2810 process_send_signal (process, SIGINT, !NILP (current_group), 0); | |
2811 return process; | |
2812 } | |
2813 | |
2814 DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0 /* | |
2815 Kill process PROCESS. May be process or name of one. | |
2816 See function `interrupt-process' for more details on usage. | |
2817 */ ) | |
2818 (process, current_group) | |
2819 Lisp_Object process, current_group; | |
2820 { | |
2821 /* This function can GC */ | |
2822 process_send_signal (process, SIGKILL, !NILP (current_group), | |
2823 0); | |
2824 return process; | |
2825 } | |
2826 | |
2827 DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0 /* | |
2828 Send QUIT signal to process PROCESS. May be process or name of one. | |
2829 See function `interrupt-process' for more details on usage. | |
2830 */ ) | |
2831 (process, current_group) | |
2832 Lisp_Object process, current_group; | |
2833 { | |
2834 /* This function can GC */ | |
2835 process_send_signal (process, SIGQUIT, !NILP (current_group), | |
2836 0); | |
2837 return process; | |
2838 } | |
2839 | |
2840 DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0 /* | |
2841 Stop process PROCESS. May be process or name of one. | |
2842 See function `interrupt-process' for more details on usage. | |
2843 */ ) | |
2844 (process, current_group) | |
2845 Lisp_Object process, current_group; | |
2846 { | |
2847 /* This function can GC */ | |
2848 #ifndef SIGTSTP | |
2849 error ("no SIGTSTP support"); | |
2850 #else | |
2851 process_send_signal (process, SIGTSTP, !NILP (current_group), | |
2852 0); | |
2853 #endif | |
2854 return process; | |
2855 } | |
2856 | |
2857 DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0 /* | |
2858 Continue process PROCESS. May be process or name of one. | |
2859 See function `interrupt-process' for more details on usage. | |
2860 */ ) | |
2861 (process, current_group) | |
2862 Lisp_Object process, current_group; | |
2863 { | |
2864 /* This function can GC */ | |
2865 #ifdef SIGCONT | |
2866 process_send_signal (process, SIGCONT, !NILP (current_group), | |
2867 0); | |
2868 #else | |
2869 error ("no SIGCONT support"); | |
2870 #endif | |
2871 return process; | |
2872 } | |
2873 | |
2874 DEFUN ("signal-process", Fsignal_process, Ssignal_process, | |
2875 2, 2, "nProcess number: \nnSignal code: " /* | |
2876 Send the process with process id PID the signal with code SIGCODE. | |
2877 PID must be an integer. The process need not be a child of this Emacs. | |
2878 SIGCODE may be an integer, or a symbol whose name is a signal name. | |
2879 */ ) | |
2880 (pid, sigcode) | |
2881 Lisp_Object pid, sigcode; | |
2882 { | |
2883 CHECK_INT (pid); | |
2884 | |
2885 #define handle_signal(NAME, VALUE) \ | |
2886 else if (!strcmp ((CONST char *) name, NAME)) \ | |
2887 XSETINT (sigcode, VALUE) | |
2888 | |
2889 if (INTP (sigcode)) | |
2890 ; | |
2891 else | |
2892 { | |
2893 Bufbyte *name; | |
2894 | |
2895 CHECK_SYMBOL (sigcode); | |
2896 name = string_data (XSYMBOL (sigcode)->name); | |
2897 | |
2898 if (0) | |
2899 ; | |
2900 #ifdef SIGHUP | |
2901 handle_signal ("SIGHUP", SIGHUP); | |
2902 #endif | |
2903 #ifdef SIGINT | |
2904 handle_signal ("SIGINT", SIGINT); | |
2905 #endif | |
2906 #ifdef SIGQUIT | |
2907 handle_signal ("SIGQUIT", SIGQUIT); | |
2908 #endif | |
2909 #ifdef SIGILL | |
2910 handle_signal ("SIGILL", SIGILL); | |
2911 #endif | |
2912 #ifdef SIGABRT | |
2913 handle_signal ("SIGABRT", SIGABRT); | |
2914 #endif | |
2915 #ifdef SIGEMT | |
2916 handle_signal ("SIGEMT", SIGEMT); | |
2917 #endif | |
2918 #ifdef SIGKILL | |
2919 handle_signal ("SIGKILL", SIGKILL); | |
2920 #endif | |
2921 #ifdef SIGFPE | |
2922 handle_signal ("SIGFPE", SIGFPE); | |
2923 #endif | |
2924 #ifdef SIGBUS | |
2925 handle_signal ("SIGBUS", SIGBUS); | |
2926 #endif | |
2927 #ifdef SIGSEGV | |
2928 handle_signal ("SIGSEGV", SIGSEGV); | |
2929 #endif | |
2930 #ifdef SIGSYS | |
2931 handle_signal ("SIGSYS", SIGSYS); | |
2932 #endif | |
2933 #ifdef SIGPIPE | |
2934 handle_signal ("SIGPIPE", SIGPIPE); | |
2935 #endif | |
2936 #ifdef SIGALRM | |
2937 handle_signal ("SIGALRM", SIGALRM); | |
2938 #endif | |
2939 #ifdef SIGTERM | |
2940 handle_signal ("SIGTERM", SIGTERM); | |
2941 #endif | |
2942 #ifdef SIGURG | |
2943 handle_signal ("SIGURG", SIGURG); | |
2944 #endif | |
2945 #ifdef SIGSTOP | |
2946 handle_signal ("SIGSTOP", SIGSTOP); | |
2947 #endif | |
2948 #ifdef SIGTSTP | |
2949 handle_signal ("SIGTSTP", SIGTSTP); | |
2950 #endif | |
2951 #ifdef SIGCONT | |
2952 handle_signal ("SIGCONT", SIGCONT); | |
2953 #endif | |
2954 #ifdef SIGCHLD | |
2955 handle_signal ("SIGCHLD", SIGCHLD); | |
2956 #endif | |
2957 #ifdef SIGTTIN | |
2958 handle_signal ("SIGTTIN", SIGTTIN); | |
2959 #endif | |
2960 #ifdef SIGTTOU | |
2961 handle_signal ("SIGTTOU", SIGTTOU); | |
2962 #endif | |
2963 #ifdef SIGIO | |
2964 handle_signal ("SIGIO", SIGIO); | |
2965 #endif | |
2966 #ifdef SIGXCPU | |
2967 handle_signal ("SIGXCPU", SIGXCPU); | |
2968 #endif | |
2969 #ifdef SIGXFSZ | |
2970 handle_signal ("SIGXFSZ", SIGXFSZ); | |
2971 #endif | |
2972 #ifdef SIGVTALRM | |
2973 handle_signal ("SIGVTALRM", SIGVTALRM); | |
2974 #endif | |
2975 #ifdef SIGPROF | |
2976 handle_signal ("SIGPROF", SIGPROF); | |
2977 #endif | |
2978 #ifdef SIGWINCH | |
2979 handle_signal ("SIGWINCH", SIGWINCH); | |
2980 #endif | |
2981 #ifdef SIGINFO | |
2982 handle_signal ("SIGINFO", SIGINFO); | |
2983 #endif | |
2984 #ifdef SIGUSR1 | |
2985 handle_signal ("SIGUSR1", SIGUSR1); | |
2986 #endif | |
2987 #ifdef SIGUSR2 | |
2988 handle_signal ("SIGUSR2", SIGUSR2); | |
2989 #endif | |
2990 else | |
2991 error ("Undefined signal name %s", name); | |
2992 } | |
2993 | |
2994 #undef handle_signal | |
2995 | |
2996 #ifdef WINDOWSNT | |
2997 /* Only works for kill-type signals */ | |
2998 return make_int (win32_kill_process (XINT (pid), XINT (sigcode))); | |
2999 #else | |
3000 return make_int (kill (XINT (pid), XINT (sigcode))); | |
3001 #endif | |
3002 } | |
3003 | |
3004 DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0 /* | |
3005 Make PROCESS see end-of-file in its input. | |
3006 PROCESS may be a process, a buffer, the name of a process or buffer, or | |
3007 nil, indicating the current buffer's process. | |
3008 If PROCESS is a network connection, or is a process communicating | |
3009 through a pipe (as opposed to a pty), then you cannot send any more | |
3010 text to PROCESS after you call this function. | |
3011 */ ) | |
3012 (process) | |
3013 Lisp_Object process; | |
3014 { | |
3015 /* This function can GC */ | |
3016 Lisp_Object proc; | |
3017 | |
3018 proc = get_process (process); | |
3019 | |
3020 /* Make sure the process is really alive. */ | |
3021 if (! EQ (XPROCESS (proc)->status_symbol, Qrun)) | |
3022 error ("Process %s not running", | |
3023 string_data (XSTRING (XPROCESS (proc)->name))); | |
3024 | |
3025 #ifdef VMS | |
3026 send_process (proc, Qnil, (Bufbyte *) "\032", 0, 1); /* ^Z */ | |
3027 #else | |
3028 if (XPROCESS (proc)->pty_flag) | |
3029 { | |
3030 /* #### get_eof_char simply doesn't return the correct character | |
3031 here. Maybe it is needed to determine the right eof | |
3032 character in init_process_fds but here it simply screws | |
3033 things up. */ | |
3034 #if 0 | |
3035 Bufbyte eof_char = get_eof_char (XPROCESS (proc)); | |
3036 send_process (proc, Qnil, &eof_char, 0, 1); | |
3037 #else | |
3038 send_process (proc, Qnil, (CONST Bufbyte *) "\004", 0, 1); | |
3039 #endif | |
3040 } | |
3041 else | |
3042 { | |
3043 close (XPROCESS (proc)->outfd); | |
3044 XPROCESS (proc)->outfd = open (NULL_DEVICE, O_WRONLY, 0); | |
3045 } | |
3046 #endif /* !VMS */ | |
3047 return process; | |
3048 } | |
3049 | |
3050 | |
3051 /************************************************************************/ | |
3052 /* deleting a process */ | |
3053 /************************************************************************/ | |
3054 | |
3055 void | |
3056 deactivate_process (Lisp_Object proc) | |
3057 { | |
3058 int inchannel, outchannel; | |
3059 struct Lisp_Process *p = XPROCESS (proc); | |
3060 SIGTYPE (*old_sigpipe) (int) = 0; | |
3061 | |
3062 inchannel = p->infd; | |
3063 outchannel = p->outfd; | |
3064 | |
3065 /* closing the outstream could result in SIGPIPE, so ignore it. */ | |
3066 old_sigpipe = | |
3067 (SIGTYPE (*) (int)) signal (SIGPIPE, SIG_IGN); | |
3068 if (!NILP (p->instream)) | |
3069 Lstream_close (XLSTREAM (p->instream)); | |
3070 if (!NILP (p->outstream)) | |
3071 Lstream_close (XLSTREAM (p->outstream)); | |
3072 signal (SIGPIPE, old_sigpipe); | |
3073 | |
3074 if (inchannel >= 0) | |
3075 { | |
3076 /* Beware SIGCHLD hereabouts. */ | |
3077 flush_pending_output (inchannel); | |
3078 close_descriptor_pair (inchannel, outchannel); | |
3079 if (!NILP (p->pid)) | |
3080 { | |
3081 /* It's possible that we got as far in the process-creation | |
3082 process as creating the descriptors but didn't get so | |
3083 far as selecting the process for input. In this | |
3084 case, p->pid is nil: p->pid is set at the same time that | |
3085 the process is selected for input. */ | |
3086 #ifdef VMS | |
3087 { | |
3088 VMS_PROC_STUFF *get_vms_process_pointer (), *vs; | |
3089 if (outchannel >= 0) | |
3090 sys$dassgn (outchannel); | |
3091 vs = get_vms_process_pointer (XINT (p->pid)); | |
3092 if (vs) | |
3093 give_back_vms_process_stuff (vs); | |
3094 } | |
3095 #endif /* VMS */ | |
3096 /* Must call this before setting the file descriptors to 0 */ | |
3097 event_stream_unselect_process (p); | |
3098 } | |
3099 | |
3100 p->infd = -1; | |
3101 p->outfd = -1; | |
3102 descriptor_to_process[inchannel] = Qnil; | |
3103 } | |
3104 } | |
3105 | |
3106 static void | |
3107 remove_process (Lisp_Object proc) | |
3108 { | |
3109 Vprocess_list = delq_no_quit (proc, Vprocess_list); | |
3110 Fset_marker (XPROCESS (proc)->mark, Qnil, Qnil); | |
3111 | |
3112 deactivate_process (proc); | |
3113 } | |
3114 | |
3115 DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0 /* | |
3116 Delete PROCESS: kill it and forget about it immediately. | |
3117 PROCESS may be a process or the name of one, or a buffer name. | |
3118 */ ) | |
3119 (proc) | |
3120 Lisp_Object proc; | |
3121 { | |
3122 /* This function can GC */ | |
3123 struct Lisp_Process *p; | |
3124 proc = get_process (proc); | |
3125 p = XPROCESS (proc); | |
3126 if (network_connection_p (proc)) | |
3127 { | |
3128 p->status_symbol = Qexit; | |
3129 p->exit_code = 0; | |
3130 p->core_dumped = 0; | |
3131 p->tick++; | |
3132 process_tick++; | |
3133 } | |
3134 else if (p->infd >= 0) | |
3135 { | |
3136 Fkill_process (proc, Qnil); | |
3137 /* Do this now, since remove_process will make sigchld_handler do nothing. */ | |
3138 p->status_symbol = Qsignal; | |
3139 p->exit_code = SIGKILL; | |
3140 p->core_dumped = 0; | |
3141 p->tick++; | |
3142 process_tick++; | |
3143 status_notify (); | |
3144 } | |
3145 remove_process (proc); | |
3146 return Qnil; | |
3147 } | |
3148 | |
3149 /* Kill all processes associated with `buffer'. | |
3150 If `buffer' is nil, kill all processes */ | |
3151 | |
3152 void | |
3153 kill_buffer_processes (Lisp_Object buffer) | |
3154 { | |
3155 Lisp_Object tail; | |
3156 | |
3157 for (tail = Vprocess_list; GC_CONSP (tail); | |
3158 tail = XCDR (tail)) | |
3159 { | |
3160 Lisp_Object proc = XCAR (tail); | |
3161 if (GC_PROCESSP (proc) | |
3162 && (GC_NILP (buffer) || GC_EQ (XPROCESS (proc)->buffer, buffer))) | |
3163 { | |
3164 if (network_connection_p (proc)) | |
3165 Fdelete_process (proc); | |
3166 else if (XPROCESS (proc)->infd >= 0) | |
3167 process_send_signal (proc, SIGHUP, 0, 1); | |
3168 } | |
3169 } | |
3170 } | |
3171 | |
3172 #if 0 /* Unused */ | |
3173 int | |
3174 count_active_processes (void) | |
3175 { | |
3176 Lisp_Object tail; | |
3177 int count = 0; | |
3178 | |
3179 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail)) | |
3180 { | |
3181 Lisp_Object status = XPROCESS (XCAR (tail))->status_symbol; | |
3182 if ((EQ (status, Qrun) || EQ (status, Qstop))) | |
3183 count++; | |
3184 } | |
3185 | |
3186 return count; | |
3187 } | |
3188 #endif /* Unused */ | |
3189 | |
3190 DEFUN ("process-kill-without-query", Fprocess_kill_without_query, | |
3191 Sprocess_kill_without_query, 1, 2, 0 /* | |
3192 Say no query needed if PROCESS is running when Emacs is exited. | |
3193 Optional second argument if non-nil says to require a query. | |
3194 Value is t if a query was formerly required. | |
3195 */ ) | |
3196 (proc, require_query_p) | |
3197 Lisp_Object proc, require_query_p; | |
3198 { | |
3199 int tem; | |
3200 | |
3201 CHECK_PROCESS (proc); | |
3202 tem = XPROCESS (proc)->kill_without_query; | |
3203 XPROCESS (proc)->kill_without_query = NILP (require_query_p); | |
3204 | |
3205 return (tem ? Qnil : Qt); | |
3206 } | |
3207 | |
3208 DEFUN ("process-kill-without-query-p", Fprocess_kill_without_query_p, | |
3209 Sprocess_kill_without_query_p, 1, 1, 0 /* | |
3210 Whether PROC will be killed without query if running when emacs is exited. | |
3211 */ ) | |
3212 (proc) | |
3213 Lisp_Object proc; | |
3214 { | |
3215 CHECK_PROCESS (proc); | |
3216 return (XPROCESS (proc)->kill_without_query ? Qt : Qnil); | |
3217 } | |
3218 | |
3219 | |
3220 /* This is not named init_process in order to avoid a conflict with NS 3.3 */ | |
3221 void | |
3222 init_xemacs_process (void) | |
3223 { | |
3224 int i; | |
3225 | |
3226 #ifdef SIGCHLD | |
3227 # ifndef CANNOT_DUMP | |
3228 if (! noninteractive || initialized) | |
3229 # endif | |
3230 signal (SIGCHLD, sigchld_handler); | |
3231 #endif /* SIGCHLD */ | |
3232 | |
3233 Vprocess_list = Qnil; | |
3234 for (i = 0; i < MAXDESC; i++) | |
3235 { | |
3236 descriptor_to_process[i] = Qnil; | |
3237 proc_buffered_char[i] = -1; | |
3238 } | |
3239 } | |
3240 #if 0 | |
3241 | |
3242 xxDEFUN ("process-connection", Fprocess_connection, Sprocess_connection, | |
3243 0, 1, 0 /* | |
3244 Return the connection type of `PROCESS'. This can be nil (pipe), | |
3245 t or pty (pty) or stream (socket connection). | |
3246 */ ) | |
3247 (process) | |
3248 Lisp_Object process; | |
3249 { | |
3250 return XPROCESS (process)->type; | |
3251 } | |
3252 | |
3253 #endif /* 0 */ | |
3254 | |
3255 void | |
3256 syms_of_process (void) | |
3257 { | |
3258 defsymbol (&Qprocessp, "processp"); | |
3259 defsymbol (&Qrun, "run"); | |
3260 defsymbol (&Qstop, "stop"); | |
3261 defsymbol (&Qsignal, "signal"); | |
3262 /* Qexit is already defined by syms_of_eval | |
3263 * defsymbol (&Qexit, "exit"); | |
3264 */ | |
3265 defsymbol (&Qopen, "open"); | |
3266 defsymbol (&Qclosed, "closed"); | |
3267 | |
3268 defsubr (&Sprocessp); | |
3269 defsubr (&Sget_process); | |
3270 defsubr (&Sget_buffer_process); | |
3271 defsubr (&Sdelete_process); | |
3272 defsubr (&Sprocess_status); | |
3273 defsubr (&Sprocess_exit_status); | |
3274 defsubr (&Sprocess_id); | |
3275 defsubr (&Sprocess_name); | |
3276 defsubr (&Sprocess_tty_name); | |
3277 defsubr (&Sprocess_command); | |
3278 defsubr (&Sset_process_buffer); | |
3279 defsubr (&Sprocess_buffer); | |
3280 defsubr (&Sprocess_mark); | |
3281 defsubr (&Sset_process_filter); | |
3282 defsubr (&Sprocess_filter); | |
3283 defsubr (&Sset_process_window_size); | |
3284 defsubr (&Sset_process_sentinel); | |
3285 defsubr (&Sprocess_sentinel); | |
3286 defsubr (&Sprocess_kill_without_query); | |
3287 defsubr (&Sprocess_kill_without_query_p); | |
3288 defsubr (&Sprocess_list); | |
3289 defsubr (&Sstart_process_internal); | |
3290 #ifdef HAVE_SOCKETS | |
3291 defsubr (&Sopen_network_stream_internal); | |
3292 #endif /* HAVE_SOCKETS */ | |
3293 defsubr (&Sprocess_send_region); | |
3294 defsubr (&Sprocess_send_string); | |
3295 defsubr (&Sinterrupt_process); | |
3296 defsubr (&Skill_process); | |
3297 defsubr (&Squit_process); | |
3298 defsubr (&Sstop_process); | |
3299 defsubr (&Scontinue_process); | |
3300 defsubr (&Sprocess_send_eof); | |
3301 defsubr (&Ssignal_process); | |
3302 /* defsubr (&Sprocess_connection); */ | |
3303 } | |
3304 | |
3305 void | |
3306 vars_of_process (void) | |
3307 { | |
3308 Fprovide (intern ("subprocesses")); | |
3309 #ifdef HAVE_SOCKETS | |
3310 Fprovide (intern ("network-streams")); | |
3311 #endif | |
3312 staticpro (&Vprocess_list); | |
3313 | |
3314 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes /* | |
3315 *Non-nil means delete processes immediately when they exit. | |
3316 nil means don't delete them until `list-processes' is run. | |
3317 */ ); | |
3318 | |
3319 delete_exited_processes = 1; | |
3320 | |
3321 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type /* | |
3322 Control type of device used to communicate with subprocesses. | |
3323 Values are nil to use a pipe, or t or `pty' to use a pty. | |
3324 The value has no effect if the system has no ptys or if all ptys are busy: | |
3325 then a pipe is used in any case. | |
3326 The value takes effect when `start-process' is called. | |
3327 */ ); | |
3328 Vprocess_connection_type = Qt; | |
3329 } | |
3330 | |
3331 #endif /* not NO_SUBPROCESSES */ |