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