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