/* Synchronous subprocess invocation for GNU Emacs.
- Copyright (C) 1985,86,87,88,93,94,95,99, 2000,01,02,03,04
- Free Software Foundation, Inc.
+ Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1999, 2000, 2001,
+ 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
This file is part of GNU Emacs.
You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING. If not, write to
-the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA. */
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA. */
#include <config.h>
#include "lisp.h"
#include "commands.h"
#include "buffer.h"
-#include "charset.h"
+#include "character.h"
#include "ccl.h"
#include "coding.h"
#include "composite.h"
#include "process.h"
#include "syssignal.h"
#include "systty.h"
+#include "blockinput.h"
#ifdef MSDOS
#include "msdos.h"
/* If synch_process_death is zero,
this is exit code of synchronous subprocess. */
int synch_process_retcode;
-
-extern Lisp_Object Vdoc_file_name;
-
-extern Lisp_Object Vfile_name_coding_system, Vdefault_file_name_coding_system;
\f
/* Clean up when exiting Fcall_process.
On MSDOS, delete the temporary file on any kind of termination.
int nargs;
register Lisp_Object *args;
{
- Lisp_Object infile, buffer, current_dir, display, path;
+ Lisp_Object infile, buffer, current_dir, path;
+ int display_p;
int fd[2];
int filefd;
register int pid;
- char buf[16384];
- char *bufptr = buf;
- int bufsize = sizeof buf;
+#define CALLPROC_BUFFER_SIZE_MIN (16 * 1024)
+#define CALLPROC_BUFFER_SIZE_MAX (4 * CALLPROC_BUFFER_SIZE_MIN)
+ char buf[CALLPROC_BUFFER_SIZE_MAX];
+ int bufsize = CALLPROC_BUFFER_SIZE_MIN;
int count = SPECPDL_INDEX ();
register const unsigned char **new_argv
protected by the caller, so all we really have to worry about is
buffer. */
{
- struct gcpro gcpro1, gcpro2, gcpro3;
+ struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
current_dir = current_buffer->directory;
- GCPRO3 (infile, buffer, current_dir);
+ GCPRO4 (infile, buffer, current_dir, error_file);
current_dir
= expand_and_dir_to_file (Funhandled_file_name_directory (current_dir),
report_file_error ("Setting current directory",
Fcons (current_buffer->directory, Qnil));
+ if (STRING_MULTIBYTE (infile))
+ infile = ENCODE_FILE (infile);
+ if (STRING_MULTIBYTE (current_dir))
+ current_dir = ENCODE_FILE (current_dir);
+ if (STRINGP (error_file) && STRING_MULTIBYTE (error_file))
+ error_file = ENCODE_FILE (error_file);
UNGCPRO;
}
- display = nargs >= 4 ? args[3] : Qnil;
+ display_p = INTERACTIVE && nargs >= 4 && !NILP (args[3]);
filefd = emacs_open (SDATA (infile), O_RDONLY, 0);
if (filefd < 0)
{
+ infile = DECODE_FILE (infile);
report_file_error ("Opening process input file", Fcons (infile, Qnil));
}
/* Search for program; barf if not found. */
{
argument_coding.src_multibyte = STRING_MULTIBYTE (args[i]);
if (CODING_REQUIRE_ENCODING (&argument_coding))
- {
- /* We must encode this argument. */
- args[i] = encode_coding_string (args[i], &argument_coding, 1);
- if (argument_coding.type == coding_type_ccl)
- setup_ccl_program (&(argument_coding.spec.ccl.encoder), Qnil);
- }
+ /* We must encode this argument. */
+ args[i] = encode_coding_string (&argument_coding, args[i], 1);
new_argv[i - 3] = SDATA (args[i]);
}
UNGCPRO;
#ifdef MSDOS
unlink (tempfile);
#endif
- report_file_error ("Cannot redirect stderr",
- Fcons ((NILP (error_file)
- ? build_string (NULL_DEVICE) : error_file),
- Qnil));
+ if (NILP (error_file))
+ error_file = build_string (NULL_DEVICE);
+ else if (STRINGP (error_file))
+ error_file = DECODE_FILE (error_file);
+ report_file_error ("Cannot redirect stderr", Fcons (error_file, Qnil));
}
- current_dir = ENCODE_FILE (current_dir);
-
#ifdef MAC_OS8
{
/* Call run_mac_command in sysdep.c here directly instead of doing
pid = child_setup (filefd, fd1, fd_error, (char **) new_argv,
0, current_dir);
#else /* not WINDOWSNT */
+ BLOCK_INPUT;
+
pid = vfork ();
if (pid == 0)
child_setup (filefd, fd1, fd_error, (char **) new_argv,
0, current_dir);
}
+
+ UNBLOCK_INPUT;
#endif /* not WINDOWSNT */
/* The MSDOS case did this already. */
else
val = Qnil;
}
- setup_coding_system (Fcheck_coding_system (val), &process_coding);
+ Fcheck_coding_system (val);
/* In unibyte mode, character code conversion should not take
place but EOL conversion should. So, setup raw-text or one
of the subsidiary according to the information just setup. */
if (NILP (current_buffer->enable_multibyte_characters)
&& !NILP (val))
- setup_raw_text_coding_system (&process_coding);
+ val = raw_text_coding_system (val);
+ setup_coding_system (val, &process_coding);
}
- process_coding.src_multibyte = 0;
- process_coding.dst_multibyte
- = (BUFFERP (buffer)
- ? ! NILP (XBUFFER (buffer)->enable_multibyte_characters)
- : ! NILP (current_buffer->enable_multibyte_characters));
immediate_quit = 1;
QUIT;
int first = 1;
int total_read = 0;
int carryover = 0;
- int display_on_the_fly = !NILP (display) && INTERACTIVE;
+ int display_on_the_fly = display_p;
struct coding_system saved_coding;
- int pt_orig = PT, pt_byte_orig = PT_BYTE;
- int inserted;
saved_coding = process_coding;
- if (process_coding.composing != COMPOSITION_DISABLED)
- coding_allocate_composition_data (&process_coding, PT);
while (1)
{
/* Repeatedly read until we've filled as much as possible
nread = carryover;
while (nread < bufsize - 1024)
{
- int this_read = emacs_read (fd[0], bufptr + nread,
+ int this_read = emacs_read (fd[0], buf + nread,
bufsize - nread);
if (this_read < 0)
if (!NILP (buffer))
{
- if (! CODING_MAY_REQUIRE_DECODING (&process_coding))
- insert_1_both (bufptr, nread, nread, 0, 1, 0);
+ if (NILP (current_buffer->enable_multibyte_characters)
+ && ! CODING_MAY_REQUIRE_DECODING (&process_coding))
+ insert_1_both (buf, nread, nread, 0, 1, 0);
else
{ /* We have to decode the input. */
- int size;
- char *decoding_buf;
-
- repeat_decoding:
- size = decoding_buffer_size (&process_coding, nread);
- decoding_buf = (char *) xmalloc (size);
-
- /* We can't use the macro CODING_REQUIRE_DETECTION
- because it always returns nonzero if the coding
- system requires EOL detection. Here, we have to
- check only whether or not the coding system
- requires text-encoding detection. */
- if (process_coding.type == coding_type_undecided)
- {
- detect_coding (&process_coding, bufptr, nread);
- if (process_coding.composing != COMPOSITION_DISABLED)
- /* We have not yet allocated the composition
- data because the coding type was undecided. */
- coding_allocate_composition_data (&process_coding, PT);
- }
- if (process_coding.cmp_data)
- process_coding.cmp_data->char_offset = PT;
-
- decode_coding (&process_coding, bufptr, decoding_buf,
- nread, size);
+ Lisp_Object curbuf;
+ XSETBUFFER (curbuf, current_buffer);
+ decode_coding_c_string (&process_coding, buf, nread,
+ curbuf);
if (display_on_the_fly
- && saved_coding.type == coding_type_undecided
- && process_coding.type != coding_type_undecided)
+ && CODING_REQUIRE_DETECTION (&saved_coding)
+ && ! CODING_REQUIRE_DETECTION (&process_coding))
{
/* We have detected some coding system. But,
there's a possibility that the detection was
done by insufficient data. So, we give up
displaying on the fly. */
- xfree (decoding_buf);
+ if (process_coding.produced > 0)
+ del_range_2 (process_coding.dst_pos,
+ process_coding.dst_pos_byte,
+ process_coding.dst_pos
+ + process_coding.produced_char,
+ process_coding.dst_pos_byte
+ + process_coding.produced, 0);
display_on_the_fly = 0;
process_coding = saved_coding;
carryover = nread;
+ /* This is to make the above condition always
+ fails in the future. */
+ saved_coding.common_flags
+ &= ~CODING_REQUIRE_DETECTION_MASK;
continue;
}
- if (process_coding.produced > 0)
- insert_1_both (decoding_buf, process_coding.produced_char,
- process_coding.produced, 0, 1, 0);
- xfree (decoding_buf);
-
- if (process_coding.result == CODING_FINISH_INCONSISTENT_EOL)
- {
- Lisp_Object eol_type, coding;
-
- if (process_coding.eol_type == CODING_EOL_CR)
- {
- /* CRs have been replaced with LFs. Undo
- that in the text inserted above. */
- unsigned char *p;
-
- move_gap_both (PT, PT_BYTE);
-
- p = BYTE_POS_ADDR (pt_byte_orig);
- for (; p < GPT_ADDR; ++p)
- if (*p == '\n')
- *p = '\r';
- }
- else if (process_coding.eol_type == CODING_EOL_CRLF)
- {
- /* CR LFs have been replaced with LFs. Undo
- that by inserting CRs in front of LFs in
- the text inserted above. */
- EMACS_INT bytepos, old_pt, old_pt_byte, nCR;
-
- old_pt = PT;
- old_pt_byte = PT_BYTE;
- nCR = 0;
-
- for (bytepos = PT_BYTE - 1;
- bytepos >= pt_byte_orig;
- --bytepos)
- if (FETCH_BYTE (bytepos) == '\n')
- {
- EMACS_INT charpos = BYTE_TO_CHAR (bytepos);
- TEMP_SET_PT_BOTH (charpos, bytepos);
- insert_1_both ("\r", 1, 1, 0, 1, 0);
- ++nCR;
- }
-
- TEMP_SET_PT_BOTH (old_pt + nCR, old_pt_byte + nCR);
- }
-
- /* Set the coding system symbol to that for
- Unix-like EOL. */
- eol_type = Fget (saved_coding.symbol, Qeol_type);
- if (VECTORP (eol_type)
- && ASIZE (eol_type) == 3
- && SYMBOLP (AREF (eol_type, CODING_EOL_LF)))
- coding = AREF (eol_type, CODING_EOL_LF);
- else
- coding = saved_coding.symbol;
-
- process_coding.symbol = coding;
- process_coding.eol_type = CODING_EOL_LF;
- process_coding.mode
- &= ~CODING_MODE_INHIBIT_INCONSISTENT_EOL;
- }
-
- nread -= process_coding.consumed;
- carryover = nread;
+ TEMP_SET_PT_BOTH (PT + process_coding.produced_char,
+ PT_BYTE + process_coding.produced);
+ carryover = process_coding.carryover_bytes;
if (carryover > 0)
/* As CARRYOVER should not be that large, we had
better avoid overhead of bcopy. */
- BCOPY_SHORT (bufptr + process_coding.consumed, bufptr,
- carryover);
- if (process_coding.result == CODING_FINISH_INSUFFICIENT_CMP)
- {
- /* The decoding ended because of insufficient data
- area to record information about composition.
- We must try decoding with additional data area
- before reading more output for the process. */
- coding_allocate_composition_data (&process_coding, PT);
- goto repeat_decoding;
- }
+ BCOPY_SHORT (process_coding.carryover, buf,
+ process_coding.carryover_bytes);
}
}
if (process_coding.mode & CODING_MODE_LAST_BLOCK)
break;
+#if (CALLPROC_BUFFER_SIZE_MIN != CALLPROC_BUFFER_SIZE_MAX)
/* Make the buffer bigger as we continue to read more data,
- but not past 64k. */
- if (bufsize < 64 * 1024 && total_read > 32 * bufsize)
- {
- char *tempptr;
- bufsize *= 2;
-
- tempptr = (char *) alloca (bufsize);
- bcopy (bufptr, tempptr, bufsize / 2);
- bufptr = tempptr;
- }
+ but not past CALLPROC_BUFFER_SIZE_MAX. */
+ if (bufsize < CALLPROC_BUFFER_SIZE_MAX && total_read > 32 * bufsize)
+ if ((bufsize *= 2) > CALLPROC_BUFFER_SIZE_MAX)
+ bufsize = CALLPROC_BUFFER_SIZE_MAX;
+#endif
- if (!NILP (display) && INTERACTIVE)
+ if (display_p)
{
if (first)
prepare_menu_bars ();
first = 0;
redisplay_preserve_echo_area (1);
+ /* This variable might have been set to 0 for code
+ detection. In that case, we set it back to 1 because
+ we should have already detected a coding system. */
+ display_on_the_fly = 1;
}
immediate_quit = 1;
QUIT;
}
give_up: ;
- if (!NILP (buffer)
- && process_coding.cmp_data)
- {
- coding_restore_composition (&process_coding, Fcurrent_buffer ());
- coding_free_composition_data (&process_coding);
- }
-
- {
- int post_read_count = SPECPDL_INDEX ();
-
- record_unwind_protect (save_excursion_restore, save_excursion_save ());
- inserted = PT - pt_orig;
- TEMP_SET_PT_BOTH (pt_orig, pt_byte_orig);
- if (SYMBOLP (process_coding.post_read_conversion)
- && !NILP (Ffboundp (process_coding.post_read_conversion)))
- call1 (process_coding.post_read_conversion, make_number (inserted));
-
- Vlast_coding_system_used = process_coding.symbol;
-
- /* If the caller required, let the buffer inherit the
- coding-system used to decode the process output. */
- if (inherit_process_coding_system)
- call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
- make_number (total_read));
-
- unbind_to (post_read_count, Qnil);
- }
+ Vlast_coding_system_used = CODING_ID_NAME (process_coding.id);
+ /* If the caller required, let the buffer inherit the
+ coding-system used to decode the process output. */
+ if (inherit_process_coding_system)
+ call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
+ make_number (total_read));
}
/* Wait for it to terminate, unless it already has. */
delete_temp_file (name)
Lisp_Object name;
{
- /* Use Fdelete_file (indirectly) because that runs a file name handler.
- We did that when writing the file, so we should do so when deleting. */
+ /* Suppress jka-compr handling, etc. */
+ int count = SPECPDL_INDEX ();
+ specbind (intern ("file-name-handler-alist"), Qnil);
internal_delete_file (name);
+ unbind_to (count, Qnil);
return Qnil;
}
int count1 = SPECPDL_INDEX ();
specbind (intern ("coding-system-for-write"), val);
+ /* POSIX lets mk[s]temp use "."; don't invoke jka-compr if we
+ happen to get a ".Z" suffix. */
+ specbind (intern ("file-name-handler-alist"), Qnil);
Fwrite_region (start, end, filename_string, Qnil, Qlambda, Qnil, Qnil);
unbind_to (count1, Qnil);