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