]> code.delx.au - gnu-emacs/blob - src/callproc.c
Merge from emacs--devo--0
[gnu-emacs] / src / callproc.c
1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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 2, or (at your option)
10 any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22
23 #include <config.h>
24 #include <signal.h>
25 #include <errno.h>
26 #include <stdio.h>
27
28 #ifndef USE_CRT_DLL
29 extern int errno;
30 #endif
31
32 /* Define SIGCHLD as an alias for SIGCLD. */
33
34 #if !defined (SIGCHLD) && defined (SIGCLD)
35 #define SIGCHLD SIGCLD
36 #endif /* SIGCLD */
37
38 #include <sys/types.h>
39
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43
44 #include <sys/file.h>
45 #ifdef HAVE_FCNTL_H
46 #define INCLUDED_FCNTL
47 #include <fcntl.h>
48 #endif
49
50 #ifdef WINDOWSNT
51 #define NOMINMAX
52 #include <windows.h>
53 #include <stdlib.h> /* for proper declaration of environ */
54 #include <fcntl.h>
55 #include "w32.h"
56 #define _P_NOWAIT 1 /* from process.h */
57 #endif
58
59 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
60 #define INCLUDED_FCNTL
61 #include <fcntl.h>
62 #include <sys/stat.h>
63 #include <sys/param.h>
64 #include <errno.h>
65 #endif /* MSDOS */
66
67 #ifndef O_RDONLY
68 #define O_RDONLY 0
69 #endif
70
71 #ifndef O_WRONLY
72 #define O_WRONLY 1
73 #endif
74
75 #include "lisp.h"
76 #include "commands.h"
77 #include "buffer.h"
78 #include "character.h"
79 #include "ccl.h"
80 #include "coding.h"
81 #include "composite.h"
82 #include <epaths.h>
83 #include "process.h"
84 #include "syssignal.h"
85 #include "systty.h"
86 #include "blockinput.h"
87
88 #ifdef MSDOS
89 #include "msdos.h"
90 #endif
91
92 #ifdef VMS
93 extern noshare char **environ;
94 #else
95 #ifndef USE_CRT_DLL
96 extern char **environ;
97 #endif
98 #endif
99
100 #ifdef HAVE_SETPGID
101 #if !defined (USG) || defined (BSD_PGRPS)
102 #undef setpgrp
103 #define setpgrp setpgid
104 #endif
105 #endif
106
107 Lisp_Object Vexec_path, Vexec_directory, Vexec_suffixes;
108 Lisp_Object Vdata_directory, Vdoc_directory;
109 Lisp_Object Vconfigure_info_directory, Vshared_game_score_directory;
110 Lisp_Object Vtemp_file_name_pattern;
111
112 Lisp_Object Vshell_file_name;
113
114 Lisp_Object Vprocess_environment;
115
116 #ifdef DOS_NT
117 Lisp_Object Qbuffer_file_type;
118 #endif /* DOS_NT */
119
120 /* True iff we are about to fork off a synchronous process or if we
121 are waiting for it. */
122 int synch_process_alive;
123
124 /* Nonzero => this is a string explaining death of synchronous subprocess. */
125 char *synch_process_death;
126
127 /* Nonzero => this is the signal number that terminated the subprocess. */
128 int synch_process_termsig;
129
130 /* If synch_process_death is zero,
131 this is exit code of synchronous subprocess. */
132 int synch_process_retcode;
133 \f
134 /* Clean up when exiting Fcall_process.
135 On MSDOS, delete the temporary file on any kind of termination.
136 On Unix, kill the process and any children on termination by signal. */
137
138 /* Nonzero if this is termination due to exit. */
139 static int call_process_exited;
140
141 #ifndef VMS /* VMS version is in vmsproc.c. */
142
143 static Lisp_Object
144 call_process_kill (fdpid)
145 Lisp_Object fdpid;
146 {
147 emacs_close (XFASTINT (Fcar (fdpid)));
148 EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
149 synch_process_alive = 0;
150 return Qnil;
151 }
152
153 Lisp_Object
154 call_process_cleanup (fdpid)
155 Lisp_Object fdpid;
156 {
157 #if defined (MSDOS) || defined (MAC_OS8)
158 /* for MSDOS fdpid is really (fd . tempfile) */
159 register Lisp_Object file;
160 file = Fcdr (fdpid);
161 emacs_close (XFASTINT (Fcar (fdpid)));
162 if (strcmp (SDATA (file), NULL_DEVICE) != 0)
163 unlink (SDATA (file));
164 #else /* not MSDOS and not MAC_OS8 */
165 register int pid = XFASTINT (Fcdr (fdpid));
166
167 if (call_process_exited)
168 {
169 emacs_close (XFASTINT (Fcar (fdpid)));
170 return Qnil;
171 }
172
173 if (EMACS_KILLPG (pid, SIGINT) == 0)
174 {
175 int count = SPECPDL_INDEX ();
176 record_unwind_protect (call_process_kill, fdpid);
177 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
178 immediate_quit = 1;
179 QUIT;
180 wait_for_termination (pid);
181 immediate_quit = 0;
182 specpdl_ptr = specpdl + count; /* Discard the unwind protect. */
183 message1 ("Waiting for process to die...done");
184 }
185 synch_process_alive = 0;
186 emacs_close (XFASTINT (Fcar (fdpid)));
187 #endif /* not MSDOS */
188 return Qnil;
189 }
190
191 DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
192 doc: /* Call PROGRAM synchronously in separate process.
193 The remaining arguments are optional.
194 The program's input comes from file INFILE (nil means `/dev/null').
195 Insert output in BUFFER before point; t means current buffer;
196 nil for BUFFER means discard it; 0 means discard and don't wait.
197 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
198 REAL-BUFFER says what to do with standard output, as above,
199 while STDERR-FILE says what to do with standard error in the child.
200 STDERR-FILE may be nil (discard standard error output),
201 t (mix it with ordinary output), or a file name string.
202
203 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
204 Remaining arguments are strings passed as command arguments to PROGRAM.
205
206 If executable PROGRAM can't be found as an executable, `call-process'
207 signals a Lisp error. `call-process' reports errors in execution of
208 the program only through its return and output.
209
210 If BUFFER is 0, `call-process' returns immediately with value nil.
211 Otherwise it waits for PROGRAM to terminate
212 and returns a numeric exit status or a signal description string.
213 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
214
215 usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */)
216 (nargs, args)
217 int nargs;
218 register Lisp_Object *args;
219 {
220 Lisp_Object infile, buffer, current_dir, path;
221 int display_p;
222 int fd[2];
223 int filefd;
224 register int pid;
225 #define CALLPROC_BUFFER_SIZE_MIN (16 * 1024)
226 #define CALLPROC_BUFFER_SIZE_MAX (4 * CALLPROC_BUFFER_SIZE_MIN)
227 char buf[CALLPROC_BUFFER_SIZE_MAX];
228 int bufsize = CALLPROC_BUFFER_SIZE_MIN;
229 int count = SPECPDL_INDEX ();
230
231 register const unsigned char **new_argv
232 = (const unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
233 struct buffer *old = current_buffer;
234 /* File to use for stderr in the child.
235 t means use same as standard output. */
236 Lisp_Object error_file;
237 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
238 char *outf, *tempfile;
239 int outfilefd;
240 #endif
241 #ifdef MAC_OS8
242 char *tempfile;
243 int outfilefd;
244 #endif
245 #if 0
246 int mask;
247 #endif
248 struct coding_system process_coding; /* coding-system of process output */
249 struct coding_system argument_coding; /* coding-system of arguments */
250 /* Set to the return value of Ffind_operation_coding_system. */
251 Lisp_Object coding_systems;
252
253 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
254 coding_systems = Qt;
255
256 CHECK_STRING (args[0]);
257
258 error_file = Qt;
259
260 #ifndef subprocesses
261 /* Without asynchronous processes we cannot have BUFFER == 0. */
262 if (nargs >= 3
263 && (INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2])))
264 error ("Operating system cannot handle asynchronous subprocesses");
265 #endif /* subprocesses */
266
267 /* Decide the coding-system for giving arguments. */
268 {
269 Lisp_Object val, *args2;
270 int i;
271
272 /* If arguments are supplied, we may have to encode them. */
273 if (nargs >= 5)
274 {
275 int must_encode = 0;
276 Lisp_Object coding_attrs;
277
278 for (i = 4; i < nargs; i++)
279 CHECK_STRING (args[i]);
280
281 for (i = 4; i < nargs; i++)
282 if (STRING_MULTIBYTE (args[i]))
283 must_encode = 1;
284
285 if (!NILP (Vcoding_system_for_write))
286 val = Vcoding_system_for_write;
287 else if (! must_encode)
288 val = Qnil;
289 else
290 {
291 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
292 args2[0] = Qcall_process;
293 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
294 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
295 if (CONSP (coding_systems))
296 val = XCDR (coding_systems);
297 else if (CONSP (Vdefault_process_coding_system))
298 val = XCDR (Vdefault_process_coding_system);
299 else
300 val = Qnil;
301 }
302 val = coding_inherit_eol_type (val, Qnil);
303 setup_coding_system (Fcheck_coding_system (val), &argument_coding);
304 coding_attrs = CODING_ID_ATTRS (argument_coding.id);
305 if (NILP (CODING_ATTR_ASCII_COMPAT (coding_attrs)))
306 {
307 /* We should not use an ASCII incompatible coding system. */
308 val = raw_text_coding_system (val);
309 setup_coding_system (val, &argument_coding);
310 }
311 }
312 }
313
314 if (nargs >= 2 && ! NILP (args[1]))
315 {
316 infile = Fexpand_file_name (args[1], current_buffer->directory);
317 CHECK_STRING (infile);
318 }
319 else
320 infile = build_string (NULL_DEVICE);
321
322 if (nargs >= 3)
323 {
324 buffer = args[2];
325
326 /* If BUFFER is a list, its meaning is
327 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
328 if (CONSP (buffer))
329 {
330 if (CONSP (XCDR (buffer)))
331 {
332 Lisp_Object stderr_file;
333 stderr_file = XCAR (XCDR (buffer));
334
335 if (NILP (stderr_file) || EQ (Qt, stderr_file))
336 error_file = stderr_file;
337 else
338 error_file = Fexpand_file_name (stderr_file, Qnil);
339 }
340
341 buffer = XCAR (buffer);
342 }
343
344 if (!(EQ (buffer, Qnil)
345 || EQ (buffer, Qt)
346 || INTEGERP (buffer)))
347 {
348 Lisp_Object spec_buffer;
349 spec_buffer = buffer;
350 buffer = Fget_buffer_create (buffer);
351 /* Mention the buffer name for a better error message. */
352 if (NILP (buffer))
353 CHECK_BUFFER (spec_buffer);
354 CHECK_BUFFER (buffer);
355 }
356 }
357 else
358 buffer = Qnil;
359
360 /* Make sure that the child will be able to chdir to the current
361 buffer's current directory, or its unhandled equivalent. We
362 can't just have the child check for an error when it does the
363 chdir, since it's in a vfork.
364
365 We have to GCPRO around this because Fexpand_file_name,
366 Funhandled_file_name_directory, and Ffile_accessible_directory_p
367 might call a file name handling function. The argument list is
368 protected by the caller, so all we really have to worry about is
369 buffer. */
370 {
371 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
372
373 current_dir = current_buffer->directory;
374
375 GCPRO4 (infile, buffer, current_dir, error_file);
376
377 current_dir
378 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
379 Qnil);
380 if (NILP (Ffile_accessible_directory_p (current_dir)))
381 report_file_error ("Setting current directory",
382 Fcons (current_buffer->directory, Qnil));
383
384 if (STRING_MULTIBYTE (infile))
385 infile = ENCODE_FILE (infile);
386 if (STRING_MULTIBYTE (current_dir))
387 current_dir = ENCODE_FILE (current_dir);
388 if (STRINGP (error_file) && STRING_MULTIBYTE (error_file))
389 error_file = ENCODE_FILE (error_file);
390 UNGCPRO;
391 }
392
393 display_p = INTERACTIVE && nargs >= 4 && !NILP (args[3]);
394
395 filefd = emacs_open (SDATA (infile), O_RDONLY, 0);
396 if (filefd < 0)
397 {
398 infile = DECODE_FILE (infile);
399 report_file_error ("Opening process input file", Fcons (infile, Qnil));
400 }
401 /* Search for program; barf if not found. */
402 {
403 struct gcpro gcpro1;
404
405 GCPRO1 (current_dir);
406 openp (Vexec_path, args[0], Vexec_suffixes, &path, make_number (X_OK));
407 UNGCPRO;
408 }
409 if (NILP (path))
410 {
411 emacs_close (filefd);
412 report_file_error ("Searching for program", Fcons (args[0], Qnil));
413 }
414
415 /* If program file name starts with /: for quoting a magic name,
416 discard that. */
417 if (SBYTES (path) > 2 && SREF (path, 0) == '/'
418 && SREF (path, 1) == ':')
419 path = Fsubstring (path, make_number (2), Qnil);
420
421 new_argv[0] = SDATA (path);
422 if (nargs > 4)
423 {
424 register int i;
425 struct gcpro gcpro1, gcpro2, gcpro3;
426
427 GCPRO3 (infile, buffer, current_dir);
428 argument_coding.dst_multibyte = 0;
429 for (i = 4; i < nargs; i++)
430 {
431 argument_coding.src_multibyte = STRING_MULTIBYTE (args[i]);
432 if (CODING_REQUIRE_ENCODING (&argument_coding))
433 /* We must encode this argument. */
434 args[i] = encode_coding_string (&argument_coding, args[i], 1);
435 new_argv[i - 3] = SDATA (args[i]);
436 }
437 UNGCPRO;
438 new_argv[nargs - 3] = 0;
439 }
440 else
441 new_argv[1] = 0;
442
443 #ifdef MSDOS /* MW, July 1993 */
444 if ((outf = egetenv ("TMPDIR")))
445 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
446 else
447 {
448 tempfile = alloca (20);
449 *tempfile = '\0';
450 }
451 dostounix_filename (tempfile);
452 if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/')
453 strcat (tempfile, "/");
454 strcat (tempfile, "detmp.XXX");
455 mktemp (tempfile);
456
457 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
458 if (outfilefd < 0)
459 {
460 emacs_close (filefd);
461 report_file_error ("Opening process output file",
462 Fcons (build_string (tempfile), Qnil));
463 }
464 fd[0] = filefd;
465 fd[1] = outfilefd;
466 #endif /* MSDOS */
467
468 #ifdef MAC_OS8
469 /* Since we don't have pipes on the Mac, create a temporary file to
470 hold the output of the subprocess. */
471 tempfile = (char *) alloca (SBYTES (Vtemp_file_name_pattern) + 1);
472 bcopy (SDATA (Vtemp_file_name_pattern), tempfile,
473 SBYTES (Vtemp_file_name_pattern) + 1);
474
475 mktemp (tempfile);
476
477 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
478 if (outfilefd < 0)
479 {
480 close (filefd);
481 report_file_error ("Opening process output file",
482 Fcons (build_string (tempfile), Qnil));
483 }
484 fd[0] = filefd;
485 fd[1] = outfilefd;
486 #endif /* MAC_OS8 */
487
488 if (INTEGERP (buffer))
489 fd[1] = emacs_open (NULL_DEVICE, O_WRONLY, 0), fd[0] = -1;
490 else
491 {
492 #ifndef MSDOS
493 #ifndef MAC_OS8
494 errno = 0;
495 if (pipe (fd) == -1)
496 {
497 emacs_close (filefd);
498 report_file_error ("Creating process pipe", Qnil);
499 }
500 #endif
501 #endif
502 #if 0
503 /* Replaced by close_process_descs */
504 set_exclusive_use (fd[0]);
505 #endif
506 }
507
508 {
509 /* child_setup must clobber environ in systems with true vfork.
510 Protect it from permanent change. */
511 register char **save_environ = environ;
512 register int fd1 = fd[1];
513 int fd_error = fd1;
514
515 #if 0 /* Some systems don't have sigblock. */
516 mask = sigblock (sigmask (SIGCHLD));
517 #endif
518
519 /* Record that we're about to create a synchronous process. */
520 synch_process_alive = 1;
521
522 /* These vars record information from process termination.
523 Clear them now before process can possibly terminate,
524 to avoid timing error if process terminates soon. */
525 synch_process_death = 0;
526 synch_process_retcode = 0;
527 synch_process_termsig = 0;
528
529 if (NILP (error_file))
530 fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0);
531 else if (STRINGP (error_file))
532 {
533 #ifdef DOS_NT
534 fd_error = emacs_open (SDATA (error_file),
535 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
536 S_IREAD | S_IWRITE);
537 #else /* not DOS_NT */
538 fd_error = creat (SDATA (error_file), 0666);
539 #endif /* not DOS_NT */
540 }
541
542 if (fd_error < 0)
543 {
544 emacs_close (filefd);
545 if (fd[0] != filefd)
546 emacs_close (fd[0]);
547 if (fd1 >= 0)
548 emacs_close (fd1);
549 #ifdef MSDOS
550 unlink (tempfile);
551 #endif
552 if (NILP (error_file))
553 error_file = build_string (NULL_DEVICE);
554 else if (STRINGP (error_file))
555 error_file = DECODE_FILE (error_file);
556 report_file_error ("Cannot redirect stderr", Fcons (error_file, Qnil));
557 }
558
559 #ifdef MAC_OS8
560 {
561 /* Call run_mac_command in sysdep.c here directly instead of doing
562 a child_setup as for MSDOS and other platforms. Note that this
563 code does not handle passing the environment to the synchronous
564 Mac subprocess. */
565 char *infn, *outfn, *errfn, *currdn;
566
567 /* close these files so subprocess can write to them */
568 close (outfilefd);
569 if (fd_error != outfilefd)
570 close (fd_error);
571 fd1 = -1; /* No harm in closing that one! */
572
573 infn = SDATA (infile);
574 outfn = tempfile;
575 if (NILP (error_file))
576 errfn = NULL_DEVICE;
577 else if (EQ (Qt, error_file))
578 errfn = outfn;
579 else
580 errfn = SDATA (error_file);
581 currdn = SDATA (current_dir);
582 pid = run_mac_command (new_argv, currdn, infn, outfn, errfn);
583
584 /* Record that the synchronous process exited and note its
585 termination status. */
586 synch_process_alive = 0;
587 synch_process_retcode = pid;
588 if (synch_process_retcode < 0) /* means it couldn't be exec'ed */
589 {
590 synchronize_system_messages_locale ();
591 synch_process_death = strerror (errno);
592 }
593
594 /* Since CRLF is converted to LF within `decode_coding', we can
595 always open a file with binary mode. */
596 fd[0] = open (tempfile, O_BINARY);
597 if (fd[0] < 0)
598 {
599 unlink (tempfile);
600 close (filefd);
601 report_file_error ("Cannot re-open temporary file", Qnil);
602 }
603 }
604 #else /* not MAC_OS8 */
605 #ifdef MSDOS /* MW, July 1993 */
606 /* Note that on MSDOS `child_setup' actually returns the child process
607 exit status, not its PID, so we assign it to `synch_process_retcode'
608 below. */
609 pid = child_setup (filefd, outfilefd, fd_error, (char **) new_argv,
610 0, current_dir);
611
612 /* Record that the synchronous process exited and note its
613 termination status. */
614 synch_process_alive = 0;
615 synch_process_retcode = pid;
616 if (synch_process_retcode < 0) /* means it couldn't be exec'ed */
617 {
618 synchronize_system_messages_locale ();
619 synch_process_death = strerror (errno);
620 }
621
622 emacs_close (outfilefd);
623 if (fd_error != outfilefd)
624 emacs_close (fd_error);
625 fd1 = -1; /* No harm in closing that one! */
626 /* Since CRLF is converted to LF within `decode_coding', we can
627 always open a file with binary mode. */
628 fd[0] = emacs_open (tempfile, O_RDONLY | O_BINARY, 0);
629 if (fd[0] < 0)
630 {
631 unlink (tempfile);
632 emacs_close (filefd);
633 report_file_error ("Cannot re-open temporary file", Qnil);
634 }
635 #else /* not MSDOS */
636 #ifdef WINDOWSNT
637 pid = child_setup (filefd, fd1, fd_error, (char **) new_argv,
638 0, current_dir);
639 #else /* not WINDOWSNT */
640 BLOCK_INPUT;
641
642 pid = vfork ();
643
644 if (pid == 0)
645 {
646 if (fd[0] >= 0)
647 emacs_close (fd[0]);
648 #ifdef HAVE_SETSID
649 setsid ();
650 #endif
651 #if defined (USG) && !defined (BSD_PGRPS)
652 setpgrp ();
653 #else
654 setpgrp (pid, pid);
655 #endif /* USG */
656 child_setup (filefd, fd1, fd_error, (char **) new_argv,
657 0, current_dir);
658 }
659
660 UNBLOCK_INPUT;
661 #endif /* not WINDOWSNT */
662
663 /* The MSDOS case did this already. */
664 if (fd_error >= 0)
665 emacs_close (fd_error);
666 #endif /* not MSDOS */
667 #endif /* not MAC_OS8 */
668
669 environ = save_environ;
670
671 /* Close most of our fd's, but not fd[0]
672 since we will use that to read input from. */
673 emacs_close (filefd);
674 if (fd1 >= 0 && fd1 != fd_error)
675 emacs_close (fd1);
676 }
677
678 if (pid < 0)
679 {
680 if (fd[0] >= 0)
681 emacs_close (fd[0]);
682 report_file_error ("Doing vfork", Qnil);
683 }
684
685 if (INTEGERP (buffer))
686 {
687 if (fd[0] >= 0)
688 emacs_close (fd[0]);
689 #ifndef subprocesses
690 /* If Emacs has been built with asynchronous subprocess support,
691 we don't need to do this, I think because it will then have
692 the facilities for handling SIGCHLD. */
693 wait_without_blocking ();
694 #endif /* subprocesses */
695 return Qnil;
696 }
697
698 /* Enable sending signal if user quits below. */
699 call_process_exited = 0;
700
701 #if defined(MSDOS) || defined(MAC_OS8)
702 /* MSDOS needs different cleanup information. */
703 record_unwind_protect (call_process_cleanup,
704 Fcons (make_number (fd[0]), build_string (tempfile)));
705 #else
706 record_unwind_protect (call_process_cleanup,
707 Fcons (make_number (fd[0]), make_number (pid)));
708 #endif /* not MSDOS and not MAC_OS8 */
709
710
711 if (BUFFERP (buffer))
712 Fset_buffer (buffer);
713
714 if (NILP (buffer))
715 {
716 /* If BUFFER is nil, we must read process output once and then
717 discard it, so setup coding system but with nil. */
718 setup_coding_system (Qnil, &process_coding);
719 }
720 else
721 {
722 Lisp_Object val, *args2;
723
724 val = Qnil;
725 if (!NILP (Vcoding_system_for_read))
726 val = Vcoding_system_for_read;
727 else
728 {
729 if (EQ (coding_systems, Qt))
730 {
731 int i;
732
733 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
734 args2[0] = Qcall_process;
735 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
736 coding_systems
737 = Ffind_operation_coding_system (nargs + 1, args2);
738 }
739 if (CONSP (coding_systems))
740 val = XCAR (coding_systems);
741 else if (CONSP (Vdefault_process_coding_system))
742 val = XCAR (Vdefault_process_coding_system);
743 else
744 val = Qnil;
745 }
746 Fcheck_coding_system (val);
747 /* In unibyte mode, character code conversion should not take
748 place but EOL conversion should. So, setup raw-text or one
749 of the subsidiary according to the information just setup. */
750 if (NILP (current_buffer->enable_multibyte_characters)
751 && !NILP (val))
752 val = raw_text_coding_system (val);
753 setup_coding_system (val, &process_coding);
754 }
755
756 immediate_quit = 1;
757 QUIT;
758
759 {
760 register int nread;
761 int first = 1;
762 int total_read = 0;
763 int carryover = 0;
764 int display_on_the_fly = display_p;
765 struct coding_system saved_coding;
766
767 saved_coding = process_coding;
768 while (1)
769 {
770 /* Repeatedly read until we've filled as much as possible
771 of the buffer size we have. But don't read
772 less than 1024--save that for the next bufferful. */
773 nread = carryover;
774 while (nread < bufsize - 1024)
775 {
776 int this_read = emacs_read (fd[0], buf + nread,
777 bufsize - nread);
778
779 if (this_read < 0)
780 goto give_up;
781
782 if (this_read == 0)
783 {
784 process_coding.mode |= CODING_MODE_LAST_BLOCK;
785 break;
786 }
787
788 nread += this_read;
789 total_read += this_read;
790
791 if (display_on_the_fly)
792 break;
793 }
794
795 /* Now NREAD is the total amount of data in the buffer. */
796 immediate_quit = 0;
797
798 if (!NILP (buffer))
799 {
800 if (NILP (current_buffer->enable_multibyte_characters)
801 && ! CODING_MAY_REQUIRE_DECODING (&process_coding))
802 insert_1_both (buf, nread, nread, 0, 1, 0);
803 else
804 { /* We have to decode the input. */
805 Lisp_Object curbuf;
806
807 XSETBUFFER (curbuf, current_buffer);
808 decode_coding_c_string (&process_coding, buf, nread,
809 curbuf);
810 if (display_on_the_fly
811 && CODING_REQUIRE_DETECTION (&saved_coding)
812 && ! CODING_REQUIRE_DETECTION (&process_coding))
813 {
814 /* We have detected some coding system. But,
815 there's a possibility that the detection was
816 done by insufficient data. So, we give up
817 displaying on the fly. */
818 if (process_coding.produced > 0)
819 del_range_2 (process_coding.dst_pos,
820 process_coding.dst_pos_byte,
821 process_coding.dst_pos
822 + process_coding.produced_char,
823 process_coding.dst_pos_byte
824 + process_coding.produced, 0);
825 display_on_the_fly = 0;
826 process_coding = saved_coding;
827 carryover = nread;
828 /* This is to make the above condition always
829 fails in the future. */
830 saved_coding.common_flags
831 &= ~CODING_REQUIRE_DETECTION_MASK;
832 continue;
833 }
834
835 TEMP_SET_PT_BOTH (PT + process_coding.produced_char,
836 PT_BYTE + process_coding.produced);
837 carryover = process_coding.carryover_bytes;
838 if (carryover > 0)
839 /* As CARRYOVER should not be that large, we had
840 better avoid overhead of bcopy. */
841 BCOPY_SHORT (process_coding.carryover, buf,
842 process_coding.carryover_bytes);
843 }
844 }
845
846 if (process_coding.mode & CODING_MODE_LAST_BLOCK)
847 break;
848
849 #if (CALLPROC_BUFFER_SIZE_MIN != CALLPROC_BUFFER_SIZE_MAX)
850 /* Make the buffer bigger as we continue to read more data,
851 but not past CALLPROC_BUFFER_SIZE_MAX. */
852 if (bufsize < CALLPROC_BUFFER_SIZE_MAX && total_read > 32 * bufsize)
853 if ((bufsize *= 2) > CALLPROC_BUFFER_SIZE_MAX)
854 bufsize = CALLPROC_BUFFER_SIZE_MAX;
855 #endif
856
857 if (display_p)
858 {
859 if (first)
860 prepare_menu_bars ();
861 first = 0;
862 redisplay_preserve_echo_area (1);
863 /* This variable might have been set to 0 for code
864 detection. In that case, we set it back to 1 because
865 we should have already detected a coding system. */
866 display_on_the_fly = 1;
867 }
868 immediate_quit = 1;
869 QUIT;
870 }
871 give_up: ;
872
873 Vlast_coding_system_used = CODING_ID_NAME (process_coding.id);
874 /* If the caller required, let the buffer inherit the
875 coding-system used to decode the process output. */
876 if (inherit_process_coding_system)
877 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
878 make_number (total_read));
879 }
880
881 /* Wait for it to terminate, unless it already has. */
882 wait_for_termination (pid);
883
884 immediate_quit = 0;
885
886 set_buffer_internal (old);
887
888 /* Don't kill any children that the subprocess may have left behind
889 when exiting. */
890 call_process_exited = 1;
891
892 unbind_to (count, Qnil);
893
894 if (synch_process_termsig)
895 {
896 char *signame;
897
898 synchronize_system_messages_locale ();
899 signame = strsignal (synch_process_termsig);
900
901 if (signame == 0)
902 signame = "unknown";
903
904 synch_process_death = signame;
905 }
906
907 if (synch_process_death)
908 return code_convert_string_norecord (build_string (synch_process_death),
909 Vlocale_coding_system, 0);
910 return make_number (synch_process_retcode);
911 }
912 #endif
913 \f
914 static Lisp_Object
915 delete_temp_file (name)
916 Lisp_Object name;
917 {
918 /* Suppress jka-compr handling, etc. */
919 int count = SPECPDL_INDEX ();
920 specbind (intern ("file-name-handler-alist"), Qnil);
921 internal_delete_file (name);
922 unbind_to (count, Qnil);
923 return Qnil;
924 }
925
926 DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
927 3, MANY, 0,
928 doc: /* Send text from START to END to a synchronous process running PROGRAM.
929 The remaining arguments are optional.
930 Delete the text if fourth arg DELETE is non-nil.
931
932 Insert output in BUFFER before point; t means current buffer;
933 nil for BUFFER means discard it; 0 means discard and don't wait.
934 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
935 REAL-BUFFER says what to do with standard output, as above,
936 while STDERR-FILE says what to do with standard error in the child.
937 STDERR-FILE may be nil (discard standard error output),
938 t (mix it with ordinary output), or a file name string.
939
940 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.
941 Remaining args are passed to PROGRAM at startup as command args.
942
943 If BUFFER is 0, `call-process-region' returns immediately with value nil.
944 Otherwise it waits for PROGRAM to terminate
945 and returns a numeric exit status or a signal description string.
946 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
947
948 usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */)
949 (nargs, args)
950 int nargs;
951 register Lisp_Object *args;
952 {
953 struct gcpro gcpro1;
954 Lisp_Object filename_string;
955 register Lisp_Object start, end;
956 int count = SPECPDL_INDEX ();
957 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
958 Lisp_Object coding_systems;
959 Lisp_Object val, *args2;
960 int i;
961 #ifdef DOS_NT
962 char *tempfile;
963 char *outf = '\0';
964
965 if ((outf = egetenv ("TMPDIR"))
966 || (outf = egetenv ("TMP"))
967 || (outf = egetenv ("TEMP")))
968 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
969 else
970 {
971 tempfile = alloca (20);
972 *tempfile = '\0';
973 }
974 if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1]))
975 strcat (tempfile, "/");
976 if ('/' == DIRECTORY_SEP)
977 dostounix_filename (tempfile);
978 else
979 unixtodos_filename (tempfile);
980 #ifdef WINDOWSNT
981 strcat (tempfile, "emXXXXXX");
982 #else
983 strcat (tempfile, "detmp.XXX");
984 #endif
985 #else /* not DOS_NT */
986 char *tempfile = (char *) alloca (SBYTES (Vtemp_file_name_pattern) + 1);
987 bcopy (SDATA (Vtemp_file_name_pattern), tempfile,
988 SBYTES (Vtemp_file_name_pattern) + 1);
989 #endif /* not DOS_NT */
990
991 coding_systems = Qt;
992
993 #ifdef HAVE_MKSTEMP
994 {
995 int fd;
996
997 BLOCK_INPUT;
998 fd = mkstemp (tempfile);
999 UNBLOCK_INPUT;
1000 if (fd == -1)
1001 report_file_error ("Failed to open temporary file",
1002 Fcons (Vtemp_file_name_pattern, Qnil));
1003 else
1004 close (fd);
1005 }
1006 #else
1007 mktemp (tempfile);
1008 #endif
1009
1010 filename_string = build_string (tempfile);
1011 GCPRO1 (filename_string);
1012 start = args[0];
1013 end = args[1];
1014 /* Decide coding-system of the contents of the temporary file. */
1015 if (!NILP (Vcoding_system_for_write))
1016 val = Vcoding_system_for_write;
1017 else if (NILP (current_buffer->enable_multibyte_characters))
1018 val = Qnil;
1019 else
1020 {
1021 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
1022 args2[0] = Qcall_process_region;
1023 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
1024 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
1025 if (CONSP (coding_systems))
1026 val = XCDR (coding_systems);
1027 else if (CONSP (Vdefault_process_coding_system))
1028 val = XCDR (Vdefault_process_coding_system);
1029 else
1030 val = Qnil;
1031 }
1032
1033 {
1034 int count1 = SPECPDL_INDEX ();
1035
1036 specbind (intern ("coding-system-for-write"), val);
1037 /* POSIX lets mk[s]temp use "."; don't invoke jka-compr if we
1038 happen to get a ".Z" suffix. */
1039 specbind (intern ("file-name-handler-alist"), Qnil);
1040 Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil);
1041
1042 unbind_to (count1, Qnil);
1043 }
1044
1045 /* Note that Fcall_process takes care of binding
1046 coding-system-for-read. */
1047
1048 record_unwind_protect (delete_temp_file, filename_string);
1049
1050 if (nargs > 3 && !NILP (args[3]))
1051 Fdelete_region (start, end);
1052
1053 if (nargs > 3)
1054 {
1055 args += 2;
1056 nargs -= 2;
1057 }
1058 else
1059 {
1060 args[0] = args[2];
1061 nargs = 2;
1062 }
1063 args[1] = filename_string;
1064
1065 RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs, args)));
1066 }
1067 \f
1068 #ifndef VMS /* VMS version is in vmsproc.c. */
1069
1070 static int relocate_fd ();
1071
1072 /* This is the last thing run in a newly forked inferior
1073 either synchronous or asynchronous.
1074 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1075 Initialize inferior's priority, pgrp, connected dir and environment.
1076 then exec another program based on new_argv.
1077
1078 This function may change environ for the superior process.
1079 Therefore, the superior process must save and restore the value
1080 of environ around the vfork and the call to this function.
1081
1082 SET_PGRP is nonzero if we should put the subprocess into a separate
1083 process group.
1084
1085 CURRENT_DIR is an elisp string giving the path of the current
1086 directory the subprocess should have. Since we can't really signal
1087 a decent error from within the child, this should be verified as an
1088 executable directory by the parent. */
1089
1090 int
1091 child_setup (in, out, err, new_argv, set_pgrp, current_dir)
1092 int in, out, err;
1093 register char **new_argv;
1094 int set_pgrp;
1095 Lisp_Object current_dir;
1096 {
1097 char **env;
1098 char *pwd_var;
1099 #ifdef WINDOWSNT
1100 int cpid;
1101 HANDLE handles[3];
1102 #endif /* WINDOWSNT */
1103
1104 int pid = getpid ();
1105
1106 #ifdef SET_EMACS_PRIORITY
1107 {
1108 extern EMACS_INT emacs_priority;
1109
1110 if (emacs_priority < 0)
1111 nice (- emacs_priority);
1112 }
1113 #endif
1114
1115 #ifdef subprocesses
1116 /* Close Emacs's descriptors that this process should not have. */
1117 close_process_descs ();
1118 #endif
1119 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1120 we will lose if we call close_load_descs here. */
1121 #ifndef DOS_NT
1122 close_load_descs ();
1123 #endif
1124
1125 /* Note that use of alloca is always safe here. It's obvious for systems
1126 that do not have true vfork or that have true (stack) alloca.
1127 If using vfork and C_ALLOCA it is safe because that changes
1128 the superior's static variables as if the superior had done alloca
1129 and will be cleaned up in the usual way. */
1130 {
1131 register char *temp;
1132 register int i;
1133
1134 i = SBYTES (current_dir);
1135 #ifdef MSDOS
1136 /* MSDOS must have all environment variables malloc'ed, because
1137 low-level libc functions that launch subsidiary processes rely
1138 on that. */
1139 pwd_var = (char *) xmalloc (i + 6);
1140 #else
1141 pwd_var = (char *) alloca (i + 6);
1142 #endif
1143 temp = pwd_var + 4;
1144 bcopy ("PWD=", pwd_var, 4);
1145 bcopy (SDATA (current_dir), temp, i);
1146 if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP;
1147 temp[i] = 0;
1148
1149 #ifndef DOS_NT
1150 /* We can't signal an Elisp error here; we're in a vfork. Since
1151 the callers check the current directory before forking, this
1152 should only return an error if the directory's permissions
1153 are changed between the check and this chdir, but we should
1154 at least check. */
1155 if (chdir (temp) < 0)
1156 _exit (errno);
1157 #endif
1158
1159 #ifdef DOS_NT
1160 /* Get past the drive letter, so that d:/ is left alone. */
1161 if (i > 2 && IS_DEVICE_SEP (temp[1]) && IS_DIRECTORY_SEP (temp[2]))
1162 {
1163 temp += 2;
1164 i -= 2;
1165 }
1166 #endif
1167
1168 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1169 while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1]))
1170 temp[--i] = 0;
1171 }
1172
1173 /* Set `env' to a vector of the strings in Vprocess_environment. */
1174 {
1175 register Lisp_Object tem;
1176 register char **new_env;
1177 register int new_length;
1178
1179 new_length = 0;
1180 for (tem = Vprocess_environment;
1181 CONSP (tem) && STRINGP (XCAR (tem));
1182 tem = XCDR (tem))
1183 new_length++;
1184
1185 /* new_length + 2 to include PWD and terminating 0. */
1186 env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *));
1187
1188 /* If we have a PWD envvar, pass one down,
1189 but with corrected value. */
1190 if (getenv ("PWD"))
1191 *new_env++ = pwd_var;
1192
1193 /* Copy the Vprocess_environment strings into new_env. */
1194 for (tem = Vprocess_environment;
1195 CONSP (tem) && STRINGP (XCAR (tem));
1196 tem = XCDR (tem))
1197 {
1198 char **ep = env;
1199 char *string = (char *) SDATA (XCAR (tem));
1200 /* See if this string duplicates any string already in the env.
1201 If so, don't put it in.
1202 When an env var has multiple definitions,
1203 we keep the definition that comes first in process-environment. */
1204 for (; ep != new_env; ep++)
1205 {
1206 char *p = *ep, *q = string;
1207 while (1)
1208 {
1209 if (*q == 0)
1210 /* The string is malformed; might as well drop it. */
1211 goto duplicate;
1212 if (*q != *p)
1213 break;
1214 if (*q == '=')
1215 goto duplicate;
1216 p++, q++;
1217 }
1218 }
1219 *new_env++ = string;
1220 duplicate: ;
1221 }
1222 *new_env = 0;
1223 }
1224 #ifdef WINDOWSNT
1225 prepare_standard_handles (in, out, err, handles);
1226 set_process_dir (SDATA (current_dir));
1227 #else /* not WINDOWSNT */
1228 /* Make sure that in, out, and err are not actually already in
1229 descriptors zero, one, or two; this could happen if Emacs is
1230 started with its standard in, out, or error closed, as might
1231 happen under X. */
1232 {
1233 int oin = in, oout = out;
1234
1235 /* We have to avoid relocating the same descriptor twice! */
1236
1237 in = relocate_fd (in, 3);
1238
1239 if (out == oin)
1240 out = in;
1241 else
1242 out = relocate_fd (out, 3);
1243
1244 if (err == oin)
1245 err = in;
1246 else if (err == oout)
1247 err = out;
1248 else
1249 err = relocate_fd (err, 3);
1250 }
1251
1252 #ifndef MSDOS
1253 emacs_close (0);
1254 emacs_close (1);
1255 emacs_close (2);
1256
1257 dup2 (in, 0);
1258 dup2 (out, 1);
1259 dup2 (err, 2);
1260 emacs_close (in);
1261 emacs_close (out);
1262 emacs_close (err);
1263 #endif /* not MSDOS */
1264 #endif /* not WINDOWSNT */
1265
1266 #if defined(USG) && !defined(BSD_PGRPS)
1267 #ifndef SETPGRP_RELEASES_CTTY
1268 setpgrp (); /* No arguments but equivalent in this case */
1269 #endif
1270 #else
1271 setpgrp (pid, pid);
1272 #endif /* USG */
1273 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1274 EMACS_SET_TTY_PGRP (0, &pid);
1275
1276 #ifdef MSDOS
1277 pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env);
1278 xfree (pwd_var);
1279 if (pid == -1)
1280 /* An error occurred while trying to run the subprocess. */
1281 report_file_error ("Spawning child process", Qnil);
1282 return pid;
1283 #else /* not MSDOS */
1284 #ifdef WINDOWSNT
1285 /* Spawn the child. (See ntproc.c:Spawnve). */
1286 cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
1287 reset_standard_handles (in, out, err, handles);
1288 if (cpid == -1)
1289 /* An error occurred while trying to spawn the process. */
1290 report_file_error ("Spawning child process", Qnil);
1291 return cpid;
1292 #else /* not WINDOWSNT */
1293 /* execvp does not accept an environment arg so the only way
1294 to pass this environment is to set environ. Our caller
1295 is responsible for restoring the ambient value of environ. */
1296 environ = env;
1297 execvp (new_argv[0], new_argv);
1298
1299 emacs_write (1, "Can't exec program: ", 20);
1300 emacs_write (1, new_argv[0], strlen (new_argv[0]));
1301 emacs_write (1, "\n", 1);
1302 _exit (1);
1303 #endif /* not WINDOWSNT */
1304 #endif /* not MSDOS */
1305 }
1306
1307 /* Move the file descriptor FD so that its number is not less than MINFD.
1308 If the file descriptor is moved at all, the original is freed. */
1309 static int
1310 relocate_fd (fd, minfd)
1311 int fd, minfd;
1312 {
1313 if (fd >= minfd)
1314 return fd;
1315 else
1316 {
1317 int new = dup (fd);
1318 if (new == -1)
1319 {
1320 char *message1 = "Error while setting up child: ";
1321 char *errmessage = strerror (errno);
1322 char *message2 = "\n";
1323 emacs_write (2, message1, strlen (message1));
1324 emacs_write (2, errmessage, strlen (errmessage));
1325 emacs_write (2, message2, strlen (message2));
1326 _exit (1);
1327 }
1328 /* Note that we hold the original FD open while we recurse,
1329 to guarantee we'll get a new FD if we need it. */
1330 new = relocate_fd (new, minfd);
1331 emacs_close (fd);
1332 return new;
1333 }
1334 }
1335
1336 static int
1337 getenv_internal (var, varlen, value, valuelen)
1338 char *var;
1339 int varlen;
1340 char **value;
1341 int *valuelen;
1342 {
1343 Lisp_Object scan;
1344
1345 for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan))
1346 {
1347 Lisp_Object entry;
1348
1349 entry = XCAR (scan);
1350 if (STRINGP (entry)
1351 && SBYTES (entry) > varlen
1352 && SREF (entry, varlen) == '='
1353 #ifdef WINDOWSNT
1354 /* NT environment variables are case insensitive. */
1355 && ! strnicmp (SDATA (entry), var, varlen)
1356 #else /* not WINDOWSNT */
1357 && ! bcmp (SDATA (entry), var, varlen)
1358 #endif /* not WINDOWSNT */
1359 )
1360 {
1361 *value = (char *) SDATA (entry) + (varlen + 1);
1362 *valuelen = SBYTES (entry) - (varlen + 1);
1363 return 1;
1364 }
1365 }
1366
1367 return 0;
1368 }
1369
1370 DEFUN ("getenv-internal", Fgetenv_internal, Sgetenv_internal, 1, 1, 0,
1371 doc: /* Return the value of environment variable VAR, as a string.
1372 VAR should be a string. Value is nil if VAR is undefined in the environment.
1373 This function consults the variable `process-environment' for its value. */)
1374 (var)
1375 Lisp_Object var;
1376 {
1377 char *value;
1378 int valuelen;
1379
1380 CHECK_STRING (var);
1381 if (getenv_internal (SDATA (var), SBYTES (var),
1382 &value, &valuelen))
1383 return make_string (value, valuelen);
1384 else
1385 return Qnil;
1386 }
1387
1388 /* A version of getenv that consults process_environment, easily
1389 callable from C. */
1390 char *
1391 egetenv (var)
1392 char *var;
1393 {
1394 char *value;
1395 int valuelen;
1396
1397 if (getenv_internal (var, strlen (var), &value, &valuelen))
1398 return value;
1399 else
1400 return 0;
1401 }
1402
1403 #endif /* not VMS */
1404 \f
1405 /* This is run before init_cmdargs. */
1406
1407 void
1408 init_callproc_1 ()
1409 {
1410 char *data_dir = egetenv ("EMACSDATA");
1411 char *doc_dir = egetenv ("EMACSDOC");
1412
1413 Vdata_directory
1414 = Ffile_name_as_directory (build_string (data_dir ? data_dir
1415 : PATH_DATA));
1416 Vdoc_directory
1417 = Ffile_name_as_directory (build_string (doc_dir ? doc_dir
1418 : PATH_DOC));
1419
1420 /* Check the EMACSPATH environment variable, defaulting to the
1421 PATH_EXEC path from epaths.h. */
1422 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
1423 Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
1424 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
1425 }
1426
1427 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1428
1429 void
1430 init_callproc ()
1431 {
1432 char *data_dir = egetenv ("EMACSDATA");
1433
1434 register char * sh;
1435 Lisp_Object tempdir;
1436
1437 if (!NILP (Vinstallation_directory))
1438 {
1439 /* Add to the path the lib-src subdir of the installation dir. */
1440 Lisp_Object tem;
1441 tem = Fexpand_file_name (build_string ("lib-src"),
1442 Vinstallation_directory);
1443 #ifndef DOS_NT
1444 /* MSDOS uses wrapped binaries, so don't do this. */
1445 if (NILP (Fmember (tem, Vexec_path)))
1446 {
1447 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
1448 Vexec_path = Fcons (tem, Vexec_path);
1449 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
1450 }
1451
1452 Vexec_directory = Ffile_name_as_directory (tem);
1453 #endif /* not DOS_NT */
1454
1455 /* Maybe use ../etc as well as ../lib-src. */
1456 if (data_dir == 0)
1457 {
1458 tem = Fexpand_file_name (build_string ("etc"),
1459 Vinstallation_directory);
1460 Vdoc_directory = Ffile_name_as_directory (tem);
1461 }
1462 }
1463
1464 /* Look for the files that should be in etc. We don't use
1465 Vinstallation_directory, because these files are never installed
1466 near the executable, and they are never in the build
1467 directory when that's different from the source directory.
1468
1469 Instead, if these files are not in the nominal place, we try the
1470 source directory. */
1471 if (data_dir == 0)
1472 {
1473 Lisp_Object tem, tem1, srcdir;
1474
1475 srcdir = Fexpand_file_name (build_string ("../src/"),
1476 build_string (PATH_DUMPLOADSEARCH));
1477 tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory);
1478 tem1 = Ffile_exists_p (tem);
1479 if (!NILP (Fequal (srcdir, Vinvocation_directory)) || NILP (tem1))
1480 {
1481 Lisp_Object newdir;
1482 newdir = Fexpand_file_name (build_string ("../etc/"),
1483 build_string (PATH_DUMPLOADSEARCH));
1484 tem = Fexpand_file_name (build_string ("GNU"), newdir);
1485 tem1 = Ffile_exists_p (tem);
1486 if (!NILP (tem1))
1487 Vdata_directory = newdir;
1488 }
1489 }
1490
1491 #ifndef CANNOT_DUMP
1492 if (initialized)
1493 #endif
1494 {
1495 tempdir = Fdirectory_file_name (Vexec_directory);
1496 if (access (SDATA (tempdir), 0) < 0)
1497 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1498 Vexec_directory);
1499 }
1500
1501 tempdir = Fdirectory_file_name (Vdata_directory);
1502 if (access (SDATA (tempdir), 0) < 0)
1503 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1504 Vdata_directory);
1505
1506 #ifdef VMS
1507 Vshell_file_name = build_string ("*dcl*");
1508 #else
1509 sh = (char *) getenv ("SHELL");
1510 Vshell_file_name = build_string (sh ? sh : "/bin/sh");
1511 #endif
1512
1513 #ifdef VMS
1514 Vtemp_file_name_pattern = build_string ("tmp:emacsXXXXXX.");
1515 #else
1516 if (getenv ("TMPDIR"))
1517 {
1518 char *dir = getenv ("TMPDIR");
1519 Vtemp_file_name_pattern
1520 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1521 build_string (dir));
1522 }
1523 else
1524 Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX");
1525 #endif
1526
1527 #ifdef DOS_NT
1528 Vshared_game_score_directory = Qnil;
1529 #else
1530 Vshared_game_score_directory = build_string (PATH_GAME);
1531 if (NILP (Ffile_directory_p (Vshared_game_score_directory)))
1532 Vshared_game_score_directory = Qnil;
1533 #endif
1534 }
1535
1536 void
1537 set_process_environment ()
1538 {
1539 register char **envp;
1540
1541 Vprocess_environment = Qnil;
1542 #ifndef CANNOT_DUMP
1543 if (initialized)
1544 #endif
1545 for (envp = environ; *envp; envp++)
1546 Vprocess_environment = Fcons (build_string (*envp),
1547 Vprocess_environment);
1548 }
1549
1550 void
1551 syms_of_callproc ()
1552 {
1553 #ifdef DOS_NT
1554 Qbuffer_file_type = intern ("buffer-file-type");
1555 staticpro (&Qbuffer_file_type);
1556 #endif /* DOS_NT */
1557
1558 DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
1559 doc: /* *File name to load inferior shells from.
1560 Initialized from the SHELL environment variable, or to a system-dependent
1561 default if SHELL is not set. */);
1562
1563 DEFVAR_LISP ("exec-path", &Vexec_path,
1564 doc: /* *List of directories to search programs to run in subprocesses.
1565 Each element is a string (directory name) or nil (try default directory). */);
1566
1567 DEFVAR_LISP ("exec-suffixes", &Vexec_suffixes,
1568 doc: /* *List of suffixes to try to find executable file names.
1569 Each element is a string. */);
1570 Vexec_suffixes = Qnil;
1571
1572 DEFVAR_LISP ("exec-directory", &Vexec_directory,
1573 doc: /* Directory for executables for Emacs to invoke.
1574 More generally, this includes any architecture-dependent files
1575 that are built and installed from the Emacs distribution. */);
1576
1577 DEFVAR_LISP ("data-directory", &Vdata_directory,
1578 doc: /* Directory of machine-independent files that come with GNU Emacs.
1579 These are files intended for Emacs to use while it runs. */);
1580
1581 DEFVAR_LISP ("doc-directory", &Vdoc_directory,
1582 doc: /* Directory containing the DOC file that comes with GNU Emacs.
1583 This is usually the same as `data-directory'. */);
1584
1585 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory,
1586 doc: /* For internal use by the build procedure only.
1587 This is the name of the directory in which the build procedure installed
1588 Emacs's info files; the default value for `Info-default-directory-list'
1589 includes this. */);
1590 Vconfigure_info_directory = build_string (PATH_INFO);
1591
1592 DEFVAR_LISP ("shared-game-score-directory", &Vshared_game_score_directory,
1593 doc: /* Directory of score files for games which come with GNU Emacs.
1594 If this variable is nil, then Emacs is unable to use a shared directory. */);
1595 #ifdef DOS_NT
1596 Vshared_game_score_directory = Qnil;
1597 #else
1598 Vshared_game_score_directory = build_string (PATH_GAME);
1599 #endif
1600
1601 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern,
1602 doc: /* Pattern for making names for temporary files.
1603 This is used by `call-process-region'. */);
1604 /* This variable is initialized in init_callproc. */
1605
1606 DEFVAR_LISP ("process-environment", &Vprocess_environment,
1607 doc: /* List of environment variables for subprocesses to inherit.
1608 Each element should be a string of the form ENVVARNAME=VALUE.
1609 If multiple entries define the same variable, the first one always
1610 takes precedence.
1611 The environment which Emacs inherits is placed in this variable
1612 when Emacs starts.
1613 Non-ASCII characters are encoded according to the initial value of
1614 `locale-coding-system', i.e. the elements must normally be decoded for use.
1615 See `setenv' and `getenv'. */);
1616
1617 #ifndef VMS
1618 defsubr (&Scall_process);
1619 defsubr (&Sgetenv_internal);
1620 #endif
1621 defsubr (&Scall_process_region);
1622 }
1623
1624 /* arch-tag: 769b8045-1df7-4d2b-8968-e3fb49017f95
1625 (do not change this comment) */