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
|
|
141 static Lisp_Object fork_error;
|
|
142 #if 0 /* UNUSED */
|
|
143 static void
|
|
144 report_fork_error (char *string, Lisp_Object data)
|
|
145 {
|
|
146 Lisp_Object errstring = lisp_strerror (errno);
|
|
147
|
|
148 fork_error = Fcons (build_string (string), Fcons (errstring, data));
|
|
149
|
|
150 /* terminate this branch of the fork, without closing stdin/out/etc. */
|
|
151 _exit (1);
|
|
152 }
|
|
153 #endif /* unused */
|
|
154
|
442
|
155 DEFUN ("old-call-process-internal", Fold_call_process_internal, 1, MANY, 0, /*
|
428
|
156 Call PROGRAM synchronously in separate process, with coding-system specified.
|
|
157 Arguments are
|
|
158 (PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS).
|
|
159 The program's input comes from file INFILE (nil means `/dev/null').
|
|
160 Insert output in BUFFER before point; t means current buffer;
|
|
161 nil for BUFFER means discard it; 0 means discard and don't wait.
|
|
162 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
|
|
163 REAL-BUFFER says what to do with standard output, as above,
|
|
164 while STDERR-FILE says what to do with standard error in the child.
|
|
165 STDERR-FILE may be nil (discard standard error output),
|
|
166 t (mix it with ordinary output), or a file name string.
|
|
167
|
|
168 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
|
|
169 Remaining arguments are strings passed as command arguments to PROGRAM.
|
|
170
|
|
171 If BUFFER is 0, `call-process' returns immediately with value nil.
|
|
172 Otherwise it waits for PROGRAM to terminate and returns a numeric exit status
|
|
173 or a signal description string.
|
|
174 If you quit, the process is killed with SIGINT, or SIGKILL if you
|
|
175 quit again.
|
|
176 */
|
|
177 (int nargs, Lisp_Object *args))
|
|
178 {
|
|
179 /* This function can GC */
|
|
180 Lisp_Object infile, buffer, current_dir, display, path;
|
|
181 int fd[2];
|
|
182 int filefd;
|
442
|
183 #ifdef WIN32_NATIVE
|
432
|
184 HANDLE pHandle;
|
|
185 #endif
|
428
|
186 int pid;
|
|
187 char buf[16384];
|
|
188 char *bufptr = buf;
|
|
189 int bufsize = 16384;
|
|
190 int speccount = specpdl_depth ();
|
442
|
191 struct gcpro gcpro1, gcpro2, gcpro3;
|
428
|
192 char **new_argv = alloca_array (char *, max (2, nargs - 2));
|
|
193
|
|
194 /* File to use for stderr in the child.
|
|
195 t means use same as standard output. */
|
|
196 Lisp_Object error_file;
|
|
197
|
|
198 CHECK_STRING (args[0]);
|
|
199
|
|
200 error_file = Qt;
|
|
201
|
|
202 #if defined (NO_SUBPROCESSES)
|
|
203 /* Without asynchronous processes we cannot have BUFFER == 0. */
|
|
204 if (nargs >= 3 && !INTP (args[2]))
|
|
205 error ("Operating system cannot handle asynchronous subprocesses");
|
|
206 #endif /* NO_SUBPROCESSES */
|
|
207
|
|
208 /* Do this before building new_argv because GC in Lisp code
|
|
209 * called by various filename-hacking routines might relocate strings */
|
|
210 locate_file (Vexec_path, args[0], Vlisp_EXEC_SUFFIXES, &path, X_OK);
|
|
211
|
|
212 /* Make sure that the child will be able to chdir to the current
|
|
213 buffer's current directory, or its unhandled equivalent. We
|
|
214 can't just have the child check for an error when it does the
|
|
215 chdir, since it's in a vfork. */
|
|
216 {
|
|
217 struct gcpro ngcpro1, ngcpro2;
|
|
218 /* Do this test before building new_argv because GC in Lisp code
|
|
219 * called by various filename-hacking routines might relocate strings */
|
|
220 /* Make sure that the child will be able to chdir to the current
|
|
221 buffer's current directory. We can't just have the child check
|
|
222 for an error when it does the chdir, since it's in a vfork. */
|
|
223
|
446
|
224 current_dir = current_buffer->directory;
|
428
|
225 NGCPRO2 (current_dir, path); /* Caller gcprotects args[] */
|
|
226 current_dir = Funhandled_file_name_directory (current_dir);
|
|
227 current_dir = expand_and_dir_to_file (current_dir, Qnil);
|
|
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)))
|
|
232 report_file_error ("Setting current directory",
|
|
233 Fcons (current_buffer->directory, Qnil));
|
|
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))
|
|
307 report_file_error ("Searching for program", Fcons (args[0], Qnil));
|
|
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)
|
|
312 report_file_error ("Opening process input file", Fcons (infile, Qnil));
|
|
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;
|
428
|
367 report_file_error ("Cannot open", Fcons(error_file, Qnil));
|
|
368 }
|
|
369
|
|
370 fork_error = Qnil;
|
442
|
371 #ifdef WIN32_NATIVE
|
428
|
372 pid = child_setup (filefd, fd1, fd_error, new_argv,
|
|
373 (char *) XSTRING_DATA (current_dir));
|
432
|
374 if (!INTP (buffer))
|
|
375 {
|
|
376 /* OpenProcess() as soon after child_setup as possible. It's too
|
|
377 late once the process terminated. */
|
|
378 pHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
|
|
379 #if 0
|
|
380 if (pHandle == NULL)
|
|
381 {
|
|
382 /* #### seems to cause crash in unbind_to(...) below. APA */
|
|
383 warn_when_safe (Qprocess, Qwarning,
|
|
384 "cannot open process to wait for");
|
|
385 }
|
|
386 #endif
|
|
387 }
|
|
388 /* Close STDERR into the parent process. We no longer need it. */
|
|
389 if (fd_error >= 0)
|
|
390 close (fd_error);
|
442
|
391 #else /* not WIN32_NATIVE */
|
428
|
392 pid = fork ();
|
|
393
|
|
394 if (pid == 0)
|
|
395 {
|
|
396 if (fd[0] >= 0)
|
|
397 close (fd[0]);
|
|
398 /* This is necessary because some shells may attempt to
|
|
399 access the current controlling terminal and will hang
|
|
400 if they are run in the background, as will be the case
|
|
401 when XEmacs is started in the background. Martin
|
|
402 Buchholz observed this problem running a subprocess
|
|
403 that used zsh to call gzip to uncompress an info
|
|
404 file. */
|
|
405 disconnect_controlling_terminal ();
|
|
406 child_setup (filefd, fd1, fd_error, new_argv,
|
|
407 (char *) XSTRING_DATA (current_dir));
|
|
408 }
|
|
409 if (fd_error >= 0)
|
|
410 close (fd_error);
|
|
411
|
442
|
412 #endif /* not WIN32_NATIVE */
|
428
|
413
|
|
414 environ = save_environ;
|
|
415
|
|
416 /* Close most of our fd's, but not fd[0]
|
|
417 since we will use that to read input from. */
|
|
418 close (filefd);
|
|
419 if (fd1 >= 0)
|
|
420 close (fd1);
|
|
421 }
|
|
422
|
|
423 if (!NILP (fork_error))
|
|
424 signal_error (Qfile_error, fork_error);
|
|
425
|
442
|
426 #ifndef WIN32_NATIVE
|
428
|
427 if (pid < 0)
|
|
428 {
|
442
|
429 int save_errno = errno;
|
428
|
430 if (fd[0] >= 0)
|
|
431 close (fd[0]);
|
442
|
432 errno = save_errno;
|
428
|
433 report_file_error ("Doing fork", Qnil);
|
|
434 }
|
432
|
435 #endif
|
428
|
436
|
|
437 if (INTP (buffer))
|
|
438 {
|
|
439 if (fd[0] >= 0)
|
|
440 close (fd[0]);
|
|
441 #if defined (NO_SUBPROCESSES)
|
|
442 /* If Emacs has been built with asynchronous subprocess support,
|
|
443 we don't need to do this, I think because it will then have
|
|
444 the facilities for handling SIGCHLD. */
|
|
445 wait_without_blocking ();
|
|
446 #endif /* NO_SUBPROCESSES */
|
|
447 return Qnil;
|
|
448 }
|
|
449
|
|
450 {
|
|
451 int nread;
|
|
452 int total_read = 0;
|
|
453 Lisp_Object instream;
|
|
454 struct gcpro ngcpro1;
|
|
455
|
|
456 /* Enable sending signal if user quits below. */
|
|
457 call_process_exited = 0;
|
|
458
|
|
459 record_unwind_protect (call_process_cleanup,
|
|
460 Fcons (make_int (fd[0]), make_int (pid)));
|
|
461
|
|
462 /* FSFmacs calls Fset_buffer() here. We don't have to because
|
|
463 we can insert into buffers other than the current one. */
|
|
464 if (EQ (buffer, Qt))
|
|
465 XSETBUFFER (buffer, current_buffer);
|
|
466 instream = make_filedesc_input_stream (fd[0], 0, -1, LSTR_ALLOW_QUIT);
|
|
467 #ifdef FILE_CODING
|
|
468 instream =
|
|
469 make_decoding_input_stream
|
|
470 (XLSTREAM (instream),
|
|
471 Fget_coding_system (Vcoding_system_for_read));
|
|
472 Lstream_set_character_mode (XLSTREAM (instream));
|
|
473 #endif
|
|
474 NGCPRO1 (instream);
|
|
475 while (1)
|
|
476 {
|
|
477 QUIT;
|
|
478 /* Repeatedly read until we've filled as much as possible
|
|
479 of the buffer size we have. But don't read
|
|
480 less than 1024--save that for the next bufferfull. */
|
|
481
|
|
482 nread = 0;
|
|
483 while (nread < bufsize - 1024)
|
|
484 {
|
|
485 ssize_t this_read
|
|
486 = Lstream_read (XLSTREAM (instream), bufptr + nread,
|
|
487 bufsize - nread);
|
|
488
|
|
489 if (this_read < 0)
|
|
490 goto give_up;
|
|
491
|
|
492 if (this_read == 0)
|
|
493 goto give_up_1;
|
|
494
|
|
495 nread += this_read;
|
|
496 }
|
|
497
|
|
498 give_up_1:
|
|
499
|
|
500 /* Now NREAD is the total amount of data in the buffer. */
|
|
501 if (nread == 0)
|
|
502 break;
|
|
503
|
440
|
504 #if 0
|
442
|
505 #ifdef WIN32_NATIVE
|
428
|
506 /* Until we pull out of MULE things like
|
|
507 make_decoding_input_stream(), we do the following which is
|
|
508 less elegant. --marcpa */
|
440
|
509 /* We did. -- kkm */
|
428
|
510 {
|
|
511 int lf_count = 0;
|
|
512 if (NILP (Vbinary_process_output)) {
|
|
513 nread = crlf_to_lf(nread, bufptr, &lf_count);
|
|
514 }
|
|
515 }
|
|
516 #endif
|
440
|
517 #endif
|
428
|
518
|
|
519 total_read += nread;
|
|
520
|
|
521 if (!NILP (buffer))
|
|
522 buffer_insert_raw_string (XBUFFER (buffer), (Bufbyte *) bufptr,
|
|
523 nread);
|
|
524
|
|
525 /* Make the buffer bigger as we continue to read more data,
|
|
526 but not past 64k. */
|
|
527 if (bufsize < 64 * 1024 && total_read > 32 * bufsize)
|
|
528 {
|
|
529 bufsize *= 2;
|
|
530 bufptr = (char *) alloca (bufsize);
|
|
531 }
|
|
532
|
|
533 if (!NILP (display) && INTERACTIVE)
|
|
534 {
|
|
535 redisplay ();
|
|
536 }
|
|
537 }
|
|
538 give_up:
|
|
539 Lstream_close (XLSTREAM (instream));
|
|
540 NUNGCPRO;
|
|
541
|
|
542 QUIT;
|
|
543 /* Wait for it to terminate, unless it already has. */
|
442
|
544 #ifdef WIN32_NATIVE
|
432
|
545 wait_for_termination (pHandle);
|
|
546 #else
|
428
|
547 wait_for_termination (pid);
|
432
|
548 #endif
|
428
|
549
|
|
550 /* Don't kill any children that the subprocess may have left behind
|
|
551 when exiting. */
|
|
552 call_process_exited = 1;
|
|
553 unbind_to (speccount, Qnil);
|
|
554
|
|
555 if (synch_process_death)
|
|
556 return build_string (synch_process_death);
|
|
557 return make_int (synch_process_retcode);
|
|
558 }
|
|
559 }
|
|
560
|
|
561
|
|
562
|
|
563 /* Move the file descriptor FD so that its number is not less than MIN. *
|
|
564 The original file descriptor remains open. */
|
|
565 static int
|
|
566 relocate_fd (int fd, int min)
|
|
567 {
|
|
568 if (fd >= min)
|
|
569 return fd;
|
|
570 else
|
|
571 {
|
|
572 int newfd = dup (fd);
|
|
573 if (newfd == -1)
|
|
574 {
|
|
575 stderr_out ("Error while setting up child: %s\n",
|
|
576 strerror (errno));
|
|
577 _exit (1);
|
|
578 }
|
|
579 return relocate_fd (newfd, min);
|
|
580 }
|
|
581 }
|
|
582
|
|
583 /* This is the last thing run in a newly forked inferior
|
|
584 either synchronous or asynchronous.
|
|
585 Copy descriptors IN, OUT and ERR
|
|
586 as descriptors STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO.
|
|
587 Initialize inferior's priority, pgrp, connected dir and environment.
|
|
588 then exec another program based on new_argv.
|
|
589
|
|
590 This function may change environ for the superior process.
|
|
591 Therefore, the superior process must save and restore the value
|
|
592 of environ around the fork and the call to this function.
|
|
593
|
|
594 ENV is the environment for the subprocess.
|
|
595
|
|
596 XEmacs: We've removed the SET_PGRP argument because it's already
|
|
597 done by the callers of child_setup.
|
|
598
|
|
599 CURRENT_DIR is an elisp string giving the path of the current
|
|
600 directory the subprocess should have. Since we can't really signal
|
|
601 a decent error from within the child, this should be verified as an
|
|
602 executable directory by the parent. */
|
|
603
|
442
|
604 #ifdef WIN32_NATIVE
|
428
|
605 int
|
|
606 #else
|
|
607 void
|
|
608 #endif
|
|
609 child_setup (int in, int out, int err, char **new_argv,
|
442
|
610 const char *current_dir)
|
428
|
611 {
|
|
612 char **env;
|
|
613 char *pwd;
|
442
|
614 #ifdef WIN32_NATIVE
|
428
|
615 int cpid;
|
|
616 HANDLE handles[4];
|
442
|
617 #endif /* WIN32_NATIVE */
|
428
|
618
|
|
619 #ifdef SET_EMACS_PRIORITY
|
|
620 if (emacs_priority != 0)
|
|
621 nice (- emacs_priority);
|
|
622 #endif
|
|
623
|
442
|
624 /* Under Windows, we are not in a child process at all, so we should
|
|
625 not close handles inherited from the parent -- we are the parent
|
|
626 and doing so will screw up all manner of things! Similarly, most
|
|
627 of the rest of the cleanup done in this function is not done
|
|
628 under Windows.
|
|
629
|
|
630 #### This entire child_setup() function is an utter and complete
|
|
631 piece of shit. I would rewrite it, at the very least splitting
|
|
632 out the Windows and non-Windows stuff into two completely
|
|
633 different functions; but instead I'm trying to make it go away
|
|
634 entirely, using the Lisp definition in process.el. What's left
|
|
635 is to fix up the routines in event-msw.c (and in event-Xt.c and
|
|
636 event-tty.c) to allow for stream devices to be handled correctly.
|
|
637 There isn't much to do, in fact, and I'll fix it shortly. That
|
|
638 way, the Lisp definition can be used non-interactively too. */
|
|
639 #if !defined (NO_SUBPROCESSES) && !defined (WIN32_NATIVE)
|
428
|
640 /* Close Emacs's descriptors that this process should not have. */
|
|
641 close_process_descs ();
|
|
642 #endif /* not NO_SUBPROCESSES */
|
442
|
643 #ifndef WIN32_NATIVE
|
428
|
644 close_load_descs ();
|
442
|
645 #endif
|
428
|
646
|
|
647 /* Note that use of alloca is always safe here. It's obvious for systems
|
|
648 that do not have true vfork or that have true (stack) alloca.
|
|
649 If using vfork and C_ALLOCA it is safe because that changes
|
|
650 the superior's static variables as if the superior had done alloca
|
|
651 and will be cleaned up in the usual way. */
|
|
652 {
|
|
653 REGISTER int i;
|
|
654
|
|
655 i = strlen (current_dir);
|
|
656 pwd = alloca_array (char, i + 6);
|
|
657 memcpy (pwd, "PWD=", 4);
|
|
658 memcpy (pwd + 4, current_dir, i);
|
|
659 i += 4;
|
|
660 if (!IS_DIRECTORY_SEP (pwd[i - 1]))
|
|
661 pwd[i++] = DIRECTORY_SEP;
|
|
662 pwd[i] = 0;
|
|
663
|
|
664 /* We can't signal an Elisp error here; we're in a vfork. Since
|
|
665 the callers check the current directory before forking, this
|
|
666 should only return an error if the directory's permissions
|
|
667 are changed between the check and this chdir, but we should
|
|
668 at least check. */
|
|
669 if (chdir (pwd + 4) < 0)
|
|
670 {
|
|
671 /* Don't report the chdir error, or ange-ftp.el doesn't work. */
|
|
672 /* (FSFmacs does _exit (errno) here.) */
|
|
673 pwd = 0;
|
|
674 }
|
|
675 else
|
|
676 {
|
|
677 /* Strip trailing "/". Cretinous *[]&@$#^%@#$% Un*x */
|
|
678 /* leave "//" (from FSF) */
|
|
679 while (i > 6 && IS_DIRECTORY_SEP (pwd[i - 1]))
|
|
680 pwd[--i] = 0;
|
|
681 }
|
|
682 }
|
|
683
|
|
684 /* Set `env' to a vector of the strings in Vprocess_environment. */
|
|
685 /* + 2 to include PWD and terminating 0. */
|
|
686 env = alloca_array (char *, XINT (Flength (Vprocess_environment)) + 2);
|
|
687 {
|
|
688 REGISTER Lisp_Object tail;
|
|
689 char **new_env = env;
|
|
690
|
|
691 /* If we have a PWD envvar and we know the real current directory,
|
|
692 pass one down, but with corrected value. */
|
|
693 if (pwd && getenv ("PWD"))
|
|
694 *new_env++ = pwd;
|
|
695
|
|
696 /* Copy the Vprocess_environment strings into new_env. */
|
|
697 for (tail = Vprocess_environment;
|
|
698 CONSP (tail) && STRINGP (XCAR (tail));
|
|
699 tail = XCDR (tail))
|
|
700 {
|
|
701 char **ep = env;
|
|
702 char *envvar_external;
|
|
703
|
440
|
704 TO_EXTERNAL_FORMAT (LISP_STRING, XCAR (tail),
|
|
705 C_STRING_ALLOCA, envvar_external,
|
|
706 Qfile_name);
|
428
|
707
|
|
708 /* See if envvar_external duplicates any string already in the env.
|
|
709 If so, don't put it in.
|
|
710 When an env var has multiple definitions,
|
|
711 we keep the definition that comes first in process-environment. */
|
|
712 for (; ep != new_env; ep++)
|
|
713 {
|
|
714 char *p = *ep, *q = envvar_external;
|
|
715 while (1)
|
|
716 {
|
|
717 if (*q == 0)
|
|
718 /* The string is malformed; might as well drop it. */
|
|
719 goto duplicate;
|
|
720 if (*q != *p)
|
|
721 break;
|
|
722 if (*q == '=')
|
|
723 goto duplicate;
|
|
724 p++, q++;
|
|
725 }
|
|
726 }
|
|
727 if (pwd && !strncmp ("PWD=", envvar_external, 4))
|
|
728 {
|
|
729 *new_env++ = pwd;
|
|
730 pwd = 0;
|
|
731 }
|
|
732 else
|
|
733 *new_env++ = envvar_external;
|
|
734
|
|
735 duplicate: ;
|
|
736 }
|
|
737 *new_env = 0;
|
|
738 }
|
|
739
|
442
|
740 #ifdef WIN32_NATIVE
|
428
|
741 prepare_standard_handles (in, out, err, handles);
|
|
742 set_process_dir (current_dir);
|
442
|
743 #else /* not WIN32_NATIVE */
|
428
|
744 /* Make sure that in, out, and err are not actually already in
|
|
745 descriptors zero, one, or two; this could happen if Emacs is
|
|
746 started with its standard in, out, or error closed, as might
|
|
747 happen under X. */
|
|
748 in = relocate_fd (in, 3);
|
|
749 out = relocate_fd (out, 3);
|
|
750 err = relocate_fd (err, 3);
|
|
751
|
|
752 /* Set the standard input/output channels of the new process. */
|
|
753 close (STDIN_FILENO);
|
|
754 close (STDOUT_FILENO);
|
|
755 close (STDERR_FILENO);
|
|
756
|
|
757 dup2 (in, STDIN_FILENO);
|
|
758 dup2 (out, STDOUT_FILENO);
|
|
759 dup2 (err, STDERR_FILENO);
|
|
760
|
|
761 close (in);
|
|
762 close (out);
|
|
763 close (err);
|
|
764
|
|
765 /* I can't think of any reason why child processes need any more
|
|
766 than the standard 3 file descriptors. It would be cleaner to
|
|
767 close just the ones that need to be, but the following brute
|
|
768 force approach is certainly effective, and not too slow. */
|
|
769 {
|
|
770 int fd;
|
|
771 for (fd=3; fd<=64; fd++)
|
|
772 close (fd);
|
|
773 }
|
442
|
774 #endif /* not WIN32_NATIVE */
|
428
|
775
|
|
776 #ifdef vipc
|
|
777 something missing here;
|
|
778 #endif /* vipc */
|
|
779
|
442
|
780 #ifdef WIN32_NATIVE
|
428
|
781 /* Spawn the child. (See ntproc.c:Spawnve). */
|
442
|
782 cpid = spawnve (_P_NOWAIT, new_argv[0], (const char* const*)new_argv,
|
|
783 (const char* const*)env);
|
428
|
784 if (cpid == -1)
|
|
785 /* An error occurred while trying to spawn the process. */
|
|
786 report_file_error ("Spawning child process", Qnil);
|
|
787 reset_standard_handles (in, out, err, handles);
|
|
788 return cpid;
|
442
|
789 #else /* not WIN32_NATIVE */
|
428
|
790 /* execvp does not accept an environment arg so the only way
|
|
791 to pass this environment is to set environ. Our caller
|
|
792 is responsible for restoring the ambient value of environ. */
|
|
793 environ = env;
|
|
794 execvp (new_argv[0], new_argv);
|
|
795
|
|
796 stdout_out ("Can't exec program %s\n", new_argv[0]);
|
|
797 _exit (1);
|
442
|
798 #endif /* not WIN32_NATIVE */
|
428
|
799 }
|
|
800
|
|
801 static int
|
442
|
802 getenv_internal (const Bufbyte *var,
|
428
|
803 Bytecount varlen,
|
|
804 Bufbyte **value,
|
|
805 Bytecount *valuelen)
|
|
806 {
|
|
807 Lisp_Object scan;
|
|
808
|
|
809 for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan))
|
|
810 {
|
|
811 Lisp_Object entry = XCAR (scan);
|
|
812
|
|
813 if (STRINGP (entry)
|
|
814 && XSTRING_LENGTH (entry) > varlen
|
|
815 && XSTRING_BYTE (entry, varlen) == '='
|
442
|
816 #ifdef WIN32_NATIVE
|
428
|
817 /* NT environment variables are case insensitive. */
|
|
818 && ! memicmp (XSTRING_DATA (entry), var, varlen)
|
442
|
819 #else /* not WIN32_NATIVE */
|
428
|
820 && ! memcmp (XSTRING_DATA (entry), var, varlen)
|
442
|
821 #endif /* not WIN32_NATIVE */
|
428
|
822 )
|
|
823 {
|
|
824 *value = XSTRING_DATA (entry) + (varlen + 1);
|
|
825 *valuelen = XSTRING_LENGTH (entry) - (varlen + 1);
|
|
826 return 1;
|
|
827 }
|
|
828 }
|
|
829
|
|
830 return 0;
|
|
831 }
|
|
832
|
|
833 DEFUN ("getenv", Fgetenv, 1, 2, "sEnvironment variable: \np", /*
|
|
834 Return the value of environment variable VAR, as a string.
|
|
835 VAR is a string, the name of the variable.
|
|
836 When invoked interactively, prints the value in the echo area.
|
|
837 */
|
|
838 (var, interactivep))
|
|
839 {
|
|
840 Bufbyte *value;
|
|
841 Bytecount valuelen;
|
|
842 Lisp_Object v = Qnil;
|
|
843 struct gcpro gcpro1;
|
|
844
|
|
845 CHECK_STRING (var);
|
|
846 GCPRO1 (v);
|
|
847 if (getenv_internal (XSTRING_DATA (var), XSTRING_LENGTH (var),
|
|
848 &value, &valuelen))
|
|
849 v = make_string (value, valuelen);
|
|
850 if (!NILP (interactivep))
|
|
851 {
|
|
852 if (NILP (v))
|
|
853 message ("%s not defined in environment", XSTRING_DATA (var));
|
|
854 else
|
|
855 /* #### Should use Fprin1_to_string or Fprin1 to handle string
|
|
856 containing quotes correctly. */
|
|
857 message ("\"%s\"", value);
|
|
858 }
|
|
859 RETURN_UNGCPRO (v);
|
|
860 }
|
|
861
|
|
862 /* A version of getenv that consults process_environment, easily
|
|
863 callable from C. */
|
|
864 char *
|
442
|
865 egetenv (const char *var)
|
428
|
866 {
|
442
|
867 /* This cannot GC -- 7-28-00 ben */
|
428
|
868 Bufbyte *value;
|
|
869 Bytecount valuelen;
|
|
870
|
442
|
871 if (getenv_internal ((const Bufbyte *) var, strlen (var), &value, &valuelen))
|
428
|
872 return (char *) value;
|
|
873 else
|
|
874 return 0;
|
|
875 }
|
|
876
|
|
877
|
|
878 void
|
|
879 init_callproc (void)
|
|
880 {
|
|
881 /* This function can GC */
|
|
882
|
|
883 {
|
|
884 /* jwz: always initialize Vprocess_environment, so that egetenv()
|
|
885 works in temacs. */
|
|
886 char **envp;
|
|
887 Vprocess_environment = Qnil;
|
|
888 for (envp = environ; envp && *envp; envp++)
|
440
|
889 Vprocess_environment =
|
|
890 Fcons (build_ext_string (*envp, Qfile_name), Vprocess_environment);
|
428
|
891 }
|
|
892
|
|
893 {
|
|
894 /* Initialize shell-file-name from environment variables or best guess. */
|
442
|
895 #ifdef WIN32_NATIVE
|
|
896 const char *shell = egetenv ("COMSPEC");
|
428
|
897 if (!shell) shell = "\\WINNT\\system32\\cmd.exe";
|
442
|
898 #else /* not WIN32_NATIVE */
|
|
899 const char *shell = egetenv ("SHELL");
|
428
|
900 if (!shell) shell = "/bin/sh";
|
|
901 #endif
|
|
902
|
|
903 Vshell_file_name = build_string (shell);
|
|
904 }
|
|
905 }
|
|
906
|
|
907 #if 0
|
|
908 void
|
|
909 set_process_environment (void)
|
|
910 {
|
|
911 REGISTER char **envp;
|
|
912
|
|
913 Vprocess_environment = Qnil;
|
|
914 #ifndef CANNOT_DUMP
|
|
915 if (initialized)
|
|
916 #endif
|
|
917 for (envp = environ; *envp; envp++)
|
|
918 Vprocess_environment = Fcons (build_string (*envp),
|
|
919 Vprocess_environment);
|
|
920 }
|
|
921 #endif /* unused */
|
|
922
|
|
923 void
|
|
924 syms_of_callproc (void)
|
|
925 {
|
442
|
926 DEFSUBR (Fold_call_process_internal);
|
428
|
927 DEFSUBR (Fgetenv);
|
|
928 }
|
|
929
|
|
930 void
|
|
931 vars_of_callproc (void)
|
|
932 {
|
|
933 /* This function can GC */
|
442
|
934 #ifdef WIN32_NATIVE
|
428
|
935 DEFVAR_LISP ("binary-process-input", &Vbinary_process_input /*
|
|
936 *If non-nil then new subprocesses are assumed to take binary input.
|
|
937 */ );
|
|
938 Vbinary_process_input = Qnil;
|
|
939
|
|
940 DEFVAR_LISP ("binary-process-output", &Vbinary_process_output /*
|
|
941 *If non-nil then new subprocesses are assumed to produce binary output.
|
|
942 */ );
|
|
943 Vbinary_process_output = Qnil;
|
442
|
944 #endif /* WIN32_NATIVE */
|
428
|
945
|
|
946 DEFVAR_LISP ("shell-file-name", &Vshell_file_name /*
|
|
947 *File name to load inferior shells from.
|
|
948 Initialized from the SHELL environment variable.
|
|
949 */ );
|
|
950
|
|
951 DEFVAR_LISP ("process-environment", &Vprocess_environment /*
|
|
952 List of environment variables for subprocesses to inherit.
|
|
953 Each element should be a string of the form ENVVARNAME=VALUE.
|
|
954 The environment which Emacs inherits is placed in this variable
|
|
955 when Emacs starts.
|
|
956 */ );
|
|
957
|
|
958 Vlisp_EXEC_SUFFIXES = build_string (EXEC_SUFFIXES);
|
|
959 staticpro (&Vlisp_EXEC_SUFFIXES);
|
|
960 }
|