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