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