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