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