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
|
365
|
622 specific information about the conversion with the PROPS argument.
|
259
|
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 */
|
361
|
1858 if (CODING_SYSTEM_TYPE (str->codesys) == CODESYS_AUTODETECT
|
|
1859 || CODING_SYSTEM_EOL_TYPE (str->codesys) == EOL_AUTODETECT)
|
|
1860 {
|
|
1861 xzero (str->decst);
|
|
1862 str->decst.eol_type = EOL_AUTODETECT;
|
|
1863 str->decst.mask = ~0;
|
|
1864 }
|
259
|
1865 str->flags = str->ch = 0;
|
|
1866 }
|
|
1867
|
|
1868 static int
|
|
1869 decoding_rewinder (Lstream *stream)
|
|
1870 {
|
|
1871 struct decoding_stream *str = DECODING_STREAM_DATA (stream);
|
|
1872 reset_decoding_stream (str);
|
|
1873 Dynarr_reset (str->runoff);
|
|
1874 return Lstream_rewind (str->other_end);
|
|
1875 }
|
|
1876
|
|
1877 static int
|
|
1878 decoding_seekable_p (Lstream *stream)
|
|
1879 {
|
|
1880 struct decoding_stream *str = DECODING_STREAM_DATA (stream);
|
|
1881 return Lstream_seekable_p (str->other_end);
|
|
1882 }
|
|
1883
|
|
1884 static int
|
|
1885 decoding_flusher (Lstream *stream)
|
|
1886 {
|
|
1887 struct decoding_stream *str = DECODING_STREAM_DATA (stream);
|
|
1888 return Lstream_flush (str->other_end);
|
|
1889 }
|
|
1890
|
|
1891 static int
|
|
1892 decoding_closer (Lstream *stream)
|
|
1893 {
|
|
1894 struct decoding_stream *str = DECODING_STREAM_DATA (stream);
|
|
1895 if (stream->flags & LSTREAM_FL_WRITE)
|
|
1896 {
|
|
1897 str->flags |= CODING_STATE_END;
|
|
1898 decoding_writer (stream, 0, 0);
|
|
1899 }
|
|
1900 Dynarr_free (str->runoff);
|
|
1901 #ifdef MULE
|
337
|
1902 #ifdef ENABLE_COMPOSITE_CHARS
|
259
|
1903 if (str->iso2022.composite_chars)
|
|
1904 Dynarr_free (str->iso2022.composite_chars);
|
|
1905 #endif
|
337
|
1906 #endif
|
259
|
1907 return Lstream_close (str->other_end);
|
|
1908 }
|
|
1909
|
|
1910 Lisp_Object
|
|
1911 decoding_stream_coding_system (Lstream *stream)
|
|
1912 {
|
272
|
1913 Lisp_Object coding_system;
|
259
|
1914 struct decoding_stream *str = DECODING_STREAM_DATA (stream);
|
|
1915
|
|
1916 XSETCODING_SYSTEM (coding_system, str->codesys);
|
|
1917 return subsidiary_coding_system (coding_system, str->eol_type);
|
|
1918 }
|
|
1919
|
|
1920 void
|
|
1921 set_decoding_stream_coding_system (Lstream *lstr, Lisp_Object codesys)
|
|
1922 {
|
|
1923 struct Lisp_Coding_System *cs = XCODING_SYSTEM (codesys);
|
|
1924 struct decoding_stream *str = DECODING_STREAM_DATA (lstr);
|
|
1925 str->codesys = cs;
|
|
1926 if (CODING_SYSTEM_EOL_TYPE (cs) != EOL_AUTODETECT)
|
|
1927 str->eol_type = CODING_SYSTEM_EOL_TYPE (cs);
|
|
1928 reset_decoding_stream (str);
|
|
1929 }
|
|
1930
|
|
1931 /* WARNING WARNING WARNING WARNING!!!!! If you open up a decoding
|
|
1932 stream for writing, no automatic code detection will be performed.
|
|
1933 The reason for this is that automatic code detection requires a
|
|
1934 seekable input. Things will also fail if you open a decoding
|
|
1935 stream for reading using a non-fully-specified coding system and
|
|
1936 a non-seekable input stream. */
|
|
1937
|
|
1938 static Lisp_Object
|
|
1939 make_decoding_stream_1 (Lstream *stream, Lisp_Object codesys,
|
|
1940 CONST char *mode)
|
|
1941 {
|
|
1942 Lstream *lstr = Lstream_new (lstream_decoding, mode);
|
|
1943 struct decoding_stream *str = DECODING_STREAM_DATA (lstr);
|
|
1944 Lisp_Object obj;
|
|
1945
|
272
|
1946 xzero (*str);
|
259
|
1947 str->other_end = stream;
|
|
1948 str->runoff = (unsigned_char_dynarr *) Dynarr_new (unsigned_char);
|
|
1949 str->eol_type = EOL_AUTODETECT;
|
|
1950 if (!strcmp (mode, "r")
|
|
1951 && Lstream_seekable_p (stream))
|
|
1952 /* We can determine the coding system now. */
|
|
1953 determine_real_coding_system (stream, &codesys, &str->eol_type);
|
|
1954 set_decoding_stream_coding_system (lstr, codesys);
|
|
1955 str->decst.eol_type = str->eol_type;
|
|
1956 str->decst.mask = ~0;
|
|
1957 XSETLSTREAM (obj, lstr);
|
|
1958 return obj;
|
|
1959 }
|
|
1960
|
|
1961 Lisp_Object
|
|
1962 make_decoding_input_stream (Lstream *stream, Lisp_Object codesys)
|
|
1963 {
|
|
1964 return make_decoding_stream_1 (stream, codesys, "r");
|
|
1965 }
|
|
1966
|
|
1967 Lisp_Object
|
|
1968 make_decoding_output_stream (Lstream *stream, Lisp_Object codesys)
|
|
1969 {
|
|
1970 return make_decoding_stream_1 (stream, codesys, "w");
|
|
1971 }
|
|
1972
|
|
1973 /* Note: the decode_coding_* functions all take the same
|
|
1974 arguments as mule_decode(), which is to say some SRC data of
|
|
1975 size N, which is to be stored into dynamic array DST.
|
|
1976 DECODING is the stream within which the decoding is
|
|
1977 taking place, but no data is actually read from or
|
|
1978 written to that stream; that is handled in decoding_reader()
|
|
1979 or decoding_writer(). This allows the same functions to
|
|
1980 be used for both reading and writing. */
|
|
1981
|
|
1982 static void
|
|
1983 mule_decode (Lstream *decoding, CONST unsigned char *src,
|
|
1984 unsigned_char_dynarr *dst, unsigned int n)
|
|
1985 {
|
|
1986 struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
|
|
1987
|
|
1988 /* If necessary, do encoding-detection now. We do this when
|
|
1989 we're a writing stream or a non-seekable reading stream,
|
|
1990 meaning that we can't just process the whole input,
|
|
1991 rewind, and start over. */
|
|
1992
|
|
1993 if (CODING_SYSTEM_TYPE (str->codesys) == CODESYS_AUTODETECT ||
|
|
1994 str->eol_type == EOL_AUTODETECT)
|
|
1995 {
|
272
|
1996 Lisp_Object codesys;
|
259
|
1997
|
|
1998 XSETCODING_SYSTEM (codesys, str->codesys);
|
|
1999 detect_coding_type (&str->decst, src, n,
|
|
2000 CODING_SYSTEM_TYPE (str->codesys) !=
|
|
2001 CODESYS_AUTODETECT);
|
|
2002 if (CODING_SYSTEM_TYPE (str->codesys) == CODESYS_AUTODETECT &&
|
|
2003 str->decst.mask != ~0)
|
|
2004 /* #### This is cheesy. What we really ought to do is
|
|
2005 buffer up a certain amount of data so as to get a
|
|
2006 less random result. */
|
|
2007 codesys = coding_system_from_mask (str->decst.mask);
|
|
2008 str->eol_type = str->decst.eol_type;
|
|
2009 if (XCODING_SYSTEM (codesys) != str->codesys)
|
|
2010 {
|
|
2011 /* Preserve the CODING_STATE_END flag in case it was set.
|
|
2012 If we erase it, bad things might happen. */
|
|
2013 int was_end = str->flags & CODING_STATE_END;
|
|
2014 set_decoding_stream_coding_system (decoding, codesys);
|
|
2015 if (was_end)
|
|
2016 str->flags |= CODING_STATE_END;
|
|
2017 }
|
|
2018 }
|
|
2019
|
|
2020 switch (CODING_SYSTEM_TYPE (str->codesys))
|
|
2021 {
|
|
2022 #ifdef DEBUG_XEMACS
|
|
2023 case CODESYS_INTERNAL:
|
|
2024 Dynarr_add_many (dst, src, n);
|
|
2025 break;
|
|
2026 #endif
|
|
2027 case CODESYS_AUTODETECT:
|
|
2028 /* If we got this far and still haven't decided on the coding
|
|
2029 system, then do no conversion. */
|
|
2030 case CODESYS_NO_CONVERSION:
|
|
2031 decode_coding_no_conversion (decoding, src, dst, n);
|
|
2032 break;
|
|
2033 #ifdef MULE
|
|
2034 case CODESYS_SHIFT_JIS:
|
|
2035 decode_coding_sjis (decoding, src, dst, n);
|
|
2036 break;
|
|
2037 case CODESYS_BIG5:
|
|
2038 decode_coding_big5 (decoding, src, dst, n);
|
|
2039 break;
|
|
2040 case CODESYS_CCL:
|
|
2041 ccl_driver (&str->ccl, src, dst, n, 0);
|
|
2042 break;
|
|
2043 case CODESYS_ISO2022:
|
|
2044 decode_coding_iso2022 (decoding, src, dst, n);
|
|
2045 break;
|
272
|
2046 #endif /* MULE */
|
259
|
2047 default:
|
|
2048 abort ();
|
|
2049 }
|
|
2050 }
|
|
2051
|
|
2052 DEFUN ("decode-coding-region", Fdecode_coding_region, 3, 4, 0, /*
|
|
2053 Decode the text between START and END which is encoded in CODING-SYSTEM.
|
|
2054 This is useful if you've read in encoded text from a file without decoding
|
|
2055 it (e.g. you read in a JIS-formatted file but used the `binary' or
|
|
2056 `no-conversion' coding system, so that it shows up as "^[$B!<!+^[(B").
|
|
2057 Return length of decoded text.
|
|
2058 BUFFER defaults to the current buffer if unspecified.
|
|
2059 */
|
|
2060 (start, end, coding_system, buffer))
|
|
2061 {
|
|
2062 Bufpos b, e;
|
|
2063 struct buffer *buf = decode_buffer (buffer, 0);
|
|
2064 Lisp_Object instream, lb_outstream, de_outstream, outstream;
|
|
2065 Lstream *istr, *ostr;
|
|
2066 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
2067
|
|
2068 get_buffer_range_char (buf, start, end, &b, &e, 0);
|
|
2069
|
|
2070 barf_if_buffer_read_only (buf, b, e);
|
|
2071
|
|
2072 coding_system = Fget_coding_system (coding_system);
|
|
2073 instream = make_lisp_buffer_input_stream (buf, b, e, 0);
|
|
2074 lb_outstream = make_lisp_buffer_output_stream (buf, b, 0);
|
|
2075 de_outstream = make_decoding_output_stream (XLSTREAM (lb_outstream),
|
|
2076 coding_system);
|
|
2077 outstream = make_encoding_output_stream (XLSTREAM (de_outstream),
|
|
2078 Fget_coding_system (Qbinary));
|
|
2079 istr = XLSTREAM (instream);
|
|
2080 ostr = XLSTREAM (outstream);
|
|
2081 GCPRO4 (instream, lb_outstream, de_outstream, outstream);
|
|
2082
|
|
2083 /* The chain of streams looks like this:
|
|
2084
|
|
2085 [BUFFER] <----- send through
|
|
2086 ------> [ENCODE AS BINARY]
|
|
2087 ------> [DECODE AS SPECIFIED]
|
|
2088 ------> [BUFFER]
|
|
2089 */
|
|
2090
|
|
2091 while (1)
|
|
2092 {
|
|
2093 char tempbuf[1024]; /* some random amount */
|
|
2094 Bufpos newpos, even_newer_pos;
|
|
2095 Bufpos oldpos = lisp_buffer_stream_startpos (istr);
|
|
2096 int size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
|
|
2097
|
|
2098 if (!size_in_bytes)
|
|
2099 break;
|
|
2100 newpos = lisp_buffer_stream_startpos (istr);
|
|
2101 Lstream_write (ostr, tempbuf, size_in_bytes);
|
|
2102 even_newer_pos = lisp_buffer_stream_startpos (istr);
|
|
2103 buffer_delete_range (buf, even_newer_pos - (newpos - oldpos),
|
|
2104 even_newer_pos, 0);
|
|
2105 }
|
|
2106 Lstream_close (istr);
|
|
2107 Lstream_close (ostr);
|
|
2108 UNGCPRO;
|
|
2109 Lstream_delete (istr);
|
|
2110 Lstream_delete (ostr);
|
|
2111 Lstream_delete (XLSTREAM (de_outstream));
|
|
2112 Lstream_delete (XLSTREAM (lb_outstream));
|
|
2113 return Qnil;
|
|
2114 }
|
|
2115
|
|
2116
|
|
2117 /************************************************************************/
|
|
2118 /* Converting to an external encoding ("encoding") */
|
|
2119 /************************************************************************/
|
|
2120
|
|
2121 /* An encoding stream is an output stream. When you create the
|
|
2122 stream, you specify the coding system that governs the encoding
|
|
2123 and another stream that the resulting encoded data is to be
|
|
2124 sent to, and then start sending data to it. */
|
|
2125
|
|
2126 #define ENCODING_STREAM_DATA(stream) LSTREAM_TYPE_DATA (stream, encoding)
|
|
2127
|
|
2128 struct encoding_stream
|
|
2129 {
|
|
2130 /* Coding system that governs the conversion. */
|
|
2131 struct Lisp_Coding_System *codesys;
|
|
2132
|
|
2133 /* Stream that we read the encoded data from or
|
|
2134 write the decoded data to. */
|
|
2135 Lstream *other_end;
|
|
2136
|
|
2137 /* If we are reading, then we can return only a fixed amount of
|
|
2138 data, so if the conversion resulted in too much data, we store it
|
|
2139 here for retrieval the next time around. */
|
|
2140 unsigned_char_dynarr *runoff;
|
|
2141
|
|
2142 /* FLAGS holds flags indicating the current state of the encoding.
|
|
2143 Some of these flags are dependent on the coding system. */
|
|
2144 unsigned int flags;
|
|
2145
|
|
2146 /* CH holds a partially built-up character. Since we only deal
|
|
2147 with one- and two-byte characters at the moment, we only use
|
|
2148 this to store the first byte of a two-byte character. */
|
|
2149 unsigned int ch;
|
|
2150 #ifdef MULE
|
|
2151 /* Additional information used by the ISO2022 encoder. */
|
|
2152 struct
|
|
2153 {
|
|
2154 /* CHARSET holds the character sets currently assigned to the G0
|
|
2155 through G3 registers. It is initialized from the array
|
|
2156 INITIAL_CHARSET in CODESYS. */
|
|
2157 Lisp_Object charset[4];
|
|
2158
|
|
2159 /* Which registers are currently invoked into the left (GL) and
|
|
2160 right (GR) halves of the 8-bit encoding space? */
|
|
2161 int register_left, register_right;
|
|
2162
|
|
2163 /* Whether we need to explicitly designate the charset in the
|
|
2164 G? register before using it. It is initialized from the
|
|
2165 array FORCE_CHARSET_ON_OUTPUT in CODESYS. */
|
|
2166 unsigned char force_charset_on_output[4];
|
|
2167
|
|
2168 /* Other state variables that need to be preserved across
|
|
2169 invocations. */
|
|
2170 Lisp_Object current_charset;
|
|
2171 int current_half;
|
|
2172 int current_char_boundary;
|
|
2173 } iso2022;
|
|
2174
|
|
2175 /* Additional information (the state of the running CCL program)
|
|
2176 used by the CCL encoder. */
|
|
2177 struct ccl_program ccl;
|
272
|
2178 #endif /* MULE */
|
259
|
2179 };
|
|
2180
|
272
|
2181 static int encoding_reader (Lstream *stream, unsigned char *data, size_t size);
|
259
|
2182 static int encoding_writer (Lstream *stream, CONST unsigned char *data,
|
272
|
2183 size_t size);
|
259
|
2184 static int encoding_rewinder (Lstream *stream);
|
|
2185 static int encoding_seekable_p (Lstream *stream);
|
|
2186 static int encoding_flusher (Lstream *stream);
|
|
2187 static int encoding_closer (Lstream *stream);
|
|
2188
|
|
2189 static Lisp_Object encoding_marker (Lisp_Object stream,
|
|
2190 void (*markobj) (Lisp_Object));
|
|
2191
|
|
2192 DEFINE_LSTREAM_IMPLEMENTATION ("encoding", lstream_encoding,
|
|
2193 sizeof (struct encoding_stream));
|
|
2194
|
|
2195 static Lisp_Object
|
|
2196 encoding_marker (Lisp_Object stream, void (*markobj) (Lisp_Object))
|
|
2197 {
|
|
2198 Lstream *str = ENCODING_STREAM_DATA (XLSTREAM (stream))->other_end;
|
|
2199 Lisp_Object str_obj;
|
|
2200
|
|
2201 /* We do not need to mark the coding systems or charsets stored
|
|
2202 within the stream because they are stored in a global list
|
|
2203 and automatically marked. */
|
|
2204
|
|
2205 XSETLSTREAM (str_obj, str);
|
|
2206 (markobj) (str_obj);
|
|
2207 if (str->imp->marker)
|
|
2208 return (str->imp->marker) (str_obj, markobj);
|
|
2209 else
|
|
2210 return Qnil;
|
|
2211 }
|
|
2212
|
|
2213 /* Read SIZE bytes of data and store it into DATA. We are a encoding stream
|
|
2214 so we read data from the other end, encode it, and store it into DATA. */
|
|
2215
|
|
2216 static int
|
272
|
2217 encoding_reader (Lstream *stream, unsigned char *data, size_t size)
|
259
|
2218 {
|
|
2219 struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
|
|
2220 unsigned char *orig_data = data;
|
|
2221 int read_size;
|
|
2222 int error_occurred = 0;
|
|
2223
|
|
2224 /* We need to interface to mule_encode(), which expects to take some
|
|
2225 amount of data and store the result into a Dynarr. We have
|
|
2226 mule_encode() store into str->runoff, and take data from there
|
|
2227 as necessary. */
|
|
2228
|
|
2229 /* We loop until we have enough data, reading chunks from the other
|
|
2230 end and encoding it. */
|
|
2231 while (1)
|
|
2232 {
|
|
2233 /* Take data from the runoff if we can. Make sure to take at
|
|
2234 most SIZE bytes, and delete the data from the runoff. */
|
|
2235 if (Dynarr_length (str->runoff) > 0)
|
|
2236 {
|
272
|
2237 int chunk = min ((int) size, Dynarr_length (str->runoff));
|
259
|
2238 memcpy (data, Dynarr_atp (str->runoff, 0), chunk);
|
|
2239 Dynarr_delete_many (str->runoff, 0, chunk);
|
|
2240 data += chunk;
|
|
2241 size -= chunk;
|
|
2242 }
|
|
2243
|
|
2244 if (size == 0)
|
|
2245 break; /* No more room for data */
|
|
2246
|
|
2247 if (str->flags & CODING_STATE_END)
|
|
2248 /* This means that on the previous iteration, we hit the EOF on
|
|
2249 the other end. We loop once more so that mule_encode() can
|
|
2250 output any final stuff it may be holding, or any "go back
|
|
2251 to a sane state" escape sequences. (This latter makes sense
|
|
2252 during encoding.) */
|
|
2253 break;
|
|
2254
|
|
2255 /* Exhausted the runoff, so get some more. DATA at least SIZE bytes
|
|
2256 left of storage in it, so it's OK to read directly into it.
|
|
2257 (We'll be overwriting above, after we've encoded it into the
|
|
2258 runoff.) */
|
|
2259 read_size = Lstream_read (str->other_end, data, size);
|
|
2260 if (read_size < 0)
|
|
2261 {
|
|
2262 error_occurred = 1;
|
|
2263 break;
|
|
2264 }
|
|
2265 if (read_size == 0)
|
|
2266 /* There might be some more end data produced in the translation.
|
|
2267 See the comment above. */
|
|
2268 str->flags |= CODING_STATE_END;
|
|
2269 mule_encode (stream, data, str->runoff, read_size);
|
|
2270 }
|
|
2271
|
|
2272 if (data == orig_data)
|
|
2273 return error_occurred ? -1 : 0;
|
|
2274 else
|
|
2275 return data - orig_data;
|
|
2276 }
|
|
2277
|
|
2278 static int
|
272
|
2279 encoding_writer (Lstream *stream, CONST unsigned char *data, size_t size)
|
259
|
2280 {
|
|
2281 struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
|
|
2282 int retval;
|
|
2283
|
|
2284 /* Encode all our data into the runoff, and then attempt to write
|
|
2285 it all out to the other end. Remove whatever chunk we succeeded
|
|
2286 in writing. */
|
|
2287 mule_encode (stream, data, str->runoff, size);
|
|
2288 retval = Lstream_write (str->other_end, Dynarr_atp (str->runoff, 0),
|
|
2289 Dynarr_length (str->runoff));
|
|
2290 if (retval > 0)
|
|
2291 Dynarr_delete_many (str->runoff, 0, retval);
|
|
2292 /* Do NOT return retval. The return value indicates how much
|
|
2293 of the incoming data was written, not how many bytes were
|
|
2294 written. */
|
|
2295 return size;
|
|
2296 }
|
|
2297
|
|
2298 static void
|
|
2299 reset_encoding_stream (struct encoding_stream *str)
|
|
2300 {
|
272
|
2301 #ifdef MULE
|
259
|
2302 switch (CODING_SYSTEM_TYPE (str->codesys))
|
|
2303 {
|
|
2304 case CODESYS_ISO2022:
|
|
2305 {
|
|
2306 int i;
|
|
2307
|
|
2308 for (i = 0; i < 4; i++)
|
|
2309 {
|
|
2310 str->iso2022.charset[i] =
|
|
2311 CODING_SYSTEM_ISO2022_INITIAL_CHARSET (str->codesys, i);
|
|
2312 str->iso2022.force_charset_on_output[i] =
|
|
2313 CODING_SYSTEM_ISO2022_FORCE_CHARSET_ON_OUTPUT (str->codesys, i);
|
|
2314 }
|
|
2315 str->iso2022.register_left = 0;
|
|
2316 str->iso2022.register_right = 1;
|
|
2317 str->iso2022.current_charset = Qnil;
|
|
2318 str->iso2022.current_half = 0;
|
|
2319 str->iso2022.current_char_boundary = 1;
|
|
2320 break;
|
|
2321 }
|
|
2322 case CODESYS_CCL:
|
|
2323 setup_ccl_program (&str->ccl, CODING_SYSTEM_CCL_ENCODE (str->codesys));
|
|
2324 break;
|
|
2325 default:
|
|
2326 break;
|
|
2327 }
|
272
|
2328 #endif /* MULE */
|
259
|
2329
|
|
2330 str->flags = str->ch = 0;
|
|
2331 }
|
|
2332
|
|
2333 static int
|
|
2334 encoding_rewinder (Lstream *stream)
|
|
2335 {
|
|
2336 struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
|
|
2337 reset_encoding_stream (str);
|
|
2338 Dynarr_reset (str->runoff);
|
|
2339 return Lstream_rewind (str->other_end);
|
|
2340 }
|
|
2341
|
|
2342 static int
|
|
2343 encoding_seekable_p (Lstream *stream)
|
|
2344 {
|
|
2345 struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
|
|
2346 return Lstream_seekable_p (str->other_end);
|
|
2347 }
|
|
2348
|
|
2349 static int
|
|
2350 encoding_flusher (Lstream *stream)
|
|
2351 {
|
|
2352 struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
|
|
2353 return Lstream_flush (str->other_end);
|
|
2354 }
|
|
2355
|
|
2356 static int
|
|
2357 encoding_closer (Lstream *stream)
|
|
2358 {
|
|
2359 struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
|
|
2360 if (stream->flags & LSTREAM_FL_WRITE)
|
|
2361 {
|
|
2362 str->flags |= CODING_STATE_END;
|
|
2363 encoding_writer (stream, 0, 0);
|
|
2364 }
|
|
2365 Dynarr_free (str->runoff);
|
|
2366 return Lstream_close (str->other_end);
|
|
2367 }
|
|
2368
|
|
2369 Lisp_Object
|
|
2370 encoding_stream_coding_system (Lstream *stream)
|
|
2371 {
|
272
|
2372 Lisp_Object coding_system;
|
259
|
2373 struct encoding_stream *str = ENCODING_STREAM_DATA (stream);
|
|
2374
|
|
2375 XSETCODING_SYSTEM (coding_system, str->codesys);
|
|
2376 return coding_system;
|
|
2377 }
|
|
2378
|
|
2379 void
|
|
2380 set_encoding_stream_coding_system (Lstream *lstr, Lisp_Object codesys)
|
|
2381 {
|
|
2382 struct Lisp_Coding_System *cs = XCODING_SYSTEM (codesys);
|
|
2383 struct encoding_stream *str = ENCODING_STREAM_DATA (lstr);
|
|
2384 str->codesys = cs;
|
|
2385 reset_encoding_stream (str);
|
|
2386 }
|
|
2387
|
|
2388 static Lisp_Object
|
|
2389 make_encoding_stream_1 (Lstream *stream, Lisp_Object codesys,
|
|
2390 CONST char *mode)
|
|
2391 {
|
|
2392 Lstream *lstr = Lstream_new (lstream_encoding, mode);
|
|
2393 struct encoding_stream *str = ENCODING_STREAM_DATA (lstr);
|
|
2394 Lisp_Object obj;
|
|
2395
|
272
|
2396 xzero (*str);
|
259
|
2397 str->runoff = Dynarr_new (unsigned_char);
|
|
2398 str->other_end = stream;
|
|
2399 set_encoding_stream_coding_system (lstr, codesys);
|
|
2400 XSETLSTREAM (obj, lstr);
|
|
2401 return obj;
|
|
2402 }
|
|
2403
|
|
2404 Lisp_Object
|
|
2405 make_encoding_input_stream (Lstream *stream, Lisp_Object codesys)
|
|
2406 {
|
|
2407 return make_encoding_stream_1 (stream, codesys, "r");
|
|
2408 }
|
|
2409
|
|
2410 Lisp_Object
|
|
2411 make_encoding_output_stream (Lstream *stream, Lisp_Object codesys)
|
|
2412 {
|
|
2413 return make_encoding_stream_1 (stream, codesys, "w");
|
|
2414 }
|
|
2415
|
|
2416 /* Convert N bytes of internally-formatted data stored in SRC to an
|
|
2417 external format, according to the encoding stream ENCODING.
|
|
2418 Store the encoded data into DST. */
|
|
2419
|
|
2420 static void
|
|
2421 mule_encode (Lstream *encoding, CONST unsigned char *src,
|
|
2422 unsigned_char_dynarr *dst, unsigned int n)
|
|
2423 {
|
|
2424 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
|
|
2425
|
|
2426 switch (CODING_SYSTEM_TYPE (str->codesys))
|
|
2427 {
|
|
2428 #ifdef DEBUG_XEMACS
|
|
2429 case CODESYS_INTERNAL:
|
|
2430 Dynarr_add_many (dst, src, n);
|
|
2431 break;
|
|
2432 #endif
|
|
2433 case CODESYS_AUTODETECT:
|
|
2434 /* If we got this far and still haven't decided on the coding
|
|
2435 system, then do no conversion. */
|
|
2436 case CODESYS_NO_CONVERSION:
|
|
2437 encode_coding_no_conversion (encoding, src, dst, n);
|
|
2438 break;
|
|
2439 #ifdef MULE
|
|
2440 case CODESYS_SHIFT_JIS:
|
|
2441 encode_coding_sjis (encoding, src, dst, n);
|
|
2442 break;
|
|
2443 case CODESYS_BIG5:
|
|
2444 encode_coding_big5 (encoding, src, dst, n);
|
|
2445 break;
|
|
2446 case CODESYS_CCL:
|
|
2447 ccl_driver (&str->ccl, src, dst, n, 0);
|
|
2448 break;
|
|
2449 case CODESYS_ISO2022:
|
|
2450 encode_coding_iso2022 (encoding, src, dst, n);
|
|
2451 break;
|
|
2452 #endif /* MULE */
|
|
2453 default:
|
|
2454 abort ();
|
|
2455 }
|
|
2456 }
|
|
2457
|
|
2458 DEFUN ("encode-coding-region", Fencode_coding_region, 3, 4, 0, /*
|
|
2459 Encode the text between START and END using CODING-SYSTEM.
|
|
2460 This will, for example, convert Japanese characters into stuff such as
|
|
2461 "^[$B!<!+^[(B" if you use the JIS encoding. Return length of encoded
|
|
2462 text. BUFFER defaults to the current buffer if unspecified.
|
|
2463 */
|
|
2464 (start, end, coding_system, buffer))
|
|
2465 {
|
|
2466 Bufpos b, e;
|
|
2467 struct buffer *buf = decode_buffer (buffer, 0);
|
|
2468 Lisp_Object instream, lb_outstream, de_outstream, outstream;
|
|
2469 Lstream *istr, *ostr;
|
|
2470 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
|
|
2471
|
|
2472 get_buffer_range_char (buf, start, end, &b, &e, 0);
|
|
2473
|
|
2474 barf_if_buffer_read_only (buf, b, e);
|
|
2475
|
|
2476 coding_system = Fget_coding_system (coding_system);
|
|
2477 instream = make_lisp_buffer_input_stream (buf, b, e, 0);
|
|
2478 lb_outstream = make_lisp_buffer_output_stream (buf, b, 0);
|
|
2479 de_outstream = make_decoding_output_stream (XLSTREAM (lb_outstream),
|
|
2480 Fget_coding_system (Qbinary));
|
|
2481 outstream = make_encoding_output_stream (XLSTREAM (de_outstream),
|
|
2482 coding_system);
|
|
2483 istr = XLSTREAM (instream);
|
|
2484 ostr = XLSTREAM (outstream);
|
|
2485 GCPRO4 (instream, outstream, de_outstream, lb_outstream);
|
|
2486 /* The chain of streams looks like this:
|
|
2487
|
|
2488 [BUFFER] <----- send through
|
|
2489 ------> [ENCODE AS SPECIFIED]
|
|
2490 ------> [DECODE AS BINARY]
|
|
2491 ------> [BUFFER]
|
|
2492 */
|
|
2493 while (1)
|
|
2494 {
|
|
2495 char tempbuf[1024]; /* some random amount */
|
|
2496 Bufpos newpos, even_newer_pos;
|
|
2497 Bufpos oldpos = lisp_buffer_stream_startpos (istr);
|
|
2498 int size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
|
|
2499
|
|
2500 if (!size_in_bytes)
|
|
2501 break;
|
|
2502 newpos = lisp_buffer_stream_startpos (istr);
|
|
2503 Lstream_write (ostr, tempbuf, size_in_bytes);
|
|
2504 even_newer_pos = lisp_buffer_stream_startpos (istr);
|
|
2505 buffer_delete_range (buf, even_newer_pos - (newpos - oldpos),
|
|
2506 even_newer_pos, 0);
|
|
2507 }
|
|
2508
|
|
2509 {
|
|
2510 Charcount retlen =
|
|
2511 lisp_buffer_stream_startpos (XLSTREAM (instream)) - b;
|
|
2512 Lstream_close (istr);
|
|
2513 Lstream_close (ostr);
|
|
2514 UNGCPRO;
|
|
2515 Lstream_delete (istr);
|
|
2516 Lstream_delete (ostr);
|
|
2517 Lstream_delete (XLSTREAM (de_outstream));
|
|
2518 Lstream_delete (XLSTREAM (lb_outstream));
|
|
2519 return make_int (retlen);
|
|
2520 }
|
|
2521 }
|
|
2522
|
|
2523 #ifdef MULE
|
|
2524
|
|
2525 /************************************************************************/
|
|
2526 /* Shift-JIS methods */
|
|
2527 /************************************************************************/
|
|
2528
|
|
2529 /* Shift-JIS is a coding system encoding three character sets: ASCII, right
|
|
2530 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
|
|
2531 as is. A character of JISX0201-Kana (TYPE94 character set) is
|
|
2532 encoded by "position-code + 0x80". A character of JISX0208
|
|
2533 (TYPE94x94 character set) is encoded in 2-byte but two
|
|
2534 position-codes are divided and shifted so that it fit in the range
|
|
2535 below.
|
|
2536
|
|
2537 --- CODE RANGE of Shift-JIS ---
|
|
2538 (character set) (range)
|
|
2539 ASCII 0x00 .. 0x7F
|
|
2540 JISX0201-Kana 0xA0 .. 0xDF
|
|
2541 JISX0208 (1st byte) 0x80 .. 0x9F and 0xE0 .. 0xEF
|
|
2542 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
|
|
2543 -------------------------------
|
|
2544
|
|
2545 */
|
|
2546
|
|
2547 /* Is this the first byte of a Shift-JIS two-byte char? */
|
|
2548
|
|
2549 #define BYTE_SJIS_TWO_BYTE_1_P(c) \
|
|
2550 (((c) >= 0x81 && (c) <= 0x9F) || ((c) >= 0xE0 && (c) <= 0xEF))
|
|
2551
|
|
2552 /* Is this the second byte of a Shift-JIS two-byte char? */
|
|
2553
|
|
2554 #define BYTE_SJIS_TWO_BYTE_2_P(c) \
|
|
2555 (((c) >= 0x40 && (c) <= 0x7E) || ((c) >= 0x80 && (c) <= 0xFC))
|
|
2556
|
|
2557 #define BYTE_SJIS_KATAKANA_P(c) \
|
|
2558 ((c) >= 0xA1 && (c) <= 0xDF)
|
|
2559
|
|
2560 static int
|
|
2561 detect_coding_sjis (struct detection_state *st, CONST unsigned char *src,
|
|
2562 unsigned int n)
|
|
2563 {
|
|
2564 int c;
|
|
2565
|
|
2566 while (n--)
|
|
2567 {
|
|
2568 c = *src++;
|
|
2569 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
|
|
2570 return 0;
|
|
2571 if (st->shift_jis.in_second_byte)
|
|
2572 {
|
|
2573 st->shift_jis.in_second_byte = 0;
|
|
2574 if (c < 0x40)
|
|
2575 return 0;
|
|
2576 }
|
|
2577 else if ((c >= 0x80 && c < 0xA0) || c >= 0xE0)
|
|
2578 st->shift_jis.in_second_byte = 1;
|
|
2579 }
|
|
2580 return CODING_CATEGORY_SHIFT_JIS_MASK;
|
|
2581 }
|
|
2582
|
|
2583 /* Convert Shift-JIS data to internal format. */
|
|
2584
|
|
2585 static void
|
|
2586 decode_coding_sjis (Lstream *decoding, CONST unsigned char *src,
|
|
2587 unsigned_char_dynarr *dst, unsigned int n)
|
|
2588 {
|
|
2589 unsigned char c;
|
|
2590 unsigned int flags, ch;
|
|
2591 enum eol_type eol_type;
|
|
2592 struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
|
|
2593
|
|
2594 CODING_STREAM_DECOMPOSE (str, flags, ch);
|
|
2595 eol_type = str->eol_type;
|
|
2596
|
|
2597 while (n--)
|
|
2598 {
|
|
2599 c = *src++;
|
|
2600
|
|
2601 if (ch)
|
|
2602 {
|
|
2603 /* Previous character was first byte of Shift-JIS Kanji char. */
|
|
2604 if (BYTE_SJIS_TWO_BYTE_2_P (c))
|
|
2605 {
|
|
2606 unsigned char e1, e2;
|
|
2607
|
|
2608 Dynarr_add (dst, LEADING_BYTE_JAPANESE_JISX0208);
|
|
2609 DECODE_SJIS (ch, c, e1, e2);
|
|
2610 Dynarr_add (dst, e1);
|
|
2611 Dynarr_add (dst, e2);
|
|
2612 }
|
|
2613 else
|
|
2614 {
|
|
2615 DECODE_ADD_BINARY_CHAR (ch, dst);
|
|
2616 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
2617 }
|
|
2618 ch = 0;
|
|
2619 }
|
|
2620 else
|
|
2621 {
|
|
2622 DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
|
|
2623 if (BYTE_SJIS_TWO_BYTE_1_P (c))
|
|
2624 ch = c;
|
|
2625 else if (BYTE_SJIS_KATAKANA_P (c))
|
|
2626 {
|
|
2627 Dynarr_add (dst, LEADING_BYTE_KATAKANA_JISX0201);
|
|
2628 Dynarr_add (dst, c);
|
|
2629 }
|
|
2630 else
|
|
2631 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
2632 }
|
|
2633 label_continue_loop:;
|
|
2634 }
|
|
2635
|
|
2636 DECODE_HANDLE_END_OF_CONVERSION (flags, ch, dst);
|
|
2637
|
|
2638 CODING_STREAM_COMPOSE (str, flags, ch);
|
|
2639 }
|
|
2640
|
|
2641 /* Convert internally-formatted data to Shift-JIS. */
|
|
2642
|
|
2643 static void
|
|
2644 encode_coding_sjis (Lstream *encoding, CONST unsigned char *src,
|
|
2645 unsigned_char_dynarr *dst, unsigned int n)
|
|
2646 {
|
|
2647 unsigned char c;
|
|
2648 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
|
|
2649 unsigned int flags, ch;
|
|
2650 enum eol_type eol_type;
|
|
2651
|
|
2652 CODING_STREAM_DECOMPOSE (str, flags, ch);
|
|
2653 eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys);
|
|
2654
|
|
2655 while (n--)
|
|
2656 {
|
|
2657 c = *src++;
|
|
2658 if (c == '\n')
|
|
2659 {
|
|
2660 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
|
|
2661 Dynarr_add (dst, '\r');
|
|
2662 if (eol_type != EOL_CR)
|
|
2663 Dynarr_add (dst, '\n');
|
|
2664 ch = 0;
|
|
2665 }
|
|
2666 else if (BYTE_ASCII_P (c))
|
|
2667 {
|
|
2668 Dynarr_add (dst, c);
|
|
2669 ch = 0;
|
|
2670 }
|
|
2671 else if (BUFBYTE_LEADING_BYTE_P (c))
|
|
2672 ch = (c == LEADING_BYTE_KATAKANA_JISX0201 ||
|
|
2673 c == LEADING_BYTE_JAPANESE_JISX0208_1978 ||
|
|
2674 c == LEADING_BYTE_JAPANESE_JISX0208) ? c : 0;
|
|
2675 else if (ch)
|
|
2676 {
|
|
2677 if (ch == LEADING_BYTE_KATAKANA_JISX0201)
|
|
2678 {
|
|
2679 Dynarr_add (dst, c);
|
|
2680 ch = 0;
|
|
2681 }
|
|
2682 else if (ch == LEADING_BYTE_JAPANESE_JISX0208_1978 ||
|
|
2683 ch == LEADING_BYTE_JAPANESE_JISX0208)
|
|
2684 ch = c;
|
|
2685 else
|
|
2686 {
|
|
2687 unsigned char j1, j2;
|
|
2688 ENCODE_SJIS (ch, c, j1, j2);
|
|
2689 Dynarr_add (dst, j1);
|
|
2690 Dynarr_add (dst, j2);
|
|
2691 ch = 0;
|
|
2692 }
|
|
2693 }
|
|
2694 }
|
|
2695
|
|
2696 CODING_STREAM_COMPOSE (str, flags, ch);
|
|
2697 }
|
|
2698
|
|
2699 DEFUN ("decode-shift-jis-char", Fdecode_shift_jis_char, 1, 1, 0, /*
|
|
2700 Decode a JISX0208 character of Shift-JIS coding-system.
|
|
2701 CODE is the character code in Shift-JIS as a cons of type bytes.
|
|
2702 Return the corresponding character.
|
|
2703 */
|
|
2704 (code))
|
|
2705 {
|
|
2706 unsigned char c1, c2, s1, s2;
|
|
2707
|
|
2708 CHECK_CONS (code);
|
|
2709 CHECK_INT (XCAR (code));
|
|
2710 CHECK_INT (XCDR (code));
|
|
2711 s1 = XINT (XCAR (code));
|
|
2712 s2 = XINT (XCDR (code));
|
|
2713 if (BYTE_SJIS_TWO_BYTE_1_P (s1) &&
|
|
2714 BYTE_SJIS_TWO_BYTE_2_P (s2))
|
|
2715 {
|
|
2716 DECODE_SJIS (s1, s2, c1, c2);
|
|
2717 return make_char (MAKE_CHAR (Vcharset_japanese_jisx0208,
|
|
2718 c1 & 0x7F, c2 & 0x7F));
|
|
2719 }
|
|
2720 else
|
|
2721 return Qnil;
|
|
2722 }
|
|
2723
|
|
2724 DEFUN ("encode-shift-jis-char", Fencode_shift_jis_char, 1, 1, 0, /*
|
|
2725 Encode a JISX0208 character CHAR to SHIFT-JIS coding-system.
|
|
2726 Return the corresponding character code in SHIFT-JIS as a cons of two bytes.
|
|
2727 */
|
|
2728 (ch))
|
|
2729 {
|
|
2730 Lisp_Object charset;
|
|
2731 int c1, c2, s1, s2;
|
|
2732
|
|
2733 CHECK_CHAR_COERCE_INT (ch);
|
|
2734 BREAKUP_CHAR (XCHAR (ch), charset, c1, c2);
|
|
2735 if (EQ (charset, Vcharset_japanese_jisx0208))
|
|
2736 {
|
|
2737 ENCODE_SJIS (c1 | 0x80, c2 | 0x80, s1, s2);
|
|
2738 return Fcons (make_int (s1), make_int (s2));
|
|
2739 }
|
|
2740 else
|
|
2741 return Qnil;
|
|
2742 }
|
|
2743
|
|
2744
|
|
2745 /************************************************************************/
|
|
2746 /* Big5 methods */
|
|
2747 /************************************************************************/
|
|
2748
|
|
2749 /* BIG5 is a coding system encoding two character sets: ASCII and
|
|
2750 Big5. An ASCII character is encoded as is. Big5 is a two-byte
|
|
2751 character set and is encoded in two-byte.
|
|
2752
|
|
2753 --- CODE RANGE of BIG5 ---
|
|
2754 (character set) (range)
|
|
2755 ASCII 0x00 .. 0x7F
|
|
2756 Big5 (1st byte) 0xA1 .. 0xFE
|
|
2757 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
|
|
2758 --------------------------
|
|
2759
|
|
2760 Since the number of characters in Big5 is larger than maximum
|
|
2761 characters in Emacs' charset (96x96), it can't be handled as one
|
|
2762 charset. So, in Emacs, Big5 is devided into two: `charset-big5-1'
|
|
2763 and `charset-big5-2'. Both <type>s are TYPE94x94. The former
|
|
2764 contains frequently used characters and the latter contains less
|
|
2765 frequently used characters. */
|
|
2766
|
|
2767 #define BYTE_BIG5_TWO_BYTE_1_P(c) \
|
|
2768 ((c) >= 0xA1 && (c) <= 0xFE)
|
|
2769
|
|
2770 /* Is this the second byte of a Shift-JIS two-byte char? */
|
|
2771
|
|
2772 #define BYTE_BIG5_TWO_BYTE_2_P(c) \
|
|
2773 (((c) >= 0x40 && (c) <= 0x7E) || ((c) >= 0xA1 && (c) <= 0xFE))
|
|
2774
|
|
2775 /* Number of Big5 characters which have the same code in 1st byte. */
|
|
2776
|
|
2777 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
|
|
2778
|
|
2779 /* Code conversion macros. These are macros because they are used in
|
|
2780 inner loops during code conversion.
|
|
2781
|
|
2782 Note that temporary variables in macros introduce the classic
|
|
2783 dynamic-scoping problems with variable names. We use capital-
|
|
2784 lettered variables in the assumption that XEmacs does not use
|
|
2785 capital letters in variables except in a very formalized way
|
|
2786 (e.g. Qstring). */
|
|
2787
|
|
2788 /* Convert Big5 code (b1, b2) into its internal string representation
|
|
2789 (lb, c1, c2). */
|
|
2790
|
|
2791 /* There is a much simpler way to split the Big5 charset into two.
|
|
2792 For the moment I'm going to leave the algorithm as-is because it
|
|
2793 claims to separate out the most-used characters into a single
|
|
2794 charset, which perhaps will lead to optimizations in various
|
|
2795 places.
|
|
2796
|
|
2797 The way the algorithm works is something like this:
|
|
2798
|
|
2799 Big5 can be viewed as a 94x157 charset, where the row is
|
|
2800 encoded into the bytes 0xA1 .. 0xFE and the column is encoded
|
|
2801 into the bytes 0x40 .. 0x7E and 0xA1 .. 0xFE. As for frequency,
|
|
2802 the split between low and high column numbers is apparently
|
|
2803 meaningless; ascending rows produce less and less frequent chars.
|
|
2804 Therefore, we assign the lower half of rows (0xA1 .. 0xC8) to
|
|
2805 the first charset, and the upper half (0xC9 .. 0xFE) to the
|
|
2806 second. To do the conversion, we convert the character into
|
|
2807 a single number where 0 .. 156 is the first row, 157 .. 313
|
|
2808 is the second, etc. That way, the characters are ordered by
|
|
2809 decreasing frequency. Then we just chop the space in two
|
|
2810 and coerce the result into a 94x94 space.
|
|
2811 */
|
|
2812
|
|
2813 #define DECODE_BIG5(b1, b2, lb, c1, c2) do \
|
|
2814 { \
|
|
2815 int B1 = b1, B2 = b2; \
|
|
2816 unsigned int I \
|
|
2817 = (B1 - 0xA1) * BIG5_SAME_ROW + B2 - (B2 < 0x7F ? 0x40 : 0x62); \
|
|
2818 \
|
|
2819 if (B1 < 0xC9) \
|
|
2820 { \
|
|
2821 lb = LEADING_BYTE_CHINESE_BIG5_1; \
|
|
2822 } \
|
|
2823 else \
|
|
2824 { \
|
|
2825 lb = LEADING_BYTE_CHINESE_BIG5_2; \
|
|
2826 I -= (BIG5_SAME_ROW) * (0xC9 - 0xA1); \
|
|
2827 } \
|
|
2828 c1 = I / (0xFF - 0xA1) + 0xA1; \
|
|
2829 c2 = I % (0xFF - 0xA1) + 0xA1; \
|
|
2830 } while (0)
|
|
2831
|
|
2832 /* Convert the internal string representation of a Big5 character
|
|
2833 (lb, c1, c2) into Big5 code (b1, b2). */
|
|
2834
|
|
2835 #define ENCODE_BIG5(lb, c1, c2, b1, b2) do \
|
|
2836 { \
|
|
2837 unsigned int I = ((c1) - 0xA1) * (0xFF - 0xA1) + ((c2) - 0xA1); \
|
|
2838 \
|
|
2839 if (lb == LEADING_BYTE_CHINESE_BIG5_2) \
|
|
2840 { \
|
|
2841 I += BIG5_SAME_ROW * (0xC9 - 0xA1); \
|
|
2842 } \
|
|
2843 b1 = I / BIG5_SAME_ROW + 0xA1; \
|
|
2844 b2 = I % BIG5_SAME_ROW; \
|
|
2845 b2 += b2 < 0x3F ? 0x40 : 0x62; \
|
|
2846 } while (0)
|
|
2847
|
|
2848 static int
|
|
2849 detect_coding_big5 (struct detection_state *st, CONST unsigned char *src,
|
|
2850 unsigned int n)
|
|
2851 {
|
|
2852 int c;
|
|
2853
|
|
2854 while (n--)
|
|
2855 {
|
|
2856 c = *src++;
|
|
2857 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO ||
|
|
2858 (c >= 0x80 && c <= 0xA0))
|
|
2859 return 0;
|
|
2860 if (st->big5.in_second_byte)
|
|
2861 {
|
|
2862 st->big5.in_second_byte = 0;
|
|
2863 if (c < 0x40 || (c >= 0x80 && c <= 0xA0))
|
|
2864 return 0;
|
|
2865 }
|
|
2866 else if (c >= 0xA1)
|
|
2867 st->big5.in_second_byte = 1;
|
|
2868 }
|
|
2869 return CODING_CATEGORY_BIG5_MASK;
|
|
2870 }
|
|
2871
|
|
2872 /* Convert Big5 data to internal format. */
|
|
2873
|
|
2874 static void
|
|
2875 decode_coding_big5 (Lstream *decoding, CONST unsigned char *src,
|
|
2876 unsigned_char_dynarr *dst, unsigned int n)
|
|
2877 {
|
|
2878 unsigned char c;
|
|
2879 unsigned int flags, ch;
|
|
2880 enum eol_type eol_type;
|
|
2881 struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
|
|
2882
|
|
2883 CODING_STREAM_DECOMPOSE (str, flags, ch);
|
|
2884 eol_type = str->eol_type;
|
|
2885
|
|
2886 while (n--)
|
|
2887 {
|
|
2888 c = *src++;
|
|
2889 if (ch)
|
|
2890 {
|
|
2891 /* Previous character was first byte of Big5 char. */
|
|
2892 if (BYTE_BIG5_TWO_BYTE_2_P (c))
|
|
2893 {
|
|
2894 unsigned char b1, b2, b3;
|
|
2895 DECODE_BIG5 (ch, c, b1, b2, b3);
|
|
2896 Dynarr_add (dst, b1);
|
|
2897 Dynarr_add (dst, b2);
|
|
2898 Dynarr_add (dst, b3);
|
|
2899 }
|
|
2900 else
|
|
2901 {
|
|
2902 DECODE_ADD_BINARY_CHAR (ch, dst);
|
|
2903 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
2904 }
|
|
2905 ch = 0;
|
|
2906 }
|
|
2907 else
|
|
2908 {
|
|
2909 DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
|
|
2910 if (BYTE_BIG5_TWO_BYTE_1_P (c))
|
|
2911 ch = c;
|
|
2912 else
|
|
2913 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
2914 }
|
|
2915 label_continue_loop:;
|
|
2916 }
|
|
2917
|
|
2918 DECODE_HANDLE_END_OF_CONVERSION (flags, ch, dst);
|
|
2919
|
|
2920 CODING_STREAM_COMPOSE (str, flags, ch);
|
|
2921 }
|
|
2922
|
|
2923 /* Convert internally-formatted data to Big5. */
|
|
2924
|
|
2925 static void
|
|
2926 encode_coding_big5 (Lstream *encoding, CONST unsigned char *src,
|
|
2927 unsigned_char_dynarr *dst, unsigned int n)
|
|
2928 {
|
|
2929 unsigned char c;
|
|
2930 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
|
|
2931 unsigned int flags, ch;
|
|
2932 enum eol_type eol_type;
|
|
2933
|
|
2934 CODING_STREAM_DECOMPOSE (str, flags, ch);
|
|
2935 eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys);
|
|
2936
|
|
2937 while (n--)
|
|
2938 {
|
|
2939 c = *src++;
|
|
2940 if (c == '\n')
|
|
2941 {
|
|
2942 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
|
|
2943 Dynarr_add (dst, '\r');
|
|
2944 if (eol_type != EOL_CR)
|
|
2945 Dynarr_add (dst, '\n');
|
|
2946 }
|
|
2947 else if (BYTE_ASCII_P (c))
|
|
2948 {
|
|
2949 /* ASCII. */
|
|
2950 Dynarr_add (dst, c);
|
|
2951 }
|
|
2952 else if (BUFBYTE_LEADING_BYTE_P (c))
|
|
2953 {
|
|
2954 if (c == LEADING_BYTE_CHINESE_BIG5_1 ||
|
|
2955 c == LEADING_BYTE_CHINESE_BIG5_2)
|
|
2956 {
|
|
2957 /* A recognized leading byte. */
|
|
2958 ch = c;
|
|
2959 continue; /* not done with this character. */
|
|
2960 }
|
|
2961 /* otherwise just ignore this character. */
|
|
2962 }
|
|
2963 else if (ch == LEADING_BYTE_CHINESE_BIG5_1 ||
|
|
2964 ch == LEADING_BYTE_CHINESE_BIG5_2)
|
|
2965 {
|
|
2966 /* Previous char was a recognized leading byte. */
|
|
2967 ch = (ch << 8) | c;
|
|
2968 continue; /* not done with this character. */
|
|
2969 }
|
|
2970 else if (ch)
|
|
2971 {
|
|
2972 /* Encountering second byte of a Big5 character. */
|
|
2973 unsigned char b1, b2;
|
|
2974
|
|
2975 ENCODE_BIG5 (ch >> 8, ch & 0xFF, c, b1, b2);
|
|
2976 Dynarr_add (dst, b1);
|
|
2977 Dynarr_add (dst, b2);
|
|
2978 }
|
|
2979
|
|
2980 ch = 0;
|
|
2981 }
|
|
2982
|
|
2983 CODING_STREAM_COMPOSE (str, flags, ch);
|
|
2984 }
|
|
2985
|
|
2986
|
|
2987 DEFUN ("decode-big5-char", Fdecode_big5_char, 1, 1, 0, /*
|
|
2988 Decode a Big5 character CODE of BIG5 coding-system.
|
|
2989 CODE is the character code in BIG5, a cons of two integers.
|
|
2990 Return the corresponding character.
|
|
2991 */
|
|
2992 (code))
|
|
2993 {
|
|
2994 unsigned char c1, c2, b1, b2;
|
|
2995
|
|
2996 CHECK_CONS (code);
|
|
2997 CHECK_INT (XCAR (code));
|
|
2998 CHECK_INT (XCDR (code));
|
|
2999 b1 = XINT (XCAR (code));
|
|
3000 b2 = XINT (XCDR (code));
|
|
3001 if (BYTE_BIG5_TWO_BYTE_1_P (b1) &&
|
|
3002 BYTE_BIG5_TWO_BYTE_2_P (b2))
|
|
3003 {
|
|
3004 int leading_byte;
|
|
3005 Lisp_Object charset;
|
|
3006 DECODE_BIG5 (b1, b2, leading_byte, c1, c2);
|
|
3007 charset = CHARSET_BY_LEADING_BYTE (leading_byte);
|
|
3008 return make_char (MAKE_CHAR (charset, c1 & 0x7F, c2 & 0x7F));
|
|
3009 }
|
|
3010 else
|
|
3011 return Qnil;
|
|
3012 }
|
|
3013
|
|
3014 DEFUN ("encode-big5-char", Fencode_big5_char, 1, 1, 0, /*
|
|
3015 Encode the Big5 character CH to BIG5 coding-system.
|
|
3016 Return the corresponding character code in Big5.
|
|
3017 */
|
|
3018 (ch))
|
|
3019 {
|
|
3020 Lisp_Object charset;
|
|
3021 int c1, c2, b1, b2;
|
|
3022
|
|
3023 CHECK_CHAR_COERCE_INT (ch);
|
|
3024 BREAKUP_CHAR (XCHAR (ch), charset, c1, c2);
|
|
3025 if (EQ (charset, Vcharset_chinese_big5_1) ||
|
|
3026 EQ (charset, Vcharset_chinese_big5_2))
|
|
3027 {
|
|
3028 ENCODE_BIG5 (XCHARSET_LEADING_BYTE (charset), c1 | 0x80, c2 | 0x80,
|
|
3029 b1, b2);
|
|
3030 return Fcons (make_int (b1), make_int (b2));
|
|
3031 }
|
|
3032 else
|
|
3033 return Qnil;
|
|
3034 }
|
|
3035
|
|
3036
|
|
3037 /************************************************************************/
|
|
3038 /* ISO2022 methods */
|
|
3039 /************************************************************************/
|
|
3040
|
|
3041 /* The following note describes the coding system ISO2022 briefly.
|
|
3042 Since the intention of this note is to help understanding of the
|
|
3043 programs in this file, some parts are NOT ACCURATE or OVERLY
|
|
3044 SIMPLIFIED. For thorough understanding, please refer to the
|
|
3045 original document of ISO2022.
|
|
3046
|
|
3047 ISO2022 provides many mechanisms to encode several character sets
|
|
3048 in 7-bit and 8-bit environments. If one chooses 7-bit environment,
|
|
3049 all text is encoded by codes of less than 128. This may make the
|
|
3050 encoded text a little bit longer, but the text get more stability
|
|
3051 to pass through several gateways (some of them strip off MSB).
|
|
3052
|
|
3053 There are two kind of character sets: control character set and
|
|
3054 graphic character set. The former contains control characters such
|
|
3055 as `newline' and `escape' to provide control functions (control
|
|
3056 functions are provided also by escape sequence). The latter
|
|
3057 contains graphic characters such as 'A' and '-'. Emacs recognizes
|
|
3058 two control character sets and many graphic character sets.
|
|
3059
|
|
3060 Graphic character sets are classified into one of four types,
|
|
3061 according to the dimension and number of characters in the set:
|
|
3062 TYPE94, TYPE96, TYPE94x94, and TYPE96x96. In addition, each
|
|
3063 character set is assigned an identification byte, unique for each
|
|
3064 type, called "final character" (denoted as <F> hereafter). The <F>
|
|
3065 of each character set is decided by ECMA(*) when it is registered
|
|
3066 in ISO. Code range of <F> is 0x30..0x7F (0x30..0x3F are for
|
|
3067 private use only).
|
|
3068
|
|
3069 Note (*): ECMA = European Computer Manufacturers Association
|
|
3070
|
|
3071 Here are examples of graphic character set [NAME(<F>)]:
|
|
3072 o TYPE94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
|
|
3073 o TYPE96 -- right-half-of-ISO8859-1('A'), ...
|
|
3074 o TYPE94x94 -- GB2312('A'), JISX0208('B'), ...
|
|
3075 o TYPE96x96 -- none for the moment
|
|
3076
|
|
3077 A code area (1byte=8bits) is divided into 4 areas, C0, GL, C1, and GR.
|
|
3078 C0 [0x00..0x1F] -- control character plane 0
|
|
3079 GL [0x20..0x7F] -- graphic character plane 0
|
|
3080 C1 [0x80..0x9F] -- control character plane 1
|
|
3081 GR [0xA0..0xFF] -- graphic character plane 1
|
|
3082
|
|
3083 A control character set is directly designated and invoked to C0 or
|
|
3084 C1 by an escape sequence. The most common case is that:
|
|
3085 - ISO646's control character set is designated/invoked to C0, and
|
|
3086 - ISO6429's control character set is designated/invoked to C1,
|
|
3087 and usually these designations/invocations are omitted in encoded
|
|
3088 text. In a 7-bit environment, only C0 can be used, and a control
|
|
3089 character for C1 is encoded by an appropriate escape sequence to
|
|
3090 fit into the environment. All control characters for C1 are
|
|
3091 defined to have corresponding escape sequences.
|
|
3092
|
|
3093 A graphic character set is at first designated to one of four
|
|
3094 graphic registers (G0 through G3), then these graphic registers are
|
|
3095 invoked to GL or GR. These designations and invocations can be
|
|
3096 done independently. The most common case is that G0 is invoked to
|
|
3097 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
|
|
3098 these invocations and designations are omitted in encoded text.
|
|
3099 In a 7-bit environment, only GL can be used.
|
|
3100
|
|
3101 When a graphic character set of TYPE94 or TYPE94x94 is invoked to
|
|
3102 GL, codes 0x20 and 0x7F of the GL area work as control characters
|
|
3103 SPACE and DEL respectively, and code 0xA0 and 0xFF of GR area
|
|
3104 should not be used.
|
|
3105
|
|
3106 There are two ways of invocation: locking-shift and single-shift.
|
|
3107 With locking-shift, the invocation lasts until the next different
|
|
3108 invocation, whereas with single-shift, the invocation works only
|
|
3109 for the following character and doesn't affect locking-shift.
|
|
3110 Invocations are done by the following control characters or escape
|
|
3111 sequences.
|
|
3112
|
|
3113 ----------------------------------------------------------------------
|
|
3114 abbrev function cntrl escape seq description
|
|
3115 ----------------------------------------------------------------------
|
|
3116 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
|
|
3117 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
|
|
3118 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR
|
|
3119 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
|
|
3120 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR
|
|
3121 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
|
|
3122 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR
|
|
3123 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
|
|
3124 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
|
|
3125 ----------------------------------------------------------------------
|
|
3126 The first four are for locking-shift. Control characters for these
|
|
3127 functions are defined by macros ISO_CODE_XXX in `coding.h'.
|
|
3128
|
|
3129 Designations are done by the following escape sequences.
|
|
3130 ----------------------------------------------------------------------
|
|
3131 escape sequence description
|
|
3132 ----------------------------------------------------------------------
|
|
3133 ESC '(' <F> designate TYPE94<F> to G0
|
|
3134 ESC ')' <F> designate TYPE94<F> to G1
|
|
3135 ESC '*' <F> designate TYPE94<F> to G2
|
|
3136 ESC '+' <F> designate TYPE94<F> to G3
|
|
3137 ESC ',' <F> designate TYPE96<F> to G0 (*)
|
|
3138 ESC '-' <F> designate TYPE96<F> to G1
|
|
3139 ESC '.' <F> designate TYPE96<F> to G2
|
|
3140 ESC '/' <F> designate TYPE96<F> to G3
|
|
3141 ESC '$' '(' <F> designate TYPE94x94<F> to G0 (**)
|
|
3142 ESC '$' ')' <F> designate TYPE94x94<F> to G1
|
|
3143 ESC '$' '*' <F> designate TYPE94x94<F> to G2
|
|
3144 ESC '$' '+' <F> designate TYPE94x94<F> to G3
|
|
3145 ESC '$' ',' <F> designate TYPE96x96<F> to G0 (*)
|
|
3146 ESC '$' '-' <F> designate TYPE96x96<F> to G1
|
|
3147 ESC '$' '.' <F> designate TYPE96x96<F> to G2
|
|
3148 ESC '$' '/' <F> designate TYPE96x96<F> to G3
|
|
3149 ----------------------------------------------------------------------
|
|
3150 In this list, "TYPE94<F>" means a graphic character set of type TYPE94
|
|
3151 and final character <F>, and etc.
|
|
3152
|
|
3153 Note (*): Although these designations are not allowed in ISO2022,
|
|
3154 Emacs accepts them on decoding, and produces them on encoding
|
|
3155 TYPE96 or TYPE96x96 character set in a coding system which is
|
|
3156 characterized as 7-bit environment, non-locking-shift, and
|
|
3157 non-single-shift.
|
|
3158
|
|
3159 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
|
|
3160 '(' can be omitted. We call this as "short-form" here after.
|
|
3161
|
|
3162 Now you may notice that there are a lot of ways for encoding the
|
|
3163 same multilingual text in ISO2022. Actually, there exist many
|
|
3164 coding systems such as Compound Text (used in X's inter client
|
|
3165 communication, ISO-2022-JP (used in Japanese internet), ISO-2022-KR
|
|
3166 (used in Korean internet), EUC (Extended UNIX Code, used in Asian
|
|
3167 localized platforms), and all of these are variants of ISO2022.
|
|
3168
|
|
3169 In addition to the above, Emacs handles two more kinds of escape
|
|
3170 sequences: ISO6429's direction specification and Emacs' private
|
|
3171 sequence for specifying character composition.
|
|
3172
|
|
3173 ISO6429's direction specification takes the following format:
|
|
3174 o CSI ']' -- end of the current direction
|
|
3175 o CSI '0' ']' -- end of the current direction
|
|
3176 o CSI '1' ']' -- start of left-to-right text
|
|
3177 o CSI '2' ']' -- start of right-to-left text
|
|
3178 The control character CSI (0x9B: control sequence introducer) is
|
|
3179 abbreviated to the escape sequence ESC '[' in 7-bit environment.
|
|
3180
|
|
3181 Character composition specification takes the following format:
|
|
3182 o ESC '0' -- start character composition
|
|
3183 o ESC '1' -- end character composition
|
|
3184 Since these are not standard escape sequences of any ISO, the use
|
|
3185 of them for these meanings is restricted to Emacs only. */
|
|
3186
|
|
3187 static void
|
|
3188 reset_iso2022 (Lisp_Object coding_system, struct iso2022_decoder *iso)
|
|
3189 {
|
|
3190 int i;
|
|
3191
|
|
3192 for (i = 0; i < 4; i++)
|
|
3193 {
|
|
3194 if (!NILP (coding_system))
|
|
3195 iso->charset[i] =
|
|
3196 XCODING_SYSTEM_ISO2022_INITIAL_CHARSET (coding_system, i);
|
|
3197 else
|
|
3198 iso->charset[i] = Qt;
|
|
3199 iso->invalid_designated[i] = 0;
|
|
3200 }
|
|
3201 iso->esc = ISO_ESC_NOTHING;
|
|
3202 iso->esc_bytes_index = 0;
|
|
3203 iso->register_left = 0;
|
|
3204 iso->register_right = 1;
|
|
3205 iso->switched_dir_and_no_valid_charset_yet = 0;
|
|
3206 iso->invalid_switch_dir = 0;
|
|
3207 iso->output_direction_sequence = 0;
|
|
3208 iso->output_literally = 0;
|
337
|
3209 #ifdef ENABLE_COMPOSITE_CHARS
|
259
|
3210 if (iso->composite_chars)
|
|
3211 Dynarr_reset (iso->composite_chars);
|
337
|
3212 #endif
|
259
|
3213 }
|
|
3214
|
|
3215 static int
|
|
3216 fit_to_be_escape_quoted (unsigned char c)
|
|
3217 {
|
|
3218 switch (c)
|
|
3219 {
|
|
3220 case ISO_CODE_ESC:
|
|
3221 case ISO_CODE_CSI:
|
|
3222 case ISO_CODE_SS2:
|
|
3223 case ISO_CODE_SS3:
|
|
3224 case ISO_CODE_SO:
|
|
3225 case ISO_CODE_SI:
|
|
3226 return 1;
|
|
3227
|
|
3228 default:
|
|
3229 return 0;
|
|
3230 }
|
|
3231 }
|
|
3232
|
|
3233 /* Parse one byte of an ISO2022 escape sequence.
|
|
3234 If the result is an invalid escape sequence, return 0 and
|
|
3235 do not change anything in STR. Otherwise, if the result is
|
|
3236 an incomplete escape sequence, update ISO2022.ESC and
|
|
3237 ISO2022.ESC_BYTES and return -1. Otherwise, update
|
|
3238 all the state variables (but not ISO2022.ESC_BYTES) and
|
|
3239 return 1.
|
|
3240
|
|
3241 If CHECK_INVALID_CHARSETS is non-zero, check for designation
|
|
3242 or invocation of an invalid character set and treat that as
|
|
3243 an unrecognized escape sequence. */
|
|
3244
|
|
3245 static int
|
|
3246 parse_iso2022_esc (Lisp_Object codesys, struct iso2022_decoder *iso,
|
|
3247 unsigned char c, unsigned int *flags,
|
|
3248 int check_invalid_charsets)
|
|
3249 {
|
|
3250 /* (1) If we're at the end of a designation sequence, CS is the
|
|
3251 charset being designated and REG is the register to designate
|
|
3252 it to.
|
|
3253
|
|
3254 (2) If we're at the end of a locking-shift sequence, REG is
|
|
3255 the register to invoke and HALF (0 == left, 1 == right) is
|
|
3256 the half to invoke it into.
|
|
3257
|
|
3258 (3) If we're at the end of a single-shift sequence, REG is
|
|
3259 the register to invoke. */
|
|
3260 Lisp_Object cs = Qnil;
|
|
3261 int reg, half;
|
|
3262
|
|
3263 /* NOTE: This code does goto's all over the fucking place.
|
|
3264 The reason for this is that we're basically implementing
|
|
3265 a state machine here, and hierarchical languages like C
|
|
3266 don't really provide a clean way of doing this. */
|
|
3267
|
|
3268 if (! (*flags & CODING_STATE_ESCAPE))
|
|
3269 /* At beginning of escape sequence; we need to reset our
|
|
3270 escape-state variables. */
|
|
3271 iso->esc = ISO_ESC_NOTHING;
|
|
3272
|
|
3273 iso->output_literally = 0;
|
|
3274 iso->output_direction_sequence = 0;
|
|
3275
|
|
3276 switch (iso->esc)
|
|
3277 {
|
|
3278 case ISO_ESC_NOTHING:
|
|
3279 iso->esc_bytes_index = 0;
|
|
3280 switch (c)
|
|
3281 {
|
|
3282 case ISO_CODE_ESC: /* Start escape sequence */
|
|
3283 *flags |= CODING_STATE_ESCAPE;
|
|
3284 iso->esc = ISO_ESC;
|
|
3285 goto not_done;
|
|
3286
|
|
3287 case ISO_CODE_CSI: /* ISO6429 (specifying directionality) */
|
|
3288 *flags |= CODING_STATE_ESCAPE;
|
|
3289 iso->esc = ISO_ESC_5_11;
|
|
3290 goto not_done;
|
|
3291
|
|
3292 case ISO_CODE_SO: /* locking shift 1 */
|
|
3293 reg = 1; half = 0;
|
|
3294 goto locking_shift;
|
|
3295 case ISO_CODE_SI: /* locking shift 0 */
|
|
3296 reg = 0; half = 0;
|
|
3297 goto locking_shift;
|
|
3298
|
|
3299 case ISO_CODE_SS2: /* single shift */
|
|
3300 reg = 2;
|
|
3301 goto single_shift;
|
|
3302 case ISO_CODE_SS3: /* single shift */
|
|
3303 reg = 3;
|
|
3304 goto single_shift;
|
|
3305
|
|
3306 default: /* Other control characters */
|
|
3307 return 0;
|
|
3308 }
|
|
3309
|
|
3310 case ISO_ESC:
|
|
3311 switch (c)
|
|
3312 {
|
|
3313 /**** single shift ****/
|
|
3314
|
|
3315 case 'N': /* single shift 2 */
|
|
3316 reg = 2;
|
|
3317 goto single_shift;
|
|
3318 case 'O': /* single shift 3 */
|
|
3319 reg = 3;
|
|
3320 goto single_shift;
|
|
3321
|
|
3322 /**** locking shift ****/
|
|
3323
|
|
3324 case '~': /* locking shift 1 right */
|
|
3325 reg = 1; half = 1;
|
|
3326 goto locking_shift;
|
|
3327 case 'n': /* locking shift 2 */
|
|
3328 reg = 2; half = 0;
|
|
3329 goto locking_shift;
|
|
3330 case '}': /* locking shift 2 right */
|
|
3331 reg = 2; half = 1;
|
|
3332 goto locking_shift;
|
|
3333 case 'o': /* locking shift 3 */
|
|
3334 reg = 3; half = 0;
|
|
3335 goto locking_shift;
|
|
3336 case '|': /* locking shift 3 right */
|
|
3337 reg = 3; half = 1;
|
|
3338 goto locking_shift;
|
|
3339
|
|
3340 /**** composite ****/
|
|
3341
|
337
|
3342 #ifdef ENABLE_COMPOSITE_CHARS
|
259
|
3343 case '0':
|
|
3344 iso->esc = ISO_ESC_START_COMPOSITE;
|
|
3345 *flags = (*flags & CODING_STATE_ISO2022_LOCK) |
|
|
3346 CODING_STATE_COMPOSITE;
|
|
3347 return 1;
|
|
3348
|
|
3349 case '1':
|
|
3350 iso->esc = ISO_ESC_END_COMPOSITE;
|
|
3351 *flags = (*flags & CODING_STATE_ISO2022_LOCK) &
|
|
3352 ~CODING_STATE_COMPOSITE;
|
|
3353 return 1;
|
337
|
3354 #endif /* ENABLE_COMPOSITE_CHARS */
|
259
|
3355
|
|
3356 /**** directionality ****/
|
|
3357
|
|
3358 case '[':
|
|
3359 iso->esc = ISO_ESC_5_11;
|
|
3360 goto not_done;
|
|
3361
|
|
3362 /**** designation ****/
|
|
3363
|
|
3364 case '$': /* multibyte charset prefix */
|
|
3365 iso->esc = ISO_ESC_2_4;
|
|
3366 goto not_done;
|
|
3367
|
|
3368 default:
|
|
3369 if (0x28 <= c && c <= 0x2F)
|
|
3370 {
|
|
3371 iso->esc = (enum iso_esc_flag) (c - 0x28 + ISO_ESC_2_8);
|
|
3372 goto not_done;
|
|
3373 }
|
|
3374
|
|
3375 /* This function is called with CODESYS equal to nil when
|
|
3376 doing coding-system detection. */
|
|
3377 if (!NILP (codesys)
|
|
3378 && XCODING_SYSTEM_ISO2022_ESCAPE_QUOTED (codesys)
|
|
3379 && fit_to_be_escape_quoted (c))
|
|
3380 {
|
|
3381 iso->esc = ISO_ESC_LITERAL;
|
|
3382 *flags &= CODING_STATE_ISO2022_LOCK;
|
|
3383 return 1;
|
|
3384 }
|
|
3385
|
|
3386 /* bzzzt! */
|
|
3387 return 0;
|
|
3388 }
|
|
3389
|
|
3390
|
|
3391
|
|
3392 /**** directionality ****/
|
|
3393
|
|
3394 case ISO_ESC_5_11: /* ISO6429 direction control */
|
|
3395 if (c == ']')
|
|
3396 {
|
|
3397 *flags &= (CODING_STATE_ISO2022_LOCK & ~CODING_STATE_R2L);
|
|
3398 goto directionality;
|
|
3399 }
|
|
3400 if (c == '0') iso->esc = ISO_ESC_5_11_0;
|
|
3401 else if (c == '1') iso->esc = ISO_ESC_5_11_1;
|
|
3402 else if (c == '2') iso->esc = ISO_ESC_5_11_2;
|
|
3403 else return 0;
|
|
3404 goto not_done;
|
|
3405
|
|
3406 case ISO_ESC_5_11_0:
|
|
3407 if (c == ']')
|
|
3408 {
|
|
3409 *flags &= (CODING_STATE_ISO2022_LOCK & ~CODING_STATE_R2L);
|
|
3410 goto directionality;
|
|
3411 }
|
|
3412 return 0;
|
|
3413
|
|
3414 case ISO_ESC_5_11_1:
|
|
3415 if (c == ']')
|
|
3416 {
|
|
3417 *flags = (CODING_STATE_ISO2022_LOCK & ~CODING_STATE_R2L);
|
|
3418 goto directionality;
|
|
3419 }
|
|
3420 return 0;
|
|
3421
|
|
3422 case ISO_ESC_5_11_2:
|
|
3423 if (c == ']')
|
|
3424 {
|
|
3425 *flags = (*flags & CODING_STATE_ISO2022_LOCK) | CODING_STATE_R2L;
|
|
3426 goto directionality;
|
|
3427 }
|
|
3428 return 0;
|
|
3429
|
|
3430 directionality:
|
|
3431 iso->esc = ISO_ESC_DIRECTIONALITY;
|
|
3432 /* Various junk here to attempt to preserve the direction sequences
|
|
3433 literally in the text if they would otherwise be swallowed due
|
|
3434 to invalid designations that don't show up as actual charset
|
|
3435 changes in the text. */
|
|
3436 if (iso->invalid_switch_dir)
|
|
3437 {
|
|
3438 /* We already inserted a direction switch literally into the
|
|
3439 text. We assume (#### this may not be right) that the
|
|
3440 next direction switch is the one going the other way,
|
|
3441 and we need to output that literally as well. */
|
|
3442 iso->output_literally = 1;
|
|
3443 iso->invalid_switch_dir = 0;
|
|
3444 }
|
|
3445 else
|
|
3446 {
|
|
3447 int jj;
|
|
3448
|
|
3449 /* If we are in the thrall of an invalid designation,
|
|
3450 then stick the directionality sequence literally into the
|
|
3451 output stream so it ends up in the original text again. */
|
|
3452 for (jj = 0; jj < 4; jj++)
|
|
3453 if (iso->invalid_designated[jj])
|
|
3454 break;
|
|
3455 if (jj < 4)
|
|
3456 {
|
|
3457 iso->output_literally = 1;
|
|
3458 iso->invalid_switch_dir = 1;
|
|
3459 }
|
|
3460 else
|
|
3461 /* Indicate that we haven't yet seen a valid designation,
|
|
3462 so that if a switch-dir is directly followed by an
|
|
3463 invalid designation, both get inserted literally. */
|
|
3464 iso->switched_dir_and_no_valid_charset_yet = 1;
|
|
3465 }
|
|
3466 return 1;
|
|
3467
|
|
3468
|
|
3469 /**** designation ****/
|
|
3470
|
|
3471 case ISO_ESC_2_4:
|
|
3472 if (0x28 <= c && c <= 0x2F)
|
|
3473 {
|
|
3474 iso->esc = (enum iso_esc_flag) (c - 0x28 + ISO_ESC_2_4_8);
|
|
3475 goto not_done;
|
|
3476 }
|
|
3477 if (0x40 <= c && c <= 0x42)
|
|
3478 {
|
|
3479 cs = CHARSET_BY_ATTRIBUTES (CHARSET_TYPE_94X94, c,
|
|
3480 *flags & CODING_STATE_R2L ?
|
|
3481 CHARSET_RIGHT_TO_LEFT :
|
|
3482 CHARSET_LEFT_TO_RIGHT);
|
|
3483 reg = 0;
|
|
3484 goto designated;
|
|
3485 }
|
|
3486 return 0;
|
|
3487
|
|
3488 default:
|
|
3489 {
|
|
3490 int type =-1;
|
|
3491
|
|
3492 if (c < '0' || c > '~')
|
|
3493 return 0; /* bad final byte */
|
|
3494
|
|
3495 if (iso->esc >= ISO_ESC_2_8 &&
|
|
3496 iso->esc <= ISO_ESC_2_15)
|
|
3497 {
|
|
3498 type = ((iso->esc >= ISO_ESC_2_12) ?
|
|
3499 CHARSET_TYPE_96 : CHARSET_TYPE_94);
|
|
3500 reg = (iso->esc - ISO_ESC_2_8) & 3;
|
|
3501 }
|
|
3502 else if (iso->esc >= ISO_ESC_2_4_8 &&
|
|
3503 iso->esc <= ISO_ESC_2_4_15)
|
|
3504 {
|
|
3505 type = ((iso->esc >= ISO_ESC_2_4_12) ?
|
|
3506 CHARSET_TYPE_96X96 : CHARSET_TYPE_94X94);
|
|
3507 reg = (iso->esc - ISO_ESC_2_4_8) & 3;
|
|
3508 }
|
|
3509 else
|
|
3510 {
|
|
3511 /* Can this ever be reached? -slb */
|
|
3512 abort();
|
|
3513 }
|
|
3514
|
|
3515 cs = CHARSET_BY_ATTRIBUTES (type, c,
|
|
3516 *flags & CODING_STATE_R2L ?
|
|
3517 CHARSET_RIGHT_TO_LEFT :
|
|
3518 CHARSET_LEFT_TO_RIGHT);
|
|
3519 goto designated;
|
|
3520 }
|
|
3521 }
|
|
3522
|
|
3523 not_done:
|
|
3524 iso->esc_bytes[iso->esc_bytes_index++] = (unsigned char) c;
|
|
3525 return -1;
|
|
3526
|
|
3527 single_shift:
|
|
3528 if (check_invalid_charsets && !CHARSETP (iso->charset[reg]))
|
|
3529 /* can't invoke something that ain't there. */
|
|
3530 return 0;
|
|
3531 iso->esc = ISO_ESC_SINGLE_SHIFT;
|
|
3532 *flags &= CODING_STATE_ISO2022_LOCK;
|
|
3533 if (reg == 2)
|
|
3534 *flags |= CODING_STATE_SS2;
|
|
3535 else
|
|
3536 *flags |= CODING_STATE_SS3;
|
|
3537 return 1;
|
|
3538
|
|
3539 locking_shift:
|
|
3540 if (check_invalid_charsets &&
|
|
3541 !CHARSETP (iso->charset[reg]))
|
|
3542 /* can't invoke something that ain't there. */
|
|
3543 return 0;
|
|
3544 if (half)
|
|
3545 iso->register_right = reg;
|
|
3546 else
|
|
3547 iso->register_left = reg;
|
|
3548 *flags &= CODING_STATE_ISO2022_LOCK;
|
|
3549 iso->esc = ISO_ESC_LOCKING_SHIFT;
|
|
3550 return 1;
|
|
3551
|
|
3552 designated:
|
|
3553 if (NILP (cs) && check_invalid_charsets)
|
|
3554 {
|
|
3555 iso->invalid_designated[reg] = 1;
|
|
3556 iso->charset[reg] = Vcharset_ascii;
|
|
3557 iso->esc = ISO_ESC_DESIGNATE;
|
|
3558 *flags &= CODING_STATE_ISO2022_LOCK;
|
|
3559 iso->output_literally = 1;
|
|
3560 if (iso->switched_dir_and_no_valid_charset_yet)
|
|
3561 {
|
|
3562 /* We encountered a switch-direction followed by an
|
|
3563 invalid designation. Ensure that the switch-direction
|
|
3564 gets outputted; otherwise it will probably get eaten
|
|
3565 when the text is written out again. */
|
|
3566 iso->switched_dir_and_no_valid_charset_yet = 0;
|
|
3567 iso->output_direction_sequence = 1;
|
|
3568 /* And make sure that the switch-dir going the other
|
|
3569 way gets outputted, as well. */
|
|
3570 iso->invalid_switch_dir = 1;
|
|
3571 }
|
|
3572 return 1;
|
|
3573 }
|
|
3574 /* This function is called with CODESYS equal to nil when
|
|
3575 doing coding-system detection. */
|
|
3576 if (!NILP (codesys))
|
|
3577 {
|
|
3578 charset_conversion_spec_dynarr *dyn =
|
|
3579 XCODING_SYSTEM (codesys)->iso2022.input_conv;
|
|
3580
|
|
3581 if (dyn)
|
|
3582 {
|
|
3583 int i;
|
|
3584
|
|
3585 for (i = 0; i < Dynarr_length (dyn); i++)
|
|
3586 {
|
|
3587 struct charset_conversion_spec *spec = Dynarr_atp (dyn, i);
|
|
3588 if (EQ (cs, spec->from_charset))
|
|
3589 cs = spec->to_charset;
|
|
3590 }
|
|
3591 }
|
|
3592 }
|
|
3593
|
|
3594 iso->charset[reg] = cs;
|
|
3595 iso->esc = ISO_ESC_DESIGNATE;
|
|
3596 *flags &= CODING_STATE_ISO2022_LOCK;
|
|
3597 if (iso->invalid_designated[reg])
|
|
3598 {
|
|
3599 iso->invalid_designated[reg] = 0;
|
|
3600 iso->output_literally = 1;
|
|
3601 }
|
|
3602 if (iso->switched_dir_and_no_valid_charset_yet)
|
|
3603 iso->switched_dir_and_no_valid_charset_yet = 0;
|
|
3604 return 1;
|
|
3605 }
|
|
3606
|
|
3607 static int
|
|
3608 detect_coding_iso2022 (struct detection_state *st, CONST unsigned char *src,
|
|
3609 unsigned int n)
|
|
3610 {
|
|
3611 int c;
|
|
3612 int mask;
|
|
3613
|
|
3614 /* #### There are serious deficiencies in the recognition mechanism
|
|
3615 here. This needs to be much smarter if it's going to cut it. */
|
|
3616
|
|
3617 if (!st->iso2022.initted)
|
|
3618 {
|
|
3619 reset_iso2022 (Qnil, &st->iso2022.iso);
|
|
3620 st->iso2022.mask = (CODING_CATEGORY_ISO_7_MASK |
|
|
3621 CODING_CATEGORY_ISO_8_DESIGNATE_MASK |
|
|
3622 CODING_CATEGORY_ISO_8_1_MASK |
|
|
3623 CODING_CATEGORY_ISO_8_2_MASK |
|
|
3624 CODING_CATEGORY_ISO_LOCK_SHIFT_MASK);
|
|
3625 st->iso2022.flags = 0;
|
|
3626 st->iso2022.high_byte_count = 0;
|
|
3627 st->iso2022.saw_single_shift = 0;
|
|
3628 st->iso2022.initted = 1;
|
|
3629 }
|
|
3630
|
|
3631 mask = st->iso2022.mask;
|
|
3632
|
|
3633 while (n--)
|
|
3634 {
|
|
3635 c = *src++;
|
|
3636 if (c >= 0xA0)
|
|
3637 {
|
|
3638 mask &= ~CODING_CATEGORY_ISO_7_MASK;
|
|
3639 st->iso2022.high_byte_count++;
|
|
3640 }
|
|
3641 else
|
|
3642 {
|
|
3643 if (st->iso2022.high_byte_count && !st->iso2022.saw_single_shift)
|
|
3644 {
|
|
3645 if (st->iso2022.high_byte_count & 1)
|
|
3646 /* odd number of high bytes; assume not iso-8-2 */
|
|
3647 mask &= ~CODING_CATEGORY_ISO_8_2_MASK;
|
|
3648 }
|
|
3649 st->iso2022.high_byte_count = 0;
|
|
3650 st->iso2022.saw_single_shift = 0;
|
|
3651 if (c > 0x80)
|
|
3652 mask &= ~CODING_CATEGORY_ISO_7_MASK;
|
|
3653 }
|
|
3654 if (!(st->iso2022.flags & CODING_STATE_ESCAPE)
|
|
3655 && (BYTE_C0_P (c) || BYTE_C1_P (c)))
|
|
3656 { /* control chars */
|
|
3657 switch (c)
|
|
3658 {
|
|
3659 /* Allow and ignore control characters that you might
|
|
3660 reasonably see in a text file */
|
|
3661 case '\r':
|
|
3662 case '\n':
|
|
3663 case '\t':
|
|
3664 case 7: /* bell */
|
|
3665 case 8: /* backspace */
|
|
3666 case 11: /* vertical tab */
|
|
3667 case 12: /* form feed */
|
|
3668 case 26: /* MS-DOS C-z junk */
|
|
3669 case 31: /* '^_' -- for info */
|
|
3670 goto label_continue_loop;
|
|
3671
|
|
3672 default:
|
|
3673 break;
|
|
3674 }
|
|
3675 }
|
|
3676
|
|
3677 if ((st->iso2022.flags & CODING_STATE_ESCAPE) || BYTE_C0_P (c)
|
|
3678 || BYTE_C1_P (c))
|
|
3679 {
|
|
3680 if (parse_iso2022_esc (Qnil, &st->iso2022.iso, c,
|
|
3681 &st->iso2022.flags, 0))
|
|
3682 {
|
|
3683 switch (st->iso2022.iso.esc)
|
|
3684 {
|
|
3685 case ISO_ESC_DESIGNATE:
|
|
3686 mask &= ~CODING_CATEGORY_ISO_8_1_MASK;
|
|
3687 mask &= ~CODING_CATEGORY_ISO_8_2_MASK;
|
|
3688 break;
|
|
3689 case ISO_ESC_LOCKING_SHIFT:
|
|
3690 mask = CODING_CATEGORY_ISO_LOCK_SHIFT_MASK;
|
|
3691 goto ran_out_of_chars;
|
|
3692 case ISO_ESC_SINGLE_SHIFT:
|
|
3693 mask &= ~CODING_CATEGORY_ISO_8_DESIGNATE_MASK;
|
|
3694 st->iso2022.saw_single_shift = 1;
|
|
3695 break;
|
|
3696 default:
|
|
3697 break;
|
|
3698 }
|
|
3699 }
|
|
3700 else
|
|
3701 {
|
|
3702 mask = 0;
|
|
3703 goto ran_out_of_chars;
|
|
3704 }
|
|
3705 }
|
|
3706 label_continue_loop:;
|
|
3707 }
|
|
3708
|
|
3709 ran_out_of_chars:
|
|
3710
|
|
3711 return mask;
|
|
3712 }
|
|
3713
|
|
3714 static int
|
|
3715 postprocess_iso2022_mask (int mask)
|
|
3716 {
|
|
3717 /* #### kind of cheesy */
|
|
3718 /* If seven-bit ISO is allowed, then assume that the encoding is
|
|
3719 entirely seven-bit and turn off the eight-bit ones. */
|
|
3720 if (mask & CODING_CATEGORY_ISO_7_MASK)
|
|
3721 mask &= ~ (CODING_CATEGORY_ISO_8_DESIGNATE_MASK |
|
|
3722 CODING_CATEGORY_ISO_8_1_MASK |
|
|
3723 CODING_CATEGORY_ISO_8_2_MASK);
|
|
3724 return mask;
|
|
3725 }
|
|
3726
|
|
3727 /* If FLAGS is a null pointer or specifies right-to-left motion,
|
|
3728 output a switch-dir-to-left-to-right sequence to DST.
|
|
3729 Also update FLAGS if it is not a null pointer.
|
|
3730 If INTERNAL_P is set, we are outputting in internal format and
|
|
3731 need to handle the CSI differently. */
|
|
3732
|
|
3733 static void
|
|
3734 restore_left_to_right_direction (struct Lisp_Coding_System *codesys,
|
|
3735 unsigned_char_dynarr *dst,
|
|
3736 unsigned int *flags,
|
|
3737 int internal_p)
|
|
3738 {
|
|
3739 if (!flags || (*flags & CODING_STATE_R2L))
|
|
3740 {
|
|
3741 if (CODING_SYSTEM_ISO2022_SEVEN (codesys))
|
|
3742 {
|
|
3743 Dynarr_add (dst, ISO_CODE_ESC);
|
|
3744 Dynarr_add (dst, '[');
|
|
3745 }
|
|
3746 else if (internal_p)
|
|
3747 DECODE_ADD_BINARY_CHAR (ISO_CODE_CSI, dst);
|
|
3748 else
|
|
3749 Dynarr_add (dst, ISO_CODE_CSI);
|
|
3750 Dynarr_add (dst, '0');
|
|
3751 Dynarr_add (dst, ']');
|
|
3752 if (flags)
|
|
3753 *flags &= ~CODING_STATE_R2L;
|
|
3754 }
|
|
3755 }
|
|
3756
|
|
3757 /* If FLAGS is a null pointer or specifies a direction different from
|
|
3758 DIRECTION (which should be either CHARSET_RIGHT_TO_LEFT or
|
|
3759 CHARSET_LEFT_TO_RIGHT), output the appropriate switch-dir escape
|
|
3760 sequence to DST. Also update FLAGS if it is not a null pointer.
|
|
3761 If INTERNAL_P is set, we are outputting in internal format and
|
|
3762 need to handle the CSI differently. */
|
|
3763
|
|
3764 static void
|
|
3765 ensure_correct_direction (int direction, struct Lisp_Coding_System *codesys,
|
|
3766 unsigned_char_dynarr *dst, unsigned int *flags,
|
|
3767 int internal_p)
|
|
3768 {
|
|
3769 if ((!flags || (*flags & CODING_STATE_R2L)) &&
|
|
3770 direction == CHARSET_LEFT_TO_RIGHT)
|
|
3771 restore_left_to_right_direction (codesys, dst, flags, internal_p);
|
|
3772 else if (!CODING_SYSTEM_ISO2022_NO_ISO6429 (codesys)
|
|
3773 && (!flags || !(*flags & CODING_STATE_R2L)) &&
|
|
3774 direction == CHARSET_RIGHT_TO_LEFT)
|
|
3775 {
|
|
3776 if (CODING_SYSTEM_ISO2022_SEVEN (codesys))
|
|
3777 {
|
|
3778 Dynarr_add (dst, ISO_CODE_ESC);
|
|
3779 Dynarr_add (dst, '[');
|
|
3780 }
|
|
3781 else if (internal_p)
|
|
3782 DECODE_ADD_BINARY_CHAR (ISO_CODE_CSI, dst);
|
|
3783 else
|
|
3784 Dynarr_add (dst, ISO_CODE_CSI);
|
|
3785 Dynarr_add (dst, '2');
|
|
3786 Dynarr_add (dst, ']');
|
|
3787 if (flags)
|
|
3788 *flags |= CODING_STATE_R2L;
|
|
3789 }
|
|
3790 }
|
|
3791
|
|
3792 /* Convert ISO2022-format data to internal format. */
|
|
3793
|
|
3794 static void
|
|
3795 decode_coding_iso2022 (Lstream *decoding, CONST unsigned char *src,
|
|
3796 unsigned_char_dynarr *dst, unsigned int n)
|
|
3797 {
|
|
3798 unsigned char c;
|
|
3799 unsigned int flags, ch;
|
|
3800 enum eol_type eol_type;
|
|
3801 struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
|
272
|
3802 Lisp_Object coding_system;
|
337
|
3803 #ifdef ENABLE_COMPOSITE_CHARS
|
259
|
3804 unsigned_char_dynarr *real_dst = dst;
|
337
|
3805 #endif
|
259
|
3806
|
|
3807 CODING_STREAM_DECOMPOSE (str, flags, ch);
|
|
3808 eol_type = str->eol_type;
|
|
3809 XSETCODING_SYSTEM (coding_system, str->codesys);
|
|
3810
|
337
|
3811 #ifdef ENABLE_COMPOSITE_CHARS
|
259
|
3812 if (flags & CODING_STATE_COMPOSITE)
|
|
3813 dst = str->iso2022.composite_chars;
|
337
|
3814 #endif /* ENABLE_COMPOSITE_CHARS */
|
259
|
3815
|
|
3816 while (n--)
|
|
3817 {
|
|
3818 c = *src++;
|
|
3819 if (flags & CODING_STATE_ESCAPE)
|
|
3820 { /* Within ESC sequence */
|
|
3821 int retval = parse_iso2022_esc (coding_system, &str->iso2022,
|
|
3822 c, &flags, 1);
|
|
3823
|
|
3824 if (retval)
|
|
3825 {
|
|
3826 switch (str->iso2022.esc)
|
|
3827 {
|
337
|
3828 #ifdef ENABLE_COMPOSITE_CHARS
|
259
|
3829 case ISO_ESC_START_COMPOSITE:
|
|
3830 if (str->iso2022.composite_chars)
|
|
3831 Dynarr_reset (str->iso2022.composite_chars);
|
|
3832 else
|
|
3833 str->iso2022.composite_chars = Dynarr_new (unsigned_char);
|
|
3834 dst = str->iso2022.composite_chars;
|
|
3835 break;
|
|
3836 case ISO_ESC_END_COMPOSITE:
|
|
3837 {
|
|
3838 Bufbyte comstr[MAX_EMCHAR_LEN];
|
|
3839 Bytecount len;
|
|
3840 Emchar emch = lookup_composite_char (Dynarr_atp (dst, 0),
|
|
3841 Dynarr_length (dst));
|
|
3842 dst = real_dst;
|
|
3843 len = set_charptr_emchar (comstr, emch);
|
|
3844 Dynarr_add_many (dst, comstr, len);
|
|
3845 break;
|
|
3846 }
|
337
|
3847 #endif /* ENABLE_COMPOSITE_CHARS */
|
259
|
3848
|
|
3849 case ISO_ESC_LITERAL:
|
|
3850 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
3851 break;
|
|
3852
|
|
3853 default:
|
|
3854 /* Everything else handled already */
|
|
3855 break;
|
|
3856 }
|
|
3857 }
|
|
3858
|
|
3859 /* Attempted error recovery. */
|
|
3860 if (str->iso2022.output_direction_sequence)
|
|
3861 ensure_correct_direction (flags & CODING_STATE_R2L ?
|
|
3862 CHARSET_RIGHT_TO_LEFT :
|
|
3863 CHARSET_LEFT_TO_RIGHT,
|
|
3864 str->codesys, dst, 0, 1);
|
|
3865 /* More error recovery. */
|
|
3866 if (!retval || str->iso2022.output_literally)
|
|
3867 {
|
|
3868 /* Output the (possibly invalid) sequence */
|
|
3869 int i;
|
|
3870 for (i = 0; i < str->iso2022.esc_bytes_index; i++)
|
|
3871 DECODE_ADD_BINARY_CHAR (str->iso2022.esc_bytes[i], dst);
|
|
3872 flags &= CODING_STATE_ISO2022_LOCK;
|
|
3873 if (!retval)
|
|
3874 n++, src--;/* Repeat the loop with the same character. */
|
|
3875 else
|
|
3876 {
|
|
3877 /* No sense in reprocessing the final byte of the
|
|
3878 escape sequence; it could mess things up anyway.
|
|
3879 Just add it now. */
|
|
3880 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
3881 }
|
|
3882 }
|
|
3883 ch = 0;
|
|
3884 }
|
|
3885 else if (BYTE_C0_P (c) || BYTE_C1_P (c))
|
|
3886 { /* Control characters */
|
|
3887
|
|
3888 /***** Error-handling *****/
|
|
3889
|
|
3890 /* If we were in the middle of a character, dump out the
|
|
3891 partial character. */
|
|
3892 DECODE_OUTPUT_PARTIAL_CHAR (ch);
|
|
3893
|
|
3894 /* If we just saw a single-shift character, dump it out.
|
|
3895 This may dump out the wrong sort of single-shift character,
|
|
3896 but least it will give an indication that something went
|
|
3897 wrong. */
|
|
3898 if (flags & CODING_STATE_SS2)
|
|
3899 {
|
|
3900 DECODE_ADD_BINARY_CHAR (ISO_CODE_SS2, dst);
|
|
3901 flags &= ~CODING_STATE_SS2;
|
|
3902 }
|
|
3903 if (flags & CODING_STATE_SS3)
|
|
3904 {
|
|
3905 DECODE_ADD_BINARY_CHAR (ISO_CODE_SS3, dst);
|
|
3906 flags &= ~CODING_STATE_SS3;
|
|
3907 }
|
|
3908
|
|
3909 /***** Now handle the control characters. *****/
|
|
3910
|
|
3911 /* Handle CR/LF */
|
|
3912 DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
|
|
3913
|
|
3914 flags &= CODING_STATE_ISO2022_LOCK;
|
|
3915
|
|
3916 if (!parse_iso2022_esc (coding_system, &str->iso2022, c, &flags, 1))
|
|
3917 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
3918 }
|
|
3919 else
|
|
3920 { /* Graphic characters */
|
|
3921 Lisp_Object charset;
|
|
3922 int lb;
|
|
3923 int reg;
|
|
3924
|
|
3925 DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
|
|
3926
|
|
3927 /* Now determine the charset. */
|
|
3928 reg = ((flags & CODING_STATE_SS2) ? 2
|
|
3929 : (flags & CODING_STATE_SS3) ? 3
|
|
3930 : !BYTE_ASCII_P (c) ? str->iso2022.register_right
|
|
3931 : str->iso2022.register_left);
|
|
3932 charset = str->iso2022.charset[reg];
|
|
3933
|
|
3934 /* Error checking: */
|
367
|
3935 if (! CHARSETP (charset)
|
|
3936 || str->iso2022.invalid_designated[reg]
|
259
|
3937 || (((c & 0x7F) == ' ' || (c & 0x7F) == ISO_CODE_DEL)
|
|
3938 && XCHARSET_CHARS (charset) == 94))
|
|
3939 /* Mrmph. We are trying to invoke a register that has no
|
|
3940 or an invalid charset in it, or trying to add a character
|
|
3941 outside the range of the charset. Insert that char literally
|
|
3942 to preserve it for the output. */
|
|
3943 {
|
|
3944 DECODE_OUTPUT_PARTIAL_CHAR (ch);
|
|
3945 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
3946 }
|
|
3947
|
|
3948 else
|
|
3949 {
|
|
3950 /* Things are probably hunky-dorey. */
|
|
3951
|
|
3952 /* Fetch reverse charset, maybe. */
|
|
3953 if (((flags & CODING_STATE_R2L) &&
|
|
3954 XCHARSET_DIRECTION (charset) == CHARSET_LEFT_TO_RIGHT)
|
|
3955 ||
|
|
3956 (!(flags & CODING_STATE_R2L) &&
|
|
3957 XCHARSET_DIRECTION (charset) == CHARSET_RIGHT_TO_LEFT))
|
|
3958 {
|
|
3959 Lisp_Object new_charset =
|
|
3960 XCHARSET_REVERSE_DIRECTION_CHARSET (charset);
|
|
3961 if (!NILP (new_charset))
|
|
3962 charset = new_charset;
|
|
3963 }
|
|
3964
|
|
3965 lb = XCHARSET_LEADING_BYTE (charset);
|
|
3966 switch (XCHARSET_REP_BYTES (charset))
|
|
3967 {
|
|
3968 case 1: /* ASCII */
|
|
3969 DECODE_OUTPUT_PARTIAL_CHAR (ch);
|
|
3970 Dynarr_add (dst, c & 0x7F);
|
|
3971 break;
|
|
3972
|
|
3973 case 2: /* one-byte official */
|
|
3974 DECODE_OUTPUT_PARTIAL_CHAR (ch);
|
|
3975 Dynarr_add (dst, lb);
|
|
3976 Dynarr_add (dst, c | 0x80);
|
|
3977 break;
|
|
3978
|
|
3979 case 3: /* one-byte private or two-byte official */
|
|
3980 if (XCHARSET_PRIVATE_P (charset))
|
|
3981 {
|
|
3982 DECODE_OUTPUT_PARTIAL_CHAR (ch);
|
|
3983 Dynarr_add (dst, PRE_LEADING_BYTE_PRIVATE_1);
|
|
3984 Dynarr_add (dst, lb);
|
|
3985 Dynarr_add (dst, c | 0x80);
|
|
3986 }
|
|
3987 else
|
|
3988 {
|
|
3989 if (ch)
|
|
3990 {
|
|
3991 Dynarr_add (dst, lb);
|
|
3992 Dynarr_add (dst, ch | 0x80);
|
|
3993 Dynarr_add (dst, c | 0x80);
|
|
3994 ch = 0;
|
|
3995 }
|
|
3996 else
|
|
3997 ch = c;
|
|
3998 }
|
|
3999 break;
|
|
4000
|
|
4001 default: /* two-byte private */
|
|
4002 if (ch)
|
|
4003 {
|
|
4004 Dynarr_add (dst, PRE_LEADING_BYTE_PRIVATE_2);
|
|
4005 Dynarr_add (dst, lb);
|
|
4006 Dynarr_add (dst, ch | 0x80);
|
|
4007 Dynarr_add (dst, c | 0x80);
|
|
4008 ch = 0;
|
|
4009 }
|
|
4010 else
|
|
4011 ch = c;
|
|
4012 }
|
|
4013 }
|
|
4014
|
|
4015 if (!ch)
|
|
4016 flags &= CODING_STATE_ISO2022_LOCK;
|
|
4017 }
|
|
4018
|
|
4019 label_continue_loop:;
|
|
4020 }
|
|
4021
|
|
4022 if (flags & CODING_STATE_END)
|
|
4023 DECODE_OUTPUT_PARTIAL_CHAR (ch);
|
|
4024
|
|
4025 CODING_STREAM_COMPOSE (str, flags, ch);
|
|
4026 }
|
|
4027
|
|
4028
|
|
4029 /***** ISO2022 encoder *****/
|
|
4030
|
|
4031 /* Designate CHARSET into register REG. */
|
|
4032
|
|
4033 static void
|
|
4034 iso2022_designate (Lisp_Object charset, unsigned char reg,
|
|
4035 struct encoding_stream *str, unsigned_char_dynarr *dst)
|
|
4036 {
|
|
4037 CONST char *inter94 = "()*+", *inter96= ",-./";
|
272
|
4038 unsigned int type;
|
259
|
4039 unsigned char final;
|
|
4040 Lisp_Object old_charset = str->iso2022.charset[reg];
|
|
4041
|
|
4042 str->iso2022.charset[reg] = charset;
|
|
4043 if (!CHARSETP (charset))
|
|
4044 /* charset might be an initial nil or t. */
|
|
4045 return;
|
|
4046 type = XCHARSET_TYPE (charset);
|
|
4047 final = XCHARSET_FINAL (charset);
|
|
4048 if (!str->iso2022.force_charset_on_output[reg] &&
|
|
4049 CHARSETP (old_charset) &&
|
|
4050 XCHARSET_TYPE (old_charset) == type &&
|
|
4051 XCHARSET_FINAL (old_charset) == final)
|
|
4052 return;
|
|
4053
|
|
4054 str->iso2022.force_charset_on_output[reg] = 0;
|
|
4055
|
|
4056 {
|
|
4057 charset_conversion_spec_dynarr *dyn =
|
|
4058 str->codesys->iso2022.output_conv;
|
|
4059
|
|
4060 if (dyn)
|
|
4061 {
|
|
4062 int i;
|
|
4063
|
|
4064 for (i = 0; i < Dynarr_length (dyn); i++)
|
|
4065 {
|
|
4066 struct charset_conversion_spec *spec = Dynarr_atp (dyn, i);
|
|
4067 if (EQ (charset, spec->from_charset))
|
|
4068 charset = spec->to_charset;
|
|
4069 }
|
|
4070 }
|
|
4071 }
|
|
4072
|
|
4073 Dynarr_add (dst, ISO_CODE_ESC);
|
|
4074 switch (type)
|
|
4075 {
|
|
4076 case CHARSET_TYPE_94:
|
|
4077 Dynarr_add (dst, inter94[reg]);
|
|
4078 break;
|
|
4079 case CHARSET_TYPE_96:
|
|
4080 Dynarr_add (dst, inter96[reg]);
|
|
4081 break;
|
|
4082 case CHARSET_TYPE_94X94:
|
|
4083 Dynarr_add (dst, '$');
|
|
4084 if (reg != 0
|
|
4085 || !(CODING_SYSTEM_ISO2022_SHORT (str->codesys))
|
|
4086 || final < '@'
|
|
4087 || final > 'B')
|
|
4088 Dynarr_add (dst, inter94[reg]);
|
|
4089 break;
|
|
4090 case CHARSET_TYPE_96X96:
|
|
4091 Dynarr_add (dst, '$');
|
|
4092 Dynarr_add (dst, inter96[reg]);
|
|
4093 break;
|
|
4094 }
|
|
4095 Dynarr_add (dst, final);
|
|
4096 }
|
|
4097
|
|
4098 static void
|
|
4099 ensure_normal_shift (struct encoding_stream *str, unsigned_char_dynarr *dst)
|
|
4100 {
|
|
4101 if (str->iso2022.register_left != 0)
|
|
4102 {
|
|
4103 Dynarr_add (dst, ISO_CODE_SI);
|
|
4104 str->iso2022.register_left = 0;
|
|
4105 }
|
|
4106 }
|
|
4107
|
|
4108 static void
|
|
4109 ensure_shift_out (struct encoding_stream *str, unsigned_char_dynarr *dst)
|
|
4110 {
|
|
4111 if (str->iso2022.register_left != 1)
|
|
4112 {
|
|
4113 Dynarr_add (dst, ISO_CODE_SO);
|
|
4114 str->iso2022.register_left = 1;
|
|
4115 }
|
|
4116 }
|
|
4117
|
|
4118 /* Convert internally-formatted data to ISO2022 format. */
|
|
4119
|
|
4120 static void
|
|
4121 encode_coding_iso2022 (Lstream *encoding, CONST unsigned char *src,
|
|
4122 unsigned_char_dynarr *dst, unsigned int n)
|
|
4123 {
|
|
4124 unsigned char charmask, c;
|
|
4125 unsigned int flags, ch;
|
|
4126 enum eol_type eol_type;
|
|
4127 unsigned char char_boundary;
|
|
4128 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
|
|
4129 struct Lisp_Coding_System *codesys = str->codesys;
|
|
4130 int i;
|
|
4131 Lisp_Object charset;
|
|
4132 int half;
|
|
4133
|
337
|
4134 #ifdef ENABLE_COMPOSITE_CHARS
|
259
|
4135 /* flags for handling composite chars. We do a little switcharoo
|
|
4136 on the source while we're outputting the composite char. */
|
|
4137 unsigned int saved_n = 0;
|
|
4138 CONST unsigned char *saved_src = NULL;
|
|
4139 int in_composite = 0;
|
337
|
4140 #endif /* ENABLE_COMPOSITE_CHARS */
|
259
|
4141
|
|
4142 CODING_STREAM_DECOMPOSE (str, flags, ch);
|
|
4143 eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys);
|
|
4144 char_boundary = str->iso2022.current_char_boundary;
|
|
4145 charset = str->iso2022.current_charset;
|
|
4146 half = str->iso2022.current_half;
|
|
4147
|
337
|
4148 #ifdef ENABLE_COMPOSITE_CHARS
|
259
|
4149 back_to_square_n:
|
337
|
4150 #endif
|
259
|
4151 while (n--)
|
|
4152 {
|
|
4153 c = *src++;
|
|
4154
|
|
4155 if (BYTE_ASCII_P (c))
|
|
4156 { /* Processing ASCII character */
|
|
4157 ch = 0;
|
|
4158
|
|
4159 restore_left_to_right_direction (codesys, dst, &flags, 0);
|
|
4160
|
|
4161 /* Make sure G0 contains ASCII */
|
|
4162 if ((c > ' ' && c < ISO_CODE_DEL) ||
|
|
4163 !CODING_SYSTEM_ISO2022_NO_ASCII_CNTL (codesys))
|
|
4164 {
|
|
4165 ensure_normal_shift (str, dst);
|
|
4166 iso2022_designate (Vcharset_ascii, 0, str, dst);
|
|
4167 }
|
|
4168
|
|
4169 /* If necessary, restore everything to the default state
|
|
4170 at end-of-line */
|
|
4171 if (c == '\n' &&
|
|
4172 !(CODING_SYSTEM_ISO2022_NO_ASCII_EOL (codesys)))
|
|
4173 {
|
|
4174 restore_left_to_right_direction (codesys, dst, &flags, 0);
|
|
4175
|
|
4176 ensure_normal_shift (str, dst);
|
|
4177
|
|
4178 for (i = 0; i < 4; i++)
|
|
4179 {
|
|
4180 Lisp_Object initial_charset =
|
|
4181 CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i);
|
|
4182 iso2022_designate (initial_charset, i, str, dst);
|
|
4183 }
|
|
4184 }
|
|
4185 if (c == '\n')
|
|
4186 {
|
|
4187 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
|
|
4188 Dynarr_add (dst, '\r');
|
|
4189 if (eol_type != EOL_CR)
|
|
4190 Dynarr_add (dst, c);
|
|
4191 }
|
|
4192 else
|
|
4193 {
|
|
4194 if (CODING_SYSTEM_ISO2022_ESCAPE_QUOTED (codesys)
|
|
4195 && fit_to_be_escape_quoted (c))
|
|
4196 Dynarr_add (dst, ISO_CODE_ESC);
|
|
4197 Dynarr_add (dst, c);
|
|
4198 }
|
|
4199 char_boundary = 1;
|
|
4200 }
|
|
4201
|
|
4202 else if (BUFBYTE_LEADING_BYTE_P (c) || BUFBYTE_LEADING_BYTE_P (ch))
|
|
4203 { /* Processing Leading Byte */
|
|
4204 ch = 0;
|
|
4205 charset = CHARSET_BY_LEADING_BYTE (c);
|
|
4206 if (LEADING_BYTE_PREFIX_P(c))
|
|
4207 ch = c;
|
|
4208 else if (!EQ (charset, Vcharset_control_1)
|
337
|
4209 #ifdef ENABLE_COMPOSITE_CHARS
|
|
4210 && !EQ (charset, Vcharset_composite)
|
|
4211 #endif
|
|
4212 )
|
259
|
4213 {
|
|
4214 int reg;
|
|
4215
|
|
4216 ensure_correct_direction (XCHARSET_DIRECTION (charset),
|
|
4217 codesys, dst, &flags, 0);
|
|
4218
|
|
4219 /* Now determine which register to use. */
|
|
4220 reg = -1;
|
|
4221 for (i = 0; i < 4; i++)
|
|
4222 {
|
|
4223 if (EQ (charset, str->iso2022.charset[i]) ||
|
|
4224 EQ (charset,
|
|
4225 CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i)))
|
|
4226 {
|
|
4227 reg = i;
|
|
4228 break;
|
|
4229 }
|
|
4230 }
|
|
4231
|
|
4232 if (reg == -1)
|
|
4233 {
|
|
4234 if (XCHARSET_GRAPHIC (charset) != 0)
|
|
4235 {
|
|
4236 if (!NILP (str->iso2022.charset[1]) &&
|
|
4237 (!CODING_SYSTEM_ISO2022_SEVEN (codesys) ||
|
|
4238 CODING_SYSTEM_ISO2022_LOCK_SHIFT (codesys)))
|
|
4239 reg = 1;
|
|
4240 else if (!NILP (str->iso2022.charset[2]))
|
|
4241 reg = 2;
|
|
4242 else if (!NILP (str->iso2022.charset[3]))
|
|
4243 reg = 3;
|
|
4244 else
|
|
4245 reg = 0;
|
|
4246 }
|
|
4247 else
|
|
4248 reg = 0;
|
|
4249 }
|
|
4250
|
|
4251 iso2022_designate (charset, reg, str, dst);
|
|
4252
|
|
4253 /* Now invoke that register. */
|
|
4254 switch (reg)
|
|
4255 {
|
|
4256 case 0:
|
|
4257 ensure_normal_shift (str, dst);
|
|
4258 half = 0;
|
|
4259 break;
|
|
4260
|
|
4261 case 1:
|
|
4262 if (CODING_SYSTEM_ISO2022_SEVEN (codesys))
|
|
4263 {
|
|
4264 ensure_shift_out (str, dst);
|
|
4265 half = 0;
|
|
4266 }
|
|
4267 else
|
|
4268 half = 1;
|
|
4269 break;
|
|
4270
|
|
4271 case 2:
|
|
4272 if (CODING_SYSTEM_ISO2022_SEVEN (str->codesys))
|
|
4273 {
|
|
4274 Dynarr_add (dst, ISO_CODE_ESC);
|
|
4275 Dynarr_add (dst, 'N');
|
|
4276 half = 0;
|
|
4277 }
|
|
4278 else
|
|
4279 {
|
|
4280 Dynarr_add (dst, ISO_CODE_SS2);
|
|
4281 half = 1;
|
|
4282 }
|
|
4283 break;
|
|
4284
|
|
4285 case 3:
|
|
4286 if (CODING_SYSTEM_ISO2022_SEVEN (str->codesys))
|
|
4287 {
|
|
4288 Dynarr_add (dst, ISO_CODE_ESC);
|
|
4289 Dynarr_add (dst, 'O');
|
|
4290 half = 0;
|
|
4291 }
|
|
4292 else
|
|
4293 {
|
|
4294 Dynarr_add (dst, ISO_CODE_SS3);
|
|
4295 half = 1;
|
|
4296 }
|
|
4297 break;
|
|
4298
|
|
4299 default:
|
|
4300 abort ();
|
|
4301 }
|
|
4302 }
|
|
4303 char_boundary = 0;
|
|
4304 }
|
|
4305 else
|
|
4306 { /* Processing Non-ASCII character */
|
|
4307 charmask = (half == 0 ? 0x7F : 0xFF);
|
|
4308 char_boundary = 1;
|
|
4309 if (EQ (charset, Vcharset_control_1))
|
|
4310 {
|
|
4311 if (CODING_SYSTEM_ISO2022_ESCAPE_QUOTED (codesys)
|
|
4312 && fit_to_be_escape_quoted (c))
|
|
4313 Dynarr_add (dst, ISO_CODE_ESC);
|
|
4314 /* you asked for it ... */
|
|
4315 Dynarr_add (dst, c - 0x20);
|
|
4316 }
|
|
4317 else
|
|
4318 {
|
|
4319 switch (XCHARSET_REP_BYTES (charset))
|
|
4320 {
|
|
4321 case 2:
|
|
4322 Dynarr_add (dst, c & charmask);
|
|
4323 break;
|
|
4324 case 3:
|
|
4325 if (XCHARSET_PRIVATE_P (charset))
|
|
4326 {
|
|
4327 Dynarr_add (dst, c & charmask);
|
|
4328 ch = 0;
|
|
4329 }
|
|
4330 else if (ch)
|
|
4331 {
|
337
|
4332 #ifdef ENABLE_COMPOSITE_CHARS
|
259
|
4333 if (EQ (charset, Vcharset_composite))
|
|
4334 {
|
|
4335 if (in_composite)
|
|
4336 {
|
|
4337 /* #### Bother! We don't know how to
|
|
4338 handle this yet. */
|
|
4339 Dynarr_add (dst, '~');
|
|
4340 }
|
|
4341 else
|
|
4342 {
|
|
4343 Emchar emch = MAKE_CHAR (Vcharset_composite,
|
|
4344 ch & 0x7F, c & 0x7F);
|
|
4345 Lisp_Object lstr = composite_char_string (emch);
|
|
4346 saved_n = n;
|
|
4347 saved_src = src;
|
|
4348 in_composite = 1;
|
|
4349 src = XSTRING_DATA (lstr);
|
|
4350 n = XSTRING_LENGTH (lstr);
|
|
4351 Dynarr_add (dst, ISO_CODE_ESC);
|
|
4352 Dynarr_add (dst, '0'); /* start composing */
|
|
4353 }
|
|
4354 }
|
|
4355 else
|
337
|
4356 #endif /* ENABLE_COMPOSITE_CHARS */
|
259
|
4357 {
|
|
4358 Dynarr_add (dst, ch & charmask);
|
|
4359 Dynarr_add (dst, c & charmask);
|
|
4360 }
|
|
4361 ch = 0;
|
|
4362 }
|
|
4363 else
|
|
4364 {
|
|
4365 ch = c;
|
|
4366 char_boundary = 0;
|
|
4367 }
|
|
4368 break;
|
|
4369 case 4:
|
|
4370 if (ch)
|
|
4371 {
|
|
4372 Dynarr_add (dst, ch & charmask);
|
|
4373 Dynarr_add (dst, c & charmask);
|
|
4374 ch = 0;
|
|
4375 }
|
|
4376 else
|
|
4377 {
|
|
4378 ch = c;
|
|
4379 char_boundary = 0;
|
|
4380 }
|
|
4381 break;
|
|
4382 default:
|
|
4383 abort ();
|
|
4384 }
|
|
4385 }
|
|
4386 }
|
|
4387 }
|
|
4388
|
337
|
4389 #ifdef ENABLE_COMPOSITE_CHARS
|
259
|
4390 if (in_composite)
|
|
4391 {
|
|
4392 n = saved_n;
|
|
4393 src = saved_src;
|
|
4394 in_composite = 0;
|
|
4395 Dynarr_add (dst, ISO_CODE_ESC);
|
|
4396 Dynarr_add (dst, '1'); /* end composing */
|
|
4397 goto back_to_square_n; /* Wheeeeeeeee ..... */
|
|
4398 }
|
337
|
4399 #endif /* ENABLE_COMPOSITE_CHARS */
|
259
|
4400
|
|
4401 if (char_boundary && flags & CODING_STATE_END)
|
|
4402 {
|
|
4403 restore_left_to_right_direction (codesys, dst, &flags, 0);
|
|
4404 ensure_normal_shift (str, dst);
|
|
4405 for (i = 0; i < 4; i++)
|
|
4406 {
|
|
4407 Lisp_Object initial_charset =
|
|
4408 CODING_SYSTEM_ISO2022_INITIAL_CHARSET (codesys, i);
|
|
4409 iso2022_designate (initial_charset, i, str, dst);
|
|
4410 }
|
|
4411 }
|
|
4412
|
|
4413 CODING_STREAM_COMPOSE (str, flags, ch);
|
|
4414 str->iso2022.current_char_boundary = char_boundary;
|
|
4415 str->iso2022.current_charset = charset;
|
|
4416 str->iso2022.current_half = half;
|
|
4417
|
|
4418 /* Verbum caro factum est! */
|
|
4419 }
|
|
4420 #endif /* MULE */
|
|
4421
|
|
4422 /************************************************************************/
|
|
4423 /* No-conversion methods */
|
|
4424 /************************************************************************/
|
|
4425
|
|
4426 /* This is used when reading in "binary" files -- i.e. files that may
|
|
4427 contain all 256 possible byte values and that are not to be
|
|
4428 interpreted as being in any particular decoding. */
|
|
4429 static void
|
|
4430 decode_coding_no_conversion (Lstream *decoding, CONST unsigned char *src,
|
|
4431 unsigned_char_dynarr *dst, unsigned int n)
|
|
4432 {
|
|
4433 unsigned char c;
|
|
4434 unsigned int flags, ch;
|
|
4435 enum eol_type eol_type;
|
|
4436 struct decoding_stream *str = DECODING_STREAM_DATA (decoding);
|
|
4437
|
|
4438 CODING_STREAM_DECOMPOSE (str, flags, ch);
|
|
4439 eol_type = str->eol_type;
|
|
4440
|
|
4441 while (n--)
|
|
4442 {
|
|
4443 c = *src++;
|
|
4444
|
|
4445 DECODE_HANDLE_EOL_TYPE (eol_type, c, flags, dst);
|
|
4446 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
4447 label_continue_loop:;
|
|
4448 }
|
|
4449
|
|
4450 DECODE_HANDLE_END_OF_CONVERSION (flags, ch, dst);
|
|
4451
|
|
4452 CODING_STREAM_COMPOSE (str, flags, ch);
|
|
4453 }
|
|
4454
|
|
4455 static void
|
|
4456 encode_coding_no_conversion (Lstream *encoding, CONST unsigned char *src,
|
|
4457 unsigned_char_dynarr *dst, unsigned int n)
|
|
4458 {
|
|
4459 unsigned char c;
|
|
4460 struct encoding_stream *str = ENCODING_STREAM_DATA (encoding);
|
|
4461 unsigned int flags, ch;
|
|
4462 enum eol_type eol_type;
|
|
4463
|
|
4464 CODING_STREAM_DECOMPOSE (str, flags, ch);
|
|
4465 eol_type = CODING_SYSTEM_EOL_TYPE (str->codesys);
|
|
4466
|
|
4467 while (n--)
|
|
4468 {
|
|
4469 c = *src++;
|
|
4470 if (c == '\n')
|
|
4471 {
|
|
4472 if (eol_type != EOL_LF && eol_type != EOL_AUTODETECT)
|
|
4473 Dynarr_add (dst, '\r');
|
|
4474 if (eol_type != EOL_CR)
|
|
4475 Dynarr_add (dst, '\n');
|
|
4476 ch = 0;
|
|
4477 }
|
|
4478 else if (BYTE_ASCII_P (c))
|
|
4479 {
|
|
4480 assert (ch == 0);
|
|
4481 Dynarr_add (dst, c);
|
|
4482 }
|
|
4483 else if (BUFBYTE_LEADING_BYTE_P (c))
|
|
4484 {
|
|
4485 assert (ch == 0);
|
|
4486 if (c == LEADING_BYTE_LATIN_ISO8859_1 ||
|
|
4487 c == LEADING_BYTE_CONTROL_1)
|
|
4488 ch = c;
|
|
4489 else
|
|
4490 Dynarr_add (dst, '~'); /* untranslatable character */
|
|
4491 }
|
|
4492 else
|
|
4493 {
|
|
4494 if (ch == LEADING_BYTE_LATIN_ISO8859_1)
|
|
4495 Dynarr_add (dst, c);
|
|
4496 else if (ch == LEADING_BYTE_CONTROL_1)
|
|
4497 {
|
|
4498 assert (c < 0xC0);
|
|
4499 Dynarr_add (dst, c - 0x20);
|
|
4500 }
|
|
4501 /* else it should be the second or third byte of an
|
|
4502 untranslatable character, so ignore it */
|
|
4503 ch = 0;
|
|
4504 }
|
|
4505 }
|
|
4506
|
|
4507 CODING_STREAM_COMPOSE (str, flags, ch);
|
|
4508 }
|
|
4509
|
|
4510
|
|
4511 /************************************************************************/
|
|
4512 /* Simple internal/external functions */
|
|
4513 /************************************************************************/
|
|
4514
|
|
4515 static Extbyte_dynarr *conversion_out_dynarr;
|
|
4516 static Bufbyte_dynarr *conversion_in_dynarr;
|
|
4517
|
|
4518 /* Determine coding system from coding format */
|
|
4519
|
|
4520 #define FILE_NAME_CODING_SYSTEM \
|
|
4521 ((NILP (Vfile_name_coding_system) || \
|
|
4522 (EQ ((Vfile_name_coding_system), Qbinary))) ? \
|
|
4523 Qnil : Fget_coding_system (Vfile_name_coding_system))
|
|
4524
|
272
|
4525 Extbyte *
|
259
|
4526 convert_to_external_format (CONST Bufbyte *ptr,
|
|
4527 Bytecount len,
|
|
4528 Extcount *len_out,
|
|
4529 enum external_data_format fmt)
|
|
4530 {
|
337
|
4531 Lisp_Object coding_system;
|
|
4532
|
|
4533 /* #### not correct for all values of `fmt'! */
|
|
4534 if (fmt == FORMAT_FILENAME || fmt == FORMAT_TERMINAL)
|
|
4535 coding_system = FILE_NAME_CODING_SYSTEM;
|
|
4536 #ifdef MULE
|
|
4537 else if (fmt == FORMAT_CTEXT)
|
|
4538 coding_system = Fget_coding_system (Qctext);
|
|
4539 #endif
|
|
4540 else
|
|
4541 coding_system = Qnil;
|
|
4542
|
|
4543 /* Lisp_Object coding_system = FMT_CODING_SYSTEM (fmt); */
|
259
|
4544
|
|
4545 if (!conversion_out_dynarr)
|
|
4546 conversion_out_dynarr = Dynarr_new (Extbyte);
|
|
4547 else
|
|
4548 Dynarr_reset (conversion_out_dynarr);
|
|
4549
|
|
4550 if (NILP (coding_system))
|
|
4551 {
|
|
4552 CONST Bufbyte *end = ptr + len;
|
|
4553
|
|
4554 for (; ptr < end;)
|
|
4555 {
|
|
4556 Bufbyte c =
|
|
4557 (BYTE_ASCII_P (*ptr)) ? *ptr :
|
|
4558 (*ptr == LEADING_BYTE_CONTROL_1) ? (*(ptr+1) - 0x20) :
|
|
4559 (*ptr == LEADING_BYTE_LATIN_ISO8859_1) ? (*(ptr+1)) :
|
|
4560 '~';
|
|
4561
|
|
4562 Dynarr_add (conversion_out_dynarr, (Extbyte) c);
|
|
4563 INC_CHARPTR (ptr);
|
|
4564 }
|
|
4565
|
|
4566 #ifdef ERROR_CHECK_BUFPOS
|
|
4567 assert (ptr == end);
|
|
4568 #endif
|
|
4569 }
|
|
4570 else
|
|
4571 {
|
|
4572 Lisp_Object instream, outstream, da_outstream;
|
|
4573 Lstream *istr, *ostr;
|
|
4574 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
4575 char tempbuf[1024]; /* some random amount */
|
|
4576
|
|
4577 instream = make_fixed_buffer_input_stream ((unsigned char *) ptr, len);
|
|
4578 da_outstream = make_dynarr_output_stream
|
|
4579 ((unsigned_char_dynarr *) conversion_out_dynarr);
|
|
4580 outstream =
|
|
4581 make_encoding_output_stream (XLSTREAM (da_outstream), coding_system);
|
|
4582 istr = XLSTREAM (instream);
|
|
4583 ostr = XLSTREAM (outstream);
|
|
4584 GCPRO3 (instream, outstream, da_outstream);
|
|
4585 while (1)
|
|
4586 {
|
|
4587 int size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
|
|
4588 if (!size_in_bytes)
|
|
4589 break;
|
|
4590 Lstream_write (ostr, tempbuf, size_in_bytes);
|
|
4591 }
|
|
4592 Lstream_close (istr);
|
|
4593 Lstream_close (ostr);
|
|
4594 UNGCPRO;
|
|
4595 Lstream_delete (istr);
|
|
4596 Lstream_delete (ostr);
|
|
4597 Lstream_delete (XLSTREAM (da_outstream));
|
|
4598 }
|
|
4599
|
|
4600 *len_out = Dynarr_length (conversion_out_dynarr);
|
|
4601 Dynarr_add (conversion_out_dynarr, 0); /* remember to zero-terminate! */
|
|
4602 return Dynarr_atp (conversion_out_dynarr, 0);
|
|
4603 }
|
|
4604
|
272
|
4605 Bufbyte *
|
259
|
4606 convert_from_external_format (CONST Extbyte *ptr,
|
|
4607 Extcount len,
|
|
4608 Bytecount *len_out,
|
|
4609 enum external_data_format fmt)
|
|
4610 {
|
337
|
4611 Lisp_Object coding_system;
|
|
4612
|
|
4613 /* #### not correct for all values of `fmt'! */
|
|
4614 if (fmt == FORMAT_FILENAME || fmt == FORMAT_TERMINAL)
|
|
4615 coding_system = FILE_NAME_CODING_SYSTEM;
|
|
4616 #ifdef MULE
|
|
4617 else if (fmt == FORMAT_CTEXT)
|
|
4618 coding_system = Fget_coding_system (Qctext);
|
|
4619 #endif
|
|
4620 else
|
|
4621 coding_system = Qnil;
|
|
4622
|
|
4623 /* Lisp_Object coding_system = FMT_CODING_SYSTEM (fmt); */
|
259
|
4624
|
|
4625 if (!conversion_in_dynarr)
|
|
4626 conversion_in_dynarr = Dynarr_new (Bufbyte);
|
|
4627 else
|
|
4628 Dynarr_reset (conversion_in_dynarr);
|
|
4629
|
|
4630 if (NILP (coding_system))
|
|
4631 {
|
|
4632 CONST Extbyte *end = ptr + len;
|
|
4633 for (; ptr < end; ptr++)
|
|
4634 {
|
|
4635 Extbyte c = *ptr;
|
|
4636 DECODE_ADD_BINARY_CHAR (c, conversion_in_dynarr);
|
|
4637 }
|
|
4638 }
|
|
4639 else
|
|
4640 {
|
|
4641 Lisp_Object instream, outstream, da_outstream;
|
|
4642 Lstream *istr, *ostr;
|
|
4643 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
4644 char tempbuf[1024]; /* some random amount */
|
|
4645
|
|
4646 instream = make_fixed_buffer_input_stream ((unsigned char *) ptr, len);
|
|
4647 da_outstream = make_dynarr_output_stream
|
|
4648 ((unsigned_char_dynarr *) conversion_in_dynarr);
|
|
4649 outstream =
|
|
4650 make_decoding_output_stream (XLSTREAM (da_outstream), coding_system);
|
|
4651 istr = XLSTREAM (instream);
|
|
4652 ostr = XLSTREAM (outstream);
|
|
4653 GCPRO3 (instream, outstream, da_outstream);
|
|
4654 while (1)
|
|
4655 {
|
|
4656 int size_in_bytes = Lstream_read (istr, tempbuf, sizeof (tempbuf));
|
|
4657 if (!size_in_bytes)
|
|
4658 break;
|
|
4659 Lstream_write (ostr, tempbuf, size_in_bytes);
|
|
4660 }
|
|
4661 Lstream_close (istr);
|
|
4662 Lstream_close (ostr);
|
|
4663 UNGCPRO;
|
|
4664 Lstream_delete (istr);
|
|
4665 Lstream_delete (ostr);
|
|
4666 Lstream_delete (XLSTREAM (da_outstream));
|
|
4667 }
|
|
4668
|
|
4669 *len_out = Dynarr_length (conversion_in_dynarr);
|
|
4670 Dynarr_add (conversion_in_dynarr, 0); /* remember to zero-terminate! */
|
|
4671 return Dynarr_atp (conversion_in_dynarr, 0);
|
|
4672 }
|
|
4673
|
|
4674
|
|
4675 /************************************************************************/
|
|
4676 /* Initialization */
|
|
4677 /************************************************************************/
|
|
4678
|
|
4679 void
|
347
|
4680 syms_of_file_coding (void)
|
259
|
4681 {
|
|
4682 defsymbol (&Qbuffer_file_coding_system, "buffer-file-coding-system");
|
|
4683 deferror (&Qcoding_system_error, "coding-system-error",
|
|
4684 "Coding-system error", Qio_error);
|
|
4685
|
|
4686 DEFSUBR (Fcoding_system_p);
|
|
4687 DEFSUBR (Ffind_coding_system);
|
|
4688 DEFSUBR (Fget_coding_system);
|
|
4689 DEFSUBR (Fcoding_system_list);
|
|
4690 DEFSUBR (Fcoding_system_name);
|
|
4691 DEFSUBR (Fmake_coding_system);
|
|
4692 DEFSUBR (Fcopy_coding_system);
|
|
4693 DEFSUBR (Fsubsidiary_coding_system);
|
|
4694
|
|
4695 DEFSUBR (Fcoding_system_type);
|
|
4696 DEFSUBR (Fcoding_system_doc_string);
|
|
4697 #ifdef MULE
|
|
4698 DEFSUBR (Fcoding_system_charset);
|
|
4699 #endif
|
|
4700 DEFSUBR (Fcoding_system_property);
|
|
4701
|
|
4702 DEFSUBR (Fcoding_category_list);
|
|
4703 DEFSUBR (Fset_coding_priority_list);
|
|
4704 DEFSUBR (Fcoding_priority_list);
|
|
4705 DEFSUBR (Fset_coding_category_system);
|
|
4706 DEFSUBR (Fcoding_category_system);
|
|
4707
|
|
4708 DEFSUBR (Fdetect_coding_region);
|
|
4709 DEFSUBR (Fdecode_coding_region);
|
|
4710 DEFSUBR (Fencode_coding_region);
|
|
4711 #ifdef MULE
|
|
4712 DEFSUBR (Fdecode_shift_jis_char);
|
|
4713 DEFSUBR (Fencode_shift_jis_char);
|
|
4714 DEFSUBR (Fdecode_big5_char);
|
|
4715 DEFSUBR (Fencode_big5_char);
|
|
4716 #endif /* MULE */
|
|
4717 defsymbol (&Qcoding_system_p, "coding-system-p");
|
|
4718 defsymbol (&Qno_conversion, "no-conversion");
|
|
4719 #ifdef MULE
|
|
4720 defsymbol (&Qbig5, "big5");
|
|
4721 defsymbol (&Qshift_jis, "shift-jis");
|
|
4722 defsymbol (&Qccl, "ccl");
|
|
4723 defsymbol (&Qiso2022, "iso2022");
|
|
4724 #endif /* MULE */
|
|
4725 defsymbol (&Qmnemonic, "mnemonic");
|
|
4726 defsymbol (&Qeol_type, "eol-type");
|
|
4727 defsymbol (&Qpost_read_conversion, "post-read-conversion");
|
|
4728 defsymbol (&Qpre_write_conversion, "pre-write-conversion");
|
|
4729
|
|
4730 defsymbol (&Qcr, "cr");
|
|
4731 defsymbol (&Qlf, "lf");
|
|
4732 defsymbol (&Qcrlf, "crlf");
|
|
4733 defsymbol (&Qeol_cr, "eol-cr");
|
|
4734 defsymbol (&Qeol_lf, "eol-lf");
|
|
4735 defsymbol (&Qeol_crlf, "eol-crlf");
|
|
4736 #ifdef MULE
|
|
4737 defsymbol (&Qcharset_g0, "charset-g0");
|
|
4738 defsymbol (&Qcharset_g1, "charset-g1");
|
|
4739 defsymbol (&Qcharset_g2, "charset-g2");
|
|
4740 defsymbol (&Qcharset_g3, "charset-g3");
|
|
4741 defsymbol (&Qforce_g0_on_output, "force-g0-on-output");
|
|
4742 defsymbol (&Qforce_g1_on_output, "force-g1-on-output");
|
|
4743 defsymbol (&Qforce_g2_on_output, "force-g2-on-output");
|
|
4744 defsymbol (&Qforce_g3_on_output, "force-g3-on-output");
|
|
4745 defsymbol (&Qno_iso6429, "no-iso6429");
|
|
4746 defsymbol (&Qinput_charset_conversion, "input-charset-conversion");
|
|
4747 defsymbol (&Qoutput_charset_conversion, "output-charset-conversion");
|
272
|
4748
|
259
|
4749 defsymbol (&Qshort, "short");
|
|
4750 defsymbol (&Qno_ascii_eol, "no-ascii-eol");
|
|
4751 defsymbol (&Qno_ascii_cntl, "no-ascii-cntl");
|
|
4752 defsymbol (&Qseven, "seven");
|
|
4753 defsymbol (&Qlock_shift, "lock-shift");
|
|
4754 defsymbol (&Qescape_quoted, "escape-quoted");
|
272
|
4755 #endif /* MULE */
|
259
|
4756 defsymbol (&Qencode, "encode");
|
|
4757 defsymbol (&Qdecode, "decode");
|
|
4758
|
|
4759 #ifdef MULE
|
|
4760 defsymbol (&Qctext, "ctext");
|
|
4761 defsymbol (&coding_category_symbol[CODING_CATEGORY_SHIFT_JIS],
|
|
4762 "shift-jis");
|
|
4763 defsymbol (&coding_category_symbol[CODING_CATEGORY_BIG5],
|
|
4764 "big5");
|
|
4765 defsymbol (&coding_category_symbol[CODING_CATEGORY_ISO_7],
|
|
4766 "iso-7");
|
|
4767 defsymbol (&coding_category_symbol[CODING_CATEGORY_ISO_8_DESIGNATE],
|
|
4768 "iso-8-designate");
|
|
4769 defsymbol (&coding_category_symbol[CODING_CATEGORY_ISO_8_1],
|
|
4770 "iso-8-1");
|
|
4771 defsymbol (&coding_category_symbol[CODING_CATEGORY_ISO_8_2],
|
|
4772 "iso-8-2");
|
|
4773 defsymbol (&coding_category_symbol[CODING_CATEGORY_ISO_LOCK_SHIFT],
|
|
4774 "iso-lock-shift");
|
272
|
4775 #endif /* MULE */
|
259
|
4776 defsymbol (&coding_category_symbol[CODING_CATEGORY_NO_CONVERSION],
|
|
4777 "no-conversion");
|
|
4778 }
|
|
4779
|
|
4780 void
|
347
|
4781 lstream_type_create_file_coding (void)
|
259
|
4782 {
|
|
4783 LSTREAM_HAS_METHOD (decoding, reader);
|
|
4784 LSTREAM_HAS_METHOD (decoding, writer);
|
|
4785 LSTREAM_HAS_METHOD (decoding, rewinder);
|
|
4786 LSTREAM_HAS_METHOD (decoding, seekable_p);
|
|
4787 LSTREAM_HAS_METHOD (decoding, flusher);
|
|
4788 LSTREAM_HAS_METHOD (decoding, closer);
|
|
4789 LSTREAM_HAS_METHOD (decoding, marker);
|
|
4790
|
|
4791 LSTREAM_HAS_METHOD (encoding, reader);
|
|
4792 LSTREAM_HAS_METHOD (encoding, writer);
|
|
4793 LSTREAM_HAS_METHOD (encoding, rewinder);
|
|
4794 LSTREAM_HAS_METHOD (encoding, seekable_p);
|
|
4795 LSTREAM_HAS_METHOD (encoding, flusher);
|
|
4796 LSTREAM_HAS_METHOD (encoding, closer);
|
|
4797 LSTREAM_HAS_METHOD (encoding, marker);
|
|
4798 }
|
|
4799
|
|
4800 void
|
347
|
4801 vars_of_file_coding (void)
|
259
|
4802 {
|
|
4803 int i;
|
|
4804
|
|
4805 /* Initialize to something reasonable ... */
|
|
4806 for (i = 0; i <= CODING_CATEGORY_LAST; i++)
|
|
4807 {
|
|
4808 coding_category_system[i] = Qnil;
|
|
4809 coding_category_by_priority[i] = i;
|
|
4810 }
|
|
4811
|
|
4812 Fprovide (intern ("file-coding"));
|
|
4813
|
|
4814 DEFVAR_LISP ("keyboard-coding-system", &Vkeyboard_coding_system /*
|
|
4815 Coding system used for TTY keyboard input.
|
|
4816 Not used under a windowing system.
|
|
4817 */ );
|
|
4818 Vkeyboard_coding_system = Qnil;
|
|
4819
|
|
4820 DEFVAR_LISP ("terminal-coding-system", &Vterminal_coding_system /*
|
|
4821 Coding system used for TTY display output.
|
|
4822 Not used under a windowing system.
|
|
4823 */ );
|
|
4824 Vterminal_coding_system = Qnil;
|
|
4825
|
|
4826 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read /*
|
|
4827 Overriding coding system used when writing a file or process.
|
|
4828 You should *bind* this, not set it. If this is non-nil, it specifies
|
|
4829 the coding system that will be used when a file or process is read
|
|
4830 in, and overrides `buffer-file-coding-system-for-read',
|
|
4831 `insert-file-contents-pre-hook', etc. Use those variables instead of
|
|
4832 this one for permanent changes to the environment.
|
|
4833 */ );
|
|
4834 Vcoding_system_for_read = Qnil;
|
|
4835
|
|
4836 DEFVAR_LISP ("coding-system-for-write",
|
|
4837 &Vcoding_system_for_write /*
|
|
4838 Overriding coding system used when writing a file or process.
|
|
4839 You should *bind* this, not set it. If this is non-nil, it specifies
|
|
4840 the coding system that will be used when a file or process is wrote
|
|
4841 in, and overrides `buffer-file-coding-system',
|
|
4842 `write-region-pre-hook', etc. Use those variables instead of this one
|
|
4843 for permanent changes to the environment.
|
|
4844 */ );
|
|
4845 Vcoding_system_for_write = Qnil;
|
|
4846
|
|
4847 DEFVAR_LISP ("file-name-coding-system", &Vfile_name_coding_system /*
|
|
4848 Coding system used to convert pathnames when accessing files.
|
|
4849 */ );
|
|
4850 Vfile_name_coding_system = Qnil;
|
|
4851
|
|
4852 DEFVAR_BOOL ("enable-multibyte-characters", &enable_multibyte_characters /*
|
|
4853 Non-nil means the buffer contents are regarded as multi-byte form
|
|
4854 of characters, not a binary code. This affects the display, file I/O,
|
|
4855 and behaviors of various editing commands.
|
|
4856
|
|
4857 Setting this to nil does not do anything.
|
|
4858 */ );
|
|
4859 enable_multibyte_characters = 1;
|
|
4860 }
|
|
4861
|
|
4862 void
|
347
|
4863 complex_vars_of_file_coding (void)
|
259
|
4864 {
|
|
4865 staticpro (&Vcoding_system_hashtable);
|
|
4866 Vcoding_system_hashtable = make_lisp_hashtable (50, HASHTABLE_NONWEAK,
|
|
4867 HASHTABLE_EQ);
|
|
4868
|
|
4869 the_codesys_prop_dynarr = Dynarr_new (codesys_prop);
|
|
4870
|
|
4871 #define DEFINE_CODESYS_PROP(Prop_Type, Sym) do \
|
|
4872 { \
|
|
4873 struct codesys_prop csp; \
|
|
4874 csp.sym = (Sym); \
|
|
4875 csp.prop_type = (Prop_Type); \
|
|
4876 Dynarr_add (the_codesys_prop_dynarr, csp); \
|
|
4877 } while (0)
|
|
4878
|
|
4879 DEFINE_CODESYS_PROP (CODESYS_PROP_ALL_OK, Qmnemonic);
|
|
4880 DEFINE_CODESYS_PROP (CODESYS_PROP_ALL_OK, Qeol_type);
|
|
4881 DEFINE_CODESYS_PROP (CODESYS_PROP_ALL_OK, Qeol_cr);
|
|
4882 DEFINE_CODESYS_PROP (CODESYS_PROP_ALL_OK, Qeol_crlf);
|
|
4883 DEFINE_CODESYS_PROP (CODESYS_PROP_ALL_OK, Qeol_lf);
|
|
4884 DEFINE_CODESYS_PROP (CODESYS_PROP_ALL_OK, Qpost_read_conversion);
|
|
4885 DEFINE_CODESYS_PROP (CODESYS_PROP_ALL_OK, Qpre_write_conversion);
|
|
4886 #ifdef MULE
|
|
4887 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qcharset_g0);
|
|
4888 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qcharset_g1);
|
|
4889 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qcharset_g2);
|
|
4890 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qcharset_g3);
|
|
4891 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qforce_g0_on_output);
|
|
4892 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qforce_g1_on_output);
|
|
4893 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qforce_g2_on_output);
|
|
4894 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qforce_g3_on_output);
|
|
4895 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qshort);
|
|
4896 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qno_ascii_eol);
|
|
4897 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qno_ascii_cntl);
|
|
4898 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qseven);
|
|
4899 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qlock_shift);
|
|
4900 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qno_iso6429);
|
|
4901 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qescape_quoted);
|
|
4902 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qinput_charset_conversion);
|
|
4903 DEFINE_CODESYS_PROP (CODESYS_PROP_ISO2022, Qoutput_charset_conversion);
|
|
4904
|
|
4905 DEFINE_CODESYS_PROP (CODESYS_PROP_CCL, Qencode);
|
|
4906 DEFINE_CODESYS_PROP (CODESYS_PROP_CCL, Qdecode);
|
|
4907 #endif /* MULE */
|
|
4908 /* Need to create this here or we're really screwed. */
|
|
4909 Fmake_coding_system (Qno_conversion, Qno_conversion, build_string ("No conversion"),
|
|
4910 list2 (Qmnemonic, build_string ("Noconv")));
|
|
4911
|
|
4912 Fcopy_coding_system (Fcoding_system_property (Qno_conversion, Qeol_lf),
|
|
4913 Qbinary);
|
|
4914
|
|
4915 /* Need this for bootstrapping */
|
|
4916 coding_category_system[CODING_CATEGORY_NO_CONVERSION] =
|
|
4917 Fget_coding_system (Qno_conversion);
|
|
4918 }
|