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