1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985, 86,87,88,93,94,95, 1999 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 /* Define SIGCHLD as an alias for SIGCLD. */
31 #if !defined (SIGCHLD) && defined (SIGCLD)
32 #define SIGCHLD SIGCLD
35 #include <sys/types.h>
43 #define INCLUDED_FCNTL
50 #include <stdlib.h> /* for proper declaration of environ */
53 #define _P_NOWAIT 1 /* from process.h */
56 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
57 #define INCLUDED_FCNTL
60 #include <sys/param.h>
80 #include "syssignal.h"
88 extern noshare
char **environ
;
90 extern char **environ
;
93 #define max(a, b) ((a) > (b) ? (a) : (b))
95 Lisp_Object Vexec_path
, Vexec_directory
, Vdata_directory
, Vdoc_directory
;
96 Lisp_Object Vconfigure_info_directory
;
97 Lisp_Object Vtemp_file_name_pattern
;
99 Lisp_Object Vshell_file_name
;
101 Lisp_Object Vprocess_environment
;
104 Lisp_Object Qbuffer_file_type
;
107 /* True iff we are about to fork off a synchronous process or if we
108 are waiting for it. */
109 int synch_process_alive
;
111 /* Nonzero => this is a string explaining death of synchronous subprocess. */
112 char *synch_process_death
;
114 /* If synch_process_death is zero,
115 this is exit code of synchronous subprocess. */
116 int synch_process_retcode
;
118 extern Lisp_Object Vdoc_file_name
;
120 extern Lisp_Object Vfile_name_coding_system
, Vdefault_file_name_coding_system
;
122 /* Clean up when exiting Fcall_process.
123 On MSDOS, delete the temporary file on any kind of termination.
124 On Unix, kill the process and any children on termination by signal. */
126 /* Nonzero if this is termination due to exit. */
127 static int call_process_exited
;
129 #ifndef VMS /* VMS version is in vmsproc.c. */
132 call_process_kill (fdpid
)
135 emacs_close (XFASTINT (Fcar (fdpid
)));
136 EMACS_KILLPG (XFASTINT (Fcdr (fdpid
)), SIGKILL
);
137 synch_process_alive
= 0;
142 call_process_cleanup (fdpid
)
145 #if defined (MSDOS) || defined (macintosh)
146 /* for MSDOS fdpid is really (fd . tempfile) */
147 register Lisp_Object file
;
149 emacs_close (XFASTINT (Fcar (fdpid
)));
150 if (strcmp (XSTRING (file
)-> data
, NULL_DEVICE
) != 0)
151 unlink (XSTRING (file
)->data
);
152 #else /* not MSDOS and not macintosh */
153 register int pid
= XFASTINT (Fcdr (fdpid
));
155 if (call_process_exited
)
157 emacs_close (XFASTINT (Fcar (fdpid
)));
161 if (EMACS_KILLPG (pid
, SIGINT
) == 0)
163 int count
= specpdl_ptr
- specpdl
;
164 record_unwind_protect (call_process_kill
, fdpid
);
165 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
168 wait_for_termination (pid
);
170 specpdl_ptr
= specpdl
+ count
; /* Discard the unwind protect. */
171 message1 ("Waiting for process to die...done");
173 synch_process_alive
= 0;
174 emacs_close (XFASTINT (Fcar (fdpid
)));
175 #endif /* not MSDOS */
179 DEFUN ("call-process", Fcall_process
, Scall_process
, 1, MANY
, 0,
180 "Call PROGRAM synchronously in separate process.\n\
181 The remaining arguments are optional.\n\
182 The program's input comes from file INFILE (nil means `/dev/null').\n\
183 Insert output in BUFFER before point; t means current buffer;\n\
184 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
185 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
186 REAL-BUFFER says what to do with standard output, as above,\n\
187 while STDERR-FILE says what to do with standard error in the child.\n\
188 STDERR-FILE may be nil (discard standard error output),\n\
189 t (mix it with ordinary output), or a file name string.\n\
191 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
192 Remaining arguments are strings passed as command arguments to PROGRAM.\n\
194 If BUFFER is 0, `call-process' returns immediately with value nil.\n\
195 Otherwise it waits for PROGRAM to terminate\n\
196 and returns a numeric exit status or a signal description string.\n\
197 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
200 register Lisp_Object
*args
;
202 Lisp_Object infile
, buffer
, current_dir
, display
, path
;
209 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
;
228 struct coding_system process_coding
; /* coding-system of process output */
229 struct coding_system argument_coding
; /* coding-system of arguments */
230 /* Set to the return value of Ffind_operation_coding_system. */
231 Lisp_Object coding_systems
;
233 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
236 CHECK_STRING (args
[0], 0);
241 /* Without asynchronous processes we cannot have BUFFER == 0. */
243 && (INTEGERP (CONSP (args
[2]) ? XCAR (args
[2]) : args
[2])))
244 error ("Operating system cannot handle asynchronous subprocesses");
245 #endif /* subprocesses */
247 /* Decide the coding-system for giving arguments. */
249 Lisp_Object val
, *args2
;
252 /* If arguments are supplied, we may have to encode them. */
257 for (i
= 4; i
< nargs
; i
++)
258 CHECK_STRING (args
[i
], i
);
260 for (i
= 4; i
< nargs
; i
++)
261 if (STRING_MULTIBYTE (args
[i
]))
264 if (!NILP (Vcoding_system_for_write
))
265 val
= Vcoding_system_for_write
;
266 else if (! must_encode
)
270 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
271 args2
[0] = Qcall_process
;
272 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
273 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
274 if (CONSP (coding_systems
))
275 val
= XCDR (coding_systems
);
276 else if (CONSP (Vdefault_process_coding_system
))
277 val
= XCDR (Vdefault_process_coding_system
);
281 setup_coding_system (Fcheck_coding_system (val
), &argument_coding
);
285 if (nargs
>= 2 && ! NILP (args
[1]))
287 infile
= Fexpand_file_name (args
[1], current_buffer
->directory
);
288 CHECK_STRING (infile
, 1);
291 infile
= build_string (NULL_DEVICE
);
297 /* If BUFFER is a list, its meaning is
298 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
301 if (CONSP (XCDR (buffer
)))
303 Lisp_Object stderr_file
;
304 stderr_file
= XCAR (XCDR (buffer
));
306 if (NILP (stderr_file
) || EQ (Qt
, stderr_file
))
307 error_file
= stderr_file
;
309 error_file
= Fexpand_file_name (stderr_file
, Qnil
);
312 buffer
= XCAR (buffer
);
315 if (!(EQ (buffer
, Qnil
)
317 || INTEGERP (buffer
)))
319 Lisp_Object spec_buffer
;
320 spec_buffer
= buffer
;
321 buffer
= Fget_buffer_create (buffer
);
322 /* Mention the buffer name for a better error message. */
324 CHECK_BUFFER (spec_buffer
, 2);
325 CHECK_BUFFER (buffer
, 2);
331 /* Make sure that the child will be able to chdir to the current
332 buffer's current directory, or its unhandled equivalent. We
333 can't just have the child check for an error when it does the
334 chdir, since it's in a vfork.
336 We have to GCPRO around this because Fexpand_file_name,
337 Funhandled_file_name_directory, and Ffile_accessible_directory_p
338 might call a file name handling function. The argument list is
339 protected by the caller, so all we really have to worry about is
342 struct gcpro gcpro1
, gcpro2
, gcpro3
;
344 current_dir
= current_buffer
->directory
;
346 GCPRO3 (infile
, buffer
, current_dir
);
349 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
351 if (NILP (Ffile_accessible_directory_p (current_dir
)))
352 report_file_error ("Setting current directory",
353 Fcons (current_buffer
->directory
, Qnil
));
358 display
= nargs
>= 4 ? args
[3] : Qnil
;
360 filefd
= emacs_open (XSTRING (infile
)->data
, O_RDONLY
, 0);
363 report_file_error ("Opening process input file", Fcons (infile
, Qnil
));
365 /* Search for program; barf if not found. */
369 GCPRO1 (current_dir
);
370 openp (Vexec_path
, args
[0], EXEC_SUFFIXES
, &path
, 1);
375 emacs_close (filefd
);
376 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
378 new_argv
[0] = XSTRING (path
)->data
;
383 if (! CODING_REQUIRE_ENCODING (&argument_coding
))
385 for (i
= 4; i
< nargs
; i
++)
386 new_argv
[i
- 3] = XSTRING (args
[i
])->data
;
390 /* We must encode the arguments. */
391 struct gcpro gcpro1
, gcpro2
, gcpro3
;
393 GCPRO3 (infile
, buffer
, current_dir
);
394 for (i
= 4; i
< nargs
; i
++)
396 int size
= encoding_buffer_size (&argument_coding
,
397 STRING_BYTES (XSTRING (args
[i
])));
398 unsigned char *dummy1
= (unsigned char *) alloca (size
);
400 /* The Irix 4.0 compiler barfs if we eliminate dummy. */
401 new_argv
[i
- 3] = dummy1
;
402 argument_coding
.mode
|= CODING_MODE_LAST_BLOCK
;
403 encode_coding (&argument_coding
,
404 XSTRING (args
[i
])->data
,
406 STRING_BYTES (XSTRING (args
[i
])),
408 new_argv
[i
- 3][argument_coding
.produced
] = 0;
409 /* We have to initialize CCL program status again. */
410 if (argument_coding
.type
== coding_type_ccl
)
411 setup_ccl_program (&(argument_coding
.spec
.ccl
.encoder
), Qnil
);
415 new_argv
[nargs
- 3] = 0;
420 #ifdef MSDOS /* MW, July 1993 */
421 if ((outf
= egetenv ("TMPDIR")))
422 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
425 tempfile
= alloca (20);
428 dostounix_filename (tempfile
);
429 if (*tempfile
== '\0' || tempfile
[strlen (tempfile
) - 1] != '/')
430 strcat (tempfile
, "/");
431 strcat (tempfile
, "detmp.XXX");
434 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
437 emacs_close (filefd
);
438 report_file_error ("Opening process output file",
439 Fcons (build_string (tempfile
), Qnil
));
446 /* Since we don't have pipes on the Mac, create a temporary file to
447 hold the output of the subprocess. */
448 tempfile
= (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
449 bcopy (XSTRING (Vtemp_file_name_pattern
)->data
, tempfile
,
450 STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
454 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
458 report_file_error ("Opening process output file",
459 Fcons (build_string (tempfile
), Qnil
));
463 #endif /* macintosh */
465 if (INTEGERP (buffer
))
466 fd
[1] = emacs_open (NULL_DEVICE
, O_WRONLY
, 0), fd
[0] = -1;
475 /* Replaced by close_process_descs */
476 set_exclusive_use (fd
[0]);
481 /* child_setup must clobber environ in systems with true vfork.
482 Protect it from permanent change. */
483 register char **save_environ
= environ
;
484 register int fd1
= fd
[1];
487 #if 0 /* Some systems don't have sigblock. */
488 mask
= sigblock (sigmask (SIGCHLD
));
491 /* Record that we're about to create a synchronous process. */
492 synch_process_alive
= 1;
494 /* These vars record information from process termination.
495 Clear them now before process can possibly terminate,
496 to avoid timing error if process terminates soon. */
497 synch_process_death
= 0;
498 synch_process_retcode
= 0;
500 if (NILP (error_file
))
501 fd_error
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
502 else if (STRINGP (error_file
))
505 fd_error
= emacs_open (XSTRING (error_file
)->data
,
506 O_WRONLY
| O_TRUNC
| O_CREAT
| O_TEXT
,
508 #else /* not DOS_NT */
509 fd_error
= creat (XSTRING (error_file
)->data
, 0666);
510 #endif /* not DOS_NT */
515 emacs_close (filefd
);
523 report_file_error ("Cannot redirect stderr",
524 Fcons ((NILP (error_file
)
525 ? build_string (NULL_DEVICE
) : error_file
),
529 current_dir
= ENCODE_FILE (current_dir
);
533 /* Call run_mac_command in sysdep.c here directly instead of doing
534 a child_setup as for MSDOS and other platforms. Note that this
535 code does not handle passing the environment to the synchronous
537 char *infn
, *outfn
, *errfn
, *currdn
;
539 /* close these files so subprocess can write to them */
541 if (fd_error
!= outfilefd
)
543 fd1
= -1; /* No harm in closing that one! */
545 infn
= XSTRING (infile
)->data
;
547 if (NILP (error_file
))
549 else if (EQ (Qt
, error_file
))
552 errfn
= XSTRING (error_file
)->data
;
553 currdn
= XSTRING (current_dir
)->data
;
554 pid
= run_mac_command (new_argv
, currdn
, infn
, outfn
, errfn
);
556 /* Record that the synchronous process exited and note its
557 termination status. */
558 synch_process_alive
= 0;
559 synch_process_retcode
= pid
;
560 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
562 synchronize_system_messages_locale ();
563 synch_process_death
= strerror (errno
);
566 /* Since CRLF is converted to LF within `decode_coding', we can
567 always open a file with binary mode. */
568 fd
[0] = open (tempfile
, O_BINARY
);
573 report_file_error ("Cannot re-open temporary file", Qnil
);
576 #else /* not macintosh */
577 #ifdef MSDOS /* MW, July 1993 */
578 /* Note that on MSDOS `child_setup' actually returns the child process
579 exit status, not its PID, so we assign it to `synch_process_retcode'
581 pid
= child_setup (filefd
, outfilefd
, fd_error
, (char **) new_argv
,
584 /* Record that the synchronous process exited and note its
585 termination status. */
586 synch_process_alive
= 0;
587 synch_process_retcode
= pid
;
588 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
590 synchronize_system_messages_locale ();
591 synch_process_death
= strerror (errno
);
594 emacs_close (outfilefd
);
595 if (fd_error
!= outfilefd
)
596 emacs_close (fd_error
);
597 fd1
= -1; /* No harm in closing that one! */
598 /* Since CRLF is converted to LF within `decode_coding', we can
599 always open a file with binary mode. */
600 fd
[0] = emacs_open (tempfile
, O_RDONLY
| O_BINARY
, 0);
604 emacs_close (filefd
);
605 report_file_error ("Cannot re-open temporary file", Qnil
);
607 #else /* not MSDOS */
609 pid
= child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
611 #else /* not WINDOWSNT */
621 #if defined (USG) && !defined (BSD_PGRPS)
626 child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
629 #endif /* not WINDOWSNT */
631 /* The MSDOS case did this already. */
633 emacs_close (fd_error
);
634 #endif /* not MSDOS */
635 #endif /* not macintosh */
637 environ
= save_environ
;
639 /* Close most of our fd's, but not fd[0]
640 since we will use that to read input from. */
641 emacs_close (filefd
);
642 if (fd1
>= 0 && fd1
!= fd_error
)
650 report_file_error ("Doing vfork", Qnil
);
653 if (INTEGERP (buffer
))
658 /* If Emacs has been built with asynchronous subprocess support,
659 we don't need to do this, I think because it will then have
660 the facilities for handling SIGCHLD. */
661 wait_without_blocking ();
662 #endif /* subprocesses */
666 /* Enable sending signal if user quits below. */
667 call_process_exited
= 0;
669 #if defined(MSDOS) || defined(macintosh)
670 /* MSDOS needs different cleanup information. */
671 record_unwind_protect (call_process_cleanup
,
672 Fcons (make_number (fd
[0]), build_string (tempfile
)));
674 record_unwind_protect (call_process_cleanup
,
675 Fcons (make_number (fd
[0]), make_number (pid
)));
676 #endif /* not MSDOS and not macintosh */
679 if (BUFFERP (buffer
))
680 Fset_buffer (buffer
);
684 /* If BUFFER is nil, we must read process output once and then
685 discard it, so setup coding system but with nil. */
686 setup_coding_system (Qnil
, &process_coding
);
690 Lisp_Object val
, *args2
;
693 if (!NILP (Vcoding_system_for_read
))
694 val
= Vcoding_system_for_read
;
697 if (EQ (coding_systems
, Qt
))
701 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
702 args2
[0] = Qcall_process
;
703 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
705 = Ffind_operation_coding_system (nargs
+ 1, args2
);
707 if (CONSP (coding_systems
))
708 val
= XCAR (coding_systems
);
709 else if (CONSP (Vdefault_process_coding_system
))
710 val
= XCAR (Vdefault_process_coding_system
);
714 setup_coding_system (Fcheck_coding_system (val
), &process_coding
);
715 /* In unibyte mode, character code conversion should not take
716 place but EOL conversion should. So, setup raw-text or one
717 of the subsidiary according to the information just setup. */
718 if (NILP (current_buffer
->enable_multibyte_characters
)
720 setup_raw_text_coding_system (&process_coding
);
731 int display_on_the_fly
= !NILP (display
) && INTERACTIVE
;
732 struct coding_system saved_coding
;
734 saved_coding
= process_coding
;
738 /* Repeatedly read until we've filled as much as possible
739 of the buffer size we have. But don't read
740 less than 1024--save that for the next bufferful. */
742 while (nread
< bufsize
- 1024)
744 int this_read
= emacs_read (fd
[0], bufptr
+ nread
,
752 process_coding
.mode
|= CODING_MODE_LAST_BLOCK
;
757 total_read
+= this_read
;
759 if (display_on_the_fly
)
763 /* Now NREAD is the total amount of data in the buffer. */
768 if (process_coding
.type
== coding_type_no_conversion
)
769 insert (bufptr
, nread
);
771 { /* We have to decode the input. */
772 int size
= decoding_buffer_size (&process_coding
, nread
);
773 char *decoding_buf
= (char *) xmalloc (size
);
775 decode_coding (&process_coding
, bufptr
, decoding_buf
,
777 if (display_on_the_fly
778 && saved_coding
.type
== coding_type_undecided
779 && process_coding
.type
!= coding_type_undecided
)
781 /* We have detected some coding system. But,
782 there's a possibility that the detection was
783 done by insufficient data. So, we give up
784 displaying on the fly. */
785 xfree (decoding_buf
);
786 display_on_the_fly
= 0;
787 process_coding
= saved_coding
;
791 if (process_coding
.produced
> 0)
792 insert (decoding_buf
, process_coding
.produced
);
793 xfree (decoding_buf
);
794 carryover
= nread
- process_coding
.consumed
;
797 /* As CARRYOVER should not be that large, we had
798 better avoid overhead of bcopy. */
799 char *p
= bufptr
+ process_coding
.consumed
;
800 char *pend
= p
+ carryover
;
803 while (p
< pend
) *dst
++ = *p
++;
807 if (process_coding
.mode
& CODING_MODE_LAST_BLOCK
)
810 insert (bufptr
, carryover
);
814 /* Make the buffer bigger as we continue to read more data,
816 if (bufsize
< 64 * 1024 && total_read
> 32 * bufsize
)
819 bufptr
= (char *) alloca (bufsize
);
822 if (!NILP (display
) && INTERACTIVE
)
825 prepare_menu_bars ();
827 redisplay_preserve_echo_area ();
834 Vlast_coding_system_used
= process_coding
.symbol
;
836 /* If the caller required, let the buffer inherit the
837 coding-system used to decode the process output. */
838 if (inherit_process_coding_system
)
839 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
840 make_number (total_read
));
843 /* Wait for it to terminate, unless it already has. */
844 wait_for_termination (pid
);
848 set_buffer_internal (old
);
850 /* Don't kill any children that the subprocess may have left behind
852 call_process_exited
= 1;
854 unbind_to (count
, Qnil
);
856 if (synch_process_death
)
857 return code_convert_string_norecord (build_string (synch_process_death
),
858 Vlocale_coding_system
, 0);
859 return make_number (synch_process_retcode
);
864 delete_temp_file (name
)
867 /* Use Fdelete_file (indirectly) because that runs a file name handler.
868 We did that when writing the file, so we should do so when deleting. */
869 internal_delete_file (name
);
872 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
874 "Send text from START to END to a synchronous process running PROGRAM.\n\
875 The remaining arguments are optional.\n\
876 Delete the text if fourth arg DELETE is non-nil.\n\
878 Insert output in BUFFER before point; t means current buffer;\n\
879 nil for BUFFER means discard it; 0 means discard and don't wait.\n\
880 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,\n\
881 REAL-BUFFER says what to do with standard output, as above,\n\
882 while STDERR-FILE says what to do with standard error in the child.\n\
883 STDERR-FILE may be nil (discard standard error output),\n\
884 t (mix it with ordinary output), or a file name string.\n\
886 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
887 Remaining args are passed to PROGRAM at startup as command args.\n\
889 If BUFFER is nil, `call-process-region' returns immediately with value nil.\n\
890 Otherwise it waits for PROGRAM to terminate\n\
891 and returns a numeric exit status or a signal description string.\n\
892 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.")
895 register Lisp_Object
*args
;
898 Lisp_Object filename_string
;
899 register Lisp_Object start
, end
;
900 int count
= specpdl_ptr
- specpdl
;
901 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
902 Lisp_Object coding_systems
;
903 Lisp_Object val
, *args2
;
909 if ((outf
= egetenv ("TMPDIR"))
910 || (outf
= egetenv ("TMP"))
911 || (outf
= egetenv ("TEMP")))
912 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
915 tempfile
= alloca (20);
918 if (!IS_DIRECTORY_SEP (tempfile
[strlen (tempfile
) - 1]))
919 strcat (tempfile
, "/");
920 if ('/' == DIRECTORY_SEP
)
921 dostounix_filename (tempfile
);
923 unixtodos_filename (tempfile
);
925 strcat (tempfile
, "emXXXXXX");
927 strcat (tempfile
, "detmp.XXX");
929 #else /* not DOS_NT */
930 char *tempfile
= (char *) alloca (STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
931 bcopy (XSTRING (Vtemp_file_name_pattern
)->data
, tempfile
,
932 STRING_BYTES (XSTRING (Vtemp_file_name_pattern
)) + 1);
933 #endif /* not DOS_NT */
939 filename_string
= build_string (tempfile
);
940 GCPRO1 (filename_string
);
943 /* Decide coding-system of the contents of the temporary file. */
944 if (!NILP (Vcoding_system_for_write
))
945 val
= Vcoding_system_for_write
;
946 else if (NILP (current_buffer
->enable_multibyte_characters
))
950 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
951 args2
[0] = Qcall_process_region
;
952 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
953 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
954 if (CONSP (coding_systems
))
955 val
= XCDR (coding_systems
);
956 else if (CONSP (Vdefault_process_coding_system
))
957 val
= XCDR (Vdefault_process_coding_system
);
963 int count1
= specpdl_ptr
- specpdl
;
965 specbind (intern ("coding-system-for-write"), val
);
966 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
, Qnil
, Qnil
);
968 unbind_to (count1
, Qnil
);
971 /* Note that Fcall_process takes care of binding
972 coding-system-for-read. */
974 record_unwind_protect (delete_temp_file
, filename_string
);
976 if (nargs
> 3 && !NILP (args
[3]))
977 Fdelete_region (start
, end
);
989 args
[1] = filename_string
;
991 RETURN_UNGCPRO (unbind_to (count
, Fcall_process (nargs
, args
)));
994 #ifndef VMS /* VMS version is in vmsproc.c. */
996 static int relocate_fd ();
998 /* This is the last thing run in a newly forked inferior
999 either synchronous or asynchronous.
1000 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1001 Initialize inferior's priority, pgrp, connected dir and environment.
1002 then exec another program based on new_argv.
1004 This function may change environ for the superior process.
1005 Therefore, the superior process must save and restore the value
1006 of environ around the vfork and the call to this function.
1008 SET_PGRP is nonzero if we should put the subprocess into a separate
1011 CURRENT_DIR is an elisp string giving the path of the current
1012 directory the subprocess should have. Since we can't really signal
1013 a decent error from within the child, this should be verified as an
1014 executable directory by the parent. */
1017 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
1019 register char **new_argv
;
1021 Lisp_Object current_dir
;
1028 #endif /* WINDOWSNT */
1030 int pid
= getpid ();
1032 #ifdef SET_EMACS_PRIORITY
1034 extern int emacs_priority
;
1036 if (emacs_priority
< 0)
1037 nice (- emacs_priority
);
1042 /* Close Emacs's descriptors that this process should not have. */
1043 close_process_descs ();
1045 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1046 we will lose if we call close_load_descs here. */
1048 close_load_descs ();
1051 /* Note that use of alloca is always safe here. It's obvious for systems
1052 that do not have true vfork or that have true (stack) alloca.
1053 If using vfork and C_ALLOCA it is safe because that changes
1054 the superior's static variables as if the superior had done alloca
1055 and will be cleaned up in the usual way. */
1057 register char *temp
;
1060 i
= STRING_BYTES (XSTRING (current_dir
));
1061 pwd_var
= (char *) alloca (i
+ 6);
1063 bcopy ("PWD=", pwd_var
, 4);
1064 bcopy (XSTRING (current_dir
)->data
, temp
, i
);
1065 if (!IS_DIRECTORY_SEP (temp
[i
- 1])) temp
[i
++] = DIRECTORY_SEP
;
1069 /* We can't signal an Elisp error here; we're in a vfork. Since
1070 the callers check the current directory before forking, this
1071 should only return an error if the directory's permissions
1072 are changed between the check and this chdir, but we should
1074 if (chdir (temp
) < 0)
1079 /* Get past the drive letter, so that d:/ is left alone. */
1080 if (i
> 2 && IS_DEVICE_SEP (temp
[1]) && IS_DIRECTORY_SEP (temp
[2]))
1087 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1088 while (i
> 2 && IS_DIRECTORY_SEP (temp
[i
- 1]))
1092 /* Set `env' to a vector of the strings in Vprocess_environment. */
1094 register Lisp_Object tem
;
1095 register char **new_env
;
1096 register int new_length
;
1099 for (tem
= Vprocess_environment
;
1100 CONSP (tem
) && STRINGP (XCAR (tem
));
1104 /* new_length + 2 to include PWD and terminating 0. */
1105 env
= new_env
= (char **) alloca ((new_length
+ 2) * sizeof (char *));
1107 /* If we have a PWD envvar, pass one down,
1108 but with corrected value. */
1110 *new_env
++ = pwd_var
;
1112 /* Copy the Vprocess_environment strings into new_env. */
1113 for (tem
= Vprocess_environment
;
1114 CONSP (tem
) && STRINGP (XCAR (tem
));
1118 char *string
= (char *) XSTRING (XCAR (tem
))->data
;
1119 /* See if this string duplicates any string already in the env.
1120 If so, don't put it in.
1121 When an env var has multiple definitions,
1122 we keep the definition that comes first in process-environment. */
1123 for (; ep
!= new_env
; ep
++)
1125 char *p
= *ep
, *q
= string
;
1129 /* The string is malformed; might as well drop it. */
1138 *new_env
++ = string
;
1144 prepare_standard_handles (in
, out
, err
, handles
);
1145 set_process_dir (XSTRING (current_dir
)->data
);
1146 #else /* not WINDOWSNT */
1147 /* Make sure that in, out, and err are not actually already in
1148 descriptors zero, one, or two; this could happen if Emacs is
1149 started with its standard in, out, or error closed, as might
1152 int oin
= in
, oout
= out
;
1154 /* We have to avoid relocating the same descriptor twice! */
1156 in
= relocate_fd (in
, 3);
1161 out
= relocate_fd (out
, 3);
1165 else if (err
== oout
)
1168 err
= relocate_fd (err
, 3);
1182 #endif /* not MSDOS */
1183 #endif /* not WINDOWSNT */
1185 #if defined(USG) && !defined(BSD_PGRPS)
1186 #ifndef SETPGRP_RELEASES_CTTY
1187 setpgrp (); /* No arguments but equivalent in this case */
1192 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1193 EMACS_SET_TTY_PGRP (0, &pid
);
1196 something missing here
;
1200 pid
= run_msdos_command (new_argv
, pwd_var
+ 4, in
, out
, err
, env
);
1202 /* An error occurred while trying to run the subprocess. */
1203 report_file_error ("Spawning child process", Qnil
);
1205 #else /* not MSDOS */
1207 /* Spawn the child. (See ntproc.c:Spawnve). */
1208 cpid
= spawnve (_P_NOWAIT
, new_argv
[0], new_argv
, env
);
1209 reset_standard_handles (in
, out
, err
, handles
);
1211 /* An error occurred while trying to spawn the process. */
1212 report_file_error ("Spawning child process", Qnil
);
1214 #else /* not WINDOWSNT */
1215 /* execvp does not accept an environment arg so the only way
1216 to pass this environment is to set environ. Our caller
1217 is responsible for restoring the ambient value of environ. */
1219 execvp (new_argv
[0], new_argv
);
1221 emacs_write (1, "Can't exec program: ", 20);
1222 emacs_write (1, new_argv
[0], strlen (new_argv
[0]));
1223 emacs_write (1, "\n", 1);
1225 #endif /* not WINDOWSNT */
1226 #endif /* not MSDOS */
1229 /* Move the file descriptor FD so that its number is not less than MINFD.
1230 If the file descriptor is moved at all, the original is freed. */
1232 relocate_fd (fd
, minfd
)
1242 char *message1
= "Error while setting up child: ";
1243 char *errmessage
= strerror (errno
);
1244 char *message2
= "\n";
1245 emacs_write (2, message1
, strlen (message1
));
1246 emacs_write (2, errmessage
, strlen (errmessage
));
1247 emacs_write (2, message2
, strlen (message2
));
1250 /* Note that we hold the original FD open while we recurse,
1251 to guarantee we'll get a new FD if we need it. */
1252 new = relocate_fd (new, minfd
);
1259 getenv_internal (var
, varlen
, value
, valuelen
)
1267 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCDR (scan
))
1271 entry
= XCAR (scan
);
1273 && STRING_BYTES (XSTRING (entry
)) > varlen
1274 && XSTRING (entry
)->data
[varlen
] == '='
1276 /* NT environment variables are case insensitive. */
1277 && ! strnicmp (XSTRING (entry
)->data
, var
, varlen
)
1278 #else /* not WINDOWSNT */
1279 && ! bcmp (XSTRING (entry
)->data
, var
, varlen
)
1280 #endif /* not WINDOWSNT */
1283 *value
= (char *) XSTRING (entry
)->data
+ (varlen
+ 1);
1284 *valuelen
= STRING_BYTES (XSTRING (entry
)) - (varlen
+ 1);
1292 DEFUN ("getenv", Fgetenv
, Sgetenv
, 1, 1, 0,
1293 "Return the value of environment variable VAR, as a string.\n\
1294 VAR should be a string. Value is nil if VAR is undefined in the environment.\n\
1295 This function consults the variable ``process-environment'' for its value.")
1302 CHECK_STRING (var
, 0);
1303 if (getenv_internal (XSTRING (var
)->data
, STRING_BYTES (XSTRING (var
)),
1305 return make_string (value
, valuelen
);
1310 /* A version of getenv that consults process_environment, easily
1319 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
1325 #endif /* not VMS */
1327 /* This is run before init_cmdargs. */
1332 char *data_dir
= egetenv ("EMACSDATA");
1333 char *doc_dir
= egetenv ("EMACSDOC");
1336 = Ffile_name_as_directory (build_string (data_dir
? data_dir
1339 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
1342 /* Check the EMACSPATH environment variable, defaulting to the
1343 PATH_EXEC path from epaths.h. */
1344 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1345 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
1346 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1349 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1354 char *data_dir
= egetenv ("EMACSDATA");
1357 Lisp_Object tempdir
;
1359 if (!NILP (Vinstallation_directory
))
1361 /* Add to the path the lib-src subdir of the installation dir. */
1363 tem
= Fexpand_file_name (build_string ("lib-src"),
1364 Vinstallation_directory
);
1366 /* MSDOS uses wrapped binaries, so don't do this. */
1367 if (NILP (Fmember (tem
, Vexec_path
)))
1368 Vexec_path
= nconc2 (Vexec_path
, Fcons (tem
, Qnil
));
1370 Vexec_directory
= Ffile_name_as_directory (tem
);
1371 #endif /* not DOS_NT */
1373 /* Maybe use ../etc as well as ../lib-src. */
1376 tem
= Fexpand_file_name (build_string ("etc"),
1377 Vinstallation_directory
);
1378 Vdoc_directory
= Ffile_name_as_directory (tem
);
1382 /* Look for the files that should be in etc. We don't use
1383 Vinstallation_directory, because these files are never installed
1384 near the executable, and they are never in the build
1385 directory when that's different from the source directory.
1387 Instead, if these files are not in the nominal place, we try the
1388 source directory. */
1391 Lisp_Object tem
, tem1
, newdir
;
1393 tem
= Fexpand_file_name (build_string ("GNU"), Vdata_directory
);
1394 tem1
= Ffile_exists_p (tem
);
1397 newdir
= Fexpand_file_name (build_string ("../etc/"),
1398 build_string (PATH_DUMPLOADSEARCH
));
1399 tem
= Fexpand_file_name (build_string ("GNU"), newdir
);
1400 tem1
= Ffile_exists_p (tem
);
1402 Vdata_directory
= newdir
;
1410 tempdir
= Fdirectory_file_name (Vexec_directory
);
1411 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1412 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1416 tempdir
= Fdirectory_file_name (Vdata_directory
);
1417 if (access (XSTRING (tempdir
)->data
, 0) < 0)
1418 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1422 Vshell_file_name
= build_string ("*dcl*");
1424 sh
= (char *) getenv ("SHELL");
1425 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
1429 Vtemp_file_name_pattern
= build_string ("tmp:emacsXXXXXX.");
1431 if (getenv ("TMPDIR"))
1433 char *dir
= getenv ("TMPDIR");
1434 Vtemp_file_name_pattern
1435 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1436 build_string (dir
));
1439 Vtemp_file_name_pattern
= build_string ("/tmp/emacsXXXXXX");
1444 set_process_environment ()
1446 register char **envp
;
1448 Vprocess_environment
= Qnil
;
1452 for (envp
= environ
; *envp
; envp
++)
1453 Vprocess_environment
= Fcons (build_string (*envp
),
1454 Vprocess_environment
);
1461 Qbuffer_file_type
= intern ("buffer-file-type");
1462 staticpro (&Qbuffer_file_type
);
1465 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
1466 "*File name to load inferior shells from.\n\
1467 Initialized from the SHELL environment variable.");
1469 DEFVAR_LISP ("exec-path", &Vexec_path
,
1470 "*List of directories to search programs to run in subprocesses.\n\
1471 Each element is a string (directory name) or nil (try default directory).");
1473 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
1474 "Directory for executables for Emacs to invoke.\n\
1475 More generally, this includes any architecture-dependent files\n\
1476 that are built and installed from the Emacs distribution.");
1478 DEFVAR_LISP ("data-directory", &Vdata_directory
,
1479 "Directory of machine-independent files that come with GNU Emacs.\n\
1480 These are files intended for Emacs to use while it runs.");
1482 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
1483 "Directory containing the DOC file that comes with GNU Emacs.\n\
1484 This is usually the same as data-directory.");
1486 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
1487 "For internal use by the build procedure only.\n\
1488 This is the name of the directory in which the build procedure installed\n\
1489 Emacs's info files; the default value for Info-default-directory-list\n\
1491 Vconfigure_info_directory
= build_string (PATH_INFO
);
1493 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern
,
1494 "Pattern for making names for temporary files.\n\
1495 This is used by `call-process-region'.");
1496 /* This variable is initialized in init_callproc. */
1498 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
1499 "List of environment variables for subprocesses to inherit.\n\
1500 Each element should be a string of the form ENVVARNAME=VALUE.\n\
1501 The environment which Emacs inherits is placed in this variable\n\
1502 when Emacs starts.");
1505 defsubr (&Scall_process
);
1508 defsubr (&Scall_process_region
);