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