Initialize Vprocess_environment to nil.
[bpt/emacs.git] / src / callproc.c
CommitLineData
80856e74 1/* Synchronous subprocess invocation for GNU Emacs.
fe12847a 2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
0b5538bd 3 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
80856e74
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
826c56ac 9the Free Software Foundation; either version 2, or (at your option)
80856e74
JB
10any later version.
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
18along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
19the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20Boston, MA 02110-1301, USA. */
80856e74
JB
21
22
68c45bf0 23#include <config.h>
80856e74 24#include <signal.h>
e576cab4 25#include <errno.h>
565620a5 26#include <stdio.h>
80856e74 27
03695ace 28#ifndef USE_CRT_DLL
426b37ae 29extern int errno;
03695ace 30#endif
426b37ae 31
80856e74
JB
32/* Define SIGCHLD as an alias for SIGCLD. */
33
34#if !defined (SIGCHLD) && defined (SIGCLD)
35#define SIGCHLD SIGCLD
36#endif /* SIGCLD */
37
38#include <sys/types.h>
88a64fef 39
3cbd6585
GM
40#ifdef HAVE_UNISTD_H
41#include <unistd.h>
42#endif
43
80856e74 44#include <sys/file.h>
776a24a1 45#ifdef HAVE_FCNTL_H
472e83fe 46#define INCLUDED_FCNTL
80856e74
JB
47#include <fcntl.h>
48#endif
49
bad95d8f
RS
50#ifdef WINDOWSNT
51#define NOMINMAX
52#include <windows.h>
53#include <stdlib.h> /* for proper declaration of environ */
54#include <fcntl.h>
489f9371 55#include "w32.h"
bad95d8f
RS
56#define _P_NOWAIT 1 /* from process.h */
57#endif
58
7e6c2178 59#ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
472e83fe 60#define INCLUDED_FCNTL
7e6c2178
RS
61#include <fcntl.h>
62#include <sys/stat.h>
63#include <sys/param.h>
64#include <errno.h>
65#endif /* MSDOS */
66
80856e74
JB
67#ifndef O_RDONLY
68#define O_RDONLY 0
69#endif
70
71#ifndef O_WRONLY
72#define O_WRONLY 1
73#endif
74
75#include "lisp.h"
76#include "commands.h"
77#include "buffer.h"
32d08644 78#include "charset.h"
edf496dd 79#include "ccl.h"
32d08644 80#include "coding.h"
f0b950cf 81#include "composite.h"
57bda87a 82#include <epaths.h>
80856e74 83#include "process.h"
d177f194 84#include "syssignal.h"
a129418f 85#include "systty.h"
aba637ec 86#include "blockinput.h"
f105f403
KL
87#include "frame.h"
88#include "termhooks.h"
80856e74 89
5f027cea
EZ
90#ifdef MSDOS
91#include "msdos.h"
92#endif
93
80856e74
JB
94#ifdef VMS
95extern noshare char **environ;
96#else
03695ace 97#ifndef USE_CRT_DLL
80856e74
JB
98extern char **environ;
99#endif
03695ace 100#endif
80856e74 101
f95c3f91 102#ifdef HAVE_SETPGID
2b7e8799 103#if !defined (USG) || defined (BSD_PGRPS)
320695d8 104#undef setpgrp
f95c3f91
GM
105#define setpgrp setpgid
106#endif
2b7e8799 107#endif
f95c3f91 108
b81a1b72
SM
109Lisp_Object Vexec_path, Vexec_directory, Vexec_suffixes;
110Lisp_Object Vdata_directory, Vdoc_directory;
1e7ce61b 111Lisp_Object Vconfigure_info_directory, Vshared_game_score_directory;
8abd035b 112Lisp_Object Vtemp_file_name_pattern;
80856e74
JB
113
114Lisp_Object Vshell_file_name;
115
5990851d 116Lisp_Object Vglobal_environment;
80856e74 117Lisp_Object Vprocess_environment;
80856e74 118
bad95d8f 119#ifdef DOS_NT
093650fe 120Lisp_Object Qbuffer_file_type;
bad95d8f 121#endif /* DOS_NT */
f105f403 122Lisp_Object Qenvironment;
093650fe 123
80856e74
JB
124/* True iff we are about to fork off a synchronous process or if we
125 are waiting for it. */
126int synch_process_alive;
127
128/* Nonzero => this is a string explaining death of synchronous subprocess. */
129char *synch_process_death;
130
ca498128
JD
131/* Nonzero => this is the signal number that terminated the subprocess. */
132int synch_process_termsig;
133
80856e74
JB
134/* If synch_process_death is zero,
135 this is exit code of synchronous subprocess. */
136int synch_process_retcode;
f105f403
KL
137
138/* List of environment variables to look up in emacsclient. */
139Lisp_Object Vlocal_environment_variables;
140
80856e74 141\f
37d54121
RS
142/* Clean up when exiting Fcall_process.
143 On MSDOS, delete the temporary file on any kind of termination.
144 On Unix, kill the process and any children on termination by signal. */
145
146/* Nonzero if this is termination due to exit. */
147static int call_process_exited;
148
80856e74
JB
149#ifndef VMS /* VMS version is in vmsproc.c. */
150
d177f194
JB
151static Lisp_Object
152call_process_kill (fdpid)
153 Lisp_Object fdpid;
154{
68c45bf0 155 emacs_close (XFASTINT (Fcar (fdpid)));
d177f194
JB
156 EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
157 synch_process_alive = 0;
158 return Qnil;
159}
160
80856e74
JB
161Lisp_Object
162call_process_cleanup (fdpid)
163 Lisp_Object fdpid;
164{
e0f712ba 165#if defined (MSDOS) || defined (MAC_OS8)
7e6c2178 166 /* for MSDOS fdpid is really (fd . tempfile) */
c1350752
KH
167 register Lisp_Object file;
168 file = Fcdr (fdpid);
68c45bf0 169 emacs_close (XFASTINT (Fcar (fdpid)));
d5db4077
KR
170 if (strcmp (SDATA (file), NULL_DEVICE) != 0)
171 unlink (SDATA (file));
e0f712ba 172#else /* not MSDOS and not MAC_OS8 */
d177f194
JB
173 register int pid = XFASTINT (Fcdr (fdpid));
174
37d54121 175 if (call_process_exited)
6b6e798b 176 {
68c45bf0 177 emacs_close (XFASTINT (Fcar (fdpid)));
6b6e798b
RS
178 return Qnil;
179 }
37d54121 180
d177f194
JB
181 if (EMACS_KILLPG (pid, SIGINT) == 0)
182 {
aed13378 183 int count = SPECPDL_INDEX ();
d177f194
JB
184 record_unwind_protect (call_process_kill, fdpid);
185 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
186 immediate_quit = 1;
187 QUIT;
188 wait_for_termination (pid);
189 immediate_quit = 0;
190 specpdl_ptr = specpdl + count; /* Discard the unwind protect. */
191 message1 ("Waiting for process to die...done");
192 }
80856e74 193 synch_process_alive = 0;
68c45bf0 194 emacs_close (XFASTINT (Fcar (fdpid)));
7e6c2178 195#endif /* not MSDOS */
80856e74
JB
196 return Qnil;
197}
198
199DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
fdb82f93
PJ
200 doc: /* Call PROGRAM synchronously in separate process.
201The remaining arguments are optional.
202The program's input comes from file INFILE (nil means `/dev/null').
203Insert output in BUFFER before point; t means current buffer;
204 nil for BUFFER means discard it; 0 means discard and don't wait.
205BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
206REAL-BUFFER says what to do with standard output, as above,
207while STDERR-FILE says what to do with standard error in the child.
208STDERR-FILE may be nil (discard standard error output),
209t (mix it with ordinary output), or a file name string.
210
211Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
212Remaining arguments are strings passed as command arguments to PROGRAM.
213
214If BUFFER is 0, `call-process' returns immediately with value nil.
215Otherwise it waits for PROGRAM to terminate
216and returns a numeric exit status or a signal description string.
d98b59b5
MB
217If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
218
219usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */)
fdb82f93 220 (nargs, args)
80856e74
JB
221 int nargs;
222 register Lisp_Object *args;
223{
0aa2630f
KS
224 Lisp_Object infile, buffer, current_dir, path;
225 int display_p;
80856e74
JB
226 int fd[2];
227 int filefd;
228 register int pid;
4da256b1
KS
229#define CALLPROC_BUFFER_SIZE_MIN (16 * 1024)
230#define CALLPROC_BUFFER_SIZE_MAX (4 * CALLPROC_BUFFER_SIZE_MIN)
231 char buf[CALLPROC_BUFFER_SIZE_MAX];
232 int bufsize = CALLPROC_BUFFER_SIZE_MIN;
aed13378 233 int count = SPECPDL_INDEX ();
2d607244 234
4d3b07fd
KR
235 register const unsigned char **new_argv
236 = (const unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
80856e74 237 struct buffer *old = current_buffer;
39eaa782
RS
238 /* File to use for stderr in the child.
239 t means use same as standard output. */
240 Lisp_Object error_file;
7e6c2178
RS
241#ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
242 char *outf, *tempfile;
243 int outfilefd;
244#endif
e0f712ba 245#ifdef MAC_OS8
052062e0
RS
246 char *tempfile;
247 int outfilefd;
248#endif
80856e74
JB
249#if 0
250 int mask;
251#endif
32d08644
KH
252 struct coding_system process_coding; /* coding-system of process output */
253 struct coding_system argument_coding; /* coding-system of arguments */
09494912
RS
254 /* Set to the return value of Ffind_operation_coding_system. */
255 Lisp_Object coding_systems;
256
257 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
258 coding_systems = Qt;
32d08644 259
b7826503 260 CHECK_STRING (args[0]);
80856e74 261
39eaa782
RS
262 error_file = Qt;
263
7e6c2178
RS
264#ifndef subprocesses
265 /* Without asynchronous processes we cannot have BUFFER == 0. */
177c0ea7 266 if (nargs >= 3
09ffb8b5 267 && (INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2])))
7e6c2178
RS
268 error ("Operating system cannot handle asynchronous subprocesses");
269#endif /* subprocesses */
270
09494912 271 /* Decide the coding-system for giving arguments. */
32d08644
KH
272 {
273 Lisp_Object val, *args2;
32d08644
KH
274 int i;
275
276 /* If arguments are supplied, we may have to encode them. */
277 if (nargs >= 5)
278 {
30d57b8e
RS
279 int must_encode = 0;
280
e7c1c20e 281 for (i = 4; i < nargs; i++)
b7826503 282 CHECK_STRING (args[i]);
e7c1c20e 283
a2286b5c 284 for (i = 4; i < nargs; i++)
30d57b8e
RS
285 if (STRING_MULTIBYTE (args[i]))
286 must_encode = 1;
287
beacaab3
KH
288 if (!NILP (Vcoding_system_for_write))
289 val = Vcoding_system_for_write;
30d57b8e 290 else if (! must_encode)
beacaab3
KH
291 val = Qnil;
292 else
32d08644
KH
293 {
294 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
295 args2[0] = Qcall_process;
296 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
08ee4e87 297 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
776b95cb 298 if (CONSP (coding_systems))
70949dac 299 val = XCDR (coding_systems);
776b95cb 300 else if (CONSP (Vdefault_process_coding_system))
70949dac 301 val = XCDR (Vdefault_process_coding_system);
beacaab3
KH
302 else
303 val = Qnil;
32d08644
KH
304 }
305 setup_coding_system (Fcheck_coding_system (val), &argument_coding);
306 }
32d08644
KH
307 }
308
e576cab4
JB
309 if (nargs >= 2 && ! NILP (args[1]))
310 {
311 infile = Fexpand_file_name (args[1], current_buffer->directory);
b7826503 312 CHECK_STRING (infile);
e576cab4 313 }
80856e74 314 else
5437e9f9 315 infile = build_string (NULL_DEVICE);
80856e74 316
e576cab4
JB
317 if (nargs >= 3)
318 {
39eaa782
RS
319 buffer = args[2];
320
321 /* If BUFFER is a list, its meaning is
322 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
323 if (CONSP (buffer))
324 {
70949dac 325 if (CONSP (XCDR (buffer)))
45be8a1e 326 {
a9d4f28a 327 Lisp_Object stderr_file;
70949dac 328 stderr_file = XCAR (XCDR (buffer));
45be8a1e
RS
329
330 if (NILP (stderr_file) || EQ (Qt, stderr_file))
331 error_file = stderr_file;
332 else
333 error_file = Fexpand_file_name (stderr_file, Qnil);
334 }
335
70949dac 336 buffer = XCAR (buffer);
39eaa782 337 }
044512ed 338
39eaa782
RS
339 if (!(EQ (buffer, Qnil)
340 || EQ (buffer, Qt)
3ffde7d6 341 || INTEGERP (buffer)))
e576cab4 342 {
39eaa782
RS
343 Lisp_Object spec_buffer;
344 spec_buffer = buffer;
50fe359b 345 buffer = Fget_buffer_create (buffer);
39eaa782
RS
346 /* Mention the buffer name for a better error message. */
347 if (NILP (buffer))
b7826503
PJ
348 CHECK_BUFFER (spec_buffer);
349 CHECK_BUFFER (buffer);
e576cab4
JB
350 }
351 }
177c0ea7 352 else
e576cab4 353 buffer = Qnil;
80856e74 354
58616e67
JB
355 /* Make sure that the child will be able to chdir to the current
356 buffer's current directory, or its unhandled equivalent. We
357 can't just have the child check for an error when it does the
358 chdir, since it's in a vfork.
359
360 We have to GCPRO around this because Fexpand_file_name,
361 Funhandled_file_name_directory, and Ffile_accessible_directory_p
362 might call a file name handling function. The argument list is
363 protected by the caller, so all we really have to worry about is
364 buffer. */
365 {
366 struct gcpro gcpro1, gcpro2, gcpro3;
367
368 current_dir = current_buffer->directory;
369
370 GCPRO3 (infile, buffer, current_dir);
371
c52b0b34
KH
372 current_dir
373 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
374 Qnil);
58616e67
JB
375 if (NILP (Ffile_accessible_directory_p (current_dir)))
376 report_file_error ("Setting current directory",
377 Fcons (current_buffer->directory, Qnil));
378
379 UNGCPRO;
380 }
381
0aa2630f 382 display_p = INTERACTIVE && nargs >= 4 && !NILP (args[3]);
80856e74 383
d5db4077 384 filefd = emacs_open (SDATA (infile), O_RDONLY, 0);
80856e74
JB
385 if (filefd < 0)
386 {
e576cab4 387 report_file_error ("Opening process input file", Fcons (infile, Qnil));
80856e74
JB
388 }
389 /* Search for program; barf if not found. */
c52b0b34
KH
390 {
391 struct gcpro gcpro1;
392
393 GCPRO1 (current_dir);
5c150961 394 openp (Vexec_path, args[0], Vexec_suffixes, &path, make_number (X_OK));
c52b0b34
KH
395 UNGCPRO;
396 }
012c6fcb 397 if (NILP (path))
80856e74 398 {
68c45bf0 399 emacs_close (filefd);
80856e74
JB
400 report_file_error ("Searching for program", Fcons (args[0], Qnil));
401 }
8ee8f447
RS
402
403 /* If program file name starts with /: for quoting a magic name,
404 discard that. */
405 if (SBYTES (path) > 2 && SREF (path, 0) == '/'
406 && SREF (path, 1) == ':')
407 path = Fsubstring (path, make_number (2), Qnil);
408
d5db4077 409 new_argv[0] = SDATA (path);
c364e618
KH
410 if (nargs > 4)
411 {
412 register int i;
c5bfa12b 413 struct gcpro gcpro1, gcpro2, gcpro3;
c364e618 414
c5bfa12b
KH
415 GCPRO3 (infile, buffer, current_dir);
416 argument_coding.dst_multibyte = 0;
417 for (i = 4; i < nargs; i++)
c364e618 418 {
c5bfa12b
KH
419 argument_coding.src_multibyte = STRING_MULTIBYTE (args[i]);
420 if (CODING_REQUIRE_ENCODING (&argument_coding))
c364e618 421 {
c5bfa12b
KH
422 /* We must encode this argument. */
423 args[i] = encode_coding_string (args[i], &argument_coding, 1);
424 if (argument_coding.type == coding_type_ccl)
425 setup_ccl_program (&(argument_coding.spec.ccl.encoder), Qnil);
c364e618 426 }
d5db4077 427 new_argv[i - 3] = SDATA (args[i]);
c364e618 428 }
c5bfa12b 429 UNGCPRO;
db54baaa 430 new_argv[nargs - 3] = 0;
c364e618 431 }
db54baaa
KH
432 else
433 new_argv[1] = 0;
80856e74 434
7e6c2178 435#ifdef MSDOS /* MW, July 1993 */
8a52365c 436 if ((outf = egetenv ("TMPDIR")))
7e6c2178
RS
437 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
438 else
439 {
440 tempfile = alloca (20);
441 *tempfile = '\0';
442 }
443 dostounix_filename (tempfile);
177c0ea7 444 if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/')
7e6c2178
RS
445 strcat (tempfile, "/");
446 strcat (tempfile, "detmp.XXX");
447 mktemp (tempfile);
448
449 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
450 if (outfilefd < 0)
451 {
68c45bf0 452 emacs_close (filefd);
6f89d28a
MB
453 report_file_error ("Opening process output file",
454 Fcons (build_string (tempfile), Qnil));
7e6c2178 455 }
6f89d28a 456 fd[0] = filefd;
2610078a 457 fd[1] = outfilefd;
6f89d28a 458#endif /* MSDOS */
7e6c2178 459
e0f712ba 460#ifdef MAC_OS8
052062e0
RS
461 /* Since we don't have pipes on the Mac, create a temporary file to
462 hold the output of the subprocess. */
d5db4077
KR
463 tempfile = (char *) alloca (SBYTES (Vtemp_file_name_pattern) + 1);
464 bcopy (SDATA (Vtemp_file_name_pattern), tempfile,
465 SBYTES (Vtemp_file_name_pattern) + 1);
052062e0
RS
466
467 mktemp (tempfile);
468
469 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
470 if (outfilefd < 0)
471 {
472 close (filefd);
473 report_file_error ("Opening process output file",
474 Fcons (build_string (tempfile), Qnil));
475 }
476 fd[0] = filefd;
477 fd[1] = outfilefd;
e0f712ba 478#endif /* MAC_OS8 */
052062e0 479
d50d3dc8 480 if (INTEGERP (buffer))
68c45bf0 481 fd[1] = emacs_open (NULL_DEVICE, O_WRONLY, 0), fd[0] = -1;
80856e74
JB
482 else
483 {
7e6c2178 484#ifndef MSDOS
e0f712ba 485#ifndef MAC_OS8
db92b288
GM
486 errno = 0;
487 if (pipe (fd) == -1)
488 {
489 emacs_close (filefd);
490 report_file_error ("Creating process pipe", Qnil);
491 }
7e6c2178 492#endif
052062e0 493#endif
80856e74
JB
494#if 0
495 /* Replaced by close_process_descs */
496 set_exclusive_use (fd[0]);
497#endif
498 }
499
500 {
501 /* child_setup must clobber environ in systems with true vfork.
502 Protect it from permanent change. */
503 register char **save_environ = environ;
504 register int fd1 = fd[1];
39eaa782 505 int fd_error = fd1;
80856e74
JB
506
507#if 0 /* Some systems don't have sigblock. */
e065a56e 508 mask = sigblock (sigmask (SIGCHLD));
80856e74
JB
509#endif
510
511 /* Record that we're about to create a synchronous process. */
512 synch_process_alive = 1;
513
5c03767e
RS
514 /* These vars record information from process termination.
515 Clear them now before process can possibly terminate,
516 to avoid timing error if process terminates soon. */
517 synch_process_death = 0;
518 synch_process_retcode = 0;
ca498128 519 synch_process_termsig = 0;
5c03767e 520
39eaa782 521 if (NILP (error_file))
68c45bf0 522 fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0);
39eaa782
RS
523 else if (STRINGP (error_file))
524 {
525#ifdef DOS_NT
d5db4077 526 fd_error = emacs_open (SDATA (error_file),
68c45bf0
PE
527 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
528 S_IREAD | S_IWRITE);
39eaa782 529#else /* not DOS_NT */
d5db4077 530 fd_error = creat (SDATA (error_file), 0666);
39eaa782
RS
531#endif /* not DOS_NT */
532 }
533
534 if (fd_error < 0)
535 {
68c45bf0 536 emacs_close (filefd);
6f89d28a 537 if (fd[0] != filefd)
68c45bf0 538 emacs_close (fd[0]);
39eaa782 539 if (fd1 >= 0)
68c45bf0 540 emacs_close (fd1);
6f89d28a
MB
541#ifdef MSDOS
542 unlink (tempfile);
543#endif
544 report_file_error ("Cannot redirect stderr",
545 Fcons ((NILP (error_file)
546 ? build_string (NULL_DEVICE) : error_file),
547 Qnil));
39eaa782 548 }
89e1ec1d 549
8d024345 550 current_dir = ENCODE_FILE (current_dir);
89e1ec1d 551
e0f712ba 552#ifdef MAC_OS8
052062e0
RS
553 {
554 /* Call run_mac_command in sysdep.c here directly instead of doing
555 a child_setup as for MSDOS and other platforms. Note that this
556 code does not handle passing the environment to the synchronous
557 Mac subprocess. */
558 char *infn, *outfn, *errfn, *currdn;
177c0ea7 559
052062e0
RS
560 /* close these files so subprocess can write to them */
561 close (outfilefd);
562 if (fd_error != outfilefd)
563 close (fd_error);
564 fd1 = -1; /* No harm in closing that one! */
565
d5db4077 566 infn = SDATA (infile);
052062e0
RS
567 outfn = tempfile;
568 if (NILP (error_file))
569 errfn = NULL_DEVICE;
570 else if (EQ (Qt, error_file))
571 errfn = outfn;
572 else
d5db4077
KR
573 errfn = SDATA (error_file);
574 currdn = SDATA (current_dir);
052062e0
RS
575 pid = run_mac_command (new_argv, currdn, infn, outfn, errfn);
576
577 /* Record that the synchronous process exited and note its
578 termination status. */
579 synch_process_alive = 0;
580 synch_process_retcode = pid;
581 if (synch_process_retcode < 0) /* means it couldn't be exec'ed */
68c45bf0 582 {
ca9c0567 583 synchronize_system_messages_locale ();
68c45bf0
PE
584 synch_process_death = strerror (errno);
585 }
052062e0
RS
586
587 /* Since CRLF is converted to LF within `decode_coding', we can
588 always open a file with binary mode. */
589 fd[0] = open (tempfile, O_BINARY);
590 if (fd[0] < 0)
591 {
592 unlink (tempfile);
593 close (filefd);
594 report_file_error ("Cannot re-open temporary file", Qnil);
595 }
596 }
e0f712ba 597#else /* not MAC_OS8 */
2610078a 598#ifdef MSDOS /* MW, July 1993 */
c17c4250 599 /* Note that on MSDOS `child_setup' actually returns the child process
2610078a
KH
600 exit status, not its PID, so we assign it to `synch_process_retcode'
601 below. */
c17c4250
EZ
602 pid = child_setup (filefd, outfilefd, fd_error, (char **) new_argv,
603 0, current_dir);
39eaa782 604
2610078a
KH
605 /* Record that the synchronous process exited and note its
606 termination status. */
607 synch_process_alive = 0;
608 synch_process_retcode = pid;
609 if (synch_process_retcode < 0) /* means it couldn't be exec'ed */
68c45bf0 610 {
ca9c0567 611 synchronize_system_messages_locale ();
68c45bf0
PE
612 synch_process_death = strerror (errno);
613 }
2610078a 614
68c45bf0 615 emacs_close (outfilefd);
2610078a 616 if (fd_error != outfilefd)
68c45bf0 617 emacs_close (fd_error);
2610078a 618 fd1 = -1; /* No harm in closing that one! */
32d08644
KH
619 /* Since CRLF is converted to LF within `decode_coding', we can
620 always open a file with binary mode. */
68c45bf0 621 fd[0] = emacs_open (tempfile, O_RDONLY | O_BINARY, 0);
2610078a
KH
622 if (fd[0] < 0)
623 {
624 unlink (tempfile);
68c45bf0 625 emacs_close (filefd);
2610078a
KH
626 report_file_error ("Cannot re-open temporary file", Qnil);
627 }
628#else /* not MSDOS */
bad95d8f 629#ifdef WINDOWSNT
2d607244
RS
630 pid = child_setup (filefd, fd1, fd_error, (char **) new_argv,
631 0, current_dir);
bad95d8f 632#else /* not WINDOWSNT */
aba637ec
KS
633 BLOCK_INPUT;
634
80856e74
JB
635 pid = vfork ();
636
637 if (pid == 0)
638 {
639 if (fd[0] >= 0)
68c45bf0 640 emacs_close (fd[0]);
1e7963c7
RS
641#ifdef HAVE_SETSID
642 setsid ();
643#endif
644#if defined (USG) && !defined (BSD_PGRPS)
80856e74
JB
645 setpgrp ();
646#else
647 setpgrp (pid, pid);
648#endif /* USG */
2d607244
RS
649 child_setup (filefd, fd1, fd_error, (char **) new_argv,
650 0, current_dir);
80856e74 651 }
aba637ec
KS
652
653 UNBLOCK_INPUT;
bad95d8f 654#endif /* not WINDOWSNT */
cd5f8f60
RS
655
656 /* The MSDOS case did this already. */
657 if (fd_error >= 0)
68c45bf0 658 emacs_close (fd_error);
2610078a 659#endif /* not MSDOS */
e0f712ba 660#endif /* not MAC_OS8 */
80856e74 661
80856e74
JB
662 environ = save_environ;
663
6b6e798b
RS
664 /* Close most of our fd's, but not fd[0]
665 since we will use that to read input from. */
68c45bf0 666 emacs_close (filefd);
799abb26 667 if (fd1 >= 0 && fd1 != fd_error)
68c45bf0 668 emacs_close (fd1);
80856e74
JB
669 }
670
671 if (pid < 0)
672 {
6b6e798b 673 if (fd[0] >= 0)
68c45bf0 674 emacs_close (fd[0]);
80856e74
JB
675 report_file_error ("Doing vfork", Qnil);
676 }
677
d50d3dc8 678 if (INTEGERP (buffer))
80856e74 679 {
6b6e798b 680 if (fd[0] >= 0)
68c45bf0 681 emacs_close (fd[0]);
80856e74 682#ifndef subprocesses
e576cab4
JB
683 /* If Emacs has been built with asynchronous subprocess support,
684 we don't need to do this, I think because it will then have
685 the facilities for handling SIGCHLD. */
80856e74
JB
686 wait_without_blocking ();
687#endif /* subprocesses */
80856e74
JB
688 return Qnil;
689 }
690
6b6e798b 691 /* Enable sending signal if user quits below. */
37d54121
RS
692 call_process_exited = 0;
693
e0f712ba 694#if defined(MSDOS) || defined(MAC_OS8)
7e6c2178
RS
695 /* MSDOS needs different cleanup information. */
696 record_unwind_protect (call_process_cleanup,
697 Fcons (make_number (fd[0]), build_string (tempfile)));
698#else
80856e74
JB
699 record_unwind_protect (call_process_cleanup,
700 Fcons (make_number (fd[0]), make_number (pid)));
e0f712ba 701#endif /* not MSDOS and not MAC_OS8 */
80856e74
JB
702
703
d50d3dc8 704 if (BUFFERP (buffer))
80856e74
JB
705 Fset_buffer (buffer);
706
09494912
RS
707 if (NILP (buffer))
708 {
709 /* If BUFFER is nil, we must read process output once and then
710 discard it, so setup coding system but with nil. */
711 setup_coding_system (Qnil, &process_coding);
712 }
713 else
714 {
715 Lisp_Object val, *args2;
716
717 val = Qnil;
718 if (!NILP (Vcoding_system_for_read))
719 val = Vcoding_system_for_read;
720 else
721 {
722 if (EQ (coding_systems, Qt))
723 {
724 int i;
725
726 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
727 args2[0] = Qcall_process;
728 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
729 coding_systems
730 = Ffind_operation_coding_system (nargs + 1, args2);
731 }
732 if (CONSP (coding_systems))
70949dac 733 val = XCAR (coding_systems);
09494912 734 else if (CONSP (Vdefault_process_coding_system))
70949dac 735 val = XCAR (Vdefault_process_coding_system);
09494912
RS
736 else
737 val = Qnil;
738 }
739 setup_coding_system (Fcheck_coding_system (val), &process_coding);
740 /* In unibyte mode, character code conversion should not take
741 place but EOL conversion should. So, setup raw-text or one
742 of the subsidiary according to the information just setup. */
743 if (NILP (current_buffer->enable_multibyte_characters)
744 && !NILP (val))
745 setup_raw_text_coding_system (&process_coding);
746 }
c5bfa12b
KH
747 process_coding.src_multibyte = 0;
748 process_coding.dst_multibyte
749 = (BUFFERP (buffer)
750 ? ! NILP (XBUFFER (buffer)->enable_multibyte_characters)
751 : ! NILP (current_buffer->enable_multibyte_characters));
09494912 752
80856e74
JB
753 immediate_quit = 1;
754 QUIT;
755
756 {
757 register int nread;
0ad477db 758 int first = 1;
6e3bfbb2 759 int total_read = 0;
321fecde 760 int carryover = 0;
0aa2630f 761 int display_on_the_fly = display_p;
05b44e90 762 struct coding_system saved_coding;
f778b157
KH
763 int pt_orig = PT, pt_byte_orig = PT_BYTE;
764 int inserted;
05b44e90
KH
765
766 saved_coding = process_coding;
f0b950cf
KH
767 if (process_coding.composing != COMPOSITION_DISABLED)
768 coding_allocate_composition_data (&process_coding, PT);
60558b19 769 while (1)
80856e74 770 {
60558b19
RS
771 /* Repeatedly read until we've filled as much as possible
772 of the buffer size we have. But don't read
8e6208c5 773 less than 1024--save that for the next bufferful. */
321fecde 774 nread = carryover;
60558b19 775 while (nread < bufsize - 1024)
00fb3e95 776 {
4da256b1 777 int this_read = emacs_read (fd[0], buf + nread,
68c45bf0 778 bufsize - nread);
60558b19
RS
779
780 if (this_read < 0)
781 goto give_up;
782
783 if (this_read == 0)
7a7ab107
KH
784 {
785 process_coding.mode |= CODING_MODE_LAST_BLOCK;
786 break;
787 }
60558b19
RS
788
789 nread += this_read;
7a7ab107 790 total_read += this_read;
60558b19 791
7a7ab107
KH
792 if (display_on_the_fly)
793 break;
794 }
60558b19
RS
795
796 /* Now NREAD is the total amount of data in the buffer. */
80856e74 797 immediate_quit = 0;
177c0ea7 798
012c6fcb 799 if (!NILP (buffer))
32d08644 800 {
c5bfa12b 801 if (! CODING_MAY_REQUIRE_DECODING (&process_coding))
4da256b1 802 insert_1_both (buf, nread, nread, 0, 1, 0);
32d08644
KH
803 else
804 { /* We have to decode the input. */
f0b950cf
KH
805 int size;
806 char *decoding_buf;
32d08644 807
f0b950cf
KH
808 repeat_decoding:
809 size = decoding_buffer_size (&process_coding, nread);
810 decoding_buf = (char *) xmalloc (size);
177c0ea7 811
b252f801
KH
812 /* We can't use the macro CODING_REQUIRE_DETECTION
813 because it always returns nonzero if the coding
814 system requires EOL detection. Here, we have to
815 check only whether or not the coding system
816 requires text-encoding detection. */
817 if (process_coding.type == coding_type_undecided)
950a45d0 818 {
4da256b1 819 detect_coding (&process_coding, buf, nread);
950a45d0 820 if (process_coding.composing != COMPOSITION_DISABLED)
07c381ad
KH
821 /* We have not yet allocated the composition
822 data because the coding type was undecided. */
950a45d0
KH
823 coding_allocate_composition_data (&process_coding, PT);
824 }
2d892150
KH
825 if (process_coding.cmp_data)
826 process_coding.cmp_data->char_offset = PT;
177c0ea7 827
4da256b1 828 decode_coding (&process_coding, buf, decoding_buf,
321fecde 829 nread, size);
177c0ea7 830
7a7ab107
KH
831 if (display_on_the_fly
832 && saved_coding.type == coding_type_undecided
833 && process_coding.type != coding_type_undecided)
834 {
835 /* We have detected some coding system. But,
836 there's a possibility that the detection was
0aa2630f
KS
837 done by insufficient data. So, we try the code
838 detection again with more data. */
a871dd58 839 xfree (decoding_buf);
7a7ab107
KH
840 display_on_the_fly = 0;
841 process_coding = saved_coding;
842 carryover = nread;
0aa2630f
KS
843 /* This is to make the above condition always
844 fails in the future. */
845 saved_coding.type = coding_type_no_conversion;
7a7ab107
KH
846 continue;
847 }
177c0ea7 848
321fecde 849 if (process_coding.produced > 0)
2d892150
KH
850 insert_1_both (decoding_buf, process_coding.produced_char,
851 process_coding.produced, 0, 1, 0);
a871dd58 852 xfree (decoding_buf);
7bdba03c
GM
853
854 if (process_coding.result == CODING_FINISH_INCONSISTENT_EOL)
855 {
856 Lisp_Object eol_type, coding;
857
858 if (process_coding.eol_type == CODING_EOL_CR)
859 {
860 /* CRs have been replaced with LFs. Undo
861 that in the text inserted above. */
862 unsigned char *p;
177c0ea7 863
7bdba03c 864 move_gap_both (PT, PT_BYTE);
177c0ea7 865
7bdba03c
GM
866 p = BYTE_POS_ADDR (pt_byte_orig);
867 for (; p < GPT_ADDR; ++p)
868 if (*p == '\n')
869 *p = '\r';
870 }
871 else if (process_coding.eol_type == CODING_EOL_CRLF)
872 {
873 /* CR LFs have been replaced with LFs. Undo
874 that by inserting CRs in front of LFs in
875 the text inserted above. */
876 EMACS_INT bytepos, old_pt, old_pt_byte, nCR;
877
878 old_pt = PT;
879 old_pt_byte = PT_BYTE;
880 nCR = 0;
177c0ea7 881
7bdba03c
GM
882 for (bytepos = PT_BYTE - 1;
883 bytepos >= pt_byte_orig;
884 --bytepos)
885 if (FETCH_BYTE (bytepos) == '\n')
886 {
887 EMACS_INT charpos = BYTE_TO_CHAR (bytepos);
888 TEMP_SET_PT_BOTH (charpos, bytepos);
889 insert_1_both ("\r", 1, 1, 0, 1, 0);
890 ++nCR;
891 }
892
893 TEMP_SET_PT_BOTH (old_pt + nCR, old_pt_byte + nCR);
894 }
895
896 /* Set the coding system symbol to that for
897 Unix-like EOL. */
898 eol_type = Fget (saved_coding.symbol, Qeol_type);
899 if (VECTORP (eol_type)
900 && ASIZE (eol_type) == 3
901 && SYMBOLP (AREF (eol_type, CODING_EOL_LF)))
902 coding = AREF (eol_type, CODING_EOL_LF);
903 else
904 coding = saved_coding.symbol;
177c0ea7 905
7bdba03c
GM
906 process_coding.symbol = coding;
907 process_coding.eol_type = CODING_EOL_LF;
908 process_coding.mode
909 &= ~CODING_MODE_INHIBIT_INCONSISTENT_EOL;
910 }
177c0ea7 911
f0b950cf
KH
912 nread -= process_coding.consumed;
913 carryover = nread;
321fecde 914 if (carryover > 0)
c5bfa12b
KH
915 /* As CARRYOVER should not be that large, we had
916 better avoid overhead of bcopy. */
4da256b1 917 BCOPY_SHORT (buf + process_coding.consumed, buf,
c5bfa12b 918 carryover);
f0b950cf
KH
919 if (process_coding.result == CODING_FINISH_INSUFFICIENT_CMP)
920 {
921 /* The decoding ended because of insufficient data
922 area to record information about composition.
923 We must try decoding with additional data area
2d892150 924 before reading more output for the process. */
f0b950cf
KH
925 coding_allocate_composition_data (&process_coding, PT);
926 goto repeat_decoding;
927 }
32d08644
KH
928 }
929 }
c5bfa12b 930
321fecde 931 if (process_coding.mode & CODING_MODE_LAST_BLOCK)
c5bfa12b 932 break;
6e3bfbb2 933
4da256b1 934#if (CALLPROC_BUFFER_SIZE_MIN != CALLPROC_BUFFER_SIZE_MAX)
6e3bfbb2 935 /* Make the buffer bigger as we continue to read more data,
4da256b1
KS
936 but not past CALLPROC_BUFFER_SIZE_MAX. */
937 if (bufsize < CALLPROC_BUFFER_SIZE_MAX && total_read > 32 * bufsize)
938 if ((bufsize *= 2) > CALLPROC_BUFFER_SIZE_MAX)
939 bufsize = CALLPROC_BUFFER_SIZE_MAX;
940#endif
6e3bfbb2 941
0aa2630f 942 if (display_p)
0ad477db
RS
943 {
944 if (first)
945 prepare_menu_bars ();
946 first = 0;
3007ebfb 947 redisplay_preserve_echo_area (1);
0aa2630f
KS
948 /* This variable might have been set to 0 for code
949 detection. In that case, we set it back to 1 because
950 we should have already detected a coding system. */
951 display_on_the_fly = 1;
0ad477db 952 }
80856e74
JB
953 immediate_quit = 1;
954 QUIT;
955 }
60558b19 956 give_up: ;
80856e74 957
2d892150
KH
958 if (!NILP (buffer)
959 && process_coding.cmp_data)
960 {
961 coding_restore_composition (&process_coding, Fcurrent_buffer ());
962 coding_free_composition_data (&process_coding);
963 }
f0b950cf 964
838c9726 965 {
aed13378 966 int post_read_count = SPECPDL_INDEX ();
838c9726
KH
967
968 record_unwind_protect (save_excursion_restore, save_excursion_save ());
969 inserted = PT - pt_orig;
970 TEMP_SET_PT_BOTH (pt_orig, pt_byte_orig);
971 if (SYMBOLP (process_coding.post_read_conversion)
972 && !NILP (Ffboundp (process_coding.post_read_conversion)))
973 call1 (process_coding.post_read_conversion, make_number (inserted));
f778b157 974
838c9726 975 Vlast_coding_system_used = process_coding.symbol;
bbd29cfe 976
838c9726
KH
977 /* If the caller required, let the buffer inherit the
978 coding-system used to decode the process output. */
979 if (inherit_process_coding_system)
980 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
981 make_number (total_read));
982
983 unbind_to (post_read_count, Qnil);
984 }
3b440bb5
EZ
985 }
986
80856e74
JB
987 /* Wait for it to terminate, unless it already has. */
988 wait_for_termination (pid);
989
990 immediate_quit = 0;
991
992 set_buffer_internal (old);
993
37d54121
RS
994 /* Don't kill any children that the subprocess may have left behind
995 when exiting. */
996 call_process_exited = 1;
997
80856e74
JB
998 unbind_to (count, Qnil);
999
ca498128
JD
1000 if (synch_process_termsig)
1001 {
1002 char *signame;
1003
1004 synchronize_system_messages_locale ();
1005 signame = strsignal (synch_process_termsig);
1006
1007 if (signame == 0)
1008 signame = "unknown";
1009
1010 synch_process_death = signame;
1011 }
1012
80856e74 1013 if (synch_process_death)
68c45bf0
PE
1014 return code_convert_string_norecord (build_string (synch_process_death),
1015 Vlocale_coding_system, 0);
80856e74
JB
1016 return make_number (synch_process_retcode);
1017}
1018#endif
1019\f
9fefd2ba 1020static Lisp_Object
80856e74
JB
1021delete_temp_file (name)
1022 Lisp_Object name;
1023{
2e3dc201 1024 /* Use Fdelete_file (indirectly) because that runs a file name handler.
59750d69 1025 We did that when writing the file, so we should do so when deleting. */
2e3dc201 1026 internal_delete_file (name);
320695d8 1027 return Qnil;
80856e74
JB
1028}
1029
1030DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
fdb82f93
PJ
1031 3, MANY, 0,
1032 doc: /* Send text from START to END to a synchronous process running PROGRAM.
1033The remaining arguments are optional.
1034Delete the text if fourth arg DELETE is non-nil.
1035
1036Insert output in BUFFER before point; t means current buffer;
1037 nil for BUFFER means discard it; 0 means discard and don't wait.
1038BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
1039REAL-BUFFER says what to do with standard output, as above,
1040while STDERR-FILE says what to do with standard error in the child.
1041STDERR-FILE may be nil (discard standard error output),
1042t (mix it with ordinary output), or a file name string.
1043
1044Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.
1045Remaining args are passed to PROGRAM at startup as command args.
1046
ba9a5174 1047If BUFFER is 0, `call-process-region' returns immediately with value nil.
fdb82f93
PJ
1048Otherwise it waits for PROGRAM to terminate
1049and returns a numeric exit status or a signal description string.
d98b59b5
MB
1050If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
1051
1052usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */)
fdb82f93 1053 (nargs, args)
80856e74
JB
1054 int nargs;
1055 register Lisp_Object *args;
1056{
39323a7e
KH
1057 struct gcpro gcpro1;
1058 Lisp_Object filename_string;
1059 register Lisp_Object start, end;
aed13378 1060 int count = SPECPDL_INDEX ();
08ee4e87 1061 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
09494912 1062 Lisp_Object coding_systems;
32d08644
KH
1063 Lisp_Object val, *args2;
1064 int i;
bad95d8f 1065#ifdef DOS_NT
7e6c2178 1066 char *tempfile;
7e6c2178
RS
1067 char *outf = '\0';
1068
8a52365c
EZ
1069 if ((outf = egetenv ("TMPDIR"))
1070 || (outf = egetenv ("TMP"))
1071 || (outf = egetenv ("TEMP")))
7e6c2178
RS
1072 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
1073 else
1074 {
1075 tempfile = alloca (20);
1076 *tempfile = '\0';
1077 }
0774fcf8 1078 if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1]))
7e6c2178 1079 strcat (tempfile, "/");
5711b547
RS
1080 if ('/' == DIRECTORY_SEP)
1081 dostounix_filename (tempfile);
1082 else
1083 unixtodos_filename (tempfile);
0774fcf8
RS
1084#ifdef WINDOWSNT
1085 strcat (tempfile, "emXXXXXX");
1086#else
7e6c2178 1087 strcat (tempfile, "detmp.XXX");
0774fcf8 1088#endif
bad95d8f 1089#else /* not DOS_NT */
d5db4077
KR
1090 char *tempfile = (char *) alloca (SBYTES (Vtemp_file_name_pattern) + 1);
1091 bcopy (SDATA (Vtemp_file_name_pattern), tempfile,
1092 SBYTES (Vtemp_file_name_pattern) + 1);
bad95d8f 1093#endif /* not DOS_NT */
7e6c2178 1094
09494912
RS
1095 coding_systems = Qt;
1096
1ddc85a4
DL
1097#ifdef HAVE_MKSTEMP
1098 {
1099 int fd = mkstemp (tempfile);
1100 if (fd == -1)
1101 report_file_error ("Failed to open temporary file",
1102 Fcons (Vtemp_file_name_pattern, Qnil));
1103 else
1104 close (fd);
1105 }
1106#else
80856e74 1107 mktemp (tempfile);
1ddc85a4 1108#endif
80856e74
JB
1109
1110 filename_string = build_string (tempfile);
39323a7e 1111 GCPRO1 (filename_string);
80856e74
JB
1112 start = args[0];
1113 end = args[1];
32d08644 1114 /* Decide coding-system of the contents of the temporary file. */
91489411
RS
1115 if (!NILP (Vcoding_system_for_write))
1116 val = Vcoding_system_for_write;
1117 else if (NILP (current_buffer->enable_multibyte_characters))
32d08644
KH
1118 val = Qnil;
1119 else
beacaab3 1120 {
91489411
RS
1121 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
1122 args2[0] = Qcall_process_region;
1123 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
1124 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
1125 if (CONSP (coding_systems))
70949dac 1126 val = XCDR (coding_systems);
91489411 1127 else if (CONSP (Vdefault_process_coding_system))
70949dac 1128 val = XCDR (Vdefault_process_coding_system);
beacaab3 1129 else
91489411 1130 val = Qnil;
beacaab3 1131 }
32d08644 1132
168afdaa 1133 {
aed13378 1134 int count1 = SPECPDL_INDEX ();
168afdaa
RS
1135
1136 specbind (intern ("coding-system-for-write"), val);
1137 Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil);
1138
1139 unbind_to (count1, Qnil);
1140 }
91489411 1141
177c0ea7 1142 /* Note that Fcall_process takes care of binding
91489411 1143 coding-system-for-read. */
093650fe 1144
80856e74
JB
1145 record_unwind_protect (delete_temp_file, filename_string);
1146
edf496dd 1147 if (nargs > 3 && !NILP (args[3]))
80856e74
JB
1148 Fdelete_region (start, end);
1149
edf496dd
KH
1150 if (nargs > 3)
1151 {
1152 args += 2;
1153 nargs -= 2;
1154 }
1155 else
1156 {
1157 args[0] = args[2];
1158 nargs = 2;
1159 }
1160 args[1] = filename_string;
80856e74 1161
edf496dd 1162 RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs, args)));
80856e74
JB
1163}
1164\f
1165#ifndef VMS /* VMS version is in vmsproc.c. */
1166
dfcf069d
AS
1167static int relocate_fd ();
1168
5990851d
KL
1169static char **
1170add_env (char **env, char **new_env, char *string)
1171{
1172 char **ep;
1173 int ok = 1;
1174 if (string == NULL)
1175 return new_env;
1176
1177 /* See if this string duplicates any string already in the env.
1178 If so, don't put it in.
1179 When an env var has multiple definitions,
1180 we keep the definition that comes first in process-environment. */
1181 for (ep = env; ok && ep != new_env; ep++)
1182 {
1183 char *p = *ep, *q = string;
1184 while (ok)
1185 {
1186 if (*q != *p)
1187 break;
1188 if (*q == 0)
1189 /* The string is a lone variable name; keep it for now, we
1190 will remove it later. It is a placeholder for a
1191 variable that is not to be included in the environment. */
1192 break;
1193 if (*q == '=')
1194 ok = 0;
1195 p++, q++;
1196 }
1197 }
1198 if (ok)
1199 *new_env++ = string;
1200 return new_env;
1201}
1202
80856e74
JB
1203/* This is the last thing run in a newly forked inferior
1204 either synchronous or asynchronous.
1205 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1206 Initialize inferior's priority, pgrp, connected dir and environment.
1207 then exec another program based on new_argv.
1208
1209 This function may change environ for the superior process.
1210 Therefore, the superior process must save and restore the value
1211 of environ around the vfork and the call to this function.
1212
80856e74 1213 SET_PGRP is nonzero if we should put the subprocess into a separate
177c0ea7 1214 process group.
e576cab4
JB
1215
1216 CURRENT_DIR is an elisp string giving the path of the current
1217 directory the subprocess should have. Since we can't really signal
1218 a decent error from within the child, this should be verified as an
1219 executable directory by the parent. */
80856e74 1220
dfcf069d 1221int
e576cab4 1222child_setup (in, out, err, new_argv, set_pgrp, current_dir)
80856e74
JB
1223 int in, out, err;
1224 register char **new_argv;
80856e74 1225 int set_pgrp;
e576cab4 1226 Lisp_Object current_dir;
80856e74 1227{
e576cab4 1228 char **env;
7fcf7f05 1229 char *pwd_var;
bad95d8f
RS
1230#ifdef WINDOWSNT
1231 int cpid;
4252a4bd 1232 HANDLE handles[3];
bad95d8f 1233#endif /* WINDOWSNT */
e576cab4 1234
33abe2d9 1235 int pid = getpid ();
80856e74 1236
68d10241 1237#ifdef SET_EMACS_PRIORITY
4f0b9d49 1238 {
31ade731 1239 extern EMACS_INT emacs_priority;
4f0b9d49 1240
68d10241
RS
1241 if (emacs_priority < 0)
1242 nice (- emacs_priority);
4f0b9d49 1243 }
5b633aeb 1244#endif
80856e74
JB
1245
1246#ifdef subprocesses
1247 /* Close Emacs's descriptors that this process should not have. */
1248 close_process_descs ();
1249#endif
c17c4250
EZ
1250 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1251 we will lose if we call close_load_descs here. */
1252#ifndef DOS_NT
4458cebe 1253 close_load_descs ();
c17c4250 1254#endif
80856e74
JB
1255
1256 /* Note that use of alloca is always safe here. It's obvious for systems
1257 that do not have true vfork or that have true (stack) alloca.
1258 If using vfork and C_ALLOCA it is safe because that changes
1259 the superior's static variables as if the superior had done alloca
1260 and will be cleaned up in the usual way. */
e576cab4 1261 {
7fcf7f05 1262 register char *temp;
e576cab4 1263 register int i;
77d78be1 1264
d5db4077 1265 i = SBYTES (current_dir);
16425c4a
EZ
1266#ifdef MSDOS
1267 /* MSDOS must have all environment variables malloc'ed, because
1268 low-level libc functions that launch subsidiary processes rely
1269 on that. */
1270 pwd_var = (char *) xmalloc (i + 6);
1271#else
7fcf7f05 1272 pwd_var = (char *) alloca (i + 6);
16425c4a 1273#endif
7fcf7f05
RS
1274 temp = pwd_var + 4;
1275 bcopy ("PWD=", pwd_var, 4);
d5db4077 1276 bcopy (SDATA (current_dir), temp, i);
bad95d8f 1277 if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP;
e576cab4
JB
1278 temp[i] = 0;
1279
c17c4250 1280#ifndef DOS_NT
e576cab4
JB
1281 /* We can't signal an Elisp error here; we're in a vfork. Since
1282 the callers check the current directory before forking, this
1283 should only return an error if the directory's permissions
1284 are changed between the check and this chdir, but we should
1285 at least check. */
1286 if (chdir (temp) < 0)
20b25e46 1287 _exit (errno);
b4c7684c 1288#endif
7fcf7f05 1289
c17c4250
EZ
1290#ifdef DOS_NT
1291 /* Get past the drive letter, so that d:/ is left alone. */
1292 if (i > 2 && IS_DEVICE_SEP (temp[1]) && IS_DIRECTORY_SEP (temp[2]))
1293 {
1294 temp += 2;
1295 i -= 2;
1296 }
1297#endif
1298
7fcf7f05 1299 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
bad95d8f 1300 while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1]))
7fcf7f05 1301 temp[--i] = 0;
e576cab4 1302 }
80856e74 1303
5990851d 1304 /* Set `env' to a vector of the strings in the environment. */
80856e74
JB
1305 {
1306 register Lisp_Object tem;
1307 register char **new_env;
5990851d 1308 char **p, **q;
80856e74 1309 register int new_length;
5990851d 1310 Lisp_Object environment = Vglobal_environment;
f105f403 1311 Lisp_Object local;
80856e74
JB
1312
1313 new_length = 0;
f105f403 1314
5990851d
KL
1315 for (tem = Vprocess_environment;
1316 CONSP (tem) && STRINGP (XCAR (tem));
1317 tem = XCDR (tem))
1318 new_length++;
1319
f105f403
KL
1320 if (!NILP (Vlocal_environment_variables))
1321 {
1322 local = get_terminal_param (FRAME_DEVICE (XFRAME (selected_frame)),
1323 Qenvironment);
1324 if (EQ (Vlocal_environment_variables, Qt)
1325 && !NILP (local))
1326 environment = local;
1327 else if (CONSP (local))
1328 {
1329 new_length += Fsafe_length (Vlocal_environment_variables);
1330 }
1331 }
1332
1333 for (tem = environment;
70949dac
KR
1334 CONSP (tem) && STRINGP (XCAR (tem));
1335 tem = XCDR (tem))
80856e74
JB
1336 new_length++;
1337
7fcf7f05
RS
1338 /* new_length + 2 to include PWD and terminating 0. */
1339 env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *));
1340
1341 /* If we have a PWD envvar, pass one down,
1342 but with corrected value. */
1343 if (getenv ("PWD"))
1344 *new_env++ = pwd_var;
5990851d
KL
1345
1346 /* Overrides. */
1347 for (tem = Vprocess_environment;
1348 CONSP (tem) && STRINGP (XCAR (tem));
1349 tem = XCDR (tem))
1350 new_env = add_env (env, new_env, SDATA (XCAR (tem)));
80856e74 1351
5990851d 1352 /* Local part of environment, if Vlocal_environment_variables is a list. */
f105f403
KL
1353 for (tem = Vlocal_environment_variables;
1354 CONSP (tem) && STRINGP (XCAR (tem));
1355 tem = XCDR (tem))
5990851d 1356 new_env = add_env (env, new_env, egetenv (SDATA (XCAR (tem))));
f105f403 1357
5990851d
KL
1358 /* The rest of the environment (either Vglobal_environment or the
1359 'environment terminal parameter). */
f105f403 1360 for (tem = environment;
70949dac
KR
1361 CONSP (tem) && STRINGP (XCAR (tem));
1362 tem = XCDR (tem))
5990851d
KL
1363 new_env = add_env (env, new_env, SDATA (XCAR (tem)));
1364
1365 *new_env = 0;
1366
1367 /* Remove variable names without values. */
1368 p = q = env;
1369 while (*p != 0)
cd9565ba 1370 {
5990851d
KL
1371 while (*q != 0 && strchr (*q, '=') == NULL)
1372 *q++;
1373 *p = *q++;
1374 if (*p != 0)
1375 p++;
cd9565ba 1376 }
80856e74 1377 }
bad95d8f
RS
1378#ifdef WINDOWSNT
1379 prepare_standard_handles (in, out, err, handles);
d5db4077 1380 set_process_dir (SDATA (current_dir));
bad95d8f 1381#else /* not WINDOWSNT */
426b37ae
JB
1382 /* Make sure that in, out, and err are not actually already in
1383 descriptors zero, one, or two; this could happen if Emacs is
7e6c2178 1384 started with its standard in, out, or error closed, as might
426b37ae 1385 happen under X. */
f29f9e4a
RS
1386 {
1387 int oin = in, oout = out;
1388
1389 /* We have to avoid relocating the same descriptor twice! */
1390
1391 in = relocate_fd (in, 3);
1392
1393 if (out == oin)
1394 out = in;
1395 else
3e9367e7 1396 out = relocate_fd (out, 3);
f29f9e4a
RS
1397
1398 if (err == oin)
1399 err = in;
1400 else if (err == oout)
1401 err = out;
1402 else
3e9367e7 1403 err = relocate_fd (err, 3);
f29f9e4a 1404 }
426b37ae 1405
c17c4250 1406#ifndef MSDOS
68c45bf0
PE
1407 emacs_close (0);
1408 emacs_close (1);
1409 emacs_close (2);
80856e74
JB
1410
1411 dup2 (in, 0);
1412 dup2 (out, 1);
1413 dup2 (err, 2);
68c45bf0
PE
1414 emacs_close (in);
1415 emacs_close (out);
1416 emacs_close (err);
c17c4250 1417#endif /* not MSDOS */
bad95d8f 1418#endif /* not WINDOWSNT */
80856e74 1419
6b2cd868 1420#if defined(USG) && !defined(BSD_PGRPS)
fdba8590 1421#ifndef SETPGRP_RELEASES_CTTY
e576cab4 1422 setpgrp (); /* No arguments but equivalent in this case */
fdba8590 1423#endif
e576cab4
JB
1424#else
1425 setpgrp (pid, pid);
1426#endif /* USG */
a129418f
RS
1427 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1428 EMACS_SET_TTY_PGRP (0, &pid);
80856e74 1429
c17c4250
EZ
1430#ifdef MSDOS
1431 pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env);
a3f0666f 1432 xfree (pwd_var);
c17c4250
EZ
1433 if (pid == -1)
1434 /* An error occurred while trying to run the subprocess. */
1435 report_file_error ("Spawning child process", Qnil);
1436 return pid;
1437#else /* not MSDOS */
bad95d8f
RS
1438#ifdef WINDOWSNT
1439 /* Spawn the child. (See ntproc.c:Spawnve). */
1440 cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
db77d785 1441 reset_standard_handles (in, out, err, handles);
ff27bfbe
KH
1442 if (cpid == -1)
1443 /* An error occurred while trying to spawn the process. */
1444 report_file_error ("Spawning child process", Qnil);
bad95d8f
RS
1445 return cpid;
1446#else /* not WINDOWSNT */
80856e74
JB
1447 /* execvp does not accept an environment arg so the only way
1448 to pass this environment is to set environ. Our caller
1449 is responsible for restoring the ambient value of environ. */
1450 environ = env;
1451 execvp (new_argv[0], new_argv);
1452
68c45bf0
PE
1453 emacs_write (1, "Can't exec program: ", 20);
1454 emacs_write (1, new_argv[0], strlen (new_argv[0]));
1455 emacs_write (1, "\n", 1);
80856e74 1456 _exit (1);
bad95d8f 1457#endif /* not WINDOWSNT */
7e6c2178 1458#endif /* not MSDOS */
80856e74
JB
1459}
1460
a3833dfe 1461/* Move the file descriptor FD so that its number is not less than MINFD.
426b37ae 1462 If the file descriptor is moved at all, the original is freed. */
dfcf069d 1463static int
a3833dfe
KH
1464relocate_fd (fd, minfd)
1465 int fd, minfd;
426b37ae 1466{
a3833dfe 1467 if (fd >= minfd)
426b37ae
JB
1468 return fd;
1469 else
1470 {
1471 int new = dup (fd);
1472 if (new == -1)
1473 {
20c018a0 1474 char *message1 = "Error while setting up child: ";
826c56ac 1475 char *errmessage = strerror (errno);
20c018a0 1476 char *message2 = "\n";
68c45bf0
PE
1477 emacs_write (2, message1, strlen (message1));
1478 emacs_write (2, errmessage, strlen (errmessage));
1479 emacs_write (2, message2, strlen (message2));
426b37ae
JB
1480 _exit (1);
1481 }
1482 /* Note that we hold the original FD open while we recurse,
1483 to guarantee we'll get a new FD if we need it. */
a3833dfe 1484 new = relocate_fd (new, minfd);
68c45bf0 1485 emacs_close (fd);
426b37ae
JB
1486 return new;
1487 }
1488}
1489
012c6fcb 1490static int
f105f403 1491getenv_internal (var, varlen, value, valuelen, terminal)
012c6fcb
JA
1492 char *var;
1493 int varlen;
1494 char **value;
1495 int *valuelen;
f105f403 1496 Lisp_Object terminal;
012c6fcb
JA
1497{
1498 Lisp_Object scan;
5990851d
KL
1499 Lisp_Object environment = Vglobal_environment;
1500
1501 /* Try to find VAR in Vprocess_environment first. */
1502 for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan))
1503 {
1504 Lisp_Object entry = XCAR (scan);
1505 if (STRINGP (entry)
1506 && SBYTES (entry) >= varlen
1507#ifdef WINDOWSNT
1508 /* NT environment variables are case insensitive. */
1509 && ! strnicmp (SDATA (entry), var, varlen)
1510#else /* not WINDOWSNT */
1511 && ! bcmp (SDATA (entry), var, varlen)
1512#endif /* not WINDOWSNT */
1513 )
1514 {
1515 if (SBYTES (entry) > varlen && SREF (entry, varlen) == '=')
1516 {
1517 *value = (char *) SDATA (entry) + (varlen + 1);
1518 *valuelen = SBYTES (entry) - (varlen + 1);
1519 return 1;
1520 }
1521 else if (SBYTES (entry) == varlen)
1522 {
1523 /* Lone variable names in Vprocess_environment mean that
1524 variable should be removed from the environment. */
1525 return 0;
1526 }
1527 }
1528 }
f105f403
KL
1529
1530 /* Find the environment in which to search the variable. */
1531 if (!NILP (terminal))
1532 {
30663b47 1533 Lisp_Object local = get_terminal_param (get_device (terminal, 1), Qenvironment);
5990851d 1534 /* Use Vglobal_environment if there is no local environment. */
f105f403
KL
1535 if (!NILP (local))
1536 environment = local;
1537 }
1538 else if (!NILP (Vlocal_environment_variables))
1539 {
1540 Lisp_Object local = get_terminal_param (FRAME_DEVICE (XFRAME (selected_frame)),
1541 Qenvironment);
1542 if (EQ (Vlocal_environment_variables, Qt)
1543 && !NILP (local))
1544 environment = local;
1545 else if (CONSP (local))
1546 {
1547 for (scan = Vlocal_environment_variables; CONSP (scan); scan = XCDR (scan))
1548 {
1549 Lisp_Object entry = XCAR (scan);
1550 if (STRINGP (entry)
1551 && SBYTES (entry) == varlen
1552#ifdef WINDOWSNT
1553 /* NT environment variables are case insensitive. */
1554 && ! strnicmp (SDATA (entry), var, varlen)
1555#else /* not WINDOWSNT */
1556 && ! bcmp (SDATA (entry), var, varlen)
1557#endif /* not WINDOWSNT */
1558 )
1559 {
1560 environment = local;
1561 break;
1562 }
1563 }
1564 }
1565 }
012c6fcb 1566
f105f403 1567 for (scan = environment; CONSP (scan); scan = XCDR (scan))
012c6fcb 1568 {
c1350752
KH
1569 Lisp_Object entry;
1570
70949dac 1571 entry = XCAR (scan);
d50d3dc8 1572 if (STRINGP (entry)
f105f403
KL
1573 && SBYTES (entry) > varlen
1574 && SREF (entry, varlen) == '='
bad95d8f 1575#ifdef WINDOWSNT
f105f403
KL
1576 /* NT environment variables are case insensitive. */
1577 && ! strnicmp (SDATA (entry), var, varlen)
bad95d8f 1578#else /* not WINDOWSNT */
f105f403 1579 && ! bcmp (SDATA (entry), var, varlen)
bad95d8f 1580#endif /* not WINDOWSNT */
f105f403 1581 )
012c6fcb 1582 {
d5db4077
KR
1583 *value = (char *) SDATA (entry) + (varlen + 1);
1584 *valuelen = SBYTES (entry) - (varlen + 1);
012c6fcb
JA
1585 return 1;
1586 }
1587 }
1588
1589 return 0;
1590}
1591
f105f403 1592DEFUN ("getenv-internal", Fgetenv_internal, Sgetenv_internal, 1, 2, 0,
5990851d
KL
1593 doc: /* Get the value of environment variable VARIABLE.
1594VARIABLE should be a string. Value is nil if VARIABLE is undefined in
1595the environment. Otherwise, value is a string.
f105f403
KL
1596
1597If optional parameter TERMINAL is non-nil, then it should be a
1598terminal id or a frame. If the specified terminal device has its own
5990851d
KL
1599set of environment variables, this function will look up VARIABLE in
1600it.
1601
1602Otherwise, this function searches `process-environment' for VARIABLE.
1603If it was not found there, then it continues the search in either
1604`global-environment' or the local environment list of the current
1605terminal device, depending on the value of
1606`local-environment-variables'. */)
1607 (variable, terminal)
1608 Lisp_Object variable, terminal;
012c6fcb
JA
1609{
1610 char *value;
1611 int valuelen;
1612
5990851d
KL
1613 CHECK_STRING (variable);
1614 if (getenv_internal (SDATA (variable), SBYTES (variable),
f105f403 1615 &value, &valuelen, terminal))
012c6fcb
JA
1616 return make_string (value, valuelen);
1617 else
1618 return Qnil;
1619}
1620
5990851d
KL
1621/* A version of getenv that consults the Lisp environment lists,
1622 easily callable from C. */
012c6fcb
JA
1623char *
1624egetenv (var)
e576cab4 1625 char *var;
012c6fcb
JA
1626{
1627 char *value;
1628 int valuelen;
1629
f105f403 1630 if (getenv_internal (var, strlen (var), &value, &valuelen, Qnil))
012c6fcb
JA
1631 return value;
1632 else
1633 return 0;
1634}
1635
80856e74
JB
1636#endif /* not VMS */
1637\f
8de15d69 1638/* This is run before init_cmdargs. */
177c0ea7 1639
dfcf069d 1640void
8de15d69
RS
1641init_callproc_1 ()
1642{
1643 char *data_dir = egetenv ("EMACSDATA");
35a2f4b8
KH
1644 char *doc_dir = egetenv ("EMACSDOC");
1645
8de15d69 1646 Vdata_directory
177c0ea7 1647 = Ffile_name_as_directory (build_string (data_dir ? data_dir
8de15d69 1648 : PATH_DATA));
35a2f4b8
KH
1649 Vdoc_directory
1650 = Ffile_name_as_directory (build_string (doc_dir ? doc_dir
1651 : PATH_DOC));
9453ea7b 1652
e576cab4 1653 /* Check the EMACSPATH environment variable, defaulting to the
57bda87a 1654 PATH_EXEC path from epaths.h. */
e576cab4 1655 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
80856e74
JB
1656 Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
1657 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
8de15d69
RS
1658}
1659
e17f7533 1660/* This is run after init_cmdargs, when Vinstallation_directory is valid. */
8de15d69 1661
dfcf069d 1662void
8de15d69
RS
1663init_callproc ()
1664{
1665 char *data_dir = egetenv ("EMACSDATA");
177c0ea7 1666
8de15d69
RS
1667 register char * sh;
1668 Lisp_Object tempdir;
1669
9cc4fad5 1670 if (!NILP (Vinstallation_directory))
8de15d69 1671 {
05630743
RS
1672 /* Add to the path the lib-src subdir of the installation dir. */
1673 Lisp_Object tem;
1674 tem = Fexpand_file_name (build_string ("lib-src"),
1675 Vinstallation_directory);
bad95d8f 1676#ifndef DOS_NT
1a6640ec 1677 /* MSDOS uses wrapped binaries, so don't do this. */
0fa248bc 1678 if (NILP (Fmember (tem, Vexec_path)))
70ec1377
RS
1679 {
1680 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
1681 Vexec_path = Fcons (tem, Vexec_path);
1682 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
1683 }
177c0ea7 1684
0fa248bc 1685 Vexec_directory = Ffile_name_as_directory (tem);
bad95d8f 1686#endif /* not DOS_NT */
8de15d69 1687
e17f7533
RS
1688 /* Maybe use ../etc as well as ../lib-src. */
1689 if (data_dir == 0)
1690 {
1691 tem = Fexpand_file_name (build_string ("etc"),
1692 Vinstallation_directory);
1693 Vdoc_directory = Ffile_name_as_directory (tem);
8de15d69
RS
1694 }
1695 }
7e933683
RS
1696
1697 /* Look for the files that should be in etc. We don't use
1698 Vinstallation_directory, because these files are never installed
e17f7533 1699 near the executable, and they are never in the build
7e933683
RS
1700 directory when that's different from the source directory.
1701
1702 Instead, if these files are not in the nominal place, we try the
1703 source directory. */
1704 if (data_dir == 0)
1705 {
70ec1377 1706 Lisp_Object tem, tem1, srcdir;
7e933683 1707
70ec1377
RS
1708 srcdir = Fexpand_file_name (build_string ("../src/"),
1709 build_string (PATH_DUMPLOADSEARCH));
7e933683
RS
1710 tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory);
1711 tem1 = Ffile_exists_p (tem);
70ec1377 1712 if (!NILP (Fequal (srcdir, Vinvocation_directory)) || NILP (tem1))
7e933683 1713 {
70ec1377 1714 Lisp_Object newdir;
7e933683
RS
1715 newdir = Fexpand_file_name (build_string ("../etc/"),
1716 build_string (PATH_DUMPLOADSEARCH));
1717 tem = Fexpand_file_name (build_string ("GNU"), newdir);
1718 tem1 = Ffile_exists_p (tem);
1719 if (!NILP (tem1))
1720 Vdata_directory = newdir;
1721 }
1722 }
80856e74 1723
d883eb62
RS
1724#ifndef CANNOT_DUMP
1725 if (initialized)
1726#endif
1727 {
1728 tempdir = Fdirectory_file_name (Vexec_directory);
d5db4077 1729 if (access (SDATA (tempdir), 0) < 0)
d883eb62
RS
1730 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1731 Vexec_directory);
1732 }
80856e74 1733
e576cab4 1734 tempdir = Fdirectory_file_name (Vdata_directory);
d5db4077 1735 if (access (SDATA (tempdir), 0) < 0)
76d5c6cf
RS
1736 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1737 Vdata_directory);
e576cab4 1738
80856e74
JB
1739#ifdef VMS
1740 Vshell_file_name = build_string ("*dcl*");
1741#else
e576cab4 1742 sh = (char *) getenv ("SHELL");
80856e74
JB
1743 Vshell_file_name = build_string (sh ? sh : "/bin/sh");
1744#endif
8abd035b
RS
1745
1746#ifdef VMS
1747 Vtemp_file_name_pattern = build_string ("tmp:emacsXXXXXX.");
1748#else
1749 if (getenv ("TMPDIR"))
1750 {
1751 char *dir = getenv ("TMPDIR");
1752 Vtemp_file_name_pattern
1753 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1754 build_string (dir));
1755 }
1756 else
1757 Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX");
1758#endif
63789758 1759
40b49d4b
JB
1760#ifdef DOS_NT
1761 Vshared_game_score_directory = Qnil;
1762#else
63789758
RS
1763 Vshared_game_score_directory = build_string (PATH_GAME);
1764 if (NILP (Ffile_directory_p (Vshared_game_score_directory)))
1765 Vshared_game_score_directory = Qnil;
40b49d4b 1766#endif
9fefd2ba
JB
1767}
1768
dfcf069d 1769void
5990851d 1770set_global_environment ()
9fefd2ba
JB
1771{
1772 register char **envp;
80856e74 1773
5990851d 1774 Vglobal_environment = Qnil;
80856e74
JB
1775#ifndef CANNOT_DUMP
1776 if (initialized)
1777#endif
1778 for (envp = environ; *envp; envp++)
5990851d
KL
1779 Vglobal_environment = Fcons (build_string (*envp),
1780 Vglobal_environment);
80856e74
JB
1781}
1782
dfcf069d 1783void
80856e74
JB
1784syms_of_callproc ()
1785{
bad95d8f 1786#ifdef DOS_NT
093650fe
RS
1787 Qbuffer_file_type = intern ("buffer-file-type");
1788 staticpro (&Qbuffer_file_type);
bad95d8f 1789#endif /* DOS_NT */
7e6c2178 1790
80856e74 1791 DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
fdb82f93
PJ
1792 doc: /* *File name to load inferior shells from.
1793Initialized from the SHELL environment variable. */);
80856e74
JB
1794
1795 DEFVAR_LISP ("exec-path", &Vexec_path,
fdb82f93
PJ
1796 doc: /* *List of directories to search programs to run in subprocesses.
1797Each element is a string (directory name) or nil (try default directory). */);
80856e74 1798
b81a1b72 1799 DEFVAR_LISP ("exec-suffixes", &Vexec_suffixes,
fdb82f93
PJ
1800 doc: /* *List of suffixes to try to find executable file names.
1801Each element is a string. */);
33d5af99 1802 Vexec_suffixes = Qnil;
b81a1b72 1803
80856e74 1804 DEFVAR_LISP ("exec-directory", &Vexec_directory,
fdb82f93
PJ
1805 doc: /* Directory for executables for Emacs to invoke.
1806More generally, this includes any architecture-dependent files
1807that are built and installed from the Emacs distribution. */);
e576cab4
JB
1808
1809 DEFVAR_LISP ("data-directory", &Vdata_directory,
fdb82f93
PJ
1810 doc: /* Directory of machine-independent files that come with GNU Emacs.
1811These are files intended for Emacs to use while it runs. */);
80856e74 1812
35a2f4b8 1813 DEFVAR_LISP ("doc-directory", &Vdoc_directory,
fdb82f93
PJ
1814 doc: /* Directory containing the DOC file that comes with GNU Emacs.
1815This is usually the same as data-directory. */);
35a2f4b8 1816
ed61592a 1817 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory,
fdb82f93
PJ
1818 doc: /* For internal use by the build procedure only.
1819This is the name of the directory in which the build procedure installed
1820Emacs's info files; the default value for Info-default-directory-list
1821includes this. */);
ed61592a
JB
1822 Vconfigure_info_directory = build_string (PATH_INFO);
1823
1e7ce61b 1824 DEFVAR_LISP ("shared-game-score-directory", &Vshared_game_score_directory,
b065672a
CW
1825 doc: /* Directory of score files for games which come with GNU Emacs.
1826If this variable is nil, then Emacs is unable to use a shared directory. */);
40b49d4b
JB
1827#ifdef DOS_NT
1828 Vshared_game_score_directory = Qnil;
1829#else
63789758 1830 Vshared_game_score_directory = build_string (PATH_GAME);
40b49d4b 1831#endif
b065672a 1832
8abd035b 1833 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern,
fdb82f93
PJ
1834 doc: /* Pattern for making names for temporary files.
1835This is used by `call-process-region'. */);
0537ec48 1836 /* This variable is initialized in init_callproc. */
8abd035b 1837
5990851d
KL
1838 DEFVAR_LISP ("global-environment", &Vglobal_environment,
1839 doc: /* Global list of environment variables for subprocesses to inherit.
fdb82f93 1840Each element should be a string of the form ENVVARNAME=VALUE.
5990851d
KL
1841
1842The environment which Emacs inherits is placed in this variable when
1843Emacs starts.
1844
1845Some terminal devices may have their own local list of environment
1846variables in their 'environment parameter, which may override this
1847global list; see `local-environment-variables'. See
1848`process-environment' for a way to modify an environment variable on
1849all terminals.
1850
fdb82f93
PJ
1851If multiple entries define the same variable, the first one always
1852takes precedence.
5990851d 1853
776a24a1
DL
1854Non-ASCII characters are encoded according to the initial value of
1855`locale-coding-system', i.e. the elements must normally be decoded for use.
5990851d
KL
1856See `setenv' and `getenv'. */);
1857
1858 DEFVAR_LISP ("process-environment", &Vprocess_environment,
1859 doc: /* List of overridden environment variables for subprocesses to inherit.
1860Each element should be a string of the form ENVVARNAME=VALUE.
1861
1862Entries in this list take precedence to those in `global-environment'
1863or the terminal environment. (See `local-environment-variables' for
1864an explanation of the terminal-local environment.) Therefore,
1865let-binding `process-environment' is an easy way to temporarily change
1866the value of an environment variable, irrespective of where it comes
1867from. To use `process-environment' to remove an environment variable,
1868include only its name in the list, without "=VALUE".
1869
1870This variable is set to nil when Emacs starts.
1871
1872If multiple entries define the same variable, the first one always
1873takes precedence.
1874
1875Non-ASCII characters are encoded according to the initial value of
1876`locale-coding-system', i.e. the elements must normally be decoded for
1877use.
1878
776a24a1 1879See `setenv' and `getenv'. */);
86f5ca04 1880 Vprocess_environment = Qnil;
80856e74
JB
1881
1882#ifndef VMS
1883 defsubr (&Scall_process);
83fa009c 1884 defsubr (&Sgetenv_internal);
986ffb24 1885#endif
e576cab4 1886 defsubr (&Scall_process_region);
f105f403
KL
1887
1888 DEFVAR_LISP ("local-environment-variables", &Vlocal_environment_variables,
1889 doc: /* Enable or disable terminal-local environment variables.
1890If set to t, `getenv', `setenv' and subprocess creation functions use
5990851d
KL
1891the local environment of the terminal device of the selected frame,
1892ignoring `global-environment'.
f105f403 1893
5990851d
KL
1894If set to nil, Emacs uses `global-environment' and ignores the
1895terminal environment.
f105f403 1896
5990851d
KL
1897Otherwise, `local-environment-variables' should be a list of variable
1898names (represented by Lisp strings) to look up in the terminal's
1899environment. The rest will come from `global-environment'. */);
f105f403
KL
1900 Vlocal_environment_variables = Qnil;
1901
1902 Qenvironment = intern ("environment");
1903 staticpro (&Qenvironment);
80856e74 1904}
ab5796a9
MB
1905
1906/* arch-tag: 769b8045-1df7-4d2b-8968-e3fb49017f95
1907 (do not change this comment) */