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