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