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