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