aboutsummaryrefslogtreecommitdiffstats
path: root/src/process.c
diff options
context:
space:
mode:
authorKenichi Handa2000-08-04 02:26:32 +0000
committerKenichi Handa2000-08-04 02:26:32 +0000
commited7a4b2d60dc9806112af7a1a69e8b81bed5a8c2 (patch)
treefab9ea557f0ca50dd1c5619a4213356dcd6030c3 /src/process.c
parentf778b157f43c349e17cb337a034fa725b5ff02ec (diff)
downloademacs-ed7a4b2d60dc9806112af7a1a69e8b81bed5a8c2.tar.gz
emacs-ed7a4b2d60dc9806112af7a1a69e8b81bed5a8c2.zip
(read_process_output): Big simplification. Handle
composition and post-read-conversion of coding system correctly. (send_process): Handle composition correctly.
Diffstat (limited to 'src/process.c')
-rw-r--r--src/process.c298
1 files changed, 89 insertions, 209 deletions
diff --git a/src/process.c b/src/process.c
index 26f3f5e429a..ce6ea82cffa 100644
--- a/src/process.c
+++ b/src/process.c
@@ -2840,20 +2840,12 @@ read_process_output (proc, channel)
2840{ 2840{
2841 register int nchars, nbytes; 2841 register int nchars, nbytes;
2842 char *chars; 2842 char *chars;
2843#ifdef VMS
2844 int chars_allocated = 0; /* If 1, `chars' should be freed later. */
2845#else
2846 char buf[1024];
2847#endif
2848 register Lisp_Object outstream; 2843 register Lisp_Object outstream;
2849 register struct buffer *old = current_buffer; 2844 register struct buffer *old = current_buffer;
2850 register struct Lisp_Process *p = XPROCESS (proc); 2845 register struct Lisp_Process *p = XPROCESS (proc);
2851 register int opoint; 2846 register int opoint;
2852 struct coding_system *coding = proc_decode_coding_system[channel]; 2847 struct coding_system *coding = proc_decode_coding_system[channel];
2853 int chars_in_decoding_buf = 0; /* If 1, `chars' points
2854 XSTRING (p->decoding_buf)->data. */
2855 int carryover = XINT (p->decoding_carryover); 2848 int carryover = XINT (p->decoding_carryover);
2856 int require_decoding;
2857 2849
2858#ifdef VMS 2850#ifdef VMS
2859 VMS_PROC_STUFF *vs, *get_vms_process_pointer(); 2851 VMS_PROC_STUFF *vs, *get_vms_process_pointer();
@@ -2880,42 +2872,33 @@ read_process_output (proc, channel)
2880 /* The data carried over in the previous decoding (which are at 2872 /* The data carried over in the previous decoding (which are at
2881 the tail of decoding buffer) should be prepended to the new 2873 the tail of decoding buffer) should be prepended to the new
2882 data read to decode all together. */ 2874 data read to decode all together. */
2883 char *buf = (char *) xmalloc (nbytes + carryover); 2875 chars = (char *) alloca (nbytes + carryover);
2884 2876 bcopy (XSTRING (p->decoding_buf)->data, buf, carryover);
2885 bcopy (XSTRING (p->decoding_buf)->data 2877 bcopy (vs->inputBuffer, chars + carryover, nbytes);
2886 + STRING_BYTES (XSTRING (p->decoding_buf)) - carryover,
2887 buf, carryover);
2888 bcopy (chars, buf + carryover, nbytes);
2889 chars = buf;
2890 chars_allocated = 1;
2891 } 2878 }
2892#else /* not VMS */ 2879#else /* not VMS */
2893 2880 chars = (char *) alloca (carryover + 1024);
2894 if (carryover) 2881 if (carryover)
2895 /* See the comment above. */ 2882 /* See the comment above. */
2896 bcopy (XSTRING (p->decoding_buf)->data 2883 bcopy (XSTRING (p->decoding_buf)->data, chars, carryover);
2897 + STRING_BYTES (XSTRING (p->decoding_buf)) - carryover,
2898 buf, carryover);
2899 2884
2900 if (proc_buffered_char[channel] < 0) 2885 if (proc_buffered_char[channel] < 0)
2901 nbytes = emacs_read (channel, buf + carryover, (sizeof buf) - carryover); 2886 nbytes = emacs_read (channel, chars + carryover, 1024 - carryover);
2902 else 2887 else
2903 { 2888 {
2904 buf[carryover] = proc_buffered_char[channel]; 2889 chars[carryover] = proc_buffered_char[channel];
2905 proc_buffered_char[channel] = -1; 2890 proc_buffered_char[channel] = -1;
2906 nbytes = emacs_read (channel, buf + carryover + 1, 2891 nbytes = emacs_read (channel, chars + carryover + 1, 1023 - carryover);
2907 (sizeof buf) - carryover - 1);
2908 if (nbytes < 0) 2892 if (nbytes < 0)
2909 nbytes = 1; 2893 nbytes = 1;
2910 else 2894 else
2911 nbytes = nbytes + 1; 2895 nbytes = nbytes + 1;
2912 } 2896 }
2913 chars = buf;
2914#endif /* not VMS */ 2897#endif /* not VMS */
2915 2898
2916 XSETINT (p->decoding_carryover, 0); 2899 XSETINT (p->decoding_carryover, 0);
2917 2900
2918 /* At this point, NBYTES holds number of characters just received 2901 /* At this point, NBYTES holds number of bytes just received
2919 (including the one in proc_buffered_char[channel]). */ 2902 (including the one in proc_buffered_char[channel]). */
2920 if (nbytes <= 0) 2903 if (nbytes <= 0)
2921 { 2904 {
@@ -2927,123 +2910,6 @@ read_process_output (proc, channel)
2927 /* Now set NBYTES how many bytes we must decode. */ 2910 /* Now set NBYTES how many bytes we must decode. */
2928 nbytes += carryover; 2911 nbytes += carryover;
2929 2912
2930 require_decoding = 1;
2931 coding->src_multibyte = 0;
2932 /* Decide the multibyteness of the decoded text. */
2933 if (!NILP (p->filter))
2934 /* We make a string given to the process filter. The
2935 multibyteness is decided by which coding system we use for
2936 decoding. */
2937 coding->dst_multibyte = (coding->type != coding_type_no_conversion
2938 && coding->type != coding_type_raw_text);
2939 else if (!NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name))
2940 /* The decoded text is inserted in a buffer. The multibyteness is
2941 decided by that of the buffer. */
2942 coding->dst_multibyte
2943 = !NILP (XBUFFER (p->buffer)->enable_multibyte_characters);
2944 else
2945 /* We can discard the source, thus no need of decoding. */
2946 require_decoding = 0;
2947
2948 if (require_decoding
2949 || CODING_MAY_REQUIRE_DECODING (coding))
2950 {
2951 int require = decoding_buffer_size (coding, nbytes);
2952 int dst_bytes = STRING_BYTES (XSTRING (p->decoding_buf));
2953 int result;
2954
2955 if (dst_bytes < require)
2956 p->decoding_buf = make_uninit_string (require), dst_bytes = require;
2957 result = decode_coding (coding, chars, XSTRING (p->decoding_buf)->data,
2958 nbytes, dst_bytes);
2959 carryover = nbytes - coding->consumed;
2960 if (carryover > 0)
2961 {
2962 /* Copy the carryover bytes to the end of p->decoding_buf, to
2963 be processed on the next read. Since decoding_buffer_size
2964 asks for an extra amount of space beyond the maximum
2965 expected for the output, there should always be sufficient
2966 space for the carryover (which is by definition a sequence
2967 of bytes that was not long enough to be decoded, and thus
2968 has a bounded length). */
2969 if (dst_bytes < coding->produced + carryover)
2970 abort ();
2971 bcopy (chars + coding->consumed,
2972 XSTRING (p->decoding_buf)->data + dst_bytes - carryover,
2973 carryover);
2974 XSETINT (p->decoding_carryover, carryover);
2975 }
2976
2977 /* A new coding system might be found by `decode_coding'. */
2978 if (!EQ (p->decode_coding_system, coding->symbol))
2979 {
2980 p->decode_coding_system = coding->symbol;
2981
2982 /* Don't call setup_coding_system for
2983 proc_decode_coding_system[channel] here. It is done in
2984 detect_coding called via decode_coding above. */
2985
2986 /* If a coding system for encoding is not yet decided, we set
2987 it as the same as coding-system for decoding.
2988
2989 But, before doing that we must check if
2990 proc_encode_coding_system[p->outfd] surely points to a
2991 valid memory because p->outfd will be changed once EOF is
2992 sent to the process. */
2993 if (NILP (p->encode_coding_system)
2994 && proc_encode_coding_system[XINT (p->outfd)])
2995 {
2996 p->encode_coding_system = coding->symbol;
2997 setup_coding_system (coding->symbol,
2998 proc_encode_coding_system[XINT (p->outfd)]);
2999 }
3000 }
3001
3002#ifdef VMS
3003 /* Now we don't need the contents of `chars'. */
3004 if (chars_allocated)
3005 xfree (chars);
3006#endif
3007 if (coding->produced == 0)
3008 return 0;
3009 chars = (char *) XSTRING (p->decoding_buf)->data;
3010 nbytes = coding->produced;
3011 nchars = coding->produced_char;
3012 chars_in_decoding_buf = 1;
3013 }
3014 else
3015 {
3016#ifdef VMS
3017 if (chars_allocated)
3018 {
3019 /* Although we don't have to decode the received data, we
3020 must move it to an area which we don't have to free. */
3021 if (! STRINGP (p->decoding_buf)
3022 || STRING_BYTES (XSTRING (p->decoding_buf)) < nbytes)
3023 p->decoding_buf = make_uninit_string (nbytes);
3024 bcopy (chars, XSTRING (p->decoding_buf)->data, nbytes);
3025 free (chars);
3026 chars_in_decoding_buf = 1;
3027 }
3028#endif
3029 nchars = nbytes;
3030 }
3031
3032 Vlast_coding_system_used = coding->symbol;
3033
3034 /* If the caller required, let the process associated buffer
3035 inherit the coding-system used to decode the process output. */
3036 if (! NILP (p->inherit_coding_system_flag)
3037 && !NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name))
3038 {
3039 struct buffer *prev_buf = current_buffer;
3040
3041 Fset_buffer (p->buffer);
3042 call1 (intern ("after-insert-file-set-buffer-file-coding-system"),
3043 make_number (nbytes));
3044 set_buffer_internal (prev_buf);
3045 }
3046
3047 /* Read and dispose of the process output. */ 2913 /* Read and dispose of the process output. */
3048 outstream = p->filter; 2914 outstream = p->filter;
3049 if (!NILP (outstream)) 2915 if (!NILP (outstream))
@@ -3084,13 +2950,39 @@ read_process_output (proc, channel)
3084 save the match data in a special nonrecursive fashion. */ 2950 save the match data in a special nonrecursive fashion. */
3085 running_asynch_code = 1; 2951 running_asynch_code = 1;
3086 2952
3087 /* The multibyteness of a string given to the filter is decided 2953 text = decode_coding_string (make_unibyte_string (chars, nbytes),
3088 by which coding system we used for decoding. */ 2954 coding, 0);
3089 if (coding->dst_multibyte) 2955 /* A new coding system might be found. */
3090 text = make_multibyte_string (chars, nchars, nbytes); 2956 if (!EQ (p->decode_coding_system, coding->symbol))
3091 else 2957 {
3092 text = make_unibyte_string (chars, nbytes); 2958 p->decode_coding_system = coding->symbol;
2959
2960 /* Don't call setup_coding_system for
2961 proc_decode_coding_system[channel] here. It is done in
2962 detect_coding called via decode_coding above. */
2963
2964 /* If a coding system for encoding is not yet decided, we set
2965 it as the same as coding-system for decoding.
2966
2967 But, before doing that we must check if
2968 proc_encode_coding_system[p->outfd] surely points to a
2969 valid memory because p->outfd will be changed once EOF is
2970 sent to the process. */
2971 if (NILP (p->encode_coding_system)
2972 && proc_encode_coding_system[XINT (p->outfd)])
2973 {
2974 p->encode_coding_system = coding->symbol;
2975 setup_coding_system (coding->symbol,
2976 proc_encode_coding_system[XINT (p->outfd)]);
2977 }
2978 }
3093 2979
2980 carryover = nbytes - coding->consumed;
2981 bcopy (chars + coding->consumed, XSTRING (p->decoding_buf)->data,
2982 carryover);
2983 XSETINT (p->decoding_carryover, carryover);
2984 nbytes = STRING_BYTES (XSTRING (text));
2985 nchars = XSTRING (text)->size;
3094 internal_condition_case_1 (read_process_output_call, 2986 internal_condition_case_1 (read_process_output_call,
3095 Fcons (outstream, 2987 Fcons (outstream,
3096 Fcons (proc, Fcons (text, Qnil))), 2988 Fcons (proc, Fcons (text, Qnil))),
@@ -3136,6 +3028,7 @@ read_process_output (proc, channel)
3136 Lisp_Object odeactivate; 3028 Lisp_Object odeactivate;
3137 int before, before_byte; 3029 int before, before_byte;
3138 int opoint_byte; 3030 int opoint_byte;
3031 Lisp_Object text;
3139 3032
3140 odeactivate = Vdeactivate_mark; 3033 odeactivate = Vdeactivate_mark;
3141 3034
@@ -3167,17 +3060,30 @@ read_process_output (proc, channel)
3167 if (! (BEGV <= PT && PT <= ZV)) 3060 if (! (BEGV <= PT && PT <= ZV))
3168 Fwiden (); 3061 Fwiden ();
3169 3062
3170 /* If the text to insert is in decoding buffer (Lisp String), we
3171 must move it to a relocation-free memory space. */
3172 if (chars_in_decoding_buf)
3173 {
3174 chars = (char *) alloca (nbytes);
3175 bcopy (XSTRING (p->decoding_buf)->data, chars, nbytes);
3176 }
3177
3178 /* Insert before markers in case we are inserting where 3063 /* Insert before markers in case we are inserting where
3179 the buffer's mark is, and the user's next command is Meta-y. */ 3064 the buffer's mark is, and the user's next command is Meta-y. */
3180 insert_1_both (chars, nchars, nbytes, 0, 1, 1); 3065 text = decode_coding_string (make_unibyte_string (chars, nbytes),
3066 coding, 0);
3067 /* A new coding system might be found. See the comment in the
3068 similar code in the previous `if' block. */
3069 if (!EQ (p->decode_coding_system, coding->symbol))
3070 {
3071 p->decode_coding_system = coding->symbol;
3072 if (NILP (p->encode_coding_system)
3073 && proc_encode_coding_system[XINT (p->outfd)])
3074 {
3075 p->encode_coding_system = coding->symbol;
3076 setup_coding_system (coding->symbol,
3077 proc_encode_coding_system[XINT (p->outfd)]);
3078 }
3079 }
3080 carryover = nbytes - coding->consumed;
3081 bcopy (chars + coding->consumed, XSTRING (p->decoding_buf)->data,
3082 carryover);
3083 XSETINT (p->decoding_carryover, carryover);
3084 nbytes = STRING_BYTES (XSTRING (text));
3085 nchars = XSTRING (text)->size;
3086 insert_from_string_before_markers (text, 0, 0, nchars, nbytes, 0);
3181 signal_after_change (before, 0, PT - before); 3087 signal_after_change (before, 0, PT - before);
3182 update_compositions (before, PT, CHECK_BORDER); 3088 update_compositions (before, PT, CHECK_BORDER);
3183 3089
@@ -3266,8 +3172,6 @@ send_process (proc, buf, len, object)
3266 int rv; 3172 int rv;
3267 struct coding_system *coding; 3173 struct coding_system *coding;
3268 struct gcpro gcpro1; 3174 struct gcpro gcpro1;
3269 int carryover = XINT (XPROCESS (proc)->encoding_carryover);
3270 int require_encoding;
3271 3175
3272 GCPRO1 (object); 3176 GCPRO1 (object);
3273 3177
@@ -3288,66 +3192,42 @@ send_process (proc, buf, len, object)
3288 coding = proc_encode_coding_system[XINT (XPROCESS (proc)->outfd)]; 3192 coding = proc_encode_coding_system[XINT (XPROCESS (proc)->outfd)];
3289 Vlast_coding_system_used = coding->symbol; 3193 Vlast_coding_system_used = coding->symbol;
3290 3194
3291 require_encoding = 0; 3195 if ((STRINGP (object) && STRING_MULTIBYTE (object))
3292 if (STRINGP (object) && STRING_MULTIBYTE (object)) 3196 || (BUFFERP (object)
3293 coding->src_multibyte = require_encoding = 1; 3197 && !NILP (XBUFFER (object)->enable_multibyte_characters)))
3294 else if (BUFFERP (object) 3198 coding->src_multibyte = 1;
3295 && !NILP (XBUFFER (object)->enable_multibyte_characters))
3296 coding->src_multibyte = require_encoding = 1;
3297 else
3298 require_encoding = 0;
3299 coding->dst_multibyte = 0; 3199 coding->dst_multibyte = 0;
3300 3200
3301 if (require_encoding 3201 if (CODING_REQUIRE_ENCODING (coding))
3302 || CODING_REQUIRE_ENCODING (coding))
3303 { 3202 {
3304 int require = encoding_buffer_size (coding, len); 3203 int require = encoding_buffer_size (coding, len);
3305 int offset; 3204 int from_byte = -1, from, to;
3306 unsigned char *temp_buf = NULL; 3205 unsigned char *temp_buf = NULL;
3307 3206
3308 /* Remember the offset of data because a string or a buffer may 3207 if (BUFFERP (object))
3309 be relocated. Setting OFFSET to -1 means we don't have to
3310 care about relocation. */
3311 offset = (BUFFERP (object)
3312 ? BUF_PTR_BYTE_POS (XBUFFER (object), buf)
3313 : (STRINGP (object)
3314 ? buf - XSTRING (object)->data
3315 : -1));
3316
3317 if (carryover > 0)
3318 { 3208 {
3319 temp_buf = (unsigned char *) xmalloc (len + carryover); 3209 from_byte = BUF_PTR_BYTE_POS (XBUFFER (object), buf);
3320 3210 from = buf_bytepos_to_charpos (XBUFFER (object), from_byte);
3321 if (offset >= 0) 3211 to = buf_bytepos_to_charpos (XBUFFER (object), from_byte + len);
3322 { 3212 }
3323 if (BUFFERP (object)) 3213 else if (STRINGP (object))
3324 buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset); 3214 {
3325 else if (STRINGP (object)) 3215 from_byte = buf - XSTRING (object)->data;
3326 buf = offset + XSTRING (object)->data; 3216 from = string_byte_to_char (object, from_byte);
3327 /* Now we don't have to care about relocation. */ 3217 to = string_byte_to_char (object, from_byte + len);
3328 offset = -1;
3329 }
3330 bcopy ((XSTRING (XPROCESS (proc)->encoding_buf)->data
3331 + STRING_BYTES (XSTRING (XPROCESS (proc)->encoding_buf))
3332 - carryover),
3333 temp_buf,
3334 carryover);
3335 bcopy (buf, temp_buf + carryover, len);
3336 buf = temp_buf;
3337 } 3218 }
3338 3219
3220 if (from_byte >= 0 && coding->composing != COMPOSITION_DISABLED)
3221 coding_save_composition (coding, from, to, object);
3222
3339 if (STRING_BYTES (XSTRING (XPROCESS (proc)->encoding_buf)) < require) 3223 if (STRING_BYTES (XSTRING (XPROCESS (proc)->encoding_buf)) < require)
3340 { 3224 XPROCESS (proc)->encoding_buf = make_uninit_string (require);
3341 XPROCESS (proc)->encoding_buf = make_uninit_string (require); 3225
3226 if (from_byte >= 0)
3227 buf = (BUFFERP (object)
3228 ? BUF_BYTE_ADDRESS (XBUFFER (object), from_byte)
3229 : XSTRING (object)->data + from_byte);
3342 3230
3343 if (offset >= 0)
3344 {
3345 if (BUFFERP (object))
3346 buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
3347 else if (STRINGP (object))
3348 buf = offset + XSTRING (object)->data;
3349 }
3350 }
3351 object = XPROCESS (proc)->encoding_buf; 3231 object = XPROCESS (proc)->encoding_buf;
3352 encode_coding (coding, buf, XSTRING (object)->data, 3232 encode_coding (coding, buf, XSTRING (object)->data,
3353 len, STRING_BYTES (XSTRING (object))); 3233 len, STRING_BYTES (XSTRING (object)));