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