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>
77 #include "syssignal.h"
85 extern noshare
char **environ
;
87 extern char **environ
;
90 #define max(a, b) ((a) > (b) ? (a) : (b))
92 Lisp_Object Vexec_path
, Vexec_directory
, Vdata_directory
, Vdoc_directory
;
93 Lisp_Object Vconfigure_info_directory
;
94 Lisp_Object Vtemp_file_name_pattern
;
96 Lisp_Object Vshell_file_name
;
98 Lisp_Object Vprocess_environment
;
101 Lisp_Object Qbuffer_file_type
;
104 /* True iff we are about to fork off a synchronous process or if we
105 are waiting for it. */
106 int synch_process_alive
;
108 /* Nonzero => this is a string explaining death of synchronous subprocess. */
109 char *synch_process_death
;
111 /* If synch_process_death is zero,
112 this is exit code of synchronous subprocess. */
113 int synch_process_retcode
;
115 extern Lisp_Object Vdoc_file_name
;
117 extern Lisp_Object Vfile_name_coding_system
, Vdefault_file_name_coding_system
;
119 /* Clean up when exiting Fcall_process.
120 On MSDOS, delete the temporary file on any kind of termination.
121 On Unix, kill the process and any children on termination by signal. */
123 /* Nonzero if this is termination due to exit. */
124 static int call_process_exited
;
126 #ifndef VMS /* VMS version is in vmsproc.c. */
129 call_process_kill (fdpid
)
132 close (XFASTINT (Fcar (fdpid
)));
133 EMACS_KILLPG (XFASTINT (Fcdr (fdpid
)), SIGKILL
);
134 synch_process_alive
= 0;
139 call_process_cleanup (fdpid
)
143 /* for MSDOS fdpid is really (fd . tempfile) */
144 register Lisp_Object file
;
146 close (XFASTINT (Fcar (fdpid
)));
147 if (strcmp (XSTRING (file
)-> data
, NULL_DEVICE
) != 0)
148 unlink (XSTRING (file
)->data
);
149 #else /* not MSDOS */
150 register int pid
= XFASTINT (Fcdr (fdpid
));
153 if (call_process_exited
)
155 close (XFASTINT (Fcar (fdpid
)));
159 if (EMACS_KILLPG (pid
, SIGINT
) == 0)
161 int count
= specpdl_ptr
- specpdl
;
162 record_unwind_protect (call_process_kill
, fdpid
);
163 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
166 wait_for_termination (pid
);
168 specpdl_ptr
= specpdl
+ count
; /* Discard the unwind protect. */
169 message1 ("Waiting for process to die...done");
171 synch_process_alive
= 0;
172 close (XFASTINT (Fcar (fdpid
)));
173 #endif /* not MSDOS */
177 DEFUN ("call-process", Fcall_process
, Scall_process
, 1, MANY
, 0,
178 "Call PROGRAM synchronously in separate process.\n\
179 The program's input comes from file INFILE (nil means `/dev/null').\n\
180 Insert output in BUFFER before point; t means current buffer;\n\
181 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
182 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
183 REAL-BUFFER says what to do with standard output, as above,\n\
184 while STDERR-FILE says what to do with standard error in the child.\n\
185 STDERR-FILE may be nil (discard standard error output),\n\
186 t (mix it with ordinary output), or a file name string.\n\
188 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
189 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
191 If BUFFER is 0, `call-process' returns immediately with value nil.\n\
192 Otherwise it waits for PROGRAM to terminate\n\
193 and returns a numeric exit status or a signal description string.\n\
194 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
197 register Lisp_Object
*args
;
199 Lisp_Object infile
, buffer
, current_dir
, display
, path
;
206 int count
= specpdl_ptr
- specpdl
;
208 register unsigned char **new_argv
209 = (unsigned char **) alloca ((max (2, nargs
- 2)) * sizeof (char *));
210 struct buffer
*old
= current_buffer
;
211 /* File to use for stderr in the child.
212 t means use same as standard output. */
213 Lisp_Object error_file
;
214 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
215 char *outf
, *tempfile
;
221 struct coding_system process_coding
; /* coding-system of process output */
222 struct coding_system argument_coding
; /* coding-system of arguments */
224 CHECK_STRING (args
[0], 0);
229 /* Without asynchronous processes we cannot have BUFFER == 0. */
230 if (nargs
>= 3 && INTEGERP (args
[2]))
231 error ("Operating system cannot handle asynchronous subprocesses");
232 #endif /* subprocesses */
234 /* Decide the coding-system for giving arguments and reading process
237 Lisp_Object val
, *args2
;
238 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
239 Lisp_Object coding_systems
= Qt
;
242 /* If arguments are supplied, we may have to encode them. */
247 for (i
= 4; i
< nargs
; i
++)
248 if (STRING_MULTIBYTE (args
[i
]))
251 if (!NILP (Vcoding_system_for_write
))
252 val
= Vcoding_system_for_write
;
253 else if (! must_encode
)
257 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
258 args2
[0] = Qcall_process
;
259 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
260 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
261 if (CONSP (coding_systems
))
262 val
= XCONS (coding_systems
)->cdr
;
263 else if (CONSP (Vdefault_process_coding_system
))
264 val
= XCONS (Vdefault_process_coding_system
)->cdr
;
268 setup_coding_system (Fcheck_coding_system (val
), &argument_coding
);
271 /* If BUFFER is nil, we must read process output once and then
272 discard it, so setup coding system but with nil. If BUFFER is
273 an integer, we can discard it without reading. */
274 if (nargs
< 3 || NILP (args
[2]))
275 setup_coding_system (Qnil
, &process_coding
);
276 else if (!INTEGERP (args
[2]))
279 if (!NILP (Vcoding_system_for_read
))
280 val
= Vcoding_system_for_read
;
281 else if (NILP (current_buffer
->enable_multibyte_characters
))
285 if (EQ (coding_systems
, Qt
))
287 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
288 args2
[0] = Qcall_process
;
289 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
291 = Ffind_operation_coding_system (nargs
+ 1, args2
);
293 if (CONSP (coding_systems
))
294 val
= XCONS (coding_systems
)->car
;
295 else if (CONSP (Vdefault_process_coding_system
))
296 val
= XCONS (Vdefault_process_coding_system
)->car
;
300 setup_coding_system (Fcheck_coding_system (val
), &process_coding
);
304 if (nargs
>= 2 && ! NILP (args
[1]))
306 infile
= Fexpand_file_name (args
[1], current_buffer
->directory
);
307 CHECK_STRING (infile
, 1);
310 infile
= build_string (NULL_DEVICE
);
316 /* If BUFFER is a list, its meaning is
317 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
320 if (CONSP (XCONS (buffer
)->cdr
))
322 Lisp_Object stderr_file
;
323 stderr_file
= XCONS (XCONS (buffer
)->cdr
)->car
;
325 if (NILP (stderr_file
) || EQ (Qt
, stderr_file
))
326 error_file
= stderr_file
;
328 error_file
= Fexpand_file_name (stderr_file
, Qnil
);
331 buffer
= XCONS (buffer
)->car
;
334 if (!(EQ (buffer
, Qnil
)
336 || XFASTINT (buffer
) == 0))
338 Lisp_Object spec_buffer
;
339 spec_buffer
= buffer
;
340 buffer
= Fget_buffer_create (buffer
);
341 /* Mention the buffer name for a better error message. */
343 CHECK_BUFFER (spec_buffer
, 2);
344 CHECK_BUFFER (buffer
, 2);
350 /* Make sure that the child will be able to chdir to the current
351 buffer's current directory, or its unhandled equivalent. We
352 can't just have the child check for an error when it does the
353 chdir, since it's in a vfork.
355 We have to GCPRO around this because Fexpand_file_name,
356 Funhandled_file_name_directory, and Ffile_accessible_directory_p
357 might call a file name handling function. The argument list is
358 protected by the caller, so all we really have to worry about is
361 struct gcpro gcpro1
, gcpro2
, gcpro3
;
363 current_dir
= current_buffer
->directory
;
365 GCPRO3 (infile
, buffer
, current_dir
);
368 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
370 if (NILP (Ffile_accessible_directory_p (current_dir
)))
371 report_file_error ("Setting current directory",
372 Fcons (current_buffer
->directory
, Qnil
));
377 display
= nargs
>= 4 ? args
[3] : Qnil
;
379 filefd
= open (XSTRING (infile
)->data
, O_RDONLY
, 0);
382 report_file_error ("Opening process input file", Fcons (infile
, Qnil
));
384 /* Search for program; barf if not found. */
388 GCPRO1 (current_dir
);
389 openp (Vexec_path
, args
[0], EXEC_SUFFIXES
, &path
, 1);
395 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
397 new_argv
[0] = XSTRING (path
)->data
;
402 for (i
= 4; i
< nargs
; i
++) CHECK_STRING (args
[i
], i
);
404 if (! CODING_REQUIRE_ENCODING (&argument_coding
))
406 for (i
= 4; i
< nargs
; i
++)
407 new_argv
[i
- 3] = XSTRING (args
[i
])->data
;
411 /* We must encode the arguments. */
412 struct gcpro gcpro1
, gcpro2
, gcpro3
;
414 GCPRO3 (infile
, buffer
, current_dir
);
415 for (i
= 4; i
< nargs
; i
++)
417 int size
= encoding_buffer_size (&argument_coding
,
418 STRING_BYTES (XSTRING (args
[i
])));
419 unsigned char *dummy1
= (unsigned char *) alloca (size
);
422 /* The Irix 4.0 compiler barfs if we eliminate dummy. */
423 new_argv
[i
- 3] = dummy1
;
424 encode_coding (&argument_coding
,
425 XSTRING (args
[i
])->data
,
427 STRING_BYTES (XSTRING (args
[i
])),
429 new_argv
[i
- 3][argument_coding
.produced
] = 0;
433 new_argv
[nargs
- 3] = 0;
438 #ifdef MSDOS /* MW, July 1993 */
439 if ((outf
= egetenv ("TMP")) || (outf
= egetenv ("TEMP")))
440 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
443 tempfile
= alloca (20);
446 dostounix_filename (tempfile
);
447 if (*tempfile
== '\0' || tempfile
[strlen (tempfile
) - 1] != '/')
448 strcat (tempfile
, "/");
449 strcat (tempfile
, "detmp.XXX");
452 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
456 report_file_error ("Opening process output file",
457 Fcons (build_string (tempfile
), Qnil
));
463 if (INTEGERP (buffer
))
464 fd
[1] = open (NULL_DEVICE
, O_WRONLY
), fd
[0] = -1;
471 /* Replaced by close_process_descs */
472 set_exclusive_use (fd
[0]);
477 /* child_setup must clobber environ in systems with true vfork.
478 Protect it from permanent change. */
479 register char **save_environ
= environ
;
480 register int fd1
= fd
[1];
483 #if 0 /* Some systems don't have sigblock. */
484 mask
= sigblock (sigmask (SIGCHLD
));
487 /* Record that we're about to create a synchronous process. */
488 synch_process_alive
= 1;
490 /* These vars record information from process termination.
491 Clear them now before process can possibly terminate,
492 to avoid timing error if process terminates soon. */
493 synch_process_death
= 0;
494 synch_process_retcode
= 0;
496 if (NILP (error_file
))
497 fd_error
= open (NULL_DEVICE
, O_WRONLY
);
498 else if (STRINGP (error_file
))
501 fd_error
= open (XSTRING (error_file
)->data
,
502 O_WRONLY
| O_TRUNC
| O_CREAT
| O_TEXT
,
504 #else /* not DOS_NT */
505 fd_error
= creat (XSTRING (error_file
)->data
, 0666);
506 #endif /* not DOS_NT */
519 report_file_error ("Cannot redirect stderr",
520 Fcons ((NILP (error_file
)
521 ? build_string (NULL_DEVICE
) : error_file
),
525 current_dir
= ENCODE_FILE (current_dir
);
527 #ifdef MSDOS /* MW, July 1993 */
528 /* Note that on MSDOS `child_setup' actually returns the child process
529 exit status, not its PID, so we assign it to `synch_process_retcode'
531 pid
= child_setup (filefd
, outfilefd
, fd_error
, (char **) new_argv
,
534 /* Record that the synchronous process exited and note its
535 termination status. */
536 synch_process_alive
= 0;
537 synch_process_retcode
= pid
;
538 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
539 synch_process_death
= strerror (errno
);
542 if (fd_error
!= outfilefd
)
544 fd1
= -1; /* No harm in closing that one! */
545 /* Since CRLF is converted to LF within `decode_coding', we can
546 always open a file with binary mode. */
547 fd
[0] = open (tempfile
, O_BINARY
);
552 report_file_error ("Cannot re-open temporary file", Qnil
);
554 #else /* not MSDOS */
556 pid
= child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
558 #else /* not WINDOWSNT */
568 #if defined (USG) && !defined (BSD_PGRPS)
573 child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
576 #endif /* not WINDOWSNT */
578 /* The MSDOS case did this already. */
581 #endif /* not MSDOS */
583 environ
= save_environ
;
585 /* Close most of our fd's, but not fd[0]
586 since we will use that to read input from. */
588 if (fd1
>= 0 && fd1
!= fd_error
)
596 report_file_error ("Doing vfork", Qnil
);
599 if (INTEGERP (buffer
))
604 /* If Emacs has been built with asynchronous subprocess support,
605 we don't need to do this, I think because it will then have
606 the facilities for handling SIGCHLD. */
607 wait_without_blocking ();
608 #endif /* subprocesses */
612 /* Enable sending signal if user quits below. */
613 call_process_exited
= 0;
616 /* MSDOS needs different cleanup information. */
617 record_unwind_protect (call_process_cleanup
,
618 Fcons (make_number (fd
[0]), build_string (tempfile
)));
620 record_unwind_protect (call_process_cleanup
,
621 Fcons (make_number (fd
[0]), make_number (pid
)));
622 #endif /* not MSDOS */
625 if (BUFFERP (buffer
))
626 Fset_buffer (buffer
);
636 int display_on_the_fly
= !NILP (display
) && INTERACTIVE
;
637 struct coding_system saved_coding
= process_coding
;
641 /* Repeatedly read until we've filled as much as possible
642 of the buffer size we have. But don't read
643 less than 1024--save that for the next bufferful. */
645 while (nread
< bufsize
- 1024)
647 int this_read
= read (fd
[0], bufptr
+ nread
, bufsize
- nread
);
654 process_coding
.mode
|= CODING_MODE_LAST_BLOCK
;
659 total_read
+= this_read
;
661 if (display_on_the_fly
)
665 /* Now NREAD is the total amount of data in the buffer. */
670 if (process_coding
.type
== coding_type_no_conversion
)
671 insert (bufptr
, nread
);
673 { /* We have to decode the input. */
674 int size
= decoding_buffer_size (&process_coding
, nread
);
675 char *decoding_buf
= get_conversion_buffer (size
);
677 decode_coding (&process_coding
, bufptr
, decoding_buf
,
679 if (display_on_the_fly
680 && saved_coding
.type
== coding_type_undecided
681 && process_coding
.type
!= coding_type_undecided
)
683 /* We have detected some coding system. But,
684 there's a possibility that the detection was
685 done by insufficient data. So, we give up
686 displaying on the fly. */
687 display_on_the_fly
= 0;
688 process_coding
= saved_coding
;
692 if (process_coding
.produced
> 0)
693 insert (decoding_buf
, process_coding
.produced
);
694 carryover
= nread
- process_coding
.consumed
;
697 /* As CARRYOVER should not be that large, we had
698 better avoid overhead of bcopy. */
699 char *p
= bufptr
+ process_coding
.consumed
;
700 char *pend
= p
+ carryover
;
703 while (p
< pend
) *dst
++ = *p
++;
707 if (process_coding
.mode
& CODING_MODE_LAST_BLOCK
)
710 insert (bufptr
, carryover
);
714 /* Make the buffer bigger as we continue to read more data,
716 if (bufsize
< 64 * 1024 && total_read
> 32 * bufsize
)
719 bufptr
= (char *) alloca (bufsize
);
722 if (!NILP (display
) && INTERACTIVE
)
725 prepare_menu_bars ();
727 redisplay_preserve_echo_area ();
734 Vlast_coding_system_used
= process_coding
.symbol
;
736 /* If the caller required, let the buffer inherit the
737 coding-system used to decode the process output. */
738 if (inherit_process_coding_system
)
739 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
740 make_number (total_read
));
743 /* Wait for it to terminate, unless it already has. */
744 wait_for_termination (pid
);
748 set_buffer_internal (old
);
750 /* Don't kill any children that the subprocess may have left behind
752 call_process_exited
= 1;
754 unbind_to (count
, Qnil
);
756 if (synch_process_death
)
757 return build_string (synch_process_death
);
758 return make_number (synch_process_retcode
);
763 delete_temp_file (name
)
766 /* Use Fdelete_file (indirectly) because that runs a file name handler.
767 We did that when writing the file, so we should do so when deleting. */
768 internal_delete_file (name
);
771 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
773 "Send text from START to END to a synchronous process running PROGRAM.\n\
774 Delete the text if fourth arg DELETE is non-nil.\n\
776 Insert output in BUFFER before point; t means current buffer;\n\
777 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
778 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
779 REAL-BUFFER says what to do with standard output, as above,\n\
780 while STDERR-FILE says what to do with standard error in the child.\n\
781 STDERR-FILE may be nil (discard standard error output),\n\
782 t (mix it with ordinary output), or a file name string.\n\
784 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
785 Remaining args are passed to PROGRAM at startup as command args.\n\
787 If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
788 Otherwise it waits for PROGRAM to terminate\n\
789 and returns a numeric exit status or a signal description string.\n\
790 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
793 register Lisp_Object
*args
;
796 Lisp_Object filename_string
;
797 register Lisp_Object start
, end
;
798 int count
= specpdl_ptr
- specpdl
;
799 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
800 Lisp_Object coding_systems
= Qt
;
801 Lisp_Object val
, *args2
;
807 if ((outf
= egetenv ("TMP")) || (outf
= egetenv ("TEMP")))
808 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
811 tempfile
= alloca (20);
814 if (!IS_DIRECTORY_SEP (tempfile
[strlen (tempfile
) - 1]))
815 strcat (tempfile
, "/");
816 if ('/' == DIRECTORY_SEP
)
817 dostounix_filename (tempfile
);
819 unixtodos_filename (tempfile
);
821 strcat (tempfile
, "emXXXXXX");
823 strcat (tempfile
, "detmp.XXX");
825 #else /* not DOS_NT */
826 char *tempfile
= (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
827 bcopy (XSTRING (Vtemp_file_name_pattern
)->data
, tempfile
,
828 STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
829 #endif /* not DOS_NT */
833 filename_string
= build_string (tempfile
);
834 GCPRO1 (filename_string
);
837 /* Decide coding-system of the contents of the temporary file. */
838 if (!NILP (Vcoding_system_for_write
))
839 val
= Vcoding_system_for_write
;
840 else if (NILP (current_buffer
->enable_multibyte_characters
))
844 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
845 args2
[0] = Qcall_process_region
;
846 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
847 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
848 if (CONSP (coding_systems
))
849 val
= XCONS (coding_systems
)->cdr
;
850 else if (CONSP (Vdefault_process_coding_system
))
851 val
= XCONS (Vdefault_process_coding_system
)->cdr
;
857 int count1
= specpdl_ptr
- specpdl
;
859 specbind (intern ("coding-system-for-write"), val
);
860 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
, Qnil
, Qnil
);
862 unbind_to (count1
, 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 static int relocate_fd ();
882 /* This is the last thing run in a newly forked inferior
883 either synchronous or asynchronous.
884 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
885 Initialize inferior's priority, pgrp, connected dir and environment.
886 then exec another program based on new_argv.
888 This function may change environ for the superior process.
889 Therefore, the superior process must save and restore the value
890 of environ around the vfork and the call to this function.
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. */
901 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
903 register char **new_argv
;
905 Lisp_Object current_dir
;
912 #endif /* WINDOWSNT */
916 #ifdef SET_EMACS_PRIORITY
918 extern int emacs_priority
;
920 if (emacs_priority
< 0)
921 nice (- emacs_priority
);
926 /* Close Emacs's descriptors that this process should not have. */
927 close_process_descs ();
929 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
930 we will lose if we call close_load_descs here. */
935 /* Note that use of alloca is always safe here. It's obvious for systems
936 that do not have true vfork or that have true (stack) alloca.
937 If using vfork and C_ALLOCA it is safe because that changes
938 the superior's static variables as if the superior had done alloca
939 and will be cleaned up in the usual way. */
944 i
= STRING_BYTES (XSTRING (current_dir
));
945 pwd_var
= (char *) alloca (i
+ 6);
947 bcopy ("PWD=", pwd_var
, 4);
948 bcopy (XSTRING (current_dir
)->data
, temp
, i
);
949 if (!IS_DIRECTORY_SEP (temp
[i
- 1])) temp
[i
++] = DIRECTORY_SEP
;
953 /* We can't signal an Elisp error here; we're in a vfork. Since
954 the callers check the current directory before forking, this
955 should only return an error if the directory's permissions
956 are changed between the check and this chdir, but we should
958 if (chdir (temp
) < 0)
963 /* Get past the drive letter, so that d:/ is left alone. */
964 if (i
> 2 && IS_DEVICE_SEP (temp
[1]) && IS_DIRECTORY_SEP (temp
[2]))
971 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
972 while (i
> 2 && IS_DIRECTORY_SEP (temp
[i
- 1]))
976 /* Set `env' to a vector of the strings in Vprocess_environment. */
978 register Lisp_Object tem
;
979 register char **new_env
;
980 register int new_length
;
983 for (tem
= Vprocess_environment
;
984 CONSP (tem
) && STRINGP (XCONS (tem
)->car
);
985 tem
= XCONS (tem
)->cdr
)
988 /* new_length + 2 to include PWD and terminating 0. */
989 env
= new_env
= (char **) alloca ((new_length
+ 2) * sizeof (char *));
991 /* If we have a PWD envvar, pass one down,
992 but with corrected value. */
994 *new_env
++ = pwd_var
;
996 /* Copy the Vprocess_environment strings into new_env. */
997 for (tem
= Vprocess_environment
;
998 CONSP (tem
) && STRINGP (XCONS (tem
)->car
);
999 tem
= XCONS (tem
)->cdr
)
1002 char *string
= (char *) XSTRING (XCONS (tem
)->car
)->data
;
1003 /* See if this string duplicates any string already in the env.
1004 If so, don't put it in.
1005 When an env var has multiple definitions,
1006 we keep the definition that comes first in process-environment. */
1007 for (; ep
!= new_env
; ep
++)
1009 char *p
= *ep
, *q
= string
;
1013 /* The string is malformed; might as well drop it. */
1022 *new_env
++ = string
;
1028 prepare_standard_handles (in
, out
, err
, handles
);
1029 set_process_dir (XSTRING (current_dir
)->data
);
1030 #else /* not WINDOWSNT */
1031 /* Make sure that in, out, and err are not actually already in
1032 descriptors zero, one, or two; this could happen if Emacs is
1033 started with its standard in, out, or error closed, as might
1036 int oin
= in
, oout
= out
;
1038 /* We have to avoid relocating the same descriptor twice! */
1040 in
= relocate_fd (in
, 3);
1045 out
= relocate_fd (out
, 3);
1049 else if (err
== oout
)
1052 err
= relocate_fd (err
, 3);
1066 #endif /* not MSDOS */
1067 #endif /* not WINDOWSNT */
1069 #if defined(USG) && !defined(BSD_PGRPS)
1070 #ifndef SETPGRP_RELEASES_CTTY
1071 setpgrp (); /* No arguments but equivalent in this case */
1076 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1077 EMACS_SET_TTY_PGRP (0, &pid
);
1080 something missing here
;
1084 pid
= run_msdos_command (new_argv
, pwd_var
+ 4, in
, out
, err
, env
);
1086 /* An error occurred while trying to run the subprocess. */
1087 report_file_error ("Spawning child process", Qnil
);
1089 #else /* not MSDOS */
1091 /* Spawn the child. (See ntproc.c:Spawnve). */
1092 cpid
= spawnve (_P_NOWAIT
, new_argv
[0], new_argv
, env
);
1093 reset_standard_handles (in
, out
, err
, handles
);
1095 /* An error occurred while trying to spawn the process. */
1096 report_file_error ("Spawning child process", Qnil
);
1098 #else /* not WINDOWSNT */
1099 /* execvp does not accept an environment arg so the only way
1100 to pass this environment is to set environ. Our caller
1101 is responsible for restoring the ambient value of environ. */
1103 execvp (new_argv
[0], new_argv
);
1105 write (1, "Can't exec program: ", 20);
1106 write (1, new_argv
[0], strlen (new_argv
[0]));
1109 #endif /* not WINDOWSNT */
1110 #endif /* not MSDOS */
1113 /* Move the file descriptor FD so that its number is not less than MINFD.
1114 If the file descriptor is moved at all, the original is freed. */
1116 relocate_fd (fd
, minfd
)
1126 char *message1
= "Error while setting up child: ";
1127 char *errmessage
= strerror (errno
);
1128 char *message2
= "\n";
1129 write (2, message1
, strlen (message1
));
1130 write (2, errmessage
, strlen (errmessage
));
1131 write (2, message2
, strlen (message2
));
1134 /* Note that we hold the original FD open while we recurse,
1135 to guarantee we'll get a new FD if we need it. */
1136 new = relocate_fd (new, minfd
);
1143 getenv_internal (var
, varlen
, value
, valuelen
)
1151 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCONS (scan
)->cdr
)
1155 entry
= XCONS (scan
)->car
;
1157 && STRING_BYTES (XSTRING (entry
)) > varlen
1158 && XSTRING (entry
)->data
[varlen
] == '='
1160 /* NT environment variables are case insensitive. */
1161 && ! strnicmp (XSTRING (entry
)->data
, var
, varlen
)
1162 #else /* not WINDOWSNT */
1163 && ! bcmp (XSTRING (entry
)->data
, var
, varlen
)
1164 #endif /* not WINDOWSNT */
1167 *value
= (char *) XSTRING (entry
)->data
+ (varlen
+ 1);
1168 *valuelen
= STRING_BYTES (XSTRING (entry
)) - (varlen
+ 1);
1176 DEFUN ("getenv", Fgetenv
, Sgetenv
, 1, 1, 0,
1177 "Return the value of environment variable VAR, as a string.\n\
1178 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
1179 This function consults the variable ``process-environment'' for its value.")
1186 CHECK_STRING (var
, 0);
1187 if (getenv_internal (XSTRING (var
)->data
, STRING_BYTES (XSTRING (var
)),
1189 return make_string (value
, valuelen
);
1194 /* A version of getenv that consults process_environment, easily
1203 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
1209 #endif /* not VMS */
1211 /* This is run before init_cmdargs. */
1216 char *data_dir
= egetenv ("EMACSDATA");
1217 char *doc_dir
= egetenv ("EMACSDOC");
1220 = Ffile_name_as_directory (build_string (data_dir
? data_dir
1223 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
1226 /* Check the EMACSPATH environment variable, defaulting to the
1227 PATH_EXEC path from paths.h. */
1228 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1229 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
1230 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1233 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1238 char *data_dir
= egetenv ("EMACSDATA");
1241 Lisp_Object tempdir
;
1243 if (initialized
&& !NILP (Vinstallation_directory
))
1245 /* Add to the path the lib-src subdir of the installation dir. */
1247 tem
= Fexpand_file_name (build_string ("lib-src"),
1248 Vinstallation_directory
);
1249 if (NILP (Fmember (tem
, Vexec_path
)))
1252 /* MSDOS uses wrapped binaries, so don't do this. */
1253 Vexec_path
= nconc2 (Vexec_path
, Fcons (tem
, Qnil
));
1254 Vexec_directory
= Ffile_name_as_directory (tem
);
1255 #endif /* not DOS_NT */
1258 /* Maybe use ../etc as well as ../lib-src. */
1261 tem
= Fexpand_file_name (build_string ("etc"),
1262 Vinstallation_directory
);
1263 Vdoc_directory
= Ffile_name_as_directory (tem
);
1267 /* Look for the files that should be in etc. We don't use
1268 Vinstallation_directory, because these files are never installed
1269 near the executable, and they are never in the build
1270 directory when that's different from the source directory.
1272 Instead, if these files are not in the nominal place, we try the
1273 source directory. */
1276 Lisp_Object tem
, tem1
, newdir
;
1278 tem
= Fexpand_file_name (build_string ("GNU"), Vdata_directory
);
1279 tem1
= Ffile_exists_p (tem
);
1282 newdir
= Fexpand_file_name (build_string ("../etc/"),
1283 build_string (PATH_DUMPLOADSEARCH
));
1284 tem
= Fexpand_file_name (build_string ("GNU"), newdir
);
1285 tem1
= Ffile_exists_p (tem
);
1287 Vdata_directory
= newdir
;
1295 tempdir
= Fdirectory_file_name (Vexec_directory
);
1296 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1297 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1301 tempdir
= Fdirectory_file_name (Vdata_directory
);
1302 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1303 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1307 Vshell_file_name
= build_string ("*dcl*");
1309 sh
= (char *) getenv ("SHELL");
1310 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
1314 Vtemp_file_name_pattern
= build_string ("tmp:emacsXXXXXX.");
1316 if (getenv ("TMPDIR"))
1318 char *dir
= getenv ("TMPDIR");
1319 Vtemp_file_name_pattern
1320 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1321 build_string (dir
));
1324 Vtemp_file_name_pattern
= build_string ("/tmp/emacsXXXXXX");
1329 set_process_environment ()
1331 register char **envp
;
1333 Vprocess_environment
= Qnil
;
1337 for (envp
= environ
; *envp
; envp
++)
1338 Vprocess_environment
= Fcons (build_string (*envp
),
1339 Vprocess_environment
);
1346 Qbuffer_file_type
= intern ("buffer-file-type");
1347 staticpro (&Qbuffer_file_type
);
1350 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
1351 "*File name to load inferior shells from.\n\
1352 Initialized from the SHELL environment variable.");
1354 DEFVAR_LISP ("exec-path", &Vexec_path
,
1355 "*List of directories to search programs to run in subprocesses.\n\
1356 Each element is a string (directory name) or nil (try default directory).");
1358 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
1359 "Directory for executables for Emacs to invoke.\n\
1360 More generally, this includes any architecture-dependent files\n\
1361 that are built and installed from the Emacs distribution.");
1363 DEFVAR_LISP ("data-directory", &Vdata_directory
,
1364 "Directory of machine-independent files that come with GNU Emacs.\n\
1365 These are files intended for Emacs to use while it runs.");
1367 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
1368 "Directory containing the DOC file that comes with GNU Emacs.\n\
1369 This is usually the same as data-directory.");
1371 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
1372 "For internal use by the build procedure only.\n\
1373 This is the name of the directory in which the build procedure installed\n\
1374 Emacs's info files; the default value for Info-default-directory-list\n\
1376 Vconfigure_info_directory
= build_string (PATH_INFO
);
1378 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern
,
1379 "Pattern for making names for temporary files.\n\
1380 This is used by `call-process-region'.");
1381 /* This variable is initialized in init_callproc. */
1383 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
1384 "List of environment variables for subprocesses to inherit.\n\
1385 Each element should be a string of the form ENVVARNAME=VALUE.\n\
1386 The environment which Emacs inherits is placed in this variable\n\
1387 when Emacs starts.");
1390 defsubr (&Scall_process
);
1393 defsubr (&Scall_process_region
);