1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985,86,87,88,93,94,95,99, 2000, 2001
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>
81 #include "composite.h"
84 #include "syssignal.h"
92 extern noshare
char **environ
;
95 extern char **environ
;
100 #if !defined (USG) || defined (BSD_PGRPS)
102 #define setpgrp setpgid
106 Lisp_Object Vexec_path
, Vexec_directory
, Vexec_suffixes
;
107 Lisp_Object Vdata_directory
, Vdoc_directory
;
108 Lisp_Object Vconfigure_info_directory
, Vshared_game_score_directory
;
109 Lisp_Object Vtemp_file_name_pattern
;
111 Lisp_Object Vshell_file_name
;
113 Lisp_Object Vprocess_environment
;
116 Lisp_Object Qbuffer_file_type
;
119 /* True iff we are about to fork off a synchronous process or if we
120 are waiting for it. */
121 int synch_process_alive
;
123 /* Nonzero => this is a string explaining death of synchronous subprocess. */
124 char *synch_process_death
;
126 /* Nonzero => this is the signal number that terminated the subprocess. */
127 int synch_process_termsig
;
129 /* If synch_process_death is zero,
130 this is exit code of synchronous subprocess. */
131 int synch_process_retcode
;
133 extern Lisp_Object Vdoc_file_name
;
135 extern Lisp_Object Vfile_name_coding_system
, Vdefault_file_name_coding_system
;
137 /* Clean up when exiting Fcall_process.
138 On MSDOS, delete the temporary file on any kind of termination.
139 On Unix, kill the process and any children on termination by signal. */
141 /* Nonzero if this is termination due to exit. */
142 static int call_process_exited
;
144 #ifndef VMS /* VMS version is in vmsproc.c. */
147 call_process_kill (fdpid
)
150 emacs_close (XFASTINT (Fcar (fdpid
)));
151 EMACS_KILLPG (XFASTINT (Fcdr (fdpid
)), SIGKILL
);
152 synch_process_alive
= 0;
157 call_process_cleanup (fdpid
)
160 #if defined (MSDOS) || defined (MAC_OS8)
161 /* for MSDOS fdpid is really (fd . tempfile) */
162 register Lisp_Object file
;
164 emacs_close (XFASTINT (Fcar (fdpid
)));
165 if (strcmp (SDATA (file
), NULL_DEVICE
) != 0)
166 unlink (SDATA (file
));
167 #else /* not MSDOS and not MAC_OS8 */
168 register int pid
= XFASTINT (Fcdr (fdpid
));
170 if (call_process_exited
)
172 emacs_close (XFASTINT (Fcar (fdpid
)));
176 if (EMACS_KILLPG (pid
, SIGINT
) == 0)
178 int count
= SPECPDL_INDEX ();
179 record_unwind_protect (call_process_kill
, fdpid
);
180 message1 ("Waiting for process to die...(type C-g again to kill it instantly)");
183 wait_for_termination (pid
);
185 specpdl_ptr
= specpdl
+ count
; /* Discard the unwind protect. */
186 message1 ("Waiting for process to die...done");
188 synch_process_alive
= 0;
189 emacs_close (XFASTINT (Fcar (fdpid
)));
190 #endif /* not MSDOS */
194 DEFUN ("call-process", Fcall_process
, Scall_process
, 1, MANY
, 0,
195 doc
: /* Call PROGRAM synchronously in separate process.
196 The remaining arguments are optional.
197 The program's input comes from file INFILE (nil means `/dev/null').
198 Insert output in BUFFER before point; t means current buffer;
199 nil for BUFFER means discard it; 0 means discard and don't wait.
200 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
201 REAL-BUFFER says what to do with standard output, as above,
202 while STDERR-FILE says what to do with standard error in the child.
203 STDERR-FILE may be nil (discard standard error output),
204 t (mix it with ordinary output), or a file name string.
206 Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.
207 Remaining arguments are strings passed as command arguments to PROGRAM.
209 If BUFFER is 0, `call-process' returns immediately with value nil.
210 Otherwise it waits for PROGRAM to terminate
211 and returns a numeric exit status or a signal description string.
212 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
214 usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */)
217 register Lisp_Object
*args
;
219 Lisp_Object infile
, buffer
, current_dir
, display
, path
;
226 int count
= SPECPDL_INDEX ();
228 register const unsigned char **new_argv
229 = (const unsigned char **) alloca ((max (2, nargs
- 2)) * sizeof (char *));
230 struct buffer
*old
= current_buffer
;
231 /* File to use for stderr in the child.
232 t means use same as standard output. */
233 Lisp_Object error_file
;
234 #ifdef MSDOS /* Demacs 1.1.1 91/10/16 HIRANO Satoshi */
235 char *outf
, *tempfile
;
245 struct coding_system process_coding
; /* coding-system of process output */
246 struct coding_system argument_coding
; /* coding-system of arguments */
247 /* Set to the return value of Ffind_operation_coding_system. */
248 Lisp_Object coding_systems
;
250 /* Qt denotes that Ffind_operation_coding_system is not yet called. */
253 CHECK_STRING (args
[0]);
258 /* Without asynchronous processes we cannot have BUFFER == 0. */
260 && (INTEGERP (CONSP (args
[2]) ? XCAR (args
[2]) : args
[2])))
261 error ("Operating system cannot handle asynchronous subprocesses");
262 #endif /* subprocesses */
264 /* Decide the coding-system for giving arguments. */
266 Lisp_Object val
, *args2
;
269 /* If arguments are supplied, we may have to encode them. */
274 for (i
= 4; i
< nargs
; i
++)
275 CHECK_STRING (args
[i
]);
277 for (i
= 4; i
< nargs
; i
++)
278 if (STRING_MULTIBYTE (args
[i
]))
281 if (!NILP (Vcoding_system_for_write
))
282 val
= Vcoding_system_for_write
;
283 else if (! must_encode
)
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
];
290 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
291 if (CONSP (coding_systems
))
292 val
= XCDR (coding_systems
);
293 else if (CONSP (Vdefault_process_coding_system
))
294 val
= XCDR (Vdefault_process_coding_system
);
298 setup_coding_system (Fcheck_coding_system (val
), &argument_coding
);
302 if (nargs
>= 2 && ! NILP (args
[1]))
304 infile
= Fexpand_file_name (args
[1], current_buffer
->directory
);
305 CHECK_STRING (infile
);
308 infile
= build_string (NULL_DEVICE
);
314 /* If BUFFER is a list, its meaning is
315 (BUFFER-FOR-STDOUT FILE-FOR-STDERR). */
318 if (CONSP (XCDR (buffer
)))
320 Lisp_Object stderr_file
;
321 stderr_file
= XCAR (XCDR (buffer
));
323 if (NILP (stderr_file
) || EQ (Qt
, stderr_file
))
324 error_file
= stderr_file
;
326 error_file
= Fexpand_file_name (stderr_file
, Qnil
);
329 buffer
= XCAR (buffer
);
332 if (!(EQ (buffer
, Qnil
)
334 || INTEGERP (buffer
)))
336 Lisp_Object spec_buffer
;
337 spec_buffer
= buffer
;
338 buffer
= Fget_buffer_create (buffer
);
339 /* Mention the buffer name for a better error message. */
341 CHECK_BUFFER (spec_buffer
);
342 CHECK_BUFFER (buffer
);
348 /* Make sure that the child will be able to chdir to the current
349 buffer's current directory, or its unhandled equivalent. We
350 can't just have the child check for an error when it does the
351 chdir, since it's in a vfork.
353 We have to GCPRO around this because Fexpand_file_name,
354 Funhandled_file_name_directory, and Ffile_accessible_directory_p
355 might call a file name handling function. The argument list is
356 protected by the caller, so all we really have to worry about is
359 struct gcpro gcpro1
, gcpro2
, gcpro3
;
361 current_dir
= current_buffer
->directory
;
363 GCPRO3 (infile
, buffer
, current_dir
);
366 = expand_and_dir_to_file (Funhandled_file_name_directory (current_dir
),
368 if (NILP (Ffile_accessible_directory_p (current_dir
)))
369 report_file_error ("Setting current directory",
370 Fcons (current_buffer
->directory
, Qnil
));
375 display
= nargs
>= 4 ? args
[3] : Qnil
;
377 filefd
= emacs_open (SDATA (infile
), O_RDONLY
, 0);
380 report_file_error ("Opening process input file", Fcons (infile
, Qnil
));
382 /* Search for program; barf if not found. */
386 GCPRO1 (current_dir
);
387 openp (Vexec_path
, args
[0], Vexec_suffixes
, &path
, make_number (X_OK
));
392 emacs_close (filefd
);
393 report_file_error ("Searching for program", Fcons (args
[0], Qnil
));
396 /* If program file name starts with /: for quoting a magic name,
398 if (SBYTES (path
) > 2 && SREF (path
, 0) == '/'
399 && SREF (path
, 1) == ':')
400 path
= Fsubstring (path
, make_number (2), Qnil
);
402 new_argv
[0] = SDATA (path
);
406 struct gcpro gcpro1
, gcpro2
, gcpro3
;
408 GCPRO3 (infile
, buffer
, current_dir
);
409 argument_coding
.dst_multibyte
= 0;
410 for (i
= 4; i
< nargs
; i
++)
412 argument_coding
.src_multibyte
= STRING_MULTIBYTE (args
[i
]);
413 if (CODING_REQUIRE_ENCODING (&argument_coding
))
415 /* We must encode this argument. */
416 args
[i
] = encode_coding_string (args
[i
], &argument_coding
, 1);
417 if (argument_coding
.type
== coding_type_ccl
)
418 setup_ccl_program (&(argument_coding
.spec
.ccl
.encoder
), Qnil
);
420 new_argv
[i
- 3] = SDATA (args
[i
]);
423 new_argv
[nargs
- 3] = 0;
428 #ifdef MSDOS /* MW, July 1993 */
429 if ((outf
= egetenv ("TMPDIR")))
430 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
433 tempfile
= alloca (20);
436 dostounix_filename (tempfile
);
437 if (*tempfile
== '\0' || tempfile
[strlen (tempfile
) - 1] != '/')
438 strcat (tempfile
, "/");
439 strcat (tempfile
, "detmp.XXX");
442 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
445 emacs_close (filefd
);
446 report_file_error ("Opening process output file",
447 Fcons (build_string (tempfile
), Qnil
));
454 /* Since we don't have pipes on the Mac, create a temporary file to
455 hold the output of the subprocess. */
456 tempfile
= (char *) alloca (SBYTES (Vtemp_file_name_pattern
) + 1);
457 bcopy (SDATA (Vtemp_file_name_pattern
), tempfile
,
458 SBYTES (Vtemp_file_name_pattern
) + 1);
462 outfilefd
= creat (tempfile
, S_IREAD
| S_IWRITE
);
466 report_file_error ("Opening process output file",
467 Fcons (build_string (tempfile
), Qnil
));
473 if (INTEGERP (buffer
))
474 fd
[1] = emacs_open (NULL_DEVICE
, O_WRONLY
, 0), fd
[0] = -1;
482 emacs_close (filefd
);
483 report_file_error ("Creating process pipe", Qnil
);
488 /* Replaced by close_process_descs */
489 set_exclusive_use (fd
[0]);
494 /* child_setup must clobber environ in systems with true vfork.
495 Protect it from permanent change. */
496 register char **save_environ
= environ
;
497 register int fd1
= fd
[1];
500 #if 0 /* Some systems don't have sigblock. */
501 mask
= sigblock (sigmask (SIGCHLD
));
504 /* Record that we're about to create a synchronous process. */
505 synch_process_alive
= 1;
507 /* These vars record information from process termination.
508 Clear them now before process can possibly terminate,
509 to avoid timing error if process terminates soon. */
510 synch_process_death
= 0;
511 synch_process_retcode
= 0;
512 synch_process_termsig
= 0;
514 if (NILP (error_file
))
515 fd_error
= emacs_open (NULL_DEVICE
, O_WRONLY
, 0);
516 else if (STRINGP (error_file
))
519 fd_error
= emacs_open (SDATA (error_file
),
520 O_WRONLY
| O_TRUNC
| O_CREAT
| O_TEXT
,
522 #else /* not DOS_NT */
523 fd_error
= creat (SDATA (error_file
), 0666);
524 #endif /* not DOS_NT */
529 emacs_close (filefd
);
537 report_file_error ("Cannot redirect stderr",
538 Fcons ((NILP (error_file
)
539 ? build_string (NULL_DEVICE
) : error_file
),
543 current_dir
= ENCODE_FILE (current_dir
);
547 /* Call run_mac_command in sysdep.c here directly instead of doing
548 a child_setup as for MSDOS and other platforms. Note that this
549 code does not handle passing the environment to the synchronous
551 char *infn
, *outfn
, *errfn
, *currdn
;
553 /* close these files so subprocess can write to them */
555 if (fd_error
!= outfilefd
)
557 fd1
= -1; /* No harm in closing that one! */
559 infn
= SDATA (infile
);
561 if (NILP (error_file
))
563 else if (EQ (Qt
, error_file
))
566 errfn
= SDATA (error_file
);
567 currdn
= SDATA (current_dir
);
568 pid
= run_mac_command (new_argv
, currdn
, infn
, outfn
, errfn
);
570 /* Record that the synchronous process exited and note its
571 termination status. */
572 synch_process_alive
= 0;
573 synch_process_retcode
= pid
;
574 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
576 synchronize_system_messages_locale ();
577 synch_process_death
= strerror (errno
);
580 /* Since CRLF is converted to LF within `decode_coding', we can
581 always open a file with binary mode. */
582 fd
[0] = open (tempfile
, O_BINARY
);
587 report_file_error ("Cannot re-open temporary file", Qnil
);
590 #else /* not MAC_OS8 */
591 #ifdef MSDOS /* MW, July 1993 */
592 /* Note that on MSDOS `child_setup' actually returns the child process
593 exit status, not its PID, so we assign it to `synch_process_retcode'
595 pid
= child_setup (filefd
, outfilefd
, fd_error
, (char **) new_argv
,
598 /* Record that the synchronous process exited and note its
599 termination status. */
600 synch_process_alive
= 0;
601 synch_process_retcode
= pid
;
602 if (synch_process_retcode
< 0) /* means it couldn't be exec'ed */
604 synchronize_system_messages_locale ();
605 synch_process_death
= strerror (errno
);
608 emacs_close (outfilefd
);
609 if (fd_error
!= outfilefd
)
610 emacs_close (fd_error
);
611 fd1
= -1; /* No harm in closing that one! */
612 /* Since CRLF is converted to LF within `decode_coding', we can
613 always open a file with binary mode. */
614 fd
[0] = emacs_open (tempfile
, O_RDONLY
| O_BINARY
, 0);
618 emacs_close (filefd
);
619 report_file_error ("Cannot re-open temporary file", Qnil
);
621 #else /* not MSDOS */
623 pid
= child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
625 #else /* not WINDOWSNT */
635 #if defined (USG) && !defined (BSD_PGRPS)
640 child_setup (filefd
, fd1
, fd_error
, (char **) new_argv
,
643 #endif /* not WINDOWSNT */
645 /* The MSDOS case did this already. */
647 emacs_close (fd_error
);
648 #endif /* not MSDOS */
649 #endif /* not MAC_OS8 */
651 environ
= save_environ
;
653 /* Close most of our fd's, but not fd[0]
654 since we will use that to read input from. */
655 emacs_close (filefd
);
656 if (fd1
>= 0 && fd1
!= fd_error
)
664 report_file_error ("Doing vfork", Qnil
);
667 if (INTEGERP (buffer
))
672 /* If Emacs has been built with asynchronous subprocess support,
673 we don't need to do this, I think because it will then have
674 the facilities for handling SIGCHLD. */
675 wait_without_blocking ();
676 #endif /* subprocesses */
680 /* Enable sending signal if user quits below. */
681 call_process_exited
= 0;
683 #if defined(MSDOS) || defined(MAC_OS8)
684 /* MSDOS needs different cleanup information. */
685 record_unwind_protect (call_process_cleanup
,
686 Fcons (make_number (fd
[0]), build_string (tempfile
)));
688 record_unwind_protect (call_process_cleanup
,
689 Fcons (make_number (fd
[0]), make_number (pid
)));
690 #endif /* not MSDOS and not MAC_OS8 */
693 if (BUFFERP (buffer
))
694 Fset_buffer (buffer
);
698 /* If BUFFER is nil, we must read process output once and then
699 discard it, so setup coding system but with nil. */
700 setup_coding_system (Qnil
, &process_coding
);
704 Lisp_Object val
, *args2
;
707 if (!NILP (Vcoding_system_for_read
))
708 val
= Vcoding_system_for_read
;
711 if (EQ (coding_systems
, Qt
))
715 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
716 args2
[0] = Qcall_process
;
717 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
719 = Ffind_operation_coding_system (nargs
+ 1, args2
);
721 if (CONSP (coding_systems
))
722 val
= XCAR (coding_systems
);
723 else if (CONSP (Vdefault_process_coding_system
))
724 val
= XCAR (Vdefault_process_coding_system
);
728 setup_coding_system (Fcheck_coding_system (val
), &process_coding
);
729 /* In unibyte mode, character code conversion should not take
730 place but EOL conversion should. So, setup raw-text or one
731 of the subsidiary according to the information just setup. */
732 if (NILP (current_buffer
->enable_multibyte_characters
)
734 setup_raw_text_coding_system (&process_coding
);
736 process_coding
.src_multibyte
= 0;
737 process_coding
.dst_multibyte
739 ? ! NILP (XBUFFER (buffer
)->enable_multibyte_characters
)
740 : ! NILP (current_buffer
->enable_multibyte_characters
));
750 int display_on_the_fly
= !NILP (display
) && INTERACTIVE
;
751 struct coding_system saved_coding
;
752 int pt_orig
= PT
, pt_byte_orig
= PT_BYTE
;
755 saved_coding
= process_coding
;
756 if (process_coding
.composing
!= COMPOSITION_DISABLED
)
757 coding_allocate_composition_data (&process_coding
, PT
);
760 /* Repeatedly read until we've filled as much as possible
761 of the buffer size we have. But don't read
762 less than 1024--save that for the next bufferful. */
764 while (nread
< bufsize
- 1024)
766 int this_read
= emacs_read (fd
[0], bufptr
+ nread
,
774 process_coding
.mode
|= CODING_MODE_LAST_BLOCK
;
779 total_read
+= this_read
;
781 if (display_on_the_fly
)
785 /* Now NREAD is the total amount of data in the buffer. */
790 if (! CODING_MAY_REQUIRE_DECODING (&process_coding
))
791 insert_1_both (bufptr
, nread
, nread
, 0, 1, 0);
793 { /* We have to decode the input. */
798 size
= decoding_buffer_size (&process_coding
, nread
);
799 decoding_buf
= (char *) xmalloc (size
);
801 /* We can't use the macro CODING_REQUIRE_DETECTION
802 because it always returns nonzero if the coding
803 system requires EOL detection. Here, we have to
804 check only whether or not the coding system
805 requires text-encoding detection. */
806 if (process_coding
.type
== coding_type_undecided
)
808 detect_coding (&process_coding
, bufptr
, nread
);
809 if (process_coding
.composing
!= COMPOSITION_DISABLED
)
810 coding_allocate_composition_data (&process_coding
, PT
);
812 if (process_coding
.cmp_data
)
813 process_coding
.cmp_data
->char_offset
= PT
;
815 decode_coding (&process_coding
, bufptr
, decoding_buf
,
818 if (display_on_the_fly
819 && saved_coding
.type
== coding_type_undecided
820 && process_coding
.type
!= coding_type_undecided
)
822 /* We have detected some coding system. But,
823 there's a possibility that the detection was
824 done by insufficient data. So, we give up
825 displaying on the fly. */
826 xfree (decoding_buf
);
827 display_on_the_fly
= 0;
828 process_coding
= saved_coding
;
833 if (process_coding
.produced
> 0)
834 insert_1_both (decoding_buf
, process_coding
.produced_char
,
835 process_coding
.produced
, 0, 1, 0);
836 xfree (decoding_buf
);
838 if (process_coding
.result
== CODING_FINISH_INCONSISTENT_EOL
)
840 Lisp_Object eol_type
, coding
;
842 if (process_coding
.eol_type
== CODING_EOL_CR
)
844 /* CRs have been replaced with LFs. Undo
845 that in the text inserted above. */
848 move_gap_both (PT
, PT_BYTE
);
850 p
= BYTE_POS_ADDR (pt_byte_orig
);
851 for (; p
< GPT_ADDR
; ++p
)
855 else if (process_coding
.eol_type
== CODING_EOL_CRLF
)
857 /* CR LFs have been replaced with LFs. Undo
858 that by inserting CRs in front of LFs in
859 the text inserted above. */
860 EMACS_INT bytepos
, old_pt
, old_pt_byte
, nCR
;
863 old_pt_byte
= PT_BYTE
;
866 for (bytepos
= PT_BYTE
- 1;
867 bytepos
>= pt_byte_orig
;
869 if (FETCH_BYTE (bytepos
) == '\n')
871 EMACS_INT charpos
= BYTE_TO_CHAR (bytepos
);
872 TEMP_SET_PT_BOTH (charpos
, bytepos
);
873 insert_1_both ("\r", 1, 1, 0, 1, 0);
877 TEMP_SET_PT_BOTH (old_pt
+ nCR
, old_pt_byte
+ nCR
);
880 /* Set the coding system symbol to that for
882 eol_type
= Fget (saved_coding
.symbol
, Qeol_type
);
883 if (VECTORP (eol_type
)
884 && ASIZE (eol_type
) == 3
885 && SYMBOLP (AREF (eol_type
, CODING_EOL_LF
)))
886 coding
= AREF (eol_type
, CODING_EOL_LF
);
888 coding
= saved_coding
.symbol
;
890 process_coding
.symbol
= coding
;
891 process_coding
.eol_type
= CODING_EOL_LF
;
893 &= ~CODING_MODE_INHIBIT_INCONSISTENT_EOL
;
896 nread
-= process_coding
.consumed
;
899 /* As CARRYOVER should not be that large, we had
900 better avoid overhead of bcopy. */
901 BCOPY_SHORT (bufptr
+ process_coding
.consumed
, bufptr
,
903 if (process_coding
.result
== CODING_FINISH_INSUFFICIENT_CMP
)
905 /* The decoding ended because of insufficient data
906 area to record information about composition.
907 We must try decoding with additional data area
908 before reading more output for the process. */
909 coding_allocate_composition_data (&process_coding
, PT
);
910 goto repeat_decoding
;
915 if (process_coding
.mode
& CODING_MODE_LAST_BLOCK
)
918 /* Make the buffer bigger as we continue to read more data,
920 if (bufsize
< 64 * 1024 && total_read
> 32 * bufsize
)
925 tempptr
= (char *) alloca (bufsize
);
926 bcopy (bufptr
, tempptr
, bufsize
/ 2);
930 if (!NILP (display
) && INTERACTIVE
)
933 prepare_menu_bars ();
935 redisplay_preserve_echo_area (1);
943 && process_coding
.cmp_data
)
945 coding_restore_composition (&process_coding
, Fcurrent_buffer ());
946 coding_free_composition_data (&process_coding
);
950 int post_read_count
= SPECPDL_INDEX ();
952 record_unwind_protect (save_excursion_restore
, save_excursion_save ());
953 inserted
= PT
- pt_orig
;
954 TEMP_SET_PT_BOTH (pt_orig
, pt_byte_orig
);
955 if (SYMBOLP (process_coding
.post_read_conversion
)
956 && !NILP (Ffboundp (process_coding
.post_read_conversion
)))
957 call1 (process_coding
.post_read_conversion
, make_number (inserted
));
959 Vlast_coding_system_used
= process_coding
.symbol
;
961 /* If the caller required, let the buffer inherit the
962 coding-system used to decode the process output. */
963 if (inherit_process_coding_system
)
964 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
965 make_number (total_read
));
967 unbind_to (post_read_count
, Qnil
);
971 /* Wait for it to terminate, unless it already has. */
972 wait_for_termination (pid
);
976 set_buffer_internal (old
);
978 /* Don't kill any children that the subprocess may have left behind
980 call_process_exited
= 1;
982 unbind_to (count
, Qnil
);
984 if (synch_process_termsig
)
988 synchronize_system_messages_locale ();
989 signame
= strsignal (synch_process_termsig
);
994 synch_process_death
= signame
;
997 if (synch_process_death
)
998 return code_convert_string_norecord (build_string (synch_process_death
),
999 Vlocale_coding_system
, 0);
1000 return make_number (synch_process_retcode
);
1005 delete_temp_file (name
)
1008 /* Use Fdelete_file (indirectly) because that runs a file name handler.
1009 We did that when writing the file, so we should do so when deleting. */
1010 internal_delete_file (name
);
1014 DEFUN ("call-process-region", Fcall_process_region
, Scall_process_region
,
1016 doc
: /* Send text from START to END to a synchronous process running PROGRAM.
1017 The remaining arguments are optional.
1018 Delete the text if fourth arg DELETE is non-nil.
1020 Insert output in BUFFER before point; t means current buffer;
1021 nil for BUFFER means discard it; 0 means discard and don't wait.
1022 BUFFER can also have the form (REAL-BUFFER STDERR-FILE); in that case,
1023 REAL-BUFFER says what to do with standard output, as above,
1024 while STDERR-FILE says what to do with standard error in the child.
1025 STDERR-FILE may be nil (discard standard error output),
1026 t (mix it with ordinary output), or a file name string.
1028 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.
1029 Remaining args are passed to PROGRAM at startup as command args.
1031 If BUFFER is 0, `call-process-region' returns immediately with value nil.
1032 Otherwise it waits for PROGRAM to terminate
1033 and returns a numeric exit status or a signal description string.
1034 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
1036 usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */)
1039 register Lisp_Object
*args
;
1041 struct gcpro gcpro1
;
1042 Lisp_Object filename_string
;
1043 register Lisp_Object start
, end
;
1044 int count
= SPECPDL_INDEX ();
1045 /* Qt denotes we have not yet called Ffind_operation_coding_system. */
1046 Lisp_Object coding_systems
;
1047 Lisp_Object val
, *args2
;
1053 if ((outf
= egetenv ("TMPDIR"))
1054 || (outf
= egetenv ("TMP"))
1055 || (outf
= egetenv ("TEMP")))
1056 strcpy (tempfile
= alloca (strlen (outf
) + 20), outf
);
1059 tempfile
= alloca (20);
1062 if (!IS_DIRECTORY_SEP (tempfile
[strlen (tempfile
) - 1]))
1063 strcat (tempfile
, "/");
1064 if ('/' == DIRECTORY_SEP
)
1065 dostounix_filename (tempfile
);
1067 unixtodos_filename (tempfile
);
1069 strcat (tempfile
, "emXXXXXX");
1071 strcat (tempfile
, "detmp.XXX");
1073 #else /* not DOS_NT */
1074 char *tempfile
= (char *) alloca (SBYTES (Vtemp_file_name_pattern
) + 1);
1075 bcopy (SDATA (Vtemp_file_name_pattern
), tempfile
,
1076 SBYTES (Vtemp_file_name_pattern
) + 1);
1077 #endif /* not DOS_NT */
1079 coding_systems
= Qt
;
1083 int fd
= mkstemp (tempfile
);
1085 report_file_error ("Failed to open temporary file",
1086 Fcons (Vtemp_file_name_pattern
, Qnil
));
1094 filename_string
= build_string (tempfile
);
1095 GCPRO1 (filename_string
);
1098 /* Decide coding-system of the contents of the temporary file. */
1099 if (!NILP (Vcoding_system_for_write
))
1100 val
= Vcoding_system_for_write
;
1101 else if (NILP (current_buffer
->enable_multibyte_characters
))
1105 args2
= (Lisp_Object
*) alloca ((nargs
+ 1) * sizeof *args2
);
1106 args2
[0] = Qcall_process_region
;
1107 for (i
= 0; i
< nargs
; i
++) args2
[i
+ 1] = args
[i
];
1108 coding_systems
= Ffind_operation_coding_system (nargs
+ 1, args2
);
1109 if (CONSP (coding_systems
))
1110 val
= XCDR (coding_systems
);
1111 else if (CONSP (Vdefault_process_coding_system
))
1112 val
= XCDR (Vdefault_process_coding_system
);
1118 int count1
= SPECPDL_INDEX ();
1120 specbind (intern ("coding-system-for-write"), val
);
1121 Fwrite_region (start
, end
, filename_string
, Qnil
, Qlambda
, Qnil
, Qnil
);
1123 unbind_to (count1
, Qnil
);
1126 /* Note that Fcall_process takes care of binding
1127 coding-system-for-read. */
1129 record_unwind_protect (delete_temp_file
, filename_string
);
1131 if (nargs
> 3 && !NILP (args
[3]))
1132 Fdelete_region (start
, end
);
1144 args
[1] = filename_string
;
1146 RETURN_UNGCPRO (unbind_to (count
, Fcall_process (nargs
, args
)));
1149 #ifndef VMS /* VMS version is in vmsproc.c. */
1151 static int relocate_fd ();
1153 /* This is the last thing run in a newly forked inferior
1154 either synchronous or asynchronous.
1155 Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
1156 Initialize inferior's priority, pgrp, connected dir and environment.
1157 then exec another program based on new_argv.
1159 This function may change environ for the superior process.
1160 Therefore, the superior process must save and restore the value
1161 of environ around the vfork and the call to this function.
1163 SET_PGRP is nonzero if we should put the subprocess into a separate
1166 CURRENT_DIR is an elisp string giving the path of the current
1167 directory the subprocess should have. Since we can't really signal
1168 a decent error from within the child, this should be verified as an
1169 executable directory by the parent. */
1172 child_setup (in
, out
, err
, new_argv
, set_pgrp
, current_dir
)
1174 register char **new_argv
;
1176 Lisp_Object current_dir
;
1183 #endif /* WINDOWSNT */
1185 int pid
= getpid ();
1187 #ifdef SET_EMACS_PRIORITY
1189 extern EMACS_INT emacs_priority
;
1191 if (emacs_priority
< 0)
1192 nice (- emacs_priority
);
1197 /* Close Emacs's descriptors that this process should not have. */
1198 close_process_descs ();
1200 /* DOS_NT isn't in a vfork, so if we are in the middle of load-file,
1201 we will lose if we call close_load_descs here. */
1203 close_load_descs ();
1206 /* Note that use of alloca is always safe here. It's obvious for systems
1207 that do not have true vfork or that have true (stack) alloca.
1208 If using vfork and C_ALLOCA it is safe because that changes
1209 the superior's static variables as if the superior had done alloca
1210 and will be cleaned up in the usual way. */
1212 register char *temp
;
1215 i
= SBYTES (current_dir
);
1217 /* MSDOS must have all environment variables malloc'ed, because
1218 low-level libc functions that launch subsidiary processes rely
1220 pwd_var
= (char *) xmalloc (i
+ 6);
1222 pwd_var
= (char *) alloca (i
+ 6);
1225 bcopy ("PWD=", pwd_var
, 4);
1226 bcopy (SDATA (current_dir
), temp
, i
);
1227 if (!IS_DIRECTORY_SEP (temp
[i
- 1])) temp
[i
++] = DIRECTORY_SEP
;
1231 /* We can't signal an Elisp error here; we're in a vfork. Since
1232 the callers check the current directory before forking, this
1233 should only return an error if the directory's permissions
1234 are changed between the check and this chdir, but we should
1236 if (chdir (temp
) < 0)
1241 /* Get past the drive letter, so that d:/ is left alone. */
1242 if (i
> 2 && IS_DEVICE_SEP (temp
[1]) && IS_DIRECTORY_SEP (temp
[2]))
1249 /* Strip trailing slashes for PWD, but leave "/" and "//" alone. */
1250 while (i
> 2 && IS_DIRECTORY_SEP (temp
[i
- 1]))
1254 /* Set `env' to a vector of the strings in Vprocess_environment. */
1256 register Lisp_Object tem
;
1257 register char **new_env
;
1258 register int new_length
;
1261 for (tem
= Vprocess_environment
;
1262 CONSP (tem
) && STRINGP (XCAR (tem
));
1266 /* new_length + 2 to include PWD and terminating 0. */
1267 env
= new_env
= (char **) alloca ((new_length
+ 2) * sizeof (char *));
1269 /* If we have a PWD envvar, pass one down,
1270 but with corrected value. */
1272 *new_env
++ = pwd_var
;
1274 /* Copy the Vprocess_environment strings into new_env. */
1275 for (tem
= Vprocess_environment
;
1276 CONSP (tem
) && STRINGP (XCAR (tem
));
1280 char *string
= (char *) SDATA (XCAR (tem
));
1281 /* See if this string duplicates any string already in the env.
1282 If so, don't put it in.
1283 When an env var has multiple definitions,
1284 we keep the definition that comes first in process-environment. */
1285 for (; ep
!= new_env
; ep
++)
1287 char *p
= *ep
, *q
= string
;
1291 /* The string is malformed; might as well drop it. */
1300 *new_env
++ = string
;
1306 prepare_standard_handles (in
, out
, err
, handles
);
1307 set_process_dir (SDATA (current_dir
));
1308 #else /* not WINDOWSNT */
1309 /* Make sure that in, out, and err are not actually already in
1310 descriptors zero, one, or two; this could happen if Emacs is
1311 started with its standard in, out, or error closed, as might
1314 int oin
= in
, oout
= out
;
1316 /* We have to avoid relocating the same descriptor twice! */
1318 in
= relocate_fd (in
, 3);
1323 out
= relocate_fd (out
, 3);
1327 else if (err
== oout
)
1330 err
= relocate_fd (err
, 3);
1344 #endif /* not MSDOS */
1345 #endif /* not WINDOWSNT */
1347 #if defined(USG) && !defined(BSD_PGRPS)
1348 #ifndef SETPGRP_RELEASES_CTTY
1349 setpgrp (); /* No arguments but equivalent in this case */
1354 /* setpgrp_of_tty is incorrect here; it uses input_fd. */
1355 EMACS_SET_TTY_PGRP (0, &pid
);
1358 pid
= run_msdos_command (new_argv
, pwd_var
+ 4, in
, out
, err
, env
);
1361 /* An error occurred while trying to run the subprocess. */
1362 report_file_error ("Spawning child process", Qnil
);
1364 #else /* not MSDOS */
1366 /* Spawn the child. (See ntproc.c:Spawnve). */
1367 cpid
= spawnve (_P_NOWAIT
, new_argv
[0], new_argv
, env
);
1368 reset_standard_handles (in
, out
, err
, handles
);
1370 /* An error occurred while trying to spawn the process. */
1371 report_file_error ("Spawning child process", Qnil
);
1373 #else /* not WINDOWSNT */
1374 /* execvp does not accept an environment arg so the only way
1375 to pass this environment is to set environ. Our caller
1376 is responsible for restoring the ambient value of environ. */
1378 execvp (new_argv
[0], new_argv
);
1380 emacs_write (1, "Can't exec program: ", 20);
1381 emacs_write (1, new_argv
[0], strlen (new_argv
[0]));
1382 emacs_write (1, "\n", 1);
1384 #endif /* not WINDOWSNT */
1385 #endif /* not MSDOS */
1388 /* Move the file descriptor FD so that its number is not less than MINFD.
1389 If the file descriptor is moved at all, the original is freed. */
1391 relocate_fd (fd
, minfd
)
1401 char *message1
= "Error while setting up child: ";
1402 char *errmessage
= strerror (errno
);
1403 char *message2
= "\n";
1404 emacs_write (2, message1
, strlen (message1
));
1405 emacs_write (2, errmessage
, strlen (errmessage
));
1406 emacs_write (2, message2
, strlen (message2
));
1409 /* Note that we hold the original FD open while we recurse,
1410 to guarantee we'll get a new FD if we need it. */
1411 new = relocate_fd (new, minfd
);
1418 getenv_internal (var
, varlen
, value
, valuelen
)
1426 for (scan
= Vprocess_environment
; CONSP (scan
); scan
= XCDR (scan
))
1430 entry
= XCAR (scan
);
1432 && SBYTES (entry
) > varlen
1433 && SREF (entry
, varlen
) == '='
1435 /* NT environment variables are case insensitive. */
1436 && ! strnicmp (SDATA (entry
), var
, varlen
)
1437 #else /* not WINDOWSNT */
1438 && ! bcmp (SDATA (entry
), var
, varlen
)
1439 #endif /* not WINDOWSNT */
1442 *value
= (char *) SDATA (entry
) + (varlen
+ 1);
1443 *valuelen
= SBYTES (entry
) - (varlen
+ 1);
1451 DEFUN ("getenv-internal", Fgetenv_internal
, Sgetenv_internal
, 1, 1, 0,
1452 doc
: /* Return the value of environment variable VAR, as a string.
1453 VAR should be a string. Value is nil if VAR is undefined in the environment.
1454 This function consults the variable ``process-environment'' for its value. */)
1462 if (getenv_internal (SDATA (var
), SBYTES (var
),
1464 return make_string (value
, valuelen
);
1469 /* A version of getenv that consults process_environment, easily
1478 if (getenv_internal (var
, strlen (var
), &value
, &valuelen
))
1484 #endif /* not VMS */
1486 /* This is run before init_cmdargs. */
1491 char *data_dir
= egetenv ("EMACSDATA");
1492 char *doc_dir
= egetenv ("EMACSDOC");
1495 = Ffile_name_as_directory (build_string (data_dir
? data_dir
1498 = Ffile_name_as_directory (build_string (doc_dir
? doc_dir
1501 /* Check the EMACSPATH environment variable, defaulting to the
1502 PATH_EXEC path from epaths.h. */
1503 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1504 Vexec_directory
= Ffile_name_as_directory (Fcar (Vexec_path
));
1505 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1508 /* This is run after init_cmdargs, when Vinstallation_directory is valid. */
1513 char *data_dir
= egetenv ("EMACSDATA");
1516 Lisp_Object tempdir
;
1518 if (!NILP (Vinstallation_directory
))
1520 /* Add to the path the lib-src subdir of the installation dir. */
1522 tem
= Fexpand_file_name (build_string ("lib-src"),
1523 Vinstallation_directory
);
1525 /* MSDOS uses wrapped binaries, so don't do this. */
1526 if (NILP (Fmember (tem
, Vexec_path
)))
1528 Vexec_path
= decode_env_path ("EMACSPATH", PATH_EXEC
);
1529 Vexec_path
= Fcons (tem
, Vexec_path
);
1530 Vexec_path
= nconc2 (decode_env_path ("PATH", ""), Vexec_path
);
1533 Vexec_directory
= Ffile_name_as_directory (tem
);
1534 #endif /* not DOS_NT */
1536 /* Maybe use ../etc as well as ../lib-src. */
1539 tem
= Fexpand_file_name (build_string ("etc"),
1540 Vinstallation_directory
);
1541 Vdoc_directory
= Ffile_name_as_directory (tem
);
1545 /* Look for the files that should be in etc. We don't use
1546 Vinstallation_directory, because these files are never installed
1547 near the executable, and they are never in the build
1548 directory when that's different from the source directory.
1550 Instead, if these files are not in the nominal place, we try the
1551 source directory. */
1554 Lisp_Object tem
, tem1
, srcdir
;
1556 srcdir
= Fexpand_file_name (build_string ("../src/"),
1557 build_string (PATH_DUMPLOADSEARCH
));
1558 tem
= Fexpand_file_name (build_string ("GNU"), Vdata_directory
);
1559 tem1
= Ffile_exists_p (tem
);
1560 if (!NILP (Fequal (srcdir
, Vinvocation_directory
)) || NILP (tem1
))
1563 newdir
= Fexpand_file_name (build_string ("../etc/"),
1564 build_string (PATH_DUMPLOADSEARCH
));
1565 tem
= Fexpand_file_name (build_string ("GNU"), newdir
);
1566 tem1
= Ffile_exists_p (tem
);
1568 Vdata_directory
= newdir
;
1576 tempdir
= Fdirectory_file_name (Vexec_directory
);
1577 if (access (SDATA (tempdir
), 0) < 0)
1578 dir_warning ("Warning: arch-dependent data dir (%s) does not exist.\n",
1582 tempdir
= Fdirectory_file_name (Vdata_directory
);
1583 if (access (SDATA (tempdir
), 0) < 0)
1584 dir_warning ("Warning: arch-independent data dir (%s) does not exist.\n",
1588 Vshell_file_name
= build_string ("*dcl*");
1590 sh
= (char *) getenv ("SHELL");
1591 Vshell_file_name
= build_string (sh
? sh
: "/bin/sh");
1595 Vtemp_file_name_pattern
= build_string ("tmp:emacsXXXXXX.");
1597 if (getenv ("TMPDIR"))
1599 char *dir
= getenv ("TMPDIR");
1600 Vtemp_file_name_pattern
1601 = Fexpand_file_name (build_string ("emacsXXXXXX"),
1602 build_string (dir
));
1605 Vtemp_file_name_pattern
= build_string ("/tmp/emacsXXXXXX");
1609 Vshared_game_score_directory
= Qnil
;
1611 Vshared_game_score_directory
= build_string (PATH_GAME
);
1612 if (NILP (Ffile_directory_p (Vshared_game_score_directory
)))
1613 Vshared_game_score_directory
= Qnil
;
1618 set_process_environment ()
1620 register char **envp
;
1622 Vprocess_environment
= Qnil
;
1626 for (envp
= environ
; *envp
; envp
++)
1627 Vprocess_environment
= Fcons (build_string (*envp
),
1628 Vprocess_environment
);
1635 Qbuffer_file_type
= intern ("buffer-file-type");
1636 staticpro (&Qbuffer_file_type
);
1639 DEFVAR_LISP ("shell-file-name", &Vshell_file_name
,
1640 doc
: /* *File name to load inferior shells from.
1641 Initialized from the SHELL environment variable. */);
1643 DEFVAR_LISP ("exec-path", &Vexec_path
,
1644 doc
: /* *List of directories to search programs to run in subprocesses.
1645 Each element is a string (directory name) or nil (try default directory). */);
1647 DEFVAR_LISP ("exec-suffixes", &Vexec_suffixes
,
1648 doc
: /* *List of suffixes to try to find executable file names.
1649 Each element is a string. */);
1650 Vexec_suffixes
= Qnil
;
1652 DEFVAR_LISP ("exec-directory", &Vexec_directory
,
1653 doc
: /* Directory for executables for Emacs to invoke.
1654 More generally, this includes any architecture-dependent files
1655 that are built and installed from the Emacs distribution. */);
1657 DEFVAR_LISP ("data-directory", &Vdata_directory
,
1658 doc
: /* Directory of machine-independent files that come with GNU Emacs.
1659 These are files intended for Emacs to use while it runs. */);
1661 DEFVAR_LISP ("doc-directory", &Vdoc_directory
,
1662 doc
: /* Directory containing the DOC file that comes with GNU Emacs.
1663 This is usually the same as data-directory. */);
1665 DEFVAR_LISP ("configure-info-directory", &Vconfigure_info_directory
,
1666 doc
: /* For internal use by the build procedure only.
1667 This is the name of the directory in which the build procedure installed
1668 Emacs's info files; the default value for Info-default-directory-list
1670 Vconfigure_info_directory
= build_string (PATH_INFO
);
1672 DEFVAR_LISP ("shared-game-score-directory", &Vshared_game_score_directory
,
1673 doc
: /* Directory of score files for games which come with GNU Emacs.
1674 If this variable is nil, then Emacs is unable to use a shared directory. */);
1676 Vshared_game_score_directory
= Qnil
;
1678 Vshared_game_score_directory
= build_string (PATH_GAME
);
1681 DEFVAR_LISP ("temp-file-name-pattern", &Vtemp_file_name_pattern
,
1682 doc
: /* Pattern for making names for temporary files.
1683 This is used by `call-process-region'. */);
1684 /* This variable is initialized in init_callproc. */
1686 DEFVAR_LISP ("process-environment", &Vprocess_environment
,
1687 doc
: /* List of environment variables for subprocesses to inherit.
1688 Each element should be a string of the form ENVVARNAME=VALUE.
1689 If multiple entries define the same variable, the first one always
1691 The environment which Emacs inherits is placed in this variable
1693 Non-ASCII characters are encoded according to the initial value of
1694 `locale-coding-system', i.e. the elements must normally be decoded for use.
1695 See `setenv' and `getenv'. */);
1698 defsubr (&Scall_process
);
1699 defsubr (&Sgetenv_internal
);
1701 defsubr (&Scall_process_region
);
1704 /* arch-tag: 769b8045-1df7-4d2b-8968-e3fb49017f95
1705 (do not change this comment) */