Misc updates for admin/make-tarball.txt
[bpt/emacs.git] / src / callproc.c
CommitLineData
80856e74 1/* Synchronous subprocess invocation for GNU Emacs.
acaf905b 2 Copyright (C) 1985-1988, 1993-1995, 1999-2012
19ed11ba 3 Free Software Foundation, Inc.
80856e74
JB
4
5This file is part of GNU Emacs.
6
9ec0b715 7GNU Emacs is free software: you can redistribute it and/or modify
80856e74 8it under the terms of the GNU General Public License as published by
9ec0b715
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
80856e74
JB
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9ec0b715 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
80856e74
JB
19
20
68c45bf0 21#include <config.h>
e576cab4 22#include <errno.h>
565620a5 23#include <stdio.h>
80856e74 24#include <sys/types.h>
3cbd6585 25#include <unistd.h>
3cbd6585 26
80856e74 27#include <sys/file.h>
80856e74 28#include <fcntl.h>
80856e74 29
0898ca10
JB
30#include "lisp.h"
31
bad95d8f
RS
32#ifdef WINDOWSNT
33#define NOMINMAX
34#include <windows.h>
489f9371 35#include "w32.h"
bad95d8f
RS
36#define _P_NOWAIT 1 /* from process.h */
37#endif
38
7e6c2178 39#ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
7e6c2178
RS
40#include <sys/stat.h>
41#include <sys/param.h>
7e6c2178
RS
42#endif /* MSDOS */
43
80856e74 44#include "commands.h"
91183bfd 45#include "character.h"
e5560ff7 46#include "buffer.h"
edf496dd 47#include "ccl.h"
32d08644 48#include "coding.h"
f0b950cf 49#include "composite.h"
57bda87a 50#include <epaths.h>
80856e74 51#include "process.h"
d177f194 52#include "syssignal.h"
a129418f 53#include "systty.h"
afea8a8a 54#include "syswait.h"
aba637ec 55#include "blockinput.h"
f105f403
KL
56#include "frame.h"
57#include "termhooks.h"
80856e74 58
5f027cea
EZ
59#ifdef MSDOS
60#include "msdos.h"
61#endif
62
d01ba2f1
GM
63#ifdef HAVE_NS
64#include "nsterm.h"
65#endif
66
f92d51d8
CY
67/* Pattern used by call-process-region to make temp files. */
68static Lisp_Object Vtemp_file_name_pattern;
69
bb5f74ee
PE
70/* The next two variables are valid only while record-unwind-protect
71 is in place during call-process for a synchronous subprocess. At
72 other times, their contents are irrelevant. Doing this via static
73 C variables is more convenient than putting them into the arguments
74 of record-unwind-protect, as they need to be updated at randomish
75 times in the code, and Lisp cannot always store these values as
76 Emacs integers. It's safe to use static variables here, as the
77 code is never invoked reentrantly. */
78
79/* If nonzero, a process-ID that has not been reaped. */
80static pid_t synch_process_pid;
81
82/* If nonnegative, a file descriptor that has not been closed. */
83static int synch_process_fd;
84\f
85/* Block SIGCHLD. */
80856e74 86
bb5f74ee
PE
87static void
88block_child_signal (void)
89{
bb5f74ee
PE
90 sigset_t blocked;
91 sigemptyset (&blocked);
92 sigaddset (&blocked, SIGCHLD);
93 pthread_sigmask (SIG_BLOCK, &blocked, 0);
bb5f74ee 94}
6b61353c 95
bb5f74ee 96/* Unblock SIGCHLD. */
f105f403 97
bb5f74ee
PE
98static void
99unblock_child_signal (void)
100{
bb5f74ee 101 pthread_sigmask (SIG_SETMASK, &empty_mask, 0);
bb5f74ee 102}
37d54121 103
35fb8050
PE
104/* If P is reapable, record it as a deleted process and kill it.
105 Do this in a critical section. Unless PID is wedged it will be
106 reaped on receipt of the first SIGCHLD after the critical section. */
107
108void
109record_kill_process (struct Lisp_Process *p)
110{
111 block_child_signal ();
112
113 if (p->alive)
114 {
115 p->alive = 0;
116 record_deleted_pid (p->pid);
d983a10b 117 kill (- p->pid, SIGKILL);
35fb8050
PE
118 }
119
120 unblock_child_signal ();
121}
122
bb5f74ee 123/* Clean up when exiting call_process_cleanup. */
37d54121 124
d177f194 125static Lisp_Object
bb5f74ee 126call_process_kill (Lisp_Object ignored)
d177f194 127{
bb5f74ee
PE
128 if (0 <= synch_process_fd)
129 emacs_close (synch_process_fd);
130
bb5f74ee
PE
131 if (synch_process_pid)
132 {
35fb8050
PE
133 struct Lisp_Process proc;
134 proc.alive = 1;
135 proc.pid = synch_process_pid;
136 record_kill_process (&proc);
bb5f74ee
PE
137 }
138
d177f194
JB
139 return Qnil;
140}
141
bb5f74ee
PE
142/* Clean up when exiting Fcall_process.
143 On MSDOS, delete the temporary file on any kind of termination.
144 On Unix, kill the process and any children on termination by signal. */
145
7710357c 146static Lisp_Object
971de7fb 147call_process_cleanup (Lisp_Object arg)
80856e74 148{
bb5f74ee
PE
149#ifdef MSDOS
150 Lisp_Object buffer = Fcar (arg);
151 Lisp_Object file = Fcdr (arg);
ad3aaf33 152#else
bb5f74ee 153 Lisp_Object buffer = arg;
ad3aaf33
AS
154#endif
155
bb5f74ee 156 Fset_buffer (buffer);
d177f194 157
bb5f74ee
PE
158#ifndef MSDOS
159 /* If the process still exists, kill its process group. */
160 if (synch_process_pid)
d177f194 161 {
d311d28c 162 ptrdiff_t count = SPECPDL_INDEX ();
d983a10b 163 kill (-synch_process_pid, SIGINT);
bb5f74ee 164 record_unwind_protect (call_process_kill, make_number (0));
d177f194
JB
165 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
166 immediate_quit = 1;
167 QUIT;
bb5f74ee
PE
168 wait_for_termination (synch_process_pid, 0, 1);
169 synch_process_pid = 0;
d177f194
JB
170 immediate_quit = 0;
171 specpdl_ptr = specpdl + count; /* Discard the unwind protect. */
172 message1 ("Waiting for process to die...done");
173 }
bb5f74ee
PE
174#endif
175
176 if (0 <= synch_process_fd)
177 emacs_close (synch_process_fd);
178
179#ifdef MSDOS
180 /* FILE is "" when we didn't actually create a temporary file in
181 call-process. */
182 if (!(strcmp (SDATA (file), NULL_DEVICE) == 0 || SREF (file, 0) == '\0'))
183 unlink (SDATA (file));
184#endif
185
80856e74
JB
186 return Qnil;
187}
188
a7ca3326 189DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
fdb82f93
PJ
190 doc: /* Call PROGRAM synchronously in separate process.
191The remaining arguments are optional.
192The program's input comes from file INFILE (nil means `/dev/null').
1ef14cb4
LMI
193Insert output in BUFFER before point; t means current buffer; nil for BUFFER
194 means discard it; 0 means discard and don't wait; and `(:file FILE)', where
1b9f60cc
GM
195 FILE is a file name string, means that it should be written to that file
196 \(if the file already exists it is overwritten).
fdb82f93
PJ
197BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
198REAL-BUFFER says what to do with standard output, as above,
199while STDERR-FILE says what to do with standard error in the child.
200STDERR-FILE may be nil (discard standard error output),
201t (mix it with ordinary output), or a file name string.
202
203Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
204Remaining arguments are strings passed as command arguments to PROGRAM.
205
a4feb144
RS
206If executable PROGRAM can't be found as an executable, `call-process'
207signals a Lisp error. `call-process' reports errors in execution of
208the program only through its return and output.
209
fdb82f93
PJ
210If BUFFER is 0, `call-process' returns immediately with value nil.
211Otherwise it waits for PROGRAM to terminate
212and returns a numeric exit status or a signal description string.
d98b59b5
MB
213If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
214
215usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */)
f66c7cf8 216 (ptrdiff_t nargs, Lisp_Object *args)
80856e74 217{
bb5f74ee 218 Lisp_Object infile, buffer, current_dir, path;
2f221583 219 bool display_p;
60aeceb8 220 int fd0, fd1, filefd;
bb5f74ee 221 int status;
d311d28c
PE
222 ptrdiff_t count = SPECPDL_INDEX ();
223 USE_SAFE_ALLOCA;
2d607244 224
60aeceb8 225 char **new_argv;
39eaa782
RS
226 /* File to use for stderr in the child.
227 t means use same as standard output. */
228 Lisp_Object error_file;
1ef14cb4 229 Lisp_Object output_file = Qnil;
7e6c2178 230#ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
888c9e86 231 char *outf, *tempfile = NULL;
7e6c2178 232 int outfilefd;
d311d28c
PE
233 int pid;
234#else
235 pid_t pid;
80856e74 236#endif
bb5f74ee 237 int child_errno;
1ef14cb4 238 int fd_output = -1;
32d08644
KH
239 struct coding_system process_coding; /* coding-system of process output */
240 struct coding_system argument_coding; /* coding-system of arguments */
09494912
RS
241 /* Set to the return value of Ffind_operation_coding_system. */
242 Lisp_Object coding_systems;
2f221583 243 bool output_to_buffer = 1;
09494912
RS
244
245 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
246 coding_systems = Qt;
32d08644 247
b7826503 248 CHECK_STRING (args[0]);
80856e74 249
39eaa782
RS
250 error_file = Qt;
251
7e6c2178
RS
252#ifndef subprocesses
253 /* Without asynchronous processes we cannot have BUFFER == 0. */
177c0ea7 254 if (nargs >= 3
09ffb8b5 255 && (INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2])))
7e6c2178
RS
256 error ("Operating system cannot handle asynchronous subprocesses");
257#endif /* subprocesses */
258
09494912 259 /* Decide the coding-system for giving arguments. */
32d08644
KH
260 {
261 Lisp_Object val, *args2;
f66c7cf8 262 ptrdiff_t i;
32d08644
KH
263
264 /* If arguments are supplied, we may have to encode them. */
265 if (nargs >= 5)
266 {
2f221583 267 bool must_encode = 0;
6e50da0a 268 Lisp_Object coding_attrs;
30d57b8e 269
e7c1c20e 270 for (i = 4; i < nargs; i++)
b7826503 271 CHECK_STRING (args[i]);
e7c1c20e 272
a2286b5c 273 for (i = 4; i < nargs; i++)
30d57b8e
RS
274 if (STRING_MULTIBYTE (args[i]))
275 must_encode = 1;
276
beacaab3
KH
277 if (!NILP (Vcoding_system_for_write))
278 val = Vcoding_system_for_write;
30d57b8e 279 else if (! must_encode)
fcaf8878 280 val = Qraw_text;
beacaab3 281 else
32d08644 282 {
0065d054 283 SAFE_NALLOCA (args2, 1, nargs + 1);
32d08644
KH
284 args2[0] = Qcall_process;
285 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
08ee4e87 286 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
fcaf8878 287 val = CONSP (coding_systems) ? XCDR (coding_systems) : Qnil;
32d08644 288 }
fcaf8878 289 val = complement_process_encoding_system (val);
32d08644 290 setup_coding_system (Fcheck_coding_system (val), &argument_coding);
6e50da0a
KH
291 coding_attrs = CODING_ID_ATTRS (argument_coding.id);
292 if (NILP (CODING_ATTR_ASCII_COMPAT (coding_attrs)))
293 {
294 /* We should not use an ASCII incompatible coding system. */
295 val = raw_text_coding_system (val);
296 setup_coding_system (val, &argument_coding);
297 }
32d08644 298 }
32d08644
KH
299 }
300
e576cab4
JB
301 if (nargs >= 2 && ! NILP (args[1]))
302 {
4b4deea2 303 infile = Fexpand_file_name (args[1], BVAR (current_buffer, directory));
b7826503 304 CHECK_STRING (infile);
e576cab4 305 }
80856e74 306 else
5437e9f9 307 infile = build_string (NULL_DEVICE);
80856e74 308
e576cab4
JB
309 if (nargs >= 3)
310 {
39eaa782
RS
311 buffer = args[2];
312
1ef14cb4
LMI
313 /* If BUFFER is a list, its meaning is (BUFFER-FOR-STDOUT
314 FILE-FOR-STDERR), unless the first element is :file, in which case see
315 the next paragraph. */
19ed11ba
AS
316 if (CONSP (buffer)
317 && (! SYMBOLP (XCAR (buffer))
318 || strcmp (SSDATA (SYMBOL_NAME (XCAR (buffer))), ":file")))
39eaa782 319 {
70949dac 320 if (CONSP (XCDR (buffer)))
45be8a1e 321 {
a9d4f28a 322 Lisp_Object stderr_file;
70949dac 323 stderr_file = XCAR (XCDR (buffer));
45be8a1e
RS
324
325 if (NILP (stderr_file) || EQ (Qt, stderr_file))
326 error_file = stderr_file;
327 else
328 error_file = Fexpand_file_name (stderr_file, Qnil);
329 }
330
70949dac 331 buffer = XCAR (buffer);
39eaa782 332 }
044512ed 333
1ef14cb4 334 /* If the buffer is (still) a list, it might be a (:file "file") spec. */
19ed11ba
AS
335 if (CONSP (buffer)
336 && SYMBOLP (XCAR (buffer))
337 && ! strcmp (SSDATA (SYMBOL_NAME (XCAR (buffer))), ":file"))
1ef14cb4
LMI
338 {
339 output_file = Fexpand_file_name (XCAR (XCDR (buffer)),
340 BVAR (current_buffer, directory));
341 CHECK_STRING (output_file);
342 buffer = Qnil;
343 }
344
39eaa782
RS
345 if (!(EQ (buffer, Qnil)
346 || EQ (buffer, Qt)
3ffde7d6 347 || INTEGERP (buffer)))
e576cab4 348 {
39eaa782
RS
349 Lisp_Object spec_buffer;
350 spec_buffer = buffer;
50fe359b 351 buffer = Fget_buffer_create (buffer);
39eaa782
RS
352 /* Mention the buffer name for a better error message. */
353 if (NILP (buffer))
b7826503
PJ
354 CHECK_BUFFER (spec_buffer);
355 CHECK_BUFFER (buffer);
e576cab4
JB
356 }
357 }
177c0ea7 358 else
e576cab4 359 buffer = Qnil;
80856e74 360
58616e67
JB
361 /* Make sure that the child will be able to chdir to the current
362 buffer's current directory, or its unhandled equivalent. We
363 can't just have the child check for an error when it does the
364 chdir, since it's in a vfork.
365
366 We have to GCPRO around this because Fexpand_file_name,
367 Funhandled_file_name_directory, and Ffile_accessible_directory_p
368 might call a file name handling function. The argument list is
369 protected by the caller, so all we really have to worry about is
370 buffer. */
371 {
1ef14cb4 372 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
58616e67 373
4b4deea2 374 current_dir = BVAR (current_buffer, directory);
58616e67 375
1ef14cb4 376 GCPRO5 (infile, buffer, current_dir, error_file, output_file);
58616e67 377
ca319910
SM
378 current_dir = Funhandled_file_name_directory (current_dir);
379 if (NILP (current_dir))
380 /* If the file name handler says that current_dir is unreachable, use
381 a sensible default. */
382 current_dir = build_string ("~/");
383 current_dir = expand_and_dir_to_file (current_dir, Qnil);
db8454b0
CY
384 current_dir = Ffile_name_as_directory (current_dir);
385
58616e67
JB
386 if (NILP (Ffile_accessible_directory_p (current_dir)))
387 report_file_error ("Setting current directory",
4b4deea2 388 Fcons (BVAR (current_buffer, directory), Qnil));
58616e67 389
34b87689
KH
390 if (STRING_MULTIBYTE (infile))
391 infile = ENCODE_FILE (infile);
392 if (STRING_MULTIBYTE (current_dir))
393 current_dir = ENCODE_FILE (current_dir);
394 if (STRINGP (error_file) && STRING_MULTIBYTE (error_file))
395 error_file = ENCODE_FILE (error_file);
1ef14cb4
LMI
396 if (STRINGP (output_file) && STRING_MULTIBYTE (output_file))
397 output_file = ENCODE_FILE (output_file);
58616e67
JB
398 UNGCPRO;
399 }
400
d311d28c 401 display_p = INTERACTIVE && nargs >= 4 && !NILP (args[3]);
80856e74 402
42a5b22f 403 filefd = emacs_open (SSDATA (infile), O_RDONLY, 0);
80856e74 404 if (filefd < 0)
b0728617
EZ
405 report_file_error ("Opening process input file",
406 Fcons (DECODE_FILE (infile), Qnil));
1ef14cb4
LMI
407
408 if (STRINGP (output_file))
409 {
410#ifdef DOS_NT
411 fd_output = emacs_open (SSDATA (output_file),
412 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
413 S_IREAD | S_IWRITE);
414#else /* not DOS_NT */
415 fd_output = creat (SSDATA (output_file), 0666);
416#endif /* not DOS_NT */
417 if (fd_output < 0)
418 {
419 output_file = DECODE_FILE (output_file);
420 report_file_error ("Opening process output file",
421 Fcons (output_file, Qnil));
19ed11ba 422 }
1ef14cb4 423 if (STRINGP (error_file) || NILP (error_file))
19ed11ba 424 output_to_buffer = 0;
1ef14cb4
LMI
425 }
426
80856e74 427 /* Search for program; barf if not found. */
c52b0b34 428 {
ad3aaf33 429 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
c52b0b34 430
ad3aaf33 431 GCPRO4 (infile, buffer, current_dir, error_file);
5c150961 432 openp (Vexec_path, args[0], Vexec_suffixes, &path, make_number (X_OK));
c52b0b34
KH
433 UNGCPRO;
434 }
012c6fcb 435 if (NILP (path))
80856e74 436 {
68c45bf0 437 emacs_close (filefd);
80856e74
JB
438 report_file_error ("Searching for program", Fcons (args[0], Qnil));
439 }
8ee8f447
RS
440
441 /* If program file name starts with /: for quoting a magic name,
442 discard that. */
443 if (SBYTES (path) > 2 && SREF (path, 0) == '/'
444 && SREF (path, 1) == ':')
445 path = Fsubstring (path, make_number (2), Qnil);
446
98c6f1e3 447 new_argv = SAFE_ALLOCA ((nargs > 4 ? nargs - 2 : 2) * sizeof *new_argv);
c364e618
KH
448 if (nargs > 4)
449 {
f66c7cf8 450 ptrdiff_t i;
ad3aaf33 451 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
c364e618 452
ad3aaf33 453 GCPRO5 (infile, buffer, current_dir, path, error_file);
c5bfa12b
KH
454 argument_coding.dst_multibyte = 0;
455 for (i = 4; i < nargs; i++)
c364e618 456 {
c5bfa12b
KH
457 argument_coding.src_multibyte = STRING_MULTIBYTE (args[i]);
458 if (CODING_REQUIRE_ENCODING (&argument_coding))
91183bfd
KH
459 /* We must encode this argument. */
460 args[i] = encode_coding_string (&argument_coding, args[i], 1);
c364e618 461 }
c5bfa12b 462 UNGCPRO;
1aa83b22 463 for (i = 4; i < nargs; i++)
60aeceb8 464 new_argv[i - 3] = SSDATA (args[i]);
1aa83b22 465 new_argv[i - 3] = 0;
c364e618 466 }
db54baaa
KH
467 else
468 new_argv[1] = 0;
60aeceb8 469 new_argv[0] = SSDATA (path);
80856e74 470
7e6c2178 471#ifdef MSDOS /* MW, July 1993 */
7e6c2178 472
888c9e86
EZ
473 /* If we're redirecting STDOUT to a file, that file is already open
474 on fd_output. */
1ef14cb4 475 if (fd_output < 0)
7e6c2178 476 {
888c9e86
EZ
477 if ((outf = egetenv ("TMPDIR")))
478 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
479 else
480 {
481 tempfile = alloca (20);
482 *tempfile = '\0';
483 }
484 dostounix_filename (tempfile);
485 if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/')
486 strcat (tempfile, "/");
487 strcat (tempfile, "detmp.XXX");
488 mktemp (tempfile);
1ef14cb4
LMI
489 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
490 if (outfilefd < 0) {
491 emacs_close (filefd);
492 report_file_error ("Opening process output file",
493 Fcons (build_string (tempfile), Qnil));
494 }
7e6c2178 495 }
1ef14cb4
LMI
496 else
497 outfilefd = fd_output;
60aeceb8
PE
498 fd0 = filefd;
499 fd1 = outfilefd;
6f89d28a 500#endif /* MSDOS */
7e6c2178 501
d50d3dc8 502 if (INTEGERP (buffer))
60aeceb8
PE
503 {
504 fd0 = -1;
505 fd1 = emacs_open (NULL_DEVICE, O_WRONLY, 0);
506 }
80856e74
JB
507 else
508 {
7e6c2178 509#ifndef MSDOS
60aeceb8 510 int fd[2];
db92b288
GM
511 if (pipe (fd) == -1)
512 {
60aeceb8 513 int pipe_errno = errno;
db92b288 514 emacs_close (filefd);
60aeceb8 515 errno = pipe_errno;
db92b288
GM
516 report_file_error ("Creating process pipe", Qnil);
517 }
60aeceb8
PE
518 fd0 = fd[0];
519 fd1 = fd[1];
80856e74
JB
520#endif
521 }
522
523 {
39eaa782 524 int fd_error = fd1;
80856e74 525
1ef14cb4
LMI
526 if (fd_output >= 0)
527 fd1 = fd_output;
80856e74 528
39eaa782 529 if (NILP (error_file))
68c45bf0 530 fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0);
39eaa782
RS
531 else if (STRINGP (error_file))
532 {
533#ifdef DOS_NT
42a5b22f 534 fd_error = emacs_open (SSDATA (error_file),
68c45bf0
PE
535 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
536 S_IREAD | S_IWRITE);
39eaa782 537#else /* not DOS_NT */
42a5b22f 538 fd_error = creat (SSDATA (error_file), 0666);
39eaa782
RS
539#endif /* not DOS_NT */
540 }
541
542 if (fd_error < 0)
543 {
68c45bf0 544 emacs_close (filefd);
60aeceb8
PE
545 if (fd0 != filefd)
546 emacs_close (fd0);
39eaa782 547 if (fd1 >= 0)
68c45bf0 548 emacs_close (fd1);
6f89d28a
MB
549#ifdef MSDOS
550 unlink (tempfile);
551#endif
34b87689
KH
552 if (NILP (error_file))
553 error_file = build_string (NULL_DEVICE);
554 else if (STRINGP (error_file))
555 error_file = DECODE_FILE (error_file);
556 report_file_error ("Cannot redirect stderr", Fcons (error_file, Qnil));
39eaa782 557 }
89e1ec1d 558
2610078a 559#ifdef MSDOS /* MW, July 1993 */
c17c4250 560 /* Note that on MSDOS `child_setup' actually returns the child process
bb5f74ee 561 exit status, not its PID, so assign it to status below. */
60aeceb8 562 pid = child_setup (filefd, outfilefd, fd_error, new_argv, 0, current_dir);
bb5f74ee 563 child_errno = errno;
2610078a 564
68c45bf0 565 emacs_close (outfilefd);
2610078a 566 if (fd_error != outfilefd)
68c45bf0 567 emacs_close (fd_error);
bb5f74ee
PE
568 if (pid < 0)
569 {
570 synchronize_system_messages_locale ();
571 return
572 code_convert_string_norecord (build_string (strerror (child_errno)),
573 Vlocale_coding_system, 0);
574 }
575 status = pid;
2610078a 576 fd1 = -1; /* No harm in closing that one! */
888c9e86 577 if (tempfile)
2610078a 578 {
888c9e86
EZ
579 /* Since CRLF is converted to LF within `decode_coding', we
580 can always open a file with binary mode. */
60aeceb8
PE
581 fd0 = emacs_open (tempfile, O_RDONLY | O_BINARY, 0);
582 if (fd0 < 0)
888c9e86
EZ
583 {
584 unlink (tempfile);
585 emacs_close (filefd);
586 report_file_error ("Cannot re-open temporary file",
7285dc67 587 Fcons (build_string (tempfile), Qnil));
888c9e86 588 }
2610078a 589 }
888c9e86 590 else
60aeceb8 591 fd0 = -1; /* We are not going to read from tempfile. */
644d3f0d 592#endif /* MSDOS */
bb5f74ee
PE
593
594 /* Do the unwind-protect now, even though the pid is not known, so
595 that no storage allocation is done in the critical section.
596 The actual PID will be filled in during the critical section. */
597 synch_process_pid = 0;
598 synch_process_fd = fd0;
644d3f0d
PE
599
600#ifdef MSDOS
601 /* MSDOS needs different cleanup information. */
602 record_unwind_protect (call_process_cleanup,
603 Fcons (Fcurrent_buffer (),
604 build_string (tempfile ? tempfile : "")));
605#else
bb5f74ee
PE
606 record_unwind_protect (call_process_cleanup, Fcurrent_buffer ());
607
608 block_input ();
609 block_child_signal ();
610
bad95d8f 611#ifdef WINDOWSNT
60aeceb8 612 pid = child_setup (filefd, fd1, fd_error, new_argv, 0, current_dir);
b0728617
EZ
613 /* We need to record the input file of this child, for when we are
614 called from call-process-region to create an async subprocess.
615 That's because call-process-region's unwind procedure will
616 attempt to delete the temporary input file, which will fail
617 because that file is still in use. Recording it with the child
618 will allow us to delete the file when the subprocess exits.
619 The second part of this is in delete_temp_file, q.v. */
620 if (pid > 0 && INTEGERP (buffer) && nargs >= 2 && !NILP (args[1]))
621 record_infile (pid, xstrdup (SSDATA (infile)));
bad95d8f 622#else /* not WINDOWSNT */
c0ad4ea5 623
db6c0e74 624 /* vfork, and prevent local vars from being clobbered by the vfork. */
b08a63cc 625 {
c74e9d86
PE
626 Lisp_Object volatile buffer_volatile = buffer;
627 Lisp_Object volatile coding_systems_volatile = coding_systems;
628 Lisp_Object volatile current_dir_volatile = current_dir;
2f221583
PE
629 bool volatile display_p_volatile = display_p;
630 bool volatile output_to_buffer_volatile = output_to_buffer;
631 bool volatile sa_must_free_volatile = sa_must_free;
5266b4bb 632 int volatile fd1_volatile = fd1;
db6c0e74
PE
633 int volatile fd_error_volatile = fd_error;
634 int volatile fd_output_volatile = fd_output;
60aeceb8
PE
635 int volatile filefd_volatile = filefd;
636 ptrdiff_t volatile count_volatile = count;
d311d28c 637 ptrdiff_t volatile sa_count_volatile = sa_count;
60aeceb8 638 char **volatile new_argv_volatile = new_argv;
80856e74 639
db6c0e74 640 pid = vfork ();
bb5f74ee 641 child_errno = errno;
80856e74 642
c74e9d86
PE
643 buffer = buffer_volatile;
644 coding_systems = coding_systems_volatile;
645 current_dir = current_dir_volatile;
d311d28c 646 display_p = display_p_volatile;
60aeceb8
PE
647 output_to_buffer = output_to_buffer_volatile;
648 sa_must_free = sa_must_free_volatile;
5266b4bb 649 fd1 = fd1_volatile;
db6c0e74
PE
650 fd_error = fd_error_volatile;
651 fd_output = fd_output_volatile;
60aeceb8
PE
652 filefd = filefd_volatile;
653 count = count_volatile;
d311d28c 654 sa_count = sa_count_volatile;
db6c0e74 655 new_argv = new_argv_volatile;
644d3f0d
PE
656
657 fd0 = synch_process_fd;
db6c0e74 658 }
b9c7f648 659
80856e74
JB
660 if (pid == 0)
661 {
bb5f74ee
PE
662 unblock_child_signal ();
663
60aeceb8
PE
664 if (fd0 >= 0)
665 emacs_close (fd0);
322aea6d 666
19ed11ba 667 setsid ();
c0ad4ea5 668
4d7e6e51 669 /* Emacs ignores SIGPIPE, but the child should not. */
2f9b9adb 670 signal (SIGPIPE, SIG_DFL);
c0ad4ea5 671
60aeceb8 672 child_setup (filefd, fd1, fd_error, new_argv, 0, current_dir);
80856e74 673 }
aba637ec 674
bad95d8f 675#endif /* not WINDOWSNT */
cd5f8f60 676
bb5f74ee
PE
677 child_errno = errno;
678
679 if (0 < pid)
680 {
681 if (INTEGERP (buffer))
682 record_deleted_pid (pid);
683 else
684 synch_process_pid = pid;
685 }
686
687 unblock_child_signal ();
688 unblock_input ();
689
cd5f8f60
RS
690 /* The MSDOS case did this already. */
691 if (fd_error >= 0)
68c45bf0 692 emacs_close (fd_error);
2610078a 693#endif /* not MSDOS */
80856e74 694
60aeceb8 695 /* Close most of our file descriptors, but not fd0
6b6e798b 696 since we will use that to read input from. */
68c45bf0 697 emacs_close (filefd);
1ef14cb4
LMI
698 if (fd_output >= 0)
699 emacs_close (fd_output);
799abb26 700 if (fd1 >= 0 && fd1 != fd_error)
68c45bf0 701 emacs_close (fd1);
80856e74
JB
702 }
703
704 if (pid < 0)
705 {
bb5f74ee 706 errno = child_errno;
80856e74
JB
707 report_file_error ("Doing vfork", Qnil);
708 }
709
d50d3dc8 710 if (INTEGERP (buffer))
644d3f0d 711 return unbind_to (count, Qnil);
80856e74 712
d50d3dc8 713 if (BUFFERP (buffer))
80856e74
JB
714 Fset_buffer (buffer);
715
09494912
RS
716 if (NILP (buffer))
717 {
718 /* If BUFFER is nil, we must read process output once and then
719 discard it, so setup coding system but with nil. */
720 setup_coding_system (Qnil, &process_coding);
659afede 721 process_coding.dst_multibyte = 0;
09494912
RS
722 }
723 else
724 {
725 Lisp_Object val, *args2;
726
727 val = Qnil;
728 if (!NILP (Vcoding_system_for_read))
729 val = Vcoding_system_for_read;
730 else
731 {
732 if (EQ (coding_systems, Qt))
733 {
f66c7cf8 734 ptrdiff_t i;
09494912 735
0065d054 736 SAFE_NALLOCA (args2, 1, nargs + 1);
09494912
RS
737 args2[0] = Qcall_process;
738 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
739 coding_systems
740 = Ffind_operation_coding_system (nargs + 1, args2);
741 }
742 if (CONSP (coding_systems))
70949dac 743 val = XCAR (coding_systems);
09494912 744 else if (CONSP (Vdefault_process_coding_system))
70949dac 745 val = XCAR (Vdefault_process_coding_system);
09494912
RS
746 else
747 val = Qnil;
748 }
91183bfd 749 Fcheck_coding_system (val);
09494912
RS
750 /* In unibyte mode, character code conversion should not take
751 place but EOL conversion should. So, setup raw-text or one
752 of the subsidiary according to the information just setup. */
4b4deea2 753 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
09494912 754 && !NILP (val))
91183bfd
KH
755 val = raw_text_coding_system (val);
756 setup_coding_system (val, &process_coding);
659afede
KH
757 process_coding.dst_multibyte
758 = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
09494912 759 }
a0241d01 760 process_coding.src_multibyte = 0;
09494912 761
80856e74
JB
762 immediate_quit = 1;
763 QUIT;
764
1ef14cb4 765 if (output_to_buffer)
19ed11ba 766 {
60aeceb8
PE
767 enum { CALLPROC_BUFFER_SIZE_MIN = 16 * 1024 };
768 enum { CALLPROC_BUFFER_SIZE_MAX = 4 * CALLPROC_BUFFER_SIZE_MIN };
769 char buf[CALLPROC_BUFFER_SIZE_MAX];
770 int bufsize = CALLPROC_BUFFER_SIZE_MIN;
2f221583
PE
771 int nread;
772 bool first = 1;
19ed11ba
AS
773 EMACS_INT total_read = 0;
774 int carryover = 0;
2f221583 775 bool display_on_the_fly = display_p;
19ed11ba
AS
776 struct coding_system saved_coding;
777
778 saved_coding = process_coding;
779 while (1)
780 {
781 /* Repeatedly read until we've filled as much as possible
782 of the buffer size we have. But don't read
783 less than 1024--save that for the next bufferful. */
784 nread = carryover;
785 while (nread < bufsize - 1024)
786 {
60aeceb8 787 int this_read = emacs_read (fd0, buf + nread,
19ed11ba 788 bufsize - nread);
60558b19 789
19ed11ba
AS
790 if (this_read < 0)
791 goto give_up;
60558b19 792
19ed11ba
AS
793 if (this_read == 0)
794 {
795 process_coding.mode |= CODING_MODE_LAST_BLOCK;
796 break;
797 }
60558b19 798
19ed11ba
AS
799 nread += this_read;
800 total_read += this_read;
60558b19 801
19ed11ba
AS
802 if (display_on_the_fly)
803 break;
804 }
60558b19 805
19ed11ba
AS
806 /* Now NREAD is the total amount of data in the buffer. */
807 immediate_quit = 0;
177c0ea7 808
19ed11ba
AS
809 if (!NILP (buffer))
810 {
811 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
812 && ! CODING_MAY_REQUIRE_DECODING (&process_coding))
813 insert_1_both (buf, nread, nread, 0, 1, 0);
814 else
815 { /* We have to decode the input. */
816 Lisp_Object curbuf;
d311d28c 817 ptrdiff_t count1 = SPECPDL_INDEX ();
19ed11ba
AS
818
819 XSETBUFFER (curbuf, current_buffer);
820 /* We cannot allow after-change-functions be run
821 during decoding, because that might modify the
822 buffer, while we rely on process_coding.produced to
823 faithfully reflect inserted text until we
824 TEMP_SET_PT_BOTH below. */
825 specbind (Qinhibit_modification_hooks, Qt);
826 decode_coding_c_string (&process_coding,
827 (unsigned char *) buf, nread, curbuf);
828 unbind_to (count1, Qnil);
829 if (display_on_the_fly
830 && CODING_REQUIRE_DETECTION (&saved_coding)
831 && ! CODING_REQUIRE_DETECTION (&process_coding))
832 {
833 /* We have detected some coding system. But,
834 there's a possibility that the detection was
835 done by insufficient data. So, we give up
836 displaying on the fly. */
837 if (process_coding.produced > 0)
838 del_range_2 (process_coding.dst_pos,
839 process_coding.dst_pos_byte,
840 process_coding.dst_pos
841 + process_coding.produced_char,
842 process_coding.dst_pos_byte
843 + process_coding.produced, 0);
844 display_on_the_fly = 0;
845 process_coding = saved_coding;
846 carryover = nread;
847 /* This is to make the above condition always
848 fails in the future. */
849 saved_coding.common_flags
850 &= ~CODING_REQUIRE_DETECTION_MASK;
851 continue;
852 }
853
854 TEMP_SET_PT_BOTH (PT + process_coding.produced_char,
855 PT_BYTE + process_coding.produced);
856 carryover = process_coding.carryover_bytes;
857 if (carryover > 0)
858 memcpy (buf, process_coding.carryover,
859 process_coding.carryover_bytes);
860 }
861 }
c5bfa12b 862
19ed11ba
AS
863 if (process_coding.mode & CODING_MODE_LAST_BLOCK)
864 break;
6e3bfbb2 865
19ed11ba
AS
866 /* Make the buffer bigger as we continue to read more data,
867 but not past CALLPROC_BUFFER_SIZE_MAX. */
868 if (bufsize < CALLPROC_BUFFER_SIZE_MAX && total_read > 32 * bufsize)
869 if ((bufsize *= 2) > CALLPROC_BUFFER_SIZE_MAX)
870 bufsize = CALLPROC_BUFFER_SIZE_MAX;
6e3bfbb2 871
19ed11ba
AS
872 if (display_p)
873 {
874 if (first)
875 prepare_menu_bars ();
876 first = 0;
877 redisplay_preserve_echo_area (1);
878 /* This variable might have been set to 0 for code
879 detection. In that case, we set it back to 1 because
880 we should have already detected a coding system. */
881 display_on_the_fly = 1;
882 }
883 immediate_quit = 1;
884 QUIT;
885 }
886 give_up: ;
887
888 Vlast_coding_system_used = CODING_ID_NAME (process_coding.id);
889 /* If the caller required, let the buffer inherit the
890 coding-system used to decode the process output. */
891 if (inherit_process_coding_system)
892 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
893 make_number (total_read));
894 }
3b440bb5 895
77defa9a 896#ifndef MSDOS
80856e74 897 /* Wait for it to terminate, unless it already has. */
bb5f74ee 898 wait_for_termination (pid, &status, !output_to_buffer);
77defa9a 899#endif
80856e74
JB
900
901 immediate_quit = 0;
902
37d54121
RS
903 /* Don't kill any children that the subprocess may have left behind
904 when exiting. */
bb5f74ee 905 synch_process_pid = 0;
37d54121 906
3c59b4c9 907 SAFE_FREE ();
80856e74
JB
908 unbind_to (count, Qnil);
909
bb5f74ee 910 if (WIFSIGNALED (status))
6b61353c 911 {
42ca4633 912 const char *signame;
6b61353c
KH
913
914 synchronize_system_messages_locale ();
bb5f74ee 915 signame = strsignal (WTERMSIG (status));
6b61353c
KH
916
917 if (signame == 0)
19ed11ba 918 signame = "unknown";
6b61353c 919
bb5f74ee
PE
920 return code_convert_string_norecord (build_string (signame),
921 Vlocale_coding_system, 0);
6b61353c
KH
922 }
923
bb5f74ee
PE
924 eassert (WIFEXITED (status));
925 return make_number (WEXITSTATUS (status));
80856e74 926}
80856e74 927\f
9fefd2ba 928static Lisp_Object
971de7fb 929delete_temp_file (Lisp_Object name)
80856e74 930{
1a271e14 931 /* Suppress jka-compr handling, etc. */
d311d28c 932 ptrdiff_t count = SPECPDL_INDEX ();
1a271e14 933 specbind (intern ("file-name-handler-alist"), Qnil);
b0728617
EZ
934#ifdef WINDOWSNT
935 /* If this is called when the subprocess didn't exit yet, the
936 attempt to delete its input file will fail. In that case, we
937 schedule the file for deletion when the subprocess exits. This
938 is the 2nd part of handling this situation; see the call to
939 record_infile in call-process above, for the first part. */
940 if (!internal_delete_file (name))
941 {
942 Lisp_Object encoded_file = ENCODE_FILE (name);
943
944 record_pending_deletion (SSDATA (encoded_file));
945 }
946#else
f1a5d776 947 internal_delete_file (name);
b0728617 948#endif
1a271e14 949 unbind_to (count, Qnil);
320695d8 950 return Qnil;
80856e74
JB
951}
952
953DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
fdb82f93
PJ
954 3, MANY, 0,
955 doc: /* Send text from START to END to a synchronous process running PROGRAM.
956The remaining arguments are optional.
957Delete the text if fourth arg DELETE is non-nil.
958
1ef14cb4
LMI
959Insert output in BUFFER before point; t means current buffer; nil for
960 BUFFER means discard it; 0 means discard and don't wait; and `(:file
961 FILE)', where FILE is a file name string, means that it should be
1b9f60cc 962 written to that file (if the file already exists it is overwritten).
fdb82f93
PJ
963BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
964REAL-BUFFER says what to do with standard output, as above,
965while STDERR-FILE says what to do with standard error in the child.
966STDERR-FILE may be nil (discard standard error output),
967t (mix it with ordinary output), or a file name string.
968
969Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.
970Remaining args are passed to PROGRAM at startup as command args.
971
ba9a5174 972If BUFFER is 0, `call-process-region' returns immediately with value nil.
fdb82f93
PJ
973Otherwise it waits for PROGRAM to terminate
974and returns a numeric exit status or a signal description string.
d98b59b5
MB
975If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
976
977usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */)
f66c7cf8 978 (ptrdiff_t nargs, Lisp_Object *args)
80856e74 979{
39323a7e
KH
980 struct gcpro gcpro1;
981 Lisp_Object filename_string;
982 register Lisp_Object start, end;
d311d28c 983 ptrdiff_t count = SPECPDL_INDEX ();
08ee4e87 984 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
09494912 985 Lisp_Object coding_systems;
32d08644 986 Lisp_Object val, *args2;
f66c7cf8 987 ptrdiff_t i;
98c6f1e3 988 Lisp_Object tmpdir;
7e6c2178 989
f92d51d8
CY
990 if (STRINGP (Vtemporary_file_directory))
991 tmpdir = Vtemporary_file_directory;
7e6c2178
RS
992 else
993 {
f92d51d8
CY
994#ifndef DOS_NT
995 if (getenv ("TMPDIR"))
996 tmpdir = build_string (getenv ("TMPDIR"));
997 else
998 tmpdir = build_string ("/tmp/");
999#else /* DOS_NT */
1000 char *outf;
1001 if ((outf = egetenv ("TMPDIR"))
1002 || (outf = egetenv ("TMP"))
1003 || (outf = egetenv ("TEMP")))
1004 tmpdir = build_string (outf);
1005 else
1006 tmpdir = Ffile_name_as_directory (build_string ("c:/temp"));
0774fcf8 1007#endif
f92d51d8 1008 }
7e6c2178 1009
3c59b4c9
PE
1010 {
1011 USE_SAFE_ALLOCA;
98c6f1e3 1012 Lisp_Object pattern = Fexpand_file_name (Vtemp_file_name_pattern, tmpdir);
7c2fcf9b
EZ
1013 Lisp_Object encoded_tem = ENCODE_FILE (pattern);
1014 char *tempfile = SAFE_ALLOCA (SBYTES (encoded_tem) + 1);
1015 memcpy (tempfile, SDATA (encoded_tem), SBYTES (encoded_tem) + 1);
3c59b4c9 1016 coding_systems = Qt;
09494912 1017
1ddc85a4 1018#ifdef HAVE_MKSTEMP
3c59b4c9
PE
1019 {
1020 int fd;
1021
4d7e6e51 1022 block_input ();
3c59b4c9 1023 fd = mkstemp (tempfile);
4d7e6e51 1024 unblock_input ();
3c59b4c9
PE
1025 if (fd == -1)
1026 report_file_error ("Failed to open temporary file",
baac5bc7 1027 Fcons (build_string (tempfile), Qnil));
3c59b4c9
PE
1028 else
1029 close (fd);
1030 }
1ddc85a4 1031#else
d3cefd13 1032 errno = 0;
3c59b4c9 1033 mktemp (tempfile);
d3cefd13
EZ
1034 if (!*tempfile)
1035 {
1036 if (!errno)
1037 errno = EEXIST;
1038 report_file_error ("Failed to open temporary file using pattern",
1039 Fcons (pattern, Qnil));
1040 }
1ddc85a4 1041#endif
80856e74 1042
3c59b4c9
PE
1043 filename_string = build_string (tempfile);
1044 GCPRO1 (filename_string);
1045 SAFE_FREE ();
1046 }
1047
80856e74
JB
1048 start = args[0];
1049 end = args[1];
32d08644 1050 /* Decide coding-system of the contents of the temporary file. */
91489411
RS
1051 if (!NILP (Vcoding_system_for_write))
1052 val = Vcoding_system_for_write;
4b4deea2 1053 else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
fcaf8878 1054 val = Qraw_text;
32d08644 1055 else
beacaab3 1056 {
3c59b4c9 1057 USE_SAFE_ALLOCA;
0065d054 1058 SAFE_NALLOCA (args2, 1, nargs + 1);
91489411
RS
1059 args2[0] = Qcall_process_region;
1060 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
1061 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
fcaf8878 1062 val = CONSP (coding_systems) ? XCDR (coding_systems) : Qnil;
3c59b4c9 1063 SAFE_FREE ();
beacaab3 1064 }
fcaf8878 1065 val = complement_process_encoding_system (val);
32d08644 1066
168afdaa 1067 {
d311d28c 1068 ptrdiff_t count1 = SPECPDL_INDEX ();
168afdaa
RS
1069
1070 specbind (intern ("coding-system-for-write"), val);
bb951f0e
KR
1071 /* POSIX lets mk[s]temp use "."; don't invoke jka-compr if we
1072 happen to get a ".Z" suffix. */
1073 specbind (intern ("file-name-handler-alist"), Qnil);
168afdaa
RS
1074 Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil);
1075
1076 unbind_to (count1, Qnil);
1077 }
91489411 1078
177c0ea7 1079 /* Note that Fcall_process takes care of binding
91489411 1080 coding-system-for-read. */
093650fe 1081
80856e74
JB
1082 record_unwind_protect (delete_temp_file, filename_string);
1083
edf496dd 1084 if (nargs > 3 && !NILP (args[3]))
80856e74
JB
1085 Fdelete_region (start, end);
1086
edf496dd
KH
1087 if (nargs > 3)
1088 {
1089 args += 2;
1090 nargs -= 2;
1091 }
1092 else
1093 {
1094 args[0] = args[2];
1095 nargs = 2;
1096 }
1097 args[1] = filename_string;
80856e74 1098
edf496dd 1099 RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs, args)));
80856e74
JB
1100}
1101\f
361358ea 1102#ifndef WINDOWSNT
971de7fb 1103static int relocate_fd (int fd, int minfd);
361358ea 1104#endif
dfcf069d 1105
5990851d
KL
1106static char **
1107add_env (char **env, char **new_env, char *string)
1108{
1109 char **ep;
2f221583 1110 bool ok = 1;
5990851d
KL
1111 if (string == NULL)
1112 return new_env;
1113
1114 /* See if this string duplicates any string already in the env.
1115 If so, don't put it in.
1116 When an env var has multiple definitions,
1117 we keep the definition that comes first in process-environment. */
1118 for (ep = env; ok && ep != new_env; ep++)
1119 {
1120 char *p = *ep, *q = string;
1121 while (ok)
19ed11ba
AS
1122 {
1123 if (*q != *p)
1124 break;
1125 if (*q == 0)
1126 /* The string is a lone variable name; keep it for now, we
1127 will remove it later. It is a placeholder for a
1128 variable that is not to be included in the environment. */
1129 break;
1130 if (*q == '=')
1131 ok = 0;
1132 p++, q++;
1133 }
5990851d
KL
1134 }
1135 if (ok)
1136 *new_env++ = string;
1137 return new_env;
1138}
1139
80856e74
JB
1140/* This is the last thing run in a newly forked inferior
1141 either synchronous or asynchronous.
1142 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1143 Initialize inferior's priority, pgrp, connected dir and environment.
1144 then exec another program based on new_argv.
1145
2f221583 1146 If SET_PGRP, put the subprocess into a separate process group.
e576cab4
JB
1147
1148 CURRENT_DIR is an elisp string giving the path of the current
1149 directory the subprocess should have. Since we can't really signal
1150 a decent error from within the child, this should be verified as an
1151 executable directory by the parent. */
80856e74 1152
dfcf069d 1153int
2f221583
PE
1154child_setup (int in, int out, int err, char **new_argv, bool set_pgrp,
1155 Lisp_Object current_dir)
80856e74 1156{
e576cab4 1157 char **env;
7fcf7f05 1158 char *pwd_var;
bad95d8f
RS
1159#ifdef WINDOWSNT
1160 int cpid;
4252a4bd 1161 HANDLE handles[3];
bad95d8f 1162#endif /* WINDOWSNT */
e576cab4 1163
d311d28c 1164 pid_t pid = getpid ();
80856e74 1165
80856e74
JB
1166 /* Close Emacs's descriptors that this process should not have. */
1167 close_process_descs ();
ded80a25 1168
c17c4250
EZ
1169 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1170 we will lose if we call close_load_descs here. */
1171#ifndef DOS_NT
4458cebe 1172 close_load_descs ();
c17c4250 1173#endif
80856e74
JB
1174
1175 /* Note that use of alloca is always safe here. It's obvious for systems
1176 that do not have true vfork or that have true (stack) alloca.
caa01fe0
GM
1177 If using vfork and C_ALLOCA (when Emacs used to include
1178 src/alloca.c) it is safe because that changes the superior's
1179 static variables as if the superior had done alloca and will be
1180 cleaned up in the usual way. */
e576cab4 1181 {
7fcf7f05 1182 register char *temp;
0065d054 1183 size_t i; /* size_t, because ptrdiff_t might overflow here! */
77d78be1 1184
d5db4077 1185 i = SBYTES (current_dir);
16425c4a
EZ
1186#ifdef MSDOS
1187 /* MSDOS must have all environment variables malloc'ed, because
1188 low-level libc functions that launch subsidiary processes rely
1189 on that. */
23f86fce 1190 pwd_var = xmalloc (i + 6);
16425c4a 1191#else
38182d90 1192 pwd_var = alloca (i + 6);
16425c4a 1193#endif
7fcf7f05 1194 temp = pwd_var + 4;
72af86bd
AS
1195 memcpy (pwd_var, "PWD=", 4);
1196 memcpy (temp, SDATA (current_dir), i);
bad95d8f 1197 if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP;
e576cab4
JB
1198 temp[i] = 0;
1199
c17c4250 1200#ifndef DOS_NT
e576cab4
JB
1201 /* We can't signal an Elisp error here; we're in a vfork. Since
1202 the callers check the current directory before forking, this
1203 should only return an error if the directory's permissions
1204 are changed between the check and this chdir, but we should
1205 at least check. */
1206 if (chdir (temp) < 0)
20b25e46 1207 _exit (errno);
f8d23104 1208#else /* DOS_NT */
c17c4250
EZ
1209 /* Get past the drive letter, so that d:/ is left alone. */
1210 if (i > 2 && IS_DEVICE_SEP (temp[1]) && IS_DIRECTORY_SEP (temp[2]))
1211 {
1212 temp += 2;
1213 i -= 2;
1214 }
f8d23104 1215#endif /* DOS_NT */
c17c4250 1216
7fcf7f05 1217 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
bad95d8f 1218 while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1]))
7fcf7f05 1219 temp[--i] = 0;
e576cab4 1220 }
80856e74 1221
5990851d 1222 /* Set `env' to a vector of the strings in the environment. */
80856e74
JB
1223 {
1224 register Lisp_Object tem;
1225 register char **new_env;
5990851d 1226 char **p, **q;
80856e74 1227 register int new_length;
d2bb6598 1228 Lisp_Object display = Qnil;
361358ea 1229
80856e74 1230 new_length = 0;
f105f403 1231
80856e74 1232 for (tem = Vprocess_environment;
19ed11ba
AS
1233 CONSP (tem) && STRINGP (XCAR (tem));
1234 tem = XCDR (tem))
d2bb6598 1235 {
42a5b22f 1236 if (strncmp (SSDATA (XCAR (tem)), "DISPLAY", 7) == 0
d2bb6598
SM
1237 && (SDATA (XCAR (tem)) [7] == '\0'
1238 || SDATA (XCAR (tem)) [7] == '='))
1239 /* DISPLAY is specified in process-environment. */
1240 display = Qt;
1241 new_length++;
1242 }
de87fb59 1243
d2bb6598
SM
1244 /* If not provided yet, use the frame's DISPLAY. */
1245 if (NILP (display))
1246 {
1247 Lisp_Object tmp = Fframe_parameter (selected_frame, Qdisplay);
1248 if (!STRINGP (tmp) && CONSP (Vinitial_environment))
1249 /* If still not found, Look for DISPLAY in Vinitial_environment. */
1250 tmp = Fgetenv_internal (build_string ("DISPLAY"),
1251 Vinitial_environment);
1252 if (STRINGP (tmp))
1253 {
1254 display = tmp;
1255 new_length++;
1256 }
1257 }
80856e74 1258
7fcf7f05 1259 /* new_length + 2 to include PWD and terminating 0. */
38182d90 1260 env = new_env = alloca ((new_length + 2) * sizeof *env);
7fcf7f05
RS
1261 /* If we have a PWD envvar, pass one down,
1262 but with corrected value. */
a13f8f50 1263 if (egetenv ("PWD"))
7fcf7f05 1264 *new_env++ = pwd_var;
361358ea 1265
6b8e474c 1266 if (STRINGP (display))
de87fb59 1267 {
38182d90 1268 char *vdata = alloca (sizeof "DISPLAY=" + SBYTES (display));
de87fb59 1269 strcpy (vdata, "DISPLAY=");
42a5b22f 1270 strcat (vdata, SSDATA (display));
de87fb59
DN
1271 new_env = add_env (env, new_env, vdata);
1272 }
80856e74 1273
5990851d 1274 /* Overrides. */
80856e74 1275 for (tem = Vprocess_environment;
70949dac
KR
1276 CONSP (tem) && STRINGP (XCAR (tem));
1277 tem = XCDR (tem))
42a5b22f 1278 new_env = add_env (env, new_env, SSDATA (XCAR (tem)));
d2bb6598 1279
5990851d
KL
1280 *new_env = 0;
1281
1282 /* Remove variable names without values. */
1283 p = q = env;
1284 while (*p != 0)
cd9565ba 1285 {
19ed11ba
AS
1286 while (*q != 0 && strchr (*q, '=') == NULL)
1287 q++;
1288 *p = *q++;
1289 if (*p != 0)
1290 p++;
cd9565ba 1291 }
80856e74 1292 }
de87fb59 1293
361358ea 1294
bad95d8f
RS
1295#ifdef WINDOWSNT
1296 prepare_standard_handles (in, out, err, handles);
d5db4077 1297 set_process_dir (SDATA (current_dir));
d3d14b40 1298 /* Spawn the child. (See w32proc.c:sys_spawnve). */
67802943
DN
1299 cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
1300 reset_standard_handles (in, out, err, handles);
1301 if (cpid == -1)
1302 /* An error occurred while trying to spawn the process. */
1303 report_file_error ("Spawning child process", Qnil);
1304 return cpid;
1305
bad95d8f 1306#else /* not WINDOWSNT */
426b37ae
JB
1307 /* Make sure that in, out, and err are not actually already in
1308 descriptors zero, one, or two; this could happen if Emacs is
7e6c2178 1309 started with its standard in, out, or error closed, as might
426b37ae 1310 happen under X. */
f29f9e4a
RS
1311 {
1312 int oin = in, oout = out;
1313
1314 /* We have to avoid relocating the same descriptor twice! */
1315
1316 in = relocate_fd (in, 3);
1317
1318 if (out == oin)
1319 out = in;
1320 else
3e9367e7 1321 out = relocate_fd (out, 3);
f29f9e4a
RS
1322
1323 if (err == oin)
1324 err = in;
1325 else if (err == oout)
1326 err = out;
1327 else
3e9367e7 1328 err = relocate_fd (err, 3);
f29f9e4a 1329 }
426b37ae 1330
c17c4250 1331#ifndef MSDOS
68c45bf0
PE
1332 emacs_close (0);
1333 emacs_close (1);
1334 emacs_close (2);
80856e74
JB
1335
1336 dup2 (in, 0);
1337 dup2 (out, 1);
1338 dup2 (err, 2);
68c45bf0 1339 emacs_close (in);
ce8f5a9a
AS
1340 if (out != in)
1341 emacs_close (out);
1342 if (err != in && err != out)
1343 emacs_close (err);
80856e74 1344
dd0333b6 1345 setpgid (0, 0);
12e610e8 1346 tcsetpgrp (0, pid);
a7ebc409 1347
21e54a94 1348 execve (new_argv[0], new_argv, env);
80856e74 1349
68c45bf0
PE
1350 emacs_write (1, "Can't exec program: ", 20);
1351 emacs_write (1, new_argv[0], strlen (new_argv[0]));
1352 emacs_write (1, "\n", 1);
80856e74 1353 _exit (1);
67802943
DN
1354
1355#else /* MSDOS */
1356 pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env);
1357 xfree (pwd_var);
1358 if (pid == -1)
1359 /* An error occurred while trying to run the subprocess. */
1360 report_file_error ("Spawning child process", Qnil);
1361 return pid;
1362#endif /* MSDOS */
ae76d9e1 1363#endif /* not WINDOWSNT */
80856e74
JB
1364}
1365
361358ea 1366#ifndef WINDOWSNT
a3833dfe 1367/* Move the file descriptor FD so that its number is not less than MINFD.
426b37ae 1368 If the file descriptor is moved at all, the original is freed. */
dfcf069d 1369static int
971de7fb 1370relocate_fd (int fd, int minfd)
426b37ae 1371{
a3833dfe 1372 if (fd >= minfd)
426b37ae
JB
1373 return fd;
1374 else
1375 {
49cdacda 1376 int new = fcntl (fd, F_DUPFD, minfd);
426b37ae
JB
1377 if (new == -1)
1378 {
cb1d0ef7 1379 const char *message_1 = "Error while setting up child: ";
a8fe7202 1380 const char *errmessage = strerror (errno);
cb1d0ef7
PE
1381 const char *message_2 = "\n";
1382 emacs_write (2, message_1, strlen (message_1));
68c45bf0 1383 emacs_write (2, errmessage, strlen (errmessage));
cb1d0ef7 1384 emacs_write (2, message_2, strlen (message_2));
426b37ae
JB
1385 _exit (1);
1386 }
68c45bf0 1387 emacs_close (fd);
426b37ae
JB
1388 return new;
1389 }
1390}
361358ea 1391#endif /* not WINDOWSNT */
426b37ae 1392
2f221583 1393static bool
989f33ba
PE
1394getenv_internal_1 (const char *var, ptrdiff_t varlen, char **value,
1395 ptrdiff_t *valuelen, Lisp_Object env)
012c6fcb 1396{
db699fc6 1397 for (; CONSP (env); env = XCDR (env))
012c6fcb 1398 {
db699fc6 1399 Lisp_Object entry = XCAR (env);
d50d3dc8 1400 if (STRINGP (entry)
db699fc6 1401 && SBYTES (entry) >= varlen
bad95d8f
RS
1402#ifdef WINDOWSNT
1403 /* NT environment variables are case insensitive. */
d5db4077 1404 && ! strnicmp (SDATA (entry), var, varlen)
bad95d8f 1405#else /* not WINDOWSNT */
72af86bd 1406 && ! memcmp (SDATA (entry), var, varlen)
bad95d8f 1407#endif /* not WINDOWSNT */
a9971c6d 1408 )
012c6fcb 1409 {
db699fc6
SM
1410 if (SBYTES (entry) > varlen && SREF (entry, varlen) == '=')
1411 {
51b59d79 1412 *value = SSDATA (entry) + (varlen + 1);
db699fc6
SM
1413 *valuelen = SBYTES (entry) - (varlen + 1);
1414 return 1;
1415 }
1416 else if (SBYTES (entry) == varlen)
1417 {
1418 /* Lone variable names in Vprocess_environment mean that
1419 variable should be removed from the environment. */
1420 *value = NULL;
1421 return 1;
1422 }
1423 }
1424 }
1425 return 0;
1426}
1427
2f221583 1428static bool
989f33ba
PE
1429getenv_internal (const char *var, ptrdiff_t varlen, char **value,
1430 ptrdiff_t *valuelen, Lisp_Object frame)
012c6fcb 1431{
d2bb6598
SM
1432 /* Try to find VAR in Vprocess_environment first. */
1433 if (getenv_internal_1 (var, varlen, value, valuelen,
1434 Vprocess_environment))
1435 return *value ? 1 : 0;
f105f403 1436
d2bb6598 1437 /* For DISPLAY try to get the values from the frame or the initial env. */
de87fb59 1438 if (strcmp (var, "DISPLAY") == 0)
d2bb6598
SM
1439 {
1440 Lisp_Object display
1441 = Fframe_parameter (NILP (frame) ? selected_frame : frame, Qdisplay);
1442 if (STRINGP (display))
1443 {
51b59d79 1444 *value = SSDATA (display);
de87fb59 1445 *valuelen = SBYTES (display);
012c6fcb
JA
1446 return 1;
1447 }
d2bb6598
SM
1448 /* If still not found, Look for DISPLAY in Vinitial_environment. */
1449 if (getenv_internal_1 (var, varlen, value, valuelen,
1450 Vinitial_environment))
1451 return *value ? 1 : 0;
012c6fcb
JA
1452 }
1453
1454 return 0;
1455}
1456
f105f403 1457DEFUN ("getenv-internal", Fgetenv_internal, Sgetenv_internal, 1, 2, 0,
5990851d
KL
1458 doc: /* Get the value of environment variable VARIABLE.
1459VARIABLE should be a string. Value is nil if VARIABLE is undefined in
1460the environment. Otherwise, value is a string.
f105f403 1461
acf20901 1462This function searches `process-environment' for VARIABLE.
5990851d 1463
db699fc6 1464If optional parameter ENV is a list, then search this list instead of
acf20901
JB
1465`process-environment', and return t when encountering a negative entry
1466\(an entry for a variable with no value). */)
5842a27b 1467 (Lisp_Object variable, Lisp_Object env)
012c6fcb
JA
1468{
1469 char *value;
989f33ba 1470 ptrdiff_t valuelen;
012c6fcb 1471
5990851d 1472 CHECK_STRING (variable);
db699fc6
SM
1473 if (CONSP (env))
1474 {
42a5b22f 1475 if (getenv_internal_1 (SSDATA (variable), SBYTES (variable),
db699fc6
SM
1476 &value, &valuelen, env))
1477 return value ? make_string (value, valuelen) : Qt;
1478 else
1479 return Qnil;
1480 }
42a5b22f 1481 else if (getenv_internal (SSDATA (variable), SBYTES (variable),
d2bb6598 1482 &value, &valuelen, env))
012c6fcb
JA
1483 return make_string (value, valuelen);
1484 else
1485 return Qnil;
1486}
1487
5990851d
KL
1488/* A version of getenv that consults the Lisp environment lists,
1489 easily callable from C. */
012c6fcb 1490char *
a8fe7202 1491egetenv (const char *var)
012c6fcb
JA
1492{
1493 char *value;
25c7e41f 1494 ptrdiff_t valuelen;
012c6fcb 1495
f105f403 1496 if (getenv_internal (var, strlen (var), &value, &valuelen, Qnil))
012c6fcb
JA
1497 return value;
1498 else
1499 return 0;
1500}
1501
80856e74 1502\f
8de15d69 1503/* This is run before init_cmdargs. */
177c0ea7 1504
dfcf069d 1505void
971de7fb 1506init_callproc_1 (void)
8de15d69 1507{
d01ba2f1
GM
1508#ifdef HAVE_NS
1509 const char *etc_dir = ns_etc_directory ();
cbb31951 1510 const char *path_exec = ns_exec_path ();
d01ba2f1 1511#endif
35a2f4b8 1512
76151e2c 1513 Vdata_directory = decode_env_path ("EMACSDATA",
d01ba2f1 1514#ifdef HAVE_NS
76151e2c 1515 etc_dir ? etc_dir :
d01ba2f1 1516#endif
76151e2c
EZ
1517 PATH_DATA);
1518 Vdata_directory = Ffile_name_as_directory (Fcar (Vdata_directory));
1519
1520 Vdoc_directory = decode_env_path ("EMACSDOC",
d01ba2f1 1521#ifdef HAVE_NS
76151e2c 1522 etc_dir ? etc_dir :
d01ba2f1 1523#endif
76151e2c
EZ
1524 PATH_DOC);
1525 Vdoc_directory = Ffile_name_as_directory (Fcar (Vdoc_directory));
9453ea7b 1526
e576cab4 1527 /* Check the EMACSPATH environment variable, defaulting to the
57bda87a 1528 PATH_EXEC path from epaths.h. */
cbb31951
GM
1529 Vexec_path = decode_env_path ("EMACSPATH",
1530#ifdef HAVE_NS
1531 path_exec ? path_exec :
1532#endif
1533 PATH_EXEC);
80856e74 1534 Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
cbb31951 1535 /* FIXME? For ns, path_exec should go at the front? */
80856e74 1536 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
8de15d69
RS
1537}
1538
e17f7533 1539/* This is run after init_cmdargs, when Vinstallation_directory is valid. */
8de15d69 1540
dfcf069d 1541void
971de7fb 1542init_callproc (void)
8de15d69
RS
1543{
1544 char *data_dir = egetenv ("EMACSDATA");
177c0ea7 1545
8de15d69
RS
1546 register char * sh;
1547 Lisp_Object tempdir;
d01ba2f1
GM
1548#ifdef HAVE_NS
1549 if (data_dir == 0)
1550 {
1551 const char *etc_dir = ns_etc_directory ();
1552 if (etc_dir)
1553 {
1554 data_dir = alloca (strlen (etc_dir) + 1);
1555 strcpy (data_dir, etc_dir);
1556 }
1557 }
1558#endif
8de15d69 1559
9cc4fad5 1560 if (!NILP (Vinstallation_directory))
8de15d69 1561 {
05630743
RS
1562 /* Add to the path the lib-src subdir of the installation dir. */
1563 Lisp_Object tem;
1564 tem = Fexpand_file_name (build_string ("lib-src"),
1565 Vinstallation_directory);
76151e2c 1566#ifndef MSDOS
1a6640ec 1567 /* MSDOS uses wrapped binaries, so don't do this. */
0fa248bc 1568 if (NILP (Fmember (tem, Vexec_path)))
70ec1377 1569 {
cbb31951
GM
1570#ifdef HAVE_NS
1571 const char *path_exec = ns_exec_path ();
1572#endif
1573 Vexec_path = decode_env_path ("EMACSPATH",
1574#ifdef HAVE_NS
1575 path_exec ? path_exec :
1576#endif
1577 PATH_EXEC);
70ec1377
RS
1578 Vexec_path = Fcons (tem, Vexec_path);
1579 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
1580 }
177c0ea7 1581
0fa248bc 1582 Vexec_directory = Ffile_name_as_directory (tem);
76151e2c 1583#endif /* not MSDOS */
8de15d69 1584
e17f7533
RS
1585 /* Maybe use ../etc as well as ../lib-src. */
1586 if (data_dir == 0)
1587 {
1588 tem = Fexpand_file_name (build_string ("etc"),
1589 Vinstallation_directory);
1590 Vdoc_directory = Ffile_name_as_directory (tem);
8de15d69
RS
1591 }
1592 }
7e933683
RS
1593
1594 /* Look for the files that should be in etc. We don't use
1595 Vinstallation_directory, because these files are never installed
e17f7533 1596 near the executable, and they are never in the build
7e933683
RS
1597 directory when that's different from the source directory.
1598
1599 Instead, if these files are not in the nominal place, we try the
1600 source directory. */
1601 if (data_dir == 0)
1602 {
70ec1377 1603 Lisp_Object tem, tem1, srcdir;
7e933683 1604
70ec1377
RS
1605 srcdir = Fexpand_file_name (build_string ("../src/"),
1606 build_string (PATH_DUMPLOADSEARCH));
7e933683
RS
1607 tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory);
1608 tem1 = Ffile_exists_p (tem);
70ec1377 1609 if (!NILP (Fequal (srcdir, Vinvocation_directory)) || NILP (tem1))
7e933683 1610 {
70ec1377 1611 Lisp_Object newdir;
7e933683
RS
1612 newdir = Fexpand_file_name (build_string ("../etc/"),
1613 build_string (PATH_DUMPLOADSEARCH));
1614 tem = Fexpand_file_name (build_string ("GNU"), newdir);
1615 tem1 = Ffile_exists_p (tem);
1616 if (!NILP (tem1))
1617 Vdata_directory = newdir;
1618 }
1619 }
80856e74 1620
d883eb62
RS
1621#ifndef CANNOT_DUMP
1622 if (initialized)
1623#endif
1624 {
1625 tempdir = Fdirectory_file_name (Vexec_directory);
73dcdb9f
PE
1626 if (! file_accessible_directory_p (SSDATA (tempdir)))
1627 dir_warning ("arch-dependent data dir", Vexec_directory);
d883eb62 1628 }
80856e74 1629
e576cab4 1630 tempdir = Fdirectory_file_name (Vdata_directory);
73dcdb9f
PE
1631 if (! file_accessible_directory_p (SSDATA (tempdir)))
1632 dir_warning ("arch-independent data dir", Vdata_directory);
e576cab4 1633
e576cab4 1634 sh = (char *) getenv ("SHELL");
80856e74 1635 Vshell_file_name = build_string (sh ? sh : "/bin/sh");
8abd035b 1636
40b49d4b
JB
1637#ifdef DOS_NT
1638 Vshared_game_score_directory = Qnil;
1639#else
63789758 1640 Vshared_game_score_directory = build_string (PATH_GAME);
73dcdb9f 1641 if (NILP (Ffile_accessible_directory_p (Vshared_game_score_directory)))
63789758 1642 Vshared_game_score_directory = Qnil;
40b49d4b 1643#endif
9fefd2ba
JB
1644}
1645
dfcf069d 1646void
971de7fb 1647set_initial_environment (void)
9fefd2ba 1648{
738db178
DN
1649 char **envp;
1650 for (envp = environ; *envp; envp++)
1651 Vprocess_environment = Fcons (build_string (*envp),
1652 Vprocess_environment);
1653 /* Ideally, the `copy' shouldn't be necessary, but it seems it's frequent
1654 to use `delete' and friends on process-environment. */
1655 Vinitial_environment = Fcopy_sequence (Vprocess_environment);
80856e74
JB
1656}
1657
dfcf069d 1658void
971de7fb 1659syms_of_callproc (void)
80856e74 1660{
f92d51d8
CY
1661#ifndef DOS_NT
1662 Vtemp_file_name_pattern = build_string ("emacsXXXXXX");
1663#elif defined (WINDOWSNT)
1664 Vtemp_file_name_pattern = build_string ("emXXXXXX");
1665#else
1666 Vtemp_file_name_pattern = build_string ("detmp.XXX");
1667#endif
1668 staticpro (&Vtemp_file_name_pattern);
1669
29208e82 1670 DEFVAR_LISP ("shell-file-name", Vshell_file_name,
fb7ada5f 1671 doc: /* File name to load inferior shells from.
b20b29be
EZ
1672Initialized from the SHELL environment variable, or to a system-dependent
1673default if SHELL is not set. */);
80856e74 1674
29208e82 1675 DEFVAR_LISP ("exec-path", Vexec_path,
fb7ada5f 1676 doc: /* List of directories to search programs to run in subprocesses.
fdb82f93 1677Each element is a string (directory name) or nil (try default directory). */);
80856e74 1678
29208e82 1679 DEFVAR_LISP ("exec-suffixes", Vexec_suffixes,
fb7ada5f 1680 doc: /* List of suffixes to try to find executable file names.
fdb82f93 1681Each element is a string. */);
33d5af99 1682 Vexec_suffixes = Qnil;
b81a1b72 1683
29208e82 1684 DEFVAR_LISP ("exec-directory", Vexec_directory,
fdb82f93
PJ
1685 doc: /* Directory for executables for Emacs to invoke.
1686More generally, this includes any architecture-dependent files
1687that are built and installed from the Emacs distribution. */);
e576cab4 1688
29208e82 1689 DEFVAR_LISP ("data-directory", Vdata_directory,
fdb82f93
PJ
1690 doc: /* Directory of machine-independent files that come with GNU Emacs.
1691These are files intended for Emacs to use while it runs. */);
80856e74 1692
29208e82 1693 DEFVAR_LISP ("doc-directory", Vdoc_directory,
fdb82f93 1694 doc: /* Directory containing the DOC file that comes with GNU Emacs.
ebf24b59 1695This is usually the same as `data-directory'. */);
35a2f4b8 1696
29208e82 1697 DEFVAR_LISP ("configure-info-directory", Vconfigure_info_directory,
fdb82f93
PJ
1698 doc: /* For internal use by the build procedure only.
1699This is the name of the directory in which the build procedure installed
ebf24b59 1700Emacs's info files; the default value for `Info-default-directory-list'
fdb82f93 1701includes this. */);
ed61592a
JB
1702 Vconfigure_info_directory = build_string (PATH_INFO);
1703
29208e82 1704 DEFVAR_LISP ("shared-game-score-directory", Vshared_game_score_directory,
b065672a
CW
1705 doc: /* Directory of score files for games which come with GNU Emacs.
1706If this variable is nil, then Emacs is unable to use a shared directory. */);
40b49d4b
JB
1707#ifdef DOS_NT
1708 Vshared_game_score_directory = Qnil;
1709#else
63789758 1710 Vshared_game_score_directory = build_string (PATH_GAME);
40b49d4b 1711#endif
b065672a 1712
29208e82 1713 DEFVAR_LISP ("initial-environment", Vinitial_environment,
6b8e474c
SM
1714 doc: /* List of environment variables inherited from the parent process.
1715Each element should be a string of the form ENVVARNAME=VALUE.
1716The elements must normally be decoded (using `locale-coding-system') for use. */);
1717 Vinitial_environment = Qnil;
1718
29208e82 1719 DEFVAR_LISP ("process-environment", Vprocess_environment,
5990851d 1720 doc: /* List of overridden environment variables for subprocesses to inherit.
fdb82f93 1721Each element should be a string of the form ENVVARNAME=VALUE.
5990851d 1722
a13f8f50
KL
1723Entries in this list take precedence to those in the frame-local
1724environments. Therefore, let-binding `process-environment' is an easy
1725way to temporarily change the value of an environment variable,
1726irrespective of where it comes from. To use `process-environment' to
1727remove an environment variable, include only its name in the list,
1728without "=VALUE".
5990851d
KL
1729
1730This variable is set to nil when Emacs starts.
1731
fdb82f93
PJ
1732If multiple entries define the same variable, the first one always
1733takes precedence.
5990851d 1734
776a24a1 1735Non-ASCII characters are encoded according to the initial value of
5990851d
KL
1736`locale-coding-system', i.e. the elements must normally be decoded for
1737use.
1738
776a24a1 1739See `setenv' and `getenv'. */);
86f5ca04 1740 Vprocess_environment = Qnil;
80856e74 1741
80856e74 1742 defsubr (&Scall_process);
83fa009c 1743 defsubr (&Sgetenv_internal);
e576cab4 1744 defsubr (&Scall_process_region);
80856e74 1745}