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