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