comparison src/process.c @ 30580:5d92193ed196

(read_process_output): Big simplification. Handle composition and post-read-conversion of coding system correctly. (send_process): Handle composition correctly.
author Kenichi Handa <handa@m17n.org>
date Fri, 04 Aug 2000 02:26:32 +0000
parents 8a4552214d84
children 280157ad4aa3
comparison
equal deleted inserted replaced
30579:13339fe0bdc0 30580:5d92193ed196
2838 Lisp_Object proc; 2838 Lisp_Object proc;
2839 register int channel; 2839 register int 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();
2860 2852
2861 vs = get_vms_process_pointer (p->pid); 2853 vs = get_vms_process_pointer (p->pid);
2878 if (carryover > 0) 2870 if (carryover > 0)
2879 { 2871 {
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 {
2922 if (nbytes < 0 || coding->mode & CODING_MODE_LAST_BLOCK) 2905 if (nbytes < 0 || coding->mode & CODING_MODE_LAST_BLOCK)
2923 return nbytes; 2906 return nbytes;
2925 } 2908 }
2926 2909
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; 2913 /* Read and dispose of the process output. */
2931 coding->src_multibyte = 0; 2914 outstream = p->filter;
2932 /* Decide the multibyteness of the decoded text. */ 2915 if (!NILP (outstream))
2933 if (!NILP (p->filter)) 2916 {
2934 /* We make a string given to the process filter. The 2917 /* We inhibit quit here instead of just catching it so that
2935 multibyteness is decided by which coding system we use for 2918 hitting ^G when a filter happens to be running won't screw
2936 decoding. */ 2919 it up. */
2937 coding->dst_multibyte = (coding->type != coding_type_no_conversion 2920 int count = specpdl_ptr - specpdl;
2938 && coding->type != coding_type_raw_text); 2921 Lisp_Object odeactivate;
2939 else if (!NILP (p->buffer) && !NILP (XBUFFER (p->buffer)->name)) 2922 Lisp_Object obuffer, okeymap;
2940 /* The decoded text is inserted in a buffer. The multibyteness is 2923 Lisp_Object text;
2941 decided by that of the buffer. */ 2924 int outer_running_asynch_code = running_asynch_code;
2942 coding->dst_multibyte 2925 int waiting = waiting_for_user_input_p;
2943 = !NILP (XBUFFER (p->buffer)->enable_multibyte_characters); 2926
2944 else 2927 /* No need to gcpro these, because all we do with them later
2945 /* We can discard the source, thus no need of decoding. */ 2928 is test them for EQness, and none of them should be a string. */
2946 require_decoding = 0; 2929 odeactivate = Vdeactivate_mark;
2947 2930 XSETBUFFER (obuffer, current_buffer);
2948 if (require_decoding 2931 okeymap = current_buffer->keymap;
2949 || CODING_MAY_REQUIRE_DECODING (coding)) 2932
2950 { 2933 specbind (Qinhibit_quit, Qt);
2951 int require = decoding_buffer_size (coding, nbytes); 2934 specbind (Qlast_nonmenu_event, Qt);
2952 int dst_bytes = STRING_BYTES (XSTRING (p->decoding_buf)); 2935
2953 int result; 2936 /* In case we get recursively called,
2954 2937 and we already saved the match data nonrecursively,
2955 if (dst_bytes < require) 2938 save the same match data in safely recursive fashion. */
2956 p->decoding_buf = make_uninit_string (require), dst_bytes = require; 2939 if (outer_running_asynch_code)
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 { 2940 {
2962 /* Copy the carryover bytes to the end of p->decoding_buf, to 2941 Lisp_Object tem;
2963 be processed on the next read. Since decoding_buffer_size 2942 /* Don't clobber the CURRENT match data, either! */
2964 asks for an extra amount of space beyond the maximum 2943 tem = Fmatch_data (Qnil, Qnil);
2965 expected for the output, there should always be sufficient 2944 restore_match_data ();
2966 space for the carryover (which is by definition a sequence 2945 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
2967 of bytes that was not long enough to be decoded, and thus 2946 Fset_match_data (tem);
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 } 2947 }
2976 2948
2977 /* A new coding system might be found by `decode_coding'. */ 2949 /* For speed, if a search happens within this code,
2950 save the match data in a special nonrecursive fashion. */
2951 running_asynch_code = 1;
2952
2953 text = decode_coding_string (make_unibyte_string (chars, nbytes),
2954 coding, 0);
2955 /* A new coding system might be found. */
2978 if (!EQ (p->decode_coding_system, coding->symbol)) 2956 if (!EQ (p->decode_coding_system, coding->symbol))
2979 { 2957 {
2980 p->decode_coding_system = coding->symbol; 2958 p->decode_coding_system = coding->symbol;
2981 2959
2982 /* Don't call setup_coding_system for 2960 /* Don't call setup_coding_system for
2983 proc_decode_coding_system[channel] here. It is done in 2961 proc_decode_coding_system[channel] here. It is done in
2984 detect_coding called via decode_coding above. */ 2962 detect_coding called via decode_coding above. */
2985 2963
2986 /* If a coding system for encoding is not yet decided, we set 2964 /* If a coding system for encoding is not yet decided, we set
2987 it as the same as coding-system for decoding. 2965 it as the same as coding-system for decoding.
2988 2966
2989 But, before doing that we must check if 2967 But, before doing that we must check if
2997 setup_coding_system (coding->symbol, 2975 setup_coding_system (coding->symbol,
2998 proc_encode_coding_system[XINT (p->outfd)]); 2976 proc_encode_coding_system[XINT (p->outfd)]);
2999 } 2977 }
3000 } 2978 }
3001 2979
3002 #ifdef VMS 2980 carryover = nbytes - coding->consumed;
3003 /* Now we don't need the contents of `chars'. */ 2981 bcopy (chars + coding->consumed, XSTRING (p->decoding_buf)->data,
3004 if (chars_allocated) 2982 carryover);
3005 xfree (chars); 2983 XSETINT (p->decoding_carryover, carryover);
3006 #endif 2984 nbytes = STRING_BYTES (XSTRING (text));
3007 if (coding->produced == 0) 2985 nchars = XSTRING (text)->size;
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. */
3048 outstream = p->filter;
3049 if (!NILP (outstream))
3050 {
3051 /* We inhibit quit here instead of just catching it so that
3052 hitting ^G when a filter happens to be running won't screw
3053 it up. */
3054 int count = specpdl_ptr - specpdl;
3055 Lisp_Object odeactivate;
3056 Lisp_Object obuffer, okeymap;
3057 Lisp_Object text;
3058 int outer_running_asynch_code = running_asynch_code;
3059 int waiting = waiting_for_user_input_p;
3060
3061 /* No need to gcpro these, because all we do with them later
3062 is test them for EQness, and none of them should be a string. */
3063 odeactivate = Vdeactivate_mark;
3064 XSETBUFFER (obuffer, current_buffer);
3065 okeymap = current_buffer->keymap;
3066
3067 specbind (Qinhibit_quit, Qt);
3068 specbind (Qlast_nonmenu_event, Qt);
3069
3070 /* In case we get recursively called,
3071 and we already saved the match data nonrecursively,
3072 save the same match data in safely recursive fashion. */
3073 if (outer_running_asynch_code)
3074 {
3075 Lisp_Object tem;
3076 /* Don't clobber the CURRENT match data, either! */
3077 tem = Fmatch_data (Qnil, Qnil);
3078 restore_match_data ();
3079 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
3080 Fset_match_data (tem);
3081 }
3082
3083 /* For speed, if a search happens within this code,
3084 save the match data in a special nonrecursive fashion. */
3085 running_asynch_code = 1;
3086
3087 /* The multibyteness of a string given to the filter is decided
3088 by which coding system we used for decoding. */
3089 if (coding->dst_multibyte)
3090 text = make_multibyte_string (chars, nchars, nbytes);
3091 else
3092 text = make_unibyte_string (chars, nbytes);
3093
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))),
3097 !NILP (Vdebug_on_error) ? Qnil : Qerror, 2989 !NILP (Vdebug_on_error) ? Qnil : Qerror,
3098 read_process_output_error_handler); 2990 read_process_output_error_handler);
3134 int old_begv, old_zv; 3026 int old_begv, old_zv;
3135 int old_begv_byte, old_zv_byte; 3027 int old_begv_byte, old_zv_byte;
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
3142 Fset_buffer (p->buffer); 3035 Fset_buffer (p->buffer);
3143 opoint = PT; 3036 opoint = PT;
3165 /* If the output marker is outside of the visible region, save 3058 /* If the output marker is outside of the visible region, save
3166 the restriction and widen. */ 3059 the restriction and widen. */
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
3184 set_marker_both (p->mark, p->buffer, PT, PT_BYTE); 3090 set_marker_both (p->mark, p->buffer, PT, PT_BYTE);
3185 3091
3264 { 3170 {
3265 /* Use volatile to protect variables from being clobbered by longjmp. */ 3171 /* Use volatile to protect variables from being clobbered by longjmp. */
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
3274 #ifdef VMS 3178 #ifdef VMS
3275 struct Lisp_Process *p = XPROCESS (proc); 3179 struct Lisp_Process *p = XPROCESS (proc);
3286 XSTRING (XPROCESS (proc)->name)->data); 3190 XSTRING (XPROCESS (proc)->name)->data);
3287 3191
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 {
3323 if (BUFFERP (object))
3324 buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset);
3325 else if (STRINGP (object))
3326 buf = offset + XSTRING (object)->data;
3327 /* Now we don't have to care about relocation. */
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 } 3212 }
3213 else if (STRINGP (object))
3214 {
3215 from_byte = buf - XSTRING (object)->data;
3216 from = string_byte_to_char (object, from_byte);
3217 to = string_byte_to_char (object, from_byte + len);
3218 }
3219
3220 if (from_byte >= 0 && coding->composing != COMPOSITION_DISABLED)
3221 coding_save_composition (coding, from, to, object);
3338 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
3342 3226 if (from_byte >= 0)
3343 if (offset >= 0) 3227 buf = (BUFFERP (object)
3344 { 3228 ? BUF_BYTE_ADDRESS (XBUFFER (object), from_byte)
3345 if (BUFFERP (object)) 3229 : XSTRING (object)->data + from_byte);
3346 buf = BUF_BYTE_ADDRESS (XBUFFER (object), offset); 3230
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)));
3354 len = coding->produced; 3234 len = coding->produced;
3355 buf = XSTRING (object)->data; 3235 buf = XSTRING (object)->data;