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