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