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