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