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