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