1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985,86,87,88,93,94,95,99, 2000,01,02,03,04
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
32 /* Define SIGCHLD as an alias for SIGCLD. */
34 #if !defined (SIGCHLD) && defined (SIGCLD)
35 #define SIGCHLD SIGCLD
38 #include <sys/types.h>
46 #define INCLUDED_FCNTL
53 #include <stdlib.h> /* for proper declaration of environ */
56 #define _P_NOWAIT 1 /* from process.h */
59 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
60 #define INCLUDED_FCNTL
63 #include <sys/param.h>
78 #include "character.h"
81 #include "composite.h"
84 #include "syssignal.h"
86 #include "blockinput.h"
93 extern noshare
char **environ
;
96 extern char **environ
;
101 #if !defined (USG) || defined (BSD_PGRPS)
103 #define setpgrp setpgid
107 Lisp_Object Vexec_path
, Vexec_directory
, Vexec_suffixes
;
108 Lisp_Object Vdata_directory
, Vdoc_directory
;
109 Lisp_Object Vconfigure_info_directory
, Vshared_game_score_directory
;
110 Lisp_Object Vtemp_file_name_pattern
;
112 Lisp_Object Vshell_file_name
;
114 Lisp_Object Vprocess_environment
;
117 Lisp_Object Qbuffer_file_type
;
120 /* True iff we are about to fork off a synchronous process or if we
121 are waiting for it. */
122 int synch_process_alive
;
124 /* Nonzero => this is a string explaining death of synchronous subprocess. */
125 char *synch_process_death
;
127 /* Nonzero => this is the signal number that terminated the subprocess. */
128 int synch_process_termsig
;
130 /* If synch_process_death is zero,
131 this is exit code of synchronous subprocess. */
132 int synch_process_retcode
;
134 extern Lisp_Object Vdoc_file_name
;
136 extern Lisp_Object Vfile_name_coding_system
, Vdefault_file_name_coding_system
;
138 /* Clean up when exiting Fcall_process.
139 On MSDOS, delete the temporary file on any kind of termination.
140 On Unix, kill the process and any children on termination by signal. */
142 /* Nonzero if this is termination due to exit. */
143 static int call_process_exited
;
145 #ifndef VMS /* VMS version is in vmsproc.c. */
148 call_process_kill (fdpid
)
151 emacs_close (XFASTINT (Fcar (fdpid
)));
152 EMACS_KILLPG (XFASTINT (Fcdr (fdpid
)), SIGKILL
);
153 synch_process_alive
= 0;
158 call_process_cleanup (fdpid
)
161 #if defined (MSDOS) || defined (MAC_OS8)
162 /* for MSDOS fdpid is really (fd . tempfile) */
163 register Lisp_Object file
;
165 emacs_close (XFASTINT (Fcar (fdpid
)));
166 if (strcmp (SDATA (file
), NULL_DEVICE
) != 0)
167 unlink (SDATA (file
));
168 #else /* not MSDOS and not MAC_OS8 */
169 register int pid
= XFASTINT (Fcdr (fdpid
));
171 if (call_process_exited
)
173 emacs_close (XFASTINT (Fcar (fdpid
)));
177 if (EMACS_KILLPG (pid
, SIGINT
) == 0)
179 int count
= SPECPDL_INDEX ();
180 record_unwind_protect (call_process_kill
, fdpid
);
181 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
184 wait_for_termination (pid
);
186 specpdl_ptr
= specpdl
+ count
; /* Discard the unwind protect. */
187 message1 ("Waiting for process to die...done");
189 synch_process_alive
= 0;
190 emacs_close (XFASTINT (Fcar (fdpid
)));
191 #endif /* not MSDOS */
195 DEFUN ("call-process", Fcall_process
, Scall_process
, 1, MANY
, 0,
196 doc
: /* Call PROGRAM synchronously in separate process.
197 The remaining arguments are optional.
198 The program's input comes from file INFILE (nil means `/dev/null').
199 Insert output in BUFFER before point; t means current buffer;
200 nil for BUFFER means discard it; 0 means discard and don't wait.
201 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
202 REAL-BUFFER says what to do with standard output, as above,
203 while STDERR-FILE says what to do with standard error in the child.
204 STDERR-FILE may be nil (discard standard error output),
205 t (mix it with ordinary output), or a file name string.
207 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
208 Remaining arguments are strings passed as command arguments to PROGRAM.
210 If BUFFER is 0, `call-process' returns immediately with value nil.
211 Otherwise it waits for PROGRAM to terminate
212 and returns a numeric exit status or a signal description string.
213 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
215 usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */)
218 register Lisp_Object
*args
;
220 Lisp_Object infile
, buffer
, current_dir
, path
;
227 int bufsize
= sizeof buf
;
228 int count
= SPECPDL_INDEX ();
230 register const unsigned char **new_argv
231 = (const unsigned char **) alloca ((max (2, nargs
- 2)) * sizeof (char *));
232 struct buffer
*old
= current_buffer
;
233 /* File to use for stderr in the child.
234 t means use same as standard output. */
235 Lisp_Object error_file
;
236 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
237 char *outf
, *tempfile
;
247 struct coding_system process_coding
; /* coding-system of process output */
248 struct coding_system argument_coding
; /* coding-system of arguments */
249 /* Set to the return value of Ffind_operation_coding_system. */
250 Lisp_Object coding_systems
;
252 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
255 CHECK_STRING (args
[0]);
260 /* Without asynchronous processes we cannot have BUFFER == 0. */
262 && (INTEGERP (CONSP (args
[2]) ? XCAR (args
[2]) : args
[2])))
263 error ("Operating system cannot handle asynchronous subprocesses");
264 #endif /* subprocesses */
266 /* Decide the coding-system for giving arguments. */
268 Lisp_Object val
, *args2
;
271 /* If arguments are supplied, we may have to encode them. */
276 for (i
= 4; i
< nargs
; i
++)
277 CHECK_STRING (args
[i
]);
279 for (i
= 4; i
< nargs
; i
++)
280 if (STRING_MULTIBYTE (args
[i
]))
283 if (!NILP (Vcoding_system_for_write
))
284 val
= Vcoding_system_for_write
;
285 else if (! must_encode
)
289 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
290 args2
[0] = Qcall_process
;
291 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
292 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
293 if (CONSP (coding_systems
))
294 val
= XCDR (coding_systems
);
295 else if (CONSP (Vdefault_process_coding_system
))
296 val
= XCDR (Vdefault_process_coding_system
);
300 setup_coding_system (Fcheck_coding_system (val
), &argument_coding
);
304 if (nargs
>= 2 && ! NILP (args
[1]))
306 infile
= Fexpand_file_name (args
[1], current_buffer
->directory
);
307 CHECK_STRING (infile
);
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 (XCDR (buffer
)))
322 Lisp_Object stderr_file
;
323 stderr_file
= XCAR (XCDR (buffer
));
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
= XCAR (buffer
);
334 if (!(EQ (buffer
, Qnil
)
336 || INTEGERP (buffer
)))
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
);
344 CHECK_BUFFER (buffer
);
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_p
= INTERACTIVE
&& nargs
>= 4 && !NILP (args
[3]);
379 filefd
= emacs_open (SDATA (infile
), 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], Vexec_suffixes
, &path
, make_number (X_OK
));
394 emacs_close (filefd
);
395 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
398 /* If program file name starts with /: for quoting a magic name,
400 if (SBYTES (path
) > 2 && SREF (path
, 0) == '/'
401 && SREF (path
, 1) == ':')
402 path
= Fsubstring (path
, make_number (2), Qnil
);
404 new_argv
[0] = SDATA (path
);
408 struct gcpro gcpro1
, gcpro2
, gcpro3
;
410 GCPRO3 (infile
, buffer
, current_dir
);
411 argument_coding
.dst_multibyte
= 0;
412 for (i
= 4; i
< nargs
; i
++)
414 argument_coding
.src_multibyte
= STRING_MULTIBYTE (args
[i
]);
415 if (CODING_REQUIRE_ENCODING (&argument_coding
))
416 /* We must encode this argument. */
417 args
[i
] = encode_coding_string (&argument_coding
, args
[i
], 1);
418 new_argv
[i
- 3] = SDATA (args
[i
]);
421 new_argv
[nargs
- 3] = 0;
426 #ifdef MSDOS /* MW, July 1993 */
427 if ((outf
= egetenv ("TMPDIR")))
428 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
431 tempfile
= alloca (20);
434 dostounix_filename (tempfile
);
435 if (*tempfile
== '\0' || tempfile
[strlen (tempfile
) - 1] != '/')
436 strcat (tempfile
, "/");
437 strcat (tempfile
, "detmp.XXX");
440 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
443 emacs_close (filefd
);
444 report_file_error ("Opening process output file",
445 Fcons (build_string (tempfile
), Qnil
));
452 /* Since we don't have pipes on the Mac, create a temporary file to
453 hold the output of the subprocess. */
454 tempfile
= (char *) alloca (SBYTES (Vtemp_file_name_pattern
) + 1);
455 bcopy (SDATA (Vtemp_file_name_pattern
), tempfile
,
456 SBYTES (Vtemp_file_name_pattern
) + 1);
460 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
464 report_file_error ("Opening process output file",
465 Fcons (build_string (tempfile
), Qnil
));
471 if (INTEGERP (buffer
))
472 fd
[1] = emacs_open (NULL_DEVICE
, O_WRONLY
, 0), fd
[0] = -1;
480 emacs_close (filefd
);
481 report_file_error ("Creating process pipe", Qnil
);
486 /* Replaced by close_process_descs */
487 set_exclusive_use (fd
[0]);
492 /* child_setup must clobber environ in systems with true vfork.
493 Protect it from permanent change. */
494 register char **save_environ
= environ
;
495 register int fd1
= fd
[1];
498 #if 0 /* Some systems don't have sigblock. */
499 mask
= sigblock (sigmask (SIGCHLD
));
502 /* Record that we're about to create a synchronous process. */
503 synch_process_alive
= 1;
505 /* These vars record information from process termination.
506 Clear them now before process can possibly terminate,
507 to avoid timing error if process terminates soon. */
508 synch_process_death
= 0;
509 synch_process_retcode
= 0;
510 synch_process_termsig
= 0;
512 if (NILP (error_file
))
513 fd_error
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
514 else if (STRINGP (error_file
))
517 fd_error
= emacs_open (SDATA (error_file
),
518 O_WRONLY
| O_TRUNC
| O_CREAT
| O_TEXT
,
520 #else /* not DOS_NT */
521 fd_error
= creat (SDATA (error_file
), 0666);
522 #endif /* not DOS_NT */
527 emacs_close (filefd
);
535 report_file_error ("Cannot redirect stderr",
536 Fcons ((NILP (error_file
)
537 ? build_string (NULL_DEVICE
) : error_file
),
541 current_dir
= ENCODE_FILE (current_dir
);
545 /* Call run_mac_command in sysdep.c here directly instead of doing
546 a child_setup as for MSDOS and other platforms. Note that this
547 code does not handle passing the environment to the synchronous
549 char *infn
, *outfn
, *errfn
, *currdn
;
551 /* close these files so subprocess can write to them */
553 if (fd_error
!= outfilefd
)
555 fd1
= -1; /* No harm in closing that one! */
557 infn
= SDATA (infile
);
559 if (NILP (error_file
))
561 else if (EQ (Qt
, error_file
))
564 errfn
= SDATA (error_file
);
565 currdn
= SDATA (current_dir
);
566 pid
= run_mac_command (new_argv
, currdn
, infn
, outfn
, errfn
);
568 /* Record that the synchronous process exited and note its
569 termination status. */
570 synch_process_alive
= 0;
571 synch_process_retcode
= pid
;
572 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
574 synchronize_system_messages_locale ();
575 synch_process_death
= strerror (errno
);
578 /* Since CRLF is converted to LF within `decode_coding', we can
579 always open a file with binary mode. */
580 fd
[0] = open (tempfile
, O_BINARY
);
585 report_file_error ("Cannot re-open temporary file", Qnil
);
588 #else /* not MAC_OS8 */
589 #ifdef MSDOS /* MW, July 1993 */
590 /* Note that on MSDOS `child_setup' actually returns the child process
591 exit status, not its PID, so we assign it to `synch_process_retcode'
593 pid
= child_setup (filefd
, outfilefd
, fd_error
, (char **) new_argv
,
596 /* Record that the synchronous process exited and note its
597 termination status. */
598 synch_process_alive
= 0;
599 synch_process_retcode
= pid
;
600 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
602 synchronize_system_messages_locale ();
603 synch_process_death
= strerror (errno
);
606 emacs_close (outfilefd
);
607 if (fd_error
!= outfilefd
)
608 emacs_close (fd_error
);
609 fd1
= -1; /* No harm in closing that one! */
610 /* Since CRLF is converted to LF within `decode_coding', we can
611 always open a file with binary mode. */
612 fd
[0] = emacs_open (tempfile
, O_RDONLY
| O_BINARY
, 0);
616 emacs_close (filefd
);
617 report_file_error ("Cannot re-open temporary file", Qnil
);
619 #else /* not MSDOS */
621 pid
= child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
623 #else /* not WINDOWSNT */
635 #if defined (USG) && !defined (BSD_PGRPS)
640 child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
645 #endif /* not WINDOWSNT */
647 /* The MSDOS case did this already. */
649 emacs_close (fd_error
);
650 #endif /* not MSDOS */
651 #endif /* not MAC_OS8 */
653 environ
= save_environ
;
655 /* Close most of our fd's, but not fd[0]
656 since we will use that to read input from. */
657 emacs_close (filefd
);
658 if (fd1
>= 0 && fd1
!= fd_error
)
666 report_file_error ("Doing vfork", Qnil
);
669 if (INTEGERP (buffer
))
674 /* If Emacs has been built with asynchronous subprocess support,
675 we don't need to do this, I think because it will then have
676 the facilities for handling SIGCHLD. */
677 wait_without_blocking ();
678 #endif /* subprocesses */
682 /* Enable sending signal if user quits below. */
683 call_process_exited
= 0;
685 #if defined(MSDOS) || defined(MAC_OS8)
686 /* MSDOS needs different cleanup information. */
687 record_unwind_protect (call_process_cleanup
,
688 Fcons (make_number (fd
[0]), build_string (tempfile
)));
690 record_unwind_protect (call_process_cleanup
,
691 Fcons (make_number (fd
[0]), make_number (pid
)));
692 #endif /* not MSDOS and not MAC_OS8 */
695 if (BUFFERP (buffer
))
696 Fset_buffer (buffer
);
700 /* If BUFFER is nil, we must read process output once and then
701 discard it, so setup coding system but with nil. */
702 setup_coding_system (Qnil
, &process_coding
);
706 Lisp_Object val
, *args2
;
709 if (!NILP (Vcoding_system_for_read
))
710 val
= Vcoding_system_for_read
;
713 if (EQ (coding_systems
, Qt
))
717 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
718 args2
[0] = Qcall_process
;
719 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
721 = Ffind_operation_coding_system (nargs
+ 1, args2
);
723 if (CONSP (coding_systems
))
724 val
= XCAR (coding_systems
);
725 else if (CONSP (Vdefault_process_coding_system
))
726 val
= XCAR (Vdefault_process_coding_system
);
730 Fcheck_coding_system (val
);
731 /* In unibyte mode, character code conversion should not take
732 place but EOL conversion should. So, setup raw-text or one
733 of the subsidiary according to the information just setup. */
734 if (NILP (current_buffer
->enable_multibyte_characters
)
736 val
= raw_text_coding_system (val
);
737 setup_coding_system (val
, &process_coding
);
748 int display_on_the_fly
= display_p
;
749 struct coding_system saved_coding
;
751 saved_coding
= process_coding
;
754 /* Repeatedly read until we've filled as much as possible
755 of the buffer size we have. But don't read
756 less than 1024--save that for the next bufferful. */
758 while (nread
< bufsize
- 1024)
760 int this_read
= emacs_read (fd
[0], bufptr
+ nread
,
768 process_coding
.mode
|= CODING_MODE_LAST_BLOCK
;
773 total_read
+= this_read
;
775 if (display_on_the_fly
)
779 /* Now NREAD is the total amount of data in the buffer. */
784 if (NILP (current_buffer
->enable_multibyte_characters
)
785 && ! CODING_MAY_REQUIRE_DECODING (&process_coding
))
786 insert_1_both (bufptr
, nread
, nread
, 0, 1, 0);
788 { /* We have to decode the input. */
791 XSETBUFFER (buf
, current_buffer
);
792 decode_coding_c_string (&process_coding
, bufptr
, nread
,
794 if (display_on_the_fly
795 && CODING_REQUIRE_DETECTION (&saved_coding
)
796 && ! CODING_REQUIRE_DETECTION (&process_coding
))
798 /* We have detected some coding system. But,
799 there's a possibility that the detection was
800 done by insufficient data. So, we give up
801 displaying on the fly. */
802 if (process_coding
.produced
> 0)
803 del_range_2 (process_coding
.dst_pos
,
804 process_coding
.dst_pos_byte
,
805 process_coding
.dst_pos
806 + process_coding
.produced_char
,
807 process_coding
.dst_pos_byte
808 + process_coding
.produced
, 0);
809 display_on_the_fly
= 0;
810 process_coding
= saved_coding
;
812 /* This is to make the above condition always
813 fails in the future. */
814 saved_coding
.common_flags
815 &= ~CODING_REQUIRE_DETECTION_MASK
;
819 TEMP_SET_PT_BOTH (PT
+ process_coding
.produced_char
,
820 PT_BYTE
+ process_coding
.produced
);
821 carryover
= process_coding
.carryover_bytes
;
823 /* As CARRYOVER should not be that large, we had
824 better avoid overhead of bcopy. */
825 BCOPY_SHORT (process_coding
.carryover
, bufptr
,
826 process_coding
.carryover_bytes
);
830 if (process_coding
.mode
& CODING_MODE_LAST_BLOCK
)
833 /* Make the buffer bigger as we continue to read more data,
835 if (bufsize
< 64 * 1024 && total_read
> 32 * bufsize
)
840 tempptr
= (char *) alloca (bufsize
);
841 bcopy (bufptr
, tempptr
, bufsize
/ 2);
848 prepare_menu_bars ();
850 redisplay_preserve_echo_area (1);
851 /* This variable might have been set to 0 for code
852 detection. In that case, we set it back to 1 because
853 we should have already detected a coding system. */
854 display_on_the_fly
= 1;
861 Vlast_coding_system_used
= CODING_ID_NAME (process_coding
.id
);
862 /* If the caller required, let the buffer inherit the
863 coding-system used to decode the process output. */
864 if (inherit_process_coding_system
)
865 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
866 make_number (total_read
));
869 /* Wait for it to terminate, unless it already has. */
870 wait_for_termination (pid
);
874 set_buffer_internal (old
);
876 /* Don't kill any children that the subprocess may have left behind
878 call_process_exited
= 1;
880 unbind_to (count
, Qnil
);
882 if (synch_process_termsig
)
886 synchronize_system_messages_locale ();
887 signame
= strsignal (synch_process_termsig
);
892 synch_process_death
= signame
;
895 if (synch_process_death
)
896 return code_convert_string_norecord (build_string (synch_process_death
),
897 Vlocale_coding_system
, 0);
898 return make_number (synch_process_retcode
);
903 delete_temp_file (name
)
906 /* Use Fdelete_file (indirectly) because that runs a file name handler.
907 We did that when writing the file, so we should do so when deleting. */
908 internal_delete_file (name
);
912 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
914 doc
: /* Send text from START to END to a synchronous process running PROGRAM.
915 The remaining arguments are optional.
916 Delete the text if fourth arg DELETE is non-nil.
918 Insert output in BUFFER before point; t means current buffer;
919 nil for BUFFER means discard it; 0 means discard and don't wait.
920 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
921 REAL-BUFFER says what to do with standard output, as above,
922 while STDERR-FILE says what to do with standard error in the child.
923 STDERR-FILE may be nil (discard standard error output),
924 t (mix it with ordinary output), or a file name string.
926 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.
927 Remaining args are passed to PROGRAM at startup as command args.
929 If BUFFER is 0, `call-process-region' returns immediately with value nil.
930 Otherwise it waits for PROGRAM to terminate
931 and returns a numeric exit status or a signal description string.
932 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
934 usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */)
937 register Lisp_Object
*args
;
940 Lisp_Object filename_string
;
941 register Lisp_Object start
, end
;
942 int count
= SPECPDL_INDEX ();
943 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
944 Lisp_Object coding_systems
;
945 Lisp_Object val
, *args2
;
951 if ((outf
= egetenv ("TMPDIR"))
952 || (outf
= egetenv ("TMP"))
953 || (outf
= egetenv ("TEMP")))
954 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
957 tempfile
= alloca (20);
960 if (!IS_DIRECTORY_SEP (tempfile
[strlen (tempfile
) - 1]))
961 strcat (tempfile
, "/");
962 if ('/' == DIRECTORY_SEP
)
963 dostounix_filename (tempfile
);
965 unixtodos_filename (tempfile
);
967 strcat (tempfile
, "emXXXXXX");
969 strcat (tempfile
, "detmp.XXX");
971 #else /* not DOS_NT */
972 char *tempfile
= (char *) alloca (SBYTES (Vtemp_file_name_pattern
) + 1);
973 bcopy (SDATA (Vtemp_file_name_pattern
), tempfile
,
974 SBYTES (Vtemp_file_name_pattern
) + 1);
975 #endif /* not DOS_NT */
981 int fd
= mkstemp (tempfile
);
983 report_file_error ("Failed to open temporary file",
984 Fcons (Vtemp_file_name_pattern
, Qnil
));
992 filename_string
= build_string (tempfile
);
993 GCPRO1 (filename_string
);
996 /* Decide coding-system of the contents of the temporary file. */
997 if (!NILP (Vcoding_system_for_write
))
998 val
= Vcoding_system_for_write
;
999 else if (NILP (current_buffer
->enable_multibyte_characters
))
1003 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1004 args2
[0] = Qcall_process_region
;
1005 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1006 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1007 if (CONSP (coding_systems
))
1008 val
= XCDR (coding_systems
);
1009 else if (CONSP (Vdefault_process_coding_system
))
1010 val
= XCDR (Vdefault_process_coding_system
);
1016 int count1
= SPECPDL_INDEX ();
1018 specbind (intern ("coding-system-for-write"), val
);
1019 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
, Qnil
, Qnil
);
1021 unbind_to (count1
, Qnil
);
1024 /* Note that Fcall_process takes care of binding
1025 coding-system-for-read. */
1027 record_unwind_protect (delete_temp_file
, filename_string
);
1029 if (nargs
> 3 && !NILP (args
[3]))
1030 Fdelete_region (start
, end
);
1042 args
[1] = filename_string
;
1044 RETURN_UNGCPRO (unbind_to (count
, Fcall_process (nargs
, args
)));
1047 #ifndef VMS /* VMS version is in vmsproc.c. */
1049 static int relocate_fd ();
1051 /* This is the last thing run in a newly forked inferior
1052 either synchronous or asynchronous.
1053 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1054 Initialize inferior's priority, pgrp, connected dir and environment.
1055 then exec another program based on new_argv.
1057 This function may change environ for the superior process.
1058 Therefore, the superior process must save and restore the value
1059 of environ around the vfork and the call to this function.
1061 SET_PGRP is nonzero if we should put the subprocess into a separate
1064 CURRENT_DIR is an elisp string giving the path of the current
1065 directory the subprocess should have. Since we can't really signal
1066 a decent error from within the child, this should be verified as an
1067 executable directory by the parent. */
1070 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
1072 register char **new_argv
;
1074 Lisp_Object current_dir
;
1081 #endif /* WINDOWSNT */
1083 int pid
= getpid ();
1085 #ifdef SET_EMACS_PRIORITY
1087 extern EMACS_INT emacs_priority
;
1089 if (emacs_priority
< 0)
1090 nice (- emacs_priority
);
1095 /* Close Emacs's descriptors that this process should not have. */
1096 close_process_descs ();
1098 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1099 we will lose if we call close_load_descs here. */
1101 close_load_descs ();
1104 /* Note that use of alloca is always safe here. It's obvious for systems
1105 that do not have true vfork or that have true (stack) alloca.
1106 If using vfork and C_ALLOCA it is safe because that changes
1107 the superior's static variables as if the superior had done alloca
1108 and will be cleaned up in the usual way. */
1110 register char *temp
;
1113 i
= SBYTES (current_dir
);
1115 /* MSDOS must have all environment variables malloc'ed, because
1116 low-level libc functions that launch subsidiary processes rely
1118 pwd_var
= (char *) xmalloc (i
+ 6);
1120 pwd_var
= (char *) alloca (i
+ 6);
1123 bcopy ("PWD=", pwd_var
, 4);
1124 bcopy (SDATA (current_dir
), temp
, i
);
1125 if (!IS_DIRECTORY_SEP (temp
[i
- 1])) temp
[i
++] = DIRECTORY_SEP
;
1129 /* We can't signal an Elisp error here; we're in a vfork. Since
1130 the callers check the current directory before forking, this
1131 should only return an error if the directory's permissions
1132 are changed between the check and this chdir, but we should
1134 if (chdir (temp
) < 0)
1139 /* Get past the drive letter, so that d:/ is left alone. */
1140 if (i
> 2 && IS_DEVICE_SEP (temp
[1]) && IS_DIRECTORY_SEP (temp
[2]))
1147 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1148 while (i
> 2 && IS_DIRECTORY_SEP (temp
[i
- 1]))
1152 /* Set `env' to a vector of the strings in Vprocess_environment. */
1154 register Lisp_Object tem
;
1155 register char **new_env
;
1156 register int new_length
;
1159 for (tem
= Vprocess_environment
;
1160 CONSP (tem
) && STRINGP (XCAR (tem
));
1164 /* new_length + 2 to include PWD and terminating 0. */
1165 env
= new_env
= (char **) alloca ((new_length
+ 2) * sizeof (char *));
1167 /* If we have a PWD envvar, pass one down,
1168 but with corrected value. */
1170 *new_env
++ = pwd_var
;
1172 /* Copy the Vprocess_environment strings into new_env. */
1173 for (tem
= Vprocess_environment
;
1174 CONSP (tem
) && STRINGP (XCAR (tem
));
1178 char *string
= (char *) SDATA (XCAR (tem
));
1179 /* See if this string duplicates any string already in the env.
1180 If so, don't put it in.
1181 When an env var has multiple definitions,
1182 we keep the definition that comes first in process-environment. */
1183 for (; ep
!= new_env
; ep
++)
1185 char *p
= *ep
, *q
= string
;
1189 /* The string is malformed; might as well drop it. */
1198 *new_env
++ = string
;
1204 prepare_standard_handles (in
, out
, err
, handles
);
1205 set_process_dir (SDATA (current_dir
));
1206 #else /* not WINDOWSNT */
1207 /* Make sure that in, out, and err are not actually already in
1208 descriptors zero, one, or two; this could happen if Emacs is
1209 started with its standard in, out, or error closed, as might
1212 int oin
= in
, oout
= out
;
1214 /* We have to avoid relocating the same descriptor twice! */
1216 in
= relocate_fd (in
, 3);
1221 out
= relocate_fd (out
, 3);
1225 else if (err
== oout
)
1228 err
= relocate_fd (err
, 3);
1242 #endif /* not MSDOS */
1243 #endif /* not WINDOWSNT */
1245 #if defined(USG) && !defined(BSD_PGRPS)
1246 #ifndef SETPGRP_RELEASES_CTTY
1247 setpgrp (); /* No arguments but equivalent in this case */
1252 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1253 EMACS_SET_TTY_PGRP (0, &pid
);
1256 pid
= run_msdos_command (new_argv
, pwd_var
+ 4, in
, out
, err
, env
);
1259 /* An error occurred while trying to run the subprocess. */
1260 report_file_error ("Spawning child process", Qnil
);
1262 #else /* not MSDOS */
1264 /* Spawn the child. (See ntproc.c:Spawnve). */
1265 cpid
= spawnve (_P_NOWAIT
, new_argv
[0], new_argv
, env
);
1266 reset_standard_handles (in
, out
, err
, handles
);
1268 /* An error occurred while trying to spawn the process. */
1269 report_file_error ("Spawning child process", Qnil
);
1271 #else /* not WINDOWSNT */
1272 /* execvp does not accept an environment arg so the only way
1273 to pass this environment is to set environ. Our caller
1274 is responsible for restoring the ambient value of environ. */
1276 execvp (new_argv
[0], new_argv
);
1278 emacs_write (1, "Can't exec program: ", 20);
1279 emacs_write (1, new_argv
[0], strlen (new_argv
[0]));
1280 emacs_write (1, "\n", 1);
1282 #endif /* not WINDOWSNT */
1283 #endif /* not MSDOS */
1286 /* Move the file descriptor FD so that its number is not less than MINFD.
1287 If the file descriptor is moved at all, the original is freed. */
1289 relocate_fd (fd
, minfd
)
1299 char *message1
= "Error while setting up child: ";
1300 char *errmessage
= strerror (errno
);
1301 char *message2
= "\n";
1302 emacs_write (2, message1
, strlen (message1
));
1303 emacs_write (2, errmessage
, strlen (errmessage
));
1304 emacs_write (2, message2
, strlen (message2
));
1307 /* Note that we hold the original FD open while we recurse,
1308 to guarantee we'll get a new FD if we need it. */
1309 new = relocate_fd (new, minfd
);
1316 getenv_internal (var
, varlen
, value
, valuelen
)
1324 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCDR (scan
))
1328 entry
= XCAR (scan
);
1330 && SBYTES (entry
) > varlen
1331 && SREF (entry
, varlen
) == '='
1333 /* NT environment variables are case insensitive. */
1334 && ! strnicmp (SDATA (entry
), var
, varlen
)
1335 #else /* not WINDOWSNT */
1336 && ! bcmp (SDATA (entry
), var
, varlen
)
1337 #endif /* not WINDOWSNT */
1340 *value
= (char *) SDATA (entry
) + (varlen
+ 1);
1341 *valuelen
= SBYTES (entry
) - (varlen
+ 1);
1349 DEFUN ("getenv-internal", Fgetenv_internal
, Sgetenv_internal
, 1, 1, 0,
1350 doc
: /* Return the value of environment variable VAR, as a string.
1351 VAR should be a string. Value is nil if VAR is undefined in the environment.
1352 This function consults the variable ``process-environment'' for its value. */)
1360 if (getenv_internal (SDATA (var
), SBYTES (var
),
1362 return make_string (value
, valuelen
);
1367 /* A version of getenv that consults process_environment, easily
1376 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
1382 #endif /* not VMS */
1384 /* This is run before init_cmdargs. */
1389 char *data_dir
= egetenv ("EMACSDATA");
1390 char *doc_dir
= egetenv ("EMACSDOC");
1393 = Ffile_name_as_directory (build_string (data_dir
? data_dir
1396 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
1399 /* Check the EMACSPATH environment variable, defaulting to the
1400 PATH_EXEC path from epaths.h. */
1401 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1402 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
1403 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1406 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1411 char *data_dir
= egetenv ("EMACSDATA");
1414 Lisp_Object tempdir
;
1416 if (!NILP (Vinstallation_directory
))
1418 /* Add to the path the lib-src subdir of the installation dir. */
1420 tem
= Fexpand_file_name (build_string ("lib-src"),
1421 Vinstallation_directory
);
1423 /* MSDOS uses wrapped binaries, so don't do this. */
1424 if (NILP (Fmember (tem
, Vexec_path
)))
1426 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1427 Vexec_path
= Fcons (tem
, Vexec_path
);
1428 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1431 Vexec_directory
= Ffile_name_as_directory (tem
);
1432 #endif /* not DOS_NT */
1434 /* Maybe use ../etc as well as ../lib-src. */
1437 tem
= Fexpand_file_name (build_string ("etc"),
1438 Vinstallation_directory
);
1439 Vdoc_directory
= Ffile_name_as_directory (tem
);
1443 /* Look for the files that should be in etc. We don't use
1444 Vinstallation_directory, because these files are never installed
1445 near the executable, and they are never in the build
1446 directory when that's different from the source directory.
1448 Instead, if these files are not in the nominal place, we try the
1449 source directory. */
1452 Lisp_Object tem
, tem1
, srcdir
;
1454 srcdir
= Fexpand_file_name (build_string ("../src/"),
1455 build_string (PATH_DUMPLOADSEARCH
));
1456 tem
= Fexpand_file_name (build_string ("GNU"), Vdata_directory
);
1457 tem1
= Ffile_exists_p (tem
);
1458 if (!NILP (Fequal (srcdir
, Vinvocation_directory
)) || NILP (tem1
))
1461 newdir
= Fexpand_file_name (build_string ("../etc/"),
1462 build_string (PATH_DUMPLOADSEARCH
));
1463 tem
= Fexpand_file_name (build_string ("GNU"), newdir
);
1464 tem1
= Ffile_exists_p (tem
);
1466 Vdata_directory
= newdir
;
1474 tempdir
= Fdirectory_file_name (Vexec_directory
);
1475 if (access (SDATA (tempdir
), 0) < 0)
1476 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1480 tempdir
= Fdirectory_file_name (Vdata_directory
);
1481 if (access (SDATA (tempdir
), 0) < 0)
1482 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1486 Vshell_file_name
= build_string ("*dcl*");
1488 sh
= (char *) getenv ("SHELL");
1489 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
1493 Vtemp_file_name_pattern
= build_string ("tmp:emacsXXXXXX.");
1495 if (getenv ("TMPDIR"))
1497 char *dir
= getenv ("TMPDIR");
1498 Vtemp_file_name_pattern
1499 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1500 build_string (dir
));
1503 Vtemp_file_name_pattern
= build_string ("/tmp/emacsXXXXXX");
1507 Vshared_game_score_directory
= Qnil
;
1509 Vshared_game_score_directory
= build_string (PATH_GAME
);
1510 if (NILP (Ffile_directory_p (Vshared_game_score_directory
)))
1511 Vshared_game_score_directory
= Qnil
;
1516 set_process_environment ()
1518 register char **envp
;
1520 Vprocess_environment
= Qnil
;
1524 for (envp
= environ
; *envp
; envp
++)
1525 Vprocess_environment
= Fcons (build_string (*envp
),
1526 Vprocess_environment
);
1533 Qbuffer_file_type
= intern ("buffer-file-type");
1534 staticpro (&Qbuffer_file_type
);
1537 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
1538 doc
: /* *File name to load inferior shells from.
1539 Initialized from the SHELL environment variable. */);
1541 DEFVAR_LISP ("exec-path", &Vexec_path
,
1542 doc
: /* *List of directories to search programs to run in subprocesses.
1543 Each element is a string (directory name) or nil (try default directory). */);
1545 DEFVAR_LISP ("exec-suffixes", &Vexec_suffixes
,
1546 doc
: /* *List of suffixes to try to find executable file names.
1547 Each element is a string. */);
1548 Vexec_suffixes
= Qnil
;
1550 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
1551 doc
: /* Directory for executables for Emacs to invoke.
1552 More generally, this includes any architecture-dependent files
1553 that are built and installed from the Emacs distribution. */);
1555 DEFVAR_LISP ("data-directory", &Vdata_directory
,
1556 doc
: /* Directory of machine-independent files that come with GNU Emacs.
1557 These are files intended for Emacs to use while it runs. */);
1559 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
1560 doc
: /* Directory containing the DOC file that comes with GNU Emacs.
1561 This is usually the same as data-directory. */);
1563 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
1564 doc
: /* For internal use by the build procedure only.
1565 This is the name of the directory in which the build procedure installed
1566 Emacs's info files; the default value for Info-default-directory-list
1568 Vconfigure_info_directory
= build_string (PATH_INFO
);
1570 DEFVAR_LISP ("shared-game-score-directory", &Vshared_game_score_directory
,
1571 doc
: /* Directory of score files for games which come with GNU Emacs.
1572 If this variable is nil, then Emacs is unable to use a shared directory. */);
1574 Vshared_game_score_directory
= Qnil
;
1576 Vshared_game_score_directory
= build_string (PATH_GAME
);
1579 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern
,
1580 doc
: /* Pattern for making names for temporary files.
1581 This is used by `call-process-region'. */);
1582 /* This variable is initialized in init_callproc. */
1584 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
1585 doc
: /* List of environment variables for subprocesses to inherit.
1586 Each element should be a string of the form ENVVARNAME=VALUE.
1587 If multiple entries define the same variable, the first one always
1589 The environment which Emacs inherits is placed in this variable
1591 Non-ASCII characters are encoded according to the initial value of
1592 `locale-coding-system', i.e. the elements must normally be decoded for use.
1593 See `setenv' and `getenv'. */);
1596 defsubr (&Scall_process
);
1597 defsubr (&Sgetenv_internal
);
1599 defsubr (&Scall_process_region
);
1602 /* arch-tag: 769b8045-1df7-4d2b-8968-e3fb49017f95
1603 (do not change this comment) */