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