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