* lisp/emulation/cua-base.el (cua-selection-mode): Make it toggle again.
[emacs.git] / src / callproc.c
blob3726eb3cc7fd8f8a088d4d054fdd8d75e274d6ae
1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985-1988, 1993-1995, 1999-2011
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <signal.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <setjmp.h>
26 #include <sys/types.h>
27 #include <unistd.h>
29 #include <sys/file.h>
30 #include <fcntl.h>
32 #ifdef WINDOWSNT
33 #define NOMINMAX
34 #include <windows.h>
35 #include "w32.h"
36 #define _P_NOWAIT 1 /* from process.h */
37 #endif
39 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
40 #include <sys/stat.h>
41 #include <sys/param.h>
42 #endif /* MSDOS */
44 #include "lisp.h"
45 #include "commands.h"
46 #include "buffer.h"
47 #include "character.h"
48 #include "ccl.h"
49 #include "coding.h"
50 #include "composite.h"
51 #include <epaths.h>
52 #include "process.h"
53 #include "syssignal.h"
54 #include "systty.h"
55 #include "blockinput.h"
56 #include "frame.h"
57 #include "termhooks.h"
59 #ifdef MSDOS
60 #include "msdos.h"
61 #endif
63 #ifndef USE_CRT_DLL
64 extern char **environ;
65 #endif
67 #ifdef HAVE_SETPGID
68 #if !defined (USG)
69 #undef setpgrp
70 #define setpgrp setpgid
71 #endif
72 #endif
74 /* Pattern used by call-process-region to make temp files. */
75 static Lisp_Object Vtemp_file_name_pattern;
77 /* True if we are about to fork off a synchronous process or if we
78 are waiting for it. */
79 int synch_process_alive;
81 /* Nonzero => this is a string explaining death of synchronous subprocess. */
82 const char *synch_process_death;
84 /* Nonzero => this is the signal number that terminated the subprocess. */
85 int synch_process_termsig;
87 /* If synch_process_death is zero,
88 this is exit code of synchronous subprocess. */
89 int synch_process_retcode;
92 /* Clean up when exiting Fcall_process.
93 On MSDOS, delete the temporary file on any kind of termination.
94 On Unix, kill the process and any children on termination by signal. */
96 /* Nonzero if this is termination due to exit. */
97 static int call_process_exited;
99 static Lisp_Object Fgetenv_internal (Lisp_Object, Lisp_Object);
101 static Lisp_Object
102 call_process_kill (Lisp_Object fdpid)
104 emacs_close (XFASTINT (Fcar (fdpid)));
105 EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
106 synch_process_alive = 0;
107 return Qnil;
110 static Lisp_Object
111 call_process_cleanup (Lisp_Object arg)
113 Lisp_Object fdpid = Fcdr (arg);
114 #if defined (MSDOS)
115 Lisp_Object file;
116 #else
117 int pid;
118 #endif
120 Fset_buffer (Fcar (arg));
122 #if defined (MSDOS)
123 /* for MSDOS fdpid is really (fd . tempfile) */
124 file = Fcdr (fdpid);
125 emacs_close (XFASTINT (Fcar (fdpid)));
126 if (strcmp (SDATA (file), NULL_DEVICE) != 0)
127 unlink (SDATA (file));
128 #else /* not MSDOS */
129 pid = XFASTINT (Fcdr (fdpid));
131 if (call_process_exited)
133 emacs_close (XFASTINT (Fcar (fdpid)));
134 return Qnil;
137 if (EMACS_KILLPG (pid, SIGINT) == 0)
139 int count = SPECPDL_INDEX ();
140 record_unwind_protect (call_process_kill, fdpid);
141 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
142 immediate_quit = 1;
143 QUIT;
144 wait_for_termination (pid);
145 immediate_quit = 0;
146 specpdl_ptr = specpdl + count; /* Discard the unwind protect. */
147 message1 ("Waiting for process to die...done");
149 synch_process_alive = 0;
150 emacs_close (XFASTINT (Fcar (fdpid)));
151 #endif /* not MSDOS */
152 return Qnil;
155 DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
156 doc: /* Call PROGRAM synchronously in separate process.
157 The remaining arguments are optional.
158 The program's input comes from file INFILE (nil means `/dev/null').
159 Insert output in BUFFER before point; t means current buffer;
160 nil for BUFFER means discard it; 0 means discard and don't wait.
161 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
162 REAL-BUFFER says what to do with standard output, as above,
163 while STDERR-FILE says what to do with standard error in the child.
164 STDERR-FILE may be nil (discard standard error output),
165 t (mix it with ordinary output), or a file name string.
167 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
168 Remaining arguments are strings passed as command arguments to PROGRAM.
170 If executable PROGRAM can't be found as an executable, `call-process'
171 signals a Lisp error. `call-process' reports errors in execution of
172 the program only through its return and output.
174 If BUFFER is 0, `call-process' returns immediately with value nil.
175 Otherwise it waits for PROGRAM to terminate
176 and returns a numeric exit status or a signal description string.
177 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
179 usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */)
180 (size_t nargs, register Lisp_Object *args)
182 Lisp_Object infile, buffer, current_dir, path;
183 volatile int display_p_volatile;
184 int fd[2];
185 int filefd;
186 register int pid;
187 #define CALLPROC_BUFFER_SIZE_MIN (16 * 1024)
188 #define CALLPROC_BUFFER_SIZE_MAX (4 * CALLPROC_BUFFER_SIZE_MIN)
189 char buf[CALLPROC_BUFFER_SIZE_MAX];
190 int bufsize = CALLPROC_BUFFER_SIZE_MIN;
191 int count = SPECPDL_INDEX ();
192 volatile USE_SAFE_ALLOCA;
194 const unsigned char **volatile new_argv_volatile;
195 register const unsigned char **new_argv;
196 /* File to use for stderr in the child.
197 t means use same as standard output. */
198 Lisp_Object error_file;
199 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
200 char *outf, *tempfile;
201 int outfilefd;
202 #endif
203 struct coding_system process_coding; /* coding-system of process output */
204 struct coding_system argument_coding; /* coding-system of arguments */
205 /* Set to the return value of Ffind_operation_coding_system. */
206 Lisp_Object coding_systems;
208 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
209 coding_systems = Qt;
211 CHECK_STRING (args[0]);
213 error_file = Qt;
215 #ifndef subprocesses
216 /* Without asynchronous processes we cannot have BUFFER == 0. */
217 if (nargs >= 3
218 && (INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2])))
219 error ("Operating system cannot handle asynchronous subprocesses");
220 #endif /* subprocesses */
222 /* Decide the coding-system for giving arguments. */
224 Lisp_Object val, *args2;
225 size_t i;
227 /* If arguments are supplied, we may have to encode them. */
228 if (nargs >= 5)
230 int must_encode = 0;
231 Lisp_Object coding_attrs;
233 for (i = 4; i < nargs; i++)
234 CHECK_STRING (args[i]);
236 for (i = 4; i < nargs; i++)
237 if (STRING_MULTIBYTE (args[i]))
238 must_encode = 1;
240 if (!NILP (Vcoding_system_for_write))
241 val = Vcoding_system_for_write;
242 else if (! must_encode)
243 val = Qraw_text;
244 else
246 SAFE_ALLOCA (args2, Lisp_Object *, (nargs + 1) * sizeof *args2);
247 args2[0] = Qcall_process;
248 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
249 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
250 val = CONSP (coding_systems) ? XCDR (coding_systems) : Qnil;
252 val = complement_process_encoding_system (val);
253 setup_coding_system (Fcheck_coding_system (val), &argument_coding);
254 coding_attrs = CODING_ID_ATTRS (argument_coding.id);
255 if (NILP (CODING_ATTR_ASCII_COMPAT (coding_attrs)))
257 /* We should not use an ASCII incompatible coding system. */
258 val = raw_text_coding_system (val);
259 setup_coding_system (val, &argument_coding);
264 if (nargs >= 2 && ! NILP (args[1]))
266 infile = Fexpand_file_name (args[1], BVAR (current_buffer, directory));
267 CHECK_STRING (infile);
269 else
270 infile = build_string (NULL_DEVICE);
272 if (nargs >= 3)
274 buffer = args[2];
276 /* If BUFFER is a list, its meaning is
277 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
278 if (CONSP (buffer))
280 if (CONSP (XCDR (buffer)))
282 Lisp_Object stderr_file;
283 stderr_file = XCAR (XCDR (buffer));
285 if (NILP (stderr_file) || EQ (Qt, stderr_file))
286 error_file = stderr_file;
287 else
288 error_file = Fexpand_file_name (stderr_file, Qnil);
291 buffer = XCAR (buffer);
294 if (!(EQ (buffer, Qnil)
295 || EQ (buffer, Qt)
296 || INTEGERP (buffer)))
298 Lisp_Object spec_buffer;
299 spec_buffer = buffer;
300 buffer = Fget_buffer_create (buffer);
301 /* Mention the buffer name for a better error message. */
302 if (NILP (buffer))
303 CHECK_BUFFER (spec_buffer);
304 CHECK_BUFFER (buffer);
307 else
308 buffer = Qnil;
310 /* Make sure that the child will be able to chdir to the current
311 buffer's current directory, or its unhandled equivalent. We
312 can't just have the child check for an error when it does the
313 chdir, since it's in a vfork.
315 We have to GCPRO around this because Fexpand_file_name,
316 Funhandled_file_name_directory, and Ffile_accessible_directory_p
317 might call a file name handling function. The argument list is
318 protected by the caller, so all we really have to worry about is
319 buffer. */
321 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
323 current_dir = BVAR (current_buffer, directory);
325 GCPRO4 (infile, buffer, current_dir, error_file);
327 current_dir = Funhandled_file_name_directory (current_dir);
328 if (NILP (current_dir))
329 /* If the file name handler says that current_dir is unreachable, use
330 a sensible default. */
331 current_dir = build_string ("~/");
332 current_dir = expand_and_dir_to_file (current_dir, Qnil);
333 current_dir = Ffile_name_as_directory (current_dir);
335 if (NILP (Ffile_accessible_directory_p (current_dir)))
336 report_file_error ("Setting current directory",
337 Fcons (BVAR (current_buffer, directory), Qnil));
339 if (STRING_MULTIBYTE (infile))
340 infile = ENCODE_FILE (infile);
341 if (STRING_MULTIBYTE (current_dir))
342 current_dir = ENCODE_FILE (current_dir);
343 if (STRINGP (error_file) && STRING_MULTIBYTE (error_file))
344 error_file = ENCODE_FILE (error_file);
345 UNGCPRO;
348 display_p_volatile = INTERACTIVE && nargs >= 4 && !NILP (args[3]);
350 filefd = emacs_open (SSDATA (infile), O_RDONLY, 0);
351 if (filefd < 0)
353 infile = DECODE_FILE (infile);
354 report_file_error ("Opening process input file", Fcons (infile, Qnil));
356 /* Search for program; barf if not found. */
358 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
360 GCPRO4 (infile, buffer, current_dir, error_file);
361 openp (Vexec_path, args[0], Vexec_suffixes, &path, make_number (X_OK));
362 UNGCPRO;
364 if (NILP (path))
366 emacs_close (filefd);
367 report_file_error ("Searching for program", Fcons (args[0], Qnil));
370 /* If program file name starts with /: for quoting a magic name,
371 discard that. */
372 if (SBYTES (path) > 2 && SREF (path, 0) == '/'
373 && SREF (path, 1) == ':')
374 path = Fsubstring (path, make_number (2), Qnil);
376 SAFE_ALLOCA (new_argv, const unsigned char **,
377 (nargs > 4 ? nargs - 2 : 2) * sizeof *new_argv);
378 new_argv_volatile = new_argv;
379 if (nargs > 4)
381 register size_t i;
382 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
384 GCPRO5 (infile, buffer, current_dir, path, error_file);
385 argument_coding.dst_multibyte = 0;
386 for (i = 4; i < nargs; i++)
388 argument_coding.src_multibyte = STRING_MULTIBYTE (args[i]);
389 if (CODING_REQUIRE_ENCODING (&argument_coding))
390 /* We must encode this argument. */
391 args[i] = encode_coding_string (&argument_coding, args[i], 1);
393 UNGCPRO;
394 for (i = 4; i < nargs; i++)
395 new_argv[i - 3] = SDATA (args[i]);
396 new_argv[i - 3] = 0;
398 else
399 new_argv[1] = 0;
400 new_argv[0] = SDATA (path);
402 #ifdef MSDOS /* MW, July 1993 */
403 if ((outf = egetenv ("TMPDIR")))
404 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
405 else
407 tempfile = alloca (20);
408 *tempfile = '\0';
410 dostounix_filename (tempfile);
411 if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/')
412 strcat (tempfile, "/");
413 strcat (tempfile, "detmp.XXX");
414 mktemp (tempfile);
416 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
417 if (outfilefd < 0)
419 emacs_close (filefd);
420 report_file_error ("Opening process output file",
421 Fcons (build_string (tempfile), Qnil));
423 fd[0] = filefd;
424 fd[1] = outfilefd;
425 #endif /* MSDOS */
427 if (INTEGERP (buffer))
428 fd[1] = emacs_open (NULL_DEVICE, O_WRONLY, 0), fd[0] = -1;
429 else
431 #ifndef MSDOS
432 errno = 0;
433 if (pipe (fd) == -1)
435 emacs_close (filefd);
436 report_file_error ("Creating process pipe", Qnil);
438 #endif
442 /* child_setup must clobber environ in systems with true vfork.
443 Protect it from permanent change. */
444 register char **save_environ = environ;
445 register int fd1 = fd[1];
446 int fd_error = fd1;
447 #ifdef HAVE_WORKING_VFORK
448 sigset_t procmask;
449 sigset_t blocked;
450 struct sigaction sigpipe_action;
451 #endif
453 #if 0 /* Some systems don't have sigblock. */
454 mask = sigblock (sigmask (SIGCHLD));
455 #endif
457 /* Record that we're about to create a synchronous process. */
458 synch_process_alive = 1;
460 /* These vars record information from process termination.
461 Clear them now before process can possibly terminate,
462 to avoid timing error if process terminates soon. */
463 synch_process_death = 0;
464 synch_process_retcode = 0;
465 synch_process_termsig = 0;
467 if (NILP (error_file))
468 fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0);
469 else if (STRINGP (error_file))
471 #ifdef DOS_NT
472 fd_error = emacs_open (SSDATA (error_file),
473 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
474 S_IREAD | S_IWRITE);
475 #else /* not DOS_NT */
476 fd_error = creat (SSDATA (error_file), 0666);
477 #endif /* not DOS_NT */
480 if (fd_error < 0)
482 emacs_close (filefd);
483 if (fd[0] != filefd)
484 emacs_close (fd[0]);
485 if (fd1 >= 0)
486 emacs_close (fd1);
487 #ifdef MSDOS
488 unlink (tempfile);
489 #endif
490 if (NILP (error_file))
491 error_file = build_string (NULL_DEVICE);
492 else if (STRINGP (error_file))
493 error_file = DECODE_FILE (error_file);
494 report_file_error ("Cannot redirect stderr", Fcons (error_file, Qnil));
497 #ifdef MSDOS /* MW, July 1993 */
498 /* Note that on MSDOS `child_setup' actually returns the child process
499 exit status, not its PID, so we assign it to `synch_process_retcode'
500 below. */
501 pid = child_setup (filefd, outfilefd, fd_error, (char **) new_argv,
502 0, current_dir);
504 /* Record that the synchronous process exited and note its
505 termination status. */
506 synch_process_alive = 0;
507 synch_process_retcode = pid;
508 if (synch_process_retcode < 0) /* means it couldn't be exec'ed */
510 synchronize_system_messages_locale ();
511 synch_process_death = strerror (errno);
514 emacs_close (outfilefd);
515 if (fd_error != outfilefd)
516 emacs_close (fd_error);
517 fd1 = -1; /* No harm in closing that one! */
518 /* Since CRLF is converted to LF within `decode_coding', we can
519 always open a file with binary mode. */
520 fd[0] = emacs_open (tempfile, O_RDONLY | O_BINARY, 0);
521 if (fd[0] < 0)
523 unlink (tempfile);
524 emacs_close (filefd);
525 report_file_error ("Cannot re-open temporary file", Qnil);
527 #else /* not MSDOS */
528 #ifdef WINDOWSNT
529 pid = child_setup (filefd, fd1, fd_error, (char **) new_argv,
530 0, current_dir);
531 #else /* not WINDOWSNT */
533 #ifdef HAVE_WORKING_VFORK
534 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
535 this sets the parent's signal handlers as well as the child's.
536 So delay all interrupts whose handlers the child might munge,
537 and record the current handlers so they can be restored later. */
538 sigemptyset (&blocked);
539 sigaddset (&blocked, SIGPIPE);
540 sigaction (SIGPIPE, 0, &sigpipe_action);
541 sigprocmask (SIG_BLOCK, &blocked, &procmask);
542 #endif
544 BLOCK_INPUT;
546 pid = vfork ();
548 new_argv = new_argv_volatile;
550 if (pid == 0)
552 if (fd[0] >= 0)
553 emacs_close (fd[0]);
554 #ifdef HAVE_SETSID
555 setsid ();
556 #endif
557 #if defined (USG)
558 setpgrp ();
559 #else
560 setpgrp (pid, pid);
561 #endif /* USG */
563 /* GConf causes us to ignore SIGPIPE, make sure it is restored
564 in the child. */
565 //signal (SIGPIPE, SIG_DFL);
566 #ifdef HAVE_WORKING_VFORK
567 sigprocmask (SIG_SETMASK, &procmask, 0);
568 #endif
570 child_setup (filefd, fd1, fd_error, (char **) new_argv,
571 0, current_dir);
574 UNBLOCK_INPUT;
576 #ifdef HAVE_WORKING_VFORK
577 /* Restore the signal state. */
578 sigaction (SIGPIPE, &sigpipe_action, 0);
579 sigprocmask (SIG_SETMASK, &procmask, 0);
580 #endif
582 #endif /* not WINDOWSNT */
584 /* The MSDOS case did this already. */
585 if (fd_error >= 0)
586 emacs_close (fd_error);
587 #endif /* not MSDOS */
589 environ = save_environ;
591 /* Close most of our fd's, but not fd[0]
592 since we will use that to read input from. */
593 emacs_close (filefd);
594 if (fd1 >= 0 && fd1 != fd_error)
595 emacs_close (fd1);
598 if (pid < 0)
600 if (fd[0] >= 0)
601 emacs_close (fd[0]);
602 report_file_error ("Doing vfork", Qnil);
605 if (INTEGERP (buffer))
607 if (fd[0] >= 0)
608 emacs_close (fd[0]);
609 return Qnil;
612 /* Enable sending signal if user quits below. */
613 call_process_exited = 0;
615 #if defined(MSDOS)
616 /* MSDOS needs different cleanup information. */
617 record_unwind_protect (call_process_cleanup,
618 Fcons (Fcurrent_buffer (),
619 Fcons (make_number (fd[0]),
620 build_string (tempfile))));
621 #else
622 record_unwind_protect (call_process_cleanup,
623 Fcons (Fcurrent_buffer (),
624 Fcons (make_number (fd[0]), make_number (pid))));
625 #endif /* not MSDOS */
628 if (BUFFERP (buffer))
629 Fset_buffer (buffer);
631 if (NILP (buffer))
633 /* If BUFFER is nil, we must read process output once and then
634 discard it, so setup coding system but with nil. */
635 setup_coding_system (Qnil, &process_coding);
637 else
639 Lisp_Object val, *args2;
641 val = Qnil;
642 if (!NILP (Vcoding_system_for_read))
643 val = Vcoding_system_for_read;
644 else
646 if (EQ (coding_systems, Qt))
648 size_t i;
650 SAFE_ALLOCA (args2, Lisp_Object *, (nargs + 1) * sizeof *args2);
651 args2[0] = Qcall_process;
652 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
653 coding_systems
654 = Ffind_operation_coding_system (nargs + 1, args2);
656 if (CONSP (coding_systems))
657 val = XCAR (coding_systems);
658 else if (CONSP (Vdefault_process_coding_system))
659 val = XCAR (Vdefault_process_coding_system);
660 else
661 val = Qnil;
663 Fcheck_coding_system (val);
664 /* In unibyte mode, character code conversion should not take
665 place but EOL conversion should. So, setup raw-text or one
666 of the subsidiary according to the information just setup. */
667 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
668 && !NILP (val))
669 val = raw_text_coding_system (val);
670 setup_coding_system (val, &process_coding);
673 immediate_quit = 1;
674 QUIT;
677 register EMACS_INT nread;
678 int first = 1;
679 EMACS_INT total_read = 0;
680 int carryover = 0;
681 int display_p = display_p_volatile;
682 int display_on_the_fly = display_p;
683 struct coding_system saved_coding;
685 saved_coding = process_coding;
686 while (1)
688 /* Repeatedly read until we've filled as much as possible
689 of the buffer size we have. But don't read
690 less than 1024--save that for the next bufferful. */
691 nread = carryover;
692 while (nread < bufsize - 1024)
694 int this_read = emacs_read (fd[0], buf + nread,
695 bufsize - nread);
697 if (this_read < 0)
698 goto give_up;
700 if (this_read == 0)
702 process_coding.mode |= CODING_MODE_LAST_BLOCK;
703 break;
706 nread += this_read;
707 total_read += this_read;
709 if (display_on_the_fly)
710 break;
713 /* Now NREAD is the total amount of data in the buffer. */
714 immediate_quit = 0;
716 if (!NILP (buffer))
718 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
719 && ! CODING_MAY_REQUIRE_DECODING (&process_coding))
720 insert_1_both (buf, nread, nread, 0, 1, 0);
721 else
722 { /* We have to decode the input. */
723 Lisp_Object curbuf;
724 int count1 = SPECPDL_INDEX ();
726 XSETBUFFER (curbuf, current_buffer);
727 /* We cannot allow after-change-functions be run
728 during decoding, because that might modify the
729 buffer, while we rely on process_coding.produced to
730 faithfully reflect inserted text until we
731 TEMP_SET_PT_BOTH below. */
732 specbind (Qinhibit_modification_hooks, Qt);
733 decode_coding_c_string (&process_coding, (unsigned char *) buf,
734 nread, curbuf);
735 unbind_to (count1, Qnil);
736 if (display_on_the_fly
737 && CODING_REQUIRE_DETECTION (&saved_coding)
738 && ! CODING_REQUIRE_DETECTION (&process_coding))
740 /* We have detected some coding system. But,
741 there's a possibility that the detection was
742 done by insufficient data. So, we give up
743 displaying on the fly. */
744 if (process_coding.produced > 0)
745 del_range_2 (process_coding.dst_pos,
746 process_coding.dst_pos_byte,
747 process_coding.dst_pos
748 + process_coding.produced_char,
749 process_coding.dst_pos_byte
750 + process_coding.produced, 0);
751 display_on_the_fly = 0;
752 process_coding = saved_coding;
753 carryover = nread;
754 /* This is to make the above condition always
755 fails in the future. */
756 saved_coding.common_flags
757 &= ~CODING_REQUIRE_DETECTION_MASK;
758 continue;
761 TEMP_SET_PT_BOTH (PT + process_coding.produced_char,
762 PT_BYTE + process_coding.produced);
763 carryover = process_coding.carryover_bytes;
764 if (carryover > 0)
765 memcpy (buf, process_coding.carryover,
766 process_coding.carryover_bytes);
770 if (process_coding.mode & CODING_MODE_LAST_BLOCK)
771 break;
773 /* Make the buffer bigger as we continue to read more data,
774 but not past CALLPROC_BUFFER_SIZE_MAX. */
775 if (bufsize < CALLPROC_BUFFER_SIZE_MAX && total_read > 32 * bufsize)
776 if ((bufsize *= 2) > CALLPROC_BUFFER_SIZE_MAX)
777 bufsize = CALLPROC_BUFFER_SIZE_MAX;
779 if (display_p)
781 if (first)
782 prepare_menu_bars ();
783 first = 0;
784 redisplay_preserve_echo_area (1);
785 /* This variable might have been set to 0 for code
786 detection. In that case, we set it back to 1 because
787 we should have already detected a coding system. */
788 display_on_the_fly = 1;
790 immediate_quit = 1;
791 QUIT;
793 give_up: ;
795 Vlast_coding_system_used = CODING_ID_NAME (process_coding.id);
796 /* If the caller required, let the buffer inherit the
797 coding-system used to decode the process output. */
798 if (inherit_process_coding_system)
799 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
800 make_number (total_read));
803 #ifndef MSDOS
804 /* Wait for it to terminate, unless it already has. */
805 wait_for_termination (pid);
806 #endif
808 immediate_quit = 0;
810 /* Don't kill any children that the subprocess may have left behind
811 when exiting. */
812 call_process_exited = 1;
814 SAFE_FREE ();
815 unbind_to (count, Qnil);
817 if (synch_process_termsig)
819 const char *signame;
821 synchronize_system_messages_locale ();
822 signame = strsignal (synch_process_termsig);
824 if (signame == 0)
825 signame = "unknown";
827 synch_process_death = signame;
830 if (synch_process_death)
831 return code_convert_string_norecord (build_string (synch_process_death),
832 Vlocale_coding_system, 0);
833 return make_number (synch_process_retcode);
836 static Lisp_Object
837 delete_temp_file (Lisp_Object name)
839 /* Suppress jka-compr handling, etc. */
840 int count = SPECPDL_INDEX ();
841 specbind (intern ("file-name-handler-alist"), Qnil);
842 internal_delete_file (name);
843 unbind_to (count, Qnil);
844 return Qnil;
847 DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
848 3, MANY, 0,
849 doc: /* Send text from START to END to a synchronous process running PROGRAM.
850 The remaining arguments are optional.
851 Delete the text if fourth arg DELETE is non-nil.
853 Insert output in BUFFER before point; t means current buffer;
854 nil for BUFFER means discard it; 0 means discard and don't wait.
855 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
856 REAL-BUFFER says what to do with standard output, as above,
857 while STDERR-FILE says what to do with standard error in the child.
858 STDERR-FILE may be nil (discard standard error output),
859 t (mix it with ordinary output), or a file name string.
861 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.
862 Remaining args are passed to PROGRAM at startup as command args.
864 If BUFFER is 0, `call-process-region' returns immediately with value nil.
865 Otherwise it waits for PROGRAM to terminate
866 and returns a numeric exit status or a signal description string.
867 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
869 usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */)
870 (size_t nargs, register Lisp_Object *args)
872 struct gcpro gcpro1;
873 Lisp_Object filename_string;
874 register Lisp_Object start, end;
875 int count = SPECPDL_INDEX ();
876 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
877 Lisp_Object coding_systems;
878 Lisp_Object val, *args2;
879 size_t i;
880 char *tempfile;
881 Lisp_Object tmpdir, pattern;
883 if (STRINGP (Vtemporary_file_directory))
884 tmpdir = Vtemporary_file_directory;
885 else
887 #ifndef DOS_NT
888 if (getenv ("TMPDIR"))
889 tmpdir = build_string (getenv ("TMPDIR"));
890 else
891 tmpdir = build_string ("/tmp/");
892 #else /* DOS_NT */
893 char *outf;
894 if ((outf = egetenv ("TMPDIR"))
895 || (outf = egetenv ("TMP"))
896 || (outf = egetenv ("TEMP")))
897 tmpdir = build_string (outf);
898 else
899 tmpdir = Ffile_name_as_directory (build_string ("c:/temp"));
900 #endif
904 USE_SAFE_ALLOCA;
905 pattern = Fexpand_file_name (Vtemp_file_name_pattern, tmpdir);
906 SAFE_ALLOCA (tempfile, char *, SBYTES (pattern) + 1);
907 memcpy (tempfile, SDATA (pattern), SBYTES (pattern) + 1);
908 coding_systems = Qt;
910 #ifdef HAVE_MKSTEMP
912 int fd;
914 BLOCK_INPUT;
915 fd = mkstemp (tempfile);
916 UNBLOCK_INPUT;
917 if (fd == -1)
918 report_file_error ("Failed to open temporary file",
919 Fcons (Vtemp_file_name_pattern, Qnil));
920 else
921 close (fd);
923 #else
924 mktemp (tempfile);
925 #endif
927 filename_string = build_string (tempfile);
928 GCPRO1 (filename_string);
929 SAFE_FREE ();
932 start = args[0];
933 end = args[1];
934 /* Decide coding-system of the contents of the temporary file. */
935 if (!NILP (Vcoding_system_for_write))
936 val = Vcoding_system_for_write;
937 else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
938 val = Qraw_text;
939 else
941 USE_SAFE_ALLOCA;
942 SAFE_ALLOCA (args2, Lisp_Object *, (nargs + 1) * sizeof *args2);
943 args2[0] = Qcall_process_region;
944 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
945 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
946 val = CONSP (coding_systems) ? XCDR (coding_systems) : Qnil;
947 SAFE_FREE ();
949 val = complement_process_encoding_system (val);
952 int count1 = SPECPDL_INDEX ();
954 specbind (intern ("coding-system-for-write"), val);
955 /* POSIX lets mk[s]temp use "."; don't invoke jka-compr if we
956 happen to get a ".Z" suffix. */
957 specbind (intern ("file-name-handler-alist"), Qnil);
958 Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil);
960 unbind_to (count1, Qnil);
963 /* Note that Fcall_process takes care of binding
964 coding-system-for-read. */
966 record_unwind_protect (delete_temp_file, filename_string);
968 if (nargs > 3 && !NILP (args[3]))
969 Fdelete_region (start, end);
971 if (nargs > 3)
973 args += 2;
974 nargs -= 2;
976 else
978 args[0] = args[2];
979 nargs = 2;
981 args[1] = filename_string;
983 RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs, args)));
986 #ifndef WINDOWSNT
987 static int relocate_fd (int fd, int minfd);
988 #endif
990 static char **
991 add_env (char **env, char **new_env, char *string)
993 char **ep;
994 int ok = 1;
995 if (string == NULL)
996 return new_env;
998 /* See if this string duplicates any string already in the env.
999 If so, don't put it in.
1000 When an env var has multiple definitions,
1001 we keep the definition that comes first in process-environment. */
1002 for (ep = env; ok && ep != new_env; ep++)
1004 char *p = *ep, *q = string;
1005 while (ok)
1007 if (*q != *p)
1008 break;
1009 if (*q == 0)
1010 /* The string is a lone variable name; keep it for now, we
1011 will remove it later. It is a placeholder for a
1012 variable that is not to be included in the environment. */
1013 break;
1014 if (*q == '=')
1015 ok = 0;
1016 p++, q++;
1019 if (ok)
1020 *new_env++ = string;
1021 return new_env;
1024 /* This is the last thing run in a newly forked inferior
1025 either synchronous or asynchronous.
1026 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1027 Initialize inferior's priority, pgrp, connected dir and environment.
1028 then exec another program based on new_argv.
1030 This function may change environ for the superior process.
1031 Therefore, the superior process must save and restore the value
1032 of environ around the vfork and the call to this function.
1034 SET_PGRP is nonzero if we should put the subprocess into a separate
1035 process group.
1037 CURRENT_DIR is an elisp string giving the path of the current
1038 directory the subprocess should have. Since we can't really signal
1039 a decent error from within the child, this should be verified as an
1040 executable directory by the parent. */
1043 child_setup (int in, int out, int err, register char **new_argv, int set_pgrp, Lisp_Object current_dir)
1045 char **env;
1046 char *pwd_var;
1047 #ifdef WINDOWSNT
1048 int cpid;
1049 HANDLE handles[3];
1050 #endif /* WINDOWSNT */
1052 int pid = getpid ();
1054 /* Close Emacs's descriptors that this process should not have. */
1055 close_process_descs ();
1057 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1058 we will lose if we call close_load_descs here. */
1059 #ifndef DOS_NT
1060 close_load_descs ();
1061 #endif
1063 /* Note that use of alloca is always safe here. It's obvious for systems
1064 that do not have true vfork or that have true (stack) alloca.
1065 If using vfork and C_ALLOCA (when Emacs used to include
1066 src/alloca.c) it is safe because that changes the superior's
1067 static variables as if the superior had done alloca and will be
1068 cleaned up in the usual way. */
1070 register char *temp;
1071 register int i;
1073 i = SBYTES (current_dir);
1074 #ifdef MSDOS
1075 /* MSDOS must have all environment variables malloc'ed, because
1076 low-level libc functions that launch subsidiary processes rely
1077 on that. */
1078 pwd_var = (char *) xmalloc (i + 6);
1079 #else
1080 pwd_var = (char *) alloca (i + 6);
1081 #endif
1082 temp = pwd_var + 4;
1083 memcpy (pwd_var, "PWD=", 4);
1084 memcpy (temp, SDATA (current_dir), i);
1085 if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP;
1086 temp[i] = 0;
1088 #ifndef DOS_NT
1089 /* We can't signal an Elisp error here; we're in a vfork. Since
1090 the callers check the current directory before forking, this
1091 should only return an error if the directory's permissions
1092 are changed between the check and this chdir, but we should
1093 at least check. */
1094 if (chdir (temp) < 0)
1095 _exit (errno);
1096 #else /* DOS_NT */
1097 /* Get past the drive letter, so that d:/ is left alone. */
1098 if (i > 2 && IS_DEVICE_SEP (temp[1]) && IS_DIRECTORY_SEP (temp[2]))
1100 temp += 2;
1101 i -= 2;
1103 #endif /* DOS_NT */
1105 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1106 while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1]))
1107 temp[--i] = 0;
1110 /* Set `env' to a vector of the strings in the environment. */
1112 register Lisp_Object tem;
1113 register char **new_env;
1114 char **p, **q;
1115 register int new_length;
1116 Lisp_Object display = Qnil;
1118 new_length = 0;
1120 for (tem = Vprocess_environment;
1121 CONSP (tem) && STRINGP (XCAR (tem));
1122 tem = XCDR (tem))
1124 if (strncmp (SSDATA (XCAR (tem)), "DISPLAY", 7) == 0
1125 && (SDATA (XCAR (tem)) [7] == '\0'
1126 || SDATA (XCAR (tem)) [7] == '='))
1127 /* DISPLAY is specified in process-environment. */
1128 display = Qt;
1129 new_length++;
1132 /* If not provided yet, use the frame's DISPLAY. */
1133 if (NILP (display))
1135 Lisp_Object tmp = Fframe_parameter (selected_frame, Qdisplay);
1136 if (!STRINGP (tmp) && CONSP (Vinitial_environment))
1137 /* If still not found, Look for DISPLAY in Vinitial_environment. */
1138 tmp = Fgetenv_internal (build_string ("DISPLAY"),
1139 Vinitial_environment);
1140 if (STRINGP (tmp))
1142 display = tmp;
1143 new_length++;
1147 /* new_length + 2 to include PWD and terminating 0. */
1148 env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *));
1149 /* If we have a PWD envvar, pass one down,
1150 but with corrected value. */
1151 if (egetenv ("PWD"))
1152 *new_env++ = pwd_var;
1154 if (STRINGP (display))
1156 int vlen = strlen ("DISPLAY=") + strlen (SSDATA (display)) + 1;
1157 char *vdata = (char *) alloca (vlen);
1158 strcpy (vdata, "DISPLAY=");
1159 strcat (vdata, SSDATA (display));
1160 new_env = add_env (env, new_env, vdata);
1163 /* Overrides. */
1164 for (tem = Vprocess_environment;
1165 CONSP (tem) && STRINGP (XCAR (tem));
1166 tem = XCDR (tem))
1167 new_env = add_env (env, new_env, SSDATA (XCAR (tem)));
1169 *new_env = 0;
1171 /* Remove variable names without values. */
1172 p = q = env;
1173 while (*p != 0)
1175 while (*q != 0 && strchr (*q, '=') == NULL)
1176 q++;
1177 *p = *q++;
1178 if (*p != 0)
1179 p++;
1184 #ifdef WINDOWSNT
1185 prepare_standard_handles (in, out, err, handles);
1186 set_process_dir (SDATA (current_dir));
1187 /* Spawn the child. (See ntproc.c:Spawnve). */
1188 cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
1189 reset_standard_handles (in, out, err, handles);
1190 if (cpid == -1)
1191 /* An error occurred while trying to spawn the process. */
1192 report_file_error ("Spawning child process", Qnil);
1193 return cpid;
1195 #else /* not WINDOWSNT */
1196 /* Make sure that in, out, and err are not actually already in
1197 descriptors zero, one, or two; this could happen if Emacs is
1198 started with its standard in, out, or error closed, as might
1199 happen under X. */
1201 int oin = in, oout = out;
1203 /* We have to avoid relocating the same descriptor twice! */
1205 in = relocate_fd (in, 3);
1207 if (out == oin)
1208 out = in;
1209 else
1210 out = relocate_fd (out, 3);
1212 if (err == oin)
1213 err = in;
1214 else if (err == oout)
1215 err = out;
1216 else
1217 err = relocate_fd (err, 3);
1220 #ifndef MSDOS
1221 emacs_close (0);
1222 emacs_close (1);
1223 emacs_close (2);
1225 dup2 (in, 0);
1226 dup2 (out, 1);
1227 dup2 (err, 2);
1228 emacs_close (in);
1229 if (out != in)
1230 emacs_close (out);
1231 if (err != in && err != out)
1232 emacs_close (err);
1234 #if defined(USG)
1235 #ifndef SETPGRP_RELEASES_CTTY
1236 setpgrp (); /* No arguments but equivalent in this case */
1237 #endif
1238 #else /* not USG */
1239 setpgrp (pid, pid);
1240 #endif /* not USG */
1242 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1243 tcsetpgrp (0, pid);
1245 /* execvp does not accept an environment arg so the only way
1246 to pass this environment is to set environ. Our caller
1247 is responsible for restoring the ambient value of environ. */
1248 environ = env;
1249 execvp (new_argv[0], new_argv);
1251 emacs_write (1, "Can't exec program: ", 20);
1252 emacs_write (1, new_argv[0], strlen (new_argv[0]));
1253 emacs_write (1, "\n", 1);
1254 _exit (1);
1256 #else /* MSDOS */
1257 pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env);
1258 xfree (pwd_var);
1259 if (pid == -1)
1260 /* An error occurred while trying to run the subprocess. */
1261 report_file_error ("Spawning child process", Qnil);
1262 return pid;
1263 #endif /* MSDOS */
1264 #endif /* not WINDOWSNT */
1267 #ifndef WINDOWSNT
1268 /* Move the file descriptor FD so that its number is not less than MINFD.
1269 If the file descriptor is moved at all, the original is freed. */
1270 static int
1271 relocate_fd (int fd, int minfd)
1273 if (fd >= minfd)
1274 return fd;
1275 else
1277 int new;
1278 #ifdef F_DUPFD
1279 new = fcntl (fd, F_DUPFD, minfd);
1280 #else
1281 new = dup (fd);
1282 if (new != -1)
1283 /* Note that we hold the original FD open while we recurse,
1284 to guarantee we'll get a new FD if we need it. */
1285 new = relocate_fd (new, minfd);
1286 #endif
1287 if (new == -1)
1289 const char *message_1 = "Error while setting up child: ";
1290 const char *errmessage = strerror (errno);
1291 const char *message_2 = "\n";
1292 emacs_write (2, message_1, strlen (message_1));
1293 emacs_write (2, errmessage, strlen (errmessage));
1294 emacs_write (2, message_2, strlen (message_2));
1295 _exit (1);
1297 emacs_close (fd);
1298 return new;
1301 #endif /* not WINDOWSNT */
1303 static int
1304 getenv_internal_1 (const char *var, int varlen, char **value, int *valuelen,
1305 Lisp_Object env)
1307 for (; CONSP (env); env = XCDR (env))
1309 Lisp_Object entry = XCAR (env);
1310 if (STRINGP (entry)
1311 && SBYTES (entry) >= varlen
1312 #ifdef WINDOWSNT
1313 /* NT environment variables are case insensitive. */
1314 && ! strnicmp (SDATA (entry), var, varlen)
1315 #else /* not WINDOWSNT */
1316 && ! memcmp (SDATA (entry), var, varlen)
1317 #endif /* not WINDOWSNT */
1320 if (SBYTES (entry) > varlen && SREF (entry, varlen) == '=')
1322 *value = SSDATA (entry) + (varlen + 1);
1323 *valuelen = SBYTES (entry) - (varlen + 1);
1324 return 1;
1326 else if (SBYTES (entry) == varlen)
1328 /* Lone variable names in Vprocess_environment mean that
1329 variable should be removed from the environment. */
1330 *value = NULL;
1331 return 1;
1335 return 0;
1338 static int
1339 getenv_internal (const char *var, int varlen, char **value, int *valuelen,
1340 Lisp_Object frame)
1342 /* Try to find VAR in Vprocess_environment first. */
1343 if (getenv_internal_1 (var, varlen, value, valuelen,
1344 Vprocess_environment))
1345 return *value ? 1 : 0;
1347 /* For DISPLAY try to get the values from the frame or the initial env. */
1348 if (strcmp (var, "DISPLAY") == 0)
1350 Lisp_Object display
1351 = Fframe_parameter (NILP (frame) ? selected_frame : frame, Qdisplay);
1352 if (STRINGP (display))
1354 *value = SSDATA (display);
1355 *valuelen = SBYTES (display);
1356 return 1;
1358 /* If still not found, Look for DISPLAY in Vinitial_environment. */
1359 if (getenv_internal_1 (var, varlen, value, valuelen,
1360 Vinitial_environment))
1361 return *value ? 1 : 0;
1364 return 0;
1367 DEFUN ("getenv-internal", Fgetenv_internal, Sgetenv_internal, 1, 2, 0,
1368 doc: /* Get the value of environment variable VARIABLE.
1369 VARIABLE should be a string. Value is nil if VARIABLE is undefined in
1370 the environment. Otherwise, value is a string.
1372 This function searches `process-environment' for VARIABLE.
1374 If optional parameter ENV is a list, then search this list instead of
1375 `process-environment', and return t when encountering a negative entry
1376 \(an entry for a variable with no value). */)
1377 (Lisp_Object variable, Lisp_Object env)
1379 char *value;
1380 int valuelen;
1382 CHECK_STRING (variable);
1383 if (CONSP (env))
1385 if (getenv_internal_1 (SSDATA (variable), SBYTES (variable),
1386 &value, &valuelen, env))
1387 return value ? make_string (value, valuelen) : Qt;
1388 else
1389 return Qnil;
1391 else if (getenv_internal (SSDATA (variable), SBYTES (variable),
1392 &value, &valuelen, env))
1393 return make_string (value, valuelen);
1394 else
1395 return Qnil;
1398 /* A version of getenv that consults the Lisp environment lists,
1399 easily callable from C. */
1400 char *
1401 egetenv (const char *var)
1403 char *value;
1404 int valuelen;
1406 if (getenv_internal (var, strlen (var), &value, &valuelen, Qnil))
1407 return value;
1408 else
1409 return 0;
1413 /* This is run before init_cmdargs. */
1415 void
1416 init_callproc_1 (void)
1418 char *data_dir = egetenv ("EMACSDATA");
1419 char *doc_dir = egetenv ("EMACSDOC");
1421 Vdata_directory
1422 = Ffile_name_as_directory (build_string (data_dir ? data_dir
1423 : PATH_DATA));
1424 Vdoc_directory
1425 = Ffile_name_as_directory (build_string (doc_dir ? doc_dir
1426 : PATH_DOC));
1428 /* Check the EMACSPATH environment variable, defaulting to the
1429 PATH_EXEC path from epaths.h. */
1430 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
1431 Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
1432 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
1435 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1437 void
1438 init_callproc (void)
1440 char *data_dir = egetenv ("EMACSDATA");
1442 register char * sh;
1443 Lisp_Object tempdir;
1445 if (!NILP (Vinstallation_directory))
1447 /* Add to the path the lib-src subdir of the installation dir. */
1448 Lisp_Object tem;
1449 tem = Fexpand_file_name (build_string ("lib-src"),
1450 Vinstallation_directory);
1451 #ifndef DOS_NT
1452 /* MSDOS uses wrapped binaries, so don't do this. */
1453 if (NILP (Fmember (tem, Vexec_path)))
1455 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
1456 Vexec_path = Fcons (tem, Vexec_path);
1457 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
1460 Vexec_directory = Ffile_name_as_directory (tem);
1461 #endif /* not DOS_NT */
1463 /* Maybe use ../etc as well as ../lib-src. */
1464 if (data_dir == 0)
1466 tem = Fexpand_file_name (build_string ("etc"),
1467 Vinstallation_directory);
1468 Vdoc_directory = Ffile_name_as_directory (tem);
1472 /* Look for the files that should be in etc. We don't use
1473 Vinstallation_directory, because these files are never installed
1474 near the executable, and they are never in the build
1475 directory when that's different from the source directory.
1477 Instead, if these files are not in the nominal place, we try the
1478 source directory. */
1479 if (data_dir == 0)
1481 Lisp_Object tem, tem1, srcdir;
1483 srcdir = Fexpand_file_name (build_string ("../src/"),
1484 build_string (PATH_DUMPLOADSEARCH));
1485 tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory);
1486 tem1 = Ffile_exists_p (tem);
1487 if (!NILP (Fequal (srcdir, Vinvocation_directory)) || NILP (tem1))
1489 Lisp_Object newdir;
1490 newdir = Fexpand_file_name (build_string ("../etc/"),
1491 build_string (PATH_DUMPLOADSEARCH));
1492 tem = Fexpand_file_name (build_string ("GNU"), newdir);
1493 tem1 = Ffile_exists_p (tem);
1494 if (!NILP (tem1))
1495 Vdata_directory = newdir;
1499 #ifndef CANNOT_DUMP
1500 if (initialized)
1501 #endif
1503 tempdir = Fdirectory_file_name (Vexec_directory);
1504 if (access (SSDATA (tempdir), 0) < 0)
1505 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1506 Vexec_directory);
1509 tempdir = Fdirectory_file_name (Vdata_directory);
1510 if (access (SSDATA (tempdir), 0) < 0)
1511 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1512 Vdata_directory);
1514 sh = (char *) getenv ("SHELL");
1515 Vshell_file_name = build_string (sh ? sh : "/bin/sh");
1517 #ifdef DOS_NT
1518 Vshared_game_score_directory = Qnil;
1519 #else
1520 Vshared_game_score_directory = build_string (PATH_GAME);
1521 if (NILP (Ffile_directory_p (Vshared_game_score_directory)))
1522 Vshared_game_score_directory = Qnil;
1523 #endif
1526 void
1527 set_initial_environment (void)
1529 register char **envp;
1530 #ifdef CANNOT_DUMP
1531 Vprocess_environment = Qnil;
1532 #else
1533 if (initialized)
1534 #endif
1536 for (envp = environ; *envp; envp++)
1537 Vprocess_environment = Fcons (build_string (*envp),
1538 Vprocess_environment);
1539 /* Ideally, the `copy' shouldn't be necessary, but it seems it's frequent
1540 to use `delete' and friends on process-environment. */
1541 Vinitial_environment = Fcopy_sequence (Vprocess_environment);
1545 void
1546 syms_of_callproc (void)
1548 #ifndef DOS_NT
1549 Vtemp_file_name_pattern = build_string ("emacsXXXXXX");
1550 #elif defined (WINDOWSNT)
1551 Vtemp_file_name_pattern = build_string ("emXXXXXX");
1552 #else
1553 Vtemp_file_name_pattern = build_string ("detmp.XXX");
1554 #endif
1555 staticpro (&Vtemp_file_name_pattern);
1557 DEFVAR_LISP ("shell-file-name", Vshell_file_name,
1558 doc: /* *File name to load inferior shells from.
1559 Initialized from the SHELL environment variable, or to a system-dependent
1560 default if SHELL is not set. */);
1562 DEFVAR_LISP ("exec-path", Vexec_path,
1563 doc: /* *List of directories to search programs to run in subprocesses.
1564 Each element is a string (directory name) or nil (try default directory). */);
1566 DEFVAR_LISP ("exec-suffixes", Vexec_suffixes,
1567 doc: /* *List of suffixes to try to find executable file names.
1568 Each element is a string. */);
1569 Vexec_suffixes = Qnil;
1571 DEFVAR_LISP ("exec-directory", Vexec_directory,
1572 doc: /* Directory for executables for Emacs to invoke.
1573 More generally, this includes any architecture-dependent files
1574 that are built and installed from the Emacs distribution. */);
1576 DEFVAR_LISP ("data-directory", Vdata_directory,
1577 doc: /* Directory of machine-independent files that come with GNU Emacs.
1578 These are files intended for Emacs to use while it runs. */);
1580 DEFVAR_LISP ("doc-directory", Vdoc_directory,
1581 doc: /* Directory containing the DOC file that comes with GNU Emacs.
1582 This is usually the same as `data-directory'. */);
1584 DEFVAR_LISP ("configure-info-directory", Vconfigure_info_directory,
1585 doc: /* For internal use by the build procedure only.
1586 This is the name of the directory in which the build procedure installed
1587 Emacs's info files; the default value for `Info-default-directory-list'
1588 includes this. */);
1589 Vconfigure_info_directory = build_string (PATH_INFO);
1591 DEFVAR_LISP ("shared-game-score-directory", Vshared_game_score_directory,
1592 doc: /* Directory of score files for games which come with GNU Emacs.
1593 If this variable is nil, then Emacs is unable to use a shared directory. */);
1594 #ifdef DOS_NT
1595 Vshared_game_score_directory = Qnil;
1596 #else
1597 Vshared_game_score_directory = build_string (PATH_GAME);
1598 #endif
1600 DEFVAR_LISP ("initial-environment", Vinitial_environment,
1601 doc: /* List of environment variables inherited from the parent process.
1602 Each element should be a string of the form ENVVARNAME=VALUE.
1603 The elements must normally be decoded (using `locale-coding-system') for use. */);
1604 Vinitial_environment = Qnil;
1606 DEFVAR_LISP ("process-environment", Vprocess_environment,
1607 doc: /* List of overridden environment variables for subprocesses to inherit.
1608 Each element should be a string of the form ENVVARNAME=VALUE.
1610 Entries in this list take precedence to those in the frame-local
1611 environments. Therefore, let-binding `process-environment' is an easy
1612 way to temporarily change the value of an environment variable,
1613 irrespective of where it comes from. To use `process-environment' to
1614 remove an environment variable, include only its name in the list,
1615 without "=VALUE".
1617 This variable is set to nil when Emacs starts.
1619 If multiple entries define the same variable, the first one always
1620 takes precedence.
1622 Non-ASCII characters are encoded according to the initial value of
1623 `locale-coding-system', i.e. the elements must normally be decoded for
1624 use.
1626 See `setenv' and `getenv'. */);
1627 Vprocess_environment = Qnil;
1629 defsubr (&Scall_process);
1630 defsubr (&Sgetenv_internal);
1631 defsubr (&Scall_process_region);