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