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