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