read_process_output_set_last_coding_system: Extract, reuse

* src/process.c (read_process_output_set_last_coding_system):
New function, extracted from read_and_dispose_of_process_output.
(read_and_dispose_of_process_output): Update accordingly.
(read_and_insert_process_output): Use it here instead of just
transferring carryover (bug#66020, also mentioned in bug#71452).
This commit is contained in:
Dmitry Gutov 2024-06-11 05:36:51 +03:00
parent f33806dd66
commit d9890bb870

View file

@ -6344,6 +6344,48 @@ read_process_output_after_insert (struct Lisp_Process *p, Lisp_Object *old_read_
SET_PT_BOTH (opoint, opoint_byte);
}
static void
read_process_output_set_last_coding_system (struct Lisp_Process *p,
struct coding_system *coding)
{
Vlast_coding_system_used = CODING_ID_NAME (coding->id);
/* A new coding system might be found. */
if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
{
pset_decode_coding_system (p, Vlast_coding_system_used);
/* Don't call setup_coding_system for
proc_decode_coding_system[channel] here. It is done in
detect_coding called via decode_coding above. */
/* If a coding system for encoding is not yet decided, we set
it as the same as coding-system for decoding.
But, before doing that we must check if
proc_encode_coding_system[p->outfd] surely points to a
valid memory because p->outfd will be changed once EOF is
sent to the process. */
eassert (p->outfd < FD_SETSIZE);
if (NILP (p->encode_coding_system) && p->outfd >= 0
&& proc_encode_coding_system[p->outfd])
{
pset_encode_coding_system
(p, coding_inherit_eol_type (Vlast_coding_system_used, Qnil));
setup_coding_system (p->encode_coding_system,
proc_encode_coding_system[p->outfd]);
}
}
if (coding->carryover_bytes > 0)
{
if (SCHARS (p->decoding_buf) < coding->carryover_bytes)
pset_decoding_buf (p, make_uninit_string (coding->carryover_bytes));
memcpy (SDATA (p->decoding_buf), coding->carryover,
coding->carryover_bytes);
p->decoding_carryover = coding->carryover_bytes;
}
}
static void
read_and_insert_process_output (struct Lisp_Process *p, char *buf,
ssize_t nread,
@ -6373,7 +6415,6 @@ read_and_insert_process_output (struct Lisp_Process *p, char *buf,
else
{ /* We have to decode the input. */
Lisp_Object curbuf;
int carryover = 0;
specpdl_ref count1 = SPECPDL_INDEX ();
XSETBUFFER (curbuf, current_buffer);
@ -6387,14 +6428,12 @@ read_and_insert_process_output (struct Lisp_Process *p, char *buf,
(unsigned char *) buf, nread, curbuf);
unbind_to (count1, Qnil);
read_process_output_set_last_coding_system (p, process_coding);
TEMP_SET_PT_BOTH (PT + process_coding->produced_char,
PT_BYTE + process_coding->produced);
signal_after_change (PT - process_coding->produced_char,
0, process_coding->produced_char);
carryover = process_coding->carryover_bytes;
if (carryover > 0)
memcpy (buf, process_coding->carryover,
process_coding->carryover_bytes);
}
read_process_output_after_insert (p, &old_read_only, old_begv, old_zv,
@ -6442,42 +6481,9 @@ read_and_dispose_of_process_output (struct Lisp_Process *p, char *chars,
decode_coding_c_string (coding, (unsigned char *) chars, nbytes, Qt);
text = coding->dst_object;
Vlast_coding_system_used = CODING_ID_NAME (coding->id);
/* A new coding system might be found. */
if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
{
pset_decode_coding_system (p, Vlast_coding_system_used);
/* Don't call setup_coding_system for
proc_decode_coding_system[channel] here. It is done in
detect_coding called via decode_coding above. */
read_process_output_set_last_coding_system (p, coding);
/* If a coding system for encoding is not yet decided, we set
it as the same as coding-system for decoding.
But, before doing that we must check if
proc_encode_coding_system[p->outfd] surely points to a
valid memory because p->outfd will be changed once EOF is
sent to the process. */
eassert (p->outfd < FD_SETSIZE);
if (NILP (p->encode_coding_system) && p->outfd >= 0
&& proc_encode_coding_system[p->outfd])
{
pset_encode_coding_system
(p, coding_inherit_eol_type (Vlast_coding_system_used, Qnil));
setup_coding_system (p->encode_coding_system,
proc_encode_coding_system[p->outfd]);
}
}
if (coding->carryover_bytes > 0)
{
if (SCHARS (p->decoding_buf) < coding->carryover_bytes)
pset_decoding_buf (p, make_uninit_string (coding->carryover_bytes));
memcpy (SDATA (p->decoding_buf), coding->carryover,
coding->carryover_bytes);
p->decoding_carryover = coding->carryover_bytes;
}
if (SBYTES (text) > 0)
/* FIXME: It's wrong to wrap or not based on debug-on-error, and
sometimes it's simply wrong to wrap (e.g. when called from