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