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