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 /* Clean up when exiting Fcall_process.
124 On MSDOS, delete the temporary file on any kind of termination.
125 On Unix, kill the process and any children on termination by signal. */
127 /* Nonzero if this is termination due to exit. */
128 static int call_process_exited
;
130 #ifndef VMS /* VMS version is in vmsproc.c. */
133 call_process_kill (fdpid
)
136 close (XFASTINT (Fcar (fdpid
)));
137 EMACS_KILLPG (XFASTINT (Fcdr (fdpid
)), SIGKILL
);
138 synch_process_alive
= 0;
143 call_process_cleanup (fdpid
)
147 /* for MSDOS fdpid is really (fd . tempfile) */
148 register Lisp_Object file
;
150 close (XFASTINT (Fcar (fdpid
)));
151 if (strcmp (XSTRING (file
)-> data
, NULL_DEVICE
) != 0)
152 unlink (XSTRING (file
)->data
);
153 #else /* not MSDOS */
154 register int pid
= XFASTINT (Fcdr (fdpid
));
157 if (call_process_exited
)
159 close (XFASTINT (Fcar (fdpid
)));
163 if (EMACS_KILLPG (pid
, SIGINT
) == 0)
165 int count
= specpdl_ptr
- specpdl
;
166 record_unwind_protect (call_process_kill
, fdpid
);
167 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
170 wait_for_termination (pid
);
172 specpdl_ptr
= specpdl
+ count
; /* Discard the unwind protect. */
173 message1 ("Waiting for process to die...done");
175 synch_process_alive
= 0;
176 close (XFASTINT (Fcar (fdpid
)));
177 #endif /* not MSDOS */
181 DEFUN ("call-process", Fcall_process
, Scall_process
, 1, MANY
, 0,
182 "Call PROGRAM synchronously in separate process.\n\
183 The program's input comes from file INFILE (nil means `/dev/null').\n\
184 Insert output in BUFFER before point; t means current buffer;\n\
185 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
186 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
187 REAL-BUFFER says what to do with standard output, as above,\n\
188 while STDERR-FILE says what to do with standard error in the child.\n\
189 STDERR-FILE may be nil (discard standard error output),\n\
190 t (mix it with ordinary output), or a file name string.\n\
192 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
193 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
195 If BUFFER is 0, `call-process' returns immediately with value nil.\n\
196 Otherwise it waits for PROGRAM to terminate\n\
197 and returns a numeric exit status or a signal description string.\n\
198 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
201 register Lisp_Object
*args
;
203 Lisp_Object infile
, buffer
, current_dir
, display
, path
;
210 int count
= specpdl_ptr
- specpdl
;
211 register unsigned char **new_argv
212 = (unsigned char **) alloca ((max (2, nargs
- 2)) * sizeof (char *));
213 struct buffer
*old
= current_buffer
;
214 /* File to use for stderr in the child.
215 t means use same as standard output. */
216 Lisp_Object error_file
;
217 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
218 char *outf
, *tempfile
;
224 struct coding_system process_coding
; /* coding-system of process output */
225 struct coding_system argument_coding
; /* coding-system of arguments */
227 CHECK_STRING (args
[0], 0);
232 /* Without asynchronous processes we cannot have BUFFER == 0. */
233 if (nargs
>= 3 && INTEGERP (args
[2]))
234 error ("Operating system cannot handle asynchronous subprocesses");
235 #endif /* subprocesses */
237 /* Decide the coding-system for giving arguments and reading process
240 Lisp_Object val
, *args2
;
241 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
242 Lisp_Object coding_systems
= Qt
;
245 /* If arguments are supplied, we may have to encode them. */
248 if (!NILP (Vcoding_system_for_write
))
249 val
= Vcoding_system_for_write
;
250 else if (NILP (current_buffer
->enable_multibyte_characters
))
254 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
255 args2
[0] = Qcall_process
;
256 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
257 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
258 if (CONSP (coding_systems
))
259 val
= XCONS (coding_systems
)->cdr
;
260 else if (CONSP (Vdefault_process_coding_system
))
261 val
= XCONS (Vdefault_process_coding_system
)->cdr
;
265 setup_coding_system (Fcheck_coding_system (val
), &argument_coding
);
268 /* If BUFFER is nil, we must read process output once and then
269 discard it, so setup coding system but with nil. If BUFFER is
270 an integer, we can discard it without reading. */
271 if (nargs
< 3 || NILP (args
[2]))
272 setup_coding_system (Qnil
, &process_coding
);
273 else if (!INTEGERP (args
[2]))
276 if (!NILP (Vcoding_system_for_read
))
277 val
= Vcoding_system_for_read
;
278 else if (NILP (current_buffer
->enable_multibyte_characters
))
282 if (!EQ (coding_systems
, Qt
))
284 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
285 args2
[0] = Qcall_process
;
286 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
288 = Ffind_operation_coding_system (nargs
+ 1, args2
);
290 if (CONSP (coding_systems
))
291 val
= XCONS (coding_systems
)->car
;
292 else if (CONSP (Vdefault_process_coding_system
))
293 val
= XCONS (Vdefault_process_coding_system
)->car
;
297 setup_coding_system (Fcheck_coding_system (val
), &process_coding
);
299 /* On MSDOS, if the user did not ask for binary, treat it as
300 "text" which means doing CRLF conversion. Otherwise, leave
303 Note that ``binary'' here only means whether EOLs should or
304 should not be converted, since that's what Vbinary_process_XXXput
305 meant in the days before the coding systems were introduced.
307 For other conversions, the caller should set coding-system
308 variables explicitly, or rely on auto-detection. */
310 /* FIXME: this probably should be moved into the guts of
311 `Ffind_operation_coding_system' for the case of `call-process'. */
312 if (NILP (Vbinary_process_output
))
314 process_coding
.eol_type
= CODING_EOL_CRLF
;
315 if (process_coding
.type
== coding_type_no_conversion
)
316 /* FIXME: should we set type to undecided? */
317 process_coding
.type
= coding_type_emacs_mule
;
320 process_coding
.eol_type
= CODING_EOL_LF
;
325 if (nargs
>= 2 && ! NILP (args
[1]))
327 infile
= Fexpand_file_name (args
[1], current_buffer
->directory
);
328 CHECK_STRING (infile
, 1);
331 infile
= build_string (NULL_DEVICE
);
337 /* If BUFFER is a list, its meaning is
338 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
341 if (CONSP (XCONS (buffer
)->cdr
))
343 Lisp_Object stderr_file
;
344 stderr_file
= XCONS (XCONS (buffer
)->cdr
)->car
;
346 if (NILP (stderr_file
) || EQ (Qt
, stderr_file
))
347 error_file
= stderr_file
;
349 error_file
= Fexpand_file_name (stderr_file
, Qnil
);
352 buffer
= XCONS (buffer
)->car
;
355 if (!(EQ (buffer
, Qnil
)
357 || XFASTINT (buffer
) == 0))
359 Lisp_Object spec_buffer
;
360 spec_buffer
= buffer
;
361 buffer
= Fget_buffer (buffer
);
362 /* Mention the buffer name for a better error message. */
364 CHECK_BUFFER (spec_buffer
, 2);
365 CHECK_BUFFER (buffer
, 2);
371 /* Make sure that the child will be able to chdir to the current
372 buffer's current directory, or its unhandled equivalent. We
373 can't just have the child check for an error when it does the
374 chdir, since it's in a vfork.
376 We have to GCPRO around this because Fexpand_file_name,
377 Funhandled_file_name_directory, and Ffile_accessible_directory_p
378 might call a file name handling function. The argument list is
379 protected by the caller, so all we really have to worry about is
382 struct gcpro gcpro1
, gcpro2
, gcpro3
;
384 current_dir
= current_buffer
->directory
;
386 GCPRO3 (infile
, buffer
, current_dir
);
389 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
391 if (NILP (Ffile_accessible_directory_p (current_dir
)))
392 report_file_error ("Setting current directory",
393 Fcons (current_buffer
->directory
, Qnil
));
398 display
= nargs
>= 4 ? args
[3] : Qnil
;
400 filefd
= open (XSTRING (infile
)->data
, O_RDONLY
, 0);
403 report_file_error ("Opening process input file", Fcons (infile
, Qnil
));
405 /* Search for program; barf if not found. */
409 GCPRO1 (current_dir
);
410 openp (Vexec_path
, args
[0], EXEC_SUFFIXES
, &path
, 1);
416 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
418 new_argv
[0] = XSTRING (path
)->data
;
421 for (i
= 4; i
< nargs
; i
++)
423 CHECK_STRING (args
[i
], i
);
424 if (argument_coding
.type
== coding_type_no_conversion
)
425 new_argv
[i
- 3] = XSTRING (args
[i
])->data
;
428 /* We must encode the arguments. */
429 int size
= encoding_buffer_size (&argument_coding
,
430 XSTRING (args
[i
])->size
);
432 unsigned char *dummy1
= (unsigned char *) alloca (size
);
434 /* The Irix 4.0 compiler barfs if we eliminate dummy. */
435 new_argv
[i
- 3] = dummy1
;
436 produced
= encode_coding (&argument_coding
,
437 XSTRING (args
[i
])->data
, new_argv
[i
- 3],
438 XSTRING (args
[i
])->size
, size
, &dummy
);
439 new_argv
[i
- 3][produced
] = 0;
445 #ifdef MSDOS /* MW, July 1993 */
446 if ((outf
= egetenv ("TMP")) || (outf
= egetenv ("TEMP")))
447 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
450 tempfile
= alloca (20);
453 dostounix_filename (tempfile
);
454 if (*tempfile
== '\0' || tempfile
[strlen (tempfile
) - 1] != '/')
455 strcat (tempfile
, "/");
456 strcat (tempfile
, "detmp.XXX");
459 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
463 report_file_error ("Opening process output file",
464 Fcons (build_string (tempfile
), Qnil
));
470 if (INTEGERP (buffer
))
471 fd
[1] = open (NULL_DEVICE
, O_WRONLY
), fd
[0] = -1;
478 /* Replaced by close_process_descs */
479 set_exclusive_use (fd
[0]);
484 /* child_setup must clobber environ in systems with true vfork.
485 Protect it from permanent change. */
486 register char **save_environ
= environ
;
487 register int fd1
= fd
[1];
490 #if 0 /* Some systems don't have sigblock. */
491 mask
= sigblock (sigmask (SIGCHLD
));
494 /* Record that we're about to create a synchronous process. */
495 synch_process_alive
= 1;
497 /* These vars record information from process termination.
498 Clear them now before process can possibly terminate,
499 to avoid timing error if process terminates soon. */
500 synch_process_death
= 0;
501 synch_process_retcode
= 0;
503 if (NILP (error_file
))
504 fd_error
= open (NULL_DEVICE
, O_WRONLY
);
505 else if (STRINGP (error_file
))
508 fd_error
= open (XSTRING (error_file
)->data
,
509 O_WRONLY
| O_TRUNC
| O_CREAT
| O_TEXT
,
511 #else /* not DOS_NT */
512 fd_error
= creat (XSTRING (error_file
)->data
, 0666);
513 #endif /* not DOS_NT */
526 report_file_error ("Cannot redirect stderr",
527 Fcons ((NILP (error_file
)
528 ? build_string (NULL_DEVICE
) : error_file
),
531 #ifdef MSDOS /* MW, July 1993 */
532 /* ??? Someone who knows MSDOG needs to check whether this properly
533 closes all descriptors that it opens.
535 Note that run_msdos_command() actually returns the child process
536 exit status, not its PID, so we assign it to `synch_process_retcode'
538 pid
= run_msdos_command (new_argv
, current_dir
,
539 filefd
, outfilefd
, fd_error
);
541 /* Record that the synchronous process exited and note its
542 termination status. */
543 synch_process_alive
= 0;
544 synch_process_retcode
= pid
;
545 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
546 synch_process_death
= strerror (errno
);
549 if (fd_error
!= outfilefd
)
551 fd1
= -1; /* No harm in closing that one! */
552 /* Since CRLF is converted to LF within `decode_coding', we can
553 always open a file with binary mode. */
554 fd
[0] = open (tempfile
, O_BINARY
);
559 report_file_error ("Cannot re-open temporary file", Qnil
);
561 #else /* not MSDOS */
563 pid
= child_setup (filefd
, fd1
, fd_error
, new_argv
, 0, current_dir
);
564 #else /* not WINDOWSNT */
574 #if defined (USG) && !defined (BSD_PGRPS)
579 child_setup (filefd
, fd1
, fd_error
, new_argv
, 0, current_dir
);
581 #endif /* not WINDOWSNT */
583 /* The MSDOS case did this already. */
586 #endif /* not MSDOS */
588 environ
= save_environ
;
590 /* Close most of our fd's, but not fd[0]
591 since we will use that to read input from. */
593 if (fd1
>= 0 && fd1
!= fd_error
)
601 report_file_error ("Doing vfork", Qnil
);
604 if (INTEGERP (buffer
))
609 /* If Emacs has been built with asynchronous subprocess support,
610 we don't need to do this, I think because it will then have
611 the facilities for handling SIGCHLD. */
612 wait_without_blocking ();
613 #endif /* subprocesses */
617 /* Enable sending signal if user quits below. */
618 call_process_exited
= 0;
621 /* MSDOS needs different cleanup information. */
622 record_unwind_protect (call_process_cleanup
,
623 Fcons (make_number (fd
[0]), build_string (tempfile
)));
625 record_unwind_protect (call_process_cleanup
,
626 Fcons (make_number (fd
[0]), make_number (pid
)));
627 #endif /* not MSDOS */
630 if (BUFFERP (buffer
))
631 Fset_buffer (buffer
);
643 /* Repeatedly read until we've filled as much as possible
644 of the buffer size we have. But don't read
645 less than 1024--save that for the next bufferful. */
647 nread
= process_coding
.carryover_size
; /* This value is initially 0. */
648 while (nread
< bufsize
- 1024)
651 = read (fd
[0], bufptr
+ nread
, bufsize
- nread
);
664 /* Now NREAD is the total amount of data in the buffer. */
666 /* Here, just tell decode_coding that we are processing the
667 last block. We break the loop after decoding. */
668 process_coding
.last_block
= 1;
675 if (process_coding
.type
== coding_type_no_conversion
)
676 insert (bufptr
, nread
);
678 { /* We have to decode the input. */
679 int size
= decoding_buffer_size (&process_coding
, bufsize
);
680 char *decoding_buf
= get_conversion_buffer (size
);
683 nread
= decode_coding (&process_coding
, bufptr
, decoding_buf
,
684 nread
, size
, &dummy
);
686 insert (decoding_buf
, nread
);
690 if (process_coding
.last_block
)
693 /* Make the buffer bigger as we continue to read more data,
695 if (bufsize
< 64 * 1024 && total_read
> 32 * bufsize
)
698 bufptr
= (char *) alloca (bufsize
);
701 if (!NILP (buffer
) && process_coding
.carryover_size
> 0)
702 /* We have carryover in the last decoding. It should be
703 processed again after reading more data. */
704 bcopy (process_coding
.carryover
, bufptr
,
705 process_coding
.carryover_size
);
707 if (!NILP (display
) && INTERACTIVE
)
710 prepare_menu_bars ();
712 redisplay_preserve_echo_area ();
720 /* Wait for it to terminate, unless it already has. */
721 wait_for_termination (pid
);
725 set_buffer_internal (old
);
727 /* Don't kill any children that the subprocess may have left behind
729 call_process_exited
= 1;
731 unbind_to (count
, Qnil
);
733 if (synch_process_death
)
734 return build_string (synch_process_death
);
735 return make_number (synch_process_retcode
);
740 delete_temp_file (name
)
743 /* Use Fdelete_file (indirectly) because that runs a file name handler.
744 We did that when writing the file, so we should do so when deleting. */
745 internal_delete_file (name
);
748 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
750 "Send text from START to END to a synchronous process running PROGRAM.\n\
751 Delete the text if fourth arg DELETE is non-nil.\n\
753 Insert output in BUFFER before point; t means current buffer;\n\
754 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
755 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
756 REAL-BUFFER says what to do with standard output, as above,\n\
757 while STDERR-FILE says what to do with standard error in the child.\n\
758 STDERR-FILE may be nil (discard standard error output),\n\
759 t (mix it with ordinary output), or a file name string.\n\
761 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
762 Remaining args are passed to PROGRAM at startup as command args.\n\
764 If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
765 Otherwise it waits for PROGRAM to terminate\n\
766 and returns a numeric exit status or a signal description string.\n\
767 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
770 register Lisp_Object
*args
;
773 Lisp_Object filename_string
;
774 register Lisp_Object start
, end
;
775 int count
= specpdl_ptr
- specpdl
;
776 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
777 Lisp_Object coding_systems
= Qt
;
778 Lisp_Object val
, *args2
;
784 if ((outf
= egetenv ("TMP")) || (outf
= egetenv ("TEMP")))
785 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
788 tempfile
= alloca (20);
791 if (!IS_DIRECTORY_SEP (tempfile
[strlen (tempfile
) - 1]))
792 strcat (tempfile
, "/");
793 if ('/' == DIRECTORY_SEP
)
794 dostounix_filename (tempfile
);
796 unixtodos_filename (tempfile
);
798 strcat (tempfile
, "emXXXXXX");
800 strcat (tempfile
, "detmp.XXX");
802 #else /* not DOS_NT */
803 char *tempfile
= (char *) alloca (XSTRING (Vtemp_file_name_pattern
)->size
+ 1);
804 bcopy (XSTRING (Vtemp_file_name_pattern
)->data
, tempfile
,
805 XSTRING (Vtemp_file_name_pattern
)->size
+ 1);
806 #endif /* not DOS_NT */
810 filename_string
= build_string (tempfile
);
811 GCPRO1 (filename_string
);
814 /* Decide coding-system of the contents of the temporary file. */
816 /* This is to cause find-buffer-file-type-coding-system (see
817 dos-w32.el) to choose correct EOL translation for write-region. */
818 specbind (Qbuffer_file_type
, Vbinary_process_input
);
820 if (!NILP (Vcoding_system_for_write
))
821 val
= Vcoding_system_for_write
;
822 else if (NILP (current_buffer
->enable_multibyte_characters
))
826 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
827 args2
[0] = Qcall_process_region
;
828 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
829 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
830 if (CONSP (coding_systems
))
831 val
= XCONS (coding_systems
)->cdr
;
832 else if (CONSP (Vdefault_process_coding_system
))
833 val
= XCONS (Vdefault_process_coding_system
)->cdr
;
839 /* binary-process-input tells whether the buffer needs to be
840 written with EOL conversions, but it doesn't say anything
841 about the rest of text encoding. It takes effect whenever
842 the coding system doesn't otherwise specify what to do for
846 if (! NILP (Vbinary_process_input
))
847 val
= intern ("undecided-unix");
849 val
= intern ("undecided-dos");
851 else if (SYMBOLP (val
))
854 eolval
= Fget (val
, Qeol_type
);
855 if (VECTORP (eolval
) && XVECTOR (eolval
)->size
> 1)
856 /* Use element 1 (CRLF conversion) for "text",
857 and element 0 (LF conversion) for "binary". */
858 val
= XVECTOR (eolval
)->contents
[NILP (Vbinary_process_input
)];
862 specbind (intern ("coding-system-for-write"), val
);
863 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
, Qnil
);
865 /* Note that Fcall_process takes care of binding
866 coding-system-for-read. */
868 record_unwind_protect (delete_temp_file
, filename_string
);
871 Fdelete_region (start
, end
);
873 args
[3] = filename_string
;
875 RETURN_UNGCPRO (unbind_to (count
, Fcall_process (nargs
- 2, args
+ 2)));
878 #ifndef VMS /* VMS version is in vmsproc.c. */
880 /* This is the last thing run in a newly forked inferior
881 either synchronous or asynchronous.
882 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
883 Initialize inferior's priority, pgrp, connected dir and environment.
884 then exec another program based on new_argv.
886 This function may change environ for the superior process.
887 Therefore, the superior process must save and restore the value
888 of environ around the vfork and the call to this function.
890 ENV is the environment for the subprocess.
892 SET_PGRP is nonzero if we should put the subprocess into a separate
895 CURRENT_DIR is an elisp string giving the path of the current
896 directory the subprocess should have. Since we can't really signal
897 a decent error from within the child, this should be verified as an
898 executable directory by the parent. */
900 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
902 register char **new_argv
;
904 Lisp_Object current_dir
;
907 /* The MSDOS port of gcc cannot fork, vfork, ... so we must call system
909 #else /* not MSDOS */
915 #endif /* WINDOWSNT */
919 #ifdef SET_EMACS_PRIORITY
921 extern int emacs_priority
;
923 if (emacs_priority
< 0)
924 nice (- emacs_priority
);
929 /* Close Emacs's descriptors that this process should not have. */
930 close_process_descs ();
934 /* Note that use of alloca is always safe here. It's obvious for systems
935 that do not have true vfork or that have true (stack) alloca.
936 If using vfork and C_ALLOCA it is safe because that changes
937 the superior's static variables as if the superior had done alloca
938 and will be cleaned up in the usual way. */
943 i
= XSTRING (current_dir
)->size
;
944 pwd_var
= (char *) alloca (i
+ 6);
946 bcopy ("PWD=", pwd_var
, 4);
947 bcopy (XSTRING (current_dir
)->data
, temp
, i
);
948 if (!IS_DIRECTORY_SEP (temp
[i
- 1])) temp
[i
++] = DIRECTORY_SEP
;
951 /* We can't signal an Elisp error here; we're in a vfork. Since
952 the callers check the current directory before forking, this
953 should only return an error if the directory's permissions
954 are changed between the check and this chdir, but we should
956 if (chdir (temp
) < 0)
959 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
960 while (i
> 2 && IS_DIRECTORY_SEP (temp
[i
- 1]))
964 /* Set `env' to a vector of the strings in Vprocess_environment. */
966 register Lisp_Object tem
;
967 register char **new_env
;
968 register int new_length
;
971 for (tem
= Vprocess_environment
;
972 CONSP (tem
) && STRINGP (XCONS (tem
)->car
);
973 tem
= XCONS (tem
)->cdr
)
976 /* new_length + 2 to include PWD and terminating 0. */
977 env
= new_env
= (char **) alloca ((new_length
+ 2) * sizeof (char *));
979 /* If we have a PWD envvar, pass one down,
980 but with corrected value. */
982 *new_env
++ = pwd_var
;
984 /* Copy the Vprocess_environment strings into new_env. */
985 for (tem
= Vprocess_environment
;
986 CONSP (tem
) && STRINGP (XCONS (tem
)->car
);
987 tem
= XCONS (tem
)->cdr
)
990 char *string
= (char *) XSTRING (XCONS (tem
)->car
)->data
;
991 /* See if this string duplicates any string already in the env.
992 If so, don't put it in.
993 When an env var has multiple definitions,
994 we keep the definition that comes first in process-environment. */
995 for (; ep
!= new_env
; ep
++)
997 char *p
= *ep
, *q
= string
;
1001 /* The string is malformed; might as well drop it. */
1010 *new_env
++ = string
;
1016 prepare_standard_handles (in
, out
, err
, handles
);
1017 #else /* not WINDOWSNT */
1018 /* Make sure that in, out, and err are not actually already in
1019 descriptors zero, one, or two; this could happen if Emacs is
1020 started with its standard in, out, or error closed, as might
1023 int oin
= in
, oout
= out
;
1025 /* We have to avoid relocating the same descriptor twice! */
1027 in
= relocate_fd (in
, 3);
1032 out
= relocate_fd (out
, 3);
1036 else if (err
== oout
)
1039 err
= relocate_fd (err
, 3);
1052 #endif /* not WINDOWSNT */
1054 #if defined(USG) && !defined(BSD_PGRPS)
1055 #ifndef SETPGRP_RELEASES_CTTY
1056 setpgrp (); /* No arguments but equivalent in this case */
1061 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1062 EMACS_SET_TTY_PGRP (0, &pid
);
1065 something missing here
;
1069 /* Spawn the child. (See ntproc.c:Spawnve). */
1070 cpid
= spawnve (_P_NOWAIT
, new_argv
[0], new_argv
, env
);
1072 /* An error occurred while trying to spawn the process. */
1073 report_file_error ("Spawning child process", Qnil
);
1074 reset_standard_handles (in
, out
, err
, handles
);
1076 #else /* not WINDOWSNT */
1077 /* execvp does not accept an environment arg so the only way
1078 to pass this environment is to set environ. Our caller
1079 is responsible for restoring the ambient value of environ. */
1081 execvp (new_argv
[0], new_argv
);
1083 write (1, "Can't exec program: ", 20);
1084 write (1, new_argv
[0], strlen (new_argv
[0]));
1087 #endif /* not WINDOWSNT */
1088 #endif /* not MSDOS */
1091 /* Move the file descriptor FD so that its number is not less than MIN.
1092 If the file descriptor is moved at all, the original is freed. */
1094 relocate_fd (fd
, min
)
1104 char *message1
= "Error while setting up child: ";
1105 char *errmessage
= strerror (errno
);
1106 char *message2
= "\n";
1107 write (2, message1
, strlen (message1
));
1108 write (2, errmessage
, strlen (errmessage
));
1109 write (2, message2
, strlen (message2
));
1112 /* Note that we hold the original FD open while we recurse,
1113 to guarantee we'll get a new FD if we need it. */
1114 new = relocate_fd (new, min
);
1121 getenv_internal (var
, varlen
, value
, valuelen
)
1129 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCONS (scan
)->cdr
)
1133 entry
= XCONS (scan
)->car
;
1135 && XSTRING (entry
)->size
> varlen
1136 && XSTRING (entry
)->data
[varlen
] == '='
1138 /* NT environment variables are case insensitive. */
1139 && ! strnicmp (XSTRING (entry
)->data
, var
, varlen
)
1140 #else /* not WINDOWSNT */
1141 && ! bcmp (XSTRING (entry
)->data
, var
, varlen
)
1142 #endif /* not WINDOWSNT */
1145 *value
= (char *) XSTRING (entry
)->data
+ (varlen
+ 1);
1146 *valuelen
= XSTRING (entry
)->size
- (varlen
+ 1);
1154 DEFUN ("getenv", Fgetenv
, Sgetenv
, 1, 1, 0,
1155 "Return the value of environment variable VAR, as a string.\n\
1156 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
1157 This function consults the variable ``process-environment'' for its value.")
1164 CHECK_STRING (var
, 0);
1165 if (getenv_internal (XSTRING (var
)->data
, XSTRING (var
)->size
,
1167 return make_string (value
, valuelen
);
1172 /* A version of getenv that consults process_environment, easily
1181 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
1187 #endif /* not VMS */
1189 /* This is run before init_cmdargs. */
1193 char *data_dir
= egetenv ("EMACSDATA");
1194 char *doc_dir
= egetenv ("EMACSDOC");
1197 = Ffile_name_as_directory (build_string (data_dir
? data_dir
1200 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
1203 /* Check the EMACSPATH environment variable, defaulting to the
1204 PATH_EXEC path from paths.h. */
1205 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1206 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
1207 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1210 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1214 char *data_dir
= egetenv ("EMACSDATA");
1217 Lisp_Object tempdir
;
1219 if (initialized
&& !NILP (Vinstallation_directory
))
1221 /* Add to the path the lib-src subdir of the installation dir. */
1223 tem
= Fexpand_file_name (build_string ("lib-src"),
1224 Vinstallation_directory
);
1225 if (NILP (Fmember (tem
, Vexec_path
)))
1228 /* MSDOS uses wrapped binaries, so don't do this. */
1229 Vexec_path
= nconc2 (Vexec_path
, Fcons (tem
, Qnil
));
1230 Vexec_directory
= Ffile_name_as_directory (tem
);
1231 #endif /* not DOS_NT */
1234 /* Maybe use ../etc as well as ../lib-src. */
1237 tem
= Fexpand_file_name (build_string ("etc"),
1238 Vinstallation_directory
);
1239 Vdoc_directory
= Ffile_name_as_directory (tem
);
1243 /* Look for the files that should be in etc. We don't use
1244 Vinstallation_directory, because these files are never installed
1245 near the executable, and they are never in the build
1246 directory when that's different from the source directory.
1248 Instead, if these files are not in the nominal place, we try the
1249 source directory. */
1252 Lisp_Object tem
, tem1
, newdir
;
1254 tem
= Fexpand_file_name (build_string ("GNU"), Vdata_directory
);
1255 tem1
= Ffile_exists_p (tem
);
1258 newdir
= Fexpand_file_name (build_string ("../etc/"),
1259 build_string (PATH_DUMPLOADSEARCH
));
1260 tem
= Fexpand_file_name (build_string ("GNU"), newdir
);
1261 tem1
= Ffile_exists_p (tem
);
1263 Vdata_directory
= newdir
;
1271 tempdir
= Fdirectory_file_name (Vexec_directory
);
1272 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1273 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1277 tempdir
= Fdirectory_file_name (Vdata_directory
);
1278 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1279 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1283 Vshell_file_name
= build_string ("*dcl*");
1285 sh
= (char *) getenv ("SHELL");
1286 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
1290 Vtemp_file_name_pattern
= build_string ("tmp:emacsXXXXXX.");
1292 if (getenv ("TMPDIR"))
1294 char *dir
= getenv ("TMPDIR");
1295 Vtemp_file_name_pattern
1296 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1297 build_string (dir
));
1300 Vtemp_file_name_pattern
= build_string ("/tmp/emacsXXXXXX");
1304 set_process_environment ()
1306 register char **envp
;
1308 Vprocess_environment
= Qnil
;
1312 for (envp
= environ
; *envp
; envp
++)
1313 Vprocess_environment
= Fcons (build_string (*envp
),
1314 Vprocess_environment
);
1320 Qbuffer_file_type
= intern ("buffer-file-type");
1321 staticpro (&Qbuffer_file_type
);
1323 DEFVAR_LISP ("binary-process-input", &Vbinary_process_input
,
1324 "*If non-nil then new subprocesses are assumed to take binary input.");
1325 Vbinary_process_input
= Qnil
;
1327 DEFVAR_LISP ("binary-process-output", &Vbinary_process_output
,
1328 "*If non-nil then new subprocesses are assumed to produce binary output.");
1329 Vbinary_process_output
= Qnil
;
1332 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
1333 "*File name to load inferior shells from.\n\
1334 Initialized from the SHELL environment variable.");
1336 DEFVAR_LISP ("exec-path", &Vexec_path
,
1337 "*List of directories to search programs to run in subprocesses.\n\
1338 Each element is a string (directory name) or nil (try default directory).");
1340 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
1341 "Directory of architecture-dependent files that come with GNU Emacs,\n\
1342 especially executable programs intended for Emacs to invoke.");
1344 DEFVAR_LISP ("data-directory", &Vdata_directory
,
1345 "Directory of architecture-independent files that come with GNU Emacs,\n\
1346 intended for Emacs to use.");
1348 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
1349 "Directory containing the DOC file that comes with GNU Emacs.\n\
1350 This is usually the same as data-directory.");
1352 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
1353 "For internal use by the build procedure only.\n\
1354 This is the name of the directory in which the build procedure installed\n\
1355 Emacs's info files; the default value for Info-default-directory-list\n\
1357 Vconfigure_info_directory
= build_string (PATH_INFO
);
1359 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern
,
1360 "Pattern for making names for temporary files.\n\
1361 This is used by `call-process-region'.");
1362 /* This variable is initialized in init_callproc. */
1364 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
1365 "List of environment variables for subprocesses to inherit.\n\
1366 Each element should be a string of the form ENVVARNAME=VALUE.\n\
1367 The environment which Emacs inherits is placed in this variable\n\
1368 when Emacs starts.");
1371 defsubr (&Scall_process
);
1374 defsubr (&Scall_process_region
);