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