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