428
|
1 /* Code conversion functions.
|
|
2 Copyright (C) 1991, 1995 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995 Sun Microsystems, Inc.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Mule 2.3. Not in FSF. */
|
|
23
|
|
24 /* Rewritten by Ben Wing <ben@xemacs.org>. */
|
|
25
|
|
26 #include <config.h>
|
|
27 #include "lisp.h"
|
|
28
|
|
29 #include "buffer.h"
|
|
30 #include "elhash.h"
|
|
31 #include "insdel.h"
|
|
32 #include "lstream.h"
|
440
|
33 #include "opaque.h"
|
428
|
34 #ifdef MULE
|
|
35 #include "mule-ccl.h"
|
|
36 #include "chartab.h"
|
|
37 #endif
|
|
38 #include "file-coding.h"
|
|
39
|
|
40 Lisp_Object Qcoding_system_error;
|
|
41
|
|
42 Lisp_Object Vkeyboard_coding_system;
|
|
43 Lisp_Object Vterminal_coding_system;
|
|
44 Lisp_Object Vcoding_system_for_read;
|
|
45 Lisp_Object Vcoding_system_for_write;
|
|
46 Lisp_Object Vfile_name_coding_system;
|
|
47
|
|
48 /* Table of symbols identifying each coding category. */
|
448
|
49 Lisp_Object coding_category_symbol[CODING_CATEGORY_LAST];
|
428
|
50
|
|
51
|
|
52
|
|
53 struct file_coding_dump {
|
|
54 /* Coding system currently associated with each coding category. */
|
448
|
55 Lisp_Object coding_category_system[CODING_CATEGORY_LAST];
|
428
|
56
|
|
57 /* Table of all coding categories in decreasing order of priority.
|
|
58 This describes a permutation of the possible coding categories. */
|
448
|
59 int coding_category_by_priority[CODING_CATEGORY_LAST];
|
428
|
60
|
440
|
61 #ifdef MULE
|
428
|
62 Lisp_Object ucs_to_mule_table[65536];
|
440
|
63 #endif
|
428
|
64 } *fcd;
|
|
65
|
|
66 static const struct lrecord_description fcd_description_1[] = {
|
448
|
67 { XD_LISP_OBJECT_ARRAY, offsetof (struct file_coding_dump, coding_category_system), CODING_CATEGORY_LAST },
|
440
|
68 #ifdef MULE
|
442
|
69 { XD_LISP_OBJECT_ARRAY, offsetof (struct file_coding_dump, ucs_to_mule_table), countof (fcd->ucs_to_mule_table) },
|
440
|
70 #endif
|
428
|
71 { XD_END }
|
|
72 };
|
|
73
|
|
74 static const struct struct_description fcd_description = {
|
440
|
75 sizeof (struct file_coding_dump),
|
428
|
76 fcd_description_1
|
|
77 };
|
|
78
|
|
79 Lisp_Object mule_to_ucs_table;
|
|
80
|
|
81 Lisp_Object Qcoding_systemp;
|
|
82
|
|
83 Lisp_Object Qraw_text, Qno_conversion, Qccl, Qiso2022;
|
|
84 /* Qinternal in general.c */
|
|
85
|
|
86 Lisp_Object Qmnemonic, Qeol_type;
|
|
87 Lisp_Object Qcr, Qcrlf, Qlf;
|
|
88 Lisp_Object Qeol_cr, Qeol_crlf, Qeol_lf;
|
|
89 Lisp_Object Qpost_read_conversion;
|
|
90 Lisp_Object Qpre_write_conversion;
|
|
91
|
|
92 #ifdef MULE
|
|
93 Lisp_Object Qucs4, Qutf8;
|
|
94 Lisp_Object Qbig5, Qshift_jis;
|
|
95 Lisp_Object Qcharset_g0, Qcharset_g1, Qcharset_g2, Qcharset_g3;
|
|
96 Lisp_Object Qforce_g0_on_output, Qforce_g1_on_output;
|
|
97 Lisp_Object Qforce_g2_on_output, Qforce_g3_on_output;
|
|
98 Lisp_Object Qno_iso6429;
|
|
99 Lisp_Object Qinput_charset_conversion, Qoutput_charset_conversion;
|
440
|
100 Lisp_Object Qescape_quoted;
|
428
|
101 Lisp_Object Qshort, Qno_ascii_eol, Qno_ascii_cntl, Qseven, Qlock_shift;
|
|
102 #endif
|
|
103 Lisp_Object Qencode, Qdecode;
|
|
104
|
|
105 Lisp_Object Vcoding_system_hash_table;
|
|
106
|
|
107 int enable_multibyte_characters;
|
|
108
|
|
109 #ifdef MULE
|
|
110 /* Additional information used by the ISO2022 decoder and detector. */
|
|
111 struct iso2022_decoder
|
|
112 {
|
|
113 /* CHARSET holds the character sets currently assigned to the G0
|
|
114 through G3 variables. It is initialized from the array
|
|
115 INITIAL_CHARSET in CODESYS. */
|
|
116 Lisp_Object charset[4];
|
|
117
|
|
118 /* Which registers are currently invoked into the left (GL) and
|
|
119 right (GR) halves of the 8-bit encoding space? */
|
|
120 int register_left, register_right;
|
|
121
|
|
122 /* ISO_ESC holds a value indicating part of an escape sequence
|
|
123 that has already been seen. */
|
|
124 enum iso_esc_flag esc;
|
|
125
|
|
126 /* This records the bytes we've seen so far in an escape sequence,
|
|
127 in case the sequence is invalid (we spit out the bytes unchanged). */
|
|
128 unsigned char esc_bytes[8];
|
|
129
|
|
130 /* Index for next byte to store in ISO escape sequence. */
|
|
131 int esc_bytes_index;
|
|
132
|
|
133 #ifdef ENABLE_COMPOSITE_CHARS
|
|
134 /* Stuff seen so far when composing a string. */
|
|
135 unsigned_char_dynarr *composite_chars;
|
|
136 #endif
|
|
137
|
|
138 /* If we saw an invalid designation sequence for a particular
|
|
139 register, we flag it here and switch to ASCII. The next time we
|
|
140 see a valid designation for this register, we turn off the flag
|
|
141 and do the designation normally, but pretend the sequence was
|
|
142 invalid. The effect of all this is that (most of the time) the
|
|
143 escape sequences for both the switch to the unknown charset, and
|
|
144 the switch back to the known charset, get inserted literally into
|
|
145 the buffer and saved out as such. The hope is that we can
|
|
146 preserve the escape sequences so that the resulting written out
|
|
147 file makes sense. If we don't do any of this, the designation
|
|
148 to the invalid charset will be preserved but that switch back
|
|
149 to the known charset will probably get eaten because it was
|
|
150 the same charset that was already present in the register. */
|
|
151 unsigned char invalid_designated[4];
|
|
152
|
|
153 /* We try to do similar things as above for direction-switching
|
|
154 sequences. If we encountered a direction switch while an
|
|
155 invalid designation was present, or an invalid designation
|
|
156 just after a direction switch (i.e. no valid designation
|
|
157 encountered yet), we insert the direction-switch escape
|
|
158 sequence literally into the output stream, and later on
|
|
159 insert the corresponding direction-restoring escape sequence
|
|
160 literally also. */
|
|
161 unsigned int switched_dir_and_no_valid_charset_yet :1;
|
|
162 unsigned int invalid_switch_dir :1;
|
|
163
|
|
164 /* Tells the decoder to output the escape sequence literally
|
|
165 even though it was valid. Used in the games we play to
|
|
166 avoid lossage when we encounter invalid designations. */
|
|
167 unsigned int output_literally :1;
|
|
168 /* We encountered a direction switch followed by an invalid
|
|
169 designation. We didn't output the direction switch
|
|
170 literally because we didn't know about the invalid designation;
|
|
171 but we have to do so now. */
|
|
172 unsigned int output_direction_sequence :1;
|
|
173 };
|
|
174 #endif /* MULE */
|
|
175 EXFUN (Fcopy_coding_system, 2);
|
|
176 #ifdef MULE
|
|
177 struct detection_state;
|
|
178 static int detect_coding_sjis (struct detection_state *st,
|
462
|
179 const Extbyte *src, Lstream_data_count n);
|
444
|
180 static void decode_coding_sjis (Lstream *decoding, const Extbyte *src,
|
462
|
181 unsigned_char_dynarr *dst, Lstream_data_count n);
|
444
|
182 static void encode_coding_sjis (Lstream *encoding, const Bufbyte *src,
|
462
|
183 unsigned_char_dynarr *dst, Lstream_data_count n);
|
428
|
184 static int detect_coding_big5 (struct detection_state *st,
|
462
|
185 const Extbyte *src, Lstream_data_count n);
|
444
|
186 static void decode_coding_big5 (Lstream *decoding, const Extbyte *src,
|
462
|
187 unsigned_char_dynarr *dst, Lstream_data_count n);
|
444
|
188 static void encode_coding_big5 (Lstream *encoding, const Bufbyte *src,
|
462
|
189 unsigned_char_dynarr *dst, Lstream_data_count n);
|
428
|
190 static int detect_coding_ucs4 (struct detection_state *st,
|
462
|
191 const Extbyte *src, Lstream_data_count n);
|
444
|
192 static void decode_coding_ucs4 (Lstream *decoding, const Extbyte *src,
|
462
|
193 unsigned_char_dynarr *dst, Lstream_data_count n);
|
444
|
194 static void encode_coding_ucs4 (Lstream *encoding, const Bufbyte *src,
|
462
|
195 unsigned_char_dynarr *dst, Lstream_data_count n);
|
428
|
196 static int detect_coding_utf8 (struct detection_state *st,
|
462
|
197 const Extbyte *src, Lstream_data_count n);
|
444
|
198 static void decode_coding_utf8 (Lstream *decoding, const Extbyte *src,
|
462
|
199 unsigned_char_dynarr *dst, Lstream_data_count n);
|
444
|
200 static void encode_coding_utf8 (Lstream *encoding, const Bufbyte *src,
|
462
|
201 unsigned_char_dynarr *dst, Lstream_data_count n);
|
428
|
202 static int postprocess_iso2022_mask (int mask);
|
|
203 static void reset_iso2022 (Lisp_Object coding_system,
|
|
204 struct iso2022_decoder *iso);
|
|
205 static int detect_coding_iso2022 (struct detection_state *st,
|
462
|
206 const Extbyte *src, Lstream_data_count n);
|
444
|
207 static void decode_coding_iso2022 (Lstream *decoding, const Extbyte *src,
|
462
|
208 unsigned_char_dynarr *dst, Lstream_data_count n);
|
444
|
209 static void encode_coding_iso2022 (Lstream *encoding, const Bufbyte *src,
|
462
|
210 unsigned_char_dynarr *dst, Lstream_data_count n);
|
428
|
211 #endif /* MULE */
|
444
|
212 static void decode_coding_no_conversion (Lstream *decoding, const Extbyte *src,
|
462
|
213 unsigned_char_dynarr *dst, Lstream_data_count n);
|
444
|
214 static void encode_coding_no_conversion (Lstream *encoding, const Bufbyte *src,
|
462
|
215 unsigned_char_dynarr *dst, Lstream_data_count n);
|
444
|
216 static void mule_decode (Lstream *decoding, const Extbyte *src,
|
462
|
217 unsigned_char_dynarr *dst, Lstream_data_count n);
|
444
|
218 static void mule_encode (Lstream *encoding, const Bufbyte *src,
|
462
|
219 unsigned_char_dynarr *dst, Lstream_data_count n);
|
428
|
220
|
|
221 typedef struct codesys_prop codesys_prop;
|
|
222 struct codesys_prop
|
|
223 {
|
|
224 Lisp_Object sym;
|
|
225 int prop_type;
|
|
226 };
|
|
227
|
|
228 typedef struct
|
|
229 {
|
|
230 Dynarr_declare (codesys_prop);
|
|
231 } codesys_prop_dynarr;
|
|
232
|
|
233 static const struct lrecord_description codesys_prop_description_1[] = {
|
440
|
234 { XD_LISP_OBJECT, offsetof (codesys_prop, sym) },
|
428
|
235 { XD_END }
|
|
236 };
|
|
237
|
|
238 static const struct struct_description codesys_prop_description = {
|
440
|
239 sizeof (codesys_prop),
|
428
|
240 codesys_prop_description_1
|
|
241 };
|
|
242
|
|
243 static const struct lrecord_description codesys_prop_dynarr_description_1[] = {
|
440
|
244 XD_DYNARR_DESC (codesys_prop_dynarr, &codesys_prop_description),
|
428
|
245 { XD_END }
|
|
246 };
|
|
247
|
|
248 static const struct struct_description codesys_prop_dynarr_description = {
|
440
|
249 sizeof (codesys_prop_dynarr),
|
428
|
250 codesys_prop_dynarr_description_1
|
|
251 };
|
|
252
|
|
253 codesys_prop_dynarr *the_codesys_prop_dynarr;
|
|
254
|
|
255 enum codesys_prop_enum
|
|
256 {
|
|
257 CODESYS_PROP_ALL_OK,
|
|
258 CODESYS_PROP_ISO2022,
|
|
259 CODESYS_PROP_CCL
|
|
260 };
|
|
261
|
|
262
|
|
263 /************************************************************************/
|
|
264 /* Coding system functions */
|
|
265 /************************************************************************/
|
|
266
|
|
267 static Lisp_Object mark_coding_system (Lisp_Object);
|
|
268 static void print_coding_system (Lisp_Object, Lisp_Object, int);
|
|
269 static void finalize_coding_system (void *header, int for_disksave);
|
|
270
|
|
271 #ifdef MULE
|
|
272 static const struct lrecord_description ccs_description_1[] = {
|
440
|
273 { XD_LISP_OBJECT, offsetof (charset_conversion_spec, from_charset) },
|
|
274 { XD_LISP_OBJECT, offsetof (charset_conversion_spec, to_charset) },
|
428
|
275 { XD_END }
|
|
276 };
|
|
277
|
|
278 static const struct struct_description ccs_description = {
|
440
|
279 sizeof (charset_conversion_spec),
|
428
|
280 ccs_description_1
|
|
281 };
|
|
282
|
|
283 static const struct lrecord_description ccsd_description_1[] = {
|
440
|
284 XD_DYNARR_DESC (charset_conversion_spec_dynarr, &ccs_description),
|
428
|
285 { XD_END }
|
|
286 };
|
|
287
|
|
288 static const struct struct_description ccsd_description = {
|
440
|
289 sizeof (charset_conversion_spec_dynarr),
|
428
|
290 ccsd_description_1
|
|
291 };
|
|
292 #endif
|
|
293
|
|
294 static const struct lrecord_description coding_system_description[] = {
|
440
|
295 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, name) },
|
|
296 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, doc_string) },
|
|
297 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, mnemonic) },
|
|
298 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, post_read_conversion) },
|
|
299 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, pre_write_conversion) },
|
|
300 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, eol_lf) },
|
|
301 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, eol_crlf) },
|
|
302 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, eol_cr) },
|
428
|
303 #ifdef MULE
|
440
|
304 { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Coding_System, iso2022.initial_charset), 4 },
|
|
305 { XD_STRUCT_PTR, offsetof (Lisp_Coding_System, iso2022.input_conv), 1, &ccsd_description },
|
|
306 { XD_STRUCT_PTR, offsetof (Lisp_Coding_System, iso2022.output_conv), 1, &ccsd_description },
|
|
307 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, ccl.decode) },
|
|
308 { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, ccl.encode) },
|
428
|
309 #endif
|
|
310 { XD_END }
|
|
311 };
|
|
312
|
|
313 DEFINE_LRECORD_IMPLEMENTATION ("coding-system", coding_system,
|
|
314 mark_coding_system, print_coding_system,
|
|
315 finalize_coding_system,
|
|
316 0, 0, coding_system_description,
|
440
|
317 Lisp_Coding_System);
|
428
|
318
|
|
319 static Lisp_Object
|
|
320 mark_coding_system (Lisp_Object obj)
|
|
321 {
|
|
322 Lisp_Coding_System *codesys = XCODING_SYSTEM (obj);
|
|
323
|
|
324 mark_object (CODING_SYSTEM_NAME (codesys));
|
|
325 mark_object (CODING_SYSTEM_DOC_STRING (codesys));
|
|
326 mark_object (CODING_SYSTEM_MNEMONIC (codesys));
|
|
327 mark_object (CODING_SYSTEM_EOL_LF (codesys));
|
|
328 mark_object (CODING_SYSTEM_EOL_CRLF (codesys));
|
|
329 mark_object (CODING_SYSTEM_EOL_CR (codesys));
|
|
330
|
|
331 switch (CODING_SYSTEM_TYPE (codesys))
|
|
332 {
|
|
333 #ifdef MULE
|
|
334 int i;
|
|
335 case CODESYS_ISO2022:
|
|
336 for (i = 0; i < 4; i++)
|
|
337 mark_object (CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i));
|
|
338 if (codesys->iso2022.input_conv)
|
|
339 {
|
|
340 for (i = 0; i < Dynarr_length (codesys->iso2022.input_conv); i++)
|
|
341 {
|
|
342 struct charset_conversion_spec *ccs =
|
|
343 Dynarr_atp (codesys->iso2022.input_conv, i);
|
|
344 mark_object (ccs->from_charset);
|
|
345 mark_object (ccs->to_charset);
|
|
346 }
|
|
347 }
|
|
348 if (codesys->iso2022.output_conv)
|
|
349 {
|
|
350 for (i = 0; i < Dynarr_length (codesys->iso2022.output_conv); i++)
|
|
351 {
|
|
352 struct charset_conversion_spec *ccs =
|
|
353 Dynarr_atp (codesys->iso2022.output_conv, i);
|
|
354 mark_object (ccs->from_charset);
|
|
355 mark_object (ccs->to_charset);
|
|
356 }
|
|
357 }
|
|
358 break;
|
|
359
|
|
360 case CODESYS_CCL:
|
|
361 mark_object (CODING_SYSTEM_CCL_DECODE (codesys));
|
|
362 mark_object (CODING_SYSTEM_CCL_ENCODE (codesys));
|
|
363 break;
|
|
364 #endif /* MULE */
|
|
365 default:
|
|
366 break;
|
|
367 }
|
|
368
|
|
369 mark_object (CODING_SYSTEM_PRE_WRITE_CONVERSION (codesys));
|
|
370 return CODING_SYSTEM_POST_READ_CONVERSION (codesys);
|
|
371 }
|
|
372
|
|
373 static void
|
|
374 print_coding_system (Lisp_Object obj, Lisp_Object printcharfun,
|
|
375 int escapeflag)
|
|
376 {
|
|
377 Lisp_Coding_System *c = XCODING_SYSTEM (obj);
|
|
378 if (print_readably)
|
|
379 error ("printing unreadable object #<coding_system 0x%x>",
|
|
380 c->header.uid);
|
|
381
|
|
382 write_c_string ("#<coding_system ", printcharfun);
|
|
383 print_internal (c->name, printcharfun, 1);
|
|
384 write_c_string (">", printcharfun);
|
|
385 }
|
|
386
|
|
387 static void
|
|
388 finalize_coding_system (void *header, int for_disksave)
|
|
389 {
|
|
390 Lisp_Coding_System *c = (Lisp_Coding_System *) header;
|
|
391 /* Since coding systems never go away, this function is not
|
|
392 necessary. But it would be necessary if we changed things
|
|
393 so that coding systems could go away. */
|
|
394 if (!for_disksave) /* see comment in lstream.c */
|
|
395 {
|
|
396 switch (CODING_SYSTEM_TYPE (c))
|
|
397 {
|
|
398 #ifdef MULE
|
|
399 case CODESYS_ISO2022:
|
|
400 if (c->iso2022.input_conv)
|
|
401 {
|
|
402 Dynarr_free (c->iso2022.input_conv);
|
|
403 c->iso2022.input_conv = 0;
|
|
404 }
|
|
405 if (c->iso2022.output_conv)
|
|
406 {
|
|
407 Dynarr_free (c->iso2022.output_conv);
|
|
408 c->iso2022.output_conv = 0;
|
|
409 }
|
|
410 break;
|
|
411 #endif /* MULE */
|
|
412 default:
|
|
413 break;
|
|
414 }
|
|
415 }
|
|
416 }
|
|
417
|
438
|
418 static eol_type_t
|
428
|
419 symbol_to_eol_type (Lisp_Object symbol)
|
|
420 {
|
|
421 CHECK_SYMBOL (symbol);
|
|
422 if (NILP (symbol)) return EOL_AUTODETECT;
|
|
423 if (EQ (symbol, Qlf)) return EOL_LF;
|
|
424 if (EQ (symbol, Qcrlf)) return EOL_CRLF;
|
|
425 if (EQ (symbol, Qcr)) return EOL_CR;
|
|
426
|
|
427 signal_simple_error ("Unrecognized eol type", symbol);
|
|
428 return EOL_AUTODETECT; /* not reached */
|
|
429 }
|
|
430
|
|
431 static Lisp_Object
|
438
|
432 eol_type_to_symbol (eol_type_t type)
|
428
|
433 {
|
|
434 switch (type)
|
|
435 {
|
|
436 default: abort ();
|
|
437 case EOL_LF: return Qlf;
|
|
438 case EOL_CRLF: return Qcrlf;
|
|
439 case EOL_CR: return Qcr;
|
|
440 case EOL_AUTODETECT: return Qnil;
|
|
441 }
|
|
442 }
|
|
443
|
|
444 static void
|
|
445 setup_eol_coding_systems (Lisp_Coding_System *codesys)
|
|
446 {
|
|
447 Lisp_Object codesys_obj;
|
|
448 int len = string_length (XSYMBOL (CODING_SYSTEM_NAME (codesys))->name);
|
|
449 char *codesys_name = (char *) alloca (len + 7);
|
|
450 int mlen = -1;
|
|
451 char *codesys_mnemonic=0;
|
|
452
|
|
453 Lisp_Object codesys_name_sym, sub_codesys_obj;
|
|
454
|
|
455 /* kludge */
|
|
456
|
|
457 XSETCODING_SYSTEM (codesys_obj, codesys);
|
|
458
|
|
459 memcpy (codesys_name,
|
|
460 string_data (XSYMBOL (CODING_SYSTEM_NAME (codesys))->name), len);
|
|
461
|
|
462 if (STRINGP (CODING_SYSTEM_MNEMONIC (codesys)))
|
|
463 {
|
|
464 mlen = XSTRING_LENGTH (CODING_SYSTEM_MNEMONIC (codesys));
|
|
465 codesys_mnemonic = (char *) alloca (mlen + 7);
|
|
466 memcpy (codesys_mnemonic,
|
|
467 XSTRING_DATA (CODING_SYSTEM_MNEMONIC (codesys)), mlen);
|
|
468 }
|
|
469
|
|
470 #define DEFINE_SUB_CODESYS(op_sys, op_sys_abbr, Type) do { \
|
|
471 strcpy (codesys_name + len, "-" op_sys); \
|
|
472 if (mlen != -1) \
|
|
473 strcpy (codesys_mnemonic + mlen, op_sys_abbr); \
|
|
474 codesys_name_sym = intern (codesys_name); \
|
|
475 sub_codesys_obj = Fcopy_coding_system (codesys_obj, codesys_name_sym); \
|
|
476 XCODING_SYSTEM_EOL_TYPE (sub_codesys_obj) = Type; \
|
|
477 if (mlen != -1) \
|
|
478 XCODING_SYSTEM_MNEMONIC(sub_codesys_obj) = \
|
|
479 build_string (codesys_mnemonic); \
|
|
480 CODING_SYSTEM_##Type (codesys) = sub_codesys_obj; \
|
|
481 } while (0)
|
|
482
|
|
483 DEFINE_SUB_CODESYS("unix", "", EOL_LF);
|
|
484 DEFINE_SUB_CODESYS("dos", ":T", EOL_CRLF);
|
|
485 DEFINE_SUB_CODESYS("mac", ":t", EOL_CR);
|
|
486 }
|
|
487
|
|
488 DEFUN ("coding-system-p", Fcoding_system_p, 1, 1, 0, /*
|
|
489 Return t if OBJECT is a coding system.
|
|
490 A coding system is an object that defines how text containing multiple
|
|
491 character sets is encoded into a stream of (typically 8-bit) bytes.
|
|
492 The coding system is used to decode the stream into a series of
|
|
493 characters (which may be from multiple charsets) when the text is read
|
|
494 from a file or process, and is used to encode the text back into the
|
|
495 same format when it is written out to a file or process.
|
|
496
|
|
497 For example, many ISO2022-compliant coding systems (such as Compound
|
|
498 Text, which is used for inter-client data under the X Window System)
|
|
499 use escape sequences to switch between different charsets -- Japanese
|
|
500 Kanji, for example, is invoked with "ESC $ ( B"; ASCII is invoked
|
|
501 with "ESC ( B"; and Cyrillic is invoked with "ESC - L". See
|
|
502 `make-coding-system' for more information.
|
|
503
|
|
504 Coding systems are normally identified using a symbol, and the
|
|
505 symbol is accepted in place of the actual coding system object whenever
|
|
506 a coding system is called for. (This is similar to how faces work.)
|
|
507 */
|
|
508 (object))
|
|
509 {
|
|
510 return CODING_SYSTEMP (object) ? Qt : Qnil;
|
|
511 }
|
|
512
|
|
513 DEFUN ("find-coding-system", Ffind_coding_system, 1, 1, 0, /*
|
|
514 Retrieve the coding system of the given name.
|
|
515
|
|
516 If CODING-SYSTEM-OR-NAME is a coding-system object, it is simply
|
|
517 returned. Otherwise, CODING-SYSTEM-OR-NAME should be a symbol.
|
|
518 If there is no such coding system, nil is returned. Otherwise the
|
|
519 associated coding system object is returned.
|
|
520 */
|
|
521 (coding_system_or_name))
|
|
522 {
|
|
523 if (NILP (coding_system_or_name))
|
|
524 coding_system_or_name = Qbinary;
|
440
|
525 else if (CODING_SYSTEMP (coding_system_or_name))
|
|
526 return coding_system_or_name;
|
428
|
527 else
|
|
528 CHECK_SYMBOL (coding_system_or_name);
|
|
529
|
440
|
530 while (1)
|
|
531 {
|
|
532 coding_system_or_name =
|
|
533 Fgethash (coding_system_or_name, Vcoding_system_hash_table, Qnil);
|
|
534
|
|
535 if (CODING_SYSTEMP (coding_system_or_name) || NILP (coding_system_or_name))
|
|
536 return coding_system_or_name;
|
|
537 }
|
428
|
538 }
|
|
539
|
|
540 DEFUN ("get-coding-system", Fget_coding_system, 1, 1, 0, /*
|
|
541 Retrieve the coding system of the given name.
|
|
542 Same as `find-coding-system' except that if there is no such
|
|
543 coding system, an error is signaled instead of returning nil.
|
|
544 */
|
|
545 (name))
|
|
546 {
|
|
547 Lisp_Object coding_system = Ffind_coding_system (name);
|
|
548
|
|
549 if (NILP (coding_system))
|
|
550 signal_simple_error ("No such coding system", name);
|
|
551 return coding_system;
|
|
552 }
|
|
553
|
|
554 /* We store the coding systems in hash tables with the names as the key and the
|
|
555 actual coding system object as the value. Occasionally we need to use them
|
|
556 in a list format. These routines provide us with that. */
|
|
557 struct coding_system_list_closure
|
|
558 {
|
|
559 Lisp_Object *coding_system_list;
|
|
560 };
|
|
561
|
|
562 static int
|
|
563 add_coding_system_to_list_mapper (Lisp_Object key, Lisp_Object value,
|
|
564 void *coding_system_list_closure)
|
|
565 {
|
|
566 /* This function can GC */
|
|
567 struct coding_system_list_closure *cscl =
|
|
568 (struct coding_system_list_closure *) coding_system_list_closure;
|
|
569 Lisp_Object *coding_system_list = cscl->coding_system_list;
|
|
570
|
432
|
571 *coding_system_list = Fcons (key, *coding_system_list);
|
428
|
572 return 0;
|
|
573 }
|
|
574
|
|
575 DEFUN ("coding-system-list", Fcoding_system_list, 0, 0, 0, /*
|
|
576 Return a list of the names of all defined coding systems.
|
|
577 */
|
|
578 ())
|
|
579 {
|
|
580 Lisp_Object coding_system_list = Qnil;
|
|
581 struct gcpro gcpro1;
|
|
582 struct coding_system_list_closure coding_system_list_closure;
|
|
583
|
|
584 GCPRO1 (coding_system_list);
|
|
585 coding_system_list_closure.coding_system_list = &coding_system_list;
|
|
586 elisp_maphash (add_coding_system_to_list_mapper, Vcoding_system_hash_table,
|
|
587 &coding_system_list_closure);
|
|
588 UNGCPRO;
|
|
589
|
|
590 return coding_system_list;
|
|
591 }
|
|
592
|
|
593 DEFUN ("coding-system-name", Fcoding_system_name, 1, 1, 0, /*
|
|
594 Return the name of the given coding system.
|
|
595 */
|
|
596 (coding_system))
|
|
597 {
|
|
598 coding_system = Fget_coding_system (coding_system);
|
|
599 return XCODING_SYSTEM_NAME (coding_system);
|
|
600 }
|
|
601
|
|
602 static Lisp_Coding_System *
|
|
603 allocate_coding_system (enum coding_system_type type, Lisp_Object name)
|
|
604 {
|
|
605 Lisp_Coding_System *codesys =
|
|
606 alloc_lcrecord_type (Lisp_Coding_System, &lrecord_coding_system);
|
|
607
|
|
608 zero_lcrecord (codesys);
|
|
609 CODING_SYSTEM_PRE_WRITE_CONVERSION (codesys) = Qnil;
|
|
610 CODING_SYSTEM_POST_READ_CONVERSION (codesys) = Qnil;
|
|
611 CODING_SYSTEM_EOL_TYPE (codesys) = EOL_AUTODETECT;
|
|
612 CODING_SYSTEM_EOL_CRLF (codesys) = Qnil;
|
|
613 CODING_SYSTEM_EOL_CR (codesys) = Qnil;
|
|
614 CODING_SYSTEM_EOL_LF (codesys) = Qnil;
|
|
615 CODING_SYSTEM_TYPE (codesys) = type;
|
|
616 CODING_SYSTEM_MNEMONIC (codesys) = Qnil;
|
|
617 #ifdef MULE
|
|
618 if (type == CODESYS_ISO2022)
|
|
619 {
|
|
620 int i;
|
|
621 for (i = 0; i < 4; i++)
|
|
622 CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i) = Qnil;
|
|
623 }
|
|
624 else if (type == CODESYS_CCL)
|
|
625 {
|
|
626 CODING_SYSTEM_CCL_DECODE (codesys) = Qnil;
|
|
627 CODING_SYSTEM_CCL_ENCODE (codesys) = Qnil;
|
|
628 }
|
|
629 #endif /* MULE */
|
|
630 CODING_SYSTEM_NAME (codesys) = name;
|
|
631
|
|
632 return codesys;
|
|
633 }
|
|
634
|
|
635 #ifdef MULE
|
|
636 /* Given a list of charset conversion specs as specified in a Lisp
|
|
637 program, parse it into STORE_HERE. */
|
|
638
|
|
639 static void
|
|
640 parse_charset_conversion_specs (charset_conversion_spec_dynarr *store_here,
|
|
641 Lisp_Object spec_list)
|
|
642 {
|
|
643 Lisp_Object rest;
|
|
644
|
|
645 EXTERNAL_LIST_LOOP (rest, spec_list)
|
|
646 {
|
|
647 Lisp_Object car = XCAR (rest);
|
|
648 Lisp_Object from, to;
|
|
649 struct charset_conversion_spec spec;
|
|
650
|
|
651 if (!CONSP (car) || !CONSP (XCDR (car)) || !NILP (XCDR (XCDR (car))))
|
|
652 signal_simple_error ("Invalid charset conversion spec", car);
|
|
653 from = Fget_charset (XCAR (car));
|
|
654 to = Fget_charset (XCAR (XCDR (car)));
|
|
655 if (XCHARSET_TYPE (from) != XCHARSET_TYPE (to))
|
|
656 signal_simple_error_2
|
|
657 ("Attempted conversion between different charset types",
|
|
658 from, to);
|
|
659 spec.from_charset = from;
|
|
660 spec.to_charset = to;
|
|
661
|
|
662 Dynarr_add (store_here, spec);
|
|
663 }
|
|
664 }
|
|
665
|
|
666 /* Given a dynarr LOAD_HERE of internally-stored charset conversion
|
|
667 specs, return the equivalent as the Lisp programmer would see it.
|
|
668
|
|
669 If LOAD_HERE is 0, return Qnil. */
|
|
670
|
|
671 static Lisp_Object
|
|
672 unparse_charset_conversion_specs (charset_conversion_spec_dynarr *load_here)
|
|
673 {
|
|
674 int i;
|
|
675 Lisp_Object result;
|
|
676
|
|
677 if (!load_here)
|
|
678 return Qnil;
|
|
679 for (i = 0, result = Qnil; i < Dynarr_length (load_here); i++)
|
|
680 {
|
|
681 struct charset_conversion_spec *ccs = Dynarr_atp (load_here, i);
|
|
682 result = Fcons (list2 (ccs->from_charset, ccs->to_charset), result);
|
|
683 }
|
|
684
|
|
685 return Fnreverse (result);
|
|
686 }
|
|
687
|
|
688 #endif /* MULE */
|
|
689
|
|
690 DEFUN ("make-coding-system", Fmake_coding_system, 2, 4, 0, /*
|
|
691 Register symbol NAME as a coding system.
|
|
692
|
|
693 TYPE describes the conversion method used and should be one of
|
|
694
|
|
695 nil or 'undecided
|
|
696 Automatic conversion. XEmacs attempts to detect the coding system
|
|
697 used in the file.
|
|
698 'no-conversion
|
|
699 No conversion. Use this for binary files and such. On output,
|
|
700 graphic characters that are not in ASCII or Latin-1 will be
|
|
701 replaced by a ?. (For a no-conversion-encoded buffer, these
|
|
702 characters will only be present if you explicitly insert them.)
|
|
703 'shift-jis
|
|
704 Shift-JIS (a Japanese encoding commonly used in PC operating systems).
|
|
705 'ucs-4
|
|
706 ISO 10646 UCS-4 encoding.
|
|
707 'utf-8
|
|
708 ISO 10646 UTF-8 encoding.
|
|
709 'iso2022
|
|
710 Any ISO2022-compliant encoding. Among other things, this includes
|
|
711 JIS (the Japanese encoding commonly used for e-mail), EUC (the
|
|
712 standard Unix encoding for Japanese and other languages), and
|
|
713 Compound Text (the encoding used in X11). You can specify more
|
442
|
714 specific information about the conversion with the PROPS argument.
|
428
|
715 'big5
|
|
716 Big5 (the encoding commonly used for Taiwanese).
|
|
717 'ccl
|
|
718 The conversion is performed using a user-written pseudo-code
|
|
719 program. CCL (Code Conversion Language) is the name of this
|
|
720 pseudo-code.
|
|
721 'internal
|
|
722 Write out or read in the raw contents of the memory representing
|
|
723 the buffer's text. This is primarily useful for debugging
|
|
724 purposes, and is only enabled when XEmacs has been compiled with
|
|
725 DEBUG_XEMACS defined (via the --debug configure option).
|
|
726 WARNING: Reading in a file using 'internal conversion can result
|
|
727 in an internal inconsistency in the memory representing a
|
|
728 buffer's text, which will produce unpredictable results and may
|
|
729 cause XEmacs to crash. Under normal circumstances you should
|
|
730 never use 'internal conversion.
|
|
731
|
|
732 DOC-STRING is a string describing the coding system.
|
|
733
|
|
734 PROPS is a property list, describing the specific nature of the
|
|
735 character set. Recognized properties are:
|
|
736
|
|
737 'mnemonic
|
|
738 String to be displayed in the modeline when this coding system is
|
|
739 active.
|
|
740
|
|
741 'eol-type
|
|
742 End-of-line conversion to be used. It should be one of
|
|
743
|
|
744 nil
|
|
745 Automatically detect the end-of-line type (LF, CRLF,
|
|
746 or CR). Also generate subsidiary coding systems named
|
|
747 `NAME-unix', `NAME-dos', and `NAME-mac', that are
|
|
748 identical to this coding system but have an EOL-TYPE
|
|
749 value of 'lf, 'crlf, and 'cr, respectively.
|
|
750 'lf
|
|
751 The end of a line is marked externally using ASCII LF.
|
|
752 Since this is also the way that XEmacs represents an
|
|
753 end-of-line internally, specifying this option results
|
|
754 in no end-of-line conversion. This is the standard
|
|
755 format for Unix text files.
|
|
756 'crlf
|
|
757 The end of a line is marked externally using ASCII
|
|
758 CRLF. This is the standard format for MS-DOS text
|
|
759 files.
|
|
760 'cr
|
|
761 The end of a line is marked externally using ASCII CR.
|
|
762 This is the standard format for Macintosh text files.
|
|
763 t
|
|
764 Automatically detect the end-of-line type but do not
|
|
765 generate subsidiary coding systems. (This value is
|
|
766 converted to nil when stored internally, and
|
|
767 `coding-system-property' will return nil.)
|
|
768
|
|
769 'post-read-conversion
|
|
770 Function called after a file has been read in, to perform the
|
444
|
771 decoding. Called with two arguments, START and END, denoting
|
428
|
772 a region of the current buffer to be decoded.
|
|
773
|
|
774 'pre-write-conversion
|
|
775 Function called before a file is written out, to perform the
|
444
|
776 encoding. Called with two arguments, START and END, denoting
|
428
|
777 a region of the current buffer to be encoded.
|
|
778
|
|
779
|
|
780 The following additional properties are recognized if TYPE is 'iso2022:
|
|
781
|
|
782 'charset-g0
|
|
783 'charset-g1
|
|
784 'charset-g2
|
|
785 'charset-g3
|
|
786 The character set initially designated to the G0 - G3 registers.
|
|
787 The value should be one of
|
|
788
|
|
789 -- A charset object (designate that character set)
|
|
790 -- nil (do not ever use this register)
|
|
791 -- t (no character set is initially designated to
|
|
792 the register, but may be later on; this automatically
|
|
793 sets the corresponding `force-g*-on-output' property)
|
|
794
|
|
795 'force-g0-on-output
|
|
796 'force-g1-on-output
|
|
797 'force-g2-on-output
|
|
798 'force-g2-on-output
|
|
799 If non-nil, send an explicit designation sequence on output before
|
|
800 using the specified register.
|
|
801
|
|
802 'short
|
|
803 If non-nil, use the short forms "ESC $ @", "ESC $ A", and
|
|
804 "ESC $ B" on output in place of the full designation sequences
|
|
805 "ESC $ ( @", "ESC $ ( A", and "ESC $ ( B".
|
|
806
|
|
807 'no-ascii-eol
|
|
808 If non-nil, don't designate ASCII to G0 at each end of line on output.
|
|
809 Setting this to non-nil also suppresses other state-resetting that
|
|
810 normally happens at the end of a line.
|
|
811
|
|
812 'no-ascii-cntl
|
|
813 If non-nil, don't designate ASCII to G0 before control chars on output.
|
|
814
|
|
815 'seven
|
|
816 If non-nil, use 7-bit environment on output. Otherwise, use 8-bit
|
|
817 environment.
|
|
818
|
|
819 'lock-shift
|
|
820 If non-nil, use locking-shift (SO/SI) instead of single-shift
|
|
821 or designation by escape sequence.
|
|
822
|
|
823 'no-iso6429
|
|
824 If non-nil, don't use ISO6429's direction specification.
|
|
825
|
|
826 'escape-quoted
|
|
827 If non-nil, literal control characters that are the same as
|
|
828 the beginning of a recognized ISO2022 or ISO6429 escape sequence
|
|
829 (in particular, ESC (0x1B), SO (0x0E), SI (0x0F), SS2 (0x8E),
|
|
830 SS3 (0x8F), and CSI (0x9B)) are "quoted" with an escape character
|
|
831 so that they can be properly distinguished from an escape sequence.
|
|
832 (Note that doing this results in a non-portable encoding.) This
|
|
833 encoding flag is used for byte-compiled files. Note that ESC
|
|
834 is a good choice for a quoting character because there are no
|
|
835 escape sequences whose second byte is a character from the Control-0
|
|
836 or Control-1 character sets; this is explicitly disallowed by the
|
|
837 ISO2022 standard.
|
|
838
|
|
839 'input-charset-conversion
|
|
840 A list of conversion specifications, specifying conversion of
|
|
841 characters in one charset to another when decoding is performed.
|
|
842 Each specification is a list of two elements: the source charset,
|
|
843 and the destination charset.
|
|
844
|
|
845 'output-charset-conversion
|
|
846 A list of conversion specifications, specifying conversion of
|
|
847 characters in one charset to another when encoding is performed.
|
|
848 The form of each specification is the same as for
|
|
849 'input-charset-conversion.
|
|
850
|
|
851
|
|
852 The following additional properties are recognized (and required)
|
|
853 if TYPE is 'ccl:
|
|
854
|
|
855 'decode
|
|
856 CCL program used for decoding (converting to internal format).
|
|
857
|
|
858 'encode
|
|
859 CCL program used for encoding (converting to external format).
|
|
860 */
|
|
861 (name, type, doc_string, props))
|
|
862 {
|
|
863 Lisp_Coding_System *codesys;
|
|
864 enum coding_system_type ty;
|
|
865 int need_to_setup_eol_systems = 1;
|
|
866
|
|
867 /* Convert type to constant */
|
|
868 if (NILP (type) || EQ (type, Qundecided))
|
|
869 { ty = CODESYS_AUTODETECT; }
|
|
870 #ifdef MULE
|
|
871 else if (EQ (type, Qshift_jis)) { ty = CODESYS_SHIFT_JIS; }
|
|
872 else if (EQ (type, Qiso2022)) { ty = CODESYS_ISO2022; }
|
|
873 else if (EQ (type, Qbig5)) { ty = CODESYS_BIG5; }
|
|
874 else if (EQ (type, Qucs4)) { ty = CODESYS_UCS4; }
|
|
875 else if (EQ (type, Qutf8)) { ty = CODESYS_UTF8; }
|
|
876 else if (EQ (type, Qccl)) { ty = CODESYS_CCL; }
|
|
877 #endif
|
|
878 else if (EQ (type, Qno_conversion)) { ty = CODESYS_NO_CONVERSION; }
|
|
879 #ifdef DEBUG_XEMACS
|
|
880 else if (EQ (type, Qinternal)) { ty = CODESYS_INTERNAL; }
|
|
881 #endif
|
|
882 else
|
|
883 signal_simple_error ("Invalid coding system type", type);
|
|
884
|
|
885 CHECK_SYMBOL (name);
|
|
886
|
|
887 codesys = allocate_coding_system (ty, name);
|
|
888
|
|
889 if (NILP (doc_string))
|
|
890 doc_string = build_string ("");
|
|
891 else
|
|
892 CHECK_STRING (doc_string);
|
|
893 CODING_SYSTEM_DOC_STRING (codesys) = doc_string;
|
|
894
|
442
|
895 {
|
|
896 EXTERNAL_PROPERTY_LIST_LOOP_3 (key, value, props)
|
|
897 {
|
|
898 if (EQ (key, Qmnemonic))
|
|
899 {
|
|
900 if (!NILP (value))
|
|
901 CHECK_STRING (value);
|
|
902 CODING_SYSTEM_MNEMONIC (codesys) = value;
|
|
903 }
|
|
904
|
|
905 else if (EQ (key, Qeol_type))
|
|
906 {
|
|
907 need_to_setup_eol_systems = NILP (value);
|
|
908 if (EQ (value, Qt))
|
|
909 value = Qnil;
|
|
910 CODING_SYSTEM_EOL_TYPE (codesys) = symbol_to_eol_type (value);
|
|
911 }
|
|
912
|
|
913 else if (EQ (key, Qpost_read_conversion)) CODING_SYSTEM_POST_READ_CONVERSION (codesys) = value;
|
|
914 else if (EQ (key, Qpre_write_conversion)) CODING_SYSTEM_PRE_WRITE_CONVERSION (codesys) = value;
|
428
|
915 #ifdef MULE
|
442
|
916 else if (ty == CODESYS_ISO2022)
|
|
917 {
|
428
|
918 #define FROB_INITIAL_CHARSET(charset_num) \
|
|
919 CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, charset_num) = \
|
|
920 ((EQ (value, Qt) || EQ (value, Qnil)) ? value : Fget_charset (value))
|
|
921
|
442
|
922 if (EQ (key, Qcharset_g0)) FROB_INITIAL_CHARSET (0);
|
|
923 else if (EQ (key, Qcharset_g1)) FROB_INITIAL_CHARSET (1);
|
|
924 else if (EQ (key, Qcharset_g2)) FROB_INITIAL_CHARSET (2);
|
|
925 else if (EQ (key, Qcharset_g3)) FROB_INITIAL_CHARSET (3);
|
428
|
926
|
|
927 #define FROB_FORCE_CHARSET(charset_num) \
|
|
928 CODING_SYSTEM_ISO2022_FORCE_CHARSET_ON_OUTPUT (codesys, charset_num) = !NILP (value)
|
|
929
|
442
|
930 else if (EQ (key, Qforce_g0_on_output)) FROB_FORCE_CHARSET (0);
|
|
931 else if (EQ (key, Qforce_g1_on_output)) FROB_FORCE_CHARSET (1);
|
|
932 else if (EQ (key, Qforce_g2_on_output)) FROB_FORCE_CHARSET (2);
|
|
933 else if (EQ (key, Qforce_g3_on_output)) FROB_FORCE_CHARSET (3);
|
428
|
934
|
|
935 #define FROB_BOOLEAN_PROPERTY(prop) \
|
|
936 CODING_SYSTEM_ISO2022_##prop (codesys) = !NILP (value)
|
|
937
|
442
|
938 else if (EQ (key, Qshort)) FROB_BOOLEAN_PROPERTY (SHORT);
|
|
939 else if (EQ (key, Qno_ascii_eol)) FROB_BOOLEAN_PROPERTY (NO_ASCII_EOL);
|
|
940 else if (EQ (key, Qno_ascii_cntl)) FROB_BOOLEAN_PROPERTY (NO_ASCII_CNTL);
|
|
941 else if (EQ (key, Qseven)) FROB_BOOLEAN_PROPERTY (SEVEN);
|
|
942 else if (EQ (key, Qlock_shift)) FROB_BOOLEAN_PROPERTY (LOCK_SHIFT);
|
|
943 else if (EQ (key, Qno_iso6429)) FROB_BOOLEAN_PROPERTY (NO_ISO6429);
|
|
944 else if (EQ (key, Qescape_quoted)) FROB_BOOLEAN_PROPERTY (ESCAPE_QUOTED);
|
|
945
|
|
946 else if (EQ (key, Qinput_charset_conversion))
|
|
947 {
|
|
948 codesys->iso2022.input_conv =
|
|
949 Dynarr_new (charset_conversion_spec);
|
|
950 parse_charset_conversion_specs (codesys->iso2022.input_conv,
|
|
951 value);
|
|
952 }
|
|
953 else if (EQ (key, Qoutput_charset_conversion))
|
|
954 {
|
|
955 codesys->iso2022.output_conv =
|
|
956 Dynarr_new (charset_conversion_spec);
|
|
957 parse_charset_conversion_specs (codesys->iso2022.output_conv,
|
|
958 value);
|
|
959 }
|
|
960 else
|
|
961 signal_simple_error ("Unrecognized property", key);
|
|
962 }
|
|
963 else if (EQ (type, Qccl))
|
|
964 {
|
444
|
965 Lisp_Object sym;
|
|
966 struct ccl_program test_ccl;
|
|
967 Extbyte *suffix;
|
|
968
|
|
969 /* Check key first. */
|
442
|
970 if (EQ (key, Qdecode))
|
444
|
971 suffix = "-ccl-decode";
|
|
972 else if (EQ (key, Qencode))
|
|
973 suffix = "-ccl-encode";
|
|
974 else
|
|
975 signal_simple_error ("Unrecognized property", key);
|
|
976
|
|
977 /* If value is vector, register it as a ccl program
|
|
978 associated with an newly created symbol for
|
|
979 backward compatibility. */
|
|
980 if (VECTORP (value))
|
442
|
981 {
|
444
|
982 sym = Fintern (concat2 (Fsymbol_name (name),
|
|
983 build_string (suffix)),
|
|
984 Qnil);
|
|
985 Fregister_ccl_program (sym, value);
|
442
|
986 }
|
|
987 else
|
444
|
988 {
|
|
989 CHECK_SYMBOL (value);
|
|
990 sym = value;
|
|
991 }
|
|
992 /* check if the given ccl programs are valid. */
|
|
993 if (setup_ccl_program (&test_ccl, sym) < 0)
|
|
994 signal_simple_error ("Invalid CCL program", value);
|
|
995
|
|
996 if (EQ (key, Qdecode))
|
|
997 CODING_SYSTEM_CCL_DECODE (codesys) = sym;
|
|
998 else if (EQ (key, Qencode))
|
|
999 CODING_SYSTEM_CCL_ENCODE (codesys) = sym;
|
|
1000
|
442
|
1001 }
|
428
|
1002 #endif /* MULE */
|
442
|
1003 else
|
|
1004 signal_simple_error ("Unrecognized property", key);
|
|
1005 }
|
|
1006 }
|
428
|
1007
|
|
1008 if (need_to_setup_eol_systems)
|
|
1009 setup_eol_coding_systems (codesys);
|
|
1010
|
|
1011 {
|
|
1012 Lisp_Object codesys_obj;
|
|
1013 XSETCODING_SYSTEM (codesys_obj, codesys);
|
|
1014 Fputhash (name, codesys_obj, Vcoding_system_hash_table);
|
|
1015 return codesys_obj;
|
|
1016 }
|
|
1017 }
|
|
1018
|
|
1019 DEFUN ("copy-coding-system", Fcopy_coding_system, 2, 2, 0, /*
|
|
1020 Copy OLD-CODING-SYSTEM to NEW-NAME.
|
|
1021 If NEW-NAME does not name an existing coding system, a new one will
|
|
1022 be created.
|
|
1023 */
|
|
1024 (old_coding_system, new_name))
|
|
1025 {
|
|
1026 Lisp_Object new_coding_system;
|
|
1027 old_coding_system = Fget_coding_system (old_coding_system);
|
|
1028 new_coding_system = Ffind_coding_system (new_name);
|
|
1029 if (NILP (new_coding_system))
|
|
1030 {
|
|
1031 XSETCODING_SYSTEM (new_coding_system,
|
|
1032 allocate_coding_system
|
|
1033 (XCODING_SYSTEM_TYPE (old_coding_system),
|
|
1034 new_name));
|
|
1035 Fputhash (new_name, new_coding_system, Vcoding_system_hash_table);
|
|
1036 }
|
|
1037
|
|
1038 {
|
|
1039 Lisp_Coding_System *to = XCODING_SYSTEM (new_coding_system);
|
|
1040 Lisp_Coding_System *from = XCODING_SYSTEM (old_coding_system);
|
|
1041 memcpy (((char *) to ) + sizeof (to->header),
|
|
1042 ((char *) from) + sizeof (from->header),
|
|
1043 sizeof (*from) - sizeof (from->header));
|
|
1044 to->name = new_name;
|
|
1045 }
|
|
1046 return new_coding_system;
|
|
1047 }
|
|
1048
|
440
|
1049 DEFUN ("coding-system-canonical-name-p", Fcoding_system_canonical_name_p, 1, 1, 0, /*
|
|
1050 Return t if OBJECT names a coding system, and is not a coding system alias.
|
428
|
1051 */
|
440
|
1052 (object))
|
|
1053 {
|
|
1054 return CODING_SYSTEMP (Fgethash (object, Vcoding_system_hash_table, Qnil))
|
|
1055 ? Qt : Qnil;
|
|
1056 }
|
|
1057
|
|
1058 DEFUN ("coding-system-alias-p", Fcoding_system_alias_p, 1, 1, 0, /*
|
|
1059 Return t if OBJECT is a coding system alias.
|
|
1060 All coding system aliases are created by `define-coding-system-alias'.
|
|
1061 */
|
|
1062 (object))
|
428
|
1063 {
|
440
|
1064 return SYMBOLP (Fgethash (object, Vcoding_system_hash_table, Qzero))
|
|
1065 ? Qt : Qnil;
|
|
1066 }
|
|
1067
|
|
1068 DEFUN ("coding-system-aliasee", Fcoding_system_aliasee, 1, 1, 0, /*
|
|
1069 Return the coding-system symbol for which symbol ALIAS is an alias.
|
|
1070 */
|
|
1071 (alias))
|
|
1072 {
|
|
1073 Lisp_Object aliasee = Fgethash (alias, Vcoding_system_hash_table, Qnil);
|
|
1074 if (SYMBOLP (aliasee))
|
|
1075 return aliasee;
|
|
1076 else
|
|
1077 signal_simple_error ("Symbol is not a coding system alias", alias);
|
442
|
1078 return Qnil; /* To keep the compiler happy */
|
440
|
1079 }
|
|
1080
|
|
1081 static Lisp_Object
|
|
1082 append_suffix_to_symbol (Lisp_Object symbol, const char *ascii_string)
|
|
1083 {
|
|
1084 return Fintern (concat2 (Fsymbol_name (symbol), build_string (ascii_string)),
|
|
1085 Qnil);
|
|
1086 }
|
|
1087
|
|
1088 /* A maphash function, for removing dangling coding system aliases. */
|
|
1089 static int
|
|
1090 dangling_coding_system_alias_p (Lisp_Object alias,
|
|
1091 Lisp_Object aliasee,
|
|
1092 void *dangling_aliases)
|
|
1093 {
|
|
1094 if (SYMBOLP (aliasee)
|
|
1095 && NILP (Fgethash (aliasee, Vcoding_system_hash_table, Qnil)))
|
428
|
1096 {
|
440
|
1097 (*(int *) dangling_aliases)++;
|
|
1098 return 1;
|
428
|
1099 }
|
440
|
1100 else
|
|
1101 return 0;
|
|
1102 }
|
|
1103
|
|
1104 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias, 2, 2, 0, /*
|
|
1105 Define symbol ALIAS as an alias for coding system ALIASEE.
|
|
1106
|
|
1107 You can use this function to redefine an alias that has already been defined,
|
|
1108 but you cannot redefine a name which is the canonical name for a coding system.
|
|
1109 \(a canonical name of a coding system is what is returned when you call
|
|
1110 `coding-system-name' on a coding system).
|
|
1111
|
|
1112 ALIASEE itself can be an alias, which allows you to define nested aliases.
|
|
1113
|
|
1114 You are forbidden, however, from creating alias loops or `dangling' aliases.
|
|
1115 These will be detected, and an error will be signaled if you attempt to do so.
|
|
1116
|
|
1117 If ALIASEE is nil, then ALIAS will simply be undefined.
|
|
1118
|
|
1119 See also `coding-system-alias-p', `coding-system-aliasee',
|
|
1120 and `coding-system-canonical-name-p'.
|
|
1121 */
|
|
1122 (alias, aliasee))
|
|
1123 {
|
|
1124 Lisp_Object real_coding_system, probe;
|
|
1125
|
|
1126 CHECK_SYMBOL (alias);
|
|
1127
|
|
1128 if (!NILP (Fcoding_system_canonical_name_p (alias)))
|
|
1129 signal_simple_error
|
|
1130 ("Symbol is the canonical name of a coding system and cannot be redefined",
|
|
1131 alias);
|
|
1132
|
|
1133 if (NILP (aliasee))
|
|
1134 {
|
|
1135 Lisp_Object subsidiary_unix = append_suffix_to_symbol (alias, "-unix");
|
|
1136 Lisp_Object subsidiary_dos = append_suffix_to_symbol (alias, "-dos");
|
|
1137 Lisp_Object subsidiary_mac = append_suffix_to_symbol (alias, "-mac");
|
|
1138
|
|
1139 Fremhash (alias, Vcoding_system_hash_table);
|
|
1140
|
|
1141 /* Undefine subsidiary aliases,
|
|
1142 presumably created by a previous call to this function */
|
|
1143 if (! NILP (Fcoding_system_alias_p (subsidiary_unix)) &&
|
|
1144 ! NILP (Fcoding_system_alias_p (subsidiary_dos)) &&
|
|
1145 ! NILP (Fcoding_system_alias_p (subsidiary_mac)))
|
|
1146 {
|
|
1147 Fdefine_coding_system_alias (subsidiary_unix, Qnil);
|
|
1148 Fdefine_coding_system_alias (subsidiary_dos, Qnil);
|
|
1149 Fdefine_coding_system_alias (subsidiary_mac, Qnil);
|
|
1150 }
|
|
1151
|
|
1152 /* Undefine dangling coding system aliases. */
|
|
1153 {
|
|
1154 int dangling_aliases;
|
|
1155
|
|
1156 do {
|
|
1157 dangling_aliases = 0;
|
|
1158 elisp_map_remhash (dangling_coding_system_alias_p,
|
|
1159 Vcoding_system_hash_table,
|
|
1160 &dangling_aliases);
|
|
1161 } while (dangling_aliases > 0);
|
|
1162 }
|
|
1163
|
|
1164 return Qnil;
|
|
1165 }
|
|
1166
|
|
1167 if (CODING_SYSTEMP (aliasee))
|
|
1168 aliasee = XCODING_SYSTEM_NAME (aliasee);
|
|
1169
|
|
1170 /* Checks that aliasee names a coding-system */
|
|
1171 real_coding_system = Fget_coding_system (aliasee);
|
|
1172
|
|
1173 /* Check for coding system alias loops */
|
|
1174 if (EQ (alias, aliasee))
|
|
1175 alias_loop: signal_simple_error_2
|
|
1176 ("Attempt to create a coding system alias loop", alias, aliasee);
|
|
1177
|
|
1178 for (probe = aliasee;
|
|
1179 SYMBOLP (probe);
|
|
1180 probe = Fgethash (probe, Vcoding_system_hash_table, Qzero))
|
|
1181 {
|
|
1182 if (EQ (probe, alias))
|
|
1183 goto alias_loop;
|
|
1184 }
|
|
1185
|
|
1186 Fputhash (alias, aliasee, Vcoding_system_hash_table);
|
|
1187
|
|
1188 /* Set up aliases for subsidiaries.
|
|
1189 #### There must be a better way to handle subsidiary coding systems. */
|
|
1190 {
|
|
1191 static const char *suffixes[] = { "-unix", "-dos", "-mac" };
|
|
1192 int i;
|
|
1193 for (i = 0; i < countof (suffixes); i++)
|
|
1194 {
|
|
1195 Lisp_Object alias_subsidiary =
|
|
1196 append_suffix_to_symbol (alias, suffixes[i]);
|
|
1197 Lisp_Object aliasee_subsidiary =
|
|
1198 append_suffix_to_symbol (aliasee, suffixes[i]);
|
|
1199
|
|
1200 if (! NILP (Ffind_coding_system (aliasee_subsidiary)))
|
|
1201 Fdefine_coding_system_alias (alias_subsidiary, aliasee_subsidiary);
|
|
1202 }
|
|
1203 }
|
428
|
1204 /* FSF return value is a vector of [ALIAS-unix ALIAS-dos ALIAS-mac],
|
|
1205 but it doesn't look intentional, so I'd rather return something
|
|
1206 meaningful or nothing at all. */
|
|
1207 return Qnil;
|
|
1208 }
|
|
1209
|
|
1210 static Lisp_Object
|
438
|
1211 subsidiary_coding_system (Lisp_Object coding_system, eol_type_t type)
|
428
|
1212 {
|
|
1213 Lisp_Coding_System *cs = XCODING_SYSTEM (coding_system);
|
|
1214 Lisp_Object new_coding_system;
|
|
1215
|
|
1216 if (CODING_SYSTEM_EOL_TYPE (cs) != EOL_AUTODETECT)
|
|
1217 return coding_system;
|
|
1218
|
|
1219 switch (type)
|
|
1220 {
|
|
1221 case EOL_AUTODETECT: return coding_system;
|
|
1222 case EOL_LF: new_coding_system = CODING_SYSTEM_EOL_LF (cs); break;
|
|
1223 case EOL_CR: new_coding_system = CODING_SYSTEM_EOL_CR (cs); break;
|
|
1224 case EOL_CRLF: new_coding_system = CODING_SYSTEM_EOL_CRLF (cs); break;
|
442
|
1225 default: abort (); return Qnil;
|
428
|
1226 }
|
|
1227
|
|
1228 return NILP (new_coding_system) ? coding_system : new_coding_system;
|
|
1229 }
|
|
1230
|
|
1231 DEFUN ("subsidiary-coding-system", Fsubsidiary_coding_system, 2, 2, 0, /*
|
|
1232 Return the subsidiary coding system of CODING-SYSTEM with eol type EOL-TYPE.
|
|
1233 */
|
|
1234 (coding_system, eol_type))
|
|
1235 {
|
|
1236 coding_system = Fget_coding_system (coding_system);
|
|
1237
|
|
1238 return subsidiary_coding_system (coding_system,
|
|
1239 symbol_to_eol_type (eol_type));
|
|
1240 }
|
|
1241
|
|
1242
|
|
1243 /************************************************************************/
|
|
1244 /* Coding system accessors */
|
|
1245 /************************************************************************/
|
|
1246
|
|
1247 DEFUN ("coding-system-doc-string", Fcoding_system_doc_string, 1, 1, 0, /*
|
|
1248 Return the doc string for CODING-SYSTEM.
|
|
1249 */
|
|
1250 (coding_system))
|
|
1251 {
|
|
1252 coding_system = Fget_coding_system (coding_system);
|
|
1253 return XCODING_SYSTEM_DOC_STRING (coding_system);
|
|
1254 }
|
|
1255
|
|
1256 DEFUN ("coding-system-type", Fcoding_system_type, 1, 1, 0, /*
|
|
1257 Return the type of CODING-SYSTEM.
|
|
1258 */
|
|
1259 (coding_system))
|
|
1260 {
|
|
1261 switch (XCODING_SYSTEM_TYPE (Fget_coding_system (coding_system)))
|
|
1262 {
|
|
1263 default: abort ();
|
|
1264 case CODESYS_AUTODETECT: return Qundecided;
|
|
1265 #ifdef MULE
|
|
1266 case CODESYS_SHIFT_JIS: return Qshift_jis;
|
|
1267 case CODESYS_ISO2022: return Qiso2022;
|
|
1268 case CODESYS_BIG5: return Qbig5;
|
|
1269 case CODESYS_UCS4: return Qucs4;
|
|
1270 case CODESYS_UTF8: return Qutf8;
|
|
1271 case CODESYS_CCL: return Qccl;
|
|
1272 #endif
|
|
1273 case CODESYS_NO_CONVERSION: return Qno_conversion;
|
|
1274 #ifdef DEBUG_XEMACS
|
|
1275 case CODESYS_INTERNAL: return Qinternal;
|
|
1276 #endif
|
|
1277 }
|
|
1278 }
|
|
1279
|
|
1280 #ifdef MULE
|
|
1281 static
|
|
1282 Lisp_Object coding_system_charset (Lisp_Object coding_system, int gnum)
|
|
1283 {
|
|
1284 Lisp_Object cs
|
|
1285 = XCODING_SYSTEM_ISO2022_INITIAL_CHARSET (coding_system, gnum);
|
|
1286
|
|
1287 return CHARSETP (cs) ? XCHARSET_NAME (cs) : Qnil;
|
|
1288 }
|
|
1289 #endif /* MULE */
|
|
1290
|
|
1291 DEFUN ("coding-system-property", Fcoding_system_property, 2, 2, 0, /*
|
|
1292 Return the PROP property of CODING-SYSTEM.
|
|
1293 */
|
|
1294 (coding_system, prop))
|
|
1295 {
|
|
1296 int i, ok = 0;
|
|
1297 enum coding_system_type type;
|
|
1298
|
|
1299 coding_system = Fget_coding_system (coding_system);
|
|
1300 CHECK_SYMBOL (prop);
|
|
1301 type = XCODING_SYSTEM_TYPE (coding_system);
|
|
1302
|
|
1303 for (i = 0; !ok && i < Dynarr_length (the_codesys_prop_dynarr); i++)
|
|
1304 if (EQ (Dynarr_at (the_codesys_prop_dynarr, i).sym, prop))
|
|
1305 {
|
|
1306 ok = 1;
|
|
1307 switch (Dynarr_at (the_codesys_prop_dynarr, i).prop_type)
|
|
1308 {
|
|
1309 case CODESYS_PROP_ALL_OK:
|
|
1310 break;
|
|
1311 #ifdef MULE
|
|
1312 case CODESYS_PROP_ISO2022:
|
|
1313 if (type != CODESYS_ISO2022)
|
|
1314 signal_simple_error
|
|
1315 ("Property only valid in ISO2022 coding systems",
|
|
1316 prop);
|
|
1317 break;
|
|
1318
|
|
1319 case CODESYS_PROP_CCL:
|
|
1320 if (type != CODESYS_CCL)
|
|
1321 signal_simple_error
|
|
1322 ("Property only valid in CCL coding systems",
|
|
1323 prop);
|
|
1324 break;
|
|
1325 #endif /* MULE */
|
|
1326 default:
|
|
1327 abort ();
|
|
1328 }
|
|
1329 }
|
|
1330
|
|
1331 if (!ok)
|
|
1332 signal_simple_error ("Unrecognized property", prop);
|
|
1333
|
|
1334 if (EQ (prop, Qname))
|
|
1335 return XCODING_SYSTEM_NAME (coding_system);
|
|
1336 else if (EQ (prop, Qtype))
|
|
1337 return Fcoding_system_type (coding_system);
|
|
1338 else if (EQ (prop, Qdoc_string))
|
|
1339 return XCODING_SYSTEM_DOC_STRING (coding_system);
|
|
1340 else if (EQ (prop, Qmnemonic))
|
|
1341 return XCODING_SYSTEM_MNEMONIC (coding_system);
|
|
1342 else if (EQ (prop, Qeol_type))
|
|
1343 return eol_type_to_symbol (XCODING_SYSTEM_EOL_TYPE (coding_system));
|
|
1344 else if (EQ (prop, Qeol_lf))
|
|
1345 return XCODING_SYSTEM_EOL_LF (coding_system);
|
|
1346 else if (EQ (prop, Qeol_crlf))
|
|
1347 return XCODING_SYSTEM_EOL_CRLF (coding_system);
|
|
1348 else if (EQ (prop, Qeol_cr))
|
|
1349 return XCODING_SYSTEM_EOL_CR (coding_system);
|
|
1350 else if (EQ (prop, Qpost_read_conversion))
|
|
1351 return XCODING_SYSTEM_POST_READ_CONVERSION (coding_system);
|
|
1352 else if (EQ (prop, Qpre_write_conversion))
|
|
1353 return XCODING_SYSTEM_PRE_WRITE_CONVERSION (coding_system);
|
|
1354 #ifdef MULE
|
|
1355 else if (type == CODESYS_ISO2022)
|
|
1356 {
|
|
1357 if (EQ (prop, Qcharset_g0))
|
|
1358 return coding_system_charset (coding_system, 0);
|
|
1359 else if (EQ (prop, Qcharset_g1))
|
|
1360 return coding_system_charset (coding_system, 1);
|
|
1361 else if (EQ (prop, Qcharset_g2))
|
|
1362 return coding_system_charset (coding_system, 2);
|
|
1363 else if (EQ (prop, Qcharset_g3))
|
|
1364 return coding_system_charset (coding_system, 3);
|
|
1365
|
|
1366 #define FORCE_CHARSET(charset_num) \
|
|
1367 (XCODING_SYSTEM_ISO2022_FORCE_CHARSET_ON_OUTPUT \
|
|
1368 (coding_system, charset_num) ? Qt : Qnil)
|
|
1369
|
|
1370 else if (EQ (prop, Qforce_g0_on_output)) return FORCE_CHARSET (0);
|
|
1371 else if (EQ (prop, Qforce_g1_on_output)) return FORCE_CHARSET (1);
|
|
1372 else if (EQ (prop, Qforce_g2_on_output)) return FORCE_CHARSET (2);
|
|
1373 else if (EQ (prop, Qforce_g3_on_output)) return FORCE_CHARSET (3);
|
|
1374
|
|
1375 #define LISP_BOOLEAN(prop) \
|
|
1376 (XCODING_SYSTEM_ISO2022_##prop (coding_system) ? Qt : Qnil)
|
|
1377
|
|
1378 else if (EQ (prop, Qshort)) return LISP_BOOLEAN (SHORT);
|
|
1379 else if (EQ (prop, Qno_ascii_eol)) return LISP_BOOLEAN (NO_ASCII_EOL);
|
|
1380 else if (EQ (prop, Qno_ascii_cntl)) return LISP_BOOLEAN (NO_ASCII_CNTL);
|
|
1381 else if (EQ (prop, Qseven)) return LISP_BOOLEAN (SEVEN);
|
|
1382 else if (EQ (prop, Qlock_shift)) return LISP_BOOLEAN (LOCK_SHIFT);
|
|
1383 else if (EQ (prop, Qno_iso6429)) return LISP_BOOLEAN (NO_ISO6429);
|
|
1384 else if (EQ (prop, Qescape_quoted)) return LISP_BOOLEAN (ESCAPE_QUOTED);
|
|
1385
|
|
1386 else if (EQ (prop, Qinput_charset_conversion))
|
|
1387 return
|
|
1388 unparse_charset_conversion_specs
|
|
1389 (XCODING_SYSTEM (coding_system)->iso2022.input_conv);
|
|
1390 else if (EQ (prop, Qoutput_charset_conversion))
|
|
1391 return
|
|
1392 unparse_charset_conversion_specs
|
|
1393 (XCODING_SYSTEM (coding_system)->iso2022.output_conv);
|
|
1394 else
|
|
1395 abort ();
|
|
1396 }
|
|
1397 else if (type == CODESYS_CCL)
|
|
1398 {
|
|
1399 if (EQ (prop, Qdecode))
|
|
1400 return XCODING_SYSTEM_CCL_DECODE (coding_system);
|
|
1401 else if (EQ (prop, Qencode))
|
|
1402 return XCODING_SYSTEM_CCL_ENCODE (coding_system);
|
|
1403 else
|
|
1404 abort ();
|
|
1405 }
|
|
1406 #endif /* MULE */
|
|
1407 else
|
|
1408 abort ();
|
|
1409
|
|
1410 return Qnil; /* not reached */
|
|
1411 }
|
|
1412
|
|
1413
|
|
1414 /************************************************************************/
|
|
1415 /* Coding category functions */
|
|
1416 /************************************************************************/
|
|
1417
|
|
1418 static int
|
|
1419 decode_coding_category (Lisp_Object symbol)
|
|
1420 {
|
|
1421 int i;
|
|
1422
|
|
1423 CHECK_SYMBOL (symbol);
|
448
|
1424 for (i = 0; i < CODING_CATEGORY_LAST; i++)
|
428
|
1425 if (EQ (coding_category_symbol[i], symbol))
|
|
1426 return i;
|
|
1427
|
|
1428 signal_simple_error ("Unrecognized coding category", symbol);
|
|
1429 return 0; /* not reached */
|
|
1430 }
|
|
1431
|
|
1432 DEFUN ("coding-category-list", Fcoding_category_list, 0, 0, 0, /*
|
|
1433 Return a list of all recognized coding categories.
|
|
1434 */
|
|
1435 ())
|
|
1436 {
|
|
1437 int i;
|
|
1438 Lisp_Object list = Qnil;
|
|
1439
|
448
|
1440 for (i = CODING_CATEGORY_LAST - 1; i >= 0; i--)
|
428
|
1441 list = Fcons (coding_category_symbol[i], list);
|
|
1442 return list;
|
|
1443 }
|
|
1444
|
|
1445 DEFUN ("set-coding-priority-list", Fset_coding_priority_list, 1, 1, 0, /*
|
|
1446 Change the priority order of the coding categories.
|
|
1447 LIST should be list of coding categories, in descending order of
|
|
1448 priority. Unspecified coding categories will be lower in priority
|
|
1449 than all specified ones, in the same relative order they were in
|
|
1450 previously.
|
|
1451 */
|
|
1452 (list))
|
|
1453 {
|
448
|
1454 int category_to_priority[CODING_CATEGORY_LAST];
|
428
|
1455 int i, j;
|
|
1456 Lisp_Object rest;
|
|
1457
|
|
1458 /* First generate a list that maps coding categories to priorities. */
|
|
1459
|
448
|
1460 for (i = 0; i < CODING_CATEGORY_LAST; i++)
|
428
|
1461 category_to_priority[i] = -1;
|
|
1462
|
|
1463 /* Highest priority comes from the specified list. */
|
|
1464 i = 0;
|
|
1465 EXTERNAL_LIST_LOOP (rest, list)
|
|
1466 {
|
|
1467 int cat = decode_coding_category (XCAR (rest));
|
|
1468
|
|
1469 if (category_to_priority[cat] >= 0)
|
|
1470 signal_simple_error ("Duplicate coding category in list", XCAR (rest));
|
|
1471 category_to_priority[cat] = i++;
|
|
1472 }
|
|
1473
|
|
1474 /* Now go through the existing categories by priority to retrieve
|
|
1475 the categories not yet specified and preserve their priority
|
|
1476 order. */
|
448
|
1477 for (j = 0; j < CODING_CATEGORY_LAST; j++)
|
428
|
1478 {
|
|
1479 int cat = fcd->coding_category_by_priority[j];
|
|
1480 if (category_to_priority[cat] < 0)
|
|
1481 category_to_priority[cat] = i++;
|
|
1482 }
|
|
1483
|
|
1484 /* Now we need to construct the inverse of the mapping we just
|
|
1485 constructed. */
|
|
1486
|
448
|
1487 for (i = 0; i < CODING_CATEGORY_LAST; i++)
|
428
|
1488 fcd->coding_category_by_priority[category_to_priority[i]] = i;
|
|
1489
|
|
1490 /* Phew! That was confusing. */
|
|
1491 return Qnil;
|
|
1492 }
|
|
1493
|
|
1494 DEFUN ("coding-priority-list", Fcoding_priority_list, 0, 0, 0, /*
|
|
1495 Return a list of coding categories in descending order of priority.
|
|
1496 */
|
|
1497 ())
|
|
1498 {
|
|
1499 int i;
|
|
1500 Lisp_Object list = Qnil;
|
|
1501
|
448
|
1502 for (i = CODING_CATEGORY_LAST - 1; i >= 0; i--)
|
428
|
1503 list = Fcons (coding_category_symbol[fcd->coding_category_by_priority[i]],
|
|
1504 list);
|
|
1505 return list;
|
|
1506 }
|
|
1507
|
|
1508 DEFUN ("set-coding-category-system", Fset_coding_category_system, 2, 2, 0, /*
|
|
1509 Change the coding system associated with a coding category.
|
|
1510 */
|
|
1511 (coding_category, coding_system))
|
|
1512 {
|
|
1513 int cat = decode_coding_category (coding_category);
|
|
1514
|
|
1515 coding_system = Fget_coding_system (coding_system);
|
|
1516 fcd->coding_category_system[cat] = coding_system;
|
|
1517 return Qnil;
|
|
1518 }
|
|
1519
|
|
1520 DEFUN ("coding-category-system", Fcoding_category_system, 1, 1, 0, /*
|
|
1521 Return the coding system associated with a coding category.
|
|
1522 */
|
|
1523 (coding_category))
|
|
1524 {
|
|
1525 int cat = decode_coding_category (coding_category);
|
|
1526 Lisp_Object sys = fcd->coding_category_system[cat];
|
|
1527
|
|
1528 if (!NILP (sys))
|
|
1529 return XCODING_SYSTEM_NAME (sys);
|
|
1530 return Qnil;
|
|
1531 }
|
|
1532
|
|
1533
|
|
1534 /************************************************************************/
|
|
1535 /* Detecting the encoding of data */
|
|
1536 /************************************************************************/
|
|
1537
|
|
1538 struct detection_state
|
|
1539 {
|
438
|
1540 eol_type_t eol_type;
|
428
|
1541 int seen_non_ascii;
|
|
1542 int mask;
|
|
1543 #ifdef MULE
|
|
1544 struct
|
|
1545 {
|
|
1546 int mask;
|
|
1547 int in_second_byte;
|
|
1548 }
|
|
1549 big5;
|
|
1550
|
|
1551 struct
|
|
1552 {
|
|
1553 int mask;
|
|
1554 int in_second_byte;
|
|
1555 }
|
|
1556 shift_jis;
|
|
1557
|
|
1558 struct
|
|
1559 {
|
|
1560 int mask;
|
|
1561 int in_byte;
|
|
1562 }
|
|
1563 ucs4;
|
|
1564
|
|
1565 struct
|
|
1566 {
|
|
1567 int mask;
|
|
1568 int in_byte;
|
|
1569 }
|
|
1570 utf8;
|
|
1571
|
|
1572 struct
|
|
1573 {
|
|
1574 int mask;
|
|
1575 int initted;
|
|
1576 struct iso2022_decoder iso;
|
|
1577 unsigned int flags;
|
|
1578 int high_byte_count;
|
|
1579 unsigned int saw_single_shift:1;
|
|
1580 }
|
|
1581 iso2022;
|
|
1582 #endif
|
|
1583 struct
|
|
1584 {
|
|
1585 int seen_anything;
|
|
1586 int just_saw_cr;
|
|
1587 }
|
|
1588 eol;
|
|
1589 };
|
|
1590
|
|
1591 static int
|
|
1592 acceptable_control_char_p (int c)
|
|
1593 {
|
|
1594 switch (c)
|
|
1595 {
|
|
1596 /* Allow and ignore control characters that you might
|
|
1597 reasonably see in a text file */
|
|
1598 case '\r':
|
|
1599 case '\n':
|
|
1600 case '\t':
|
|
1601 case 7: /* bell */
|
|
1602 case 8: /* backspace */
|
|
1603 case 11: /* vertical tab */
|
|
1604 case 12: /* form feed */
|
|
1605 case 26: /* MS-DOS C-z junk */
|
|
1606 case 31: /* '^_' -- for info */
|
|
1607 return 1;
|
|
1608 default:
|
|
1609 return 0;
|
|
1610 }
|
|
1611 }
|
|
1612
|
|
1613 static int
|
|
1614 mask_has_at_most_one_bit_p (int mask)
|
|
1615 {
|
|
1616 /* Perhaps the only thing useful you learn from intensive Microsoft
|
|
1617 technical interviews */
|
|
1618 return (mask & (mask - 1)) == 0;
|
|
1619 }
|
|
1620
|
438
|
1621 static eol_type_t
|
444
|
1622 detect_eol_type (struct detection_state *st, const Extbyte *src,
|
462
|
1623 Lstream_data_count n)
|
428
|
1624 {
|
|
1625 while (n--)
|
|
1626 {
|
444
|
1627 unsigned char c = *(unsigned char *)src++;
|
428
|
1628 if (c == '\n')
|
|
1629 {
|
|
1630 if (st->eol.just_saw_cr)
|
|
1631 return EOL_CRLF;
|
|
1632 else if (st->eol.seen_anything)
|
|
1633 return EOL_LF;
|
|
1634 }
|
|
1635 else if (st->eol.just_saw_cr)
|
|
1636 return EOL_CR;
|
|
1637 else if (c == '\r')
|
|
1638 st->eol.just_saw_cr = 1;
|
|
1639 else
|
|
1640 st->eol.just_saw_cr = 0;
|
|
1641 st->eol.seen_anything = 1;
|
|
1642 }
|
|
1643
|
|
1644 return EOL_AUTODETECT;
|
|
1645 }
|
|
1646
|
|
1647 /* Attempt to determine the encoding and EOL type of the given text.
|
|
1648 Before calling this function for the first type, you must initialize
|
|
1649 st->eol_type as appropriate and initialize st->mask to ~0.
|
|
1650
|
|
1651 st->eol_type holds the determined EOL type, or EOL_AUTODETECT if
|
|
1652 not yet known.
|
|
1653
|
|
1654 st->mask holds the determined coding category mask, or ~0 if only
|
|
1655 ASCII has been seen so far.
|
|
1656
|
|
1657 Returns:
|
|
1658
|
|
1659 0 == st->eol_type is EOL_AUTODETECT and/or more than coding category
|
|
1660 is present in st->mask
|
|
1661 1 == definitive answers are here for both st->eol_type and st->mask
|
|
1662 */
|
|
1663
|
|
1664 static int
|
442
|
1665 detect_coding_type (struct detection_state *st, const Extbyte *src,
|
462
|
1666 Lstream_data_count n, int just_do_eol)
|
428
|
1667 {
|
|
1668 if (st->eol_type == EOL_AUTODETECT)
|
|
1669 st->eol_type = detect_eol_type (st, src, n);
|
|
1670
|
|
1671 if (just_do_eol)
|
|
1672 return st->eol_type != EOL_AUTODETECT;
|
|
1673
|
|
1674 if (!st->seen_non_ascii)
|
|
1675 {
|
|
1676 for (; n; n--, src++)
|
|
1677 {
|
444
|
1678 unsigned char c = *(unsigned char *) src;
|
428
|
1679 if ((c < 0x20 && !acceptable_control_char_p (c)) || c >= 0x80)
|
|
1680 {
|
|
1681 st->seen_non_ascii = 1;
|
|
1682 #ifdef MULE
|
|
1683 st->shift_jis.mask = ~0;
|
|
1684 st->big5.mask = ~0;
|
|
1685 st->ucs4.mask = ~0;
|
|
1686 st->utf8.mask = ~0;
|
|
1687 st->iso2022.mask = ~0;
|
|
1688 #endif
|
|
1689 break;
|
|
1690 }
|
|
1691 }
|
|
1692 }
|
|
1693
|
|
1694 if (!n)
|
|
1695 return 0;
|
|
1696 #ifdef MULE
|
|
1697 if (!mask_has_at_most_one_bit_p (st->iso2022.mask))
|
|
1698 st->iso2022.mask = detect_coding_iso2022 (st, src, n);
|
|
1699 if (!mask_has_at_most_one_bit_p (st->shift_jis.mask))
|
|
1700 st->shift_jis.mask = detect_coding_sjis (st, src, n);
|
|
1701 if (!mask_has_at_most_one_bit_p (st->big5.mask))
|
|
1702 st->big5.mask = detect_coding_big5 (st, src, n);
|
|
1703 if (!mask_has_at_most_one_bit_p (st->utf8.mask))
|
|
1704 st->utf8.mask = detect_coding_utf8 (st, src, n);
|
|
1705 if (!mask_has_at_most_one_bit_p (st->ucs4.mask))
|
|
1706 st->ucs4.mask = detect_coding_ucs4 (st, src, n);
|
|
1707
|
|
1708 st->mask
|
|
1709 = st->iso2022.mask | st->shift_jis.mask | st->big5.mask
|
|
1710 | st->utf8.mask | st->ucs4.mask;
|
|
1711 #endif
|
|
1712 {
|
|
1713 int retval = mask_has_at_most_one_bit_p (st->mask);
|
|
1714 st->mask |= CODING_CATEGORY_NO_CONVERSION_MASK;
|
|
1715 return retval && st->eol_type != EOL_AUTODETECT;
|
|
1716 }
|
|
1717 }
|
|
1718
|
|
1719 static Lisp_Object
|
|
1720 coding_system_from_mask (int mask)
|
|
1721 {
|
|
1722 if (mask == ~0)
|
|
1723 {
|
|
1724 /* If the file was entirely or basically ASCII, use the
|
|
1725 default value of `buffer-file-coding-system'. */
|
|
1726 Lisp_Object retval =
|
|
1727 XBUFFER (Vbuffer_defaults)->buffer_file_coding_system;
|
|
1728 if (!NILP (retval))
|
|
1729 {
|
|
1730 retval = Ffind_coding_system (retval);
|
|
1731 if (NILP (retval))
|
|
1732 {
|
|
1733 warn_when_safe
|
|
1734 (Qbad_variable, Qwarning,
|
|
1735 "Invalid `default-buffer-file-coding-system', set to nil");
|
|
1736 XBUFFER (Vbuffer_defaults)->buffer_file_coding_system = Qnil;
|
|
1737 }
|
|
1738 }
|
|
1739 if (NILP (retval))
|
|
1740 retval = Fget_coding_system (Qraw_text);
|
|
1741 return retval;
|
|
1742 }
|
|
1743 else
|
|
1744 {
|
|
1745 int i;
|
|
1746 int cat = -1;
|
|
1747 #ifdef MULE
|
|
1748 mask = postprocess_iso2022_mask (mask);
|
|
1749 #endif
|
|
1750 /* Look through the coding categories by priority and find
|
|
1751 the first one that is allowed. */
|
448
|
1752 for (i = 0; i < CODING_CATEGORY_LAST; i++)
|
428
|
1753 {
|
|
1754 cat = fcd->coding_category_by_priority[i];
|
|
1755 if ((mask & (1 << cat)) &&
|
|
1756 !NILP (fcd->coding_category_system[cat]))
|
|
1757 break;
|
|
1758 }
|
|
1759 if (cat >= 0)
|
|
1760 return fcd->coding_category_system[cat];
|
|
1761 else
|
|
1762 return Fget_coding_system (Qraw_text);
|
|
1763 }
|
|
1764 }
|
|
1765
|
|
1766 /* Given a seekable read stream and potential coding system and EOL type
|
|
1767 as specified, do any autodetection that is called for. If the
|
|
1768 coding system and/or EOL type are not `autodetect', they will be left
|
|
1769 alone; but this function will never return an autodetect coding system
|
|
1770 or EOL type.
|
|
1771
|
|
1772 This function does not automatically fetch subsidiary coding systems;
|
|
1773 that should be unnecessary with the explicit eol-type argument. */
|
|
1774
|
|
1775 #define LENGTH(string_constant) (sizeof (string_constant) - 1)
|
|
1776
|
|
1777 void
|
|
1778 determine_real_coding_system (Lstream *stream, Lisp_Object *codesys_in_out,
|
438
|
1779 eol_type_t *eol_type_in_out)
|
428
|
1780 {
|
|
1781 struct detection_state decst;
|
|
1782
|
|
1783 if (*eol_type_in_out == EOL_AUTODETECT)
|
|
1784 *eol_type_in_out = XCODING_SYSTEM_EOL_TYPE (*codesys_in_out);
|
|
1785
|
|
1786 xzero (decst);
|
|
1787 decst.eol_type = *eol_type_in_out;
|
|
1788 decst.mask = ~0;
|
|
1789
|
|
1790 /* If autodetection is called for, do it now. */
|
|
1791 if (XCODING_SYSTEM_TYPE (*codesys_in_out) == CODESYS_AUTODETECT
|
|
1792 || *eol_type_in_out == EOL_AUTODETECT)
|
|
1793 {
|
|
1794 Extbyte buf[4096];
|
|
1795 Lisp_Object coding_system = Qnil;
|
|
1796 Extbyte *p;
|
462
|
1797 Lstream_data_count nread = Lstream_read (stream, buf, sizeof (buf));
|
428
|
1798 Extbyte *scan_end;
|
|
1799
|
|
1800 /* Look for initial "-*-"; mode line prefix */
|
|
1801 for (p = buf,
|
|
1802 scan_end = buf + nread - LENGTH ("-*-coding:?-*-");
|
|
1803 p <= scan_end
|
|
1804 && *p != '\n'
|
|
1805 && *p != '\r';
|
|
1806 p++)
|
|
1807 if (*p == '-' && *(p+1) == '*' && *(p+2) == '-')
|
|
1808 {
|
|
1809 Extbyte *local_vars_beg = p + 3;
|
|
1810 /* Look for final "-*-"; mode line suffix */
|
|
1811 for (p = local_vars_beg,
|
|
1812 scan_end = buf + nread - LENGTH ("-*-");
|
|
1813 p <= scan_end
|
|
1814 && *p != '\n'
|
|
1815 && *p != '\r';
|
|
1816 p++)
|
|
1817 if (*p == '-' && *(p+1) == '*' && *(p+2) == '-')
|
|
1818 {
|
|
1819 Extbyte *suffix = p;
|
|
1820 /* Look for "coding:" */
|
|
1821 for (p = local_vars_beg,
|
|
1822 scan_end = suffix - LENGTH ("coding:?");
|
|
1823 p <= scan_end;
|
|
1824 p++)
|
|
1825 if (memcmp ("coding:", p, LENGTH ("coding:")) == 0
|
|
1826 && (p == local_vars_beg
|
|
1827 || (*(p-1) == ' ' ||
|
|
1828 *(p-1) == '\t' ||
|
|
1829 *(p-1) == ';')))
|
|
1830 {
|
|
1831 Extbyte save;
|
|
1832 int n;
|
|
1833 p += LENGTH ("coding:");
|
|
1834 while (*p == ' ' || *p == '\t') p++;
|
|
1835
|
|
1836 /* Get coding system name */
|
|
1837 save = *suffix; *suffix = '\0';
|
|
1838 /* Characters valid in a MIME charset name (rfc 1521),
|
|
1839 and in a Lisp symbol name. */
|
|
1840 n = strspn ( (char *) p,
|
|
1841 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
1842 "abcdefghijklmnopqrstuvwxyz"
|
|
1843 "0123456789"
|
|
1844 "!$%&*+-.^_{|}~");
|
|
1845 *suffix = save;
|
|
1846 if (n > 0)
|
|
1847 {
|
|
1848 save = p[n]; p[n] = '\0';
|
|
1849 coding_system =
|
|
1850 Ffind_coding_system (intern ((char *) p));
|
|
1851 p[n] = save;
|
|
1852 }
|
|
1853 break;
|
|
1854 }
|
|
1855 break;
|
|
1856 }
|
|
1857 break;
|
|
1858 }
|
|
1859
|
|
1860 if (NILP (coding_system))
|
|
1861 do
|
|
1862 {
|
|
1863 if (detect_coding_type (&decst, buf, nread,
|
|
1864 XCODING_SYSTEM_TYPE (*codesys_in_out)
|
|
1865 != CODESYS_AUTODETECT))
|
|
1866 break;
|
|
1867 nread = Lstream_read (stream, buf, sizeof (buf));
|
|
1868 if (nread == 0)
|
|
1869 break;
|
|
1870 }
|
|
1871 while (1);
|
|
1872
|
|
1873 else if (XCODING_SYSTEM_TYPE (*codesys_in_out) == CODESYS_AUTODETECT
|
|
1874 && XCODING_SYSTEM_EOL_TYPE (coding_system) == EOL_AUTODETECT)
|
|
1875 do
|
|
1876 {
|
|
1877 if (detect_coding_type (&decst, buf, nread, 1))
|
|
1878 break;
|
|
1879 nread = Lstream_read (stream, buf, sizeof (buf));
|
|
1880 if (!nread)
|
|
1881 break;
|
|
1882 }
|
|
1883 while (1);
|
|
1884
|
|
1885 *eol_type_in_out = decst.eol_type;
|
|
1886 if (XCODING_SYSTEM_TYPE (*codesys_in_out) == CODESYS_AUTODETECT)
|
|
1887 {
|
|
1888 if (NILP (coding_system))
|
|
1889 *codesys_in_out = coding_system_from_mask (decst.mask);
|
|
1890 else
|
|
1891 *codesys_in_out = coding_system;
|
|
1892 }
|
|
1893 }
|
|
1894
|
|
1895 /* If we absolutely can't determine the EOL type, just assume LF. */
|
|
1896 if (*eol_type_in_out == EOL_AUTODETECT)
|
|
1897 *eol_type_in_out = EOL_LF;
|
|
1898
|
|
1899 Lstream_rewind (stream);
|
|
1900 }
|
|
1901
|
|
1902 DEFUN ("detect-coding-region", Fdetect_coding_region, 2, 3, 0, /*
|
|
1903 Detect coding system of the text in the region between START and END.
|
444
|
1904 Return a list of possible coding systems ordered by priority.
|
|
1905 If only ASCII characters are found, return 'undecided or one of
|
428
|
1906 its subsidiary coding systems according to a detected end-of-line
|
|
1907 type. Optional arg BUFFER defaults to the current buffer.
|
|
1908 */
|
|
1909 (start, end, buffer))
|
|
1910 {
|
|
1911 Lisp_Object val = Qnil;
|
|
1912 struct buffer *buf = decode_buffer (buffer, 0);
|
|
1913 Bufpos b, e;
|
|
1914 Lisp_Object instream, lb_instream;
|
|
1915 Lstream *istr, *lb_istr;
|
|
1916 struct detection_state decst;
|
|
1917 struct gcpro gcpro1, gcpro2;
|
|
1918
|
|
1919 get_buffer_range_char (buf, start, end, &b, &e, 0);
|
|
1920 lb_instream = make_lisp_buffer_input_stream (buf, b, e, 0);
|
|
1921 lb_istr = XLSTREAM (lb_instream);
|
|
1922 instream = make_encoding_input_stream (lb_istr, Fget_coding_system (Qbinary));
|
|
1923 istr = XLSTREAM (instream);
|
|
1924 GCPRO2 (instream, lb_instream);
|
|
1925 xzero (decst);
|
|
1926 decst.eol_type = EOL_AUTODETECT;
|
|
1927 decst.mask = ~0;
|
|
1928 while (1)
|
|
1929 {
|
444
|
1930 Extbyte random_buffer[4096];
|
462
|
1931 Lstream_data_count nread = Lstream_read (istr, random_buffer, sizeof (random_buffer));
|
428
|
1932
|
|
1933 if (!nread)
|
|
1934 break;
|
|
1935 if (detect_coding_type (&decst, random_buffer, nread, 0))
|
|
1936 break;
|
|
1937 }
|
|
1938
|
|
1939 if (decst.mask == ~0)
|
|
1940 val = subsidiary_coding_system (Fget_coding_system (Qundecided),
|
|
1941 decst.eol_type);
|
|
1942 else
|
|
1943 {
|
|
1944 int i;
|
|
1945
|
|
1946 val = Qnil;
|
|
1947 #ifdef MULE
|
|
1948 decst.mask = postprocess_iso2022_mask (decst.mask);
|
|
1949 #endif
|
448
|
1950 for (i = CODING_CATEGORY_LAST - 1; i >= 0; i--)
|
428
|
1951 {
|
|
1952 int sys = fcd->coding_category_by_priority[i];
|
|
1953 if (decst.mask & (1 << sys))
|
|
1954 {
|
|
1955 Lisp_Object codesys = fcd->coding_category_system[sys];
|
|
1956 if (!NILP (codesys))
|
|
1957 codesys = subsidiary_coding_system (codesys, decst.eol_type);
|
|
1958 val = Fcons (codesys, val);
|
|
1959 }
|
|
1960 }
|
|
1961 }
|
|
1962 Lstream_close (istr);
|
|
1963 UNGCPRO;
|
|
1964 Lstream_delete (istr);
|
|
1965 Lstream_delete (lb_istr);
|
|
1966 return val;
|
|
1967 }
|
|
1968
|
|
1969
|
|
1970 /************************************************************************/
|
|
1971 /* Converting to internal Mule format ("decoding") */
|
|
1972 /************************************************************************/
|
|
1973
|
|
1974 /* A decoding stream is a stream used for decoding text (i.e.
|
|
1975 converting from some external format to internal format).
|
|
1976 The decoding-stream object keeps track of the actual coding
|
|
1977 stream, the stream that is at the other end, and data that
|
|
1978 needs to be persistent across the lifetime of the stream. */
|
|
1979
|
|
1980 /* Handle the EOL stuff related to just-read-in character C.
|
|
1981 EOL_TYPE is the EOL type of the coding stream.
|
|
1982 FLAGS is the current value of FLAGS in the coding stream, and may
|
|
1983 be modified by this macro. (The macro only looks at the
|
|
1984 CODING_STATE_CR flag.) DST is the Dynarr to which the decoded
|
|
1985 bytes are to be written. You need to also define a local goto
|
|
1986 label "label_continue_loop" that is at the end of the main
|
|
1987 character-reading loop.
|
|
1988
|
|
1989 If C is a CR character, then this macro handles it entirely and
|
|
1990 jumps to label_continue_loop. Otherwise, this macro does not add
|
|
1991 anything to DST, and continues normally. You should continue
|
|
1992 processing C normally after this macro. */
|
|
1993
|
|
1994 #define DECODE_HANDLE_EOL_TYPE(eol_type, c, flags, dst) \
|
|
1995 do { \
|
|
1996 if (c == '\r') \
|
|
1997 { \
|
|
1998 if (eol_type == EOL_CR) \
|
|
1999 Dynarr_add (dst, '\n'); \
|
|
2000 else if (eol_type != EOL_CRLF || flags & CODING_STATE_CR) \
|
|
2001 Dynarr_add (dst, c); \
|
|
2002 else \
|
|
2003 flags |= CODING_STATE_CR; \
|
|
2004 goto label_continue_loop; \
|
|
2005 } \
|
|
2006 else if (flags & CODING_STATE_CR) \
|
|
2007 { /* eol_type == CODING_SYSTEM_EOL_CRLF */ \
|
|
2008 if (c != '\n') \
|
|
2009 Dynarr_add (dst, '\r'); \
|
|
2010 flags &= ~CODING_STATE_CR; \
|
|
2011 } \
|
|
2012 } while (0)
|
|
2013
|
|
2014 /* C should be a binary character in the range 0 - 255; convert
|
|
2015 to internal format and add to Dynarr DST. */
|
|
2016
|
|
2017 #define DECODE_ADD_BINARY_CHAR(c, dst) \
|
|
2018 do { \
|
|
2019 if (BYTE_ASCII_P (c)) \
|
|
2020 Dynarr_add (dst, c); \
|
|
2021 else if (BYTE_C1_P (c)) \
|
|
2022 { \
|
|
2023 Dynarr_add (dst, LEADING_BYTE_CONTROL_1); \
|
|
2024 Dynarr_add (dst, c + 0x20); \
|
|
2025 } \
|
|
2026 else \
|
|
2027 { \
|
|
2028 Dynarr_add (dst, LEADING_BYTE_LATIN_ISO8859_1); \
|
|
2029 Dynarr_add (dst, c); \
|
|
2030 } \
|
|
2031 } while (0)
|
|
2032
|
|
2033 #define DECODE_OUTPUT_PARTIAL_CHAR(ch) \
|
|
2034 do { \
|
|
2035 if (ch) \
|
|
2036 { \
|
|
2037 DECODE_ADD_BINARY_CHAR (ch, dst); \
|
|
2038 ch = 0; \
|
|
2039 } \
|
|
2040 } while (0)
|
|
2041
|
|
2042 #define DECODE_HANDLE_END_OF_CONVERSION(flags, ch, dst) \
|
|
2043 do { \
|
|
2044 if (flags & CODING_STATE_END) \
|
|
2045 { \
|
|
2046 DECODE_OUTPUT_PARTIAL_CHAR (ch); \
|
|
2047 if (flags & CODING_STATE_CR) \
|
|
2048 Dynarr_add (dst, '\r'); \
|
|
2049 } \
|
|
2050 } while (0)
|
|
2051
|
|
2052 #define DECODING_STREAM_DATA(stream) LSTREAM_TYPE_DATA (stream, decoding)
|
|
2053
|
|
2054 struct decoding_stream
|
|
2055 {
|
|
2056 /* Coding system that governs the conversion. */
|
|
2057 Lisp_Coding_System *codesys;
|
|
2058
|
|
2059 /* Stream that we read the encoded data from or
|
|
2060 write the decoded data to. */
|
|
2061 Lstream *other_end;
|
|
2062
|
|
2063 /* If we are reading, then we can return only a fixed amount of
|
|
2064 data, so if the conversion resulted in too much data, we store it
|
|
2065 here for retrieval the next time around. */
|
|
2066 unsigned_char_dynarr *runoff;
|
|
2067
|
|
2068 /* FLAGS holds flags indicating the current state of the decoding.
|
|
2069 Some of these flags are dependent on the coding system. */
|
|
2070 unsigned int flags;
|
|
2071
|
|
2072 /* CH holds a partially built-up character. Since we only deal
|
|
2073 with one- and two-byte characters at the moment, we only use
|
|
2074 this to store the first byte of a two-byte character. */
|
|
2075 unsigned int ch;
|
|
2076
|
|
2077 /* EOL_TYPE specifies the type of end-of-line conversion that
|
|
2078 currently applies. We need to keep this separate from the
|
|
2079 EOL type stored in CODESYS because the latter might indicate
|
|
2080 automatic EOL-type detection while the former will always
|
|
2081 indicate a particular EOL type. */
|
438
|
2082 eol_type_t eol_type;
|
428
|
2083 #ifdef MULE
|
|
2084 /* Additional ISO2022 information. We define the structure above
|
|
2085 because it's also needed by the detection routines. */
|
|
2086 struct iso2022_decoder iso2022;
|
|
2087
|
|
2088 /* Additional information (the state of the running CCL program)
|
|
2089 used by the CCL decoder. */
|
|
2090 struct ccl_program ccl;
|
|
2091
|
|
2092 /* counter for UTF-8 or UCS-4 */
|
|
2093 unsigned char counter;
|
|
2094 #endif
|
|
2095 struct detection_state decst;
|
|
2096 };
|
|
2097
|
462
|
2098 static Lstream_data_count decoding_reader (Lstream *stream,
|
|
2099 unsigned char *data, Lstream_data_count size);
|
|
2100 static Lstream_data_count decoding_writer (Lstream *stream,
|
|
2101 const unsigned char *data, Lstream_data_count size);
|
428
|
2102 static int decoding_rewinder (Lstream *stream);
|
|
2103 static int decoding_seekable_p (Lstream *stream);
|
|
2104 static int decoding_flusher (Lstream *stream);
|
|
2105 static int decoding_closer (Lstream *stream);
|
|
2106
|
|
2107 static Lisp_Object decoding_marker (Lisp_Object stream);
|
|
2108
|
|
2109 DEFINE_LSTREAM_IMPLEMENTATION ("decoding", lstream_decoding,
|
|
2110 sizeof (struct decoding_stream));
|
|
2111
|
|
2112 static Lisp_Object
|
|
2113 decoding_marker (Lisp_Object stream)
|
|
2114 {
|
|
2115 Lstream *str = DECODING_STREAM_DATA (XLSTREAM (stream))->other_end;
|
|
2116 Lisp_Object str_obj;
|
|
2117
|
|
2118 /* We do not need to mark the coding systems or charsets stored
|
|
2119 within the stream because they are stored in a global list
|
|
2120 and automatically marked. */
|
|
2121
|
|
2122 XSETLSTREAM (str_obj, str);
|
|
2123 mark_object (str_obj);
|
|
2124 if (str->imp->marker)
|
|
2125 return (str->imp->marker) (str_obj);
|
|
2126 else
|
|
2127 return Qnil;
|
|
2128 }
|
|
2129
|
|
2130 /* Read SIZE bytes of data and store it into DATA. We are a decoding stream
|
|
2131 so we read data from the other end, decode it, and store it into DATA. */
|
|
2132
|
462
|
2133 static Lstream_data_count
|
|
2134 decoding_reader (Lstream *stream, unsigned char *data, Lstream_data_count size)
|
428
|
2135 {
|
|
2136 struct decoding_stream *str = DECODING_STREAM_DATA (stream);
|
|
2137 unsigned char *orig_data = data;
|
462
|
2138 Lstream_data_count read_size;
|
428
|
2139 int error_occurred = 0;
|
|
2140
|
|
2141 /* We need to interface to mule_decode(), which expects to take some
|
|
2142 amount of data and store the result into a Dynarr. We have
|
|
2143 mule_decode() store into str->runoff, and take data from there
|
|
2144 as necessary. */
|
|
2145
|
|
2146 /* We loop until we have enough data, reading chunks from the other
|
|
2147 end and decoding it. */
|
|
2148 while (1)
|
|
2149 {
|
|
2150 /* Take data from the runoff if we can. Make sure to take at
|
|
2151 most SIZE bytes, and delete the data from the runoff. */
|
|
2152 if (Dynarr_length (str->runoff) > 0)
|
|
2153 {
|
462
|
2154 Lstream_data_count chunk = min (size, (Lstream_data_count) Dynarr_length (str->runoff));
|
428
|
2155 memcpy (data, Dynarr_atp (str->runoff, 0), chunk);
|
|
2156 Dynarr_delete_many (str->runoff, 0, chunk);
|
|
2157 data += chunk;
|
|
2158 size -= chunk;
|
|
2159 }
|
|
2160
|
|
2161 if (size == 0)
|
|
2162 break; /* No more room for data */
|
|
2163
|
|
2164 if (str->flags & CODING_STATE_END)
|
|
2165 /* This means that on the previous iteration, we hit the EOF on
|
|
2166 the other end. We loop once more so that mule_decode() can
|
|
2167 output any final stuff it may be holding, or any "go back
|
|
2168 to a sane state" escape sequences. (This latter makes sense
|
|
2169 during encoding.) */
|
|
2170 break;
|
|
2171
|
|
2172 /* Exhausted the runoff, so get some more. DATA has at least
|
|
2173 SIZE bytes left of storage in it, so it's OK to read directly
|
|
2174 into it. (We'll be overwriting above, after we've decoded it
|
|
2175 into the runoff.) */
|
|
2176 read_size = Lstream_read (str->other_end, data, size);
|
|
2177 if (read_size < 0)
|
|
2178 {
|
|
2179 error_occurred = 1;
|
|
2180 break;
|
|
2181 }
|
|
2182 if (read_size == 0)
|
|
2183 /* There might be some more end data produced in the translation.
|
|
2184 See the comment above. */
|
|
2185 str->flags |= CODING_STATE_END;
|
444
|
2186 mule_decode (stream, (Extbyte *) data, str->runoff, read_size);
|
428
|
2187 }
|
|
2188
|
|
2189 if (data - orig_data == 0)
|
|
2190 return error_occurred ? -1 : 0;
|
|
2191 else
|
|
2192 return data - orig_data;
|
|
2193 }
|
|
2194
|
462
|
2195 static Lstream_data_count
|
|
2196 decoding_writer (Lstream *stream, const unsigned char *data, Lstream_data_count size)
|
428
|
2197 {
|
|
2198 struct decoding_stream *str = DECODING_STREAM_DATA (stream);
|
462
|
2199 Lstream_data_count retval;
|
428
|
2200
|
|
2201 /* Decode all our data into the runoff, and then attempt to write
|
|
2202 it all out to the other end. Remove whatever chunk we succeeded
|
|
2203 in writing. */
|
444
|
2204 mule_decode (stream, (Extbyte *) data, str->runoff, size);
|
428
|
2205 retval = Lstream_write (str->other_end, Dynarr_atp (str->runoff, 0),
|
|
2206 Dynarr_length (str->runoff));
|
|
2207 if (retval > 0)
|
|
2208 Dynarr_delete_many (str->runoff, 0, retval);
|
|
2209 /* Do NOT return retval. The return value indicates how much
|
|
2210 of the incoming data was written, not how many bytes were
|
|
2211 written. */
|
|
2212 return size;
|
|
2213 }
|
|
2214
|
|
2215 static void
|
|
2216 reset_decoding_stream (struct decoding_stream *str)
|
|
2217 {
|
|
2218 #ifdef MULE
|
|
2219 if (CODING_SYSTEM_TYPE (str->codesys) == CODESYS_ISO2022)
|
|
2220 {
|
|
2221 Lisp_Object coding_system;
|
|
2222 XSETCODING_SYSTEM (coding_system, str->codesys);
|
|
2223 reset_iso2022 (coding_system, &str->iso2022);
|
|
2224 }
|
|
2225 else if (CODING_SYSTEM_TYPE (str->codesys) == CODESYS_CCL)
|
|
2226 {
|
|
2227 setup_ccl_program (&str->ccl, CODING_SYSTEM_CCL_DECODE (str->codesys));
|
|
2228 }
|
|
2229 str->counter = 0;
|
|
2230 #endif /* MULE */
|
442
|
2231 if (CODING_SYSTEM_TYPE (str->codesys) == CODESYS_AUTODETECT
|
|
2232 || CODING_SYSTEM_EOL_TYPE (str->codesys) == EOL_AUTODETECT)
|
|
2233 {
|
|
2234 xzero (str->decst);
|
|
2235 str->decst.eol_type = EOL_AUTODETECT;
|
|
2236 str->decst.mask = ~0;
|
|
2237 }
|
428
|
2238 str->flags = str->ch = 0;
|
|
2239 }
|
|
2240
|
|
2241 static int
|
|
2242 decoding_rewinder (Lstream *stream)
|
|
2243 {
|
|
2244 struct decoding_stream *str = DECODING_STREAM_DATA (stream);
|
|
2245 reset_decoding_stream (str);
|
|
2246 Dynarr_reset (str->runoff);
|
|
2247 return Lstream_rewind (str->other_end);
|
|
2248 }
|
|
2249
|
|
2250 static int
|
|
2251 decoding_seekable_p (Lstream *stream)
|
|
2252 {
|
|
2253 struct decoding_stream *str = DECODING_STREAM_DATA (stream);
|
|
2254 return Lstream_seekable_p (str->other_end);
|
|
2255 }
|
|
2256
|
|
2257 static int
|
|
2258 decoding_flusher (Lstream *stream)
|
|
2259 {
|
|
2260 struct decoding_stream *str = DECODING_STREAM_DATA (stream);
|
|
2261 return Lstream_flush (str->other_end);
|
|
2262 }
|
|
2263
|
|
2264 static int
|
|
2265 decoding_closer (Lstream *stream)
|
|
2266 {
|
|
2267 struct decoding_stream *str = DECODING_STREAM_DATA (stream);
|
|
2268 if (stream->flags & LSTREAM_FL_WRITE)
|
|
2269 {
|
|
2270 str->flags |= CODING_STATE_END;
|
|
2271 decoding_writer (stream, 0, 0);
|
|
2272 }
|
|
2273 Dynarr_free (str->runoff);
|
|
2274 #ifdef MULE
|
|
2275 #ifdef ENABLE_COMPOSITE_CHARS
|
|
2276 if (str->iso2022.composite_chars)
|
|
2277 Dynarr_free (str->iso2022.composite_chars);
|
|
2278 #endif
|
|
2279 #endif
|
|
2280 return Lstream_close (str->other_end);
|
|
2281 }
|
|
2282
|
|
2283 Lisp_Object
|
|
2284 decoding_stream_coding_system (Lstream *stream)
|
|
2285 {
|
|
2286 Lisp_Object coding_system;
|
|
2287 struct decoding_stream *str = DECODING_STREAM_DATA (stream);
|
|
2288
|
|
2289 XSETCODING_SYSTEM (coding_system, str->codesys);
|
|
2290 return subsidiary_coding_system (coding_system, str->eol_type);
|
|
2291 }
|
|
2292
|
|
2293 void
|
|
2294 set_decoding_stream_coding_system (Lstream *lstr, Lisp_Object codesys)
|
|
2295 {
|
|
2296 Lisp_Coding_System *cs = XCODING_SYSTEM (codesys);
|
|
2297 struct decoding_stream *str = DECODING_STREAM_DATA (lstr);
|
|
2298 str->codesys = cs;
|
|
2299 if (CODING_SYSTEM_EOL_TYPE (cs) != EOL_AUTODETECT)
|
|
2300 str->eol_type = CODING_SYSTEM_EOL_TYPE (cs);
|
|
2301 reset_decoding_stream (str);
|
|
2302 }
|
|
2303
|
|
2304 /* WARNING WARNING WARNING WARNING!!!!! If you open up a decoding
|
|
2305 stream for writing, no automatic code detection will be performed.
|
|
2306 The reason for this is that automatic code detection requires a
|
|
2307 seekable input. Things will also fail if you open a decoding
|
|
2308 stream for reading using a non-fully-specified coding system and
|
|
2309 a non-seekable input stream. */
|
|
2310
|
|
2311 static Lisp_Object
|
|
2312 make_decoding_stream_1 (Lstream *stream, Lisp_Object codesys,
|
442
|
2313 const char *mode)
|
428
|
2314 {
|
|
2315 Lstream *lstr = Lstream_new (lstream_decoding, mode);
|
|
2316 struct decoding_stream *str = DECODING_STREAM_DATA (lstr);
|
|
2317 Lisp_Object obj;
|
|
2318
|
|
2319 xzero (*str);
|
|
2320 str->other_end = stream;
|
|
2321 str->runoff = (unsigned_char_dynarr *) Dynarr_new (unsigned_char);
|
|
2322 str->eol_type = EOL_AUTODETECT;
|
|
2323 if (!strcmp (mode, "r")
|
|
2324 && Lstream_seekable_p (stream))
|
|
2325 /* We can determine the coding system now. */
|
|
2326 determine_real_coding_system (stream, &codesys, &str->eol_type);
|
|
2327 set_decoding_stream_coding_system (lstr, codesys);
|
|
2328 str->decst.eol_type = str->eol_type;
|
|
2329 str->decst.mask = ~0;
|
|
2330 XSETLSTREAM (obj, lstr);
|
|
2331 return obj;
|
|
2332 }
|
|
2333
|
|
2334 Lisp_Object
|
|
2335 make_decoding_input_stream (Lstream *stream, Lisp_Object codesys)
|
|
2336 {
|
|
2337 return make_decoding_stream_1 (stream, codesys, "r");
|
|
2338 }
|
|
2339
|
|
2340 Lisp_Object
|
|
2341 make_decoding_output_stream (Lstream *stream, Lisp_Object codesys)
|
|
2342 {
|
|
2343 return make_decoding_stream_1 (stream, codesys, "w");
|
|
2344 }
|
|
2345
|
|
2346 /* Note: the decode_coding_* functions all take the same
|
|
2347 arguments as mule_decode(), which is to say some SRC data of
|
|
2348 size N, which is to be stored into dynamic array DST.
|
|
2349 DECODING is the stream within which the decoding is
|
|
2350 taking place, but no data is actually read from or
|
|
2351 written to that stream; that is handled in decoding_reader()
|
|
2352 or decoding_writer(). This allows the same functions to
|
|
2353 be used for both reading and writing. */
|
|
2354
|
|
2355 static void
|
444
|
2356 mule_decode (Lstream *decoding, const Extbyte *src,
|
462
|
2357 unsigned_char_dynarr *dst, Lstream_data_count n)
|
428
|
2358 {
|
|
2359 struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
|
|
2360
|
|
2361 /* If necessary, do encoding-detection now. We do this when
|
|
2362 we're a writing stream or a non-seekable reading stream,
|
|
2363 meaning that we can't just process the whole input,
|
|
2364 rewind, and start over. */
|
|
2365
|
|
2366 if (CODING_SYSTEM_TYPE (str->codesys) == CODESYS_AUTODETECT ||
|
|
2367 str->eol_type == EOL_AUTODETECT)
|
|
2368 {
|
|
2369 Lisp_Object codesys;
|
|
2370
|
|
2371 XSETCODING_SYSTEM (codesys, str->codesys);
|
|
2372 detect_coding_type (&str->decst, src, n,
|
|
2373 CODING_SYSTEM_TYPE (str->codesys) !=
|
|
2374 CODESYS_AUTODETECT);
|
|
2375 if (CODING_SYSTEM_TYPE (str->codesys) == CODESYS_AUTODETECT &&
|
|
2376 str->decst.mask != ~0)
|
|
2377 /* #### This is cheesy. What we really ought to do is
|
|
2378 buffer up a certain amount of data so as to get a
|
|
2379 less random result. */
|
|
2380 codesys = coding_system_from_mask (str->decst.mask);
|
|
2381 str->eol_type = str->decst.eol_type;
|
|
2382 if (XCODING_SYSTEM (codesys) != str->codesys)
|
|
2383 {
|
|
2384 /* Preserve the CODING_STATE_END flag in case it was set.
|
|
2385 If we erase it, bad things might happen. */
|
|
2386 int was_end = str->flags & CODING_STATE_END;
|
|
2387 set_decoding_stream_coding_system (decoding, codesys);
|
|
2388 if (was_end)
|
|
2389 str->flags |= CODING_STATE_END;
|
|
2390 }
|
|
2391 }
|
|
2392
|
|
2393 switch (CODING_SYSTEM_TYPE (str->codesys))
|
|
2394 {
|
|
2395 #ifdef DEBUG_XEMACS
|
|
2396 case CODESYS_INTERNAL:
|
|
2397 Dynarr_add_many (dst, src, n);
|
|
2398 break;
|
|
2399 #endif
|
|
2400 case CODESYS_AUTODETECT:
|
|
2401 /* If we got this far and still haven't decided on the coding
|
|
2402 system, then do no conversion. */
|
|
2403 case CODESYS_NO_CONVERSION:
|
|
2404 decode_coding_no_conversion (decoding, src, dst, n);
|
|
2405 break;
|
|
2406 #ifdef MULE
|
|
2407 case CODESYS_SHIFT_JIS:
|
|
2408 decode_coding_sjis (decoding, src, dst, n);
|
|
2409 break;
|
|
2410 case CODESYS_BIG5:
|
|
2411 decode_coding_big5 (decoding, src, dst, n);
|
|
2412 break;
|
|
2413 case CODESYS_UCS4:
|
|
2414 decode_coding_ucs4 (decoding, src, dst, n);
|
|
2415 break;
|
|
2416 case CODESYS_UTF8:
|
|
2417 decode_coding_utf8 (decoding, src, dst, n);
|
|
2418 break;
|
|
2419 case CODESYS_CCL:
|
|
2420 str->ccl.last_block = str->flags & CODING_STATE_END;
|
444
|
2421 /* When applying ccl program to stream, MUST NOT set NULL
|
|
2422 pointer to src. */
|
|
2423 ccl_driver (&str->ccl, (src ? (unsigned char *)src : (unsigned char*)""),
|
|
2424 dst, n, 0, CCL_MODE_DECODING);
|
428
|
2425 break;
|
|
2426 case CODESYS_ISO2022:
|
|
2427 decode_coding_iso2022 (decoding, src, dst, n);
|
|
2428 break;
|
|
2429 #endif /* MULE */
|
|
2430 default:
|
|
2431 abort ();
|
|
2432 }
|
|
2433 }
|
|
2434
|
|
2435 DEFUN ("decode-coding-region", Fdecode_coding_region, 3, 4, 0, /*
|
|
2436 Decode the text between START and END which is encoded in CODING-SYSTEM.
|
|
2437 This is useful if you've read in encoded text from a file without decoding
|
|
2438 it (e.g. you read in a JIS-formatted file but used the `binary' or
|
|
2439 `no-conversion' coding system, so that it shows up as "^[$B!<!+^[(B").
|
|
2440 Return length of decoded text.
|
|
2441 BUFFER defaults to the current buffer if unspecified.
|
|
2442 */
|
|
2443 (start, end, coding_system, buffer))
|
|
2444 {
|
|
2445 Bufpos b, e;
|
|
2446 struct buffer *buf = decode_buffer (buffer, 0);
|
|
2447 Lisp_Object instream, lb_outstream, de_outstream, outstream;
|
|
2448 Lstream *istr, *ostr;
|
|
2449 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
2450
|
|
2451 get_buffer_range_char (buf, start, end, &b, &e, 0);
|
|
2452
|
|
2453 barf_if_buffer_read_only (buf, b, e);
|
|
2454
|
|
2455 coding_system = Fget_coding_system (coding_system);
|
|
2456 instream = make_lisp_buffer_input_stream (buf, b, e, 0);
|
|
2457 lb_outstream = make_lisp_buffer_output_stream (buf, b, 0);
|
|
2458 de_outstream = make_decoding_output_stream (XLSTREAM (lb_outstream),
|
|
2459 coding_system);
|
|
2460 outstream = make_encoding_output_stream (XLSTREAM (de_outstream),
|
|
2461 Fget_coding_system (Qbinary));
|
|
2462 istr = XLSTREAM (instream);
|
|
2463 ostr = XLSTREAM (outstream);
|
|
2464 GCPRO4 (instream, lb_outstream, de_outstream, outstream);
|
|
2465
|
|
2466 /* The chain of streams looks like this:
|
|
2467
|
|
2468 [BUFFER] <----- send through
|
|
2469 ------> [ENCODE AS BINARY]
|
|
2470 ------> [DECODE AS SPECIFIED]
|
|
2471 ------> [BUFFER]
|
|
2472 */
|
|
2473
|
|
2474 while (1)
|
|
2475 {
|
|
2476 char tempbuf[1024]; /* some random amount */
|
|
2477 Bufpos newpos, even_newer_pos;
|
|
2478 Bufpos oldpos = lisp_buffer_stream_startpos (istr);
|
462
|
2479 Lstream_data_count size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
|
428
|
2480
|
|
2481 if (!size_in_bytes)
|
|
2482 break;
|
|
2483 newpos = lisp_buffer_stream_startpos (istr);
|
|
2484 Lstream_write (ostr, tempbuf, size_in_bytes);
|
|
2485 even_newer_pos = lisp_buffer_stream_startpos (istr);
|
|
2486 buffer_delete_range (buf, even_newer_pos - (newpos - oldpos),
|
|
2487 even_newer_pos, 0);
|
|
2488 }
|
|
2489 Lstream_close (istr);
|
|
2490 Lstream_close (ostr);
|
|
2491 UNGCPRO;
|
|
2492 Lstream_delete (istr);
|
|
2493 Lstream_delete (ostr);
|
|
2494 Lstream_delete (XLSTREAM (de_outstream));
|
|
2495 Lstream_delete (XLSTREAM (lb_outstream));
|
|
2496 return Qnil;
|
|
2497 }
|
|
2498
|
|
2499
|
|
2500 /************************************************************************/
|
|
2501 /* Converting to an external encoding ("encoding") */
|
|
2502 /************************************************************************/
|
|
2503
|
|
2504 /* An encoding stream is an output stream. When you create the
|
|
2505 stream, you specify the coding system that governs the encoding
|
|
2506 and another stream that the resulting encoded data is to be
|
|
2507 sent to, and then start sending data to it. */
|
|
2508
|
|
2509 #define ENCODING_STREAM_DATA(stream) LSTREAM_TYPE_DATA (stream, encoding)
|
|
2510
|
|
2511 struct encoding_stream
|
|
2512 {
|
|
2513 /* Coding system that governs the conversion. */
|
|
2514 Lisp_Coding_System *codesys;
|
|
2515
|
|
2516 /* Stream that we read the encoded data from or
|
|
2517 write the decoded data to. */
|
|
2518 Lstream *other_end;
|
|
2519
|
|
2520 /* If we are reading, then we can return only a fixed amount of
|
|
2521 data, so if the conversion resulted in too much data, we store it
|
|
2522 here for retrieval the next time around. */
|
|
2523 unsigned_char_dynarr *runoff;
|
|
2524
|
|
2525 /* FLAGS holds flags indicating the current state of the encoding.
|
|
2526 Some of these flags are dependent on the coding system. */
|
|
2527 unsigned int flags;
|
|
2528
|
|
2529 /* CH holds a partially built-up character. Since we only deal
|
|
2530 with one- and two-byte characters at the moment, we only use
|
|
2531 this to store the first byte of a two-byte character. */
|
|
2532 unsigned int ch;
|
|
2533 #ifdef MULE
|
|
2534 /* Additional information used by the ISO2022 encoder. */
|
|
2535 struct
|
|
2536 {
|
|
2537 /* CHARSET holds the character sets currently assigned to the G0
|
|
2538 through G3 registers. It is initialized from the array
|
|
2539 INITIAL_CHARSET in CODESYS. */
|
|
2540 Lisp_Object charset[4];
|
|
2541
|
|
2542 /* Which registers are currently invoked into the left (GL) and
|
|
2543 right (GR) halves of the 8-bit encoding space? */
|
|
2544 int register_left, register_right;
|
|
2545
|
|
2546 /* Whether we need to explicitly designate the charset in the
|
|
2547 G? register before using it. It is initialized from the
|
|
2548 array FORCE_CHARSET_ON_OUTPUT in CODESYS. */
|
|
2549 unsigned char force_charset_on_output[4];
|
|
2550
|
|
2551 /* Other state variables that need to be preserved across
|
|
2552 invocations. */
|
|
2553 Lisp_Object current_charset;
|
|
2554 int current_half;
|
|
2555 int current_char_boundary;
|
|
2556 } iso2022;
|
|
2557
|
|
2558 /* Additional information (the state of the running CCL program)
|
|
2559 used by the CCL encoder. */
|
|
2560 struct ccl_program ccl;
|
|
2561 #endif /* MULE */
|
|
2562 };
|
|
2563
|
462
|
2564 static Lstream_data_count encoding_reader (Lstream *stream, unsigned char *data, Lstream_data_count size);
|
|
2565 static Lstream_data_count encoding_writer (Lstream *stream, const unsigned char *data,
|
|
2566 Lstream_data_count size);
|
428
|
2567 static int encoding_rewinder (Lstream *stream);
|
|
2568 static int encoding_seekable_p (Lstream *stream);
|
|
2569 static int encoding_flusher (Lstream *stream);
|
|
2570 static int encoding_closer (Lstream *stream);
|
|
2571
|
|
2572 static Lisp_Object encoding_marker (Lisp_Object stream);
|
|
2573
|
|
2574 DEFINE_LSTREAM_IMPLEMENTATION ("encoding", lstream_encoding,
|
|
2575 sizeof (struct encoding_stream));
|
|
2576
|
|
2577 static Lisp_Object
|
|
2578 encoding_marker (Lisp_Object stream)
|
|
2579 {
|
|
2580 Lstream *str = ENCODING_STREAM_DATA (XLSTREAM (stream))->other_end;
|
|
2581 Lisp_Object str_obj;
|
|
2582
|
|
2583 /* We do not need to mark the coding systems or charsets stored
|
|
2584 within the stream because they are stored in a global list
|
|
2585 and automatically marked. */
|
|
2586
|
|
2587 XSETLSTREAM (str_obj, str);
|
|
2588 mark_object (str_obj);
|
|
2589 if (str->imp->marker)
|
|
2590 return (str->imp->marker) (str_obj);
|
|
2591 else
|
|
2592 return Qnil;
|
|
2593 }
|
|
2594
|
|
2595 /* Read SIZE bytes of data and store it into DATA. We are a encoding stream
|
|
2596 so we read data from the other end, encode it, and store it into DATA. */
|
|
2597
|
462
|
2598 static Lstream_data_count
|
|
2599 encoding_reader (Lstream *stream, unsigned char *data, Lstream_data_count size)
|
428
|
2600 {
|
|
2601 struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
|
|
2602 unsigned char *orig_data = data;
|
462
|
2603 Lstream_data_count read_size;
|
428
|
2604 int error_occurred = 0;
|
|
2605
|
|
2606 /* We need to interface to mule_encode(), which expects to take some
|
|
2607 amount of data and store the result into a Dynarr. We have
|
|
2608 mule_encode() store into str->runoff, and take data from there
|
|
2609 as necessary. */
|
|
2610
|
|
2611 /* We loop until we have enough data, reading chunks from the other
|
|
2612 end and encoding it. */
|
|
2613 while (1)
|
|
2614 {
|
|
2615 /* Take data from the runoff if we can. Make sure to take at
|
|
2616 most SIZE bytes, and delete the data from the runoff. */
|
|
2617 if (Dynarr_length (str->runoff) > 0)
|
|
2618 {
|
|
2619 int chunk = min ((int) size, Dynarr_length (str->runoff));
|
|
2620 memcpy (data, Dynarr_atp (str->runoff, 0), chunk);
|
|
2621 Dynarr_delete_many (str->runoff, 0, chunk);
|
|
2622 data += chunk;
|
|
2623 size -= chunk;
|
|
2624 }
|
|
2625
|
|
2626 if (size == 0)
|
|
2627 break; /* No more room for data */
|
|
2628
|
|
2629 if (str->flags & CODING_STATE_END)
|
|
2630 /* This means that on the previous iteration, we hit the EOF on
|
|
2631 the other end. We loop once more so that mule_encode() can
|
|
2632 output any final stuff it may be holding, or any "go back
|
|
2633 to a sane state" escape sequences. (This latter makes sense
|
|
2634 during encoding.) */
|
|
2635 break;
|
|
2636
|
|
2637 /* Exhausted the runoff, so get some more. DATA at least SIZE bytes
|
|
2638 left of storage in it, so it's OK to read directly into it.
|
|
2639 (We'll be overwriting above, after we've encoded it into the
|
|
2640 runoff.) */
|
|
2641 read_size = Lstream_read (str->other_end, data, size);
|
|
2642 if (read_size < 0)
|
|
2643 {
|
|
2644 error_occurred = 1;
|
|
2645 break;
|
|
2646 }
|
|
2647 if (read_size == 0)
|
|
2648 /* There might be some more end data produced in the translation.
|
|
2649 See the comment above. */
|
|
2650 str->flags |= CODING_STATE_END;
|
|
2651 mule_encode (stream, data, str->runoff, read_size);
|
|
2652 }
|
|
2653
|
|
2654 if (data == orig_data)
|
|
2655 return error_occurred ? -1 : 0;
|
|
2656 else
|
|
2657 return data - orig_data;
|
|
2658 }
|
|
2659
|
462
|
2660 static Lstream_data_count
|
|
2661 encoding_writer (Lstream *stream, const unsigned char *data, Lstream_data_count size)
|
428
|
2662 {
|
|
2663 struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
|
462
|
2664 Lstream_data_count retval;
|
428
|
2665
|
|
2666 /* Encode all our data into the runoff, and then attempt to write
|
|
2667 it all out to the other end. Remove whatever chunk we succeeded
|
|
2668 in writing. */
|
|
2669 mule_encode (stream, data, str->runoff, size);
|
|
2670 retval = Lstream_write (str->other_end, Dynarr_atp (str->runoff, 0),
|
|
2671 Dynarr_length (str->runoff));
|
|
2672 if (retval > 0)
|
|
2673 Dynarr_delete_many (str->runoff, 0, retval);
|
|
2674 /* Do NOT return retval. The return value indicates how much
|
|
2675 of the incoming data was written, not how many bytes were
|
|
2676 written. */
|
|
2677 return size;
|
|
2678 }
|
|
2679
|
|
2680 static void
|
|
2681 reset_encoding_stream (struct encoding_stream *str)
|
|
2682 {
|
|
2683 #ifdef MULE
|
|
2684 switch (CODING_SYSTEM_TYPE (str->codesys))
|
|
2685 {
|
|
2686 case CODESYS_ISO2022:
|
|
2687 {
|
|
2688 int i;
|
|
2689
|
|
2690 for (i = 0; i < 4; i++)
|
|
2691 {
|
|
2692 str->iso2022.charset[i] =
|
|
2693 CODING_SYSTEM_ISO2022_INITIAL_CHARSET (str->codesys, i);
|
|
2694 str->iso2022.force_charset_on_output[i] =
|
|
2695 CODING_SYSTEM_ISO2022_FORCE_CHARSET_ON_OUTPUT (str->codesys, i);
|
|
2696 }
|
|
2697 str->iso2022.register_left = 0;
|
|
2698 str->iso2022.register_right = 1;
|
|
2699 str->iso2022.current_charset = Qnil;
|
|
2700 str->iso2022.current_half = 0;
|
|
2701 str->iso2022.current_char_boundary = 1;
|
|
2702 break;
|
|
2703 }
|
|
2704 case CODESYS_CCL:
|
|
2705 setup_ccl_program (&str->ccl, CODING_SYSTEM_CCL_ENCODE (str->codesys));
|
|
2706 break;
|
|
2707 default:
|
|
2708 break;
|
|
2709 }
|
|
2710 #endif /* MULE */
|
|
2711
|
|
2712 str->flags = str->ch = 0;
|
|
2713 }
|
|
2714
|
|
2715 static int
|
|
2716 encoding_rewinder (Lstream *stream)
|
|
2717 {
|
|
2718 struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
|
|
2719 reset_encoding_stream (str);
|
|
2720 Dynarr_reset (str->runoff);
|
|
2721 return Lstream_rewind (str->other_end);
|
|
2722 }
|
|
2723
|
|
2724 static int
|
|
2725 encoding_seekable_p (Lstream *stream)
|
|
2726 {
|
|
2727 struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
|
|
2728 return Lstream_seekable_p (str->other_end);
|
|
2729 }
|
|
2730
|
|
2731 static int
|
|
2732 encoding_flusher (Lstream *stream)
|
|
2733 {
|
|
2734 struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
|
|
2735 return Lstream_flush (str->other_end);
|
|
2736 }
|
|
2737
|
|
2738 static int
|
|
2739 encoding_closer (Lstream *stream)
|
|
2740 {
|
|
2741 struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
|
|
2742 if (stream->flags & LSTREAM_FL_WRITE)
|
|
2743 {
|
|
2744 str->flags |= CODING_STATE_END;
|
|
2745 encoding_writer (stream, 0, 0);
|
|
2746 }
|
|
2747 Dynarr_free (str->runoff);
|
|
2748 return Lstream_close (str->other_end);
|
|
2749 }
|
|
2750
|
|
2751 Lisp_Object
|
|
2752 encoding_stream_coding_system (Lstream *stream)
|
|
2753 {
|
|
2754 Lisp_Object coding_system;
|
|
2755 struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
|
|
2756
|
|
2757 XSETCODING_SYSTEM (coding_system, str->codesys);
|
|
2758 return coding_system;
|
|
2759 }
|
|
2760
|
|
2761 void
|
|
2762 set_encoding_stream_coding_system (Lstream *lstr, Lisp_Object codesys)
|
|
2763 {
|
|
2764 Lisp_Coding_System *cs = XCODING_SYSTEM (codesys);
|
|
2765 struct encoding_stream *str = ENCODING_STREAM_DATA (lstr);
|
|
2766 str->codesys = cs;
|
|
2767 reset_encoding_stream (str);
|
|
2768 }
|
|
2769
|
|
2770 static Lisp_Object
|
|
2771 make_encoding_stream_1 (Lstream *stream, Lisp_Object codesys,
|
442
|
2772 const char *mode)
|
428
|
2773 {
|
|
2774 Lstream *lstr = Lstream_new (lstream_encoding, mode);
|
|
2775 struct encoding_stream *str = ENCODING_STREAM_DATA (lstr);
|
|
2776 Lisp_Object obj;
|
|
2777
|
|
2778 xzero (*str);
|
|
2779 str->runoff = Dynarr_new (unsigned_char);
|
|
2780 str->other_end = stream;
|
|
2781 set_encoding_stream_coding_system (lstr, codesys);
|
|
2782 XSETLSTREAM (obj, lstr);
|
|
2783 return obj;
|
|
2784 }
|
|
2785
|
|
2786 Lisp_Object
|
|
2787 make_encoding_input_stream (Lstream *stream, Lisp_Object codesys)
|
|
2788 {
|
|
2789 return make_encoding_stream_1 (stream, codesys, "r");
|
|
2790 }
|
|
2791
|
|
2792 Lisp_Object
|
|
2793 make_encoding_output_stream (Lstream *stream, Lisp_Object codesys)
|
|
2794 {
|
|
2795 return make_encoding_stream_1 (stream, codesys, "w");
|
|
2796 }
|
|
2797
|
|
2798 /* Convert N bytes of internally-formatted data stored in SRC to an
|
|
2799 external format, according to the encoding stream ENCODING.
|
|
2800 Store the encoded data into DST. */
|
|
2801
|
|
2802 static void
|
444
|
2803 mule_encode (Lstream *encoding, const Bufbyte *src,
|
462
|
2804 unsigned_char_dynarr *dst, Lstream_data_count n)
|
428
|
2805 {
|
|
2806 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
|
|
2807
|
|
2808 switch (CODING_SYSTEM_TYPE (str->codesys))
|
|
2809 {
|
|
2810 #ifdef DEBUG_XEMACS
|
|
2811 case CODESYS_INTERNAL:
|
|
2812 Dynarr_add_many (dst, src, n);
|
|
2813 break;
|
|
2814 #endif
|
|
2815 case CODESYS_AUTODETECT:
|
|
2816 /* If we got this far and still haven't decided on the coding
|
|
2817 system, then do no conversion. */
|
|
2818 case CODESYS_NO_CONVERSION:
|
|
2819 encode_coding_no_conversion (encoding, src, dst, n);
|
|
2820 break;
|
|
2821 #ifdef MULE
|
|
2822 case CODESYS_SHIFT_JIS:
|
|
2823 encode_coding_sjis (encoding, src, dst, n);
|
|
2824 break;
|
|
2825 case CODESYS_BIG5:
|
|
2826 encode_coding_big5 (encoding, src, dst, n);
|
|
2827 break;
|
|
2828 case CODESYS_UCS4:
|
|
2829 encode_coding_ucs4 (encoding, src, dst, n);
|
|
2830 break;
|
|
2831 case CODESYS_UTF8:
|
|
2832 encode_coding_utf8 (encoding, src, dst, n);
|
|
2833 break;
|
|
2834 case CODESYS_CCL:
|
|
2835 str->ccl.last_block = str->flags & CODING_STATE_END;
|
444
|
2836 /* When applying ccl program to stream, MUST NOT set NULL
|
|
2837 pointer to src. */
|
|
2838 ccl_driver (&str->ccl, ((src) ? src : (unsigned char*)""),
|
|
2839 dst, n, 0, CCL_MODE_ENCODING);
|
428
|
2840 break;
|
|
2841 case CODESYS_ISO2022:
|
|
2842 encode_coding_iso2022 (encoding, src, dst, n);
|
|
2843 break;
|
|
2844 #endif /* MULE */
|
|
2845 default:
|
|
2846 abort ();
|
|
2847 }
|
|
2848 }
|
|
2849
|
|
2850 DEFUN ("encode-coding-region", Fencode_coding_region, 3, 4, 0, /*
|
|
2851 Encode the text between START and END using CODING-SYSTEM.
|
|
2852 This will, for example, convert Japanese characters into stuff such as
|
|
2853 "^[$B!<!+^[(B" if you use the JIS encoding. Return length of encoded
|
|
2854 text. BUFFER defaults to the current buffer if unspecified.
|
|
2855 */
|
|
2856 (start, end, coding_system, buffer))
|
|
2857 {
|
|
2858 Bufpos b, e;
|
|
2859 struct buffer *buf = decode_buffer (buffer, 0);
|
|
2860 Lisp_Object instream, lb_outstream, de_outstream, outstream;
|
|
2861 Lstream *istr, *ostr;
|
|
2862 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
2863
|
|
2864 get_buffer_range_char (buf, start, end, &b, &e, 0);
|
|
2865
|
|
2866 barf_if_buffer_read_only (buf, b, e);
|
|
2867
|
|
2868 coding_system = Fget_coding_system (coding_system);
|
|
2869 instream = make_lisp_buffer_input_stream (buf, b, e, 0);
|
|
2870 lb_outstream = make_lisp_buffer_output_stream (buf, b, 0);
|
|
2871 de_outstream = make_decoding_output_stream (XLSTREAM (lb_outstream),
|
|
2872 Fget_coding_system (Qbinary));
|
|
2873 outstream = make_encoding_output_stream (XLSTREAM (de_outstream),
|
|
2874 coding_system);
|
|
2875 istr = XLSTREAM (instream);
|
|
2876 ostr = XLSTREAM (outstream);
|
|
2877 GCPRO4 (instream, outstream, de_outstream, lb_outstream);
|
|
2878 /* The chain of streams looks like this:
|
|
2879
|
|
2880 [BUFFER] <----- send through
|
|
2881 ------> [ENCODE AS SPECIFIED]
|
|
2882 ------> [DECODE AS BINARY]
|
|
2883 ------> [BUFFER]
|
|
2884 */
|
|
2885 while (1)
|
|
2886 {
|
|
2887 char tempbuf[1024]; /* some random amount */
|
|
2888 Bufpos newpos, even_newer_pos;
|
|
2889 Bufpos oldpos = lisp_buffer_stream_startpos (istr);
|
462
|
2890 Lstream_data_count size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
|
428
|
2891
|
|
2892 if (!size_in_bytes)
|
|
2893 break;
|
|
2894 newpos = lisp_buffer_stream_startpos (istr);
|
|
2895 Lstream_write (ostr, tempbuf, size_in_bytes);
|
|
2896 even_newer_pos = lisp_buffer_stream_startpos (istr);
|
|
2897 buffer_delete_range (buf, even_newer_pos - (newpos - oldpos),
|
|
2898 even_newer_pos, 0);
|
|
2899 }
|
|
2900
|
|
2901 {
|
|
2902 Charcount retlen =
|
|
2903 lisp_buffer_stream_startpos (XLSTREAM (instream)) - b;
|
|
2904 Lstream_close (istr);
|
|
2905 Lstream_close (ostr);
|
|
2906 UNGCPRO;
|
|
2907 Lstream_delete (istr);
|
|
2908 Lstream_delete (ostr);
|
|
2909 Lstream_delete (XLSTREAM (de_outstream));
|
|
2910 Lstream_delete (XLSTREAM (lb_outstream));
|
|
2911 return make_int (retlen);
|
|
2912 }
|
|
2913 }
|
|
2914
|
|
2915 #ifdef MULE
|
|
2916
|
|
2917 /************************************************************************/
|
|
2918 /* Shift-JIS methods */
|
|
2919 /************************************************************************/
|
|
2920
|
|
2921 /* Shift-JIS is a coding system encoding three character sets: ASCII, right
|
|
2922 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
|
|
2923 as is. A character of JISX0201-Kana (DIMENSION1_CHARS94 character set) is
|
|
2924 encoded by "position-code + 0x80". A character of JISX0208
|
|
2925 (DIMENSION2_CHARS94 character set) is encoded in 2-byte but two
|
|
2926 position-codes are divided and shifted so that it fit in the range
|
|
2927 below.
|
|
2928
|
|
2929 --- CODE RANGE of Shift-JIS ---
|
|
2930 (character set) (range)
|
|
2931 ASCII 0x00 .. 0x7F
|
|
2932 JISX0201-Kana 0xA0 .. 0xDF
|
|
2933 JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xEF
|
|
2934 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
|
|
2935 -------------------------------
|
|
2936
|
|
2937 */
|
|
2938
|
|
2939 /* Is this the first byte of a Shift-JIS two-byte char? */
|
|
2940
|
|
2941 #define BYTE_SJIS_TWO_BYTE_1_P(c) \
|
|
2942 (((c) >= 0x81 && (c) <= 0x9F) || ((c) >= 0xE0 && (c) <= 0xEF))
|
|
2943
|
|
2944 /* Is this the second byte of a Shift-JIS two-byte char? */
|
|
2945
|
|
2946 #define BYTE_SJIS_TWO_BYTE_2_P(c) \
|
|
2947 (((c) >= 0x40 && (c) <= 0x7E) || ((c) >= 0x80 && (c) <= 0xFC))
|
|
2948
|
|
2949 #define BYTE_SJIS_KATAKANA_P(c) \
|
|
2950 ((c) >= 0xA1 && (c) <= 0xDF)
|
|
2951
|
|
2952 static int
|
462
|
2953 detect_coding_sjis (struct detection_state *st, const Extbyte *src, Lstream_data_count n)
|
428
|
2954 {
|
|
2955 while (n--)
|
|
2956 {
|
444
|
2957 unsigned char c = *(unsigned char *)src++;
|
428
|
2958 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
|
|
2959 return 0;
|
|
2960 if (st->shift_jis.in_second_byte)
|
|
2961 {
|
|
2962 st->shift_jis.in_second_byte = 0;
|
|
2963 if (c < 0x40)
|
|
2964 return 0;
|
|
2965 }
|
|
2966 else if ((c >= 0x80 && c < 0xA0) || c >= 0xE0)
|
|
2967 st->shift_jis.in_second_byte = 1;
|
|
2968 }
|
|
2969 return CODING_CATEGORY_SHIFT_JIS_MASK;
|
|
2970 }
|
|
2971
|
|
2972 /* Convert Shift-JIS data to internal format. */
|
|
2973
|
|
2974 static void
|
444
|
2975 decode_coding_sjis (Lstream *decoding, const Extbyte *src,
|
462
|
2976 unsigned_char_dynarr *dst, Lstream_data_count n)
|
428
|
2977 {
|
|
2978 struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
|
|
2979 unsigned int flags = str->flags;
|
|
2980 unsigned int ch = str->ch;
|
|
2981 eol_type_t eol_type = str->eol_type;
|
|
2982
|
|
2983 while (n--)
|
|
2984 {
|
444
|
2985 unsigned char c = *(unsigned char *)src++;
|
428
|
2986
|
|
2987 if (ch)
|
|
2988 {
|
|
2989 /* Previous character was first byte of Shift-JIS Kanji char. */
|
|
2990 if (BYTE_SJIS_TWO_BYTE_2_P (c))
|
|
2991 {
|
|
2992 unsigned char e1, e2;
|
|
2993
|
|
2994 Dynarr_add (dst, LEADING_BYTE_JAPANESE_JISX0208);
|
|
2995 DECODE_SJIS (ch, c, e1, e2);
|
|
2996 Dynarr_add (dst, e1);
|
|
2997 Dynarr_add (dst, e2);
|
|
2998 }
|
|
2999 else
|
|
3000 {
|
|
3001 DECODE_ADD_BINARY_CHAR (ch, dst);
|
|
3002 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
3003 }
|
|
3004 ch = 0;
|
|
3005 }
|
|
3006 else
|
|
3007 {
|
|
3008 DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
|
|
3009 if (BYTE_SJIS_TWO_BYTE_1_P (c))
|
|
3010 ch = c;
|
|
3011 else if (BYTE_SJIS_KATAKANA_P (c))
|
|
3012 {
|
|
3013 Dynarr_add (dst, LEADING_BYTE_KATAKANA_JISX0201);
|
|
3014 Dynarr_add (dst, c);
|
|
3015 }
|
|
3016 else
|
|
3017 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
3018 }
|
|
3019 label_continue_loop:;
|
|
3020 }
|
|
3021
|
|
3022 DECODE_HANDLE_END_OF_CONVERSION (flags, ch, dst);
|
|
3023
|
|
3024 str->flags = flags;
|
|
3025 str->ch = ch;
|
|
3026 }
|
|
3027
|
|
3028 /* Convert internally-formatted data to Shift-JIS. */
|
|
3029
|
|
3030 static void
|
444
|
3031 encode_coding_sjis (Lstream *encoding, const Bufbyte *src,
|
462
|
3032 unsigned_char_dynarr *dst, Lstream_data_count n)
|
428
|
3033 {
|
|
3034 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
|
|
3035 unsigned int flags = str->flags;
|
|
3036 unsigned int ch = str->ch;
|
|
3037 eol_type_t eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys);
|
|
3038
|
|
3039 while (n--)
|
|
3040 {
|
444
|
3041 Bufbyte c = *src++;
|
428
|
3042 if (c == '\n')
|
|
3043 {
|
|
3044 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
|
|
3045 Dynarr_add (dst, '\r');
|
|
3046 if (eol_type != EOL_CR)
|
|
3047 Dynarr_add (dst, '\n');
|
|
3048 ch = 0;
|
|
3049 }
|
|
3050 else if (BYTE_ASCII_P (c))
|
|
3051 {
|
|
3052 Dynarr_add (dst, c);
|
|
3053 ch = 0;
|
|
3054 }
|
|
3055 else if (BUFBYTE_LEADING_BYTE_P (c))
|
|
3056 ch = (c == LEADING_BYTE_KATAKANA_JISX0201 ||
|
|
3057 c == LEADING_BYTE_JAPANESE_JISX0208_1978 ||
|
|
3058 c == LEADING_BYTE_JAPANESE_JISX0208) ? c : 0;
|
|
3059 else if (ch)
|
|
3060 {
|
|
3061 if (ch == LEADING_BYTE_KATAKANA_JISX0201)
|
|
3062 {
|
|
3063 Dynarr_add (dst, c);
|
|
3064 ch = 0;
|
|
3065 }
|
|
3066 else if (ch == LEADING_BYTE_JAPANESE_JISX0208_1978 ||
|
|
3067 ch == LEADING_BYTE_JAPANESE_JISX0208)
|
|
3068 ch = c;
|
|
3069 else
|
|
3070 {
|
|
3071 unsigned char j1, j2;
|
|
3072 ENCODE_SJIS (ch, c, j1, j2);
|
|
3073 Dynarr_add (dst, j1);
|
|
3074 Dynarr_add (dst, j2);
|
|
3075 ch = 0;
|
|
3076 }
|
|
3077 }
|
|
3078 }
|
|
3079
|
|
3080 str->flags = flags;
|
|
3081 str->ch = ch;
|
|
3082 }
|
|
3083
|
|
3084 DEFUN ("decode-shift-jis-char", Fdecode_shift_jis_char, 1, 1, 0, /*
|
|
3085 Decode a JISX0208 character of Shift-JIS coding-system.
|
|
3086 CODE is the character code in Shift-JIS as a cons of type bytes.
|
|
3087 Return the corresponding character.
|
|
3088 */
|
|
3089 (code))
|
|
3090 {
|
|
3091 unsigned char c1, c2, s1, s2;
|
|
3092
|
|
3093 CHECK_CONS (code);
|
|
3094 CHECK_INT (XCAR (code));
|
|
3095 CHECK_INT (XCDR (code));
|
|
3096 s1 = XINT (XCAR (code));
|
|
3097 s2 = XINT (XCDR (code));
|
|
3098 if (BYTE_SJIS_TWO_BYTE_1_P (s1) &&
|
|
3099 BYTE_SJIS_TWO_BYTE_2_P (s2))
|
|
3100 {
|
|
3101 DECODE_SJIS (s1, s2, c1, c2);
|
|
3102 return make_char (MAKE_CHAR (Vcharset_japanese_jisx0208,
|
|
3103 c1 & 0x7F, c2 & 0x7F));
|
|
3104 }
|
|
3105 else
|
|
3106 return Qnil;
|
|
3107 }
|
|
3108
|
|
3109 DEFUN ("encode-shift-jis-char", Fencode_shift_jis_char, 1, 1, 0, /*
|
444
|
3110 Encode a JISX0208 character CHARACTER to SHIFT-JIS coding-system.
|
428
|
3111 Return the corresponding character code in SHIFT-JIS as a cons of two bytes.
|
|
3112 */
|
444
|
3113 (character))
|
428
|
3114 {
|
|
3115 Lisp_Object charset;
|
|
3116 int c1, c2, s1, s2;
|
|
3117
|
444
|
3118 CHECK_CHAR_COERCE_INT (character);
|
|
3119 BREAKUP_CHAR (XCHAR (character), charset, c1, c2);
|
428
|
3120 if (EQ (charset, Vcharset_japanese_jisx0208))
|
|
3121 {
|
|
3122 ENCODE_SJIS (c1 | 0x80, c2 | 0x80, s1, s2);
|
|
3123 return Fcons (make_int (s1), make_int (s2));
|
|
3124 }
|
|
3125 else
|
|
3126 return Qnil;
|
|
3127 }
|
|
3128
|
|
3129
|
|
3130 /************************************************************************/
|
|
3131 /* Big5 methods */
|
|
3132 /************************************************************************/
|
|
3133
|
|
3134 /* BIG5 is a coding system encoding two character sets: ASCII and
|
|
3135 Big5. An ASCII character is encoded as is. Big5 is a two-byte
|
|
3136 character set and is encoded in two-byte.
|
|
3137
|
|
3138 --- CODE RANGE of BIG5 ---
|
|
3139 (character set) (range)
|
|
3140 ASCII 0x00 .. 0x7F
|
|
3141 Big5 (1st byte) 0xA1 .. 0xFE
|
|
3142 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
|
|
3143 --------------------------
|
|
3144
|
|
3145 Since the number of characters in Big5 is larger than maximum
|
|
3146 characters in Emacs' charset (96x96), it can't be handled as one
|
|
3147 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
|
|
3148 and `charset-big5-2'. Both <type>s are DIMENSION2_CHARS94. The former
|
|
3149 contains frequently used characters and the latter contains less
|
|
3150 frequently used characters. */
|
|
3151
|
|
3152 #define BYTE_BIG5_TWO_BYTE_1_P(c) \
|
|
3153 ((c) >= 0xA1 && (c) <= 0xFE)
|
|
3154
|
|
3155 /* Is this the second byte of a Shift-JIS two-byte char? */
|
|
3156
|
|
3157 #define BYTE_BIG5_TWO_BYTE_2_P(c) \
|
|
3158 (((c) >= 0x40 && (c) <= 0x7E) || ((c) >= 0xA1 && (c) <= 0xFE))
|
|
3159
|
|
3160 /* Number of Big5 characters which have the same code in 1st byte. */
|
|
3161
|
|
3162 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
|
|
3163
|
|
3164 /* Code conversion macros. These are macros because they are used in
|
|
3165 inner loops during code conversion.
|
|
3166
|
|
3167 Note that temporary variables in macros introduce the classic
|
|
3168 dynamic-scoping problems with variable names. We use capital-
|
|
3169 lettered variables in the assumption that XEmacs does not use
|
|
3170 capital letters in variables except in a very formalized way
|
|
3171 (e.g. Qstring). */
|
|
3172
|
|
3173 /* Convert Big5 code (b1, b2) into its internal string representation
|
|
3174 (lb, c1, c2). */
|
|
3175
|
|
3176 /* There is a much simpler way to split the Big5 charset into two.
|
|
3177 For the moment I'm going to leave the algorithm as-is because it
|
|
3178 claims to separate out the most-used characters into a single
|
|
3179 charset, which perhaps will lead to optimizations in various
|
|
3180 places.
|
|
3181
|
|
3182 The way the algorithm works is something like this:
|
|
3183
|
|
3184 Big5 can be viewed as a 94x157 charset, where the row is
|
|
3185 encoded into the bytes 0xA1 .. 0xFE and the column is encoded
|
|
3186 into the bytes 0x40 .. 0x7E and 0xA1 .. 0xFE. As for frequency,
|
|
3187 the split between low and high column numbers is apparently
|
|
3188 meaningless; ascending rows produce less and less frequent chars.
|
|
3189 Therefore, we assign the lower half of rows (0xA1 .. 0xC8) to
|
|
3190 the first charset, and the upper half (0xC9 .. 0xFE) to the
|
|
3191 second. To do the conversion, we convert the character into
|
|
3192 a single number where 0 .. 156 is the first row, 157 .. 313
|
|
3193 is the second, etc. That way, the characters are ordered by
|
|
3194 decreasing frequency. Then we just chop the space in two
|
|
3195 and coerce the result into a 94x94 space.
|
|
3196 */
|
|
3197
|
|
3198 #define DECODE_BIG5(b1, b2, lb, c1, c2) do \
|
|
3199 { \
|
|
3200 int B1 = b1, B2 = b2; \
|
|
3201 unsigned int I \
|
|
3202 = (B1 - 0xA1) * BIG5_SAME_ROW + B2 - (B2 < 0x7F ? 0x40 : 0x62); \
|
|
3203 \
|
|
3204 if (B1 < 0xC9) \
|
|
3205 { \
|
|
3206 lb = LEADING_BYTE_CHINESE_BIG5_1; \
|
|
3207 } \
|
|
3208 else \
|
|
3209 { \
|
|
3210 lb = LEADING_BYTE_CHINESE_BIG5_2; \
|
|
3211 I -= (BIG5_SAME_ROW) * (0xC9 - 0xA1); \
|
|
3212 } \
|
|
3213 c1 = I / (0xFF - 0xA1) + 0xA1; \
|
|
3214 c2 = I % (0xFF - 0xA1) + 0xA1; \
|
|
3215 } while (0)
|
|
3216
|
|
3217 /* Convert the internal string representation of a Big5 character
|
|
3218 (lb, c1, c2) into Big5 code (b1, b2). */
|
|
3219
|
|
3220 #define ENCODE_BIG5(lb, c1, c2, b1, b2) do \
|
|
3221 { \
|
|
3222 unsigned int I = ((c1) - 0xA1) * (0xFF - 0xA1) + ((c2) - 0xA1); \
|
|
3223 \
|
|
3224 if (lb == LEADING_BYTE_CHINESE_BIG5_2) \
|
|
3225 { \
|
|
3226 I += BIG5_SAME_ROW * (0xC9 - 0xA1); \
|
|
3227 } \
|
|
3228 b1 = I / BIG5_SAME_ROW + 0xA1; \
|
|
3229 b2 = I % BIG5_SAME_ROW; \
|
|
3230 b2 += b2 < 0x3F ? 0x40 : 0x62; \
|
|
3231 } while (0)
|
|
3232
|
|
3233 static int
|
462
|
3234 detect_coding_big5 (struct detection_state *st, const Extbyte *src, Lstream_data_count n)
|
428
|
3235 {
|
|
3236 while (n--)
|
|
3237 {
|
444
|
3238 unsigned char c = *(unsigned char *)src++;
|
428
|
3239 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO ||
|
|
3240 (c >= 0x80 && c <= 0xA0))
|
|
3241 return 0;
|
|
3242 if (st->big5.in_second_byte)
|
|
3243 {
|
|
3244 st->big5.in_second_byte = 0;
|
|
3245 if (c < 0x40 || (c >= 0x80 && c <= 0xA0))
|
|
3246 return 0;
|
|
3247 }
|
|
3248 else if (c >= 0xA1)
|
|
3249 st->big5.in_second_byte = 1;
|
|
3250 }
|
|
3251 return CODING_CATEGORY_BIG5_MASK;
|
|
3252 }
|
|
3253
|
|
3254 /* Convert Big5 data to internal format. */
|
|
3255
|
|
3256 static void
|
444
|
3257 decode_coding_big5 (Lstream *decoding, const Extbyte *src,
|
462
|
3258 unsigned_char_dynarr *dst, Lstream_data_count n)
|
428
|
3259 {
|
|
3260 struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
|
|
3261 unsigned int flags = str->flags;
|
|
3262 unsigned int ch = str->ch;
|
|
3263 eol_type_t eol_type = str->eol_type;
|
|
3264
|
|
3265 while (n--)
|
|
3266 {
|
444
|
3267 unsigned char c = *(unsigned char *)src++;
|
428
|
3268 if (ch)
|
|
3269 {
|
|
3270 /* Previous character was first byte of Big5 char. */
|
|
3271 if (BYTE_BIG5_TWO_BYTE_2_P (c))
|
|
3272 {
|
|
3273 unsigned char b1, b2, b3;
|
|
3274 DECODE_BIG5 (ch, c, b1, b2, b3);
|
|
3275 Dynarr_add (dst, b1);
|
|
3276 Dynarr_add (dst, b2);
|
|
3277 Dynarr_add (dst, b3);
|
|
3278 }
|
|
3279 else
|
|
3280 {
|
|
3281 DECODE_ADD_BINARY_CHAR (ch, dst);
|
|
3282 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
3283 }
|
|
3284 ch = 0;
|
|
3285 }
|
|
3286 else
|
|
3287 {
|
|
3288 DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
|
|
3289 if (BYTE_BIG5_TWO_BYTE_1_P (c))
|
|
3290 ch = c;
|
|
3291 else
|
|
3292 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
3293 }
|
|
3294 label_continue_loop:;
|
|
3295 }
|
|
3296
|
|
3297 DECODE_HANDLE_END_OF_CONVERSION (flags, ch, dst);
|
|
3298
|
|
3299 str->flags = flags;
|
|
3300 str->ch = ch;
|
|
3301 }
|
|
3302
|
|
3303 /* Convert internally-formatted data to Big5. */
|
|
3304
|
|
3305 static void
|
444
|
3306 encode_coding_big5 (Lstream *encoding, const Bufbyte *src,
|
462
|
3307 unsigned_char_dynarr *dst, Lstream_data_count n)
|
428
|
3308 {
|
|
3309 unsigned char c;
|
|
3310 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
|
|
3311 unsigned int flags = str->flags;
|
|
3312 unsigned int ch = str->ch;
|
|
3313 eol_type_t eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys);
|
|
3314
|
|
3315 while (n--)
|
|
3316 {
|
|
3317 c = *src++;
|
|
3318 if (c == '\n')
|
|
3319 {
|
|
3320 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
|
|
3321 Dynarr_add (dst, '\r');
|
|
3322 if (eol_type != EOL_CR)
|
|
3323 Dynarr_add (dst, '\n');
|
|
3324 }
|
|
3325 else if (BYTE_ASCII_P (c))
|
|
3326 {
|
|
3327 /* ASCII. */
|
|
3328 Dynarr_add (dst, c);
|
|
3329 }
|
|
3330 else if (BUFBYTE_LEADING_BYTE_P (c))
|
|
3331 {
|
|
3332 if (c == LEADING_BYTE_CHINESE_BIG5_1 ||
|
|
3333 c == LEADING_BYTE_CHINESE_BIG5_2)
|
|
3334 {
|
|
3335 /* A recognized leading byte. */
|
|
3336 ch = c;
|
|
3337 continue; /* not done with this character. */
|
|
3338 }
|
|
3339 /* otherwise just ignore this character. */
|
|
3340 }
|
|
3341 else if (ch == LEADING_BYTE_CHINESE_BIG5_1 ||
|
|
3342 ch == LEADING_BYTE_CHINESE_BIG5_2)
|
|
3343 {
|
|
3344 /* Previous char was a recognized leading byte. */
|
|
3345 ch = (ch << 8) | c;
|
|
3346 continue; /* not done with this character. */
|
|
3347 }
|
|
3348 else if (ch)
|
|
3349 {
|
|
3350 /* Encountering second byte of a Big5 character. */
|
|
3351 unsigned char b1, b2;
|
|
3352
|
|
3353 ENCODE_BIG5 (ch >> 8, ch & 0xFF, c, b1, b2);
|
|
3354 Dynarr_add (dst, b1);
|
|
3355 Dynarr_add (dst, b2);
|
|
3356 }
|
|
3357
|
|
3358 ch = 0;
|
|
3359 }
|
|
3360
|
|
3361 str->flags = flags;
|
|
3362 str->ch = ch;
|
|
3363 }
|
|
3364
|
|
3365
|
|
3366 DEFUN ("decode-big5-char", Fdecode_big5_char, 1, 1, 0, /*
|
|
3367 Decode a Big5 character CODE of BIG5 coding-system.
|
|
3368 CODE is the character code in BIG5, a cons of two integers.
|
|
3369 Return the corresponding character.
|
|
3370 */
|
|
3371 (code))
|
|
3372 {
|
|
3373 unsigned char c1, c2, b1, b2;
|
|
3374
|
|
3375 CHECK_CONS (code);
|
|
3376 CHECK_INT (XCAR (code));
|
|
3377 CHECK_INT (XCDR (code));
|
|
3378 b1 = XINT (XCAR (code));
|
|
3379 b2 = XINT (XCDR (code));
|
|
3380 if (BYTE_BIG5_TWO_BYTE_1_P (b1) &&
|
|
3381 BYTE_BIG5_TWO_BYTE_2_P (b2))
|
|
3382 {
|
|
3383 int leading_byte;
|
|
3384 Lisp_Object charset;
|
|
3385 DECODE_BIG5 (b1, b2, leading_byte, c1, c2);
|
|
3386 charset = CHARSET_BY_LEADING_BYTE (leading_byte);
|
|
3387 return make_char (MAKE_CHAR (charset, c1 & 0x7F, c2 & 0x7F));
|
|
3388 }
|
|
3389 else
|
|
3390 return Qnil;
|
|
3391 }
|
|
3392
|
|
3393 DEFUN ("encode-big5-char", Fencode_big5_char, 1, 1, 0, /*
|
444
|
3394 Encode the Big5 character CHARACTER in the BIG5 coding-system.
|
428
|
3395 Return the corresponding character code in Big5.
|
|
3396 */
|
444
|
3397 (character))
|
428
|
3398 {
|
|
3399 Lisp_Object charset;
|
|
3400 int c1, c2, b1, b2;
|
|
3401
|
444
|
3402 CHECK_CHAR_COERCE_INT (character);
|
|
3403 BREAKUP_CHAR (XCHAR (character), charset, c1, c2);
|
428
|
3404 if (EQ (charset, Vcharset_chinese_big5_1) ||
|
|
3405 EQ (charset, Vcharset_chinese_big5_2))
|
|
3406 {
|
|
3407 ENCODE_BIG5 (XCHARSET_LEADING_BYTE (charset), c1 | 0x80, c2 | 0x80,
|
|
3408 b1, b2);
|
|
3409 return Fcons (make_int (b1), make_int (b2));
|
|
3410 }
|
|
3411 else
|
|
3412 return Qnil;
|
|
3413 }
|
|
3414
|
|
3415
|
|
3416 /************************************************************************/
|
|
3417 /* UCS-4 methods */
|
|
3418 /* */
|
|
3419 /* UCS-4 character codes are implemented as nonnegative integers. */
|
|
3420 /* */
|
|
3421 /************************************************************************/
|
|
3422
|
|
3423
|
|
3424 DEFUN ("set-ucs-char", Fset_ucs_char, 2, 2, 0, /*
|
|
3425 Map UCS-4 code CODE to Mule character CHARACTER.
|
|
3426
|
|
3427 Return T on success, NIL on failure.
|
|
3428 */
|
|
3429 (code, character))
|
|
3430 {
|
442
|
3431 size_t c;
|
428
|
3432
|
|
3433 CHECK_CHAR (character);
|
442
|
3434 CHECK_NATNUM (code);
|
428
|
3435 c = XINT (code);
|
|
3436
|
442
|
3437 if (c < countof (fcd->ucs_to_mule_table))
|
428
|
3438 {
|
|
3439 fcd->ucs_to_mule_table[c] = character;
|
|
3440 return Qt;
|
|
3441 }
|
|
3442 else
|
|
3443 return Qnil;
|
|
3444 }
|
|
3445
|
|
3446 static Lisp_Object
|
|
3447 ucs_to_char (unsigned long code)
|
|
3448 {
|
442
|
3449 if (code < countof (fcd->ucs_to_mule_table))
|
428
|
3450 {
|
|
3451 return fcd->ucs_to_mule_table[code];
|
|
3452 }
|
|
3453 else if ((0xe00000 <= code) && (code <= 0xe00000 + 94 * 94 * 14))
|
|
3454 {
|
|
3455 unsigned int c;
|
|
3456
|
|
3457 code -= 0xe00000;
|
|
3458 c = code % (94 * 94);
|
|
3459 return make_char
|
|
3460 (MAKE_CHAR (CHARSET_BY_ATTRIBUTES
|
|
3461 (CHARSET_TYPE_94X94, code / (94 * 94) + '@',
|
|
3462 CHARSET_LEFT_TO_RIGHT),
|
|
3463 c / 94 + 33, c % 94 + 33));
|
|
3464 }
|
|
3465 else
|
|
3466 return Qnil;
|
|
3467 }
|
|
3468
|
|
3469 DEFUN ("ucs-char", Fucs_char, 1, 1, 0, /*
|
|
3470 Return Mule character corresponding to UCS code CODE (a positive integer).
|
|
3471 */
|
|
3472 (code))
|
|
3473 {
|
|
3474 CHECK_NATNUM (code);
|
|
3475 return ucs_to_char (XINT (code));
|
|
3476 }
|
|
3477
|
|
3478 DEFUN ("set-char-ucs", Fset_char_ucs, 2, 2, 0, /*
|
|
3479 Map Mule character CHARACTER to UCS code CODE (a positive integer).
|
|
3480 */
|
|
3481 (character, code))
|
|
3482 {
|
|
3483 /* #### Isn't this gilding the lily? Fput_char_table checks its args.
|
|
3484 Fset_char_ucs is more restrictive on index arg, but should
|
|
3485 check code arg in a char_table method. */
|
|
3486 CHECK_CHAR (character);
|
|
3487 CHECK_NATNUM (code);
|
|
3488 return Fput_char_table (character, code, mule_to_ucs_table);
|
|
3489 }
|
|
3490
|
|
3491 DEFUN ("char-ucs", Fchar_ucs, 1, 1, 0, /*
|
|
3492 Return the UCS code (a positive integer) corresponding to CHARACTER.
|
|
3493 */
|
|
3494 (character))
|
|
3495 {
|
|
3496 return Fget_char_table (character, mule_to_ucs_table);
|
|
3497 }
|
|
3498
|
|
3499 /* Decode a UCS-4 character into a buffer. If the lookup fails, use
|
|
3500 <GETA MARK> (U+3013) of JIS X 0208, which means correct character
|
|
3501 is not found, instead.
|
|
3502 #### do something more appropriate (use blob?)
|
|
3503 Danger, Will Robinson! Data loss. Should we signal user? */
|
|
3504 static void
|
|
3505 decode_ucs4 (unsigned long ch, unsigned_char_dynarr *dst)
|
|
3506 {
|
|
3507 Lisp_Object chr = ucs_to_char (ch);
|
|
3508
|
|
3509 if (! NILP (chr))
|
|
3510 {
|
|
3511 Bufbyte work[MAX_EMCHAR_LEN];
|
|
3512 int len;
|
|
3513
|
|
3514 ch = XCHAR (chr);
|
|
3515 len = (ch < 128) ?
|
|
3516 simple_set_charptr_emchar (work, ch) :
|
|
3517 non_ascii_set_charptr_emchar (work, ch);
|
|
3518 Dynarr_add_many (dst, work, len);
|
|
3519 }
|
|
3520 else
|
|
3521 {
|
|
3522 Dynarr_add (dst, LEADING_BYTE_JAPANESE_JISX0208);
|
|
3523 Dynarr_add (dst, 34 + 128);
|
|
3524 Dynarr_add (dst, 46 + 128);
|
|
3525 }
|
|
3526 }
|
|
3527
|
|
3528 static unsigned long
|
|
3529 mule_char_to_ucs4 (Lisp_Object charset,
|
|
3530 unsigned char h, unsigned char l)
|
|
3531 {
|
|
3532 Lisp_Object code
|
|
3533 = Fget_char_table (make_char (MAKE_CHAR (charset, h & 127, l & 127)),
|
|
3534 mule_to_ucs_table);
|
|
3535
|
|
3536 if (INTP (code))
|
|
3537 {
|
|
3538 return XINT (code);
|
|
3539 }
|
|
3540 else if ( (XCHARSET_DIMENSION (charset) == 2) &&
|
|
3541 (XCHARSET_CHARS (charset) == 94) )
|
|
3542 {
|
|
3543 unsigned char final = XCHARSET_FINAL (charset);
|
|
3544
|
|
3545 if ( ('@' <= final) && (final < 0x7f) )
|
|
3546 {
|
|
3547 return 0xe00000 + (final - '@') * 94 * 94
|
|
3548 + ((h & 127) - 33) * 94 + (l & 127) - 33;
|
|
3549 }
|
|
3550 else
|
|
3551 {
|
|
3552 return '?';
|
|
3553 }
|
|
3554 }
|
|
3555 else
|
|
3556 {
|
|
3557 return '?';
|
|
3558 }
|
|
3559 }
|
|
3560
|
|
3561 static void
|
|
3562 encode_ucs4 (Lisp_Object charset,
|
|
3563 unsigned char h, unsigned char l, unsigned_char_dynarr *dst)
|
|
3564 {
|
|
3565 unsigned long code = mule_char_to_ucs4 (charset, h, l);
|
|
3566 Dynarr_add (dst, code >> 24);
|
|
3567 Dynarr_add (dst, (code >> 16) & 255);
|
|
3568 Dynarr_add (dst, (code >> 8) & 255);
|
|
3569 Dynarr_add (dst, code & 255);
|
|
3570 }
|
|
3571
|
|
3572 static int
|
462
|
3573 detect_coding_ucs4 (struct detection_state *st, const Extbyte *src, Lstream_data_count n)
|
428
|
3574 {
|
|
3575 while (n--)
|
|
3576 {
|
444
|
3577 unsigned char c = *(unsigned char *)src++;
|
428
|
3578 switch (st->ucs4.in_byte)
|
|
3579 {
|
|
3580 case 0:
|
|
3581 if (c >= 128)
|
|
3582 return 0;
|
|
3583 else
|
|
3584 st->ucs4.in_byte++;
|
|
3585 break;
|
|
3586 case 3:
|
|
3587 st->ucs4.in_byte = 0;
|
|
3588 break;
|
|
3589 default:
|
|
3590 st->ucs4.in_byte++;
|
|
3591 }
|
|
3592 }
|
|
3593 return CODING_CATEGORY_UCS4_MASK;
|
|
3594 }
|
|
3595
|
|
3596 static void
|
444
|
3597 decode_coding_ucs4 (Lstream *decoding, const Extbyte *src,
|
462
|
3598 unsigned_char_dynarr *dst, Lstream_data_count n)
|
428
|
3599 {
|
|
3600 struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
|
|
3601 unsigned int flags = str->flags;
|
|
3602 unsigned int ch = str->ch;
|
|
3603 unsigned char counter = str->counter;
|
|
3604
|
|
3605 while (n--)
|
|
3606 {
|
444
|
3607 unsigned char c = *(unsigned char *)src++;
|
428
|
3608 switch (counter)
|
|
3609 {
|
|
3610 case 0:
|
|
3611 ch = c;
|
|
3612 counter = 3;
|
|
3613 break;
|
|
3614 case 1:
|
|
3615 decode_ucs4 ( ( ch << 8 ) | c, dst);
|
|
3616 ch = 0;
|
|
3617 counter = 0;
|
|
3618 break;
|
|
3619 default:
|
|
3620 ch = ( ch << 8 ) | c;
|
|
3621 counter--;
|
|
3622 }
|
|
3623 }
|
|
3624 if (counter & CODING_STATE_END)
|
|
3625 DECODE_OUTPUT_PARTIAL_CHAR (ch);
|
|
3626
|
|
3627 str->flags = flags;
|
|
3628 str->ch = ch;
|
|
3629 str->counter = counter;
|
|
3630 }
|
|
3631
|
|
3632 static void
|
444
|
3633 encode_coding_ucs4 (Lstream *encoding, const Bufbyte *src,
|
462
|
3634 unsigned_char_dynarr *dst, Lstream_data_count n)
|
428
|
3635 {
|
|
3636 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
|
|
3637 unsigned int flags = str->flags;
|
|
3638 unsigned int ch = str->ch;
|
|
3639 unsigned char char_boundary = str->iso2022.current_char_boundary;
|
|
3640 Lisp_Object charset = str->iso2022.current_charset;
|
|
3641
|
|
3642 #ifdef ENABLE_COMPOSITE_CHARS
|
|
3643 /* flags for handling composite chars. We do a little switcharoo
|
|
3644 on the source while we're outputting the composite char. */
|
|
3645 unsigned int saved_n = 0;
|
442
|
3646 const unsigned char *saved_src = NULL;
|
428
|
3647 int in_composite = 0;
|
|
3648
|
|
3649 back_to_square_n:
|
|
3650 #endif
|
|
3651
|
|
3652 while (n--)
|
|
3653 {
|
|
3654 unsigned char c = *src++;
|
|
3655
|
|
3656 if (BYTE_ASCII_P (c))
|
|
3657 { /* Processing ASCII character */
|
|
3658 ch = 0;
|
|
3659 encode_ucs4 (Vcharset_ascii, c, 0, dst);
|
|
3660 char_boundary = 1;
|
|
3661 }
|
|
3662 else if (BUFBYTE_LEADING_BYTE_P (c) || BUFBYTE_LEADING_BYTE_P (ch))
|
|
3663 { /* Processing Leading Byte */
|
|
3664 ch = 0;
|
|
3665 charset = CHARSET_BY_LEADING_BYTE (c);
|
|
3666 if (LEADING_BYTE_PREFIX_P(c))
|
|
3667 ch = c;
|
|
3668 char_boundary = 0;
|
|
3669 }
|
|
3670 else
|
|
3671 { /* Processing Non-ASCII character */
|
|
3672 char_boundary = 1;
|
|
3673 if (EQ (charset, Vcharset_control_1))
|
|
3674 {
|
|
3675 encode_ucs4 (Vcharset_control_1, c, 0, dst);
|
|
3676 }
|
|
3677 else
|
|
3678 {
|
|
3679 switch (XCHARSET_REP_BYTES (charset))
|
|
3680 {
|
|
3681 case 2:
|
|
3682 encode_ucs4 (charset, c, 0, dst);
|
|
3683 break;
|
|
3684 case 3:
|
|
3685 if (XCHARSET_PRIVATE_P (charset))
|
|
3686 {
|
|
3687 encode_ucs4 (charset, c, 0, dst);
|
|
3688 ch = 0;
|
|
3689 }
|
|
3690 else if (ch)
|
|
3691 {
|
|
3692 #ifdef ENABLE_COMPOSITE_CHARS
|
|
3693 if (EQ (charset, Vcharset_composite))
|
|
3694 {
|
|
3695 if (in_composite)
|
|
3696 {
|
|
3697 /* #### Bother! We don't know how to
|
|
3698 handle this yet. */
|
444
|
3699 Dynarr_add (dst, '\0');
|
|
3700 Dynarr_add (dst, '\0');
|
|
3701 Dynarr_add (dst, '\0');
|
428
|
3702 Dynarr_add (dst, '~');
|
|
3703 }
|
|
3704 else
|
|
3705 {
|
|
3706 Emchar emch = MAKE_CHAR (Vcharset_composite,
|
|
3707 ch & 0x7F, c & 0x7F);
|
|
3708 Lisp_Object lstr = composite_char_string (emch);
|
|
3709 saved_n = n;
|
|
3710 saved_src = src;
|
|
3711 in_composite = 1;
|
|
3712 src = XSTRING_DATA (lstr);
|
|
3713 n = XSTRING_LENGTH (lstr);
|
|
3714 }
|
|
3715 }
|
|
3716 else
|
|
3717 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
3718 {
|
|
3719 encode_ucs4(charset, ch, c, dst);
|
|
3720 }
|
|
3721 ch = 0;
|
|
3722 }
|
|
3723 else
|
|
3724 {
|
|
3725 ch = c;
|
|
3726 char_boundary = 0;
|
|
3727 }
|
|
3728 break;
|
|
3729 case 4:
|
|
3730 if (ch)
|
|
3731 {
|
|
3732 encode_ucs4 (charset, ch, c, dst);
|
|
3733 ch = 0;
|
|
3734 }
|
|
3735 else
|
|
3736 {
|
|
3737 ch = c;
|
|
3738 char_boundary = 0;
|
|
3739 }
|
|
3740 break;
|
|
3741 default:
|
|
3742 abort ();
|
|
3743 }
|
|
3744 }
|
|
3745 }
|
|
3746 }
|
|
3747
|
|
3748 #ifdef ENABLE_COMPOSITE_CHARS
|
|
3749 if (in_composite)
|
|
3750 {
|
|
3751 n = saved_n;
|
|
3752 src = saved_src;
|
|
3753 in_composite = 0;
|
|
3754 goto back_to_square_n; /* Wheeeeeeeee ..... */
|
|
3755 }
|
|
3756 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
3757
|
|
3758 str->flags = flags;
|
|
3759 str->ch = ch;
|
|
3760 str->iso2022.current_char_boundary = char_boundary;
|
|
3761 str->iso2022.current_charset = charset;
|
|
3762
|
|
3763 /* Verbum caro factum est! */
|
|
3764 }
|
|
3765
|
|
3766
|
|
3767 /************************************************************************/
|
|
3768 /* UTF-8 methods */
|
|
3769 /************************************************************************/
|
|
3770
|
|
3771 static int
|
462
|
3772 detect_coding_utf8 (struct detection_state *st, const Extbyte *src, Lstream_data_count n)
|
428
|
3773 {
|
|
3774 while (n--)
|
|
3775 {
|
444
|
3776 unsigned char c = *(unsigned char *)src++;
|
428
|
3777 switch (st->utf8.in_byte)
|
|
3778 {
|
|
3779 case 0:
|
|
3780 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
|
|
3781 return 0;
|
|
3782 else if (c >= 0xfc)
|
|
3783 st->utf8.in_byte = 5;
|
|
3784 else if (c >= 0xf8)
|
|
3785 st->utf8.in_byte = 4;
|
|
3786 else if (c >= 0xf0)
|
|
3787 st->utf8.in_byte = 3;
|
|
3788 else if (c >= 0xe0)
|
|
3789 st->utf8.in_byte = 2;
|
|
3790 else if (c >= 0xc0)
|
|
3791 st->utf8.in_byte = 1;
|
|
3792 else if (c >= 0x80)
|
|
3793 return 0;
|
|
3794 break;
|
|
3795 default:
|
|
3796 if ((c & 0xc0) != 0x80)
|
|
3797 return 0;
|
|
3798 else
|
|
3799 st->utf8.in_byte--;
|
|
3800 }
|
|
3801 }
|
|
3802 return CODING_CATEGORY_UTF8_MASK;
|
|
3803 }
|
|
3804
|
|
3805 static void
|
444
|
3806 decode_coding_utf8 (Lstream *decoding, const Extbyte *src,
|
462
|
3807 unsigned_char_dynarr *dst, Lstream_data_count n)
|
428
|
3808 {
|
|
3809 struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
|
|
3810 unsigned int flags = str->flags;
|
|
3811 unsigned int ch = str->ch;
|
|
3812 eol_type_t eol_type = str->eol_type;
|
|
3813 unsigned char counter = str->counter;
|
|
3814
|
|
3815 while (n--)
|
|
3816 {
|
444
|
3817 unsigned char c = *(unsigned char *)src++;
|
428
|
3818 switch (counter)
|
|
3819 {
|
|
3820 case 0:
|
|
3821 if ( c >= 0xfc )
|
|
3822 {
|
|
3823 ch = c & 0x01;
|
|
3824 counter = 5;
|
|
3825 }
|
|
3826 else if ( c >= 0xf8 )
|
|
3827 {
|
|
3828 ch = c & 0x03;
|
|
3829 counter = 4;
|
|
3830 }
|
|
3831 else if ( c >= 0xf0 )
|
|
3832 {
|
|
3833 ch = c & 0x07;
|
|
3834 counter = 3;
|
|
3835 }
|
|
3836 else if ( c >= 0xe0 )
|
|
3837 {
|
|
3838 ch = c & 0x0f;
|
|
3839 counter = 2;
|
|
3840 }
|
|
3841 else if ( c >= 0xc0 )
|
|
3842 {
|
|
3843 ch = c & 0x1f;
|
|
3844 counter = 1;
|
|
3845 }
|
|
3846 else
|
|
3847 {
|
|
3848 DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
|
|
3849 decode_ucs4 (c, dst);
|
|
3850 }
|
|
3851 break;
|
|
3852 case 1:
|
|
3853 ch = ( ch << 6 ) | ( c & 0x3f );
|
|
3854 decode_ucs4 (ch, dst);
|
|
3855 ch = 0;
|
|
3856 counter = 0;
|
|
3857 break;
|
|
3858 default:
|
|
3859 ch = ( ch << 6 ) | ( c & 0x3f );
|
|
3860 counter--;
|
|
3861 }
|
|
3862 label_continue_loop:;
|
|
3863 }
|
|
3864
|
|
3865 if (flags & CODING_STATE_END)
|
|
3866 DECODE_OUTPUT_PARTIAL_CHAR (ch);
|
|
3867
|
|
3868 str->flags = flags;
|
|
3869 str->ch = ch;
|
|
3870 str->counter = counter;
|
|
3871 }
|
|
3872
|
|
3873 static void
|
|
3874 encode_utf8 (Lisp_Object charset,
|
|
3875 unsigned char h, unsigned char l, unsigned_char_dynarr *dst)
|
|
3876 {
|
|
3877 unsigned long code = mule_char_to_ucs4 (charset, h, l);
|
|
3878 if ( code <= 0x7f )
|
|
3879 {
|
|
3880 Dynarr_add (dst, code);
|
|
3881 }
|
|
3882 else if ( code <= 0x7ff )
|
|
3883 {
|
|
3884 Dynarr_add (dst, (code >> 6) | 0xc0);
|
|
3885 Dynarr_add (dst, (code & 0x3f) | 0x80);
|
|
3886 }
|
|
3887 else if ( code <= 0xffff )
|
|
3888 {
|
|
3889 Dynarr_add (dst, (code >> 12) | 0xe0);
|
|
3890 Dynarr_add (dst, ((code >> 6) & 0x3f) | 0x80);
|
|
3891 Dynarr_add (dst, (code & 0x3f) | 0x80);
|
|
3892 }
|
|
3893 else if ( code <= 0x1fffff )
|
|
3894 {
|
|
3895 Dynarr_add (dst, (code >> 18) | 0xf0);
|
|
3896 Dynarr_add (dst, ((code >> 12) & 0x3f) | 0x80);
|
|
3897 Dynarr_add (dst, ((code >> 6) & 0x3f) | 0x80);
|
|
3898 Dynarr_add (dst, (code & 0x3f) | 0x80);
|
|
3899 }
|
|
3900 else if ( code <= 0x3ffffff )
|
|
3901 {
|
|
3902 Dynarr_add (dst, (code >> 24) | 0xf8);
|
|
3903 Dynarr_add (dst, ((code >> 18) & 0x3f) | 0x80);
|
|
3904 Dynarr_add (dst, ((code >> 12) & 0x3f) | 0x80);
|
|
3905 Dynarr_add (dst, ((code >> 6) & 0x3f) | 0x80);
|
|
3906 Dynarr_add (dst, (code & 0x3f) | 0x80);
|
|
3907 }
|
|
3908 else
|
|
3909 {
|
|
3910 Dynarr_add (dst, (code >> 30) | 0xfc);
|
|
3911 Dynarr_add (dst, ((code >> 24) & 0x3f) | 0x80);
|
|
3912 Dynarr_add (dst, ((code >> 18) & 0x3f) | 0x80);
|
|
3913 Dynarr_add (dst, ((code >> 12) & 0x3f) | 0x80);
|
|
3914 Dynarr_add (dst, ((code >> 6) & 0x3f) | 0x80);
|
|
3915 Dynarr_add (dst, (code & 0x3f) | 0x80);
|
|
3916 }
|
|
3917 }
|
|
3918
|
|
3919 static void
|
444
|
3920 encode_coding_utf8 (Lstream *encoding, const Bufbyte *src,
|
462
|
3921 unsigned_char_dynarr *dst, Lstream_data_count n)
|
428
|
3922 {
|
|
3923 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
|
|
3924 unsigned int flags = str->flags;
|
|
3925 unsigned int ch = str->ch;
|
|
3926 eol_type_t eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys);
|
|
3927 unsigned char char_boundary = str->iso2022.current_char_boundary;
|
|
3928 Lisp_Object charset = str->iso2022.current_charset;
|
|
3929
|
|
3930 #ifdef ENABLE_COMPOSITE_CHARS
|
|
3931 /* flags for handling composite chars. We do a little switcharoo
|
|
3932 on the source while we're outputting the composite char. */
|
|
3933 unsigned int saved_n = 0;
|
442
|
3934 const unsigned char *saved_src = NULL;
|
428
|
3935 int in_composite = 0;
|
|
3936
|
|
3937 back_to_square_n:
|
|
3938 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
3939
|
|
3940 while (n--)
|
|
3941 {
|
|
3942 unsigned char c = *src++;
|
|
3943
|
|
3944 if (BYTE_ASCII_P (c))
|
|
3945 { /* Processing ASCII character */
|
|
3946 ch = 0;
|
|
3947 if (c == '\n')
|
|
3948 {
|
|
3949 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
|
|
3950 Dynarr_add (dst, '\r');
|
|
3951 if (eol_type != EOL_CR)
|
|
3952 Dynarr_add (dst, c);
|
|
3953 }
|
|
3954 else
|
|
3955 encode_utf8 (Vcharset_ascii, c, 0, dst);
|
|
3956 char_boundary = 1;
|
|
3957 }
|
|
3958 else if (BUFBYTE_LEADING_BYTE_P (c) || BUFBYTE_LEADING_BYTE_P (ch))
|
|
3959 { /* Processing Leading Byte */
|
|
3960 ch = 0;
|
|
3961 charset = CHARSET_BY_LEADING_BYTE (c);
|
|
3962 if (LEADING_BYTE_PREFIX_P(c))
|
|
3963 ch = c;
|
|
3964 char_boundary = 0;
|
|
3965 }
|
|
3966 else
|
|
3967 { /* Processing Non-ASCII character */
|
|
3968 char_boundary = 1;
|
|
3969 if (EQ (charset, Vcharset_control_1))
|
|
3970 {
|
|
3971 encode_utf8 (Vcharset_control_1, c, 0, dst);
|
|
3972 }
|
|
3973 else
|
|
3974 {
|
|
3975 switch (XCHARSET_REP_BYTES (charset))
|
|
3976 {
|
|
3977 case 2:
|
|
3978 encode_utf8 (charset, c, 0, dst);
|
|
3979 break;
|
|
3980 case 3:
|
|
3981 if (XCHARSET_PRIVATE_P (charset))
|
|
3982 {
|
|
3983 encode_utf8 (charset, c, 0, dst);
|
|
3984 ch = 0;
|
|
3985 }
|
|
3986 else if (ch)
|
|
3987 {
|
|
3988 #ifdef ENABLE_COMPOSITE_CHARS
|
|
3989 if (EQ (charset, Vcharset_composite))
|
|
3990 {
|
|
3991 if (in_composite)
|
|
3992 {
|
|
3993 /* #### Bother! We don't know how to
|
|
3994 handle this yet. */
|
|
3995 encode_utf8 (Vcharset_ascii, '~', 0, dst);
|
|
3996 }
|
|
3997 else
|
|
3998 {
|
|
3999 Emchar emch = MAKE_CHAR (Vcharset_composite,
|
|
4000 ch & 0x7F, c & 0x7F);
|
|
4001 Lisp_Object lstr = composite_char_string (emch);
|
|
4002 saved_n = n;
|
|
4003 saved_src = src;
|
|
4004 in_composite = 1;
|
|
4005 src = XSTRING_DATA (lstr);
|
|
4006 n = XSTRING_LENGTH (lstr);
|
|
4007 }
|
|
4008 }
|
|
4009 else
|
|
4010 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
4011 {
|
|
4012 encode_utf8 (charset, ch, c, dst);
|
|
4013 }
|
|
4014 ch = 0;
|
|
4015 }
|
|
4016 else
|
|
4017 {
|
|
4018 ch = c;
|
|
4019 char_boundary = 0;
|
|
4020 }
|
|
4021 break;
|
|
4022 case 4:
|
|
4023 if (ch)
|
|
4024 {
|
|
4025 encode_utf8 (charset, ch, c, dst);
|
|
4026 ch = 0;
|
|
4027 }
|
|
4028 else
|
|
4029 {
|
|
4030 ch = c;
|
|
4031 char_boundary = 0;
|
|
4032 }
|
|
4033 break;
|
|
4034 default:
|
|
4035 abort ();
|
|
4036 }
|
|
4037 }
|
|
4038 }
|
|
4039 }
|
|
4040
|
|
4041 #ifdef ENABLE_COMPOSITE_CHARS
|
|
4042 if (in_composite)
|
|
4043 {
|
|
4044 n = saved_n;
|
|
4045 src = saved_src;
|
|
4046 in_composite = 0;
|
|
4047 goto back_to_square_n; /* Wheeeeeeeee ..... */
|
|
4048 }
|
|
4049 #endif
|
|
4050
|
|
4051 str->flags = flags;
|
|
4052 str->ch = ch;
|
|
4053 str->iso2022.current_char_boundary = char_boundary;
|
|
4054 str->iso2022.current_charset = charset;
|
|
4055
|
|
4056 /* Verbum caro factum est! */
|
|
4057 }
|
|
4058
|
|
4059
|
|
4060 /************************************************************************/
|
|
4061 /* ISO2022 methods */
|
|
4062 /************************************************************************/
|
|
4063
|
|
4064 /* The following note describes the coding system ISO2022 briefly.
|
|
4065 Since the intention of this note is to help understand the
|
|
4066 functions in this file, some parts are NOT ACCURATE or OVERLY
|
|
4067 SIMPLIFIED. For thorough understanding, please refer to the
|
|
4068 original document of ISO2022.
|
|
4069
|
|
4070 ISO2022 provides many mechanisms to encode several character sets
|
|
4071 in 7-bit and 8-bit environments. For 7-bit environments, all text
|
|
4072 is encoded using bytes less than 128. This may make the encoded
|
|
4073 text a little bit longer, but the text passes more easily through
|
|
4074 several gateways, some of which strip off MSB (Most Signigant Bit).
|
|
4075
|
|
4076 There are two kinds of character sets: control character set and
|
|
4077 graphic character set. The former contains control characters such
|
|
4078 as `newline' and `escape' to provide control functions (control
|
|
4079 functions are also provided by escape sequences). The latter
|
|
4080 contains graphic characters such as 'A' and '-'. Emacs recognizes
|
|
4081 two control character sets and many graphic character sets.
|
|
4082
|
|
4083 Graphic character sets are classified into one of the following
|
|
4084 four classes, according to the number of bytes (DIMENSION) and
|
|
4085 number of characters in one dimension (CHARS) of the set:
|
|
4086 - DIMENSION1_CHARS94
|
|
4087 - DIMENSION1_CHARS96
|
|
4088 - DIMENSION2_CHARS94
|
|
4089 - DIMENSION2_CHARS96
|
|
4090
|
|
4091 In addition, each character set is assigned an identification tag,
|
|
4092 unique for each set, called "final character" (denoted as <F>
|
|
4093 hereafter). The <F> of each character set is decided by ECMA(*)
|
|
4094 when it is registered in ISO. The code range of <F> is 0x30..0x7F
|
|
4095 (0x30..0x3F are for private use only).
|
|
4096
|
|
4097 Note (*): ECMA = European Computer Manufacturers Association
|
|
4098
|
|
4099 Here are examples of graphic character set [NAME(<F>)]:
|
|
4100 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
|
|
4101 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
|
|
4102 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
|
|
4103 o DIMENSION2_CHARS96 -- none for the moment
|
|
4104
|
|
4105 A code area (1 byte = 8 bits) is divided into 4 areas, C0, GL, C1, and GR.
|
|
4106 C0 [0x00..0x1F] -- control character plane 0
|
|
4107 GL [0x20..0x7F] -- graphic character plane 0
|
|
4108 C1 [0x80..0x9F] -- control character plane 1
|
|
4109 GR [0xA0..0xFF] -- graphic character plane 1
|
|
4110
|
|
4111 A control character set is directly designated and invoked to C0 or
|
|
4112 C1 by an escape sequence. The most common case is that:
|
|
4113 - ISO646's control character set is designated/invoked to C0, and
|
|
4114 - ISO6429's control character set is designated/invoked to C1,
|
|
4115 and usually these designations/invocations are omitted in encoded
|
|
4116 text. In a 7-bit environment, only C0 can be used, and a control
|
|
4117 character for C1 is encoded by an appropriate escape sequence to
|
|
4118 fit into the environment. All control characters for C1 are
|
|
4119 defined to have corresponding escape sequences.
|
|
4120
|
|
4121 A graphic character set is at first designated to one of four
|
|
4122 graphic registers (G0 through G3), then these graphic registers are
|
|
4123 invoked to GL or GR. These designations and invocations can be
|
|
4124 done independently. The most common case is that G0 is invoked to
|
|
4125 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
|
|
4126 these invocations and designations are omitted in encoded text.
|
|
4127 In a 7-bit environment, only GL can be used.
|
|
4128
|
|
4129 When a graphic character set of CHARS94 is invoked to GL, codes
|
|
4130 0x20 and 0x7F of the GL area work as control characters SPACE and
|
|
4131 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
|
|
4132 be used.
|
|
4133
|
|
4134 There are two ways of invocation: locking-shift and single-shift.
|
|
4135 With locking-shift, the invocation lasts until the next different
|
|
4136 invocation, whereas with single-shift, the invocation affects the
|
|
4137 following character only and doesn't affect the locking-shift
|
|
4138 state. Invocations are done by the following control characters or
|
|
4139 escape sequences:
|
|
4140
|
|
4141 ----------------------------------------------------------------------
|
|
4142 abbrev function cntrl escape seq description
|
|
4143 ----------------------------------------------------------------------
|
|
4144 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
|
|
4145 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
|
|
4146 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
|
|
4147 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
|
|
4148 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
|
|
4149 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
|
|
4150 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
|
|
4151 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
|
|
4152 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
|
|
4153 ----------------------------------------------------------------------
|
|
4154 (*) These are not used by any known coding system.
|
|
4155
|
|
4156 Control characters for these functions are defined by macros
|
|
4157 ISO_CODE_XXX in `coding.h'.
|
|
4158
|
|
4159 Designations are done by the following escape sequences:
|
|
4160 ----------------------------------------------------------------------
|
|
4161 escape sequence description
|
|
4162 ----------------------------------------------------------------------
|
|
4163 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
|
|
4164 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
|
|
4165 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
|
|
4166 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
|
|
4167 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
|
|
4168 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
|
|
4169 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
|
|
4170 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
|
|
4171 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
|
|
4172 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
|
|
4173 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
|
|
4174 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
|
|
4175 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
|
|
4176 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
|
|
4177 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
|
|
4178 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
|
|
4179 ----------------------------------------------------------------------
|
|
4180
|
|
4181 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
|
|
4182 of dimension 1, chars 94, and final character <F>, etc...
|
|
4183
|
|
4184 Note (*): Although these designations are not allowed in ISO2022,
|
|
4185 Emacs accepts them on decoding, and produces them on encoding
|
|
4186 CHARS96 character sets in a coding system which is characterized as
|
|
4187 7-bit environment, non-locking-shift, and non-single-shift.
|
|
4188
|
|
4189 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
|
|
4190 '(' can be omitted. We refer to this as "short-form" hereafter.
|
|
4191
|
|
4192 Now you may notice that there are a lot of ways for encoding the
|
|
4193 same multilingual text in ISO2022. Actually, there exist many
|
|
4194 coding systems such as Compound Text (used in X11's inter client
|
|
4195 communication, ISO-2022-JP (used in Japanese internet), ISO-2022-KR
|
|
4196 (used in Korean internet), EUC (Extended UNIX Code, used in Asian
|
|
4197 localized platforms), and all of these are variants of ISO2022.
|
|
4198
|
|
4199 In addition to the above, Emacs handles two more kinds of escape
|
|
4200 sequences: ISO6429's direction specification and Emacs' private
|
|
4201 sequence for specifying character composition.
|
|
4202
|
|
4203 ISO6429's direction specification takes the following form:
|
|
4204 o CSI ']' -- end of the current direction
|
|
4205 o CSI '0' ']' -- end of the current direction
|
|
4206 o CSI '1' ']' -- start of left-to-right text
|
|
4207 o CSI '2' ']' -- start of right-to-left text
|
|
4208 The control character CSI (0x9B: control sequence introducer) is
|
|
4209 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
|
|
4210
|
|
4211 Character composition specification takes the following form:
|
|
4212 o ESC '0' -- start character composition
|
|
4213 o ESC '1' -- end character composition
|
|
4214 Since these are not standard escape sequences of any ISO standard,
|
|
4215 their use with these meanings is restricted to Emacs only. */
|
|
4216
|
|
4217 static void
|
|
4218 reset_iso2022 (Lisp_Object coding_system, struct iso2022_decoder *iso)
|
|
4219 {
|
|
4220 int i;
|
|
4221
|
|
4222 for (i = 0; i < 4; i++)
|
|
4223 {
|
|
4224 if (!NILP (coding_system))
|
|
4225 iso->charset[i] =
|
|
4226 XCODING_SYSTEM_ISO2022_INITIAL_CHARSET (coding_system, i);
|
|
4227 else
|
|
4228 iso->charset[i] = Qt;
|
|
4229 iso->invalid_designated[i] = 0;
|
|
4230 }
|
|
4231 iso->esc = ISO_ESC_NOTHING;
|
|
4232 iso->esc_bytes_index = 0;
|
|
4233 iso->register_left = 0;
|
|
4234 iso->register_right = 1;
|
|
4235 iso->switched_dir_and_no_valid_charset_yet = 0;
|
|
4236 iso->invalid_switch_dir = 0;
|
|
4237 iso->output_direction_sequence = 0;
|
|
4238 iso->output_literally = 0;
|
|
4239 #ifdef ENABLE_COMPOSITE_CHARS
|
|
4240 if (iso->composite_chars)
|
|
4241 Dynarr_reset (iso->composite_chars);
|
|
4242 #endif
|
|
4243 }
|
|
4244
|
|
4245 static int
|
|
4246 fit_to_be_escape_quoted (unsigned char c)
|
|
4247 {
|
|
4248 switch (c)
|
|
4249 {
|
|
4250 case ISO_CODE_ESC:
|
|
4251 case ISO_CODE_CSI:
|
|
4252 case ISO_CODE_SS2:
|
|
4253 case ISO_CODE_SS3:
|
|
4254 case ISO_CODE_SO:
|
|
4255 case ISO_CODE_SI:
|
|
4256 return 1;
|
|
4257
|
|
4258 default:
|
|
4259 return 0;
|
|
4260 }
|
|
4261 }
|
|
4262
|
|
4263 /* Parse one byte of an ISO2022 escape sequence.
|
|
4264 If the result is an invalid escape sequence, return 0 and
|
|
4265 do not change anything in STR. Otherwise, if the result is
|
|
4266 an incomplete escape sequence, update ISO2022.ESC and
|
|
4267 ISO2022.ESC_BYTES and return -1. Otherwise, update
|
|
4268 all the state variables (but not ISO2022.ESC_BYTES) and
|
|
4269 return 1.
|
|
4270
|
|
4271 If CHECK_INVALID_CHARSETS is non-zero, check for designation
|
|
4272 or invocation of an invalid character set and treat that as
|
448
|
4273 an unrecognized escape sequence.
|
|
4274
|
|
4275 ********************************************************************
|
|
4276
|
|
4277 #### Strategies for error annotation and coding orthogonalization
|
|
4278
|
|
4279 We really want to separate out a number of things. Conceptually,
|
|
4280 there is a nested syntax.
|
|
4281
|
|
4282 At the top level is the ISO 2022 extension syntax, including charset
|
|
4283 designation and invocation, and certain auxiliary controls such as the
|
|
4284 ISO 6429 direction specification. These are octet-oriented, with the
|
|
4285 single exception (AFAIK) of the "exit Unicode" sequence which uses the
|
|
4286 UTF's natural width (1 byte for UTF-7 and UTF-8, 2 bytes for UCS-2 and
|
|
4287 UTF-16, and 4 bytes for UCS-4 and UTF-32). This will be treated as a
|
|
4288 (deprecated) special case in Unicode processing.
|
|
4289
|
|
4290 The middle layer is ISO 2022 character interpretation. This will depend
|
|
4291 on the current state of the ISO 2022 registers, and assembles octets
|
|
4292 into the character's internal representation.
|
|
4293
|
|
4294 The lowest level is translating system control conventions. At present
|
|
4295 this is restricted to newline translation, but one could imagine doing
|
|
4296 tab conversion or line wrapping here. "Escape from Unicode" processing
|
|
4297 would be done at this level.
|
|
4298
|
|
4299 At each level the parser will verify the syntax. In the case of a
|
|
4300 syntax error or warning (such as a redundant escape sequence that affects
|
|
4301 no characters), the parser will take some action, typically inserting the
|
|
4302 erroneous octets directly into the output and creating an annotation
|
|
4303 which can be used by higher level I/O to mark the affected region.
|
|
4304
|
|
4305 This should make it possible to do something sensible about separating
|
|
4306 newline convention processing from character construction, and about
|
|
4307 preventing ISO 2022 escape sequences from being recognized
|
|
4308 inappropriately.
|
|
4309
|
|
4310 The basic strategy will be to have octet classification tables, and
|
|
4311 switch processing according to the table entry.
|
|
4312
|
|
4313 It's possible that, by doing the processing with tables of functions or
|
|
4314 the like, the parser can be used for both detection and translation. */
|
428
|
4315
|
|
4316 static int
|
|
4317 parse_iso2022_esc (Lisp_Object codesys, struct iso2022_decoder *iso,
|
|
4318 unsigned char c, unsigned int *flags,
|
|
4319 int check_invalid_charsets)
|
|
4320 {
|
|
4321 /* (1) If we're at the end of a designation sequence, CS is the
|
|
4322 charset being designated and REG is the register to designate
|
|
4323 it to.
|
|
4324
|
|
4325 (2) If we're at the end of a locking-shift sequence, REG is
|
|
4326 the register to invoke and HALF (0 == left, 1 == right) is
|
|
4327 the half to invoke it into.
|
|
4328
|
|
4329 (3) If we're at the end of a single-shift sequence, REG is
|
|
4330 the register to invoke. */
|
|
4331 Lisp_Object cs = Qnil;
|
|
4332 int reg, half;
|
|
4333
|
|
4334 /* NOTE: This code does goto's all over the fucking place.
|
|
4335 The reason for this is that we're basically implementing
|
|
4336 a state machine here, and hierarchical languages like C
|
|
4337 don't really provide a clean way of doing this. */
|
|
4338
|
|
4339 if (! (*flags & CODING_STATE_ESCAPE))
|
|
4340 /* At beginning of escape sequence; we need to reset our
|
|
4341 escape-state variables. */
|
|
4342 iso->esc = ISO_ESC_NOTHING;
|
|
4343
|
|
4344 iso->output_literally = 0;
|
|
4345 iso->output_direction_sequence = 0;
|
|
4346
|
|
4347 switch (iso->esc)
|
|
4348 {
|
|
4349 case ISO_ESC_NOTHING:
|
|
4350 iso->esc_bytes_index = 0;
|
|
4351 switch (c)
|
|
4352 {
|
|
4353 case ISO_CODE_ESC: /* Start escape sequence */
|
|
4354 *flags |= CODING_STATE_ESCAPE;
|
|
4355 iso->esc = ISO_ESC;
|
|
4356 goto not_done;
|
|
4357
|
|
4358 case ISO_CODE_CSI: /* ISO6429 (specifying directionality) */
|
|
4359 *flags |= CODING_STATE_ESCAPE;
|
|
4360 iso->esc = ISO_ESC_5_11;
|
|
4361 goto not_done;
|
|
4362
|
|
4363 case ISO_CODE_SO: /* locking shift 1 */
|
|
4364 reg = 1; half = 0;
|
|
4365 goto locking_shift;
|
|
4366 case ISO_CODE_SI: /* locking shift 0 */
|
|
4367 reg = 0; half = 0;
|
|
4368 goto locking_shift;
|
|
4369
|
|
4370 case ISO_CODE_SS2: /* single shift */
|
|
4371 reg = 2;
|
|
4372 goto single_shift;
|
|
4373 case ISO_CODE_SS3: /* single shift */
|
|
4374 reg = 3;
|
|
4375 goto single_shift;
|
|
4376
|
|
4377 default: /* Other control characters */
|
|
4378 return 0;
|
|
4379 }
|
|
4380
|
|
4381 case ISO_ESC:
|
|
4382 switch (c)
|
|
4383 {
|
|
4384 /**** single shift ****/
|
|
4385
|
|
4386 case 'N': /* single shift 2 */
|
|
4387 reg = 2;
|
|
4388 goto single_shift;
|
|
4389 case 'O': /* single shift 3 */
|
|
4390 reg = 3;
|
|
4391 goto single_shift;
|
|
4392
|
|
4393 /**** locking shift ****/
|
|
4394
|
|
4395 case '~': /* locking shift 1 right */
|
|
4396 reg = 1; half = 1;
|
|
4397 goto locking_shift;
|
|
4398 case 'n': /* locking shift 2 */
|
|
4399 reg = 2; half = 0;
|
|
4400 goto locking_shift;
|
|
4401 case '}': /* locking shift 2 right */
|
|
4402 reg = 2; half = 1;
|
|
4403 goto locking_shift;
|
|
4404 case 'o': /* locking shift 3 */
|
|
4405 reg = 3; half = 0;
|
|
4406 goto locking_shift;
|
|
4407 case '|': /* locking shift 3 right */
|
|
4408 reg = 3; half = 1;
|
|
4409 goto locking_shift;
|
|
4410
|
|
4411 #ifdef ENABLE_COMPOSITE_CHARS
|
|
4412 /**** composite ****/
|
|
4413
|
|
4414 case '0':
|
|
4415 iso->esc = ISO_ESC_START_COMPOSITE;
|
|
4416 *flags = (*flags & CODING_STATE_ISO2022_LOCK) |
|
|
4417 CODING_STATE_COMPOSITE;
|
|
4418 return 1;
|
|
4419
|
|
4420 case '1':
|
|
4421 iso->esc = ISO_ESC_END_COMPOSITE;
|
|
4422 *flags = (*flags & CODING_STATE_ISO2022_LOCK) &
|
|
4423 ~CODING_STATE_COMPOSITE;
|
|
4424 return 1;
|
|
4425 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
4426
|
|
4427 /**** directionality ****/
|
|
4428
|
|
4429 case '[':
|
|
4430 iso->esc = ISO_ESC_5_11;
|
|
4431 goto not_done;
|
|
4432
|
|
4433 /**** designation ****/
|
|
4434
|
|
4435 case '$': /* multibyte charset prefix */
|
|
4436 iso->esc = ISO_ESC_2_4;
|
|
4437 goto not_done;
|
|
4438
|
|
4439 default:
|
|
4440 if (0x28 <= c && c <= 0x2F)
|
|
4441 {
|
|
4442 iso->esc = (enum iso_esc_flag) (c - 0x28 + ISO_ESC_2_8);
|
|
4443 goto not_done;
|
|
4444 }
|
|
4445
|
|
4446 /* This function is called with CODESYS equal to nil when
|
|
4447 doing coding-system detection. */
|
|
4448 if (!NILP (codesys)
|
|
4449 && XCODING_SYSTEM_ISO2022_ESCAPE_QUOTED (codesys)
|
|
4450 && fit_to_be_escape_quoted (c))
|
|
4451 {
|
|
4452 iso->esc = ISO_ESC_LITERAL;
|
|
4453 *flags &= CODING_STATE_ISO2022_LOCK;
|
|
4454 return 1;
|
|
4455 }
|
|
4456
|
|
4457 /* bzzzt! */
|
|
4458 return 0;
|
|
4459 }
|
|
4460
|
|
4461
|
|
4462
|
|
4463 /**** directionality ****/
|
|
4464
|
|
4465 case ISO_ESC_5_11: /* ISO6429 direction control */
|
|
4466 if (c == ']')
|
|
4467 {
|
|
4468 *flags &= (CODING_STATE_ISO2022_LOCK & ~CODING_STATE_R2L);
|
|
4469 goto directionality;
|
|
4470 }
|
|
4471 if (c == '0') iso->esc = ISO_ESC_5_11_0;
|
|
4472 else if (c == '1') iso->esc = ISO_ESC_5_11_1;
|
|
4473 else if (c == '2') iso->esc = ISO_ESC_5_11_2;
|
|
4474 else return 0;
|
|
4475 goto not_done;
|
|
4476
|
|
4477 case ISO_ESC_5_11_0:
|
|
4478 if (c == ']')
|
|
4479 {
|
|
4480 *flags &= (CODING_STATE_ISO2022_LOCK & ~CODING_STATE_R2L);
|
|
4481 goto directionality;
|
|
4482 }
|
|
4483 return 0;
|
|
4484
|
|
4485 case ISO_ESC_5_11_1:
|
|
4486 if (c == ']')
|
|
4487 {
|
|
4488 *flags = (CODING_STATE_ISO2022_LOCK & ~CODING_STATE_R2L);
|
|
4489 goto directionality;
|
|
4490 }
|
|
4491 return 0;
|
|
4492
|
|
4493 case ISO_ESC_5_11_2:
|
|
4494 if (c == ']')
|
|
4495 {
|
|
4496 *flags = (*flags & CODING_STATE_ISO2022_LOCK) | CODING_STATE_R2L;
|
|
4497 goto directionality;
|
|
4498 }
|
|
4499 return 0;
|
|
4500
|
|
4501 directionality:
|
|
4502 iso->esc = ISO_ESC_DIRECTIONALITY;
|
|
4503 /* Various junk here to attempt to preserve the direction sequences
|
|
4504 literally in the text if they would otherwise be swallowed due
|
|
4505 to invalid designations that don't show up as actual charset
|
|
4506 changes in the text. */
|
|
4507 if (iso->invalid_switch_dir)
|
|
4508 {
|
|
4509 /* We already inserted a direction switch literally into the
|
|
4510 text. We assume (#### this may not be right) that the
|
|
4511 next direction switch is the one going the other way,
|
|
4512 and we need to output that literally as well. */
|
|
4513 iso->output_literally = 1;
|
|
4514 iso->invalid_switch_dir = 0;
|
|
4515 }
|
|
4516 else
|
|
4517 {
|
|
4518 int jj;
|
|
4519
|
|
4520 /* If we are in the thrall of an invalid designation,
|
|
4521 then stick the directionality sequence literally into the
|
|
4522 output stream so it ends up in the original text again. */
|
|
4523 for (jj = 0; jj < 4; jj++)
|
|
4524 if (iso->invalid_designated[jj])
|
|
4525 break;
|
|
4526 if (jj < 4)
|
|
4527 {
|
|
4528 iso->output_literally = 1;
|
|
4529 iso->invalid_switch_dir = 1;
|
|
4530 }
|
|
4531 else
|
|
4532 /* Indicate that we haven't yet seen a valid designation,
|
|
4533 so that if a switch-dir is directly followed by an
|
|
4534 invalid designation, both get inserted literally. */
|
|
4535 iso->switched_dir_and_no_valid_charset_yet = 1;
|
|
4536 }
|
|
4537 return 1;
|
|
4538
|
|
4539
|
|
4540 /**** designation ****/
|
|
4541
|
|
4542 case ISO_ESC_2_4:
|
|
4543 if (0x28 <= c && c <= 0x2F)
|
|
4544 {
|
|
4545 iso->esc = (enum iso_esc_flag) (c - 0x28 + ISO_ESC_2_4_8);
|
|
4546 goto not_done;
|
|
4547 }
|
|
4548 if (0x40 <= c && c <= 0x42)
|
|
4549 {
|
|
4550 cs = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_94X94, c,
|
|
4551 *flags & CODING_STATE_R2L ?
|
|
4552 CHARSET_RIGHT_TO_LEFT :
|
|
4553 CHARSET_LEFT_TO_RIGHT);
|
|
4554 reg = 0;
|
|
4555 goto designated;
|
|
4556 }
|
|
4557 return 0;
|
|
4558
|
|
4559 default:
|
|
4560 {
|
|
4561 int type =-1;
|
|
4562
|
|
4563 if (c < '0' || c > '~')
|
|
4564 return 0; /* bad final byte */
|
|
4565
|
|
4566 if (iso->esc >= ISO_ESC_2_8 &&
|
|
4567 iso->esc <= ISO_ESC_2_15)
|
|
4568 {
|
|
4569 type = ((iso->esc >= ISO_ESC_2_12) ?
|
|
4570 CHARSET_TYPE_96 : CHARSET_TYPE_94);
|
|
4571 reg = (iso->esc - ISO_ESC_2_8) & 3;
|
|
4572 }
|
|
4573 else if (iso->esc >= ISO_ESC_2_4_8 &&
|
|
4574 iso->esc <= ISO_ESC_2_4_15)
|
|
4575 {
|
|
4576 type = ((iso->esc >= ISO_ESC_2_4_12) ?
|
|
4577 CHARSET_TYPE_96X96 : CHARSET_TYPE_94X94);
|
|
4578 reg = (iso->esc - ISO_ESC_2_4_8) & 3;
|
|
4579 }
|
|
4580 else
|
|
4581 {
|
|
4582 /* Can this ever be reached? -slb */
|
|
4583 abort();
|
442
|
4584 return 0;
|
428
|
4585 }
|
|
4586
|
|
4587 cs = CHARSET_BY_ATTRIBUTES (type, c,
|
|
4588 *flags & CODING_STATE_R2L ?
|
|
4589 CHARSET_RIGHT_TO_LEFT :
|
|
4590 CHARSET_LEFT_TO_RIGHT);
|
|
4591 goto designated;
|
|
4592 }
|
|
4593 }
|
|
4594
|
|
4595 not_done:
|
|
4596 iso->esc_bytes[iso->esc_bytes_index++] = (unsigned char) c;
|
|
4597 return -1;
|
|
4598
|
|
4599 single_shift:
|
|
4600 if (check_invalid_charsets && !CHARSETP (iso->charset[reg]))
|
|
4601 /* can't invoke something that ain't there. */
|
|
4602 return 0;
|
|
4603 iso->esc = ISO_ESC_SINGLE_SHIFT;
|
|
4604 *flags &= CODING_STATE_ISO2022_LOCK;
|
|
4605 if (reg == 2)
|
|
4606 *flags |= CODING_STATE_SS2;
|
|
4607 else
|
|
4608 *flags |= CODING_STATE_SS3;
|
|
4609 return 1;
|
|
4610
|
|
4611 locking_shift:
|
|
4612 if (check_invalid_charsets &&
|
|
4613 !CHARSETP (iso->charset[reg]))
|
|
4614 /* can't invoke something that ain't there. */
|
|
4615 return 0;
|
|
4616 if (half)
|
|
4617 iso->register_right = reg;
|
|
4618 else
|
|
4619 iso->register_left = reg;
|
|
4620 *flags &= CODING_STATE_ISO2022_LOCK;
|
|
4621 iso->esc = ISO_ESC_LOCKING_SHIFT;
|
|
4622 return 1;
|
|
4623
|
|
4624 designated:
|
|
4625 if (NILP (cs) && check_invalid_charsets)
|
|
4626 {
|
|
4627 iso->invalid_designated[reg] = 1;
|
|
4628 iso->charset[reg] = Vcharset_ascii;
|
|
4629 iso->esc = ISO_ESC_DESIGNATE;
|
|
4630 *flags &= CODING_STATE_ISO2022_LOCK;
|
|
4631 iso->output_literally = 1;
|
|
4632 if (iso->switched_dir_and_no_valid_charset_yet)
|
|
4633 {
|
|
4634 /* We encountered a switch-direction followed by an
|
|
4635 invalid designation. Ensure that the switch-direction
|
|
4636 gets outputted; otherwise it will probably get eaten
|
|
4637 when the text is written out again. */
|
|
4638 iso->switched_dir_and_no_valid_charset_yet = 0;
|
|
4639 iso->output_direction_sequence = 1;
|
|
4640 /* And make sure that the switch-dir going the other
|
|
4641 way gets outputted, as well. */
|
|
4642 iso->invalid_switch_dir = 1;
|
|
4643 }
|
|
4644 return 1;
|
|
4645 }
|
|
4646 /* This function is called with CODESYS equal to nil when
|
|
4647 doing coding-system detection. */
|
|
4648 if (!NILP (codesys))
|
|
4649 {
|
|
4650 charset_conversion_spec_dynarr *dyn =
|
|
4651 XCODING_SYSTEM (codesys)->iso2022.input_conv;
|
|
4652
|
|
4653 if (dyn)
|
|
4654 {
|
|
4655 int i;
|
|
4656
|
|
4657 for (i = 0; i < Dynarr_length (dyn); i++)
|
|
4658 {
|
|
4659 struct charset_conversion_spec *spec = Dynarr_atp (dyn, i);
|
|
4660 if (EQ (cs, spec->from_charset))
|
|
4661 cs = spec->to_charset;
|
|
4662 }
|
|
4663 }
|
|
4664 }
|
|
4665
|
|
4666 iso->charset[reg] = cs;
|
|
4667 iso->esc = ISO_ESC_DESIGNATE;
|
|
4668 *flags &= CODING_STATE_ISO2022_LOCK;
|
|
4669 if (iso->invalid_designated[reg])
|
|
4670 {
|
|
4671 iso->invalid_designated[reg] = 0;
|
|
4672 iso->output_literally = 1;
|
|
4673 }
|
|
4674 if (iso->switched_dir_and_no_valid_charset_yet)
|
|
4675 iso->switched_dir_and_no_valid_charset_yet = 0;
|
|
4676 return 1;
|
|
4677 }
|
|
4678
|
|
4679 static int
|
462
|
4680 detect_coding_iso2022 (struct detection_state *st, const Extbyte *src, Lstream_data_count n)
|
428
|
4681 {
|
|
4682 int mask;
|
|
4683
|
|
4684 /* #### There are serious deficiencies in the recognition mechanism
|
|
4685 here. This needs to be much smarter if it's going to cut it.
|
|
4686 The sequence "\xff\x0f" is currently detected as LOCK_SHIFT while
|
|
4687 it should be detected as Latin-1.
|
|
4688 All the ISO2022 stuff in this file should be synced up with the
|
|
4689 code from FSF Emacs-20.4, in which Mule should be more or less stable.
|
|
4690 Perhaps we should wait till R2L works in FSF Emacs? */
|
|
4691
|
|
4692 if (!st->iso2022.initted)
|
|
4693 {
|
|
4694 reset_iso2022 (Qnil, &st->iso2022.iso);
|
|
4695 st->iso2022.mask = (CODING_CATEGORY_ISO_7_MASK |
|
|
4696 CODING_CATEGORY_ISO_8_DESIGNATE_MASK |
|
|
4697 CODING_CATEGORY_ISO_8_1_MASK |
|
|
4698 CODING_CATEGORY_ISO_8_2_MASK |
|
|
4699 CODING_CATEGORY_ISO_LOCK_SHIFT_MASK);
|
|
4700 st->iso2022.flags = 0;
|
|
4701 st->iso2022.high_byte_count = 0;
|
|
4702 st->iso2022.saw_single_shift = 0;
|
|
4703 st->iso2022.initted = 1;
|
|
4704 }
|
|
4705
|
|
4706 mask = st->iso2022.mask;
|
|
4707
|
|
4708 while (n--)
|
|
4709 {
|
444
|
4710 unsigned char c = *(unsigned char *)src++;
|
428
|
4711 if (c >= 0xA0)
|
|
4712 {
|
|
4713 mask &= ~CODING_CATEGORY_ISO_7_MASK;
|
|
4714 st->iso2022.high_byte_count++;
|
|
4715 }
|
|
4716 else
|
|
4717 {
|
|
4718 if (st->iso2022.high_byte_count && !st->iso2022.saw_single_shift)
|
|
4719 {
|
|
4720 if (st->iso2022.high_byte_count & 1)
|
|
4721 /* odd number of high bytes; assume not iso-8-2 */
|
|
4722 mask &= ~CODING_CATEGORY_ISO_8_2_MASK;
|
|
4723 }
|
|
4724 st->iso2022.high_byte_count = 0;
|
|
4725 st->iso2022.saw_single_shift = 0;
|
|
4726 if (c > 0x80)
|
|
4727 mask &= ~CODING_CATEGORY_ISO_7_MASK;
|
|
4728 }
|
|
4729 if (!(st->iso2022.flags & CODING_STATE_ESCAPE)
|
|
4730 && (BYTE_C0_P (c) || BYTE_C1_P (c)))
|
|
4731 { /* control chars */
|
|
4732 switch (c)
|
|
4733 {
|
|
4734 /* Allow and ignore control characters that you might
|
|
4735 reasonably see in a text file */
|
|
4736 case '\r':
|
|
4737 case '\n':
|
|
4738 case '\t':
|
|
4739 case 7: /* bell */
|
|
4740 case 8: /* backspace */
|
|
4741 case 11: /* vertical tab */
|
|
4742 case 12: /* form feed */
|
|
4743 case 26: /* MS-DOS C-z junk */
|
|
4744 case 31: /* '^_' -- for info */
|
|
4745 goto label_continue_loop;
|
|
4746
|
|
4747 default:
|
|
4748 break;
|
|
4749 }
|
|
4750 }
|
|
4751
|
|
4752 if ((st->iso2022.flags & CODING_STATE_ESCAPE) || BYTE_C0_P (c)
|
|
4753 || BYTE_C1_P (c))
|
|
4754 {
|
|
4755 if (parse_iso2022_esc (Qnil, &st->iso2022.iso, c,
|
|
4756 &st->iso2022.flags, 0))
|
|
4757 {
|
|
4758 switch (st->iso2022.iso.esc)
|
|
4759 {
|
|
4760 case ISO_ESC_DESIGNATE:
|
|
4761 mask &= ~CODING_CATEGORY_ISO_8_1_MASK;
|
|
4762 mask &= ~CODING_CATEGORY_ISO_8_2_MASK;
|
|
4763 break;
|
|
4764 case ISO_ESC_LOCKING_SHIFT:
|
|
4765 mask = CODING_CATEGORY_ISO_LOCK_SHIFT_MASK;
|
|
4766 goto ran_out_of_chars;
|
|
4767 case ISO_ESC_SINGLE_SHIFT:
|
|
4768 mask &= ~CODING_CATEGORY_ISO_8_DESIGNATE_MASK;
|
|
4769 st->iso2022.saw_single_shift = 1;
|
|
4770 break;
|
|
4771 default:
|
|
4772 break;
|
|
4773 }
|
|
4774 }
|
|
4775 else
|
|
4776 {
|
|
4777 mask = 0;
|
|
4778 goto ran_out_of_chars;
|
|
4779 }
|
|
4780 }
|
|
4781 label_continue_loop:;
|
|
4782 }
|
|
4783
|
|
4784 ran_out_of_chars:
|
|
4785
|
|
4786 return mask;
|
|
4787 }
|
|
4788
|
|
4789 static int
|
|
4790 postprocess_iso2022_mask (int mask)
|
|
4791 {
|
|
4792 /* #### kind of cheesy */
|
|
4793 /* If seven-bit ISO is allowed, then assume that the encoding is
|
|
4794 entirely seven-bit and turn off the eight-bit ones. */
|
|
4795 if (mask & CODING_CATEGORY_ISO_7_MASK)
|
|
4796 mask &= ~ (CODING_CATEGORY_ISO_8_DESIGNATE_MASK |
|
|
4797 CODING_CATEGORY_ISO_8_1_MASK |
|
|
4798 CODING_CATEGORY_ISO_8_2_MASK);
|
|
4799 return mask;
|
|
4800 }
|
|
4801
|
|
4802 /* If FLAGS is a null pointer or specifies right-to-left motion,
|
|
4803 output a switch-dir-to-left-to-right sequence to DST.
|
|
4804 Also update FLAGS if it is not a null pointer.
|
|
4805 If INTERNAL_P is set, we are outputting in internal format and
|
|
4806 need to handle the CSI differently. */
|
|
4807
|
|
4808 static void
|
|
4809 restore_left_to_right_direction (Lisp_Coding_System *codesys,
|
|
4810 unsigned_char_dynarr *dst,
|
|
4811 unsigned int *flags,
|
|
4812 int internal_p)
|
|
4813 {
|
|
4814 if (!flags || (*flags & CODING_STATE_R2L))
|
|
4815 {
|
|
4816 if (CODING_SYSTEM_ISO2022_SEVEN (codesys))
|
|
4817 {
|
|
4818 Dynarr_add (dst, ISO_CODE_ESC);
|
|
4819 Dynarr_add (dst, '[');
|
|
4820 }
|
|
4821 else if (internal_p)
|
|
4822 DECODE_ADD_BINARY_CHAR (ISO_CODE_CSI, dst);
|
|
4823 else
|
|
4824 Dynarr_add (dst, ISO_CODE_CSI);
|
|
4825 Dynarr_add (dst, '0');
|
|
4826 Dynarr_add (dst, ']');
|
|
4827 if (flags)
|
|
4828 *flags &= ~CODING_STATE_R2L;
|
|
4829 }
|
|
4830 }
|
|
4831
|
|
4832 /* If FLAGS is a null pointer or specifies a direction different from
|
|
4833 DIRECTION (which should be either CHARSET_RIGHT_TO_LEFT or
|
|
4834 CHARSET_LEFT_TO_RIGHT), output the appropriate switch-dir escape
|
|
4835 sequence to DST. Also update FLAGS if it is not a null pointer.
|
|
4836 If INTERNAL_P is set, we are outputting in internal format and
|
|
4837 need to handle the CSI differently. */
|
|
4838
|
|
4839 static void
|
|
4840 ensure_correct_direction (int direction, Lisp_Coding_System *codesys,
|
|
4841 unsigned_char_dynarr *dst, unsigned int *flags,
|
|
4842 int internal_p)
|
|
4843 {
|
|
4844 if ((!flags || (*flags & CODING_STATE_R2L)) &&
|
|
4845 direction == CHARSET_LEFT_TO_RIGHT)
|
|
4846 restore_left_to_right_direction (codesys, dst, flags, internal_p);
|
|
4847 else if (!CODING_SYSTEM_ISO2022_NO_ISO6429 (codesys)
|
|
4848 && (!flags || !(*flags & CODING_STATE_R2L)) &&
|
|
4849 direction == CHARSET_RIGHT_TO_LEFT)
|
|
4850 {
|
|
4851 if (CODING_SYSTEM_ISO2022_SEVEN (codesys))
|
|
4852 {
|
|
4853 Dynarr_add (dst, ISO_CODE_ESC);
|
|
4854 Dynarr_add (dst, '[');
|
|
4855 }
|
|
4856 else if (internal_p)
|
|
4857 DECODE_ADD_BINARY_CHAR (ISO_CODE_CSI, dst);
|
|
4858 else
|
|
4859 Dynarr_add (dst, ISO_CODE_CSI);
|
|
4860 Dynarr_add (dst, '2');
|
|
4861 Dynarr_add (dst, ']');
|
|
4862 if (flags)
|
|
4863 *flags |= CODING_STATE_R2L;
|
|
4864 }
|
|
4865 }
|
|
4866
|
|
4867 /* Convert ISO2022-format data to internal format. */
|
|
4868
|
|
4869 static void
|
444
|
4870 decode_coding_iso2022 (Lstream *decoding, const Extbyte *src,
|
462
|
4871 unsigned_char_dynarr *dst, Lstream_data_count n)
|
428
|
4872 {
|
|
4873 struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
|
|
4874 unsigned int flags = str->flags;
|
|
4875 unsigned int ch = str->ch;
|
|
4876 eol_type_t eol_type = str->eol_type;
|
|
4877 #ifdef ENABLE_COMPOSITE_CHARS
|
|
4878 unsigned_char_dynarr *real_dst = dst;
|
|
4879 #endif
|
|
4880 Lisp_Object coding_system;
|
|
4881
|
|
4882 XSETCODING_SYSTEM (coding_system, str->codesys);
|
|
4883
|
|
4884 #ifdef ENABLE_COMPOSITE_CHARS
|
|
4885 if (flags & CODING_STATE_COMPOSITE)
|
|
4886 dst = str->iso2022.composite_chars;
|
|
4887 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
4888
|
|
4889 while (n--)
|
|
4890 {
|
444
|
4891 unsigned char c = *(unsigned char *)src++;
|
428
|
4892 if (flags & CODING_STATE_ESCAPE)
|
|
4893 { /* Within ESC sequence */
|
|
4894 int retval = parse_iso2022_esc (coding_system, &str->iso2022,
|
|
4895 c, &flags, 1);
|
|
4896
|
|
4897 if (retval)
|
|
4898 {
|
|
4899 switch (str->iso2022.esc)
|
|
4900 {
|
|
4901 #ifdef ENABLE_COMPOSITE_CHARS
|
|
4902 case ISO_ESC_START_COMPOSITE:
|
|
4903 if (str->iso2022.composite_chars)
|
|
4904 Dynarr_reset (str->iso2022.composite_chars);
|
|
4905 else
|
|
4906 str->iso2022.composite_chars = Dynarr_new (unsigned_char);
|
|
4907 dst = str->iso2022.composite_chars;
|
|
4908 break;
|
|
4909 case ISO_ESC_END_COMPOSITE:
|
|
4910 {
|
|
4911 Bufbyte comstr[MAX_EMCHAR_LEN];
|
|
4912 Bytecount len;
|
|
4913 Emchar emch = lookup_composite_char (Dynarr_atp (dst, 0),
|
|
4914 Dynarr_length (dst));
|
|
4915 dst = real_dst;
|
|
4916 len = set_charptr_emchar (comstr, emch);
|
|
4917 Dynarr_add_many (dst, comstr, len);
|
|
4918 break;
|
|
4919 }
|
|
4920 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
4921
|
|
4922 case ISO_ESC_LITERAL:
|
|
4923 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
4924 break;
|
|
4925
|
|
4926 default:
|
|
4927 /* Everything else handled already */
|
|
4928 break;
|
|
4929 }
|
|
4930 }
|
|
4931
|
|
4932 /* Attempted error recovery. */
|
|
4933 if (str->iso2022.output_direction_sequence)
|
|
4934 ensure_correct_direction (flags & CODING_STATE_R2L ?
|
|
4935 CHARSET_RIGHT_TO_LEFT :
|
|
4936 CHARSET_LEFT_TO_RIGHT,
|
|
4937 str->codesys, dst, 0, 1);
|
|
4938 /* More error recovery. */
|
|
4939 if (!retval || str->iso2022.output_literally)
|
|
4940 {
|
|
4941 /* Output the (possibly invalid) sequence */
|
|
4942 int i;
|
|
4943 for (i = 0; i < str->iso2022.esc_bytes_index; i++)
|
|
4944 DECODE_ADD_BINARY_CHAR (str->iso2022.esc_bytes[i], dst);
|
|
4945 flags &= CODING_STATE_ISO2022_LOCK;
|
|
4946 if (!retval)
|
|
4947 n++, src--;/* Repeat the loop with the same character. */
|
|
4948 else
|
|
4949 {
|
|
4950 /* No sense in reprocessing the final byte of the
|
|
4951 escape sequence; it could mess things up anyway.
|
|
4952 Just add it now. */
|
|
4953 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
4954 }
|
|
4955 }
|
|
4956 ch = 0;
|
|
4957 }
|
|
4958 else if (BYTE_C0_P (c) || BYTE_C1_P (c))
|
|
4959 { /* Control characters */
|
|
4960
|
|
4961 /***** Error-handling *****/
|
|
4962
|
|
4963 /* If we were in the middle of a character, dump out the
|
|
4964 partial character. */
|
|
4965 DECODE_OUTPUT_PARTIAL_CHAR (ch);
|
|
4966
|
|
4967 /* If we just saw a single-shift character, dump it out.
|
|
4968 This may dump out the wrong sort of single-shift character,
|
|
4969 but least it will give an indication that something went
|
|
4970 wrong. */
|
|
4971 if (flags & CODING_STATE_SS2)
|
|
4972 {
|
|
4973 DECODE_ADD_BINARY_CHAR (ISO_CODE_SS2, dst);
|
|
4974 flags &= ~CODING_STATE_SS2;
|
|
4975 }
|
|
4976 if (flags & CODING_STATE_SS3)
|
|
4977 {
|
|
4978 DECODE_ADD_BINARY_CHAR (ISO_CODE_SS3, dst);
|
|
4979 flags &= ~CODING_STATE_SS3;
|
|
4980 }
|
|
4981
|
|
4982 /***** Now handle the control characters. *****/
|
|
4983
|
|
4984 /* Handle CR/LF */
|
|
4985 DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
|
|
4986
|
|
4987 flags &= CODING_STATE_ISO2022_LOCK;
|
|
4988
|
|
4989 if (!parse_iso2022_esc (coding_system, &str->iso2022, c, &flags, 1))
|
|
4990 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
4991 }
|
|
4992 else
|
|
4993 { /* Graphic characters */
|
|
4994 Lisp_Object charset;
|
|
4995 int lb;
|
|
4996 int reg;
|
|
4997
|
|
4998 DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
|
|
4999
|
|
5000 /* Now determine the charset. */
|
|
5001 reg = ((flags & CODING_STATE_SS2) ? 2
|
|
5002 : (flags & CODING_STATE_SS3) ? 3
|
|
5003 : !BYTE_ASCII_P (c) ? str->iso2022.register_right
|
|
5004 : str->iso2022.register_left);
|
|
5005 charset = str->iso2022.charset[reg];
|
|
5006
|
|
5007 /* Error checking: */
|
|
5008 if (! CHARSETP (charset)
|
|
5009 || str->iso2022.invalid_designated[reg]
|
|
5010 || (((c & 0x7F) == ' ' || (c & 0x7F) == ISO_CODE_DEL)
|
|
5011 && XCHARSET_CHARS (charset) == 94))
|
|
5012 /* Mrmph. We are trying to invoke a register that has no
|
|
5013 or an invalid charset in it, or trying to add a character
|
|
5014 outside the range of the charset. Insert that char literally
|
|
5015 to preserve it for the output. */
|
|
5016 {
|
|
5017 DECODE_OUTPUT_PARTIAL_CHAR (ch);
|
|
5018 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
5019 }
|
|
5020
|
|
5021 else
|
|
5022 {
|
|
5023 /* Things are probably hunky-dorey. */
|
|
5024
|
|
5025 /* Fetch reverse charset, maybe. */
|
|
5026 if (((flags & CODING_STATE_R2L) &&
|
|
5027 XCHARSET_DIRECTION (charset) == CHARSET_LEFT_TO_RIGHT)
|
|
5028 ||
|
|
5029 (!(flags & CODING_STATE_R2L) &&
|
|
5030 XCHARSET_DIRECTION (charset) == CHARSET_RIGHT_TO_LEFT))
|
|
5031 {
|
|
5032 Lisp_Object new_charset =
|
|
5033 XCHARSET_REVERSE_DIRECTION_CHARSET (charset);
|
|
5034 if (!NILP (new_charset))
|
|
5035 charset = new_charset;
|
|
5036 }
|
|
5037
|
|
5038 lb = XCHARSET_LEADING_BYTE (charset);
|
|
5039 switch (XCHARSET_REP_BYTES (charset))
|
|
5040 {
|
|
5041 case 1: /* ASCII */
|
|
5042 DECODE_OUTPUT_PARTIAL_CHAR (ch);
|
|
5043 Dynarr_add (dst, c & 0x7F);
|
|
5044 break;
|
|
5045
|
|
5046 case 2: /* one-byte official */
|
|
5047 DECODE_OUTPUT_PARTIAL_CHAR (ch);
|
|
5048 Dynarr_add (dst, lb);
|
|
5049 Dynarr_add (dst, c | 0x80);
|
|
5050 break;
|
|
5051
|
|
5052 case 3: /* one-byte private or two-byte official */
|
|
5053 if (XCHARSET_PRIVATE_P (charset))
|
|
5054 {
|
|
5055 DECODE_OUTPUT_PARTIAL_CHAR (ch);
|
|
5056 Dynarr_add (dst, PRE_LEADING_BYTE_PRIVATE_1);
|
|
5057 Dynarr_add (dst, lb);
|
|
5058 Dynarr_add (dst, c | 0x80);
|
|
5059 }
|
|
5060 else
|
|
5061 {
|
|
5062 if (ch)
|
|
5063 {
|
|
5064 Dynarr_add (dst, lb);
|
|
5065 Dynarr_add (dst, ch | 0x80);
|
|
5066 Dynarr_add (dst, c | 0x80);
|
|
5067 ch = 0;
|
|
5068 }
|
|
5069 else
|
|
5070 ch = c;
|
|
5071 }
|
|
5072 break;
|
|
5073
|
|
5074 default: /* two-byte private */
|
|
5075 if (ch)
|
|
5076 {
|
|
5077 Dynarr_add (dst, PRE_LEADING_BYTE_PRIVATE_2);
|
|
5078 Dynarr_add (dst, lb);
|
|
5079 Dynarr_add (dst, ch | 0x80);
|
|
5080 Dynarr_add (dst, c | 0x80);
|
|
5081 ch = 0;
|
|
5082 }
|
|
5083 else
|
|
5084 ch = c;
|
|
5085 }
|
|
5086 }
|
|
5087
|
|
5088 if (!ch)
|
|
5089 flags &= CODING_STATE_ISO2022_LOCK;
|
|
5090 }
|
|
5091
|
|
5092 label_continue_loop:;
|
|
5093 }
|
|
5094
|
|
5095 if (flags & CODING_STATE_END)
|
|
5096 DECODE_OUTPUT_PARTIAL_CHAR (ch);
|
|
5097
|
|
5098 str->flags = flags;
|
|
5099 str->ch = ch;
|
|
5100 }
|
|
5101
|
|
5102
|
|
5103 /***** ISO2022 encoder *****/
|
|
5104
|
|
5105 /* Designate CHARSET into register REG. */
|
|
5106
|
|
5107 static void
|
|
5108 iso2022_designate (Lisp_Object charset, unsigned char reg,
|
|
5109 struct encoding_stream *str, unsigned_char_dynarr *dst)
|
|
5110 {
|
442
|
5111 static const char inter94[] = "()*+";
|
|
5112 static const char inter96[] = ",-./";
|
428
|
5113 unsigned int type;
|
|
5114 unsigned char final;
|
|
5115 Lisp_Object old_charset = str->iso2022.charset[reg];
|
|
5116
|
|
5117 str->iso2022.charset[reg] = charset;
|
|
5118 if (!CHARSETP (charset))
|
|
5119 /* charset might be an initial nil or t. */
|
|
5120 return;
|
|
5121 type = XCHARSET_TYPE (charset);
|
|
5122 final = XCHARSET_FINAL (charset);
|
|
5123 if (!str->iso2022.force_charset_on_output[reg] &&
|
|
5124 CHARSETP (old_charset) &&
|
|
5125 XCHARSET_TYPE (old_charset) == type &&
|
|
5126 XCHARSET_FINAL (old_charset) == final)
|
|
5127 return;
|
|
5128
|
|
5129 str->iso2022.force_charset_on_output[reg] = 0;
|
|
5130
|
|
5131 {
|
|
5132 charset_conversion_spec_dynarr *dyn =
|
|
5133 str->codesys->iso2022.output_conv;
|
|
5134
|
|
5135 if (dyn)
|
|
5136 {
|
|
5137 int i;
|
|
5138
|
|
5139 for (i = 0; i < Dynarr_length (dyn); i++)
|
|
5140 {
|
|
5141 struct charset_conversion_spec *spec = Dynarr_atp (dyn, i);
|
|
5142 if (EQ (charset, spec->from_charset))
|
|
5143 charset = spec->to_charset;
|
|
5144 }
|
|
5145 }
|
|
5146 }
|
|
5147
|
|
5148 Dynarr_add (dst, ISO_CODE_ESC);
|
|
5149 switch (type)
|
|
5150 {
|
|
5151 case CHARSET_TYPE_94:
|
|
5152 Dynarr_add (dst, inter94[reg]);
|
|
5153 break;
|
|
5154 case CHARSET_TYPE_96:
|
|
5155 Dynarr_add (dst, inter96[reg]);
|
|
5156 break;
|
|
5157 case CHARSET_TYPE_94X94:
|
|
5158 Dynarr_add (dst, '$');
|
|
5159 if (reg != 0
|
|
5160 || !(CODING_SYSTEM_ISO2022_SHORT (str->codesys))
|
|
5161 || final < '@'
|
|
5162 || final > 'B')
|
|
5163 Dynarr_add (dst, inter94[reg]);
|
|
5164 break;
|
|
5165 case CHARSET_TYPE_96X96:
|
|
5166 Dynarr_add (dst, '$');
|
|
5167 Dynarr_add (dst, inter96[reg]);
|
|
5168 break;
|
|
5169 }
|
|
5170 Dynarr_add (dst, final);
|
|
5171 }
|
|
5172
|
|
5173 static void
|
|
5174 ensure_normal_shift (struct encoding_stream *str, unsigned_char_dynarr *dst)
|
|
5175 {
|
|
5176 if (str->iso2022.register_left != 0)
|
|
5177 {
|
|
5178 Dynarr_add (dst, ISO_CODE_SI);
|
|
5179 str->iso2022.register_left = 0;
|
|
5180 }
|
|
5181 }
|
|
5182
|
|
5183 static void
|
|
5184 ensure_shift_out (struct encoding_stream *str, unsigned_char_dynarr *dst)
|
|
5185 {
|
|
5186 if (str->iso2022.register_left != 1)
|
|
5187 {
|
|
5188 Dynarr_add (dst, ISO_CODE_SO);
|
|
5189 str->iso2022.register_left = 1;
|
|
5190 }
|
|
5191 }
|
|
5192
|
|
5193 /* Convert internally-formatted data to ISO2022 format. */
|
|
5194
|
|
5195 static void
|
444
|
5196 encode_coding_iso2022 (Lstream *encoding, const Bufbyte *src,
|
462
|
5197 unsigned_char_dynarr *dst, Lstream_data_count n)
|
428
|
5198 {
|
|
5199 unsigned char charmask, c;
|
|
5200 unsigned char char_boundary;
|
|
5201 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
|
|
5202 unsigned int flags = str->flags;
|
|
5203 unsigned int ch = str->ch;
|
|
5204 Lisp_Coding_System *codesys = str->codesys;
|
|
5205 eol_type_t eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys);
|
|
5206 int i;
|
|
5207 Lisp_Object charset;
|
|
5208 int half;
|
|
5209
|
|
5210 #ifdef ENABLE_COMPOSITE_CHARS
|
|
5211 /* flags for handling composite chars. We do a little switcharoo
|
|
5212 on the source while we're outputting the composite char. */
|
|
5213 unsigned int saved_n = 0;
|
442
|
5214 const unsigned char *saved_src = NULL;
|
428
|
5215 int in_composite = 0;
|
|
5216 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
5217
|
|
5218 char_boundary = str->iso2022.current_char_boundary;
|
|
5219 charset = str->iso2022.current_charset;
|
|
5220 half = str->iso2022.current_half;
|
|
5221
|
|
5222 #ifdef ENABLE_COMPOSITE_CHARS
|
|
5223 back_to_square_n:
|
|
5224 #endif
|
|
5225 while (n--)
|
|
5226 {
|
|
5227 c = *src++;
|
|
5228
|
|
5229 if (BYTE_ASCII_P (c))
|
|
5230 { /* Processing ASCII character */
|
|
5231 ch = 0;
|
|
5232
|
|
5233 restore_left_to_right_direction (codesys, dst, &flags, 0);
|
|
5234
|
|
5235 /* Make sure G0 contains ASCII */
|
|
5236 if ((c > ' ' && c < ISO_CODE_DEL) ||
|
|
5237 !CODING_SYSTEM_ISO2022_NO_ASCII_CNTL (codesys))
|
|
5238 {
|
|
5239 ensure_normal_shift (str, dst);
|
|
5240 iso2022_designate (Vcharset_ascii, 0, str, dst);
|
|
5241 }
|
|
5242
|
|
5243 /* If necessary, restore everything to the default state
|
|
5244 at end-of-line */
|
|
5245 if (c == '\n' &&
|
|
5246 !(CODING_SYSTEM_ISO2022_NO_ASCII_EOL (codesys)))
|
|
5247 {
|
|
5248 restore_left_to_right_direction (codesys, dst, &flags, 0);
|
|
5249
|
|
5250 ensure_normal_shift (str, dst);
|
|
5251
|
|
5252 for (i = 0; i < 4; i++)
|
|
5253 {
|
|
5254 Lisp_Object initial_charset =
|
|
5255 CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i);
|
|
5256 iso2022_designate (initial_charset, i, str, dst);
|
|
5257 }
|
|
5258 }
|
|
5259 if (c == '\n')
|
|
5260 {
|
|
5261 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
|
|
5262 Dynarr_add (dst, '\r');
|
|
5263 if (eol_type != EOL_CR)
|
|
5264 Dynarr_add (dst, c);
|
|
5265 }
|
|
5266 else
|
|
5267 {
|
|
5268 if (CODING_SYSTEM_ISO2022_ESCAPE_QUOTED (codesys)
|
|
5269 && fit_to_be_escape_quoted (c))
|
|
5270 Dynarr_add (dst, ISO_CODE_ESC);
|
|
5271 Dynarr_add (dst, c);
|
|
5272 }
|
|
5273 char_boundary = 1;
|
|
5274 }
|
|
5275
|
|
5276 else if (BUFBYTE_LEADING_BYTE_P (c) || BUFBYTE_LEADING_BYTE_P (ch))
|
|
5277 { /* Processing Leading Byte */
|
|
5278 ch = 0;
|
|
5279 charset = CHARSET_BY_LEADING_BYTE (c);
|
|
5280 if (LEADING_BYTE_PREFIX_P(c))
|
|
5281 ch = c;
|
|
5282 else if (!EQ (charset, Vcharset_control_1)
|
|
5283 #ifdef ENABLE_COMPOSITE_CHARS
|
|
5284 && !EQ (charset, Vcharset_composite)
|
|
5285 #endif
|
|
5286 )
|
|
5287 {
|
|
5288 int reg;
|
|
5289
|
|
5290 ensure_correct_direction (XCHARSET_DIRECTION (charset),
|
|
5291 codesys, dst, &flags, 0);
|
|
5292
|
|
5293 /* Now determine which register to use. */
|
|
5294 reg = -1;
|
|
5295 for (i = 0; i < 4; i++)
|
|
5296 {
|
|
5297 if (EQ (charset, str->iso2022.charset[i]) ||
|
|
5298 EQ (charset,
|
|
5299 CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i)))
|
|
5300 {
|
|
5301 reg = i;
|
|
5302 break;
|
|
5303 }
|
|
5304 }
|
|
5305
|
|
5306 if (reg == -1)
|
|
5307 {
|
|
5308 if (XCHARSET_GRAPHIC (charset) != 0)
|
|
5309 {
|
|
5310 if (!NILP (str->iso2022.charset[1]) &&
|
|
5311 (!CODING_SYSTEM_ISO2022_SEVEN (codesys) ||
|
|
5312 CODING_SYSTEM_ISO2022_LOCK_SHIFT (codesys)))
|
|
5313 reg = 1;
|
|
5314 else if (!NILP (str->iso2022.charset[2]))
|
|
5315 reg = 2;
|
|
5316 else if (!NILP (str->iso2022.charset[3]))
|
|
5317 reg = 3;
|
|
5318 else
|
|
5319 reg = 0;
|
|
5320 }
|
|
5321 else
|
|
5322 reg = 0;
|
|
5323 }
|
|
5324
|
|
5325 iso2022_designate (charset, reg, str, dst);
|
|
5326
|
|
5327 /* Now invoke that register. */
|
|
5328 switch (reg)
|
|
5329 {
|
|
5330 case 0:
|
|
5331 ensure_normal_shift (str, dst);
|
|
5332 half = 0;
|
|
5333 break;
|
|
5334
|
|
5335 case 1:
|
|
5336 if (CODING_SYSTEM_ISO2022_SEVEN (codesys))
|
|
5337 {
|
|
5338 ensure_shift_out (str, dst);
|
|
5339 half = 0;
|
|
5340 }
|
|
5341 else
|
|
5342 half = 1;
|
|
5343 break;
|
|
5344
|
|
5345 case 2:
|
|
5346 if (CODING_SYSTEM_ISO2022_SEVEN (str->codesys))
|
|
5347 {
|
|
5348 Dynarr_add (dst, ISO_CODE_ESC);
|
|
5349 Dynarr_add (dst, 'N');
|
|
5350 half = 0;
|
|
5351 }
|
|
5352 else
|
|
5353 {
|
|
5354 Dynarr_add (dst, ISO_CODE_SS2);
|
|
5355 half = 1;
|
|
5356 }
|
|
5357 break;
|
|
5358
|
|
5359 case 3:
|
|
5360 if (CODING_SYSTEM_ISO2022_SEVEN (str->codesys))
|
|
5361 {
|
|
5362 Dynarr_add (dst, ISO_CODE_ESC);
|
|
5363 Dynarr_add (dst, 'O');
|
|
5364 half = 0;
|
|
5365 }
|
|
5366 else
|
|
5367 {
|
|
5368 Dynarr_add (dst, ISO_CODE_SS3);
|
|
5369 half = 1;
|
|
5370 }
|
|
5371 break;
|
|
5372
|
|
5373 default:
|
|
5374 abort ();
|
|
5375 }
|
|
5376 }
|
|
5377 char_boundary = 0;
|
|
5378 }
|
|
5379 else
|
|
5380 { /* Processing Non-ASCII character */
|
|
5381 charmask = (half == 0 ? 0x7F : 0xFF);
|
|
5382 char_boundary = 1;
|
|
5383 if (EQ (charset, Vcharset_control_1))
|
|
5384 {
|
|
5385 if (CODING_SYSTEM_ISO2022_ESCAPE_QUOTED (codesys)
|
|
5386 && fit_to_be_escape_quoted (c))
|
|
5387 Dynarr_add (dst, ISO_CODE_ESC);
|
|
5388 /* you asked for it ... */
|
|
5389 Dynarr_add (dst, c - 0x20);
|
|
5390 }
|
|
5391 else
|
|
5392 {
|
|
5393 switch (XCHARSET_REP_BYTES (charset))
|
|
5394 {
|
|
5395 case 2:
|
|
5396 Dynarr_add (dst, c & charmask);
|
|
5397 break;
|
|
5398 case 3:
|
|
5399 if (XCHARSET_PRIVATE_P (charset))
|
|
5400 {
|
|
5401 Dynarr_add (dst, c & charmask);
|
|
5402 ch = 0;
|
|
5403 }
|
|
5404 else if (ch)
|
|
5405 {
|
|
5406 #ifdef ENABLE_COMPOSITE_CHARS
|
|
5407 if (EQ (charset, Vcharset_composite))
|
|
5408 {
|
|
5409 if (in_composite)
|
|
5410 {
|
|
5411 /* #### Bother! We don't know how to
|
|
5412 handle this yet. */
|
|
5413 Dynarr_add (dst, '~');
|
|
5414 }
|
|
5415 else
|
|
5416 {
|
|
5417 Emchar emch = MAKE_CHAR (Vcharset_composite,
|
|
5418 ch & 0x7F, c & 0x7F);
|
|
5419 Lisp_Object lstr = composite_char_string (emch);
|
|
5420 saved_n = n;
|
|
5421 saved_src = src;
|
|
5422 in_composite = 1;
|
|
5423 src = XSTRING_DATA (lstr);
|
|
5424 n = XSTRING_LENGTH (lstr);
|
|
5425 Dynarr_add (dst, ISO_CODE_ESC);
|
|
5426 Dynarr_add (dst, '0'); /* start composing */
|
|
5427 }
|
|
5428 }
|
|
5429 else
|
|
5430 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
5431 {
|
|
5432 Dynarr_add (dst, ch & charmask);
|
|
5433 Dynarr_add (dst, c & charmask);
|
|
5434 }
|
|
5435 ch = 0;
|
|
5436 }
|
|
5437 else
|
|
5438 {
|
|
5439 ch = c;
|
|
5440 char_boundary = 0;
|
|
5441 }
|
|
5442 break;
|
|
5443 case 4:
|
|
5444 if (ch)
|
|
5445 {
|
|
5446 Dynarr_add (dst, ch & charmask);
|
|
5447 Dynarr_add (dst, c & charmask);
|
|
5448 ch = 0;
|
|
5449 }
|
|
5450 else
|
|
5451 {
|
|
5452 ch = c;
|
|
5453 char_boundary = 0;
|
|
5454 }
|
|
5455 break;
|
|
5456 default:
|
|
5457 abort ();
|
|
5458 }
|
|
5459 }
|
|
5460 }
|
|
5461 }
|
|
5462
|
|
5463 #ifdef ENABLE_COMPOSITE_CHARS
|
|
5464 if (in_composite)
|
|
5465 {
|
|
5466 n = saved_n;
|
|
5467 src = saved_src;
|
|
5468 in_composite = 0;
|
|
5469 Dynarr_add (dst, ISO_CODE_ESC);
|
|
5470 Dynarr_add (dst, '1'); /* end composing */
|
|
5471 goto back_to_square_n; /* Wheeeeeeeee ..... */
|
|
5472 }
|
|
5473 #endif /* ENABLE_COMPOSITE_CHARS */
|
|
5474
|
|
5475 if (char_boundary && flags & CODING_STATE_END)
|
|
5476 {
|
|
5477 restore_left_to_right_direction (codesys, dst, &flags, 0);
|
|
5478 ensure_normal_shift (str, dst);
|
|
5479 for (i = 0; i < 4; i++)
|
|
5480 {
|
|
5481 Lisp_Object initial_charset =
|
|
5482 CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i);
|
|
5483 iso2022_designate (initial_charset, i, str, dst);
|
|
5484 }
|
|
5485 }
|
|
5486
|
|
5487 str->flags = flags;
|
|
5488 str->ch = ch;
|
|
5489 str->iso2022.current_char_boundary = char_boundary;
|
|
5490 str->iso2022.current_charset = charset;
|
|
5491 str->iso2022.current_half = half;
|
|
5492
|
|
5493 /* Verbum caro factum est! */
|
|
5494 }
|
|
5495 #endif /* MULE */
|
|
5496
|
|
5497 /************************************************************************/
|
|
5498 /* No-conversion methods */
|
|
5499 /************************************************************************/
|
|
5500
|
|
5501 /* This is used when reading in "binary" files -- i.e. files that may
|
|
5502 contain all 256 possible byte values and that are not to be
|
|
5503 interpreted as being in any particular decoding. */
|
|
5504 static void
|
444
|
5505 decode_coding_no_conversion (Lstream *decoding, const Extbyte *src,
|
462
|
5506 unsigned_char_dynarr *dst, Lstream_data_count n)
|
428
|
5507 {
|
|
5508 struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
|
|
5509 unsigned int flags = str->flags;
|
|
5510 unsigned int ch = str->ch;
|
|
5511 eol_type_t eol_type = str->eol_type;
|
|
5512
|
|
5513 while (n--)
|
|
5514 {
|
444
|
5515 unsigned char c = *(unsigned char *)src++;
|
428
|
5516
|
|
5517 DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
|
|
5518 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
5519 label_continue_loop:;
|
|
5520 }
|
|
5521
|
|
5522 DECODE_HANDLE_END_OF_CONVERSION (flags, ch, dst);
|
|
5523
|
|
5524 str->flags = flags;
|
|
5525 str->ch = ch;
|
|
5526 }
|
|
5527
|
|
5528 static void
|
444
|
5529 encode_coding_no_conversion (Lstream *encoding, const Bufbyte *src,
|
462
|
5530 unsigned_char_dynarr *dst, Lstream_data_count n)
|
428
|
5531 {
|
|
5532 unsigned char c;
|
|
5533 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
|
|
5534 unsigned int flags = str->flags;
|
|
5535 unsigned int ch = str->ch;
|
|
5536 eol_type_t eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys);
|
|
5537
|
|
5538 while (n--)
|
|
5539 {
|
|
5540 c = *src++;
|
|
5541 if (c == '\n')
|
|
5542 {
|
|
5543 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
|
|
5544 Dynarr_add (dst, '\r');
|
|
5545 if (eol_type != EOL_CR)
|
|
5546 Dynarr_add (dst, '\n');
|
|
5547 ch = 0;
|
|
5548 }
|
|
5549 else if (BYTE_ASCII_P (c))
|
|
5550 {
|
|
5551 assert (ch == 0);
|
|
5552 Dynarr_add (dst, c);
|
|
5553 }
|
|
5554 else if (BUFBYTE_LEADING_BYTE_P (c))
|
|
5555 {
|
|
5556 assert (ch == 0);
|
|
5557 if (c == LEADING_BYTE_LATIN_ISO8859_1 ||
|
|
5558 c == LEADING_BYTE_CONTROL_1)
|
|
5559 ch = c;
|
|
5560 else
|
|
5561 Dynarr_add (dst, '~'); /* untranslatable character */
|
|
5562 }
|
|
5563 else
|
|
5564 {
|
|
5565 if (ch == LEADING_BYTE_LATIN_ISO8859_1)
|
|
5566 Dynarr_add (dst, c);
|
|
5567 else if (ch == LEADING_BYTE_CONTROL_1)
|
|
5568 {
|
|
5569 assert (c < 0xC0);
|
|
5570 Dynarr_add (dst, c - 0x20);
|
|
5571 }
|
|
5572 /* else it should be the second or third byte of an
|
|
5573 untranslatable character, so ignore it */
|
|
5574 ch = 0;
|
|
5575 }
|
|
5576 }
|
|
5577
|
|
5578 str->flags = flags;
|
|
5579 str->ch = ch;
|
|
5580 }
|
|
5581
|
|
5582
|
440
|
5583
|
428
|
5584 /************************************************************************/
|
|
5585 /* Initialization */
|
|
5586 /************************************************************************/
|
|
5587
|
|
5588 void
|
|
5589 syms_of_file_coding (void)
|
|
5590 {
|
442
|
5591 INIT_LRECORD_IMPLEMENTATION (coding_system);
|
|
5592
|
|
5593 DEFERROR_STANDARD (Qcoding_system_error, Qio_error);
|
428
|
5594
|
|
5595 DEFSUBR (Fcoding_system_p);
|
|
5596 DEFSUBR (Ffind_coding_system);
|
|
5597 DEFSUBR (Fget_coding_system);
|
|
5598 DEFSUBR (Fcoding_system_list);
|
|
5599 DEFSUBR (Fcoding_system_name);
|
|
5600 DEFSUBR (Fmake_coding_system);
|
|
5601 DEFSUBR (Fcopy_coding_system);
|
440
|
5602 DEFSUBR (Fcoding_system_canonical_name_p);
|
|
5603 DEFSUBR (Fcoding_system_alias_p);
|
|
5604 DEFSUBR (Fcoding_system_aliasee);
|
428
|
5605 DEFSUBR (Fdefine_coding_system_alias);
|
|
5606 DEFSUBR (Fsubsidiary_coding_system);
|
|
5607
|
|
5608 DEFSUBR (Fcoding_system_type);
|
|
5609 DEFSUBR (Fcoding_system_doc_string);
|
|
5610 DEFSUBR (Fcoding_system_property);
|
|
5611
|
|
5612 DEFSUBR (Fcoding_category_list);
|
|
5613 DEFSUBR (Fset_coding_priority_list);
|
|
5614 DEFSUBR (Fcoding_priority_list);
|
|
5615 DEFSUBR (Fset_coding_category_system);
|
|
5616 DEFSUBR (Fcoding_category_system);
|
|
5617
|
|
5618 DEFSUBR (Fdetect_coding_region);
|
|
5619 DEFSUBR (Fdecode_coding_region);
|
|
5620 DEFSUBR (Fencode_coding_region);
|
|
5621 #ifdef MULE
|
|
5622 DEFSUBR (Fdecode_shift_jis_char);
|
|
5623 DEFSUBR (Fencode_shift_jis_char);
|
|
5624 DEFSUBR (Fdecode_big5_char);
|
|
5625 DEFSUBR (Fencode_big5_char);
|
|
5626 DEFSUBR (Fset_ucs_char);
|
|
5627 DEFSUBR (Fucs_char);
|
|
5628 DEFSUBR (Fset_char_ucs);
|
|
5629 DEFSUBR (Fchar_ucs);
|
|
5630 #endif /* MULE */
|
|
5631 defsymbol (&Qcoding_systemp, "coding-system-p");
|
|
5632 defsymbol (&Qno_conversion, "no-conversion");
|
|
5633 defsymbol (&Qraw_text, "raw-text");
|
|
5634 #ifdef MULE
|
|
5635 defsymbol (&Qbig5, "big5");
|
|
5636 defsymbol (&Qshift_jis, "shift-jis");
|
|
5637 defsymbol (&Qucs4, "ucs-4");
|
|
5638 defsymbol (&Qutf8, "utf-8");
|
|
5639 defsymbol (&Qccl, "ccl");
|
|
5640 defsymbol (&Qiso2022, "iso2022");
|
|
5641 #endif /* MULE */
|
|
5642 defsymbol (&Qmnemonic, "mnemonic");
|
|
5643 defsymbol (&Qeol_type, "eol-type");
|
|
5644 defsymbol (&Qpost_read_conversion, "post-read-conversion");
|
|
5645 defsymbol (&Qpre_write_conversion, "pre-write-conversion");
|
|
5646
|
|
5647 defsymbol (&Qcr, "cr");
|
|
5648 defsymbol (&Qlf, "lf");
|
|
5649 defsymbol (&Qcrlf, "crlf");
|
|
5650 defsymbol (&Qeol_cr, "eol-cr");
|
|
5651 defsymbol (&Qeol_lf, "eol-lf");
|
|
5652 defsymbol (&Qeol_crlf, "eol-crlf");
|
|
5653 #ifdef MULE
|
|
5654 defsymbol (&Qcharset_g0, "charset-g0");
|
|
5655 defsymbol (&Qcharset_g1, "charset-g1");
|
|
5656 defsymbol (&Qcharset_g2, "charset-g2");
|
|
5657 defsymbol (&Qcharset_g3, "charset-g3");
|
|
5658 defsymbol (&Qforce_g0_on_output, "force-g0-on-output");
|
|
5659 defsymbol (&Qforce_g1_on_output, "force-g1-on-output");
|
|
5660 defsymbol (&Qforce_g2_on_output, "force-g2-on-output");
|
|
5661 defsymbol (&Qforce_g3_on_output, "force-g3-on-output");
|
|
5662 defsymbol (&Qno_iso6429, "no-iso6429");
|
|
5663 defsymbol (&Qinput_charset_conversion, "input-charset-conversion");
|
|
5664 defsymbol (&Qoutput_charset_conversion, "output-charset-conversion");
|
|
5665
|
|
5666 defsymbol (&Qshort, "short");
|
|
5667 defsymbol (&Qno_ascii_eol, "no-ascii-eol");
|
|
5668 defsymbol (&Qno_ascii_cntl, "no-ascii-cntl");
|
|
5669 defsymbol (&Qseven, "seven");
|
|
5670 defsymbol (&Qlock_shift, "lock-shift");
|
|
5671 defsymbol (&Qescape_quoted, "escape-quoted");
|
|
5672 #endif /* MULE */
|
|
5673 defsymbol (&Qencode, "encode");
|
|
5674 defsymbol (&Qdecode, "decode");
|
|
5675
|
|
5676 #ifdef MULE
|
|
5677 defsymbol (&coding_category_symbol[CODING_CATEGORY_SHIFT_JIS],
|
|
5678 "shift-jis");
|
|
5679 defsymbol (&coding_category_symbol[CODING_CATEGORY_BIG5],
|
|
5680 "big5");
|
|
5681 defsymbol (&coding_category_symbol[CODING_CATEGORY_UCS4],
|
|
5682 "ucs-4");
|
|
5683 defsymbol (&coding_category_symbol[CODING_CATEGORY_UTF8],
|
|
5684 "utf-8");
|
|
5685 defsymbol (&coding_category_symbol[CODING_CATEGORY_ISO_7],
|
|
5686 "iso-7");
|
|
5687 defsymbol (&coding_category_symbol[CODING_CATEGORY_ISO_8_DESIGNATE],
|
|
5688 "iso-8-designate");
|
|
5689 defsymbol (&coding_category_symbol[CODING_CATEGORY_ISO_8_1],
|
|
5690 "iso-8-1");
|
|
5691 defsymbol (&coding_category_symbol[CODING_CATEGORY_ISO_8_2],
|
|
5692 "iso-8-2");
|
|
5693 defsymbol (&coding_category_symbol[CODING_CATEGORY_ISO_LOCK_SHIFT],
|
|
5694 "iso-lock-shift");
|
|
5695 #endif /* MULE */
|
|
5696 defsymbol (&coding_category_symbol[CODING_CATEGORY_NO_CONVERSION],
|
|
5697 "no-conversion");
|
|
5698 }
|
|
5699
|
|
5700 void
|
|
5701 lstream_type_create_file_coding (void)
|
|
5702 {
|
|
5703 LSTREAM_HAS_METHOD (decoding, reader);
|
|
5704 LSTREAM_HAS_METHOD (decoding, writer);
|
|
5705 LSTREAM_HAS_METHOD (decoding, rewinder);
|
|
5706 LSTREAM_HAS_METHOD (decoding, seekable_p);
|
|
5707 LSTREAM_HAS_METHOD (decoding, flusher);
|
|
5708 LSTREAM_HAS_METHOD (decoding, closer);
|
|
5709 LSTREAM_HAS_METHOD (decoding, marker);
|
|
5710
|
|
5711 LSTREAM_HAS_METHOD (encoding, reader);
|
|
5712 LSTREAM_HAS_METHOD (encoding, writer);
|
|
5713 LSTREAM_HAS_METHOD (encoding, rewinder);
|
|
5714 LSTREAM_HAS_METHOD (encoding, seekable_p);
|
|
5715 LSTREAM_HAS_METHOD (encoding, flusher);
|
|
5716 LSTREAM_HAS_METHOD (encoding, closer);
|
|
5717 LSTREAM_HAS_METHOD (encoding, marker);
|
|
5718 }
|
|
5719
|
|
5720 void
|
|
5721 vars_of_file_coding (void)
|
|
5722 {
|
|
5723 int i;
|
|
5724
|
|
5725 fcd = xnew (struct file_coding_dump);
|
452
|
5726 dump_add_root_struct_ptr (&fcd, &fcd_description);
|
428
|
5727
|
|
5728 /* Initialize to something reasonable ... */
|
448
|
5729 for (i = 0; i < CODING_CATEGORY_LAST; i++)
|
428
|
5730 {
|
|
5731 fcd->coding_category_system[i] = Qnil;
|
|
5732 fcd->coding_category_by_priority[i] = i;
|
|
5733 }
|
|
5734
|
|
5735 Fprovide (intern ("file-coding"));
|
|
5736
|
|
5737 DEFVAR_LISP ("keyboard-coding-system", &Vkeyboard_coding_system /*
|
|
5738 Coding system used for TTY keyboard input.
|
|
5739 Not used under a windowing system.
|
|
5740 */ );
|
|
5741 Vkeyboard_coding_system = Qnil;
|
|
5742
|
|
5743 DEFVAR_LISP ("terminal-coding-system", &Vterminal_coding_system /*
|
|
5744 Coding system used for TTY display output.
|
|
5745 Not used under a windowing system.
|
|
5746 */ );
|
|
5747 Vterminal_coding_system = Qnil;
|
|
5748
|
|
5749 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read /*
|
440
|
5750 Overriding coding system used when reading from a file or process.
|
|
5751 You should bind this variable with `let', but do not set it globally.
|
|
5752 If this is non-nil, it specifies the coding system that will be used
|
|
5753 to decode input on read operations, such as from a file or process.
|
|
5754 It overrides `buffer-file-coding-system-for-read',
|
428
|
5755 `insert-file-contents-pre-hook', etc. Use those variables instead of
|
440
|
5756 this one for permanent changes to the environment. */ );
|
428
|
5757 Vcoding_system_for_read = Qnil;
|
|
5758
|
|
5759 DEFVAR_LISP ("coding-system-for-write",
|
|
5760 &Vcoding_system_for_write /*
|
440
|
5761 Overriding coding system used when writing to a file or process.
|
|
5762 You should bind this variable with `let', but do not set it globally.
|
|
5763 If this is non-nil, it specifies the coding system that will be used
|
|
5764 to encode output for write operations, such as to a file or process.
|
|
5765 It overrides `buffer-file-coding-system', `write-region-pre-hook', etc.
|
|
5766 Use those variables instead of this one for permanent changes to the
|
|
5767 environment. */ );
|
428
|
5768 Vcoding_system_for_write = Qnil;
|
|
5769
|
|
5770 DEFVAR_LISP ("file-name-coding-system", &Vfile_name_coding_system /*
|
|
5771 Coding system used to convert pathnames when accessing files.
|
|
5772 */ );
|
|
5773 Vfile_name_coding_system = Qnil;
|
|
5774
|
|
5775 DEFVAR_BOOL ("enable-multibyte-characters", &enable_multibyte_characters /*
|
|
5776 Non-nil means the buffer contents are regarded as multi-byte form
|
|
5777 of characters, not a binary code. This affects the display, file I/O,
|
|
5778 and behaviors of various editing commands.
|
|
5779
|
|
5780 Setting this to nil does not do anything.
|
|
5781 */ );
|
|
5782 enable_multibyte_characters = 1;
|
|
5783 }
|
|
5784
|
|
5785 void
|
|
5786 complex_vars_of_file_coding (void)
|
|
5787 {
|
|
5788 staticpro (&Vcoding_system_hash_table);
|
|
5789 Vcoding_system_hash_table =
|
|
5790 make_lisp_hash_table (50, HASH_TABLE_NON_WEAK, HASH_TABLE_EQ);
|
|
5791
|
|
5792 the_codesys_prop_dynarr = Dynarr_new (codesys_prop);
|
452
|
5793 dump_add_root_struct_ptr (&the_codesys_prop_dynarr, &codesys_prop_dynarr_description);
|
428
|
5794
|
|
5795 #define DEFINE_CODESYS_PROP(Prop_Type, Sym) do \
|
|
5796 { \
|
|
5797 struct codesys_prop csp; \
|
|
5798 csp.sym = (Sym); \
|
|
5799 csp.prop_type = (Prop_Type); \
|
|
5800 Dynarr_add (the_codesys_prop_dynarr, csp); \
|
|
5801 } while (0)
|
|
5802
|
|
5803 DEFINE_CODESYS_PROP (CODESYS_PROP_ALL_OK, Qmnemonic);
|
|
5804 DEFINE_CODESYS_PROP (CODESYS_PROP_ALL_OK, Qeol_type);
|
|
5805 DEFINE_CODESYS_PROP (CODESYS_PROP_ALL_OK, Qeol_cr);
|
|
5806 DEFINE_CODESYS_PROP (CODESYS_PROP_ALL_OK, Qeol_crlf);
|
|
5807 DEFINE_CODESYS_PROP (CODESYS_PROP_ALL_OK, Qeol_lf);
|
|
5808 DEFINE_CODESYS_PROP (CODESYS_PROP_ALL_OK, Qpost_read_conversion);
|
|
5809 DEFINE_CODESYS_PROP (CODESYS_PROP_ALL_OK, Qpre_write_conversion);
|
|
5810 #ifdef MULE
|
|
5811 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qcharset_g0);
|
|
5812 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qcharset_g1);
|
|
5813 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qcharset_g2);
|
|
5814 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qcharset_g3);
|
|
5815 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qforce_g0_on_output);
|
|
5816 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qforce_g1_on_output);
|
|
5817 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qforce_g2_on_output);
|
|
5818 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qforce_g3_on_output);
|
|
5819 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qshort);
|
|
5820 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qno_ascii_eol);
|
|
5821 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qno_ascii_cntl);
|
|
5822 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qseven);
|
|
5823 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qlock_shift);
|
|
5824 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qno_iso6429);
|
|
5825 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qescape_quoted);
|
|
5826 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qinput_charset_conversion);
|
|
5827 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qoutput_charset_conversion);
|
|
5828
|
|
5829 DEFINE_CODESYS_PROP (CODESYS_PROP_CCL, Qencode);
|
|
5830 DEFINE_CODESYS_PROP (CODESYS_PROP_CCL, Qdecode);
|
|
5831 #endif /* MULE */
|
|
5832 /* Need to create this here or we're really screwed. */
|
|
5833 Fmake_coding_system
|
|
5834 (Qraw_text, Qno_conversion,
|
|
5835 build_string ("Raw text, which means it converts only line-break-codes."),
|
|
5836 list2 (Qmnemonic, build_string ("Raw")));
|
|
5837
|
|
5838 Fmake_coding_system
|
|
5839 (Qbinary, Qno_conversion,
|
|
5840 build_string ("Binary, which means it does not convert anything."),
|
|
5841 list4 (Qeol_type, Qlf,
|
|
5842 Qmnemonic, build_string ("Binary")));
|
|
5843
|
|
5844 Fdefine_coding_system_alias (Qno_conversion, Qraw_text);
|
|
5845
|
440
|
5846 Fdefine_coding_system_alias (Qfile_name, Qbinary);
|
|
5847
|
|
5848 Fdefine_coding_system_alias (Qterminal, Qbinary);
|
|
5849 Fdefine_coding_system_alias (Qkeyboard, Qbinary);
|
|
5850
|
428
|
5851 /* Need this for bootstrapping */
|
|
5852 fcd->coding_category_system[CODING_CATEGORY_NO_CONVERSION] =
|
|
5853 Fget_coding_system (Qraw_text);
|
|
5854
|
|
5855 #ifdef MULE
|
|
5856 {
|
442
|
5857 size_t i;
|
|
5858
|
|
5859 for (i = 0; i < countof (fcd->ucs_to_mule_table); i++)
|
428
|
5860 fcd->ucs_to_mule_table[i] = Qnil;
|
|
5861 }
|
|
5862 staticpro (&mule_to_ucs_table);
|
|
5863 mule_to_ucs_table = Fmake_char_table(Qgeneric);
|
|
5864 #endif /* MULE */
|
|
5865 }
|