inferior-octave-directory-tracker: Change regexp so that it doesn't
[bpt/emacs.git] / src / callproc.c
CommitLineData
80856e74 1/* Synchronous subprocess invocation for GNU Emacs.
f8c25f1b 2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95 Free Software Foundation, Inc.
80856e74
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
826c56ac 8the Free Software Foundation; either version 2, or (at your option)
80856e74
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
80856e74
JB
20
21
22#include <signal.h>
e576cab4 23#include <errno.h>
80856e74 24
18160b98 25#include <config.h>
565620a5 26#include <stdio.h>
80856e74 27
426b37ae 28extern int errno;
826c56ac 29extern char *strerror ();
426b37ae 30
80856e74
JB
31/* Define SIGCHLD as an alias for SIGCLD. */
32
33#if !defined (SIGCHLD) && defined (SIGCLD)
34#define SIGCHLD SIGCLD
35#endif /* SIGCLD */
36
37#include <sys/types.h>
88a64fef 38
80856e74
JB
39#include <sys/file.h>
40#ifdef USG5
472e83fe 41#define INCLUDED_FCNTL
80856e74
JB
42#include <fcntl.h>
43#endif
44
bad95d8f
RS
45#ifdef WINDOWSNT
46#define NOMINMAX
47#include <windows.h>
48#include <stdlib.h> /* for proper declaration of environ */
49#include <fcntl.h>
489f9371 50#include "w32.h"
bad95d8f
RS
51#define _P_NOWAIT 1 /* from process.h */
52#endif
53
7e6c2178 54#ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
472e83fe 55#define INCLUDED_FCNTL
7e6c2178
RS
56#include <fcntl.h>
57#include <sys/stat.h>
58#include <sys/param.h>
59#include <errno.h>
60#endif /* MSDOS */
61
80856e74
JB
62#ifndef O_RDONLY
63#define O_RDONLY 0
64#endif
65
66#ifndef O_WRONLY
67#define O_WRONLY 1
68#endif
69
70#include "lisp.h"
71#include "commands.h"
72#include "buffer.h"
32d08644 73#include "charset.h"
edf496dd 74#include "ccl.h"
32d08644 75#include "coding.h"
57bda87a 76#include <epaths.h>
80856e74 77#include "process.h"
d177f194 78#include "syssignal.h"
a129418f 79#include "systty.h"
80856e74 80
5f027cea
EZ
81#ifdef MSDOS
82#include "msdos.h"
83#endif
84
80856e74
JB
85#ifdef VMS
86extern noshare char **environ;
87#else
88extern char **environ;
89#endif
90
91#define max(a, b) ((a) > (b) ? (a) : (b))
92
35a2f4b8 93Lisp_Object Vexec_path, Vexec_directory, Vdata_directory, Vdoc_directory;
ed61592a 94Lisp_Object Vconfigure_info_directory;
8abd035b 95Lisp_Object Vtemp_file_name_pattern;
80856e74
JB
96
97Lisp_Object Vshell_file_name;
98
80856e74 99Lisp_Object Vprocess_environment;
80856e74 100
bad95d8f 101#ifdef DOS_NT
093650fe 102Lisp_Object Qbuffer_file_type;
bad95d8f 103#endif /* DOS_NT */
093650fe 104
80856e74
JB
105/* True iff we are about to fork off a synchronous process or if we
106 are waiting for it. */
107int synch_process_alive;
108
109/* Nonzero => this is a string explaining death of synchronous subprocess. */
110char *synch_process_death;
111
112/* If synch_process_death is zero,
113 this is exit code of synchronous subprocess. */
114int synch_process_retcode;
8de15d69
RS
115
116extern Lisp_Object Vdoc_file_name;
89e1ec1d 117
8d024345 118extern Lisp_Object Vfile_name_coding_system, Vdefault_file_name_coding_system;
80856e74 119\f
37d54121
RS
120/* Clean up when exiting Fcall_process.
121 On MSDOS, delete the temporary file on any kind of termination.
122 On Unix, kill the process and any children on termination by signal. */
123
124/* Nonzero if this is termination due to exit. */
125static int call_process_exited;
126
80856e74
JB
127#ifndef VMS /* VMS version is in vmsproc.c. */
128
d177f194
JB
129static Lisp_Object
130call_process_kill (fdpid)
131 Lisp_Object fdpid;
132{
133 close (XFASTINT (Fcar (fdpid)));
134 EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
135 synch_process_alive = 0;
136 return Qnil;
137}
138
80856e74
JB
139Lisp_Object
140call_process_cleanup (fdpid)
141 Lisp_Object fdpid;
142{
7e6c2178
RS
143#ifdef MSDOS
144 /* for MSDOS fdpid is really (fd . tempfile) */
c1350752
KH
145 register Lisp_Object file;
146 file = Fcdr (fdpid);
7e6c2178
RS
147 close (XFASTINT (Fcar (fdpid)));
148 if (strcmp (XSTRING (file)-> data, NULL_DEVICE) != 0)
149 unlink (XSTRING (file)->data);
150#else /* not MSDOS */
d177f194
JB
151 register int pid = XFASTINT (Fcdr (fdpid));
152
6b6e798b 153
37d54121 154 if (call_process_exited)
6b6e798b
RS
155 {
156 close (XFASTINT (Fcar (fdpid)));
157 return Qnil;
158 }
37d54121 159
d177f194
JB
160 if (EMACS_KILLPG (pid, SIGINT) == 0)
161 {
162 int count = specpdl_ptr - specpdl;
163 record_unwind_protect (call_process_kill, fdpid);
164 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
165 immediate_quit = 1;
166 QUIT;
167 wait_for_termination (pid);
168 immediate_quit = 0;
169 specpdl_ptr = specpdl + count; /* Discard the unwind protect. */
170 message1 ("Waiting for process to die...done");
171 }
80856e74 172 synch_process_alive = 0;
d177f194 173 close (XFASTINT (Fcar (fdpid)));
7e6c2178 174#endif /* not MSDOS */
80856e74
JB
175 return Qnil;
176}
177
178DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
179 "Call PROGRAM synchronously in separate process.\n\
edf496dd 180The remaining arguments are optional.\n\
80856e74
JB
181The program's input comes from file INFILE (nil means `/dev/null').\n\
182Insert output in BUFFER before point; t means current buffer;\n\
183 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
39eaa782
RS
184BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
185REAL-BUFFER says what to do with standard output, as above,\n\
186while STDERR-FILE says what to do with standard error in the child.\n\
187STDERR-FILE may be nil (discard standard error output),\n\
188t (mix it with ordinary output), or a file name string.\n\
189\n\
80856e74
JB
190Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
191Remaining arguments are strings passed as command arguments to PROGRAM.\n\
39eaa782
RS
192\n\
193If BUFFER is 0, `call-process' returns immediately with value nil.\n\
194Otherwise it waits for PROGRAM to terminate\n\
e576cab4 195and returns a numeric exit status or a signal description string.\n\
d177f194 196If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
80856e74
JB
197 (nargs, args)
198 int nargs;
199 register Lisp_Object *args;
200{
58616e67 201 Lisp_Object infile, buffer, current_dir, display, path;
80856e74
JB
202 int fd[2];
203 int filefd;
204 register int pid;
6e3bfbb2
RS
205 char buf[16384];
206 char *bufptr = buf;
207 int bufsize = 16384;
80856e74 208 int count = specpdl_ptr - specpdl;
2d607244 209
80856e74
JB
210 register unsigned char **new_argv
211 = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
212 struct buffer *old = current_buffer;
39eaa782
RS
213 /* File to use for stderr in the child.
214 t means use same as standard output. */
215 Lisp_Object error_file;
7e6c2178
RS
216#ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
217 char *outf, *tempfile;
218 int outfilefd;
219#endif
80856e74
JB
220#if 0
221 int mask;
222#endif
32d08644
KH
223 struct coding_system process_coding; /* coding-system of process output */
224 struct coding_system argument_coding; /* coding-system of arguments */
225
80856e74
JB
226 CHECK_STRING (args[0], 0);
227
39eaa782
RS
228 error_file = Qt;
229
7e6c2178
RS
230#ifndef subprocesses
231 /* Without asynchronous processes we cannot have BUFFER == 0. */
3ffde7d6 232 if (nargs >= 3
09ffb8b5 233 && (INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2])))
7e6c2178
RS
234 error ("Operating system cannot handle asynchronous subprocesses");
235#endif /* subprocesses */
236
32d08644
KH
237 /* Decide the coding-system for giving arguments and reading process
238 output. */
239 {
240 Lisp_Object val, *args2;
08ee4e87 241 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
32d08644
KH
242 Lisp_Object coding_systems = Qt;
243 int i;
244
245 /* If arguments are supplied, we may have to encode them. */
246 if (nargs >= 5)
247 {
30d57b8e
RS
248 int must_encode = 0;
249
e7c1c20e
RS
250 for (i = 4; i < nargs; i++)
251 CHECK_STRING (args[i], i);
252
a2286b5c 253 for (i = 4; i < nargs; i++)
30d57b8e
RS
254 if (STRING_MULTIBYTE (args[i]))
255 must_encode = 1;
256
beacaab3
KH
257 if (!NILP (Vcoding_system_for_write))
258 val = Vcoding_system_for_write;
30d57b8e 259 else if (! must_encode)
beacaab3
KH
260 val = Qnil;
261 else
32d08644
KH
262 {
263 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
264 args2[0] = Qcall_process;
265 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
08ee4e87 266 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
776b95cb
KH
267 if (CONSP (coding_systems))
268 val = XCONS (coding_systems)->cdr;
269 else if (CONSP (Vdefault_process_coding_system))
270 val = XCONS (Vdefault_process_coding_system)->cdr;
beacaab3
KH
271 else
272 val = Qnil;
32d08644
KH
273 }
274 setup_coding_system (Fcheck_coding_system (val), &argument_coding);
275 }
276
277 /* If BUFFER is nil, we must read process output once and then
278 discard it, so setup coding system but with nil. If BUFFER is
279 an integer, we can discard it without reading. */
3ffde7d6
RS
280 if (nargs < 3 || NILP (args[2])
281 || (CONSP (args[2]) && NILP (XCAR (args[2]))))
32d08644 282 setup_coding_system (Qnil, &process_coding);
3ffde7d6 283 else if (!INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2]))
32d08644 284 {
beacaab3
KH
285 val = Qnil;
286 if (!NILP (Vcoding_system_for_read))
287 val = Vcoding_system_for_read;
288 else if (NILP (current_buffer->enable_multibyte_characters))
321fecde 289 val = Qraw_text;
beacaab3 290 else
32d08644 291 {
4724eab4 292 if (EQ (coding_systems, Qt))
32d08644
KH
293 {
294 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
295 args2[0] = Qcall_process;
296 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
08ee4e87
KH
297 coding_systems
298 = Ffind_operation_coding_system (nargs + 1, args2);
32d08644 299 }
776b95cb
KH
300 if (CONSP (coding_systems))
301 val = XCONS (coding_systems)->car;
302 else if (CONSP (Vdefault_process_coding_system))
303 val = XCONS (Vdefault_process_coding_system)->car;
beacaab3
KH
304 else
305 val = Qnil;
32d08644
KH
306 }
307 setup_coding_system (Fcheck_coding_system (val), &process_coding);
308 }
309 }
310
e576cab4
JB
311 if (nargs >= 2 && ! NILP (args[1]))
312 {
313 infile = Fexpand_file_name (args[1], current_buffer->directory);
314 CHECK_STRING (infile, 1);
315 }
80856e74 316 else
5437e9f9 317 infile = build_string (NULL_DEVICE);
80856e74 318
e576cab4
JB
319 if (nargs >= 3)
320 {
39eaa782
RS
321 buffer = args[2];
322
323 /* If BUFFER is a list, its meaning is
324 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
325 if (CONSP (buffer))
326 {
327 if (CONSP (XCONS (buffer)->cdr))
45be8a1e 328 {
a9d4f28a 329 Lisp_Object stderr_file;
45be8a1e
RS
330 stderr_file = XCONS (XCONS (buffer)->cdr)->car;
331
332 if (NILP (stderr_file) || EQ (Qt, stderr_file))
333 error_file = stderr_file;
334 else
335 error_file = Fexpand_file_name (stderr_file, Qnil);
336 }
337
39eaa782
RS
338 buffer = XCONS (buffer)->car;
339 }
044512ed 340
39eaa782
RS
341 if (!(EQ (buffer, Qnil)
342 || EQ (buffer, Qt)
3ffde7d6 343 || INTEGERP (buffer)))
e576cab4 344 {
39eaa782
RS
345 Lisp_Object spec_buffer;
346 spec_buffer = buffer;
50fe359b 347 buffer = Fget_buffer_create (buffer);
39eaa782
RS
348 /* Mention the buffer name for a better error message. */
349 if (NILP (buffer))
350 CHECK_BUFFER (spec_buffer, 2);
e576cab4
JB
351 CHECK_BUFFER (buffer, 2);
352 }
353 }
354 else
355 buffer = Qnil;
80856e74 356
58616e67
JB
357 /* Make sure that the child will be able to chdir to the current
358 buffer's current directory, or its unhandled equivalent. We
359 can't just have the child check for an error when it does the
360 chdir, since it's in a vfork.
361
362 We have to GCPRO around this because Fexpand_file_name,
363 Funhandled_file_name_directory, and Ffile_accessible_directory_p
364 might call a file name handling function. The argument list is
365 protected by the caller, so all we really have to worry about is
366 buffer. */
367 {
368 struct gcpro gcpro1, gcpro2, gcpro3;
369
370 current_dir = current_buffer->directory;
371
372 GCPRO3 (infile, buffer, current_dir);
373
c52b0b34
KH
374 current_dir
375 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
376 Qnil);
58616e67
JB
377 if (NILP (Ffile_accessible_directory_p (current_dir)))
378 report_file_error ("Setting current directory",
379 Fcons (current_buffer->directory, Qnil));
380
381 UNGCPRO;
382 }
383
e576cab4 384 display = nargs >= 4 ? args[3] : Qnil;
80856e74 385
e576cab4 386 filefd = open (XSTRING (infile)->data, O_RDONLY, 0);
80856e74
JB
387 if (filefd < 0)
388 {
e576cab4 389 report_file_error ("Opening process input file", Fcons (infile, Qnil));
80856e74
JB
390 }
391 /* Search for program; barf if not found. */
c52b0b34
KH
392 {
393 struct gcpro gcpro1;
394
395 GCPRO1 (current_dir);
396 openp (Vexec_path, args[0], EXEC_SUFFIXES, &path, 1);
397 UNGCPRO;
398 }
012c6fcb 399 if (NILP (path))
80856e74
JB
400 {
401 close (filefd);
402 report_file_error ("Searching for program", Fcons (args[0], Qnil));
403 }
404 new_argv[0] = XSTRING (path)->data;
c364e618
KH
405 if (nargs > 4)
406 {
407 register int i;
408
c364e618
KH
409 if (! CODING_REQUIRE_ENCODING (&argument_coding))
410 {
411 for (i = 4; i < nargs; i++)
412 new_argv[i - 3] = XSTRING (args[i])->data;
413 }
414 else
415 {
416 /* We must encode the arguments. */
417 struct gcpro gcpro1, gcpro2, gcpro3;
418
419 GCPRO3 (infile, buffer, current_dir);
420 for (i = 4; i < nargs; i++)
421 {
422 int size = encoding_buffer_size (&argument_coding,
fc932ac6 423 STRING_BYTES (XSTRING (args[i])));
c364e618 424 unsigned char *dummy1 = (unsigned char *) alloca (size);
321fecde 425 int dummy;
c364e618
KH
426
427 /* The Irix 4.0 compiler barfs if we eliminate dummy. */
428 new_argv[i - 3] = dummy1;
edf496dd 429 argument_coding.mode |= CODING_MODE_LAST_BLOCK;
321fecde
KH
430 encode_coding (&argument_coding,
431 XSTRING (args[i])->data,
432 new_argv[i - 3],
fc932ac6 433 STRING_BYTES (XSTRING (args[i])),
321fecde
KH
434 size);
435 new_argv[i - 3][argument_coding.produced] = 0;
edf496dd
KH
436 /* We have to initialize CCL program status again. */
437 if (argument_coding.type == coding_type_ccl)
438 setup_ccl_program (&(argument_coding.spec.ccl.encoder), Qnil);
c364e618
KH
439 }
440 UNGCPRO;
441 }
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);
456 if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/')
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 {
464 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
d50d3dc8 472 if (INTEGERP (buffer))
5437e9f9 473 fd[1] = open (NULL_DEVICE, O_WRONLY), fd[0] = -1;
80856e74
JB
474 else
475 {
7e6c2178 476#ifndef MSDOS
80856e74 477 pipe (fd);
7e6c2178 478#endif
80856e74
JB
479#if 0
480 /* Replaced by close_process_descs */
481 set_exclusive_use (fd[0]);
482#endif
483 }
484
485 {
486 /* child_setup must clobber environ in systems with true vfork.
487 Protect it from permanent change. */
488 register char **save_environ = environ;
489 register int fd1 = fd[1];
39eaa782 490 int fd_error = fd1;
80856e74
JB
491
492#if 0 /* Some systems don't have sigblock. */
e065a56e 493 mask = sigblock (sigmask (SIGCHLD));
80856e74
JB
494#endif
495
496 /* Record that we're about to create a synchronous process. */
497 synch_process_alive = 1;
498
5c03767e
RS
499 /* These vars record information from process termination.
500 Clear them now before process can possibly terminate,
501 to avoid timing error if process terminates soon. */
502 synch_process_death = 0;
503 synch_process_retcode = 0;
504
39eaa782
RS
505 if (NILP (error_file))
506 fd_error = open (NULL_DEVICE, O_WRONLY);
507 else if (STRINGP (error_file))
508 {
509#ifdef DOS_NT
510 fd_error = open (XSTRING (error_file)->data,
511 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
512 S_IREAD | S_IWRITE);
513#else /* not DOS_NT */
514 fd_error = creat (XSTRING (error_file)->data, 0666);
515#endif /* not DOS_NT */
516 }
517
518 if (fd_error < 0)
519 {
520 close (filefd);
6f89d28a
MB
521 if (fd[0] != filefd)
522 close (fd[0]);
39eaa782
RS
523 if (fd1 >= 0)
524 close (fd1);
6f89d28a
MB
525#ifdef MSDOS
526 unlink (tempfile);
527#endif
528 report_file_error ("Cannot redirect stderr",
529 Fcons ((NILP (error_file)
530 ? build_string (NULL_DEVICE) : error_file),
531 Qnil));
39eaa782 532 }
89e1ec1d 533
8d024345 534 current_dir = ENCODE_FILE (current_dir);
89e1ec1d 535
2610078a 536#ifdef MSDOS /* MW, July 1993 */
c17c4250 537 /* Note that on MSDOS `child_setup' actually returns the child process
2610078a
KH
538 exit status, not its PID, so we assign it to `synch_process_retcode'
539 below. */
c17c4250
EZ
540 pid = child_setup (filefd, outfilefd, fd_error, (char **) new_argv,
541 0, current_dir);
39eaa782 542
2610078a
KH
543 /* Record that the synchronous process exited and note its
544 termination status. */
545 synch_process_alive = 0;
546 synch_process_retcode = pid;
547 if (synch_process_retcode < 0) /* means it couldn't be exec'ed */
759ae811 548 synch_process_death = strerror (errno);
2610078a
KH
549
550 close (outfilefd);
551 if (fd_error != outfilefd)
552 close (fd_error);
553 fd1 = -1; /* No harm in closing that one! */
32d08644
KH
554 /* Since CRLF is converted to LF within `decode_coding', we can
555 always open a file with binary mode. */
556 fd[0] = open (tempfile, O_BINARY);
2610078a
KH
557 if (fd[0] < 0)
558 {
559 unlink (tempfile);
560 close (filefd);
561 report_file_error ("Cannot re-open temporary file", Qnil);
562 }
563#else /* not MSDOS */
bad95d8f 564#ifdef WINDOWSNT
2d607244
RS
565 pid = child_setup (filefd, fd1, fd_error, (char **) new_argv,
566 0, current_dir);
bad95d8f 567#else /* not WINDOWSNT */
80856e74
JB
568 pid = vfork ();
569
570 if (pid == 0)
571 {
572 if (fd[0] >= 0)
573 close (fd[0]);
1e7963c7
RS
574#ifdef HAVE_SETSID
575 setsid ();
576#endif
577#if defined (USG) && !defined (BSD_PGRPS)
80856e74
JB
578 setpgrp ();
579#else
580 setpgrp (pid, pid);
581#endif /* USG */
2d607244
RS
582 child_setup (filefd, fd1, fd_error, (char **) new_argv,
583 0, current_dir);
80856e74 584 }
bad95d8f 585#endif /* not WINDOWSNT */
cd5f8f60
RS
586
587 /* The MSDOS case did this already. */
588 if (fd_error >= 0)
589 close (fd_error);
2610078a 590#endif /* not MSDOS */
80856e74 591
80856e74
JB
592 environ = save_environ;
593
6b6e798b
RS
594 /* Close most of our fd's, but not fd[0]
595 since we will use that to read input from. */
80856e74 596 close (filefd);
799abb26 597 if (fd1 >= 0 && fd1 != fd_error)
7e6c2178 598 close (fd1);
80856e74
JB
599 }
600
601 if (pid < 0)
602 {
6b6e798b
RS
603 if (fd[0] >= 0)
604 close (fd[0]);
80856e74
JB
605 report_file_error ("Doing vfork", Qnil);
606 }
607
d50d3dc8 608 if (INTEGERP (buffer))
80856e74 609 {
6b6e798b
RS
610 if (fd[0] >= 0)
611 close (fd[0]);
80856e74 612#ifndef subprocesses
e576cab4
JB
613 /* If Emacs has been built with asynchronous subprocess support,
614 we don't need to do this, I think because it will then have
615 the facilities for handling SIGCHLD. */
80856e74
JB
616 wait_without_blocking ();
617#endif /* subprocesses */
80856e74
JB
618 return Qnil;
619 }
620
6b6e798b 621 /* Enable sending signal if user quits below. */
37d54121
RS
622 call_process_exited = 0;
623
7e6c2178
RS
624#ifdef MSDOS
625 /* MSDOS needs different cleanup information. */
626 record_unwind_protect (call_process_cleanup,
627 Fcons (make_number (fd[0]), build_string (tempfile)));
628#else
80856e74
JB
629 record_unwind_protect (call_process_cleanup,
630 Fcons (make_number (fd[0]), make_number (pid)));
7e6c2178 631#endif /* not MSDOS */
80856e74
JB
632
633
d50d3dc8 634 if (BUFFERP (buffer))
80856e74
JB
635 Fset_buffer (buffer);
636
637 immediate_quit = 1;
638 QUIT;
639
640 {
641 register int nread;
0ad477db 642 int first = 1;
6e3bfbb2 643 int total_read = 0;
321fecde 644 int carryover = 0;
7a7ab107 645 int display_on_the_fly = !NILP (display) && INTERACTIVE;
05b44e90
KH
646 struct coding_system saved_coding;
647
648 saved_coding = process_coding;
80856e74 649
60558b19 650 while (1)
80856e74 651 {
60558b19
RS
652 /* Repeatedly read until we've filled as much as possible
653 of the buffer size we have. But don't read
8e6208c5 654 less than 1024--save that for the next bufferful. */
321fecde 655 nread = carryover;
60558b19 656 while (nread < bufsize - 1024)
00fb3e95 657 {
321fecde 658 int this_read = read (fd[0], bufptr + nread, bufsize - nread);
60558b19
RS
659
660 if (this_read < 0)
661 goto give_up;
662
663 if (this_read == 0)
7a7ab107
KH
664 {
665 process_coding.mode |= CODING_MODE_LAST_BLOCK;
666 break;
667 }
60558b19
RS
668
669 nread += this_read;
7a7ab107 670 total_read += this_read;
60558b19 671
7a7ab107
KH
672 if (display_on_the_fly)
673 break;
674 }
60558b19
RS
675
676 /* Now NREAD is the total amount of data in the buffer. */
80856e74 677 immediate_quit = 0;
6e3bfbb2 678
012c6fcb 679 if (!NILP (buffer))
32d08644
KH
680 {
681 if (process_coding.type == coding_type_no_conversion)
682 insert (bufptr, nread);
683 else
684 { /* We have to decode the input. */
321fecde 685 int size = decoding_buffer_size (&process_coding, nread);
a871dd58 686 char *decoding_buf = (char *) xmalloc (size);
32d08644 687
321fecde
KH
688 decode_coding (&process_coding, bufptr, decoding_buf,
689 nread, size);
7a7ab107
KH
690 if (display_on_the_fly
691 && saved_coding.type == coding_type_undecided
692 && process_coding.type != coding_type_undecided)
693 {
694 /* We have detected some coding system. But,
695 there's a possibility that the detection was
696 done by insufficient data. So, we give up
697 displaying on the fly. */
a871dd58 698 xfree (decoding_buf);
7a7ab107
KH
699 display_on_the_fly = 0;
700 process_coding = saved_coding;
701 carryover = nread;
702 continue;
703 }
321fecde
KH
704 if (process_coding.produced > 0)
705 insert (decoding_buf, process_coding.produced);
a871dd58 706 xfree (decoding_buf);
11218c68 707 carryover = nread - process_coding.consumed;
321fecde
KH
708 if (carryover > 0)
709 {
710 /* As CARRYOVER should not be that large, we had
711 better avoid overhead of bcopy. */
712 char *p = bufptr + process_coding.consumed;
713 char *pend = p + carryover;
714 char *dst = bufptr;
715
716 while (p < pend) *dst++ = *p++;
717 }
32d08644
KH
718 }
719 }
321fecde
KH
720 if (process_coding.mode & CODING_MODE_LAST_BLOCK)
721 {
722 if (carryover > 0)
723 insert (bufptr, carryover);
724 break;
725 }
6e3bfbb2
RS
726
727 /* Make the buffer bigger as we continue to read more data,
728 but not past 64k. */
729 if (bufsize < 64 * 1024 && total_read > 32 * bufsize)
730 {
731 bufsize *= 2;
732 bufptr = (char *) alloca (bufsize);
733 }
734
012c6fcb 735 if (!NILP (display) && INTERACTIVE)
0ad477db
RS
736 {
737 if (first)
738 prepare_menu_bars ();
739 first = 0;
740 redisplay_preserve_echo_area ();
741 }
80856e74
JB
742 immediate_quit = 1;
743 QUIT;
744 }
60558b19 745 give_up: ;
80856e74 746
bbd29cfe
KH
747 Vlast_coding_system_used = process_coding.symbol;
748
3b440bb5
EZ
749 /* If the caller required, let the buffer inherit the
750 coding-system used to decode the process output. */
751 if (inherit_process_coding_system)
752 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
753 make_number (total_read));
754 }
755
80856e74
JB
756 /* Wait for it to terminate, unless it already has. */
757 wait_for_termination (pid);
758
759 immediate_quit = 0;
760
761 set_buffer_internal (old);
762
37d54121
RS
763 /* Don't kill any children that the subprocess may have left behind
764 when exiting. */
765 call_process_exited = 1;
766
80856e74
JB
767 unbind_to (count, Qnil);
768
80856e74
JB
769 if (synch_process_death)
770 return build_string (synch_process_death);
771 return make_number (synch_process_retcode);
772}
773#endif
774\f
9fefd2ba 775static Lisp_Object
80856e74
JB
776delete_temp_file (name)
777 Lisp_Object name;
778{
2e3dc201 779 /* Use Fdelete_file (indirectly) because that runs a file name handler.
59750d69 780 We did that when writing the file, so we should do so when deleting. */
2e3dc201 781 internal_delete_file (name);
80856e74
JB
782}
783
784DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
785 3, MANY, 0,
786 "Send text from START to END to a synchronous process running PROGRAM.\n\
edf496dd 787The remaining arguments are optional.\n\
80856e74 788Delete the text if fourth arg DELETE is non-nil.\n\
39eaa782 789\n\
80856e74
JB
790Insert output in BUFFER before point; t means current buffer;\n\
791 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
39eaa782
RS
792BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
793REAL-BUFFER says what to do with standard output, as above,\n\
794while STDERR-FILE says what to do with standard error in the child.\n\
795STDERR-FILE may be nil (discard standard error output),\n\
796t (mix it with ordinary output), or a file name string.\n\
797\n\
80856e74
JB
798Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
799Remaining args are passed to PROGRAM at startup as command args.\n\
39eaa782
RS
800\n\
801If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
802Otherwise it waits for PROGRAM to terminate\n\
e576cab4 803and returns a numeric exit status or a signal description string.\n\
d177f194 804If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
80856e74
JB
805 (nargs, args)
806 int nargs;
807 register Lisp_Object *args;
808{
39323a7e
KH
809 struct gcpro gcpro1;
810 Lisp_Object filename_string;
811 register Lisp_Object start, end;
d3e81d0a 812 int count = specpdl_ptr - specpdl;
08ee4e87 813 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
32d08644
KH
814 Lisp_Object coding_systems = Qt;
815 Lisp_Object val, *args2;
816 int i;
bad95d8f 817#ifdef DOS_NT
7e6c2178 818 char *tempfile;
7e6c2178
RS
819 char *outf = '\0';
820
8a52365c
EZ
821 if ((outf = egetenv ("TMPDIR"))
822 || (outf = egetenv ("TMP"))
823 || (outf = egetenv ("TEMP")))
7e6c2178
RS
824 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
825 else
826 {
827 tempfile = alloca (20);
828 *tempfile = '\0';
829 }
0774fcf8 830 if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1]))
7e6c2178 831 strcat (tempfile, "/");
5711b547
RS
832 if ('/' == DIRECTORY_SEP)
833 dostounix_filename (tempfile);
834 else
835 unixtodos_filename (tempfile);
0774fcf8
RS
836#ifdef WINDOWSNT
837 strcat (tempfile, "emXXXXXX");
838#else
7e6c2178 839 strcat (tempfile, "detmp.XXX");
0774fcf8 840#endif
bad95d8f 841#else /* not DOS_NT */
fc932ac6 842 char *tempfile = (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern)) + 1);
8abd035b 843 bcopy (XSTRING (Vtemp_file_name_pattern)->data, tempfile,
fc932ac6 844 STRING_BYTES (XSTRING (Vtemp_file_name_pattern)) + 1);
bad95d8f 845#endif /* not DOS_NT */
7e6c2178 846
80856e74
JB
847 mktemp (tempfile);
848
849 filename_string = build_string (tempfile);
39323a7e 850 GCPRO1 (filename_string);
80856e74
JB
851 start = args[0];
852 end = args[1];
32d08644 853 /* Decide coding-system of the contents of the temporary file. */
91489411
RS
854 if (!NILP (Vcoding_system_for_write))
855 val = Vcoding_system_for_write;
856 else if (NILP (current_buffer->enable_multibyte_characters))
32d08644
KH
857 val = Qnil;
858 else
beacaab3 859 {
91489411
RS
860 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
861 args2[0] = Qcall_process_region;
862 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
863 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
864 if (CONSP (coding_systems))
865 val = XCONS (coding_systems)->cdr;
866 else if (CONSP (Vdefault_process_coding_system))
867 val = XCONS (Vdefault_process_coding_system)->cdr;
beacaab3 868 else
91489411 869 val = Qnil;
beacaab3 870 }
32d08644 871
168afdaa
RS
872 {
873 int count1 = specpdl_ptr - specpdl;
874
875 specbind (intern ("coding-system-for-write"), val);
876 Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil);
877
878 unbind_to (count1, Qnil);
879 }
91489411
RS
880
881 /* Note that Fcall_process takes care of binding
882 coding-system-for-read. */
093650fe 883
80856e74
JB
884 record_unwind_protect (delete_temp_file, filename_string);
885
edf496dd 886 if (nargs > 3 && !NILP (args[3]))
80856e74
JB
887 Fdelete_region (start, end);
888
edf496dd
KH
889 if (nargs > 3)
890 {
891 args += 2;
892 nargs -= 2;
893 }
894 else
895 {
896 args[0] = args[2];
897 nargs = 2;
898 }
899 args[1] = filename_string;
80856e74 900
edf496dd 901 RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs, args)));
80856e74
JB
902}
903\f
904#ifndef VMS /* VMS version is in vmsproc.c. */
905
dfcf069d
AS
906static int relocate_fd ();
907
80856e74
JB
908/* This is the last thing run in a newly forked inferior
909 either synchronous or asynchronous.
910 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
911 Initialize inferior's priority, pgrp, connected dir and environment.
912 then exec another program based on new_argv.
913
914 This function may change environ for the superior process.
915 Therefore, the superior process must save and restore the value
916 of environ around the vfork and the call to this function.
917
80856e74 918 SET_PGRP is nonzero if we should put the subprocess into a separate
e576cab4
JB
919 process group.
920
921 CURRENT_DIR is an elisp string giving the path of the current
922 directory the subprocess should have. Since we can't really signal
923 a decent error from within the child, this should be verified as an
924 executable directory by the parent. */
80856e74 925
dfcf069d 926int
e576cab4 927child_setup (in, out, err, new_argv, set_pgrp, current_dir)
80856e74
JB
928 int in, out, err;
929 register char **new_argv;
80856e74 930 int set_pgrp;
e576cab4 931 Lisp_Object current_dir;
80856e74 932{
e576cab4 933 char **env;
7fcf7f05 934 char *pwd_var;
bad95d8f
RS
935#ifdef WINDOWSNT
936 int cpid;
4252a4bd 937 HANDLE handles[3];
bad95d8f 938#endif /* WINDOWSNT */
e576cab4 939
33abe2d9 940 int pid = getpid ();
80856e74 941
68d10241 942#ifdef SET_EMACS_PRIORITY
4f0b9d49
JB
943 {
944 extern int emacs_priority;
945
68d10241
RS
946 if (emacs_priority < 0)
947 nice (- emacs_priority);
4f0b9d49 948 }
5b633aeb 949#endif
80856e74
JB
950
951#ifdef subprocesses
952 /* Close Emacs's descriptors that this process should not have. */
953 close_process_descs ();
954#endif
c17c4250
EZ
955 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
956 we will lose if we call close_load_descs here. */
957#ifndef DOS_NT
4458cebe 958 close_load_descs ();
c17c4250 959#endif
80856e74
JB
960
961 /* Note that use of alloca is always safe here. It's obvious for systems
962 that do not have true vfork or that have true (stack) alloca.
963 If using vfork and C_ALLOCA it is safe because that changes
964 the superior's static variables as if the superior had done alloca
965 and will be cleaned up in the usual way. */
e576cab4 966 {
7fcf7f05 967 register char *temp;
e576cab4 968 register int i;
77d78be1 969
fc932ac6 970 i = STRING_BYTES (XSTRING (current_dir));
7fcf7f05
RS
971 pwd_var = (char *) alloca (i + 6);
972 temp = pwd_var + 4;
973 bcopy ("PWD=", pwd_var, 4);
e576cab4 974 bcopy (XSTRING (current_dir)->data, temp, i);
bad95d8f 975 if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP;
e576cab4
JB
976 temp[i] = 0;
977
c17c4250 978#ifndef DOS_NT
e576cab4
JB
979 /* We can't signal an Elisp error here; we're in a vfork. Since
980 the callers check the current directory before forking, this
981 should only return an error if the directory's permissions
982 are changed between the check and this chdir, but we should
983 at least check. */
984 if (chdir (temp) < 0)
20b25e46 985 _exit (errno);
b4c7684c 986#endif
7fcf7f05 987
c17c4250
EZ
988#ifdef DOS_NT
989 /* Get past the drive letter, so that d:/ is left alone. */
990 if (i > 2 && IS_DEVICE_SEP (temp[1]) && IS_DIRECTORY_SEP (temp[2]))
991 {
992 temp += 2;
993 i -= 2;
994 }
995#endif
996
7fcf7f05 997 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
bad95d8f 998 while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1]))
7fcf7f05 999 temp[--i] = 0;
e576cab4 1000 }
80856e74 1001
80856e74
JB
1002 /* Set `env' to a vector of the strings in Vprocess_environment. */
1003 {
1004 register Lisp_Object tem;
1005 register char **new_env;
1006 register int new_length;
1007
1008 new_length = 0;
1009 for (tem = Vprocess_environment;
d50d3dc8 1010 CONSP (tem) && STRINGP (XCONS (tem)->car);
80856e74
JB
1011 tem = XCONS (tem)->cdr)
1012 new_length++;
1013
7fcf7f05
RS
1014 /* new_length + 2 to include PWD and terminating 0. */
1015 env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *));
1016
1017 /* If we have a PWD envvar, pass one down,
1018 but with corrected value. */
1019 if (getenv ("PWD"))
1020 *new_env++ = pwd_var;
80856e74 1021
cd9565ba 1022 /* Copy the Vprocess_environment strings into new_env. */
80856e74 1023 for (tem = Vprocess_environment;
d50d3dc8 1024 CONSP (tem) && STRINGP (XCONS (tem)->car);
80856e74 1025 tem = XCONS (tem)->cdr)
cd9565ba
RS
1026 {
1027 char **ep = env;
1028 char *string = (char *) XSTRING (XCONS (tem)->car)->data;
1029 /* See if this string duplicates any string already in the env.
1030 If so, don't put it in.
1031 When an env var has multiple definitions,
1032 we keep the definition that comes first in process-environment. */
1033 for (; ep != new_env; ep++)
1034 {
1035 char *p = *ep, *q = string;
1036 while (1)
1037 {
1038 if (*q == 0)
1039 /* The string is malformed; might as well drop it. */
1040 goto duplicate;
1041 if (*q != *p)
1042 break;
1043 if (*q == '=')
1044 goto duplicate;
1045 p++, q++;
1046 }
1047 }
1048 *new_env++ = string;
1049 duplicate: ;
1050 }
80856e74
JB
1051 *new_env = 0;
1052 }
bad95d8f
RS
1053#ifdef WINDOWSNT
1054 prepare_standard_handles (in, out, err, handles);
b4c7684c 1055 set_process_dir (XSTRING (current_dir)->data);
bad95d8f 1056#else /* not WINDOWSNT */
426b37ae
JB
1057 /* Make sure that in, out, and err are not actually already in
1058 descriptors zero, one, or two; this could happen if Emacs is
7e6c2178 1059 started with its standard in, out, or error closed, as might
426b37ae 1060 happen under X. */
f29f9e4a
RS
1061 {
1062 int oin = in, oout = out;
1063
1064 /* We have to avoid relocating the same descriptor twice! */
1065
1066 in = relocate_fd (in, 3);
1067
1068 if (out == oin)
1069 out = in;
1070 else
3e9367e7 1071 out = relocate_fd (out, 3);
f29f9e4a
RS
1072
1073 if (err == oin)
1074 err = in;
1075 else if (err == oout)
1076 err = out;
1077 else
3e9367e7 1078 err = relocate_fd (err, 3);
f29f9e4a 1079 }
426b37ae 1080
c17c4250 1081#ifndef MSDOS
80856e74
JB
1082 close (0);
1083 close (1);
1084 close (2);
1085
1086 dup2 (in, 0);
1087 dup2 (out, 1);
1088 dup2 (err, 2);
1089 close (in);
1090 close (out);
1091 close (err);
c17c4250 1092#endif /* not MSDOS */
bad95d8f 1093#endif /* not WINDOWSNT */
80856e74 1094
6b2cd868 1095#if defined(USG) && !defined(BSD_PGRPS)
fdba8590 1096#ifndef SETPGRP_RELEASES_CTTY
e576cab4 1097 setpgrp (); /* No arguments but equivalent in this case */
fdba8590 1098#endif
e576cab4
JB
1099#else
1100 setpgrp (pid, pid);
1101#endif /* USG */
a129418f
RS
1102 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1103 EMACS_SET_TTY_PGRP (0, &pid);
80856e74
JB
1104
1105#ifdef vipc
1106 something missing here;
1107#endif /* vipc */
1108
c17c4250
EZ
1109#ifdef MSDOS
1110 pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env);
1111 if (pid == -1)
1112 /* An error occurred while trying to run the subprocess. */
1113 report_file_error ("Spawning child process", Qnil);
1114 return pid;
1115#else /* not MSDOS */
bad95d8f
RS
1116#ifdef WINDOWSNT
1117 /* Spawn the child. (See ntproc.c:Spawnve). */
1118 cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
db77d785 1119 reset_standard_handles (in, out, err, handles);
ff27bfbe
KH
1120 if (cpid == -1)
1121 /* An error occurred while trying to spawn the process. */
1122 report_file_error ("Spawning child process", Qnil);
bad95d8f
RS
1123 return cpid;
1124#else /* not WINDOWSNT */
80856e74
JB
1125 /* execvp does not accept an environment arg so the only way
1126 to pass this environment is to set environ. Our caller
1127 is responsible for restoring the ambient value of environ. */
1128 environ = env;
1129 execvp (new_argv[0], new_argv);
1130
f040c0ba 1131 write (1, "Can't exec program: ", 20);
80856e74 1132 write (1, new_argv[0], strlen (new_argv[0]));
d20b8af6 1133 write (1, "\n", 1);
80856e74 1134 _exit (1);
bad95d8f 1135#endif /* not WINDOWSNT */
7e6c2178 1136#endif /* not MSDOS */
80856e74
JB
1137}
1138
a3833dfe 1139/* Move the file descriptor FD so that its number is not less than MINFD.
426b37ae 1140 If the file descriptor is moved at all, the original is freed. */
dfcf069d 1141static int
a3833dfe
KH
1142relocate_fd (fd, minfd)
1143 int fd, minfd;
426b37ae 1144{
a3833dfe 1145 if (fd >= minfd)
426b37ae
JB
1146 return fd;
1147 else
1148 {
1149 int new = dup (fd);
1150 if (new == -1)
1151 {
20c018a0 1152 char *message1 = "Error while setting up child: ";
826c56ac 1153 char *errmessage = strerror (errno);
20c018a0
JB
1154 char *message2 = "\n";
1155 write (2, message1, strlen (message1));
826c56ac 1156 write (2, errmessage, strlen (errmessage));
20c018a0 1157 write (2, message2, strlen (message2));
426b37ae
JB
1158 _exit (1);
1159 }
1160 /* Note that we hold the original FD open while we recurse,
1161 to guarantee we'll get a new FD if we need it. */
a3833dfe 1162 new = relocate_fd (new, minfd);
426b37ae
JB
1163 close (fd);
1164 return new;
1165 }
1166}
1167
012c6fcb
JA
1168static int
1169getenv_internal (var, varlen, value, valuelen)
1170 char *var;
1171 int varlen;
1172 char **value;
1173 int *valuelen;
1174{
1175 Lisp_Object scan;
1176
1177 for (scan = Vprocess_environment; CONSP (scan); scan = XCONS (scan)->cdr)
1178 {
c1350752
KH
1179 Lisp_Object entry;
1180
1181 entry = XCONS (scan)->car;
d50d3dc8 1182 if (STRINGP (entry)
fc932ac6 1183 && STRING_BYTES (XSTRING (entry)) > varlen
012c6fcb 1184 && XSTRING (entry)->data[varlen] == '='
bad95d8f
RS
1185#ifdef WINDOWSNT
1186 /* NT environment variables are case insensitive. */
a9971c6d 1187 && ! strnicmp (XSTRING (entry)->data, var, varlen)
bad95d8f 1188#else /* not WINDOWSNT */
a9971c6d 1189 && ! bcmp (XSTRING (entry)->data, var, varlen)
bad95d8f 1190#endif /* not WINDOWSNT */
a9971c6d 1191 )
012c6fcb
JA
1192 {
1193 *value = (char *) XSTRING (entry)->data + (varlen + 1);
fc932ac6 1194 *valuelen = STRING_BYTES (XSTRING (entry)) - (varlen + 1);
012c6fcb
JA
1195 return 1;
1196 }
1197 }
1198
1199 return 0;
1200}
1201
0ad477db 1202DEFUN ("getenv", Fgetenv, Sgetenv, 1, 1, 0,
012c6fcb
JA
1203 "Return the value of environment variable VAR, as a string.\n\
1204VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
1205This function consults the variable ``process-environment'' for its value.")
1206 (var)
1207 Lisp_Object var;
1208{
1209 char *value;
1210 int valuelen;
1211
1212 CHECK_STRING (var, 0);
fc932ac6 1213 if (getenv_internal (XSTRING (var)->data, STRING_BYTES (XSTRING (var)),
012c6fcb
JA
1214 &value, &valuelen))
1215 return make_string (value, valuelen);
1216 else
1217 return Qnil;
1218}
1219
1220/* A version of getenv that consults process_environment, easily
e576cab4 1221 callable from C. */
012c6fcb
JA
1222char *
1223egetenv (var)
e576cab4 1224 char *var;
012c6fcb
JA
1225{
1226 char *value;
1227 int valuelen;
1228
1229 if (getenv_internal (var, strlen (var), &value, &valuelen))
1230 return value;
1231 else
1232 return 0;
1233}
1234
80856e74
JB
1235#endif /* not VMS */
1236\f
8de15d69 1237/* This is run before init_cmdargs. */
7e6c2178 1238
dfcf069d 1239void
8de15d69
RS
1240init_callproc_1 ()
1241{
1242 char *data_dir = egetenv ("EMACSDATA");
35a2f4b8
KH
1243 char *doc_dir = egetenv ("EMACSDOC");
1244
8de15d69 1245 Vdata_directory
7e6c2178 1246 = Ffile_name_as_directory (build_string (data_dir ? data_dir
8de15d69 1247 : PATH_DATA));
35a2f4b8
KH
1248 Vdoc_directory
1249 = Ffile_name_as_directory (build_string (doc_dir ? doc_dir
1250 : PATH_DOC));
9453ea7b 1251
e576cab4 1252 /* Check the EMACSPATH environment variable, defaulting to the
57bda87a 1253 PATH_EXEC path from epaths.h. */
e576cab4 1254 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
80856e74
JB
1255 Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
1256 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
8de15d69
RS
1257}
1258
e17f7533 1259/* This is run after init_cmdargs, when Vinstallation_directory is valid. */
8de15d69 1260
dfcf069d 1261void
8de15d69
RS
1262init_callproc ()
1263{
1264 char *data_dir = egetenv ("EMACSDATA");
1265
1266 register char * sh;
1267 Lisp_Object tempdir;
1268
9cc4fad5 1269 if (!NILP (Vinstallation_directory))
8de15d69 1270 {
05630743
RS
1271 /* Add to the path the lib-src subdir of the installation dir. */
1272 Lisp_Object tem;
1273 tem = Fexpand_file_name (build_string ("lib-src"),
1274 Vinstallation_directory);
bad95d8f 1275#ifndef DOS_NT
1a6640ec 1276 /* MSDOS uses wrapped binaries, so don't do this. */
0fa248bc
RS
1277 if (NILP (Fmember (tem, Vexec_path)))
1278 Vexec_path = nconc2 (Vexec_path, Fcons (tem, Qnil));
1279
1280 Vexec_directory = Ffile_name_as_directory (tem);
bad95d8f 1281#endif /* not DOS_NT */
8de15d69 1282
e17f7533
RS
1283 /* Maybe use ../etc as well as ../lib-src. */
1284 if (data_dir == 0)
1285 {
1286 tem = Fexpand_file_name (build_string ("etc"),
1287 Vinstallation_directory);
1288 Vdoc_directory = Ffile_name_as_directory (tem);
8de15d69
RS
1289 }
1290 }
7e933683
RS
1291
1292 /* Look for the files that should be in etc. We don't use
1293 Vinstallation_directory, because these files are never installed
e17f7533 1294 near the executable, and they are never in the build
7e933683
RS
1295 directory when that's different from the source directory.
1296
1297 Instead, if these files are not in the nominal place, we try the
1298 source directory. */
1299 if (data_dir == 0)
1300 {
1301 Lisp_Object tem, tem1, newdir;
1302
1303 tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory);
1304 tem1 = Ffile_exists_p (tem);
1305 if (NILP (tem1))
1306 {
1307 newdir = Fexpand_file_name (build_string ("../etc/"),
1308 build_string (PATH_DUMPLOADSEARCH));
1309 tem = Fexpand_file_name (build_string ("GNU"), newdir);
1310 tem1 = Ffile_exists_p (tem);
1311 if (!NILP (tem1))
1312 Vdata_directory = newdir;
1313 }
1314 }
80856e74 1315
d883eb62
RS
1316#ifndef CANNOT_DUMP
1317 if (initialized)
1318#endif
1319 {
1320 tempdir = Fdirectory_file_name (Vexec_directory);
1321 if (access (XSTRING (tempdir)->data, 0) < 0)
1322 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1323 Vexec_directory);
1324 }
80856e74 1325
e576cab4
JB
1326 tempdir = Fdirectory_file_name (Vdata_directory);
1327 if (access (XSTRING (tempdir)->data, 0) < 0)
76d5c6cf
RS
1328 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1329 Vdata_directory);
e576cab4 1330
80856e74
JB
1331#ifdef VMS
1332 Vshell_file_name = build_string ("*dcl*");
1333#else
e576cab4 1334 sh = (char *) getenv ("SHELL");
80856e74
JB
1335 Vshell_file_name = build_string (sh ? sh : "/bin/sh");
1336#endif
8abd035b
RS
1337
1338#ifdef VMS
1339 Vtemp_file_name_pattern = build_string ("tmp:emacsXXXXXX.");
1340#else
1341 if (getenv ("TMPDIR"))
1342 {
1343 char *dir = getenv ("TMPDIR");
1344 Vtemp_file_name_pattern
1345 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1346 build_string (dir));
1347 }
1348 else
1349 Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX");
1350#endif
9fefd2ba
JB
1351}
1352
dfcf069d 1353void
9fefd2ba
JB
1354set_process_environment ()
1355{
1356 register char **envp;
80856e74 1357
80856e74
JB
1358 Vprocess_environment = Qnil;
1359#ifndef CANNOT_DUMP
1360 if (initialized)
1361#endif
1362 for (envp = environ; *envp; envp++)
1363 Vprocess_environment = Fcons (build_string (*envp),
1364 Vprocess_environment);
80856e74
JB
1365}
1366
dfcf069d 1367void
80856e74
JB
1368syms_of_callproc ()
1369{
bad95d8f 1370#ifdef DOS_NT
093650fe
RS
1371 Qbuffer_file_type = intern ("buffer-file-type");
1372 staticpro (&Qbuffer_file_type);
bad95d8f 1373#endif /* DOS_NT */
7e6c2178 1374
80856e74
JB
1375 DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
1376 "*File name to load inferior shells from.\n\
1377Initialized from the SHELL environment variable.");
1378
1379 DEFVAR_LISP ("exec-path", &Vexec_path,
1380 "*List of directories to search programs to run in subprocesses.\n\
1381Each element is a string (directory name) or nil (try default directory).");
1382
1383 DEFVAR_LISP ("exec-directory", &Vexec_directory,
57ca9855
RS
1384 "Directory for executables for Emacs to invoke.\n\
1385More generally, this includes any architecture-dependent files\n\
1386that are built and installed from the Emacs distribution.");
e576cab4
JB
1387
1388 DEFVAR_LISP ("data-directory", &Vdata_directory,
57ca9855
RS
1389 "Directory of machine-independent files that come with GNU Emacs.\n\
1390These are files intended for Emacs to use while it runs.");
80856e74 1391
35a2f4b8
KH
1392 DEFVAR_LISP ("doc-directory", &Vdoc_directory,
1393 "Directory containing the DOC file that comes with GNU Emacs.\n\
1394This is usually the same as data-directory.");
1395
ed61592a
JB
1396 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory,
1397 "For internal use by the build procedure only.\n\
1398This is the name of the directory in which the build procedure installed\n\
1399Emacs's info files; the default value for Info-default-directory-list\n\
1400includes this.");
1401 Vconfigure_info_directory = build_string (PATH_INFO);
1402
8abd035b
RS
1403 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern,
1404 "Pattern for making names for temporary files.\n\
1405This is used by `call-process-region'.");
0537ec48 1406 /* This variable is initialized in init_callproc. */
8abd035b 1407
80856e74 1408 DEFVAR_LISP ("process-environment", &Vprocess_environment,
e576cab4
JB
1409 "List of environment variables for subprocesses to inherit.\n\
1410Each element should be a string of the form ENVVARNAME=VALUE.\n\
1411The environment which Emacs inherits is placed in this variable\n\
1412when Emacs starts.");
80856e74
JB
1413
1414#ifndef VMS
1415 defsubr (&Scall_process);
012c6fcb 1416 defsubr (&Sgetenv);
986ffb24 1417#endif
e576cab4 1418 defsubr (&Scall_process_region);
80856e74 1419}