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