src/gnutls.c, src/image.c: Make function pointers static.
[emacs.git] / src / callproc.c
blob1d3d8764ff88d050f05c6469ebaa840ea7302d08
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 #include "lisp.h"
34 #ifdef WINDOWSNT
35 #define NOMINMAX
36 #include <windows.h>
37 #include "w32.h"
38 #define _P_NOWAIT 1 /* from process.h */
39 #endif
41 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
42 #include <sys/stat.h>
43 #include <sys/param.h>
44 #endif /* MSDOS */
46 #include "commands.h"
47 #include "buffer.h"
48 #include "character.h"
49 #include "ccl.h"
50 #include "coding.h"
51 #include "composite.h"
52 #include <epaths.h>
53 #include "process.h"
54 #include "syssignal.h"
55 #include "systty.h"
56 #include "blockinput.h"
57 #include "frame.h"
58 #include "termhooks.h"
60 #ifdef MSDOS
61 #include "msdos.h"
62 #endif
64 #ifndef USE_CRT_DLL
65 extern char **environ;
66 #endif
68 #ifdef HAVE_SETPGID
69 #if !defined (USG)
70 #undef setpgrp
71 #define setpgrp setpgid
72 #endif
73 #endif
75 /* Pattern used by call-process-region to make temp files. */
76 static Lisp_Object Vtemp_file_name_pattern;
78 /* True if we are about to fork off a synchronous process or if we
79 are waiting for it. */
80 int synch_process_alive;
82 /* Nonzero => this is a string explaining death of synchronous subprocess. */
83 const char *synch_process_death;
85 /* Nonzero => this is the signal number that terminated the subprocess. */
86 int synch_process_termsig;
88 /* If synch_process_death is zero,
89 this is exit code of synchronous subprocess. */
90 int synch_process_retcode;
93 /* Clean up when exiting Fcall_process.
94 On MSDOS, delete the temporary file on any kind of termination.
95 On Unix, kill the process and any children on termination by signal. */
97 /* Nonzero if this is termination due to exit. */
98 static int call_process_exited;
100 static Lisp_Object Fgetenv_internal (Lisp_Object, Lisp_Object);
102 static Lisp_Object
103 call_process_kill (Lisp_Object fdpid)
105 emacs_close (XFASTINT (Fcar (fdpid)));
106 EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
107 synch_process_alive = 0;
108 return Qnil;
111 static Lisp_Object
112 call_process_cleanup (Lisp_Object arg)
114 Lisp_Object fdpid = Fcdr (arg);
115 #if defined (MSDOS)
116 Lisp_Object file;
117 #else
118 int pid;
119 #endif
121 Fset_buffer (Fcar (arg));
123 #if defined (MSDOS)
124 /* for MSDOS fdpid is really (fd . tempfile) */
125 file = Fcdr (fdpid);
126 emacs_close (XFASTINT (Fcar (fdpid)));
127 if (strcmp (SDATA (file), NULL_DEVICE) != 0)
128 unlink (SDATA (file));
129 #else /* not MSDOS */
130 pid = XFASTINT (Fcdr (fdpid));
132 if (call_process_exited)
134 emacs_close (XFASTINT (Fcar (fdpid)));
135 return Qnil;
138 if (EMACS_KILLPG (pid, SIGINT) == 0)
140 int count = SPECPDL_INDEX ();
141 record_unwind_protect (call_process_kill, fdpid);
142 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
143 immediate_quit = 1;
144 QUIT;
145 wait_for_termination (pid);
146 immediate_quit = 0;
147 specpdl_ptr = specpdl + count; /* Discard the unwind protect. */
148 message1 ("Waiting for process to die...done");
150 synch_process_alive = 0;
151 emacs_close (XFASTINT (Fcar (fdpid)));
152 #endif /* not MSDOS */
153 return Qnil;
156 DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
157 doc: /* Call PROGRAM synchronously in separate process.
158 The remaining arguments are optional.
159 The program's input comes from file INFILE (nil means `/dev/null').
160 Insert output in BUFFER before point; t means current buffer; nil for BUFFER
161 means discard it; 0 means discard and don't wait; and `(:file FILE)', where
162 FILE is a file name string, means that it should be written to that file.
163 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
164 REAL-BUFFER says what to do with standard output, as above,
165 while STDERR-FILE says what to do with standard error in the child.
166 STDERR-FILE may be nil (discard standard error output),
167 t (mix it with ordinary output), or a file name string.
169 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
170 Remaining arguments are strings passed as command arguments to PROGRAM.
172 If executable PROGRAM can't be found as an executable, `call-process'
173 signals a Lisp error. `call-process' reports errors in execution of
174 the program only through its return and output.
176 If BUFFER is 0, `call-process' returns immediately with value nil.
177 Otherwise it waits for PROGRAM to terminate
178 and returns a numeric exit status or a signal description string.
179 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
181 usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */)
182 (size_t nargs, register Lisp_Object *args)
184 Lisp_Object infile, buffer, current_dir, path;
185 volatile int display_p_volatile;
186 int fd[2];
187 int filefd;
188 register int pid;
189 #define CALLPROC_BUFFER_SIZE_MIN (16 * 1024)
190 #define CALLPROC_BUFFER_SIZE_MAX (4 * CALLPROC_BUFFER_SIZE_MIN)
191 char buf[CALLPROC_BUFFER_SIZE_MAX];
192 int bufsize = CALLPROC_BUFFER_SIZE_MIN;
193 int count = SPECPDL_INDEX ();
194 volatile USE_SAFE_ALLOCA;
196 const unsigned char **volatile new_argv_volatile;
197 register const unsigned char **new_argv;
198 /* File to use for stderr in the child.
199 t means use same as standard output. */
200 Lisp_Object error_file;
201 Lisp_Object output_file = Qnil;
202 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
203 char *outf, *tempfile;
204 int outfilefd;
205 #endif
206 int fd_output = -1;
207 struct coding_system process_coding; /* coding-system of process output */
208 struct coding_system argument_coding; /* coding-system of arguments */
209 /* Set to the return value of Ffind_operation_coding_system. */
210 Lisp_Object coding_systems;
211 int output_to_buffer = 1;
213 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
214 coding_systems = Qt;
216 CHECK_STRING (args[0]);
218 error_file = Qt;
220 #ifndef subprocesses
221 /* Without asynchronous processes we cannot have BUFFER == 0. */
222 if (nargs >= 3
223 && (INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2])))
224 error ("Operating system cannot handle asynchronous subprocesses");
225 #endif /* subprocesses */
227 /* Decide the coding-system for giving arguments. */
229 Lisp_Object val, *args2;
230 size_t i;
232 /* If arguments are supplied, we may have to encode them. */
233 if (nargs >= 5)
235 int must_encode = 0;
236 Lisp_Object coding_attrs;
238 for (i = 4; i < nargs; i++)
239 CHECK_STRING (args[i]);
241 for (i = 4; i < nargs; i++)
242 if (STRING_MULTIBYTE (args[i]))
243 must_encode = 1;
245 if (!NILP (Vcoding_system_for_write))
246 val = Vcoding_system_for_write;
247 else if (! must_encode)
248 val = Qraw_text;
249 else
251 SAFE_ALLOCA (args2, Lisp_Object *, (nargs + 1) * sizeof *args2);
252 args2[0] = Qcall_process;
253 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
254 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
255 val = CONSP (coding_systems) ? XCDR (coding_systems) : Qnil;
257 val = complement_process_encoding_system (val);
258 setup_coding_system (Fcheck_coding_system (val), &argument_coding);
259 coding_attrs = CODING_ID_ATTRS (argument_coding.id);
260 if (NILP (CODING_ATTR_ASCII_COMPAT (coding_attrs)))
262 /* We should not use an ASCII incompatible coding system. */
263 val = raw_text_coding_system (val);
264 setup_coding_system (val, &argument_coding);
269 if (nargs >= 2 && ! NILP (args[1]))
271 infile = Fexpand_file_name (args[1], BVAR (current_buffer, directory));
272 CHECK_STRING (infile);
274 else
275 infile = build_string (NULL_DEVICE);
277 if (nargs >= 3)
279 buffer = args[2];
281 /* If BUFFER is a list, its meaning is (BUFFER-FOR-STDOUT
282 FILE-FOR-STDERR), unless the first element is :file, in which case see
283 the next paragraph. */
284 if (CONSP (buffer)
285 && (! SYMBOLP (XCAR (buffer))
286 || strcmp (SSDATA (SYMBOL_NAME (XCAR (buffer))), ":file")))
288 if (CONSP (XCDR (buffer)))
290 Lisp_Object stderr_file;
291 stderr_file = XCAR (XCDR (buffer));
293 if (NILP (stderr_file) || EQ (Qt, stderr_file))
294 error_file = stderr_file;
295 else
296 error_file = Fexpand_file_name (stderr_file, Qnil);
299 buffer = XCAR (buffer);
302 /* If the buffer is (still) a list, it might be a (:file "file") spec. */
303 if (CONSP (buffer)
304 && SYMBOLP (XCAR (buffer))
305 && ! strcmp (SSDATA (SYMBOL_NAME (XCAR (buffer))), ":file"))
307 output_file = Fexpand_file_name (XCAR (XCDR (buffer)),
308 BVAR (current_buffer, directory));
309 CHECK_STRING (output_file);
310 buffer = Qnil;
313 if (!(EQ (buffer, Qnil)
314 || EQ (buffer, Qt)
315 || INTEGERP (buffer)))
317 Lisp_Object spec_buffer;
318 spec_buffer = buffer;
319 buffer = Fget_buffer_create (buffer);
320 /* Mention the buffer name for a better error message. */
321 if (NILP (buffer))
322 CHECK_BUFFER (spec_buffer);
323 CHECK_BUFFER (buffer);
326 else
327 buffer = Qnil;
329 /* Make sure that the child will be able to chdir to the current
330 buffer's current directory, or its unhandled equivalent. We
331 can't just have the child check for an error when it does the
332 chdir, since it's in a vfork.
334 We have to GCPRO around this because Fexpand_file_name,
335 Funhandled_file_name_directory, and Ffile_accessible_directory_p
336 might call a file name handling function. The argument list is
337 protected by the caller, so all we really have to worry about is
338 buffer. */
340 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
342 current_dir = BVAR (current_buffer, directory);
344 GCPRO5 (infile, buffer, current_dir, error_file, output_file);
346 current_dir = Funhandled_file_name_directory (current_dir);
347 if (NILP (current_dir))
348 /* If the file name handler says that current_dir is unreachable, use
349 a sensible default. */
350 current_dir = build_string ("~/");
351 current_dir = expand_and_dir_to_file (current_dir, Qnil);
352 current_dir = Ffile_name_as_directory (current_dir);
354 if (NILP (Ffile_accessible_directory_p (current_dir)))
355 report_file_error ("Setting current directory",
356 Fcons (BVAR (current_buffer, directory), Qnil));
358 if (STRING_MULTIBYTE (infile))
359 infile = ENCODE_FILE (infile);
360 if (STRING_MULTIBYTE (current_dir))
361 current_dir = ENCODE_FILE (current_dir);
362 if (STRINGP (error_file) && STRING_MULTIBYTE (error_file))
363 error_file = ENCODE_FILE (error_file);
364 if (STRINGP (output_file) && STRING_MULTIBYTE (output_file))
365 output_file = ENCODE_FILE (output_file);
366 UNGCPRO;
369 display_p_volatile = INTERACTIVE && nargs >= 4 && !NILP (args[3]);
371 filefd = emacs_open (SSDATA (infile), O_RDONLY, 0);
372 if (filefd < 0)
374 infile = DECODE_FILE (infile);
375 report_file_error ("Opening process input file", Fcons (infile, Qnil));
378 if (STRINGP (output_file))
380 #ifdef DOS_NT
381 fd_output = emacs_open (SSDATA (output_file),
382 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
383 S_IREAD | S_IWRITE);
384 #else /* not DOS_NT */
385 fd_output = creat (SSDATA (output_file), 0666);
386 #endif /* not DOS_NT */
387 if (fd_output < 0)
389 output_file = DECODE_FILE (output_file);
390 report_file_error ("Opening process output file",
391 Fcons (output_file, Qnil));
393 if (STRINGP (error_file) || NILP (error_file))
394 output_to_buffer = 0;
397 /* Search for program; barf if not found. */
399 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
401 GCPRO4 (infile, buffer, current_dir, error_file);
402 openp (Vexec_path, args[0], Vexec_suffixes, &path, make_number (X_OK));
403 UNGCPRO;
405 if (NILP (path))
407 emacs_close (filefd);
408 report_file_error ("Searching for program", Fcons (args[0], Qnil));
411 /* If program file name starts with /: for quoting a magic name,
412 discard that. */
413 if (SBYTES (path) > 2 && SREF (path, 0) == '/'
414 && SREF (path, 1) == ':')
415 path = Fsubstring (path, make_number (2), Qnil);
417 SAFE_ALLOCA (new_argv, const unsigned char **,
418 (nargs > 4 ? nargs - 2 : 2) * sizeof *new_argv);
419 new_argv_volatile = new_argv;
420 if (nargs > 4)
422 register size_t i;
423 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
425 GCPRO5 (infile, buffer, current_dir, path, error_file);
426 argument_coding.dst_multibyte = 0;
427 for (i = 4; i < nargs; i++)
429 argument_coding.src_multibyte = STRING_MULTIBYTE (args[i]);
430 if (CODING_REQUIRE_ENCODING (&argument_coding))
431 /* We must encode this argument. */
432 args[i] = encode_coding_string (&argument_coding, args[i], 1);
434 UNGCPRO;
435 for (i = 4; i < nargs; i++)
436 new_argv[i - 3] = SDATA (args[i]);
437 new_argv[i - 3] = 0;
439 else
440 new_argv[1] = 0;
441 new_argv[0] = SDATA (path);
443 #ifdef MSDOS /* MW, July 1993 */
444 if ((outf = egetenv ("TMPDIR")))
445 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
446 else
448 tempfile = alloca (20);
449 *tempfile = '\0';
451 dostounix_filename (tempfile);
452 if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/')
453 strcat (tempfile, "/");
454 strcat (tempfile, "detmp.XXX");
455 mktemp (tempfile);
457 /* If we're redirecting STDOUT to a file, this is already opened. */
458 if (fd_output < 0)
460 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
461 if (outfilefd < 0) {
462 emacs_close (filefd);
463 report_file_error ("Opening process output file",
464 Fcons (build_string (tempfile), Qnil));
467 else
468 outfilefd = fd_output;
469 fd[0] = filefd;
470 fd[1] = outfilefd;
471 #endif /* MSDOS */
473 if (INTEGERP (buffer))
474 fd[1] = emacs_open (NULL_DEVICE, O_WRONLY, 0), fd[0] = -1;
475 else
477 #ifndef MSDOS
478 errno = 0;
479 if (pipe (fd) == -1)
481 emacs_close (filefd);
482 report_file_error ("Creating process pipe", Qnil);
484 #endif
488 /* child_setup must clobber environ in systems with true vfork.
489 Protect it from permanent change. */
490 register char **save_environ = environ;
491 register int fd1 = fd[1];
492 int fd_error = fd1;
493 #ifdef HAVE_WORKING_VFORK
494 sigset_t procmask;
495 sigset_t blocked;
496 struct sigaction sigpipe_action;
497 #endif
499 if (fd_output >= 0)
500 fd1 = fd_output;
501 #if 0 /* Some systems don't have sigblock. */
502 mask = sigblock (sigmask (SIGCHLD));
503 #endif
505 /* Record that we're about to create a synchronous process. */
506 synch_process_alive = 1;
508 /* These vars record information from process termination.
509 Clear them now before process can possibly terminate,
510 to avoid timing error if process terminates soon. */
511 synch_process_death = 0;
512 synch_process_retcode = 0;
513 synch_process_termsig = 0;
515 if (NILP (error_file))
516 fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0);
517 else if (STRINGP (error_file))
519 #ifdef DOS_NT
520 fd_error = emacs_open (SSDATA (error_file),
521 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
522 S_IREAD | S_IWRITE);
523 #else /* not DOS_NT */
524 fd_error = creat (SSDATA (error_file), 0666);
525 #endif /* not DOS_NT */
528 if (fd_error < 0)
530 emacs_close (filefd);
531 if (fd[0] != filefd)
532 emacs_close (fd[0]);
533 if (fd1 >= 0)
534 emacs_close (fd1);
535 #ifdef MSDOS
536 unlink (tempfile);
537 #endif
538 if (NILP (error_file))
539 error_file = build_string (NULL_DEVICE);
540 else if (STRINGP (error_file))
541 error_file = DECODE_FILE (error_file);
542 report_file_error ("Cannot redirect stderr", Fcons (error_file, Qnil));
545 #ifdef MSDOS /* MW, July 1993 */
546 /* Note that on MSDOS `child_setup' actually returns the child process
547 exit status, not its PID, so we assign it to `synch_process_retcode'
548 below. */
549 pid = child_setup (filefd, outfilefd, fd_error, (char **) new_argv,
550 0, current_dir);
552 /* Record that the synchronous process exited and note its
553 termination status. */
554 synch_process_alive = 0;
555 synch_process_retcode = pid;
556 if (synch_process_retcode < 0) /* means it couldn't be exec'ed */
558 synchronize_system_messages_locale ();
559 synch_process_death = strerror (errno);
562 emacs_close (outfilefd);
563 if (fd_error != outfilefd)
564 emacs_close (fd_error);
565 fd1 = -1; /* No harm in closing that one! */
566 /* Since CRLF is converted to LF within `decode_coding', we can
567 always open a file with binary mode. */
568 fd[0] = emacs_open (tempfile, O_RDONLY | O_BINARY, 0);
569 if (fd[0] < 0)
571 unlink (tempfile);
572 emacs_close (filefd);
573 report_file_error ("Cannot re-open temporary file", Qnil);
575 #else /* not MSDOS */
576 #ifdef WINDOWSNT
577 pid = child_setup (filefd, fd1, fd_error, (char **) new_argv,
578 0, current_dir);
579 #else /* not WINDOWSNT */
581 #ifdef HAVE_WORKING_VFORK
582 /* On many hosts (e.g. Solaris 2.4), if a vforked child calls `signal',
583 this sets the parent's signal handlers as well as the child's.
584 So delay all interrupts whose handlers the child might munge,
585 and record the current handlers so they can be restored later. */
586 sigemptyset (&blocked);
587 sigaddset (&blocked, SIGPIPE);
588 sigaction (SIGPIPE, 0, &sigpipe_action);
589 sigprocmask (SIG_BLOCK, &blocked, &procmask);
590 #endif
592 BLOCK_INPUT;
594 pid = vfork ();
596 new_argv = new_argv_volatile;
598 if (pid == 0)
600 if (fd[0] >= 0)
601 emacs_close (fd[0]);
602 #ifdef HAVE_SETSID
603 setsid ();
604 #endif
605 #if defined (USG)
606 setpgrp ();
607 #else
608 setpgrp (pid, pid);
609 #endif /* USG */
611 /* GConf causes us to ignore SIGPIPE, make sure it is restored
612 in the child. */
613 //signal (SIGPIPE, SIG_DFL);
614 #ifdef HAVE_WORKING_VFORK
615 sigprocmask (SIG_SETMASK, &procmask, 0);
616 #endif
618 child_setup (filefd, fd1, fd_error, (char **) new_argv,
619 0, current_dir);
622 UNBLOCK_INPUT;
624 #ifdef HAVE_WORKING_VFORK
625 /* Restore the signal state. */
626 sigaction (SIGPIPE, &sigpipe_action, 0);
627 sigprocmask (SIG_SETMASK, &procmask, 0);
628 #endif
630 #endif /* not WINDOWSNT */
632 /* The MSDOS case did this already. */
633 if (fd_error >= 0)
634 emacs_close (fd_error);
635 #endif /* not MSDOS */
637 environ = save_environ;
639 /* Close most of our fd's, but not fd[0]
640 since we will use that to read input from. */
641 emacs_close (filefd);
642 if (fd_output >= 0)
643 emacs_close (fd_output);
644 if (fd1 >= 0 && fd1 != fd_error)
645 emacs_close (fd1);
648 if (pid < 0)
650 if (fd[0] >= 0)
651 emacs_close (fd[0]);
652 report_file_error ("Doing vfork", Qnil);
655 if (INTEGERP (buffer))
657 if (fd[0] >= 0)
658 emacs_close (fd[0]);
659 return Qnil;
662 /* Enable sending signal if user quits below. */
663 call_process_exited = 0;
665 #if defined(MSDOS)
666 /* MSDOS needs different cleanup information. */
667 record_unwind_protect (call_process_cleanup,
668 Fcons (Fcurrent_buffer (),
669 Fcons (make_number (fd[0]),
670 build_string (tempfile))));
671 #else
672 record_unwind_protect (call_process_cleanup,
673 Fcons (Fcurrent_buffer (),
674 Fcons (make_number (fd[0]), make_number (pid))));
675 #endif /* not MSDOS */
678 if (BUFFERP (buffer))
679 Fset_buffer (buffer);
681 if (NILP (buffer))
683 /* If BUFFER is nil, we must read process output once and then
684 discard it, so setup coding system but with nil. */
685 setup_coding_system (Qnil, &process_coding);
687 else
689 Lisp_Object val, *args2;
691 val = Qnil;
692 if (!NILP (Vcoding_system_for_read))
693 val = Vcoding_system_for_read;
694 else
696 if (EQ (coding_systems, Qt))
698 size_t i;
700 SAFE_ALLOCA (args2, Lisp_Object *, (nargs + 1) * sizeof *args2);
701 args2[0] = Qcall_process;
702 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
703 coding_systems
704 = Ffind_operation_coding_system (nargs + 1, args2);
706 if (CONSP (coding_systems))
707 val = XCAR (coding_systems);
708 else if (CONSP (Vdefault_process_coding_system))
709 val = XCAR (Vdefault_process_coding_system);
710 else
711 val = Qnil;
713 Fcheck_coding_system (val);
714 /* In unibyte mode, character code conversion should not take
715 place but EOL conversion should. So, setup raw-text or one
716 of the subsidiary according to the information just setup. */
717 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
718 && !NILP (val))
719 val = raw_text_coding_system (val);
720 setup_coding_system (val, &process_coding);
723 immediate_quit = 1;
724 QUIT;
726 if (output_to_buffer)
728 register EMACS_INT nread;
729 int first = 1;
730 EMACS_INT total_read = 0;
731 int carryover = 0;
732 int display_p = display_p_volatile;
733 int display_on_the_fly = display_p;
734 struct coding_system saved_coding;
736 saved_coding = process_coding;
737 while (1)
739 /* Repeatedly read until we've filled as much as possible
740 of the buffer size we have. But don't read
741 less than 1024--save that for the next bufferful. */
742 nread = carryover;
743 while (nread < bufsize - 1024)
745 int this_read = emacs_read (fd[0], buf + nread,
746 bufsize - nread);
748 if (this_read < 0)
749 goto give_up;
751 if (this_read == 0)
753 process_coding.mode |= CODING_MODE_LAST_BLOCK;
754 break;
757 nread += this_read;
758 total_read += this_read;
760 if (display_on_the_fly)
761 break;
764 /* Now NREAD is the total amount of data in the buffer. */
765 immediate_quit = 0;
767 if (!NILP (buffer))
769 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
770 && ! CODING_MAY_REQUIRE_DECODING (&process_coding))
771 insert_1_both (buf, nread, nread, 0, 1, 0);
772 else
773 { /* We have to decode the input. */
774 Lisp_Object curbuf;
775 int count1 = SPECPDL_INDEX ();
777 XSETBUFFER (curbuf, current_buffer);
778 /* We cannot allow after-change-functions be run
779 during decoding, because that might modify the
780 buffer, while we rely on process_coding.produced to
781 faithfully reflect inserted text until we
782 TEMP_SET_PT_BOTH below. */
783 specbind (Qinhibit_modification_hooks, Qt);
784 decode_coding_c_string (&process_coding,
785 (unsigned char *) buf, nread, curbuf);
786 unbind_to (count1, Qnil);
787 if (display_on_the_fly
788 && CODING_REQUIRE_DETECTION (&saved_coding)
789 && ! CODING_REQUIRE_DETECTION (&process_coding))
791 /* We have detected some coding system. But,
792 there's a possibility that the detection was
793 done by insufficient data. So, we give up
794 displaying on the fly. */
795 if (process_coding.produced > 0)
796 del_range_2 (process_coding.dst_pos,
797 process_coding.dst_pos_byte,
798 process_coding.dst_pos
799 + process_coding.produced_char,
800 process_coding.dst_pos_byte
801 + process_coding.produced, 0);
802 display_on_the_fly = 0;
803 process_coding = saved_coding;
804 carryover = nread;
805 /* This is to make the above condition always
806 fails in the future. */
807 saved_coding.common_flags
808 &= ~CODING_REQUIRE_DETECTION_MASK;
809 continue;
812 TEMP_SET_PT_BOTH (PT + process_coding.produced_char,
813 PT_BYTE + process_coding.produced);
814 carryover = process_coding.carryover_bytes;
815 if (carryover > 0)
816 memcpy (buf, process_coding.carryover,
817 process_coding.carryover_bytes);
821 if (process_coding.mode & CODING_MODE_LAST_BLOCK)
822 break;
824 /* Make the buffer bigger as we continue to read more data,
825 but not past CALLPROC_BUFFER_SIZE_MAX. */
826 if (bufsize < CALLPROC_BUFFER_SIZE_MAX && total_read > 32 * bufsize)
827 if ((bufsize *= 2) > CALLPROC_BUFFER_SIZE_MAX)
828 bufsize = CALLPROC_BUFFER_SIZE_MAX;
830 if (display_p)
832 if (first)
833 prepare_menu_bars ();
834 first = 0;
835 redisplay_preserve_echo_area (1);
836 /* This variable might have been set to 0 for code
837 detection. In that case, we set it back to 1 because
838 we should have already detected a coding system. */
839 display_on_the_fly = 1;
841 immediate_quit = 1;
842 QUIT;
844 give_up: ;
846 Vlast_coding_system_used = CODING_ID_NAME (process_coding.id);
847 /* If the caller required, let the buffer inherit the
848 coding-system used to decode the process output. */
849 if (inherit_process_coding_system)
850 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
851 make_number (total_read));
854 #ifndef MSDOS
855 /* Wait for it to terminate, unless it already has. */
856 if (output_to_buffer)
857 wait_for_termination (pid);
858 else
859 interruptible_wait_for_termination (pid);
860 #endif
862 immediate_quit = 0;
864 /* Don't kill any children that the subprocess may have left behind
865 when exiting. */
866 call_process_exited = 1;
868 SAFE_FREE ();
869 unbind_to (count, Qnil);
871 if (synch_process_termsig)
873 const char *signame;
875 synchronize_system_messages_locale ();
876 signame = strsignal (synch_process_termsig);
878 if (signame == 0)
879 signame = "unknown";
881 synch_process_death = signame;
884 if (synch_process_death)
885 return code_convert_string_norecord (build_string (synch_process_death),
886 Vlocale_coding_system, 0);
887 return make_number (synch_process_retcode);
890 static Lisp_Object
891 delete_temp_file (Lisp_Object name)
893 /* Suppress jka-compr handling, etc. */
894 int count = SPECPDL_INDEX ();
895 specbind (intern ("file-name-handler-alist"), Qnil);
896 internal_delete_file (name);
897 unbind_to (count, Qnil);
898 return Qnil;
901 DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
902 3, MANY, 0,
903 doc: /* Send text from START to END to a synchronous process running PROGRAM.
904 The remaining arguments are optional.
905 Delete the text if fourth arg DELETE is non-nil.
907 Insert output in BUFFER before point; t means current buffer; nil for
908 BUFFER means discard it; 0 means discard and don't wait; and `(:file
909 FILE)', where FILE is a file name string, means that it should be
910 written to that file.
911 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
912 REAL-BUFFER says what to do with standard output, as above,
913 while STDERR-FILE says what to do with standard error in the child.
914 STDERR-FILE may be nil (discard standard error output),
915 t (mix it with ordinary output), or a file name string.
917 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.
918 Remaining args are passed to PROGRAM at startup as command args.
920 If BUFFER is 0, `call-process-region' returns immediately with value nil.
921 Otherwise it waits for PROGRAM to terminate
922 and returns a numeric exit status or a signal description string.
923 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
925 usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */)
926 (size_t nargs, register Lisp_Object *args)
928 struct gcpro gcpro1;
929 Lisp_Object filename_string;
930 register Lisp_Object start, end;
931 int count = SPECPDL_INDEX ();
932 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
933 Lisp_Object coding_systems;
934 Lisp_Object val, *args2;
935 size_t i;
936 char *tempfile;
937 Lisp_Object tmpdir, pattern;
939 if (STRINGP (Vtemporary_file_directory))
940 tmpdir = Vtemporary_file_directory;
941 else
943 #ifndef DOS_NT
944 if (getenv ("TMPDIR"))
945 tmpdir = build_string (getenv ("TMPDIR"));
946 else
947 tmpdir = build_string ("/tmp/");
948 #else /* DOS_NT */
949 char *outf;
950 if ((outf = egetenv ("TMPDIR"))
951 || (outf = egetenv ("TMP"))
952 || (outf = egetenv ("TEMP")))
953 tmpdir = build_string (outf);
954 else
955 tmpdir = Ffile_name_as_directory (build_string ("c:/temp"));
956 #endif
960 USE_SAFE_ALLOCA;
961 pattern = Fexpand_file_name (Vtemp_file_name_pattern, tmpdir);
962 SAFE_ALLOCA (tempfile, char *, SBYTES (pattern) + 1);
963 memcpy (tempfile, SDATA (pattern), SBYTES (pattern) + 1);
964 coding_systems = Qt;
966 #ifdef HAVE_MKSTEMP
968 int fd;
970 BLOCK_INPUT;
971 fd = mkstemp (tempfile);
972 UNBLOCK_INPUT;
973 if (fd == -1)
974 report_file_error ("Failed to open temporary file",
975 Fcons (Vtemp_file_name_pattern, Qnil));
976 else
977 close (fd);
979 #else
980 mktemp (tempfile);
981 #endif
983 filename_string = build_string (tempfile);
984 GCPRO1 (filename_string);
985 SAFE_FREE ();
988 start = args[0];
989 end = args[1];
990 /* Decide coding-system of the contents of the temporary file. */
991 if (!NILP (Vcoding_system_for_write))
992 val = Vcoding_system_for_write;
993 else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
994 val = Qraw_text;
995 else
997 USE_SAFE_ALLOCA;
998 SAFE_ALLOCA (args2, Lisp_Object *, (nargs + 1) * sizeof *args2);
999 args2[0] = Qcall_process_region;
1000 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
1001 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
1002 val = CONSP (coding_systems) ? XCDR (coding_systems) : Qnil;
1003 SAFE_FREE ();
1005 val = complement_process_encoding_system (val);
1008 int count1 = SPECPDL_INDEX ();
1010 specbind (intern ("coding-system-for-write"), val);
1011 /* POSIX lets mk[s]temp use "."; don't invoke jka-compr if we
1012 happen to get a ".Z" suffix. */
1013 specbind (intern ("file-name-handler-alist"), Qnil);
1014 Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil);
1016 unbind_to (count1, Qnil);
1019 /* Note that Fcall_process takes care of binding
1020 coding-system-for-read. */
1022 record_unwind_protect (delete_temp_file, filename_string);
1024 if (nargs > 3 && !NILP (args[3]))
1025 Fdelete_region (start, end);
1027 if (nargs > 3)
1029 args += 2;
1030 nargs -= 2;
1032 else
1034 args[0] = args[2];
1035 nargs = 2;
1037 args[1] = filename_string;
1039 RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs, args)));
1042 #ifndef WINDOWSNT
1043 static int relocate_fd (int fd, int minfd);
1044 #endif
1046 static char **
1047 add_env (char **env, char **new_env, char *string)
1049 char **ep;
1050 int ok = 1;
1051 if (string == NULL)
1052 return new_env;
1054 /* See if this string duplicates any string already in the env.
1055 If so, don't put it in.
1056 When an env var has multiple definitions,
1057 we keep the definition that comes first in process-environment. */
1058 for (ep = env; ok && ep != new_env; ep++)
1060 char *p = *ep, *q = string;
1061 while (ok)
1063 if (*q != *p)
1064 break;
1065 if (*q == 0)
1066 /* The string is a lone variable name; keep it for now, we
1067 will remove it later. It is a placeholder for a
1068 variable that is not to be included in the environment. */
1069 break;
1070 if (*q == '=')
1071 ok = 0;
1072 p++, q++;
1075 if (ok)
1076 *new_env++ = string;
1077 return new_env;
1080 /* This is the last thing run in a newly forked inferior
1081 either synchronous or asynchronous.
1082 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1083 Initialize inferior's priority, pgrp, connected dir and environment.
1084 then exec another program based on new_argv.
1086 This function may change environ for the superior process.
1087 Therefore, the superior process must save and restore the value
1088 of environ around the vfork and the call to this function.
1090 SET_PGRP is nonzero if we should put the subprocess into a separate
1091 process group.
1093 CURRENT_DIR is an elisp string giving the path of the current
1094 directory the subprocess should have. Since we can't really signal
1095 a decent error from within the child, this should be verified as an
1096 executable directory by the parent. */
1099 child_setup (int in, int out, int err, register char **new_argv, int set_pgrp, Lisp_Object current_dir)
1101 char **env;
1102 char *pwd_var;
1103 #ifdef WINDOWSNT
1104 int cpid;
1105 HANDLE handles[3];
1106 #endif /* WINDOWSNT */
1108 int pid = getpid ();
1110 /* Close Emacs's descriptors that this process should not have. */
1111 close_process_descs ();
1113 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1114 we will lose if we call close_load_descs here. */
1115 #ifndef DOS_NT
1116 close_load_descs ();
1117 #endif
1119 /* Note that use of alloca is always safe here. It's obvious for systems
1120 that do not have true vfork or that have true (stack) alloca.
1121 If using vfork and C_ALLOCA (when Emacs used to include
1122 src/alloca.c) it is safe because that changes the superior's
1123 static variables as if the superior had done alloca and will be
1124 cleaned up in the usual way. */
1126 register char *temp;
1127 register int i;
1129 i = SBYTES (current_dir);
1130 #ifdef MSDOS
1131 /* MSDOS must have all environment variables malloc'ed, because
1132 low-level libc functions that launch subsidiary processes rely
1133 on that. */
1134 pwd_var = (char *) xmalloc (i + 6);
1135 #else
1136 pwd_var = (char *) alloca (i + 6);
1137 #endif
1138 temp = pwd_var + 4;
1139 memcpy (pwd_var, "PWD=", 4);
1140 memcpy (temp, SDATA (current_dir), i);
1141 if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP;
1142 temp[i] = 0;
1144 #ifndef DOS_NT
1145 /* We can't signal an Elisp error here; we're in a vfork. Since
1146 the callers check the current directory before forking, this
1147 should only return an error if the directory's permissions
1148 are changed between the check and this chdir, but we should
1149 at least check. */
1150 if (chdir (temp) < 0)
1151 _exit (errno);
1152 #else /* DOS_NT */
1153 /* Get past the drive letter, so that d:/ is left alone. */
1154 if (i > 2 && IS_DEVICE_SEP (temp[1]) && IS_DIRECTORY_SEP (temp[2]))
1156 temp += 2;
1157 i -= 2;
1159 #endif /* DOS_NT */
1161 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1162 while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1]))
1163 temp[--i] = 0;
1166 /* Set `env' to a vector of the strings in the environment. */
1168 register Lisp_Object tem;
1169 register char **new_env;
1170 char **p, **q;
1171 register int new_length;
1172 Lisp_Object display = Qnil;
1174 new_length = 0;
1176 for (tem = Vprocess_environment;
1177 CONSP (tem) && STRINGP (XCAR (tem));
1178 tem = XCDR (tem))
1180 if (strncmp (SSDATA (XCAR (tem)), "DISPLAY", 7) == 0
1181 && (SDATA (XCAR (tem)) [7] == '\0'
1182 || SDATA (XCAR (tem)) [7] == '='))
1183 /* DISPLAY is specified in process-environment. */
1184 display = Qt;
1185 new_length++;
1188 /* If not provided yet, use the frame's DISPLAY. */
1189 if (NILP (display))
1191 Lisp_Object tmp = Fframe_parameter (selected_frame, Qdisplay);
1192 if (!STRINGP (tmp) && CONSP (Vinitial_environment))
1193 /* If still not found, Look for DISPLAY in Vinitial_environment. */
1194 tmp = Fgetenv_internal (build_string ("DISPLAY"),
1195 Vinitial_environment);
1196 if (STRINGP (tmp))
1198 display = tmp;
1199 new_length++;
1203 /* new_length + 2 to include PWD and terminating 0. */
1204 env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *));
1205 /* If we have a PWD envvar, pass one down,
1206 but with corrected value. */
1207 if (egetenv ("PWD"))
1208 *new_env++ = pwd_var;
1210 if (STRINGP (display))
1212 int vlen = strlen ("DISPLAY=") + strlen (SSDATA (display)) + 1;
1213 char *vdata = (char *) alloca (vlen);
1214 strcpy (vdata, "DISPLAY=");
1215 strcat (vdata, SSDATA (display));
1216 new_env = add_env (env, new_env, vdata);
1219 /* Overrides. */
1220 for (tem = Vprocess_environment;
1221 CONSP (tem) && STRINGP (XCAR (tem));
1222 tem = XCDR (tem))
1223 new_env = add_env (env, new_env, SSDATA (XCAR (tem)));
1225 *new_env = 0;
1227 /* Remove variable names without values. */
1228 p = q = env;
1229 while (*p != 0)
1231 while (*q != 0 && strchr (*q, '=') == NULL)
1232 q++;
1233 *p = *q++;
1234 if (*p != 0)
1235 p++;
1240 #ifdef WINDOWSNT
1241 prepare_standard_handles (in, out, err, handles);
1242 set_process_dir (SDATA (current_dir));
1243 /* Spawn the child. (See ntproc.c:Spawnve). */
1244 cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
1245 reset_standard_handles (in, out, err, handles);
1246 if (cpid == -1)
1247 /* An error occurred while trying to spawn the process. */
1248 report_file_error ("Spawning child process", Qnil);
1249 return cpid;
1251 #else /* not WINDOWSNT */
1252 /* Make sure that in, out, and err are not actually already in
1253 descriptors zero, one, or two; this could happen if Emacs is
1254 started with its standard in, out, or error closed, as might
1255 happen under X. */
1257 int oin = in, oout = out;
1259 /* We have to avoid relocating the same descriptor twice! */
1261 in = relocate_fd (in, 3);
1263 if (out == oin)
1264 out = in;
1265 else
1266 out = relocate_fd (out, 3);
1268 if (err == oin)
1269 err = in;
1270 else if (err == oout)
1271 err = out;
1272 else
1273 err = relocate_fd (err, 3);
1276 #ifndef MSDOS
1277 emacs_close (0);
1278 emacs_close (1);
1279 emacs_close (2);
1281 dup2 (in, 0);
1282 dup2 (out, 1);
1283 dup2 (err, 2);
1284 emacs_close (in);
1285 if (out != in)
1286 emacs_close (out);
1287 if (err != in && err != out)
1288 emacs_close (err);
1290 #if defined(USG)
1291 #ifndef SETPGRP_RELEASES_CTTY
1292 setpgrp (); /* No arguments but equivalent in this case */
1293 #endif
1294 #else /* not USG */
1295 setpgrp (pid, pid);
1296 #endif /* not USG */
1298 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1299 tcsetpgrp (0, pid);
1301 /* execvp does not accept an environment arg so the only way
1302 to pass this environment is to set environ. Our caller
1303 is responsible for restoring the ambient value of environ. */
1304 environ = env;
1305 execvp (new_argv[0], new_argv);
1307 emacs_write (1, "Can't exec program: ", 20);
1308 emacs_write (1, new_argv[0], strlen (new_argv[0]));
1309 emacs_write (1, "\n", 1);
1310 _exit (1);
1312 #else /* MSDOS */
1313 pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env);
1314 xfree (pwd_var);
1315 if (pid == -1)
1316 /* An error occurred while trying to run the subprocess. */
1317 report_file_error ("Spawning child process", Qnil);
1318 return pid;
1319 #endif /* MSDOS */
1320 #endif /* not WINDOWSNT */
1323 #ifndef WINDOWSNT
1324 /* Move the file descriptor FD so that its number is not less than MINFD.
1325 If the file descriptor is moved at all, the original is freed. */
1326 static int
1327 relocate_fd (int fd, int minfd)
1329 if (fd >= minfd)
1330 return fd;
1331 else
1333 int new;
1334 #ifdef F_DUPFD
1335 new = fcntl (fd, F_DUPFD, minfd);
1336 #else
1337 new = dup (fd);
1338 if (new != -1)
1339 /* Note that we hold the original FD open while we recurse,
1340 to guarantee we'll get a new FD if we need it. */
1341 new = relocate_fd (new, minfd);
1342 #endif
1343 if (new == -1)
1345 const char *message_1 = "Error while setting up child: ";
1346 const char *errmessage = strerror (errno);
1347 const char *message_2 = "\n";
1348 emacs_write (2, message_1, strlen (message_1));
1349 emacs_write (2, errmessage, strlen (errmessage));
1350 emacs_write (2, message_2, strlen (message_2));
1351 _exit (1);
1353 emacs_close (fd);
1354 return new;
1357 #endif /* not WINDOWSNT */
1359 static int
1360 getenv_internal_1 (const char *var, int varlen, char **value, int *valuelen,
1361 Lisp_Object env)
1363 for (; CONSP (env); env = XCDR (env))
1365 Lisp_Object entry = XCAR (env);
1366 if (STRINGP (entry)
1367 && SBYTES (entry) >= varlen
1368 #ifdef WINDOWSNT
1369 /* NT environment variables are case insensitive. */
1370 && ! strnicmp (SDATA (entry), var, varlen)
1371 #else /* not WINDOWSNT */
1372 && ! memcmp (SDATA (entry), var, varlen)
1373 #endif /* not WINDOWSNT */
1376 if (SBYTES (entry) > varlen && SREF (entry, varlen) == '=')
1378 *value = SSDATA (entry) + (varlen + 1);
1379 *valuelen = SBYTES (entry) - (varlen + 1);
1380 return 1;
1382 else if (SBYTES (entry) == varlen)
1384 /* Lone variable names in Vprocess_environment mean that
1385 variable should be removed from the environment. */
1386 *value = NULL;
1387 return 1;
1391 return 0;
1394 static int
1395 getenv_internal (const char *var, int varlen, char **value, int *valuelen,
1396 Lisp_Object frame)
1398 /* Try to find VAR in Vprocess_environment first. */
1399 if (getenv_internal_1 (var, varlen, value, valuelen,
1400 Vprocess_environment))
1401 return *value ? 1 : 0;
1403 /* For DISPLAY try to get the values from the frame or the initial env. */
1404 if (strcmp (var, "DISPLAY") == 0)
1406 Lisp_Object display
1407 = Fframe_parameter (NILP (frame) ? selected_frame : frame, Qdisplay);
1408 if (STRINGP (display))
1410 *value = SSDATA (display);
1411 *valuelen = SBYTES (display);
1412 return 1;
1414 /* If still not found, Look for DISPLAY in Vinitial_environment. */
1415 if (getenv_internal_1 (var, varlen, value, valuelen,
1416 Vinitial_environment))
1417 return *value ? 1 : 0;
1420 return 0;
1423 DEFUN ("getenv-internal", Fgetenv_internal, Sgetenv_internal, 1, 2, 0,
1424 doc: /* Get the value of environment variable VARIABLE.
1425 VARIABLE should be a string. Value is nil if VARIABLE is undefined in
1426 the environment. Otherwise, value is a string.
1428 This function searches `process-environment' for VARIABLE.
1430 If optional parameter ENV is a list, then search this list instead of
1431 `process-environment', and return t when encountering a negative entry
1432 \(an entry for a variable with no value). */)
1433 (Lisp_Object variable, Lisp_Object env)
1435 char *value;
1436 int valuelen;
1438 CHECK_STRING (variable);
1439 if (CONSP (env))
1441 if (getenv_internal_1 (SSDATA (variable), SBYTES (variable),
1442 &value, &valuelen, env))
1443 return value ? make_string (value, valuelen) : Qt;
1444 else
1445 return Qnil;
1447 else if (getenv_internal (SSDATA (variable), SBYTES (variable),
1448 &value, &valuelen, env))
1449 return make_string (value, valuelen);
1450 else
1451 return Qnil;
1454 /* A version of getenv that consults the Lisp environment lists,
1455 easily callable from C. */
1456 char *
1457 egetenv (const char *var)
1459 char *value;
1460 int valuelen;
1462 if (getenv_internal (var, strlen (var), &value, &valuelen, Qnil))
1463 return value;
1464 else
1465 return 0;
1469 /* This is run before init_cmdargs. */
1471 void
1472 init_callproc_1 (void)
1474 char *data_dir = egetenv ("EMACSDATA");
1475 char *doc_dir = egetenv ("EMACSDOC");
1477 Vdata_directory
1478 = Ffile_name_as_directory (build_string (data_dir ? data_dir
1479 : PATH_DATA));
1480 Vdoc_directory
1481 = Ffile_name_as_directory (build_string (doc_dir ? doc_dir
1482 : PATH_DOC));
1484 /* Check the EMACSPATH environment variable, defaulting to the
1485 PATH_EXEC path from epaths.h. */
1486 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
1487 Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
1488 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
1491 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1493 void
1494 init_callproc (void)
1496 char *data_dir = egetenv ("EMACSDATA");
1498 register char * sh;
1499 Lisp_Object tempdir;
1501 if (!NILP (Vinstallation_directory))
1503 /* Add to the path the lib-src subdir of the installation dir. */
1504 Lisp_Object tem;
1505 tem = Fexpand_file_name (build_string ("lib-src"),
1506 Vinstallation_directory);
1507 #ifndef DOS_NT
1508 /* MSDOS uses wrapped binaries, so don't do this. */
1509 if (NILP (Fmember (tem, Vexec_path)))
1511 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
1512 Vexec_path = Fcons (tem, Vexec_path);
1513 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
1516 Vexec_directory = Ffile_name_as_directory (tem);
1517 #endif /* not DOS_NT */
1519 /* Maybe use ../etc as well as ../lib-src. */
1520 if (data_dir == 0)
1522 tem = Fexpand_file_name (build_string ("etc"),
1523 Vinstallation_directory);
1524 Vdoc_directory = Ffile_name_as_directory (tem);
1528 /* Look for the files that should be in etc. We don't use
1529 Vinstallation_directory, because these files are never installed
1530 near the executable, and they are never in the build
1531 directory when that's different from the source directory.
1533 Instead, if these files are not in the nominal place, we try the
1534 source directory. */
1535 if (data_dir == 0)
1537 Lisp_Object tem, tem1, srcdir;
1539 srcdir = Fexpand_file_name (build_string ("../src/"),
1540 build_string (PATH_DUMPLOADSEARCH));
1541 tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory);
1542 tem1 = Ffile_exists_p (tem);
1543 if (!NILP (Fequal (srcdir, Vinvocation_directory)) || NILP (tem1))
1545 Lisp_Object newdir;
1546 newdir = Fexpand_file_name (build_string ("../etc/"),
1547 build_string (PATH_DUMPLOADSEARCH));
1548 tem = Fexpand_file_name (build_string ("GNU"), newdir);
1549 tem1 = Ffile_exists_p (tem);
1550 if (!NILP (tem1))
1551 Vdata_directory = newdir;
1555 #ifndef CANNOT_DUMP
1556 if (initialized)
1557 #endif
1559 tempdir = Fdirectory_file_name (Vexec_directory);
1560 if (access (SSDATA (tempdir), 0) < 0)
1561 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1562 Vexec_directory);
1565 tempdir = Fdirectory_file_name (Vdata_directory);
1566 if (access (SSDATA (tempdir), 0) < 0)
1567 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1568 Vdata_directory);
1570 sh = (char *) getenv ("SHELL");
1571 Vshell_file_name = build_string (sh ? sh : "/bin/sh");
1573 #ifdef DOS_NT
1574 Vshared_game_score_directory = Qnil;
1575 #else
1576 Vshared_game_score_directory = build_string (PATH_GAME);
1577 if (NILP (Ffile_directory_p (Vshared_game_score_directory)))
1578 Vshared_game_score_directory = Qnil;
1579 #endif
1582 void
1583 set_initial_environment (void)
1585 register char **envp;
1586 #ifdef CANNOT_DUMP
1587 Vprocess_environment = Qnil;
1588 #else
1589 if (initialized)
1590 #endif
1592 for (envp = environ; *envp; envp++)
1593 Vprocess_environment = Fcons (build_string (*envp),
1594 Vprocess_environment);
1595 /* Ideally, the `copy' shouldn't be necessary, but it seems it's frequent
1596 to use `delete' and friends on process-environment. */
1597 Vinitial_environment = Fcopy_sequence (Vprocess_environment);
1601 void
1602 syms_of_callproc (void)
1604 #ifndef DOS_NT
1605 Vtemp_file_name_pattern = build_string ("emacsXXXXXX");
1606 #elif defined (WINDOWSNT)
1607 Vtemp_file_name_pattern = build_string ("emXXXXXX");
1608 #else
1609 Vtemp_file_name_pattern = build_string ("detmp.XXX");
1610 #endif
1611 staticpro (&Vtemp_file_name_pattern);
1613 DEFVAR_LISP ("shell-file-name", Vshell_file_name,
1614 doc: /* *File name to load inferior shells from.
1615 Initialized from the SHELL environment variable, or to a system-dependent
1616 default if SHELL is not set. */);
1618 DEFVAR_LISP ("exec-path", Vexec_path,
1619 doc: /* *List of directories to search programs to run in subprocesses.
1620 Each element is a string (directory name) or nil (try default directory). */);
1622 DEFVAR_LISP ("exec-suffixes", Vexec_suffixes,
1623 doc: /* *List of suffixes to try to find executable file names.
1624 Each element is a string. */);
1625 Vexec_suffixes = Qnil;
1627 DEFVAR_LISP ("exec-directory", Vexec_directory,
1628 doc: /* Directory for executables for Emacs to invoke.
1629 More generally, this includes any architecture-dependent files
1630 that are built and installed from the Emacs distribution. */);
1632 DEFVAR_LISP ("data-directory", Vdata_directory,
1633 doc: /* Directory of machine-independent files that come with GNU Emacs.
1634 These are files intended for Emacs to use while it runs. */);
1636 DEFVAR_LISP ("doc-directory", Vdoc_directory,
1637 doc: /* Directory containing the DOC file that comes with GNU Emacs.
1638 This is usually the same as `data-directory'. */);
1640 DEFVAR_LISP ("configure-info-directory", Vconfigure_info_directory,
1641 doc: /* For internal use by the build procedure only.
1642 This is the name of the directory in which the build procedure installed
1643 Emacs's info files; the default value for `Info-default-directory-list'
1644 includes this. */);
1645 Vconfigure_info_directory = build_string (PATH_INFO);
1647 DEFVAR_LISP ("shared-game-score-directory", Vshared_game_score_directory,
1648 doc: /* Directory of score files for games which come with GNU Emacs.
1649 If this variable is nil, then Emacs is unable to use a shared directory. */);
1650 #ifdef DOS_NT
1651 Vshared_game_score_directory = Qnil;
1652 #else
1653 Vshared_game_score_directory = build_string (PATH_GAME);
1654 #endif
1656 DEFVAR_LISP ("initial-environment", Vinitial_environment,
1657 doc: /* List of environment variables inherited from the parent process.
1658 Each element should be a string of the form ENVVARNAME=VALUE.
1659 The elements must normally be decoded (using `locale-coding-system') for use. */);
1660 Vinitial_environment = Qnil;
1662 DEFVAR_LISP ("process-environment", Vprocess_environment,
1663 doc: /* List of overridden environment variables for subprocesses to inherit.
1664 Each element should be a string of the form ENVVARNAME=VALUE.
1666 Entries in this list take precedence to those in the frame-local
1667 environments. Therefore, let-binding `process-environment' is an easy
1668 way to temporarily change the value of an environment variable,
1669 irrespective of where it comes from. To use `process-environment' to
1670 remove an environment variable, include only its name in the list,
1671 without "=VALUE".
1673 This variable is set to nil when Emacs starts.
1675 If multiple entries define the same variable, the first one always
1676 takes precedence.
1678 Non-ASCII characters are encoded according to the initial value of
1679 `locale-coding-system', i.e. the elements must normally be decoded for
1680 use.
1682 See `setenv' and `getenv'. */);
1683 Vprocess_environment = Qnil;
1685 defsubr (&Scall_process);
1686 defsubr (&Sgetenv_internal);
1687 defsubr (&Scall_process_region);