Doc and coding conventions fixes.
[emacs/old-mirror.git] / src / callproc.c
blob9101f264ccecf7cf1e102e2db3e0d5f7f12564cf
1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985,86,87,88,93,94,95,99, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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. */
23 #include <config.h>
24 #include <signal.h>
25 #include <errno.h>
26 #include <stdio.h>
28 #ifndef USE_CRT_DLL
29 extern int errno;
30 #endif
32 /* Define SIGCHLD as an alias for SIGCLD. */
34 #if !defined (SIGCHLD) && defined (SIGCLD)
35 #define SIGCHLD SIGCLD
36 #endif /* SIGCLD */
38 #include <sys/types.h>
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
44 #include <sys/file.h>
45 #ifdef USG5
46 #define INCLUDED_FCNTL
47 #include <fcntl.h>
48 #endif
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
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 */
67 #ifndef O_RDONLY
68 #define O_RDONLY 0
69 #endif
71 #ifndef O_WRONLY
72 #define O_WRONLY 1
73 #endif
75 #include "lisp.h"
76 #include "commands.h"
77 #include "buffer.h"
78 #include "charset.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"
87 #ifdef MSDOS
88 #include "msdos.h"
89 #endif
91 #ifdef VMS
92 extern noshare char **environ;
93 #else
94 #ifndef USE_CRT_DLL
95 extern char **environ;
96 #endif
97 #endif
99 #ifdef HAVE_SETPGID
100 #if !defined (USG) || defined (BSD_PGRPS)
101 #undef setpgrp
102 #define setpgrp setpgid
103 #endif
104 #endif
106 #define max(a, b) ((a) > (b) ? (a) : (b))
108 Lisp_Object Vexec_path, Vexec_directory, Vdata_directory, Vdoc_directory;
109 Lisp_Object Vconfigure_info_directory;
110 Lisp_Object Vtemp_file_name_pattern;
112 Lisp_Object Vshell_file_name;
114 Lisp_Object Vprocess_environment;
116 #ifdef DOS_NT
117 Lisp_Object Qbuffer_file_type;
118 #endif /* DOS_NT */
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;
124 /* Nonzero => this is a string explaining death of synchronous subprocess. */
125 char *synch_process_death;
127 /* If synch_process_death is zero,
128 this is exit code of synchronous subprocess. */
129 int synch_process_retcode;
131 extern Lisp_Object Vdoc_file_name;
133 extern Lisp_Object Vfile_name_coding_system, Vdefault_file_name_coding_system;
135 /* Clean up when exiting Fcall_process.
136 On MSDOS, delete the temporary file on any kind of termination.
137 On Unix, kill the process and any children on termination by signal. */
139 /* Nonzero if this is termination due to exit. */
140 static int call_process_exited;
142 #ifndef VMS /* VMS version is in vmsproc.c. */
144 static Lisp_Object
145 call_process_kill (fdpid)
146 Lisp_Object fdpid;
148 emacs_close (XFASTINT (Fcar (fdpid)));
149 EMACS_KILLPG (XFASTINT (Fcdr (fdpid)), SIGKILL);
150 synch_process_alive = 0;
151 return Qnil;
154 Lisp_Object
155 call_process_cleanup (fdpid)
156 Lisp_Object fdpid;
158 #if defined (MSDOS) || defined (macintosh)
159 /* for MSDOS fdpid is really (fd . tempfile) */
160 register Lisp_Object file;
161 file = Fcdr (fdpid);
162 emacs_close (XFASTINT (Fcar (fdpid)));
163 if (strcmp (XSTRING (file)-> data, NULL_DEVICE) != 0)
164 unlink (XSTRING (file)->data);
165 #else /* not MSDOS and not macintosh */
166 register int pid = XFASTINT (Fcdr (fdpid));
168 if (call_process_exited)
170 emacs_close (XFASTINT (Fcar (fdpid)));
171 return Qnil;
174 if (EMACS_KILLPG (pid, SIGINT) == 0)
176 int count = specpdl_ptr - specpdl;
177 record_unwind_protect (call_process_kill, fdpid);
178 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
179 immediate_quit = 1;
180 QUIT;
181 wait_for_termination (pid);
182 immediate_quit = 0;
183 specpdl_ptr = specpdl + count; /* Discard the unwind protect. */
184 message1 ("Waiting for process to die...done");
186 synch_process_alive = 0;
187 emacs_close (XFASTINT (Fcar (fdpid)));
188 #endif /* not MSDOS */
189 return Qnil;
192 DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
193 "Call PROGRAM synchronously in separate process.\n\
194 The remaining arguments are optional.\n\
195 The program's input comes from file INFILE (nil means `/dev/null').\n\
196 Insert output in BUFFER before point; t means current buffer;\n\
197 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
198 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
199 REAL-BUFFER says what to do with standard output, as above,\n\
200 while STDERR-FILE says what to do with standard error in the child.\n\
201 STDERR-FILE may be nil (discard standard error output),\n\
202 t (mix it with ordinary output), or a file name string.\n\
204 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
205 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
207 If BUFFER is 0, `call-process' returns immediately with value nil.\n\
208 Otherwise it waits for PROGRAM to terminate\n\
209 and returns a numeric exit status or a signal description string.\n\
210 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
211 (nargs, args)
212 int nargs;
213 register Lisp_Object *args;
215 Lisp_Object infile, buffer, current_dir, display, path;
216 int fd[2];
217 int filefd;
218 register int pid;
219 char buf[16384];
220 char *bufptr = buf;
221 int bufsize = 16384;
222 int count = specpdl_ptr - specpdl;
224 register unsigned char **new_argv
225 = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
226 struct buffer *old = current_buffer;
227 /* File to use for stderr in the child.
228 t means use same as standard output. */
229 Lisp_Object error_file;
230 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
231 char *outf, *tempfile;
232 int outfilefd;
233 #endif
234 #ifdef macintosh
235 char *tempfile;
236 int outfilefd;
237 #endif
238 #if 0
239 int mask;
240 #endif
241 struct coding_system process_coding; /* coding-system of process output */
242 struct coding_system argument_coding; /* coding-system of arguments */
243 /* Set to the return value of Ffind_operation_coding_system. */
244 Lisp_Object coding_systems;
246 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
247 coding_systems = Qt;
249 CHECK_STRING (args[0], 0);
251 error_file = Qt;
253 #ifndef subprocesses
254 /* Without asynchronous processes we cannot have BUFFER == 0. */
255 if (nargs >= 3
256 && (INTEGERP (CONSP (args[2]) ? XCAR (args[2]) : args[2])))
257 error ("Operating system cannot handle asynchronous subprocesses");
258 #endif /* subprocesses */
260 /* Decide the coding-system for giving arguments. */
262 Lisp_Object val, *args2;
263 int i;
265 /* If arguments are supplied, we may have to encode them. */
266 if (nargs >= 5)
268 int must_encode = 0;
270 for (i = 4; i < nargs; i++)
271 CHECK_STRING (args[i], i);
273 for (i = 4; i < nargs; i++)
274 if (STRING_MULTIBYTE (args[i]))
275 must_encode = 1;
277 if (!NILP (Vcoding_system_for_write))
278 val = Vcoding_system_for_write;
279 else if (! must_encode)
280 val = Qnil;
281 else
283 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
284 args2[0] = Qcall_process;
285 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
286 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
287 if (CONSP (coding_systems))
288 val = XCDR (coding_systems);
289 else if (CONSP (Vdefault_process_coding_system))
290 val = XCDR (Vdefault_process_coding_system);
291 else
292 val = Qnil;
294 setup_coding_system (Fcheck_coding_system (val), &argument_coding);
298 if (nargs >= 2 && ! NILP (args[1]))
300 infile = Fexpand_file_name (args[1], current_buffer->directory);
301 CHECK_STRING (infile, 1);
303 else
304 infile = build_string (NULL_DEVICE);
306 if (nargs >= 3)
308 buffer = args[2];
310 /* If BUFFER is a list, its meaning is
311 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
312 if (CONSP (buffer))
314 if (CONSP (XCDR (buffer)))
316 Lisp_Object stderr_file;
317 stderr_file = XCAR (XCDR (buffer));
319 if (NILP (stderr_file) || EQ (Qt, stderr_file))
320 error_file = stderr_file;
321 else
322 error_file = Fexpand_file_name (stderr_file, Qnil);
325 buffer = XCAR (buffer);
328 if (!(EQ (buffer, Qnil)
329 || EQ (buffer, Qt)
330 || INTEGERP (buffer)))
332 Lisp_Object spec_buffer;
333 spec_buffer = buffer;
334 buffer = Fget_buffer_create (buffer);
335 /* Mention the buffer name for a better error message. */
336 if (NILP (buffer))
337 CHECK_BUFFER (spec_buffer, 2);
338 CHECK_BUFFER (buffer, 2);
341 else
342 buffer = Qnil;
344 /* Make sure that the child will be able to chdir to the current
345 buffer's current directory, or its unhandled equivalent. We
346 can't just have the child check for an error when it does the
347 chdir, since it's in a vfork.
349 We have to GCPRO around this because Fexpand_file_name,
350 Funhandled_file_name_directory, and Ffile_accessible_directory_p
351 might call a file name handling function. The argument list is
352 protected by the caller, so all we really have to worry about is
353 buffer. */
355 struct gcpro gcpro1, gcpro2, gcpro3;
357 current_dir = current_buffer->directory;
359 GCPRO3 (infile, buffer, current_dir);
361 current_dir
362 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
363 Qnil);
364 if (NILP (Ffile_accessible_directory_p (current_dir)))
365 report_file_error ("Setting current directory",
366 Fcons (current_buffer->directory, Qnil));
368 UNGCPRO;
371 display = nargs >= 4 ? args[3] : Qnil;
373 filefd = emacs_open (XSTRING (infile)->data, O_RDONLY, 0);
374 if (filefd < 0)
376 report_file_error ("Opening process input file", Fcons (infile, Qnil));
378 /* Search for program; barf if not found. */
380 struct gcpro gcpro1;
382 GCPRO1 (current_dir);
383 openp (Vexec_path, args[0], EXEC_SUFFIXES, &path, 1);
384 UNGCPRO;
386 if (NILP (path))
388 emacs_close (filefd);
389 report_file_error ("Searching for program", Fcons (args[0], Qnil));
391 new_argv[0] = XSTRING (path)->data;
392 if (nargs > 4)
394 register int i;
395 struct gcpro gcpro1, gcpro2, gcpro3;
397 GCPRO3 (infile, buffer, current_dir);
398 argument_coding.dst_multibyte = 0;
399 for (i = 4; i < nargs; i++)
401 argument_coding.src_multibyte = STRING_MULTIBYTE (args[i]);
402 if (CODING_REQUIRE_ENCODING (&argument_coding))
404 /* We must encode this argument. */
405 args[i] = encode_coding_string (args[i], &argument_coding, 1);
406 if (argument_coding.type == coding_type_ccl)
407 setup_ccl_program (&(argument_coding.spec.ccl.encoder), Qnil);
409 new_argv[i - 3] = XSTRING (args[i])->data;
411 UNGCPRO;
412 new_argv[nargs - 3] = 0;
414 else
415 new_argv[1] = 0;
417 #ifdef MSDOS /* MW, July 1993 */
418 if ((outf = egetenv ("TMPDIR")))
419 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
420 else
422 tempfile = alloca (20);
423 *tempfile = '\0';
425 dostounix_filename (tempfile);
426 if (*tempfile == '\0' || tempfile[strlen (tempfile) - 1] != '/')
427 strcat (tempfile, "/");
428 strcat (tempfile, "detmp.XXX");
429 mktemp (tempfile);
431 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
432 if (outfilefd < 0)
434 emacs_close (filefd);
435 report_file_error ("Opening process output file",
436 Fcons (build_string (tempfile), Qnil));
438 fd[0] = filefd;
439 fd[1] = outfilefd;
440 #endif /* MSDOS */
442 #ifdef macintosh
443 /* Since we don't have pipes on the Mac, create a temporary file to
444 hold the output of the subprocess. */
445 tempfile = (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern)) + 1);
446 bcopy (XSTRING (Vtemp_file_name_pattern)->data, tempfile,
447 STRING_BYTES (XSTRING (Vtemp_file_name_pattern)) + 1);
449 mktemp (tempfile);
451 outfilefd = creat (tempfile, S_IREAD | S_IWRITE);
452 if (outfilefd < 0)
454 close (filefd);
455 report_file_error ("Opening process output file",
456 Fcons (build_string (tempfile), Qnil));
458 fd[0] = filefd;
459 fd[1] = outfilefd;
460 #endif /* macintosh */
462 if (INTEGERP (buffer))
463 fd[1] = emacs_open (NULL_DEVICE, O_WRONLY, 0), fd[0] = -1;
464 else
466 #ifndef MSDOS
467 #ifndef macintosh
468 pipe (fd);
469 #endif
470 #endif
471 #if 0
472 /* Replaced by close_process_descs */
473 set_exclusive_use (fd[0]);
474 #endif
478 /* child_setup must clobber environ in systems with true vfork.
479 Protect it from permanent change. */
480 register char **save_environ = environ;
481 register int fd1 = fd[1];
482 int fd_error = fd1;
484 #if 0 /* Some systems don't have sigblock. */
485 mask = sigblock (sigmask (SIGCHLD));
486 #endif
488 /* Record that we're about to create a synchronous process. */
489 synch_process_alive = 1;
491 /* These vars record information from process termination.
492 Clear them now before process can possibly terminate,
493 to avoid timing error if process terminates soon. */
494 synch_process_death = 0;
495 synch_process_retcode = 0;
497 if (NILP (error_file))
498 fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0);
499 else if (STRINGP (error_file))
501 #ifdef DOS_NT
502 fd_error = emacs_open (XSTRING (error_file)->data,
503 O_WRONLY | O_TRUNC | O_CREAT | O_TEXT,
504 S_IREAD | S_IWRITE);
505 #else /* not DOS_NT */
506 fd_error = creat (XSTRING (error_file)->data, 0666);
507 #endif /* not DOS_NT */
510 if (fd_error < 0)
512 emacs_close (filefd);
513 if (fd[0] != filefd)
514 emacs_close (fd[0]);
515 if (fd1 >= 0)
516 emacs_close (fd1);
517 #ifdef MSDOS
518 unlink (tempfile);
519 #endif
520 report_file_error ("Cannot redirect stderr",
521 Fcons ((NILP (error_file)
522 ? build_string (NULL_DEVICE) : error_file),
523 Qnil));
526 current_dir = ENCODE_FILE (current_dir);
528 #ifdef macintosh
530 /* Call run_mac_command in sysdep.c here directly instead of doing
531 a child_setup as for MSDOS and other platforms. Note that this
532 code does not handle passing the environment to the synchronous
533 Mac subprocess. */
534 char *infn, *outfn, *errfn, *currdn;
536 /* close these files so subprocess can write to them */
537 close (outfilefd);
538 if (fd_error != outfilefd)
539 close (fd_error);
540 fd1 = -1; /* No harm in closing that one! */
542 infn = XSTRING (infile)->data;
543 outfn = tempfile;
544 if (NILP (error_file))
545 errfn = NULL_DEVICE;
546 else if (EQ (Qt, error_file))
547 errfn = outfn;
548 else
549 errfn = XSTRING (error_file)->data;
550 currdn = XSTRING (current_dir)->data;
551 pid = run_mac_command (new_argv, currdn, infn, outfn, errfn);
553 /* Record that the synchronous process exited and note its
554 termination status. */
555 synch_process_alive = 0;
556 synch_process_retcode = pid;
557 if (synch_process_retcode < 0) /* means it couldn't be exec'ed */
559 synchronize_system_messages_locale ();
560 synch_process_death = strerror (errno);
563 /* Since CRLF is converted to LF within `decode_coding', we can
564 always open a file with binary mode. */
565 fd[0] = open (tempfile, O_BINARY);
566 if (fd[0] < 0)
568 unlink (tempfile);
569 close (filefd);
570 report_file_error ("Cannot re-open temporary file", Qnil);
573 #else /* not macintosh */
574 #ifdef MSDOS /* MW, July 1993 */
575 /* Note that on MSDOS `child_setup' actually returns the child process
576 exit status, not its PID, so we assign it to `synch_process_retcode'
577 below. */
578 pid = child_setup (filefd, outfilefd, fd_error, (char **) new_argv,
579 0, current_dir);
581 /* Record that the synchronous process exited and note its
582 termination status. */
583 synch_process_alive = 0;
584 synch_process_retcode = pid;
585 if (synch_process_retcode < 0) /* means it couldn't be exec'ed */
587 synchronize_system_messages_locale ();
588 synch_process_death = strerror (errno);
591 emacs_close (outfilefd);
592 if (fd_error != outfilefd)
593 emacs_close (fd_error);
594 fd1 = -1; /* No harm in closing that one! */
595 /* Since CRLF is converted to LF within `decode_coding', we can
596 always open a file with binary mode. */
597 fd[0] = emacs_open (tempfile, O_RDONLY | O_BINARY, 0);
598 if (fd[0] < 0)
600 unlink (tempfile);
601 emacs_close (filefd);
602 report_file_error ("Cannot re-open temporary file", Qnil);
604 #else /* not MSDOS */
605 #ifdef WINDOWSNT
606 pid = child_setup (filefd, fd1, fd_error, (char **) new_argv,
607 0, current_dir);
608 #else /* not WINDOWSNT */
609 pid = vfork ();
611 if (pid == 0)
613 if (fd[0] >= 0)
614 emacs_close (fd[0]);
615 #ifdef HAVE_SETSID
616 setsid ();
617 #endif
618 #if defined (USG) && !defined (BSD_PGRPS)
619 setpgrp ();
620 #else
621 setpgrp (pid, pid);
622 #endif /* USG */
623 child_setup (filefd, fd1, fd_error, (char **) new_argv,
624 0, current_dir);
626 #endif /* not WINDOWSNT */
628 /* The MSDOS case did this already. */
629 if (fd_error >= 0)
630 emacs_close (fd_error);
631 #endif /* not MSDOS */
632 #endif /* not macintosh */
634 environ = save_environ;
636 /* Close most of our fd's, but not fd[0]
637 since we will use that to read input from. */
638 emacs_close (filefd);
639 if (fd1 >= 0 && fd1 != fd_error)
640 emacs_close (fd1);
643 if (pid < 0)
645 if (fd[0] >= 0)
646 emacs_close (fd[0]);
647 report_file_error ("Doing vfork", Qnil);
650 if (INTEGERP (buffer))
652 if (fd[0] >= 0)
653 emacs_close (fd[0]);
654 #ifndef subprocesses
655 /* If Emacs has been built with asynchronous subprocess support,
656 we don't need to do this, I think because it will then have
657 the facilities for handling SIGCHLD. */
658 wait_without_blocking ();
659 #endif /* subprocesses */
660 return Qnil;
663 /* Enable sending signal if user quits below. */
664 call_process_exited = 0;
666 #if defined(MSDOS) || defined(macintosh)
667 /* MSDOS needs different cleanup information. */
668 record_unwind_protect (call_process_cleanup,
669 Fcons (make_number (fd[0]), build_string (tempfile)));
670 #else
671 record_unwind_protect (call_process_cleanup,
672 Fcons (make_number (fd[0]), make_number (pid)));
673 #endif /* not MSDOS and not macintosh */
676 if (BUFFERP (buffer))
677 Fset_buffer (buffer);
679 if (NILP (buffer))
681 /* If BUFFER is nil, we must read process output once and then
682 discard it, so setup coding system but with nil. */
683 setup_coding_system (Qnil, &process_coding);
685 else
687 Lisp_Object val, *args2;
689 val = Qnil;
690 if (!NILP (Vcoding_system_for_read))
691 val = Vcoding_system_for_read;
692 else
694 if (EQ (coding_systems, Qt))
696 int i;
698 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
699 args2[0] = Qcall_process;
700 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
701 coding_systems
702 = Ffind_operation_coding_system (nargs + 1, args2);
704 if (CONSP (coding_systems))
705 val = XCAR (coding_systems);
706 else if (CONSP (Vdefault_process_coding_system))
707 val = XCAR (Vdefault_process_coding_system);
708 else
709 val = Qnil;
711 setup_coding_system (Fcheck_coding_system (val), &process_coding);
712 /* In unibyte mode, character code conversion should not take
713 place but EOL conversion should. So, setup raw-text or one
714 of the subsidiary according to the information just setup. */
715 if (NILP (current_buffer->enable_multibyte_characters)
716 && !NILP (val))
717 setup_raw_text_coding_system (&process_coding);
719 process_coding.src_multibyte = 0;
720 process_coding.dst_multibyte
721 = (BUFFERP (buffer)
722 ? ! NILP (XBUFFER (buffer)->enable_multibyte_characters)
723 : ! NILP (current_buffer->enable_multibyte_characters));
725 immediate_quit = 1;
726 QUIT;
729 register int nread;
730 int first = 1;
731 int total_read = 0;
732 int carryover = 0;
733 int display_on_the_fly = !NILP (display) && INTERACTIVE;
734 struct coding_system saved_coding;
735 int pt_orig = PT, pt_byte_orig = PT_BYTE;
736 int inserted;
738 saved_coding = process_coding;
739 if (process_coding.composing != COMPOSITION_DISABLED)
740 coding_allocate_composition_data (&process_coding, PT);
741 while (1)
743 /* Repeatedly read until we've filled as much as possible
744 of the buffer size we have. But don't read
745 less than 1024--save that for the next bufferful. */
746 nread = carryover;
747 while (nread < bufsize - 1024)
749 int this_read = emacs_read (fd[0], bufptr + nread,
750 bufsize - nread);
752 if (this_read < 0)
753 goto give_up;
755 if (this_read == 0)
757 process_coding.mode |= CODING_MODE_LAST_BLOCK;
758 break;
761 nread += this_read;
762 total_read += this_read;
764 if (display_on_the_fly)
765 break;
768 /* Now NREAD is the total amount of data in the buffer. */
769 immediate_quit = 0;
771 if (!NILP (buffer))
773 if (! CODING_MAY_REQUIRE_DECODING (&process_coding))
774 insert_1_both (bufptr, nread, nread, 0, 1, 0);
775 else
776 { /* We have to decode the input. */
777 int size;
778 char *decoding_buf;
780 repeat_decoding:
781 size = decoding_buffer_size (&process_coding, nread);
782 decoding_buf = (char *) xmalloc (size);
784 if (process_coding.cmp_data)
785 process_coding.cmp_data->char_offset = PT;
787 decode_coding (&process_coding, bufptr, decoding_buf,
788 nread, size);
790 if (display_on_the_fly
791 && saved_coding.type == coding_type_undecided
792 && process_coding.type != coding_type_undecided)
794 /* We have detected some coding system. But,
795 there's a possibility that the detection was
796 done by insufficient data. So, we give up
797 displaying on the fly. */
798 xfree (decoding_buf);
799 display_on_the_fly = 0;
800 process_coding = saved_coding;
801 carryover = nread;
802 continue;
805 if (process_coding.produced > 0)
806 insert_1_both (decoding_buf, process_coding.produced_char,
807 process_coding.produced, 0, 1, 0);
808 xfree (decoding_buf);
810 if (process_coding.result == CODING_FINISH_INCONSISTENT_EOL)
812 Lisp_Object eol_type, coding;
814 if (process_coding.eol_type == CODING_EOL_CR)
816 /* CRs have been replaced with LFs. Undo
817 that in the text inserted above. */
818 unsigned char *p;
820 move_gap_both (PT, PT_BYTE);
822 p = BYTE_POS_ADDR (pt_byte_orig);
823 for (; p < GPT_ADDR; ++p)
824 if (*p == '\n')
825 *p = '\r';
827 else if (process_coding.eol_type == CODING_EOL_CRLF)
829 /* CR LFs have been replaced with LFs. Undo
830 that by inserting CRs in front of LFs in
831 the text inserted above. */
832 EMACS_INT bytepos, old_pt, old_pt_byte, nCR;
834 old_pt = PT;
835 old_pt_byte = PT_BYTE;
836 nCR = 0;
838 for (bytepos = PT_BYTE - 1;
839 bytepos >= pt_byte_orig;
840 --bytepos)
841 if (FETCH_BYTE (bytepos) == '\n')
843 EMACS_INT charpos = BYTE_TO_CHAR (bytepos);
844 TEMP_SET_PT_BOTH (charpos, bytepos);
845 insert_1_both ("\r", 1, 1, 0, 1, 0);
846 ++nCR;
849 TEMP_SET_PT_BOTH (old_pt + nCR, old_pt_byte + nCR);
852 /* Set the coding system symbol to that for
853 Unix-like EOL. */
854 eol_type = Fget (saved_coding.symbol, Qeol_type);
855 if (VECTORP (eol_type)
856 && ASIZE (eol_type) == 3
857 && SYMBOLP (AREF (eol_type, CODING_EOL_LF)))
858 coding = AREF (eol_type, CODING_EOL_LF);
859 else
860 coding = saved_coding.symbol;
862 process_coding.symbol = coding;
863 process_coding.eol_type = CODING_EOL_LF;
864 process_coding.mode
865 &= ~CODING_MODE_INHIBIT_INCONSISTENT_EOL;
868 nread -= process_coding.consumed;
869 carryover = nread;
870 if (carryover > 0)
871 /* As CARRYOVER should not be that large, we had
872 better avoid overhead of bcopy. */
873 BCOPY_SHORT (bufptr + process_coding.consumed, bufptr,
874 carryover);
875 if (process_coding.result == CODING_FINISH_INSUFFICIENT_CMP)
877 /* The decoding ended because of insufficient data
878 area to record information about composition.
879 We must try decoding with additional data area
880 before reading more output for the process. */
881 coding_allocate_composition_data (&process_coding, PT);
882 goto repeat_decoding;
887 if (process_coding.mode & CODING_MODE_LAST_BLOCK)
888 break;
890 /* Make the buffer bigger as we continue to read more data,
891 but not past 64k. */
892 if (bufsize < 64 * 1024 && total_read > 32 * bufsize)
894 bufsize *= 2;
895 bufptr = (char *) alloca (bufsize);
898 if (!NILP (display) && INTERACTIVE)
900 if (first)
901 prepare_menu_bars ();
902 first = 0;
903 redisplay_preserve_echo_area (1);
905 immediate_quit = 1;
906 QUIT;
908 give_up: ;
910 if (!NILP (buffer)
911 && process_coding.cmp_data)
913 coding_restore_composition (&process_coding, Fcurrent_buffer ());
914 coding_free_composition_data (&process_coding);
918 int post_read_count = specpdl_ptr - specpdl;
920 record_unwind_protect (save_excursion_restore, save_excursion_save ());
921 inserted = PT - pt_orig;
922 TEMP_SET_PT_BOTH (pt_orig, pt_byte_orig);
923 if (SYMBOLP (process_coding.post_read_conversion)
924 && !NILP (Ffboundp (process_coding.post_read_conversion)))
925 call1 (process_coding.post_read_conversion, make_number (inserted));
927 Vlast_coding_system_used = process_coding.symbol;
929 /* If the caller required, let the buffer inherit the
930 coding-system used to decode the process output. */
931 if (inherit_process_coding_system)
932 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
933 make_number (total_read));
935 unbind_to (post_read_count, Qnil);
939 /* Wait for it to terminate, unless it already has. */
940 wait_for_termination (pid);
942 immediate_quit = 0;
944 set_buffer_internal (old);
946 /* Don't kill any children that the subprocess may have left behind
947 when exiting. */
948 call_process_exited = 1;
950 unbind_to (count, Qnil);
952 if (synch_process_death)
953 return code_convert_string_norecord (build_string (synch_process_death),
954 Vlocale_coding_system, 0);
955 return make_number (synch_process_retcode);
957 #endif
959 static Lisp_Object
960 delete_temp_file (name)
961 Lisp_Object name;
963 /* Use Fdelete_file (indirectly) because that runs a file name handler.
964 We did that when writing the file, so we should do so when deleting. */
965 internal_delete_file (name);
966 return Qnil;
969 DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
970 3, MANY, 0,
971 "Send text from START to END to a synchronous process running PROGRAM.\n\
972 The remaining arguments are optional.\n\
973 Delete the text if fourth arg DELETE is non-nil.\n\
975 Insert output in BUFFER before point; t means current buffer;\n\
976 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
977 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
978 REAL-BUFFER says what to do with standard output, as above,\n\
979 while STDERR-FILE says what to do with standard error in the child.\n\
980 STDERR-FILE may be nil (discard standard error output),\n\
981 t (mix it with ordinary output), or a file name string.\n\
983 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
984 Remaining args are passed to PROGRAM at startup as command args.\n\
986 If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
987 Otherwise it waits for PROGRAM to terminate\n\
988 and returns a numeric exit status or a signal description string.\n\
989 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
990 (nargs, args)
991 int nargs;
992 register Lisp_Object *args;
994 struct gcpro gcpro1;
995 Lisp_Object filename_string;
996 register Lisp_Object start, end;
997 int count = specpdl_ptr - specpdl;
998 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
999 Lisp_Object coding_systems;
1000 Lisp_Object val, *args2;
1001 int i;
1002 #ifdef DOS_NT
1003 char *tempfile;
1004 char *outf = '\0';
1006 if ((outf = egetenv ("TMPDIR"))
1007 || (outf = egetenv ("TMP"))
1008 || (outf = egetenv ("TEMP")))
1009 strcpy (tempfile = alloca (strlen (outf) + 20), outf);
1010 else
1012 tempfile = alloca (20);
1013 *tempfile = '\0';
1015 if (!IS_DIRECTORY_SEP (tempfile[strlen (tempfile) - 1]))
1016 strcat (tempfile, "/");
1017 if ('/' == DIRECTORY_SEP)
1018 dostounix_filename (tempfile);
1019 else
1020 unixtodos_filename (tempfile);
1021 #ifdef WINDOWSNT
1022 strcat (tempfile, "emXXXXXX");
1023 #else
1024 strcat (tempfile, "detmp.XXX");
1025 #endif
1026 #else /* not DOS_NT */
1027 char *tempfile = (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern)) + 1);
1028 bcopy (XSTRING (Vtemp_file_name_pattern)->data, tempfile,
1029 STRING_BYTES (XSTRING (Vtemp_file_name_pattern)) + 1);
1030 #endif /* not DOS_NT */
1032 coding_systems = Qt;
1034 #ifdef HAVE_MKSTEMP
1036 int fd = mkstemp (tempfile);
1037 if (fd == -1)
1038 report_file_error ("Failed to open temporary file",
1039 Fcons (Vtemp_file_name_pattern, Qnil));
1040 else
1041 close (fd);
1043 #else
1044 mktemp (tempfile);
1045 #endif
1047 filename_string = build_string (tempfile);
1048 GCPRO1 (filename_string);
1049 start = args[0];
1050 end = args[1];
1051 /* Decide coding-system of the contents of the temporary file. */
1052 if (!NILP (Vcoding_system_for_write))
1053 val = Vcoding_system_for_write;
1054 else if (NILP (current_buffer->enable_multibyte_characters))
1055 val = Qnil;
1056 else
1058 args2 = (Lisp_Object *) alloca ((nargs + 1) * sizeof *args2);
1059 args2[0] = Qcall_process_region;
1060 for (i = 0; i < nargs; i++) args2[i + 1] = args[i];
1061 coding_systems = Ffind_operation_coding_system (nargs + 1, args2);
1062 if (CONSP (coding_systems))
1063 val = XCDR (coding_systems);
1064 else if (CONSP (Vdefault_process_coding_system))
1065 val = XCDR (Vdefault_process_coding_system);
1066 else
1067 val = Qnil;
1071 int count1 = specpdl_ptr - specpdl;
1073 specbind (intern ("coding-system-for-write"), val);
1074 Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil);
1076 unbind_to (count1, Qnil);
1079 /* Note that Fcall_process takes care of binding
1080 coding-system-for-read. */
1082 record_unwind_protect (delete_temp_file, filename_string);
1084 if (nargs > 3 && !NILP (args[3]))
1085 Fdelete_region (start, end);
1087 if (nargs > 3)
1089 args += 2;
1090 nargs -= 2;
1092 else
1094 args[0] = args[2];
1095 nargs = 2;
1097 args[1] = filename_string;
1099 RETURN_UNGCPRO (unbind_to (count, Fcall_process (nargs, args)));
1102 #ifndef VMS /* VMS version is in vmsproc.c. */
1104 static int relocate_fd ();
1106 /* This is the last thing run in a newly forked inferior
1107 either synchronous or asynchronous.
1108 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1109 Initialize inferior's priority, pgrp, connected dir and environment.
1110 then exec another program based on new_argv.
1112 This function may change environ for the superior process.
1113 Therefore, the superior process must save and restore the value
1114 of environ around the vfork and the call to this function.
1116 SET_PGRP is nonzero if we should put the subprocess into a separate
1117 process group.
1119 CURRENT_DIR is an elisp string giving the path of the current
1120 directory the subprocess should have. Since we can't really signal
1121 a decent error from within the child, this should be verified as an
1122 executable directory by the parent. */
1125 child_setup (in, out, err, new_argv, set_pgrp, current_dir)
1126 int in, out, err;
1127 register char **new_argv;
1128 int set_pgrp;
1129 Lisp_Object current_dir;
1131 char **env;
1132 char *pwd_var;
1133 #ifdef WINDOWSNT
1134 int cpid;
1135 HANDLE handles[3];
1136 #endif /* WINDOWSNT */
1138 int pid = getpid ();
1140 #ifdef SET_EMACS_PRIORITY
1142 extern int emacs_priority;
1144 if (emacs_priority < 0)
1145 nice (- emacs_priority);
1147 #endif
1149 #ifdef subprocesses
1150 /* Close Emacs's descriptors that this process should not have. */
1151 close_process_descs ();
1152 #endif
1153 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1154 we will lose if we call close_load_descs here. */
1155 #ifndef DOS_NT
1156 close_load_descs ();
1157 #endif
1159 /* Note that use of alloca is always safe here. It's obvious for systems
1160 that do not have true vfork or that have true (stack) alloca.
1161 If using vfork and C_ALLOCA it is safe because that changes
1162 the superior's static variables as if the superior had done alloca
1163 and will be cleaned up in the usual way. */
1165 register char *temp;
1166 register int i;
1168 i = STRING_BYTES (XSTRING (current_dir));
1169 #ifdef MSDOS
1170 /* MSDOS must have all environment variables malloc'ed, because
1171 low-level libc functions that launch subsidiary processes rely
1172 on that. */
1173 pwd_var = (char *) xmalloc (i + 6);
1174 #else
1175 pwd_var = (char *) alloca (i + 6);
1176 #endif
1177 temp = pwd_var + 4;
1178 bcopy ("PWD=", pwd_var, 4);
1179 bcopy (XSTRING (current_dir)->data, temp, i);
1180 if (!IS_DIRECTORY_SEP (temp[i - 1])) temp[i++] = DIRECTORY_SEP;
1181 temp[i] = 0;
1183 #ifndef DOS_NT
1184 /* We can't signal an Elisp error here; we're in a vfork. Since
1185 the callers check the current directory before forking, this
1186 should only return an error if the directory's permissions
1187 are changed between the check and this chdir, but we should
1188 at least check. */
1189 if (chdir (temp) < 0)
1190 _exit (errno);
1191 #endif
1193 #ifdef DOS_NT
1194 /* Get past the drive letter, so that d:/ is left alone. */
1195 if (i > 2 && IS_DEVICE_SEP (temp[1]) && IS_DIRECTORY_SEP (temp[2]))
1197 temp += 2;
1198 i -= 2;
1200 #endif
1202 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1203 while (i > 2 && IS_DIRECTORY_SEP (temp[i - 1]))
1204 temp[--i] = 0;
1207 /* Set `env' to a vector of the strings in Vprocess_environment. */
1209 register Lisp_Object tem;
1210 register char **new_env;
1211 register int new_length;
1213 new_length = 0;
1214 for (tem = Vprocess_environment;
1215 CONSP (tem) && STRINGP (XCAR (tem));
1216 tem = XCDR (tem))
1217 new_length++;
1219 /* new_length + 2 to include PWD and terminating 0. */
1220 env = new_env = (char **) alloca ((new_length + 2) * sizeof (char *));
1222 /* If we have a PWD envvar, pass one down,
1223 but with corrected value. */
1224 if (getenv ("PWD"))
1225 *new_env++ = pwd_var;
1227 /* Copy the Vprocess_environment strings into new_env. */
1228 for (tem = Vprocess_environment;
1229 CONSP (tem) && STRINGP (XCAR (tem));
1230 tem = XCDR (tem))
1232 char **ep = env;
1233 char *string = (char *) XSTRING (XCAR (tem))->data;
1234 /* See if this string duplicates any string already in the env.
1235 If so, don't put it in.
1236 When an env var has multiple definitions,
1237 we keep the definition that comes first in process-environment. */
1238 for (; ep != new_env; ep++)
1240 char *p = *ep, *q = string;
1241 while (1)
1243 if (*q == 0)
1244 /* The string is malformed; might as well drop it. */
1245 goto duplicate;
1246 if (*q != *p)
1247 break;
1248 if (*q == '=')
1249 goto duplicate;
1250 p++, q++;
1253 *new_env++ = string;
1254 duplicate: ;
1256 *new_env = 0;
1258 #ifdef WINDOWSNT
1259 prepare_standard_handles (in, out, err, handles);
1260 set_process_dir (XSTRING (current_dir)->data);
1261 #else /* not WINDOWSNT */
1262 /* Make sure that in, out, and err are not actually already in
1263 descriptors zero, one, or two; this could happen if Emacs is
1264 started with its standard in, out, or error closed, as might
1265 happen under X. */
1267 int oin = in, oout = out;
1269 /* We have to avoid relocating the same descriptor twice! */
1271 in = relocate_fd (in, 3);
1273 if (out == oin)
1274 out = in;
1275 else
1276 out = relocate_fd (out, 3);
1278 if (err == oin)
1279 err = in;
1280 else if (err == oout)
1281 err = out;
1282 else
1283 err = relocate_fd (err, 3);
1286 #ifndef MSDOS
1287 emacs_close (0);
1288 emacs_close (1);
1289 emacs_close (2);
1291 dup2 (in, 0);
1292 dup2 (out, 1);
1293 dup2 (err, 2);
1294 emacs_close (in);
1295 emacs_close (out);
1296 emacs_close (err);
1297 #endif /* not MSDOS */
1298 #endif /* not WINDOWSNT */
1300 #if defined(USG) && !defined(BSD_PGRPS)
1301 #ifndef SETPGRP_RELEASES_CTTY
1302 setpgrp (); /* No arguments but equivalent in this case */
1303 #endif
1304 #else
1305 setpgrp (pid, pid);
1306 #endif /* USG */
1307 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1308 EMACS_SET_TTY_PGRP (0, &pid);
1310 #ifdef vipc
1311 something missing here;
1312 #endif /* vipc */
1314 #ifdef MSDOS
1315 pid = run_msdos_command (new_argv, pwd_var + 4, in, out, err, env);
1316 xfree (pwd_var);
1317 if (pid == -1)
1318 /* An error occurred while trying to run the subprocess. */
1319 report_file_error ("Spawning child process", Qnil);
1320 return pid;
1321 #else /* not MSDOS */
1322 #ifdef WINDOWSNT
1323 /* Spawn the child. (See ntproc.c:Spawnve). */
1324 cpid = spawnve (_P_NOWAIT, new_argv[0], new_argv, env);
1325 reset_standard_handles (in, out, err, handles);
1326 if (cpid == -1)
1327 /* An error occurred while trying to spawn the process. */
1328 report_file_error ("Spawning child process", Qnil);
1329 return cpid;
1330 #else /* not WINDOWSNT */
1331 /* execvp does not accept an environment arg so the only way
1332 to pass this environment is to set environ. Our caller
1333 is responsible for restoring the ambient value of environ. */
1334 environ = env;
1335 execvp (new_argv[0], new_argv);
1337 emacs_write (1, "Can't exec program: ", 20);
1338 emacs_write (1, new_argv[0], strlen (new_argv[0]));
1339 emacs_write (1, "\n", 1);
1340 _exit (1);
1341 #endif /* not WINDOWSNT */
1342 #endif /* not MSDOS */
1345 /* Move the file descriptor FD so that its number is not less than MINFD.
1346 If the file descriptor is moved at all, the original is freed. */
1347 static int
1348 relocate_fd (fd, minfd)
1349 int fd, minfd;
1351 if (fd >= minfd)
1352 return fd;
1353 else
1355 int new = dup (fd);
1356 if (new == -1)
1358 char *message1 = "Error while setting up child: ";
1359 char *errmessage = strerror (errno);
1360 char *message2 = "\n";
1361 emacs_write (2, message1, strlen (message1));
1362 emacs_write (2, errmessage, strlen (errmessage));
1363 emacs_write (2, message2, strlen (message2));
1364 _exit (1);
1366 /* Note that we hold the original FD open while we recurse,
1367 to guarantee we'll get a new FD if we need it. */
1368 new = relocate_fd (new, minfd);
1369 emacs_close (fd);
1370 return new;
1374 static int
1375 getenv_internal (var, varlen, value, valuelen)
1376 char *var;
1377 int varlen;
1378 char **value;
1379 int *valuelen;
1381 Lisp_Object scan;
1383 for (scan = Vprocess_environment; CONSP (scan); scan = XCDR (scan))
1385 Lisp_Object entry;
1387 entry = XCAR (scan);
1388 if (STRINGP (entry)
1389 && STRING_BYTES (XSTRING (entry)) > varlen
1390 && XSTRING (entry)->data[varlen] == '='
1391 #ifdef WINDOWSNT
1392 /* NT environment variables are case insensitive. */
1393 && ! strnicmp (XSTRING (entry)->data, var, varlen)
1394 #else /* not WINDOWSNT */
1395 && ! bcmp (XSTRING (entry)->data, var, varlen)
1396 #endif /* not WINDOWSNT */
1399 *value = (char *) XSTRING (entry)->data + (varlen + 1);
1400 *valuelen = STRING_BYTES (XSTRING (entry)) - (varlen + 1);
1401 return 1;
1405 return 0;
1408 DEFUN ("getenv-internal", Fgetenv_internal, Sgetenv_internal, 1, 1, 0,
1409 "Return the value of environment variable VAR, as a string.\n\
1410 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
1411 This function consults the variable ``process-environment'' for its value.")
1412 (var)
1413 Lisp_Object var;
1415 char *value;
1416 int valuelen;
1418 CHECK_STRING (var, 0);
1419 if (getenv_internal (XSTRING (var)->data, STRING_BYTES (XSTRING (var)),
1420 &value, &valuelen))
1421 return make_string (value, valuelen);
1422 else
1423 return Qnil;
1426 /* A version of getenv that consults process_environment, easily
1427 callable from C. */
1428 char *
1429 egetenv (var)
1430 char *var;
1432 char *value;
1433 int valuelen;
1435 if (getenv_internal (var, strlen (var), &value, &valuelen))
1436 return value;
1437 else
1438 return 0;
1441 #endif /* not VMS */
1443 /* This is run before init_cmdargs. */
1445 void
1446 init_callproc_1 ()
1448 char *data_dir = egetenv ("EMACSDATA");
1449 char *doc_dir = egetenv ("EMACSDOC");
1451 Vdata_directory
1452 = Ffile_name_as_directory (build_string (data_dir ? data_dir
1453 : PATH_DATA));
1454 Vdoc_directory
1455 = Ffile_name_as_directory (build_string (doc_dir ? doc_dir
1456 : PATH_DOC));
1458 /* Check the EMACSPATH environment variable, defaulting to the
1459 PATH_EXEC path from epaths.h. */
1460 Vexec_path = decode_env_path ("EMACSPATH", PATH_EXEC);
1461 Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
1462 Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
1465 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1467 void
1468 init_callproc ()
1470 char *data_dir = egetenv ("EMACSDATA");
1472 register char * sh;
1473 Lisp_Object tempdir;
1475 if (!NILP (Vinstallation_directory))
1477 /* Add to the path the lib-src subdir of the installation dir. */
1478 Lisp_Object tem;
1479 tem = Fexpand_file_name (build_string ("lib-src"),
1480 Vinstallation_directory);
1481 #ifndef DOS_NT
1482 /* MSDOS uses wrapped binaries, so don't do this. */
1483 if (NILP (Fmember (tem, Vexec_path)))
1484 Vexec_path = nconc2 (Vexec_path, Fcons (tem, Qnil));
1486 Vexec_directory = Ffile_name_as_directory (tem);
1487 #endif /* not DOS_NT */
1489 /* Maybe use ../etc as well as ../lib-src. */
1490 if (data_dir == 0)
1492 tem = Fexpand_file_name (build_string ("etc"),
1493 Vinstallation_directory);
1494 Vdoc_directory = Ffile_name_as_directory (tem);
1498 /* Look for the files that should be in etc. We don't use
1499 Vinstallation_directory, because these files are never installed
1500 near the executable, and they are never in the build
1501 directory when that's different from the source directory.
1503 Instead, if these files are not in the nominal place, we try the
1504 source directory. */
1505 if (data_dir == 0)
1507 Lisp_Object tem, tem1, newdir;
1509 tem = Fexpand_file_name (build_string ("GNU"), Vdata_directory);
1510 tem1 = Ffile_exists_p (tem);
1511 if (NILP (tem1))
1513 newdir = Fexpand_file_name (build_string ("../etc/"),
1514 build_string (PATH_DUMPLOADSEARCH));
1515 tem = Fexpand_file_name (build_string ("GNU"), newdir);
1516 tem1 = Ffile_exists_p (tem);
1517 if (!NILP (tem1))
1518 Vdata_directory = newdir;
1522 #ifndef CANNOT_DUMP
1523 if (initialized)
1524 #endif
1526 tempdir = Fdirectory_file_name (Vexec_directory);
1527 if (access (XSTRING (tempdir)->data, 0) < 0)
1528 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1529 Vexec_directory);
1532 tempdir = Fdirectory_file_name (Vdata_directory);
1533 if (access (XSTRING (tempdir)->data, 0) < 0)
1534 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1535 Vdata_directory);
1537 #ifdef VMS
1538 Vshell_file_name = build_string ("*dcl*");
1539 #else
1540 sh = (char *) getenv ("SHELL");
1541 Vshell_file_name = build_string (sh ? sh : "/bin/sh");
1542 #endif
1544 #ifdef VMS
1545 Vtemp_file_name_pattern = build_string ("tmp:emacsXXXXXX.");
1546 #else
1547 if (getenv ("TMPDIR"))
1549 char *dir = getenv ("TMPDIR");
1550 Vtemp_file_name_pattern
1551 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1552 build_string (dir));
1554 else
1555 Vtemp_file_name_pattern = build_string ("/tmp/emacsXXXXXX");
1556 #endif
1559 void
1560 set_process_environment ()
1562 register char **envp;
1564 Vprocess_environment = Qnil;
1565 #ifndef CANNOT_DUMP
1566 if (initialized)
1567 #endif
1568 for (envp = environ; *envp; envp++)
1569 Vprocess_environment = Fcons (build_string (*envp),
1570 Vprocess_environment);
1573 void
1574 syms_of_callproc ()
1576 #ifdef DOS_NT
1577 Qbuffer_file_type = intern ("buffer-file-type");
1578 staticpro (&Qbuffer_file_type);
1579 #endif /* DOS_NT */
1581 DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
1582 "*File name to load inferior shells from.\n\
1583 Initialized from the SHELL environment variable.");
1585 DEFVAR_LISP ("exec-path", &Vexec_path,
1586 "*List of directories to search programs to run in subprocesses.\n\
1587 Each element is a string (directory name) or nil (try default directory).");
1589 DEFVAR_LISP ("exec-directory", &Vexec_directory,
1590 "Directory for executables for Emacs to invoke.\n\
1591 More generally, this includes any architecture-dependent files\n\
1592 that are built and installed from the Emacs distribution.");
1594 DEFVAR_LISP ("data-directory", &Vdata_directory,
1595 "Directory of machine-independent files that come with GNU Emacs.\n\
1596 These are files intended for Emacs to use while it runs.");
1598 DEFVAR_LISP ("doc-directory", &Vdoc_directory,
1599 "Directory containing the DOC file that comes with GNU Emacs.\n\
1600 This is usually the same as data-directory.");
1602 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory,
1603 "For internal use by the build procedure only.\n\
1604 This is the name of the directory in which the build procedure installed\n\
1605 Emacs's info files; the default value for Info-default-directory-list\n\
1606 includes this.");
1607 Vconfigure_info_directory = build_string (PATH_INFO);
1609 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern,
1610 "Pattern for making names for temporary files.\n\
1611 This is used by `call-process-region'.");
1612 /* This variable is initialized in init_callproc. */
1614 DEFVAR_LISP ("process-environment", &Vprocess_environment,
1615 "List of environment variables for subprocesses to inherit.\n\
1616 Each element should be a string of the form ENVVARNAME=VALUE.\n\
1617 The environment which Emacs inherits is placed in this variable\n\
1618 when Emacs starts.");
1620 #ifndef VMS
1621 defsubr (&Scall_process);
1622 defsubr (&Sgetenv_internal);
1623 #endif
1624 defsubr (&Scall_process_region);