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