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