Mercurial > hg > xemacs-beta
annotate src/process.c @ 5118:e0db3c197671 ben-lisp-object
merge up to latest default branch, doesn't compile yet
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Sat, 26 Dec 2009 21:18:49 -0600 |
parents | 3742ea8250b5 aa5ed11f473b |
children | 623d57b7fbe8 |
rev | line source |
---|---|
428 | 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. | |
3025 | 5 Copyright (C) 1995, 1996, 2001, 2002, 2004, 2005 Ben Wing. |
428 | 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 | |
814 | 24 /* This file has been Mule-ized. */ |
428 | 25 |
26 /* This file has been split into process.c and process-unix.c by | |
27 Kirill M. Katsnelson <kkm@kis.ru>, so please bash him and not | |
814 | 28 the original author(s). |
29 | |
30 Non-synch-subprocess stuff (mostly process environment) moved from | |
853 | 31 callproc.c, 4-3-02, Ben Wing. |
32 | |
33 callproc.c deleted entirely 5-23-02, Ben Wing. Good riddance! | |
34 */ | |
428 | 35 |
36 #include <config.h> | |
37 | |
38 #include "lisp.h" | |
39 | |
40 #include "buffer.h" | |
41 #include "commands.h" | |
800 | 42 #include "device.h" |
428 | 43 #include "events.h" |
800 | 44 #include "file-coding.h" |
428 | 45 #include "frame.h" |
46 #include "hash.h" | |
47 #include "insdel.h" | |
48 #include "lstream.h" | |
49 #include "opaque.h" | |
50 #include "process.h" | |
51 #include "procimpl.h" | |
816 | 52 #include "sysdep.h" |
428 | 53 #include "window.h" |
54 | |
55 #include "sysfile.h" | |
56 #include "sysproc.h" | |
859 | 57 #include "syssignal.h" |
428 | 58 #include "systime.h" |
59 #include "systty.h" | |
60 #include "syswait.h" | |
61 | |
2367 | 62 #ifdef WIN32_NATIVE |
63 #include "syswindows.h" | |
64 #endif | |
65 | |
863 | 66 Lisp_Object Qprocessp, Qprocess_live_p, Qprocess_readable_p; |
428 | 67 |
68 /* Process methods */ | |
69 struct process_methods the_process_methods; | |
70 | |
71 /* a process object is a network connection when its pid field a cons | |
72 (name of name of port we are connected to . foreign host name) */ | |
73 | |
74 /* Valid values of process->status_symbol */ | |
75 Lisp_Object Qrun, Qstop; | |
76 /* Qrun => Qopen, Qexit => Qclosed for "network connection" processes */ | |
77 Lisp_Object Qopen, Qclosed; | |
78 /* Protocol families */ | |
79 Lisp_Object Qtcp, Qudp; | |
80 | |
81 #ifdef HAVE_MULTICAST | |
82 Lisp_Object Qmulticast; /* Will be used for occasional warnings */ | |
83 #endif | |
84 | |
85 /* t means use pty, nil means use a pipe, | |
86 maybe other values to come. */ | |
87 Lisp_Object Vprocess_connection_type; | |
88 | |
89 /* Read comments to DEFVAR of this */ | |
90 int windowed_process_io; | |
91 | |
92 #ifdef PROCESS_IO_BLOCKING | |
93 /* List of port numbers or port names to set a blocking I/O mode. | |
94 Nil means set a non-blocking I/O mode [default]. */ | |
95 Lisp_Object network_stream_blocking_port_list; | |
96 #endif /* PROCESS_IO_BLOCKING */ | |
97 | |
98 /* Number of events of change of status of a process. */ | |
99 volatile int process_tick; | |
100 | |
101 /* Number of events for which the user or sentinel has been notified. */ | |
102 static int update_tick; | |
103 | |
104 /* Nonzero means delete a process right away if it exits. */ | |
105 int delete_exited_processes; | |
106 | |
853 | 107 /* Hash table which maps USIDs as returned by create_io_streams_cb to |
428 | 108 process objects. Processes are not GC-protected through this! */ |
109 struct hash_table *usid_to_process; | |
110 | |
4729
428d7c571110
Fix issue145: accept nil in default-process-coding-system.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4693
diff
changeset
|
111 /* Read-only to Lisp. See DEFUN Fprocess_list. */ |
428 | 112 Lisp_Object Vprocess_list; |
113 | |
4729
428d7c571110
Fix issue145: accept nil in default-process-coding-system.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4693
diff
changeset
|
114 /* Lisp variables; see docstrings below. */ |
442 | 115 Lisp_Object Vnull_device; |
771 | 116 Lisp_Object Vdefault_process_coding_system; |
853 | 117 Lisp_Object Vdefault_network_coding_system; |
563 | 118 Lisp_Object Qprocess_error; |
119 Lisp_Object Qnetwork_error; | |
771 | 120 Fixnum debug_process_io; |
814 | 121 Lisp_Object Vshell_file_name; |
122 Lisp_Object Vprocess_environment; | |
123 | |
124 /* Make sure egetenv() not called too soon */ | |
125 int env_initted; | |
126 | |
4729
428d7c571110
Fix issue145: accept nil in default-process-coding-system.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4693
diff
changeset
|
127 /* Internal Lisp variable. */ |
814 | 128 Lisp_Object Vlisp_EXEC_SUFFIXES; |
129 | |
428 | 130 |
131 | |
1204 | 132 static const struct memory_description process_description [] = { |
133 #define MARKED_SLOT(x) { XD_LISP_OBJECT, offsetof (Lisp_Process, x) }, | |
134 #include "process-slots.h" | |
934 | 135 { XD_END } |
136 }; | |
137 | |
428 | 138 static Lisp_Object |
444 | 139 mark_process (Lisp_Object object) |
428 | 140 { |
444 | 141 Lisp_Process *process = XPROCESS (object); |
1204 | 142 #define MARKED_SLOT(x) mark_object (process->x); |
143 #include "process-slots.h" | |
144 return Qnil; | |
428 | 145 } |
146 | |
147 static void | |
444 | 148 print_process (Lisp_Object object, Lisp_Object printcharfun, int escapeflag) |
428 | 149 { |
444 | 150 Lisp_Process *process = XPROCESS (object); |
428 | 151 |
152 if (print_readably) | |
563 | 153 printing_unreadable_object ("#<process %s>", XSTRING_DATA (process->name)); |
428 | 154 |
155 if (!escapeflag) | |
156 { | |
444 | 157 print_internal (process->name, printcharfun, 0); |
428 | 158 } |
159 else | |
160 { | |
444 | 161 int netp = network_connection_p (object); |
826 | 162 write_c_string (printcharfun, |
163 netp ? GETTEXT ("#<network connection ") : | |
164 GETTEXT ("#<process ")); | |
444 | 165 print_internal (process->name, printcharfun, 1); |
826 | 166 write_c_string (printcharfun, (netp ? " " : " pid ")); |
444 | 167 print_internal (process->pid, printcharfun, 1); |
800 | 168 write_fmt_string_lisp (printcharfun, " state:%S", 1, process->status_symbol); |
444 | 169 MAYBE_PROCMETH (print_process_data, (process, printcharfun)); |
826 | 170 write_c_string (printcharfun, ">"); |
428 | 171 } |
172 } | |
173 | |
174 #ifdef HAVE_WINDOW_SYSTEM | |
440 | 175 extern void debug_process_finalization (Lisp_Process *p); |
428 | 176 #endif /* HAVE_WINDOW_SYSTEM */ |
177 | |
178 static void | |
179 finalize_process (void *header, int for_disksave) | |
180 { | |
181 /* #### this probably needs to be tied into the tty event loop */ | |
182 /* #### when there is one */ | |
440 | 183 Lisp_Process *p = (Lisp_Process *) header; |
428 | 184 #ifdef HAVE_WINDOW_SYSTEM |
185 if (!for_disksave) | |
186 { | |
187 debug_process_finalization (p); | |
188 } | |
189 #endif /* HAVE_WINDOW_SYSTEM */ | |
190 | |
191 if (p->process_data) | |
192 { | |
193 MAYBE_PROCMETH (finalize_process_data, (p, for_disksave)); | |
194 if (!for_disksave) | |
1726 | 195 xfree (p->process_data, void *); |
428 | 196 } |
197 } | |
198 | |
5118
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
199 DEFINE_NODUMP_LISP_OBJECT ("process", process, |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
200 mark_process, print_process, finalize_process, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
201 0, 0, process_description, Lisp_Process); |
428 | 202 |
203 /************************************************************************/ | |
204 /* basic process accessors */ | |
205 /************************************************************************/ | |
206 | |
771 | 207 /* This function returns low-level streams, connected directly to the child |
208 process, rather than en/decoding streams */ | |
428 | 209 void |
853 | 210 get_process_streams (Lisp_Process *p, Lisp_Object *instr, Lisp_Object *outstr, |
211 Lisp_Object *errstr) | |
428 | 212 { |
213 assert (p); | |
853 | 214 assert (NILP (p->pipe_instream) || LSTREAMP (p->pipe_instream)); |
215 assert (NILP (p->pipe_outstream) || LSTREAMP (p->pipe_outstream)); | |
216 assert (NILP (p->pipe_errstream) || LSTREAMP (p->pipe_errstream)); | |
428 | 217 *instr = p->pipe_instream; |
218 *outstr = p->pipe_outstream; | |
853 | 219 *errstr = p->pipe_errstream; |
428 | 220 } |
221 | |
853 | 222 /* Given a USID referring to either a process's instream or errstream, |
223 return the associated process. */ | |
440 | 224 Lisp_Process * |
428 | 225 get_process_from_usid (USID usid) |
226 { | |
442 | 227 const void *vval; |
428 | 228 |
229 assert (usid != USID_ERROR && usid != USID_DONTHASH); | |
230 | |
442 | 231 if (gethash ((const void*)usid, usid_to_process, &vval)) |
428 | 232 { |
444 | 233 Lisp_Object process; |
826 | 234 process = VOID_TO_LISP (vval); |
444 | 235 return XPROCESS (process); |
428 | 236 } |
237 else | |
238 return 0; | |
239 } | |
240 | |
241 int | |
853 | 242 get_process_selected_p (Lisp_Process *p, int do_err) |
428 | 243 { |
853 | 244 return do_err ? p->err_selected : p->in_selected; |
428 | 245 } |
246 | |
247 void | |
853 | 248 set_process_selected_p (Lisp_Process *p, int in_selected, int err_selected) |
428 | 249 { |
853 | 250 p->in_selected = !!in_selected; |
251 p->err_selected = !!err_selected; | |
428 | 252 } |
253 | |
254 int | |
440 | 255 connected_via_filedesc_p (Lisp_Process *p) |
428 | 256 { |
257 return MAYBE_INT_PROCMETH (tooltalk_connection_p, (p)); | |
258 } | |
259 | |
260 #ifdef HAVE_SOCKETS | |
261 int | |
262 network_connection_p (Lisp_Object process) | |
263 { | |
264 return CONSP (XPROCESS (process)->pid); | |
265 } | |
266 #endif | |
267 | |
268 DEFUN ("processp", Fprocessp, 1, 1, 0, /* | |
269 Return t if OBJECT is a process. | |
270 */ | |
444 | 271 (object)) |
428 | 272 { |
444 | 273 return PROCESSP (object) ? Qt : Qnil; |
428 | 274 } |
275 | |
440 | 276 DEFUN ("process-live-p", Fprocess_live_p, 1, 1, 0, /* |
277 Return t if OBJECT is a process that is alive. | |
278 */ | |
444 | 279 (object)) |
440 | 280 { |
444 | 281 return PROCESSP (object) && PROCESS_LIVE_P (XPROCESS (object)) |
282 ? Qt : Qnil; | |
440 | 283 } |
284 | |
863 | 285 #if 0 |
286 /* This is a reasonable definition for this new primitive. Kyle sez: | |
287 | |
288 "The patch looks OK to me except for the creation and exporting of the | |
289 Fprocess_readable_p function. I don't think a new Lisp function | |
290 should be created until we know something actually needs it. If | |
291 we later want to give process-readable-p different semantics it | |
292 may be hard to do it and stay compatible with what we hastily | |
293 create today." | |
294 | |
295 He's right, not yet. Let's discuss the semantics on XEmacs Design | |
296 before enabling this. | |
297 */ | |
298 DEFUN ("process-readable-p", Fprocess_readable_p, 1, 1, 0, /* | |
299 Return t if OBJECT is a process from which input may be available. | |
300 */ | |
301 (object)) | |
302 { | |
303 return PROCESSP (object) && PROCESS_READABLE_P (XPROCESS (object)) | |
304 ? Qt : Qnil; | |
305 } | |
306 #endif | |
307 | |
428 | 308 DEFUN ("process-list", Fprocess_list, 0, 0, 0, /* |
309 Return a list of all processes. | |
310 */ | |
311 ()) | |
312 { | |
313 return Fcopy_sequence (Vprocess_list); | |
314 } | |
315 | |
316 DEFUN ("get-process", Fget_process, 1, 1, 0, /* | |
444 | 317 Return the process named PROCESS-NAME (a string), or nil if there is none. |
318 PROCESS-NAME may also be a process; if so, the value is that process. | |
428 | 319 */ |
444 | 320 (process_name)) |
428 | 321 { |
444 | 322 if (PROCESSP (process_name)) |
323 return process_name; | |
428 | 324 |
325 if (!gc_in_progress) | |
326 /* this only gets called during GC when emacs is going away as a result | |
327 of a signal or crash. */ | |
444 | 328 CHECK_STRING (process_name); |
428 | 329 |
444 | 330 { |
331 LIST_LOOP_2 (process, Vprocess_list) | |
332 if (internal_equal (process_name, XPROCESS (process)->name, 0)) | |
333 return process; | |
334 } | |
428 | 335 return Qnil; |
336 } | |
337 | |
338 DEFUN ("get-buffer-process", Fget_buffer_process, 1, 1, 0, /* | |
339 Return the (or, a) process associated with BUFFER. | |
340 BUFFER may be a buffer or the name of one. | |
341 */ | |
444 | 342 (buffer)) |
428 | 343 { |
444 | 344 if (NILP (buffer)) return Qnil; |
345 buffer = Fget_buffer (buffer); | |
346 if (NILP (buffer)) return Qnil; | |
428 | 347 |
444 | 348 { |
349 LIST_LOOP_2 (process, Vprocess_list) | |
350 if (EQ (XPROCESS (process)->buffer, buffer)) | |
351 return process; | |
352 } | |
428 | 353 return Qnil; |
354 } | |
355 | |
356 /* This is how commands for the user decode process arguments. It | |
357 accepts a process, a process name, a buffer, a buffer name, or nil. | |
358 Buffers denote the first process in the buffer, and nil denotes the | |
359 current buffer. */ | |
360 | |
361 static Lisp_Object | |
362 get_process (Lisp_Object name) | |
363 { | |
444 | 364 Lisp_Object buffer; |
428 | 365 |
366 #ifdef I18N3 | |
367 /* #### Look more closely into translating process names. */ | |
368 #endif | |
369 | |
370 /* This may be called during a GC from process_send_signal() from | |
2500 | 371 kill_buffer_processes() if emacs decides to ABORT(). */ |
428 | 372 if (PROCESSP (name)) |
373 return name; | |
444 | 374 else if (STRINGP (name)) |
428 | 375 { |
444 | 376 Lisp_Object object = Fget_process (name); |
377 if (PROCESSP (object)) | |
378 return object; | |
379 | |
380 buffer = Fget_buffer (name); | |
381 if (BUFFERP (buffer)) | |
382 goto have_buffer_object; | |
383 | |
563 | 384 invalid_argument ("Process does not exist", name); |
428 | 385 } |
386 else if (NILP (name)) | |
444 | 387 { |
388 buffer = Fcurrent_buffer (); | |
389 goto have_buffer_object; | |
390 } | |
391 else if (BUFFERP (name)) | |
392 { | |
393 Lisp_Object process; | |
394 buffer = name; | |
428 | 395 |
444 | 396 have_buffer_object: |
397 process = Fget_buffer_process (buffer); | |
398 if (PROCESSP (process)) | |
399 return process; | |
400 | |
563 | 401 invalid_argument ("Buffer has no process", buffer); |
428 | 402 } |
403 else | |
444 | 404 return get_process (Fsignal (Qwrong_type_argument, |
771 | 405 (list2 (build_msg_string ("process or buffer or nil"), |
444 | 406 name)))); |
428 | 407 } |
408 | |
409 DEFUN ("process-id", Fprocess_id, 1, 1, 0, /* | |
410 Return the process id of PROCESS. | |
411 This is the pid of the Unix process which PROCESS uses or talks to. | |
412 For a network connection, this value is a cons of | |
413 (foreign-network-port . foreign-host-name). | |
414 */ | |
444 | 415 (process)) |
428 | 416 { |
417 Lisp_Object pid; | |
444 | 418 CHECK_PROCESS (process); |
428 | 419 |
444 | 420 pid = XPROCESS (process)->pid; |
421 if (network_connection_p (process)) | |
428 | 422 /* return Qnil; */ |
423 return Fcons (Fcar (pid), Fcdr (pid)); | |
424 else | |
425 return pid; | |
426 } | |
427 | |
428 DEFUN ("process-name", Fprocess_name, 1, 1, 0, /* | |
429 Return the name of PROCESS, as a string. | |
430 This is the name of the program invoked in PROCESS, | |
431 possibly modified to make it unique among process names. | |
432 */ | |
444 | 433 (process)) |
428 | 434 { |
444 | 435 CHECK_PROCESS (process); |
436 return XPROCESS (process)->name; | |
428 | 437 } |
438 | |
439 DEFUN ("process-command", Fprocess_command, 1, 1, 0, /* | |
440 Return the command that was executed to start PROCESS. | |
441 This is a list of strings, the first string being the program executed | |
442 and the rest of the strings being the arguments given to it. | |
443 */ | |
444 | 444 (process)) |
428 | 445 { |
444 | 446 CHECK_PROCESS (process); |
447 return XPROCESS (process)->command; | |
428 | 448 } |
449 | |
450 | |
451 /************************************************************************/ | |
452 /* creating a process */ | |
453 /************************************************************************/ | |
454 | |
563 | 455 DOESNT_RETURN |
456 report_process_error (const char *string, Lisp_Object data) | |
457 { | |
458 report_error_with_errno (Qprocess_error, string, data); | |
459 } | |
460 | |
461 DOESNT_RETURN | |
462 report_network_error (const char *string, Lisp_Object data) | |
463 { | |
464 report_error_with_errno (Qnetwork_error, string, data); | |
465 } | |
466 | |
428 | 467 Lisp_Object |
468 make_process_internal (Lisp_Object name) | |
469 { | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
470 Lisp_Object name1; |
428 | 471 int i; |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
472 Lisp_Object obj = ALLOC_LISP_OBJECT (process); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
473 Lisp_Process *p = XPROCESS (obj); |
428 | 474 |
1204 | 475 #define MARKED_SLOT(x) p->x = Qnil; |
476 #include "process-slots.h" | |
477 | |
428 | 478 /* If name is already in use, modify it until it is unused. */ |
479 name1 = name; | |
480 for (i = 1; ; i++) | |
481 { | |
482 char suffix[10]; | |
483 Lisp_Object tem = Fget_process (name1); | |
484 if (NILP (tem)) | |
485 break; | |
486 sprintf (suffix, "<%d>", i); | |
487 name1 = concat2 (name, build_string (suffix)); | |
488 } | |
489 name = name1; | |
490 p->name = name; | |
491 | |
492 p->mark = Fmake_marker (); | |
853 | 493 p->stderr_mark = Fmake_marker (); |
428 | 494 p->status_symbol = Qrun; |
495 | |
496 MAYBE_PROCMETH (alloc_process_data, (p)); | |
497 | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
498 Vprocess_list = Fcons (obj, Vprocess_list); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
499 return obj; |
428 | 500 } |
501 | |
502 void | |
853 | 503 init_process_io_handles (Lisp_Process *p, void* in, void* out, void* err, |
504 int flags) | |
428 | 505 { |
853 | 506 USID in_usid, err_usid; |
771 | 507 Lisp_Object incode, outcode; |
508 | |
853 | 509 if (flags & STREAM_NETWORK_CONNECTION) |
510 { | |
4729
428d7c571110
Fix issue145: accept nil in default-process-coding-system.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4693
diff
changeset
|
511 if (!LISTP (Vdefault_network_coding_system) || |
853 | 512 NILP (incode = (find_coding_system_for_text_file |
513 (Fcar (Vdefault_network_coding_system), 1))) || | |
514 NILP (outcode = (find_coding_system_for_text_file | |
515 (Fcdr (Vdefault_network_coding_system), 0)))) | |
516 signal_error (Qinvalid_state, | |
517 "Bogus value for `default-network-coding-system'", | |
518 Vdefault_network_coding_system); | |
519 } | |
520 else | |
521 { | |
4729
428d7c571110
Fix issue145: accept nil in default-process-coding-system.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4693
diff
changeset
|
522 if (!LISTP (Vdefault_process_coding_system) || |
853 | 523 NILP (incode = (find_coding_system_for_text_file |
524 (Fcar (Vdefault_process_coding_system), 1))) || | |
525 NILP (outcode = (find_coding_system_for_text_file | |
526 (Fcdr (Vdefault_process_coding_system), 0)))) | |
527 signal_error (Qinvalid_state, | |
528 "Bogus value for `default-process-coding-system'", | |
529 Vdefault_process_coding_system); | |
530 } | |
771 | 531 |
784 | 532 if (!NILP (Vcoding_system_for_read) && |
533 NILP (incode = (find_coding_system_for_text_file | |
534 (Vcoding_system_for_read, 1)))) | |
535 signal_error (Qinvalid_state, | |
536 "Bogus value for `coding-system-for-read'", | |
537 Vcoding_system_for_read); | |
538 | |
539 if (!NILP (Vcoding_system_for_write) && | |
540 NILP (outcode = (find_coding_system_for_text_file | |
541 (Vcoding_system_for_write, 0)))) | |
542 signal_error (Qinvalid_state, | |
543 "Bogus value for `coding-system-for-write'", | |
544 Vcoding_system_for_write); | |
545 | |
853 | 546 event_stream_create_io_streams (in, out, err, |
547 &p->pipe_instream, | |
548 &p->pipe_outstream, | |
549 &p->pipe_errstream, | |
550 &in_usid, &err_usid, | |
551 flags); | |
428 | 552 |
853 | 553 if (in_usid == USID_ERROR || err_usid == USID_ERROR) |
563 | 554 signal_error (Qprocess_error, "Setting up communication with subprocess", |
853 | 555 wrap_process (p)); |
428 | 556 |
853 | 557 if (in_usid != USID_DONTHASH) |
428 | 558 { |
444 | 559 Lisp_Object process = Qnil; |
793 | 560 process = wrap_process (p); |
853 | 561 puthash ((const void*) in_usid, LISP_TO_VOID (process), usid_to_process); |
428 | 562 } |
563 | |
853 | 564 if (err_usid != USID_DONTHASH) |
565 { | |
566 Lisp_Object process = Qnil; | |
567 process = wrap_process (p); | |
568 puthash ((const void*) err_usid, LISP_TO_VOID (process), | |
569 usid_to_process); | |
570 } | |
571 | |
572 MAYBE_PROCMETH (init_process_io_handles, (p, in, out, err, flags)); | |
428 | 573 |
771 | 574 p->coding_instream = |
800 | 575 make_coding_input_stream (XLSTREAM (p->pipe_instream), incode, |
576 CODING_DECODE, 0); | |
853 | 577 if (!NILP (p->pipe_errstream)) |
578 p->coding_errstream = | |
579 make_coding_input_stream | |
580 (XLSTREAM (p->pipe_errstream), incode, CODING_DECODE, 0); | |
771 | 581 p->coding_outstream = |
800 | 582 make_coding_output_stream (XLSTREAM (p->pipe_outstream), outcode, |
583 CODING_ENCODE, 0); | |
428 | 584 } |
585 | |
586 static void | |
587 create_process (Lisp_Object process, Lisp_Object *argv, int nargv, | |
853 | 588 Lisp_Object program, Lisp_Object cur_dir, |
589 int separate_err) | |
428 | 590 { |
440 | 591 Lisp_Process *p = XPROCESS (process); |
428 | 592 int pid; |
593 | |
594 /* *_create_process may change status_symbol, if the process | |
595 is a kind of "fire-and-forget" (no I/O, unwaitable) */ | |
596 p->status_symbol = Qrun; | |
597 p->exit_code = 0; | |
598 | |
853 | 599 pid = PROCMETH (create_process, (p, argv, nargv, program, cur_dir, |
600 separate_err)); | |
428 | 601 |
602 p->pid = make_int (pid); | |
863 | 603 if (PROCESS_READABLE_P (p)) |
853 | 604 event_stream_select_process (p, 1, 1); |
428 | 605 } |
606 | |
607 /* This function is the unwind_protect form for Fstart_process_internal. If | |
444 | 608 PROCESS doesn't have its pid set, then we know someone has signalled |
428 | 609 an error and the process wasn't started successfully, so we should |
610 remove it from the process list. */ | |
444 | 611 static void remove_process (Lisp_Object process); |
428 | 612 static Lisp_Object |
444 | 613 start_process_unwind (Lisp_Object process) |
428 | 614 { |
444 | 615 /* Was PROCESS started successfully? */ |
616 if (EQ (XPROCESS (process)->pid, Qnil)) | |
617 remove_process (process); | |
428 | 618 return Qnil; |
619 } | |
620 | |
621 DEFUN ("start-process-internal", Fstart_process_internal, 3, MANY, 0, /* | |
853 | 622 Internal function to start a program in a subprocess. |
623 Lisp callers should use `start-process' instead. | |
624 | |
625 Returns the process object for it. | |
428 | 626 Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS |
627 NAME is name for process. It is modified if necessary to make it unique. | |
628 BUFFER is the buffer or (buffer-name) to associate with the process. | |
629 Process output goes at end of that buffer, unless you specify | |
630 an output stream or filter function to handle the output. | |
631 BUFFER may be also nil, meaning that this process is not associated | |
853 | 632 with any buffer. |
633 BUFFER can also have the form (REAL-BUFFER STDERR-BUFFER); in that case, | |
634 REAL-BUFFER says what to do with standard output, as above, | |
635 while STDERR-BUFFER says what to do with standard error in the child. | |
636 STDERR-BUFFER may be nil (discard standard error output, unless a stderr | |
637 filter is set). Note that if you do not use this form at process creation, | |
638 stdout and stderr will be mixed in the output buffer, and this cannot be | |
639 changed, even by setting a stderr filter. | |
428 | 640 Third arg is program file name. It is searched for as in the shell. |
641 Remaining arguments are strings to give program as arguments. | |
853 | 642 |
643 Read and write coding systems for the process are determined from | |
644 `coding-system-for-read' and `coding-system-for-write' (intended as | |
645 overriding coding systems to be *bound* by Lisp code, not set), or | |
646 from `default-process-coding-system' if either or both are nil. You can | |
647 change the coding systems later on using `set-process-coding-system', | |
648 `set-process-input-coding-system', or `set-process-output-coding-system'. | |
649 | |
650 See also `set-process-filter' and `set-process-stderr-filter'. | |
4693
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3025
diff
changeset
|
651 |
80cd90837ac5
Add argument information to remaining MANY or UNEVALLED C subrs.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3025
diff
changeset
|
652 arguments: (NAME BUFFER PROGRAM &rest PROGRAM-ARGS) |
428 | 653 */ |
654 (int nargs, Lisp_Object *args)) | |
655 { | |
656 /* This function can call lisp */ | |
853 | 657 Lisp_Object buffer, stderr_buffer, name, program, process, current_dir; |
658 int separate_stderr; | |
428 | 659 Lisp_Object tem; |
910 | 660 int i; |
428 | 661 int speccount = specpdl_depth (); |
662 struct gcpro gcpro1, gcpro2, gcpro3; | |
663 | |
664 name = args[0]; | |
665 buffer = args[1]; | |
666 program = args[2]; | |
667 current_dir = Qnil; | |
668 | |
669 /* Protect against various file handlers doing GCs below. */ | |
670 GCPRO3 (buffer, program, current_dir); | |
671 | |
853 | 672 if (CONSP (buffer)) |
673 { | |
674 if (!CONSP (XCDR (buffer))) | |
675 invalid_argument ("Invalid BUFFER argument to `start-process'", | |
676 buffer); | |
677 if (!NILP (XCDR (XCDR (buffer)))) | |
678 invalid_argument ("Invalid BUFFER argument to `start-process'", | |
679 buffer); | |
680 stderr_buffer = XCAR (XCDR (buffer)); | |
681 buffer = XCAR (buffer); | |
682 separate_stderr = 1; | |
683 } | |
684 else | |
685 { | |
686 stderr_buffer = Qnil; | |
687 separate_stderr = 0; | |
688 } | |
689 | |
428 | 690 if (!NILP (buffer)) |
691 buffer = Fget_buffer_create (buffer); | |
853 | 692 if (!NILP (stderr_buffer)) |
693 stderr_buffer = Fget_buffer_create (stderr_buffer); | |
428 | 694 |
695 CHECK_STRING (name); | |
696 CHECK_STRING (program); | |
910 | 697 for (i = 3; i < nargs; ++i) |
698 CHECK_STRING (args[i]); | |
428 | 699 |
700 /* Make sure that the child will be able to chdir to the current | |
502 | 701 buffer's current directory, or its unhandled equivalent. [[ We |
428 | 702 can't just have the child check for an error when it does the |
502 | 703 chdir, since it's in a vfork. ]] -- not any more, we don't use |
704 vfork. -ben | |
428 | 705 |
502 | 706 Note: These calls are spread out to insure that the return values |
707 of the calls (which may be newly-created strings) are properly | |
708 GC-protected. */ | |
428 | 709 current_dir = current_buffer->directory; |
502 | 710 /* If the current dir has no terminating slash, we'll get undesirable |
711 results, so put the slash back. */ | |
712 current_dir = Ffile_name_as_directory (current_dir); | |
428 | 713 current_dir = Funhandled_file_name_directory (current_dir); |
714 current_dir = expand_and_dir_to_file (current_dir, Qnil); | |
715 | |
716 #if 0 /* This loser breaks ange-ftp */ | |
717 /* dmoore - if you re-enable this code, you have to gcprotect | |
718 current_buffer through the above calls. */ | |
719 if (NILP (Ffile_accessible_directory_p (current_dir))) | |
563 | 720 signal_error (Qprocess_error, "Setting current directory", |
721 current_buffer->directory); | |
428 | 722 #endif /* 0 */ |
723 | |
724 /* If program file name is not absolute, search our path for it */ | |
826 | 725 if (!IS_DIRECTORY_SEP (string_byte (program, 0)) |
428 | 726 && !(XSTRING_LENGTH (program) > 1 |
826 | 727 && IS_DEVICE_SEP (string_byte (program, 1)))) |
428 | 728 { |
729 struct gcpro ngcpro1; | |
730 | |
731 tem = Qnil; | |
732 NGCPRO1 (tem); | |
733 locate_file (Vexec_path, program, Vlisp_EXEC_SUFFIXES, &tem, X_OK); | |
734 if (NILP (tem)) | |
563 | 735 signal_error (Qprocess_error, "Searching for program", program); |
428 | 736 program = Fexpand_file_name (tem, Qnil); |
737 NUNGCPRO; | |
738 } | |
739 else | |
740 { | |
442 | 741 /* we still need to canonicalize it and ensure it has the proper |
742 ending, e.g. .exe */ | |
743 struct gcpro ngcpro1; | |
744 | |
745 tem = Qnil; | |
746 NGCPRO1 (tem); | |
747 locate_file (list1 (build_string ("")), program, Vlisp_EXEC_SUFFIXES, | |
748 &tem, X_OK); | |
749 if (NILP (tem)) | |
563 | 750 signal_error (Qprocess_error, "Searching for program", program); |
442 | 751 program = tem; |
752 NUNGCPRO; | |
428 | 753 } |
754 | |
442 | 755 if (!NILP (Ffile_directory_p (program))) |
756 invalid_operation ("Specified program for new process is a directory", | |
757 program); | |
758 | |
444 | 759 process = make_process_internal (name); |
428 | 760 |
444 | 761 XPROCESS (process)->buffer = buffer; |
853 | 762 XPROCESS (process)->stderr_buffer = stderr_buffer; |
763 XPROCESS (process)->separate_stderr = separate_stderr; | |
814 | 764 XPROCESS (process)->command = Flist (nargs - 2, args + 2); |
428 | 765 |
766 /* Make the process marker point into the process buffer (if any). */ | |
767 if (!NILP (buffer)) | |
444 | 768 Fset_marker (XPROCESS (process)->mark, |
428 | 769 make_int (BUF_ZV (XBUFFER (buffer))), buffer); |
853 | 770 if (!NILP (stderr_buffer)) |
771 Fset_marker (XPROCESS (process)->stderr_mark, | |
772 make_int (BUF_ZV (XBUFFER (stderr_buffer))), stderr_buffer); | |
428 | 773 |
774 /* If an error occurs and we can't start the process, we want to | |
775 remove it from the process list. This means that each error | |
776 check in create_process doesn't need to call remove_process | |
777 itself; it's all taken care of here. */ | |
444 | 778 record_unwind_protect (start_process_unwind, process); |
428 | 779 |
853 | 780 create_process (process, args + 3, nargs - 3, program, current_dir, |
781 separate_stderr); | |
428 | 782 |
783 UNGCPRO; | |
771 | 784 return unbind_to_1 (speccount, process); |
428 | 785 } |
786 | |
787 | |
788 #ifdef HAVE_SOCKETS | |
789 | |
790 | |
791 /* #### The network support is fairly synthetical. What we actually | |
792 need is a single function, which supports all datagram, stream and | |
793 packet stream connections, arbitrary protocol families should they | |
794 be supported by the target system, multicast groups, in both data | |
795 and control rooted/nonrooted flavors, service quality etc whatever | |
796 is supported by the underlying network. | |
797 | |
798 It must accept a property list describing the connection. The current | |
799 functions must then go to lisp and provide a suitable list for the | |
800 generalized connection function. | |
801 | |
802 Both UNIX and Win32 support BSD sockets, and there are many extensions | |
803 available (Sockets 2 spec). | |
804 | |
805 A todo is define a consistent set of properties abstracting a | |
806 network connection. -kkm | |
807 */ | |
808 | |
809 | |
810 /* open a TCP network connection to a given HOST/SERVICE. Treated | |
811 exactly like a normal process when reading and writing. Only | |
812 differences are in status display and process deletion. A network | |
813 connection has no PID; you cannot signal it. All you can do is | |
814 deactivate and close it via delete-process */ | |
815 | |
442 | 816 DEFUN ("open-network-stream-internal", Fopen_network_stream_internal, 4, 5, |
817 0, /* | |
428 | 818 Open a TCP connection for a service to a host. |
444 | 819 Return a process object to represent the connection. |
428 | 820 Input and output work as for subprocesses; `delete-process' closes it. |
821 | |
822 NAME is name for process. It is modified if necessary to make it unique. | |
823 BUFFER is the buffer (or buffer-name) to associate with the process. | |
824 Process output goes at end of that buffer, unless you specify | |
825 an output stream or filter function to handle the output. | |
826 BUFFER may also be nil, meaning that this process is not associated | |
827 with any buffer. | |
444 | 828 Third arg HOST (a string) is the name of the host to connect to, |
829 or its IP address. | |
830 Fourth arg SERVICE is the name of the service desired (a string), | |
831 or an integer specifying a port number to connect to. | |
3025 | 832 Optional fifth arg PROTOCOL is a network protocol. Currently only `tcp' |
833 (Transmission Control Protocol) and `udp' (User Datagram Protocol) are | |
834 supported. When omitted, `tcp' is assumed. | |
428 | 835 |
442 | 836 Output via `process-send-string' and input via buffer or filter (see |
428 | 837 `set-process-filter') are stream-oriented. That means UDP datagrams are |
838 not guaranteed to be sent and received in discrete packets. (But small | |
839 datagrams around 500 bytes that are not truncated by `process-send-string' | |
444 | 840 are usually fine.) Note further that the UDP protocol does not guard |
841 against lost packets. | |
428 | 842 */ |
843 (name, buffer, host, service, protocol)) | |
844 { | |
845 /* This function can GC */ | |
444 | 846 Lisp_Object process = Qnil; |
428 | 847 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, ngcpro1; |
848 void *inch, *outch; | |
849 | |
850 GCPRO5 (name, buffer, host, service, protocol); | |
851 CHECK_STRING (name); | |
852 | |
771 | 853 if (NILP (protocol)) |
428 | 854 protocol = Qtcp; |
855 else | |
856 CHECK_SYMBOL (protocol); | |
857 | |
858 /* Since this code is inside HAVE_SOCKETS, existence of | |
859 open_network_stream is mandatory */ | |
860 PROCMETH (open_network_stream, (name, host, service, protocol, | |
861 &inch, &outch)); | |
862 | |
863 if (!NILP (buffer)) | |
864 buffer = Fget_buffer_create (buffer); | |
444 | 865 process = make_process_internal (name); |
866 NGCPRO1 (process); | |
428 | 867 |
444 | 868 XPROCESS (process)->pid = Fcons (service, host); |
869 XPROCESS (process)->buffer = buffer; | |
771 | 870 init_process_io_handles (XPROCESS (process), (void *) inch, (void *) outch, |
853 | 871 (void *) -1, |
428 | 872 STREAM_NETWORK_CONNECTION); |
873 | |
853 | 874 event_stream_select_process (XPROCESS (process), 1, 1); |
428 | 875 |
1204 | 876 NUNGCPRO; |
428 | 877 UNGCPRO; |
444 | 878 return process; |
428 | 879 } |
880 | |
881 #ifdef HAVE_MULTICAST | |
882 | |
883 DEFUN ("open-multicast-group-internal", Fopen_multicast_group_internal, 5, 5, 0, /* | |
884 Open a multicast connection on the specified dest/port/ttl. | |
444 | 885 Return a process object to represent the connection. |
428 | 886 Input and output work as for subprocesses; `delete-process' closes it. |
887 | |
888 NAME is name for process. It is modified if necessary to make it unique. | |
889 BUFFER is the buffer (or buffer-name) to associate with the process. | |
890 Process output goes at end of that buffer, unless you specify | |
891 an output stream or filter function to handle the output. | |
892 BUFFER may also be nil, meaning that this process is not associated | |
893 with any buffer. | |
894 Third, fourth and fifth args are the multicast destination group, port and ttl. | |
895 dest must be an internet address between 224.0.0.0 and 239.255.255.255 | |
896 port is a communication port like in traditional unicast | |
897 ttl is the time-to-live (15 for site, 63 for region and 127 for world) | |
898 */ | |
899 (name, buffer, dest, port, ttl)) | |
900 { | |
901 /* This function can GC */ | |
444 | 902 Lisp_Object process = Qnil; |
428 | 903 struct gcpro gcpro1; |
904 void *inch, *outch; | |
905 | |
906 CHECK_STRING (name); | |
907 | |
908 /* Since this code is inside HAVE_MULTICAST, existence of | |
771 | 909 open_multicast_group is mandatory */ |
428 | 910 PROCMETH (open_multicast_group, (name, dest, port, ttl, |
911 &inch, &outch)); | |
912 | |
913 if (!NILP (buffer)) | |
914 buffer = Fget_buffer_create (buffer); | |
915 | |
444 | 916 process = make_process_internal (name); |
917 GCPRO1 (process); | |
428 | 918 |
444 | 919 XPROCESS (process)->pid = Fcons (port, dest); |
920 XPROCESS (process)->buffer = buffer; | |
853 | 921 init_process_io_handles (XPROCESS (process), (void *) inch, (void *) outch, |
922 (void *) -1, | |
428 | 923 STREAM_NETWORK_CONNECTION); |
924 | |
853 | 925 event_stream_select_process (XPROCESS (process), 1, 1); |
428 | 926 |
927 UNGCPRO; | |
444 | 928 return process; |
428 | 929 } |
930 #endif /* HAVE_MULTICAST */ | |
931 | |
932 #endif /* HAVE_SOCKETS */ | |
933 | |
934 Lisp_Object | |
935 canonicalize_host_name (Lisp_Object host) | |
936 { | |
937 return PROCMETH_OR_GIVEN (canonicalize_host_name, (host), host); | |
938 } | |
939 | |
940 | |
941 DEFUN ("set-process-window-size", Fset_process_window_size, 3, 3, 0, /* | |
942 Tell PROCESS that it has logical window size HEIGHT and WIDTH. | |
943 */ | |
444 | 944 (process, height, width)) |
428 | 945 { |
444 | 946 CHECK_PROCESS (process); |
428 | 947 CHECK_NATNUM (height); |
948 CHECK_NATNUM (width); | |
949 return | |
444 | 950 MAYBE_INT_PROCMETH (set_window_size, |
951 (XPROCESS (process), XINT (height), XINT (width))) <= 0 | |
428 | 952 ? Qnil : Qt; |
953 } | |
954 | |
955 | |
956 /************************************************************************/ | |
957 /* Process I/O */ | |
958 /************************************************************************/ | |
959 | |
844 | 960 /* Set up PROCESS's buffer for insertion of process data at PROCESS's |
961 mark. | |
962 | |
963 Sets the current buffer to PROCESS's buffer, inhibits read only, | |
964 remembers current point, sets point to PROCESS'S mark, widens if | |
965 necessary. | |
966 */ | |
967 static int | |
853 | 968 process_setup_for_insertion (Lisp_Object process, int read_stderr) |
844 | 969 { |
970 Lisp_Process *p = XPROCESS (process); | |
971 int spec = specpdl_depth (); | |
853 | 972 Lisp_Object buffer = read_stderr ? p->stderr_buffer : p->buffer; |
973 Lisp_Object mark = read_stderr ? p->stderr_mark : p->mark; | |
974 struct buffer *buf = XBUFFER (buffer); | |
844 | 975 Charbpos output_pt; |
976 | |
977 if (buf != current_buffer) | |
978 { | |
979 record_unwind_protect (save_current_buffer_restore, | |
980 Fcurrent_buffer ()); | |
981 set_buffer_internal (buf); | |
982 } | |
983 | |
984 record_unwind_protect (save_excursion_restore, save_excursion_save ()); | |
985 specbind (Qinhibit_read_only, Qt); | |
854 | 986 |
844 | 987 /* Insert new output into buffer |
988 at the current end-of-output marker, | |
989 thus preserving logical ordering of input and output. */ | |
853 | 990 if (XMARKER (mark)->buffer) |
991 output_pt = marker_position (mark); | |
844 | 992 else |
993 output_pt = BUF_ZV (buf); | |
994 | |
995 /* If the output marker is outside of the visible region, save | |
996 the restriction and widen. */ | |
997 if (! (BUF_BEGV (buf) <= output_pt && output_pt <= BUF_ZV (buf))) | |
998 { | |
999 record_unwind_protect (save_restriction_restore, | |
1000 save_restriction_save (buf)); | |
1001 Fwiden (wrap_buffer (buf)); | |
1002 } | |
1003 | |
1004 BUF_SET_PT (buf, output_pt); | |
1005 return spec; | |
1006 } | |
1007 | |
428 | 1008 /* Read pending output from the process channel, |
1009 starting with our buffered-ahead character if we have one. | |
1010 Yield number of characters read. | |
1011 | |
1012 This function reads at most 1024 bytes. | |
1013 If you want to read all available subprocess output, | |
1014 you must call it repeatedly until it returns zero. */ | |
1015 | |
1016 Charcount | |
853 | 1017 read_process_output (Lisp_Object process, int read_stderr) |
428 | 1018 { |
1019 /* This function can GC */ | |
1020 Bytecount nbytes, nchars; | |
867 | 1021 Ibyte chars[1025]; |
428 | 1022 Lisp_Object outstream; |
444 | 1023 Lisp_Process *p = XPROCESS (process); |
853 | 1024 Lisp_Object filter = read_stderr ? p->stderr_filter : p->filter; |
1025 Lisp_Object buffer = read_stderr ? p->stderr_buffer : p->buffer; | |
1026 Lisp_Object mark = read_stderr ? p->stderr_mark : p->mark; | |
428 | 1027 |
1028 /* If there is a lot of output from the subprocess, the loop in | |
1029 execute_internal_event() might call read_process_output() more | |
1030 than once. If the filter that was executed from one of these | |
1031 calls set the filter to t, we have to stop now. Return -1 rather | |
1032 than 0 so execute_internal_event() doesn't close the process. | |
1033 Really, the loop in execute_internal_event() should check itself | |
1034 for a process-filter change, like in status_notify(); but the | |
1035 struct Lisp_Process is not exported outside of this file. */ | |
863 | 1036 if (!PROCESS_READABLE_P (p)) |
853 | 1037 { |
1038 errno = 0; | |
1039 return -1; /* already closed */ | |
1040 } | |
428 | 1041 |
853 | 1042 if (!NILP (filter) && (p->filter_does_read)) |
428 | 1043 { |
1044 Lisp_Object filter_result; | |
1045 | |
1046 /* Some weird FSFmacs crap here with | |
853 | 1047 Vdeactivate_mark and current_buffer->keymap. |
1048 Some FSF junk with running_asynch_code, to preserve the match | |
1049 data. Not necessary because we don't call process filters | |
1050 asynchronously (i.e. from within QUIT). */ | |
1051 /* Don't catch errors here; we're not in any critical code. */ | |
1052 filter_result = call2 (filter, process, Qnil); | |
428 | 1053 CHECK_INT (filter_result); |
1054 return XINT (filter_result); | |
1055 } | |
1056 | |
853 | 1057 nbytes = Lstream_read (read_stderr ? XLSTREAM (DATA_ERRSTREAM (p)) : |
1058 XLSTREAM (DATA_INSTREAM (p)), chars, | |
771 | 1059 sizeof (chars) - 1); |
428 | 1060 if (nbytes <= 0) return nbytes; |
1061 | |
771 | 1062 if (debug_process_io) |
1063 { | |
1064 chars[nbytes] = '\0'; | |
1065 stderr_out ("Read: %s\n", chars); | |
1066 } | |
1067 | |
1068 /* !!#### if the coding system changed as a result of reading, we | |
1069 need to change the output coding system accordingly. */ | |
428 | 1070 nchars = bytecount_to_charcount (chars, nbytes); |
853 | 1071 outstream = filter; |
428 | 1072 if (!NILP (outstream)) |
1073 { | |
853 | 1074 /* Some FSF junk with running_asynch_code, to preserve the match |
1075 data. Not necessary because we don't call process filters | |
1076 asynchronously (i.e. from within QUIT). */ | |
1077 /* Don't catch errors here; we're not in any critical code. */ | |
1078 call2 (outstream, process, make_string (chars, nbytes)); | |
428 | 1079 return nchars; |
1080 } | |
1081 | |
1082 /* If no filter, write into buffer if it isn't dead. */ | |
853 | 1083 if (!NILP (buffer) && BUFFER_LIVE_P (XBUFFER (buffer))) |
428 | 1084 { |
844 | 1085 struct gcpro gcpro1; |
853 | 1086 struct buffer *buf = XBUFFER (buffer); |
1087 int spec = process_setup_for_insertion (process, read_stderr); | |
428 | 1088 |
844 | 1089 GCPRO1 (process); |
428 | 1090 |
1091 #if 0 | |
1092 /* This screws up initial display of the window. jla */ | |
1093 | |
1094 /* Insert before markers in case we are inserting where | |
1095 the buffer's mark is, and the user's next command is Meta-y. */ | |
1096 buffer_insert_raw_string_1 (buf, -1, chars, | |
1097 nbytes, INSDEL_BEFORE_MARKERS); | |
1098 #else | |
1099 buffer_insert_raw_string (buf, chars, nbytes); | |
1100 #endif | |
1101 | |
853 | 1102 Fset_marker (mark, make_int (BUF_PT (buf)), buffer); |
1103 | |
428 | 1104 MARK_MODELINE_CHANGED; |
844 | 1105 unbind_to (spec); |
428 | 1106 UNGCPRO; |
1107 } | |
1108 return nchars; | |
1109 } | |
853 | 1110 |
1111 int | |
1112 process_has_separate_stderr (Lisp_Object process) | |
1113 { | |
1114 return XPROCESS (process)->separate_stderr; | |
1115 } | |
1116 | |
859 | 1117 DEFUN ("process-has-separate-stderr-p", Fprocess_has_separate_stderr_p, 1, 1, |
1118 0, /* | |
1119 Return non-nil if process has stderr separate from stdout. | |
1120 */ | |
1121 (process)) | |
1122 { | |
1123 CHECK_PROCESS (process); | |
1124 return process_has_separate_stderr (process) ? Qt : Qnil; | |
1125 } | |
1126 | |
428 | 1127 |
1128 /* Sending data to subprocess */ | |
1129 | |
444 | 1130 /* send some data to process PROCESS. If NONRELOCATABLE is non-NULL, it |
428 | 1131 specifies the address of the data. Otherwise, the data comes from the |
1132 object RELOCATABLE (either a string or a buffer). START and LEN | |
1133 specify the offset and length of the data to send. | |
1134 | |
665 | 1135 Note that START and LEN are in Charbpos's if RELOCATABLE is a buffer, |
428 | 1136 and in Bytecounts otherwise. */ |
1137 | |
1138 void | |
444 | 1139 send_process (Lisp_Object process, |
867 | 1140 Lisp_Object relocatable, const Ibyte *nonrelocatable, |
428 | 1141 int start, int len) |
1142 { | |
1143 /* This function can GC */ | |
1144 struct gcpro gcpro1, gcpro2; | |
1145 Lisp_Object lstream = Qnil; | |
1146 | |
444 | 1147 GCPRO2 (process, lstream); |
428 | 1148 |
444 | 1149 if (NILP (DATA_OUTSTREAM (XPROCESS (process)))) |
563 | 1150 invalid_operation ("Process not open for writing", process); |
428 | 1151 |
1152 if (nonrelocatable) | |
1153 lstream = | |
1154 make_fixed_buffer_input_stream (nonrelocatable + start, len); | |
1155 else if (BUFFERP (relocatable)) | |
1156 lstream = make_lisp_buffer_input_stream (XBUFFER (relocatable), | |
1157 start, start + len, 0); | |
1158 else | |
1159 lstream = make_lisp_string_input_stream (relocatable, start, len); | |
1160 | |
771 | 1161 if (debug_process_io) |
1162 { | |
1163 if (nonrelocatable) | |
1164 stderr_out ("Writing: %s\n", nonrelocatable); | |
1165 else | |
1166 { | |
1167 stderr_out ("Writing: "); | |
1168 print_internal (relocatable, Qexternal_debugging_output, 0); | |
1169 } | |
1170 } | |
1171 | |
444 | 1172 PROCMETH (send_process, (process, XLSTREAM (lstream))); |
428 | 1173 |
1174 UNGCPRO; | |
1175 Lstream_delete (XLSTREAM (lstream)); | |
1176 } | |
1177 | |
1178 DEFUN ("process-tty-name", Fprocess_tty_name, 1, 1, 0, /* | |
1179 Return the name of the terminal PROCESS uses, or nil if none. | |
1180 This is the terminal that the process itself reads and writes on, | |
1181 not the name of the pty that Emacs uses to talk with that terminal. | |
1182 */ | |
444 | 1183 (process)) |
428 | 1184 { |
444 | 1185 CHECK_PROCESS (process); |
1204 | 1186 return XPROCESS (process)->tty_name; |
428 | 1187 } |
1188 | |
1189 DEFUN ("set-process-buffer", Fset_process_buffer, 2, 2, 0, /* | |
1190 Set buffer associated with PROCESS to BUFFER (a buffer, or nil). | |
2297 | 1191 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. |
428 | 1192 */ |
444 | 1193 (process, buffer)) |
428 | 1194 { |
444 | 1195 CHECK_PROCESS (process); |
428 | 1196 if (!NILP (buffer)) |
1197 CHECK_BUFFER (buffer); | |
444 | 1198 XPROCESS (process)->buffer = buffer; |
428 | 1199 return buffer; |
1200 } | |
1201 | |
1202 DEFUN ("process-buffer", Fprocess_buffer, 1, 1, 0, /* | |
1203 Return the buffer PROCESS is associated with. | |
2297 | 1204 Output from PROCESS is inserted in this buffer unless PROCESS has a filter. |
1205 Set the buffer with `set-process-buffer'. | |
428 | 1206 */ |
444 | 1207 (process)) |
428 | 1208 { |
444 | 1209 CHECK_PROCESS (process); |
1210 return XPROCESS (process)->buffer; | |
428 | 1211 } |
1212 | |
853 | 1213 DEFUN ("set-process-stderr-buffer", Fset_process_stderr_buffer, 2, 2, 0, /* |
2297 | 1214 Output from the stderr of PROCESS is inserted in this buffer unless |
1215 PROCESS has a stderr filter. | |
853 | 1216 Set stderr buffer associated with PROCESS to BUFFER (a buffer, or nil). |
1217 */ | |
1218 (process, buffer)) | |
1219 { | |
1220 CHECK_PROCESS (process); | |
1221 if (!XPROCESS (process)->separate_stderr) | |
1222 invalid_change ("stdout and stderr not separate", process); | |
1223 if (!NILP (buffer)) | |
1224 CHECK_BUFFER (buffer); | |
1225 XPROCESS (process)->stderr_buffer = buffer; | |
1226 return buffer; | |
1227 } | |
1228 | |
1229 DEFUN ("process-stderr-buffer", Fprocess_stderr_buffer, 1, 1, 0, /* | |
1230 Return the stderr buffer PROCESS is associated with. | |
2297 | 1231 Output from the stderr of PROCESS is inserted in this buffer unless PROCESS |
1232 has a stderr filter. Set the buffer with `set-process-stderr-buffer'. | |
853 | 1233 */ |
1234 (process)) | |
1235 { | |
1236 CHECK_PROCESS (process); | |
1237 if (!XPROCESS (process)->separate_stderr) | |
1238 invalid_change ("stdout and stderr not separate", process); | |
1239 return XPROCESS (process)->stderr_buffer; | |
1240 } | |
1241 | |
428 | 1242 DEFUN ("process-mark", Fprocess_mark, 1, 1, 0, /* |
1243 Return the marker for the end of the last output from PROCESS. | |
1244 */ | |
444 | 1245 (process)) |
428 | 1246 { |
444 | 1247 CHECK_PROCESS (process); |
1248 return XPROCESS (process)->mark; | |
428 | 1249 } |
1250 | |
853 | 1251 DEFUN ("process-stderr-mark", Fprocess_stderr_mark, 1, 1, 0, /* |
1252 Return the marker for the end of the last stderr output from PROCESS. | |
1253 */ | |
1254 (process)) | |
1255 { | |
1256 CHECK_PROCESS (process); | |
1257 if (!XPROCESS (process)->separate_stderr) | |
1258 invalid_operation ("stdout and stderr not separate", process); | |
1259 return XPROCESS (process)->stderr_mark; | |
1260 } | |
1261 | |
428 | 1262 void |
853 | 1263 set_process_filter (Lisp_Object process, Lisp_Object filter, |
1264 int filter_does_read, int set_stderr) | |
428 | 1265 { |
444 | 1266 CHECK_PROCESS (process); |
853 | 1267 if (set_stderr && !XPROCESS (process)->separate_stderr) |
1268 invalid_change ("stdout and stderr not separate", process); | |
863 | 1269 if (PROCESS_READABLE_P (XPROCESS (process))) |
853 | 1270 { |
1271 if (EQ (filter, Qt)) | |
1272 event_stream_unselect_process (XPROCESS (process), !set_stderr, | |
1273 set_stderr); | |
1274 else | |
1275 event_stream_select_process (XPROCESS (process), !set_stderr, | |
1276 set_stderr); | |
1277 } | |
428 | 1278 |
853 | 1279 if (set_stderr) |
1280 XPROCESS (process)->stderr_filter = filter; | |
1281 else | |
1282 XPROCESS (process)->filter = filter; | |
444 | 1283 XPROCESS (process)->filter_does_read = filter_does_read; |
428 | 1284 } |
1285 | |
1286 DEFUN ("set-process-filter", Fset_process_filter, 2, 2, 0, /* | |
1287 Give PROCESS the filter function FILTER; nil means no filter. | |
853 | 1288 t means stop accepting output from the process. (If process was created |
854 | 1289 with |
853 | 1290 When a process has a filter, each time it does output |
1291 the entire string of output is passed to the filter. | |
1292 The filter gets two arguments: the process and the string of output. | |
1293 If the process has a filter, its buffer is not used for output. | |
1294 */ | |
1295 (process, filter)) | |
1296 { | |
1297 set_process_filter (process, filter, 0, 0); | |
1298 return filter; | |
1299 } | |
1300 | |
1301 DEFUN ("set-process-stderr-filter", Fset_process_stderr_filter, 2, 2, 0, /* | |
1302 Give PROCESS the stderr filter function FILTER; nil means no filter. | |
428 | 1303 t means stop accepting output from the process. |
1304 When a process has a filter, each time it does output | |
1305 the entire string of output is passed to the filter. | |
1306 The filter gets two arguments: the process and the string of output. | |
1307 If the process has a filter, its buffer is not used for output. | |
1308 */ | |
444 | 1309 (process, filter)) |
428 | 1310 { |
853 | 1311 set_process_filter (process, filter, 0, 1); |
428 | 1312 return filter; |
1313 } | |
1314 | |
1315 DEFUN ("process-filter", Fprocess_filter, 1, 1, 0, /* | |
1316 Return the filter function of PROCESS; nil if none. | |
1317 See `set-process-filter' for more info on filter functions. | |
1318 */ | |
444 | 1319 (process)) |
428 | 1320 { |
444 | 1321 CHECK_PROCESS (process); |
1322 return XPROCESS (process)->filter; | |
428 | 1323 } |
1324 | |
853 | 1325 DEFUN ("process-stderr-filter", Fprocess_stderr_filter, 1, 1, 0, /* |
1326 Return the filter function of PROCESS; nil if none. | |
1327 See `set-process-stderr-filter' for more info on filter functions. | |
1328 */ | |
1329 (process)) | |
1330 { | |
1331 CHECK_PROCESS (process); | |
1332 if (!XPROCESS (process)->separate_stderr) | |
1333 invalid_operation ("stdout and stderr not separate", process); | |
1334 return XPROCESS (process)->stderr_filter; | |
1335 } | |
1336 | |
442 | 1337 DEFUN ("process-send-region", Fprocess_send_region, 3, 4, 0, /* |
1338 Send current contents of the region between START and END as input to PROCESS. | |
444 | 1339 PROCESS may be a process or the name of a process, or a buffer or the |
1340 name of a buffer, in which case the buffer's process is used. If it | |
1341 is nil, the current buffer's process is used. | |
442 | 1342 BUFFER specifies the buffer to look in; if nil, the current buffer is used. |
853 | 1343 If the region is more than 100 or so characters long, it may be sent in |
1344 several chunks. This may happen even for shorter regions. Output | |
444 | 1345 from processes can arrive in between chunks. |
428 | 1346 */ |
442 | 1347 (process, start, end, buffer)) |
428 | 1348 { |
1349 /* This function can GC */ | |
665 | 1350 Charbpos bstart, bend; |
442 | 1351 struct buffer *buf = decode_buffer (buffer, 0); |
428 | 1352 |
793 | 1353 buffer = wrap_buffer (buf); |
444 | 1354 process = get_process (process); |
1355 get_buffer_range_char (buf, start, end, &bstart, &bend, 0); | |
442 | 1356 |
444 | 1357 send_process (process, buffer, 0, bstart, bend - bstart); |
428 | 1358 return Qnil; |
1359 } | |
1360 | |
1361 DEFUN ("process-send-string", Fprocess_send_string, 2, 4, 0, /* | |
1362 Send PROCESS the contents of STRING as input. | |
444 | 1363 PROCESS may be a process or the name of a process, or a buffer or the |
1364 name of a buffer, in which case the buffer's process is used. If it | |
1365 is nil, the current buffer's process is used. | |
1366 Optional arguments START and END specify part of STRING; see `substring'. | |
1367 If STRING is more than 100 or so characters long, it may be sent in | |
1368 several chunks. This may happen even for shorter strings. Output | |
1369 from processes can arrive in between chunks. | |
428 | 1370 */ |
444 | 1371 (process, string, start, end)) |
428 | 1372 { |
1373 /* This function can GC */ | |
444 | 1374 Bytecount bstart, bend; |
428 | 1375 |
444 | 1376 process = get_process (process); |
428 | 1377 CHECK_STRING (string); |
444 | 1378 get_string_range_byte (string, start, end, &bstart, &bend, |
428 | 1379 GB_HISTORICAL_STRING_BEHAVIOR); |
1380 | |
444 | 1381 send_process (process, string, 0, bstart, bend - bstart); |
428 | 1382 return Qnil; |
1383 } | |
1384 | |
1385 | |
1386 DEFUN ("process-input-coding-system", Fprocess_input_coding_system, 1, 1, 0, /* | |
1387 Return PROCESS's input coding system. | |
1388 */ | |
1389 (process)) | |
1390 { | |
1391 process = get_process (process); | |
863 | 1392 CHECK_READABLE_PROCESS (process); |
771 | 1393 return (coding_stream_detected_coding_system |
1394 (XLSTREAM (XPROCESS (process)->coding_instream))); | |
428 | 1395 } |
1396 | |
1397 DEFUN ("process-output-coding-system", Fprocess_output_coding_system, 1, 1, 0, /* | |
1398 Return PROCESS's output coding system. | |
1399 */ | |
1400 (process)) | |
1401 { | |
1402 process = get_process (process); | |
440 | 1403 CHECK_LIVE_PROCESS (process); |
771 | 1404 return (coding_stream_coding_system |
1405 (XLSTREAM (XPROCESS (process)->coding_outstream))); | |
428 | 1406 } |
1407 | |
1408 DEFUN ("process-coding-system", Fprocess_coding_system, 1, 1, 0, /* | |
1409 Return a pair of coding-system for decoding and encoding of PROCESS. | |
1410 */ | |
1411 (process)) | |
1412 { | |
1413 process = get_process (process); | |
863 | 1414 CHECK_READABLE_PROCESS (process); |
771 | 1415 return Fcons (coding_stream_detected_coding_system |
428 | 1416 (XLSTREAM (XPROCESS (process)->coding_instream)), |
771 | 1417 coding_stream_coding_system |
428 | 1418 (XLSTREAM (XPROCESS (process)->coding_outstream))); |
1419 } | |
1420 | |
1421 DEFUN ("set-process-input-coding-system", Fset_process_input_coding_system, | |
1422 2, 2, 0, /* | |
1423 Set PROCESS's input coding system to CODESYS. | |
771 | 1424 This is used for reading data from PROCESS. |
428 | 1425 */ |
1426 (process, codesys)) | |
1427 { | |
771 | 1428 codesys = get_coding_system_for_text_file (codesys, 1); |
428 | 1429 process = get_process (process); |
863 | 1430 CHECK_READABLE_PROCESS (process); |
440 | 1431 |
771 | 1432 set_coding_stream_coding_system |
428 | 1433 (XLSTREAM (XPROCESS (process)->coding_instream), codesys); |
1434 return Qnil; | |
1435 } | |
1436 | |
1437 DEFUN ("set-process-output-coding-system", Fset_process_output_coding_system, | |
1438 2, 2, 0, /* | |
1439 Set PROCESS's output coding system to CODESYS. | |
771 | 1440 This is used for writing data to PROCESS. |
428 | 1441 */ |
1442 (process, codesys)) | |
1443 { | |
771 | 1444 codesys = get_coding_system_for_text_file (codesys, 0); |
428 | 1445 process = get_process (process); |
440 | 1446 CHECK_LIVE_PROCESS (process); |
1447 | |
771 | 1448 set_coding_stream_coding_system |
428 | 1449 (XLSTREAM (XPROCESS (process)->coding_outstream), codesys); |
1450 return Qnil; | |
1451 } | |
1452 | |
1453 DEFUN ("set-process-coding-system", Fset_process_coding_system, | |
1454 1, 3, 0, /* | |
1455 Set coding-systems of PROCESS to DECODING and ENCODING. | |
440 | 1456 DECODING will be used to decode subprocess output and ENCODING to |
1457 encode subprocess input. | |
428 | 1458 */ |
1459 (process, decoding, encoding)) | |
1460 { | |
1461 if (!NILP (decoding)) | |
1462 Fset_process_input_coding_system (process, decoding); | |
1463 | |
1464 if (!NILP (encoding)) | |
1465 Fset_process_output_coding_system (process, encoding); | |
1466 | |
1467 return Qnil; | |
1468 } | |
1469 | |
1470 | |
1471 /************************************************************************/ | |
1472 /* process status */ | |
1473 /************************************************************************/ | |
1474 | |
1475 static Lisp_Object | |
1476 exec_sentinel_unwind (Lisp_Object datum) | |
1477 { | |
853 | 1478 XPROCESS (XCAR (datum))->sentinel = XCDR (datum); |
1479 free_cons (datum); | |
428 | 1480 return Qnil; |
1481 } | |
1482 | |
1483 static void | |
444 | 1484 exec_sentinel (Lisp_Object process, Lisp_Object reason) |
428 | 1485 { |
1486 /* This function can GC */ | |
1487 int speccount = specpdl_depth (); | |
444 | 1488 Lisp_Process *p = XPROCESS (process); |
428 | 1489 Lisp_Object sentinel = p->sentinel; |
1490 | |
1491 if (NILP (sentinel)) | |
1492 return; | |
1493 | |
1494 /* Some weird FSFmacs crap here with | |
1495 Vdeactivate_mark and current_buffer->keymap */ | |
1496 | |
853 | 1497 /* Some FSF junk with running_asynch_code, to preserve the match |
1498 data. Not necessary because we don't call process filters | |
1499 asynchronously (i.e. from within QUIT). */ | |
1500 | |
428 | 1501 /* Zilch the sentinel while it's running, to avoid recursive invocations; |
853 | 1502 assure that it gets restored no matter how the sentinel exits. |
1503 | |
1504 (#### Why is this necessary? Probably another relic of asynchronous | |
1505 calling of process filters/sentinels.) */ | |
428 | 1506 p->sentinel = Qnil; |
853 | 1507 record_unwind_protect (exec_sentinel_unwind, |
1508 noseeum_cons (process, sentinel)); | |
1509 /* Don't catch errors here; we're not in any critical code. */ | |
1510 call2 (sentinel, process, reason); | |
771 | 1511 unbind_to (speccount); |
428 | 1512 } |
1513 | |
1514 DEFUN ("set-process-sentinel", Fset_process_sentinel, 2, 2, 0, /* | |
1515 Give PROCESS the sentinel SENTINEL; nil for none. | |
1516 The sentinel is called as a function when the process changes state. | |
1517 It gets two arguments: the process, and a string describing the change. | |
1518 */ | |
444 | 1519 (process, sentinel)) |
428 | 1520 { |
444 | 1521 CHECK_PROCESS (process); |
1522 XPROCESS (process)->sentinel = sentinel; | |
428 | 1523 return sentinel; |
1524 } | |
1525 | |
1526 DEFUN ("process-sentinel", Fprocess_sentinel, 1, 1, 0, /* | |
1527 Return the sentinel of PROCESS; nil if none. | |
1528 See `set-process-sentinel' for more info on sentinels. | |
1529 */ | |
444 | 1530 (process)) |
428 | 1531 { |
444 | 1532 CHECK_PROCESS (process); |
1533 return XPROCESS (process)->sentinel; | |
428 | 1534 } |
1535 | |
1536 | |
442 | 1537 const char * |
428 | 1538 signal_name (int signum) |
1539 { | |
1540 if (signum >= 0 && signum < NSIG) | |
442 | 1541 return (const char *) sys_siglist[signum]; |
428 | 1542 |
442 | 1543 return (const char *) GETTEXT ("unknown signal"); |
428 | 1544 } |
1545 | |
1546 void | |
1547 update_process_status (Lisp_Object p, | |
1548 Lisp_Object status_symbol, | |
1549 int exit_code, | |
1550 int core_dumped) | |
1551 { | |
1552 XPROCESS (p)->tick++; | |
1553 process_tick++; | |
1554 XPROCESS (p)->status_symbol = status_symbol; | |
1555 XPROCESS (p)->exit_code = exit_code; | |
1556 XPROCESS (p)->core_dumped = core_dumped; | |
1557 } | |
1558 | |
1559 /* Return a string describing a process status list. */ | |
1560 | |
1561 static Lisp_Object | |
440 | 1562 status_message (Lisp_Process *p) |
428 | 1563 { |
1564 Lisp_Object symbol = p->status_symbol; | |
1565 int code = p->exit_code; | |
1566 int coredump = p->core_dumped; | |
1567 Lisp_Object string, string2; | |
1568 | |
1569 if (EQ (symbol, Qsignal) || EQ (symbol, Qstop)) | |
1570 { | |
1571 string = build_string (signal_name (code)); | |
1572 if (coredump) | |
771 | 1573 string2 = build_msg_string (" (core dumped)\n"); |
428 | 1574 else |
1575 string2 = build_string ("\n"); | |
793 | 1576 set_string_char (string, 0, |
867 | 1577 DOWNCASE (0, string_ichar (string, 0))); |
428 | 1578 return concat2 (string, string2); |
1579 } | |
1580 else if (EQ (symbol, Qexit)) | |
1581 { | |
1582 if (code == 0) | |
771 | 1583 return build_msg_string ("finished\n"); |
428 | 1584 string = Fnumber_to_string (make_int (code)); |
1585 if (coredump) | |
771 | 1586 string2 = build_msg_string (" (core dumped)\n"); |
428 | 1587 else |
1588 string2 = build_string ("\n"); | |
771 | 1589 return concat2 (build_msg_string ("exited abnormally with code "), |
428 | 1590 concat2 (string, string2)); |
1591 } | |
1592 else | |
1593 return Fcopy_sequence (Fsymbol_name (symbol)); | |
1594 } | |
1595 | |
1596 /* Tell status_notify() to check for terminated processes. We do this | |
1597 because on some systems we sometimes miss SIGCHLD calls. (Not sure | |
853 | 1598 why.) This is also used under Mswin. */ |
428 | 1599 |
1600 void | |
1601 kick_status_notify (void) | |
1602 { | |
1603 process_tick++; | |
1604 } | |
1605 | |
1606 | |
1607 /* Report all recent events of a change in process status | |
1608 (either run the sentinel or output a message). | |
1609 This is done while Emacs is waiting for keyboard input. */ | |
1610 | |
1611 void | |
1612 status_notify (void) | |
1613 { | |
1614 /* This function can GC */ | |
1615 Lisp_Object tail = Qnil; | |
1616 Lisp_Object symbol = Qnil; | |
1617 Lisp_Object msg = Qnil; | |
1618 struct gcpro gcpro1, gcpro2, gcpro3; | |
1619 /* process_tick is volatile, so we have to remember it now. | |
444 | 1620 Otherwise, we get a race condition if SIGCHLD happens during |
428 | 1621 this function. |
1622 | |
1623 (Actually, this is not the case anymore. The code to | |
1624 update the process structures has been moved out of the | |
1625 SIGCHLD handler. But for the moment I'm leaving this | |
1626 stuff in -- it can't hurt.) */ | |
1627 int temp_process_tick; | |
1628 | |
1629 MAYBE_PROCMETH (reap_exited_processes, ()); | |
1630 | |
1631 temp_process_tick = process_tick; | |
1632 | |
1633 if (update_tick == temp_process_tick) | |
1634 return; | |
1635 | |
1636 /* We need to gcpro tail; if read_process_output calls a filter | |
1637 which deletes a process and removes the cons to which tail points | |
1638 from Vprocess_alist, and then causes a GC, tail is an unprotected | |
1639 reference. */ | |
1640 GCPRO3 (tail, symbol, msg); | |
1641 | |
1642 for (tail = Vprocess_list; CONSP (tail); tail = XCDR (tail)) | |
1643 { | |
444 | 1644 Lisp_Object process = XCAR (tail); |
1645 Lisp_Process *p = XPROCESS (process); | |
428 | 1646 /* p->tick is also volatile. Same thing as above applies. */ |
1647 int this_process_tick; | |
1648 | |
1649 /* #### extra check for terminated processes, in case a SIGCHLD | |
1650 got missed (this seems to happen sometimes, I'm not sure why). | |
1651 */ | |
1652 if (INTP (p->pid)) | |
1653 MAYBE_PROCMETH (update_status_if_terminated, (p)); | |
1654 | |
1655 this_process_tick = p->tick; | |
1656 if (this_process_tick != p->update_tick) | |
1657 { | |
1658 p->update_tick = this_process_tick; | |
1659 | |
1660 /* If process is still active, read any output that remains. */ | |
1661 while (!EQ (p->filter, Qt) | |
853 | 1662 && read_process_output (process, 0) > 0) |
1663 ; | |
1664 while (p->separate_stderr && !EQ (p->stderr_filter, Qt) | |
1665 && read_process_output (process, 1) > 0) | |
428 | 1666 ; |
1667 | |
1668 /* Get the text to use for the message. */ | |
1669 msg = status_message (p); | |
1670 | |
1671 /* If process is terminated, deactivate it or delete it. */ | |
1672 symbol = p->status_symbol; | |
1673 | |
1674 if (EQ (symbol, Qsignal) | |
1675 || EQ (symbol, Qexit)) | |
1676 { | |
1677 if (delete_exited_processes) | |
444 | 1678 remove_process (process); |
428 | 1679 else |
444 | 1680 deactivate_process (process); |
428 | 1681 } |
1682 | |
1683 /* Now output the message suitably. */ | |
1684 if (!NILP (p->sentinel)) | |
444 | 1685 exec_sentinel (process, msg); |
428 | 1686 /* Don't bother with a message in the buffer |
1687 when a process becomes runnable. */ | |
844 | 1688 else if (!EQ (symbol, Qrun) && !NILP (p->buffer) && |
1689 /* Avoid error if buffer is deleted | |
1690 (probably that's why the process is dead, too) */ | |
1691 BUFFER_LIVE_P (XBUFFER (p->buffer))) | |
428 | 1692 { |
844 | 1693 struct gcpro ngcpro1; |
853 | 1694 int spec = process_setup_for_insertion (process, 0); |
428 | 1695 |
844 | 1696 NGCPRO1 (process); |
428 | 1697 buffer_insert_c_string (current_buffer, "\nProcess "); |
1698 Finsert (1, &p->name); | |
1699 buffer_insert_c_string (current_buffer, " "); | |
1700 Finsert (1, &msg); | |
1701 Fset_marker (p->mark, make_int (BUF_PT (current_buffer)), | |
1702 p->buffer); | |
1703 | |
844 | 1704 unbind_to (spec); |
428 | 1705 NUNGCPRO; |
1706 } | |
1707 } | |
1708 } /* end for */ | |
1709 | |
1710 /* in case buffers use %s in modeline-format */ | |
1711 MARK_MODELINE_CHANGED; | |
1712 redisplay (); | |
1713 | |
1714 update_tick = temp_process_tick; | |
1715 | |
1716 UNGCPRO; | |
1717 } | |
1718 | |
1719 DEFUN ("process-status", Fprocess_status, 1, 1, 0, /* | |
1720 Return the status of PROCESS. | |
1721 This is a symbol, one of these: | |
1722 | |
1723 run -- for a process that is running. | |
1724 stop -- for a process stopped but continuable. | |
1725 exit -- for a process that has exited. | |
1726 signal -- for a process that has got a fatal signal. | |
1727 open -- for a network stream connection that is open. | |
1728 closed -- for a network stream connection that is closed. | |
1729 nil -- if arg is a process name and no such process exists. | |
1730 | |
1731 PROCESS may be a process, a buffer, the name of a process or buffer, or | |
1732 nil, indicating the current buffer's process. | |
1733 */ | |
444 | 1734 (process)) |
428 | 1735 { |
1736 Lisp_Object status_symbol; | |
1737 | |
444 | 1738 if (STRINGP (process)) |
1739 process = Fget_process (process); | |
428 | 1740 else |
444 | 1741 process = get_process (process); |
428 | 1742 |
444 | 1743 if (NILP (process)) |
428 | 1744 return Qnil; |
1745 | |
444 | 1746 status_symbol = XPROCESS (process)->status_symbol; |
1747 if (network_connection_p (process)) | |
428 | 1748 { |
1749 if (EQ (status_symbol, Qrun)) | |
1750 status_symbol = Qopen; | |
1751 else if (EQ (status_symbol, Qexit)) | |
1752 status_symbol = Qclosed; | |
1753 } | |
1754 return status_symbol; | |
1755 } | |
1756 | |
1757 DEFUN ("process-exit-status", Fprocess_exit_status, 1, 1, 0, /* | |
1758 Return the exit status of PROCESS or the signal number that killed it. | |
1759 If PROCESS has not yet exited or died, return 0. | |
1760 */ | |
444 | 1761 (process)) |
428 | 1762 { |
444 | 1763 CHECK_PROCESS (process); |
1764 return make_int (XPROCESS (process)->exit_code); | |
428 | 1765 } |
1766 | |
1767 | |
1768 | |
442 | 1769 static int |
1770 decode_signal (Lisp_Object signal_) | |
428 | 1771 { |
442 | 1772 if (INTP (signal_)) |
1773 return XINT (signal_); | |
428 | 1774 else |
1775 { | |
867 | 1776 Ibyte *name; |
428 | 1777 |
442 | 1778 CHECK_SYMBOL (signal_); |
793 | 1779 name = XSTRING_DATA (XSYMBOL (signal_)->name); |
428 | 1780 |
793 | 1781 #define handle_signal(sym) do { \ |
2367 | 1782 if (!qxestrcmp_ascii ( name, #sym)) \ |
793 | 1783 return sym; \ |
442 | 1784 } while (0) |
428 | 1785 |
1786 handle_signal (SIGINT); /* ANSI */ | |
1787 handle_signal (SIGILL); /* ANSI */ | |
1788 handle_signal (SIGABRT); /* ANSI */ | |
1789 handle_signal (SIGFPE); /* ANSI */ | |
1790 handle_signal (SIGSEGV); /* ANSI */ | |
1791 handle_signal (SIGTERM); /* ANSI */ | |
1792 | |
1793 #ifdef SIGHUP | |
1794 handle_signal (SIGHUP); /* POSIX */ | |
1795 #endif | |
1796 #ifdef SIGQUIT | |
1797 handle_signal (SIGQUIT); /* POSIX */ | |
1798 #endif | |
1799 #ifdef SIGTRAP | |
1800 handle_signal (SIGTRAP); /* POSIX */ | |
1801 #endif | |
1802 #ifdef SIGKILL | |
1803 handle_signal (SIGKILL); /* POSIX */ | |
1804 #endif | |
1805 #ifdef SIGUSR1 | |
1806 handle_signal (SIGUSR1); /* POSIX */ | |
1807 #endif | |
1808 #ifdef SIGUSR2 | |
1809 handle_signal (SIGUSR2); /* POSIX */ | |
1810 #endif | |
1811 #ifdef SIGPIPE | |
1812 handle_signal (SIGPIPE); /* POSIX */ | |
1813 #endif | |
1814 #ifdef SIGALRM | |
1815 handle_signal (SIGALRM); /* POSIX */ | |
1816 #endif | |
1817 #ifdef SIGCHLD | |
1818 handle_signal (SIGCHLD); /* POSIX */ | |
1819 #endif | |
1820 #ifdef SIGCONT | |
1821 handle_signal (SIGCONT); /* POSIX */ | |
1822 #endif | |
1823 #ifdef SIGSTOP | |
1824 handle_signal (SIGSTOP); /* POSIX */ | |
1825 #endif | |
1826 #ifdef SIGTSTP | |
1827 handle_signal (SIGTSTP); /* POSIX */ | |
1828 #endif | |
1829 #ifdef SIGTTIN | |
1830 handle_signal (SIGTTIN); /* POSIX */ | |
1831 #endif | |
1832 #ifdef SIGTTOU | |
1833 handle_signal (SIGTTOU); /* POSIX */ | |
1834 #endif | |
1835 | |
1836 #ifdef SIGBUS | |
1837 handle_signal (SIGBUS); /* XPG5 */ | |
1838 #endif | |
1839 #ifdef SIGPOLL | |
1840 handle_signal (SIGPOLL); /* XPG5 */ | |
1841 #endif | |
1842 #ifdef SIGPROF | |
1843 handle_signal (SIGPROF); /* XPG5 */ | |
1844 #endif | |
1845 #ifdef SIGSYS | |
1846 handle_signal (SIGSYS); /* XPG5 */ | |
1847 #endif | |
1848 #ifdef SIGURG | |
1849 handle_signal (SIGURG); /* XPG5 */ | |
1850 #endif | |
1851 #ifdef SIGXCPU | |
1852 handle_signal (SIGXCPU); /* XPG5 */ | |
1853 #endif | |
1854 #ifdef SIGXFSZ | |
1855 handle_signal (SIGXFSZ); /* XPG5 */ | |
1856 #endif | |
1857 #ifdef SIGVTALRM | |
1858 handle_signal (SIGVTALRM); /* XPG5 */ | |
1859 #endif | |
1860 | |
1861 #ifdef SIGIO | |
1862 handle_signal (SIGIO); /* BSD 4.2 */ | |
1863 #endif | |
1864 #ifdef SIGWINCH | |
1865 handle_signal (SIGWINCH); /* BSD 4.3 */ | |
1866 #endif | |
1867 | |
1868 #ifdef SIGEMT | |
1869 handle_signal (SIGEMT); | |
1870 #endif | |
1871 #ifdef SIGINFO | |
1872 handle_signal (SIGINFO); | |
1873 #endif | |
1874 #ifdef SIGHWE | |
1875 handle_signal (SIGHWE); | |
1876 #endif | |
1877 #ifdef SIGPRE | |
1878 handle_signal (SIGPRE); | |
1879 #endif | |
1880 #ifdef SIGUME | |
1881 handle_signal (SIGUME); | |
1882 #endif | |
1883 #ifdef SIGDLK | |
1884 handle_signal (SIGDLK); | |
1885 #endif | |
1886 #ifdef SIGCPULIM | |
1887 handle_signal (SIGCPULIM); | |
1888 #endif | |
1889 #ifdef SIGIOT | |
1890 handle_signal (SIGIOT); | |
1891 #endif | |
1892 #ifdef SIGLOST | |
1893 handle_signal (SIGLOST); | |
1894 #endif | |
1895 #ifdef SIGSTKFLT | |
1896 handle_signal (SIGSTKFLT); | |
1897 #endif | |
1898 #ifdef SIGUNUSED | |
1899 handle_signal (SIGUNUSED); | |
1900 #endif | |
1901 #ifdef SIGDANGER | |
1902 handle_signal (SIGDANGER); /* AIX */ | |
1903 #endif | |
1904 #ifdef SIGMSG | |
1905 handle_signal (SIGMSG); | |
1906 #endif | |
1907 #ifdef SIGSOUND | |
1908 handle_signal (SIGSOUND); | |
1909 #endif | |
1910 #ifdef SIGRETRACT | |
1911 handle_signal (SIGRETRACT); | |
1912 #endif | |
1913 #ifdef SIGGRANT | |
1914 handle_signal (SIGGRANT); | |
1915 #endif | |
1916 #ifdef SIGPWR | |
1917 handle_signal (SIGPWR); | |
1918 #endif | |
1919 | |
1920 #undef handle_signal | |
1921 | |
563 | 1922 invalid_constant ("Undefined signal name", signal_); |
1204 | 1923 RETURN_NOT_REACHED (0); |
442 | 1924 } |
1925 } | |
1926 | |
1927 /* Send signal number SIGNO to PROCESS. | |
1928 CURRENT-GROUP non-nil means send signal to the current | |
1929 foreground process group of the process's controlling terminal rather | |
1930 than to the process's own process group. | |
1931 This is used for various commands in shell mode. | |
1932 If NOMSG is zero, insert signal-announcements into process's buffers | |
1933 right away. | |
1934 | |
1935 If we can, we try to signal PROCESS by sending control characters | |
1936 down the pty. This allows us to signal inferiors who have changed | |
1937 their uid, for which kill() would return an EPERM error, or to | |
1938 processes running on another computer through a remote login. */ | |
1939 | |
1940 static void | |
1941 process_send_signal (Lisp_Object process, int signo, | |
1942 int current_group, int nomsg) | |
1943 { | |
1944 /* This function can GC */ | |
444 | 1945 process = get_process (process); |
442 | 1946 |
444 | 1947 if (network_connection_p (process)) |
563 | 1948 invalid_operation ("Network connection is not a subprocess", process); |
444 | 1949 CHECK_LIVE_PROCESS (process); |
442 | 1950 |
444 | 1951 MAYBE_PROCMETH (kill_child_process, (process, signo, current_group, nomsg)); |
442 | 1952 } |
1953 | |
1954 DEFUN ("process-send-signal", Fprocess_send_signal, 1, 3, 0, /* | |
1955 Send signal SIGNAL to process PROCESS. | |
1956 SIGNAL may be an integer, or a symbol naming a signal, like `SIGSEGV'. | |
1957 PROCESS may be a process, a buffer, the name of a process or buffer, or | |
1958 nil, indicating the current buffer's process. | |
1959 Third arg CURRENT-GROUP non-nil means send signal to the current | |
1960 foreground process group of the process's controlling terminal rather | |
1961 than to the process's own process group. | |
1962 If the process is a shell that supports job control, this means | |
1963 send the signal to the current subjob rather than the shell. | |
1964 */ | |
1965 (signal_, process, current_group)) | |
1966 { | |
1967 /* This function can GC */ | |
1968 process_send_signal (process, decode_signal (signal_), | |
1969 !NILP (current_group), 0); | |
1970 return process; | |
1971 } | |
1972 | |
1973 DEFUN ("interrupt-process", Finterrupt_process, 0, 2, 0, /* | |
1974 Interrupt process PROCESS. | |
1975 See function `process-send-signal' for more details on usage. | |
1976 */ | |
1977 (process, current_group)) | |
1978 { | |
1979 /* This function can GC */ | |
1980 process_send_signal (process, SIGINT, !NILP (current_group), 0); | |
1981 return process; | |
1982 } | |
1983 | |
1984 DEFUN ("kill-process", Fkill_process, 0, 2, 0, /* | |
1985 Kill process PROCESS. | |
1986 See function `process-send-signal' for more details on usage. | |
1987 */ | |
1988 (process, current_group)) | |
1989 { | |
1990 /* This function can GC */ | |
1991 #ifdef SIGKILL | |
1992 process_send_signal (process, SIGKILL, !NILP (current_group), 0); | |
1993 #else | |
563 | 1994 signal_error (Qunimplemented, |
1995 "kill-process: Not supported on this system", | |
1996 Qunbound); | |
442 | 1997 #endif |
1998 return process; | |
1999 } | |
2000 | |
2001 DEFUN ("quit-process", Fquit_process, 0, 2, 0, /* | |
2002 Send QUIT signal to process PROCESS. | |
2003 See function `process-send-signal' for more details on usage. | |
2004 */ | |
2005 (process, current_group)) | |
2006 { | |
2007 /* This function can GC */ | |
2008 #ifdef SIGQUIT | |
2009 process_send_signal (process, SIGQUIT, !NILP (current_group), 0); | |
2010 #else | |
563 | 2011 signal_error (Qunimplemented, |
2012 "quit-process: Not supported on this system", | |
2013 Qunbound); | |
442 | 2014 #endif |
2015 return process; | |
2016 } | |
2017 | |
2018 DEFUN ("stop-process", Fstop_process, 0, 2, 0, /* | |
2019 Stop process PROCESS. | |
2020 See function `process-send-signal' for more details on usage. | |
2021 */ | |
2022 (process, current_group)) | |
2023 { | |
2024 /* This function can GC */ | |
2025 #ifdef SIGTSTP | |
2026 process_send_signal (process, SIGTSTP, !NILP (current_group), 0); | |
2027 #else | |
563 | 2028 signal_error (Qunimplemented, |
2029 "stop-process: Not supported on this system", | |
2030 Qunbound); | |
442 | 2031 #endif |
2032 return process; | |
2033 } | |
2034 | |
2035 DEFUN ("continue-process", Fcontinue_process, 0, 2, 0, /* | |
2036 Continue process PROCESS. | |
2037 See function `process-send-signal' for more details on usage. | |
2038 */ | |
2039 (process, current_group)) | |
2040 { | |
2041 /* This function can GC */ | |
2042 #ifdef SIGCONT | |
2043 process_send_signal (process, SIGCONT, !NILP (current_group), 0); | |
2044 #else | |
563 | 2045 signal_error (Qunimplemented, |
2046 "continue-process: Not supported on this system", | |
2047 Qunbound); | |
442 | 2048 #endif |
2049 return process; | |
2050 } | |
2051 | |
2052 DEFUN ("signal-process", Fsignal_process, 2, 2, | |
2053 "nProcess number: \nnSignal code: ", /* | |
2054 Send the process with process id PID the signal with code SIGNAL. | |
2055 PID must be an integer. The process need not be a child of this Emacs. | |
2056 SIGNAL may be an integer, or a symbol naming a signal, like `SIGSEGV'. | |
2057 */ | |
2058 (pid, signal_)) | |
2059 { | |
2060 CHECK_INT (pid); | |
2061 | |
428 | 2062 return make_int (PROCMETH_OR_GIVEN (kill_process_by_pid, |
442 | 2063 (XINT (pid), decode_signal (signal_)), |
2064 -1)); | |
428 | 2065 } |
2066 | |
2067 DEFUN ("process-send-eof", Fprocess_send_eof, 0, 1, 0, /* | |
2068 Make PROCESS see end-of-file in its input. | |
2069 PROCESS may be a process, a buffer, the name of a process or buffer, or | |
2070 nil, indicating the current buffer's process. | |
2071 If PROCESS is a network connection, or is a process communicating | |
2072 through a pipe (as opposed to a pty), then you cannot send any more | |
2073 text to PROCESS after you call this function. | |
2074 */ | |
2075 (process)) | |
2076 { | |
2077 /* This function can GC */ | |
444 | 2078 process = get_process (process); |
428 | 2079 |
2080 /* Make sure the process is really alive. */ | |
444 | 2081 if (! EQ (XPROCESS (process)->status_symbol, Qrun)) |
563 | 2082 invalid_operation ("Process not running", process); |
428 | 2083 |
444 | 2084 if (!MAYBE_INT_PROCMETH (process_send_eof, (process))) |
428 | 2085 { |
444 | 2086 if (!NILP (DATA_OUTSTREAM (XPROCESS (process)))) |
428 | 2087 { |
853 | 2088 USID humpty, dumpty; |
444 | 2089 Lstream_close (XLSTREAM (DATA_OUTSTREAM (XPROCESS (process)))); |
853 | 2090 event_stream_delete_io_streams (Qnil, |
2091 XPROCESS (process)->pipe_outstream, | |
2092 Qnil, &humpty, &dumpty); | |
444 | 2093 XPROCESS (process)->pipe_outstream = Qnil; |
2094 XPROCESS (process)->coding_outstream = Qnil; | |
428 | 2095 } |
2096 } | |
2097 | |
2098 return process; | |
2099 } | |
2100 | |
2101 | |
2102 /************************************************************************/ | |
2103 /* deleting a process */ | |
2104 /************************************************************************/ | |
2105 | |
2106 void | |
444 | 2107 deactivate_process (Lisp_Object process) |
428 | 2108 { |
444 | 2109 Lisp_Process *p = XPROCESS (process); |
853 | 2110 USID in_usid, err_usid; |
428 | 2111 |
2112 /* It's possible that we got as far in the process-creation | |
2113 process as creating the descriptors but didn't get so | |
2114 far as selecting the process for input. In this | |
2115 case, p->pid is nil: p->pid is set at the same time that | |
2116 the process is selected for input. */ | |
2117 /* #### The comment does not look correct. event_stream_unselect_process | |
853 | 2118 is guarded by process->*_selected, so this is not a problem. - kkm*/ |
428 | 2119 /* Must call this before setting the streams to nil */ |
853 | 2120 event_stream_unselect_process (p, 1, 1); |
428 | 2121 |
2122 if (!NILP (DATA_OUTSTREAM (p))) | |
2123 Lstream_close (XLSTREAM (DATA_OUTSTREAM (p))); | |
2124 if (!NILP (DATA_INSTREAM (p))) | |
2125 Lstream_close (XLSTREAM (DATA_INSTREAM (p))); | |
853 | 2126 if (!NILP (DATA_ERRSTREAM (p))) |
2127 Lstream_close (XLSTREAM (DATA_ERRSTREAM (p))); | |
428 | 2128 |
2129 /* Provide minimal implementation for deactivate_process | |
2130 if there's no process-specific one */ | |
2131 if (HAS_PROCMETH_P (deactivate_process)) | |
853 | 2132 PROCMETH (deactivate_process, (p, &in_usid, &err_usid)); |
428 | 2133 else |
853 | 2134 event_stream_delete_io_streams (p->pipe_instream, |
2135 p->pipe_outstream, | |
2136 p->pipe_errstream, | |
2137 &in_usid, &err_usid); | |
428 | 2138 |
853 | 2139 if (in_usid != USID_DONTHASH) |
2367 | 2140 remhash ((const void *) in_usid, usid_to_process); |
853 | 2141 if (err_usid != USID_DONTHASH) |
2367 | 2142 remhash ((const void *) err_usid, usid_to_process); |
428 | 2143 |
2144 p->pipe_instream = Qnil; | |
2145 p->pipe_outstream = Qnil; | |
853 | 2146 p->pipe_errstream = Qnil; |
428 | 2147 p->coding_instream = Qnil; |
2148 p->coding_outstream = Qnil; | |
853 | 2149 p->coding_errstream = Qnil; |
428 | 2150 } |
2151 | |
2152 static void | |
444 | 2153 remove_process (Lisp_Object process) |
428 | 2154 { |
444 | 2155 Vprocess_list = delq_no_quit (process, Vprocess_list); |
428 | 2156 |
444 | 2157 deactivate_process (process); |
428 | 2158 } |
2159 | |
2160 DEFUN ("delete-process", Fdelete_process, 1, 1, 0, /* | |
2161 Delete PROCESS: kill it and forget about it immediately. | |
2162 PROCESS may be a process or the name of one, or a buffer name. | |
2163 */ | |
444 | 2164 (process)) |
428 | 2165 { |
2166 /* This function can GC */ | |
440 | 2167 Lisp_Process *p; |
444 | 2168 process = get_process (process); |
2169 p = XPROCESS (process); | |
2170 if (network_connection_p (process)) | |
428 | 2171 { |
2172 p->status_symbol = Qexit; | |
2173 p->exit_code = 0; | |
2174 p->core_dumped = 0; | |
2175 p->tick++; | |
2176 process_tick++; | |
2177 } | |
440 | 2178 else if (PROCESS_LIVE_P (p)) |
428 | 2179 { |
444 | 2180 Fkill_process (process, Qnil); |
428 | 2181 /* Do this now, since remove_process will make sigchld_handler do nothing. */ |
2182 p->status_symbol = Qsignal; | |
2183 p->exit_code = SIGKILL; | |
2184 p->core_dumped = 0; | |
2185 p->tick++; | |
2186 process_tick++; | |
2187 status_notify (); | |
2188 } | |
444 | 2189 remove_process (process); |
428 | 2190 return Qnil; |
2191 } | |
2192 | |
2193 /* Kill all processes associated with `buffer'. | |
2194 If `buffer' is nil, kill all processes */ | |
2195 | |
2196 void | |
2197 kill_buffer_processes (Lisp_Object buffer) | |
2198 { | |
444 | 2199 LIST_LOOP_2 (process, Vprocess_list) |
2200 if ((NILP (buffer) || EQ (XPROCESS (process)->buffer, buffer))) | |
2201 { | |
2202 if (network_connection_p (process)) | |
2203 Fdelete_process (process); | |
2204 else if (PROCESS_LIVE_P (XPROCESS (process))) | |
2205 process_send_signal (process, SIGHUP, 0, 1); | |
2206 } | |
428 | 2207 } |
2208 | |
2209 DEFUN ("process-kill-without-query", Fprocess_kill_without_query, 1, 2, 0, /* | |
2210 Say no query needed if PROCESS is running when Emacs is exited. | |
2211 Optional second argument if non-nil says to require a query. | |
2212 Value is t if a query was formerly required. | |
2213 */ | |
444 | 2214 (process, require_query_p)) |
428 | 2215 { |
2216 int tem; | |
2217 | |
444 | 2218 CHECK_PROCESS (process); |
2219 tem = XPROCESS (process)->kill_without_query; | |
2220 XPROCESS (process)->kill_without_query = NILP (require_query_p); | |
428 | 2221 |
2222 return tem ? Qnil : Qt; | |
2223 } | |
2224 | |
2225 DEFUN ("process-kill-without-query-p", Fprocess_kill_without_query_p, 1, 1, 0, /* | |
444 | 2226 Return t if PROCESS will be killed without query when emacs is exited. |
428 | 2227 */ |
444 | 2228 (process)) |
428 | 2229 { |
444 | 2230 CHECK_PROCESS (process); |
2231 return XPROCESS (process)->kill_without_query ? Qt : Qnil; | |
428 | 2232 } |
2233 | |
2234 | |
2235 #if 0 | |
2236 | |
826 | 2237 DEFUN ("process-connection", Fprocess_connection, 0, 1, 0, /* |
428 | 2238 Return the connection type of `PROCESS'. This can be nil (pipe), |
2239 t or pty (pty) or stream (socket connection). | |
2240 */ | |
2241 (process)) | |
2242 { | |
2243 return XPROCESS (process)->type; | |
2244 } | |
2245 | |
2246 #endif /* 0 */ | |
2247 | |
814 | 2248 |
2249 static int | |
867 | 2250 getenv_internal (const Ibyte *var, |
814 | 2251 Bytecount varlen, |
867 | 2252 Ibyte **value, |
814 | 2253 Bytecount *valuelen) |
2254 { | |
2255 Lisp_Object scan; | |
2256 | |
2257 assert (env_initted); | |
2258 | |
2259 for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan)) | |
2260 { | |
2261 Lisp_Object entry = XCAR (scan); | |
2262 | |
2263 if (STRINGP (entry) | |
2264 && XSTRING_LENGTH (entry) > varlen | |
826 | 2265 && string_byte (entry, varlen) == '=' |
814 | 2266 #ifdef WIN32_NATIVE |
2267 /* NT environment variables are case insensitive. */ | |
2268 && ! memicmp (XSTRING_DATA (entry), var, varlen) | |
2269 #else /* not WIN32_NATIVE */ | |
2270 && ! memcmp (XSTRING_DATA (entry), var, varlen) | |
2271 #endif /* not WIN32_NATIVE */ | |
2272 ) | |
2273 { | |
2274 *value = XSTRING_DATA (entry) + (varlen + 1); | |
2275 *valuelen = XSTRING_LENGTH (entry) - (varlen + 1); | |
2276 return 1; | |
2277 } | |
2278 } | |
2279 | |
2280 return 0; | |
2281 } | |
2282 | |
2283 static void | |
867 | 2284 putenv_internal (const Ibyte *var, |
814 | 2285 Bytecount varlen, |
867 | 2286 const Ibyte *value, |
814 | 2287 Bytecount valuelen) |
2288 { | |
2289 Lisp_Object scan; | |
2290 | |
2291 assert (env_initted); | |
2292 | |
2293 for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan)) | |
2294 { | |
2295 Lisp_Object entry = XCAR (scan); | |
2296 | |
2297 if (STRINGP (entry) | |
2298 && XSTRING_LENGTH (entry) > varlen | |
826 | 2299 && string_byte (entry, varlen) == '=' |
814 | 2300 #ifdef WIN32_NATIVE |
2301 /* NT environment variables are case insensitive. */ | |
2302 && ! memicmp (XSTRING_DATA (entry), var, varlen) | |
2303 #else /* not WIN32_NATIVE */ | |
2304 && ! memcmp (XSTRING_DATA (entry), var, varlen) | |
2305 #endif /* not WIN32_NATIVE */ | |
2306 ) | |
2307 { | |
2308 XCAR (scan) = concat3 (make_string (var, varlen), | |
2309 build_string ("="), | |
2310 make_string (value, valuelen)); | |
2311 return; | |
2312 } | |
2313 } | |
2314 | |
2315 Vprocess_environment = Fcons (concat3 (make_string (var, varlen), | |
2316 build_string ("="), | |
2317 make_string (value, valuelen)), | |
2318 Vprocess_environment); | |
2319 } | |
2320 | |
2321 /* NOTE: | |
2322 | |
2323 FSF has this as a Lisp function, as follows. Generally moving things | |
2324 out of C and into Lisp is a good idea, but in this case the Lisp | |
2325 function is used so early in the startup sequence that it would be ugly | |
2326 to rearrange the early dumped code to accommodate this. | |
854 | 2327 |
814 | 2328 (defun getenv (variable) |
2329 "Get the value of environment variable VARIABLE. | |
2330 VARIABLE should be a string. Value is nil if VARIABLE is undefined in | |
2331 the environment. Otherwise, value is a string. | |
2332 | |
2333 This function consults the variable `process-environment' | |
2334 for its value." | |
2335 (interactive (list (read-envvar-name "Get environment variable: " t))) | |
2336 (let ((value (getenv-internal variable))) | |
2337 (when (interactive-p) | |
2338 (message "%s" (if value value "Not set"))) | |
2339 value)) | |
2340 */ | |
2341 | |
2342 DEFUN ("getenv", Fgetenv, 1, 2, "sEnvironment variable: \np", /* | |
2343 Return the value of environment variable VAR, as a string. | |
2344 VAR is a string, the name of the variable. | |
2345 When invoked interactively, prints the value in the echo area. | |
2346 */ | |
2347 (var, interactivep)) | |
2348 { | |
867 | 2349 Ibyte *value; |
814 | 2350 Bytecount valuelen; |
2351 Lisp_Object v = Qnil; | |
2352 struct gcpro gcpro1; | |
2353 | |
2354 CHECK_STRING (var); | |
2355 GCPRO1 (v); | |
2356 if (getenv_internal (XSTRING_DATA (var), XSTRING_LENGTH (var), | |
2357 &value, &valuelen)) | |
2358 v = make_string (value, valuelen); | |
2359 if (!NILP (interactivep)) | |
2360 { | |
2361 if (NILP (v)) | |
2362 message ("%s not defined in environment", XSTRING_DATA (var)); | |
2363 else | |
2364 /* #### Should use Fprin1_to_string or Fprin1 to handle string | |
2365 containing quotes correctly. */ | |
2366 message ("\"%s\"", value); | |
2367 } | |
2368 RETURN_UNGCPRO (v); | |
2369 } | |
2370 | |
2371 /* A version of getenv that consults Vprocess_environment, easily | |
2372 callable from C. | |
2373 | |
2374 (At init time, Vprocess_environment is initialized from the | |
2375 environment, stored in the global variable environ. [Note that | |
2376 at startup time, `environ' should be the same as the envp parameter | |
2377 passed to main(); however, later calls to putenv() may change | |
2378 `environ', making the envp parameter inaccurate.] Calls to getenv() | |
2379 and putenv() consult and modify `environ'. However, once | |
2380 Vprocess_environment is initted, XEmacs C code should *NEVER* call | |
2381 getenv() or putenv() directly, because (1) Lisp code that modifies | |
2382 the environment only modifies Vprocess_environment, not `environ'; | |
2383 and (2) Vprocess_environment is in internal format but `environ' | |
2384 is in some external format, and getenv()/putenv() are not Mule- | |
2385 encapsulated. | |
2386 | |
2387 WARNING: This value points into Lisp string data and thus will become | |
2388 invalid after a GC. */ | |
2389 | |
867 | 2390 Ibyte * |
2391 egetenv (const CIbyte *var) | |
814 | 2392 { |
2393 /* This cannot GC -- 7-28-00 ben */ | |
867 | 2394 Ibyte *value; |
814 | 2395 Bytecount valuelen; |
2396 | |
867 | 2397 if (getenv_internal ((const Ibyte *) var, strlen (var), &value, &valuelen)) |
814 | 2398 return value; |
2399 else | |
2400 return 0; | |
2401 } | |
2402 | |
2403 void | |
867 | 2404 eputenv (const CIbyte *var, const CIbyte *value) |
814 | 2405 { |
867 | 2406 putenv_internal ((Ibyte *) var, strlen (var), (Ibyte *) value, |
814 | 2407 strlen (value)); |
2408 } | |
2409 | |
2410 | |
2411 /* This is not named init_process in order to avoid a conflict with NS 3.3 */ | |
2412 void | |
2413 init_xemacs_process (void) | |
2414 { | |
2415 /* This function can GC */ | |
2416 | |
2417 MAYBE_PROCMETH (init_process, ()); | |
2418 | |
2419 Vprocess_list = Qnil; | |
2420 | |
2421 if (usid_to_process) | |
2422 clrhash (usid_to_process); | |
2423 else | |
2424 usid_to_process = make_hash_table (32); | |
854 | 2425 |
814 | 2426 { |
2427 /* jwz: always initialize Vprocess_environment, so that egetenv() | |
2428 works in temacs. */ | |
2367 | 2429 Extbyte **envp; |
814 | 2430 Vprocess_environment = Qnil; |
2367 | 2431 #ifdef WIN32_NATIVE |
2432 _wgetenv (L""); /* force initialization of _wenviron */ | |
2433 for (envp = (Extbyte **) _wenviron; envp && *envp; envp++) | |
2434 Vprocess_environment = | |
2435 Fcons (build_ext_string (*envp, Qmswindows_unicode), | |
2436 Vprocess_environment); | |
2437 #else | |
814 | 2438 for (envp = environ; envp && *envp; envp++) |
2439 Vprocess_environment = | |
2440 Fcons (build_ext_string (*envp, Qnative), Vprocess_environment); | |
2367 | 2441 #endif |
814 | 2442 /* This gets set back to 0 in disksave_object_finalization() */ |
2443 env_initted = 1; | |
2444 } | |
2445 | |
2446 { | |
2447 /* Initialize shell-file-name from environment variables or best guess. */ | |
2448 #ifdef WIN32_NATIVE | |
867 | 2449 const Ibyte *shell = egetenv ("SHELL"); |
814 | 2450 if (!shell) shell = egetenv ("COMSPEC"); |
2451 /* Should never happen! */ | |
2452 if (!shell) shell = | |
867 | 2453 (Ibyte *) (GetVersion () & 0x80000000 ? "command" : "cmd"); |
814 | 2454 #else /* not WIN32_NATIVE */ |
867 | 2455 const Ibyte *shell = egetenv ("SHELL"); |
2456 if (!shell) shell = (Ibyte *) "/bin/sh"; | |
814 | 2457 #endif |
2458 | |
2459 #if 0 /* defined (WIN32_NATIVE) */ | |
2460 /* BAD BAD BAD. We do not wanting to be passing an XEmacs-created | |
2461 SHELL var down to some inferior Cygwin process, which might get | |
2462 screwed up. | |
854 | 2463 |
814 | 2464 There are a few broken apps (eterm/term.el, eterm/tshell.el, |
2465 os-utils/terminal.el, texinfo/tex-mode.el) where this will | |
2466 cause problems. Those broken apps don't look at | |
2467 shell-file-name, instead just at explicit-shell-file-name, | |
2468 ESHELL and SHELL. They are apparently attempting to borrow | |
2469 what `M-x shell' uses, but that latter also looks at | |
2470 shell-file-name. What we want is for all of these apps to look | |
2471 at shell-file-name, so that the user can change the value of | |
2472 shell-file-name and everything will work out hunky-dorey. | |
2473 */ | |
854 | 2474 |
814 | 2475 if (!egetenv ("SHELL")) |
2476 { | |
2367 | 2477 Ibyte *faux_var = alloca_ibytes (7 + qxestrlen (shell)); |
814 | 2478 qxesprintf (faux_var, "SHELL=%s", shell); |
2479 Vprocess_environment = Fcons (build_intstring (faux_var), | |
2480 Vprocess_environment); | |
2481 } | |
2482 #endif /* 0 */ | |
2483 | |
2484 Vshell_file_name = build_intstring (shell); | |
2485 } | |
2486 } | |
2487 | |
428 | 2488 void |
2489 syms_of_process (void) | |
2490 { | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
2491 INIT_LISP_OBJECT (process); |
442 | 2492 |
563 | 2493 DEFSYMBOL (Qprocessp); |
2494 DEFSYMBOL (Qprocess_live_p); | |
2495 DEFSYMBOL (Qrun); | |
2496 DEFSYMBOL (Qstop); | |
2497 DEFSYMBOL (Qopen); | |
2498 DEFSYMBOL (Qclosed); | |
863 | 2499 #if 0 |
2500 /* see comment at Fprocess_readable_p */ | |
2501 DEFSYMBOL (&Qprocess_readable_p); | |
2502 #endif | |
563 | 2503 DEFSYMBOL (Qtcp); |
2504 DEFSYMBOL (Qudp); | |
428 | 2505 |
2506 #ifdef HAVE_MULTICAST | |
563 | 2507 DEFSYMBOL (Qmulticast); /* Used for occasional warnings */ |
428 | 2508 #endif |
2509 | |
563 | 2510 DEFERROR_STANDARD (Qprocess_error, Qio_error); |
2511 DEFERROR_STANDARD (Qnetwork_error, Qio_error); | |
2512 | |
428 | 2513 DEFSUBR (Fprocessp); |
440 | 2514 DEFSUBR (Fprocess_live_p); |
863 | 2515 #if 0 |
2516 /* see comment at Fprocess_readable_p */ | |
2517 DEFSUBR (Fprocess_readable_p); | |
2518 #endif | |
428 | 2519 DEFSUBR (Fget_process); |
2520 DEFSUBR (Fget_buffer_process); | |
2521 DEFSUBR (Fdelete_process); | |
2522 DEFSUBR (Fprocess_status); | |
2523 DEFSUBR (Fprocess_exit_status); | |
2524 DEFSUBR (Fprocess_id); | |
2525 DEFSUBR (Fprocess_name); | |
2526 DEFSUBR (Fprocess_tty_name); | |
2527 DEFSUBR (Fprocess_command); | |
859 | 2528 DEFSUBR (Fprocess_has_separate_stderr_p); |
428 | 2529 DEFSUBR (Fset_process_buffer); |
853 | 2530 DEFSUBR (Fset_process_stderr_buffer); |
428 | 2531 DEFSUBR (Fprocess_buffer); |
2532 DEFSUBR (Fprocess_mark); | |
853 | 2533 DEFSUBR (Fprocess_stderr_buffer); |
2534 DEFSUBR (Fprocess_stderr_mark); | |
428 | 2535 DEFSUBR (Fset_process_filter); |
2536 DEFSUBR (Fprocess_filter); | |
853 | 2537 DEFSUBR (Fset_process_stderr_filter); |
2538 DEFSUBR (Fprocess_stderr_filter); | |
428 | 2539 DEFSUBR (Fset_process_window_size); |
2540 DEFSUBR (Fset_process_sentinel); | |
2541 DEFSUBR (Fprocess_sentinel); | |
2542 DEFSUBR (Fprocess_kill_without_query); | |
2543 DEFSUBR (Fprocess_kill_without_query_p); | |
2544 DEFSUBR (Fprocess_list); | |
2545 DEFSUBR (Fstart_process_internal); | |
2546 #ifdef HAVE_SOCKETS | |
2547 DEFSUBR (Fopen_network_stream_internal); | |
2548 #ifdef HAVE_MULTICAST | |
2549 DEFSUBR (Fopen_multicast_group_internal); | |
2550 #endif /* HAVE_MULTICAST */ | |
2551 #endif /* HAVE_SOCKETS */ | |
2552 DEFSUBR (Fprocess_send_region); | |
2553 DEFSUBR (Fprocess_send_string); | |
442 | 2554 DEFSUBR (Fprocess_send_signal); |
428 | 2555 DEFSUBR (Finterrupt_process); |
2556 DEFSUBR (Fkill_process); | |
2557 DEFSUBR (Fquit_process); | |
2558 DEFSUBR (Fstop_process); | |
2559 DEFSUBR (Fcontinue_process); | |
2560 DEFSUBR (Fprocess_send_eof); | |
2561 DEFSUBR (Fsignal_process); | |
2562 /* DEFSUBR (Fprocess_connection); */ | |
2563 DEFSUBR (Fprocess_input_coding_system); | |
2564 DEFSUBR (Fprocess_output_coding_system); | |
2565 DEFSUBR (Fset_process_input_coding_system); | |
2566 DEFSUBR (Fset_process_output_coding_system); | |
2567 DEFSUBR (Fprocess_coding_system); | |
2568 DEFSUBR (Fset_process_coding_system); | |
814 | 2569 DEFSUBR (Fgetenv); |
428 | 2570 } |
2571 | |
2572 void | |
2573 vars_of_process (void) | |
2574 { | |
2575 Fprovide (intern ("subprocesses")); | |
2576 #ifdef HAVE_SOCKETS | |
2577 Fprovide (intern ("network-streams")); | |
2578 #ifdef HAVE_MULTICAST | |
2579 Fprovide (intern ("multicast")); | |
2580 #endif /* HAVE_MULTICAST */ | |
2581 #endif /* HAVE_SOCKETS */ | |
2582 staticpro (&Vprocess_list); | |
2583 | |
2584 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes /* | |
2585 *Non-nil means delete processes immediately when they exit. | |
2586 nil means don't delete them until `list-processes' is run. | |
2587 */ ); | |
2588 | |
2589 delete_exited_processes = 1; | |
2590 | |
442 | 2591 DEFVAR_CONST_LISP ("null-device", &Vnull_device /* |
2592 Name of the null device, which differs from system to system. | |
2593 The null device is a filename that acts as a sink for arbitrary amounts of | |
2594 data, which is discarded, or as a source for a zero-length file. | |
2595 It is available on all the systems that we currently support, but with | |
2596 different names (typically either `/dev/null' or `nul'). | |
2597 | |
2598 Note that there is also a /dev/zero on most modern Unix versions (including | |
2599 Cygwin), which acts like /dev/null when used as a sink, but as a source | |
2600 it sends a non-ending stream of zero bytes. It's used most often along | |
2601 with memory-mapping. We don't provide a Lisp variable for this because | |
2602 the operations needing this are lower level than what ELisp programs | |
2603 typically do, and in any case no equivalent exists under native MS Windows. | |
2604 */ ); | |
2605 Vnull_device = build_string (NULL_DEVICE); | |
2606 | |
428 | 2607 DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type /* |
2608 Control type of device used to communicate with subprocesses. | |
2609 Values are nil to use a pipe, or t or `pty' to use a pty. | |
2610 The value has no effect if the system has no ptys or if all ptys are busy: | |
2611 then a pipe is used in any case. | |
2612 The value takes effect when `start-process' is called. | |
2613 */ ); | |
2614 Vprocess_connection_type = Qt; | |
2615 | |
2616 DEFVAR_BOOL ("windowed-process-io", &windowed_process_io /* | |
2617 Enables input/output on standard handles of a windowed process. | |
2618 When this variable is nil (the default), XEmacs does not attempt to read | |
2619 standard output handle of a windowed process. Instead, the process is | |
2620 immediately marked as exited immediately upon successful launching. This is | |
2621 done because normal windowed processes do not use standard I/O, as they are | |
2622 not connected to any console. | |
2623 | |
2624 When launching a specially crafted windowed process, which expects to be | |
2625 launched by XEmacs, or by other program which pipes its standard input and | |
2626 output, this variable must be set to non-nil, in which case XEmacs will | |
2627 treat this process just like a console process. | |
2628 | |
2629 NOTE: You should never set this variable, only bind it. | |
2630 | |
2631 Only Windows processes can be "windowed" or "console". This variable has no | |
2632 effect on UNIX processes, because all UNIX processes are "console". | |
2633 */ ); | |
2634 windowed_process_io = 0; | |
2635 | |
771 | 2636 DEFVAR_INT ("debug-process-io", &debug_process_io /* |
2637 If non-zero, display data sent to or received from a process. | |
2638 */ ); | |
2639 debug_process_io = 0; | |
2640 | |
2641 DEFVAR_LISP ("default-process-coding-system", | |
2642 &Vdefault_process_coding_system /* | |
2643 Cons of coding systems used for process I/O by default. | |
4729
428d7c571110
Fix issue145: accept nil in default-process-coding-system.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4693
diff
changeset
|
2644 May also be nil, interpreted as (nil . nil). |
771 | 2645 The car part is used for reading (decoding) data from a process, and |
2646 the cdr part is used for writing (encoding) data to a process. | |
2647 */ ); | |
4729
428d7c571110
Fix issue145: accept nil in default-process-coding-system.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4693
diff
changeset
|
2648 /* Better, system-dependent defaults are set in code-init.el. */ |
771 | 2649 Vdefault_process_coding_system = Fcons (Qundecided, Qnil); |
2650 | |
853 | 2651 DEFVAR_LISP ("default-network-coding-system", |
2652 &Vdefault_network_coding_system /* | |
2653 Cons of coding systems used for network I/O by default. | |
4729
428d7c571110
Fix issue145: accept nil in default-process-coding-system.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4693
diff
changeset
|
2654 May also be nil, interpreted as (nil . nil). |
853 | 2655 The car part is used for reading (decoding) data from a process, and |
2656 the cdr part is used for writing (encoding) data to a process. | |
2657 */ ); | |
2658 Vdefault_network_coding_system = Fcons (Qundecided, Qnil); | |
2659 | |
428 | 2660 #ifdef PROCESS_IO_BLOCKING |
2661 DEFVAR_LISP ("network-stream-blocking-port-list", &network_stream_blocking_port_list /* | |
2662 List of port numbers or port names to set a blocking I/O mode with connection. | |
862 | 2663 Nil value means to set a default (non-blocking) I/O mode. |
428 | 2664 The value takes effect when `open-network-stream-internal' is called. |
2665 */ ); | |
2666 network_stream_blocking_port_list = Qnil; | |
2667 #endif /* PROCESS_IO_BLOCKING */ | |
814 | 2668 |
2669 /* This function can GC */ | |
2670 DEFVAR_LISP ("shell-file-name", &Vshell_file_name /* | |
2671 *File name to load inferior shells from. | |
2672 Initialized from the SHELL environment variable. | |
2673 */ ); | |
428 | 2674 |
4729
428d7c571110
Fix issue145: accept nil in default-process-coding-system.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4693
diff
changeset
|
2675 /* ben? thinks the format of this variable is "semi-bogus". |
428d7c571110
Fix issue145: accept nil in default-process-coding-system.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4693
diff
changeset
|
2676 sjt doesn't agree, since it captures a restriction that is |
428d7c571110
Fix issue145: accept nil in default-process-coding-system.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4693
diff
changeset
|
2677 present in POSIX shells, after all. */ |
814 | 2678 DEFVAR_LISP ("process-environment", &Vprocess_environment /* |
2679 List of environment variables for subprocesses to inherit. | |
2680 Each element should be a string of the form ENVVARNAME=VALUE. | |
2681 The environment which Emacs inherits is placed in this variable | |
2682 when Emacs starts. | |
2683 */ ); | |
2684 | |
2685 Vlisp_EXEC_SUFFIXES = build_string (EXEC_SUFFIXES); | |
2686 staticpro (&Vlisp_EXEC_SUFFIXES); | |
2687 } |