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