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