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