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 */
55 #define INCLUDED_FCNTL
58 #include <sys/param.h>
78 #include "syssignal.h"
86 extern noshare
char **environ
;
88 extern char **environ
;
91 #define max(a, b) ((a) > (b) ? (a) : (b))
93 Lisp_Object Vexec_path
, Vexec_directory
, Vdata_directory
, Vdoc_directory
;
94 Lisp_Object Vconfigure_info_directory
;
95 Lisp_Object Vtemp_file_name_pattern
;
97 Lisp_Object Vshell_file_name
;
99 Lisp_Object Vprocess_environment
;
102 Lisp_Object Qbuffer_file_type
;
105 /* True iff we are about to fork off a synchronous process or if we
106 are waiting for it. */
107 int synch_process_alive
;
109 /* Nonzero => this is a string explaining death of synchronous subprocess. */
110 char *synch_process_death
;
112 /* If synch_process_death is zero,
113 this is exit code of synchronous subprocess. */
114 int synch_process_retcode
;
116 extern Lisp_Object Vdoc_file_name
;
118 extern Lisp_Object Vfile_name_coding_system
, Vdefault_file_name_coding_system
;
120 /* Clean up when exiting Fcall_process.
121 On MSDOS, delete the temporary file on any kind of termination.
122 On Unix, kill the process and any children on termination by signal. */
124 /* Nonzero if this is termination due to exit. */
125 static int call_process_exited
;
127 #ifndef VMS /* VMS version is in vmsproc.c. */
130 call_process_kill (fdpid
)
133 close (XFASTINT (Fcar (fdpid
)));
134 EMACS_KILLPG (XFASTINT (Fcdr (fdpid
)), SIGKILL
);
135 synch_process_alive
= 0;
140 call_process_cleanup (fdpid
)
144 /* for MSDOS fdpid is really (fd . tempfile) */
145 register Lisp_Object file
;
147 close (XFASTINT (Fcar (fdpid
)));
148 if (strcmp (XSTRING (file
)-> data
, NULL_DEVICE
) != 0)
149 unlink (XSTRING (file
)->data
);
150 #else /* not MSDOS */
151 register int pid
= XFASTINT (Fcdr (fdpid
));
154 if (call_process_exited
)
156 close (XFASTINT (Fcar (fdpid
)));
160 if (EMACS_KILLPG (pid
, SIGINT
) == 0)
162 int count
= specpdl_ptr
- specpdl
;
163 record_unwind_protect (call_process_kill
, fdpid
);
164 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
167 wait_for_termination (pid
);
169 specpdl_ptr
= specpdl
+ count
; /* Discard the unwind protect. */
170 message1 ("Waiting for process to die...done");
172 synch_process_alive
= 0;
173 close (XFASTINT (Fcar (fdpid
)));
174 #endif /* not MSDOS */
178 DEFUN ("call-process", Fcall_process
, Scall_process
, 1, MANY
, 0,
179 "Call PROGRAM synchronously in separate process.\n\
180 The remaining arguments are optional.\n\
181 The program's input comes from file INFILE (nil means `/dev/null').\n\
182 Insert output in BUFFER before point; t means current buffer;\n\
183 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
184 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
185 REAL-BUFFER says what to do with standard output, as above,\n\
186 while STDERR-FILE says what to do with standard error in the child.\n\
187 STDERR-FILE may be nil (discard standard error output),\n\
188 t (mix it with ordinary output), or a file name string.\n\
190 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
191 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
193 If BUFFER is 0, `call-process' returns immediately with value nil.\n\
194 Otherwise it waits for PROGRAM to terminate\n\
195 and returns a numeric exit status or a signal description string.\n\
196 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
199 register Lisp_Object
*args
;
201 Lisp_Object infile
, buffer
, current_dir
, display
, path
;
208 int count
= specpdl_ptr
- specpdl
;
210 register unsigned char **new_argv
211 = (unsigned char **) alloca ((max (2, nargs
- 2)) * sizeof (char *));
212 struct buffer
*old
= current_buffer
;
213 /* File to use for stderr in the child.
214 t means use same as standard output. */
215 Lisp_Object error_file
;
216 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
217 char *outf
, *tempfile
;
223 struct coding_system process_coding
; /* coding-system of process output */
224 struct coding_system argument_coding
; /* coding-system of arguments */
226 CHECK_STRING (args
[0], 0);
231 /* Without asynchronous processes we cannot have BUFFER == 0. */
233 && (INTEGERP (CONSP (args
[2]) ? XCAR (args
[2]) : 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. */
250 for (i
= 4; i
< nargs
; i
++)
251 CHECK_STRING (args
[i
], i
);
253 for (i
= 4; i
< nargs
; i
++)
254 if (STRING_MULTIBYTE (args
[i
]))
257 if (!NILP (Vcoding_system_for_write
))
258 val
= Vcoding_system_for_write
;
259 else if (! must_encode
)
263 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
264 args2
[0] = Qcall_process
;
265 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
266 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
267 if (CONSP (coding_systems
))
268 val
= XCONS (coding_systems
)->cdr
;
269 else if (CONSP (Vdefault_process_coding_system
))
270 val
= XCONS (Vdefault_process_coding_system
)->cdr
;
274 setup_coding_system (Fcheck_coding_system (val
), &argument_coding
);
277 /* If BUFFER is nil, we must read process output once and then
278 discard it, so setup coding system but with nil. If BUFFER is
279 an integer, we can discard it without reading. */
280 if (nargs
< 3 || NILP (args
[2])
281 || (CONSP (args
[2]) && NILP (XCAR (args
[2]))))
282 setup_coding_system (Qnil
, &process_coding
);
283 else if (!INTEGERP (CONSP (args
[2]) ? XCAR (args
[2]) : args
[2]))
286 if (!NILP (Vcoding_system_for_read
))
287 val
= Vcoding_system_for_read
;
288 else if (NILP (current_buffer
->enable_multibyte_characters
))
292 if (EQ (coding_systems
, Qt
))
294 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
295 args2
[0] = Qcall_process
;
296 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
298 = Ffind_operation_coding_system (nargs
+ 1, args2
);
300 if (CONSP (coding_systems
))
301 val
= XCONS (coding_systems
)->car
;
302 else if (CONSP (Vdefault_process_coding_system
))
303 val
= XCONS (Vdefault_process_coding_system
)->car
;
307 setup_coding_system (Fcheck_coding_system (val
), &process_coding
);
311 if (nargs
>= 2 && ! NILP (args
[1]))
313 infile
= Fexpand_file_name (args
[1], current_buffer
->directory
);
314 CHECK_STRING (infile
, 1);
317 infile
= build_string (NULL_DEVICE
);
323 /* If BUFFER is a list, its meaning is
324 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
327 if (CONSP (XCONS (buffer
)->cdr
))
329 Lisp_Object stderr_file
;
330 stderr_file
= XCONS (XCONS (buffer
)->cdr
)->car
;
332 if (NILP (stderr_file
) || EQ (Qt
, stderr_file
))
333 error_file
= stderr_file
;
335 error_file
= Fexpand_file_name (stderr_file
, Qnil
);
338 buffer
= XCONS (buffer
)->car
;
341 if (!(EQ (buffer
, Qnil
)
343 || INTEGERP (buffer
)))
345 Lisp_Object spec_buffer
;
346 spec_buffer
= buffer
;
347 buffer
= Fget_buffer_create (buffer
);
348 /* Mention the buffer name for a better error message. */
350 CHECK_BUFFER (spec_buffer
, 2);
351 CHECK_BUFFER (buffer
, 2);
357 /* Make sure that the child will be able to chdir to the current
358 buffer's current directory, or its unhandled equivalent. We
359 can't just have the child check for an error when it does the
360 chdir, since it's in a vfork.
362 We have to GCPRO around this because Fexpand_file_name,
363 Funhandled_file_name_directory, and Ffile_accessible_directory_p
364 might call a file name handling function. The argument list is
365 protected by the caller, so all we really have to worry about is
368 struct gcpro gcpro1
, gcpro2
, gcpro3
;
370 current_dir
= current_buffer
->directory
;
372 GCPRO3 (infile
, buffer
, current_dir
);
375 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
377 if (NILP (Ffile_accessible_directory_p (current_dir
)))
378 report_file_error ("Setting current directory",
379 Fcons (current_buffer
->directory
, Qnil
));
384 display
= nargs
>= 4 ? args
[3] : Qnil
;
386 filefd
= open (XSTRING (infile
)->data
, O_RDONLY
, 0);
389 report_file_error ("Opening process input file", Fcons (infile
, Qnil
));
391 /* Search for program; barf if not found. */
395 GCPRO1 (current_dir
);
396 openp (Vexec_path
, args
[0], EXEC_SUFFIXES
, &path
, 1);
402 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
404 new_argv
[0] = XSTRING (path
)->data
;
409 if (! CODING_REQUIRE_ENCODING (&argument_coding
))
411 for (i
= 4; i
< nargs
; i
++)
412 new_argv
[i
- 3] = XSTRING (args
[i
])->data
;
416 /* We must encode the arguments. */
417 struct gcpro gcpro1
, gcpro2
, gcpro3
;
419 GCPRO3 (infile
, buffer
, current_dir
);
420 for (i
= 4; i
< nargs
; i
++)
422 int size
= encoding_buffer_size (&argument_coding
,
423 STRING_BYTES (XSTRING (args
[i
])));
424 unsigned char *dummy1
= (unsigned char *) alloca (size
);
427 /* The Irix 4.0 compiler barfs if we eliminate dummy. */
428 new_argv
[i
- 3] = dummy1
;
429 argument_coding
.mode
|= CODING_MODE_LAST_BLOCK
;
430 encode_coding (&argument_coding
,
431 XSTRING (args
[i
])->data
,
433 STRING_BYTES (XSTRING (args
[i
])),
435 new_argv
[i
- 3][argument_coding
.produced
] = 0;
436 /* We have to initialize CCL program status again. */
437 if (argument_coding
.type
== coding_type_ccl
)
438 setup_ccl_program (&(argument_coding
.spec
.ccl
.encoder
), Qnil
);
442 new_argv
[nargs
- 3] = 0;
447 #ifdef MSDOS /* MW, July 1993 */
448 if ((outf
= egetenv ("TMPDIR")))
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
),
534 current_dir
= ENCODE_FILE (current_dir
);
536 #ifdef MSDOS /* MW, July 1993 */
537 /* Note that on MSDOS `child_setup' actually returns the child process
538 exit status, not its PID, so we assign it to `synch_process_retcode'
540 pid
= child_setup (filefd
, outfilefd
, fd_error
, (char **) new_argv
,
543 /* Record that the synchronous process exited and note its
544 termination status. */
545 synch_process_alive
= 0;
546 synch_process_retcode
= pid
;
547 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
548 synch_process_death
= strerror (errno
);
551 if (fd_error
!= outfilefd
)
553 fd1
= -1; /* No harm in closing that one! */
554 /* Since CRLF is converted to LF within `decode_coding', we can
555 always open a file with binary mode. */
556 fd
[0] = open (tempfile
, O_BINARY
);
561 report_file_error ("Cannot re-open temporary file", Qnil
);
563 #else /* not MSDOS */
565 pid
= child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
567 #else /* not WINDOWSNT */
577 #if defined (USG) && !defined (BSD_PGRPS)
582 child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
585 #endif /* not WINDOWSNT */
587 /* The MSDOS case did this already. */
590 #endif /* not MSDOS */
592 environ
= save_environ
;
594 /* Close most of our fd's, but not fd[0]
595 since we will use that to read input from. */
597 if (fd1
>= 0 && fd1
!= fd_error
)
605 report_file_error ("Doing vfork", Qnil
);
608 if (INTEGERP (buffer
))
613 /* If Emacs has been built with asynchronous subprocess support,
614 we don't need to do this, I think because it will then have
615 the facilities for handling SIGCHLD. */
616 wait_without_blocking ();
617 #endif /* subprocesses */
621 /* Enable sending signal if user quits below. */
622 call_process_exited
= 0;
625 /* MSDOS needs different cleanup information. */
626 record_unwind_protect (call_process_cleanup
,
627 Fcons (make_number (fd
[0]), build_string (tempfile
)));
629 record_unwind_protect (call_process_cleanup
,
630 Fcons (make_number (fd
[0]), make_number (pid
)));
631 #endif /* not MSDOS */
634 if (BUFFERP (buffer
))
635 Fset_buffer (buffer
);
645 int display_on_the_fly
= !NILP (display
) && INTERACTIVE
;
646 struct coding_system saved_coding
;
648 saved_coding
= process_coding
;
652 /* Repeatedly read until we've filled as much as possible
653 of the buffer size we have. But don't read
654 less than 1024--save that for the next bufferful. */
656 while (nread
< bufsize
- 1024)
658 int this_read
= read (fd
[0], bufptr
+ nread
, bufsize
- nread
);
665 process_coding
.mode
|= CODING_MODE_LAST_BLOCK
;
670 total_read
+= this_read
;
672 if (display_on_the_fly
)
676 /* Now NREAD is the total amount of data in the buffer. */
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
, nread
);
686 char *decoding_buf
= (char *) malloc (size
);
688 decode_coding (&process_coding
, bufptr
, decoding_buf
,
690 if (display_on_the_fly
691 && saved_coding
.type
== coding_type_undecided
692 && process_coding
.type
!= coding_type_undecided
)
694 /* We have detected some coding system. But,
695 there's a possibility that the detection was
696 done by insufficient data. So, we give up
697 displaying on the fly. */
699 display_on_the_fly
= 0;
700 process_coding
= saved_coding
;
704 if (process_coding
.produced
> 0)
705 insert (decoding_buf
, process_coding
.produced
);
707 carryover
= nread
- process_coding
.consumed
;
710 /* As CARRYOVER should not be that large, we had
711 better avoid overhead of bcopy. */
712 char *p
= bufptr
+ process_coding
.consumed
;
713 char *pend
= p
+ carryover
;
716 while (p
< pend
) *dst
++ = *p
++;
720 if (process_coding
.mode
& CODING_MODE_LAST_BLOCK
)
723 insert (bufptr
, carryover
);
727 /* Make the buffer bigger as we continue to read more data,
729 if (bufsize
< 64 * 1024 && total_read
> 32 * bufsize
)
732 bufptr
= (char *) alloca (bufsize
);
735 if (!NILP (display
) && INTERACTIVE
)
738 prepare_menu_bars ();
740 redisplay_preserve_echo_area ();
747 Vlast_coding_system_used
= process_coding
.symbol
;
749 /* If the caller required, let the buffer inherit the
750 coding-system used to decode the process output. */
751 if (inherit_process_coding_system
)
752 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
753 make_number (total_read
));
756 /* Wait for it to terminate, unless it already has. */
757 wait_for_termination (pid
);
761 set_buffer_internal (old
);
763 /* Don't kill any children that the subprocess may have left behind
765 call_process_exited
= 1;
767 unbind_to (count
, Qnil
);
769 if (synch_process_death
)
770 return build_string (synch_process_death
);
771 return make_number (synch_process_retcode
);
776 delete_temp_file (name
)
779 /* Use Fdelete_file (indirectly) because that runs a file name handler.
780 We did that when writing the file, so we should do so when deleting. */
781 internal_delete_file (name
);
784 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
786 "Send text from START to END to a synchronous process running PROGRAM.\n\
787 The remaining arguments are optional.\n\
788 Delete the text if fourth arg DELETE is non-nil.\n\
790 Insert output in BUFFER before point; t means current buffer;\n\
791 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
792 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
793 REAL-BUFFER says what to do with standard output, as above,\n\
794 while STDERR-FILE says what to do with standard error in the child.\n\
795 STDERR-FILE may be nil (discard standard error output),\n\
796 t (mix it with ordinary output), or a file name string.\n\
798 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
799 Remaining args are passed to PROGRAM at startup as command args.\n\
801 If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
802 Otherwise it waits for PROGRAM to terminate\n\
803 and returns a numeric exit status or a signal description string.\n\
804 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
807 register Lisp_Object
*args
;
810 Lisp_Object filename_string
;
811 register Lisp_Object start
, end
;
812 int count
= specpdl_ptr
- specpdl
;
813 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
814 Lisp_Object coding_systems
= Qt
;
815 Lisp_Object val
, *args2
;
821 if ((outf
= egetenv ("TMPDIR"))
822 || (outf
= egetenv ("TMP"))
823 || (outf
= egetenv ("TEMP")))
824 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
827 tempfile
= alloca (20);
830 if (!IS_DIRECTORY_SEP (tempfile
[strlen (tempfile
) - 1]))
831 strcat (tempfile
, "/");
832 if ('/' == DIRECTORY_SEP
)
833 dostounix_filename (tempfile
);
835 unixtodos_filename (tempfile
);
837 strcat (tempfile
, "emXXXXXX");
839 strcat (tempfile
, "detmp.XXX");
841 #else /* not DOS_NT */
842 char *tempfile
= (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
843 bcopy (XSTRING (Vtemp_file_name_pattern
)->data
, tempfile
,
844 STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
845 #endif /* not DOS_NT */
849 filename_string
= build_string (tempfile
);
850 GCPRO1 (filename_string
);
853 /* Decide coding-system of the contents of the temporary file. */
854 if (!NILP (Vcoding_system_for_write
))
855 val
= Vcoding_system_for_write
;
856 else if (NILP (current_buffer
->enable_multibyte_characters
))
860 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
861 args2
[0] = Qcall_process_region
;
862 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
863 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
864 if (CONSP (coding_systems
))
865 val
= XCONS (coding_systems
)->cdr
;
866 else if (CONSP (Vdefault_process_coding_system
))
867 val
= XCONS (Vdefault_process_coding_system
)->cdr
;
873 int count1
= specpdl_ptr
- specpdl
;
875 specbind (intern ("coding-system-for-write"), val
);
876 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
, Qnil
, Qnil
);
878 unbind_to (count1
, Qnil
);
881 /* Note that Fcall_process takes care of binding
882 coding-system-for-read. */
884 record_unwind_protect (delete_temp_file
, filename_string
);
886 if (nargs
> 3 && !NILP (args
[3]))
887 Fdelete_region (start
, end
);
899 args
[1] = filename_string
;
901 RETURN_UNGCPRO (unbind_to (count
, Fcall_process (nargs
, args
)));
904 #ifndef VMS /* VMS version is in vmsproc.c. */
906 static int relocate_fd ();
908 /* This is the last thing run in a newly forked inferior
909 either synchronous or asynchronous.
910 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
911 Initialize inferior's priority, pgrp, connected dir and environment.
912 then exec another program based on new_argv.
914 This function may change environ for the superior process.
915 Therefore, the superior process must save and restore the value
916 of environ around the vfork and the call to this function.
918 SET_PGRP is nonzero if we should put the subprocess into a separate
921 CURRENT_DIR is an elisp string giving the path of the current
922 directory the subprocess should have. Since we can't really signal
923 a decent error from within the child, this should be verified as an
924 executable directory by the parent. */
927 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
929 register char **new_argv
;
931 Lisp_Object current_dir
;
938 #endif /* WINDOWSNT */
942 #ifdef SET_EMACS_PRIORITY
944 extern int emacs_priority
;
946 if (emacs_priority
< 0)
947 nice (- emacs_priority
);
952 /* Close Emacs's descriptors that this process should not have. */
953 close_process_descs ();
955 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
956 we will lose if we call close_load_descs here. */
961 /* Note that use of alloca is always safe here. It's obvious for systems
962 that do not have true vfork or that have true (stack) alloca.
963 If using vfork and C_ALLOCA it is safe because that changes
964 the superior's static variables as if the superior had done alloca
965 and will be cleaned up in the usual way. */
970 i
= STRING_BYTES (XSTRING (current_dir
));
971 pwd_var
= (char *) alloca (i
+ 6);
973 bcopy ("PWD=", pwd_var
, 4);
974 bcopy (XSTRING (current_dir
)->data
, temp
, i
);
975 if (!IS_DIRECTORY_SEP (temp
[i
- 1])) temp
[i
++] = DIRECTORY_SEP
;
979 /* We can't signal an Elisp error here; we're in a vfork. Since
980 the callers check the current directory before forking, this
981 should only return an error if the directory's permissions
982 are changed between the check and this chdir, but we should
984 if (chdir (temp
) < 0)
989 /* Get past the drive letter, so that d:/ is left alone. */
990 if (i
> 2 && IS_DEVICE_SEP (temp
[1]) && IS_DIRECTORY_SEP (temp
[2]))
997 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
998 while (i
> 2 && IS_DIRECTORY_SEP (temp
[i
- 1]))
1002 /* Set `env' to a vector of the strings in Vprocess_environment. */
1004 register Lisp_Object tem
;
1005 register char **new_env
;
1006 register int new_length
;
1009 for (tem
= Vprocess_environment
;
1010 CONSP (tem
) && STRINGP (XCONS (tem
)->car
);
1011 tem
= XCONS (tem
)->cdr
)
1014 /* new_length + 2 to include PWD and terminating 0. */
1015 env
= new_env
= (char **) alloca ((new_length
+ 2) * sizeof (char *));
1017 /* If we have a PWD envvar, pass one down,
1018 but with corrected value. */
1020 *new_env
++ = pwd_var
;
1022 /* Copy the Vprocess_environment strings into new_env. */
1023 for (tem
= Vprocess_environment
;
1024 CONSP (tem
) && STRINGP (XCONS (tem
)->car
);
1025 tem
= XCONS (tem
)->cdr
)
1028 char *string
= (char *) XSTRING (XCONS (tem
)->car
)->data
;
1029 /* See if this string duplicates any string already in the env.
1030 If so, don't put it in.
1031 When an env var has multiple definitions,
1032 we keep the definition that comes first in process-environment. */
1033 for (; ep
!= new_env
; ep
++)
1035 char *p
= *ep
, *q
= string
;
1039 /* The string is malformed; might as well drop it. */
1048 *new_env
++ = string
;
1054 prepare_standard_handles (in
, out
, err
, handles
);
1055 set_process_dir (XSTRING (current_dir
)->data
);
1056 #else /* not WINDOWSNT */
1057 /* Make sure that in, out, and err are not actually already in
1058 descriptors zero, one, or two; this could happen if Emacs is
1059 started with its standard in, out, or error closed, as might
1062 int oin
= in
, oout
= out
;
1064 /* We have to avoid relocating the same descriptor twice! */
1066 in
= relocate_fd (in
, 3);
1071 out
= relocate_fd (out
, 3);
1075 else if (err
== oout
)
1078 err
= relocate_fd (err
, 3);
1092 #endif /* not MSDOS */
1093 #endif /* not WINDOWSNT */
1095 #if defined(USG) && !defined(BSD_PGRPS)
1096 #ifndef SETPGRP_RELEASES_CTTY
1097 setpgrp (); /* No arguments but equivalent in this case */
1102 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1103 EMACS_SET_TTY_PGRP (0, &pid
);
1106 something missing here
;
1110 pid
= run_msdos_command (new_argv
, pwd_var
+ 4, in
, out
, err
, env
);
1112 /* An error occurred while trying to run the subprocess. */
1113 report_file_error ("Spawning child process", Qnil
);
1115 #else /* not MSDOS */
1117 /* Spawn the child. (See ntproc.c:Spawnve). */
1118 cpid
= spawnve (_P_NOWAIT
, new_argv
[0], new_argv
, env
);
1119 reset_standard_handles (in
, out
, err
, handles
);
1121 /* An error occurred while trying to spawn the process. */
1122 report_file_error ("Spawning child process", Qnil
);
1124 #else /* not WINDOWSNT */
1125 /* execvp does not accept an environment arg so the only way
1126 to pass this environment is to set environ. Our caller
1127 is responsible for restoring the ambient value of environ. */
1129 execvp (new_argv
[0], new_argv
);
1131 write (1, "Can't exec program: ", 20);
1132 write (1, new_argv
[0], strlen (new_argv
[0]));
1135 #endif /* not WINDOWSNT */
1136 #endif /* not MSDOS */
1139 /* Move the file descriptor FD so that its number is not less than MINFD.
1140 If the file descriptor is moved at all, the original is freed. */
1142 relocate_fd (fd
, minfd
)
1152 char *message1
= "Error while setting up child: ";
1153 char *errmessage
= strerror (errno
);
1154 char *message2
= "\n";
1155 write (2, message1
, strlen (message1
));
1156 write (2, errmessage
, strlen (errmessage
));
1157 write (2, message2
, strlen (message2
));
1160 /* Note that we hold the original FD open while we recurse,
1161 to guarantee we'll get a new FD if we need it. */
1162 new = relocate_fd (new, minfd
);
1169 getenv_internal (var
, varlen
, value
, valuelen
)
1177 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCONS (scan
)->cdr
)
1181 entry
= XCONS (scan
)->car
;
1183 && STRING_BYTES (XSTRING (entry
)) > varlen
1184 && XSTRING (entry
)->data
[varlen
] == '='
1186 /* NT environment variables are case insensitive. */
1187 && ! strnicmp (XSTRING (entry
)->data
, var
, varlen
)
1188 #else /* not WINDOWSNT */
1189 && ! bcmp (XSTRING (entry
)->data
, var
, varlen
)
1190 #endif /* not WINDOWSNT */
1193 *value
= (char *) XSTRING (entry
)->data
+ (varlen
+ 1);
1194 *valuelen
= STRING_BYTES (XSTRING (entry
)) - (varlen
+ 1);
1202 DEFUN ("getenv", Fgetenv
, Sgetenv
, 1, 1, 0,
1203 "Return the value of environment variable VAR, as a string.\n\
1204 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
1205 This function consults the variable ``process-environment'' for its value.")
1212 CHECK_STRING (var
, 0);
1213 if (getenv_internal (XSTRING (var
)->data
, STRING_BYTES (XSTRING (var
)),
1215 return make_string (value
, valuelen
);
1220 /* A version of getenv that consults process_environment, easily
1229 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
1235 #endif /* not VMS */
1237 /* This is run before init_cmdargs. */
1242 char *data_dir
= egetenv ("EMACSDATA");
1243 char *doc_dir
= egetenv ("EMACSDOC");
1246 = Ffile_name_as_directory (build_string (data_dir
? data_dir
1249 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
1252 /* Check the EMACSPATH environment variable, defaulting to the
1253 PATH_EXEC path from paths.h. */
1254 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1255 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
1256 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1259 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1264 char *data_dir
= egetenv ("EMACSDATA");
1267 Lisp_Object tempdir
;
1269 if (!NILP (Vinstallation_directory
))
1271 /* Add to the path the lib-src subdir of the installation dir. */
1273 tem
= Fexpand_file_name (build_string ("lib-src"),
1274 Vinstallation_directory
);
1275 if (NILP (Fmember (tem
, Vexec_path
)))
1278 /* MSDOS uses wrapped binaries, so don't do this. */
1279 Vexec_path
= nconc2 (Vexec_path
, Fcons (tem
, Qnil
));
1280 Vexec_directory
= Ffile_name_as_directory (tem
);
1281 #endif /* not DOS_NT */
1284 /* Maybe use ../etc as well as ../lib-src. */
1287 tem
= Fexpand_file_name (build_string ("etc"),
1288 Vinstallation_directory
);
1289 Vdoc_directory
= Ffile_name_as_directory (tem
);
1293 /* Look for the files that should be in etc. We don't use
1294 Vinstallation_directory, because these files are never installed
1295 near the executable, and they are never in the build
1296 directory when that's different from the source directory.
1298 Instead, if these files are not in the nominal place, we try the
1299 source directory. */
1302 Lisp_Object tem
, tem1
, newdir
;
1304 tem
= Fexpand_file_name (build_string ("GNU"), Vdata_directory
);
1305 tem1
= Ffile_exists_p (tem
);
1308 newdir
= Fexpand_file_name (build_string ("../etc/"),
1309 build_string (PATH_DUMPLOADSEARCH
));
1310 tem
= Fexpand_file_name (build_string ("GNU"), newdir
);
1311 tem1
= Ffile_exists_p (tem
);
1313 Vdata_directory
= newdir
;
1321 tempdir
= Fdirectory_file_name (Vexec_directory
);
1322 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1323 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1327 tempdir
= Fdirectory_file_name (Vdata_directory
);
1328 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1329 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1333 Vshell_file_name
= build_string ("*dcl*");
1335 sh
= (char *) getenv ("SHELL");
1336 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
1340 Vtemp_file_name_pattern
= build_string ("tmp:emacsXXXXXX.");
1342 if (getenv ("TMPDIR"))
1344 char *dir
= getenv ("TMPDIR");
1345 Vtemp_file_name_pattern
1346 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1347 build_string (dir
));
1350 Vtemp_file_name_pattern
= build_string ("/tmp/emacsXXXXXX");
1355 set_process_environment ()
1357 register char **envp
;
1359 Vprocess_environment
= Qnil
;
1363 for (envp
= environ
; *envp
; envp
++)
1364 Vprocess_environment
= Fcons (build_string (*envp
),
1365 Vprocess_environment
);
1372 Qbuffer_file_type
= intern ("buffer-file-type");
1373 staticpro (&Qbuffer_file_type
);
1376 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
1377 "*File name to load inferior shells from.\n\
1378 Initialized from the SHELL environment variable.");
1380 DEFVAR_LISP ("exec-path", &Vexec_path
,
1381 "*List of directories to search programs to run in subprocesses.\n\
1382 Each element is a string (directory name) or nil (try default directory).");
1384 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
1385 "Directory for executables for Emacs to invoke.\n\
1386 More generally, this includes any architecture-dependent files\n\
1387 that are built and installed from the Emacs distribution.");
1389 DEFVAR_LISP ("data-directory", &Vdata_directory
,
1390 "Directory of machine-independent files that come with GNU Emacs.\n\
1391 These are files intended for Emacs to use while it runs.");
1393 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
1394 "Directory containing the DOC file that comes with GNU Emacs.\n\
1395 This is usually the same as data-directory.");
1397 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
1398 "For internal use by the build procedure only.\n\
1399 This is the name of the directory in which the build procedure installed\n\
1400 Emacs's info files; the default value for Info-default-directory-list\n\
1402 Vconfigure_info_directory
= build_string (PATH_INFO
);
1404 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern
,
1405 "Pattern for making names for temporary files.\n\
1406 This is used by `call-process-region'.");
1407 /* This variable is initialized in init_callproc. */
1409 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
1410 "List of environment variables for subprocesses to inherit.\n\
1411 Each element should be a string of the form ENVVARNAME=VALUE.\n\
1412 The environment which Emacs inherits is placed in this variable\n\
1413 when Emacs starts.");
1416 defsubr (&Scall_process
);
1419 defsubr (&Scall_process_region
);