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