1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985, 86, 87, 88, 93, 94, 95 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
29 extern char *strerror ();
31 /* Define SIGCHLD as an alias for SIGCLD. */
33 #if !defined (SIGCHLD) && defined (SIGCLD)
34 #define SIGCHLD SIGCLD
37 #include <sys/types.h>
41 #define INCLUDED_FCNTL
48 #include <stdlib.h> /* for proper declaration of environ */
51 #define _P_NOWAIT 1 /* from process.h */
54 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
56 #define INCLUDED_FCNTL
59 #include <sys/param.h>
78 #include "syssignal.h"
82 extern noshare
char **environ
;
84 extern char **environ
;
87 #define max(a, b) ((a) > (b) ? (a) : (b))
90 /* When we are starting external processes we need to know whether they
91 take binary input (no conversion) or text input (\n is converted to
92 \r\n). Similar for output: if newlines are written as \r\n then it's
93 text process output, otherwise it's binary. */
94 Lisp_Object Vbinary_process_input
;
95 Lisp_Object Vbinary_process_output
;
98 Lisp_Object Vexec_path
, Vexec_directory
, Vdata_directory
, Vdoc_directory
;
99 Lisp_Object Vconfigure_info_directory
;
100 Lisp_Object Vtemp_file_name_pattern
;
102 Lisp_Object Vshell_file_name
;
104 Lisp_Object Vprocess_environment
;
107 Lisp_Object Qbuffer_file_type
;
110 /* True iff we are about to fork off a synchronous process or if we
111 are waiting for it. */
112 int synch_process_alive
;
114 /* Nonzero => this is a string explaining death of synchronous subprocess. */
115 char *synch_process_death
;
117 /* If synch_process_death is zero,
118 this is exit code of synchronous subprocess. */
119 int synch_process_retcode
;
121 extern Lisp_Object Vdoc_file_name
;
123 extern Lisp_Object Vfile_name_coding_system
;
125 /* Clean up when exiting Fcall_process.
126 On MSDOS, delete the temporary file on any kind of termination.
127 On Unix, kill the process and any children on termination by signal. */
129 /* Nonzero if this is termination due to exit. */
130 static int call_process_exited
;
132 #ifndef VMS /* VMS version is in vmsproc.c. */
135 call_process_kill (fdpid
)
138 close (XFASTINT (Fcar (fdpid
)));
139 EMACS_KILLPG (XFASTINT (Fcdr (fdpid
)), SIGKILL
);
140 synch_process_alive
= 0;
145 call_process_cleanup (fdpid
)
149 /* for MSDOS fdpid is really (fd . tempfile) */
150 register Lisp_Object file
;
152 close (XFASTINT (Fcar (fdpid
)));
153 if (strcmp (XSTRING (file
)-> data
, NULL_DEVICE
) != 0)
154 unlink (XSTRING (file
)->data
);
155 #else /* not MSDOS */
156 register int pid
= XFASTINT (Fcdr (fdpid
));
159 if (call_process_exited
)
161 close (XFASTINT (Fcar (fdpid
)));
165 if (EMACS_KILLPG (pid
, SIGINT
) == 0)
167 int count
= specpdl_ptr
- specpdl
;
168 record_unwind_protect (call_process_kill
, fdpid
);
169 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
172 wait_for_termination (pid
);
174 specpdl_ptr
= specpdl
+ count
; /* Discard the unwind protect. */
175 message1 ("Waiting for process to die...done");
177 synch_process_alive
= 0;
178 close (XFASTINT (Fcar (fdpid
)));
179 #endif /* not MSDOS */
183 DEFUN ("call-process", Fcall_process
, Scall_process
, 1, MANY
, 0,
184 "Call PROGRAM synchronously in separate process.\n\
185 The program's input comes from file INFILE (nil means `/dev/null').\n\
186 Insert output in BUFFER before point; t means current buffer;\n\
187 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
188 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
189 REAL-BUFFER says what to do with standard output, as above,\n\
190 while STDERR-FILE says what to do with standard error in the child.\n\
191 STDERR-FILE may be nil (discard standard error output),\n\
192 t (mix it with ordinary output), or a file name string.\n\
194 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
195 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
197 If BUFFER is 0, `call-process' returns immediately with value nil.\n\
198 Otherwise it waits for PROGRAM to terminate\n\
199 and returns a numeric exit status or a signal description string.\n\
200 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
203 register Lisp_Object
*args
;
205 Lisp_Object infile
, buffer
, current_dir
, display
, path
;
212 int count
= specpdl_ptr
- specpdl
;
213 register unsigned char **new_argv
214 = (unsigned char **) alloca ((max (2, nargs
- 2)) * sizeof (char *));
215 struct buffer
*old
= current_buffer
;
216 /* File to use for stderr in the child.
217 t means use same as standard output. */
218 Lisp_Object error_file
;
219 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
220 char *outf
, *tempfile
;
226 struct coding_system process_coding
; /* coding-system of process output */
227 struct coding_system argument_coding
; /* coding-system of arguments */
229 CHECK_STRING (args
[0], 0);
234 /* Without asynchronous processes we cannot have BUFFER == 0. */
235 if (nargs
>= 3 && INTEGERP (args
[2]))
236 error ("Operating system cannot handle asynchronous subprocesses");
237 #endif /* subprocesses */
239 /* Decide the coding-system for giving arguments and reading process
242 Lisp_Object val
, *args2
;
243 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
244 Lisp_Object coding_systems
= Qt
;
247 /* If arguments are supplied, we may have to encode them. */
250 if (!NILP (Vcoding_system_for_write
))
251 val
= Vcoding_system_for_write
;
252 else if (NILP (current_buffer
->enable_multibyte_characters
))
256 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
257 args2
[0] = Qcall_process
;
258 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
259 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
260 if (CONSP (coding_systems
))
261 val
= XCONS (coding_systems
)->cdr
;
262 else if (CONSP (Vdefault_process_coding_system
))
263 val
= XCONS (Vdefault_process_coding_system
)->cdr
;
267 setup_coding_system (Fcheck_coding_system (val
), &argument_coding
);
270 /* If BUFFER is nil, we must read process output once and then
271 discard it, so setup coding system but with nil. If BUFFER is
272 an integer, we can discard it without reading. */
273 if (nargs
< 3 || NILP (args
[2]))
274 setup_coding_system (Qnil
, &process_coding
);
275 else if (!INTEGERP (args
[2]))
278 if (!NILP (Vcoding_system_for_read
))
279 val
= Vcoding_system_for_read
;
280 else if (NILP (current_buffer
->enable_multibyte_characters
))
284 if (!EQ (coding_systems
, Qt
))
286 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
287 args2
[0] = Qcall_process
;
288 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
290 = Ffind_operation_coding_system (nargs
+ 1, args2
);
292 if (CONSP (coding_systems
))
293 val
= XCONS (coding_systems
)->car
;
294 else if (CONSP (Vdefault_process_coding_system
))
295 val
= XCONS (Vdefault_process_coding_system
)->car
;
299 setup_coding_system (Fcheck_coding_system (val
), &process_coding
);
301 /* On MSDOS, if the user did not ask for binary, treat it as
302 "text" which means doing CRLF conversion. Otherwise, leave
305 Note that ``binary'' here only means whether EOLs should or
306 should not be converted, since that's what Vbinary_process_XXXput
307 meant in the days before the coding systems were introduced.
309 For other conversions, the caller should set coding-system
310 variables explicitly, or rely on auto-detection. */
312 /* FIXME: this probably should be moved into the guts of
313 `Ffind_operation_coding_system' for the case of `call-process'. */
314 if (NILP (Vbinary_process_output
))
316 process_coding
.eol_type
= CODING_EOL_CRLF
;
317 if (process_coding
.type
== coding_type_no_conversion
)
318 /* FIXME: should we set type to undecided? */
319 process_coding
.type
= coding_type_emacs_mule
;
322 process_coding
.eol_type
= CODING_EOL_LF
;
327 if (nargs
>= 2 && ! NILP (args
[1]))
329 infile
= Fexpand_file_name (args
[1], current_buffer
->directory
);
330 CHECK_STRING (infile
, 1);
333 infile
= build_string (NULL_DEVICE
);
339 /* If BUFFER is a list, its meaning is
340 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
343 if (CONSP (XCONS (buffer
)->cdr
))
345 Lisp_Object stderr_file
;
346 stderr_file
= XCONS (XCONS (buffer
)->cdr
)->car
;
348 if (NILP (stderr_file
) || EQ (Qt
, stderr_file
))
349 error_file
= stderr_file
;
351 error_file
= Fexpand_file_name (stderr_file
, Qnil
);
354 buffer
= XCONS (buffer
)->car
;
357 if (!(EQ (buffer
, Qnil
)
359 || XFASTINT (buffer
) == 0))
361 Lisp_Object spec_buffer
;
362 spec_buffer
= buffer
;
363 buffer
= Fget_buffer (buffer
);
364 /* Mention the buffer name for a better error message. */
366 CHECK_BUFFER (spec_buffer
, 2);
367 CHECK_BUFFER (buffer
, 2);
373 /* Make sure that the child will be able to chdir to the current
374 buffer's current directory, or its unhandled equivalent. We
375 can't just have the child check for an error when it does the
376 chdir, since it's in a vfork.
378 We have to GCPRO around this because Fexpand_file_name,
379 Funhandled_file_name_directory, and Ffile_accessible_directory_p
380 might call a file name handling function. The argument list is
381 protected by the caller, so all we really have to worry about is
384 struct gcpro gcpro1
, gcpro2
, gcpro3
;
386 current_dir
= current_buffer
->directory
;
388 GCPRO3 (infile
, buffer
, current_dir
);
391 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
393 if (NILP (Ffile_accessible_directory_p (current_dir
)))
394 report_file_error ("Setting current directory",
395 Fcons (current_buffer
->directory
, Qnil
));
400 display
= nargs
>= 4 ? args
[3] : Qnil
;
402 filefd
= open (XSTRING (infile
)->data
, O_RDONLY
, 0);
405 report_file_error ("Opening process input file", Fcons (infile
, Qnil
));
407 /* Search for program; barf if not found. */
411 GCPRO1 (current_dir
);
412 openp (Vexec_path
, args
[0], EXEC_SUFFIXES
, &path
, 1);
418 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
420 new_argv
[0] = XSTRING (path
)->data
;
423 for (i
= 4; i
< nargs
; i
++)
425 CHECK_STRING (args
[i
], i
);
426 if (argument_coding
.type
== coding_type_no_conversion
)
427 new_argv
[i
- 3] = XSTRING (args
[i
])->data
;
430 /* We must encode the arguments. */
431 int size
= encoding_buffer_size (&argument_coding
,
432 XSTRING (args
[i
])->size
);
434 unsigned char *dummy1
= (unsigned char *) alloca (size
);
436 /* The Irix 4.0 compiler barfs if we eliminate dummy. */
437 new_argv
[i
- 3] = dummy1
;
438 produced
= encode_coding (&argument_coding
,
439 XSTRING (args
[i
])->data
, new_argv
[i
- 3],
440 XSTRING (args
[i
])->size
, size
, &dummy
);
441 new_argv
[i
- 3][produced
] = 0;
447 #ifdef MSDOS /* MW, July 1993 */
448 if ((outf
= egetenv ("TMP")) || (outf
= egetenv ("TEMP")))
449 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
452 tempfile
= alloca (20);
455 dostounix_filename (tempfile
);
456 if (*tempfile
== '\0' || tempfile
[strlen (tempfile
) - 1] != '/')
457 strcat (tempfile
, "/");
458 strcat (tempfile
, "detmp.XXX");
461 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
465 report_file_error ("Opening process output file",
466 Fcons (build_string (tempfile
), Qnil
));
472 if (INTEGERP (buffer
))
473 fd
[1] = open (NULL_DEVICE
, O_WRONLY
), fd
[0] = -1;
480 /* Replaced by close_process_descs */
481 set_exclusive_use (fd
[0]);
486 /* child_setup must clobber environ in systems with true vfork.
487 Protect it from permanent change. */
488 register char **save_environ
= environ
;
489 register int fd1
= fd
[1];
492 #if 0 /* Some systems don't have sigblock. */
493 mask
= sigblock (sigmask (SIGCHLD
));
496 /* Record that we're about to create a synchronous process. */
497 synch_process_alive
= 1;
499 /* These vars record information from process termination.
500 Clear them now before process can possibly terminate,
501 to avoid timing error if process terminates soon. */
502 synch_process_death
= 0;
503 synch_process_retcode
= 0;
505 if (NILP (error_file
))
506 fd_error
= open (NULL_DEVICE
, O_WRONLY
);
507 else if (STRINGP (error_file
))
510 fd_error
= open (XSTRING (error_file
)->data
,
511 O_WRONLY
| O_TRUNC
| O_CREAT
| O_TEXT
,
513 #else /* not DOS_NT */
514 fd_error
= creat (XSTRING (error_file
)->data
, 0666);
515 #endif /* not DOS_NT */
528 report_file_error ("Cannot redirect stderr",
529 Fcons ((NILP (error_file
)
530 ? build_string (NULL_DEVICE
) : error_file
),
535 = Fencode_coding_string (current_dir
, Vfile_name_coding_system
, Qt
);
537 #ifdef MSDOS /* MW, July 1993 */
538 /* ??? Someone who knows MSDOG needs to check whether this properly
539 closes all descriptors that it opens.
541 Note that run_msdos_command() actually returns the child process
542 exit status, not its PID, so we assign it to `synch_process_retcode'
544 pid
= run_msdos_command (new_argv
, current_dir
,
545 filefd
, outfilefd
, fd_error
);
547 /* Record that the synchronous process exited and note its
548 termination status. */
549 synch_process_alive
= 0;
550 synch_process_retcode
= pid
;
551 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
552 synch_process_death
= strerror (errno
);
555 if (fd_error
!= outfilefd
)
557 fd1
= -1; /* No harm in closing that one! */
558 /* Since CRLF is converted to LF within `decode_coding', we can
559 always open a file with binary mode. */
560 fd
[0] = open (tempfile
, O_BINARY
);
565 report_file_error ("Cannot re-open temporary file", Qnil
);
567 #else /* not MSDOS */
569 pid
= child_setup (filefd
, fd1
, fd_error
, new_argv
, 0, current_dir
);
570 #else /* not WINDOWSNT */
580 #if defined (USG) && !defined (BSD_PGRPS)
585 child_setup (filefd
, fd1
, fd_error
, new_argv
, 0, current_dir
);
587 #endif /* not WINDOWSNT */
589 /* The MSDOS case did this already. */
592 #endif /* not MSDOS */
594 environ
= save_environ
;
596 /* Close most of our fd's, but not fd[0]
597 since we will use that to read input from. */
599 if (fd1
>= 0 && fd1
!= fd_error
)
607 report_file_error ("Doing vfork", Qnil
);
610 if (INTEGERP (buffer
))
615 /* If Emacs has been built with asynchronous subprocess support,
616 we don't need to do this, I think because it will then have
617 the facilities for handling SIGCHLD. */
618 wait_without_blocking ();
619 #endif /* subprocesses */
623 /* Enable sending signal if user quits below. */
624 call_process_exited
= 0;
627 /* MSDOS needs different cleanup information. */
628 record_unwind_protect (call_process_cleanup
,
629 Fcons (make_number (fd
[0]), build_string (tempfile
)));
631 record_unwind_protect (call_process_cleanup
,
632 Fcons (make_number (fd
[0]), make_number (pid
)));
633 #endif /* not MSDOS */
636 if (BUFFERP (buffer
))
637 Fset_buffer (buffer
);
649 /* Repeatedly read until we've filled as much as possible
650 of the buffer size we have. But don't read
651 less than 1024--save that for the next bufferful. */
653 nread
= process_coding
.carryover_size
; /* This value is initially 0. */
654 while (nread
< bufsize
- 1024)
657 = read (fd
[0], bufptr
+ nread
, bufsize
- nread
);
670 /* Now NREAD is the total amount of data in the buffer. */
672 /* Here, just tell decode_coding that we are processing the
673 last block. We break the loop after decoding. */
674 process_coding
.last_block
= 1;
681 if (process_coding
.type
== coding_type_no_conversion
)
682 insert (bufptr
, nread
);
684 { /* We have to decode the input. */
685 int size
= decoding_buffer_size (&process_coding
, bufsize
);
686 char *decoding_buf
= get_conversion_buffer (size
);
689 nread
= decode_coding (&process_coding
, bufptr
, decoding_buf
,
690 nread
, size
, &dummy
);
692 insert (decoding_buf
, nread
);
696 if (process_coding
.last_block
)
699 /* Make the buffer bigger as we continue to read more data,
701 if (bufsize
< 64 * 1024 && total_read
> 32 * bufsize
)
704 bufptr
= (char *) alloca (bufsize
);
707 if (!NILP (buffer
) && process_coding
.carryover_size
> 0)
708 /* We have carryover in the last decoding. It should be
709 processed again after reading more data. */
710 bcopy (process_coding
.carryover
, bufptr
,
711 process_coding
.carryover_size
);
713 if (!NILP (display
) && INTERACTIVE
)
716 prepare_menu_bars ();
718 redisplay_preserve_echo_area ();
726 /* Wait for it to terminate, unless it already has. */
727 wait_for_termination (pid
);
731 set_buffer_internal (old
);
733 /* Don't kill any children that the subprocess may have left behind
735 call_process_exited
= 1;
737 unbind_to (count
, Qnil
);
739 if (synch_process_death
)
740 return build_string (synch_process_death
);
741 return make_number (synch_process_retcode
);
746 delete_temp_file (name
)
749 /* Use Fdelete_file (indirectly) because that runs a file name handler.
750 We did that when writing the file, so we should do so when deleting. */
751 internal_delete_file (name
);
754 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
756 "Send text from START to END to a synchronous process running PROGRAM.\n\
757 Delete the text if fourth arg DELETE is non-nil.\n\
759 Insert output in BUFFER before point; t means current buffer;\n\
760 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
761 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
762 REAL-BUFFER says what to do with standard output, as above,\n\
763 while STDERR-FILE says what to do with standard error in the child.\n\
764 STDERR-FILE may be nil (discard standard error output),\n\
765 t (mix it with ordinary output), or a file name string.\n\
767 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
768 Remaining args are passed to PROGRAM at startup as command args.\n\
770 If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
771 Otherwise it waits for PROGRAM to terminate\n\
772 and returns a numeric exit status or a signal description string.\n\
773 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
776 register Lisp_Object
*args
;
779 Lisp_Object filename_string
;
780 register Lisp_Object start
, end
;
781 int count
= specpdl_ptr
- specpdl
;
782 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
783 Lisp_Object coding_systems
= Qt
;
784 Lisp_Object val
, *args2
;
790 if ((outf
= egetenv ("TMP")) || (outf
= egetenv ("TEMP")))
791 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
794 tempfile
= alloca (20);
797 if (!IS_DIRECTORY_SEP (tempfile
[strlen (tempfile
) - 1]))
798 strcat (tempfile
, "/");
799 if ('/' == DIRECTORY_SEP
)
800 dostounix_filename (tempfile
);
802 unixtodos_filename (tempfile
);
804 strcat (tempfile
, "emXXXXXX");
806 strcat (tempfile
, "detmp.XXX");
808 #else /* not DOS_NT */
809 char *tempfile
= (char *) alloca (XSTRING (Vtemp_file_name_pattern
)->size
+ 1);
810 bcopy (XSTRING (Vtemp_file_name_pattern
)->data
, tempfile
,
811 XSTRING (Vtemp_file_name_pattern
)->size
+ 1);
812 #endif /* not DOS_NT */
816 filename_string
= build_string (tempfile
);
817 GCPRO1 (filename_string
);
820 /* Decide coding-system of the contents of the temporary file. */
822 /* This is to cause find-buffer-file-type-coding-system (see
823 dos-w32.el) to choose correct EOL translation for write-region. */
824 specbind (Qbuffer_file_type
, Vbinary_process_input
);
826 if (!NILP (Vcoding_system_for_write
))
827 val
= Vcoding_system_for_write
;
828 else if (NILP (current_buffer
->enable_multibyte_characters
))
832 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
833 args2
[0] = Qcall_process_region
;
834 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
835 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
836 if (CONSP (coding_systems
))
837 val
= XCONS (coding_systems
)->cdr
;
838 else if (CONSP (Vdefault_process_coding_system
))
839 val
= XCONS (Vdefault_process_coding_system
)->cdr
;
845 /* binary-process-input tells whether the buffer needs to be
846 written with EOL conversions, but it doesn't say anything
847 about the rest of text encoding. It takes effect whenever
848 the coding system doesn't otherwise specify what to do for
852 if (! NILP (Vbinary_process_input
))
853 val
= intern ("undecided-unix");
855 val
= intern ("undecided-dos");
857 else if (SYMBOLP (val
) && NILP (Vcoding_system_for_write
))
860 eolval
= Fget (val
, Qeol_type
);
861 if (VECTORP (eolval
) && XVECTOR (eolval
)->size
> 1)
862 /* Use element 1 (CRLF conversion) for "text",
863 and element 0 (LF conversion) for "binary". */
864 val
= XVECTOR (eolval
)->contents
[NILP (Vbinary_process_input
)];
868 specbind (intern ("coding-system-for-write"), val
);
869 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
, Qnil
);
871 /* Note that Fcall_process takes care of binding
872 coding-system-for-read. */
874 record_unwind_protect (delete_temp_file
, filename_string
);
877 Fdelete_region (start
, end
);
879 args
[3] = filename_string
;
881 RETURN_UNGCPRO (unbind_to (count
, Fcall_process (nargs
- 2, args
+ 2)));
884 #ifndef VMS /* VMS version is in vmsproc.c. */
886 /* This is the last thing run in a newly forked inferior
887 either synchronous or asynchronous.
888 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
889 Initialize inferior's priority, pgrp, connected dir and environment.
890 then exec another program based on new_argv.
892 This function may change environ for the superior process.
893 Therefore, the superior process must save and restore the value
894 of environ around the vfork and the call to this function.
896 ENV is the environment for the subprocess.
898 SET_PGRP is nonzero if we should put the subprocess into a separate
901 CURRENT_DIR is an elisp string giving the path of the current
902 directory the subprocess should have. Since we can't really signal
903 a decent error from within the child, this should be verified as an
904 executable directory by the parent. */
906 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
908 register char **new_argv
;
910 Lisp_Object current_dir
;
913 /* The MSDOS port of gcc cannot fork, vfork, ... so we must call system
915 #else /* not MSDOS */
921 #endif /* WINDOWSNT */
925 #ifdef SET_EMACS_PRIORITY
927 extern int emacs_priority
;
929 if (emacs_priority
< 0)
930 nice (- emacs_priority
);
935 /* Close Emacs's descriptors that this process should not have. */
936 close_process_descs ();
940 /* Note that use of alloca is always safe here. It's obvious for systems
941 that do not have true vfork or that have true (stack) alloca.
942 If using vfork and C_ALLOCA it is safe because that changes
943 the superior's static variables as if the superior had done alloca
944 and will be cleaned up in the usual way. */
949 i
= XSTRING (current_dir
)->size
;
950 pwd_var
= (char *) alloca (i
+ 6);
952 bcopy ("PWD=", pwd_var
, 4);
953 bcopy (XSTRING (current_dir
)->data
, temp
, i
);
954 if (!IS_DIRECTORY_SEP (temp
[i
- 1])) temp
[i
++] = DIRECTORY_SEP
;
958 /* We can't signal an Elisp error here; we're in a vfork. Since
959 the callers check the current directory before forking, this
960 should only return an error if the directory's permissions
961 are changed between the check and this chdir, but we should
963 if (chdir (temp
) < 0)
967 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
968 while (i
> 2 && IS_DIRECTORY_SEP (temp
[i
- 1]))
972 /* Set `env' to a vector of the strings in Vprocess_environment. */
974 register Lisp_Object tem
;
975 register char **new_env
;
976 register int new_length
;
979 for (tem
= Vprocess_environment
;
980 CONSP (tem
) && STRINGP (XCONS (tem
)->car
);
981 tem
= XCONS (tem
)->cdr
)
984 /* new_length + 2 to include PWD and terminating 0. */
985 env
= new_env
= (char **) alloca ((new_length
+ 2) * sizeof (char *));
987 /* If we have a PWD envvar, pass one down,
988 but with corrected value. */
990 *new_env
++ = pwd_var
;
992 /* Copy the Vprocess_environment strings into new_env. */
993 for (tem
= Vprocess_environment
;
994 CONSP (tem
) && STRINGP (XCONS (tem
)->car
);
995 tem
= XCONS (tem
)->cdr
)
998 char *string
= (char *) XSTRING (XCONS (tem
)->car
)->data
;
999 /* See if this string duplicates any string already in the env.
1000 If so, don't put it in.
1001 When an env var has multiple definitions,
1002 we keep the definition that comes first in process-environment. */
1003 for (; ep
!= new_env
; ep
++)
1005 char *p
= *ep
, *q
= string
;
1009 /* The string is malformed; might as well drop it. */
1018 *new_env
++ = string
;
1024 prepare_standard_handles (in
, out
, err
, handles
);
1025 set_process_dir (XSTRING (current_dir
)->data
);
1026 #else /* not WINDOWSNT */
1027 /* Make sure that in, out, and err are not actually already in
1028 descriptors zero, one, or two; this could happen if Emacs is
1029 started with its standard in, out, or error closed, as might
1032 int oin
= in
, oout
= out
;
1034 /* We have to avoid relocating the same descriptor twice! */
1036 in
= relocate_fd (in
, 3);
1041 out
= relocate_fd (out
, 3);
1045 else if (err
== oout
)
1048 err
= relocate_fd (err
, 3);
1061 #endif /* not WINDOWSNT */
1063 #if defined(USG) && !defined(BSD_PGRPS)
1064 #ifndef SETPGRP_RELEASES_CTTY
1065 setpgrp (); /* No arguments but equivalent in this case */
1070 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1071 EMACS_SET_TTY_PGRP (0, &pid
);
1074 something missing here
;
1078 /* Spawn the child. (See ntproc.c:Spawnve). */
1079 cpid
= spawnve (_P_NOWAIT
, new_argv
[0], new_argv
, env
);
1080 reset_standard_handles (in
, out
, err
, handles
);
1082 /* An error occurred while trying to spawn the process. */
1083 report_file_error ("Spawning child process", Qnil
);
1085 #else /* not WINDOWSNT */
1086 /* execvp does not accept an environment arg so the only way
1087 to pass this environment is to set environ. Our caller
1088 is responsible for restoring the ambient value of environ. */
1090 execvp (new_argv
[0], new_argv
);
1092 write (1, "Can't exec program: ", 20);
1093 write (1, new_argv
[0], strlen (new_argv
[0]));
1096 #endif /* not WINDOWSNT */
1097 #endif /* not MSDOS */
1100 /* Move the file descriptor FD so that its number is not less than MIN.
1101 If the file descriptor is moved at all, the original is freed. */
1103 relocate_fd (fd
, min
)
1113 char *message1
= "Error while setting up child: ";
1114 char *errmessage
= strerror (errno
);
1115 char *message2
= "\n";
1116 write (2, message1
, strlen (message1
));
1117 write (2, errmessage
, strlen (errmessage
));
1118 write (2, message2
, strlen (message2
));
1121 /* Note that we hold the original FD open while we recurse,
1122 to guarantee we'll get a new FD if we need it. */
1123 new = relocate_fd (new, min
);
1130 getenv_internal (var
, varlen
, value
, valuelen
)
1138 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCONS (scan
)->cdr
)
1142 entry
= XCONS (scan
)->car
;
1144 && XSTRING (entry
)->size
> varlen
1145 && XSTRING (entry
)->data
[varlen
] == '='
1147 /* NT environment variables are case insensitive. */
1148 && ! strnicmp (XSTRING (entry
)->data
, var
, varlen
)
1149 #else /* not WINDOWSNT */
1150 && ! bcmp (XSTRING (entry
)->data
, var
, varlen
)
1151 #endif /* not WINDOWSNT */
1154 *value
= (char *) XSTRING (entry
)->data
+ (varlen
+ 1);
1155 *valuelen
= XSTRING (entry
)->size
- (varlen
+ 1);
1163 DEFUN ("getenv", Fgetenv
, Sgetenv
, 1, 1, 0,
1164 "Return the value of environment variable VAR, as a string.\n\
1165 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
1166 This function consults the variable ``process-environment'' for its value.")
1173 CHECK_STRING (var
, 0);
1174 if (getenv_internal (XSTRING (var
)->data
, XSTRING (var
)->size
,
1176 return make_string (value
, valuelen
);
1181 /* A version of getenv that consults process_environment, easily
1190 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
1196 #endif /* not VMS */
1198 /* This is run before init_cmdargs. */
1202 char *data_dir
= egetenv ("EMACSDATA");
1203 char *doc_dir
= egetenv ("EMACSDOC");
1206 = Ffile_name_as_directory (build_string (data_dir
? data_dir
1209 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
1212 /* Check the EMACSPATH environment variable, defaulting to the
1213 PATH_EXEC path from paths.h. */
1214 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1215 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
1216 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1219 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1223 char *data_dir
= egetenv ("EMACSDATA");
1226 Lisp_Object tempdir
;
1228 if (initialized
&& !NILP (Vinstallation_directory
))
1230 /* Add to the path the lib-src subdir of the installation dir. */
1232 tem
= Fexpand_file_name (build_string ("lib-src"),
1233 Vinstallation_directory
);
1234 if (NILP (Fmember (tem
, Vexec_path
)))
1237 /* MSDOS uses wrapped binaries, so don't do this. */
1238 Vexec_path
= nconc2 (Vexec_path
, Fcons (tem
, Qnil
));
1239 Vexec_directory
= Ffile_name_as_directory (tem
);
1240 #endif /* not DOS_NT */
1243 /* Maybe use ../etc as well as ../lib-src. */
1246 tem
= Fexpand_file_name (build_string ("etc"),
1247 Vinstallation_directory
);
1248 Vdoc_directory
= Ffile_name_as_directory (tem
);
1252 /* Look for the files that should be in etc. We don't use
1253 Vinstallation_directory, because these files are never installed
1254 near the executable, and they are never in the build
1255 directory when that's different from the source directory.
1257 Instead, if these files are not in the nominal place, we try the
1258 source directory. */
1261 Lisp_Object tem
, tem1
, newdir
;
1263 tem
= Fexpand_file_name (build_string ("GNU"), Vdata_directory
);
1264 tem1
= Ffile_exists_p (tem
);
1267 newdir
= Fexpand_file_name (build_string ("../etc/"),
1268 build_string (PATH_DUMPLOADSEARCH
));
1269 tem
= Fexpand_file_name (build_string ("GNU"), newdir
);
1270 tem1
= Ffile_exists_p (tem
);
1272 Vdata_directory
= newdir
;
1280 tempdir
= Fdirectory_file_name (Vexec_directory
);
1281 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1282 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1286 tempdir
= Fdirectory_file_name (Vdata_directory
);
1287 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1288 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1292 Vshell_file_name
= build_string ("*dcl*");
1294 sh
= (char *) getenv ("SHELL");
1295 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
1299 Vtemp_file_name_pattern
= build_string ("tmp:emacsXXXXXX.");
1301 if (getenv ("TMPDIR"))
1303 char *dir
= getenv ("TMPDIR");
1304 Vtemp_file_name_pattern
1305 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1306 build_string (dir
));
1309 Vtemp_file_name_pattern
= build_string ("/tmp/emacsXXXXXX");
1313 set_process_environment ()
1315 register char **envp
;
1317 Vprocess_environment
= Qnil
;
1321 for (envp
= environ
; *envp
; envp
++)
1322 Vprocess_environment
= Fcons (build_string (*envp
),
1323 Vprocess_environment
);
1329 Qbuffer_file_type
= intern ("buffer-file-type");
1330 staticpro (&Qbuffer_file_type
);
1332 DEFVAR_LISP ("binary-process-input", &Vbinary_process_input
,
1333 "*If non-nil then new subprocesses are assumed to take binary input.");
1334 Vbinary_process_input
= Qnil
;
1336 DEFVAR_LISP ("binary-process-output", &Vbinary_process_output
,
1337 "*If non-nil then new subprocesses are assumed to produce binary output.");
1338 Vbinary_process_output
= Qnil
;
1341 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
1342 "*File name to load inferior shells from.\n\
1343 Initialized from the SHELL environment variable.");
1345 DEFVAR_LISP ("exec-path", &Vexec_path
,
1346 "*List of directories to search programs to run in subprocesses.\n\
1347 Each element is a string (directory name) or nil (try default directory).");
1349 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
1350 "Directory of architecture-dependent files that come with GNU Emacs,\n\
1351 especially executable programs intended for Emacs to invoke.");
1353 DEFVAR_LISP ("data-directory", &Vdata_directory
,
1354 "Directory of architecture-independent files that come with GNU Emacs,\n\
1355 intended for Emacs to use.");
1357 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
1358 "Directory containing the DOC file that comes with GNU Emacs.\n\
1359 This is usually the same as data-directory.");
1361 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
1362 "For internal use by the build procedure only.\n\
1363 This is the name of the directory in which the build procedure installed\n\
1364 Emacs's info files; the default value for Info-default-directory-list\n\
1366 Vconfigure_info_directory
= build_string (PATH_INFO
);
1368 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern
,
1369 "Pattern for making names for temporary files.\n\
1370 This is used by `call-process-region'.");
1371 /* This variable is initialized in init_callproc. */
1373 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
1374 "List of environment variables for subprocesses to inherit.\n\
1375 Each element should be a string of the form ENVVARNAME=VALUE.\n\
1376 The environment which Emacs inherits is placed in this variable\n\
1377 when Emacs starts.");
1380 defsubr (&Scall_process
);
1383 defsubr (&Scall_process_region
);