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