771
|
1 /* Text encoding conversion functions; coding-system object.
|
|
2 #### rename me to coding-system.c or coding.c
|
428
|
3 Copyright (C) 1991, 1995 Free Software Foundation, Inc.
|
|
4 Copyright (C) 1995 Sun Microsystems, Inc.
|
3025
|
5 Copyright (C) 2000, 2001, 2002, 2003, 2005 Ben Wing.
|
428
|
6
|
|
7 This file is part of XEmacs.
|
|
8
|
|
9 XEmacs is free software; you can redistribute it and/or modify it
|
|
10 under the terms of the GNU General Public License as published by the
|
|
11 Free Software Foundation; either version 2, or (at your option) any
|
|
12 later version.
|
|
13
|
|
14 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
17 for more details.
|
|
18
|
|
19 You should have received a copy of the GNU General Public License
|
|
20 along with XEmacs; see the file COPYING. If not, write to
|
|
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 Boston, MA 02111-1307, USA. */
|
|
23
|
771
|
24 /* Synched up with: Not in FSF. */
|
|
25
|
|
26 /* Authorship:
|
|
27
|
|
28 Current primary author: Ben Wing <ben@xemacs.org>
|
|
29
|
|
30 Rewritten by Ben Wing <ben@xemacs.org>, based originally on coding.c
|
|
31 from Mule 2.? but probably does not share one line of code with that
|
|
32 original source. Rewriting work started around Dec. 1994. or Jan. 1995.
|
|
33 Proceeded in earnest till Nov. 1995.
|
|
34
|
|
35 Around Feb. 17, 1998, Andy Piper renamed what was then mule-coding.c to
|
|
36 file-coding.c, with the intention of using it to do end-of-line conversion
|
|
37 on non-MULE machines (specifically, on Windows machines). He separated
|
|
38 out the MULE stuff from non-MULE using ifdef's, and searched throughout
|
|
39 the rest of the source tree looking for coding-system-related code that
|
|
40 was ifdef MULE but should be ifdef HAVE_CODING_SYSTEMS.
|
|
41
|
|
42 Sept. 4 - 8, 1998, Tomohiko Morioka added the UCS_4 and UTF_8 coding system
|
|
43 types, providing a primitive means of decoding and encoding externally-
|
|
44 formatted Unicode/UCS_4 and Unicode/UTF_8 data.
|
|
45
|
|
46 January 25, 2000, Martin Buchholz redid and fleshed out the coding
|
|
47 system alias handling that was first added in prototype form by
|
|
48 Hrjove Niksic, April 15, 1999.
|
|
49
|
|
50 April to May 2000, Ben Wing: More major reorganization. Adding features
|
|
51 needed for MS Windows (multibyte, unicode, unicode-to-multibyte), the
|
|
52 "chain" coding system for chaining two together, and doing a lot of
|
|
53 reorganization in preparation for properly abstracting out the different
|
|
54 coding system types.
|
|
55
|
|
56 June 2001, Ben Wing: Added Unicode support. Eliminated previous
|
|
57 junky Unicode translation support.
|
|
58
|
|
59 August 2001, Ben Wing: Moved Unicode support to unicode.c. Finished
|
|
60 abstracting everything except detection, which is hard to abstract (see
|
|
61 just below).
|
|
62
|
|
63 September 2001, Ben Wing: Moved Mule code to mule-coding.c, Windows code
|
|
64 to intl-win32.c. Lots more rewriting; very little code is untouched
|
|
65 from before April 2000. Abstracted the detection code, added multiple
|
|
66 levels of likelihood to increase the reliability of the algorithm.
|
|
67
|
|
68 October 2001, Ben Wing: HAVE_CODING_SYSTEMS is always now defined.
|
|
69 Removed the conditionals.
|
|
70 */
|
|
71
|
428
|
72 #include <config.h>
|
|
73 #include "lisp.h"
|
|
74
|
|
75 #include "buffer.h"
|
|
76 #include "elhash.h"
|
|
77 #include "insdel.h"
|
|
78 #include "lstream.h"
|
440
|
79 #include "opaque.h"
|
771
|
80 #include "file-coding.h"
|
|
81
|
|
82 #ifdef HAVE_ZLIB
|
|
83 #include "zlib.h"
|
428
|
84 #endif
|
|
85
|
|
86 Lisp_Object Vkeyboard_coding_system;
|
|
87 Lisp_Object Vterminal_coding_system;
|
|
88 Lisp_Object Vcoding_system_for_read;
|
|
89 Lisp_Object Vcoding_system_for_write;
|
|
90 Lisp_Object Vfile_name_coding_system;
|
|
91
|
771
|
92 #ifdef DEBUG_XEMACS
|
|
93 Lisp_Object Vdebug_coding_detection;
|
440
|
94 #endif
|
771
|
95
|
|
96 typedef struct coding_system_type_entry
|
|
97 {
|
|
98 struct coding_system_methods *meths;
|
|
99 } coding_system_type_entry;
|
|
100
|
|
101 typedef struct
|
|
102 {
|
|
103 Dynarr_declare (coding_system_type_entry);
|
|
104 } coding_system_type_entry_dynarr;
|
|
105
|
|
106 static coding_system_type_entry_dynarr *the_coding_system_type_entry_dynarr;
|
|
107
|
1204
|
108 static const struct memory_description cste_description_1[] = {
|
2551
|
109 { XD_BLOCK_PTR, offsetof (coding_system_type_entry, meths), 1,
|
|
110 { &coding_system_methods_description } },
|
771
|
111 { XD_END }
|
|
112 };
|
|
113
|
1204
|
114 static const struct sized_memory_description cste_description = {
|
771
|
115 sizeof (coding_system_type_entry),
|
|
116 cste_description_1
|
|
117 };
|
|
118
|
1204
|
119 static const struct memory_description csted_description_1[] = {
|
771
|
120 XD_DYNARR_DESC (coding_system_type_entry_dynarr, &cste_description),
|
428
|
121 { XD_END }
|
|
122 };
|
|
123
|
1204
|
124 static const struct sized_memory_description csted_description = {
|
771
|
125 sizeof (coding_system_type_entry_dynarr),
|
|
126 csted_description_1
|
|
127 };
|
|
128
|
|
129 static Lisp_Object Vcoding_system_type_list;
|
|
130
|
|
131 /* Coding system currently associated with each coding category. */
|
|
132 Lisp_Object coding_category_system[MAX_DETECTOR_CATEGORIES];
|
|
133
|
|
134 /* Table of all coding categories in decreasing order of priority.
|
|
135 This describes a permutation of the possible coding categories. */
|
|
136 int coding_category_by_priority[MAX_DETECTOR_CATEGORIES];
|
|
137
|
|
138 /* Value used with to give a unique name to nameless coding systems */
|
|
139 int coding_system_tick;
|
|
140
|
|
141 int coding_detector_count;
|
|
142 int coding_detector_category_count;
|
|
143
|
|
144 detector_dynarr *all_coding_detectors;
|
|
145
|
1204
|
146 static const struct memory_description struct_detector_category_description_1[]
|
771
|
147 =
|
|
148 {
|
|
149 { XD_LISP_OBJECT, offsetof (struct detector_category, sym) },
|
|
150 { XD_END }
|
|
151 };
|
|
152
|
1204
|
153 static const struct sized_memory_description struct_detector_category_description =
|
771
|
154 {
|
|
155 sizeof (struct detector_category),
|
|
156 struct_detector_category_description_1
|
428
|
157 };
|
|
158
|
1204
|
159 static const struct memory_description detector_category_dynarr_description_1[] =
|
771
|
160 {
|
|
161 XD_DYNARR_DESC (detector_category_dynarr,
|
|
162 &struct_detector_category_description),
|
|
163 { XD_END }
|
|
164 };
|
|
165
|
1204
|
166 static const struct sized_memory_description detector_category_dynarr_description = {
|
771
|
167 sizeof (detector_category_dynarr),
|
|
168 detector_category_dynarr_description_1
|
|
169 };
|
|
170
|
1204
|
171 static const struct memory_description struct_detector_description_1[]
|
771
|
172 =
|
|
173 {
|
2367
|
174 { XD_BLOCK_PTR, offsetof (struct detector, cats), 1,
|
2551
|
175 { &detector_category_dynarr_description } },
|
771
|
176 { XD_END }
|
|
177 };
|
|
178
|
1204
|
179 static const struct sized_memory_description struct_detector_description =
|
771
|
180 {
|
|
181 sizeof (struct detector),
|
|
182 struct_detector_description_1
|
|
183 };
|
|
184
|
1204
|
185 static const struct memory_description detector_dynarr_description_1[] =
|
771
|
186 {
|
|
187 XD_DYNARR_DESC (detector_dynarr, &struct_detector_description),
|
|
188 { XD_END }
|
|
189 };
|
|
190
|
1204
|
191 static const struct sized_memory_description detector_dynarr_description = {
|
771
|
192 sizeof (detector_dynarr),
|
|
193 detector_dynarr_description_1
|
|
194 };
|
428
|
195
|
|
196 Lisp_Object Qcoding_systemp;
|
|
197
|
771
|
198 Lisp_Object Qraw_text;
|
428
|
199
|
|
200 Lisp_Object Qmnemonic, Qeol_type;
|
|
201 Lisp_Object Qcr, Qcrlf, Qlf;
|
|
202 Lisp_Object Qeol_cr, Qeol_crlf, Qeol_lf;
|
|
203 Lisp_Object Qpost_read_conversion;
|
|
204 Lisp_Object Qpre_write_conversion;
|
|
205
|
771
|
206 Lisp_Object Qtranslation_table_for_decode;
|
|
207 Lisp_Object Qtranslation_table_for_encode;
|
|
208 Lisp_Object Qsafe_chars;
|
|
209 Lisp_Object Qsafe_charsets;
|
|
210 Lisp_Object Qmime_charset;
|
|
211 Lisp_Object Qvalid_codes;
|
|
212
|
|
213 Lisp_Object Qno_conversion;
|
|
214 Lisp_Object Qconvert_eol;
|
440
|
215 Lisp_Object Qescape_quoted;
|
771
|
216 Lisp_Object Qencode, Qdecode;
|
|
217
|
|
218 Lisp_Object Qconvert_eol_lf, Qconvert_eol_cr, Qconvert_eol_crlf;
|
|
219 Lisp_Object Qconvert_eol_autodetect;
|
|
220
|
|
221 Lisp_Object Qnear_certainty, Qquite_probable, Qsomewhat_likely;
|
1494
|
222 Lisp_Object Qslightly_likely;
|
771
|
223 Lisp_Object Qas_likely_as_unlikely, Qsomewhat_unlikely, Qquite_improbable;
|
|
224 Lisp_Object Qnearly_impossible;
|
|
225
|
|
226 Lisp_Object Qdo_eol, Qdo_coding;
|
|
227
|
|
228 Lisp_Object Qcanonicalize_after_coding;
|
|
229
|
1347
|
230 Lisp_Object QScoding_system_cookie;
|
|
231
|
771
|
232 /* This is used to convert autodetected coding systems into existing
|
|
233 systems. For example, the chain undecided->convert-eol-autodetect may
|
|
234 have its separate parts detected as mswindows-multibyte and
|
|
235 convert-eol-crlf, and the result needs to be mapped to
|
|
236 mswindows-multibyte-dos. */
|
|
237 /* #### It's not clear we need this whole chain-canonicalize mechanism
|
|
238 any more. */
|
|
239 static Lisp_Object Vchain_canonicalize_hash_table;
|
|
240
|
|
241 #ifdef HAVE_ZLIB
|
|
242 Lisp_Object Qgzip;
|
428
|
243 #endif
|
771
|
244
|
2297
|
245 /* Maps symbols (coding system names) to either coding system objects or
|
|
246 (for aliases) other names. */
|
771
|
247 static Lisp_Object Vcoding_system_hash_table;
|
428
|
248
|
|
249 int enable_multibyte_characters;
|
|
250
|
|
251 EXFUN (Fcopy_coding_system, 2);
|
|
252
|
|
253
|
|
254 /************************************************************************/
|
771
|
255 /* Coding system object methods */
|
428
|
256 /************************************************************************/
|
|
257
|
|
258 static Lisp_Object
|
|
259 mark_coding_system (Lisp_Object obj)
|
|
260 {
|
|
261 Lisp_Coding_System *codesys = XCODING_SYSTEM (obj);
|
|
262
|
1204
|
263 #define MARKED_SLOT(x) mark_object (codesys->x);
|
|
264 #include "coding-system-slots.h"
|
771
|
265
|
|
266 MAYBE_CODESYSMETH (codesys, mark, (obj));
|
428
|
267
|
1204
|
268 return Qnil;
|
428
|
269 }
|
|
270
|
|
271 static void
|
771
|
272 print_coding_system_properties (Lisp_Object obj, Lisp_Object printcharfun)
|
|
273 {
|
|
274 Lisp_Coding_System *c = XCODING_SYSTEM (obj);
|
|
275 print_internal (c->methods->type, printcharfun, 1);
|
|
276 MAYBE_CODESYSMETH (c, print, (obj, printcharfun, 1));
|
|
277 if (CODING_SYSTEM_EOL_TYPE (c) != EOL_AUTODETECT)
|
|
278 write_fmt_string_lisp (printcharfun, " eol-type=%s",
|
|
279 1, Fcoding_system_property (obj, Qeol_type));
|
|
280 }
|
|
281
|
|
282 static void
|
428
|
283 print_coding_system (Lisp_Object obj, Lisp_Object printcharfun,
|
2286
|
284 int UNUSED (escapeflag))
|
428
|
285 {
|
|
286 Lisp_Coding_System *c = XCODING_SYSTEM (obj);
|
|
287 if (print_readably)
|
771
|
288 printing_unreadable_object
|
|
289 ("printing unreadable object #<coding-system 0x%x>", c->header.uid);
|
|
290
|
|
291 write_fmt_string_lisp (printcharfun, "#<coding-system %s ", 1, c->name);
|
|
292 print_coding_system_properties (obj, printcharfun);
|
826
|
293 write_c_string (printcharfun, ">");
|
428
|
294 }
|
|
295
|
771
|
296 /* Print an abbreviated version of a coding system (but still containing
|
|
297 all the information), for use within a coding system print method. */
|
|
298
|
|
299 static void
|
|
300 print_coding_system_in_print_method (Lisp_Object cs, Lisp_Object printcharfun,
|
2286
|
301 int UNUSED (escapeflag))
|
771
|
302 {
|
800
|
303 write_fmt_string_lisp (printcharfun, "%s[", 1, XCODING_SYSTEM_NAME (cs));
|
771
|
304 print_coding_system_properties (cs, printcharfun);
|
826
|
305 write_c_string (printcharfun, "]");
|
771
|
306 }
|
|
307
|
3263
|
308 #ifndef NEW_GC
|
428
|
309 static void
|
|
310 finalize_coding_system (void *header, int for_disksave)
|
|
311 {
|
771
|
312 Lisp_Object cs = wrap_coding_system ((Lisp_Coding_System *) header);
|
428
|
313 /* Since coding systems never go away, this function is not
|
|
314 necessary. But it would be necessary if we changed things
|
|
315 so that coding systems could go away. */
|
|
316 if (!for_disksave) /* see comment in lstream.c */
|
771
|
317 MAYBE_XCODESYSMETH (cs, finalize, (cs));
|
|
318 }
|
3263
|
319 #endif /* not NEW_GC */
|
771
|
320
|
|
321 static Bytecount
|
|
322 sizeof_coding_system (const void *header)
|
|
323 {
|
|
324 const Lisp_Coding_System *p = (const Lisp_Coding_System *) header;
|
|
325 return offsetof (Lisp_Coding_System, data) + p->methods->extra_data_size;
|
428
|
326 }
|
|
327
|
1204
|
328 static const struct memory_description coding_system_methods_description_1[]
|
771
|
329 = {
|
|
330 { XD_LISP_OBJECT,
|
|
331 offsetof (struct coding_system_methods, type) },
|
|
332 { XD_LISP_OBJECT,
|
|
333 offsetof (struct coding_system_methods, predicate_symbol) },
|
|
334 { XD_END }
|
|
335 };
|
|
336
|
1204
|
337 const struct sized_memory_description coding_system_methods_description = {
|
771
|
338 sizeof (struct coding_system_methods),
|
|
339 coding_system_methods_description_1
|
|
340 };
|
|
341
|
1204
|
342 static const struct sized_memory_description coding_system_extra_description_map[] =
|
|
343 {
|
|
344 { offsetof (Lisp_Coding_System, methods) },
|
|
345 { offsetof (struct coding_system_methods, extra_description) },
|
|
346 { -1 },
|
771
|
347 };
|
|
348
|
1204
|
349 static const struct memory_description coding_system_description[] =
|
428
|
350 {
|
2367
|
351 { XD_BLOCK_PTR, offsetof (Lisp_Coding_System, methods), 1,
|
2551
|
352 { &coding_system_methods_description } },
|
1204
|
353 #define MARKED_SLOT(x) { XD_LISP_OBJECT, offsetof (Lisp_Coding_System, x) },
|
|
354 #define MARKED_SLOT_ARRAY(slot, size) \
|
|
355 { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Coding_System, slot), size },
|
|
356 #include "coding-system-slots.h"
|
2367
|
357 { XD_BLOCK_ARRAY, offsetof (Lisp_Coding_System, data), 1,
|
2551
|
358 { coding_system_extra_description_map } },
|
1204
|
359 { XD_END }
|
771
|
360 };
|
|
361
|
1204
|
362 static const struct memory_description coding_system_empty_extra_description_1[] =
|
|
363 {
|
|
364 { XD_END }
|
|
365 };
|
|
366
|
|
367 const struct sized_memory_description coding_system_empty_extra_description = {
|
|
368 0, coding_system_empty_extra_description_1
|
|
369 };
|
|
370
|
3263
|
371 #ifdef NEW_GC
|
|
372 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION ("coding-system", coding_system,
|
|
373 1, /*dumpable-flag*/
|
|
374 mark_coding_system,
|
|
375 print_coding_system,
|
|
376 0, 0, 0, coding_system_description,
|
|
377 sizeof_coding_system,
|
|
378 Lisp_Coding_System);
|
|
379 #else /* not NEW_GC */
|
934
|
380 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION ("coding-system", coding_system,
|
|
381 1, /*dumpable-flag*/
|
|
382 mark_coding_system,
|
|
383 print_coding_system,
|
|
384 finalize_coding_system,
|
|
385 0, 0, coding_system_description,
|
|
386 sizeof_coding_system,
|
|
387 Lisp_Coding_System);
|
3263
|
388 #endif /* not NEW_GC */
|
771
|
389
|
|
390 /************************************************************************/
|
|
391 /* Creating coding systems */
|
|
392 /************************************************************************/
|
|
393
|
|
394 static struct coding_system_methods *
|
|
395 decode_coding_system_type (Lisp_Object type, Error_Behavior errb)
|
428
|
396 {
|
771
|
397 int i;
|
|
398
|
|
399 for (i = 0; i < Dynarr_length (the_coding_system_type_entry_dynarr); i++)
|
428
|
400 {
|
771
|
401 if (EQ (type,
|
|
402 Dynarr_at (the_coding_system_type_entry_dynarr, i).meths->type))
|
|
403 return Dynarr_at (the_coding_system_type_entry_dynarr, i).meths;
|
428
|
404 }
|
771
|
405
|
|
406 maybe_invalid_constant ("Invalid coding system type", type,
|
|
407 Qcoding_system, errb);
|
|
408
|
|
409 return 0;
|
428
|
410 }
|
|
411
|
771
|
412 static int
|
|
413 valid_coding_system_type_p (Lisp_Object type)
|
428
|
414 {
|
771
|
415 return decode_coding_system_type (type, ERROR_ME_NOT) != 0;
|
|
416 }
|
|
417
|
|
418 DEFUN ("valid-coding-system-type-p", Fvalid_coding_system_type_p, 1, 1, 0, /*
|
|
419 Given a CODING-SYSTEM-TYPE, return non-nil if it is valid.
|
|
420 Valid types depend on how XEmacs was compiled but may include
|
3025
|
421 `undecided', `chain', `integer', `ccl', `iso2022', `big5', `shift-jis',
|
|
422 `utf-16', `ucs-4', `utf-8', etc.
|
771
|
423 */
|
|
424 (coding_system_type))
|
|
425 {
|
|
426 return valid_coding_system_type_p (coding_system_type) ? Qt : Qnil;
|
|
427 }
|
|
428
|
|
429 DEFUN ("coding-system-type-list", Fcoding_system_type_list, 0, 0, 0, /*
|
|
430 Return a list of valid coding system types.
|
|
431 */
|
|
432 ())
|
|
433 {
|
|
434 return Fcopy_sequence (Vcoding_system_type_list);
|
|
435 }
|
|
436
|
|
437 void
|
|
438 add_entry_to_coding_system_type_list (struct coding_system_methods *meths)
|
|
439 {
|
|
440 struct coding_system_type_entry entry;
|
|
441
|
|
442 entry.meths = meths;
|
|
443 Dynarr_add (the_coding_system_type_entry_dynarr, entry);
|
|
444 Vcoding_system_type_list = Fcons (meths->type, Vcoding_system_type_list);
|
428
|
445 }
|
|
446
|
|
447 DEFUN ("coding-system-p", Fcoding_system_p, 1, 1, 0, /*
|
|
448 Return t if OBJECT is a coding system.
|
|
449 A coding system is an object that defines how text containing multiple
|
|
450 character sets is encoded into a stream of (typically 8-bit) bytes.
|
|
451 The coding system is used to decode the stream into a series of
|
|
452 characters (which may be from multiple charsets) when the text is read
|
|
453 from a file or process, and is used to encode the text back into the
|
|
454 same format when it is written out to a file or process.
|
|
455
|
|
456 For example, many ISO2022-compliant coding systems (such as Compound
|
|
457 Text, which is used for inter-client data under the X Window System)
|
|
458 use escape sequences to switch between different charsets -- Japanese
|
|
459 Kanji, for example, is invoked with "ESC $ ( B"; ASCII is invoked
|
|
460 with "ESC ( B"; and Cyrillic is invoked with "ESC - L". See
|
|
461 `make-coding-system' for more information.
|
|
462
|
|
463 Coding systems are normally identified using a symbol, and the
|
|
464 symbol is accepted in place of the actual coding system object whenever
|
|
465 a coding system is called for. (This is similar to how faces work.)
|
|
466 */
|
|
467 (object))
|
|
468 {
|
|
469 return CODING_SYSTEMP (object) ? Qt : Qnil;
|
|
470 }
|
|
471
|
|
472 DEFUN ("find-coding-system", Ffind_coding_system, 1, 1, 0, /*
|
|
473 Retrieve the coding system of the given name.
|
|
474
|
|
475 If CODING-SYSTEM-OR-NAME is a coding-system object, it is simply
|
|
476 returned. Otherwise, CODING-SYSTEM-OR-NAME should be a symbol.
|
|
477 If there is no such coding system, nil is returned. Otherwise the
|
|
478 associated coding system object is returned.
|
|
479 */
|
|
480 (coding_system_or_name))
|
|
481 {
|
|
482 if (NILP (coding_system_or_name))
|
|
483 coding_system_or_name = Qbinary;
|
440
|
484 else if (CODING_SYSTEMP (coding_system_or_name))
|
|
485 return coding_system_or_name;
|
428
|
486 else
|
|
487 CHECK_SYMBOL (coding_system_or_name);
|
|
488
|
440
|
489 while (1)
|
|
490 {
|
|
491 coding_system_or_name =
|
|
492 Fgethash (coding_system_or_name, Vcoding_system_hash_table, Qnil);
|
|
493
|
771
|
494 if (CODING_SYSTEMP (coding_system_or_name)
|
|
495 || NILP (coding_system_or_name))
|
440
|
496 return coding_system_or_name;
|
|
497 }
|
428
|
498 }
|
|
499
|
|
500 DEFUN ("get-coding-system", Fget_coding_system, 1, 1, 0, /*
|
|
501 Retrieve the coding system of the given name.
|
|
502 Same as `find-coding-system' except that if there is no such
|
|
503 coding system, an error is signaled instead of returning nil.
|
|
504 */
|
|
505 (name))
|
|
506 {
|
|
507 Lisp_Object coding_system = Ffind_coding_system (name);
|
|
508
|
|
509 if (NILP (coding_system))
|
563
|
510 invalid_argument ("No such coding system", name);
|
428
|
511 return coding_system;
|
|
512 }
|
|
513
|
771
|
514 int
|
|
515 coding_system_is_binary (Lisp_Object coding_system)
|
|
516 {
|
|
517 Lisp_Coding_System *cs = XCODING_SYSTEM (coding_system);
|
|
518 return
|
|
519 (EQ (CODING_SYSTEM_TYPE (cs), Qno_conversion) &&
|
|
520 CODING_SYSTEM_EOL_TYPE (cs) == EOL_LF &&
|
|
521 EQ (CODING_SYSTEM_POST_READ_CONVERSION (cs), Qnil) &&
|
|
522 EQ (CODING_SYSTEM_PRE_WRITE_CONVERSION (cs), Qnil));
|
|
523 }
|
|
524
|
|
525 static Lisp_Object
|
|
526 coding_system_real_canonical (Lisp_Object cs)
|
|
527 {
|
|
528 if (!NILP (XCODING_SYSTEM_CANONICAL (cs)))
|
|
529 return XCODING_SYSTEM_CANONICAL (cs);
|
|
530 return cs;
|
|
531 }
|
|
532
|
|
533 /* Return true if coding system is of the "standard" type that decodes
|
|
534 bytes into characters (suitable for decoding a text file). */
|
|
535 int
|
|
536 coding_system_is_for_text_file (Lisp_Object coding_system)
|
|
537 {
|
|
538 return (XCODESYSMETH_OR_GIVEN
|
|
539 (coding_system, conversion_end_type,
|
|
540 (coding_system_real_canonical (coding_system)),
|
|
541 DECODES_BYTE_TO_CHARACTER) ==
|
|
542 DECODES_BYTE_TO_CHARACTER);
|
|
543 }
|
|
544
|
|
545 static int
|
|
546 decoding_source_sink_type_is_char (Lisp_Object cs, enum source_or_sink sex)
|
|
547 {
|
|
548 enum source_sink_type type =
|
|
549 XCODESYSMETH_OR_GIVEN (cs, conversion_end_type,
|
|
550 (coding_system_real_canonical (cs)),
|
|
551 DECODES_BYTE_TO_CHARACTER);
|
|
552 if (sex == CODING_SOURCE)
|
|
553 return (type == DECODES_CHARACTER_TO_CHARACTER ||
|
|
554 type == DECODES_CHARACTER_TO_BYTE);
|
|
555 else
|
|
556 return (type == DECODES_CHARACTER_TO_CHARACTER ||
|
|
557 type == DECODES_BYTE_TO_CHARACTER);
|
|
558 }
|
|
559
|
|
560 static int
|
|
561 encoding_source_sink_type_is_char (Lisp_Object cs, enum source_or_sink sex)
|
|
562 {
|
|
563 return decoding_source_sink_type_is_char (cs,
|
|
564 /* Sex change */
|
|
565 sex == CODING_SOURCE ?
|
|
566 CODING_SINK : CODING_SOURCE);
|
|
567 }
|
|
568
|
|
569 /* Like Ffind_coding_system() but check that the coding system is of the
|
|
570 "standard" type that decodes bytes into characters (suitable for
|
|
571 decoding a text file), and if not, returns an appropriate wrapper that
|
|
572 does. Also, if EOL_WRAP is non-zero, check whether this coding system
|
|
573 wants EOL auto-detection, and if so, wrap with a convert-eol coding
|
|
574 system to do this. */
|
|
575
|
|
576 Lisp_Object
|
|
577 find_coding_system_for_text_file (Lisp_Object name, int eol_wrap)
|
|
578 {
|
|
579 Lisp_Object coding_system = Ffind_coding_system (name);
|
|
580 Lisp_Object wrapper = coding_system;
|
|
581
|
|
582 if (NILP (coding_system))
|
|
583 return Qnil;
|
|
584 if (!coding_system_is_for_text_file (coding_system))
|
|
585 {
|
|
586 wrapper = XCODING_SYSTEM_TEXT_FILE_WRAPPER (coding_system);
|
|
587 if (NILP (wrapper))
|
|
588 {
|
|
589 Lisp_Object chain;
|
|
590 if (!decoding_source_sink_type_is_char (coding_system, CODING_SINK))
|
|
591 chain = list2 (coding_system, Qbinary);
|
|
592 else
|
|
593 chain = list1 (coding_system);
|
|
594 if (decoding_source_sink_type_is_char (coding_system, CODING_SOURCE))
|
|
595 chain = Fcons (Qbinary, chain);
|
|
596 wrapper =
|
|
597 make_internal_coding_system
|
|
598 (coding_system,
|
|
599 "internal-text-file-wrapper",
|
|
600 Qchain,
|
|
601 Qunbound, list4 (Qchain, chain,
|
|
602 Qcanonicalize_after_coding, coding_system));
|
|
603 XCODING_SYSTEM_TEXT_FILE_WRAPPER (coding_system) = wrapper;
|
|
604 }
|
|
605 }
|
|
606
|
|
607 if (!eol_wrap || XCODING_SYSTEM_EOL_TYPE (coding_system) != EOL_AUTODETECT)
|
|
608 return wrapper;
|
|
609
|
|
610 coding_system = wrapper;
|
|
611 wrapper = XCODING_SYSTEM_AUTO_EOL_WRAPPER (coding_system);
|
|
612 if (!NILP (wrapper))
|
|
613 return wrapper;
|
|
614 wrapper =
|
|
615 make_internal_coding_system
|
|
616 (coding_system,
|
|
617 "internal-auto-eol-wrapper",
|
|
618 Qundecided, Qunbound,
|
|
619 list4 (Qcoding_system, coding_system,
|
|
620 Qdo_eol, Qt));
|
|
621 XCODING_SYSTEM_AUTO_EOL_WRAPPER (coding_system) = wrapper;
|
|
622 return wrapper;
|
|
623 }
|
|
624
|
|
625 /* Like Fget_coding_system() but verify that the coding system is of the
|
|
626 "standard" type that decodes bytes into characters (suitable for
|
|
627 decoding a text file), and if not, returns an appropriate wrapper that
|
|
628 does. Also, if EOL_WRAP is non-zero, check whether this coding system
|
|
629 wants EOL auto-detection, and if so, wrap with a convert-eol coding
|
|
630 system to do this. */
|
|
631
|
|
632 Lisp_Object
|
|
633 get_coding_system_for_text_file (Lisp_Object name, int eol_wrap)
|
|
634 {
|
|
635 Lisp_Object coding_system = find_coding_system_for_text_file (name,
|
|
636 eol_wrap);
|
|
637 if (NILP (coding_system))
|
|
638 invalid_argument ("No such coding system", name);
|
|
639 return coding_system;
|
|
640 }
|
|
641
|
|
642 /* We store the coding systems in hash tables with the names as the
|
|
643 key and the actual coding system object as the value. Occasionally
|
|
644 we need to use them in a list format. These routines provide us
|
|
645 with that. */
|
428
|
646 struct coding_system_list_closure
|
|
647 {
|
|
648 Lisp_Object *coding_system_list;
|
771
|
649 int normal;
|
|
650 int internal;
|
428
|
651 };
|
|
652
|
|
653 static int
|
2286
|
654 add_coding_system_to_list_mapper (Lisp_Object key, Lisp_Object UNUSED (value),
|
428
|
655 void *coding_system_list_closure)
|
|
656 {
|
|
657 /* This function can GC */
|
|
658 struct coding_system_list_closure *cscl =
|
|
659 (struct coding_system_list_closure *) coding_system_list_closure;
|
|
660 Lisp_Object *coding_system_list = cscl->coding_system_list;
|
|
661
|
771
|
662 /* We can't just use VALUE because KEY might be an alias, and we need
|
|
663 the real coding system object. */
|
|
664 if (XCODING_SYSTEM (Ffind_coding_system (key))->internal_p ?
|
|
665 cscl->internal : cscl->normal)
|
|
666 *coding_system_list = Fcons (key, *coding_system_list);
|
428
|
667 return 0;
|
|
668 }
|
|
669
|
2297
|
670 /* #### should we specify a conventional for "all coding systems"? */
|
771
|
671 DEFUN ("coding-system-list", Fcoding_system_list, 0, 1, 0, /*
|
428
|
672 Return a list of the names of all defined coding systems.
|
771
|
673 If INTERNAL is nil, only the normal (non-internal) coding systems are
|
|
674 included. (Internal coding systems are created for various internal
|
|
675 purposes, such as implementing EOL types of CRLF and CR; generally, you do
|
|
676 not want to see these.) If it is t, only the internal coding systems are
|
|
677 included. If it is any other non-nil value both normal and internal are
|
|
678 included.
|
428
|
679 */
|
771
|
680 (internal))
|
428
|
681 {
|
|
682 Lisp_Object coding_system_list = Qnil;
|
|
683 struct gcpro gcpro1;
|
|
684 struct coding_system_list_closure coding_system_list_closure;
|
|
685
|
|
686 GCPRO1 (coding_system_list);
|
|
687 coding_system_list_closure.coding_system_list = &coding_system_list;
|
771
|
688 coding_system_list_closure.normal = !EQ (internal, Qt);
|
|
689 coding_system_list_closure.internal = !NILP (internal);
|
428
|
690 elisp_maphash (add_coding_system_to_list_mapper, Vcoding_system_hash_table,
|
|
691 &coding_system_list_closure);
|
|
692 UNGCPRO;
|
|
693
|
|
694 return coding_system_list;
|
|
695 }
|
|
696
|
|
697 DEFUN ("coding-system-name", Fcoding_system_name, 1, 1, 0, /*
|
|
698 Return the name of the given coding system.
|
|
699 */
|
|
700 (coding_system))
|
|
701 {
|
|
702 coding_system = Fget_coding_system (coding_system);
|
|
703 return XCODING_SYSTEM_NAME (coding_system);
|
|
704 }
|
|
705
|
|
706 static Lisp_Coding_System *
|
771
|
707 allocate_coding_system (struct coding_system_methods *codesys_meths,
|
|
708 Bytecount data_size,
|
|
709 Lisp_Object name)
|
428
|
710 {
|
771
|
711 Bytecount total_size = offsetof (Lisp_Coding_System, data) + data_size;
|
428
|
712 Lisp_Coding_System *codesys =
|
3024
|
713 (Lisp_Coding_System *) BASIC_ALLOC_LCRECORD (total_size,
|
1204
|
714 &lrecord_coding_system);
|
|
715
|
771
|
716 codesys->methods = codesys_meths;
|
1204
|
717 #define MARKED_SLOT(x) codesys->x = Qnil;
|
|
718 #include "coding-system-slots.h"
|
|
719
|
771
|
720 CODING_SYSTEM_EOL_TYPE (codesys) = EOL_LF;
|
|
721 CODING_SYSTEM_NAME (codesys) = name;
|
|
722
|
|
723 MAYBE_CODESYSMETH (codesys, init, (wrap_coding_system (codesys)));
|
428
|
724
|
|
725 return codesys;
|
|
726 }
|
|
727
|
771
|
728 static enum eol_type
|
|
729 symbol_to_eol_type (Lisp_Object symbol)
|
|
730 {
|
|
731 CHECK_SYMBOL (symbol);
|
|
732 if (NILP (symbol)) return EOL_AUTODETECT;
|
|
733 if (EQ (symbol, Qlf)) return EOL_LF;
|
|
734 if (EQ (symbol, Qcrlf)) return EOL_CRLF;
|
|
735 if (EQ (symbol, Qcr)) return EOL_CR;
|
|
736
|
|
737 invalid_constant ("Unrecognized eol type", symbol);
|
1204
|
738 RETURN_NOT_REACHED (EOL_AUTODETECT);
|
771
|
739 }
|
|
740
|
|
741 static Lisp_Object
|
|
742 eol_type_to_symbol (enum eol_type type)
|
|
743 {
|
|
744 switch (type)
|
|
745 {
|
2500
|
746 default: ABORT ();
|
771
|
747 case EOL_LF: return Qlf;
|
|
748 case EOL_CRLF: return Qcrlf;
|
|
749 case EOL_CR: return Qcr;
|
|
750 case EOL_AUTODETECT: return Qnil;
|
|
751 }
|
|
752 }
|
|
753
|
|
754 struct subsidiary_type
|
|
755 {
|
2367
|
756 Ascbyte *extension;
|
|
757 Ascbyte *mnemonic_ext;
|
771
|
758 enum eol_type eol;
|
|
759 };
|
|
760
|
|
761 static struct subsidiary_type coding_subsidiary_list[] =
|
|
762 { { "-unix", "", EOL_LF },
|
|
763 { "-dos", ":T", EOL_CRLF },
|
|
764 { "-mac", ":t", EOL_CR } };
|
|
765
|
|
766 /* kludge */
|
428
|
767 static void
|
771
|
768 setup_eol_coding_systems (Lisp_Object codesys)
|
428
|
769 {
|
793
|
770 int len = XSTRING_LENGTH (XSYMBOL (XCODING_SYSTEM_NAME (codesys))->name);
|
2367
|
771 Ibyte *codesys_name = alloca_ibytes (len + 7);
|
771
|
772 int mlen = -1;
|
867
|
773 Ibyte *codesys_mnemonic = 0;
|
771
|
774 Lisp_Object codesys_name_sym, sub_codesys;
|
|
775 int i;
|
|
776
|
|
777 memcpy (codesys_name,
|
793
|
778 XSTRING_DATA (XSYMBOL (XCODING_SYSTEM_NAME (codesys))->name), len);
|
771
|
779
|
|
780 if (STRINGP (XCODING_SYSTEM_MNEMONIC (codesys)))
|
428
|
781 {
|
771
|
782 mlen = XSTRING_LENGTH (XCODING_SYSTEM_MNEMONIC (codesys));
|
2367
|
783 codesys_mnemonic = alloca_ibytes (mlen + 7);
|
771
|
784 memcpy (codesys_mnemonic,
|
|
785 XSTRING_DATA (XCODING_SYSTEM_MNEMONIC (codesys)), mlen);
|
|
786 }
|
|
787
|
|
788 /* Create three "subsidiary" coding systems, decoding data encoded using
|
|
789 each of the three EOL types. We do this for each subsidiary by
|
|
790 copying the original coding system, setting the EOL type
|
|
791 appropriately, and setting the CANONICAL member of the new coding
|
|
792 system to be a chain consisting of the original coding system followed
|
|
793 by a convert-eol coding system to do the EOL decoding. For EOL type
|
|
794 LF, however, we don't need any decoding, so we skip creating a
|
|
795 CANONICAL.
|
|
796
|
|
797 If the original coding system is not a text-type coding system
|
|
798 (decodes byte->char), we need to coerce it to one by the appropriate
|
|
799 wrapping in CANONICAL. */
|
|
800
|
|
801 for (i = 0; i < countof (coding_subsidiary_list); i++)
|
|
802 {
|
2367
|
803 Ascbyte *extension = coding_subsidiary_list[i].extension;
|
|
804 Ascbyte *mnemonic_ext = coding_subsidiary_list[i].mnemonic_ext;
|
771
|
805 enum eol_type eol = coding_subsidiary_list[i].eol;
|
|
806
|
2367
|
807 qxestrcpy_ascii (codesys_name + len, extension);
|
771
|
808 codesys_name_sym = intern_int (codesys_name);
|
|
809 if (mlen != -1)
|
2367
|
810 qxestrcpy_ascii (codesys_mnemonic + mlen, mnemonic_ext);
|
771
|
811
|
|
812 sub_codesys = Fcopy_coding_system (codesys, codesys_name_sym);
|
|
813 if (mlen != -1)
|
|
814 XCODING_SYSTEM_MNEMONIC (sub_codesys) =
|
|
815 build_intstring (codesys_mnemonic);
|
|
816
|
|
817 if (eol != EOL_LF)
|
|
818 {
|
|
819 Lisp_Object chain = list2 (get_coding_system_for_text_file
|
|
820 (codesys, 0),
|
|
821 eol == EOL_CR ? Qconvert_eol_cr :
|
|
822 Qconvert_eol_crlf);
|
|
823 Lisp_Object canon =
|
|
824 make_internal_coding_system
|
|
825 (sub_codesys, "internal-subsidiary-eol-wrapper",
|
|
826 Qchain, Qunbound,
|
|
827 mlen != -1 ?
|
|
828 list6 (Qmnemonic, build_intstring (codesys_mnemonic),
|
|
829 Qchain, chain,
|
|
830 Qcanonicalize_after_coding, sub_codesys) :
|
|
831 list4 (Qchain, chain,
|
|
832 Qcanonicalize_after_coding, sub_codesys));
|
|
833 XCODING_SYSTEM_CANONICAL (sub_codesys) = canon;
|
|
834 }
|
|
835 XCODING_SYSTEM_EOL_TYPE (sub_codesys) = eol;
|
|
836 XCODING_SYSTEM_SUBSIDIARY_PARENT (sub_codesys) = codesys;
|
|
837 XCODING_SYSTEM (codesys)->eol[eol] = sub_codesys;
|
428
|
838 }
|
|
839 }
|
|
840
|
771
|
841 /* Basic function to create new coding systems. For `make-coding-system',
|
|
842 NAME-OR-EXISTING is the NAME argument, PREFIX is null, and TYPE,
|
|
843 DESCRIPTION, and PROPS are the same. All created coding systems are put
|
|
844 in a hash table indexed by NAME.
|
|
845
|
|
846 If PREFIX is a string, NAME-OR-EXISTING should specify an existing
|
|
847 coding system (or nil), and an internal coding system will be created.
|
|
848 The name of the coding system will be constructed by combining PREFIX
|
|
849 with the name of the existing coding system (if given), and a number
|
|
850 will be appended to insure uniqueness. In such a case, if Qunbound is
|
|
851 given for DESCRIPTION, the description gets created based on the
|
|
852 generated name. Also, if no mnemonic is given in the properties list, a
|
|
853 mnemonic is created based on the generated name.
|
|
854
|
|
855 For internal coding systems, the coding system is marked as internal
|
|
856 (see `coding-system-list'), and no subsidiaries will be created or
|
|
857 eol-wrapping will happen. Otherwise:
|
|
858
|
|
859 -- if the eol-type property is `lf' or t, the coding system is merely
|
|
860 created and returned. (For t, the coding system will be wrapped with
|
|
861 an EOL autodetector when it's used to read a file.)
|
|
862
|
|
863 -- if eol-type is `crlf' or `cr', after the coding system object is
|
|
864 created, it will be wrapped in a chain with the appropriate
|
|
865 convert-eol coding system (either `convert-eol-crlf' or
|
|
866 `convert-eol-cr'), so that CRLF->LF or CR->LF conversion is done at
|
|
867 decoding time, and the opposite at encoding time. The resulting
|
|
868 chain becomes the CANONICAL field of the coding system object.
|
|
869
|
|
870 -- if eol-type is nil or omitted, "subsidiaries" are generated: Three
|
|
871 coding systems where the original coding system (before wrapping with
|
|
872 convert-eol-autodetect) is either unwrapped or wrapped with
|
|
873 convert-eol-crlf or convert-eol-cr, respectively, so that coding systems
|
|
874 to handle LF, CRLF, and CR end-of-line indicators are created. (This
|
|
875 crazy crap is based on existing behavior in other Mule versions,
|
|
876 including FSF Emacs.)
|
|
877 */
|
428
|
878
|
|
879 static Lisp_Object
|
2367
|
880 make_coding_system_1 (Lisp_Object name_or_existing, Ascbyte *prefix,
|
771
|
881 Lisp_Object type, Lisp_Object description,
|
|
882 Lisp_Object props)
|
428
|
883 {
|
771
|
884 Lisp_Coding_System *cs;
|
|
885 int need_to_setup_eol_systems = 1;
|
|
886 enum eol_type eol_wrapper = EOL_AUTODETECT;
|
|
887 struct coding_system_methods *meths;
|
|
888 Lisp_Object csobj;
|
|
889 Lisp_Object defmnem = Qnil;
|
|
890
|
|
891 if (NILP (type))
|
|
892 type = Qundecided;
|
|
893 meths = decode_coding_system_type (type, ERROR_ME);
|
|
894
|
|
895 if (prefix)
|
428
|
896 {
|
867
|
897 Ibyte *newname =
|
771
|
898 emacs_sprintf_malloc (NULL, "%s-%s-%d",
|
|
899 prefix,
|
867
|
900 NILP (name_or_existing) ? (Ibyte *) "nil" :
|
771
|
901 XSTRING_DATA (Fsymbol_name (XCODING_SYSTEM_NAME
|
|
902 (name_or_existing))),
|
|
903 ++coding_system_tick);
|
|
904 name_or_existing = intern_int (newname);
|
1726
|
905 xfree (newname, Ibyte *);
|
771
|
906
|
|
907 if (UNBOUNDP (description))
|
|
908 {
|
|
909 newname =
|
|
910 emacs_sprintf_malloc
|
|
911 (NULL, "For Internal Use (%s)",
|
|
912 XSTRING_DATA (Fsymbol_name (name_or_existing)));
|
|
913 description = build_intstring (newname);
|
1726
|
914 xfree (newname, Ibyte *);
|
771
|
915 }
|
|
916
|
|
917 newname = emacs_sprintf_malloc (NULL, "Int%d", coding_system_tick);
|
|
918 defmnem = build_intstring (newname);
|
1726
|
919 xfree (newname, Ibyte *);
|
428
|
920 }
|
771
|
921 else
|
|
922 CHECK_SYMBOL (name_or_existing);
|
|
923
|
|
924 if (!NILP (Ffind_coding_system (name_or_existing)))
|
|
925 invalid_operation ("Cannot redefine existing coding system",
|
|
926 name_or_existing);
|
|
927
|
|
928 cs = allocate_coding_system (meths, meths->extra_data_size,
|
|
929 name_or_existing);
|
793
|
930 csobj = wrap_coding_system (cs);
|
771
|
931
|
|
932 cs->internal_p = !!prefix;
|
|
933
|
|
934 if (NILP (description))
|
|
935 description = build_string ("");
|
|
936 else
|
|
937 CHECK_STRING (description);
|
|
938 CODING_SYSTEM_DESCRIPTION (cs) = description;
|
|
939
|
|
940 if (!NILP (defmnem))
|
|
941 CODING_SYSTEM_MNEMONIC (cs) = defmnem;
|
|
942
|
|
943 {
|
|
944 EXTERNAL_PROPERTY_LIST_LOOP_3 (key, value, props)
|
|
945 {
|
|
946 int recognized = 1;
|
|
947
|
|
948 if (EQ (key, Qmnemonic))
|
|
949 {
|
|
950 if (!NILP (value))
|
|
951 CHECK_STRING (value);
|
|
952 CODING_SYSTEM_MNEMONIC (cs) = value;
|
|
953 }
|
|
954
|
|
955 else if (EQ (key, Qdocumentation))
|
|
956 {
|
|
957 if (!NILP (value))
|
|
958 CHECK_STRING (value);
|
|
959 CODING_SYSTEM_DOCUMENTATION (cs) = value;
|
|
960 }
|
|
961
|
|
962 else if (EQ (key, Qeol_type))
|
|
963 {
|
|
964 need_to_setup_eol_systems = NILP (value);
|
|
965 if (EQ (value, Qt))
|
|
966 value = Qnil;
|
|
967 eol_wrapper = symbol_to_eol_type (value);
|
|
968 }
|
|
969
|
|
970 else if (EQ (key, Qpost_read_conversion))
|
|
971 CODING_SYSTEM_POST_READ_CONVERSION (cs) = value;
|
|
972 else if (EQ (key, Qpre_write_conversion))
|
|
973 CODING_SYSTEM_PRE_WRITE_CONVERSION (cs) = value;
|
|
974 /* FSF compatibility */
|
|
975 else if (EQ (key, Qtranslation_table_for_decode))
|
|
976 ;
|
|
977 else if (EQ (key, Qtranslation_table_for_encode))
|
|
978 ;
|
|
979 else if (EQ (key, Qsafe_chars))
|
|
980 ;
|
|
981 else if (EQ (key, Qsafe_charsets))
|
|
982 ;
|
|
983 else if (EQ (key, Qmime_charset))
|
|
984 ;
|
|
985 else if (EQ (key, Qvalid_codes))
|
|
986 ;
|
|
987 else
|
|
988 recognized = CODESYSMETH_OR_GIVEN (cs, putprop,
|
|
989 (csobj, key, value), 0);
|
|
990
|
|
991 if (!recognized)
|
|
992 invalid_constant ("Unrecognized property", key);
|
|
993 }
|
|
994 }
|
|
995
|
|
996 {
|
|
997 XCODING_SYSTEM_CANONICAL (csobj) =
|
|
998 CODESYSMETH_OR_GIVEN (cs, canonicalize, (csobj), Qnil);
|
|
999 XCODING_SYSTEM_EOL_TYPE (csobj) = EOL_AUTODETECT; /* for copy-coding-system
|
|
1000 below */
|
|
1001
|
|
1002 if (need_to_setup_eol_systems && !cs->internal_p)
|
|
1003 setup_eol_coding_systems (csobj);
|
|
1004 else if (eol_wrapper == EOL_CR || eol_wrapper == EOL_CRLF)
|
|
1005 {
|
|
1006 /* If a specific eol-type (other than LF) was specified, we handle
|
|
1007 this by converting the coding system into a chain that wraps the
|
|
1008 coding system along with a convert-eol system after it, in
|
|
1009 exactly that same switcheroo fashion that the normal
|
|
1010 canonicalize method works -- BUT we will run into a problem if
|
|
1011 we do it the obvious way, because when `chain' creates its
|
|
1012 substreams, the substream containing the coding system we're
|
|
1013 creating will have canonicalization expansion done on it,
|
|
1014 leading to infinite recursion. So we have to generate a new,
|
|
1015 internal coding system with the previous value of CANONICAL. */
|
867
|
1016 Ibyte *newname =
|
771
|
1017 emacs_sprintf_malloc
|
|
1018 (NULL, "internal-eol-copy-%s-%d",
|
|
1019 XSTRING_DATA (Fsymbol_name (name_or_existing)),
|
|
1020 ++coding_system_tick);
|
|
1021 Lisp_Object newnamesym = intern_int (newname);
|
|
1022 Lisp_Object copied = Fcopy_coding_system (csobj, newnamesym);
|
1726
|
1023 xfree (newname, Ibyte *);
|
771
|
1024
|
|
1025 XCODING_SYSTEM_CANONICAL (csobj) =
|
|
1026 make_internal_coding_system
|
|
1027 (csobj,
|
|
1028 "internal-eol-wrapper",
|
|
1029 Qchain, Qunbound,
|
|
1030 list4 (Qchain,
|
|
1031 list2 (copied,
|
|
1032 eol_wrapper == EOL_CR ?
|
|
1033 Qconvert_eol_cr :
|
|
1034 Qconvert_eol_crlf),
|
|
1035 Qcanonicalize_after_coding,
|
|
1036 csobj));
|
|
1037 }
|
|
1038 XCODING_SYSTEM_EOL_TYPE (csobj) = eol_wrapper;
|
|
1039 }
|
|
1040
|
|
1041 Fputhash (name_or_existing, csobj, Vcoding_system_hash_table);
|
|
1042
|
|
1043 return csobj;
|
428
|
1044 }
|
|
1045
|
771
|
1046 Lisp_Object
|
2367
|
1047 make_internal_coding_system (Lisp_Object existing, Ascbyte *prefix,
|
771
|
1048 Lisp_Object type, Lisp_Object description,
|
|
1049 Lisp_Object props)
|
|
1050 {
|
|
1051 return make_coding_system_1 (existing, prefix, type, description, props);
|
|
1052 }
|
428
|
1053
|
|
1054 DEFUN ("make-coding-system", Fmake_coding_system, 2, 4, 0, /*
|
|
1055 Register symbol NAME as a coding system.
|
|
1056
|
|
1057 TYPE describes the conversion method used and should be one of
|
|
1058
|
3025
|
1059 nil or `undecided'
|
428
|
1060 Automatic conversion. XEmacs attempts to detect the coding system
|
|
1061 used in the file.
|
3025
|
1062 `chain'
|
771
|
1063 Chain two or more coding systems together to make a combination coding
|
|
1064 system.
|
3025
|
1065 `no-conversion'
|
428
|
1066 No conversion. Use this for binary files and such. On output,
|
|
1067 graphic characters that are not in ASCII or Latin-1 will be
|
|
1068 replaced by a ?. (For a no-conversion-encoded buffer, these
|
|
1069 characters will only be present if you explicitly insert them.)
|
3025
|
1070 `convert-eol'
|
771
|
1071 Convert CRLF sequences or CR to LF.
|
3025
|
1072 `shift-jis'
|
428
|
1073 Shift-JIS (a Japanese encoding commonly used in PC operating systems).
|
3025
|
1074 `unicode'
|
771
|
1075 Any Unicode encoding (UCS-4, UTF-8, UTF-16, etc.).
|
3025
|
1076 `mswindows-unicode-to-multibyte'
|
771
|
1077 (MS Windows only) Converts from Windows Unicode to Windows Multibyte
|
|
1078 (any code page encoding) upon encoding, and the other way upon decoding.
|
3025
|
1079 `mswindows-multibyte'
|
771
|
1080 Converts to or from Windows Multibyte (any code page encoding).
|
|
1081 This is resolved into a chain of `mswindows-unicode' and
|
|
1082 `mswindows-unicode-to-multibyte'.
|
3025
|
1083 `iso2022'
|
428
|
1084 Any ISO2022-compliant encoding. Among other things, this includes
|
|
1085 JIS (the Japanese encoding commonly used for e-mail), EUC (the
|
|
1086 standard Unix encoding for Japanese and other languages), and
|
|
1087 Compound Text (the encoding used in X11). You can specify more
|
442
|
1088 specific information about the conversion with the PROPS argument.
|
3025
|
1089 `big5'
|
2819
|
1090 Big5 (the encoding commonly used for Mandarin Chinese in Taiwan).
|
3025
|
1091 `ccl'
|
428
|
1092 The conversion is performed using a user-written pseudo-code
|
|
1093 program. CCL (Code Conversion Language) is the name of this
|
|
1094 pseudo-code.
|
3025
|
1095 `gzip'
|
771
|
1096 GZIP compression format.
|
3025
|
1097 `internal'
|
428
|
1098 Write out or read in the raw contents of the memory representing
|
|
1099 the buffer's text. This is primarily useful for debugging
|
|
1100 purposes, and is only enabled when XEmacs has been compiled with
|
|
1101 DEBUG_XEMACS defined (via the --debug configure option).
|
3025
|
1102 WARNING: Reading in a file using `internal' conversion can result
|
428
|
1103 in an internal inconsistency in the memory representing a
|
|
1104 buffer's text, which will produce unpredictable results and may
|
|
1105 cause XEmacs to crash. Under normal circumstances you should
|
3025
|
1106 never use `internal' conversion.
|
428
|
1107
|
771
|
1108 DESCRIPTION is a short English phrase describing the coding system,
|
|
1109 suitable for use as a menu item. (See also the `documentation' property
|
|
1110 below.)
|
428
|
1111
|
|
1112 PROPS is a property list, describing the specific nature of the
|
|
1113 character set. Recognized properties are:
|
|
1114
|
3025
|
1115 `mnemonic'
|
428
|
1116 String to be displayed in the modeline when this coding system is
|
|
1117 active.
|
|
1118
|
3025
|
1119 `documentation'
|
771
|
1120 Detailed documentation on the coding system.
|
|
1121
|
3025
|
1122 `eol-type'
|
428
|
1123 End-of-line conversion to be used. It should be one of
|
|
1124
|
|
1125 nil
|
|
1126 Automatically detect the end-of-line type (LF, CRLF,
|
|
1127 or CR). Also generate subsidiary coding systems named
|
|
1128 `NAME-unix', `NAME-dos', and `NAME-mac', that are
|
|
1129 identical to this coding system but have an EOL-TYPE
|
3025
|
1130 value of `lf', `crlf', and `cr', respectively.
|
|
1131 `lf'
|
428
|
1132 The end of a line is marked externally using ASCII LF.
|
|
1133 Since this is also the way that XEmacs represents an
|
|
1134 end-of-line internally, specifying this option results
|
|
1135 in no end-of-line conversion. This is the standard
|
|
1136 format for Unix text files.
|
3025
|
1137 `crlf'
|
428
|
1138 The end of a line is marked externally using ASCII
|
|
1139 CRLF. This is the standard format for MS-DOS text
|
|
1140 files.
|
3025
|
1141 `cr'
|
428
|
1142 The end of a line is marked externally using ASCII CR.
|
|
1143 This is the standard format for Macintosh text files.
|
|
1144 t
|
|
1145 Automatically detect the end-of-line type but do not
|
|
1146 generate subsidiary coding systems. (This value is
|
|
1147 converted to nil when stored internally, and
|
|
1148 `coding-system-property' will return nil.)
|
|
1149
|
3025
|
1150 `post-read-conversion'
|
771
|
1151 The value is a function to call after some text is inserted and
|
|
1152 decoded by the coding system itself and before any functions in
|
|
1153 `after-change-functions' are called. (#### Not actually true in
|
|
1154 XEmacs. `after-change-functions' will be called twice if
|
|
1155 `post-read-conversion' changes something.) The argument of this
|
|
1156 function is the same as for a function in
|
|
1157 `after-insert-file-functions', i.e. LENGTH of the text inserted,
|
|
1158 with point at the head of the text to be decoded.
|
428
|
1159
|
3025
|
1160 `pre-write-conversion'
|
771
|
1161 The value is a function to call after all functions in
|
|
1162 `write-region-annotate-functions' and `buffer-file-format' are
|
|
1163 called, and before the text is encoded by the coding system itself.
|
|
1164 The arguments to this function are the same as those of a function
|
|
1165 in `write-region-annotate-functions', i.e. FROM and TO, specifying
|
|
1166 a region of text.
|
|
1167
|
|
1168
|
|
1169
|
|
1170 The following properties are allowed for FSF compatibility but currently
|
|
1171 ignored:
|
|
1172
|
3025
|
1173 `translation-table-for-decode'
|
771
|
1174 The value is a translation table to be applied on decoding. See
|
|
1175 the function `make-translation-table' for the format of translation
|
|
1176 table. This is not applicable to CCL-based coding systems.
|
|
1177
|
3025
|
1178 `translation-table-for-encode'
|
771
|
1179 The value is a translation table to be applied on encoding. This is
|
|
1180 not applicable to CCL-based coding systems.
|
|
1181
|
3025
|
1182 `safe-chars'
|
771
|
1183 The value is a char table. If a character has non-nil value in it,
|
|
1184 the character is safely supported by the coding system. This
|
|
1185 overrides the specification of safe-charsets.
|
|
1186
|
3025
|
1187 `safe-charsets'
|
771
|
1188 The value is a list of charsets safely supported by the coding
|
|
1189 system. The value t means that all charsets Emacs handles are
|
|
1190 supported. Even if some charset is not in this list, it doesn't
|
|
1191 mean that the charset can't be encoded in the coding system;
|
|
1192 it just means that some other receiver of text encoded
|
|
1193 in the coding system won't be able to handle that charset.
|
|
1194
|
3025
|
1195 `mime-charset'
|
771
|
1196 The value is a symbol of which name is `MIME-charset' parameter of
|
|
1197 the coding system.
|
|
1198
|
3025
|
1199 `valid-codes' (meaningful only for a coding system based on CCL)
|
771
|
1200 The value is a list to indicate valid byte ranges of the encoded
|
|
1201 file. Each element of the list is an integer or a cons of integer.
|
|
1202 In the former case, the integer value is a valid byte code. In the
|
|
1203 latter case, the integers specifies the range of valid byte codes.
|
|
1204
|
|
1205
|
|
1206
|
3025
|
1207 The following additional property is recognized if TYPE is `convert-eol':
|
|
1208
|
|
1209 `subtype'
|
793
|
1210 One of `lf', `crlf', `cr' or nil (for autodetection). When decoding,
|
|
1211 the corresponding sequence will be converted to LF. When encoding,
|
|
1212 the opposite happens. This coding system converts characters to
|
771
|
1213 characters.
|
|
1214
|
428
|
1215
|
|
1216
|
3025
|
1217 The following additional properties are recognized if TYPE is `iso2022':
|
|
1218
|
|
1219 `charset-g0'
|
|
1220 `charset-g1'
|
|
1221 `charset-g2'
|
|
1222 `charset-g3'
|
428
|
1223 The character set initially designated to the G0 - G3 registers.
|
|
1224 The value should be one of
|
|
1225
|
|
1226 -- A charset object (designate that character set)
|
|
1227 -- nil (do not ever use this register)
|
|
1228 -- t (no character set is initially designated to
|
|
1229 the register, but may be later on; this automatically
|
|
1230 sets the corresponding `force-g*-on-output' property)
|
|
1231
|
3025
|
1232 `force-g0-on-output'
|
|
1233 `force-g1-on-output'
|
|
1234 `force-g2-on-output'
|
|
1235 `force-g2-on-output'
|
428
|
1236 If non-nil, send an explicit designation sequence on output before
|
|
1237 using the specified register.
|
|
1238
|
3025
|
1239 `short'
|
428
|
1240 If non-nil, use the short forms "ESC $ @", "ESC $ A", and
|
|
1241 "ESC $ B" on output in place of the full designation sequences
|
|
1242 "ESC $ ( @", "ESC $ ( A", and "ESC $ ( B".
|
|
1243
|
3025
|
1244 `no-ascii-eol'
|
428
|
1245 If non-nil, don't designate ASCII to G0 at each end of line on output.
|
|
1246 Setting this to non-nil also suppresses other state-resetting that
|
|
1247 normally happens at the end of a line.
|
|
1248
|
3025
|
1249 `no-ascii-cntl'
|
428
|
1250 If non-nil, don't designate ASCII to G0 before control chars on output.
|
|
1251
|
3025
|
1252 `seven'
|
428
|
1253 If non-nil, use 7-bit environment on output. Otherwise, use 8-bit
|
|
1254 environment.
|
|
1255
|
3025
|
1256 `lock-shift'
|
428
|
1257 If non-nil, use locking-shift (SO/SI) instead of single-shift
|
|
1258 or designation by escape sequence.
|
|
1259
|
3025
|
1260 `no-iso6429'
|
428
|
1261 If non-nil, don't use ISO6429's direction specification.
|
|
1262
|
3025
|
1263 `escape-quoted'
|
428
|
1264 If non-nil, literal control characters that are the same as
|
|
1265 the beginning of a recognized ISO2022 or ISO6429 escape sequence
|
|
1266 (in particular, ESC (0x1B), SO (0x0E), SI (0x0F), SS2 (0x8E),
|
|
1267 SS3 (0x8F), and CSI (0x9B)) are "quoted" with an escape character
|
|
1268 so that they can be properly distinguished from an escape sequence.
|
|
1269 (Note that doing this results in a non-portable encoding.) This
|
|
1270 encoding flag is used for byte-compiled files. Note that ESC
|
|
1271 is a good choice for a quoting character because there are no
|
|
1272 escape sequences whose second byte is a character from the Control-0
|
|
1273 or Control-1 character sets; this is explicitly disallowed by the
|
|
1274 ISO2022 standard.
|
|
1275
|
3025
|
1276 `input-charset-conversion'
|
428
|
1277 A list of conversion specifications, specifying conversion of
|
|
1278 characters in one charset to another when decoding is performed.
|
|
1279 Each specification is a list of two elements: the source charset,
|
|
1280 and the destination charset.
|
|
1281
|
3025
|
1282 `output-charset-conversion'
|
428
|
1283 A list of conversion specifications, specifying conversion of
|
|
1284 characters in one charset to another when encoding is performed.
|
|
1285 The form of each specification is the same as for
|
3025
|
1286 `input-charset-conversion'.
|
428
|
1287
|
|
1288
|
771
|
1289
|
428
|
1290 The following additional properties are recognized (and required)
|
3025
|
1291 if TYPE is `ccl':
|
|
1292
|
|
1293 `decode'
|
428
|
1294 CCL program used for decoding (converting to internal format).
|
|
1295
|
3025
|
1296 `encode'
|
428
|
1297 CCL program used for encoding (converting to external format).
|
771
|
1298
|
|
1299
|
3025
|
1300 The following additional properties are recognized if TYPE is `chain':
|
|
1301
|
|
1302 `chain'
|
771
|
1303 List of coding systems to be chained together, in decoding order.
|
|
1304
|
3025
|
1305 `canonicalize-after-coding'
|
771
|
1306 Coding system to be returned by the detector routines in place of
|
|
1307 this coding system.
|
|
1308
|
|
1309
|
|
1310
|
3025
|
1311 The following additional properties are recognized if TYPE is `unicode':
|
|
1312
|
3767
|
1313 `unicode-type'
|
771
|
1314 One of `utf-16', `utf-8', `ucs-4', or `utf-7' (the latter is not
|
|
1315 yet implemented). `utf-16' is the basic two-byte encoding;
|
|
1316 `ucs-4' is the four-byte encoding; `utf-8' is an ASCII-compatible
|
|
1317 variable-width 8-bit encoding; `utf-7' is a 7-bit encoding using
|
|
1318 only characters that will safely pass through all mail gateways.
|
2297
|
1319 [[ This should be \"transformation format\". There should also be
|
|
1320 `ucs-2' (or `bmp' -- no surrogates) and `utf-32' (range checked). ]]
|
771
|
1321
|
3025
|
1322 `little-endian'
|
771
|
1323 If non-nil, `utf-16' and `ucs-4' will write out the groups of two
|
|
1324 or four bytes little-endian instead of big-endian. This is required,
|
|
1325 for example, under Windows.
|
|
1326
|
3025
|
1327 `need-bom'
|
771
|
1328 If non-nil, a byte order mark (BOM, or Unicode FFFE) should be
|
|
1329 written out at the beginning of the data. This serves both to
|
|
1330 identify the endianness of the following data and to mark the
|
|
1331 data as Unicode (at least, this is how Windows uses it).
|
2297
|
1332 [[ The correct term is \"signature\", since this technique may also
|
|
1333 be used with UTF-8. That is the term used in the standard. ]]
|
771
|
1334
|
|
1335
|
|
1336 The following additional properties are recognized if TYPE is
|
3025
|
1337 `mswindows-multibyte':
|
|
1338
|
|
1339 `code-page'
|
771
|
1340 Either a number (specifying a particular code page) or one of the
|
|
1341 symbols `ansi', `oem', `mac', or `ebcdic', specifying the ANSI,
|
|
1342 OEM, Macintosh, or EBCDIC code page associated with a particular
|
|
1343 locale (given by the `locale' property). NOTE: EBCDIC code pages
|
|
1344 only exist in Windows 2000 and later.
|
|
1345
|
3025
|
1346 `locale'
|
771
|
1347 If `code-page' is a symbol, this specifies the locale whose code
|
|
1348 page of the corresponding type should be used. This should be
|
|
1349 one of the following: A cons of two strings, (LANGUAGE
|
|
1350 . SUBLANGUAGE) (see `mswindows-set-current-locale'); a string (a
|
|
1351 language; SUBLANG_DEFAULT, i.e. the default sublanguage, is
|
|
1352 used); or one of the symbols `current', `user-default', or
|
|
1353 `system-default', corresponding to the values of
|
|
1354 `mswindows-current-locale', `mswindows-user-default-locale', or
|
|
1355 `mswindows-system-default-locale', respectively.
|
|
1356
|
|
1357
|
|
1358
|
3025
|
1359 The following additional properties are recognized if TYPE is `undecided':
|
2297
|
1360 [[ Doesn't GNU use \"detect-*\" for the following two? ]]
|
771
|
1361
|
3025
|
1362 `do-eol'
|
771
|
1363 Do EOL detection.
|
|
1364
|
3025
|
1365 `do-coding'
|
771
|
1366 Do encoding detection.
|
|
1367
|
3025
|
1368 `coding-system'
|
771
|
1369 If encoding detection is not done, use the specified coding system
|
|
1370 to do decoding. This is used internally when implementing coding
|
|
1371 systems with an EOL type that specifies autodetection (the default),
|
|
1372 so that the detector routines return the proper subsidiary.
|
|
1373
|
|
1374
|
|
1375
|
3025
|
1376 The following additional property is recognized if TYPE is `gzip':
|
|
1377
|
|
1378 `level'
|
771
|
1379 Compression level: 0 through 9, or `default' (currently 6).
|
|
1380
|
428
|
1381 */
|
771
|
1382 (name, type, description, props))
|
428
|
1383 {
|
771
|
1384 return make_coding_system_1 (name, 0, type, description, props);
|
428
|
1385 }
|
|
1386
|
|
1387 DEFUN ("copy-coding-system", Fcopy_coding_system, 2, 2, 0, /*
|
|
1388 Copy OLD-CODING-SYSTEM to NEW-NAME.
|
|
1389 If NEW-NAME does not name an existing coding system, a new one will
|
|
1390 be created.
|
771
|
1391 If you are using this function to create an alias, think again:
|
|
1392 Use `define-coding-system-alias' instead.
|
428
|
1393 */
|
|
1394 (old_coding_system, new_name))
|
|
1395 {
|
|
1396 Lisp_Object new_coding_system;
|
|
1397 old_coding_system = Fget_coding_system (old_coding_system);
|
771
|
1398 new_coding_system =
|
|
1399 UNBOUNDP (new_name) ? Qnil : Ffind_coding_system (new_name);
|
428
|
1400 if (NILP (new_coding_system))
|
|
1401 {
|
793
|
1402 new_coding_system =
|
|
1403 wrap_coding_system
|
|
1404 (allocate_coding_system
|
|
1405 (XCODING_SYSTEM (old_coding_system)->methods,
|
|
1406 XCODING_SYSTEM (old_coding_system)->methods->extra_data_size,
|
|
1407 new_name));
|
771
|
1408 if (!UNBOUNDP (new_name))
|
|
1409 Fputhash (new_name, new_coding_system, Vcoding_system_hash_table);
|
428
|
1410 }
|
771
|
1411 else if (XCODING_SYSTEM (old_coding_system)->methods !=
|
|
1412 XCODING_SYSTEM (new_coding_system)->methods)
|
|
1413 invalid_operation_2 ("Coding systems not same type",
|
|
1414 old_coding_system, new_coding_system);
|
428
|
1415
|
|
1416 {
|
|
1417 Lisp_Coding_System *to = XCODING_SYSTEM (new_coding_system);
|
|
1418 Lisp_Coding_System *from = XCODING_SYSTEM (old_coding_system);
|
3017
|
1419 COPY_SIZED_LCRECORD (to, from, sizeof_coding_system (from));
|
428
|
1420 to->name = new_name;
|
|
1421 }
|
|
1422 return new_coding_system;
|
|
1423 }
|
|
1424
|
771
|
1425 DEFUN ("coding-system-canonical-name-p", Fcoding_system_canonical_name_p,
|
|
1426 1, 1, 0, /*
|
440
|
1427 Return t if OBJECT names a coding system, and is not a coding system alias.
|
428
|
1428 */
|
440
|
1429 (object))
|
|
1430 {
|
|
1431 return CODING_SYSTEMP (Fgethash (object, Vcoding_system_hash_table, Qnil))
|
|
1432 ? Qt : Qnil;
|
|
1433 }
|
|
1434
|
2297
|
1435 /* #### Shouldn't this really be a find/get pair? */
|
|
1436
|
440
|
1437 DEFUN ("coding-system-alias-p", Fcoding_system_alias_p, 1, 1, 0, /*
|
|
1438 Return t if OBJECT is a coding system alias.
|
|
1439 All coding system aliases are created by `define-coding-system-alias'.
|
|
1440 */
|
|
1441 (object))
|
428
|
1442 {
|
440
|
1443 return SYMBOLP (Fgethash (object, Vcoding_system_hash_table, Qzero))
|
|
1444 ? Qt : Qnil;
|
|
1445 }
|
|
1446
|
|
1447 DEFUN ("coding-system-aliasee", Fcoding_system_aliasee, 1, 1, 0, /*
|
|
1448 Return the coding-system symbol for which symbol ALIAS is an alias.
|
|
1449 */
|
|
1450 (alias))
|
|
1451 {
|
|
1452 Lisp_Object aliasee = Fgethash (alias, Vcoding_system_hash_table, Qnil);
|
|
1453 if (SYMBOLP (aliasee))
|
|
1454 return aliasee;
|
|
1455 else
|
563
|
1456 invalid_argument ("Symbol is not a coding system alias", alias);
|
1204
|
1457 RETURN_NOT_REACHED (Qnil);
|
440
|
1458 }
|
|
1459
|
|
1460 /* A maphash function, for removing dangling coding system aliases. */
|
|
1461 static int
|
2286
|
1462 dangling_coding_system_alias_p (Lisp_Object UNUSED (alias),
|
440
|
1463 Lisp_Object aliasee,
|
|
1464 void *dangling_aliases)
|
|
1465 {
|
|
1466 if (SYMBOLP (aliasee)
|
|
1467 && NILP (Fgethash (aliasee, Vcoding_system_hash_table, Qnil)))
|
428
|
1468 {
|
440
|
1469 (*(int *) dangling_aliases)++;
|
|
1470 return 1;
|
428
|
1471 }
|
440
|
1472 else
|
|
1473 return 0;
|
|
1474 }
|
|
1475
|
|
1476 DEFUN ("define-coding-system-alias", Fdefine_coding_system_alias, 2, 2, 0, /*
|
|
1477 Define symbol ALIAS as an alias for coding system ALIASEE.
|
|
1478
|
|
1479 You can use this function to redefine an alias that has already been defined,
|
|
1480 but you cannot redefine a name which is the canonical name for a coding system.
|
|
1481 \(a canonical name of a coding system is what is returned when you call
|
|
1482 `coding-system-name' on a coding system).
|
|
1483
|
|
1484 ALIASEE itself can be an alias, which allows you to define nested aliases.
|
|
1485
|
|
1486 You are forbidden, however, from creating alias loops or `dangling' aliases.
|
|
1487 These will be detected, and an error will be signaled if you attempt to do so.
|
|
1488
|
|
1489 If ALIASEE is nil, then ALIAS will simply be undefined.
|
|
1490
|
|
1491 See also `coding-system-alias-p', `coding-system-aliasee',
|
|
1492 and `coding-system-canonical-name-p'.
|
|
1493 */
|
|
1494 (alias, aliasee))
|
|
1495 {
|
2286
|
1496 Lisp_Object probe;
|
440
|
1497
|
|
1498 CHECK_SYMBOL (alias);
|
|
1499
|
|
1500 if (!NILP (Fcoding_system_canonical_name_p (alias)))
|
563
|
1501 invalid_change
|
440
|
1502 ("Symbol is the canonical name of a coding system and cannot be redefined",
|
|
1503 alias);
|
|
1504
|
|
1505 if (NILP (aliasee))
|
|
1506 {
|
771
|
1507 Lisp_Object subsidiary_unix = add_suffix_to_symbol (alias, "-unix");
|
|
1508 Lisp_Object subsidiary_dos = add_suffix_to_symbol (alias, "-dos");
|
|
1509 Lisp_Object subsidiary_mac = add_suffix_to_symbol (alias, "-mac");
|
440
|
1510
|
|
1511 Fremhash (alias, Vcoding_system_hash_table);
|
|
1512
|
|
1513 /* Undefine subsidiary aliases,
|
|
1514 presumably created by a previous call to this function */
|
|
1515 if (! NILP (Fcoding_system_alias_p (subsidiary_unix)) &&
|
|
1516 ! NILP (Fcoding_system_alias_p (subsidiary_dos)) &&
|
|
1517 ! NILP (Fcoding_system_alias_p (subsidiary_mac)))
|
|
1518 {
|
|
1519 Fdefine_coding_system_alias (subsidiary_unix, Qnil);
|
|
1520 Fdefine_coding_system_alias (subsidiary_dos, Qnil);
|
|
1521 Fdefine_coding_system_alias (subsidiary_mac, Qnil);
|
|
1522 }
|
|
1523
|
|
1524 /* Undefine dangling coding system aliases. */
|
|
1525 {
|
|
1526 int dangling_aliases;
|
|
1527
|
|
1528 do {
|
|
1529 dangling_aliases = 0;
|
|
1530 elisp_map_remhash (dangling_coding_system_alias_p,
|
|
1531 Vcoding_system_hash_table,
|
|
1532 &dangling_aliases);
|
|
1533 } while (dangling_aliases > 0);
|
|
1534 }
|
|
1535
|
|
1536 return Qnil;
|
|
1537 }
|
|
1538
|
|
1539 if (CODING_SYSTEMP (aliasee))
|
|
1540 aliasee = XCODING_SYSTEM_NAME (aliasee);
|
|
1541
|
|
1542 /* Checks that aliasee names a coding-system */
|
2286
|
1543 (void) Fget_coding_system (aliasee);
|
440
|
1544
|
|
1545 /* Check for coding system alias loops */
|
|
1546 if (EQ (alias, aliasee))
|
563
|
1547 alias_loop: invalid_operation_2
|
440
|
1548 ("Attempt to create a coding system alias loop", alias, aliasee);
|
|
1549
|
|
1550 for (probe = aliasee;
|
|
1551 SYMBOLP (probe);
|
|
1552 probe = Fgethash (probe, Vcoding_system_hash_table, Qzero))
|
|
1553 {
|
|
1554 if (EQ (probe, alias))
|
|
1555 goto alias_loop;
|
|
1556 }
|
|
1557
|
|
1558 Fputhash (alias, aliasee, Vcoding_system_hash_table);
|
|
1559
|
|
1560 /* Set up aliases for subsidiaries.
|
2297
|
1561 #### There must be a better way to handle subsidiary coding systems.
|
|
1562 Inquiring Minds Want To Know: shouldn't they always be chains? */
|
440
|
1563 {
|
|
1564 static const char *suffixes[] = { "-unix", "-dos", "-mac" };
|
|
1565 int i;
|
|
1566 for (i = 0; i < countof (suffixes); i++)
|
|
1567 {
|
|
1568 Lisp_Object alias_subsidiary =
|
771
|
1569 add_suffix_to_symbol (alias, suffixes[i]);
|
440
|
1570 Lisp_Object aliasee_subsidiary =
|
771
|
1571 add_suffix_to_symbol (aliasee, suffixes[i]);
|
440
|
1572
|
|
1573 if (! NILP (Ffind_coding_system (aliasee_subsidiary)))
|
|
1574 Fdefine_coding_system_alias (alias_subsidiary, aliasee_subsidiary);
|
|
1575 }
|
|
1576 }
|
428
|
1577 /* FSF return value is a vector of [ALIAS-unix ALIAS-dos ALIAS-mac],
|
|
1578 but it doesn't look intentional, so I'd rather return something
|
|
1579 meaningful or nothing at all. */
|
|
1580 return Qnil;
|
|
1581 }
|
|
1582
|
|
1583 static Lisp_Object
|
771
|
1584 subsidiary_coding_system (Lisp_Object coding_system, enum eol_type type)
|
428
|
1585 {
|
|
1586 Lisp_Coding_System *cs = XCODING_SYSTEM (coding_system);
|
|
1587 Lisp_Object new_coding_system;
|
|
1588
|
|
1589 switch (type)
|
|
1590 {
|
|
1591 case EOL_AUTODETECT: return coding_system;
|
|
1592 case EOL_LF: new_coding_system = CODING_SYSTEM_EOL_LF (cs); break;
|
|
1593 case EOL_CR: new_coding_system = CODING_SYSTEM_EOL_CR (cs); break;
|
|
1594 case EOL_CRLF: new_coding_system = CODING_SYSTEM_EOL_CRLF (cs); break;
|
2500
|
1595 default: ABORT (); return Qnil;
|
428
|
1596 }
|
|
1597
|
|
1598 return NILP (new_coding_system) ? coding_system : new_coding_system;
|
|
1599 }
|
|
1600
|
|
1601 DEFUN ("subsidiary-coding-system", Fsubsidiary_coding_system, 2, 2, 0, /*
|
|
1602 Return the subsidiary coding system of CODING-SYSTEM with eol type EOL-TYPE.
|
771
|
1603 The logically opposite operation is `coding-system-base'.
|
428
|
1604 */
|
|
1605 (coding_system, eol_type))
|
|
1606 {
|
771
|
1607 coding_system = get_coding_system_for_text_file (coding_system, 0);
|
428
|
1608
|
|
1609 return subsidiary_coding_system (coding_system,
|
|
1610 symbol_to_eol_type (eol_type));
|
|
1611 }
|
|
1612
|
771
|
1613 DEFUN ("coding-system-base", Fcoding_system_base,
|
|
1614 1, 1, 0, /*
|
|
1615 Return the base coding system of CODING-SYSTEM.
|
|
1616 If CODING-SYSTEM is a subsidiary, this returns its parent; otherwise, it
|
|
1617 returns CODING-SYSTEM.
|
|
1618 The logically opposite operation is `subsidiary-coding-system'.
|
|
1619 */
|
|
1620 (coding_system))
|
|
1621 {
|
|
1622 Lisp_Object base;
|
|
1623
|
|
1624 coding_system = Fget_coding_system (coding_system);
|
|
1625 if (EQ (XCODING_SYSTEM_NAME (coding_system), Qbinary))
|
|
1626 return Fget_coding_system (Qraw_text); /* hack! */
|
|
1627 base = XCODING_SYSTEM_SUBSIDIARY_PARENT (coding_system);
|
|
1628 if (!NILP (base))
|
|
1629 return base;
|
|
1630 return coding_system;
|
|
1631 }
|
|
1632
|
|
1633 DEFUN ("coding-system-used-for-io", Fcoding_system_used_for_io,
|
|
1634 1, 1, 0, /*
|
|
1635 Return the coding system actually used for I/O.
|
|
1636 In some cases (e.g. when a particular EOL type is specified) this won't be
|
2297
|
1637 the coding system itself. This can be useful when trying to determine
|
|
1638 precisely how data was decoded.
|
771
|
1639 */
|
|
1640 (coding_system))
|
|
1641 {
|
|
1642 Lisp_Object canon;
|
|
1643
|
|
1644 coding_system = Fget_coding_system (coding_system);
|
|
1645 canon = XCODING_SYSTEM_CANONICAL (coding_system);
|
|
1646 if (!NILP (canon))
|
|
1647 return canon;
|
|
1648 return coding_system;
|
|
1649 }
|
|
1650
|
428
|
1651
|
|
1652 /************************************************************************/
|
|
1653 /* Coding system accessors */
|
|
1654 /************************************************************************/
|
|
1655
|
771
|
1656 DEFUN ("coding-system-description", Fcoding_system_description, 1, 1, 0, /*
|
|
1657 Return the description for CODING-SYSTEM.
|
|
1658 The `description' of a coding system is a short English phrase giving the
|
|
1659 name rendered according to English punctuation rules, plus possibly some
|
|
1660 explanatory text (typically in the form of a parenthetical phrase). The
|
|
1661 description is intended to be short enough that it can appear as a menu item,
|
|
1662 and clear enough to be recognizable even to someone who is assumed to have
|
|
1663 some basic familiarity with different encodings but may not know all the
|
|
1664 technical names; thus, for `cn-gb-2312' is described as "Chinese EUC" and
|
|
1665 `hz-gb-2312' is described as "Hz/ZW (Chinese)", where the actual name of
|
|
1666 the encoding is given, followed by a note that this is a Chinese encoding,
|
|
1667 because the great majority of people encountering this would have no idea
|
|
1668 what it is, and giving the language indicates whether the encoding should
|
|
1669 just be ignored or (conceivably) investigated more thoroughly.
|
428
|
1670 */
|
|
1671 (coding_system))
|
|
1672 {
|
|
1673 coding_system = Fget_coding_system (coding_system);
|
771
|
1674 return XCODING_SYSTEM_DESCRIPTION (coding_system);
|
428
|
1675 }
|
|
1676
|
|
1677 DEFUN ("coding-system-type", Fcoding_system_type, 1, 1, 0, /*
|
|
1678 Return the type of CODING-SYSTEM.
|
|
1679 */
|
|
1680 (coding_system))
|
|
1681 {
|
771
|
1682 coding_system = Fget_coding_system (coding_system);
|
|
1683 return XCODING_SYSTEM_TYPE (coding_system);
|
428
|
1684 }
|
|
1685
|
|
1686 DEFUN ("coding-system-property", Fcoding_system_property, 2, 2, 0, /*
|
|
1687 Return the PROP property of CODING-SYSTEM.
|
|
1688 */
|
|
1689 (coding_system, prop))
|
|
1690 {
|
|
1691 coding_system = Fget_coding_system (coding_system);
|
|
1692 CHECK_SYMBOL (prop);
|
|
1693
|
|
1694 if (EQ (prop, Qname))
|
|
1695 return XCODING_SYSTEM_NAME (coding_system);
|
|
1696 else if (EQ (prop, Qtype))
|
|
1697 return Fcoding_system_type (coding_system);
|
771
|
1698 else if (EQ (prop, Qdescription))
|
|
1699 return XCODING_SYSTEM_DESCRIPTION (coding_system);
|
428
|
1700 else if (EQ (prop, Qmnemonic))
|
|
1701 return XCODING_SYSTEM_MNEMONIC (coding_system);
|
771
|
1702 else if (EQ (prop, Qdocumentation))
|
|
1703 return XCODING_SYSTEM_DOCUMENTATION (coding_system);
|
428
|
1704 else if (EQ (prop, Qeol_type))
|
771
|
1705 return eol_type_to_symbol (XCODING_SYSTEM_EOL_TYPE
|
|
1706 (coding_system));
|
428
|
1707 else if (EQ (prop, Qeol_lf))
|
|
1708 return XCODING_SYSTEM_EOL_LF (coding_system);
|
|
1709 else if (EQ (prop, Qeol_crlf))
|
|
1710 return XCODING_SYSTEM_EOL_CRLF (coding_system);
|
|
1711 else if (EQ (prop, Qeol_cr))
|
|
1712 return XCODING_SYSTEM_EOL_CR (coding_system);
|
|
1713 else if (EQ (prop, Qpost_read_conversion))
|
|
1714 return XCODING_SYSTEM_POST_READ_CONVERSION (coding_system);
|
|
1715 else if (EQ (prop, Qpre_write_conversion))
|
|
1716 return XCODING_SYSTEM_PRE_WRITE_CONVERSION (coding_system);
|
771
|
1717 else
|
|
1718 {
|
|
1719 Lisp_Object value = CODESYSMETH_OR_GIVEN (XCODING_SYSTEM (coding_system),
|
|
1720 getprop,
|
|
1721 (coding_system, prop),
|
|
1722 Qunbound);
|
|
1723 if (UNBOUNDP (value))
|
|
1724 invalid_constant ("Unrecognized property", prop);
|
|
1725 return value;
|
|
1726 }
|
|
1727 }
|
|
1728
|
|
1729
|
|
1730 /************************************************************************/
|
|
1731 /* Coding stream functions */
|
|
1732 /************************************************************************/
|
|
1733
|
|
1734 /* A coding stream is a stream used for encoding or decoding text. The
|
|
1735 coding-stream object keeps track of the actual coding system, the stream
|
|
1736 that is at the other end, and data that needs to be persistent across
|
|
1737 the lifetime of the stream. */
|
|
1738
|
1204
|
1739 extern const struct sized_memory_description chain_coding_stream_description;
|
|
1740 extern const struct sized_memory_description undecided_coding_stream_description;
|
|
1741
|
|
1742 static const struct memory_description coding_stream_data_description_1 []= {
|
2551
|
1743 { XD_BLOCK_PTR, chain_coding_system, 1,
|
|
1744 { &chain_coding_stream_description } },
|
|
1745 { XD_BLOCK_PTR, undecided_coding_system, 1,
|
|
1746 { &undecided_coding_stream_description } },
|
1204
|
1747 { XD_END }
|
|
1748 };
|
|
1749
|
|
1750 static const struct sized_memory_description coding_stream_data_description = {
|
|
1751 sizeof (void *), coding_stream_data_description_1
|
|
1752 };
|
|
1753
|
|
1754 static const struct memory_description coding_lstream_description[] = {
|
|
1755 { XD_INT, offsetof (struct coding_stream, type) },
|
|
1756 { XD_LISP_OBJECT, offsetof (struct coding_stream, orig_codesys) },
|
|
1757 { XD_LISP_OBJECT, offsetof (struct coding_stream, codesys) },
|
|
1758 { XD_LISP_OBJECT, offsetof (struct coding_stream, other_end) },
|
|
1759 { XD_UNION, offsetof (struct coding_stream, data),
|
2551
|
1760 XD_INDIRECT (0, 0), { &coding_stream_data_description } },
|
1204
|
1761 { XD_END }
|
|
1762 };
|
|
1763
|
|
1764 DEFINE_LSTREAM_IMPLEMENTATION_WITH_DATA ("coding", coding);
|
771
|
1765
|
|
1766 /* Encoding and decoding are parallel operations, so we create just one
|
|
1767 stream for both. "Decoding" may involve the extra step of autodetection
|
|
1768 of the data format, but that's only because of the conventional
|
|
1769 definition of decoding as converting from external- to
|
|
1770 internal-formatted data.
|
|
1771
|
2297
|
1772 [[ REWRITE ME! ]]
|
|
1773
|
771
|
1774 #### We really need to abstract out the concept of "data formats" and
|
|
1775 define "converters" that convert from and to specified formats,
|
|
1776 eliminating the idea of decoding and encoding. When specifying a
|
|
1777 conversion process, we need to give the data formats themselves, not the
|
|
1778 conversion processes -- e.g. a coding system called "Unicode->multibyte"
|
|
1779 converts in both directions, and we could auto-detect the format of data
|
|
1780 at either end. */
|
|
1781
|
|
1782 static Bytecount
|
|
1783 coding_reader (Lstream *stream, unsigned char *data, Bytecount size)
|
|
1784 {
|
|
1785 unsigned char *orig_data = data;
|
|
1786 Bytecount read_size;
|
|
1787 int error_occurred = 0;
|
|
1788 struct coding_stream *str = CODING_STREAM_DATA (stream);
|
|
1789
|
|
1790 /* We need to interface to coding_{de,en}code_1(), which expects to take
|
|
1791 some amount of data and store the result into a Dynarr. We have
|
|
1792 coding_{de,en}code_1() store into c->runoff, and take data from there
|
|
1793 as necessary. */
|
|
1794
|
|
1795 /* We loop until we have enough data, reading chunks from the other
|
|
1796 end and converting it. */
|
|
1797 while (1)
|
|
1798 {
|
|
1799 /* Take data from convert_to if we can. Make sure to take at
|
|
1800 most SIZE bytes, and delete the data from convert_to. */
|
|
1801 if (Dynarr_length (str->convert_to) > 0)
|
|
1802 {
|
|
1803 Bytecount chunk =
|
|
1804 min (size, (Bytecount) Dynarr_length (str->convert_to));
|
|
1805 memcpy (data, Dynarr_atp (str->convert_to, 0), chunk);
|
|
1806 Dynarr_delete_many (str->convert_to, 0, chunk);
|
|
1807 data += chunk;
|
|
1808 size -= chunk;
|
|
1809 }
|
|
1810
|
|
1811 if (size == 0)
|
|
1812 break; /* No more room for data */
|
|
1813
|
|
1814 if (str->eof)
|
|
1815 break;
|
|
1816
|
|
1817 {
|
|
1818 /* Exhausted convert_to, so get some more. Read into convert_from,
|
|
1819 after existing "rejected" data from the last conversion. */
|
|
1820 Bytecount rejected = Dynarr_length (str->convert_from);
|
|
1821 /* #### 1024 is arbitrary; we really need to separate 0 from EOF,
|
|
1822 and when we get 0, keep taking more data until we don't get 0 --
|
|
1823 we don't know how much data the conversion routine might need
|
2297
|
1824 before it can generate any data of its own (eg, bzip2). */
|
814
|
1825 Bytecount readmore =
|
|
1826 str->one_byte_at_a_time ? (Bytecount) 1 :
|
|
1827 max (size, (Bytecount) 1024);
|
771
|
1828
|
|
1829 Dynarr_add_many (str->convert_from, 0, readmore);
|
|
1830 read_size = Lstream_read (str->other_end,
|
|
1831 Dynarr_atp (str->convert_from, rejected),
|
|
1832 readmore);
|
|
1833 /* Trim size down to how much we actually got */
|
|
1834 Dynarr_set_size (str->convert_from, rejected + max (0, read_size));
|
|
1835 }
|
|
1836
|
|
1837 if (read_size < 0) /* LSTREAM_ERROR */
|
|
1838 {
|
|
1839 error_occurred = 1;
|
|
1840 break;
|
|
1841 }
|
|
1842 if (read_size == 0) /* LSTREAM_EOF */
|
|
1843 /* There might be some more end data produced in the translation,
|
|
1844 so we set a flag and call the conversion method once more to
|
|
1845 output any final stuff it may be holding, any "go back to a sane
|
|
1846 state" escape sequences, etc. The conversion method is free to
|
|
1847 look at this flag, and we use it above to stop looping. */
|
|
1848 str->eof = 1;
|
|
1849 {
|
|
1850 Bytecount processed;
|
|
1851 Bytecount to_process = Dynarr_length (str->convert_from);
|
|
1852
|
|
1853 /* Convert the data, and save any rejected data in convert_from */
|
|
1854 processed =
|
|
1855 XCODESYSMETH (str->codesys, convert,
|
|
1856 (str, Dynarr_atp (str->convert_from, 0),
|
|
1857 str->convert_to, to_process));
|
|
1858 if (processed < 0)
|
|
1859 {
|
|
1860 error_occurred = 1;
|
|
1861 break;
|
|
1862 }
|
|
1863 assert (processed <= to_process);
|
|
1864 if (processed < to_process)
|
|
1865 memmove (Dynarr_atp (str->convert_from, 0),
|
|
1866 Dynarr_atp (str->convert_from, processed),
|
|
1867 to_process - processed);
|
|
1868 Dynarr_set_size (str->convert_from, to_process - processed);
|
|
1869 }
|
|
1870 }
|
|
1871
|
|
1872 if (data - orig_data == 0)
|
|
1873 return error_occurred ? -1 : 0;
|
|
1874 else
|
|
1875 return data - orig_data;
|
|
1876 }
|
|
1877
|
|
1878 static Bytecount
|
|
1879 coding_writer (Lstream *stream, const unsigned char *data, Bytecount size)
|
|
1880 {
|
|
1881 struct coding_stream *str = CODING_STREAM_DATA (stream);
|
|
1882
|
|
1883 /* Convert all our data into convert_to, and then attempt to write
|
|
1884 it all out to the other end. */
|
|
1885 Dynarr_reset (str->convert_to);
|
|
1886 size = XCODESYSMETH (str->codesys, convert,
|
|
1887 (str, data, str->convert_to, size));
|
|
1888 if (Lstream_write (str->other_end, Dynarr_atp (str->convert_to, 0),
|
|
1889 Dynarr_length (str->convert_to)) < 0)
|
|
1890 return -1;
|
|
1891 else
|
|
1892 /* The return value indicates how much of the incoming data was
|
|
1893 processed, not how many bytes were written. */
|
|
1894 return size;
|
|
1895 }
|
|
1896
|
|
1897 static int
|
|
1898 encode_decode_source_sink_type_is_char (Lisp_Object cs,
|
|
1899 enum source_or_sink sex,
|
|
1900 enum encode_decode direction)
|
|
1901 {
|
|
1902 return (direction == CODING_DECODE ?
|
|
1903 decoding_source_sink_type_is_char (cs, sex) :
|
|
1904 encoding_source_sink_type_is_char (cs, sex));
|
|
1905 }
|
|
1906
|
|
1907 /* Ensure that the convert methods only get full characters sent to them to
|
|
1908 convert if the source of that conversion is characters; and that no such
|
|
1909 full-character checking happens when the source is bytes. Keep in mind
|
|
1910 that (1) the conversion_end_type return values take the perspective of
|
|
1911 encoding; (2) the source for decoding is the same as the sink for
|
|
1912 encoding; (3) when writing, the data is given to us, and we set our own
|
|
1913 stream to be character mode or not; (4) when reading, the data comes
|
|
1914 from the other_end stream, and we set that one to be character mode or
|
|
1915 not. This is consistent with the comment above the prototype for
|
|
1916 Lstream_set_character_mode(), which lays out rules for who is allowed to
|
|
1917 modify the character type mode on a stream.
|
|
1918
|
814
|
1919 If we're a read stream, we're always setting character mode on the
|
|
1920 source, but we also set it on ourselves consistent with the flag that
|
|
1921 can disable this (see again the comment above
|
|
1922 Lstream_set_character_mode()).
|
|
1923 */
|
771
|
1924
|
|
1925 static void
|
|
1926 set_coding_character_mode (Lstream *stream)
|
|
1927 {
|
|
1928 struct coding_stream *str = CODING_STREAM_DATA (stream);
|
|
1929 Lstream *stream_to_set =
|
|
1930 stream->flags & LSTREAM_FL_WRITE ? stream : str->other_end;
|
|
1931 if (encode_decode_source_sink_type_is_char
|
|
1932 (str->codesys, CODING_SOURCE, str->direction))
|
|
1933 Lstream_set_character_mode (stream_to_set);
|
|
1934 else
|
|
1935 Lstream_unset_character_mode (stream_to_set);
|
814
|
1936 if (str->set_char_mode_on_us_when_reading &&
|
|
1937 (stream->flags & LSTREAM_FL_READ))
|
|
1938 {
|
|
1939 if (encode_decode_source_sink_type_is_char
|
|
1940 (str->codesys, CODING_SINK, str->direction))
|
|
1941 Lstream_set_character_mode (stream);
|
|
1942 else
|
|
1943 Lstream_unset_character_mode (stream);
|
|
1944 }
|
771
|
1945 }
|
|
1946
|
|
1947 static Lisp_Object
|
|
1948 coding_marker (Lisp_Object stream)
|
|
1949 {
|
|
1950 struct coding_stream *str = CODING_STREAM_DATA (XLSTREAM (stream));
|
|
1951
|
|
1952 mark_object (str->orig_codesys);
|
|
1953 mark_object (str->codesys);
|
|
1954 MAYBE_XCODESYSMETH (str->codesys, mark_coding_stream, (str));
|
|
1955 return wrap_lstream (str->other_end);
|
|
1956 }
|
|
1957
|
|
1958 static int
|
|
1959 coding_rewinder (Lstream *stream)
|
|
1960 {
|
|
1961 struct coding_stream *str = CODING_STREAM_DATA (stream);
|
|
1962 MAYBE_XCODESYSMETH (str->codesys, rewind_coding_stream, (str));
|
|
1963
|
|
1964 str->ch = 0;
|
|
1965 Dynarr_reset (str->convert_to);
|
|
1966 Dynarr_reset (str->convert_from);
|
|
1967 return Lstream_rewind (str->other_end);
|
|
1968 }
|
|
1969
|
|
1970 static int
|
|
1971 coding_seekable_p (Lstream *stream)
|
|
1972 {
|
|
1973 struct coding_stream *str = CODING_STREAM_DATA (stream);
|
|
1974 return Lstream_seekable_p (str->other_end);
|
|
1975 }
|
|
1976
|
|
1977 static int
|
|
1978 coding_flusher (Lstream *stream)
|
|
1979 {
|
|
1980 struct coding_stream *str = CODING_STREAM_DATA (stream);
|
|
1981 return Lstream_flush (str->other_end);
|
|
1982 }
|
|
1983
|
|
1984 static int
|
|
1985 coding_closer (Lstream *stream)
|
|
1986 {
|
|
1987 struct coding_stream *str = CODING_STREAM_DATA (stream);
|
|
1988 if (stream->flags & LSTREAM_FL_WRITE)
|
|
1989 {
|
|
1990 str->eof = 1;
|
|
1991 coding_writer (stream, 0, 0);
|
|
1992 str->eof = 0;
|
|
1993 }
|
|
1994 /* It's safe to free the runoff dynarrs now because they are used only
|
|
1995 during conversion. We need to keep the type-specific data around,
|
|
1996 though, because of canonicalize_after_coding. */
|
|
1997 if (str->convert_to)
|
|
1998 {
|
|
1999 Dynarr_free (str->convert_to);
|
|
2000 str->convert_to = 0;
|
|
2001 }
|
|
2002 if (str->convert_from)
|
428
|
2003 {
|
771
|
2004 Dynarr_free (str->convert_from);
|
|
2005 str->convert_from = 0;
|
|
2006 }
|
|
2007
|
800
|
2008 if (str->no_close_other)
|
|
2009 return Lstream_flush (str->other_end);
|
|
2010 else
|
|
2011 return Lstream_close (str->other_end);
|
771
|
2012 }
|
|
2013
|
|
2014 static void
|
|
2015 coding_finalizer (Lstream *stream)
|
|
2016 {
|
|
2017 struct coding_stream *str = CODING_STREAM_DATA (stream);
|
|
2018
|
|
2019 assert (!str->finalized);
|
|
2020 MAYBE_XCODESYSMETH (str->codesys, finalize_coding_stream, (str));
|
|
2021 if (str->data)
|
|
2022 {
|
1726
|
2023 xfree (str->data, void *);
|
771
|
2024 str->data = 0;
|
|
2025 }
|
|
2026 str->finalized = 1;
|
|
2027 }
|
|
2028
|
|
2029 static Lisp_Object
|
|
2030 coding_stream_canonicalize_after_coding (Lstream *stream)
|
|
2031 {
|
|
2032 struct coding_stream *str = CODING_STREAM_DATA (stream);
|
|
2033
|
|
2034 return XCODESYSMETH_OR_GIVEN (str->codesys, canonicalize_after_coding,
|
|
2035 (str), str->codesys);
|
|
2036 }
|
|
2037
|
|
2038 Lisp_Object
|
|
2039 coding_stream_detected_coding_system (Lstream *stream)
|
|
2040 {
|
|
2041 Lisp_Object codesys =
|
|
2042 coding_stream_canonicalize_after_coding (stream);
|
|
2043 if (NILP (codesys))
|
|
2044 return Fget_coding_system (Qidentity);
|
|
2045 return codesys;
|
|
2046 }
|
|
2047
|
|
2048 Lisp_Object
|
|
2049 coding_stream_coding_system (Lstream *stream)
|
|
2050 {
|
|
2051 return CODING_STREAM_DATA (stream)->codesys;
|
|
2052 }
|
|
2053
|
|
2054 /* Change the coding system associated with a stream. */
|
|
2055
|
|
2056 void
|
|
2057 set_coding_stream_coding_system (Lstream *lstr, Lisp_Object codesys)
|
|
2058 {
|
|
2059 struct coding_stream *str = CODING_STREAM_DATA (lstr);
|
|
2060 if (EQ (str->orig_codesys, codesys))
|
|
2061 return;
|
|
2062 /* We do the equivalent of closing the stream, destroying it, and
|
|
2063 reinitializing it. This includes flushing out the data and signalling
|
|
2064 EOF, if we're a writing stream; we also replace the type-specific data
|
|
2065 with the data appropriate for the new coding system. */
|
|
2066 if (!NILP (str->codesys))
|
|
2067 {
|
|
2068 if (lstr->flags & LSTREAM_FL_WRITE)
|
|
2069 {
|
|
2070 Lstream_flush (lstr);
|
|
2071 str->eof = 1;
|
|
2072 coding_writer (lstr, 0, 0);
|
|
2073 str->eof = 0;
|
|
2074 }
|
|
2075 MAYBE_XCODESYSMETH (str->codesys, finalize_coding_stream, (str));
|
|
2076 }
|
|
2077 str->orig_codesys = codesys;
|
|
2078 str->codesys = coding_system_real_canonical (codesys);
|
|
2079
|
|
2080 if (str->data)
|
|
2081 {
|
1726
|
2082 xfree (str->data, void *);
|
771
|
2083 str->data = 0;
|
428
|
2084 }
|
771
|
2085 if (XCODING_SYSTEM_METHODS (str->codesys)->coding_data_size)
|
1204
|
2086 {
|
|
2087 str->data =
|
|
2088 xmalloc_and_zero (XCODING_SYSTEM_METHODS (str->codesys)->
|
|
2089 coding_data_size);
|
|
2090 str->type = XCODING_SYSTEM_METHODS (str->codesys)->enumtype;
|
|
2091 }
|
771
|
2092 MAYBE_XCODESYSMETH (str->codesys, init_coding_stream, (str));
|
|
2093 /* The new coding system may have different ideas regarding whether its
|
|
2094 ends are characters or bytes. */
|
|
2095 set_coding_character_mode (lstr);
|
|
2096 }
|
|
2097
|
|
2098 /* WARNING WARNING WARNING WARNING!!!!! If you open up a coding
|
|
2099 stream for writing, no automatic code detection will be performed.
|
|
2100 The reason for this is that automatic code detection requires a
|
|
2101 seekable input. Things will also fail if you open a coding
|
|
2102 stream for reading using a non-fully-specified coding system and
|
|
2103 a non-seekable input stream. */
|
|
2104
|
|
2105 static Lisp_Object
|
|
2106 make_coding_stream_1 (Lstream *stream, Lisp_Object codesys,
|
800
|
2107 const char *mode, enum encode_decode direction,
|
802
|
2108 int flags)
|
771
|
2109 {
|
|
2110 Lstream *lstr = Lstream_new (lstream_coding, mode);
|
|
2111 struct coding_stream *str = CODING_STREAM_DATA (lstr);
|
|
2112
|
|
2113 codesys = Fget_coding_system (codesys);
|
|
2114 xzero (*str);
|
|
2115 str->codesys = Qnil;
|
|
2116 str->orig_codesys = Qnil;
|
|
2117 str->us = lstr;
|
|
2118 str->other_end = stream;
|
|
2119 str->convert_to = Dynarr_new (unsigned_char);
|
|
2120 str->convert_from = Dynarr_new (unsigned_char);
|
|
2121 str->direction = direction;
|
814
|
2122 if (flags & LSTREAM_FL_NO_CLOSE_OTHER)
|
802
|
2123 str->no_close_other = 1;
|
814
|
2124 if (flags & LSTREAM_FL_READ_ONE_BYTE_AT_A_TIME)
|
802
|
2125 str->one_byte_at_a_time = 1;
|
814
|
2126 if (!(flags & LSTREAM_FL_NO_INIT_CHAR_MODE_WHEN_READING))
|
|
2127 str->set_char_mode_on_us_when_reading = 1;
|
802
|
2128
|
771
|
2129 set_coding_stream_coding_system (lstr, codesys);
|
793
|
2130 return wrap_lstream (lstr);
|
771
|
2131 }
|
|
2132
|
814
|
2133 /* FLAGS:
|
|
2134
|
|
2135 LSTREAM_FL_NO_CLOSE_OTHER
|
|
2136 Don't close STREAM (the stream at the other end) when this stream is
|
|
2137 closed.
|
|
2138
|
|
2139 LSTREAM_FL_READ_ONE_BYTE_AT_A_TIME
|
|
2140 When reading from STREAM, read and process one byte at a time rather
|
|
2141 than in large chunks. This is for reading from TTY's, so we don't
|
|
2142 block. #### We should instead create a non-blocking filedesc stream
|
|
2143 that emulates the behavior as necessary using select(), when the
|
|
2144 fcntls don't work. (As seems to be the case on Cygwin.)
|
|
2145
|
|
2146 LSTREAM_FL_NO_INIT_CHAR_MODE_WHEN_READING
|
|
2147 When reading from STREAM, read and process one byte at a time rather
|
|
2148 than in large chunks. This is for reading from TTY's, so we don't
|
|
2149 block. #### We should instead create a non-blocking filedesc stream
|
|
2150 that emulates the behavior as necessary using select(), when the
|
|
2151 fcntls don't work. (As seems to be the case on Cygwin.)
|
|
2152 */
|
771
|
2153 Lisp_Object
|
|
2154 make_coding_input_stream (Lstream *stream, Lisp_Object codesys,
|
802
|
2155 enum encode_decode direction, int flags)
|
771
|
2156 {
|
800
|
2157 return make_coding_stream_1 (stream, codesys, "r", direction,
|
802
|
2158 flags);
|
771
|
2159 }
|
|
2160
|
814
|
2161 /* FLAGS:
|
|
2162
|
|
2163 LSTREAM_FL_NO_CLOSE_OTHER
|
|
2164 Don't close STREAM (the stream at the other end) when this stream is
|
|
2165 closed.
|
|
2166 */
|
771
|
2167 Lisp_Object
|
|
2168 make_coding_output_stream (Lstream *stream, Lisp_Object codesys,
|
802
|
2169 enum encode_decode direction, int flags)
|
771
|
2170 {
|
800
|
2171 return make_coding_stream_1 (stream, codesys, "w", direction,
|
802
|
2172 flags);
|
771
|
2173 }
|
|
2174
|
|
2175 static Lisp_Object
|
|
2176 encode_decode_coding_region (Lisp_Object start, Lisp_Object end,
|
|
2177 Lisp_Object coding_system, Lisp_Object buffer,
|
|
2178 enum encode_decode direction)
|
|
2179 {
|
|
2180 Charbpos b, e;
|
|
2181 struct buffer *buf = decode_buffer (buffer, 0);
|
|
2182 Lisp_Object instream = Qnil, to_outstream = Qnil, outstream = Qnil;
|
|
2183 Lisp_Object from_outstream = Qnil, auto_outstream = Qnil;
|
|
2184 Lisp_Object lb_outstream = Qnil;
|
|
2185 Lisp_Object next;
|
|
2186 Lstream *istr, *ostr;
|
|
2187 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
|
|
2188 struct gcpro ngcpro1;
|
|
2189 int source_char, sink_char;
|
|
2190
|
|
2191 get_buffer_range_char (buf, start, end, &b, &e, 0);
|
|
2192 barf_if_buffer_read_only (buf, b, e);
|
|
2193
|
|
2194 GCPRO5 (instream, to_outstream, outstream, from_outstream, lb_outstream);
|
|
2195 NGCPRO1 (auto_outstream);
|
|
2196
|
|
2197 coding_system = Fget_coding_system (coding_system);
|
|
2198 source_char = encode_decode_source_sink_type_is_char (coding_system,
|
|
2199 CODING_SOURCE,
|
|
2200 direction);
|
|
2201 sink_char = encode_decode_source_sink_type_is_char (coding_system,
|
|
2202 CODING_SINK,
|
|
2203 direction);
|
|
2204
|
|
2205 /* Order is IN <---> [TO] -> OUT -> [FROM] -> [AUTODETECT-EOL] -> LB */
|
|
2206 instream = make_lisp_buffer_input_stream (buf, b, e, 0);
|
|
2207 next = lb_outstream = make_lisp_buffer_output_stream (buf, b, 0);
|
|
2208
|
|
2209 if (direction == CODING_DECODE &&
|
|
2210 XCODING_SYSTEM_EOL_TYPE (coding_system) == EOL_AUTODETECT)
|
|
2211 next = auto_outstream =
|
|
2212 make_coding_output_stream
|
800
|
2213 (XLSTREAM (next), Fget_coding_system (Qconvert_eol_autodetect),
|
|
2214 CODING_DECODE, 0);
|
771
|
2215
|
|
2216 if (!sink_char)
|
|
2217 next = from_outstream =
|
800
|
2218 make_coding_output_stream (XLSTREAM (next), Qbinary, CODING_DECODE, 0);
|
771
|
2219 outstream = make_coding_output_stream (XLSTREAM (next), coding_system,
|
800
|
2220 direction, 0);
|
771
|
2221 if (!source_char)
|
428
|
2222 {
|
771
|
2223 to_outstream =
|
|
2224 make_coding_output_stream (XLSTREAM (outstream),
|
800
|
2225 Qbinary, CODING_ENCODE, 0);
|
771
|
2226 ostr = XLSTREAM (to_outstream);
|
|
2227 }
|
|
2228 else
|
|
2229 ostr = XLSTREAM (outstream);
|
|
2230 istr = XLSTREAM (instream);
|
|
2231
|
|
2232 /* The chain of streams looks like this:
|
|
2233
|
2297
|
2234 [BUFFER] <----- (( read from/send to loop ))
|
771
|
2235 ------> [CHAR->BYTE i.e. ENCODE AS BINARY if source is
|
|
2236 in bytes]
|
|
2237 ------> [ENCODE/DECODE AS SPECIFIED]
|
|
2238 ------> [BYTE->CHAR i.e. DECODE AS BINARY
|
|
2239 if sink is in bytes]
|
|
2240 ------> [AUTODETECT EOL if
|
|
2241 we're decoding and
|
|
2242 coding system calls
|
|
2243 for this]
|
|
2244 ------> [BUFFER]
|
|
2245 */
|
2367
|
2246
|
|
2247 /* #### See comment
|
|
2248
|
|
2249 EFFICIENCY OF CODING CONVERSION WITH MULTIPLE COPIES/CHAINS
|
|
2250
|
|
2251 in text.c.
|
|
2252 */
|
|
2253
|
771
|
2254 while (1)
|
|
2255 {
|
|
2256 char tempbuf[1024]; /* some random amount */
|
|
2257 Charbpos newpos, even_newer_pos;
|
|
2258 Charbpos oldpos = lisp_buffer_stream_startpos (istr);
|
|
2259 Bytecount size_in_bytes =
|
|
2260 Lstream_read (istr, tempbuf, sizeof (tempbuf));
|
|
2261
|
|
2262 if (!size_in_bytes)
|
|
2263 break;
|
|
2264 newpos = lisp_buffer_stream_startpos (istr);
|
|
2265 Lstream_write (ostr, tempbuf, size_in_bytes);
|
|
2266 even_newer_pos = lisp_buffer_stream_startpos (istr);
|
|
2267 buffer_delete_range (buf, even_newer_pos - (newpos - oldpos),
|
|
2268 even_newer_pos, 0);
|
428
|
2269 }
|
771
|
2270
|
|
2271 {
|
|
2272 Charcount retlen =
|
|
2273 lisp_buffer_stream_startpos (XLSTREAM (instream)) - b;
|
|
2274 Lstream_close (istr);
|
|
2275 Lstream_close (ostr);
|
|
2276 NUNGCPRO;
|
|
2277 UNGCPRO;
|
|
2278 Lstream_delete (istr);
|
|
2279 if (!NILP (from_outstream))
|
|
2280 Lstream_delete (XLSTREAM (from_outstream));
|
|
2281 Lstream_delete (XLSTREAM (outstream));
|
|
2282 if (!NILP (to_outstream))
|
|
2283 Lstream_delete (XLSTREAM (to_outstream));
|
|
2284 if (!NILP (auto_outstream))
|
|
2285 Lstream_delete (XLSTREAM (auto_outstream));
|
|
2286 Lstream_delete (XLSTREAM (lb_outstream));
|
|
2287 return make_int (retlen);
|
|
2288 }
|
|
2289 }
|
|
2290
|
3302
|
2291 DEFUN ("decode-coding-region", Fdecode_coding_region, 3, 4,
|
|
2292 "*r\nzDecode from coding system: \ni", /*
|
771
|
2293 Decode the text between START and END which is encoded in CODING-SYSTEM.
|
|
2294 This is useful if you've read in encoded text from a file without decoding
|
|
2295 it (e.g. you read in a JIS-formatted file but used the `binary' or
|
|
2296 `no-conversion' coding system, so that it shows up as "^[$B!<!+^[(B").
|
|
2297 Return length of decoded text.
|
3302
|
2298 BUFFER defaults to the current buffer if unspecified, and when interactive.
|
771
|
2299 */
|
|
2300 (start, end, coding_system, buffer))
|
|
2301 {
|
|
2302 return encode_decode_coding_region (start, end, coding_system, buffer,
|
|
2303 CODING_DECODE);
|
|
2304 }
|
|
2305
|
3302
|
2306 DEFUN ("encode-coding-region", Fencode_coding_region, 3, 4,
|
|
2307 "*r\nzEncode to coding system: \ni", /*
|
771
|
2308 Encode the text between START and END using CODING-SYSTEM.
|
|
2309 This will, for example, convert Japanese characters into stuff such as
|
3302
|
2310 "^[$B!<!+^[(B" if you use the JIS encoding. Return length of encoded text.
|
|
2311 BUFFER defaults to the current buffer if unspecified, and when interactive.
|
771
|
2312 */
|
|
2313 (start, end, coding_system, buffer))
|
|
2314 {
|
|
2315 return encode_decode_coding_region (start, end, coding_system, buffer,
|
|
2316 CODING_ENCODE);
|
428
|
2317 }
|
|
2318
|
|
2319
|
|
2320 /************************************************************************/
|
771
|
2321 /* Chain methods */
|
428
|
2322 /************************************************************************/
|
|
2323
|
771
|
2324 /* #### Need a way to create "opposite-direction" coding systems. */
|
|
2325
|
|
2326 /* Chain two or more coding systems together to make a combination coding
|
|
2327 system. */
|
|
2328
|
|
2329 struct chain_coding_system
|
|
2330 {
|
|
2331 /* List of coding systems, in decode order */
|
|
2332 Lisp_Object *chain;
|
|
2333 /* Number of coding systems in list */
|
|
2334 int count;
|
|
2335 /* Coding system to return as a result of canonicalize-after-coding */
|
|
2336 Lisp_Object canonicalize_after_coding;
|
|
2337 };
|
|
2338
|
|
2339 struct chain_coding_stream
|
|
2340 {
|
|
2341 int initted;
|
|
2342 /* Lstreams for chain coding system */
|
|
2343 Lisp_Object *lstreams;
|
|
2344 int lstream_count;
|
|
2345 };
|
|
2346
|
1204
|
2347 static const struct memory_description chain_coding_system_description[] = {
|
|
2348 { XD_INT, offsetof (struct chain_coding_system, count) },
|
2367
|
2349 { XD_BLOCK_PTR, offsetof (struct chain_coding_system, chain),
|
2551
|
2350 XD_INDIRECT (0, 0), { &lisp_object_description } },
|
1204
|
2351 { XD_LISP_OBJECT, offsetof (struct chain_coding_system,
|
|
2352 canonicalize_after_coding) },
|
771
|
2353 { XD_END }
|
|
2354 };
|
|
2355
|
1204
|
2356 static const struct memory_description chain_coding_stream_description_1 [] = {
|
|
2357 { XD_INT, offsetof (struct chain_coding_stream, lstream_count) },
|
2367
|
2358 { XD_BLOCK_PTR, offsetof (struct chain_coding_stream, lstreams),
|
2551
|
2359 XD_INDIRECT (0, 0), { &lisp_object_description } },
|
771
|
2360 { XD_END }
|
|
2361 };
|
|
2362
|
1204
|
2363 const struct sized_memory_description chain_coding_stream_description = {
|
|
2364 sizeof (struct chain_coding_stream), chain_coding_stream_description_1
|
|
2365 };
|
|
2366
|
|
2367 DEFINE_CODING_SYSTEM_TYPE_WITH_DATA (chain);
|
|
2368
|
771
|
2369 static Lisp_Object
|
|
2370 chain_canonicalize (Lisp_Object codesys)
|
|
2371 {
|
|
2372 /* We make use of the fact that this method is called at init time, after
|
|
2373 properties have been parsed. init_method is called too early. */
|
|
2374 /* #### It's not clear we need this whole chain-canonicalize mechanism
|
|
2375 any more. */
|
|
2376 Lisp_Object chain = Flist (XCODING_SYSTEM_CHAIN_COUNT (codesys),
|
|
2377 XCODING_SYSTEM_CHAIN_CHAIN (codesys));
|
|
2378 chain = Fcons (XCODING_SYSTEM_PRE_WRITE_CONVERSION (codesys),
|
|
2379 Fcons (XCODING_SYSTEM_POST_READ_CONVERSION (codesys),
|
|
2380 chain));
|
|
2381 Fputhash (chain, codesys, Vchain_canonicalize_hash_table);
|
|
2382 return codesys;
|
|
2383 }
|
|
2384
|
|
2385 static Lisp_Object
|
|
2386 chain_canonicalize_after_coding (struct coding_stream *str)
|
|
2387 {
|
|
2388 Lisp_Object cac =
|
|
2389 XCODING_SYSTEM_CHAIN_CANONICALIZE_AFTER_CODING (str->codesys);
|
|
2390 if (!NILP (cac))
|
|
2391 return cac;
|
|
2392 return str->codesys;
|
|
2393 #if 0
|
|
2394 struct chain_coding_stream *data = CODING_STREAM_TYPE_DATA (str, chain);
|
|
2395 Lisp_Object us = str->codesys, codesys;
|
|
2396 int i;
|
|
2397 Lisp_Object chain;
|
|
2398 Lisp_Object tail;
|
|
2399 int changed = 0;
|
|
2400
|
|
2401 /* #### It's not clear we need this whole chain-canonicalize mechanism
|
|
2402 any more. */
|
|
2403 if (str->direction == CODING_ENCODE || !data->initted)
|
|
2404 return us;
|
|
2405
|
|
2406 chain = Flist (XCODING_SYSTEM_CHAIN_COUNT (us),
|
|
2407 XCODING_SYSTEM_CHAIN_CHAIN (us));
|
|
2408
|
|
2409 tail = chain;
|
|
2410 for (i = 0; i < XCODING_SYSTEM_CHAIN_COUNT (us); i++)
|
|
2411 {
|
|
2412 codesys = (coding_stream_canonicalize_after_coding
|
|
2413 (XLSTREAM (data->lstreams[i])));
|
|
2414 if (!EQ (codesys, XCAR (tail)))
|
|
2415 changed = 1;
|
|
2416 XCAR (tail) = codesys;
|
|
2417 tail = XCDR (tail);
|
|
2418 }
|
|
2419
|
|
2420 if (!changed)
|
|
2421 return us;
|
|
2422
|
|
2423 chain = delq_no_quit (Qnil, chain);
|
|
2424
|
|
2425 if (NILP (XCODING_SYSTEM_PRE_WRITE_CONVERSION (us)) &&
|
|
2426 NILP (XCODING_SYSTEM_POST_READ_CONVERSION (us)))
|
|
2427 {
|
|
2428 if (NILP (chain))
|
|
2429 return Qnil;
|
|
2430 if (NILP (XCDR (chain)))
|
|
2431 return XCAR (chain);
|
|
2432 }
|
|
2433
|
|
2434 codesys = Fgethash (Fcons (XCODING_SYSTEM_PRE_WRITE_CONVERSION (us),
|
|
2435 Fcons (XCODING_SYSTEM_POST_READ_CONVERSION (us),
|
|
2436 chain)), Vchain_canonicalize_hash_table,
|
|
2437 Qnil);
|
|
2438 if (!NILP (codesys))
|
|
2439 return codesys;
|
|
2440 return make_internal_coding_system
|
|
2441 (us, "internal-chain-canonicalizer-wrapper",
|
|
2442 Qchain, Qunbound, list2 (Qchain, chain));
|
|
2443 #endif /* 0 */
|
|
2444 }
|
|
2445
|
|
2446 static void
|
|
2447 chain_init (Lisp_Object codesys)
|
|
2448 {
|
|
2449 XCODING_SYSTEM_CHAIN_CANONICALIZE_AFTER_CODING (codesys) = Qnil;
|
|
2450 }
|
|
2451
|
|
2452 static void
|
|
2453 chain_mark (Lisp_Object codesys)
|
|
2454 {
|
|
2455 int i;
|
|
2456
|
|
2457 for (i = 0; i < XCODING_SYSTEM_CHAIN_COUNT (codesys); i++)
|
|
2458 mark_object (XCODING_SYSTEM_CHAIN_CHAIN (codesys)[i]);
|
|
2459 mark_object (XCODING_SYSTEM_CHAIN_CANONICALIZE_AFTER_CODING (codesys));
|
|
2460 }
|
|
2461
|
|
2462 static void
|
|
2463 chain_mark_coding_stream_1 (struct chain_coding_stream *data)
|
|
2464 {
|
|
2465 int i;
|
|
2466
|
|
2467 for (i = 0; i < data->lstream_count; i++)
|
|
2468 mark_object (data->lstreams[i]);
|
|
2469 }
|
|
2470
|
|
2471 static void
|
|
2472 chain_mark_coding_stream (struct coding_stream *str)
|
|
2473 {
|
|
2474 chain_mark_coding_stream_1 (CODING_STREAM_TYPE_DATA (str, chain));
|
|
2475 }
|
|
2476
|
|
2477 static void
|
|
2478 chain_print (Lisp_Object cs, Lisp_Object printcharfun, int escapeflag)
|
|
2479 {
|
|
2480 int i;
|
|
2481
|
826
|
2482 write_c_string (printcharfun, "(");
|
771
|
2483 for (i = 0; i < XCODING_SYSTEM_CHAIN_COUNT (cs); i++)
|
|
2484 {
|
826
|
2485 write_c_string (printcharfun, i == 0 ? "" : "->");
|
771
|
2486 print_coding_system_in_print_method (XCODING_SYSTEM_CHAIN_CHAIN (cs)[i],
|
|
2487 printcharfun, escapeflag);
|
|
2488 }
|
|
2489 {
|
|
2490 Lisp_Object cac = XCODING_SYSTEM_CHAIN_CANONICALIZE_AFTER_CODING (cs);
|
|
2491 if (!NILP (cac))
|
|
2492 {
|
|
2493 if (i > 0)
|
826
|
2494 write_c_string (printcharfun, " ");
|
|
2495 write_c_string (printcharfun, "canonicalize-after-coding=");
|
771
|
2496 print_coding_system_in_print_method (cac, printcharfun, escapeflag);
|
|
2497 }
|
|
2498 }
|
|
2499
|
826
|
2500 write_c_string (printcharfun, ")");
|
771
|
2501 }
|
|
2502
|
|
2503 static void
|
|
2504 chain_rewind_coding_stream_1 (struct chain_coding_stream *data)
|
|
2505 {
|
|
2506 /* Each will rewind the next; there is always at least one stream (the
|
|
2507 dynarr stream at the end) if we're initted */
|
|
2508 if (data->initted)
|
|
2509 Lstream_rewind (XLSTREAM (data->lstreams[0]));
|
|
2510 }
|
|
2511
|
|
2512 static void
|
|
2513 chain_rewind_coding_stream (struct coding_stream *str)
|
|
2514 {
|
|
2515 chain_rewind_coding_stream_1 (CODING_STREAM_TYPE_DATA (str, chain));
|
|
2516 }
|
|
2517
|
|
2518 static void
|
|
2519 chain_init_coding_streams_1 (struct chain_coding_stream *data,
|
|
2520 unsigned_char_dynarr *dst,
|
|
2521 int ncodesys, Lisp_Object *codesys,
|
|
2522 enum encode_decode direction)
|
|
2523 {
|
|
2524 int i;
|
|
2525 Lisp_Object lstream_out;
|
|
2526
|
|
2527 data->lstream_count = ncodesys + 1;
|
|
2528 data->lstreams = xnew_array (Lisp_Object, data->lstream_count);
|
|
2529
|
|
2530 lstream_out = make_dynarr_output_stream (dst);
|
|
2531 Lstream_set_buffering (XLSTREAM (lstream_out), LSTREAM_UNBUFFERED, 0);
|
|
2532 data->lstreams[data->lstream_count - 1] = lstream_out;
|
|
2533
|
|
2534 for (i = ncodesys - 1; i >= 0; i--)
|
|
2535 {
|
|
2536 data->lstreams[i] =
|
|
2537 make_coding_output_stream
|
|
2538 (XLSTREAM (lstream_out),
|
|
2539 codesys[direction == CODING_ENCODE ? ncodesys - (i + 1) : i],
|
800
|
2540 direction, 0);
|
771
|
2541 lstream_out = data->lstreams[i];
|
|
2542 Lstream_set_buffering (XLSTREAM (lstream_out), LSTREAM_UNBUFFERED,
|
|
2543 0);
|
|
2544 }
|
|
2545 data->initted = 1;
|
|
2546 }
|
|
2547
|
|
2548 static Bytecount
|
|
2549 chain_convert (struct coding_stream *str, const UExtbyte *src,
|
|
2550 unsigned_char_dynarr *dst, Bytecount n)
|
|
2551 {
|
|
2552 struct chain_coding_stream *data = CODING_STREAM_TYPE_DATA (str, chain);
|
|
2553
|
|
2554 if (str->eof)
|
|
2555 {
|
|
2556 /* Each will close the next; there is always at least one stream (the
|
|
2557 dynarr stream at the end) if we're initted. We need to close now
|
|
2558 because more data may be generated. */
|
|
2559 if (data->initted)
|
|
2560 Lstream_close (XLSTREAM (data->lstreams[0]));
|
|
2561 return n;
|
|
2562 }
|
|
2563
|
|
2564 if (!data->initted)
|
|
2565 chain_init_coding_streams_1
|
|
2566 (data, dst, XCODING_SYSTEM_CHAIN_COUNT (str->codesys),
|
|
2567 XCODING_SYSTEM_CHAIN_CHAIN (str->codesys), str->direction);
|
|
2568
|
|
2569 if (Lstream_write (XLSTREAM (data->lstreams[0]), src, n) < 0)
|
|
2570 return -1;
|
|
2571 return n;
|
|
2572 }
|
|
2573
|
|
2574 static void
|
|
2575 chain_finalize_coding_stream_1 (struct chain_coding_stream *data)
|
|
2576 {
|
|
2577 if (data->lstreams)
|
|
2578 {
|
2297
|
2579 /* During GC, these objects are unmarked, and are about to be freed.
|
|
2580 We do NOT want them on the free list, and that will cause lots of
|
|
2581 nastiness including crashes. Just let them be freed normally. */
|
771
|
2582 if (!gc_in_progress)
|
|
2583 {
|
|
2584 int i;
|
2297
|
2585 /* Order of deletion is important here! Delete from the head of
|
|
2586 the chain and work your way towards the tail. In general,
|
|
2587 when you delete an object, there should be *NO* pointers to it
|
|
2588 anywhere. Deleting back-to-front would be a problem because
|
|
2589 there are pointers going forward. If there were pointers in
|
|
2590 both directions, you'd have to disconnect the pointers to a
|
|
2591 particular object before deleting it. */
|
771
|
2592 for (i = 0; i < data->lstream_count; i++)
|
|
2593 Lstream_delete (XLSTREAM ((data->lstreams)[i]));
|
|
2594 }
|
1726
|
2595 xfree (data->lstreams, Lisp_Object *);
|
771
|
2596 }
|
|
2597 }
|
|
2598
|
|
2599 static void
|
|
2600 chain_finalize_coding_stream (struct coding_stream *str)
|
|
2601 {
|
|
2602 chain_finalize_coding_stream_1 (CODING_STREAM_TYPE_DATA (str, chain));
|
|
2603 }
|
|
2604
|
|
2605 static void
|
|
2606 chain_finalize (Lisp_Object c)
|
|
2607 {
|
|
2608 if (XCODING_SYSTEM_CHAIN_CHAIN (c))
|
1726
|
2609 xfree (XCODING_SYSTEM_CHAIN_CHAIN (c), Lisp_Object *);
|
771
|
2610 }
|
|
2611
|
428
|
2612 static int
|
771
|
2613 chain_putprop (Lisp_Object codesys, Lisp_Object key, Lisp_Object value)
|
|
2614 {
|
|
2615 if (EQ (key, Qchain))
|
|
2616 {
|
|
2617 Lisp_Object *cslist;
|
|
2618 int count = 0;
|
|
2619 int i;
|
|
2620
|
2367
|
2621 {
|
|
2622 EXTERNAL_LIST_LOOP_2 (elt, value)
|
|
2623 {
|
|
2624 Fget_coding_system (elt);
|
|
2625 count++;
|
|
2626 }
|
|
2627 }
|
771
|
2628
|
|
2629 cslist = xnew_array (Lisp_Object, count);
|
|
2630 XCODING_SYSTEM_CHAIN_CHAIN (codesys) = cslist;
|
|
2631
|
|
2632 count = 0;
|
2367
|
2633 {
|
|
2634 EXTERNAL_LIST_LOOP_2 (elt, value)
|
|
2635 {
|
|
2636 cslist[count] = Fget_coding_system (elt);
|
|
2637 count++;
|
|
2638 }
|
|
2639 }
|
771
|
2640
|
|
2641 XCODING_SYSTEM_CHAIN_COUNT (codesys) = count;
|
|
2642
|
|
2643 for (i = 0; i < count - 1; i++)
|
|
2644 {
|
|
2645 if (decoding_source_sink_type_is_char (cslist[i], CODING_SINK) !=
|
|
2646 decoding_source_sink_type_is_char (cslist[i + 1], CODING_SOURCE))
|
|
2647 invalid_argument_2 ("Sink of first must match source of second",
|
|
2648 cslist[i], cslist[i + 1]);
|
|
2649 }
|
|
2650 }
|
|
2651 else if (EQ (key, Qcanonicalize_after_coding))
|
|
2652 XCODING_SYSTEM_CHAIN_CANONICALIZE_AFTER_CODING (codesys) =
|
|
2653 Fget_coding_system (value);
|
|
2654 else
|
|
2655 return 0;
|
|
2656 return 1;
|
|
2657 }
|
|
2658
|
|
2659 static Lisp_Object
|
|
2660 chain_getprop (Lisp_Object coding_system, Lisp_Object prop)
|
|
2661 {
|
|
2662 if (EQ (prop, Qchain))
|
|
2663 {
|
|
2664 Lisp_Object result = Qnil;
|
|
2665 int i;
|
|
2666
|
|
2667 for (i = 0; i < XCODING_SYSTEM_CHAIN_COUNT (coding_system); i++)
|
|
2668 result = Fcons (XCODING_SYSTEM_CHAIN_CHAIN (coding_system)[i],
|
|
2669 result);
|
|
2670
|
|
2671 return Fnreverse (result);
|
|
2672 }
|
|
2673 else if (EQ (prop, Qcanonicalize_after_coding))
|
|
2674 return XCODING_SYSTEM_CHAIN_CANONICALIZE_AFTER_CODING (coding_system);
|
|
2675 else
|
|
2676 return Qunbound;
|
|
2677 }
|
|
2678
|
|
2679 static enum source_sink_type
|
|
2680 chain_conversion_end_type (Lisp_Object codesys)
|
|
2681 {
|
|
2682 Lisp_Object *cslist = XCODING_SYSTEM_CHAIN_CHAIN (codesys);
|
|
2683 int n = XCODING_SYSTEM_CHAIN_COUNT (codesys);
|
|
2684 int charp_source, charp_sink;
|
|
2685
|
|
2686 if (n == 0)
|
|
2687 return DECODES_BYTE_TO_BYTE; /* arbitrary */
|
|
2688 charp_source = decoding_source_sink_type_is_char (cslist[0], CODING_SOURCE);
|
|
2689 charp_sink = decoding_source_sink_type_is_char (cslist[n - 1], CODING_SINK);
|
|
2690
|
|
2691 switch (charp_source * 2 + charp_sink)
|
|
2692 {
|
|
2693 case 0: return DECODES_BYTE_TO_BYTE;
|
|
2694 case 1: return DECODES_BYTE_TO_CHARACTER;
|
|
2695 case 2: return DECODES_CHARACTER_TO_BYTE;
|
|
2696 case 3: return DECODES_CHARACTER_TO_CHARACTER;
|
|
2697 }
|
|
2698
|
2500
|
2699 ABORT ();
|
771
|
2700 return DECODES_BYTE_TO_BYTE;
|
|
2701 }
|
|
2702
|
|
2703
|
|
2704 /************************************************************************/
|
|
2705 /* No-conversion methods */
|
|
2706 /************************************************************************/
|
|
2707
|
|
2708 /* "No conversion"; used for binary files. We use quotes because there
|
|
2709 really is some conversion being applied (it does byte<->char
|
|
2710 conversion), but it appears to the user as if the text is read in
|
2297
|
2711 without conversion.
|
|
2712
|
|
2713 #### Shouldn't we _call_ it that, then? And while we're at it,
|
|
2714 separate it into "to_internal" and "to_external"? */
|
771
|
2715 DEFINE_CODING_SYSTEM_TYPE (no_conversion);
|
|
2716
|
|
2717 /* This is used when reading in "binary" files -- i.e. files that may
|
|
2718 contain all 256 possible byte values and that are not to be
|
|
2719 interpreted as being in any particular encoding. */
|
|
2720 static Bytecount
|
|
2721 no_conversion_convert (struct coding_stream *str,
|
|
2722 const UExtbyte *src,
|
|
2723 unsigned_char_dynarr *dst, Bytecount n)
|
|
2724 {
|
|
2725 UExtbyte c;
|
|
2726 unsigned int ch = str->ch;
|
|
2727 Bytecount orign = n;
|
|
2728
|
|
2729 if (str->direction == CODING_DECODE)
|
|
2730 {
|
|
2731 while (n--)
|
|
2732 {
|
|
2733 c = *src++;
|
|
2734
|
|
2735 DECODE_ADD_BINARY_CHAR (c, dst);
|
|
2736 }
|
|
2737
|
|
2738 if (str->eof)
|
|
2739 DECODE_OUTPUT_PARTIAL_CHAR (ch, dst);
|
|
2740 }
|
|
2741 else
|
|
2742 {
|
|
2743
|
|
2744 while (n--)
|
|
2745 {
|
|
2746 c = *src++;
|
826
|
2747 if (byte_ascii_p (c))
|
771
|
2748 {
|
|
2749 assert (ch == 0);
|
|
2750 Dynarr_add (dst, c);
|
|
2751 }
|
|
2752 #ifdef MULE
|
867
|
2753 else if (ibyte_leading_byte_p (c))
|
771
|
2754 {
|
|
2755 assert (ch == 0);
|
|
2756 if (c == LEADING_BYTE_LATIN_ISO8859_1 ||
|
|
2757 c == LEADING_BYTE_CONTROL_1)
|
|
2758 ch = c;
|
|
2759 else
|
2297
|
2760 /* #### This is just plain unacceptable. */
|
771
|
2761 Dynarr_add (dst, '~'); /* untranslatable character */
|
|
2762 }
|
|
2763 else
|
|
2764 {
|
|
2765 if (ch == LEADING_BYTE_LATIN_ISO8859_1)
|
|
2766 Dynarr_add (dst, c);
|
|
2767 else if (ch == LEADING_BYTE_CONTROL_1)
|
|
2768 {
|
|
2769 assert (c < 0xC0);
|
|
2770 Dynarr_add (dst, c - 0x20);
|
|
2771 }
|
|
2772 /* else it should be the second or third byte of an
|
|
2773 untranslatable character, so ignore it */
|
|
2774 ch = 0;
|
|
2775 }
|
|
2776 #endif /* MULE */
|
|
2777
|
|
2778 }
|
|
2779 }
|
|
2780
|
|
2781 str->ch = ch;
|
|
2782 return orign;
|
|
2783 }
|
|
2784
|
|
2785 DEFINE_DETECTOR (no_conversion);
|
|
2786 DEFINE_DETECTOR_CATEGORY (no_conversion, no_conversion);
|
|
2787
|
|
2788 struct no_conversion_detector
|
|
2789 {
|
|
2790 int dummy;
|
|
2791 };
|
|
2792
|
|
2793 static void
|
2286
|
2794 no_conversion_detect (struct detection_state *st, const UExtbyte *UNUSED (src),
|
|
2795 Bytecount UNUSED (n))
|
771
|
2796 {
|
|
2797 /* Hack until we get better handling of this stuff! */
|
|
2798 DET_RESULT (st, no_conversion) = DET_SLIGHTLY_LIKELY;
|
|
2799 }
|
|
2800
|
|
2801
|
|
2802 /************************************************************************/
|
|
2803 /* Convert-eol methods */
|
|
2804 /************************************************************************/
|
|
2805
|
|
2806 /* This is used to handle end-of-line (EOL) differences. It is
|
2819
|
2807 character-to-character, and works (when encoding) *BEFORE* sending data to
|
|
2808 the main encoding routine -- thus, that routine must handle different EOL
|
|
2809 types itself if it does line-oriented type processing. This is unavoidable
|
|
2810 because we don't know whether the output of the main encoding routine is
|
|
2811 ASCII compatible (UTF-16 is definitely not, for example). [[ sjt sez this
|
|
2812 is bogus. There should be _no_ EOL processing (or processing of any kind)
|
|
2813 after conversion to external. ]]
|
771
|
2814
|
793
|
2815 There is one parameter: `subtype', either `cr', `lf', `crlf', or nil.
|
771
|
2816 */
|
|
2817
|
|
2818 struct convert_eol_coding_system
|
|
2819 {
|
|
2820 enum eol_type subtype;
|
2132
|
2821 int dummy; /* On some architectures (eg ia64) the portable dumper can
|
|
2822 produce unaligned access errors without this field. Probably
|
|
2823 because the combined structure of this structure and
|
|
2824 Lisp_Coding_System is not properly aligned. */
|
771
|
2825 };
|
|
2826
|
|
2827 #define CODING_SYSTEM_CONVERT_EOL_SUBTYPE(codesys) \
|
|
2828 (CODING_SYSTEM_TYPE_DATA (codesys, convert_eol)->subtype)
|
|
2829 #define XCODING_SYSTEM_CONVERT_EOL_SUBTYPE(codesys) \
|
|
2830 (XCODING_SYSTEM_TYPE_DATA (codesys, convert_eol)->subtype)
|
|
2831
|
|
2832 struct convert_eol_coding_stream
|
|
2833 {
|
|
2834 enum eol_type actual;
|
|
2835 };
|
|
2836
|
1204
|
2837 static const struct memory_description
|
771
|
2838 convert_eol_coding_system_description[] = {
|
|
2839 { XD_END }
|
|
2840 };
|
|
2841
|
1204
|
2842 DEFINE_CODING_SYSTEM_TYPE_WITH_DATA (convert_eol);
|
|
2843
|
771
|
2844 static void
|
2286
|
2845 convert_eol_print (Lisp_Object cs, Lisp_Object printcharfun,
|
|
2846 int UNUSED (escapeflag))
|
771
|
2847 {
|
|
2848 struct convert_eol_coding_system *data =
|
|
2849 XCODING_SYSTEM_TYPE_DATA (cs, convert_eol);
|
|
2850
|
|
2851 write_fmt_string (printcharfun, "(%s)",
|
|
2852 data->subtype == EOL_LF ? "lf" :
|
|
2853 data->subtype == EOL_CRLF ? "crlf" :
|
|
2854 data->subtype == EOL_CR ? "cr" :
|
793
|
2855 data->subtype == EOL_AUTODETECT ? "nil" :
|
2500
|
2856 (ABORT(), ""));
|
771
|
2857 }
|
|
2858
|
|
2859 static enum source_sink_type
|
2286
|
2860 convert_eol_conversion_end_type (Lisp_Object UNUSED (codesys))
|
771
|
2861 {
|
|
2862 return DECODES_CHARACTER_TO_CHARACTER;
|
|
2863 }
|
|
2864
|
|
2865 static int
|
|
2866 convert_eol_putprop (Lisp_Object codesys,
|
|
2867 Lisp_Object key,
|
|
2868 Lisp_Object value)
|
|
2869 {
|
|
2870 struct convert_eol_coding_system *data =
|
|
2871 XCODING_SYSTEM_TYPE_DATA (codesys, convert_eol);
|
|
2872
|
|
2873 if (EQ (key, Qsubtype))
|
|
2874 {
|
|
2875 if (EQ (value, Qlf) /* || EQ (value, Qunix) */)
|
|
2876 data->subtype = EOL_LF;
|
|
2877 else if (EQ (value, Qcrlf) /* || EQ (value, Qdos) */)
|
|
2878 data->subtype = EOL_CRLF;
|
|
2879 else if (EQ (value, Qcr) /* || EQ (value, Qmac) */)
|
|
2880 data->subtype = EOL_CR;
|
793
|
2881 else if (EQ (value, Qnil))
|
771
|
2882 data->subtype = EOL_AUTODETECT;
|
|
2883 else invalid_constant ("Unrecognized eol type", value);
|
|
2884 }
|
|
2885 else
|
|
2886 return 0;
|
|
2887 return 1;
|
|
2888 }
|
|
2889
|
|
2890 static Lisp_Object
|
|
2891 convert_eol_getprop (Lisp_Object coding_system, Lisp_Object prop)
|
|
2892 {
|
|
2893 struct convert_eol_coding_system *data =
|
|
2894 XCODING_SYSTEM_TYPE_DATA (coding_system, convert_eol);
|
|
2895
|
|
2896 if (EQ (prop, Qsubtype))
|
|
2897 {
|
|
2898 switch (data->subtype)
|
|
2899 {
|
|
2900 case EOL_LF: return Qlf;
|
|
2901 case EOL_CRLF: return Qcrlf;
|
|
2902 case EOL_CR: return Qcr;
|
793
|
2903 case EOL_AUTODETECT: return Qnil;
|
2500
|
2904 default: ABORT ();
|
771
|
2905 }
|
|
2906 }
|
|
2907
|
|
2908 return Qunbound;
|
|
2909 }
|
|
2910
|
|
2911 static void
|
|
2912 convert_eol_init_coding_stream (struct coding_stream *str)
|
|
2913 {
|
|
2914 struct convert_eol_coding_stream *data =
|
|
2915 CODING_STREAM_TYPE_DATA (str, convert_eol);
|
|
2916 data->actual = XCODING_SYSTEM_CONVERT_EOL_SUBTYPE (str->codesys);
|
|
2917 }
|
|
2918
|
|
2919 static Bytecount
|
867
|
2920 convert_eol_convert (struct coding_stream *str, const Ibyte *src,
|
771
|
2921 unsigned_char_dynarr *dst, Bytecount n)
|
|
2922 {
|
|
2923 if (str->direction == CODING_DECODE)
|
|
2924 {
|
|
2925 struct convert_eol_coding_stream *data =
|
|
2926 CODING_STREAM_TYPE_DATA (str, convert_eol);
|
|
2927
|
|
2928 if (data->actual == EOL_AUTODETECT)
|
|
2929 {
|
|
2930 Bytecount n2 = n;
|
867
|
2931 const Ibyte *src2 = src;
|
771
|
2932
|
|
2933 for (; n2; n2--)
|
|
2934 {
|
867
|
2935 Ibyte c = *src2++;
|
771
|
2936 if (c == '\n')
|
|
2937 {
|
|
2938 data->actual = EOL_LF;
|
|
2939 break;
|
|
2940 }
|
|
2941 else if (c == '\r')
|
|
2942 {
|
|
2943 if (n2 == 1)
|
|
2944 {
|
|
2945 /* If we're seeing a '\r' at the end of the data, then
|
|
2946 reject the '\r' right now so it doesn't become an
|
|
2947 issue in the code below -- unless we're at the end of
|
|
2948 the stream, in which case we can't do that (because
|
|
2949 then the '\r' will never get written out), and in any
|
|
2950 case we should be recognizing it at EOL_CR format. */
|
|
2951 if (str->eof)
|
|
2952 data->actual = EOL_CR;
|
|
2953 else
|
|
2954 n--;
|
|
2955 break;
|
|
2956 }
|
|
2957 else if (*src2 == '\n')
|
|
2958 data->actual = EOL_CRLF;
|
|
2959 else
|
|
2960 data->actual = EOL_CR;
|
|
2961 break;
|
|
2962 }
|
|
2963 }
|
|
2964 }
|
|
2965
|
|
2966 /* str->eof is set, the caller reached EOF on the other end and has
|
|
2967 no new data to give us. The only data we get is the data we
|
|
2968 rejected from last time. */
|
|
2969 if (data->actual == EOL_LF || data->actual == EOL_AUTODETECT ||
|
|
2970 (str->eof))
|
|
2971 Dynarr_add_many (dst, src, n);
|
|
2972 else
|
|
2973 {
|
867
|
2974 const Ibyte *end = src + n;
|
771
|
2975 while (1)
|
|
2976 {
|
|
2977 /* Find the next section with no \r and add it. */
|
867
|
2978 const Ibyte *runstart = src;
|
|
2979 src = (Ibyte *) memchr (src, '\r', end - src);
|
771
|
2980 if (!src)
|
|
2981 src = end;
|
|
2982 Dynarr_add_many (dst, runstart, src - runstart);
|
|
2983 /* Stop if at end ... */
|
|
2984 if (src == end)
|
|
2985 break;
|
|
2986 /* ... else, translate as necessary. */
|
|
2987 src++;
|
|
2988 if (data->actual == EOL_CR)
|
|
2989 Dynarr_add (dst, '\n');
|
|
2990 /* We need to be careful here with CRLF. If we see a CR at the
|
|
2991 end of the data, we don't know if it's part of a CRLF, so we
|
|
2992 reject it. Otherwise: If it's part of a CRLF, eat it and
|
|
2993 loop; the following LF gets added next time around. If it's
|
|
2994 not part of a CRLF, add the CR and loop. The following
|
|
2995 character will be processed in the next loop iteration. This
|
|
2996 correctly handles a sequence like CR+CR+LF. */
|
|
2997 else if (src == end)
|
|
2998 return n - 1; /* reject the CR at the end; we'll get it again
|
|
2999 next time the convert method is called */
|
|
3000 else if (*src != '\n')
|
|
3001 Dynarr_add (dst, '\r');
|
|
3002 }
|
|
3003 }
|
|
3004
|
|
3005 return n;
|
|
3006 }
|
|
3007 else
|
|
3008 {
|
|
3009 enum eol_type subtype =
|
|
3010 XCODING_SYSTEM_CONVERT_EOL_SUBTYPE (str->codesys);
|
867
|
3011 const Ibyte *end = src + n;
|
771
|
3012
|
|
3013 /* We try to be relatively efficient here. */
|
|
3014 if (subtype == EOL_LF)
|
|
3015 Dynarr_add_many (dst, src, n);
|
|
3016 else
|
|
3017 {
|
|
3018 while (1)
|
|
3019 {
|
|
3020 /* Find the next section with no \n and add it. */
|
867
|
3021 const Ibyte *runstart = src;
|
|
3022 src = (Ibyte *) memchr (src, '\n', end - src);
|
771
|
3023 if (!src)
|
|
3024 src = end;
|
|
3025 Dynarr_add_many (dst, runstart, src - runstart);
|
|
3026 /* Stop if at end ... */
|
|
3027 if (src == end)
|
|
3028 break;
|
|
3029 /* ... else, skip over \n and add its translation. */
|
|
3030 src++;
|
|
3031 Dynarr_add (dst, '\r');
|
|
3032 if (subtype == EOL_CRLF)
|
|
3033 Dynarr_add (dst, '\n');
|
|
3034 }
|
|
3035 }
|
|
3036
|
|
3037 return n;
|
|
3038 }
|
|
3039 }
|
|
3040
|
|
3041 static Lisp_Object
|
|
3042 convert_eol_canonicalize_after_coding (struct coding_stream *str)
|
|
3043 {
|
|
3044 struct convert_eol_coding_stream *data =
|
|
3045 CODING_STREAM_TYPE_DATA (str, convert_eol);
|
|
3046
|
|
3047 if (str->direction == CODING_ENCODE)
|
|
3048 return str->codesys;
|
|
3049
|
|
3050 switch (data->actual)
|
|
3051 {
|
|
3052 case EOL_LF: return Fget_coding_system (Qconvert_eol_lf);
|
|
3053 case EOL_CRLF: return Fget_coding_system (Qconvert_eol_crlf);
|
|
3054 case EOL_CR: return Fget_coding_system (Qconvert_eol_cr);
|
|
3055 case EOL_AUTODETECT: return str->codesys;
|
2500
|
3056 default: ABORT (); return Qnil;
|
771
|
3057 }
|
|
3058 }
|
|
3059
|
|
3060
|
|
3061 /************************************************************************/
|
|
3062 /* Undecided methods */
|
|
3063 /************************************************************************/
|
|
3064
|
|
3065 /* Do autodetection. We can autodetect the EOL type only, the coding
|
|
3066 system only, or both. We only do autodetection when decoding; when
|
|
3067 encoding, we just pass the data through.
|
|
3068
|
|
3069 When doing just EOL detection, a coding system can be specified; if so,
|
|
3070 we will decode this data through the coding system before doing EOL
|
|
3071 detection. The reason for specifying this is so that
|
|
3072 canonicalize-after-coding works: We will canonicalize the specified
|
|
3073 coding system into the appropriate EOL type. When doing both coding and
|
|
3074 EOL detection, we do similar canonicalization, and also catch situations
|
|
3075 where the EOL type is overspecified, i.e. the detected coding system
|
|
3076 specifies an EOL type, and either switch to the equivalent
|
|
3077 non-EOL-processing coding system (if possible), or terminate EOL
|
|
3078 detection and use the specified EOL type. This prevents data from being
|
|
3079 EOL-processed twice.
|
|
3080 */
|
|
3081
|
|
3082 struct undecided_coding_system
|
|
3083 {
|
|
3084 int do_eol, do_coding;
|
|
3085 Lisp_Object cs;
|
|
3086 };
|
|
3087
|
|
3088 struct undecided_coding_stream
|
|
3089 {
|
|
3090 Lisp_Object actual;
|
|
3091 /* Either 2 or 3 lstreams here; see undecided_convert */
|
|
3092 struct chain_coding_stream c;
|
|
3093
|
|
3094 struct detection_state *st;
|
|
3095 };
|
|
3096
|
1204
|
3097 static const struct memory_description undecided_coding_system_description[] = {
|
|
3098 { XD_LISP_OBJECT, offsetof (struct undecided_coding_system, cs) },
|
771
|
3099 { XD_END }
|
|
3100 };
|
|
3101
|
1204
|
3102 static const struct memory_description undecided_coding_stream_description_1 [] = {
|
|
3103 { XD_LISP_OBJECT, offsetof (struct undecided_coding_stream, actual) },
|
2367
|
3104 { XD_BLOCK_ARRAY, offsetof (struct undecided_coding_stream, c),
|
2551
|
3105 1, { &chain_coding_stream_description } },
|
1204
|
3106 { XD_END }
|
|
3107 };
|
|
3108
|
|
3109 const struct sized_memory_description undecided_coding_stream_description = {
|
|
3110 sizeof (struct undecided_coding_stream), undecided_coding_stream_description_1
|
|
3111 };
|
|
3112
|
|
3113 DEFINE_CODING_SYSTEM_TYPE_WITH_DATA (undecided);
|
|
3114
|
771
|
3115 static void
|
|
3116 undecided_init (Lisp_Object codesys)
|
|
3117 {
|
|
3118 struct undecided_coding_system *data =
|
|
3119 XCODING_SYSTEM_TYPE_DATA (codesys, undecided);
|
|
3120
|
|
3121 data->cs = Qnil;
|
|
3122 }
|
|
3123
|
|
3124 static void
|
|
3125 undecided_mark (Lisp_Object codesys)
|
|
3126 {
|
|
3127 struct undecided_coding_system *data =
|
|
3128 XCODING_SYSTEM_TYPE_DATA (codesys, undecided);
|
|
3129
|
|
3130 mark_object (data->cs);
|
|
3131 }
|
|
3132
|
|
3133 static void
|
|
3134 undecided_print (Lisp_Object cs, Lisp_Object printcharfun, int escapeflag)
|
|
3135 {
|
|
3136 struct undecided_coding_system *data =
|
|
3137 XCODING_SYSTEM_TYPE_DATA (cs, undecided);
|
|
3138 int need_space = 0;
|
|
3139
|
826
|
3140 write_c_string (printcharfun, "(");
|
771
|
3141 if (data->do_eol)
|
|
3142 {
|
826
|
3143 write_c_string (printcharfun, "do-eol");
|
771
|
3144 need_space = 1;
|
|
3145 }
|
|
3146 if (data->do_coding)
|
|
3147 {
|
|
3148 if (need_space)
|
826
|
3149 write_c_string (printcharfun, " ");
|
|
3150 write_c_string (printcharfun, "do-coding");
|
771
|
3151 need_space = 1;
|
|
3152 }
|
|
3153 if (!NILP (data->cs))
|
|
3154 {
|
|
3155 if (need_space)
|
826
|
3156 write_c_string (printcharfun, " ");
|
|
3157 write_c_string (printcharfun, "coding-system=");
|
771
|
3158 print_coding_system_in_print_method (data->cs, printcharfun, escapeflag);
|
|
3159 }
|
826
|
3160 write_c_string (printcharfun, ")");
|
771
|
3161 }
|
|
3162
|
|
3163 static void
|
|
3164 undecided_mark_coding_stream (struct coding_stream *str)
|
|
3165 {
|
1204
|
3166 mark_object (CODING_STREAM_TYPE_DATA (str, undecided)->actual);
|
771
|
3167 chain_mark_coding_stream_1 (&CODING_STREAM_TYPE_DATA (str, undecided)->c);
|
|
3168 }
|
|
3169
|
|
3170 static int
|
|
3171 undecided_putprop (Lisp_Object codesys, Lisp_Object key, Lisp_Object value)
|
|
3172 {
|
|
3173 struct undecided_coding_system *data =
|
|
3174 XCODING_SYSTEM_TYPE_DATA (codesys, undecided);
|
|
3175
|
|
3176 if (EQ (key, Qdo_eol))
|
|
3177 data->do_eol = 1;
|
|
3178 else if (EQ (key, Qdo_coding))
|
|
3179 data->do_coding = 1;
|
|
3180 else if (EQ (key, Qcoding_system))
|
|
3181 data->cs = get_coding_system_for_text_file (value, 0);
|
|
3182 else
|
|
3183 return 0;
|
|
3184 return 1;
|
|
3185 }
|
|
3186
|
|
3187 static Lisp_Object
|
|
3188 undecided_getprop (Lisp_Object codesys, Lisp_Object prop)
|
|
3189 {
|
|
3190 struct undecided_coding_system *data =
|
|
3191 XCODING_SYSTEM_TYPE_DATA (codesys, undecided);
|
|
3192
|
|
3193 if (EQ (prop, Qdo_eol))
|
|
3194 return data->do_eol ? Qt : Qnil;
|
|
3195 if (EQ (prop, Qdo_coding))
|
|
3196 return data->do_coding ? Qt : Qnil;
|
|
3197 if (EQ (prop, Qcoding_system))
|
|
3198 return data->cs;
|
|
3199 return Qunbound;
|
|
3200 }
|
|
3201
|
|
3202 static struct detection_state *
|
|
3203 allocate_detection_state (void)
|
|
3204 {
|
|
3205 int i;
|
|
3206 Bytecount size = MAX_ALIGN_SIZE (sizeof (struct detection_state));
|
|
3207 struct detection_state *block;
|
|
3208
|
|
3209 for (i = 0; i < coding_detector_count; i++)
|
|
3210 size += MAX_ALIGN_SIZE (Dynarr_at (all_coding_detectors, i).data_size);
|
|
3211
|
|
3212 block = (struct detection_state *) xmalloc_and_zero (size);
|
|
3213
|
|
3214 size = MAX_ALIGN_SIZE (sizeof (struct detection_state));
|
|
3215 for (i = 0; i < coding_detector_count; i++)
|
|
3216 {
|
|
3217 block->data_offset[i] = size;
|
|
3218 size += MAX_ALIGN_SIZE (Dynarr_at (all_coding_detectors, i).data_size);
|
|
3219 }
|
|
3220
|
|
3221 return block;
|
|
3222 }
|
|
3223
|
|
3224 static void
|
|
3225 free_detection_state (struct detection_state *st)
|
|
3226 {
|
|
3227 int i;
|
|
3228
|
|
3229 for (i = 0; i < coding_detector_count; i++)
|
|
3230 {
|
|
3231 if (Dynarr_at (all_coding_detectors, i).finalize_detection_state_method)
|
|
3232 Dynarr_at (all_coding_detectors, i).finalize_detection_state_method
|
|
3233 (st);
|
|
3234 }
|
|
3235
|
1726
|
3236 xfree (st, struct detection_state *);
|
771
|
3237 }
|
|
3238
|
|
3239 static int
|
|
3240 coding_category_symbol_to_id (Lisp_Object symbol)
|
428
|
3241 {
|
|
3242 int i;
|
|
3243
|
|
3244 CHECK_SYMBOL (symbol);
|
771
|
3245 for (i = 0; i < coding_detector_count; i++)
|
|
3246 {
|
|
3247 detector_category_dynarr *cats =
|
|
3248 Dynarr_at (all_coding_detectors, i).cats;
|
|
3249 int j;
|
|
3250
|
|
3251 for (j = 0; j < Dynarr_length (cats); j++)
|
|
3252 if (EQ (Dynarr_at (cats, j).sym, symbol))
|
|
3253 return Dynarr_at (cats, j).id;
|
|
3254 }
|
|
3255
|
563
|
3256 invalid_constant ("Unrecognized coding category", symbol);
|
1204
|
3257 RETURN_NOT_REACHED (0);
|
428
|
3258 }
|
|
3259
|
771
|
3260 static Lisp_Object
|
|
3261 coding_category_id_to_symbol (int id)
|
428
|
3262 {
|
|
3263 int i;
|
771
|
3264
|
|
3265 for (i = 0; i < coding_detector_count; i++)
|
|
3266 {
|
|
3267 detector_category_dynarr *cats =
|
|
3268 Dynarr_at (all_coding_detectors, i).cats;
|
|
3269 int j;
|
|
3270
|
|
3271 for (j = 0; j < Dynarr_length (cats); j++)
|
|
3272 if (id == Dynarr_at (cats, j).id)
|
|
3273 return Dynarr_at (cats, j).sym;
|
|
3274 }
|
|
3275
|
2500
|
3276 ABORT ();
|
771
|
3277 return Qnil; /* (usually) not reached */
|
428
|
3278 }
|
|
3279
|
771
|
3280 static Lisp_Object
|
|
3281 detection_result_number_to_symbol (enum detection_result result)
|
428
|
3282 {
|
1494
|
3283 /* let compiler warn if not all enumerators are handled */
|
|
3284 switch (result) {
|
|
3285 #define FROB(sym, num) case num: return (sym)
|
771
|
3286 FROB (Qnear_certainty, DET_NEAR_CERTAINTY);
|
|
3287 FROB (Qquite_probable, DET_QUITE_PROBABLE);
|
|
3288 FROB (Qsomewhat_likely, DET_SOMEWHAT_LIKELY);
|
1494
|
3289 FROB (Qslightly_likely, DET_SLIGHTLY_LIKELY);
|
771
|
3290 FROB (Qas_likely_as_unlikely, DET_AS_LIKELY_AS_UNLIKELY);
|
|
3291 FROB (Qsomewhat_unlikely, DET_SOMEWHAT_UNLIKELY);
|
|
3292 FROB (Qquite_improbable, DET_QUITE_IMPROBABLE);
|
|
3293 FROB (Qnearly_impossible, DET_NEARLY_IMPOSSIBLE);
|
|
3294 #undef FROB
|
1494
|
3295 }
|
771
|
3296
|
2500
|
3297 ABORT ();
|
771
|
3298 return Qnil; /* (usually) not reached */
|
|
3299 }
|
|
3300
|
778
|
3301 #if 0 /* not used */
|
771
|
3302 static enum detection_result
|
|
3303 detection_result_symbol_to_number (Lisp_Object symbol)
|
|
3304 {
|
1494
|
3305 /* using switch here would be bad style, and doesn't help */
|
771
|
3306 #define FROB(sym, num) if (EQ (symbol, sym)) return (num)
|
|
3307 FROB (Qnear_certainty, DET_NEAR_CERTAINTY);
|
|
3308 FROB (Qquite_probable, DET_QUITE_PROBABLE);
|
|
3309 FROB (Qsomewhat_likely, DET_SOMEWHAT_LIKELY);
|
1494
|
3310 FROB (Qslightly_likely, DET_SLIGHTLY_LIKELY);
|
771
|
3311 FROB (Qas_likely_as_unlikely, DET_AS_LIKELY_AS_UNLIKELY);
|
|
3312 FROB (Qsomewhat_unlikely, DET_SOMEWHAT_UNLIKELY);
|
|
3313 FROB (Qquite_improbable, DET_QUITE_IMPROBABLE);
|
|
3314 FROB (Qnearly_impossible, DET_NEARLY_IMPOSSIBLE);
|
|
3315 #undef FROB
|
|
3316
|
|
3317 invalid_constant ("Unrecognized detection result", symbol);
|
|
3318 return ((enum detection_result) 0); /* not reached */
|
|
3319 }
|
778
|
3320 #endif /* 0 */
|
771
|
3321
|
|
3322 /* Set all detection results for a given detector to a specified value. */
|
|
3323 void
|
|
3324 set_detection_results (struct detection_state *st, int detector, int given)
|
|
3325 {
|
|
3326 detector_category_dynarr *cats =
|
|
3327 Dynarr_at (all_coding_detectors, detector).cats;
|
|
3328 int i;
|
|
3329
|
|
3330 for (i = 0; i < Dynarr_length (cats); i++)
|
|
3331 st->categories[Dynarr_at (cats, i).id] = given;
|
|
3332 }
|
428
|
3333
|
|
3334 static int
|
|
3335 acceptable_control_char_p (int c)
|
|
3336 {
|
|
3337 switch (c)
|
|
3338 {
|
|
3339 /* Allow and ignore control characters that you might
|
|
3340 reasonably see in a text file */
|
|
3341 case '\r':
|
|
3342 case '\n':
|
|
3343 case '\t':
|
|
3344 case 7: /* bell */
|
|
3345 case 8: /* backspace */
|
|
3346 case 11: /* vertical tab */
|
|
3347 case 12: /* form feed */
|
|
3348 case 26: /* MS-DOS C-z junk */
|
|
3349 case 31: /* '^_' -- for info */
|
|
3350 return 1;
|
|
3351 default:
|
|
3352 return 0;
|
|
3353 }
|
|
3354 }
|
|
3355
|
771
|
3356 #ifdef DEBUG_XEMACS
|
|
3357
|
|
3358 static UExtbyte
|
|
3359 hex_digit_to_char (int digit)
|
428
|
3360 {
|
771
|
3361 if (digit < 10)
|
|
3362 return digit + '0';
|
|
3363 else
|
|
3364 return digit - 10 + 'A';
|
428
|
3365 }
|
|
3366
|
771
|
3367 static void
|
|
3368 output_bytes_in_ascii_and_hex (const UExtbyte *src, Bytecount n)
|
428
|
3369 {
|
3425
|
3370 Extbyte *ascii = alloca_array (Extbyte, n + 1);
|
|
3371 Extbyte *hex = alloca_array (Extbyte, 3 * n + 1);
|
771
|
3372 int i;
|
3413
|
3373 DECLARE_EISTRING (eistr_ascii);
|
|
3374 DECLARE_EISTRING (eistr_hex);
|
771
|
3375
|
|
3376 for (i = 0; i < n; i++)
|
428
|
3377 {
|
3425
|
3378 Extbyte c = src[i];
|
771
|
3379 if (c < 0x20)
|
|
3380 ascii[i] = '.';
|
428
|
3381 else
|
771
|
3382 ascii[i] = c;
|
|
3383 hex[3 * i] = hex_digit_to_char (c >> 4);
|
|
3384 hex[3 * i + 1] = hex_digit_to_char (c & 0xF);
|
|
3385 hex[3 * i + 2] = ' ';
|
428
|
3386 }
|
771
|
3387 ascii[i] = '\0';
|
|
3388 hex[3 * i - 1] = '\0';
|
3413
|
3389
|
|
3390 eicpy_ext(eistr_hex, hex, Qbinary);
|
|
3391 eicpy_ext(eistr_ascii, ascii, Qbinary);
|
|
3392
|
3425
|
3393 stderr_out ("%s %s", eidata(eistr_ascii), eidata(eistr_hex));
|
428
|
3394 }
|
|
3395
|
771
|
3396 #endif /* DEBUG_XEMACS */
|
|
3397
|
|
3398 /* Attempt to determine the encoding of the given text. Before calling
|
|
3399 this function for the first time, you must zero out the detection state.
|
428
|
3400
|
|
3401 Returns:
|
|
3402
|
771
|
3403 0 == keep going
|
|
3404 1 == stop
|
428
|
3405 */
|
|
3406
|
|
3407 static int
|
771
|
3408 detect_coding_type (struct detection_state *st, const UExtbyte *src,
|
|
3409 Bytecount n)
|
428
|
3410 {
|
771
|
3411 Bytecount n2 = n;
|
|
3412 const UExtbyte *src2 = src;
|
|
3413 int i;
|
|
3414
|
|
3415 #ifdef DEBUG_XEMACS
|
|
3416 if (!NILP (Vdebug_coding_detection))
|
|
3417 {
|
|
3418 int bytes = min (16, n);
|
|
3419 stderr_out ("detect_coding_type: processing %ld bytes\n", n);
|
|
3420 stderr_out ("First %d: ", bytes);
|
|
3421 output_bytes_in_ascii_and_hex (src, bytes);
|
|
3422 stderr_out ("\nLast %d: ", bytes);
|
|
3423 output_bytes_in_ascii_and_hex (src + n - bytes, bytes);
|
|
3424 stderr_out ("\n");
|
|
3425 }
|
|
3426 #endif /* DEBUG_XEMACS */
|
428
|
3427 if (!st->seen_non_ascii)
|
|
3428 {
|
771
|
3429 for (; n2; n2--, src2++)
|
428
|
3430 {
|
771
|
3431 UExtbyte c = *src2;
|
428
|
3432 if ((c < 0x20 && !acceptable_control_char_p (c)) || c >= 0x80)
|
|
3433 {
|
|
3434 st->seen_non_ascii = 1;
|
|
3435 break;
|
|
3436 }
|
|
3437 }
|
|
3438 }
|
|
3439
|
771
|
3440 for (i = 0; i < coding_detector_count; i++)
|
|
3441 Dynarr_at (all_coding_detectors, i).detect_method (st, src, n);
|
|
3442
|
|
3443 st->bytes_seen += n;
|
|
3444
|
|
3445 #ifdef DEBUG_XEMACS
|
|
3446 if (!NILP (Vdebug_coding_detection))
|
|
3447 {
|
|
3448 stderr_out ("seen_non_ascii: %d\n", st->seen_non_ascii);
|
1494
|
3449 if (coding_detector_category_count <= 0)
|
|
3450 stderr_out ("found %d detector categories\n",
|
|
3451 coding_detector_category_count);
|
771
|
3452 for (i = 0; i < coding_detector_category_count; i++)
|
|
3453 stderr_out_lisp
|
|
3454 ("%s: %s\n",
|
|
3455 2,
|
|
3456 coding_category_id_to_symbol (i),
|
|
3457 detection_result_number_to_symbol ((enum detection_result)
|
|
3458 st->categories[i]));
|
|
3459 }
|
|
3460 #endif /* DEBUG_XEMACS */
|
|
3461
|
|
3462 {
|
|
3463 int not_unlikely = 0;
|
|
3464 int retval;
|
|
3465
|
|
3466 for (i = 0; i < coding_detector_category_count; i++)
|
|
3467 if (st->categories[i] >= 0)
|
|
3468 not_unlikely++;
|
|
3469
|
|
3470 retval = (not_unlikely <= 1
|
|
3471 #if 0 /* this is bogus */
|
|
3472 || st->bytes_seen >= MAX_BYTES_PROCESSED_FOR_DETECTION
|
428
|
3473 #endif
|
771
|
3474 );
|
|
3475
|
|
3476 #ifdef DEBUG_XEMACS
|
|
3477 if (!NILP (Vdebug_coding_detection))
|
|
3478 stderr_out ("detect_coding_type: returning %d (%s)\n",
|
|
3479 retval, retval ? "stop" : "keep going");
|
|
3480 #endif /* DEBUG_XEMACS */
|
|
3481
|
|
3482 return retval;
|
428
|
3483 }
|
|
3484 }
|
|
3485
|
|
3486 static Lisp_Object
|
771
|
3487 detected_coding_system (struct detection_state *st)
|
428
|
3488 {
|
771
|
3489 int i;
|
|
3490 int even = 1;
|
|
3491
|
|
3492 if (st->seen_non_ascii)
|
|
3493 {
|
|
3494 for (i = 0; i < coding_detector_category_count; i++)
|
|
3495 if (st->categories[i] != DET_AS_LIKELY_AS_UNLIKELY)
|
|
3496 {
|
|
3497 even = 0;
|
|
3498 break;
|
|
3499 }
|
|
3500 }
|
|
3501
|
|
3502 /* #### Here we are ignoring the results of detection when it's all
|
|
3503 ASCII. This is obviously a bad thing. But we need to fix up the
|
|
3504 existing detection methods somewhat before we can switch. */
|
|
3505 if (even)
|
428
|
3506 {
|
|
3507 /* If the file was entirely or basically ASCII, use the
|
|
3508 default value of `buffer-file-coding-system'. */
|
|
3509 Lisp_Object retval =
|
|
3510 XBUFFER (Vbuffer_defaults)->buffer_file_coding_system;
|
|
3511 if (!NILP (retval))
|
|
3512 {
|
771
|
3513 retval = find_coding_system_for_text_file (retval, 0);
|
428
|
3514 if (NILP (retval))
|
|
3515 {
|
|
3516 warn_when_safe
|
|
3517 (Qbad_variable, Qwarning,
|
|
3518 "Invalid `default-buffer-file-coding-system', set to nil");
|
|
3519 XBUFFER (Vbuffer_defaults)->buffer_file_coding_system = Qnil;
|
|
3520 }
|
|
3521 }
|
|
3522 if (NILP (retval))
|
|
3523 retval = Fget_coding_system (Qraw_text);
|
|
3524 return retval;
|
|
3525 }
|
|
3526 else
|
|
3527 {
|
771
|
3528 int likelihood;
|
|
3529 Lisp_Object retval = Qnil;
|
|
3530
|
|
3531 /* Look through the coding categories first by likelihood and then by
|
|
3532 priority and find the first one that is allowed. */
|
|
3533
|
|
3534 for (likelihood = DET_HIGHEST; likelihood >= DET_LOWEST; likelihood--)
|
428
|
3535 {
|
771
|
3536 for (i = 0; i < coding_detector_category_count; i++)
|
|
3537 {
|
|
3538 int cat = coding_category_by_priority[i];
|
|
3539 if (st->categories[cat] == likelihood &&
|
|
3540 !NILP (coding_category_system[cat]))
|
|
3541 {
|
|
3542 retval = (get_coding_system_for_text_file
|
|
3543 (coding_category_system[cat], 0));
|
|
3544 if (likelihood < DET_AS_LIKELY_AS_UNLIKELY)
|
|
3545 warn_when_safe_lispobj
|
|
3546 (intern ("detection"),
|
793
|
3547 Qwarning,
|
771
|
3548 emacs_sprintf_string_lisp
|
|
3549 (
|
|
3550 "Detected coding %s is unlikely to be correct (likelihood == `%s')",
|
|
3551 Qnil, 2, XCODING_SYSTEM_NAME (retval),
|
|
3552 detection_result_number_to_symbol
|
|
3553 ((enum detection_result) likelihood)));
|
|
3554 return retval;
|
|
3555 }
|
|
3556 }
|
428
|
3557 }
|
771
|
3558
|
|
3559 return Fget_coding_system (Qraw_text);
|
428
|
3560 }
|
|
3561 }
|
|
3562
|
1347
|
3563 /* Look for a coding system in the string (skipping over leading
|
|
3564 blanks). If found, return it, otherwise nil. */
|
|
3565
|
|
3566 static Lisp_Object
|
2531
|
3567 snarf_coding_system (const UExtbyte *p, Bytecount len)
|
1347
|
3568 {
|
|
3569 Bytecount n;
|
2531
|
3570 UExtbyte *name;
|
1347
|
3571
|
|
3572 while (*p == ' ' || *p == '\t') p++, len--;
|
|
3573 len = min (len, 1000);
|
|
3574 name = alloca_ibytes (len + 1);
|
|
3575 memcpy (name, p, len);
|
|
3576 name[len] = '\0';
|
|
3577
|
|
3578 /* Get coding system name */
|
|
3579 /* Characters valid in a MIME charset name (rfc 1521),
|
|
3580 and in a Lisp symbol name. */
|
|
3581 n = qxestrspn (name,
|
|
3582 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
3583 "abcdefghijklmnopqrstuvwxyz"
|
|
3584 "0123456789"
|
|
3585 "!$%&*+-.^_{|}~");
|
|
3586 if (n > 0)
|
|
3587 {
|
|
3588 name[n] = '\0';
|
2531
|
3589 /* This call to intern_int() is OK because we already verified that
|
|
3590 there are only ASCII characters in the string */
|
|
3591 return find_coding_system_for_text_file (intern_int ((Ibyte *) name), 0);
|
1347
|
3592 }
|
|
3593
|
|
3594 return Qnil;
|
|
3595 }
|
|
3596
|
428
|
3597 /* Given a seekable read stream and potential coding system and EOL type
|
|
3598 as specified, do any autodetection that is called for. If the
|
|
3599 coding system and/or EOL type are not `autodetect', they will be left
|
|
3600 alone; but this function will never return an autodetect coding system
|
|
3601 or EOL type.
|
|
3602
|
|
3603 This function does not automatically fetch subsidiary coding systems;
|
|
3604 that should be unnecessary with the explicit eol-type argument. */
|
|
3605
|
|
3606 #define LENGTH(string_constant) (sizeof (string_constant) - 1)
|
|
3607
|
771
|
3608 static Lisp_Object
|
|
3609 unwind_free_detection_state (Lisp_Object opaque)
|
|
3610 {
|
|
3611 struct detection_state *st =
|
|
3612 (struct detection_state *) get_opaque_ptr (opaque);
|
|
3613 free_detection_state (st);
|
|
3614 free_opaque_ptr (opaque);
|
|
3615 return Qnil;
|
|
3616 }
|
|
3617
|
1347
|
3618 /* #### This duplicates code in `find-coding-system-magic-cookie-in-file'
|
|
3619 in files.el. Look into combining them. */
|
|
3620
|
771
|
3621 static Lisp_Object
|
|
3622 look_for_coding_system_magic_cookie (const UExtbyte *data, Bytecount len)
|
428
|
3623 {
|
771
|
3624 const UExtbyte *p;
|
|
3625 const UExtbyte *scan_end;
|
2531
|
3626 Bytecount cookie_len;
|
771
|
3627
|
|
3628 /* Look for initial "-*-"; mode line prefix */
|
|
3629 for (p = data,
|
|
3630 scan_end = data + len - LENGTH ("-*-coding:?-*-");
|
|
3631 p <= scan_end
|
|
3632 && *p != '\n'
|
|
3633 && *p != '\r';
|
|
3634 p++)
|
|
3635 if (*p == '-' && *(p+1) == '*' && *(p+2) == '-')
|
|
3636 {
|
|
3637 const UExtbyte *local_vars_beg = p + 3;
|
|
3638 /* Look for final "-*-"; mode line suffix */
|
|
3639 for (p = local_vars_beg,
|
|
3640 scan_end = data + len - LENGTH ("-*-");
|
|
3641 p <= scan_end
|
428
|
3642 && *p != '\n'
|
|
3643 && *p != '\r';
|
771
|
3644 p++)
|
|
3645 if (*p == '-' && *(p+1) == '*' && *(p+2) == '-')
|
|
3646 {
|
|
3647 const UExtbyte *suffix = p;
|
|
3648 /* Look for "coding:" */
|
|
3649 for (p = local_vars_beg,
|
|
3650 scan_end = suffix - LENGTH ("coding:?");
|
|
3651 p <= scan_end;
|
|
3652 p++)
|
|
3653 if (memcmp ("coding:", p, LENGTH ("coding:")) == 0
|
|
3654 && (p == local_vars_beg
|
|
3655 || (*(p-1) == ' ' ||
|
|
3656 *(p-1) == '\t' ||
|
|
3657 *(p-1) == ';')))
|
|
3658 {
|
|
3659 p += LENGTH ("coding:");
|
1347
|
3660 return snarf_coding_system (p, suffix - p);
|
771
|
3661 break;
|
|
3662 }
|
|
3663 break;
|
|
3664 }
|
|
3665 break;
|
|
3666 }
|
|
3667
|
2531
|
3668 /* Look for ;;;###coding system */
|
|
3669
|
|
3670 cookie_len = LENGTH (";;;###coding system: ");
|
|
3671
|
|
3672 for (p = data,
|
|
3673 scan_end = data + len - cookie_len;
|
|
3674 p <= scan_end;
|
|
3675 p++)
|
1347
|
3676 {
|
2531
|
3677 if (*p == ';' && !memcmp (p, ";;;###coding system: ", cookie_len))
|
|
3678 {
|
|
3679 const UExtbyte *suffix;
|
|
3680
|
|
3681 p += cookie_len;
|
|
3682 suffix = p;
|
|
3683 while (suffix < scan_end && !isspace (*suffix))
|
|
3684 suffix++;
|
|
3685 return snarf_coding_system (p, suffix - p);
|
|
3686 }
|
1347
|
3687 }
|
|
3688
|
|
3689 return Qnil;
|
771
|
3690 }
|
|
3691
|
|
3692 static Lisp_Object
|
|
3693 determine_real_coding_system (Lstream *stream)
|
|
3694 {
|
|
3695 struct detection_state *st = allocate_detection_state ();
|
|
3696 int depth = record_unwind_protect (unwind_free_detection_state,
|
|
3697 make_opaque_ptr (st));
|
|
3698 UExtbyte buf[4096];
|
|
3699 Bytecount nread = Lstream_read (stream, buf, sizeof (buf));
|
|
3700 Lisp_Object coding_system = look_for_coding_system_magic_cookie (buf, nread);
|
|
3701
|
|
3702 if (NILP (coding_system))
|
|
3703 {
|
|
3704 while (1)
|
|
3705 {
|
|
3706 if (detect_coding_type (st, buf, nread))
|
428
|
3707 break;
|
771
|
3708 nread = Lstream_read (stream, buf, sizeof (buf));
|
|
3709 if (nread == 0)
|
|
3710 break;
|
428
|
3711 }
|
771
|
3712
|
|
3713 coding_system = detected_coding_system (st);
|
428
|
3714 }
|
|
3715
|
|
3716 Lstream_rewind (stream);
|
771
|
3717
|
|
3718 unbind_to (depth);
|
|
3719 return coding_system;
|
|
3720 }
|
|
3721
|
|
3722 static void
|
|
3723 undecided_init_coding_stream (struct coding_stream *str)
|
|
3724 {
|
|
3725 struct undecided_coding_stream *data =
|
|
3726 CODING_STREAM_TYPE_DATA (str, undecided);
|
|
3727 struct undecided_coding_system *csdata =
|
|
3728 XCODING_SYSTEM_TYPE_DATA (str->codesys, undecided);
|
|
3729
|
|
3730 data->actual = Qnil;
|
|
3731
|
|
3732 if (str->direction == CODING_DECODE)
|
|
3733 {
|
|
3734 Lstream *lst = str->other_end;
|
|
3735
|
|
3736 if ((lst->flags & LSTREAM_FL_READ) &&
|
|
3737 Lstream_seekable_p (lst) &&
|
|
3738 csdata->do_coding)
|
|
3739 /* We can determine the coding system now. */
|
|
3740 data->actual = determine_real_coding_system (lst);
|
|
3741 }
|
1494
|
3742
|
|
3743 #ifdef DEBUG_XEMACS
|
|
3744 if (!NILP (Vdebug_coding_detection))
|
|
3745 stderr_out_lisp ("detected coding system: %s\n", 1, data->actual);
|
|
3746 #endif /* DEBUG_XEMACS */
|
771
|
3747 }
|
|
3748
|
|
3749 static void
|
|
3750 undecided_rewind_coding_stream (struct coding_stream *str)
|
|
3751 {
|
|
3752 chain_rewind_coding_stream_1 (&CODING_STREAM_TYPE_DATA (str, undecided)->c);
|
|
3753 }
|
|
3754
|
|
3755 static void
|
|
3756 undecided_finalize_coding_stream (struct coding_stream *str)
|
|
3757 {
|
|
3758 struct undecided_coding_stream *data =
|
|
3759 CODING_STREAM_TYPE_DATA (str, undecided);
|
|
3760
|
|
3761 chain_finalize_coding_stream_1
|
|
3762 (&CODING_STREAM_TYPE_DATA (str, undecided)->c);
|
|
3763 if (data->st)
|
|
3764 free_detection_state (data->st);
|
|
3765 }
|
|
3766
|
|
3767 static Lisp_Object
|
|
3768 undecided_canonicalize (Lisp_Object codesys)
|
|
3769 {
|
|
3770 struct undecided_coding_system *csdata =
|
|
3771 XCODING_SYSTEM_TYPE_DATA (codesys, undecided);
|
|
3772 if (!csdata->do_eol && !csdata->do_coding)
|
|
3773 return NILP (csdata->cs) ? Fget_coding_system (Qbinary) : csdata->cs;
|
|
3774 if (csdata->do_eol && !csdata->do_coding && NILP (csdata->cs))
|
|
3775 return Fget_coding_system (Qconvert_eol_autodetect);
|
|
3776 return codesys;
|
|
3777 }
|
|
3778
|
|
3779 static Bytecount
|
|
3780 undecided_convert (struct coding_stream *str, const UExtbyte *src,
|
|
3781 unsigned_char_dynarr *dst, Bytecount n)
|
|
3782 {
|
|
3783 int first_time = 0;
|
|
3784
|
|
3785 if (str->direction == CODING_DECODE)
|
|
3786 {
|
|
3787 /* At this point, we have only the following possibilities:
|
|
3788
|
|
3789 do_eol && do_coding
|
|
3790 do_coding only
|
|
3791 do_eol only and a coding system was specified
|
|
3792
|
|
3793 Other possibilities are removed during undecided_canonicalize.
|
|
3794
|
|
3795 Therefore, our substreams are either
|
|
3796
|
|
3797 lstream_coding -> lstream_dynarr, or
|
|
3798 lstream_coding -> lstream_eol -> lstream_dynarr.
|
|
3799 */
|
|
3800 struct undecided_coding_system *csdata =
|
|
3801 XCODING_SYSTEM_TYPE_DATA (str->codesys, undecided);
|
|
3802 struct undecided_coding_stream *data =
|
|
3803 CODING_STREAM_TYPE_DATA (str, undecided);
|
|
3804
|
|
3805 if (str->eof)
|
|
3806 {
|
|
3807 /* Each will close the next. We need to close now because more
|
|
3808 data may be generated. */
|
|
3809 if (data->c.initted)
|
|
3810 Lstream_close (XLSTREAM (data->c.lstreams[0]));
|
|
3811 return n;
|
|
3812 }
|
|
3813
|
|
3814 if (!data->c.initted)
|
|
3815 {
|
|
3816 data->c.lstream_count = csdata->do_eol ? 3 : 2;
|
|
3817 data->c.lstreams = xnew_array (Lisp_Object, data->c.lstream_count);
|
|
3818
|
|
3819 data->c.lstreams[data->c.lstream_count - 1] =
|
|
3820 make_dynarr_output_stream (dst);
|
|
3821 Lstream_set_buffering
|
|
3822 (XLSTREAM (data->c.lstreams[data->c.lstream_count - 1]),
|
|
3823 LSTREAM_UNBUFFERED, 0);
|
|
3824 if (csdata->do_eol)
|
|
3825 {
|
|
3826 data->c.lstreams[1] =
|
|
3827 make_coding_output_stream
|
|
3828 (XLSTREAM (data->c.lstreams[data->c.lstream_count - 1]),
|
|
3829 Fget_coding_system (Qconvert_eol_autodetect),
|
800
|
3830 CODING_DECODE, 0);
|
771
|
3831 Lstream_set_buffering
|
|
3832 (XLSTREAM (data->c.lstreams[1]),
|
|
3833 LSTREAM_UNBUFFERED, 0);
|
|
3834 }
|
|
3835
|
|
3836 data->c.lstreams[0] =
|
|
3837 make_coding_output_stream
|
|
3838 (XLSTREAM (data->c.lstreams[1]),
|
|
3839 /* Substitute binary if we need to detect the encoding */
|
|
3840 csdata->do_coding ? Qbinary : csdata->cs,
|
800
|
3841 CODING_DECODE, 0);
|
771
|
3842 Lstream_set_buffering (XLSTREAM (data->c.lstreams[0]),
|
|
3843 LSTREAM_UNBUFFERED, 0);
|
|
3844
|
|
3845 first_time = 1;
|
|
3846 data->c.initted = 1;
|
|
3847 }
|
|
3848
|
|
3849 /* If necessary, do encoding-detection now. We do this when we're a
|
|
3850 writing stream or a non-seekable reading stream, meaning that we
|
|
3851 can't just process the whole input, rewind, and start over. */
|
|
3852
|
|
3853 if (csdata->do_coding)
|
|
3854 {
|
|
3855 int actual_was_nil = NILP (data->actual);
|
|
3856 if (NILP (data->actual))
|
|
3857 {
|
|
3858 if (!data->st)
|
|
3859 data->st = allocate_detection_state ();
|
|
3860 if (first_time)
|
|
3861 /* #### This is cheesy. What we really ought to do is buffer
|
|
3862 up a certain minimum amount of data to get a better result.
|
|
3863 */
|
|
3864 data->actual = look_for_coding_system_magic_cookie (src, n);
|
|
3865 if (NILP (data->actual))
|
|
3866 {
|
|
3867 /* #### This is cheesy. What we really ought to do is buffer
|
|
3868 up a certain minimum amount of data so as to get a less
|
|
3869 random result when doing subprocess detection. */
|
|
3870 detect_coding_type (data->st, src, n);
|
|
3871 data->actual = detected_coding_system (data->st);
|
|
3872 }
|
|
3873 }
|
|
3874 /* We need to set the detected coding system if we actually have
|
|
3875 such a coding system but didn't before. That is the case
|
|
3876 either when we just detected it in the previous code or when
|
|
3877 it was detected during undecided_init_coding_stream(). We
|
|
3878 can check for that using first_time. */
|
|
3879 if (!NILP (data->actual) && (actual_was_nil || first_time))
|
|
3880 {
|
|
3881 /* If the detected coding system doesn't allow for EOL
|
|
3882 autodetection, try to get the equivalent that does;
|
|
3883 otherwise, disable EOL detection (overriding whatever
|
|
3884 may already have been detected). */
|
|
3885 if (XCODING_SYSTEM_EOL_TYPE (data->actual) != EOL_AUTODETECT)
|
|
3886 {
|
|
3887 if (!NILP (XCODING_SYSTEM_SUBSIDIARY_PARENT (data->actual)))
|
|
3888 data->actual =
|
|
3889 XCODING_SYSTEM_SUBSIDIARY_PARENT (data->actual);
|
|
3890 else if (data->c.lstream_count == 3)
|
|
3891 set_coding_stream_coding_system
|
|
3892 (XLSTREAM (data->c.lstreams[1]),
|
|
3893 Fget_coding_system (Qidentity));
|
|
3894 }
|
|
3895 set_coding_stream_coding_system
|
|
3896 (XLSTREAM (data->c.lstreams[0]), data->actual);
|
|
3897 }
|
|
3898 }
|
|
3899
|
|
3900 if (Lstream_write (XLSTREAM (data->c.lstreams[0]), src, n) < 0)
|
|
3901 return -1;
|
|
3902 return n;
|
|
3903 }
|
|
3904 else
|
|
3905 return no_conversion_convert (str, src, dst, n);
|
|
3906 }
|
|
3907
|
|
3908 static Lisp_Object
|
|
3909 undecided_canonicalize_after_coding (struct coding_stream *str)
|
|
3910 {
|
|
3911 struct undecided_coding_stream *data =
|
|
3912 CODING_STREAM_TYPE_DATA (str, undecided);
|
|
3913 Lisp_Object ret, eolret;
|
|
3914
|
|
3915 if (str->direction == CODING_ENCODE)
|
|
3916 return str->codesys;
|
|
3917
|
|
3918 if (!data->c.initted)
|
|
3919 return Fget_coding_system (Qundecided);
|
|
3920
|
|
3921 ret = coding_stream_canonicalize_after_coding
|
|
3922 (XLSTREAM (data->c.lstreams[0]));
|
|
3923 if (NILP (ret))
|
|
3924 ret = Fget_coding_system (Qundecided);
|
|
3925 if (XCODING_SYSTEM_EOL_TYPE (ret) != EOL_AUTODETECT)
|
|
3926 return ret;
|
|
3927 eolret = coding_stream_canonicalize_after_coding
|
|
3928 (XLSTREAM (data->c.lstreams[1]));
|
|
3929 if (!EQ (XCODING_SYSTEM_TYPE (eolret), Qconvert_eol))
|
|
3930 return ret;
|
|
3931 return
|
|
3932 Fsubsidiary_coding_system (ret, Fcoding_system_property (eolret,
|
|
3933 Qsubtype));
|
|
3934 }
|
|
3935
|
|
3936
|
|
3937 /************************************************************************/
|
|
3938 /* Lisp interface: Coding category functions and detection */
|
|
3939 /************************************************************************/
|
|
3940
|
|
3941 DEFUN ("coding-category-list", Fcoding_category_list, 0, 0, 0, /*
|
|
3942 Return a list of all recognized coding categories.
|
|
3943 */
|
|
3944 ())
|
|
3945 {
|
|
3946 int i;
|
|
3947 Lisp_Object list = Qnil;
|
|
3948
|
|
3949 for (i = 0; i < coding_detector_count; i++)
|
|
3950 {
|
|
3951 detector_category_dynarr *cats =
|
|
3952 Dynarr_at (all_coding_detectors, i).cats;
|
|
3953 int j;
|
|
3954
|
|
3955 for (j = 0; j < Dynarr_length (cats); j++)
|
|
3956 list = Fcons (Dynarr_at (cats, j).sym, list);
|
|
3957 }
|
|
3958
|
|
3959 return Fnreverse (list);
|
|
3960 }
|
|
3961
|
|
3962 DEFUN ("set-coding-priority-list", Fset_coding_priority_list, 1, 1, 0, /*
|
|
3963 Change the priority order of the coding categories.
|
|
3964 LIST should be list of coding categories, in descending order of
|
|
3965 priority. Unspecified coding categories will be lower in priority
|
|
3966 than all specified ones, in the same relative order they were in
|
|
3967 previously.
|
|
3968 */
|
|
3969 (list))
|
|
3970 {
|
|
3971 int *category_to_priority =
|
|
3972 alloca_array (int, coding_detector_category_count);
|
|
3973 int i, j;
|
|
3974
|
|
3975 /* First generate a list that maps coding categories to priorities. */
|
|
3976
|
|
3977 for (i = 0; i < coding_detector_category_count; i++)
|
|
3978 category_to_priority[i] = -1;
|
|
3979
|
|
3980 /* Highest priority comes from the specified list. */
|
|
3981 i = 0;
|
2367
|
3982 {
|
|
3983 EXTERNAL_LIST_LOOP_2 (elt, list)
|
|
3984 {
|
|
3985 int cat = coding_category_symbol_to_id (elt);
|
|
3986
|
|
3987 if (category_to_priority[cat] >= 0)
|
|
3988 sferror ("Duplicate coding category in list", elt);
|
|
3989 category_to_priority[cat] = i++;
|
|
3990 }
|
|
3991 }
|
771
|
3992
|
|
3993 /* Now go through the existing categories by priority to retrieve
|
|
3994 the categories not yet specified and preserve their priority
|
|
3995 order. */
|
|
3996 for (j = 0; j < coding_detector_category_count; j++)
|
|
3997 {
|
|
3998 int cat = coding_category_by_priority[j];
|
|
3999 if (category_to_priority[cat] < 0)
|
|
4000 category_to_priority[cat] = i++;
|
|
4001 }
|
|
4002
|
|
4003 /* Now we need to construct the inverse of the mapping we just
|
|
4004 constructed. */
|
|
4005
|
|
4006 for (i = 0; i < coding_detector_category_count; i++)
|
|
4007 coding_category_by_priority[category_to_priority[i]] = i;
|
|
4008
|
|
4009 /* Phew! That was confusing. */
|
|
4010 return Qnil;
|
|
4011 }
|
|
4012
|
|
4013 DEFUN ("coding-priority-list", Fcoding_priority_list, 0, 0, 0, /*
|
|
4014 Return a list of coding categories in descending order of priority.
|
|
4015 */
|
|
4016 ())
|
|
4017 {
|
|
4018 int i;
|
|
4019 Lisp_Object list = Qnil;
|
|
4020
|
|
4021 for (i = 0; i < coding_detector_category_count; i++)
|
|
4022 list =
|
|
4023 Fcons (coding_category_id_to_symbol (coding_category_by_priority[i]),
|
|
4024 list);
|
|
4025 return Fnreverse (list);
|
|
4026 }
|
|
4027
|
|
4028 DEFUN ("set-coding-category-system", Fset_coding_category_system, 2, 2, 0, /*
|
|
4029 Change the coding system associated with a coding category.
|
|
4030 */
|
|
4031 (coding_category, coding_system))
|
|
4032 {
|
|
4033 coding_category_system[coding_category_symbol_to_id (coding_category)] =
|
|
4034 Fget_coding_system (coding_system);
|
|
4035 return Qnil;
|
|
4036 }
|
|
4037
|
|
4038 DEFUN ("coding-category-system", Fcoding_category_system, 1, 1, 0, /*
|
|
4039 Return the coding system associated with a coding category.
|
|
4040 */
|
|
4041 (coding_category))
|
|
4042 {
|
|
4043 Lisp_Object sys =
|
|
4044 coding_category_system[coding_category_symbol_to_id (coding_category)];
|
|
4045
|
|
4046 if (!NILP (sys))
|
|
4047 return XCODING_SYSTEM_NAME (sys);
|
|
4048 return Qnil;
|
|
4049 }
|
|
4050
|
800
|
4051 /* Detect the encoding of STREAM. Assumes stream is at the begnning and will
|
|
4052 read through to the end of STREAM, leaving it there but open. */
|
|
4053
|
771
|
4054 Lisp_Object
|
|
4055 detect_coding_stream (Lisp_Object stream)
|
|
4056 {
|
|
4057 Lisp_Object val = Qnil;
|
|
4058 struct gcpro gcpro1, gcpro2, gcpro3;
|
|
4059 UExtbyte random_buffer[65536];
|
|
4060 Lisp_Object binary_instream =
|
|
4061 make_coding_input_stream
|
|
4062 (XLSTREAM (stream), Qbinary,
|
814
|
4063 CODING_ENCODE, LSTREAM_FL_NO_CLOSE_OTHER);
|
771
|
4064 Lisp_Object decstream =
|
|
4065 make_coding_input_stream
|
|
4066 (XLSTREAM (binary_instream),
|
800
|
4067 Qundecided, CODING_DECODE, 0);
|
771
|
4068 Lstream *decstr = XLSTREAM (decstream);
|
|
4069
|
|
4070 GCPRO3 (decstream, stream, binary_instream);
|
|
4071 /* Read and discard all data; detection happens as a side effect of this,
|
|
4072 and we examine what was detected afterwards. */
|
|
4073 while (Lstream_read (decstr, random_buffer, sizeof (random_buffer)) > 0)
|
|
4074 ;
|
|
4075
|
|
4076 val = coding_stream_detected_coding_system (decstr);
|
|
4077 Lstream_close (decstr);
|
|
4078 Lstream_delete (decstr);
|
|
4079 Lstream_delete (XLSTREAM (binary_instream));
|
|
4080 UNGCPRO;
|
|
4081 return val;
|
428
|
4082 }
|
|
4083
|
|
4084 DEFUN ("detect-coding-region", Fdetect_coding_region, 2, 3, 0, /*
|
|
4085 Detect coding system of the text in the region between START and END.
|
444
|
4086 Return a list of possible coding systems ordered by priority.
|
3025
|
4087 If only ASCII characters are found, return `undecided' or one of
|
428
|
4088 its subsidiary coding systems according to a detected end-of-line
|
|
4089 type. Optional arg BUFFER defaults to the current buffer.
|
|
4090 */
|
|
4091 (start, end, buffer))
|
|
4092 {
|
|
4093 Lisp_Object val = Qnil;
|
|
4094 struct buffer *buf = decode_buffer (buffer, 0);
|
665
|
4095 Charbpos b, e;
|
771
|
4096 Lisp_Object lb_instream;
|
428
|
4097
|
|
4098 get_buffer_range_char (buf, start, end, &b, &e, 0);
|
|
4099 lb_instream = make_lisp_buffer_input_stream (buf, b, e, 0);
|
771
|
4100
|
|
4101 val = detect_coding_stream (lb_instream);
|
|
4102 Lstream_delete (XLSTREAM (lb_instream));
|
428
|
4103 return val;
|
|
4104 }
|
|
4105
|
|
4106
|
771
|
4107
|
|
4108 #ifdef DEBUG_XEMACS
|
|
4109
|
428
|
4110 /************************************************************************/
|
771
|
4111 /* Internal methods */
|
|
4112 /************************************************************************/
|
|
4113
|
|
4114 /* Raw (internally-formatted) data. */
|
|
4115 DEFINE_CODING_SYSTEM_TYPE (internal);
|
428
|
4116
|
665
|
4117 static Bytecount
|
2286
|
4118 internal_convert (struct coding_stream *UNUSED (str), const UExtbyte *src,
|
771
|
4119 unsigned_char_dynarr *dst, Bytecount n)
|
|
4120 {
|
|
4121 Bytecount orign = n;
|
|
4122 Dynarr_add_many (dst, src, n);
|
|
4123 return orign;
|
|
4124 }
|
|
4125
|
|
4126 #endif /* DEBUG_XEMACS */
|
|
4127
|
|
4128
|
|
4129
|
|
4130 #ifdef HAVE_ZLIB
|
|
4131
|
|
4132 /************************************************************************/
|
|
4133 /* Gzip methods */
|
|
4134 /************************************************************************/
|
|
4135
|
|
4136 struct gzip_coding_system
|
428
|
4137 {
|
771
|
4138 int level; /* 0 through 9, or -1 for default */
|
|
4139 };
|
|
4140
|
|
4141 #define CODING_SYSTEM_GZIP_LEVEL(codesys) \
|
|
4142 (CODING_SYSTEM_TYPE_DATA (codesys, gzip)->level)
|
|
4143 #define XCODING_SYSTEM_GZIP_LEVEL(codesys) \
|
|
4144 (XCODING_SYSTEM_TYPE_DATA (codesys, gzip)->level)
|
|
4145
|
|
4146 struct gzip_coding_stream
|
428
|
4147 {
|
771
|
4148 z_stream stream;
|
|
4149 int stream_initted;
|
|
4150 int reached_eof; /* #### this should be handled by the caller, once we
|
|
4151 return LSTREAM_EOF */
|
|
4152 };
|
|
4153
|
1204
|
4154 static const struct memory_description
|
771
|
4155 gzip_coding_system_description[] = {
|
|
4156 { XD_END }
|
|
4157 };
|
|
4158
|
1204
|
4159 DEFINE_CODING_SYSTEM_TYPE_WITH_DATA (gzip);
|
|
4160
|
771
|
4161 enum source_sink_type
|
|
4162 gzip_conversion_end_type (Lisp_Object codesys)
|
|
4163 {
|
|
4164 return DECODES_BYTE_TO_BYTE;
|
428
|
4165 }
|
|
4166
|
|
4167 static void
|
771
|
4168 gzip_init (Lisp_Object codesys)
|
|
4169 {
|
|
4170 struct gzip_coding_system *data = XCODING_SYSTEM_TYPE_DATA (codesys, gzip);
|
|
4171 data->level = -1;
|
|
4172 }
|
|
4173
|
|
4174 static void
|
|
4175 gzip_print (Lisp_Object cs, Lisp_Object printcharfun, int escapeflag)
|
428
|
4176 {
|
771
|
4177 struct gzip_coding_system *data = XCODING_SYSTEM_TYPE_DATA (cs, gzip);
|
|
4178
|
826
|
4179 write_c_string (printcharfun, "(");
|
771
|
4180 if (data->level == -1)
|
826
|
4181 write_c_string (printcharfun, "default");
|
771
|
4182 else
|
|
4183 print_internal (make_int (data->level), printcharfun, 0);
|
826
|
4184 write_c_string (printcharfun, ")");
|
428
|
4185 }
|
|
4186
|
|
4187 static int
|
771
|
4188 gzip_putprop (Lisp_Object codesys, Lisp_Object key, Lisp_Object value)
|
428
|
4189 {
|
771
|
4190 struct gzip_coding_system *data = XCODING_SYSTEM_TYPE_DATA (codesys, gzip);
|
|
4191
|
|
4192 if (EQ (key, Qlevel))
|
428
|
4193 {
|
771
|
4194 if (EQ (value, Qdefault))
|
|
4195 data->level = -1;
|
|
4196 else
|
428
|
4197 {
|
771
|
4198 CHECK_INT (value);
|
|
4199 check_int_range (XINT (value), 0, 9);
|
|
4200 data->level = XINT (value);
|
428
|
4201 }
|
|
4202 }
|
|
4203 else
|
771
|
4204 return 0;
|
|
4205 return 1;
|
428
|
4206 }
|
|
4207
|
|
4208 static Lisp_Object
|
771
|
4209 gzip_getprop (Lisp_Object coding_system, Lisp_Object prop)
|
428
|
4210 {
|
771
|
4211 struct gzip_coding_system *data =
|
|
4212 XCODING_SYSTEM_TYPE_DATA (coding_system, gzip);
|
|
4213
|
|
4214 if (EQ (prop, Qlevel))
|
428
|
4215 {
|
771
|
4216 if (data->level == -1)
|
|
4217 return Qdefault;
|
|
4218 return make_int (data->level);
|
428
|
4219 }
|
771
|
4220
|
|
4221 return Qunbound;
|
428
|
4222 }
|
|
4223
|
|
4224 static void
|
771
|
4225 gzip_init_coding_stream (struct coding_stream *str)
|
428
|
4226 {
|
771
|
4227 struct gzip_coding_stream *data = CODING_STREAM_TYPE_DATA (str, gzip);
|
|
4228 if (data->stream_initted)
|
428
|
4229 {
|
771
|
4230 if (str->direction == CODING_DECODE)
|
|
4231 inflateEnd (&data->stream);
|
|
4232 else
|
|
4233 deflateEnd (&data->stream);
|
|
4234 data->stream_initted = 0;
|
428
|
4235 }
|
771
|
4236 data->reached_eof = 0;
|
428
|
4237 }
|
|
4238
|
|
4239 static void
|
771
|
4240 gzip_rewind_coding_stream (struct coding_stream *str)
|
428
|
4241 {
|
771
|
4242 gzip_init_coding_stream (str);
|
428
|
4243 }
|
|
4244
|
771
|
4245 static Bytecount
|
|
4246 gzip_convert (struct coding_stream *str,
|
|
4247 const UExtbyte *src,
|
|
4248 unsigned_char_dynarr *dst, Bytecount n)
|
428
|
4249 {
|
771
|
4250 struct gzip_coding_stream *data = CODING_STREAM_TYPE_DATA (str, gzip);
|
|
4251 int zerr;
|
|
4252 if (str->direction == CODING_DECODE)
|
428
|
4253 {
|
771
|
4254 if (data->reached_eof)
|
|
4255 return n; /* eat the data */
|
|
4256
|
|
4257 if (!data->stream_initted)
|
428
|
4258 {
|
771
|
4259 xzero (data->stream);
|
|
4260 if (inflateInit (&data->stream) != Z_OK)
|
|
4261 return LSTREAM_ERROR;
|
|
4262 data->stream_initted = 1;
|
428
|
4263 }
|
771
|
4264
|
|
4265 data->stream.next_in = (Bytef *) src;
|
|
4266 data->stream.avail_in = n;
|
|
4267
|
|
4268 /* Normally we stop when we've fed all data to the decompressor; but
|
|
4269 if we're at the end of the input, and the decompressor hasn't
|
|
4270 reported EOF, we need to keep going, as there might be more output
|
|
4271 to generate. Z_OK from the decompressor means input was processed
|
|
4272 or output was generated; if neither, we break out of the loop.
|
|
4273 Other return values are:
|
|
4274
|
|
4275 Z_STREAM_END EOF from decompressor
|
|
4276 Z_DATA_ERROR Corrupted data
|
|
4277 Z_BUF_ERROR No progress possible (this should happen if
|
|
4278 we try to feed it an incomplete file)
|
|
4279 Z_MEM_ERROR Out of memory
|
|
4280 Z_STREAM_ERROR (should never happen)
|
|
4281 Z_NEED_DICT (#### when will this happen?)
|
|
4282 */
|
|
4283 while (data->stream.avail_in > 0 || str->eof)
|
|
4284 {
|
|
4285 /* Reserve an output buffer of the same size as the input buffer;
|
|
4286 if that's not enough, we keep reserving the same size. */
|
|
4287 Bytecount reserved = n;
|
|
4288 Dynarr_add_many (dst, 0, reserved);
|
|
4289 /* Careful here! Don't retrieve the pointer until after
|
|
4290 reserving the space, or it might be bogus */
|
|
4291 data->stream.next_out =
|
|
4292 Dynarr_atp (dst, Dynarr_length (dst) - reserved);
|
|
4293 data->stream.avail_out = reserved;
|
|
4294 zerr = inflate (&data->stream, Z_NO_FLUSH);
|
|
4295 /* Lop off the unused portion */
|
|
4296 Dynarr_set_size (dst, Dynarr_length (dst) - data->stream.avail_out);
|
|
4297 if (zerr != Z_OK)
|
|
4298 break;
|
|
4299 }
|
|
4300
|
|
4301 if (zerr == Z_STREAM_END)
|
|
4302 data->reached_eof = 1;
|
|
4303
|
|
4304 if ((Bytecount) data->stream.avail_in < n)
|
|
4305 return n - data->stream.avail_in;
|
|
4306
|
|
4307 if (zerr == Z_OK || zerr == Z_STREAM_END)
|
|
4308 return 0;
|
|
4309
|
|
4310 return LSTREAM_ERROR;
|
428
|
4311 }
|
|
4312 else
|
|
4313 {
|
771
|
4314 if (!data->stream_initted)
|
|
4315 {
|
|
4316 int level = XCODING_SYSTEM_GZIP_LEVEL (str->codesys);
|
|
4317 xzero (data->stream);
|
|
4318 if (deflateInit (&data->stream,
|
|
4319 level == -1 ? Z_DEFAULT_COMPRESSION : level) !=
|
|
4320 Z_OK)
|
|
4321 return LSTREAM_ERROR;
|
|
4322 data->stream_initted = 1;
|
428
|
4323 }
|
771
|
4324
|
|
4325 data->stream.next_in = (Bytef *) src;
|
|
4326 data->stream.avail_in = n;
|
|
4327
|
|
4328 /* Normally we stop when we've fed all data to the compressor; but if
|
|
4329 we're at the end of the input, and the compressor hasn't reported
|
|
4330 EOF, we need to keep going, as there might be more output to
|
|
4331 generate. (To signal EOF on our end, we set the FLUSH parameter
|
|
4332 to Z_FINISH; when all data is output, Z_STREAM_END will be
|
|
4333 returned.) Z_OK from the compressor means input was processed or
|
|
4334 output was generated; if neither, we break out of the loop. Other
|
|
4335 return values are:
|
|
4336
|
|
4337 Z_STREAM_END EOF from compressor
|
|
4338 Z_BUF_ERROR No progress possible (should never happen)
|
|
4339 Z_STREAM_ERROR (should never happen)
|
|
4340 */
|
|
4341 while (data->stream.avail_in > 0 || str->eof)
|
|
4342 {
|
|
4343 /* Reserve an output buffer of the same size as the input buffer;
|
|
4344 if that's not enough, we keep reserving the same size. */
|
|
4345 Bytecount reserved = n;
|
|
4346 Dynarr_add_many (dst, 0, reserved);
|
|
4347 /* Careful here! Don't retrieve the pointer until after
|
|
4348 reserving the space, or it might be bogus */
|
|
4349 data->stream.next_out =
|
|
4350 Dynarr_atp (dst, Dynarr_length (dst) - reserved);
|
|
4351 data->stream.avail_out = reserved;
|
|
4352 zerr =
|
|
4353 deflate (&data->stream,
|
|
4354 str->eof ? Z_FINISH : Z_NO_FLUSH);
|
|
4355 /* Lop off the unused portion */
|
|
4356 Dynarr_set_size (dst, Dynarr_length (dst) - data->stream.avail_out);
|
|
4357 if (zerr != Z_OK)
|
|
4358 break;
|
|
4359 }
|
|
4360
|
|
4361 if ((Bytecount) data->stream.avail_in < n)
|
|
4362 return n - data->stream.avail_in;
|
|
4363
|
|
4364 if (zerr == Z_OK || zerr == Z_STREAM_END)
|
|
4365 return 0;
|
|
4366
|
|
4367 return LSTREAM_ERROR;
|
428
|
4368 }
|
|
4369 }
|
|
4370
|
771
|
4371 #endif /* HAVE_ZLIB */
|
428
|
4372
|
|
4373
|
|
4374 /************************************************************************/
|
|
4375 /* Initialization */
|
|
4376 /************************************************************************/
|
|
4377
|
|
4378 void
|
|
4379 syms_of_file_coding (void)
|
|
4380 {
|
442
|
4381 INIT_LRECORD_IMPLEMENTATION (coding_system);
|
|
4382
|
771
|
4383 DEFSUBR (Fvalid_coding_system_type_p);
|
|
4384 DEFSUBR (Fcoding_system_type_list);
|
428
|
4385 DEFSUBR (Fcoding_system_p);
|
|
4386 DEFSUBR (Ffind_coding_system);
|
|
4387 DEFSUBR (Fget_coding_system);
|
|
4388 DEFSUBR (Fcoding_system_list);
|
|
4389 DEFSUBR (Fcoding_system_name);
|
|
4390 DEFSUBR (Fmake_coding_system);
|
|
4391 DEFSUBR (Fcopy_coding_system);
|
440
|
4392 DEFSUBR (Fcoding_system_canonical_name_p);
|
|
4393 DEFSUBR (Fcoding_system_alias_p);
|
|
4394 DEFSUBR (Fcoding_system_aliasee);
|
428
|
4395 DEFSUBR (Fdefine_coding_system_alias);
|
|
4396 DEFSUBR (Fsubsidiary_coding_system);
|
771
|
4397 DEFSUBR (Fcoding_system_base);
|
|
4398 DEFSUBR (Fcoding_system_used_for_io);
|
428
|
4399
|
|
4400 DEFSUBR (Fcoding_system_type);
|
771
|
4401 DEFSUBR (Fcoding_system_description);
|
428
|
4402 DEFSUBR (Fcoding_system_property);
|
|
4403
|
|
4404 DEFSUBR (Fcoding_category_list);
|
|
4405 DEFSUBR (Fset_coding_priority_list);
|
|
4406 DEFSUBR (Fcoding_priority_list);
|
|
4407 DEFSUBR (Fset_coding_category_system);
|
|
4408 DEFSUBR (Fcoding_category_system);
|
|
4409
|
|
4410 DEFSUBR (Fdetect_coding_region);
|
|
4411 DEFSUBR (Fdecode_coding_region);
|
|
4412 DEFSUBR (Fencode_coding_region);
|
563
|
4413 DEFSYMBOL_MULTIWORD_PREDICATE (Qcoding_systemp);
|
|
4414 DEFSYMBOL (Qno_conversion);
|
771
|
4415 DEFSYMBOL (Qconvert_eol);
|
|
4416 DEFSYMBOL (Qconvert_eol_autodetect);
|
|
4417 DEFSYMBOL (Qconvert_eol_lf);
|
|
4418 DEFSYMBOL (Qconvert_eol_cr);
|
|
4419 DEFSYMBOL (Qconvert_eol_crlf);
|
563
|
4420 DEFSYMBOL (Qraw_text);
|
771
|
4421
|
563
|
4422 DEFSYMBOL (Qmnemonic);
|
|
4423 DEFSYMBOL (Qeol_type);
|
|
4424 DEFSYMBOL (Qpost_read_conversion);
|
|
4425 DEFSYMBOL (Qpre_write_conversion);
|
|
4426
|
771
|
4427 DEFSYMBOL (Qtranslation_table_for_decode);
|
|
4428 DEFSYMBOL (Qtranslation_table_for_encode);
|
|
4429 DEFSYMBOL (Qsafe_chars);
|
|
4430 DEFSYMBOL (Qsafe_charsets);
|
|
4431 DEFSYMBOL (Qmime_charset);
|
|
4432 DEFSYMBOL (Qvalid_codes);
|
|
4433
|
563
|
4434 DEFSYMBOL (Qcr);
|
|
4435 DEFSYMBOL (Qlf);
|
|
4436 DEFSYMBOL (Qcrlf);
|
|
4437 DEFSYMBOL (Qeol_cr);
|
|
4438 DEFSYMBOL (Qeol_lf);
|
|
4439 DEFSYMBOL (Qeol_crlf);
|
|
4440 DEFSYMBOL (Qencode);
|
|
4441 DEFSYMBOL (Qdecode);
|
428
|
4442
|
771
|
4443 DEFSYMBOL (Qnear_certainty);
|
|
4444 DEFSYMBOL (Qquite_probable);
|
|
4445 DEFSYMBOL (Qsomewhat_likely);
|
1494
|
4446 DEFSYMBOL (Qslightly_likely);
|
771
|
4447 DEFSYMBOL (Qas_likely_as_unlikely);
|
|
4448 DEFSYMBOL (Qsomewhat_unlikely);
|
|
4449 DEFSYMBOL (Qquite_improbable);
|
|
4450 DEFSYMBOL (Qnearly_impossible);
|
|
4451
|
|
4452 DEFSYMBOL (Qdo_eol);
|
|
4453 DEFSYMBOL (Qdo_coding);
|
|
4454
|
|
4455 DEFSYMBOL (Qcanonicalize_after_coding);
|
|
4456
|
|
4457 DEFSYMBOL (Qescape_quoted);
|
|
4458
|
|
4459 #ifdef HAVE_ZLIB
|
|
4460 DEFSYMBOL (Qgzip);
|
|
4461 #endif
|
|
4462
|
428
|
4463 }
|
|
4464
|
|
4465 void
|
|
4466 lstream_type_create_file_coding (void)
|
|
4467 {
|
771
|
4468 LSTREAM_HAS_METHOD (coding, reader);
|
|
4469 LSTREAM_HAS_METHOD (coding, writer);
|
|
4470 LSTREAM_HAS_METHOD (coding, rewinder);
|
|
4471 LSTREAM_HAS_METHOD (coding, seekable_p);
|
|
4472 LSTREAM_HAS_METHOD (coding, marker);
|
|
4473 LSTREAM_HAS_METHOD (coding, flusher);
|
|
4474 LSTREAM_HAS_METHOD (coding, closer);
|
|
4475 LSTREAM_HAS_METHOD (coding, finalizer);
|
|
4476 }
|
|
4477
|
|
4478 void
|
|
4479 coding_system_type_create (void)
|
|
4480 {
|
|
4481 int i;
|
|
4482
|
|
4483 staticpro (&Vcoding_system_hash_table);
|
|
4484 Vcoding_system_hash_table =
|
|
4485 make_lisp_hash_table (50, HASH_TABLE_NON_WEAK, HASH_TABLE_EQ);
|
|
4486
|
|
4487 the_coding_system_type_entry_dynarr = Dynarr_new (coding_system_type_entry);
|
2367
|
4488 dump_add_root_block_ptr (&the_coding_system_type_entry_dynarr,
|
771
|
4489 &csted_description);
|
|
4490
|
|
4491 Vcoding_system_type_list = Qnil;
|
|
4492 staticpro (&Vcoding_system_type_list);
|
|
4493
|
|
4494 /* Initialize to something reasonable ... */
|
|
4495 for (i = 0; i < MAX_DETECTOR_CATEGORIES; i++)
|
|
4496 {
|
|
4497 coding_category_system[i] = Qnil;
|
1204
|
4498 dump_add_root_lisp_object (&coding_category_system[i]);
|
771
|
4499 coding_category_by_priority[i] = i;
|
|
4500 }
|
|
4501
|
|
4502 dump_add_opaque (coding_category_by_priority,
|
|
4503 sizeof (coding_category_by_priority));
|
|
4504
|
|
4505 all_coding_detectors = Dynarr_new2 (detector_dynarr, struct detector);
|
2367
|
4506 dump_add_root_block_ptr (&all_coding_detectors,
|
771
|
4507 &detector_dynarr_description);
|
|
4508
|
|
4509 dump_add_opaque_int (&coding_system_tick);
|
|
4510 dump_add_opaque_int (&coding_detector_count);
|
|
4511 dump_add_opaque_int (&coding_detector_category_count);
|
|
4512
|
|
4513 INITIALIZE_CODING_SYSTEM_TYPE (no_conversion,
|
|
4514 "no-conversion-coding-system-p");
|
|
4515 CODING_SYSTEM_HAS_METHOD (no_conversion, convert);
|
|
4516
|
|
4517 INITIALIZE_DETECTOR (no_conversion);
|
|
4518 DETECTOR_HAS_METHOD (no_conversion, detect);
|
|
4519 INITIALIZE_DETECTOR_CATEGORY (no_conversion, no_conversion);
|
|
4520
|
|
4521 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (convert_eol,
|
|
4522 "convert-eol-coding-system-p");
|
|
4523 CODING_SYSTEM_HAS_METHOD (convert_eol, print);
|
|
4524 CODING_SYSTEM_HAS_METHOD (convert_eol, convert);
|
|
4525 CODING_SYSTEM_HAS_METHOD (convert_eol, getprop);
|
|
4526 CODING_SYSTEM_HAS_METHOD (convert_eol, putprop);
|
|
4527 CODING_SYSTEM_HAS_METHOD (convert_eol, conversion_end_type);
|
|
4528 CODING_SYSTEM_HAS_METHOD (convert_eol, canonicalize_after_coding);
|
|
4529 CODING_SYSTEM_HAS_METHOD (convert_eol, init_coding_stream);
|
|
4530
|
|
4531 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (undecided,
|
|
4532 "undecided-coding-system-p");
|
|
4533 CODING_SYSTEM_HAS_METHOD (undecided, init);
|
|
4534 CODING_SYSTEM_HAS_METHOD (undecided, mark);
|
|
4535 CODING_SYSTEM_HAS_METHOD (undecided, print);
|
|
4536 CODING_SYSTEM_HAS_METHOD (undecided, convert);
|
|
4537 CODING_SYSTEM_HAS_METHOD (undecided, putprop);
|
|
4538 CODING_SYSTEM_HAS_METHOD (undecided, getprop);
|
|
4539 CODING_SYSTEM_HAS_METHOD (undecided, init_coding_stream);
|
|
4540 CODING_SYSTEM_HAS_METHOD (undecided, rewind_coding_stream);
|
|
4541 CODING_SYSTEM_HAS_METHOD (undecided, finalize_coding_stream);
|
|
4542 CODING_SYSTEM_HAS_METHOD (undecided, mark_coding_stream);
|
|
4543 CODING_SYSTEM_HAS_METHOD (undecided, canonicalize);
|
|
4544 CODING_SYSTEM_HAS_METHOD (undecided, canonicalize_after_coding);
|
|
4545
|
|
4546 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (chain, "chain-coding-system-p");
|
|
4547
|
|
4548 CODING_SYSTEM_HAS_METHOD (chain, print);
|
|
4549 CODING_SYSTEM_HAS_METHOD (chain, canonicalize);
|
|
4550 CODING_SYSTEM_HAS_METHOD (chain, init);
|
|
4551 CODING_SYSTEM_HAS_METHOD (chain, mark);
|
|
4552 CODING_SYSTEM_HAS_METHOD (chain, mark_coding_stream);
|
|
4553 CODING_SYSTEM_HAS_METHOD (chain, convert);
|
|
4554 CODING_SYSTEM_HAS_METHOD (chain, rewind_coding_stream);
|
|
4555 CODING_SYSTEM_HAS_METHOD (chain, finalize_coding_stream);
|
|
4556 CODING_SYSTEM_HAS_METHOD (chain, finalize);
|
|
4557 CODING_SYSTEM_HAS_METHOD (chain, putprop);
|
|
4558 CODING_SYSTEM_HAS_METHOD (chain, getprop);
|
|
4559 CODING_SYSTEM_HAS_METHOD (chain, conversion_end_type);
|
|
4560 CODING_SYSTEM_HAS_METHOD (chain, canonicalize_after_coding);
|
|
4561
|
|
4562 #ifdef DEBUG_XEMACS
|
|
4563 INITIALIZE_CODING_SYSTEM_TYPE (internal, "internal-coding-system-p");
|
|
4564 CODING_SYSTEM_HAS_METHOD (internal, convert);
|
|
4565 #endif
|
|
4566
|
|
4567 #ifdef HAVE_ZLIB
|
|
4568 INITIALIZE_CODING_SYSTEM_TYPE_WITH_DATA (gzip, "gzip-coding-system-p");
|
|
4569 CODING_SYSTEM_HAS_METHOD (gzip, conversion_end_type);
|
|
4570 CODING_SYSTEM_HAS_METHOD (gzip, convert);
|
|
4571 CODING_SYSTEM_HAS_METHOD (gzip, init);
|
|
4572 CODING_SYSTEM_HAS_METHOD (gzip, print);
|
|
4573 CODING_SYSTEM_HAS_METHOD (gzip, init_coding_stream);
|
|
4574 CODING_SYSTEM_HAS_METHOD (gzip, rewind_coding_stream);
|
|
4575 CODING_SYSTEM_HAS_METHOD (gzip, putprop);
|
|
4576 CODING_SYSTEM_HAS_METHOD (gzip, getprop);
|
|
4577 #endif
|
|
4578 }
|
|
4579
|
|
4580 void
|
|
4581 reinit_coding_system_type_create (void)
|
|
4582 {
|
|
4583 REINITIALIZE_CODING_SYSTEM_TYPE (no_conversion);
|
|
4584 REINITIALIZE_CODING_SYSTEM_TYPE (convert_eol);
|
|
4585 REINITIALIZE_CODING_SYSTEM_TYPE (undecided);
|
|
4586 REINITIALIZE_CODING_SYSTEM_TYPE (chain);
|
|
4587 #if 0
|
|
4588 REINITIALIZE_CODING_SYSTEM_TYPE (text_file_wrapper);
|
|
4589 #endif /* 0 */
|
|
4590 #ifdef DEBUG_XEMACS
|
|
4591 REINITIALIZE_CODING_SYSTEM_TYPE (internal);
|
|
4592 #endif
|
|
4593 #ifdef HAVE_ZLIB
|
|
4594 REINITIALIZE_CODING_SYSTEM_TYPE (gzip);
|
|
4595 #endif
|
|
4596 }
|
|
4597
|
|
4598 void
|
|
4599 reinit_vars_of_file_coding (void)
|
|
4600 {
|
428
|
4601 }
|
|
4602
|
|
4603 void
|
|
4604 vars_of_file_coding (void)
|
|
4605 {
|
771
|
4606 /* We always have file-coding support */
|
428
|
4607 Fprovide (intern ("file-coding"));
|
|
4608
|
1347
|
4609 QScoding_system_cookie = build_string (";;;###coding system: ");
|
|
4610 staticpro (&QScoding_system_cookie);
|
|
4611
|
1242
|
4612 #ifdef HAVE_DEFAULT_EOL_DETECTION
|
2297
|
4613 /* #### Find a more appropriate place for this comment.
|
|
4614 WARNING: The existing categories are intimately tied to the function
|
1242
|
4615 `coding-system-category' in coding.el. If you change a category, or
|
|
4616 change the layout of any coding system associated with a category, you
|
|
4617 need to check that function and make sure it's written properly. */
|
|
4618
|
|
4619 Fprovide (intern ("unix-default-eol-detection"));
|
|
4620 #endif
|
|
4621
|
428
|
4622 DEFVAR_LISP ("keyboard-coding-system", &Vkeyboard_coding_system /*
|
3142
|
4623 Default coding system used for TTY and X11 keyboard input.
|
|
4624 Under X11, used only to interpet the character for a key event when that
|
|
4625 event has a KeySym of NoSymbol but does have an associated string keysym,
|
|
4626 something that's seen with input methods.
|
|
4627
|
|
4628 If you need to set these things to different coding systems, call the
|
|
4629 function `set-console-tty-coding-system' for the TTY and use this variable
|
|
4630 for X11.
|
428
|
4631 */ );
|
|
4632 Vkeyboard_coding_system = Qnil;
|
|
4633
|
|
4634 DEFVAR_LISP ("terminal-coding-system", &Vterminal_coding_system /*
|
|
4635 Coding system used for TTY display output.
|
|
4636 Not used under a windowing system.
|
|
4637 */ );
|
|
4638 Vterminal_coding_system = Qnil;
|
|
4639
|
|
4640 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read /*
|
440
|
4641 Overriding coding system used when reading from a file or process.
|
|
4642 You should bind this variable with `let', but do not set it globally.
|
|
4643 If this is non-nil, it specifies the coding system that will be used
|
|
4644 to decode input on read operations, such as from a file or process.
|
|
4645 It overrides `buffer-file-coding-system-for-read',
|
428
|
4646 `insert-file-contents-pre-hook', etc. Use those variables instead of
|
440
|
4647 this one for permanent changes to the environment. */ );
|
428
|
4648 Vcoding_system_for_read = Qnil;
|
|
4649
|
|
4650 DEFVAR_LISP ("coding-system-for-write",
|
|
4651 &Vcoding_system_for_write /*
|
440
|
4652 Overriding coding system used when writing to a file or process.
|
|
4653 You should bind this variable with `let', but do not set it globally.
|
|
4654 If this is non-nil, it specifies the coding system that will be used
|
|
4655 to encode output for write operations, such as to a file or process.
|
|
4656 It overrides `buffer-file-coding-system', `write-region-pre-hook', etc.
|
|
4657 Use those variables instead of this one for permanent changes to the
|
|
4658 environment. */ );
|
428
|
4659 Vcoding_system_for_write = Qnil;
|
|
4660
|
|
4661 DEFVAR_LISP ("file-name-coding-system", &Vfile_name_coding_system /*
|
|
4662 Coding system used to convert pathnames when accessing files.
|
|
4663 */ );
|
|
4664 Vfile_name_coding_system = Qnil;
|
|
4665
|
|
4666 DEFVAR_BOOL ("enable-multibyte-characters", &enable_multibyte_characters /*
|
771
|
4667 Setting this has no effect. It is purely for FSF compatibility.
|
428
|
4668 */ );
|
|
4669 enable_multibyte_characters = 1;
|
771
|
4670
|
|
4671 Vchain_canonicalize_hash_table =
|
|
4672 make_lisp_hash_table (50, HASH_TABLE_NON_WEAK, HASH_TABLE_EQUAL);
|
|
4673 staticpro (&Vchain_canonicalize_hash_table);
|
|
4674
|
|
4675 #ifdef DEBUG_XEMACS
|
|
4676 DEFVAR_LISP ("debug-coding-detection", &Vdebug_coding_detection /*
|
|
4677 If non-nil, display debug information about detection operations in progress.
|
|
4678 Information is displayed on stderr.
|
|
4679 */ );
|
|
4680 Vdebug_coding_detection = Qnil;
|
|
4681 #endif
|
428
|
4682 }
|
|
4683
|
2297
|
4684 /* #### reformat this for consistent appearance? */
|
|
4685
|
428
|
4686 void
|
|
4687 complex_vars_of_file_coding (void)
|
|
4688 {
|
771
|
4689 Fmake_coding_system
|
|
4690 (Qconvert_eol_cr, Qconvert_eol,
|
|
4691 build_msg_string ("Convert CR to LF"),
|
|
4692 nconc2 (list6 (Qdocumentation,
|
|
4693 build_msg_string (
|
|
4694 "Converts CR (used to mark the end of a line on Macintosh systems) to LF\n"
|
|
4695 "(used internally and under Unix to mark the end of a line)."),
|
|
4696 Qmnemonic, build_string ("CR->LF"),
|
|
4697 Qsubtype, Qcr),
|
|
4698 /* VERY IMPORTANT! Tell make-coding-system not to generate
|
|
4699 subsidiaries -- it needs the coding systems we're creating
|
|
4700 to do so! */
|
|
4701 list2 (Qeol_type, Qlf)));
|
|
4702
|
|
4703 Fmake_coding_system
|
|
4704 (Qconvert_eol_lf, Qconvert_eol,
|
|
4705 build_msg_string ("Convert LF to LF (do nothing)"),
|
|
4706 nconc2 (list6 (Qdocumentation,
|
|
4707 build_msg_string (
|
|
4708 "Do nothing."),
|
|
4709 Qmnemonic, build_string ("LF->LF"),
|
|
4710 Qsubtype, Qlf),
|
|
4711 /* VERY IMPORTANT! Tell make-coding-system not to generate
|
|
4712 subsidiaries -- it needs the coding systems we're creating
|
|
4713 to do so! */
|
|
4714 list2 (Qeol_type, Qlf)));
|
|
4715
|
|
4716 Fmake_coding_system
|
|
4717 (Qconvert_eol_crlf, Qconvert_eol,
|
|
4718 build_msg_string ("Convert CRLF to LF"),
|
|
4719 nconc2 (list6 (Qdocumentation,
|
|
4720 build_msg_string (
|
|
4721 "Converts CR+LF (used to mark the end of a line on Macintosh systems) to LF\n"
|
|
4722 "(used internally and under Unix to mark the end of a line)."),
|
|
4723 Qmnemonic, build_string ("CRLF->LF"),
|
|
4724 Qsubtype, Qcrlf),
|
|
4725 /* VERY IMPORTANT! Tell make-coding-system not to generate
|
|
4726 subsidiaries -- it needs the coding systems we're creating
|
|
4727 to do so! */
|
|
4728 list2 (Qeol_type, Qlf)));
|
|
4729
|
|
4730 Fmake_coding_system
|
|
4731 (Qconvert_eol_autodetect, Qconvert_eol,
|
|
4732 build_msg_string ("Autodetect EOL type"),
|
|
4733 nconc2 (list6 (Qdocumentation,
|
|
4734 build_msg_string (
|
|
4735 "Autodetect the end-of-line type."),
|
|
4736 Qmnemonic, build_string ("Auto-EOL"),
|
793
|
4737 Qsubtype, Qnil),
|
771
|
4738 /* VERY IMPORTANT! Tell make-coding-system not to generate
|
|
4739 subsidiaries -- it needs the coding systems we're creating
|
|
4740 to do so! */
|
|
4741 list2 (Qeol_type, Qlf)));
|
|
4742
|
|
4743 Fmake_coding_system
|
|
4744 (Qundecided, Qundecided,
|
|
4745 build_msg_string ("Undecided (auto-detect)"),
|
|
4746 nconc2 (list4 (Qdocumentation,
|
|
4747 build_msg_string
|
|
4748 ("Automatically detects the correct encoding."),
|
|
4749 Qmnemonic, build_string ("Auto")),
|
|
4750 list6 (Qdo_eol, Qt, Qdo_coding, Qt,
|
|
4751 /* We do EOL detection ourselves so we don't need to be
|
|
4752 wrapped in an EOL detector. (It doesn't actually hurt,
|
|
4753 though, I don't think.) */
|
|
4754 Qeol_type, Qlf)));
|
|
4755
|
|
4756 Fmake_coding_system
|
|
4757 (intern ("undecided-dos"), Qundecided,
|
|
4758 build_msg_string ("Undecided (auto-detect) (CRLF)"),
|
|
4759 nconc2 (list4 (Qdocumentation,
|
|
4760 build_msg_string
|
|
4761 ("Automatically detects the correct encoding; EOL type of CRLF forced."),
|
|
4762 Qmnemonic, build_string ("Auto")),
|
|
4763 list4 (Qdo_coding, Qt,
|
|
4764 Qeol_type, Qcrlf)));
|
|
4765
|
|
4766 Fmake_coding_system
|
|
4767 (intern ("undecided-unix"), Qundecided,
|
|
4768 build_msg_string ("Undecided (auto-detect) (LF)"),
|
|
4769 nconc2 (list4 (Qdocumentation,
|
|
4770 build_msg_string
|
|
4771 ("Automatically detects the correct encoding; EOL type of LF forced."),
|
|
4772 Qmnemonic, build_string ("Auto")),
|
|
4773 list4 (Qdo_coding, Qt,
|
|
4774 Qeol_type, Qlf)));
|
|
4775
|
|
4776 Fmake_coding_system
|
|
4777 (intern ("undecided-mac"), Qundecided,
|
|
4778 build_msg_string ("Undecided (auto-detect) (CR)"),
|
|
4779 nconc2 (list4 (Qdocumentation,
|
|
4780 build_msg_string
|
|
4781 ("Automatically detects the correct encoding; EOL type of CR forced."),
|
|
4782 Qmnemonic, build_string ("Auto")),
|
|
4783 list4 (Qdo_coding, Qt,
|
|
4784 Qeol_type, Qcr)));
|
|
4785
|
428
|
4786 /* Need to create this here or we're really screwed. */
|
|
4787 Fmake_coding_system
|
|
4788 (Qraw_text, Qno_conversion,
|
771
|
4789 build_msg_string ("Raw Text"),
|
|
4790 list4 (Qdocumentation,
|
|
4791 build_msg_string ("Raw text converts only line-break codes, and acts otherwise like `binary'."),
|
|
4792 Qmnemonic, build_string ("Raw")));
|
428
|
4793
|
|
4794 Fmake_coding_system
|
|
4795 (Qbinary, Qno_conversion,
|
771
|
4796 build_msg_string ("Binary"),
|
|
4797 list6 (Qdocumentation,
|
|
4798 build_msg_string (
|
|
4799 "This coding system is as close as it comes to doing no conversion.\n"
|
|
4800 "On input, each byte is converted directly into the character\n"
|
|
4801 "with the corresponding code -- i.e. from the `ascii', `control-1',\n"
|
|
4802 "or `latin-1' character sets. On output, these characters are\n"
|
|
4803 "converted back to the corresponding bytes, and other characters\n"
|
|
4804 "are converted to the default character, i.e. `~'."),
|
|
4805 Qeol_type, Qlf,
|
428
|
4806 Qmnemonic, build_string ("Binary")));
|
|
4807
|
771
|
4808 /* Formerly aliased to raw-text! Completely bogus and not even the same
|
|
4809 as FSF Emacs. */
|
|
4810 Fdefine_coding_system_alias (Qno_conversion, Qbinary);
|
|
4811 Fdefine_coding_system_alias (intern ("no-conversion-unix"),
|
|
4812 intern ("raw-text-unix"));
|
|
4813 Fdefine_coding_system_alias (intern ("no-conversion-dos"),
|
|
4814 intern ("raw-text-dos"));
|
|
4815 Fdefine_coding_system_alias (intern ("no-conversion-mac"),
|
|
4816 intern ("raw-text-mac"));
|
|
4817
|
1318
|
4818 /* These three below will get their defaults set correctly
|
|
4819 in code-init.el. We init them now so we can handle stuff at dump
|
771
|
4820 time before we get to code-init.el. */
|
1318
|
4821 Fdefine_coding_system_alias (Qnative, Qbinary);
|
440
|
4822 Fdefine_coding_system_alias (Qterminal, Qbinary);
|
|
4823 Fdefine_coding_system_alias (Qkeyboard, Qbinary);
|
|
4824
|
1318
|
4825 Fdefine_coding_system_alias (Qfile_name, Qnative);
|
771
|
4826 Fdefine_coding_system_alias (Qidentity, Qconvert_eol_lf);
|
|
4827
|
428
|
4828 /* Need this for bootstrapping */
|
771
|
4829 coding_category_system[detector_category_no_conversion] =
|
428
|
4830 Fget_coding_system (Qraw_text);
|
|
4831 }
|