(init_display): Treat null string DISPLAY var like not set.
[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
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include <signal.h>
e576cab4 22#include <errno.h>
80856e74 23
18160b98 24#include <config.h>
565620a5 25#include <stdio.h>
80856e74 26
426b37ae 27extern int errno;
826c56ac 28extern char *strerror ();
426b37ae 29
80856e74
JB
30/* Define SIGCHLD as an alias for SIGCLD. */
31
32#if !defined (SIGCHLD) && defined (SIGCLD)
33#define SIGCHLD SIGCLD
34#endif /* SIGCLD */
35
36#include <sys/types.h>
88a64fef 37
80856e74
JB
38#include <sys/file.h>
39#ifdef USG5
472e83fe 40#define INCLUDED_FCNTL
80856e74
JB
41#include <fcntl.h>
42#endif
43
bad95d8f
RS
44#ifdef WINDOWSNT
45#define NOMINMAX
46#include <windows.h>
47#include <stdlib.h> /* for proper declaration of environ */
48#include <fcntl.h>
49#include "nt.h"
50#define _P_NOWAIT 1 /* from process.h */
51#endif
52
7e6c2178 53#ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
00353d4f 54#include "msdos.h"
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"
2a6b3537 73#include <paths.h>
80856e74 74#include "process.h"
d177f194 75#include "syssignal.h"
a129418f 76#include "systty.h"
80856e74
JB
77
78#ifdef VMS
79extern noshare char **environ;
80#else
81extern char **environ;
82#endif
83
84#define max(a, b) ((a) > (b) ? (a) : (b))
85
bad95d8f 86#ifdef DOS_NT
093650fe
RS
87/* When we are starting external processes we need to know whether they
88 take binary input (no conversion) or text input (\n is converted to
89 \r\n). Similar for output: if newlines are written as \r\n then it's
90 text process output, otherwise it's binary. */
91Lisp_Object Vbinary_process_input;
92Lisp_Object Vbinary_process_output;
bad95d8f 93#endif /* DOS_NT */
7e6c2178 94
35a2f4b8 95Lisp_Object Vexec_path, Vexec_directory, Vdata_directory, Vdoc_directory;
ed61592a 96Lisp_Object Vconfigure_info_directory;
80856e74
JB
97
98Lisp_Object Vshell_file_name;
99
80856e74 100Lisp_Object Vprocess_environment;
80856e74 101
bad95d8f 102#ifdef DOS_NT
093650fe 103Lisp_Object Qbuffer_file_type;
bad95d8f 104#endif /* DOS_NT */
093650fe 105
80856e74
JB
106/* True iff we are about to fork off a synchronous process or if we
107 are waiting for it. */
108int synch_process_alive;
109
110/* Nonzero => this is a string explaining death of synchronous subprocess. */
111char *synch_process_death;
112
113/* If synch_process_death is zero,
114 this is exit code of synchronous subprocess. */
115int synch_process_retcode;
8de15d69
RS
116
117extern Lisp_Object Vdoc_file_name;
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
JB
206 int count = specpdl_ptr - specpdl;
207 register unsigned char **new_argv
208 = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
209 struct buffer *old = current_buffer;
39eaa782
RS
210 /* File to use for stderr in the child.
211 t means use same as standard output. */
212 Lisp_Object error_file;
7e6c2178
RS
213#ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
214 char *outf, *tempfile;
215 int outfilefd;
216#endif
80856e74
JB
217#if 0
218 int mask;
219#endif
80856e74
JB
220 CHECK_STRING (args[0], 0);
221
39eaa782
RS
222 error_file = Qt;
223
7e6c2178
RS
224#ifndef subprocesses
225 /* Without asynchronous processes we cannot have BUFFER == 0. */
d50d3dc8 226 if (nargs >= 3 && INTEGERP (args[2]))
7e6c2178
RS
227 error ("Operating system cannot handle asynchronous subprocesses");
228#endif /* subprocesses */
229
e576cab4
JB
230 if (nargs >= 2 && ! NILP (args[1]))
231 {
232 infile = Fexpand_file_name (args[1], current_buffer->directory);
233 CHECK_STRING (infile, 1);
234 }
80856e74 235 else
5437e9f9 236 infile = build_string (NULL_DEVICE);
80856e74 237
e576cab4
JB
238 if (nargs >= 3)
239 {
39eaa782
RS
240 buffer = args[2];
241
242 /* If BUFFER is a list, its meaning is
243 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
244 if (CONSP (buffer))
245 {
246 if (CONSP (XCONS (buffer)->cdr))
d20b8af6
RS
247 error_file = Fexpand_file_name (XCONS (XCONS (buffer)->cdr)->car,
248 Qnil);
39eaa782
RS
249 buffer = XCONS (buffer)->car;
250 }
044512ed 251
39eaa782
RS
252 if (!(EQ (buffer, Qnil)
253 || EQ (buffer, Qt)
254 || XFASTINT (buffer) == 0))
e576cab4 255 {
39eaa782
RS
256 Lisp_Object spec_buffer;
257 spec_buffer = buffer;
258 buffer = Fget_buffer (buffer);
259 /* Mention the buffer name for a better error message. */
260 if (NILP (buffer))
261 CHECK_BUFFER (spec_buffer, 2);
e576cab4
JB
262 CHECK_BUFFER (buffer, 2);
263 }
264 }
265 else
266 buffer = Qnil;
80856e74 267
58616e67
JB
268 /* Make sure that the child will be able to chdir to the current
269 buffer's current directory, or its unhandled equivalent. We
270 can't just have the child check for an error when it does the
271 chdir, since it's in a vfork.
272
273 We have to GCPRO around this because Fexpand_file_name,
274 Funhandled_file_name_directory, and Ffile_accessible_directory_p
275 might call a file name handling function. The argument list is
276 protected by the caller, so all we really have to worry about is
277 buffer. */
278 {
279 struct gcpro gcpro1, gcpro2, gcpro3;
280
281 current_dir = current_buffer->directory;
282
283 GCPRO3 (infile, buffer, current_dir);
284
c52b0b34
KH
285 current_dir
286 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
287 Qnil);
58616e67
JB
288 if (NILP (Ffile_accessible_directory_p (current_dir)))
289 report_file_error ("Setting current directory",
290 Fcons (current_buffer->directory, Qnil));
291
292 UNGCPRO;
293 }
294
e576cab4 295 display = nargs >= 4 ? args[3] : Qnil;
80856e74 296
e576cab4 297 filefd = open (XSTRING (infile)->data, O_RDONLY, 0);
80856e74
JB
298 if (filefd < 0)
299 {
e576cab4 300 report_file_error ("Opening process input file", Fcons (infile, Qnil));
80856e74
JB
301 }
302 /* Search for program; barf if not found. */
c52b0b34
KH
303 {
304 struct gcpro gcpro1;
305
306 GCPRO1 (current_dir);
307 openp (Vexec_path, args[0], EXEC_SUFFIXES, &path, 1);
308 UNGCPRO;
309 }
012c6fcb 310 if (NILP (path))
80856e74
JB
311 {
312 close (filefd);
313 report_file_error ("Searching for program", Fcons (args[0], Qnil));
314 }
315 new_argv[0] = XSTRING (path)->data;
c52b0b34
KH
316 {
317 register int i;
318 for (i = 4; i < nargs; i++)
319 {
320 CHECK_STRING (args[i], i);
321 new_argv[i - 3] = XSTRING (args[i])->data;
322 }
323 new_argv[i - 3] = 0;
324 }
80856e74 325
7e6c2178 326#ifdef MSDOS /* MW, July 1993 */
7e6c2178
RS
327 if ((outf = egetenv ("TMP")) || (outf = egetenv ("TEMP")))
328 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
329 else
330 {
331 tempfile = alloca (20);
332 *tempfile = '\0';
333 }
334 dostounix_filename (tempfile);
335 if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/')
336 strcat (tempfile, "/");
337 strcat (tempfile, "detmp.XXX");
338 mktemp (tempfile);
339
340 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
341 if (outfilefd < 0)
342 {
343 close (filefd);
344 report_file_error ("Opening process output file", Fcons (tempfile, Qnil));
345 }
2610078a 346 fd[1] = outfilefd;
7e6c2178
RS
347#endif
348
d50d3dc8 349 if (INTEGERP (buffer))
5437e9f9 350 fd[1] = open (NULL_DEVICE, O_WRONLY), fd[0] = -1;
80856e74
JB
351 else
352 {
7e6c2178 353#ifndef MSDOS
bad95d8f
RS
354#ifdef WINDOWSNT
355 pipe_with_inherited_out (fd);
356#else /* not WINDOWSNT */
80856e74 357 pipe (fd);
bad95d8f 358#endif /* not WINDOWSNT */
7e6c2178 359#endif
80856e74
JB
360#if 0
361 /* Replaced by close_process_descs */
362 set_exclusive_use (fd[0]);
363#endif
364 }
365
366 {
367 /* child_setup must clobber environ in systems with true vfork.
368 Protect it from permanent change. */
369 register char **save_environ = environ;
370 register int fd1 = fd[1];
39eaa782 371 int fd_error = fd1;
80856e74
JB
372
373#if 0 /* Some systems don't have sigblock. */
e065a56e 374 mask = sigblock (sigmask (SIGCHLD));
80856e74
JB
375#endif
376
377 /* Record that we're about to create a synchronous process. */
378 synch_process_alive = 1;
379
5c03767e
RS
380 /* These vars record information from process termination.
381 Clear them now before process can possibly terminate,
382 to avoid timing error if process terminates soon. */
383 synch_process_death = 0;
384 synch_process_retcode = 0;
385
39eaa782
RS
386 if (NILP (error_file))
387 fd_error = open (NULL_DEVICE, O_WRONLY);
388 else if (STRINGP (error_file))
389 {
390#ifdef DOS_NT
391 fd_error = open (XSTRING (error_file)->data,
392 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
393 S_IREAD | S_IWRITE);
394#else /* not DOS_NT */
395 fd_error = creat (XSTRING (error_file)->data, 0666);
396#endif /* not DOS_NT */
397 }
398
399 if (fd_error < 0)
400 {
401 close (filefd);
402 close (fd[0]);
403 if (fd1 >= 0)
404 close (fd1);
405 report_file_error ("Cannot open", error_file);
406 }
2610078a
KH
407#ifdef MSDOS /* MW, July 1993 */
408 /* ??? Someone who knows MSDOG needs to check whether this properly
409 closes all descriptors that it opens.
410
411 Note that run_msdos_command() actually returns the child process
412 exit status, not its PID, so we assign it to `synch_process_retcode'
413 below. */
414 pid = run_msdos_command (new_argv, current_dir,
415 filefd, outfilefd, fd_error);
39eaa782 416
2610078a
KH
417 /* Record that the synchronous process exited and note its
418 termination status. */
419 synch_process_alive = 0;
420 synch_process_retcode = pid;
421 if (synch_process_retcode < 0) /* means it couldn't be exec'ed */
422 synch_process_death = strerror(errno);
423
424 close (outfilefd);
425 if (fd_error != outfilefd)
426 close (fd_error);
427 fd1 = -1; /* No harm in closing that one! */
428 fd[0] = open (tempfile, NILP (Vbinary_process_output) ? O_TEXT : O_BINARY);
429 if (fd[0] < 0)
430 {
431 unlink (tempfile);
432 close (filefd);
433 report_file_error ("Cannot re-open temporary file", Qnil);
434 }
435#else /* not MSDOS */
bad95d8f 436#ifdef WINDOWSNT
39eaa782 437 pid = child_setup (filefd, fd1, fd_error, new_argv, 0, current_dir);
bad95d8f 438#else /* not WINDOWSNT */
80856e74
JB
439 pid = vfork ();
440
441 if (pid == 0)
442 {
443 if (fd[0] >= 0)
444 close (fd[0]);
5a570e37 445#ifdef USG
80856e74
JB
446 setpgrp ();
447#else
448 setpgrp (pid, pid);
449#endif /* USG */
39eaa782 450 child_setup (filefd, fd1, fd_error, new_argv, 0, current_dir);
80856e74 451 }
bad95d8f 452#endif /* not WINDOWSNT */
2610078a 453#endif /* not MSDOS */
80856e74 454
80856e74
JB
455 environ = save_environ;
456
6b6e798b
RS
457 /* Close most of our fd's, but not fd[0]
458 since we will use that to read input from. */
80856e74 459 close (filefd);
7e6c2178
RS
460 if (fd1 >= 0)
461 close (fd1);
80856e74
JB
462 }
463
464 if (pid < 0)
465 {
6b6e798b
RS
466 if (fd[0] >= 0)
467 close (fd[0]);
80856e74
JB
468 report_file_error ("Doing vfork", Qnil);
469 }
470
d50d3dc8 471 if (INTEGERP (buffer))
80856e74 472 {
6b6e798b
RS
473 if (fd[0] >= 0)
474 close (fd[0]);
80856e74 475#ifndef subprocesses
e576cab4
JB
476 /* If Emacs has been built with asynchronous subprocess support,
477 we don't need to do this, I think because it will then have
478 the facilities for handling SIGCHLD. */
80856e74
JB
479 wait_without_blocking ();
480#endif /* subprocesses */
80856e74
JB
481 return Qnil;
482 }
483
6b6e798b 484 /* Enable sending signal if user quits below. */
37d54121
RS
485 call_process_exited = 0;
486
7e6c2178
RS
487#ifdef MSDOS
488 /* MSDOS needs different cleanup information. */
489 record_unwind_protect (call_process_cleanup,
490 Fcons (make_number (fd[0]), build_string (tempfile)));
491#else
80856e74
JB
492 record_unwind_protect (call_process_cleanup,
493 Fcons (make_number (fd[0]), make_number (pid)));
7e6c2178 494#endif /* not MSDOS */
80856e74
JB
495
496
d50d3dc8 497 if (BUFFERP (buffer))
80856e74
JB
498 Fset_buffer (buffer);
499
500 immediate_quit = 1;
501 QUIT;
502
503 {
504 register int nread;
0ad477db 505 int first = 1;
6e3bfbb2 506 int total_read = 0;
80856e74 507
60558b19 508 while (1)
80856e74 509 {
60558b19
RS
510 /* Repeatedly read until we've filled as much as possible
511 of the buffer size we have. But don't read
8e6208c5 512 less than 1024--save that for the next bufferful. */
60558b19
RS
513
514 nread = 0;
515 while (nread < bufsize - 1024)
00fb3e95 516 {
60558b19
RS
517 int this_read
518 = read (fd[0], bufptr + nread, bufsize - nread);
519
520 if (this_read < 0)
521 goto give_up;
522
523 if (this_read == 0)
524 goto give_up_1;
525
526 nread += this_read;
00fb3e95 527 }
60558b19
RS
528
529 give_up_1:
530
531 /* Now NREAD is the total amount of data in the buffer. */
532 if (nread == 0)
533 break;
534
80856e74 535 immediate_quit = 0;
6e3bfbb2
RS
536 total_read += nread;
537
012c6fcb 538 if (!NILP (buffer))
6e3bfbb2
RS
539 insert (bufptr, nread);
540
541 /* Make the buffer bigger as we continue to read more data,
542 but not past 64k. */
543 if (bufsize < 64 * 1024 && total_read > 32 * bufsize)
544 {
545 bufsize *= 2;
546 bufptr = (char *) alloca (bufsize);
547 }
548
012c6fcb 549 if (!NILP (display) && INTERACTIVE)
0ad477db
RS
550 {
551 if (first)
552 prepare_menu_bars ();
553 first = 0;
554 redisplay_preserve_echo_area ();
555 }
80856e74
JB
556 immediate_quit = 1;
557 QUIT;
558 }
60558b19 559 give_up: ;
80856e74
JB
560 }
561
562 /* Wait for it to terminate, unless it already has. */
563 wait_for_termination (pid);
564
565 immediate_quit = 0;
566
567 set_buffer_internal (old);
568
37d54121
RS
569 /* Don't kill any children that the subprocess may have left behind
570 when exiting. */
571 call_process_exited = 1;
572
80856e74
JB
573 unbind_to (count, Qnil);
574
80856e74
JB
575 if (synch_process_death)
576 return build_string (synch_process_death);
577 return make_number (synch_process_retcode);
578}
579#endif
580\f
9fefd2ba 581static Lisp_Object
80856e74
JB
582delete_temp_file (name)
583 Lisp_Object name;
584{
2e3dc201 585 /* Use Fdelete_file (indirectly) because that runs a file name handler.
59750d69 586 We did that when writing the file, so we should do so when deleting. */
2e3dc201 587 internal_delete_file (name);
80856e74
JB
588}
589
590DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
591 3, MANY, 0,
592 "Send text from START to END to a synchronous process running PROGRAM.\n\
593Delete the text if fourth arg DELETE is non-nil.\n\
39eaa782 594\n\
80856e74
JB
595Insert output in BUFFER before point; t means current buffer;\n\
596 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
39eaa782
RS
597BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
598REAL-BUFFER says what to do with standard output, as above,\n\
599while STDERR-FILE says what to do with standard error in the child.\n\
600STDERR-FILE may be nil (discard standard error output),\n\
601t (mix it with ordinary output), or a file name string.\n\
602\n\
80856e74
JB
603Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
604Remaining args are passed to PROGRAM at startup as command args.\n\
39eaa782
RS
605\n\
606If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
607Otherwise it waits for PROGRAM to terminate\n\
e576cab4 608and returns a numeric exit status or a signal description string.\n\
d177f194 609If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
80856e74
JB
610 (nargs, args)
611 int nargs;
612 register Lisp_Object *args;
613{
39323a7e
KH
614 struct gcpro gcpro1;
615 Lisp_Object filename_string;
616 register Lisp_Object start, end;
bad95d8f 617#ifdef DOS_NT
7e6c2178
RS
618 char *tempfile;
619#else
80856e74 620 char tempfile[20];
7e6c2178 621#endif
80856e74 622 int count = specpdl_ptr - specpdl;
bad95d8f 623#ifdef DOS_NT
7e6c2178
RS
624 char *outf = '\0';
625
626 if ((outf = egetenv ("TMP")) || (outf = egetenv ("TEMP")))
627 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
628 else
629 {
630 tempfile = alloca (20);
631 *tempfile = '\0';
632 }
633 dostounix_filename (tempfile);
0774fcf8 634 if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1]))
7e6c2178 635 strcat (tempfile, "/");
0774fcf8
RS
636#ifdef WINDOWSNT
637 strcat (tempfile, "emXXXXXX");
638#else
7e6c2178 639 strcat (tempfile, "detmp.XXX");
0774fcf8 640#endif
bad95d8f 641#else /* not DOS_NT */
80856e74
JB
642
643#ifdef VMS
644 strcpy (tempfile, "tmp:emacsXXXXXX.");
645#else
646 strcpy (tempfile, "/tmp/emacsXXXXXX");
647#endif
bad95d8f 648#endif /* not DOS_NT */
7e6c2178 649
80856e74
JB
650 mktemp (tempfile);
651
652 filename_string = build_string (tempfile);
39323a7e 653 GCPRO1 (filename_string);
80856e74
JB
654 start = args[0];
655 end = args[1];
bad95d8f 656#ifdef DOS_NT
093650fe 657 specbind (Qbuffer_file_type, Vbinary_process_input);
bc4498bb 658 Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil);
093650fe 659 unbind_to (count, Qnil);
bad95d8f 660#else /* not DOS_NT */
bc4498bb 661 Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil);
bad95d8f 662#endif /* not DOS_NT */
093650fe 663
80856e74
JB
664 record_unwind_protect (delete_temp_file, filename_string);
665
012c6fcb 666 if (!NILP (args[3]))
80856e74
JB
667 Fdelete_region (start, end);
668
669 args[3] = filename_string;
80856e74 670
39323a7e 671 RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs - 2, args + 2)));
80856e74
JB
672}
673\f
674#ifndef VMS /* VMS version is in vmsproc.c. */
675
676/* This is the last thing run in a newly forked inferior
677 either synchronous or asynchronous.
678 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
679 Initialize inferior's priority, pgrp, connected dir and environment.
680 then exec another program based on new_argv.
681
682 This function may change environ for the superior process.
683 Therefore, the superior process must save and restore the value
684 of environ around the vfork and the call to this function.
685
686 ENV is the environment for the subprocess.
687
688 SET_PGRP is nonzero if we should put the subprocess into a separate
e576cab4
JB
689 process group.
690
691 CURRENT_DIR is an elisp string giving the path of the current
692 directory the subprocess should have. Since we can't really signal
693 a decent error from within the child, this should be verified as an
694 executable directory by the parent. */
80856e74 695
e576cab4 696child_setup (in, out, err, new_argv, set_pgrp, current_dir)
80856e74
JB
697 int in, out, err;
698 register char **new_argv;
80856e74 699 int set_pgrp;
e576cab4 700 Lisp_Object current_dir;
80856e74 701{
7e6c2178
RS
702#ifdef MSDOS
703 /* The MSDOS port of gcc cannot fork, vfork, ... so we must call system
704 instead. */
705#else /* not MSDOS */
e576cab4 706 char **env;
7fcf7f05 707 char *pwd_var;
bad95d8f
RS
708#ifdef WINDOWSNT
709 int cpid;
710 HANDLE handles[4];
711#endif /* WINDOWSNT */
e576cab4 712
33abe2d9 713 int pid = getpid ();
80856e74 714
68d10241 715#ifdef SET_EMACS_PRIORITY
4f0b9d49
JB
716 {
717 extern int emacs_priority;
718
68d10241
RS
719 if (emacs_priority < 0)
720 nice (- emacs_priority);
4f0b9d49 721 }
5b633aeb 722#endif
80856e74
JB
723
724#ifdef subprocesses
725 /* Close Emacs's descriptors that this process should not have. */
726 close_process_descs ();
727#endif
4458cebe 728 close_load_descs ();
80856e74
JB
729
730 /* Note that use of alloca is always safe here. It's obvious for systems
731 that do not have true vfork or that have true (stack) alloca.
732 If using vfork and C_ALLOCA it is safe because that changes
733 the superior's static variables as if the superior had done alloca
734 and will be cleaned up in the usual way. */
e576cab4 735 {
7fcf7f05 736 register char *temp;
e576cab4 737 register int i;
77d78be1 738
e576cab4 739 i = XSTRING (current_dir)->size;
7fcf7f05
RS
740 pwd_var = (char *) alloca (i + 6);
741 temp = pwd_var + 4;
742 bcopy ("PWD=", pwd_var, 4);
e576cab4 743 bcopy (XSTRING (current_dir)->data, temp, i);
bad95d8f 744 if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP;
e576cab4
JB
745 temp[i] = 0;
746
747 /* We can't signal an Elisp error here; we're in a vfork. Since
748 the callers check the current directory before forking, this
749 should only return an error if the directory's permissions
750 are changed between the check and this chdir, but we should
751 at least check. */
752 if (chdir (temp) < 0)
20b25e46 753 _exit (errno);
7fcf7f05
RS
754
755 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
bad95d8f 756 while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1]))
7fcf7f05 757 temp[--i] = 0;
e576cab4 758 }
80856e74 759
80856e74
JB
760 /* Set `env' to a vector of the strings in Vprocess_environment. */
761 {
762 register Lisp_Object tem;
763 register char **new_env;
764 register int new_length;
765
766 new_length = 0;
767 for (tem = Vprocess_environment;
d50d3dc8 768 CONSP (tem) && STRINGP (XCONS (tem)->car);
80856e74
JB
769 tem = XCONS (tem)->cdr)
770 new_length++;
771
7fcf7f05
RS
772 /* new_length + 2 to include PWD and terminating 0. */
773 env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *));
774
775 /* If we have a PWD envvar, pass one down,
776 but with corrected value. */
777 if (getenv ("PWD"))
778 *new_env++ = pwd_var;
80856e74 779
cd9565ba 780 /* Copy the Vprocess_environment strings into new_env. */
80856e74 781 for (tem = Vprocess_environment;
d50d3dc8 782 CONSP (tem) && STRINGP (XCONS (tem)->car);
80856e74 783 tem = XCONS (tem)->cdr)
cd9565ba
RS
784 {
785 char **ep = env;
786 char *string = (char *) XSTRING (XCONS (tem)->car)->data;
787 /* See if this string duplicates any string already in the env.
788 If so, don't put it in.
789 When an env var has multiple definitions,
790 we keep the definition that comes first in process-environment. */
791 for (; ep != new_env; ep++)
792 {
793 char *p = *ep, *q = string;
794 while (1)
795 {
796 if (*q == 0)
797 /* The string is malformed; might as well drop it. */
798 goto duplicate;
799 if (*q != *p)
800 break;
801 if (*q == '=')
802 goto duplicate;
803 p++, q++;
804 }
805 }
806 *new_env++ = string;
807 duplicate: ;
808 }
80856e74
JB
809 *new_env = 0;
810 }
bad95d8f
RS
811#ifdef WINDOWSNT
812 prepare_standard_handles (in, out, err, handles);
813#else /* not WINDOWSNT */
426b37ae
JB
814 /* Make sure that in, out, and err are not actually already in
815 descriptors zero, one, or two; this could happen if Emacs is
7e6c2178 816 started with its standard in, out, or error closed, as might
426b37ae 817 happen under X. */
f29f9e4a
RS
818 {
819 int oin = in, oout = out;
820
821 /* We have to avoid relocating the same descriptor twice! */
822
823 in = relocate_fd (in, 3);
824
825 if (out == oin)
826 out = in;
827 else
3e9367e7 828 out = relocate_fd (out, 3);
f29f9e4a
RS
829
830 if (err == oin)
831 err = in;
832 else if (err == oout)
833 err = out;
834 else
3e9367e7 835 err = relocate_fd (err, 3);
f29f9e4a 836 }
426b37ae 837
80856e74
JB
838 close (0);
839 close (1);
840 close (2);
841
842 dup2 (in, 0);
843 dup2 (out, 1);
844 dup2 (err, 2);
845 close (in);
846 close (out);
847 close (err);
bad95d8f 848#endif /* not WINDOWSNT */
80856e74 849
fdba8590
RS
850#ifdef USG
851#ifndef SETPGRP_RELEASES_CTTY
e576cab4 852 setpgrp (); /* No arguments but equivalent in this case */
fdba8590 853#endif
e576cab4
JB
854#else
855 setpgrp (pid, pid);
856#endif /* USG */
a129418f
RS
857 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
858 EMACS_SET_TTY_PGRP (0, &pid);
80856e74
JB
859
860#ifdef vipc
861 something missing here;
862#endif /* vipc */
863
bad95d8f
RS
864#ifdef WINDOWSNT
865 /* Spawn the child. (See ntproc.c:Spawnve). */
866 cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
ff27bfbe
KH
867 if (cpid == -1)
868 /* An error occurred while trying to spawn the process. */
869 report_file_error ("Spawning child process", Qnil);
bad95d8f
RS
870 reset_standard_handles (in, out, err, handles);
871 return cpid;
872#else /* not WINDOWSNT */
80856e74
JB
873 /* execvp does not accept an environment arg so the only way
874 to pass this environment is to set environ. Our caller
875 is responsible for restoring the ambient value of environ. */
876 environ = env;
877 execvp (new_argv[0], new_argv);
878
d20b8af6 879 write (1, "Can't exec program: ", 26);
80856e74 880 write (1, new_argv[0], strlen (new_argv[0]));
d20b8af6 881 write (1, "\n", 1);
80856e74 882 _exit (1);
bad95d8f 883#endif /* not WINDOWSNT */
7e6c2178 884#endif /* not MSDOS */
80856e74
JB
885}
886
426b37ae
JB
887/* Move the file descriptor FD so that its number is not less than MIN.
888 If the file descriptor is moved at all, the original is freed. */
889int
890relocate_fd (fd, min)
891 int fd, min;
892{
893 if (fd >= min)
894 return fd;
895 else
896 {
897 int new = dup (fd);
898 if (new == -1)
899 {
20c018a0 900 char *message1 = "Error while setting up child: ";
826c56ac 901 char *errmessage = strerror (errno);
20c018a0
JB
902 char *message2 = "\n";
903 write (2, message1, strlen (message1));
826c56ac 904 write (2, errmessage, strlen (errmessage));
20c018a0 905 write (2, message2, strlen (message2));
426b37ae
JB
906 _exit (1);
907 }
908 /* Note that we hold the original FD open while we recurse,
909 to guarantee we'll get a new FD if we need it. */
910 new = relocate_fd (new, min);
911 close (fd);
912 return new;
913 }
914}
915
012c6fcb
JA
916static int
917getenv_internal (var, varlen, value, valuelen)
918 char *var;
919 int varlen;
920 char **value;
921 int *valuelen;
922{
923 Lisp_Object scan;
924
925 for (scan = Vprocess_environment; CONSP (scan); scan = XCONS (scan)->cdr)
926 {
c1350752
KH
927 Lisp_Object entry;
928
929 entry = XCONS (scan)->car;
d50d3dc8 930 if (STRINGP (entry)
012c6fcb
JA
931 && XSTRING (entry)->size > varlen
932 && XSTRING (entry)->data[varlen] == '='
bad95d8f
RS
933#ifdef WINDOWSNT
934 /* NT environment variables are case insensitive. */
a9971c6d 935 && ! strnicmp (XSTRING (entry)->data, var, varlen)
bad95d8f 936#else /* not WINDOWSNT */
a9971c6d 937 && ! bcmp (XSTRING (entry)->data, var, varlen)
bad95d8f 938#endif /* not WINDOWSNT */
a9971c6d 939 )
012c6fcb
JA
940 {
941 *value = (char *) XSTRING (entry)->data + (varlen + 1);
942 *valuelen = XSTRING (entry)->size - (varlen + 1);
943 return 1;
944 }
945 }
946
947 return 0;
948}
949
0ad477db 950DEFUN ("getenv", Fgetenv, Sgetenv, 1, 1, 0,
012c6fcb
JA
951 "Return the value of environment variable VAR, as a string.\n\
952VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
953This function consults the variable ``process-environment'' for its value.")
954 (var)
955 Lisp_Object var;
956{
957 char *value;
958 int valuelen;
959
960 CHECK_STRING (var, 0);
961 if (getenv_internal (XSTRING (var)->data, XSTRING (var)->size,
962 &value, &valuelen))
963 return make_string (value, valuelen);
964 else
965 return Qnil;
966}
967
968/* A version of getenv that consults process_environment, easily
e576cab4 969 callable from C. */
012c6fcb
JA
970char *
971egetenv (var)
e576cab4 972 char *var;
012c6fcb
JA
973{
974 char *value;
975 int valuelen;
976
977 if (getenv_internal (var, strlen (var), &value, &valuelen))
978 return value;
979 else
980 return 0;
981}
982
80856e74
JB
983#endif /* not VMS */
984\f
8de15d69 985/* This is run before init_cmdargs. */
7e6c2178 986
8de15d69
RS
987init_callproc_1 ()
988{
989 char *data_dir = egetenv ("EMACSDATA");
35a2f4b8
KH
990 char *doc_dir = egetenv ("EMACSDOC");
991
8de15d69 992 Vdata_directory
7e6c2178 993 = Ffile_name_as_directory (build_string (data_dir ? data_dir
8de15d69 994 : PATH_DATA));
35a2f4b8
KH
995 Vdoc_directory
996 = Ffile_name_as_directory (build_string (doc_dir ? doc_dir
997 : PATH_DOC));
9453ea7b 998
e576cab4
JB
999 /* Check the EMACSPATH environment variable, defaulting to the
1000 PATH_EXEC path from paths.h. */
1001 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
80856e74
JB
1002 Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
1003 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
8de15d69
RS
1004}
1005
e17f7533 1006/* This is run after init_cmdargs, when Vinstallation_directory is valid. */
8de15d69
RS
1007
1008init_callproc ()
1009{
1010 char *data_dir = egetenv ("EMACSDATA");
1011
1012 register char * sh;
1013 Lisp_Object tempdir;
1014
05630743 1015 if (initialized && !NILP (Vinstallation_directory))
8de15d69 1016 {
05630743
RS
1017 /* Add to the path the lib-src subdir of the installation dir. */
1018 Lisp_Object tem;
1019 tem = Fexpand_file_name (build_string ("lib-src"),
1020 Vinstallation_directory);
1021 if (NILP (Fmember (tem, Vexec_path)))
8de15d69 1022 {
bad95d8f 1023#ifndef DOS_NT
1a6640ec 1024 /* MSDOS uses wrapped binaries, so don't do this. */
8de15d69
RS
1025 Vexec_path = nconc2 (Vexec_path, Fcons (tem, Qnil));
1026 Vexec_directory = Ffile_name_as_directory (tem);
bad95d8f 1027#endif /* not DOS_NT */
e17f7533 1028 }
8de15d69 1029
e17f7533
RS
1030 /* Maybe use ../etc as well as ../lib-src. */
1031 if (data_dir == 0)
1032 {
1033 tem = Fexpand_file_name (build_string ("etc"),
1034 Vinstallation_directory);
1035 Vdoc_directory = Ffile_name_as_directory (tem);
8de15d69
RS
1036 }
1037 }
7e933683
RS
1038
1039 /* Look for the files that should be in etc. We don't use
1040 Vinstallation_directory, because these files are never installed
e17f7533 1041 near the executable, and they are never in the build
7e933683
RS
1042 directory when that's different from the source directory.
1043
1044 Instead, if these files are not in the nominal place, we try the
1045 source directory. */
1046 if (data_dir == 0)
1047 {
1048 Lisp_Object tem, tem1, newdir;
1049
1050 tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory);
1051 tem1 = Ffile_exists_p (tem);
1052 if (NILP (tem1))
1053 {
1054 newdir = Fexpand_file_name (build_string ("../etc/"),
1055 build_string (PATH_DUMPLOADSEARCH));
1056 tem = Fexpand_file_name (build_string ("GNU"), newdir);
1057 tem1 = Ffile_exists_p (tem);
1058 if (!NILP (tem1))
1059 Vdata_directory = newdir;
1060 }
1061 }
80856e74 1062
e576cab4
JB
1063 tempdir = Fdirectory_file_name (Vexec_directory);
1064 if (access (XSTRING (tempdir)->data, 0) < 0)
80856e74 1065 {
0af6a831
RS
1066 fprintf (stderr,
1067 "Warning: arch-dependent data dir (%s) does not exist.\n",
1068 XSTRING (Vexec_directory)->data);
80856e74
JB
1069 sleep (2);
1070 }
1071
e576cab4
JB
1072 tempdir = Fdirectory_file_name (Vdata_directory);
1073 if (access (XSTRING (tempdir)->data, 0) < 0)
1074 {
0af6a831
RS
1075 fprintf (stderr,
1076 "Warning: arch-independent data dir (%s) does not exist.\n",
1077 XSTRING (Vdata_directory)->data);
e576cab4
JB
1078 sleep (2);
1079 }
1080
80856e74
JB
1081#ifdef VMS
1082 Vshell_file_name = build_string ("*dcl*");
1083#else
e576cab4 1084 sh = (char *) getenv ("SHELL");
80856e74
JB
1085 Vshell_file_name = build_string (sh ? sh : "/bin/sh");
1086#endif
9fefd2ba
JB
1087}
1088
1089set_process_environment ()
1090{
1091 register char **envp;
80856e74 1092
80856e74
JB
1093 Vprocess_environment = Qnil;
1094#ifndef CANNOT_DUMP
1095 if (initialized)
1096#endif
1097 for (envp = environ; *envp; envp++)
1098 Vprocess_environment = Fcons (build_string (*envp),
1099 Vprocess_environment);
80856e74
JB
1100}
1101
1102syms_of_callproc ()
1103{
bad95d8f 1104#ifdef DOS_NT
093650fe
RS
1105 Qbuffer_file_type = intern ("buffer-file-type");
1106 staticpro (&Qbuffer_file_type);
1107
1108 DEFVAR_LISP ("binary-process-input", &Vbinary_process_input,
1109 "*If non-nil then new subprocesses are assumed to take binary input.");
1110 Vbinary_process_input = Qnil;
1111
1112 DEFVAR_LISP ("binary-process-output", &Vbinary_process_output,
7e6c2178 1113 "*If non-nil then new subprocesses are assumed to produce binary output.");
093650fe 1114 Vbinary_process_output = Qnil;
bad95d8f 1115#endif /* DOS_NT */
7e6c2178 1116
80856e74
JB
1117 DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
1118 "*File name to load inferior shells from.\n\
1119Initialized from the SHELL environment variable.");
1120
1121 DEFVAR_LISP ("exec-path", &Vexec_path,
1122 "*List of directories to search programs to run in subprocesses.\n\
1123Each element is a string (directory name) or nil (try default directory).");
1124
1125 DEFVAR_LISP ("exec-directory", &Vexec_directory,
e576cab4
JB
1126 "Directory of architecture-dependent files that come with GNU Emacs,\n\
1127especially executable programs intended for Emacs to invoke.");
1128
1129 DEFVAR_LISP ("data-directory", &Vdata_directory,
1130 "Directory of architecture-independent files that come with GNU Emacs,\n\
1131intended for Emacs to use.");
80856e74 1132
35a2f4b8
KH
1133 DEFVAR_LISP ("doc-directory", &Vdoc_directory,
1134 "Directory containing the DOC file that comes with GNU Emacs.\n\
1135This is usually the same as data-directory.");
1136
ed61592a
JB
1137 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory,
1138 "For internal use by the build procedure only.\n\
1139This is the name of the directory in which the build procedure installed\n\
1140Emacs's info files; the default value for Info-default-directory-list\n\
1141includes this.");
1142 Vconfigure_info_directory = build_string (PATH_INFO);
1143
80856e74 1144 DEFVAR_LISP ("process-environment", &Vprocess_environment,
e576cab4
JB
1145 "List of environment variables for subprocesses to inherit.\n\
1146Each element should be a string of the form ENVVARNAME=VALUE.\n\
1147The environment which Emacs inherits is placed in this variable\n\
1148when Emacs starts.");
80856e74
JB
1149
1150#ifndef VMS
1151 defsubr (&Scall_process);
012c6fcb 1152 defsubr (&Sgetenv);
986ffb24 1153#endif
e576cab4 1154 defsubr (&Scall_process_region);
80856e74 1155}