comparison src/callproc.c @ 88155:d7ddb3e565de

sync with trunk
author Henrik Enberg <henrik.enberg@telia.com>
date Mon, 16 Jan 2006 00:03:54 +0000
parents 23a1cea22d13
children
comparison
equal deleted inserted replaced
88154:8ce476d3ba36 88155:d7ddb3e565de
1 /* Synchronous subprocess invocation for GNU Emacs. 1 /* Synchronous subprocess invocation for GNU Emacs.
2 Copyright (C) 1985,86,87,88,93,94,95,99, 2000, 2001 2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
3 Free Software Foundation, Inc. 3 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 4
5 This file is part of GNU Emacs. 5 This file is part of GNU Emacs.
6 6
7 GNU Emacs is free software; you can redistribute it and/or modify 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 8 it under the terms of the GNU General Public License as published by
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 15 GNU General Public License for more details.
16 16
17 You should have received a copy of the GNU General Public License 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 18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02111-1307, USA. */ 20 Boston, MA 02110-1301, USA. */
21 21
22 22
23 #include <config.h> 23 #include <config.h>
24 #include <signal.h> 24 #include <signal.h>
25 #include <errno.h> 25 #include <errno.h>
40 #ifdef HAVE_UNISTD_H 40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h> 41 #include <unistd.h>
42 #endif 42 #endif
43 43
44 #include <sys/file.h> 44 #include <sys/file.h>
45 #ifdef USG5 45 #ifdef HAVE_FCNTL_H
46 #define INCLUDED_FCNTL 46 #define INCLUDED_FCNTL
47 #include <fcntl.h> 47 #include <fcntl.h>
48 #endif 48 #endif
49 49
50 #ifdef WINDOWSNT 50 #ifdef WINDOWSNT
81 #include "composite.h" 81 #include "composite.h"
82 #include <epaths.h> 82 #include <epaths.h>
83 #include "process.h" 83 #include "process.h"
84 #include "syssignal.h" 84 #include "syssignal.h"
85 #include "systty.h" 85 #include "systty.h"
86 #include "blockinput.h"
86 87
87 #ifdef MSDOS 88 #ifdef MSDOS
88 #include "msdos.h" 89 #include "msdos.h"
89 #endif 90 #endif
90 91
121 int synch_process_alive; 122 int synch_process_alive;
122 123
123 /* Nonzero => this is a string explaining death of synchronous subprocess. */ 124 /* Nonzero => this is a string explaining death of synchronous subprocess. */
124 char *synch_process_death; 125 char *synch_process_death;
125 126
127 /* Nonzero => this is the signal number that terminated the subprocess. */
128 int synch_process_termsig;
129
126 /* If synch_process_death is zero, 130 /* If synch_process_death is zero,
127 this is exit code of synchronous subprocess. */ 131 this is exit code of synchronous subprocess. */
128 int synch_process_retcode; 132 int synch_process_retcode;
129
130 extern Lisp_Object Vdoc_file_name;
131
132 extern Lisp_Object Vfile_name_coding_system, Vdefault_file_name_coding_system;
133 133
134 /* Clean up when exiting Fcall_process. 134 /* Clean up when exiting Fcall_process.
135 On MSDOS, delete the temporary file on any kind of termination. 135 On MSDOS, delete the temporary file on any kind of termination.
136 On Unix, kill the process and any children on termination by signal. */ 136 On Unix, kill the process and any children on termination by signal. */
137 137
211 usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */) 211 usage: (call-process PROGRAM &optional INFILE BUFFER DISPLAY &rest ARGS) */)
212 (nargs, args) 212 (nargs, args)
213 int nargs; 213 int nargs;
214 register Lisp_Object *args; 214 register Lisp_Object *args;
215 { 215 {
216 Lisp_Object infile, buffer, current_dir, display, path; 216 Lisp_Object infile, buffer, current_dir, path;
217 int display_p;
217 int fd[2]; 218 int fd[2];
218 int filefd; 219 int filefd;
219 register int pid; 220 register int pid;
220 char buf[16384]; 221 #define CALLPROC_BUFFER_SIZE_MIN (16 * 1024)
221 char *bufptr = buf; 222 #define CALLPROC_BUFFER_SIZE_MAX (4 * CALLPROC_BUFFER_SIZE_MIN)
222 int bufsize = 16384; 223 char buf[CALLPROC_BUFFER_SIZE_MAX];
224 int bufsize = CALLPROC_BUFFER_SIZE_MIN;
223 int count = SPECPDL_INDEX (); 225 int count = SPECPDL_INDEX ();
224 226
225 register const unsigned char **new_argv 227 register const unsigned char **new_argv
226 = (const unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *)); 228 = (const unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
227 struct buffer *old = current_buffer; 229 struct buffer *old = current_buffer;
367 Fcons (current_buffer->directory, Qnil)); 369 Fcons (current_buffer->directory, Qnil));
368 370
369 UNGCPRO; 371 UNGCPRO;
370 } 372 }
371 373
372 display = nargs >= 4 ? args[3] : Qnil; 374 display_p = INTERACTIVE && nargs >= 4 && !NILP (args[3]);
373 375
374 filefd = emacs_open (SDATA (infile), O_RDONLY, 0); 376 filefd = emacs_open (SDATA (infile), O_RDONLY, 0);
375 if (filefd < 0) 377 if (filefd < 0)
376 { 378 {
377 report_file_error ("Opening process input file", Fcons (infile, Qnil)); 379 report_file_error ("Opening process input file", Fcons (infile, Qnil));
504 /* These vars record information from process termination. 506 /* These vars record information from process termination.
505 Clear them now before process can possibly terminate, 507 Clear them now before process can possibly terminate,
506 to avoid timing error if process terminates soon. */ 508 to avoid timing error if process terminates soon. */
507 synch_process_death = 0; 509 synch_process_death = 0;
508 synch_process_retcode = 0; 510 synch_process_retcode = 0;
511 synch_process_termsig = 0;
509 512
510 if (NILP (error_file)) 513 if (NILP (error_file))
511 fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0); 514 fd_error = emacs_open (NULL_DEVICE, O_WRONLY, 0);
512 else if (STRINGP (error_file)) 515 else if (STRINGP (error_file))
513 { 516 {
617 #else /* not MSDOS */ 620 #else /* not MSDOS */
618 #ifdef WINDOWSNT 621 #ifdef WINDOWSNT
619 pid = child_setup (filefd, fd1, fd_error, (char **) new_argv, 622 pid = child_setup (filefd, fd1, fd_error, (char **) new_argv,
620 0, current_dir); 623 0, current_dir);
621 #else /* not WINDOWSNT */ 624 #else /* not WINDOWSNT */
625 BLOCK_INPUT;
626
622 pid = vfork (); 627 pid = vfork ();
623 628
624 if (pid == 0) 629 if (pid == 0)
625 { 630 {
626 if (fd[0] >= 0) 631 if (fd[0] >= 0)
634 setpgrp (pid, pid); 639 setpgrp (pid, pid);
635 #endif /* USG */ 640 #endif /* USG */
636 child_setup (filefd, fd1, fd_error, (char **) new_argv, 641 child_setup (filefd, fd1, fd_error, (char **) new_argv,
637 0, current_dir); 642 0, current_dir);
638 } 643 }
644
645 UNBLOCK_INPUT;
639 #endif /* not WINDOWSNT */ 646 #endif /* not WINDOWSNT */
640 647
641 /* The MSDOS case did this already. */ 648 /* The MSDOS case did this already. */
642 if (fd_error >= 0) 649 if (fd_error >= 0)
643 emacs_close (fd_error); 650 emacs_close (fd_error);
741 { 748 {
742 register int nread; 749 register int nread;
743 int first = 1; 750 int first = 1;
744 int total_read = 0; 751 int total_read = 0;
745 int carryover = 0; 752 int carryover = 0;
746 int display_on_the_fly = !NILP (display) && INTERACTIVE; 753 int display_on_the_fly = display_p;
747 struct coding_system saved_coding; 754 struct coding_system saved_coding;
748 int pt_orig = PT, pt_byte_orig = PT_BYTE; 755 int pt_orig = PT, pt_byte_orig = PT_BYTE;
749 int inserted; 756 int inserted;
750 757
751 saved_coding = process_coding; 758 saved_coding = process_coding;
757 of the buffer size we have. But don't read 764 of the buffer size we have. But don't read
758 less than 1024--save that for the next bufferful. */ 765 less than 1024--save that for the next bufferful. */
759 nread = carryover; 766 nread = carryover;
760 while (nread < bufsize - 1024) 767 while (nread < bufsize - 1024)
761 { 768 {
762 int this_read = emacs_read (fd[0], bufptr + nread, 769 int this_read = emacs_read (fd[0], buf + nread,
763 bufsize - nread); 770 bufsize - nread);
764 771
765 if (this_read < 0) 772 if (this_read < 0)
766 goto give_up; 773 goto give_up;
767 774
782 immediate_quit = 0; 789 immediate_quit = 0;
783 790
784 if (!NILP (buffer)) 791 if (!NILP (buffer))
785 { 792 {
786 if (! CODING_MAY_REQUIRE_DECODING (&process_coding)) 793 if (! CODING_MAY_REQUIRE_DECODING (&process_coding))
787 insert_1_both (bufptr, nread, nread, 0, 1, 0); 794 insert_1_both (buf, nread, nread, 0, 1, 0);
788 else 795 else
789 { /* We have to decode the input. */ 796 { /* We have to decode the input. */
790 int size; 797 int size;
791 char *decoding_buf; 798 char *decoding_buf;
792 799
799 system requires EOL detection. Here, we have to 806 system requires EOL detection. Here, we have to
800 check only whether or not the coding system 807 check only whether or not the coding system
801 requires text-encoding detection. */ 808 requires text-encoding detection. */
802 if (process_coding.type == coding_type_undecided) 809 if (process_coding.type == coding_type_undecided)
803 { 810 {
804 detect_coding (&process_coding, bufptr, nread); 811 detect_coding (&process_coding, buf, nread);
805 if (process_coding.composing != COMPOSITION_DISABLED) 812 if (process_coding.composing != COMPOSITION_DISABLED)
813 /* We have not yet allocated the composition
814 data because the coding type was undecided. */
806 coding_allocate_composition_data (&process_coding, PT); 815 coding_allocate_composition_data (&process_coding, PT);
807 } 816 }
808 if (process_coding.cmp_data) 817 if (process_coding.cmp_data)
809 process_coding.cmp_data->char_offset = PT; 818 process_coding.cmp_data->char_offset = PT;
810 819
811 decode_coding (&process_coding, bufptr, decoding_buf, 820 decode_coding (&process_coding, buf, decoding_buf,
812 nread, size); 821 nread, size);
813 822
814 if (display_on_the_fly 823 if (display_on_the_fly
815 && saved_coding.type == coding_type_undecided 824 && saved_coding.type == coding_type_undecided
816 && process_coding.type != coding_type_undecided) 825 && process_coding.type != coding_type_undecided)
817 { 826 {
818 /* We have detected some coding system. But, 827 /* We have detected some coding system. But,
819 there's a possibility that the detection was 828 there's a possibility that the detection was
820 done by insufficient data. So, we give up 829 done by insufficient data. So, we try the code
821 displaying on the fly. */ 830 detection again with more data. */
822 xfree (decoding_buf); 831 xfree (decoding_buf);
823 display_on_the_fly = 0; 832 display_on_the_fly = 0;
824 process_coding = saved_coding; 833 process_coding = saved_coding;
825 carryover = nread; 834 carryover = nread;
835 /* This is to make the above condition always
836 fails in the future. */
837 saved_coding.type = coding_type_no_conversion;
826 continue; 838 continue;
827 } 839 }
828 840
829 if (process_coding.produced > 0) 841 if (process_coding.produced > 0)
830 insert_1_both (decoding_buf, process_coding.produced_char, 842 insert_1_both (decoding_buf, process_coding.produced_char,
892 nread -= process_coding.consumed; 904 nread -= process_coding.consumed;
893 carryover = nread; 905 carryover = nread;
894 if (carryover > 0) 906 if (carryover > 0)
895 /* As CARRYOVER should not be that large, we had 907 /* As CARRYOVER should not be that large, we had
896 better avoid overhead of bcopy. */ 908 better avoid overhead of bcopy. */
897 BCOPY_SHORT (bufptr + process_coding.consumed, bufptr, 909 BCOPY_SHORT (buf + process_coding.consumed, buf,
898 carryover); 910 carryover);
899 if (process_coding.result == CODING_FINISH_INSUFFICIENT_CMP) 911 if (process_coding.result == CODING_FINISH_INSUFFICIENT_CMP)
900 { 912 {
901 /* The decoding ended because of insufficient data 913 /* The decoding ended because of insufficient data
902 area to record information about composition. 914 area to record information about composition.
909 } 921 }
910 922
911 if (process_coding.mode & CODING_MODE_LAST_BLOCK) 923 if (process_coding.mode & CODING_MODE_LAST_BLOCK)
912 break; 924 break;
913 925
926 #if (CALLPROC_BUFFER_SIZE_MIN != CALLPROC_BUFFER_SIZE_MAX)
914 /* Make the buffer bigger as we continue to read more data, 927 /* Make the buffer bigger as we continue to read more data,
915 but not past 64k. */ 928 but not past CALLPROC_BUFFER_SIZE_MAX. */
916 if (bufsize < 64 * 1024 && total_read > 32 * bufsize) 929 if (bufsize < CALLPROC_BUFFER_SIZE_MAX && total_read > 32 * bufsize)
917 { 930 if ((bufsize *= 2) > CALLPROC_BUFFER_SIZE_MAX)
918 char *tempptr; 931 bufsize = CALLPROC_BUFFER_SIZE_MAX;
919 bufsize *= 2; 932 #endif
920 933
921 tempptr = (char *) alloca (bufsize); 934 if (display_p)
922 bcopy (bufptr, tempptr, bufsize / 2);
923 bufptr = tempptr;
924 }
925
926 if (!NILP (display) && INTERACTIVE)
927 { 935 {
928 if (first) 936 if (first)
929 prepare_menu_bars (); 937 prepare_menu_bars ();
930 first = 0; 938 first = 0;
931 redisplay_preserve_echo_area (1); 939 redisplay_preserve_echo_area (1);
940 /* This variable might have been set to 0 for code
941 detection. In that case, we set it back to 1 because
942 we should have already detected a coding system. */
943 display_on_the_fly = 1;
932 } 944 }
933 immediate_quit = 1; 945 immediate_quit = 1;
934 QUIT; 946 QUIT;
935 } 947 }
936 give_up: ; 948 give_up: ;
975 when exiting. */ 987 when exiting. */
976 call_process_exited = 1; 988 call_process_exited = 1;
977 989
978 unbind_to (count, Qnil); 990 unbind_to (count, Qnil);
979 991
992 if (synch_process_termsig)
993 {
994 char *signame;
995
996 synchronize_system_messages_locale ();
997 signame = strsignal (synch_process_termsig);
998
999 if (signame == 0)
1000 signame = "unknown";
1001
1002 synch_process_death = signame;
1003 }
1004
980 if (synch_process_death) 1005 if (synch_process_death)
981 return code_convert_string_norecord (build_string (synch_process_death), 1006 return code_convert_string_norecord (build_string (synch_process_death),
982 Vlocale_coding_system, 0); 1007 Vlocale_coding_system, 0);
983 return make_number (synch_process_retcode); 1008 return make_number (synch_process_retcode);
984 } 1009 }
986 1011
987 static Lisp_Object 1012 static Lisp_Object
988 delete_temp_file (name) 1013 delete_temp_file (name)
989 Lisp_Object name; 1014 Lisp_Object name;
990 { 1015 {
991 /* Use Fdelete_file (indirectly) because that runs a file name handler. 1016 /* Suppress jka-compr handling, etc. */
992 We did that when writing the file, so we should do so when deleting. */ 1017 int count = SPECPDL_INDEX ();
1018 specbind (intern ("file-name-handler-alist"), Qnil);
993 internal_delete_file (name); 1019 internal_delete_file (name);
1020 unbind_to (count, Qnil);
994 return Qnil; 1021 return Qnil;
995 } 1022 }
996 1023
997 DEFUN ("call-process-region", Fcall_process_region, Scall_process_region, 1024 DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
998 3, MANY, 0, 1025 3, MANY, 0,
1009 t (mix it with ordinary output), or a file name string. 1036 t (mix it with ordinary output), or a file name string.
1010 1037
1011 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted. 1038 Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.
1012 Remaining args are passed to PROGRAM at startup as command args. 1039 Remaining args are passed to PROGRAM at startup as command args.
1013 1040
1014 If BUFFER is nil, `call-process-region' returns immediately with value nil. 1041 If BUFFER is 0, `call-process-region' returns immediately with value nil.
1015 Otherwise it waits for PROGRAM to terminate 1042 Otherwise it waits for PROGRAM to terminate
1016 and returns a numeric exit status or a signal description string. 1043 and returns a numeric exit status or a signal description string.
1017 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again. 1044 If you quit, the process is killed with SIGINT, or SIGKILL if you quit again.
1018 1045
1019 usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */) 1046 usage: (call-process-region START END PROGRAM &optional DELETE BUFFER DISPLAY &rest ARGS) */)
1099 1126
1100 { 1127 {
1101 int count1 = SPECPDL_INDEX (); 1128 int count1 = SPECPDL_INDEX ();
1102 1129
1103 specbind (intern ("coding-system-for-write"), val); 1130 specbind (intern ("coding-system-for-write"), val);
1131 /* POSIX lets mk[s]temp use "."; don't invoke jka-compr if we
1132 happen to get a ".Z" suffix. */
1133 specbind (intern ("file-name-handler-alist"), Qnil);
1104 Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil); 1134 Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil);
1105 1135
1106 unbind_to (count1, Qnil); 1136 unbind_to (count1, Qnil);
1107 } 1137 }
1108 1138
1670 doc: /* List of environment variables for subprocesses to inherit. 1700 doc: /* List of environment variables for subprocesses to inherit.
1671 Each element should be a string of the form ENVVARNAME=VALUE. 1701 Each element should be a string of the form ENVVARNAME=VALUE.
1672 If multiple entries define the same variable, the first one always 1702 If multiple entries define the same variable, the first one always
1673 takes precedence. 1703 takes precedence.
1674 The environment which Emacs inherits is placed in this variable 1704 The environment which Emacs inherits is placed in this variable
1675 when Emacs starts. */); 1705 when Emacs starts.
1706 Non-ASCII characters are encoded according to the initial value of
1707 `locale-coding-system', i.e. the elements must normally be decoded for use.
1708 See `setenv' and `getenv'. */);
1676 1709
1677 #ifndef VMS 1710 #ifndef VMS
1678 defsubr (&Scall_process); 1711 defsubr (&Scall_process);
1679 defsubr (&Sgetenv_internal); 1712 defsubr (&Sgetenv_internal);
1680 #endif 1713 #endif
1681 defsubr (&Scall_process_region); 1714 defsubr (&Scall_process_region);
1682 } 1715 }
1716
1717 /* arch-tag: 769b8045-1df7-4d2b-8968-e3fb49017f95
1718 (do not change this comment) */