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