611
|
1 /* Old synchronous subprocess invocation for XEmacs.
|
428
|
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: Mule 2.0, FSF 19.30. */
|
|
22 /* Partly sync'ed with 19.36.4 */
|
|
23
|
611
|
24
|
|
25 /* #### This ENTIRE file is only used in batch mode.
|
|
26
|
|
27 We only need two things to get rid of both this and ntproc.c:
|
|
28
|
|
29 -- my `stderr-proc' ws, which adds support for a separate stderr
|
|
30 in asynch. subprocesses. (it's a feature in `old-call-process-internal'.)
|
|
31 -- a noninteractive event loop that supports processes.
|
|
32 */
|
|
33
|
428
|
34 #include <config.h>
|
|
35 #include "lisp.h"
|
|
36
|
|
37 #include "buffer.h"
|
|
38 #include "commands.h"
|
|
39 #include "insdel.h"
|
|
40 #include "lstream.h"
|
|
41 #include "process.h"
|
|
42 #include "sysdep.h"
|
|
43 #include "window.h"
|
|
44 #ifdef FILE_CODING
|
|
45 #include "file-coding.h"
|
|
46 #endif
|
|
47
|
|
48 #include "systime.h"
|
|
49 #include "sysproc.h"
|
|
50 #include "sysfile.h" /* Always include after sysproc.h */
|
|
51 #include "syssignal.h" /* Always include before systty.h */
|
|
52 #include "systty.h"
|
|
53
|
442
|
54 #ifdef WIN32_NATIVE
|
428
|
55 #define _P_NOWAIT 1 /* from process.h */
|
|
56 #include "nt.h"
|
|
57 #endif
|
|
58
|
442
|
59 #ifdef WIN32_NATIVE
|
428
|
60 /* When we are starting external processes we need to know whether they
|
|
61 take binary input (no conversion) or text input (\n is converted to
|
|
62 \r\n). Similarly for output: if newlines are written as \r\n then it's
|
|
63 text process output, otherwise it's binary. */
|
|
64 Lisp_Object Vbinary_process_input;
|
|
65 Lisp_Object Vbinary_process_output;
|
442
|
66 #endif /* WIN32_NATIVE */
|
428
|
67
|
|
68 Lisp_Object Vshell_file_name;
|
|
69
|
|
70 /* The environment to pass to all subprocesses when they are started.
|
|
71 This is in the semi-bogus format of ("VAR=VAL" "VAR2=VAL2" ... )
|
|
72 */
|
|
73 Lisp_Object Vprocess_environment;
|
|
74
|
|
75 /* True iff we are about to fork off a synchronous process or if we
|
|
76 are waiting for it. */
|
|
77 volatile int synch_process_alive;
|
|
78
|
|
79 /* Nonzero => this is a string explaining death of synchronous subprocess. */
|
442
|
80 const char *synch_process_death;
|
428
|
81
|
|
82 /* If synch_process_death is zero,
|
|
83 this is exit code of synchronous subprocess. */
|
|
84 int synch_process_retcode;
|
|
85
|
|
86 /* Clean up when exiting Fcall_process_internal.
|
442
|
87 On Windows, delete the temporary file on any kind of termination.
|
428
|
88 On Unix, kill the process and any children on termination by signal. */
|
|
89
|
|
90 /* Nonzero if this is termination due to exit. */
|
|
91 static int call_process_exited;
|
|
92
|
|
93 Lisp_Object Vlisp_EXEC_SUFFIXES;
|
|
94
|
|
95 static Lisp_Object
|
|
96 call_process_kill (Lisp_Object fdpid)
|
|
97 {
|
|
98 Lisp_Object fd = Fcar (fdpid);
|
|
99 Lisp_Object pid = Fcdr (fdpid);
|
|
100
|
|
101 if (!NILP (fd))
|
|
102 close (XINT (fd));
|
|
103
|
|
104 if (!NILP (pid))
|
|
105 EMACS_KILLPG (XINT (pid), SIGKILL);
|
|
106
|
|
107 synch_process_alive = 0;
|
|
108 return Qnil;
|
|
109 }
|
|
110
|
|
111 static Lisp_Object
|
|
112 call_process_cleanup (Lisp_Object fdpid)
|
|
113 {
|
440
|
114 int fd = XINT (Fcar (fdpid));
|
428
|
115 int pid = XINT (Fcdr (fdpid));
|
|
116
|
|
117 if (!call_process_exited &&
|
|
118 EMACS_KILLPG (pid, SIGINT) == 0)
|
|
119 {
|
|
120 int speccount = specpdl_depth ();
|
|
121
|
|
122 record_unwind_protect (call_process_kill, fdpid);
|
|
123 /* #### "c-G" -- need non-consing Single-key-description */
|
|
124 message ("Waiting for process to die...(type C-g again to kill it instantly)");
|
|
125
|
442
|
126 #ifdef WIN32_NATIVE
|
440
|
127 {
|
|
128 HANDLE pHandle = OpenProcess (PROCESS_ALL_ACCESS, 0, pid);
|
|
129 if (pHandle == NULL)
|
432
|
130 warn_when_safe (Qprocess, Qwarning,
|
|
131 "cannot open process (PID %d) for cleanup", pid);
|
440
|
132 else
|
|
133 wait_for_termination (pHandle);
|
|
134 }
|
432
|
135 #else
|
428
|
136 wait_for_termination (pid);
|
432
|
137 #endif
|
428
|
138
|
|
139 /* "Discard" the unwind protect. */
|
|
140 XCAR (fdpid) = Qnil;
|
|
141 XCDR (fdpid) = Qnil;
|
|
142 unbind_to (speccount, Qnil);
|
|
143
|
|
144 message ("Waiting for process to die... done");
|
|
145 }
|
|
146 synch_process_alive = 0;
|
|
147 close (fd);
|
|
148 return Qnil;
|
|
149 }
|
|
150
|
442
|
151 DEFUN ("old-call-process-internal", Fold_call_process_internal, 1, MANY, 0, /*
|
428
|
152 Call PROGRAM synchronously in separate process, with coding-system specified.
|
|
153 Arguments are
|
|
154 (PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS).
|
|
155 The program's input comes from file INFILE (nil means `/dev/null').
|
|
156 Insert output in BUFFER before point; t means current buffer;
|
|
157 nil for BUFFER means discard it; 0 means discard and don't wait.
|
|
158 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
|
|
159 REAL-BUFFER says what to do with standard output, as above,
|
|
160 while STDERR-FILE says what to do with standard error in the child.
|
|
161 STDERR-FILE may be nil (discard standard error output),
|
|
162 t (mix it with ordinary output), or a file name string.
|
|
163
|
|
164 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
|
|
165 Remaining arguments are strings passed as command arguments to PROGRAM.
|
|
166
|
|
167 If BUFFER is 0, `call-process' returns immediately with value nil.
|
|
168 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
|
|
169 or a signal description string.
|
|
170 If you quit, the process is killed with SIGINT, or SIGKILL if you
|
|
171 quit again.
|
|
172 */
|
|
173 (int nargs, Lisp_Object *args))
|
|
174 {
|
|
175 /* This function can GC */
|
|
176 Lisp_Object infile, buffer, current_dir, display, path;
|
|
177 int fd[2];
|
|
178 int filefd;
|
442
|
179 #ifdef WIN32_NATIVE
|
432
|
180 HANDLE pHandle;
|
|
181 #endif
|
428
|
182 int pid;
|
|
183 char buf[16384];
|
|
184 char *bufptr = buf;
|
|
185 int bufsize = 16384;
|
|
186 int speccount = specpdl_depth ();
|
442
|
187 struct gcpro gcpro1, gcpro2, gcpro3;
|
428
|
188 char **new_argv = alloca_array (char *, max (2, nargs - 2));
|
|
189
|
|
190 /* File to use for stderr in the child.
|
|
191 t means use same as standard output. */
|
|
192 Lisp_Object error_file;
|
|
193
|
|
194 CHECK_STRING (args[0]);
|
|
195
|
|
196 error_file = Qt;
|
|
197
|
|
198 #if defined (NO_SUBPROCESSES)
|
|
199 /* Without asynchronous processes we cannot have BUFFER == 0. */
|
|
200 if (nargs >= 3 && !INTP (args[2]))
|
563
|
201 signal_error (Qunimplemented, "Operating system cannot handle asynchronous subprocesses", Qunbound);
|
428
|
202 #endif /* NO_SUBPROCESSES */
|
|
203
|
502
|
204 /* Do all filename munging before building new_argv because GC in
|
|
205 * Lisp code called by various filename-hacking routines might
|
|
206 * relocate strings */
|
428
|
207 locate_file (Vexec_path, args[0], Vlisp_EXEC_SUFFIXES, &path, X_OK);
|
|
208
|
|
209 /* Make sure that the child will be able to chdir to the current
|
502
|
210 buffer's current directory, or its unhandled equivalent. [[ We
|
428
|
211 can't just have the child check for an error when it does the
|
502
|
212 chdir, since it's in a vfork. ]] -- not any more, we don't use
|
|
213 vfork. -ben
|
|
214
|
|
215 Note: These calls are spread out to insure that the return values
|
|
216 of the calls (which may be newly-created strings) are properly
|
|
217 GC-protected. */
|
428
|
218 {
|
|
219 struct gcpro ngcpro1, ngcpro2;
|
502
|
220 NGCPRO2 (current_dir, path); /* Caller gcprotects args[] */
|
446
|
221 current_dir = current_buffer->directory;
|
502
|
222 /* If the current dir has no terminating slash, we'll get undesirable
|
|
223 results, so put the slash back. */
|
|
224 current_dir = Ffile_name_as_directory (current_dir);
|
428
|
225 current_dir = Funhandled_file_name_directory (current_dir);
|
|
226 current_dir = expand_and_dir_to_file (current_dir, Qnil);
|
502
|
227
|
428
|
228 #if 0
|
|
229 /* This is in FSF, but it breaks everything in the presence of
|
|
230 ange-ftp-visited files, so away with it. */
|
|
231 if (NILP (Ffile_accessible_directory_p (current_dir)))
|
563
|
232 signal_error (Qprocess_error, "Setting current directory",
|
|
233 current_buffer->directory);
|
428
|
234 #endif /* 0 */
|
|
235 NUNGCPRO;
|
|
236 }
|
|
237
|
442
|
238 GCPRO2 (current_dir, path);
|
428
|
239
|
|
240 if (nargs >= 2 && ! NILP (args[1]))
|
|
241 {
|
|
242 struct gcpro ngcpro1;
|
|
243 NGCPRO1 (current_buffer->directory);
|
|
244 infile = Fexpand_file_name (args[1], current_buffer->directory);
|
|
245 NUNGCPRO;
|
|
246 CHECK_STRING (infile);
|
|
247 }
|
|
248 else
|
|
249 infile = build_string (NULL_DEVICE);
|
|
250
|
|
251 UNGCPRO;
|
|
252
|
442
|
253 GCPRO3 (infile, current_dir, path); /* Fexpand_file_name might trash it */
|
428
|
254
|
|
255 if (nargs >= 3)
|
|
256 {
|
|
257 buffer = args[2];
|
|
258
|
|
259 /* If BUFFER is a list, its meaning is
|
|
260 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
|
|
261 if (CONSP (buffer))
|
|
262 {
|
|
263 if (CONSP (XCDR (buffer)))
|
|
264 {
|
|
265 Lisp_Object file_for_stderr = XCAR (XCDR (buffer));
|
|
266
|
|
267 if (NILP (file_for_stderr) || EQ (Qt, file_for_stderr))
|
|
268 error_file = file_for_stderr;
|
|
269 else
|
|
270 error_file = Fexpand_file_name (file_for_stderr, Qnil);
|
|
271 }
|
|
272
|
|
273 buffer = XCAR (buffer);
|
|
274 }
|
|
275
|
|
276 if (!(EQ (buffer, Qnil)
|
|
277 || EQ (buffer, Qt)
|
|
278 || ZEROP (buffer)))
|
|
279 {
|
|
280 Lisp_Object spec_buffer = buffer;
|
|
281 buffer = Fget_buffer (buffer);
|
|
282 /* Mention the buffer name for a better error message. */
|
|
283 if (NILP (buffer))
|
|
284 CHECK_BUFFER (spec_buffer);
|
|
285 CHECK_BUFFER (buffer);
|
|
286 }
|
|
287 }
|
|
288 else
|
|
289 buffer = Qnil;
|
|
290
|
|
291 UNGCPRO;
|
|
292
|
|
293 display = ((nargs >= 4) ? args[3] : Qnil);
|
|
294
|
|
295 /* From here we assume we won't GC (unless an error is signaled). */
|
|
296 {
|
|
297 REGISTER int i;
|
|
298 for (i = 4; i < nargs; i++)
|
|
299 {
|
|
300 CHECK_STRING (args[i]);
|
|
301 new_argv[i - 3] = (char *) XSTRING_DATA (args[i]);
|
|
302 }
|
|
303 }
|
430
|
304 new_argv[max(nargs - 3,1)] = 0;
|
428
|
305
|
|
306 if (NILP (path))
|
563
|
307 signal_error (Qprocess_error, "Searching for program", args[0]);
|
428
|
308 new_argv[0] = (char *) XSTRING_DATA (path);
|
|
309
|
|
310 filefd = open ((char *) XSTRING_DATA (infile), O_RDONLY | OPEN_BINARY, 0);
|
|
311 if (filefd < 0)
|
563
|
312 report_process_error ("Opening process input file", infile);
|
428
|
313
|
|
314 if (INTP (buffer))
|
|
315 {
|
|
316 fd[1] = open (NULL_DEVICE, O_WRONLY | OPEN_BINARY, 0);
|
|
317 fd[0] = -1;
|
|
318 }
|
|
319 else
|
|
320 {
|
|
321 pipe (fd);
|
|
322 #if 0
|
|
323 /* Replaced by close_process_descs */
|
|
324 set_exclusive_use (fd[0]);
|
|
325 #endif
|
|
326 }
|
|
327
|
|
328 {
|
|
329 /* child_setup must clobber environ in systems with true vfork.
|
|
330 Protect it from permanent change. */
|
|
331 REGISTER char **save_environ = environ;
|
|
332 REGISTER int fd1 = fd[1];
|
|
333 int fd_error = fd1;
|
|
334
|
|
335 /* Record that we're about to create a synchronous process. */
|
|
336 synch_process_alive = 1;
|
|
337
|
|
338 /* These vars record information from process termination.
|
|
339 Clear them now before process can possibly terminate,
|
|
340 to avoid timing error if process terminates soon. */
|
|
341 synch_process_death = 0;
|
|
342 synch_process_retcode = 0;
|
|
343
|
|
344 if (NILP (error_file))
|
|
345 fd_error = open (NULL_DEVICE, O_WRONLY | OPEN_BINARY);
|
|
346 else if (STRINGP (error_file))
|
|
347 {
|
442
|
348 fd_error = open ((const char *) XSTRING_DATA (error_file),
|
|
349 #ifdef WIN32_NATIVE
|
428
|
350 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
|
|
351 S_IREAD | S_IWRITE
|
442
|
352 #else /* not WIN32_NATIVE */
|
428
|
353 O_WRONLY | O_TRUNC | O_CREAT | OPEN_BINARY,
|
|
354 CREAT_MODE
|
442
|
355 #endif /* not WIN32_NATIVE */
|
428
|
356 );
|
|
357 }
|
|
358
|
|
359 if (fd_error < 0)
|
|
360 {
|
442
|
361 int save_errno = errno;
|
428
|
362 close (filefd);
|
|
363 close (fd[0]);
|
|
364 if (fd1 >= 0)
|
|
365 close (fd1);
|
442
|
366 errno = save_errno;
|
563
|
367 report_process_error ("Cannot open", error_file);
|
428
|
368 }
|
|
369
|
442
|
370 #ifdef WIN32_NATIVE
|
428
|
371 pid = child_setup (filefd, fd1, fd_error, new_argv,
|
|
372 (char *) XSTRING_DATA (current_dir));
|
432
|
373 if (!INTP (buffer))
|
|
374 {
|
|
375 /* OpenProcess() as soon after child_setup as possible. It's too
|
|
376 late once the process terminated. */
|
|
377 pHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
|
|
378 #if 0
|
|
379 if (pHandle == NULL)
|
|
380 {
|
|
381 /* #### seems to cause crash in unbind_to(...) below. APA */
|
|
382 warn_when_safe (Qprocess, Qwarning,
|
|
383 "cannot open process to wait for");
|
|
384 }
|
|
385 #endif
|
|
386 }
|
|
387 /* Close STDERR into the parent process. We no longer need it. */
|
|
388 if (fd_error >= 0)
|
|
389 close (fd_error);
|
442
|
390 #else /* not WIN32_NATIVE */
|
428
|
391 pid = fork ();
|
|
392
|
|
393 if (pid == 0)
|
|
394 {
|
|
395 if (fd[0] >= 0)
|
|
396 close (fd[0]);
|
|
397 /* This is necessary because some shells may attempt to
|
|
398 access the current controlling terminal and will hang
|
|
399 if they are run in the background, as will be the case
|
|
400 when XEmacs is started in the background. Martin
|
|
401 Buchholz observed this problem running a subprocess
|
|
402 that used zsh to call gzip to uncompress an info
|
|
403 file. */
|
|
404 disconnect_controlling_terminal ();
|
|
405 child_setup (filefd, fd1, fd_error, new_argv,
|
|
406 (char *) XSTRING_DATA (current_dir));
|
|
407 }
|
|
408 if (fd_error >= 0)
|
|
409 close (fd_error);
|
|
410
|
442
|
411 #endif /* not WIN32_NATIVE */
|
428
|
412
|
|
413 environ = save_environ;
|
|
414
|
|
415 /* Close most of our fd's, but not fd[0]
|
|
416 since we will use that to read input from. */
|
|
417 close (filefd);
|
|
418 if (fd1 >= 0)
|
|
419 close (fd1);
|
|
420 }
|
|
421
|
442
|
422 #ifndef WIN32_NATIVE
|
428
|
423 if (pid < 0)
|
|
424 {
|
442
|
425 int save_errno = errno;
|
428
|
426 if (fd[0] >= 0)
|
|
427 close (fd[0]);
|
442
|
428 errno = save_errno;
|
563
|
429 report_process_error ("Doing fork", Qunbound);
|
428
|
430 }
|
432
|
431 #endif
|
428
|
432
|
|
433 if (INTP (buffer))
|
|
434 {
|
|
435 if (fd[0] >= 0)
|
|
436 close (fd[0]);
|
|
437 #if defined (NO_SUBPROCESSES)
|
|
438 /* If Emacs has been built with asynchronous subprocess support,
|
|
439 we don't need to do this, I think because it will then have
|
|
440 the facilities for handling SIGCHLD. */
|
|
441 wait_without_blocking ();
|
|
442 #endif /* NO_SUBPROCESSES */
|
|
443 return Qnil;
|
|
444 }
|
|
445
|
|
446 {
|
|
447 int nread;
|
|
448 int total_read = 0;
|
|
449 Lisp_Object instream;
|
|
450 struct gcpro ngcpro1;
|
|
451
|
|
452 /* Enable sending signal if user quits below. */
|
|
453 call_process_exited = 0;
|
|
454
|
|
455 record_unwind_protect (call_process_cleanup,
|
|
456 Fcons (make_int (fd[0]), make_int (pid)));
|
|
457
|
|
458 /* FSFmacs calls Fset_buffer() here. We don't have to because
|
|
459 we can insert into buffers other than the current one. */
|
|
460 if (EQ (buffer, Qt))
|
|
461 XSETBUFFER (buffer, current_buffer);
|
|
462 instream = make_filedesc_input_stream (fd[0], 0, -1, LSTR_ALLOW_QUIT);
|
|
463 #ifdef FILE_CODING
|
|
464 instream =
|
|
465 make_decoding_input_stream
|
|
466 (XLSTREAM (instream),
|
|
467 Fget_coding_system (Vcoding_system_for_read));
|
|
468 Lstream_set_character_mode (XLSTREAM (instream));
|
|
469 #endif
|
|
470 NGCPRO1 (instream);
|
|
471 while (1)
|
|
472 {
|
|
473 QUIT;
|
|
474 /* Repeatedly read until we've filled as much as possible
|
|
475 of the buffer size we have. But don't read
|
|
476 less than 1024--save that for the next bufferfull. */
|
|
477
|
|
478 nread = 0;
|
|
479 while (nread < bufsize - 1024)
|
|
480 {
|
665
|
481 Bytecount this_read
|
428
|
482 = Lstream_read (XLSTREAM (instream), bufptr + nread,
|
|
483 bufsize - nread);
|
|
484
|
|
485 if (this_read < 0)
|
|
486 goto give_up;
|
|
487
|
|
488 if (this_read == 0)
|
|
489 goto give_up_1;
|
|
490
|
|
491 nread += this_read;
|
|
492 }
|
|
493
|
|
494 give_up_1:
|
|
495
|
|
496 /* Now NREAD is the total amount of data in the buffer. */
|
|
497 if (nread == 0)
|
|
498 break;
|
|
499
|
440
|
500 #if 0
|
442
|
501 #ifdef WIN32_NATIVE
|
428
|
502 /* Until we pull out of MULE things like
|
|
503 make_decoding_input_stream(), we do the following which is
|
|
504 less elegant. --marcpa */
|
440
|
505 /* We did. -- kkm */
|
428
|
506 {
|
|
507 int lf_count = 0;
|
|
508 if (NILP (Vbinary_process_output)) {
|
|
509 nread = crlf_to_lf(nread, bufptr, &lf_count);
|
|
510 }
|
|
511 }
|
|
512 #endif
|
440
|
513 #endif
|
428
|
514
|
|
515 total_read += nread;
|
|
516
|
|
517 if (!NILP (buffer))
|
665
|
518 buffer_insert_raw_string (XBUFFER (buffer), (Intbyte *) bufptr,
|
428
|
519 nread);
|
|
520
|
|
521 /* Make the buffer bigger as we continue to read more data,
|
|
522 but not past 64k. */
|
|
523 if (bufsize < 64 * 1024 && total_read > 32 * bufsize)
|
|
524 {
|
|
525 bufsize *= 2;
|
|
526 bufptr = (char *) alloca (bufsize);
|
|
527 }
|
|
528
|
|
529 if (!NILP (display) && INTERACTIVE)
|
|
530 {
|
|
531 redisplay ();
|
|
532 }
|
|
533 }
|
|
534 give_up:
|
|
535 Lstream_close (XLSTREAM (instream));
|
|
536 NUNGCPRO;
|
|
537
|
|
538 QUIT;
|
|
539 /* Wait for it to terminate, unless it already has. */
|
442
|
540 #ifdef WIN32_NATIVE
|
432
|
541 wait_for_termination (pHandle);
|
|
542 #else
|
428
|
543 wait_for_termination (pid);
|
432
|
544 #endif
|
428
|
545
|
|
546 /* Don't kill any children that the subprocess may have left behind
|
|
547 when exiting. */
|
|
548 call_process_exited = 1;
|
|
549 unbind_to (speccount, Qnil);
|
|
550
|
|
551 if (synch_process_death)
|
|
552 return build_string (synch_process_death);
|
|
553 return make_int (synch_process_retcode);
|
|
554 }
|
|
555 }
|
|
556
|
|
557
|
|
558
|
|
559 /* Move the file descriptor FD so that its number is not less than MIN. *
|
|
560 The original file descriptor remains open. */
|
|
561 static int
|
|
562 relocate_fd (int fd, int min)
|
|
563 {
|
|
564 if (fd >= min)
|
|
565 return fd;
|
|
566 else
|
|
567 {
|
|
568 int newfd = dup (fd);
|
|
569 if (newfd == -1)
|
|
570 {
|
|
571 stderr_out ("Error while setting up child: %s\n",
|
|
572 strerror (errno));
|
|
573 _exit (1);
|
|
574 }
|
|
575 return relocate_fd (newfd, min);
|
|
576 }
|
|
577 }
|
|
578
|
|
579 /* This is the last thing run in a newly forked inferior
|
|
580 either synchronous or asynchronous.
|
|
581 Copy descriptors IN, OUT and ERR
|
|
582 as descriptors STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO.
|
|
583 Initialize inferior's priority, pgrp, connected dir and environment.
|
|
584 then exec another program based on new_argv.
|
|
585
|
|
586 This function may change environ for the superior process.
|
|
587 Therefore, the superior process must save and restore the value
|
|
588 of environ around the fork and the call to this function.
|
|
589
|
|
590 ENV is the environment for the subprocess.
|
|
591
|
|
592 XEmacs: We've removed the SET_PGRP argument because it's already
|
|
593 done by the callers of child_setup.
|
|
594
|
|
595 CURRENT_DIR is an elisp string giving the path of the current
|
|
596 directory the subprocess should have. Since we can't really signal
|
|
597 a decent error from within the child, this should be verified as an
|
|
598 executable directory by the parent. */
|
|
599
|
442
|
600 #ifdef WIN32_NATIVE
|
428
|
601 int
|
|
602 #else
|
|
603 void
|
|
604 #endif
|
|
605 child_setup (int in, int out, int err, char **new_argv,
|
442
|
606 const char *current_dir)
|
428
|
607 {
|
|
608 char **env;
|
|
609 char *pwd;
|
442
|
610 #ifdef WIN32_NATIVE
|
428
|
611 int cpid;
|
|
612 HANDLE handles[4];
|
442
|
613 #endif /* WIN32_NATIVE */
|
428
|
614
|
|
615 #ifdef SET_EMACS_PRIORITY
|
|
616 if (emacs_priority != 0)
|
|
617 nice (- emacs_priority);
|
|
618 #endif
|
|
619
|
442
|
620 /* Under Windows, we are not in a child process at all, so we should
|
|
621 not close handles inherited from the parent -- we are the parent
|
|
622 and doing so will screw up all manner of things! Similarly, most
|
|
623 of the rest of the cleanup done in this function is not done
|
|
624 under Windows.
|
|
625
|
|
626 #### This entire child_setup() function is an utter and complete
|
|
627 piece of shit. I would rewrite it, at the very least splitting
|
|
628 out the Windows and non-Windows stuff into two completely
|
|
629 different functions; but instead I'm trying to make it go away
|
|
630 entirely, using the Lisp definition in process.el. What's left
|
|
631 is to fix up the routines in event-msw.c (and in event-Xt.c and
|
|
632 event-tty.c) to allow for stream devices to be handled correctly.
|
|
633 There isn't much to do, in fact, and I'll fix it shortly. That
|
|
634 way, the Lisp definition can be used non-interactively too. */
|
|
635 #if !defined (NO_SUBPROCESSES) && !defined (WIN32_NATIVE)
|
428
|
636 /* Close Emacs's descriptors that this process should not have. */
|
|
637 close_process_descs ();
|
|
638 #endif /* not NO_SUBPROCESSES */
|
442
|
639 #ifndef WIN32_NATIVE
|
428
|
640 close_load_descs ();
|
442
|
641 #endif
|
428
|
642
|
|
643 /* Note that use of alloca is always safe here. It's obvious for systems
|
|
644 that do not have true vfork or that have true (stack) alloca.
|
|
645 If using vfork and C_ALLOCA it is safe because that changes
|
|
646 the superior's static variables as if the superior had done alloca
|
|
647 and will be cleaned up in the usual way. */
|
|
648 {
|
|
649 REGISTER int i;
|
|
650
|
|
651 i = strlen (current_dir);
|
|
652 pwd = alloca_array (char, i + 6);
|
|
653 memcpy (pwd, "PWD=", 4);
|
|
654 memcpy (pwd + 4, current_dir, i);
|
|
655 i += 4;
|
|
656 if (!IS_DIRECTORY_SEP (pwd[i - 1]))
|
|
657 pwd[i++] = DIRECTORY_SEP;
|
|
658 pwd[i] = 0;
|
|
659
|
|
660 /* We can't signal an Elisp error here; we're in a vfork. Since
|
|
661 the callers check the current directory before forking, this
|
|
662 should only return an error if the directory's permissions
|
|
663 are changed between the check and this chdir, but we should
|
|
664 at least check. */
|
|
665 if (chdir (pwd + 4) < 0)
|
|
666 {
|
|
667 /* Don't report the chdir error, or ange-ftp.el doesn't work. */
|
|
668 /* (FSFmacs does _exit (errno) here.) */
|
|
669 pwd = 0;
|
|
670 }
|
|
671 else
|
|
672 {
|
|
673 /* Strip trailing "/". Cretinous *[]&@$#^%@#$% Un*x */
|
|
674 /* leave "//" (from FSF) */
|
|
675 while (i > 6 && IS_DIRECTORY_SEP (pwd[i - 1]))
|
|
676 pwd[--i] = 0;
|
|
677 }
|
|
678 }
|
|
679
|
|
680 /* Set `env' to a vector of the strings in Vprocess_environment. */
|
|
681 /* + 2 to include PWD and terminating 0. */
|
|
682 env = alloca_array (char *, XINT (Flength (Vprocess_environment)) + 2);
|
|
683 {
|
|
684 REGISTER Lisp_Object tail;
|
|
685 char **new_env = env;
|
|
686
|
|
687 /* If we have a PWD envvar and we know the real current directory,
|
|
688 pass one down, but with corrected value. */
|
|
689 if (pwd && getenv ("PWD"))
|
|
690 *new_env++ = pwd;
|
|
691
|
|
692 /* Copy the Vprocess_environment strings into new_env. */
|
|
693 for (tail = Vprocess_environment;
|
|
694 CONSP (tail) && STRINGP (XCAR (tail));
|
|
695 tail = XCDR (tail))
|
|
696 {
|
|
697 char **ep = env;
|
|
698 char *envvar_external;
|
|
699
|
440
|
700 TO_EXTERNAL_FORMAT (LISP_STRING, XCAR (tail),
|
|
701 C_STRING_ALLOCA, envvar_external,
|
|
702 Qfile_name);
|
428
|
703
|
|
704 /* See if envvar_external duplicates any string already in the env.
|
|
705 If so, don't put it in.
|
|
706 When an env var has multiple definitions,
|
|
707 we keep the definition that comes first in process-environment. */
|
|
708 for (; ep != new_env; ep++)
|
|
709 {
|
|
710 char *p = *ep, *q = envvar_external;
|
|
711 while (1)
|
|
712 {
|
|
713 if (*q == 0)
|
|
714 /* The string is malformed; might as well drop it. */
|
|
715 goto duplicate;
|
|
716 if (*q != *p)
|
|
717 break;
|
|
718 if (*q == '=')
|
|
719 goto duplicate;
|
|
720 p++, q++;
|
|
721 }
|
|
722 }
|
|
723 if (pwd && !strncmp ("PWD=", envvar_external, 4))
|
|
724 {
|
|
725 *new_env++ = pwd;
|
|
726 pwd = 0;
|
|
727 }
|
|
728 else
|
|
729 *new_env++ = envvar_external;
|
|
730
|
|
731 duplicate: ;
|
|
732 }
|
|
733 *new_env = 0;
|
|
734 }
|
|
735
|
442
|
736 #ifdef WIN32_NATIVE
|
428
|
737 prepare_standard_handles (in, out, err, handles);
|
|
738 set_process_dir (current_dir);
|
442
|
739 #else /* not WIN32_NATIVE */
|
428
|
740 /* Make sure that in, out, and err are not actually already in
|
|
741 descriptors zero, one, or two; this could happen if Emacs is
|
|
742 started with its standard in, out, or error closed, as might
|
|
743 happen under X. */
|
|
744 in = relocate_fd (in, 3);
|
|
745 out = relocate_fd (out, 3);
|
|
746 err = relocate_fd (err, 3);
|
|
747
|
|
748 /* Set the standard input/output channels of the new process. */
|
|
749 close (STDIN_FILENO);
|
|
750 close (STDOUT_FILENO);
|
|
751 close (STDERR_FILENO);
|
|
752
|
|
753 dup2 (in, STDIN_FILENO);
|
|
754 dup2 (out, STDOUT_FILENO);
|
|
755 dup2 (err, STDERR_FILENO);
|
|
756
|
|
757 close (in);
|
|
758 close (out);
|
|
759 close (err);
|
|
760
|
|
761 /* I can't think of any reason why child processes need any more
|
|
762 than the standard 3 file descriptors. It would be cleaner to
|
|
763 close just the ones that need to be, but the following brute
|
|
764 force approach is certainly effective, and not too slow. */
|
|
765 {
|
|
766 int fd;
|
|
767 for (fd=3; fd<=64; fd++)
|
|
768 close (fd);
|
|
769 }
|
442
|
770 #endif /* not WIN32_NATIVE */
|
428
|
771
|
|
772 #ifdef vipc
|
|
773 something missing here;
|
|
774 #endif /* vipc */
|
|
775
|
442
|
776 #ifdef WIN32_NATIVE
|
428
|
777 /* Spawn the child. (See ntproc.c:Spawnve). */
|
442
|
778 cpid = spawnve (_P_NOWAIT, new_argv[0], (const char* const*)new_argv,
|
|
779 (const char* const*)env);
|
428
|
780 if (cpid == -1)
|
|
781 /* An error occurred while trying to spawn the process. */
|
563
|
782 report_process_error ("Spawning child process", Qunbound);
|
428
|
783 reset_standard_handles (in, out, err, handles);
|
|
784 return cpid;
|
442
|
785 #else /* not WIN32_NATIVE */
|
428
|
786 /* execvp does not accept an environment arg so the only way
|
|
787 to pass this environment is to set environ. Our caller
|
|
788 is responsible for restoring the ambient value of environ. */
|
|
789 environ = env;
|
|
790 execvp (new_argv[0], new_argv);
|
|
791
|
|
792 stdout_out ("Can't exec program %s\n", new_argv[0]);
|
|
793 _exit (1);
|
442
|
794 #endif /* not WIN32_NATIVE */
|
428
|
795 }
|
|
796
|
|
797 static int
|
665
|
798 getenv_internal (const Intbyte *var,
|
428
|
799 Bytecount varlen,
|
665
|
800 Intbyte **value,
|
428
|
801 Bytecount *valuelen)
|
|
802 {
|
|
803 Lisp_Object scan;
|
|
804
|
|
805 for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan))
|
|
806 {
|
|
807 Lisp_Object entry = XCAR (scan);
|
|
808
|
|
809 if (STRINGP (entry)
|
|
810 && XSTRING_LENGTH (entry) > varlen
|
|
811 && XSTRING_BYTE (entry, varlen) == '='
|
442
|
812 #ifdef WIN32_NATIVE
|
428
|
813 /* NT environment variables are case insensitive. */
|
|
814 && ! memicmp (XSTRING_DATA (entry), var, varlen)
|
442
|
815 #else /* not WIN32_NATIVE */
|
428
|
816 && ! memcmp (XSTRING_DATA (entry), var, varlen)
|
442
|
817 #endif /* not WIN32_NATIVE */
|
428
|
818 )
|
|
819 {
|
|
820 *value = XSTRING_DATA (entry) + (varlen + 1);
|
|
821 *valuelen = XSTRING_LENGTH (entry) - (varlen + 1);
|
|
822 return 1;
|
|
823 }
|
|
824 }
|
|
825
|
|
826 return 0;
|
|
827 }
|
|
828
|
|
829 DEFUN ("getenv", Fgetenv, 1, 2, "sEnvironment variable: \np", /*
|
|
830 Return the value of environment variable VAR, as a string.
|
|
831 VAR is a string, the name of the variable.
|
|
832 When invoked interactively, prints the value in the echo area.
|
|
833 */
|
|
834 (var, interactivep))
|
|
835 {
|
665
|
836 Intbyte *value;
|
428
|
837 Bytecount valuelen;
|
|
838 Lisp_Object v = Qnil;
|
|
839 struct gcpro gcpro1;
|
|
840
|
|
841 CHECK_STRING (var);
|
|
842 GCPRO1 (v);
|
|
843 if (getenv_internal (XSTRING_DATA (var), XSTRING_LENGTH (var),
|
|
844 &value, &valuelen))
|
|
845 v = make_string (value, valuelen);
|
|
846 if (!NILP (interactivep))
|
|
847 {
|
|
848 if (NILP (v))
|
|
849 message ("%s not defined in environment", XSTRING_DATA (var));
|
|
850 else
|
|
851 /* #### Should use Fprin1_to_string or Fprin1 to handle string
|
|
852 containing quotes correctly. */
|
|
853 message ("\"%s\"", value);
|
|
854 }
|
|
855 RETURN_UNGCPRO (v);
|
|
856 }
|
|
857
|
|
858 /* A version of getenv that consults process_environment, easily
|
|
859 callable from C. */
|
|
860 char *
|
442
|
861 egetenv (const char *var)
|
428
|
862 {
|
442
|
863 /* This cannot GC -- 7-28-00 ben */
|
665
|
864 Intbyte *value;
|
428
|
865 Bytecount valuelen;
|
|
866
|
665
|
867 if (getenv_internal ((const Intbyte *) var, strlen (var), &value, &valuelen))
|
428
|
868 return (char *) value;
|
|
869 else
|
|
870 return 0;
|
|
871 }
|
|
872
|
|
873
|
|
874 void
|
|
875 init_callproc (void)
|
|
876 {
|
|
877 /* This function can GC */
|
|
878
|
|
879 {
|
|
880 /* jwz: always initialize Vprocess_environment, so that egetenv()
|
|
881 works in temacs. */
|
|
882 char **envp;
|
|
883 Vprocess_environment = Qnil;
|
|
884 for (envp = environ; envp && *envp; envp++)
|
440
|
885 Vprocess_environment =
|
|
886 Fcons (build_ext_string (*envp, Qfile_name), Vprocess_environment);
|
428
|
887 }
|
|
888
|
|
889 {
|
|
890 /* Initialize shell-file-name from environment variables or best guess. */
|
442
|
891 #ifdef WIN32_NATIVE
|
611
|
892 const char *shell = egetenv ("SHELL");
|
|
893 if (!shell) shell = egetenv ("COMSPEC");
|
|
894 /* Should never happen! */
|
|
895 if (!shell) shell = (GetVersion () & 0x80000000 ? "command" : "cmd");
|
442
|
896 #else /* not WIN32_NATIVE */
|
|
897 const char *shell = egetenv ("SHELL");
|
428
|
898 if (!shell) shell = "/bin/sh";
|
|
899 #endif
|
|
900
|
611
|
901 #if 0 /* defined (WIN32_NATIVE) */
|
|
902 /* BAD BAD BAD. We do not wanting to be passing an XEmacs-created
|
|
903 SHELL var down to some inferior Cygwin process, which might get
|
|
904 screwed up.
|
|
905
|
|
906 There are a few broken apps (eterm/term.el, eterm/tshell.el,
|
|
907 os-utils/terminal.el, texinfo/tex-mode.el) where this will
|
|
908 cause problems. Those broken apps don't look at
|
|
909 shell-file-name, instead just at explicit-shell-file-name,
|
|
910 ESHELL and SHELL. They are apparently attempting to borrow
|
|
911 what `M-x shell' uses, but that latter also looks at
|
|
912 shell-file-name. What we want is for all of these apps to look
|
|
913 at shell-file-name, so that the user can change the value of
|
|
914 shell-file-name and everything will work out hunky-dorey.
|
|
915 */
|
|
916
|
|
917 if (!egetenv ("SHELL"))
|
|
918 {
|
665
|
919 CIntbyte *faux_var = alloca_array (CIntbyte, 7 + strlen (shell));
|
611
|
920 sprintf (faux_var, "SHELL=%s", shell);
|
|
921 Vprocess_environment = Fcons (build_string (faux_var),
|
|
922 Vprocess_environment);
|
|
923 }
|
|
924 #endif /* 0 */
|
|
925
|
428
|
926 Vshell_file_name = build_string (shell);
|
|
927 }
|
|
928 }
|
|
929
|
|
930 #if 0
|
|
931 void
|
|
932 set_process_environment (void)
|
|
933 {
|
|
934 REGISTER char **envp;
|
|
935
|
|
936 Vprocess_environment = Qnil;
|
|
937 #ifndef CANNOT_DUMP
|
|
938 if (initialized)
|
|
939 #endif
|
|
940 for (envp = environ; *envp; envp++)
|
|
941 Vprocess_environment = Fcons (build_string (*envp),
|
|
942 Vprocess_environment);
|
|
943 }
|
|
944 #endif /* unused */
|
|
945
|
|
946 void
|
|
947 syms_of_callproc (void)
|
|
948 {
|
442
|
949 DEFSUBR (Fold_call_process_internal);
|
428
|
950 DEFSUBR (Fgetenv);
|
|
951 }
|
|
952
|
|
953 void
|
|
954 vars_of_callproc (void)
|
|
955 {
|
|
956 /* This function can GC */
|
442
|
957 #ifdef WIN32_NATIVE
|
428
|
958 DEFVAR_LISP ("binary-process-input", &Vbinary_process_input /*
|
|
959 *If non-nil then new subprocesses are assumed to take binary input.
|
|
960 */ );
|
|
961 Vbinary_process_input = Qnil;
|
|
962
|
|
963 DEFVAR_LISP ("binary-process-output", &Vbinary_process_output /*
|
|
964 *If non-nil then new subprocesses are assumed to produce binary output.
|
|
965 */ );
|
|
966 Vbinary_process_output = Qnil;
|
442
|
967 #endif /* WIN32_NATIVE */
|
428
|
968
|
|
969 DEFVAR_LISP ("shell-file-name", &Vshell_file_name /*
|
|
970 *File name to load inferior shells from.
|
|
971 Initialized from the SHELL environment variable.
|
|
972 */ );
|
|
973
|
|
974 DEFVAR_LISP ("process-environment", &Vprocess_environment /*
|
|
975 List of environment variables for subprocesses to inherit.
|
|
976 Each element should be a string of the form ENVVARNAME=VALUE.
|
|
977 The environment which Emacs inherits is placed in this variable
|
|
978 when Emacs starts.
|
|
979 */ );
|
|
980
|
|
981 Vlisp_EXEC_SUFFIXES = build_string (EXEC_SUFFIXES);
|
|
982 staticpro (&Vlisp_EXEC_SUFFIXES);
|
|
983 }
|