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