]> code.delx.au - gnu-emacs/blob - src/coding.c
(safe_bcopy): Source pointer now points to const.
[gnu-emacs] / src / coding.c
1 /* Coding system handler (conversion, detection, and etc).
2 Copyright (C) 1995, 1997, 1998, 2002 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
4 Copyright (C) 2001 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 /*** TABLE OF CONTENTS ***
24
25 0. General comments
26 1. Preamble
27 2. Emacs' internal format (emacs-mule) handlers
28 3. ISO2022 handlers
29 4. Shift-JIS and BIG5 handlers
30 5. CCL handlers
31 6. End-of-line handlers
32 7. C library functions
33 8. Emacs Lisp library functions
34 9. Post-amble
35
36 */
37
38 /*** 0. General comments ***/
39
40
41 /*** GENERAL NOTE on CODING SYSTEMS ***
42
43 A coding system is an encoding mechanism for one or more character
44 sets. Here's a list of coding systems which Emacs can handle. When
45 we say "decode", it means converting some other coding system to
46 Emacs' internal format (emacs-mule), and when we say "encode",
47 it means converting the coding system emacs-mule to some other
48 coding system.
49
50 0. Emacs' internal format (emacs-mule)
51
52 Emacs itself holds a multi-lingual character in buffers and strings
53 in a special format. Details are described in section 2.
54
55 1. ISO2022
56
57 The most famous coding system for multiple character sets. X's
58 Compound Text, various EUCs (Extended Unix Code), and coding
59 systems used in Internet communication such as ISO-2022-JP are
60 all variants of ISO2022. Details are described in section 3.
61
62 2. SJIS (or Shift-JIS or MS-Kanji-Code)
63
64 A coding system to encode character sets: ASCII, JISX0201, and
65 JISX0208. Widely used for PC's in Japan. Details are described in
66 section 4.
67
68 3. BIG5
69
70 A coding system to encode the character sets ASCII and Big5. Widely
71 used for Chinese (mainly in Taiwan and Hong Kong). Details are
72 described in section 4. In this file, when we write "BIG5"
73 (all uppercase), we mean the coding system, and when we write
74 "Big5" (capitalized), we mean the character set.
75
76 4. Raw text
77
78 A coding system for text containing random 8-bit code. Emacs does
79 no code conversion on such text except for end-of-line format.
80
81 5. Other
82
83 If a user wants to read/write text encoded in a coding system not
84 listed above, he can supply a decoder and an encoder for it as CCL
85 (Code Conversion Language) programs. Emacs executes the CCL program
86 while reading/writing.
87
88 Emacs represents a coding system by a Lisp symbol that has a property
89 `coding-system'. But, before actually using the coding system, the
90 information about it is set in a structure of type `struct
91 coding_system' for rapid processing. See section 6 for more details.
92
93 */
94
95 /*** GENERAL NOTES on END-OF-LINE FORMAT ***
96
97 How end-of-line of text is encoded depends on the operating system.
98 For instance, Unix's format is just one byte of `line-feed' code,
99 whereas DOS's format is two-byte sequence of `carriage-return' and
100 `line-feed' codes. MacOS's format is usually one byte of
101 `carriage-return'.
102
103 Since text character encoding and end-of-line encoding are
104 independent, any coding system described above can have any
105 end-of-line format. So Emacs has information about end-of-line
106 format in each coding-system. See section 6 for more details.
107
108 */
109
110 /*** GENERAL NOTES on `detect_coding_XXX ()' functions ***
111
112 These functions check if a text between SRC and SRC_END is encoded
113 in the coding system category XXX. Each returns an integer value in
114 which appropriate flag bits for the category XXX are set. The flag
115 bits are defined in macros CODING_CATEGORY_MASK_XXX. Below is the
116 template for these functions. If MULTIBYTEP is nonzero, 8-bit codes
117 of the range 0x80..0x9F are in multibyte form. */
118 #if 0
119 int
120 detect_coding_emacs_mule (src, src_end, multibytep)
121 unsigned char *src, *src_end;
122 int multibytep;
123 {
124 ...
125 }
126 #endif
127
128 /*** GENERAL NOTES on `decode_coding_XXX ()' functions ***
129
130 These functions decode SRC_BYTES length of unibyte text at SOURCE
131 encoded in CODING to Emacs' internal format. The resulting
132 multibyte text goes to a place pointed to by DESTINATION, the length
133 of which should not exceed DST_BYTES.
134
135 These functions set the information about original and decoded texts
136 in the members `produced', `produced_char', `consumed', and
137 `consumed_char' of the structure *CODING. They also set the member
138 `result' to one of CODING_FINISH_XXX indicating how the decoding
139 finished.
140
141 DST_BYTES zero means that the source area and destination area are
142 overlapped, which means that we can produce a decoded text until it
143 reaches the head of the not-yet-decoded source text.
144
145 Below is a template for these functions. */
146 #if 0
147 static void
148 decode_coding_XXX (coding, source, destination, src_bytes, dst_bytes)
149 struct coding_system *coding;
150 unsigned char *source, *destination;
151 int src_bytes, dst_bytes;
152 {
153 ...
154 }
155 #endif
156
157 /*** GENERAL NOTES on `encode_coding_XXX ()' functions ***
158
159 These functions encode SRC_BYTES length text at SOURCE from Emacs'
160 internal multibyte format to CODING. The resulting unibyte text
161 goes to a place pointed to by DESTINATION, the length of which
162 should not exceed DST_BYTES.
163
164 These functions set the information about original and encoded texts
165 in the members `produced', `produced_char', `consumed', and
166 `consumed_char' of the structure *CODING. They also set the member
167 `result' to one of CODING_FINISH_XXX indicating how the encoding
168 finished.
169
170 DST_BYTES zero means that the source area and destination area are
171 overlapped, which means that we can produce encoded text until it
172 reaches at the head of the not-yet-encoded source text.
173
174 Below is a template for these functions. */
175 #if 0
176 static void
177 encode_coding_XXX (coding, source, destination, src_bytes, dst_bytes)
178 struct coding_system *coding;
179 unsigned char *source, *destination;
180 int src_bytes, dst_bytes;
181 {
182 ...
183 }
184 #endif
185
186 /*** COMMONLY USED MACROS ***/
187
188 /* The following two macros ONE_MORE_BYTE and TWO_MORE_BYTES safely
189 get one, two, and three bytes from the source text respectively.
190 If there are not enough bytes in the source, they jump to
191 `label_end_of_loop'. The caller should set variables `coding',
192 `src' and `src_end' to appropriate pointer in advance. These
193 macros are called from decoding routines `decode_coding_XXX', thus
194 it is assumed that the source text is unibyte. */
195
196 #define ONE_MORE_BYTE(c1) \
197 do { \
198 if (src >= src_end) \
199 { \
200 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
201 goto label_end_of_loop; \
202 } \
203 c1 = *src++; \
204 } while (0)
205
206 #define TWO_MORE_BYTES(c1, c2) \
207 do { \
208 if (src + 1 >= src_end) \
209 { \
210 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
211 goto label_end_of_loop; \
212 } \
213 c1 = *src++; \
214 c2 = *src++; \
215 } while (0)
216
217
218 /* Like ONE_MORE_BYTE, but 8-bit bytes of data at SRC are in multibyte
219 form if MULTIBYTEP is nonzero. */
220
221 #define ONE_MORE_BYTE_CHECK_MULTIBYTE(c1, multibytep) \
222 do { \
223 if (src >= src_end) \
224 { \
225 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
226 goto label_end_of_loop; \
227 } \
228 c1 = *src++; \
229 if (multibytep && c1 == LEADING_CODE_8_BIT_CONTROL) \
230 c1 = *src++ - 0x20; \
231 } while (0)
232
233 /* Set C to the next character at the source text pointed by `src'.
234 If there are not enough characters in the source, jump to
235 `label_end_of_loop'. The caller should set variables `coding'
236 `src', `src_end', and `translation_table' to appropriate pointers
237 in advance. This macro is used in encoding routines
238 `encode_coding_XXX', thus it assumes that the source text is in
239 multibyte form except for 8-bit characters. 8-bit characters are
240 in multibyte form if coding->src_multibyte is nonzero, else they
241 are represented by a single byte. */
242
243 #define ONE_MORE_CHAR(c) \
244 do { \
245 int len = src_end - src; \
246 int bytes; \
247 if (len <= 0) \
248 { \
249 coding->result = CODING_FINISH_INSUFFICIENT_SRC; \
250 goto label_end_of_loop; \
251 } \
252 if (coding->src_multibyte \
253 || UNIBYTE_STR_AS_MULTIBYTE_P (src, len, bytes)) \
254 c = STRING_CHAR_AND_LENGTH (src, len, bytes); \
255 else \
256 c = *src, bytes = 1; \
257 if (!NILP (translation_table)) \
258 c = translate_char (translation_table, c, -1, 0, 0); \
259 src += bytes; \
260 } while (0)
261
262
263 /* Produce a multibyte form of character C to `dst'. Jump to
264 `label_end_of_loop' if there's not enough space at `dst'.
265
266 If we are now in the middle of a composition sequence, the decoded
267 character may be ALTCHAR (for the current composition). In that
268 case, the character goes to coding->cmp_data->data instead of
269 `dst'.
270
271 This macro is used in decoding routines. */
272
273 #define EMIT_CHAR(c) \
274 do { \
275 if (! COMPOSING_P (coding) \
276 || coding->composing == COMPOSITION_RELATIVE \
277 || coding->composing == COMPOSITION_WITH_RULE) \
278 { \
279 int bytes = CHAR_BYTES (c); \
280 if ((dst + bytes) > (dst_bytes ? dst_end : src)) \
281 { \
282 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
283 goto label_end_of_loop; \
284 } \
285 dst += CHAR_STRING (c, dst); \
286 coding->produced_char++; \
287 } \
288 \
289 if (COMPOSING_P (coding) \
290 && coding->composing != COMPOSITION_RELATIVE) \
291 { \
292 CODING_ADD_COMPOSITION_COMPONENT (coding, c); \
293 coding->composition_rule_follows \
294 = coding->composing != COMPOSITION_WITH_ALTCHARS; \
295 } \
296 } while (0)
297
298
299 #define EMIT_ONE_BYTE(c) \
300 do { \
301 if (dst >= (dst_bytes ? dst_end : src)) \
302 { \
303 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
304 goto label_end_of_loop; \
305 } \
306 *dst++ = c; \
307 } while (0)
308
309 #define EMIT_TWO_BYTES(c1, c2) \
310 do { \
311 if (dst + 2 > (dst_bytes ? dst_end : src)) \
312 { \
313 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
314 goto label_end_of_loop; \
315 } \
316 *dst++ = c1, *dst++ = c2; \
317 } while (0)
318
319 #define EMIT_BYTES(from, to) \
320 do { \
321 if (dst + (to - from) > (dst_bytes ? dst_end : src)) \
322 { \
323 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
324 goto label_end_of_loop; \
325 } \
326 while (from < to) \
327 *dst++ = *from++; \
328 } while (0)
329
330 \f
331 /*** 1. Preamble ***/
332
333 #ifdef emacs
334 #include <config.h>
335 #endif
336
337 #include <stdio.h>
338
339 #ifdef emacs
340
341 #include "lisp.h"
342 #include "buffer.h"
343 #include "charset.h"
344 #include "composite.h"
345 #include "ccl.h"
346 #include "coding.h"
347 #include "window.h"
348
349 #else /* not emacs */
350
351 #include "mulelib.h"
352
353 #endif /* not emacs */
354
355 Lisp_Object Qcoding_system, Qeol_type;
356 Lisp_Object Qbuffer_file_coding_system;
357 Lisp_Object Qpost_read_conversion, Qpre_write_conversion;
358 Lisp_Object Qno_conversion, Qundecided;
359 Lisp_Object Qcoding_system_history;
360 Lisp_Object Qsafe_chars;
361 Lisp_Object Qvalid_codes;
362
363 extern Lisp_Object Qinsert_file_contents, Qwrite_region;
364 Lisp_Object Qcall_process, Qcall_process_region, Qprocess_argument;
365 Lisp_Object Qstart_process, Qopen_network_stream;
366 Lisp_Object Qtarget_idx;
367
368 Lisp_Object Vselect_safe_coding_system_function;
369
370 /* Mnemonic string for each format of end-of-line. */
371 Lisp_Object eol_mnemonic_unix, eol_mnemonic_dos, eol_mnemonic_mac;
372 /* Mnemonic string to indicate format of end-of-line is not yet
373 decided. */
374 Lisp_Object eol_mnemonic_undecided;
375
376 /* Format of end-of-line decided by system. This is CODING_EOL_LF on
377 Unix, CODING_EOL_CRLF on DOS/Windows, and CODING_EOL_CR on Mac. */
378 int system_eol_type;
379
380 #ifdef emacs
381
382 Lisp_Object Vcoding_system_list, Vcoding_system_alist;
383
384 Lisp_Object Qcoding_system_p, Qcoding_system_error;
385
386 /* Coding system emacs-mule and raw-text are for converting only
387 end-of-line format. */
388 Lisp_Object Qemacs_mule, Qraw_text;
389
390 /* Coding-systems are handed between Emacs Lisp programs and C internal
391 routines by the following three variables. */
392 /* Coding-system for reading files and receiving data from process. */
393 Lisp_Object Vcoding_system_for_read;
394 /* Coding-system for writing files and sending data to process. */
395 Lisp_Object Vcoding_system_for_write;
396 /* Coding-system actually used in the latest I/O. */
397 Lisp_Object Vlast_coding_system_used;
398
399 /* A vector of length 256 which contains information about special
400 Latin codes (especially for dealing with Microsoft codes). */
401 Lisp_Object Vlatin_extra_code_table;
402
403 /* Flag to inhibit code conversion of end-of-line format. */
404 int inhibit_eol_conversion;
405
406 /* Flag to inhibit ISO2022 escape sequence detection. */
407 int inhibit_iso_escape_detection;
408
409 /* Flag to make buffer-file-coding-system inherit from process-coding. */
410 int inherit_process_coding_system;
411
412 /* Coding system to be used to encode text for terminal display. */
413 struct coding_system terminal_coding;
414
415 /* Coding system to be used to encode text for terminal display when
416 terminal coding system is nil. */
417 struct coding_system safe_terminal_coding;
418
419 /* Coding system of what is sent from terminal keyboard. */
420 struct coding_system keyboard_coding;
421
422 /* Default coding system to be used to write a file. */
423 struct coding_system default_buffer_file_coding;
424
425 Lisp_Object Vfile_coding_system_alist;
426 Lisp_Object Vprocess_coding_system_alist;
427 Lisp_Object Vnetwork_coding_system_alist;
428
429 Lisp_Object Vlocale_coding_system;
430
431 #endif /* emacs */
432
433 Lisp_Object Qcoding_category, Qcoding_category_index;
434
435 /* List of symbols `coding-category-xxx' ordered by priority. */
436 Lisp_Object Vcoding_category_list;
437
438 /* Table of coding categories (Lisp symbols). */
439 Lisp_Object Vcoding_category_table;
440
441 /* Table of names of symbol for each coding-category. */
442 char *coding_category_name[CODING_CATEGORY_IDX_MAX] = {
443 "coding-category-emacs-mule",
444 "coding-category-sjis",
445 "coding-category-iso-7",
446 "coding-category-iso-7-tight",
447 "coding-category-iso-8-1",
448 "coding-category-iso-8-2",
449 "coding-category-iso-7-else",
450 "coding-category-iso-8-else",
451 "coding-category-ccl",
452 "coding-category-big5",
453 "coding-category-utf-8",
454 "coding-category-utf-16-be",
455 "coding-category-utf-16-le",
456 "coding-category-raw-text",
457 "coding-category-binary"
458 };
459
460 /* Table of pointers to coding systems corresponding to each coding
461 categories. */
462 struct coding_system *coding_system_table[CODING_CATEGORY_IDX_MAX];
463
464 /* Table of coding category masks. Nth element is a mask for a coding
465 category of which priority is Nth. */
466 static
467 int coding_priorities[CODING_CATEGORY_IDX_MAX];
468
469 /* Flag to tell if we look up translation table on character code
470 conversion. */
471 Lisp_Object Venable_character_translation;
472 /* Standard translation table to look up on decoding (reading). */
473 Lisp_Object Vstandard_translation_table_for_decode;
474 /* Standard translation table to look up on encoding (writing). */
475 Lisp_Object Vstandard_translation_table_for_encode;
476
477 Lisp_Object Qtranslation_table;
478 Lisp_Object Qtranslation_table_id;
479 Lisp_Object Qtranslation_table_for_decode;
480 Lisp_Object Qtranslation_table_for_encode;
481
482 /* Alist of charsets vs revision number. */
483 Lisp_Object Vcharset_revision_alist;
484
485 /* Default coding systems used for process I/O. */
486 Lisp_Object Vdefault_process_coding_system;
487
488 /* Global flag to tell that we can't call post-read-conversion and
489 pre-write-conversion functions. Usually the value is zero, but it
490 is set to 1 temporarily while such functions are running. This is
491 to avoid infinite recursive call. */
492 static int inhibit_pre_post_conversion;
493
494 /* Char-table containing safe coding systems of each character. */
495 Lisp_Object Vchar_coding_system_table;
496 Lisp_Object Qchar_coding_system;
497
498 /* Return `safe-chars' property of coding system CODING. Don't check
499 validity of CODING. */
500
501 Lisp_Object
502 coding_safe_chars (coding)
503 struct coding_system *coding;
504 {
505 Lisp_Object coding_spec, plist, safe_chars;
506
507 coding_spec = Fget (coding->symbol, Qcoding_system);
508 plist = XVECTOR (coding_spec)->contents[3];
509 safe_chars = Fplist_get (XVECTOR (coding_spec)->contents[3], Qsafe_chars);
510 return (CHAR_TABLE_P (safe_chars) ? safe_chars : Qt);
511 }
512
513 #define CODING_SAFE_CHAR_P(safe_chars, c) \
514 (EQ (safe_chars, Qt) || !NILP (CHAR_TABLE_REF (safe_chars, c)))
515
516 \f
517 /*** 2. Emacs internal format (emacs-mule) handlers ***/
518
519 /* Emacs' internal format for representation of multiple character
520 sets is a kind of multi-byte encoding, i.e. characters are
521 represented by variable-length sequences of one-byte codes.
522
523 ASCII characters and control characters (e.g. `tab', `newline') are
524 represented by one-byte sequences which are their ASCII codes, in
525 the range 0x00 through 0x7F.
526
527 8-bit characters of the range 0x80..0x9F are represented by
528 two-byte sequences of LEADING_CODE_8_BIT_CONTROL and (their 8-bit
529 code + 0x20).
530
531 8-bit characters of the range 0xA0..0xFF are represented by
532 one-byte sequences which are their 8-bit code.
533
534 The other characters are represented by a sequence of `base
535 leading-code', optional `extended leading-code', and one or two
536 `position-code's. The length of the sequence is determined by the
537 base leading-code. Leading-code takes the range 0x81 through 0x9D,
538 whereas extended leading-code and position-code take the range 0xA0
539 through 0xFF. See `charset.h' for more details about leading-code
540 and position-code.
541
542 --- CODE RANGE of Emacs' internal format ---
543 character set range
544 ------------- -----
545 ascii 0x00..0x7F
546 eight-bit-control LEADING_CODE_8_BIT_CONTROL + 0xA0..0xBF
547 eight-bit-graphic 0xA0..0xBF
548 ELSE 0x81..0x9D + [0xA0..0xFF]+
549 ---------------------------------------------
550
551 As this is the internal character representation, the format is
552 usually not used externally (i.e. in a file or in a data sent to a
553 process). But, it is possible to have a text externally in this
554 format (i.e. by encoding by the coding system `emacs-mule').
555
556 In that case, a sequence of one-byte codes has a slightly different
557 form.
558
559 Firstly, all characters in eight-bit-control are represented by
560 one-byte sequences which are their 8-bit code.
561
562 Next, character composition data are represented by the byte
563 sequence of the form: 0x80 METHOD BYTES CHARS COMPONENT ...,
564 where,
565 METHOD is 0xF0 plus one of composition method (enum
566 composition_method),
567
568 BYTES is 0xA0 plus the byte length of these composition data,
569
570 CHARS is 0xA0 plus the number of characters composed by these
571 data,
572
573 COMPONENTs are characters of multibyte form or composition
574 rules encoded by two-byte of ASCII codes.
575
576 In addition, for backward compatibility, the following formats are
577 also recognized as composition data on decoding.
578
579 0x80 MSEQ ...
580 0x80 0xFF MSEQ RULE MSEQ RULE ... MSEQ
581
582 Here,
583 MSEQ is a multibyte form but in these special format:
584 ASCII: 0xA0 ASCII_CODE+0x80,
585 other: LEADING_CODE+0x20 FOLLOWING-BYTE ...,
586 RULE is a one byte code of the range 0xA0..0xF0 that
587 represents a composition rule.
588 */
589
590 enum emacs_code_class_type emacs_code_class[256];
591
592 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
593 Check if a text is encoded in Emacs' internal format. If it is,
594 return CODING_CATEGORY_MASK_EMACS_MULE, else return 0. */
595
596 static int
597 detect_coding_emacs_mule (src, src_end, multibytep)
598 unsigned char *src, *src_end;
599 int multibytep;
600 {
601 unsigned char c;
602 int composing = 0;
603 /* Dummy for ONE_MORE_BYTE. */
604 struct coding_system dummy_coding;
605 struct coding_system *coding = &dummy_coding;
606
607 while (1)
608 {
609 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
610
611 if (composing)
612 {
613 if (c < 0xA0)
614 composing = 0;
615 else if (c == 0xA0)
616 {
617 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
618 c &= 0x7F;
619 }
620 else
621 c -= 0x20;
622 }
623
624 if (c < 0x20)
625 {
626 if (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO)
627 return 0;
628 }
629 else if (c >= 0x80 && c < 0xA0)
630 {
631 if (c == 0x80)
632 /* Old leading code for a composite character. */
633 composing = 1;
634 else
635 {
636 unsigned char *src_base = src - 1;
637 int bytes;
638
639 if (!UNIBYTE_STR_AS_MULTIBYTE_P (src_base, src_end - src_base,
640 bytes))
641 return 0;
642 src = src_base + bytes;
643 }
644 }
645 }
646 label_end_of_loop:
647 return CODING_CATEGORY_MASK_EMACS_MULE;
648 }
649
650
651 /* Record the starting position START and METHOD of one composition. */
652
653 #define CODING_ADD_COMPOSITION_START(coding, start, method) \
654 do { \
655 struct composition_data *cmp_data = coding->cmp_data; \
656 int *data = cmp_data->data + cmp_data->used; \
657 coding->cmp_data_start = cmp_data->used; \
658 data[0] = -1; \
659 data[1] = cmp_data->char_offset + start; \
660 data[3] = (int) method; \
661 cmp_data->used += 4; \
662 } while (0)
663
664 /* Record the ending position END of the current composition. */
665
666 #define CODING_ADD_COMPOSITION_END(coding, end) \
667 do { \
668 struct composition_data *cmp_data = coding->cmp_data; \
669 int *data = cmp_data->data + coding->cmp_data_start; \
670 data[0] = cmp_data->used - coding->cmp_data_start; \
671 data[2] = cmp_data->char_offset + end; \
672 } while (0)
673
674 /* Record one COMPONENT (alternate character or composition rule). */
675
676 #define CODING_ADD_COMPOSITION_COMPONENT(coding, component) \
677 (coding->cmp_data->data[coding->cmp_data->used++] = component)
678
679
680 /* Get one byte from a data pointed by SRC and increment SRC. If SRC
681 is not less than SRC_END, return -1 without incrementing Src. */
682
683 #define SAFE_ONE_MORE_BYTE() (src >= src_end ? -1 : *src++)
684
685
686 /* Decode a character represented as a component of composition
687 sequence of Emacs 20 style at SRC. Set C to that character, store
688 its multibyte form sequence at P, and set P to the end of that
689 sequence. If no valid character is found, set C to -1. */
690
691 #define DECODE_EMACS_MULE_COMPOSITION_CHAR(c, p) \
692 do { \
693 int bytes; \
694 \
695 c = SAFE_ONE_MORE_BYTE (); \
696 if (c < 0) \
697 break; \
698 if (CHAR_HEAD_P (c)) \
699 c = -1; \
700 else if (c == 0xA0) \
701 { \
702 c = SAFE_ONE_MORE_BYTE (); \
703 if (c < 0xA0) \
704 c = -1; \
705 else \
706 { \
707 c -= 0xA0; \
708 *p++ = c; \
709 } \
710 } \
711 else if (BASE_LEADING_CODE_P (c - 0x20)) \
712 { \
713 unsigned char *p0 = p; \
714 \
715 c -= 0x20; \
716 *p++ = c; \
717 bytes = BYTES_BY_CHAR_HEAD (c); \
718 while (--bytes) \
719 { \
720 c = SAFE_ONE_MORE_BYTE (); \
721 if (c < 0) \
722 break; \
723 *p++ = c; \
724 } \
725 if (UNIBYTE_STR_AS_MULTIBYTE_P (p0, p - p0, bytes)) \
726 c = STRING_CHAR (p0, bytes); \
727 else \
728 c = -1; \
729 } \
730 else \
731 c = -1; \
732 } while (0)
733
734
735 /* Decode a composition rule represented as a component of composition
736 sequence of Emacs 20 style at SRC. Set C to the rule. If not
737 valid rule is found, set C to -1. */
738
739 #define DECODE_EMACS_MULE_COMPOSITION_RULE(c) \
740 do { \
741 c = SAFE_ONE_MORE_BYTE (); \
742 c -= 0xA0; \
743 if (c < 0 || c >= 81) \
744 c = -1; \
745 else \
746 { \
747 gref = c / 9, nref = c % 9; \
748 c = COMPOSITION_ENCODE_RULE (gref, nref); \
749 } \
750 } while (0)
751
752
753 /* Decode composition sequence encoded by `emacs-mule' at the source
754 pointed by SRC. SRC_END is the end of source. Store information
755 of the composition in CODING->cmp_data.
756
757 For backward compatibility, decode also a composition sequence of
758 Emacs 20 style. In that case, the composition sequence contains
759 characters that should be extracted into a buffer or string. Store
760 those characters at *DESTINATION in multibyte form.
761
762 If we encounter an invalid byte sequence, return 0.
763 If we encounter an insufficient source or destination, or
764 insufficient space in CODING->cmp_data, return 1.
765 Otherwise, return consumed bytes in the source.
766
767 */
768 static INLINE int
769 decode_composition_emacs_mule (coding, src, src_end,
770 destination, dst_end, dst_bytes)
771 struct coding_system *coding;
772 unsigned char *src, *src_end, **destination, *dst_end;
773 int dst_bytes;
774 {
775 unsigned char *dst = *destination;
776 int method, data_len, nchars;
777 unsigned char *src_base = src++;
778 /* Store components of composition. */
779 int component[COMPOSITION_DATA_MAX_BUNCH_LENGTH];
780 int ncomponent;
781 /* Store multibyte form of characters to be composed. This is for
782 Emacs 20 style composition sequence. */
783 unsigned char buf[MAX_COMPOSITION_COMPONENTS * MAX_MULTIBYTE_LENGTH];
784 unsigned char *bufp = buf;
785 int c, i, gref, nref;
786
787 if (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH
788 >= COMPOSITION_DATA_SIZE)
789 {
790 coding->result = CODING_FINISH_INSUFFICIENT_CMP;
791 return -1;
792 }
793
794 ONE_MORE_BYTE (c);
795 if (c - 0xF0 >= COMPOSITION_RELATIVE
796 && c - 0xF0 <= COMPOSITION_WITH_RULE_ALTCHARS)
797 {
798 int with_rule;
799
800 method = c - 0xF0;
801 with_rule = (method == COMPOSITION_WITH_RULE
802 || method == COMPOSITION_WITH_RULE_ALTCHARS);
803 ONE_MORE_BYTE (c);
804 data_len = c - 0xA0;
805 if (data_len < 4
806 || src_base + data_len > src_end)
807 return 0;
808 ONE_MORE_BYTE (c);
809 nchars = c - 0xA0;
810 if (c < 1)
811 return 0;
812 for (ncomponent = 0; src < src_base + data_len; ncomponent++)
813 {
814 /* If it is longer than this, it can't be valid. */
815 if (ncomponent >= COMPOSITION_DATA_MAX_BUNCH_LENGTH)
816 return 0;
817
818 if (ncomponent % 2 && with_rule)
819 {
820 ONE_MORE_BYTE (gref);
821 gref -= 32;
822 ONE_MORE_BYTE (nref);
823 nref -= 32;
824 c = COMPOSITION_ENCODE_RULE (gref, nref);
825 }
826 else
827 {
828 int bytes;
829 if (UNIBYTE_STR_AS_MULTIBYTE_P (src, src_end - src, bytes))
830 c = STRING_CHAR (src, bytes);
831 else
832 c = *src, bytes = 1;
833 src += bytes;
834 }
835 component[ncomponent] = c;
836 }
837 }
838 else
839 {
840 /* This may be an old Emacs 20 style format. See the comment at
841 the section 2 of this file. */
842 while (src < src_end && !CHAR_HEAD_P (*src)) src++;
843 if (src == src_end
844 && !(coding->mode & CODING_MODE_LAST_BLOCK))
845 goto label_end_of_loop;
846
847 src_end = src;
848 src = src_base + 1;
849 if (c < 0xC0)
850 {
851 method = COMPOSITION_RELATIVE;
852 for (ncomponent = 0; ncomponent < MAX_COMPOSITION_COMPONENTS;)
853 {
854 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
855 if (c < 0)
856 break;
857 component[ncomponent++] = c;
858 }
859 if (ncomponent < 2)
860 return 0;
861 nchars = ncomponent;
862 }
863 else if (c == 0xFF)
864 {
865 method = COMPOSITION_WITH_RULE;
866 src++;
867 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
868 if (c < 0)
869 return 0;
870 component[0] = c;
871 for (ncomponent = 1;
872 ncomponent < MAX_COMPOSITION_COMPONENTS * 2 - 1;)
873 {
874 DECODE_EMACS_MULE_COMPOSITION_RULE (c);
875 if (c < 0)
876 break;
877 component[ncomponent++] = c;
878 DECODE_EMACS_MULE_COMPOSITION_CHAR (c, bufp);
879 if (c < 0)
880 break;
881 component[ncomponent++] = c;
882 }
883 if (ncomponent < 3)
884 return 0;
885 nchars = (ncomponent + 1) / 2;
886 }
887 else
888 return 0;
889 }
890
891 if (buf == bufp || dst + (bufp - buf) <= (dst_bytes ? dst_end : src))
892 {
893 CODING_ADD_COMPOSITION_START (coding, coding->produced_char, method);
894 for (i = 0; i < ncomponent; i++)
895 CODING_ADD_COMPOSITION_COMPONENT (coding, component[i]);
896 CODING_ADD_COMPOSITION_END (coding, coding->produced_char + nchars);
897 if (buf < bufp)
898 {
899 unsigned char *p = buf;
900 EMIT_BYTES (p, bufp);
901 *destination += bufp - buf;
902 coding->produced_char += nchars;
903 }
904 return (src - src_base);
905 }
906 label_end_of_loop:
907 return -1;
908 }
909
910 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
911
912 static void
913 decode_coding_emacs_mule (coding, source, destination, src_bytes, dst_bytes)
914 struct coding_system *coding;
915 unsigned char *source, *destination;
916 int src_bytes, dst_bytes;
917 {
918 unsigned char *src = source;
919 unsigned char *src_end = source + src_bytes;
920 unsigned char *dst = destination;
921 unsigned char *dst_end = destination + dst_bytes;
922 /* SRC_BASE remembers the start position in source in each loop.
923 The loop will be exited when there's not enough source code, or
924 when there's not enough destination area to produce a
925 character. */
926 unsigned char *src_base;
927
928 coding->produced_char = 0;
929 while ((src_base = src) < src_end)
930 {
931 unsigned char tmp[MAX_MULTIBYTE_LENGTH], *p;
932 int bytes;
933
934 if (*src == '\r')
935 {
936 int c = *src++;
937
938 if (coding->eol_type == CODING_EOL_CR)
939 c = '\n';
940 else if (coding->eol_type == CODING_EOL_CRLF)
941 {
942 ONE_MORE_BYTE (c);
943 if (c != '\n')
944 {
945 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
946 {
947 coding->result = CODING_FINISH_INCONSISTENT_EOL;
948 goto label_end_of_loop;
949 }
950 src--;
951 c = '\r';
952 }
953 }
954 *dst++ = c;
955 coding->produced_char++;
956 continue;
957 }
958 else if (*src == '\n')
959 {
960 if ((coding->eol_type == CODING_EOL_CR
961 || coding->eol_type == CODING_EOL_CRLF)
962 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
963 {
964 coding->result = CODING_FINISH_INCONSISTENT_EOL;
965 goto label_end_of_loop;
966 }
967 *dst++ = *src++;
968 coding->produced_char++;
969 continue;
970 }
971 else if (*src == 0x80)
972 {
973 /* Start of composition data. */
974 int consumed = decode_composition_emacs_mule (coding, src, src_end,
975 &dst, dst_end,
976 dst_bytes);
977 if (consumed < 0)
978 goto label_end_of_loop;
979 else if (consumed > 0)
980 {
981 src += consumed;
982 continue;
983 }
984 bytes = CHAR_STRING (*src, tmp);
985 p = tmp;
986 src++;
987 }
988 else if (UNIBYTE_STR_AS_MULTIBYTE_P (src, src_end - src, bytes))
989 {
990 p = src;
991 src += bytes;
992 }
993 else
994 {
995 bytes = CHAR_STRING (*src, tmp);
996 p = tmp;
997 src++;
998 }
999 if (dst + bytes >= (dst_bytes ? dst_end : src))
1000 {
1001 coding->result = CODING_FINISH_INSUFFICIENT_DST;
1002 break;
1003 }
1004 while (bytes--) *dst++ = *p++;
1005 coding->produced_char++;
1006 }
1007 label_end_of_loop:
1008 coding->consumed = coding->consumed_char = src_base - source;
1009 coding->produced = dst - destination;
1010 }
1011
1012
1013 /* Encode composition data stored at DATA into a special byte sequence
1014 starting by 0x80. Update CODING->cmp_data_start and maybe
1015 CODING->cmp_data for the next call. */
1016
1017 #define ENCODE_COMPOSITION_EMACS_MULE(coding, data) \
1018 do { \
1019 unsigned char buf[1024], *p0 = buf, *p; \
1020 int len = data[0]; \
1021 int i; \
1022 \
1023 buf[0] = 0x80; \
1024 buf[1] = 0xF0 + data[3]; /* METHOD */ \
1025 buf[3] = 0xA0 + (data[2] - data[1]); /* COMPOSED-CHARS */ \
1026 p = buf + 4; \
1027 if (data[3] == COMPOSITION_WITH_RULE \
1028 || data[3] == COMPOSITION_WITH_RULE_ALTCHARS) \
1029 { \
1030 p += CHAR_STRING (data[4], p); \
1031 for (i = 5; i < len; i += 2) \
1032 { \
1033 int gref, nref; \
1034 COMPOSITION_DECODE_RULE (data[i], gref, nref); \
1035 *p++ = 0x20 + gref; \
1036 *p++ = 0x20 + nref; \
1037 p += CHAR_STRING (data[i + 1], p); \
1038 } \
1039 } \
1040 else \
1041 { \
1042 for (i = 4; i < len; i++) \
1043 p += CHAR_STRING (data[i], p); \
1044 } \
1045 buf[2] = 0xA0 + (p - buf); /* COMPONENTS-BYTES */ \
1046 \
1047 if (dst + (p - buf) + 4 > (dst_bytes ? dst_end : src)) \
1048 { \
1049 coding->result = CODING_FINISH_INSUFFICIENT_DST; \
1050 goto label_end_of_loop; \
1051 } \
1052 while (p0 < p) \
1053 *dst++ = *p0++; \
1054 coding->cmp_data_start += data[0]; \
1055 if (coding->cmp_data_start == coding->cmp_data->used \
1056 && coding->cmp_data->next) \
1057 { \
1058 coding->cmp_data = coding->cmp_data->next; \
1059 coding->cmp_data_start = 0; \
1060 } \
1061 } while (0)
1062
1063
1064 static void encode_eol P_ ((struct coding_system *, const unsigned char *,
1065 unsigned char *, int, int));
1066
1067 static void
1068 encode_coding_emacs_mule (coding, source, destination, src_bytes, dst_bytes)
1069 struct coding_system *coding;
1070 unsigned char *source, *destination;
1071 int src_bytes, dst_bytes;
1072 {
1073 unsigned char *src = source;
1074 unsigned char *src_end = source + src_bytes;
1075 unsigned char *dst = destination;
1076 unsigned char *dst_end = destination + dst_bytes;
1077 unsigned char *src_base;
1078 int c;
1079 int char_offset;
1080 int *data;
1081
1082 Lisp_Object translation_table;
1083
1084 translation_table = Qnil;
1085
1086 /* Optimization for the case that there's no composition. */
1087 if (!coding->cmp_data || coding->cmp_data->used == 0)
1088 {
1089 encode_eol (coding, source, destination, src_bytes, dst_bytes);
1090 return;
1091 }
1092
1093 char_offset = coding->cmp_data->char_offset;
1094 data = coding->cmp_data->data + coding->cmp_data_start;
1095 while (1)
1096 {
1097 src_base = src;
1098
1099 /* If SRC starts a composition, encode the information about the
1100 composition in advance. */
1101 if (coding->cmp_data_start < coding->cmp_data->used
1102 && char_offset + coding->consumed_char == data[1])
1103 {
1104 ENCODE_COMPOSITION_EMACS_MULE (coding, data);
1105 char_offset = coding->cmp_data->char_offset;
1106 data = coding->cmp_data->data + coding->cmp_data_start;
1107 }
1108
1109 ONE_MORE_CHAR (c);
1110 if (c == '\n' && (coding->eol_type == CODING_EOL_CRLF
1111 || coding->eol_type == CODING_EOL_CR))
1112 {
1113 if (coding->eol_type == CODING_EOL_CRLF)
1114 EMIT_TWO_BYTES ('\r', c);
1115 else
1116 EMIT_ONE_BYTE ('\r');
1117 }
1118 else if (SINGLE_BYTE_CHAR_P (c))
1119 EMIT_ONE_BYTE (c);
1120 else
1121 EMIT_BYTES (src_base, src);
1122 coding->consumed_char++;
1123 }
1124 label_end_of_loop:
1125 coding->consumed = src_base - source;
1126 coding->produced = coding->produced_char = dst - destination;
1127 return;
1128 }
1129
1130 \f
1131 /*** 3. ISO2022 handlers ***/
1132
1133 /* The following note describes the coding system ISO2022 briefly.
1134 Since the intention of this note is to help understand the
1135 functions in this file, some parts are NOT ACCURATE or are OVERLY
1136 SIMPLIFIED. For thorough understanding, please refer to the
1137 original document of ISO2022. This is equivalent to the standard
1138 ECMA-35, obtainable from <URL:http://www.ecma.ch/> (*).
1139
1140 ISO2022 provides many mechanisms to encode several character sets
1141 in 7-bit and 8-bit environments. For 7-bit environments, all text
1142 is encoded using bytes less than 128. This may make the encoded
1143 text a little bit longer, but the text passes more easily through
1144 several types of gateway, some of which strip off the MSB (Most
1145 Significant Bit).
1146
1147 There are two kinds of character sets: control character sets and
1148 graphic character sets. The former contain control characters such
1149 as `newline' and `escape' to provide control functions (control
1150 functions are also provided by escape sequences). The latter
1151 contain graphic characters such as 'A' and '-'. Emacs recognizes
1152 two control character sets and many graphic character sets.
1153
1154 Graphic character sets are classified into one of the following
1155 four classes, according to the number of bytes (DIMENSION) and
1156 number of characters in one dimension (CHARS) of the set:
1157 - DIMENSION1_CHARS94
1158 - DIMENSION1_CHARS96
1159 - DIMENSION2_CHARS94
1160 - DIMENSION2_CHARS96
1161
1162 In addition, each character set is assigned an identification tag,
1163 unique for each set, called the "final character" (denoted as <F>
1164 hereafter). The <F> of each character set is decided by ECMA(*)
1165 when it is registered in ISO. The code range of <F> is 0x30..0x7F
1166 (0x30..0x3F are for private use only).
1167
1168 Note (*): ECMA = European Computer Manufacturers Association
1169
1170 Here are examples of graphic character sets [NAME(<F>)]:
1171 o DIMENSION1_CHARS94 -- ASCII('B'), right-half-of-JISX0201('I'), ...
1172 o DIMENSION1_CHARS96 -- right-half-of-ISO8859-1('A'), ...
1173 o DIMENSION2_CHARS94 -- GB2312('A'), JISX0208('B'), ...
1174 o DIMENSION2_CHARS96 -- none for the moment
1175
1176 A code area (1 byte=8 bits) is divided into 4 areas, C0, GL, C1, and GR.
1177 C0 [0x00..0x1F] -- control character plane 0
1178 GL [0x20..0x7F] -- graphic character plane 0
1179 C1 [0x80..0x9F] -- control character plane 1
1180 GR [0xA0..0xFF] -- graphic character plane 1
1181
1182 A control character set is directly designated and invoked to C0 or
1183 C1 by an escape sequence. The most common case is that:
1184 - ISO646's control character set is designated/invoked to C0, and
1185 - ISO6429's control character set is designated/invoked to C1,
1186 and usually these designations/invocations are omitted in encoded
1187 text. In a 7-bit environment, only C0 can be used, and a control
1188 character for C1 is encoded by an appropriate escape sequence to
1189 fit into the environment. All control characters for C1 are
1190 defined to have corresponding escape sequences.
1191
1192 A graphic character set is at first designated to one of four
1193 graphic registers (G0 through G3), then these graphic registers are
1194 invoked to GL or GR. These designations and invocations can be
1195 done independently. The most common case is that G0 is invoked to
1196 GL, G1 is invoked to GR, and ASCII is designated to G0. Usually
1197 these invocations and designations are omitted in encoded text.
1198 In a 7-bit environment, only GL can be used.
1199
1200 When a graphic character set of CHARS94 is invoked to GL, codes
1201 0x20 and 0x7F of the GL area work as control characters SPACE and
1202 DEL respectively, and codes 0xA0 and 0xFF of the GR area should not
1203 be used.
1204
1205 There are two ways of invocation: locking-shift and single-shift.
1206 With locking-shift, the invocation lasts until the next different
1207 invocation, whereas with single-shift, the invocation affects the
1208 following character only and doesn't affect the locking-shift
1209 state. Invocations are done by the following control characters or
1210 escape sequences:
1211
1212 ----------------------------------------------------------------------
1213 abbrev function cntrl escape seq description
1214 ----------------------------------------------------------------------
1215 SI/LS0 (shift-in) 0x0F none invoke G0 into GL
1216 SO/LS1 (shift-out) 0x0E none invoke G1 into GL
1217 LS2 (locking-shift-2) none ESC 'n' invoke G2 into GL
1218 LS3 (locking-shift-3) none ESC 'o' invoke G3 into GL
1219 LS1R (locking-shift-1 right) none ESC '~' invoke G1 into GR (*)
1220 LS2R (locking-shift-2 right) none ESC '}' invoke G2 into GR (*)
1221 LS3R (locking-shift 3 right) none ESC '|' invoke G3 into GR (*)
1222 SS2 (single-shift-2) 0x8E ESC 'N' invoke G2 for one char
1223 SS3 (single-shift-3) 0x8F ESC 'O' invoke G3 for one char
1224 ----------------------------------------------------------------------
1225 (*) These are not used by any known coding system.
1226
1227 Control characters for these functions are defined by macros
1228 ISO_CODE_XXX in `coding.h'.
1229
1230 Designations are done by the following escape sequences:
1231 ----------------------------------------------------------------------
1232 escape sequence description
1233 ----------------------------------------------------------------------
1234 ESC '(' <F> designate DIMENSION1_CHARS94<F> to G0
1235 ESC ')' <F> designate DIMENSION1_CHARS94<F> to G1
1236 ESC '*' <F> designate DIMENSION1_CHARS94<F> to G2
1237 ESC '+' <F> designate DIMENSION1_CHARS94<F> to G3
1238 ESC ',' <F> designate DIMENSION1_CHARS96<F> to G0 (*)
1239 ESC '-' <F> designate DIMENSION1_CHARS96<F> to G1
1240 ESC '.' <F> designate DIMENSION1_CHARS96<F> to G2
1241 ESC '/' <F> designate DIMENSION1_CHARS96<F> to G3
1242 ESC '$' '(' <F> designate DIMENSION2_CHARS94<F> to G0 (**)
1243 ESC '$' ')' <F> designate DIMENSION2_CHARS94<F> to G1
1244 ESC '$' '*' <F> designate DIMENSION2_CHARS94<F> to G2
1245 ESC '$' '+' <F> designate DIMENSION2_CHARS94<F> to G3
1246 ESC '$' ',' <F> designate DIMENSION2_CHARS96<F> to G0 (*)
1247 ESC '$' '-' <F> designate DIMENSION2_CHARS96<F> to G1
1248 ESC '$' '.' <F> designate DIMENSION2_CHARS96<F> to G2
1249 ESC '$' '/' <F> designate DIMENSION2_CHARS96<F> to G3
1250 ----------------------------------------------------------------------
1251
1252 In this list, "DIMENSION1_CHARS94<F>" means a graphic character set
1253 of dimension 1, chars 94, and final character <F>, etc...
1254
1255 Note (*): Although these designations are not allowed in ISO2022,
1256 Emacs accepts them on decoding, and produces them on encoding
1257 CHARS96 character sets in a coding system which is characterized as
1258 7-bit environment, non-locking-shift, and non-single-shift.
1259
1260 Note (**): If <F> is '@', 'A', or 'B', the intermediate character
1261 '(' can be omitted. We refer to this as "short-form" hereafter.
1262
1263 Now you may notice that there are a lot of ways of encoding the
1264 same multilingual text in ISO2022. Actually, there exist many
1265 coding systems such as Compound Text (used in X11's inter client
1266 communication, ISO-2022-JP (used in Japanese Internet), ISO-2022-KR
1267 (used in Korean Internet), EUC (Extended UNIX Code, used in Asian
1268 localized platforms), and all of these are variants of ISO2022.
1269
1270 In addition to the above, Emacs handles two more kinds of escape
1271 sequences: ISO6429's direction specification and Emacs' private
1272 sequence for specifying character composition.
1273
1274 ISO6429's direction specification takes the following form:
1275 o CSI ']' -- end of the current direction
1276 o CSI '0' ']' -- end of the current direction
1277 o CSI '1' ']' -- start of left-to-right text
1278 o CSI '2' ']' -- start of right-to-left text
1279 The control character CSI (0x9B: control sequence introducer) is
1280 abbreviated to the escape sequence ESC '[' in a 7-bit environment.
1281
1282 Character composition specification takes the following form:
1283 o ESC '0' -- start relative composition
1284 o ESC '1' -- end composition
1285 o ESC '2' -- start rule-base composition (*)
1286 o ESC '3' -- start relative composition with alternate chars (**)
1287 o ESC '4' -- start rule-base composition with alternate chars (**)
1288 Since these are not standard escape sequences of any ISO standard,
1289 the use of them with these meanings is restricted to Emacs only.
1290
1291 (*) This form is used only in Emacs 20.5 and older versions,
1292 but the newer versions can safely decode it.
1293 (**) This form is used only in Emacs 21.1 and newer versions,
1294 and the older versions can't decode it.
1295
1296 Here's a list of example usages of these composition escape
1297 sequences (categorized by `enum composition_method').
1298
1299 COMPOSITION_RELATIVE:
1300 ESC 0 CHAR [ CHAR ] ESC 1
1301 COMPOSITION_WITH_RULE:
1302 ESC 2 CHAR [ RULE CHAR ] ESC 1
1303 COMPOSITION_WITH_ALTCHARS:
1304 ESC 3 ALTCHAR [ ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1
1305 COMPOSITION_WITH_RULE_ALTCHARS:
1306 ESC 4 ALTCHAR [ RULE ALTCHAR ] ESC 0 CHAR [ CHAR ] ESC 1 */
1307
1308 enum iso_code_class_type iso_code_class[256];
1309
1310 #define CHARSET_OK(idx, charset, c) \
1311 (coding_system_table[idx] \
1312 && (charset == CHARSET_ASCII \
1313 || (safe_chars = coding_safe_chars (coding_system_table[idx]), \
1314 CODING_SAFE_CHAR_P (safe_chars, c))) \
1315 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding_system_table[idx], \
1316 charset) \
1317 != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION))
1318
1319 #define SHIFT_OUT_OK(idx) \
1320 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding_system_table[idx], 1) >= 0)
1321
1322 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
1323 Check if a text is encoded in ISO2022. If it is, return an
1324 integer in which appropriate flag bits any of:
1325 CODING_CATEGORY_MASK_ISO_7
1326 CODING_CATEGORY_MASK_ISO_7_TIGHT
1327 CODING_CATEGORY_MASK_ISO_8_1
1328 CODING_CATEGORY_MASK_ISO_8_2
1329 CODING_CATEGORY_MASK_ISO_7_ELSE
1330 CODING_CATEGORY_MASK_ISO_8_ELSE
1331 are set. If a code which should never appear in ISO2022 is found,
1332 returns 0. */
1333
1334 static int
1335 detect_coding_iso2022 (src, src_end, multibytep)
1336 unsigned char *src, *src_end;
1337 int multibytep;
1338 {
1339 int mask = CODING_CATEGORY_MASK_ISO;
1340 int mask_found = 0;
1341 int reg[4], shift_out = 0, single_shifting = 0;
1342 int c, c1, charset;
1343 /* Dummy for ONE_MORE_BYTE. */
1344 struct coding_system dummy_coding;
1345 struct coding_system *coding = &dummy_coding;
1346 Lisp_Object safe_chars;
1347
1348 reg[0] = CHARSET_ASCII, reg[1] = reg[2] = reg[3] = -1;
1349 while (mask && src < src_end)
1350 {
1351 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
1352 switch (c)
1353 {
1354 case ISO_CODE_ESC:
1355 if (inhibit_iso_escape_detection)
1356 break;
1357 single_shifting = 0;
1358 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
1359 if (c >= '(' && c <= '/')
1360 {
1361 /* Designation sequence for a charset of dimension 1. */
1362 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep);
1363 if (c1 < ' ' || c1 >= 0x80
1364 || (charset = iso_charset_table[0][c >= ','][c1]) < 0)
1365 /* Invalid designation sequence. Just ignore. */
1366 break;
1367 reg[(c - '(') % 4] = charset;
1368 }
1369 else if (c == '$')
1370 {
1371 /* Designation sequence for a charset of dimension 2. */
1372 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
1373 if (c >= '@' && c <= 'B')
1374 /* Designation for JISX0208.1978, GB2312, or JISX0208. */
1375 reg[0] = charset = iso_charset_table[1][0][c];
1376 else if (c >= '(' && c <= '/')
1377 {
1378 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep);
1379 if (c1 < ' ' || c1 >= 0x80
1380 || (charset = iso_charset_table[1][c >= ','][c1]) < 0)
1381 /* Invalid designation sequence. Just ignore. */
1382 break;
1383 reg[(c - '(') % 4] = charset;
1384 }
1385 else
1386 /* Invalid designation sequence. Just ignore. */
1387 break;
1388 }
1389 else if (c == 'N' || c == 'O')
1390 {
1391 /* ESC <Fe> for SS2 or SS3. */
1392 mask &= CODING_CATEGORY_MASK_ISO_7_ELSE;
1393 break;
1394 }
1395 else if (c >= '0' && c <= '4')
1396 {
1397 /* ESC <Fp> for start/end composition. */
1398 mask_found |= CODING_CATEGORY_MASK_ISO;
1399 break;
1400 }
1401 else
1402 /* Invalid escape sequence. Just ignore. */
1403 break;
1404
1405 /* We found a valid designation sequence for CHARSET. */
1406 mask &= ~CODING_CATEGORY_MASK_ISO_8BIT;
1407 c = MAKE_CHAR (charset, 0, 0);
1408 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7, charset, c))
1409 mask_found |= CODING_CATEGORY_MASK_ISO_7;
1410 else
1411 mask &= ~CODING_CATEGORY_MASK_ISO_7;
1412 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_TIGHT, charset, c))
1413 mask_found |= CODING_CATEGORY_MASK_ISO_7_TIGHT;
1414 else
1415 mask &= ~CODING_CATEGORY_MASK_ISO_7_TIGHT;
1416 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_7_ELSE, charset, c))
1417 mask_found |= CODING_CATEGORY_MASK_ISO_7_ELSE;
1418 else
1419 mask &= ~CODING_CATEGORY_MASK_ISO_7_ELSE;
1420 if (CHARSET_OK (CODING_CATEGORY_IDX_ISO_8_ELSE, charset, c))
1421 mask_found |= CODING_CATEGORY_MASK_ISO_8_ELSE;
1422 else
1423 mask &= ~CODING_CATEGORY_MASK_ISO_8_ELSE;
1424 break;
1425
1426 case ISO_CODE_SO:
1427 if (inhibit_iso_escape_detection)
1428 break;
1429 single_shifting = 0;
1430 if (shift_out == 0
1431 && (reg[1] >= 0
1432 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_7_ELSE)
1433 || SHIFT_OUT_OK (CODING_CATEGORY_IDX_ISO_8_ELSE)))
1434 {
1435 /* Locking shift out. */
1436 mask &= ~CODING_CATEGORY_MASK_ISO_7BIT;
1437 mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT;
1438 }
1439 break;
1440
1441 case ISO_CODE_SI:
1442 if (inhibit_iso_escape_detection)
1443 break;
1444 single_shifting = 0;
1445 if (shift_out == 1)
1446 {
1447 /* Locking shift in. */
1448 mask &= ~CODING_CATEGORY_MASK_ISO_7BIT;
1449 mask_found |= CODING_CATEGORY_MASK_ISO_SHIFT;
1450 }
1451 break;
1452
1453 case ISO_CODE_CSI:
1454 single_shifting = 0;
1455 case ISO_CODE_SS2:
1456 case ISO_CODE_SS3:
1457 {
1458 int newmask = CODING_CATEGORY_MASK_ISO_8_ELSE;
1459
1460 if (inhibit_iso_escape_detection)
1461 break;
1462 if (c != ISO_CODE_CSI)
1463 {
1464 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags
1465 & CODING_FLAG_ISO_SINGLE_SHIFT)
1466 newmask |= CODING_CATEGORY_MASK_ISO_8_1;
1467 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags
1468 & CODING_FLAG_ISO_SINGLE_SHIFT)
1469 newmask |= CODING_CATEGORY_MASK_ISO_8_2;
1470 single_shifting = 1;
1471 }
1472 if (VECTORP (Vlatin_extra_code_table)
1473 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
1474 {
1475 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags
1476 & CODING_FLAG_ISO_LATIN_EXTRA)
1477 newmask |= CODING_CATEGORY_MASK_ISO_8_1;
1478 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags
1479 & CODING_FLAG_ISO_LATIN_EXTRA)
1480 newmask |= CODING_CATEGORY_MASK_ISO_8_2;
1481 }
1482 mask &= newmask;
1483 mask_found |= newmask;
1484 }
1485 break;
1486
1487 default:
1488 if (c < 0x80)
1489 {
1490 single_shifting = 0;
1491 break;
1492 }
1493 else if (c < 0xA0)
1494 {
1495 single_shifting = 0;
1496 if (VECTORP (Vlatin_extra_code_table)
1497 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
1498 {
1499 int newmask = 0;
1500
1501 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_1]->flags
1502 & CODING_FLAG_ISO_LATIN_EXTRA)
1503 newmask |= CODING_CATEGORY_MASK_ISO_8_1;
1504 if (coding_system_table[CODING_CATEGORY_IDX_ISO_8_2]->flags
1505 & CODING_FLAG_ISO_LATIN_EXTRA)
1506 newmask |= CODING_CATEGORY_MASK_ISO_8_2;
1507 mask &= newmask;
1508 mask_found |= newmask;
1509 }
1510 else
1511 return 0;
1512 }
1513 else
1514 {
1515 mask &= ~(CODING_CATEGORY_MASK_ISO_7BIT
1516 | CODING_CATEGORY_MASK_ISO_7_ELSE);
1517 mask_found |= CODING_CATEGORY_MASK_ISO_8_1;
1518 /* Check the length of succeeding codes of the range
1519 0xA0..0FF. If the byte length is odd, we exclude
1520 CODING_CATEGORY_MASK_ISO_8_2. We can check this only
1521 when we are not single shifting. */
1522 if (!single_shifting
1523 && mask & CODING_CATEGORY_MASK_ISO_8_2)
1524 {
1525 int i = 1;
1526 while (src < src_end)
1527 {
1528 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
1529 if (c < 0xA0)
1530 break;
1531 i++;
1532 }
1533
1534 if (i & 1 && src < src_end)
1535 mask &= ~CODING_CATEGORY_MASK_ISO_8_2;
1536 else
1537 mask_found |= CODING_CATEGORY_MASK_ISO_8_2;
1538 }
1539 }
1540 break;
1541 }
1542 }
1543 label_end_of_loop:
1544 return (mask & mask_found);
1545 }
1546
1547 /* Decode a character of which charset is CHARSET, the 1st position
1548 code is C1, the 2nd position code is C2, and return the decoded
1549 character code. If the variable `translation_table' is non-nil,
1550 returned the translated code. */
1551
1552 #define DECODE_ISO_CHARACTER(charset, c1, c2) \
1553 (NILP (translation_table) \
1554 ? MAKE_CHAR (charset, c1, c2) \
1555 : translate_char (translation_table, -1, charset, c1, c2))
1556
1557 /* Set designation state into CODING. */
1558 #define DECODE_DESIGNATION(reg, dimension, chars, final_char) \
1559 do { \
1560 int charset, c; \
1561 \
1562 if (final_char < '0' || final_char >= 128) \
1563 goto label_invalid_code; \
1564 charset = ISO_CHARSET_TABLE (make_number (dimension), \
1565 make_number (chars), \
1566 make_number (final_char)); \
1567 c = MAKE_CHAR (charset, 0, 0); \
1568 if (charset >= 0 \
1569 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) == reg \
1570 || CODING_SAFE_CHAR_P (safe_chars, c))) \
1571 { \
1572 if (coding->spec.iso2022.last_invalid_designation_register == 0 \
1573 && reg == 0 \
1574 && charset == CHARSET_ASCII) \
1575 { \
1576 /* We should insert this designation sequence as is so \
1577 that it is surely written back to a file. */ \
1578 coding->spec.iso2022.last_invalid_designation_register = -1; \
1579 goto label_invalid_code; \
1580 } \
1581 coding->spec.iso2022.last_invalid_designation_register = -1; \
1582 if ((coding->mode & CODING_MODE_DIRECTION) \
1583 && CHARSET_REVERSE_CHARSET (charset) >= 0) \
1584 charset = CHARSET_REVERSE_CHARSET (charset); \
1585 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
1586 } \
1587 else \
1588 { \
1589 coding->spec.iso2022.last_invalid_designation_register = reg; \
1590 goto label_invalid_code; \
1591 } \
1592 } while (0)
1593
1594 /* Allocate a memory block for storing information about compositions.
1595 The block is chained to the already allocated blocks. */
1596
1597 void
1598 coding_allocate_composition_data (coding, char_offset)
1599 struct coding_system *coding;
1600 int char_offset;
1601 {
1602 struct composition_data *cmp_data
1603 = (struct composition_data *) xmalloc (sizeof *cmp_data);
1604
1605 cmp_data->char_offset = char_offset;
1606 cmp_data->used = 0;
1607 cmp_data->prev = coding->cmp_data;
1608 cmp_data->next = NULL;
1609 if (coding->cmp_data)
1610 coding->cmp_data->next = cmp_data;
1611 coding->cmp_data = cmp_data;
1612 coding->cmp_data_start = 0;
1613 }
1614
1615 /* Handle composition start sequence ESC 0, ESC 2, ESC 3, or ESC 4.
1616 ESC 0 : relative composition : ESC 0 CHAR ... ESC 1
1617 ESC 2 : rulebase composition : ESC 2 CHAR RULE CHAR RULE ... CHAR ESC 1
1618 ESC 3 : altchar composition : ESC 3 ALT ... ESC 0 CHAR ... ESC 1
1619 ESC 4 : alt&rule composition : ESC 4 ALT RULE .. ALT ESC 0 CHAR ... ESC 1
1620 */
1621
1622 #define DECODE_COMPOSITION_START(c1) \
1623 do { \
1624 if (coding->composing == COMPOSITION_DISABLED) \
1625 { \
1626 *dst++ = ISO_CODE_ESC; \
1627 *dst++ = c1 & 0x7f; \
1628 coding->produced_char += 2; \
1629 } \
1630 else if (!COMPOSING_P (coding)) \
1631 { \
1632 /* This is surely the start of a composition. We must be sure \
1633 that coding->cmp_data has enough space to store the \
1634 information about the composition. If not, terminate the \
1635 current decoding loop, allocate one more memory block for \
1636 coding->cmp_data in the caller, then start the decoding \
1637 loop again. We can't allocate memory here directly because \
1638 it may cause buffer/string relocation. */ \
1639 if (!coding->cmp_data \
1640 || (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH \
1641 >= COMPOSITION_DATA_SIZE)) \
1642 { \
1643 coding->result = CODING_FINISH_INSUFFICIENT_CMP; \
1644 goto label_end_of_loop; \
1645 } \
1646 coding->composing = (c1 == '0' ? COMPOSITION_RELATIVE \
1647 : c1 == '2' ? COMPOSITION_WITH_RULE \
1648 : c1 == '3' ? COMPOSITION_WITH_ALTCHARS \
1649 : COMPOSITION_WITH_RULE_ALTCHARS); \
1650 CODING_ADD_COMPOSITION_START (coding, coding->produced_char, \
1651 coding->composing); \
1652 coding->composition_rule_follows = 0; \
1653 } \
1654 else \
1655 { \
1656 /* We are already handling a composition. If the method is \
1657 the following two, the codes following the current escape \
1658 sequence are actual characters stored in a buffer. */ \
1659 if (coding->composing == COMPOSITION_WITH_ALTCHARS \
1660 || coding->composing == COMPOSITION_WITH_RULE_ALTCHARS) \
1661 { \
1662 coding->composing = COMPOSITION_RELATIVE; \
1663 coding->composition_rule_follows = 0; \
1664 } \
1665 } \
1666 } while (0)
1667
1668 /* Handle composition end sequence ESC 1. */
1669
1670 #define DECODE_COMPOSITION_END(c1) \
1671 do { \
1672 if (! COMPOSING_P (coding)) \
1673 { \
1674 *dst++ = ISO_CODE_ESC; \
1675 *dst++ = c1; \
1676 coding->produced_char += 2; \
1677 } \
1678 else \
1679 { \
1680 CODING_ADD_COMPOSITION_END (coding, coding->produced_char); \
1681 coding->composing = COMPOSITION_NO; \
1682 } \
1683 } while (0)
1684
1685 /* Decode a composition rule from the byte C1 (and maybe one more byte
1686 from SRC) and store one encoded composition rule in
1687 coding->cmp_data. */
1688
1689 #define DECODE_COMPOSITION_RULE(c1) \
1690 do { \
1691 int rule = 0; \
1692 (c1) -= 32; \
1693 if (c1 < 81) /* old format (before ver.21) */ \
1694 { \
1695 int gref = (c1) / 9; \
1696 int nref = (c1) % 9; \
1697 if (gref == 4) gref = 10; \
1698 if (nref == 4) nref = 10; \
1699 rule = COMPOSITION_ENCODE_RULE (gref, nref); \
1700 } \
1701 else if (c1 < 93) /* new format (after ver.21) */ \
1702 { \
1703 ONE_MORE_BYTE (c2); \
1704 rule = COMPOSITION_ENCODE_RULE (c1 - 81, c2 - 32); \
1705 } \
1706 CODING_ADD_COMPOSITION_COMPONENT (coding, rule); \
1707 coding->composition_rule_follows = 0; \
1708 } while (0)
1709
1710
1711 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
1712
1713 static void
1714 decode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
1715 struct coding_system *coding;
1716 unsigned char *source, *destination;
1717 int src_bytes, dst_bytes;
1718 {
1719 unsigned char *src = source;
1720 unsigned char *src_end = source + src_bytes;
1721 unsigned char *dst = destination;
1722 unsigned char *dst_end = destination + dst_bytes;
1723 /* Charsets invoked to graphic plane 0 and 1 respectively. */
1724 int charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1725 int charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1);
1726 /* SRC_BASE remembers the start position in source in each loop.
1727 The loop will be exited when there's not enough source code
1728 (within macro ONE_MORE_BYTE), or when there's not enough
1729 destination area to produce a character (within macro
1730 EMIT_CHAR). */
1731 unsigned char *src_base;
1732 int c, charset;
1733 Lisp_Object translation_table;
1734 Lisp_Object safe_chars;
1735
1736 safe_chars = coding_safe_chars (coding);
1737
1738 if (NILP (Venable_character_translation))
1739 translation_table = Qnil;
1740 else
1741 {
1742 translation_table = coding->translation_table_for_decode;
1743 if (NILP (translation_table))
1744 translation_table = Vstandard_translation_table_for_decode;
1745 }
1746
1747 coding->result = CODING_FINISH_NORMAL;
1748
1749 while (1)
1750 {
1751 int c1, c2;
1752
1753 src_base = src;
1754 ONE_MORE_BYTE (c1);
1755
1756 /* We produce no character or one character. */
1757 switch (iso_code_class [c1])
1758 {
1759 case ISO_0x20_or_0x7F:
1760 if (COMPOSING_P (coding) && coding->composition_rule_follows)
1761 {
1762 DECODE_COMPOSITION_RULE (c1);
1763 continue;
1764 }
1765 if (charset0 < 0 || CHARSET_CHARS (charset0) == 94)
1766 {
1767 /* This is SPACE or DEL. */
1768 charset = CHARSET_ASCII;
1769 break;
1770 }
1771 /* This is a graphic character, we fall down ... */
1772
1773 case ISO_graphic_plane_0:
1774 if (COMPOSING_P (coding) && coding->composition_rule_follows)
1775 {
1776 DECODE_COMPOSITION_RULE (c1);
1777 continue;
1778 }
1779 charset = charset0;
1780 break;
1781
1782 case ISO_0xA0_or_0xFF:
1783 if (charset1 < 0 || CHARSET_CHARS (charset1) == 94
1784 || coding->flags & CODING_FLAG_ISO_SEVEN_BITS)
1785 goto label_invalid_code;
1786 /* This is a graphic character, we fall down ... */
1787
1788 case ISO_graphic_plane_1:
1789 if (charset1 < 0)
1790 goto label_invalid_code;
1791 charset = charset1;
1792 break;
1793
1794 case ISO_control_0:
1795 if (COMPOSING_P (coding))
1796 DECODE_COMPOSITION_END ('1');
1797
1798 /* All ISO2022 control characters in this class have the
1799 same representation in Emacs internal format. */
1800 if (c1 == '\n'
1801 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
1802 && (coding->eol_type == CODING_EOL_CR
1803 || coding->eol_type == CODING_EOL_CRLF))
1804 {
1805 coding->result = CODING_FINISH_INCONSISTENT_EOL;
1806 goto label_end_of_loop;
1807 }
1808 charset = CHARSET_ASCII;
1809 break;
1810
1811 case ISO_control_1:
1812 if (COMPOSING_P (coding))
1813 DECODE_COMPOSITION_END ('1');
1814 goto label_invalid_code;
1815
1816 case ISO_carriage_return:
1817 if (COMPOSING_P (coding))
1818 DECODE_COMPOSITION_END ('1');
1819
1820 if (coding->eol_type == CODING_EOL_CR)
1821 c1 = '\n';
1822 else if (coding->eol_type == CODING_EOL_CRLF)
1823 {
1824 ONE_MORE_BYTE (c1);
1825 if (c1 != ISO_CODE_LF)
1826 {
1827 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
1828 {
1829 coding->result = CODING_FINISH_INCONSISTENT_EOL;
1830 goto label_end_of_loop;
1831 }
1832 src--;
1833 c1 = '\r';
1834 }
1835 }
1836 charset = CHARSET_ASCII;
1837 break;
1838
1839 case ISO_shift_out:
1840 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)
1841 || CODING_SPEC_ISO_DESIGNATION (coding, 1) < 0)
1842 goto label_invalid_code;
1843 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1;
1844 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1845 continue;
1846
1847 case ISO_shift_in:
1848 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT))
1849 goto label_invalid_code;
1850 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0;
1851 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1852 continue;
1853
1854 case ISO_single_shift_2_7:
1855 case ISO_single_shift_2:
1856 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT))
1857 goto label_invalid_code;
1858 /* SS2 is handled as an escape sequence of ESC 'N' */
1859 c1 = 'N';
1860 goto label_escape_sequence;
1861
1862 case ISO_single_shift_3:
1863 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT))
1864 goto label_invalid_code;
1865 /* SS2 is handled as an escape sequence of ESC 'O' */
1866 c1 = 'O';
1867 goto label_escape_sequence;
1868
1869 case ISO_control_sequence_introducer:
1870 /* CSI is handled as an escape sequence of ESC '[' ... */
1871 c1 = '[';
1872 goto label_escape_sequence;
1873
1874 case ISO_escape:
1875 ONE_MORE_BYTE (c1);
1876 label_escape_sequence:
1877 /* Escape sequences handled by Emacs are invocation,
1878 designation, direction specification, and character
1879 composition specification. */
1880 switch (c1)
1881 {
1882 case '&': /* revision of following character set */
1883 ONE_MORE_BYTE (c1);
1884 if (!(c1 >= '@' && c1 <= '~'))
1885 goto label_invalid_code;
1886 ONE_MORE_BYTE (c1);
1887 if (c1 != ISO_CODE_ESC)
1888 goto label_invalid_code;
1889 ONE_MORE_BYTE (c1);
1890 goto label_escape_sequence;
1891
1892 case '$': /* designation of 2-byte character set */
1893 if (! (coding->flags & CODING_FLAG_ISO_DESIGNATION))
1894 goto label_invalid_code;
1895 ONE_MORE_BYTE (c1);
1896 if (c1 >= '@' && c1 <= 'B')
1897 { /* designation of JISX0208.1978, GB2312.1980,
1898 or JISX0208.1980 */
1899 DECODE_DESIGNATION (0, 2, 94, c1);
1900 }
1901 else if (c1 >= 0x28 && c1 <= 0x2B)
1902 { /* designation of DIMENSION2_CHARS94 character set */
1903 ONE_MORE_BYTE (c2);
1904 DECODE_DESIGNATION (c1 - 0x28, 2, 94, c2);
1905 }
1906 else if (c1 >= 0x2C && c1 <= 0x2F)
1907 { /* designation of DIMENSION2_CHARS96 character set */
1908 ONE_MORE_BYTE (c2);
1909 DECODE_DESIGNATION (c1 - 0x2C, 2, 96, c2);
1910 }
1911 else
1912 goto label_invalid_code;
1913 /* We must update these variables now. */
1914 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1915 charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1);
1916 continue;
1917
1918 case 'n': /* invocation of locking-shift-2 */
1919 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)
1920 || CODING_SPEC_ISO_DESIGNATION (coding, 2) < 0)
1921 goto label_invalid_code;
1922 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2;
1923 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1924 continue;
1925
1926 case 'o': /* invocation of locking-shift-3 */
1927 if (! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT)
1928 || CODING_SPEC_ISO_DESIGNATION (coding, 3) < 0)
1929 goto label_invalid_code;
1930 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3;
1931 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
1932 continue;
1933
1934 case 'N': /* invocation of single-shift-2 */
1935 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
1936 || CODING_SPEC_ISO_DESIGNATION (coding, 2) < 0)
1937 goto label_invalid_code;
1938 charset = CODING_SPEC_ISO_DESIGNATION (coding, 2);
1939 ONE_MORE_BYTE (c1);
1940 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
1941 goto label_invalid_code;
1942 break;
1943
1944 case 'O': /* invocation of single-shift-3 */
1945 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
1946 || CODING_SPEC_ISO_DESIGNATION (coding, 3) < 0)
1947 goto label_invalid_code;
1948 charset = CODING_SPEC_ISO_DESIGNATION (coding, 3);
1949 ONE_MORE_BYTE (c1);
1950 if (c1 < 0x20 || (c1 >= 0x80 && c1 < 0xA0))
1951 goto label_invalid_code;
1952 break;
1953
1954 case '0': case '2': case '3': case '4': /* start composition */
1955 DECODE_COMPOSITION_START (c1);
1956 continue;
1957
1958 case '1': /* end composition */
1959 DECODE_COMPOSITION_END (c1);
1960 continue;
1961
1962 case '[': /* specification of direction */
1963 if (coding->flags & CODING_FLAG_ISO_NO_DIRECTION)
1964 goto label_invalid_code;
1965 /* For the moment, nested direction is not supported.
1966 So, `coding->mode & CODING_MODE_DIRECTION' zero means
1967 left-to-right, and nonzero means right-to-left. */
1968 ONE_MORE_BYTE (c1);
1969 switch (c1)
1970 {
1971 case ']': /* end of the current direction */
1972 coding->mode &= ~CODING_MODE_DIRECTION;
1973
1974 case '0': /* end of the current direction */
1975 case '1': /* start of left-to-right direction */
1976 ONE_MORE_BYTE (c1);
1977 if (c1 == ']')
1978 coding->mode &= ~CODING_MODE_DIRECTION;
1979 else
1980 goto label_invalid_code;
1981 break;
1982
1983 case '2': /* start of right-to-left direction */
1984 ONE_MORE_BYTE (c1);
1985 if (c1 == ']')
1986 coding->mode |= CODING_MODE_DIRECTION;
1987 else
1988 goto label_invalid_code;
1989 break;
1990
1991 default:
1992 goto label_invalid_code;
1993 }
1994 continue;
1995
1996 default:
1997 if (! (coding->flags & CODING_FLAG_ISO_DESIGNATION))
1998 goto label_invalid_code;
1999 if (c1 >= 0x28 && c1 <= 0x2B)
2000 { /* designation of DIMENSION1_CHARS94 character set */
2001 ONE_MORE_BYTE (c2);
2002 DECODE_DESIGNATION (c1 - 0x28, 1, 94, c2);
2003 }
2004 else if (c1 >= 0x2C && c1 <= 0x2F)
2005 { /* designation of DIMENSION1_CHARS96 character set */
2006 ONE_MORE_BYTE (c2);
2007 DECODE_DESIGNATION (c1 - 0x2C, 1, 96, c2);
2008 }
2009 else
2010 goto label_invalid_code;
2011 /* We must update these variables now. */
2012 charset0 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 0);
2013 charset1 = CODING_SPEC_ISO_PLANE_CHARSET (coding, 1);
2014 continue;
2015 }
2016 }
2017
2018 /* Now we know CHARSET and 1st position code C1 of a character.
2019 Produce a multibyte sequence for that character while getting
2020 2nd position code C2 if necessary. */
2021 if (CHARSET_DIMENSION (charset) == 2)
2022 {
2023 ONE_MORE_BYTE (c2);
2024 if (c1 < 0x80 ? c2 < 0x20 || c2 >= 0x80 : c2 < 0xA0)
2025 /* C2 is not in a valid range. */
2026 goto label_invalid_code;
2027 }
2028 c = DECODE_ISO_CHARACTER (charset, c1, c2);
2029 EMIT_CHAR (c);
2030 continue;
2031
2032 label_invalid_code:
2033 coding->errors++;
2034 if (COMPOSING_P (coding))
2035 DECODE_COMPOSITION_END ('1');
2036 src = src_base;
2037 c = *src++;
2038 EMIT_CHAR (c);
2039 }
2040
2041 label_end_of_loop:
2042 coding->consumed = coding->consumed_char = src_base - source;
2043 coding->produced = dst - destination;
2044 return;
2045 }
2046
2047
2048 /* ISO2022 encoding stuff. */
2049
2050 /*
2051 It is not enough to say just "ISO2022" on encoding, we have to
2052 specify more details. In Emacs, each ISO2022 coding system
2053 variant has the following specifications:
2054 1. Initial designation to G0 through G3.
2055 2. Allows short-form designation?
2056 3. ASCII should be designated to G0 before control characters?
2057 4. ASCII should be designated to G0 at end of line?
2058 5. 7-bit environment or 8-bit environment?
2059 6. Use locking-shift?
2060 7. Use Single-shift?
2061 And the following two are only for Japanese:
2062 8. Use ASCII in place of JIS0201-1976-Roman?
2063 9. Use JISX0208-1983 in place of JISX0208-1978?
2064 These specifications are encoded in `coding->flags' as flag bits
2065 defined by macros CODING_FLAG_ISO_XXX. See `coding.h' for more
2066 details.
2067 */
2068
2069 /* Produce codes (escape sequence) for designating CHARSET to graphic
2070 register REG at DST, and increment DST. If <final-char> of CHARSET is
2071 '@', 'A', or 'B' and the coding system CODING allows, produce
2072 designation sequence of short-form. */
2073
2074 #define ENCODE_DESIGNATION(charset, reg, coding) \
2075 do { \
2076 unsigned char final_char = CHARSET_ISO_FINAL_CHAR (charset); \
2077 char *intermediate_char_94 = "()*+"; \
2078 char *intermediate_char_96 = ",-./"; \
2079 int revision = CODING_SPEC_ISO_REVISION_NUMBER(coding, charset); \
2080 \
2081 if (revision < 255) \
2082 { \
2083 *dst++ = ISO_CODE_ESC; \
2084 *dst++ = '&'; \
2085 *dst++ = '@' + revision; \
2086 } \
2087 *dst++ = ISO_CODE_ESC; \
2088 if (CHARSET_DIMENSION (charset) == 1) \
2089 { \
2090 if (CHARSET_CHARS (charset) == 94) \
2091 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
2092 else \
2093 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
2094 } \
2095 else \
2096 { \
2097 *dst++ = '$'; \
2098 if (CHARSET_CHARS (charset) == 94) \
2099 { \
2100 if (! (coding->flags & CODING_FLAG_ISO_SHORT_FORM) \
2101 || reg != 0 \
2102 || final_char < '@' || final_char > 'B') \
2103 *dst++ = (unsigned char) (intermediate_char_94[reg]); \
2104 } \
2105 else \
2106 *dst++ = (unsigned char) (intermediate_char_96[reg]); \
2107 } \
2108 *dst++ = final_char; \
2109 CODING_SPEC_ISO_DESIGNATION (coding, reg) = charset; \
2110 } while (0)
2111
2112 /* The following two macros produce codes (control character or escape
2113 sequence) for ISO2022 single-shift functions (single-shift-2 and
2114 single-shift-3). */
2115
2116 #define ENCODE_SINGLE_SHIFT_2 \
2117 do { \
2118 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2119 *dst++ = ISO_CODE_ESC, *dst++ = 'N'; \
2120 else \
2121 *dst++ = ISO_CODE_SS2; \
2122 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
2123 } while (0)
2124
2125 #define ENCODE_SINGLE_SHIFT_3 \
2126 do { \
2127 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2128 *dst++ = ISO_CODE_ESC, *dst++ = 'O'; \
2129 else \
2130 *dst++ = ISO_CODE_SS3; \
2131 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 1; \
2132 } while (0)
2133
2134 /* The following four macros produce codes (control character or
2135 escape sequence) for ISO2022 locking-shift functions (shift-in,
2136 shift-out, locking-shift-2, and locking-shift-3). */
2137
2138 #define ENCODE_SHIFT_IN \
2139 do { \
2140 *dst++ = ISO_CODE_SI; \
2141 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0; \
2142 } while (0)
2143
2144 #define ENCODE_SHIFT_OUT \
2145 do { \
2146 *dst++ = ISO_CODE_SO; \
2147 CODING_SPEC_ISO_INVOCATION (coding, 0) = 1; \
2148 } while (0)
2149
2150 #define ENCODE_LOCKING_SHIFT_2 \
2151 do { \
2152 *dst++ = ISO_CODE_ESC, *dst++ = 'n'; \
2153 CODING_SPEC_ISO_INVOCATION (coding, 0) = 2; \
2154 } while (0)
2155
2156 #define ENCODE_LOCKING_SHIFT_3 \
2157 do { \
2158 *dst++ = ISO_CODE_ESC, *dst++ = 'o'; \
2159 CODING_SPEC_ISO_INVOCATION (coding, 0) = 3; \
2160 } while (0)
2161
2162 /* Produce codes for a DIMENSION1 character whose character set is
2163 CHARSET and whose position-code is C1. Designation and invocation
2164 sequences are also produced in advance if necessary. */
2165
2166 #define ENCODE_ISO_CHARACTER_DIMENSION1(charset, c1) \
2167 do { \
2168 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
2169 { \
2170 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2171 *dst++ = c1 & 0x7F; \
2172 else \
2173 *dst++ = c1 | 0x80; \
2174 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
2175 break; \
2176 } \
2177 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
2178 { \
2179 *dst++ = c1 & 0x7F; \
2180 break; \
2181 } \
2182 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
2183 { \
2184 *dst++ = c1 | 0x80; \
2185 break; \
2186 } \
2187 else \
2188 /* Since CHARSET is not yet invoked to any graphic planes, we \
2189 must invoke it, or, at first, designate it to some graphic \
2190 register. Then repeat the loop to actually produce the \
2191 character. */ \
2192 dst = encode_invocation_designation (charset, coding, dst); \
2193 } while (1)
2194
2195 /* Produce codes for a DIMENSION2 character whose character set is
2196 CHARSET and whose position-codes are C1 and C2. Designation and
2197 invocation codes are also produced in advance if necessary. */
2198
2199 #define ENCODE_ISO_CHARACTER_DIMENSION2(charset, c1, c2) \
2200 do { \
2201 if (CODING_SPEC_ISO_SINGLE_SHIFTING (coding)) \
2202 { \
2203 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS) \
2204 *dst++ = c1 & 0x7F, *dst++ = c2 & 0x7F; \
2205 else \
2206 *dst++ = c1 | 0x80, *dst++ = c2 | 0x80; \
2207 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0; \
2208 break; \
2209 } \
2210 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 0)) \
2211 { \
2212 *dst++ = c1 & 0x7F, *dst++= c2 & 0x7F; \
2213 break; \
2214 } \
2215 else if (charset == CODING_SPEC_ISO_PLANE_CHARSET (coding, 1)) \
2216 { \
2217 *dst++ = c1 | 0x80, *dst++= c2 | 0x80; \
2218 break; \
2219 } \
2220 else \
2221 /* Since CHARSET is not yet invoked to any graphic planes, we \
2222 must invoke it, or, at first, designate it to some graphic \
2223 register. Then repeat the loop to actually produce the \
2224 character. */ \
2225 dst = encode_invocation_designation (charset, coding, dst); \
2226 } while (1)
2227
2228 #define ENCODE_ISO_CHARACTER(c) \
2229 do { \
2230 int charset, c1, c2; \
2231 \
2232 SPLIT_CHAR (c, charset, c1, c2); \
2233 if (CHARSET_DEFINED_P (charset)) \
2234 { \
2235 if (CHARSET_DIMENSION (charset) == 1) \
2236 { \
2237 if (charset == CHARSET_ASCII \
2238 && coding->flags & CODING_FLAG_ISO_USE_ROMAN) \
2239 charset = charset_latin_jisx0201; \
2240 ENCODE_ISO_CHARACTER_DIMENSION1 (charset, c1); \
2241 } \
2242 else \
2243 { \
2244 if (charset == charset_jisx0208 \
2245 && coding->flags & CODING_FLAG_ISO_USE_OLDJIS) \
2246 charset = charset_jisx0208_1978; \
2247 ENCODE_ISO_CHARACTER_DIMENSION2 (charset, c1, c2); \
2248 } \
2249 } \
2250 else \
2251 { \
2252 *dst++ = c1; \
2253 if (c2 >= 0) \
2254 *dst++ = c2; \
2255 } \
2256 } while (0)
2257
2258
2259 /* Instead of encoding character C, produce one or two `?'s. */
2260
2261 #define ENCODE_UNSAFE_CHARACTER(c) \
2262 do { \
2263 ENCODE_ISO_CHARACTER (CODING_INHIBIT_CHARACTER_SUBSTITUTION); \
2264 if (CHARSET_WIDTH (CHAR_CHARSET (c)) > 1) \
2265 ENCODE_ISO_CHARACTER (CODING_INHIBIT_CHARACTER_SUBSTITUTION); \
2266 } while (0)
2267
2268
2269 /* Produce designation and invocation codes at a place pointed by DST
2270 to use CHARSET. The element `spec.iso2022' of *CODING is updated.
2271 Return new DST. */
2272
2273 unsigned char *
2274 encode_invocation_designation (charset, coding, dst)
2275 int charset;
2276 struct coding_system *coding;
2277 unsigned char *dst;
2278 {
2279 int reg; /* graphic register number */
2280
2281 /* At first, check designations. */
2282 for (reg = 0; reg < 4; reg++)
2283 if (charset == CODING_SPEC_ISO_DESIGNATION (coding, reg))
2284 break;
2285
2286 if (reg >= 4)
2287 {
2288 /* CHARSET is not yet designated to any graphic registers. */
2289 /* At first check the requested designation. */
2290 reg = CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset);
2291 if (reg == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION)
2292 /* Since CHARSET requests no special designation, designate it
2293 to graphic register 0. */
2294 reg = 0;
2295
2296 ENCODE_DESIGNATION (charset, reg, coding);
2297 }
2298
2299 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != reg
2300 && CODING_SPEC_ISO_INVOCATION (coding, 1) != reg)
2301 {
2302 /* Since the graphic register REG is not invoked to any graphic
2303 planes, invoke it to graphic plane 0. */
2304 switch (reg)
2305 {
2306 case 0: /* graphic register 0 */
2307 ENCODE_SHIFT_IN;
2308 break;
2309
2310 case 1: /* graphic register 1 */
2311 ENCODE_SHIFT_OUT;
2312 break;
2313
2314 case 2: /* graphic register 2 */
2315 if (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
2316 ENCODE_SINGLE_SHIFT_2;
2317 else
2318 ENCODE_LOCKING_SHIFT_2;
2319 break;
2320
2321 case 3: /* graphic register 3 */
2322 if (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT)
2323 ENCODE_SINGLE_SHIFT_3;
2324 else
2325 ENCODE_LOCKING_SHIFT_3;
2326 break;
2327 }
2328 }
2329
2330 return dst;
2331 }
2332
2333 /* Produce 2-byte codes for encoded composition rule RULE. */
2334
2335 #define ENCODE_COMPOSITION_RULE(rule) \
2336 do { \
2337 int gref, nref; \
2338 COMPOSITION_DECODE_RULE (rule, gref, nref); \
2339 *dst++ = 32 + 81 + gref; \
2340 *dst++ = 32 + nref; \
2341 } while (0)
2342
2343 /* Produce codes for indicating the start of a composition sequence
2344 (ESC 0, ESC 3, or ESC 4). DATA points to an array of integers
2345 which specify information about the composition. See the comment
2346 in coding.h for the format of DATA. */
2347
2348 #define ENCODE_COMPOSITION_START(coding, data) \
2349 do { \
2350 coding->composing = data[3]; \
2351 *dst++ = ISO_CODE_ESC; \
2352 if (coding->composing == COMPOSITION_RELATIVE) \
2353 *dst++ = '0'; \
2354 else \
2355 { \
2356 *dst++ = (coding->composing == COMPOSITION_WITH_ALTCHARS \
2357 ? '3' : '4'); \
2358 coding->cmp_data_index = coding->cmp_data_start + 4; \
2359 coding->composition_rule_follows = 0; \
2360 } \
2361 } while (0)
2362
2363 /* Produce codes for indicating the end of the current composition. */
2364
2365 #define ENCODE_COMPOSITION_END(coding, data) \
2366 do { \
2367 *dst++ = ISO_CODE_ESC; \
2368 *dst++ = '1'; \
2369 coding->cmp_data_start += data[0]; \
2370 coding->composing = COMPOSITION_NO; \
2371 if (coding->cmp_data_start == coding->cmp_data->used \
2372 && coding->cmp_data->next) \
2373 { \
2374 coding->cmp_data = coding->cmp_data->next; \
2375 coding->cmp_data_start = 0; \
2376 } \
2377 } while (0)
2378
2379 /* Produce composition start sequence ESC 0. Here, this sequence
2380 doesn't mean the start of a new composition but means that we have
2381 just produced components (alternate chars and composition rules) of
2382 the composition and the actual text follows in SRC. */
2383
2384 #define ENCODE_COMPOSITION_FAKE_START(coding) \
2385 do { \
2386 *dst++ = ISO_CODE_ESC; \
2387 *dst++ = '0'; \
2388 coding->composing = COMPOSITION_RELATIVE; \
2389 } while (0)
2390
2391 /* The following three macros produce codes for indicating direction
2392 of text. */
2393 #define ENCODE_CONTROL_SEQUENCE_INTRODUCER \
2394 do { \
2395 if (coding->flags == CODING_FLAG_ISO_SEVEN_BITS) \
2396 *dst++ = ISO_CODE_ESC, *dst++ = '['; \
2397 else \
2398 *dst++ = ISO_CODE_CSI; \
2399 } while (0)
2400
2401 #define ENCODE_DIRECTION_R2L \
2402 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst), *dst++ = '2', *dst++ = ']'
2403
2404 #define ENCODE_DIRECTION_L2R \
2405 ENCODE_CONTROL_SEQUENCE_INTRODUCER (dst), *dst++ = '0', *dst++ = ']'
2406
2407 /* Produce codes for designation and invocation to reset the graphic
2408 planes and registers to initial state. */
2409 #define ENCODE_RESET_PLANE_AND_REGISTER \
2410 do { \
2411 int reg; \
2412 if (CODING_SPEC_ISO_INVOCATION (coding, 0) != 0) \
2413 ENCODE_SHIFT_IN; \
2414 for (reg = 0; reg < 4; reg++) \
2415 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg) >= 0 \
2416 && (CODING_SPEC_ISO_DESIGNATION (coding, reg) \
2417 != CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg))) \
2418 ENCODE_DESIGNATION \
2419 (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, reg), reg, coding); \
2420 } while (0)
2421
2422 /* Produce designation sequences of charsets in the line started from
2423 SRC to a place pointed by DST, and return updated DST.
2424
2425 If the current block ends before any end-of-line, we may fail to
2426 find all the necessary designations. */
2427
2428 static unsigned char *
2429 encode_designation_at_bol (coding, translation_table, src, src_end, dst)
2430 struct coding_system *coding;
2431 Lisp_Object translation_table;
2432 unsigned char *src, *src_end, *dst;
2433 {
2434 int charset, c, found = 0, reg;
2435 /* Table of charsets to be designated to each graphic register. */
2436 int r[4];
2437
2438 for (reg = 0; reg < 4; reg++)
2439 r[reg] = -1;
2440
2441 while (found < 4)
2442 {
2443 ONE_MORE_CHAR (c);
2444 if (c == '\n')
2445 break;
2446
2447 charset = CHAR_CHARSET (c);
2448 reg = CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset);
2449 if (reg != CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION && r[reg] < 0)
2450 {
2451 found++;
2452 r[reg] = charset;
2453 }
2454 }
2455
2456 label_end_of_loop:
2457 if (found)
2458 {
2459 for (reg = 0; reg < 4; reg++)
2460 if (r[reg] >= 0
2461 && CODING_SPEC_ISO_DESIGNATION (coding, reg) != r[reg])
2462 ENCODE_DESIGNATION (r[reg], reg, coding);
2463 }
2464
2465 return dst;
2466 }
2467
2468 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions". */
2469
2470 static void
2471 encode_coding_iso2022 (coding, source, destination, src_bytes, dst_bytes)
2472 struct coding_system *coding;
2473 unsigned char *source, *destination;
2474 int src_bytes, dst_bytes;
2475 {
2476 unsigned char *src = source;
2477 unsigned char *src_end = source + src_bytes;
2478 unsigned char *dst = destination;
2479 unsigned char *dst_end = destination + dst_bytes;
2480 /* Since the maximum bytes produced by each loop is 20, we subtract 19
2481 from DST_END to assure overflow checking is necessary only at the
2482 head of loop. */
2483 unsigned char *adjusted_dst_end = dst_end - 19;
2484 /* SRC_BASE remembers the start position in source in each loop.
2485 The loop will be exited when there's not enough source text to
2486 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
2487 there's not enough destination area to produce encoded codes
2488 (within macro EMIT_BYTES). */
2489 unsigned char *src_base;
2490 int c;
2491 Lisp_Object translation_table;
2492 Lisp_Object safe_chars;
2493
2494 safe_chars = coding_safe_chars (coding);
2495
2496 if (NILP (Venable_character_translation))
2497 translation_table = Qnil;
2498 else
2499 {
2500 translation_table = coding->translation_table_for_encode;
2501 if (NILP (translation_table))
2502 translation_table = Vstandard_translation_table_for_encode;
2503 }
2504
2505 coding->consumed_char = 0;
2506 coding->errors = 0;
2507 while (1)
2508 {
2509 src_base = src;
2510
2511 if (dst >= (dst_bytes ? adjusted_dst_end : (src - 19)))
2512 {
2513 coding->result = CODING_FINISH_INSUFFICIENT_DST;
2514 break;
2515 }
2516
2517 if (coding->flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL
2518 && CODING_SPEC_ISO_BOL (coding))
2519 {
2520 /* We have to produce designation sequences if any now. */
2521 dst = encode_designation_at_bol (coding, translation_table,
2522 src, src_end, dst);
2523 CODING_SPEC_ISO_BOL (coding) = 0;
2524 }
2525
2526 /* Check composition start and end. */
2527 if (coding->composing != COMPOSITION_DISABLED
2528 && coding->cmp_data_start < coding->cmp_data->used)
2529 {
2530 struct composition_data *cmp_data = coding->cmp_data;
2531 int *data = cmp_data->data + coding->cmp_data_start;
2532 int this_pos = cmp_data->char_offset + coding->consumed_char;
2533
2534 if (coding->composing == COMPOSITION_RELATIVE)
2535 {
2536 if (this_pos == data[2])
2537 {
2538 ENCODE_COMPOSITION_END (coding, data);
2539 cmp_data = coding->cmp_data;
2540 data = cmp_data->data + coding->cmp_data_start;
2541 }
2542 }
2543 else if (COMPOSING_P (coding))
2544 {
2545 /* COMPOSITION_WITH_ALTCHARS or COMPOSITION_WITH_RULE_ALTCHAR */
2546 if (coding->cmp_data_index == coding->cmp_data_start + data[0])
2547 /* We have consumed components of the composition.
2548 What follows in SRC is the composition's base
2549 text. */
2550 ENCODE_COMPOSITION_FAKE_START (coding);
2551 else
2552 {
2553 int c = cmp_data->data[coding->cmp_data_index++];
2554 if (coding->composition_rule_follows)
2555 {
2556 ENCODE_COMPOSITION_RULE (c);
2557 coding->composition_rule_follows = 0;
2558 }
2559 else
2560 {
2561 if (coding->flags & CODING_FLAG_ISO_SAFE
2562 && ! CODING_SAFE_CHAR_P (safe_chars, c))
2563 ENCODE_UNSAFE_CHARACTER (c);
2564 else
2565 ENCODE_ISO_CHARACTER (c);
2566 if (coding->composing == COMPOSITION_WITH_RULE_ALTCHARS)
2567 coding->composition_rule_follows = 1;
2568 }
2569 continue;
2570 }
2571 }
2572 if (!COMPOSING_P (coding))
2573 {
2574 if (this_pos == data[1])
2575 {
2576 ENCODE_COMPOSITION_START (coding, data);
2577 continue;
2578 }
2579 }
2580 }
2581
2582 ONE_MORE_CHAR (c);
2583
2584 /* Now encode the character C. */
2585 if (c < 0x20 || c == 0x7F)
2586 {
2587 if (c == '\r')
2588 {
2589 if (! (coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
2590 {
2591 if (coding->flags & CODING_FLAG_ISO_RESET_AT_CNTL)
2592 ENCODE_RESET_PLANE_AND_REGISTER;
2593 *dst++ = c;
2594 continue;
2595 }
2596 /* fall down to treat '\r' as '\n' ... */
2597 c = '\n';
2598 }
2599 if (c == '\n')
2600 {
2601 if (coding->flags & CODING_FLAG_ISO_RESET_AT_EOL)
2602 ENCODE_RESET_PLANE_AND_REGISTER;
2603 if (coding->flags & CODING_FLAG_ISO_INIT_AT_BOL)
2604 bcopy (coding->spec.iso2022.initial_designation,
2605 coding->spec.iso2022.current_designation,
2606 sizeof coding->spec.iso2022.initial_designation);
2607 if (coding->eol_type == CODING_EOL_LF
2608 || coding->eol_type == CODING_EOL_UNDECIDED)
2609 *dst++ = ISO_CODE_LF;
2610 else if (coding->eol_type == CODING_EOL_CRLF)
2611 *dst++ = ISO_CODE_CR, *dst++ = ISO_CODE_LF;
2612 else
2613 *dst++ = ISO_CODE_CR;
2614 CODING_SPEC_ISO_BOL (coding) = 1;
2615 }
2616 else
2617 {
2618 if (coding->flags & CODING_FLAG_ISO_RESET_AT_CNTL)
2619 ENCODE_RESET_PLANE_AND_REGISTER;
2620 *dst++ = c;
2621 }
2622 }
2623 else if (ASCII_BYTE_P (c))
2624 ENCODE_ISO_CHARACTER (c);
2625 else if (SINGLE_BYTE_CHAR_P (c))
2626 {
2627 *dst++ = c;
2628 coding->errors++;
2629 }
2630 else if (coding->flags & CODING_FLAG_ISO_SAFE
2631 && ! CODING_SAFE_CHAR_P (safe_chars, c))
2632 ENCODE_UNSAFE_CHARACTER (c);
2633 else
2634 ENCODE_ISO_CHARACTER (c);
2635
2636 coding->consumed_char++;
2637 }
2638
2639 label_end_of_loop:
2640 coding->consumed = src_base - source;
2641 coding->produced = coding->produced_char = dst - destination;
2642 }
2643
2644 \f
2645 /*** 4. SJIS and BIG5 handlers ***/
2646
2647 /* Although SJIS and BIG5 are not ISO coding systems, they are used
2648 quite widely. So, for the moment, Emacs supports them in the bare
2649 C code. But, in the future, they may be supported only by CCL. */
2650
2651 /* SJIS is a coding system encoding three character sets: ASCII, right
2652 half of JISX0201-Kana, and JISX0208. An ASCII character is encoded
2653 as is. A character of charset katakana-jisx0201 is encoded by
2654 "position-code + 0x80". A character of charset japanese-jisx0208
2655 is encoded in 2-byte but two position-codes are divided and shifted
2656 so that it fits in the range below.
2657
2658 --- CODE RANGE of SJIS ---
2659 (character set) (range)
2660 ASCII 0x00 .. 0x7F
2661 KATAKANA-JISX0201 0xA1 .. 0xDF
2662 JISX0208 (1st byte) 0x81 .. 0x9F and 0xE0 .. 0xEF
2663 (2nd byte) 0x40 .. 0x7E and 0x80 .. 0xFC
2664 -------------------------------
2665
2666 */
2667
2668 /* BIG5 is a coding system encoding two character sets: ASCII and
2669 Big5. An ASCII character is encoded as is. Big5 is a two-byte
2670 character set and is encoded in two bytes.
2671
2672 --- CODE RANGE of BIG5 ---
2673 (character set) (range)
2674 ASCII 0x00 .. 0x7F
2675 Big5 (1st byte) 0xA1 .. 0xFE
2676 (2nd byte) 0x40 .. 0x7E and 0xA1 .. 0xFE
2677 --------------------------
2678
2679 Since the number of characters in Big5 is larger than maximum
2680 characters in Emacs' charset (96x96), it can't be handled as one
2681 charset. So, in Emacs, Big5 is divided into two: `charset-big5-1'
2682 and `charset-big5-2'. Both are DIMENSION2 and CHARS94. The former
2683 contains frequently used characters and the latter contains less
2684 frequently used characters. */
2685
2686 /* Macros to decode or encode a character of Big5 in BIG5. B1 and B2
2687 are the 1st and 2nd position-codes of Big5 in BIG5 coding system.
2688 C1 and C2 are the 1st and 2nd position-codes of Emacs' internal
2689 format. CHARSET is `charset_big5_1' or `charset_big5_2'. */
2690
2691 /* Number of Big5 characters which have the same code in 1st byte. */
2692 #define BIG5_SAME_ROW (0xFF - 0xA1 + 0x7F - 0x40)
2693
2694 #define DECODE_BIG5(b1, b2, charset, c1, c2) \
2695 do { \
2696 unsigned int temp \
2697 = (b1 - 0xA1) * BIG5_SAME_ROW + b2 - (b2 < 0x7F ? 0x40 : 0x62); \
2698 if (b1 < 0xC9) \
2699 charset = charset_big5_1; \
2700 else \
2701 { \
2702 charset = charset_big5_2; \
2703 temp -= (0xC9 - 0xA1) * BIG5_SAME_ROW; \
2704 } \
2705 c1 = temp / (0xFF - 0xA1) + 0x21; \
2706 c2 = temp % (0xFF - 0xA1) + 0x21; \
2707 } while (0)
2708
2709 #define ENCODE_BIG5(charset, c1, c2, b1, b2) \
2710 do { \
2711 unsigned int temp = (c1 - 0x21) * (0xFF - 0xA1) + (c2 - 0x21); \
2712 if (charset == charset_big5_2) \
2713 temp += BIG5_SAME_ROW * (0xC9 - 0xA1); \
2714 b1 = temp / BIG5_SAME_ROW + 0xA1; \
2715 b2 = temp % BIG5_SAME_ROW; \
2716 b2 += b2 < 0x3F ? 0x40 : 0x62; \
2717 } while (0)
2718
2719 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2720 Check if a text is encoded in SJIS. If it is, return
2721 CODING_CATEGORY_MASK_SJIS, else return 0. */
2722
2723 static int
2724 detect_coding_sjis (src, src_end, multibytep)
2725 unsigned char *src, *src_end;
2726 int multibytep;
2727 {
2728 int c;
2729 /* Dummy for ONE_MORE_BYTE. */
2730 struct coding_system dummy_coding;
2731 struct coding_system *coding = &dummy_coding;
2732
2733 while (1)
2734 {
2735 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
2736 if (c < 0x80)
2737 continue;
2738 if (c == 0x80 || c == 0xA0 || c > 0xEF)
2739 return 0;
2740 if (c <= 0x9F || c >= 0xE0)
2741 {
2742 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
2743 if (c < 0x40 || c == 0x7F || c > 0xFC)
2744 return 0;
2745 }
2746 }
2747 label_end_of_loop:
2748 return CODING_CATEGORY_MASK_SJIS;
2749 }
2750
2751 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2752 Check if a text is encoded in BIG5. If it is, return
2753 CODING_CATEGORY_MASK_BIG5, else return 0. */
2754
2755 static int
2756 detect_coding_big5 (src, src_end, multibytep)
2757 unsigned char *src, *src_end;
2758 int multibytep;
2759 {
2760 int c;
2761 /* Dummy for ONE_MORE_BYTE. */
2762 struct coding_system dummy_coding;
2763 struct coding_system *coding = &dummy_coding;
2764
2765 while (1)
2766 {
2767 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
2768 if (c < 0x80)
2769 continue;
2770 if (c < 0xA1 || c > 0xFE)
2771 return 0;
2772 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
2773 if (c < 0x40 || (c > 0x7F && c < 0xA1) || c > 0xFE)
2774 return 0;
2775 }
2776 label_end_of_loop:
2777 return CODING_CATEGORY_MASK_BIG5;
2778 }
2779
2780 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2781 Check if a text is encoded in UTF-8. If it is, return
2782 CODING_CATEGORY_MASK_UTF_8, else return 0. */
2783
2784 #define UTF_8_1_OCTET_P(c) ((c) < 0x80)
2785 #define UTF_8_EXTRA_OCTET_P(c) (((c) & 0xC0) == 0x80)
2786 #define UTF_8_2_OCTET_LEADING_P(c) (((c) & 0xE0) == 0xC0)
2787 #define UTF_8_3_OCTET_LEADING_P(c) (((c) & 0xF0) == 0xE0)
2788 #define UTF_8_4_OCTET_LEADING_P(c) (((c) & 0xF8) == 0xF0)
2789 #define UTF_8_5_OCTET_LEADING_P(c) (((c) & 0xFC) == 0xF8)
2790 #define UTF_8_6_OCTET_LEADING_P(c) (((c) & 0xFE) == 0xFC)
2791
2792 static int
2793 detect_coding_utf_8 (src, src_end, multibytep)
2794 unsigned char *src, *src_end;
2795 int multibytep;
2796 {
2797 unsigned char c;
2798 int seq_maybe_bytes;
2799 /* Dummy for ONE_MORE_BYTE. */
2800 struct coding_system dummy_coding;
2801 struct coding_system *coding = &dummy_coding;
2802
2803 while (1)
2804 {
2805 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
2806 if (UTF_8_1_OCTET_P (c))
2807 continue;
2808 else if (UTF_8_2_OCTET_LEADING_P (c))
2809 seq_maybe_bytes = 1;
2810 else if (UTF_8_3_OCTET_LEADING_P (c))
2811 seq_maybe_bytes = 2;
2812 else if (UTF_8_4_OCTET_LEADING_P (c))
2813 seq_maybe_bytes = 3;
2814 else if (UTF_8_5_OCTET_LEADING_P (c))
2815 seq_maybe_bytes = 4;
2816 else if (UTF_8_6_OCTET_LEADING_P (c))
2817 seq_maybe_bytes = 5;
2818 else
2819 return 0;
2820
2821 do
2822 {
2823 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
2824 if (!UTF_8_EXTRA_OCTET_P (c))
2825 return 0;
2826 seq_maybe_bytes--;
2827 }
2828 while (seq_maybe_bytes > 0);
2829 }
2830
2831 label_end_of_loop:
2832 return CODING_CATEGORY_MASK_UTF_8;
2833 }
2834
2835 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
2836 Check if a text is encoded in UTF-16 Big Endian (endian == 1) or
2837 Little Endian (otherwise). If it is, return
2838 CODING_CATEGORY_MASK_UTF_16_BE or CODING_CATEGORY_MASK_UTF_16_LE,
2839 else return 0. */
2840
2841 #define UTF_16_INVALID_P(val) \
2842 (((val) == 0xFFFE) \
2843 || ((val) == 0xFFFF))
2844
2845 #define UTF_16_HIGH_SURROGATE_P(val) \
2846 (((val) & 0xD800) == 0xD800)
2847
2848 #define UTF_16_LOW_SURROGATE_P(val) \
2849 (((val) & 0xDC00) == 0xDC00)
2850
2851 static int
2852 detect_coding_utf_16 (src, src_end, multibytep)
2853 unsigned char *src, *src_end;
2854 int multibytep;
2855 {
2856 unsigned char c1, c2;
2857 /* Dummy for TWO_MORE_BYTES. */
2858 struct coding_system dummy_coding;
2859 struct coding_system *coding = &dummy_coding;
2860
2861 ONE_MORE_BYTE_CHECK_MULTIBYTE (c1, multibytep);
2862 ONE_MORE_BYTE_CHECK_MULTIBYTE (c2, multibytep);
2863
2864 if ((c1 == 0xFF) && (c2 == 0xFE))
2865 return CODING_CATEGORY_MASK_UTF_16_LE;
2866 else if ((c1 == 0xFE) && (c2 == 0xFF))
2867 return CODING_CATEGORY_MASK_UTF_16_BE;
2868
2869 label_end_of_loop:
2870 return 0;
2871 }
2872
2873 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions".
2874 If SJIS_P is 1, decode SJIS text, else decode BIG5 test. */
2875
2876 static void
2877 decode_coding_sjis_big5 (coding, source, destination,
2878 src_bytes, dst_bytes, sjis_p)
2879 struct coding_system *coding;
2880 unsigned char *source, *destination;
2881 int src_bytes, dst_bytes;
2882 int sjis_p;
2883 {
2884 unsigned char *src = source;
2885 unsigned char *src_end = source + src_bytes;
2886 unsigned char *dst = destination;
2887 unsigned char *dst_end = destination + dst_bytes;
2888 /* SRC_BASE remembers the start position in source in each loop.
2889 The loop will be exited when there's not enough source code
2890 (within macro ONE_MORE_BYTE), or when there's not enough
2891 destination area to produce a character (within macro
2892 EMIT_CHAR). */
2893 unsigned char *src_base;
2894 Lisp_Object translation_table;
2895
2896 if (NILP (Venable_character_translation))
2897 translation_table = Qnil;
2898 else
2899 {
2900 translation_table = coding->translation_table_for_decode;
2901 if (NILP (translation_table))
2902 translation_table = Vstandard_translation_table_for_decode;
2903 }
2904
2905 coding->produced_char = 0;
2906 while (1)
2907 {
2908 int c, charset, c1, c2;
2909
2910 src_base = src;
2911 ONE_MORE_BYTE (c1);
2912
2913 if (c1 < 0x80)
2914 {
2915 charset = CHARSET_ASCII;
2916 if (c1 < 0x20)
2917 {
2918 if (c1 == '\r')
2919 {
2920 if (coding->eol_type == CODING_EOL_CRLF)
2921 {
2922 ONE_MORE_BYTE (c2);
2923 if (c2 == '\n')
2924 c1 = c2;
2925 else if (coding->mode
2926 & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
2927 {
2928 coding->result = CODING_FINISH_INCONSISTENT_EOL;
2929 goto label_end_of_loop;
2930 }
2931 else
2932 /* To process C2 again, SRC is subtracted by 1. */
2933 src--;
2934 }
2935 else if (coding->eol_type == CODING_EOL_CR)
2936 c1 = '\n';
2937 }
2938 else if (c1 == '\n'
2939 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
2940 && (coding->eol_type == CODING_EOL_CR
2941 || coding->eol_type == CODING_EOL_CRLF))
2942 {
2943 coding->result = CODING_FINISH_INCONSISTENT_EOL;
2944 goto label_end_of_loop;
2945 }
2946 }
2947 }
2948 else
2949 {
2950 if (sjis_p)
2951 {
2952 if (c1 == 0x80 || c1 == 0xA0 || c1 > 0xEF)
2953 goto label_invalid_code;
2954 if (c1 <= 0x9F || c1 >= 0xE0)
2955 {
2956 /* SJIS -> JISX0208 */
2957 ONE_MORE_BYTE (c2);
2958 if (c2 < 0x40 || c2 == 0x7F || c2 > 0xFC)
2959 goto label_invalid_code;
2960 DECODE_SJIS (c1, c2, c1, c2);
2961 charset = charset_jisx0208;
2962 }
2963 else
2964 /* SJIS -> JISX0201-Kana */
2965 charset = charset_katakana_jisx0201;
2966 }
2967 else
2968 {
2969 /* BIG5 -> Big5 */
2970 if (c1 < 0xA0 || c1 > 0xFE)
2971 goto label_invalid_code;
2972 ONE_MORE_BYTE (c2);
2973 if (c2 < 0x40 || (c2 > 0x7E && c2 < 0xA1) || c2 > 0xFE)
2974 goto label_invalid_code;
2975 DECODE_BIG5 (c1, c2, charset, c1, c2);
2976 }
2977 }
2978
2979 c = DECODE_ISO_CHARACTER (charset, c1, c2);
2980 EMIT_CHAR (c);
2981 continue;
2982
2983 label_invalid_code:
2984 coding->errors++;
2985 src = src_base;
2986 c = *src++;
2987 EMIT_CHAR (c);
2988 }
2989
2990 label_end_of_loop:
2991 coding->consumed = coding->consumed_char = src_base - source;
2992 coding->produced = dst - destination;
2993 return;
2994 }
2995
2996 /* See the above "GENERAL NOTES on `encode_coding_XXX ()' functions".
2997 This function can encode charsets `ascii', `katakana-jisx0201',
2998 `japanese-jisx0208', `chinese-big5-1', and `chinese-big5-2'. We
2999 are sure that all these charsets are registered as official charset
3000 (i.e. do not have extended leading-codes). Characters of other
3001 charsets are produced without any encoding. If SJIS_P is 1, encode
3002 SJIS text, else encode BIG5 text. */
3003
3004 static void
3005 encode_coding_sjis_big5 (coding, source, destination,
3006 src_bytes, dst_bytes, sjis_p)
3007 struct coding_system *coding;
3008 unsigned char *source, *destination;
3009 int src_bytes, dst_bytes;
3010 int sjis_p;
3011 {
3012 unsigned char *src = source;
3013 unsigned char *src_end = source + src_bytes;
3014 unsigned char *dst = destination;
3015 unsigned char *dst_end = destination + dst_bytes;
3016 /* SRC_BASE remembers the start position in source in each loop.
3017 The loop will be exited when there's not enough source text to
3018 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
3019 there's not enough destination area to produce encoded codes
3020 (within macro EMIT_BYTES). */
3021 unsigned char *src_base;
3022 Lisp_Object translation_table;
3023
3024 if (NILP (Venable_character_translation))
3025 translation_table = Qnil;
3026 else
3027 {
3028 translation_table = coding->translation_table_for_encode;
3029 if (NILP (translation_table))
3030 translation_table = Vstandard_translation_table_for_encode;
3031 }
3032
3033 while (1)
3034 {
3035 int c, charset, c1, c2;
3036
3037 src_base = src;
3038 ONE_MORE_CHAR (c);
3039
3040 /* Now encode the character C. */
3041 if (SINGLE_BYTE_CHAR_P (c))
3042 {
3043 switch (c)
3044 {
3045 case '\r':
3046 if (!(coding->mode & CODING_MODE_SELECTIVE_DISPLAY))
3047 {
3048 EMIT_ONE_BYTE (c);
3049 break;
3050 }
3051 c = '\n';
3052 case '\n':
3053 if (coding->eol_type == CODING_EOL_CRLF)
3054 {
3055 EMIT_TWO_BYTES ('\r', c);
3056 break;
3057 }
3058 else if (coding->eol_type == CODING_EOL_CR)
3059 c = '\r';
3060 default:
3061 EMIT_ONE_BYTE (c);
3062 }
3063 }
3064 else
3065 {
3066 SPLIT_CHAR (c, charset, c1, c2);
3067 if (sjis_p)
3068 {
3069 if (charset == charset_jisx0208
3070 || charset == charset_jisx0208_1978)
3071 {
3072 ENCODE_SJIS (c1, c2, c1, c2);
3073 EMIT_TWO_BYTES (c1, c2);
3074 }
3075 else if (charset == charset_katakana_jisx0201)
3076 EMIT_ONE_BYTE (c1 | 0x80);
3077 else if (charset == charset_latin_jisx0201)
3078 EMIT_ONE_BYTE (c1);
3079 else
3080 /* There's no way other than producing the internal
3081 codes as is. */
3082 EMIT_BYTES (src_base, src);
3083 }
3084 else
3085 {
3086 if (charset == charset_big5_1 || charset == charset_big5_2)
3087 {
3088 ENCODE_BIG5 (charset, c1, c2, c1, c2);
3089 EMIT_TWO_BYTES (c1, c2);
3090 }
3091 else
3092 /* There's no way other than producing the internal
3093 codes as is. */
3094 EMIT_BYTES (src_base, src);
3095 }
3096 }
3097 coding->consumed_char++;
3098 }
3099
3100 label_end_of_loop:
3101 coding->consumed = src_base - source;
3102 coding->produced = coding->produced_char = dst - destination;
3103 }
3104
3105 \f
3106 /*** 5. CCL handlers ***/
3107
3108 /* See the above "GENERAL NOTES on `detect_coding_XXX ()' functions".
3109 Check if a text is encoded in a coding system of which
3110 encoder/decoder are written in CCL program. If it is, return
3111 CODING_CATEGORY_MASK_CCL, else return 0. */
3112
3113 static int
3114 detect_coding_ccl (src, src_end, multibytep)
3115 unsigned char *src, *src_end;
3116 int multibytep;
3117 {
3118 unsigned char *valid;
3119 int c;
3120 /* Dummy for ONE_MORE_BYTE. */
3121 struct coding_system dummy_coding;
3122 struct coding_system *coding = &dummy_coding;
3123
3124 /* No coding system is assigned to coding-category-ccl. */
3125 if (!coding_system_table[CODING_CATEGORY_IDX_CCL])
3126 return 0;
3127
3128 valid = coding_system_table[CODING_CATEGORY_IDX_CCL]->spec.ccl.valid_codes;
3129 while (1)
3130 {
3131 ONE_MORE_BYTE_CHECK_MULTIBYTE (c, multibytep);
3132 if (! valid[c])
3133 return 0;
3134 }
3135 label_end_of_loop:
3136 return CODING_CATEGORY_MASK_CCL;
3137 }
3138
3139 \f
3140 /*** 6. End-of-line handlers ***/
3141
3142 /* See the above "GENERAL NOTES on `decode_coding_XXX ()' functions". */
3143
3144 static void
3145 decode_eol (coding, source, destination, src_bytes, dst_bytes)
3146 struct coding_system *coding;
3147 unsigned char *source, *destination;
3148 int src_bytes, dst_bytes;
3149 {
3150 unsigned char *src = source;
3151 unsigned char *dst = destination;
3152 unsigned char *src_end = src + src_bytes;
3153 unsigned char *dst_end = dst + dst_bytes;
3154 Lisp_Object translation_table;
3155 /* SRC_BASE remembers the start position in source in each loop.
3156 The loop will be exited when there's not enough source code
3157 (within macro ONE_MORE_BYTE), or when there's not enough
3158 destination area to produce a character (within macro
3159 EMIT_CHAR). */
3160 unsigned char *src_base;
3161 int c;
3162
3163 translation_table = Qnil;
3164 switch (coding->eol_type)
3165 {
3166 case CODING_EOL_CRLF:
3167 while (1)
3168 {
3169 src_base = src;
3170 ONE_MORE_BYTE (c);
3171 if (c == '\r')
3172 {
3173 ONE_MORE_BYTE (c);
3174 if (c != '\n')
3175 {
3176 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
3177 {
3178 coding->result = CODING_FINISH_INCONSISTENT_EOL;
3179 goto label_end_of_loop;
3180 }
3181 src--;
3182 c = '\r';
3183 }
3184 }
3185 else if (c == '\n'
3186 && (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL))
3187 {
3188 coding->result = CODING_FINISH_INCONSISTENT_EOL;
3189 goto label_end_of_loop;
3190 }
3191 EMIT_CHAR (c);
3192 }
3193 break;
3194
3195 case CODING_EOL_CR:
3196 while (1)
3197 {
3198 src_base = src;
3199 ONE_MORE_BYTE (c);
3200 if (c == '\n')
3201 {
3202 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
3203 {
3204 coding->result = CODING_FINISH_INCONSISTENT_EOL;
3205 goto label_end_of_loop;
3206 }
3207 }
3208 else if (c == '\r')
3209 c = '\n';
3210 EMIT_CHAR (c);
3211 }
3212 break;
3213
3214 default: /* no need for EOL handling */
3215 while (1)
3216 {
3217 src_base = src;
3218 ONE_MORE_BYTE (c);
3219 EMIT_CHAR (c);
3220 }
3221 }
3222
3223 label_end_of_loop:
3224 coding->consumed = coding->consumed_char = src_base - source;
3225 coding->produced = dst - destination;
3226 return;
3227 }
3228
3229 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". Encode
3230 format of end-of-line according to `coding->eol_type'. It also
3231 convert multibyte form 8-bit characters to unibyte if
3232 CODING->src_multibyte is nonzero. If `coding->mode &
3233 CODING_MODE_SELECTIVE_DISPLAY' is nonzero, code '\r' in source text
3234 also means end-of-line. */
3235
3236 static void
3237 encode_eol (coding, source, destination, src_bytes, dst_bytes)
3238 struct coding_system *coding;
3239 const unsigned char *source;
3240 unsigned char *destination;
3241 int src_bytes, dst_bytes;
3242 {
3243 const unsigned char *src = source;
3244 unsigned char *dst = destination;
3245 const unsigned char *src_end = src + src_bytes;
3246 unsigned char *dst_end = dst + dst_bytes;
3247 Lisp_Object translation_table;
3248 /* SRC_BASE remembers the start position in source in each loop.
3249 The loop will be exited when there's not enough source text to
3250 analyze multi-byte codes (within macro ONE_MORE_CHAR), or when
3251 there's not enough destination area to produce encoded codes
3252 (within macro EMIT_BYTES). */
3253 const unsigned char *src_base;
3254 unsigned char *tmp;
3255 int c;
3256 int selective_display = coding->mode & CODING_MODE_SELECTIVE_DISPLAY;
3257
3258 translation_table = Qnil;
3259 if (coding->src_multibyte
3260 && *(src_end - 1) == LEADING_CODE_8_BIT_CONTROL)
3261 {
3262 src_end--;
3263 src_bytes--;
3264 coding->result = CODING_FINISH_INSUFFICIENT_SRC;
3265 }
3266
3267 if (coding->eol_type == CODING_EOL_CRLF)
3268 {
3269 while (src < src_end)
3270 {
3271 src_base = src;
3272 c = *src++;
3273 if (c >= 0x20)
3274 EMIT_ONE_BYTE (c);
3275 else if (c == '\n' || (c == '\r' && selective_display))
3276 EMIT_TWO_BYTES ('\r', '\n');
3277 else
3278 EMIT_ONE_BYTE (c);
3279 }
3280 src_base = src;
3281 label_end_of_loop:
3282 ;
3283 }
3284 else
3285 {
3286 if (!dst_bytes || src_bytes <= dst_bytes)
3287 {
3288 safe_bcopy (src, dst, src_bytes);
3289 src_base = src_end;
3290 dst += src_bytes;
3291 }
3292 else
3293 {
3294 if (coding->src_multibyte
3295 && *(src + dst_bytes - 1) == LEADING_CODE_8_BIT_CONTROL)
3296 dst_bytes--;
3297 safe_bcopy (src, dst, dst_bytes);
3298 src_base = src + dst_bytes;
3299 dst = destination + dst_bytes;
3300 coding->result = CODING_FINISH_INSUFFICIENT_DST;
3301 }
3302 if (coding->eol_type == CODING_EOL_CR)
3303 {
3304 for (tmp = destination; tmp < dst; tmp++)
3305 if (*tmp == '\n') *tmp = '\r';
3306 }
3307 else if (selective_display)
3308 {
3309 for (tmp = destination; tmp < dst; tmp++)
3310 if (*tmp == '\r') *tmp = '\n';
3311 }
3312 }
3313 if (coding->src_multibyte)
3314 dst = destination + str_as_unibyte (destination, dst - destination);
3315
3316 coding->consumed = src_base - source;
3317 coding->produced = dst - destination;
3318 coding->produced_char = coding->produced;
3319 }
3320
3321 \f
3322 /*** 7. C library functions ***/
3323
3324 /* In Emacs Lisp, a coding system is represented by a Lisp symbol which
3325 has a property `coding-system'. The value of this property is a
3326 vector of length 5 (called the coding-vector). Among elements of
3327 this vector, the first (element[0]) and the fifth (element[4])
3328 carry important information for decoding/encoding. Before
3329 decoding/encoding, this information should be set in fields of a
3330 structure of type `coding_system'.
3331
3332 The value of the property `coding-system' can be a symbol of another
3333 subsidiary coding-system. In that case, Emacs gets coding-vector
3334 from that symbol.
3335
3336 `element[0]' contains information to be set in `coding->type'. The
3337 value and its meaning is as follows:
3338
3339 0 -- coding_type_emacs_mule
3340 1 -- coding_type_sjis
3341 2 -- coding_type_iso2022
3342 3 -- coding_type_big5
3343 4 -- coding_type_ccl encoder/decoder written in CCL
3344 nil -- coding_type_no_conversion
3345 t -- coding_type_undecided (automatic conversion on decoding,
3346 no-conversion on encoding)
3347
3348 `element[4]' contains information to be set in `coding->flags' and
3349 `coding->spec'. The meaning varies by `coding->type'.
3350
3351 If `coding->type' is `coding_type_iso2022', element[4] is a vector
3352 of length 32 (of which the first 13 sub-elements are used now).
3353 Meanings of these sub-elements are:
3354
3355 sub-element[N] where N is 0 through 3: to be set in `coding->spec.iso2022'
3356 If the value is an integer of valid charset, the charset is
3357 assumed to be designated to graphic register N initially.
3358
3359 If the value is minus, it is a minus value of charset which
3360 reserves graphic register N, which means that the charset is
3361 not designated initially but should be designated to graphic
3362 register N just before encoding a character in that charset.
3363
3364 If the value is nil, graphic register N is never used on
3365 encoding.
3366
3367 sub-element[N] where N is 4 through 11: to be set in `coding->flags'
3368 Each value takes t or nil. See the section ISO2022 of
3369 `coding.h' for more information.
3370
3371 If `coding->type' is `coding_type_big5', element[4] is t to denote
3372 BIG5-ETen or nil to denote BIG5-HKU.
3373
3374 If `coding->type' takes the other value, element[4] is ignored.
3375
3376 Emacs Lisp's coding systems also carry information about format of
3377 end-of-line in a value of property `eol-type'. If the value is
3378 integer, 0 means CODING_EOL_LF, 1 means CODING_EOL_CRLF, and 2
3379 means CODING_EOL_CR. If it is not integer, it should be a vector
3380 of subsidiary coding systems of which property `eol-type' has one
3381 of the above values.
3382
3383 */
3384
3385 /* Extract information for decoding/encoding from CODING_SYSTEM_SYMBOL
3386 and set it in CODING. If CODING_SYSTEM_SYMBOL is invalid, CODING
3387 is setup so that no conversion is necessary and return -1, else
3388 return 0. */
3389
3390 int
3391 setup_coding_system (coding_system, coding)
3392 Lisp_Object coding_system;
3393 struct coding_system *coding;
3394 {
3395 Lisp_Object coding_spec, coding_type, eol_type, plist;
3396 Lisp_Object val;
3397
3398 /* At first, zero clear all members. */
3399 bzero (coding, sizeof (struct coding_system));
3400
3401 /* Initialize some fields required for all kinds of coding systems. */
3402 coding->symbol = coding_system;
3403 coding->heading_ascii = -1;
3404 coding->post_read_conversion = coding->pre_write_conversion = Qnil;
3405 coding->composing = COMPOSITION_DISABLED;
3406 coding->cmp_data = NULL;
3407
3408 if (NILP (coding_system))
3409 goto label_invalid_coding_system;
3410
3411 coding_spec = Fget (coding_system, Qcoding_system);
3412
3413 if (!VECTORP (coding_spec)
3414 || XVECTOR (coding_spec)->size != 5
3415 || !CONSP (XVECTOR (coding_spec)->contents[3]))
3416 goto label_invalid_coding_system;
3417
3418 eol_type = inhibit_eol_conversion ? Qnil : Fget (coding_system, Qeol_type);
3419 if (VECTORP (eol_type))
3420 {
3421 coding->eol_type = CODING_EOL_UNDECIDED;
3422 coding->common_flags = CODING_REQUIRE_DETECTION_MASK;
3423 }
3424 else if (XFASTINT (eol_type) == 1)
3425 {
3426 coding->eol_type = CODING_EOL_CRLF;
3427 coding->common_flags
3428 = CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3429 }
3430 else if (XFASTINT (eol_type) == 2)
3431 {
3432 coding->eol_type = CODING_EOL_CR;
3433 coding->common_flags
3434 = CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3435 }
3436 else
3437 coding->eol_type = CODING_EOL_LF;
3438
3439 coding_type = XVECTOR (coding_spec)->contents[0];
3440 /* Try short cut. */
3441 if (SYMBOLP (coding_type))
3442 {
3443 if (EQ (coding_type, Qt))
3444 {
3445 coding->type = coding_type_undecided;
3446 coding->common_flags |= CODING_REQUIRE_DETECTION_MASK;
3447 }
3448 else
3449 coding->type = coding_type_no_conversion;
3450 /* Initialize this member. Any thing other than
3451 CODING_CATEGORY_IDX_UTF_16_BE and
3452 CODING_CATEGORY_IDX_UTF_16_LE are ok because they have
3453 special treatment in detect_eol. */
3454 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE;
3455
3456 return 0;
3457 }
3458
3459 /* Get values of coding system properties:
3460 `post-read-conversion', `pre-write-conversion',
3461 `translation-table-for-decode', `translation-table-for-encode'. */
3462 plist = XVECTOR (coding_spec)->contents[3];
3463 /* Pre & post conversion functions should be disabled if
3464 inhibit_eol_conversion is nonzero. This is the case that a code
3465 conversion function is called while those functions are running. */
3466 if (! inhibit_pre_post_conversion)
3467 {
3468 coding->post_read_conversion = Fplist_get (plist, Qpost_read_conversion);
3469 coding->pre_write_conversion = Fplist_get (plist, Qpre_write_conversion);
3470 }
3471 val = Fplist_get (plist, Qtranslation_table_for_decode);
3472 if (SYMBOLP (val))
3473 val = Fget (val, Qtranslation_table_for_decode);
3474 coding->translation_table_for_decode = CHAR_TABLE_P (val) ? val : Qnil;
3475 val = Fplist_get (plist, Qtranslation_table_for_encode);
3476 if (SYMBOLP (val))
3477 val = Fget (val, Qtranslation_table_for_encode);
3478 coding->translation_table_for_encode = CHAR_TABLE_P (val) ? val : Qnil;
3479 val = Fplist_get (plist, Qcoding_category);
3480 if (!NILP (val))
3481 {
3482 val = Fget (val, Qcoding_category_index);
3483 if (INTEGERP (val))
3484 coding->category_idx = XINT (val);
3485 else
3486 goto label_invalid_coding_system;
3487 }
3488 else
3489 goto label_invalid_coding_system;
3490
3491 /* If the coding system has non-nil `composition' property, enable
3492 composition handling. */
3493 val = Fplist_get (plist, Qcomposition);
3494 if (!NILP (val))
3495 coding->composing = COMPOSITION_NO;
3496
3497 switch (XFASTINT (coding_type))
3498 {
3499 case 0:
3500 coding->type = coding_type_emacs_mule;
3501 coding->common_flags
3502 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3503 coding->composing = COMPOSITION_NO;
3504 if (!NILP (coding->post_read_conversion))
3505 coding->common_flags |= CODING_REQUIRE_DECODING_MASK;
3506 if (!NILP (coding->pre_write_conversion))
3507 coding->common_flags |= CODING_REQUIRE_ENCODING_MASK;
3508 break;
3509
3510 case 1:
3511 coding->type = coding_type_sjis;
3512 coding->common_flags
3513 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3514 break;
3515
3516 case 2:
3517 coding->type = coding_type_iso2022;
3518 coding->common_flags
3519 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3520 {
3521 Lisp_Object val, temp;
3522 Lisp_Object *flags;
3523 int i, charset, reg_bits = 0;
3524
3525 val = XVECTOR (coding_spec)->contents[4];
3526
3527 if (!VECTORP (val) || XVECTOR (val)->size != 32)
3528 goto label_invalid_coding_system;
3529
3530 flags = XVECTOR (val)->contents;
3531 coding->flags
3532 = ((NILP (flags[4]) ? 0 : CODING_FLAG_ISO_SHORT_FORM)
3533 | (NILP (flags[5]) ? 0 : CODING_FLAG_ISO_RESET_AT_EOL)
3534 | (NILP (flags[6]) ? 0 : CODING_FLAG_ISO_RESET_AT_CNTL)
3535 | (NILP (flags[7]) ? 0 : CODING_FLAG_ISO_SEVEN_BITS)
3536 | (NILP (flags[8]) ? 0 : CODING_FLAG_ISO_LOCKING_SHIFT)
3537 | (NILP (flags[9]) ? 0 : CODING_FLAG_ISO_SINGLE_SHIFT)
3538 | (NILP (flags[10]) ? 0 : CODING_FLAG_ISO_USE_ROMAN)
3539 | (NILP (flags[11]) ? 0 : CODING_FLAG_ISO_USE_OLDJIS)
3540 | (NILP (flags[12]) ? 0 : CODING_FLAG_ISO_NO_DIRECTION)
3541 | (NILP (flags[13]) ? 0 : CODING_FLAG_ISO_INIT_AT_BOL)
3542 | (NILP (flags[14]) ? 0 : CODING_FLAG_ISO_DESIGNATE_AT_BOL)
3543 | (NILP (flags[15]) ? 0 : CODING_FLAG_ISO_SAFE)
3544 | (NILP (flags[16]) ? 0 : CODING_FLAG_ISO_LATIN_EXTRA)
3545 );
3546
3547 /* Invoke graphic register 0 to plane 0. */
3548 CODING_SPEC_ISO_INVOCATION (coding, 0) = 0;
3549 /* Invoke graphic register 1 to plane 1 if we can use full 8-bit. */
3550 CODING_SPEC_ISO_INVOCATION (coding, 1)
3551 = (coding->flags & CODING_FLAG_ISO_SEVEN_BITS ? -1 : 1);
3552 /* Not single shifting at first. */
3553 CODING_SPEC_ISO_SINGLE_SHIFTING (coding) = 0;
3554 /* Beginning of buffer should also be regarded as bol. */
3555 CODING_SPEC_ISO_BOL (coding) = 1;
3556
3557 for (charset = 0; charset <= MAX_CHARSET; charset++)
3558 CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = 255;
3559 val = Vcharset_revision_alist;
3560 while (CONSP (val))
3561 {
3562 charset = get_charset_id (Fcar_safe (XCAR (val)));
3563 if (charset >= 0
3564 && (temp = Fcdr_safe (XCAR (val)), INTEGERP (temp))
3565 && (i = XINT (temp), (i >= 0 && (i + '@') < 128)))
3566 CODING_SPEC_ISO_REVISION_NUMBER (coding, charset) = i;
3567 val = XCDR (val);
3568 }
3569
3570 /* Checks FLAGS[REG] (REG = 0, 1, 2 3) and decide designations.
3571 FLAGS[REG] can be one of below:
3572 integer CHARSET: CHARSET occupies register I,
3573 t: designate nothing to REG initially, but can be used
3574 by any charsets,
3575 list of integer, nil, or t: designate the first
3576 element (if integer) to REG initially, the remaining
3577 elements (if integer) is designated to REG on request,
3578 if an element is t, REG can be used by any charsets,
3579 nil: REG is never used. */
3580 for (charset = 0; charset <= MAX_CHARSET; charset++)
3581 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3582 = CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION;
3583 for (i = 0; i < 4; i++)
3584 {
3585 if ((INTEGERP (flags[i])
3586 && (charset = XINT (flags[i]), CHARSET_VALID_P (charset)))
3587 || (charset = get_charset_id (flags[i])) >= 0)
3588 {
3589 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset;
3590 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) = i;
3591 }
3592 else if (EQ (flags[i], Qt))
3593 {
3594 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
3595 reg_bits |= 1 << i;
3596 coding->flags |= CODING_FLAG_ISO_DESIGNATION;
3597 }
3598 else if (CONSP (flags[i]))
3599 {
3600 Lisp_Object tail;
3601 tail = flags[i];
3602
3603 coding->flags |= CODING_FLAG_ISO_DESIGNATION;
3604 if ((INTEGERP (XCAR (tail))
3605 && (charset = XINT (XCAR (tail)),
3606 CHARSET_VALID_P (charset)))
3607 || (charset = get_charset_id (XCAR (tail))) >= 0)
3608 {
3609 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = charset;
3610 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset) =i;
3611 }
3612 else
3613 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
3614 tail = XCDR (tail);
3615 while (CONSP (tail))
3616 {
3617 if ((INTEGERP (XCAR (tail))
3618 && (charset = XINT (XCAR (tail)),
3619 CHARSET_VALID_P (charset)))
3620 || (charset = get_charset_id (XCAR (tail))) >= 0)
3621 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3622 = i;
3623 else if (EQ (XCAR (tail), Qt))
3624 reg_bits |= 1 << i;
3625 tail = XCDR (tail);
3626 }
3627 }
3628 else
3629 CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i) = -1;
3630
3631 CODING_SPEC_ISO_DESIGNATION (coding, i)
3632 = CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, i);
3633 }
3634
3635 if (reg_bits && ! (coding->flags & CODING_FLAG_ISO_LOCKING_SHIFT))
3636 {
3637 /* REG 1 can be used only by locking shift in 7-bit env. */
3638 if (coding->flags & CODING_FLAG_ISO_SEVEN_BITS)
3639 reg_bits &= ~2;
3640 if (! (coding->flags & CODING_FLAG_ISO_SINGLE_SHIFT))
3641 /* Without any shifting, only REG 0 and 1 can be used. */
3642 reg_bits &= 3;
3643 }
3644
3645 if (reg_bits)
3646 for (charset = 0; charset <= MAX_CHARSET; charset++)
3647 {
3648 if (CHARSET_DEFINED_P (charset)
3649 && (CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3650 == CODING_SPEC_ISO_NO_REQUESTED_DESIGNATION))
3651 {
3652 /* There exist some default graphic registers to be
3653 used by CHARSET. */
3654
3655 /* We had better avoid designating a charset of
3656 CHARS96 to REG 0 as far as possible. */
3657 if (CHARSET_CHARS (charset) == 96)
3658 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3659 = (reg_bits & 2
3660 ? 1 : (reg_bits & 4 ? 2 : (reg_bits & 8 ? 3 : 0)));
3661 else
3662 CODING_SPEC_ISO_REQUESTED_DESIGNATION (coding, charset)
3663 = (reg_bits & 1
3664 ? 0 : (reg_bits & 2 ? 1 : (reg_bits & 4 ? 2 : 3)));
3665 }
3666 }
3667 }
3668 coding->common_flags |= CODING_REQUIRE_FLUSHING_MASK;
3669 coding->spec.iso2022.last_invalid_designation_register = -1;
3670 break;
3671
3672 case 3:
3673 coding->type = coding_type_big5;
3674 coding->common_flags
3675 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3676 coding->flags
3677 = (NILP (XVECTOR (coding_spec)->contents[4])
3678 ? CODING_FLAG_BIG5_HKU
3679 : CODING_FLAG_BIG5_ETEN);
3680 break;
3681
3682 case 4:
3683 coding->type = coding_type_ccl;
3684 coding->common_flags
3685 |= CODING_REQUIRE_DECODING_MASK | CODING_REQUIRE_ENCODING_MASK;
3686 {
3687 val = XVECTOR (coding_spec)->contents[4];
3688 if (! CONSP (val)
3689 || setup_ccl_program (&(coding->spec.ccl.decoder),
3690 XCAR (val)) < 0
3691 || setup_ccl_program (&(coding->spec.ccl.encoder),
3692 XCDR (val)) < 0)
3693 goto label_invalid_coding_system;
3694
3695 bzero (coding->spec.ccl.valid_codes, 256);
3696 val = Fplist_get (plist, Qvalid_codes);
3697 if (CONSP (val))
3698 {
3699 Lisp_Object this;
3700
3701 for (; CONSP (val); val = XCDR (val))
3702 {
3703 this = XCAR (val);
3704 if (INTEGERP (this)
3705 && XINT (this) >= 0 && XINT (this) < 256)
3706 coding->spec.ccl.valid_codes[XINT (this)] = 1;
3707 else if (CONSP (this)
3708 && INTEGERP (XCAR (this))
3709 && INTEGERP (XCDR (this)))
3710 {
3711 int start = XINT (XCAR (this));
3712 int end = XINT (XCDR (this));
3713
3714 if (start >= 0 && start <= end && end < 256)
3715 while (start <= end)
3716 coding->spec.ccl.valid_codes[start++] = 1;
3717 }
3718 }
3719 }
3720 }
3721 coding->common_flags |= CODING_REQUIRE_FLUSHING_MASK;
3722 coding->spec.ccl.cr_carryover = 0;
3723 coding->spec.ccl.eight_bit_carryover[0] = 0;
3724 break;
3725
3726 case 5:
3727 coding->type = coding_type_raw_text;
3728 break;
3729
3730 default:
3731 goto label_invalid_coding_system;
3732 }
3733 return 0;
3734
3735 label_invalid_coding_system:
3736 coding->type = coding_type_no_conversion;
3737 coding->category_idx = CODING_CATEGORY_IDX_BINARY;
3738 coding->common_flags = 0;
3739 coding->eol_type = CODING_EOL_LF;
3740 coding->pre_write_conversion = coding->post_read_conversion = Qnil;
3741 return -1;
3742 }
3743
3744 /* Free memory blocks allocated for storing composition information. */
3745
3746 void
3747 coding_free_composition_data (coding)
3748 struct coding_system *coding;
3749 {
3750 struct composition_data *cmp_data = coding->cmp_data, *next;
3751
3752 if (!cmp_data)
3753 return;
3754 /* Memory blocks are chained. At first, rewind to the first, then,
3755 free blocks one by one. */
3756 while (cmp_data->prev)
3757 cmp_data = cmp_data->prev;
3758 while (cmp_data)
3759 {
3760 next = cmp_data->next;
3761 xfree (cmp_data);
3762 cmp_data = next;
3763 }
3764 coding->cmp_data = NULL;
3765 }
3766
3767 /* Set `char_offset' member of all memory blocks pointed by
3768 coding->cmp_data to POS. */
3769
3770 void
3771 coding_adjust_composition_offset (coding, pos)
3772 struct coding_system *coding;
3773 int pos;
3774 {
3775 struct composition_data *cmp_data;
3776
3777 for (cmp_data = coding->cmp_data; cmp_data; cmp_data = cmp_data->next)
3778 cmp_data->char_offset = pos;
3779 }
3780
3781 /* Setup raw-text or one of its subsidiaries in the structure
3782 coding_system CODING according to the already setup value eol_type
3783 in CODING. CODING should be setup for some coding system in
3784 advance. */
3785
3786 void
3787 setup_raw_text_coding_system (coding)
3788 struct coding_system *coding;
3789 {
3790 if (coding->type != coding_type_raw_text)
3791 {
3792 coding->symbol = Qraw_text;
3793 coding->type = coding_type_raw_text;
3794 if (coding->eol_type != CODING_EOL_UNDECIDED)
3795 {
3796 Lisp_Object subsidiaries;
3797 subsidiaries = Fget (Qraw_text, Qeol_type);
3798
3799 if (VECTORP (subsidiaries)
3800 && XVECTOR (subsidiaries)->size == 3)
3801 coding->symbol
3802 = XVECTOR (subsidiaries)->contents[coding->eol_type];
3803 }
3804 setup_coding_system (coding->symbol, coding);
3805 }
3806 return;
3807 }
3808
3809 /* Emacs has a mechanism to automatically detect a coding system if it
3810 is one of Emacs' internal format, ISO2022, SJIS, and BIG5. But,
3811 it's impossible to distinguish some coding systems accurately
3812 because they use the same range of codes. So, at first, coding
3813 systems are categorized into 7, those are:
3814
3815 o coding-category-emacs-mule
3816
3817 The category for a coding system which has the same code range
3818 as Emacs' internal format. Assigned the coding-system (Lisp
3819 symbol) `emacs-mule' by default.
3820
3821 o coding-category-sjis
3822
3823 The category for a coding system which has the same code range
3824 as SJIS. Assigned the coding-system (Lisp
3825 symbol) `japanese-shift-jis' by default.
3826
3827 o coding-category-iso-7
3828
3829 The category for a coding system which has the same code range
3830 as ISO2022 of 7-bit environment. This doesn't use any locking
3831 shift and single shift functions. This can encode/decode all
3832 charsets. Assigned the coding-system (Lisp symbol)
3833 `iso-2022-7bit' by default.
3834
3835 o coding-category-iso-7-tight
3836
3837 Same as coding-category-iso-7 except that this can
3838 encode/decode only the specified charsets.
3839
3840 o coding-category-iso-8-1
3841
3842 The category for a coding system which has the same code range
3843 as ISO2022 of 8-bit environment and graphic plane 1 used only
3844 for DIMENSION1 charset. This doesn't use any locking shift
3845 and single shift functions. Assigned the coding-system (Lisp
3846 symbol) `iso-latin-1' by default.
3847
3848 o coding-category-iso-8-2
3849
3850 The category for a coding system which has the same code range
3851 as ISO2022 of 8-bit environment and graphic plane 1 used only
3852 for DIMENSION2 charset. This doesn't use any locking shift
3853 and single shift functions. Assigned the coding-system (Lisp
3854 symbol) `japanese-iso-8bit' by default.
3855
3856 o coding-category-iso-7-else
3857
3858 The category for a coding system which has the same code range
3859 as ISO2022 of 7-bit environment but uses locking shift or
3860 single shift functions. Assigned the coding-system (Lisp
3861 symbol) `iso-2022-7bit-lock' by default.
3862
3863 o coding-category-iso-8-else
3864
3865 The category for a coding system which has the same code range
3866 as ISO2022 of 8-bit environment but uses locking shift or
3867 single shift functions. Assigned the coding-system (Lisp
3868 symbol) `iso-2022-8bit-ss2' by default.
3869
3870 o coding-category-big5
3871
3872 The category for a coding system which has the same code range
3873 as BIG5. Assigned the coding-system (Lisp symbol)
3874 `cn-big5' by default.
3875
3876 o coding-category-utf-8
3877
3878 The category for a coding system which has the same code range
3879 as UTF-8 (cf. RFC2279). Assigned the coding-system (Lisp
3880 symbol) `utf-8' by default.
3881
3882 o coding-category-utf-16-be
3883
3884 The category for a coding system in which a text has an
3885 Unicode signature (cf. Unicode Standard) in the order of BIG
3886 endian at the head. Assigned the coding-system (Lisp symbol)
3887 `utf-16-be' by default.
3888
3889 o coding-category-utf-16-le
3890
3891 The category for a coding system in which a text has an
3892 Unicode signature (cf. Unicode Standard) in the order of
3893 LITTLE endian at the head. Assigned the coding-system (Lisp
3894 symbol) `utf-16-le' by default.
3895
3896 o coding-category-ccl
3897
3898 The category for a coding system of which encoder/decoder is
3899 written in CCL programs. The default value is nil, i.e., no
3900 coding system is assigned.
3901
3902 o coding-category-binary
3903
3904 The category for a coding system not categorized in any of the
3905 above. Assigned the coding-system (Lisp symbol)
3906 `no-conversion' by default.
3907
3908 Each of them is a Lisp symbol and the value is an actual
3909 `coding-system' (this is also a Lisp symbol) assigned by a user.
3910 What Emacs does actually is to detect a category of coding system.
3911 Then, it uses a `coding-system' assigned to it. If Emacs can't
3912 decide a single possible category, it selects a category of the
3913 highest priority. Priorities of categories are also specified by a
3914 user in a Lisp variable `coding-category-list'.
3915
3916 */
3917
3918 static
3919 int ascii_skip_code[256];
3920
3921 /* Detect how a text of length SRC_BYTES pointed by SOURCE is encoded.
3922 If it detects possible coding systems, return an integer in which
3923 appropriate flag bits are set. Flag bits are defined by macros
3924 CODING_CATEGORY_MASK_XXX in `coding.h'. If PRIORITIES is non-NULL,
3925 it should point the table `coding_priorities'. In that case, only
3926 the flag bit for a coding system of the highest priority is set in
3927 the returned value. If MULTIBYTEP is nonzero, 8-bit codes of the
3928 range 0x80..0x9F are in multibyte form.
3929
3930 How many ASCII characters are at the head is returned as *SKIP. */
3931
3932 static int
3933 detect_coding_mask (source, src_bytes, priorities, skip, multibytep)
3934 unsigned char *source;
3935 int src_bytes, *priorities, *skip;
3936 int multibytep;
3937 {
3938 register unsigned char c;
3939 unsigned char *src = source, *src_end = source + src_bytes;
3940 unsigned int mask, utf16_examined_p, iso2022_examined_p;
3941 int i;
3942
3943 /* At first, skip all ASCII characters and control characters except
3944 for three ISO2022 specific control characters. */
3945 ascii_skip_code[ISO_CODE_SO] = 0;
3946 ascii_skip_code[ISO_CODE_SI] = 0;
3947 ascii_skip_code[ISO_CODE_ESC] = 0;
3948
3949 label_loop_detect_coding:
3950 while (src < src_end && ascii_skip_code[*src]) src++;
3951 *skip = src - source;
3952
3953 if (src >= src_end)
3954 /* We found nothing other than ASCII. There's nothing to do. */
3955 return 0;
3956
3957 c = *src;
3958 /* The text seems to be encoded in some multilingual coding system.
3959 Now, try to find in which coding system the text is encoded. */
3960 if (c < 0x80)
3961 {
3962 /* i.e. (c == ISO_CODE_ESC || c == ISO_CODE_SI || c == ISO_CODE_SO) */
3963 /* C is an ISO2022 specific control code of C0. */
3964 mask = detect_coding_iso2022 (src, src_end, multibytep);
3965 if (mask == 0)
3966 {
3967 /* No valid ISO2022 code follows C. Try again. */
3968 src++;
3969 if (c == ISO_CODE_ESC)
3970 ascii_skip_code[ISO_CODE_ESC] = 1;
3971 else
3972 ascii_skip_code[ISO_CODE_SO] = ascii_skip_code[ISO_CODE_SI] = 1;
3973 goto label_loop_detect_coding;
3974 }
3975 if (priorities)
3976 {
3977 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
3978 {
3979 if (mask & priorities[i])
3980 return priorities[i];
3981 }
3982 return CODING_CATEGORY_MASK_RAW_TEXT;
3983 }
3984 }
3985 else
3986 {
3987 int try;
3988
3989 if (multibytep && c == LEADING_CODE_8_BIT_CONTROL)
3990 c = src[1] - 0x20;
3991
3992 if (c < 0xA0)
3993 {
3994 /* C is the first byte of SJIS character code,
3995 or a leading-code of Emacs' internal format (emacs-mule),
3996 or the first byte of UTF-16. */
3997 try = (CODING_CATEGORY_MASK_SJIS
3998 | CODING_CATEGORY_MASK_EMACS_MULE
3999 | CODING_CATEGORY_MASK_UTF_16_BE
4000 | CODING_CATEGORY_MASK_UTF_16_LE);
4001
4002 /* Or, if C is a special latin extra code,
4003 or is an ISO2022 specific control code of C1 (SS2 or SS3),
4004 or is an ISO2022 control-sequence-introducer (CSI),
4005 we should also consider the possibility of ISO2022 codings. */
4006 if ((VECTORP (Vlatin_extra_code_table)
4007 && !NILP (XVECTOR (Vlatin_extra_code_table)->contents[c]))
4008 || (c == ISO_CODE_SS2 || c == ISO_CODE_SS3)
4009 || (c == ISO_CODE_CSI
4010 && (src < src_end
4011 && (*src == ']'
4012 || ((*src == '0' || *src == '1' || *src == '2')
4013 && src + 1 < src_end
4014 && src[1] == ']')))))
4015 try |= (CODING_CATEGORY_MASK_ISO_8_ELSE
4016 | CODING_CATEGORY_MASK_ISO_8BIT);
4017 }
4018 else
4019 /* C is a character of ISO2022 in graphic plane right,
4020 or a SJIS's 1-byte character code (i.e. JISX0201),
4021 or the first byte of BIG5's 2-byte code,
4022 or the first byte of UTF-8/16. */
4023 try = (CODING_CATEGORY_MASK_ISO_8_ELSE
4024 | CODING_CATEGORY_MASK_ISO_8BIT
4025 | CODING_CATEGORY_MASK_SJIS
4026 | CODING_CATEGORY_MASK_BIG5
4027 | CODING_CATEGORY_MASK_UTF_8
4028 | CODING_CATEGORY_MASK_UTF_16_BE
4029 | CODING_CATEGORY_MASK_UTF_16_LE);
4030
4031 /* Or, we may have to consider the possibility of CCL. */
4032 if (coding_system_table[CODING_CATEGORY_IDX_CCL]
4033 && (coding_system_table[CODING_CATEGORY_IDX_CCL]
4034 ->spec.ccl.valid_codes)[c])
4035 try |= CODING_CATEGORY_MASK_CCL;
4036
4037 mask = 0;
4038 utf16_examined_p = iso2022_examined_p = 0;
4039 if (priorities)
4040 {
4041 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
4042 {
4043 if (!iso2022_examined_p
4044 && (priorities[i] & try & CODING_CATEGORY_MASK_ISO))
4045 {
4046 mask |= detect_coding_iso2022 (src, src_end, multibytep);
4047 iso2022_examined_p = 1;
4048 }
4049 else if (priorities[i] & try & CODING_CATEGORY_MASK_SJIS)
4050 mask |= detect_coding_sjis (src, src_end, multibytep);
4051 else if (priorities[i] & try & CODING_CATEGORY_MASK_UTF_8)
4052 mask |= detect_coding_utf_8 (src, src_end, multibytep);
4053 else if (!utf16_examined_p
4054 && (priorities[i] & try &
4055 CODING_CATEGORY_MASK_UTF_16_BE_LE))
4056 {
4057 mask |= detect_coding_utf_16 (src, src_end, multibytep);
4058 utf16_examined_p = 1;
4059 }
4060 else if (priorities[i] & try & CODING_CATEGORY_MASK_BIG5)
4061 mask |= detect_coding_big5 (src, src_end, multibytep);
4062 else if (priorities[i] & try & CODING_CATEGORY_MASK_EMACS_MULE)
4063 mask |= detect_coding_emacs_mule (src, src_end, multibytep);
4064 else if (priorities[i] & try & CODING_CATEGORY_MASK_CCL)
4065 mask |= detect_coding_ccl (src, src_end, multibytep);
4066 else if (priorities[i] & CODING_CATEGORY_MASK_RAW_TEXT)
4067 mask |= CODING_CATEGORY_MASK_RAW_TEXT;
4068 else if (priorities[i] & CODING_CATEGORY_MASK_BINARY)
4069 mask |= CODING_CATEGORY_MASK_BINARY;
4070 if (mask & priorities[i])
4071 return priorities[i];
4072 }
4073 return CODING_CATEGORY_MASK_RAW_TEXT;
4074 }
4075 if (try & CODING_CATEGORY_MASK_ISO)
4076 mask |= detect_coding_iso2022 (src, src_end, multibytep);
4077 if (try & CODING_CATEGORY_MASK_SJIS)
4078 mask |= detect_coding_sjis (src, src_end, multibytep);
4079 if (try & CODING_CATEGORY_MASK_BIG5)
4080 mask |= detect_coding_big5 (src, src_end, multibytep);
4081 if (try & CODING_CATEGORY_MASK_UTF_8)
4082 mask |= detect_coding_utf_8 (src, src_end, multibytep);
4083 if (try & CODING_CATEGORY_MASK_UTF_16_BE_LE)
4084 mask |= detect_coding_utf_16 (src, src_end, multibytep);
4085 if (try & CODING_CATEGORY_MASK_EMACS_MULE)
4086 mask |= detect_coding_emacs_mule (src, src_end, multibytep);
4087 if (try & CODING_CATEGORY_MASK_CCL)
4088 mask |= detect_coding_ccl (src, src_end, multibytep);
4089 }
4090 return (mask | CODING_CATEGORY_MASK_RAW_TEXT | CODING_CATEGORY_MASK_BINARY);
4091 }
4092
4093 /* Detect how a text of length SRC_BYTES pointed by SRC is encoded.
4094 The information of the detected coding system is set in CODING. */
4095
4096 void
4097 detect_coding (coding, src, src_bytes)
4098 struct coding_system *coding;
4099 const unsigned char *src;
4100 int src_bytes;
4101 {
4102 unsigned int idx;
4103 int skip, mask;
4104 Lisp_Object val;
4105
4106 val = Vcoding_category_list;
4107 mask = detect_coding_mask (src, src_bytes, coding_priorities, &skip,
4108 coding->src_multibyte);
4109 coding->heading_ascii = skip;
4110
4111 if (!mask) return;
4112
4113 /* We found a single coding system of the highest priority in MASK. */
4114 idx = 0;
4115 while (mask && ! (mask & 1)) mask >>= 1, idx++;
4116 if (! mask)
4117 idx = CODING_CATEGORY_IDX_RAW_TEXT;
4118
4119 val = SYMBOL_VALUE (XVECTOR (Vcoding_category_table)->contents[idx]);
4120
4121 if (coding->eol_type != CODING_EOL_UNDECIDED)
4122 {
4123 Lisp_Object tmp;
4124
4125 tmp = Fget (val, Qeol_type);
4126 if (VECTORP (tmp))
4127 val = XVECTOR (tmp)->contents[coding->eol_type];
4128 }
4129
4130 /* Setup this new coding system while preserving some slots. */
4131 {
4132 int src_multibyte = coding->src_multibyte;
4133 int dst_multibyte = coding->dst_multibyte;
4134
4135 setup_coding_system (val, coding);
4136 coding->src_multibyte = src_multibyte;
4137 coding->dst_multibyte = dst_multibyte;
4138 coding->heading_ascii = skip;
4139 }
4140 }
4141
4142 /* Detect how end-of-line of a text of length SRC_BYTES pointed by
4143 SOURCE is encoded. Return one of CODING_EOL_LF, CODING_EOL_CRLF,
4144 CODING_EOL_CR, and CODING_EOL_UNDECIDED.
4145
4146 How many non-eol characters are at the head is returned as *SKIP. */
4147
4148 #define MAX_EOL_CHECK_COUNT 3
4149
4150 static int
4151 detect_eol_type (source, src_bytes, skip)
4152 unsigned char *source;
4153 int src_bytes, *skip;
4154 {
4155 unsigned char *src = source, *src_end = src + src_bytes;
4156 unsigned char c;
4157 int total = 0; /* How many end-of-lines are found so far. */
4158 int eol_type = CODING_EOL_UNDECIDED;
4159 int this_eol_type;
4160
4161 *skip = 0;
4162
4163 while (src < src_end && total < MAX_EOL_CHECK_COUNT)
4164 {
4165 c = *src++;
4166 if (c == '\n' || c == '\r')
4167 {
4168 if (*skip == 0)
4169 *skip = src - 1 - source;
4170 total++;
4171 if (c == '\n')
4172 this_eol_type = CODING_EOL_LF;
4173 else if (src >= src_end || *src != '\n')
4174 this_eol_type = CODING_EOL_CR;
4175 else
4176 this_eol_type = CODING_EOL_CRLF, src++;
4177
4178 if (eol_type == CODING_EOL_UNDECIDED)
4179 /* This is the first end-of-line. */
4180 eol_type = this_eol_type;
4181 else if (eol_type != this_eol_type)
4182 {
4183 /* The found type is different from what found before. */
4184 eol_type = CODING_EOL_INCONSISTENT;
4185 break;
4186 }
4187 }
4188 }
4189
4190 if (*skip == 0)
4191 *skip = src_end - source;
4192 return eol_type;
4193 }
4194
4195 /* Like detect_eol_type, but detect EOL type in 2-octet
4196 big-endian/little-endian format for coding systems utf-16-be and
4197 utf-16-le. */
4198
4199 static int
4200 detect_eol_type_in_2_octet_form (source, src_bytes, skip, big_endian_p)
4201 unsigned char *source;
4202 int src_bytes, *skip, big_endian_p;
4203 {
4204 unsigned char *src = source, *src_end = src + src_bytes;
4205 unsigned int c1, c2;
4206 int total = 0; /* How many end-of-lines are found so far. */
4207 int eol_type = CODING_EOL_UNDECIDED;
4208 int this_eol_type;
4209 int msb, lsb;
4210
4211 if (big_endian_p)
4212 msb = 0, lsb = 1;
4213 else
4214 msb = 1, lsb = 0;
4215
4216 *skip = 0;
4217
4218 while ((src + 1) < src_end && total < MAX_EOL_CHECK_COUNT)
4219 {
4220 c1 = (src[msb] << 8) | (src[lsb]);
4221 src += 2;
4222
4223 if (c1 == '\n' || c1 == '\r')
4224 {
4225 if (*skip == 0)
4226 *skip = src - 2 - source;
4227 total++;
4228 if (c1 == '\n')
4229 {
4230 this_eol_type = CODING_EOL_LF;
4231 }
4232 else
4233 {
4234 if ((src + 1) >= src_end)
4235 {
4236 this_eol_type = CODING_EOL_CR;
4237 }
4238 else
4239 {
4240 c2 = (src[msb] << 8) | (src[lsb]);
4241 if (c2 == '\n')
4242 this_eol_type = CODING_EOL_CRLF, src += 2;
4243 else
4244 this_eol_type = CODING_EOL_CR;
4245 }
4246 }
4247
4248 if (eol_type == CODING_EOL_UNDECIDED)
4249 /* This is the first end-of-line. */
4250 eol_type = this_eol_type;
4251 else if (eol_type != this_eol_type)
4252 {
4253 /* The found type is different from what found before. */
4254 eol_type = CODING_EOL_INCONSISTENT;
4255 break;
4256 }
4257 }
4258 }
4259
4260 if (*skip == 0)
4261 *skip = src_end - source;
4262 return eol_type;
4263 }
4264
4265 /* Detect how end-of-line of a text of length SRC_BYTES pointed by SRC
4266 is encoded. If it detects an appropriate format of end-of-line, it
4267 sets the information in *CODING. */
4268
4269 void
4270 detect_eol (coding, src, src_bytes)
4271 struct coding_system *coding;
4272 const unsigned char *src;
4273 int src_bytes;
4274 {
4275 Lisp_Object val;
4276 int skip;
4277 int eol_type;
4278
4279 switch (coding->category_idx)
4280 {
4281 case CODING_CATEGORY_IDX_UTF_16_BE:
4282 eol_type = detect_eol_type_in_2_octet_form (src, src_bytes, &skip, 1);
4283 break;
4284 case CODING_CATEGORY_IDX_UTF_16_LE:
4285 eol_type = detect_eol_type_in_2_octet_form (src, src_bytes, &skip, 0);
4286 break;
4287 default:
4288 eol_type = detect_eol_type (src, src_bytes, &skip);
4289 break;
4290 }
4291
4292 if (coding->heading_ascii > skip)
4293 coding->heading_ascii = skip;
4294 else
4295 skip = coding->heading_ascii;
4296
4297 if (eol_type == CODING_EOL_UNDECIDED)
4298 return;
4299 if (eol_type == CODING_EOL_INCONSISTENT)
4300 {
4301 #if 0
4302 /* This code is suppressed until we find a better way to
4303 distinguish raw text file and binary file. */
4304
4305 /* If we have already detected that the coding is raw-text, the
4306 coding should actually be no-conversion. */
4307 if (coding->type == coding_type_raw_text)
4308 {
4309 setup_coding_system (Qno_conversion, coding);
4310 return;
4311 }
4312 /* Else, let's decode only text code anyway. */
4313 #endif /* 0 */
4314 eol_type = CODING_EOL_LF;
4315 }
4316
4317 val = Fget (coding->symbol, Qeol_type);
4318 if (VECTORP (val) && XVECTOR (val)->size == 3)
4319 {
4320 int src_multibyte = coding->src_multibyte;
4321 int dst_multibyte = coding->dst_multibyte;
4322 struct composition_data *cmp_data = coding->cmp_data;
4323
4324 setup_coding_system (XVECTOR (val)->contents[eol_type], coding);
4325 coding->src_multibyte = src_multibyte;
4326 coding->dst_multibyte = dst_multibyte;
4327 coding->heading_ascii = skip;
4328 coding->cmp_data = cmp_data;
4329 }
4330 }
4331
4332 #define CONVERSION_BUFFER_EXTRA_ROOM 256
4333
4334 #define DECODING_BUFFER_MAG(coding) \
4335 (coding->type == coding_type_iso2022 \
4336 ? 3 \
4337 : (coding->type == coding_type_ccl \
4338 ? coding->spec.ccl.decoder.buf_magnification \
4339 : 2))
4340
4341 /* Return maximum size (bytes) of a buffer enough for decoding
4342 SRC_BYTES of text encoded in CODING. */
4343
4344 int
4345 decoding_buffer_size (coding, src_bytes)
4346 struct coding_system *coding;
4347 int src_bytes;
4348 {
4349 return (src_bytes * DECODING_BUFFER_MAG (coding)
4350 + CONVERSION_BUFFER_EXTRA_ROOM);
4351 }
4352
4353 /* Return maximum size (bytes) of a buffer enough for encoding
4354 SRC_BYTES of text to CODING. */
4355
4356 int
4357 encoding_buffer_size (coding, src_bytes)
4358 struct coding_system *coding;
4359 int src_bytes;
4360 {
4361 int magnification;
4362
4363 if (coding->type == coding_type_ccl)
4364 magnification = coding->spec.ccl.encoder.buf_magnification;
4365 else if (CODING_REQUIRE_ENCODING (coding))
4366 magnification = 3;
4367 else
4368 magnification = 1;
4369
4370 return (src_bytes * magnification + CONVERSION_BUFFER_EXTRA_ROOM);
4371 }
4372
4373 /* Working buffer for code conversion. */
4374 struct conversion_buffer
4375 {
4376 int size; /* size of data. */
4377 int on_stack; /* 1 if allocated by alloca. */
4378 unsigned char *data;
4379 };
4380
4381 /* Don't use alloca for allocating memory space larger than this, lest
4382 we overflow their stack. */
4383 #define MAX_ALLOCA 16*1024
4384
4385 /* Allocate LEN bytes of memory for BUF (struct conversion_buffer). */
4386 #define allocate_conversion_buffer(buf, len) \
4387 do { \
4388 if (len < MAX_ALLOCA) \
4389 { \
4390 buf.data = (unsigned char *) alloca (len); \
4391 buf.on_stack = 1; \
4392 } \
4393 else \
4394 { \
4395 buf.data = (unsigned char *) xmalloc (len); \
4396 buf.on_stack = 0; \
4397 } \
4398 buf.size = len; \
4399 } while (0)
4400
4401 /* Double the allocated memory for *BUF. */
4402 static void
4403 extend_conversion_buffer (buf)
4404 struct conversion_buffer *buf;
4405 {
4406 if (buf->on_stack)
4407 {
4408 unsigned char *save = buf->data;
4409 buf->data = (unsigned char *) xmalloc (buf->size * 2);
4410 bcopy (save, buf->data, buf->size);
4411 buf->on_stack = 0;
4412 }
4413 else
4414 {
4415 buf->data = (unsigned char *) xrealloc (buf->data, buf->size * 2);
4416 }
4417 buf->size *= 2;
4418 }
4419
4420 /* Free the allocated memory for BUF if it is not on stack. */
4421 static void
4422 free_conversion_buffer (buf)
4423 struct conversion_buffer *buf;
4424 {
4425 if (!buf->on_stack)
4426 xfree (buf->data);
4427 }
4428
4429 int
4430 ccl_coding_driver (coding, source, destination, src_bytes, dst_bytes, encodep)
4431 struct coding_system *coding;
4432 unsigned char *source, *destination;
4433 int src_bytes, dst_bytes, encodep;
4434 {
4435 struct ccl_program *ccl
4436 = encodep ? &coding->spec.ccl.encoder : &coding->spec.ccl.decoder;
4437 unsigned char *dst = destination;
4438
4439 ccl->suppress_error = coding->suppress_error;
4440 ccl->last_block = coding->mode & CODING_MODE_LAST_BLOCK;
4441 if (encodep)
4442 {
4443 /* On encoding, EOL format is converted within ccl_driver. For
4444 that, setup proper information in the structure CCL. */
4445 ccl->eol_type = coding->eol_type;
4446 if (ccl->eol_type ==CODING_EOL_UNDECIDED)
4447 ccl->eol_type = CODING_EOL_LF;
4448 ccl->cr_consumed = coding->spec.ccl.cr_carryover;
4449 }
4450 ccl->multibyte = coding->src_multibyte;
4451 if (coding->spec.ccl.eight_bit_carryover[0] != 0)
4452 {
4453 /* Move carryover bytes to DESTINATION. */
4454 unsigned char *p = coding->spec.ccl.eight_bit_carryover;
4455 while (*p)
4456 *dst++ = *p++;
4457 coding->spec.ccl.eight_bit_carryover[0] = 0;
4458 if (dst_bytes)
4459 dst_bytes -= dst - destination;
4460 }
4461
4462 coding->produced = (ccl_driver (ccl, source, dst, src_bytes, dst_bytes,
4463 &(coding->consumed))
4464 + dst - destination);
4465
4466 if (encodep)
4467 {
4468 coding->produced_char = coding->produced;
4469 coding->spec.ccl.cr_carryover = ccl->cr_consumed;
4470 }
4471 else if (!ccl->eight_bit_control)
4472 {
4473 /* The produced bytes forms a valid multibyte sequence. */
4474 coding->produced_char
4475 = multibyte_chars_in_text (destination, coding->produced);
4476 coding->spec.ccl.eight_bit_carryover[0] = 0;
4477 }
4478 else
4479 {
4480 /* On decoding, the destination should always multibyte. But,
4481 CCL program might have been generated an invalid multibyte
4482 sequence. Here we make such a sequence valid as
4483 multibyte. */
4484 int bytes
4485 = dst_bytes ? dst_bytes : source + coding->consumed - destination;
4486
4487 if ((coding->consumed < src_bytes
4488 || !ccl->last_block)
4489 && coding->produced >= 1
4490 && destination[coding->produced - 1] >= 0x80)
4491 {
4492 /* We should not convert the tailing 8-bit codes to
4493 multibyte form even if they doesn't form a valid
4494 multibyte sequence. They may form a valid sequence in
4495 the next call. */
4496 int carryover = 0;
4497
4498 if (destination[coding->produced - 1] < 0xA0)
4499 carryover = 1;
4500 else if (coding->produced >= 2)
4501 {
4502 if (destination[coding->produced - 2] >= 0x80)
4503 {
4504 if (destination[coding->produced - 2] < 0xA0)
4505 carryover = 2;
4506 else if (coding->produced >= 3
4507 && destination[coding->produced - 3] >= 0x80
4508 && destination[coding->produced - 3] < 0xA0)
4509 carryover = 3;
4510 }
4511 }
4512 if (carryover > 0)
4513 {
4514 BCOPY_SHORT (destination + coding->produced - carryover,
4515 coding->spec.ccl.eight_bit_carryover,
4516 carryover);
4517 coding->spec.ccl.eight_bit_carryover[carryover] = 0;
4518 coding->produced -= carryover;
4519 }
4520 }
4521 coding->produced = str_as_multibyte (destination, bytes,
4522 coding->produced,
4523 &(coding->produced_char));
4524 }
4525
4526 switch (ccl->status)
4527 {
4528 case CCL_STAT_SUSPEND_BY_SRC:
4529 coding->result = CODING_FINISH_INSUFFICIENT_SRC;
4530 break;
4531 case CCL_STAT_SUSPEND_BY_DST:
4532 coding->result = CODING_FINISH_INSUFFICIENT_DST;
4533 break;
4534 case CCL_STAT_QUIT:
4535 case CCL_STAT_INVALID_CMD:
4536 coding->result = CODING_FINISH_INTERRUPT;
4537 break;
4538 default:
4539 coding->result = CODING_FINISH_NORMAL;
4540 break;
4541 }
4542 return coding->result;
4543 }
4544
4545 /* Decode EOL format of the text at PTR of BYTES length destructively
4546 according to CODING->eol_type. This is called after the CCL
4547 program produced a decoded text at PTR. If we do CRLF->LF
4548 conversion, update CODING->produced and CODING->produced_char. */
4549
4550 static void
4551 decode_eol_post_ccl (coding, ptr, bytes)
4552 struct coding_system *coding;
4553 unsigned char *ptr;
4554 int bytes;
4555 {
4556 Lisp_Object val, saved_coding_symbol;
4557 unsigned char *pend = ptr + bytes;
4558 int dummy;
4559
4560 /* Remember the current coding system symbol. We set it back when
4561 an inconsistent EOL is found so that `last-coding-system-used' is
4562 set to the coding system that doesn't specify EOL conversion. */
4563 saved_coding_symbol = coding->symbol;
4564
4565 coding->spec.ccl.cr_carryover = 0;
4566 if (coding->eol_type == CODING_EOL_UNDECIDED)
4567 {
4568 /* Here, to avoid the call of setup_coding_system, we directly
4569 call detect_eol_type. */
4570 coding->eol_type = detect_eol_type (ptr, bytes, &dummy);
4571 if (coding->eol_type == CODING_EOL_INCONSISTENT)
4572 coding->eol_type = CODING_EOL_LF;
4573 if (coding->eol_type != CODING_EOL_UNDECIDED)
4574 {
4575 val = Fget (coding->symbol, Qeol_type);
4576 if (VECTORP (val) && XVECTOR (val)->size == 3)
4577 coding->symbol = XVECTOR (val)->contents[coding->eol_type];
4578 }
4579 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
4580 }
4581
4582 if (coding->eol_type == CODING_EOL_LF
4583 || coding->eol_type == CODING_EOL_UNDECIDED)
4584 {
4585 /* We have nothing to do. */
4586 ptr = pend;
4587 }
4588 else if (coding->eol_type == CODING_EOL_CRLF)
4589 {
4590 unsigned char *pstart = ptr, *p = ptr;
4591
4592 if (! (coding->mode & CODING_MODE_LAST_BLOCK)
4593 && *(pend - 1) == '\r')
4594 {
4595 /* If the last character is CR, we can't handle it here
4596 because LF will be in the not-yet-decoded source text.
4597 Record that the CR is not yet processed. */
4598 coding->spec.ccl.cr_carryover = 1;
4599 coding->produced--;
4600 coding->produced_char--;
4601 pend--;
4602 }
4603 while (ptr < pend)
4604 {
4605 if (*ptr == '\r')
4606 {
4607 if (ptr + 1 < pend && *(ptr + 1) == '\n')
4608 {
4609 *p++ = '\n';
4610 ptr += 2;
4611 }
4612 else
4613 {
4614 if (coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
4615 goto undo_eol_conversion;
4616 *p++ = *ptr++;
4617 }
4618 }
4619 else if (*ptr == '\n'
4620 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
4621 goto undo_eol_conversion;
4622 else
4623 *p++ = *ptr++;
4624 continue;
4625
4626 undo_eol_conversion:
4627 /* We have faced with inconsistent EOL format at PTR.
4628 Convert all LFs before PTR back to CRLFs. */
4629 for (p--, ptr--; p >= pstart; p--)
4630 {
4631 if (*p == '\n')
4632 *ptr-- = '\n', *ptr-- = '\r';
4633 else
4634 *ptr-- = *p;
4635 }
4636 /* If carryover is recorded, cancel it because we don't
4637 convert CRLF anymore. */
4638 if (coding->spec.ccl.cr_carryover)
4639 {
4640 coding->spec.ccl.cr_carryover = 0;
4641 coding->produced++;
4642 coding->produced_char++;
4643 pend++;
4644 }
4645 p = ptr = pend;
4646 coding->eol_type = CODING_EOL_LF;
4647 coding->symbol = saved_coding_symbol;
4648 }
4649 if (p < pend)
4650 {
4651 /* As each two-byte sequence CRLF was converted to LF, (PEND
4652 - P) is the number of deleted characters. */
4653 coding->produced -= pend - p;
4654 coding->produced_char -= pend - p;
4655 }
4656 }
4657 else /* i.e. coding->eol_type == CODING_EOL_CR */
4658 {
4659 unsigned char *p = ptr;
4660
4661 for (; ptr < pend; ptr++)
4662 {
4663 if (*ptr == '\r')
4664 *ptr = '\n';
4665 else if (*ptr == '\n'
4666 && coding->mode & CODING_MODE_INHIBIT_INCONSISTENT_EOL)
4667 {
4668 for (; p < ptr; p++)
4669 {
4670 if (*p == '\n')
4671 *p = '\r';
4672 }
4673 ptr = pend;
4674 coding->eol_type = CODING_EOL_LF;
4675 coding->symbol = saved_coding_symbol;
4676 }
4677 }
4678 }
4679 }
4680
4681 /* See "GENERAL NOTES about `decode_coding_XXX ()' functions". Before
4682 decoding, it may detect coding system and format of end-of-line if
4683 those are not yet decided. The source should be unibyte, the
4684 result is multibyte if CODING->dst_multibyte is nonzero, else
4685 unibyte. */
4686
4687 int
4688 decode_coding (coding, source, destination, src_bytes, dst_bytes)
4689 struct coding_system *coding;
4690 const unsigned char *source;
4691 unsigned char *destination;
4692 int src_bytes, dst_bytes;
4693 {
4694 int extra = 0;
4695
4696 if (coding->type == coding_type_undecided)
4697 detect_coding (coding, source, src_bytes);
4698
4699 if (coding->eol_type == CODING_EOL_UNDECIDED
4700 && coding->type != coding_type_ccl)
4701 {
4702 detect_eol (coding, source, src_bytes);
4703 /* We had better recover the original eol format if we
4704 encounter an inconsistent eol format while decoding. */
4705 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
4706 }
4707
4708 coding->produced = coding->produced_char = 0;
4709 coding->consumed = coding->consumed_char = 0;
4710 coding->errors = 0;
4711 coding->result = CODING_FINISH_NORMAL;
4712
4713 switch (coding->type)
4714 {
4715 case coding_type_sjis:
4716 decode_coding_sjis_big5 (coding, source, destination,
4717 src_bytes, dst_bytes, 1);
4718 break;
4719
4720 case coding_type_iso2022:
4721 decode_coding_iso2022 (coding, source, destination,
4722 src_bytes, dst_bytes);
4723 break;
4724
4725 case coding_type_big5:
4726 decode_coding_sjis_big5 (coding, source, destination,
4727 src_bytes, dst_bytes, 0);
4728 break;
4729
4730 case coding_type_emacs_mule:
4731 decode_coding_emacs_mule (coding, source, destination,
4732 src_bytes, dst_bytes);
4733 break;
4734
4735 case coding_type_ccl:
4736 if (coding->spec.ccl.cr_carryover)
4737 {
4738 /* Put the CR which was not processed by the previous call
4739 of decode_eol_post_ccl in DESTINATION. It will be
4740 decoded together with the following LF by the call to
4741 decode_eol_post_ccl below. */
4742 *destination = '\r';
4743 coding->produced++;
4744 coding->produced_char++;
4745 dst_bytes--;
4746 extra = coding->spec.ccl.cr_carryover;
4747 }
4748 ccl_coding_driver (coding, source, destination + extra,
4749 src_bytes, dst_bytes, 0);
4750 if (coding->eol_type != CODING_EOL_LF)
4751 {
4752 coding->produced += extra;
4753 coding->produced_char += extra;
4754 decode_eol_post_ccl (coding, destination, coding->produced);
4755 }
4756 break;
4757
4758 default:
4759 decode_eol (coding, source, destination, src_bytes, dst_bytes);
4760 }
4761
4762 if (coding->result == CODING_FINISH_INSUFFICIENT_SRC
4763 && coding->mode & CODING_MODE_LAST_BLOCK
4764 && coding->consumed == src_bytes)
4765 coding->result = CODING_FINISH_NORMAL;
4766
4767 if (coding->mode & CODING_MODE_LAST_BLOCK
4768 && coding->result == CODING_FINISH_INSUFFICIENT_SRC)
4769 {
4770 const unsigned char *src = source + coding->consumed;
4771 unsigned char *dst = destination + coding->produced;
4772
4773 src_bytes -= coding->consumed;
4774 coding->errors++;
4775 if (COMPOSING_P (coding))
4776 DECODE_COMPOSITION_END ('1');
4777 while (src_bytes--)
4778 {
4779 int c = *src++;
4780 dst += CHAR_STRING (c, dst);
4781 coding->produced_char++;
4782 }
4783 coding->consumed = coding->consumed_char = src - source;
4784 coding->produced = dst - destination;
4785 coding->result = CODING_FINISH_NORMAL;
4786 }
4787
4788 if (!coding->dst_multibyte)
4789 {
4790 coding->produced = str_as_unibyte (destination, coding->produced);
4791 coding->produced_char = coding->produced;
4792 }
4793
4794 return coding->result;
4795 }
4796
4797 /* See "GENERAL NOTES about `encode_coding_XXX ()' functions". The
4798 multibyteness of the source is CODING->src_multibyte, the
4799 multibyteness of the result is always unibyte. */
4800
4801 int
4802 encode_coding (coding, source, destination, src_bytes, dst_bytes)
4803 struct coding_system *coding;
4804 const unsigned char *source;
4805 unsigned char *destination;
4806 int src_bytes, dst_bytes;
4807 {
4808 coding->produced = coding->produced_char = 0;
4809 coding->consumed = coding->consumed_char = 0;
4810 coding->errors = 0;
4811 coding->result = CODING_FINISH_NORMAL;
4812
4813 switch (coding->type)
4814 {
4815 case coding_type_sjis:
4816 encode_coding_sjis_big5 (coding, source, destination,
4817 src_bytes, dst_bytes, 1);
4818 break;
4819
4820 case coding_type_iso2022:
4821 encode_coding_iso2022 (coding, source, destination,
4822 src_bytes, dst_bytes);
4823 break;
4824
4825 case coding_type_big5:
4826 encode_coding_sjis_big5 (coding, source, destination,
4827 src_bytes, dst_bytes, 0);
4828 break;
4829
4830 case coding_type_emacs_mule:
4831 encode_coding_emacs_mule (coding, source, destination,
4832 src_bytes, dst_bytes);
4833 break;
4834
4835 case coding_type_ccl:
4836 ccl_coding_driver (coding, source, destination,
4837 src_bytes, dst_bytes, 1);
4838 break;
4839
4840 default:
4841 encode_eol (coding, source, destination, src_bytes, dst_bytes);
4842 }
4843
4844 if (coding->mode & CODING_MODE_LAST_BLOCK
4845 && coding->result == CODING_FINISH_INSUFFICIENT_SRC)
4846 {
4847 const unsigned char *src = source + coding->consumed;
4848 unsigned char *dst = destination + coding->produced;
4849
4850 if (coding->type == coding_type_iso2022)
4851 ENCODE_RESET_PLANE_AND_REGISTER;
4852 if (COMPOSING_P (coding))
4853 *dst++ = ISO_CODE_ESC, *dst++ = '1';
4854 if (coding->consumed < src_bytes)
4855 {
4856 int len = src_bytes - coding->consumed;
4857
4858 BCOPY_SHORT (src, dst, len);
4859 if (coding->src_multibyte)
4860 len = str_as_unibyte (dst, len);
4861 dst += len;
4862 coding->consumed = src_bytes;
4863 }
4864 coding->produced = coding->produced_char = dst - destination;
4865 coding->result = CODING_FINISH_NORMAL;
4866 }
4867
4868 if (coding->result == CODING_FINISH_INSUFFICIENT_SRC
4869 && coding->consumed == src_bytes)
4870 coding->result = CODING_FINISH_NORMAL;
4871
4872 return coding->result;
4873 }
4874
4875 /* Scan text in the region between *BEG and *END (byte positions),
4876 skip characters which we don't have to decode by coding system
4877 CODING at the head and tail, then set *BEG and *END to the region
4878 of the text we actually have to convert. The caller should move
4879 the gap out of the region in advance if the region is from a
4880 buffer.
4881
4882 If STR is not NULL, *BEG and *END are indices into STR. */
4883
4884 static void
4885 shrink_decoding_region (beg, end, coding, str)
4886 int *beg, *end;
4887 struct coding_system *coding;
4888 unsigned char *str;
4889 {
4890 unsigned char *begp_orig, *begp, *endp_orig, *endp, c;
4891 int eol_conversion;
4892 Lisp_Object translation_table;
4893
4894 if (coding->type == coding_type_ccl
4895 || coding->type == coding_type_undecided
4896 || coding->eol_type != CODING_EOL_LF
4897 || !NILP (coding->post_read_conversion)
4898 || coding->composing != COMPOSITION_DISABLED)
4899 {
4900 /* We can't skip any data. */
4901 return;
4902 }
4903 if (coding->type == coding_type_no_conversion
4904 || coding->type == coding_type_raw_text
4905 || coding->type == coding_type_emacs_mule)
4906 {
4907 /* We need no conversion, but don't have to skip any data here.
4908 Decoding routine handles them effectively anyway. */
4909 return;
4910 }
4911
4912 translation_table = coding->translation_table_for_decode;
4913 if (NILP (translation_table) && !NILP (Venable_character_translation))
4914 translation_table = Vstandard_translation_table_for_decode;
4915 if (CHAR_TABLE_P (translation_table))
4916 {
4917 int i;
4918 for (i = 0; i < 128; i++)
4919 if (!NILP (CHAR_TABLE_REF (translation_table, i)))
4920 break;
4921 if (i < 128)
4922 /* Some ASCII character should be translated. We give up
4923 shrinking. */
4924 return;
4925 }
4926
4927 if (coding->heading_ascii >= 0)
4928 /* Detection routine has already found how much we can skip at the
4929 head. */
4930 *beg += coding->heading_ascii;
4931
4932 if (str)
4933 {
4934 begp_orig = begp = str + *beg;
4935 endp_orig = endp = str + *end;
4936 }
4937 else
4938 {
4939 begp_orig = begp = BYTE_POS_ADDR (*beg);
4940 endp_orig = endp = begp + *end - *beg;
4941 }
4942
4943 eol_conversion = (coding->eol_type == CODING_EOL_CR
4944 || coding->eol_type == CODING_EOL_CRLF);
4945
4946 switch (coding->type)
4947 {
4948 case coding_type_sjis:
4949 case coding_type_big5:
4950 /* We can skip all ASCII characters at the head. */
4951 if (coding->heading_ascii < 0)
4952 {
4953 if (eol_conversion)
4954 while (begp < endp && *begp < 0x80 && *begp != '\r') begp++;
4955 else
4956 while (begp < endp && *begp < 0x80) begp++;
4957 }
4958 /* We can skip all ASCII characters at the tail except for the
4959 second byte of SJIS or BIG5 code. */
4960 if (eol_conversion)
4961 while (begp < endp && endp[-1] < 0x80 && endp[-1] != '\r') endp--;
4962 else
4963 while (begp < endp && endp[-1] < 0x80) endp--;
4964 /* Do not consider LF as ascii if preceded by CR, since that
4965 confuses eol decoding. */
4966 if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n')
4967 endp++;
4968 if (begp < endp && endp < endp_orig && endp[-1] >= 0x80)
4969 endp++;
4970 break;
4971
4972 case coding_type_iso2022:
4973 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, 0) != CHARSET_ASCII)
4974 /* We can't skip any data. */
4975 break;
4976 if (coding->heading_ascii < 0)
4977 {
4978 /* We can skip all ASCII characters at the head except for a
4979 few control codes. */
4980 while (begp < endp && (c = *begp) < 0x80
4981 && c != ISO_CODE_CR && c != ISO_CODE_SO
4982 && c != ISO_CODE_SI && c != ISO_CODE_ESC
4983 && (!eol_conversion || c != ISO_CODE_LF))
4984 begp++;
4985 }
4986 switch (coding->category_idx)
4987 {
4988 case CODING_CATEGORY_IDX_ISO_8_1:
4989 case CODING_CATEGORY_IDX_ISO_8_2:
4990 /* We can skip all ASCII characters at the tail. */
4991 if (eol_conversion)
4992 while (begp < endp && (c = endp[-1]) < 0x80 && c != '\r') endp--;
4993 else
4994 while (begp < endp && endp[-1] < 0x80) endp--;
4995 /* Do not consider LF as ascii if preceded by CR, since that
4996 confuses eol decoding. */
4997 if (begp < endp && endp < endp_orig && endp[-1] == '\r' && endp[0] == '\n')
4998 endp++;
4999 break;
5000
5001 case CODING_CATEGORY_IDX_ISO_7:
5002 case CODING_CATEGORY_IDX_ISO_7_TIGHT:
5003 {
5004 /* We can skip all characters at the tail except for 8-bit
5005 codes and ESC and the following 2-byte at the tail. */
5006 unsigned char *eight_bit = NULL;
5007
5008 if (eol_conversion)
5009 while (begp < endp
5010 && (c = endp[-1]) != ISO_CODE_ESC && c != '\r')
5011 {
5012 if (!eight_bit && c & 0x80) eight_bit = endp;
5013 endp--;
5014 }
5015 else
5016 while (begp < endp
5017 && (c = endp[-1]) != ISO_CODE_ESC)
5018 {
5019 if (!eight_bit && c & 0x80) eight_bit = endp;
5020 endp--;
5021 }
5022 /* Do not consider LF as ascii if preceded by CR, since that
5023 confuses eol decoding. */
5024 if (begp < endp && endp < endp_orig
5025 && endp[-1] == '\r' && endp[0] == '\n')
5026 endp++;
5027 if (begp < endp && endp[-1] == ISO_CODE_ESC)
5028 {
5029 if (endp + 1 < endp_orig && end[0] == '(' && end[1] == 'B')
5030 /* This is an ASCII designation sequence. We can
5031 surely skip the tail. But, if we have
5032 encountered an 8-bit code, skip only the codes
5033 after that. */
5034 endp = eight_bit ? eight_bit : endp + 2;
5035 else
5036 /* Hmmm, we can't skip the tail. */
5037 endp = endp_orig;
5038 }
5039 else if (eight_bit)
5040 endp = eight_bit;
5041 }
5042 }
5043 break;
5044
5045 default:
5046 abort ();
5047 }
5048 *beg += begp - begp_orig;
5049 *end += endp - endp_orig;
5050 return;
5051 }
5052
5053 /* Like shrink_decoding_region but for encoding. */
5054
5055 static void
5056 shrink_encoding_region (beg, end, coding, str)
5057 int *beg, *end;
5058 struct coding_system *coding;
5059 unsigned char *str;
5060 {
5061 unsigned char *begp_orig, *begp, *endp_orig, *endp;
5062 int eol_conversion;
5063 Lisp_Object translation_table;
5064
5065 if (coding->type == coding_type_ccl
5066 || coding->eol_type == CODING_EOL_CRLF
5067 || coding->eol_type == CODING_EOL_CR
5068 || (coding->cmp_data && coding->cmp_data->used > 0))
5069 {
5070 /* We can't skip any data. */
5071 return;
5072 }
5073 if (coding->type == coding_type_no_conversion
5074 || coding->type == coding_type_raw_text
5075 || coding->type == coding_type_emacs_mule
5076 || coding->type == coding_type_undecided)
5077 {
5078 /* We need no conversion, but don't have to skip any data here.
5079 Encoding routine handles them effectively anyway. */
5080 return;
5081 }
5082
5083 translation_table = coding->translation_table_for_encode;
5084 if (NILP (translation_table) && !NILP (Venable_character_translation))
5085 translation_table = Vstandard_translation_table_for_encode;
5086 if (CHAR_TABLE_P (translation_table))
5087 {
5088 int i;
5089 for (i = 0; i < 128; i++)
5090 if (!NILP (CHAR_TABLE_REF (translation_table, i)))
5091 break;
5092 if (i < 128)
5093 /* Some ASCII character should be translated. We give up
5094 shrinking. */
5095 return;
5096 }
5097
5098 if (str)
5099 {
5100 begp_orig = begp = str + *beg;
5101 endp_orig = endp = str + *end;
5102 }
5103 else
5104 {
5105 begp_orig = begp = BYTE_POS_ADDR (*beg);
5106 endp_orig = endp = begp + *end - *beg;
5107 }
5108
5109 eol_conversion = (coding->eol_type == CODING_EOL_CR
5110 || coding->eol_type == CODING_EOL_CRLF);
5111
5112 /* Here, we don't have to check coding->pre_write_conversion because
5113 the caller is expected to have handled it already. */
5114 switch (coding->type)
5115 {
5116 case coding_type_iso2022:
5117 if (CODING_SPEC_ISO_INITIAL_DESIGNATION (coding, 0) != CHARSET_ASCII)
5118 /* We can't skip any data. */
5119 break;
5120 if (coding->flags & CODING_FLAG_ISO_DESIGNATE_AT_BOL)
5121 {
5122 unsigned char *bol = begp;
5123 while (begp < endp && *begp < 0x80)
5124 {
5125 begp++;
5126 if (begp[-1] == '\n')
5127 bol = begp;
5128 }
5129 begp = bol;
5130 goto label_skip_tail;
5131 }
5132 /* fall down ... */
5133
5134 case coding_type_sjis:
5135 case coding_type_big5:
5136 /* We can skip all ASCII characters at the head and tail. */
5137 if (eol_conversion)
5138 while (begp < endp && *begp < 0x80 && *begp != '\n') begp++;
5139 else
5140 while (begp < endp && *begp < 0x80) begp++;
5141 label_skip_tail:
5142 if (eol_conversion)
5143 while (begp < endp && endp[-1] < 0x80 && endp[-1] != '\n') endp--;
5144 else
5145 while (begp < endp && *(endp - 1) < 0x80) endp--;
5146 break;
5147
5148 default:
5149 abort ();
5150 }
5151
5152 *beg += begp - begp_orig;
5153 *end += endp - endp_orig;
5154 return;
5155 }
5156
5157 /* As shrinking conversion region requires some overhead, we don't try
5158 shrinking if the length of conversion region is less than this
5159 value. */
5160 static int shrink_conversion_region_threshhold = 1024;
5161
5162 #define SHRINK_CONVERSION_REGION(beg, end, coding, str, encodep) \
5163 do { \
5164 if (*(end) - *(beg) > shrink_conversion_region_threshhold) \
5165 { \
5166 if (encodep) shrink_encoding_region (beg, end, coding, str); \
5167 else shrink_decoding_region (beg, end, coding, str); \
5168 } \
5169 } while (0)
5170
5171 static Lisp_Object
5172 code_convert_region_unwind (dummy)
5173 Lisp_Object dummy;
5174 {
5175 inhibit_pre_post_conversion = 0;
5176 return Qnil;
5177 }
5178
5179 /* Store information about all compositions in the range FROM and TO
5180 of OBJ in memory blocks pointed by CODING->cmp_data. OBJ is a
5181 buffer or a string, defaults to the current buffer. */
5182
5183 void
5184 coding_save_composition (coding, from, to, obj)
5185 struct coding_system *coding;
5186 int from, to;
5187 Lisp_Object obj;
5188 {
5189 Lisp_Object prop;
5190 int start, end;
5191
5192 if (coding->composing == COMPOSITION_DISABLED)
5193 return;
5194 if (!coding->cmp_data)
5195 coding_allocate_composition_data (coding, from);
5196 if (!find_composition (from, to, &start, &end, &prop, obj)
5197 || end > to)
5198 return;
5199 if (start < from
5200 && (!find_composition (end, to, &start, &end, &prop, obj)
5201 || end > to))
5202 return;
5203 coding->composing = COMPOSITION_NO;
5204 do
5205 {
5206 if (COMPOSITION_VALID_P (start, end, prop))
5207 {
5208 enum composition_method method = COMPOSITION_METHOD (prop);
5209 if (coding->cmp_data->used + COMPOSITION_DATA_MAX_BUNCH_LENGTH
5210 >= COMPOSITION_DATA_SIZE)
5211 coding_allocate_composition_data (coding, from);
5212 /* For relative composition, we remember start and end
5213 positions, for the other compositions, we also remember
5214 components. */
5215 CODING_ADD_COMPOSITION_START (coding, start - from, method);
5216 if (method != COMPOSITION_RELATIVE)
5217 {
5218 /* We must store a*/
5219 Lisp_Object val, ch;
5220
5221 val = COMPOSITION_COMPONENTS (prop);
5222 if (CONSP (val))
5223 while (CONSP (val))
5224 {
5225 ch = XCAR (val), val = XCDR (val);
5226 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (ch));
5227 }
5228 else if (VECTORP (val) || STRINGP (val))
5229 {
5230 int len = (VECTORP (val)
5231 ? XVECTOR (val)->size : SCHARS (val));
5232 int i;
5233 for (i = 0; i < len; i++)
5234 {
5235 ch = (STRINGP (val)
5236 ? Faref (val, make_number (i))
5237 : XVECTOR (val)->contents[i]);
5238 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (ch));
5239 }
5240 }
5241 else /* INTEGERP (val) */
5242 CODING_ADD_COMPOSITION_COMPONENT (coding, XINT (val));
5243 }
5244 CODING_ADD_COMPOSITION_END (coding, end - from);
5245 }
5246 start = end;
5247 }
5248 while (start < to
5249 && find_composition (start, to, &start, &end, &prop, obj)
5250 && end <= to);
5251
5252 /* Make coding->cmp_data point to the first memory block. */
5253 while (coding->cmp_data->prev)
5254 coding->cmp_data = coding->cmp_data->prev;
5255 coding->cmp_data_start = 0;
5256 }
5257
5258 /* Reflect the saved information about compositions to OBJ.
5259 CODING->cmp_data points to a memory block for the information. OBJ
5260 is a buffer or a string, defaults to the current buffer. */
5261
5262 void
5263 coding_restore_composition (coding, obj)
5264 struct coding_system *coding;
5265 Lisp_Object obj;
5266 {
5267 struct composition_data *cmp_data = coding->cmp_data;
5268
5269 if (!cmp_data)
5270 return;
5271
5272 while (cmp_data->prev)
5273 cmp_data = cmp_data->prev;
5274
5275 while (cmp_data)
5276 {
5277 int i;
5278
5279 for (i = 0; i < cmp_data->used && cmp_data->data[i] > 0;
5280 i += cmp_data->data[i])
5281 {
5282 int *data = cmp_data->data + i;
5283 enum composition_method method = (enum composition_method) data[3];
5284 Lisp_Object components;
5285
5286 if (method == COMPOSITION_RELATIVE)
5287 components = Qnil;
5288 else
5289 {
5290 int len = data[0] - 4, j;
5291 Lisp_Object args[MAX_COMPOSITION_COMPONENTS * 2 - 1];
5292
5293 for (j = 0; j < len; j++)
5294 args[j] = make_number (data[4 + j]);
5295 components = (method == COMPOSITION_WITH_ALTCHARS
5296 ? Fstring (len, args) : Fvector (len, args));
5297 }
5298 compose_text (data[1], data[2], components, Qnil, obj);
5299 }
5300 cmp_data = cmp_data->next;
5301 }
5302 }
5303
5304 /* Decode (if ENCODEP is zero) or encode (if ENCODEP is nonzero) the
5305 text from FROM to TO (byte positions are FROM_BYTE and TO_BYTE) by
5306 coding system CODING, and return the status code of code conversion
5307 (currently, this value has no meaning).
5308
5309 How many characters (and bytes) are converted to how many
5310 characters (and bytes) are recorded in members of the structure
5311 CODING.
5312
5313 If REPLACE is nonzero, we do various things as if the original text
5314 is deleted and a new text is inserted. See the comments in
5315 replace_range (insdel.c) to know what we are doing.
5316
5317 If REPLACE is zero, it is assumed that the source text is unibyte.
5318 Otherwise, it is assumed that the source text is multibyte. */
5319
5320 int
5321 code_convert_region (from, from_byte, to, to_byte, coding, encodep, replace)
5322 int from, from_byte, to, to_byte, encodep, replace;
5323 struct coding_system *coding;
5324 {
5325 int len = to - from, len_byte = to_byte - from_byte;
5326 int nchars_del = 0, nbytes_del = 0;
5327 int require, inserted, inserted_byte;
5328 int head_skip, tail_skip, total_skip = 0;
5329 Lisp_Object saved_coding_symbol;
5330 int first = 1;
5331 unsigned char *src, *dst;
5332 Lisp_Object deletion;
5333 int orig_point = PT, orig_len = len;
5334 int prev_Z;
5335 int multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5336
5337 deletion = Qnil;
5338 saved_coding_symbol = coding->symbol;
5339
5340 if (from < PT && PT < to)
5341 {
5342 TEMP_SET_PT_BOTH (from, from_byte);
5343 orig_point = from;
5344 }
5345
5346 if (replace)
5347 {
5348 int saved_from = from;
5349 int saved_inhibit_modification_hooks;
5350
5351 prepare_to_modify_buffer (from, to, &from);
5352 if (saved_from != from)
5353 {
5354 to = from + len;
5355 from_byte = CHAR_TO_BYTE (from), to_byte = CHAR_TO_BYTE (to);
5356 len_byte = to_byte - from_byte;
5357 }
5358
5359 /* The code conversion routine can not preserve text properties
5360 for now. So, we must remove all text properties in the
5361 region. Here, we must suppress all modification hooks. */
5362 saved_inhibit_modification_hooks = inhibit_modification_hooks;
5363 inhibit_modification_hooks = 1;
5364 Fset_text_properties (make_number (from), make_number (to), Qnil, Qnil);
5365 inhibit_modification_hooks = saved_inhibit_modification_hooks;
5366 }
5367
5368 if (! encodep && CODING_REQUIRE_DETECTION (coding))
5369 {
5370 /* We must detect encoding of text and eol format. */
5371
5372 if (from < GPT && to > GPT)
5373 move_gap_both (from, from_byte);
5374 if (coding->type == coding_type_undecided)
5375 {
5376 detect_coding (coding, BYTE_POS_ADDR (from_byte), len_byte);
5377 if (coding->type == coding_type_undecided)
5378 {
5379 /* It seems that the text contains only ASCII, but we
5380 should not leave it undecided because the deeper
5381 decoding routine (decode_coding) tries to detect the
5382 encodings again in vain. */
5383 coding->type = coding_type_emacs_mule;
5384 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE;
5385 /* As emacs-mule decoder will handle composition, we
5386 need this setting to allocate coding->cmp_data
5387 later. */
5388 coding->composing = COMPOSITION_NO;
5389 }
5390 }
5391 if (coding->eol_type == CODING_EOL_UNDECIDED
5392 && coding->type != coding_type_ccl)
5393 {
5394 detect_eol (coding, BYTE_POS_ADDR (from_byte), len_byte);
5395 if (coding->eol_type == CODING_EOL_UNDECIDED)
5396 coding->eol_type = CODING_EOL_LF;
5397 /* We had better recover the original eol format if we
5398 encounter an inconsistent eol format while decoding. */
5399 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
5400 }
5401 }
5402
5403 /* Now we convert the text. */
5404
5405 /* For encoding, we must process pre-write-conversion in advance. */
5406 if (! inhibit_pre_post_conversion
5407 && encodep
5408 && SYMBOLP (coding->pre_write_conversion)
5409 && ! NILP (Ffboundp (coding->pre_write_conversion)))
5410 {
5411 /* The function in pre-write-conversion may put a new text in a
5412 new buffer. */
5413 struct buffer *prev = current_buffer;
5414 Lisp_Object new;
5415
5416 record_unwind_protect (code_convert_region_unwind, Qnil);
5417 /* We should not call any more pre-write/post-read-conversion
5418 functions while this pre-write-conversion is running. */
5419 inhibit_pre_post_conversion = 1;
5420 call2 (coding->pre_write_conversion,
5421 make_number (from), make_number (to));
5422 inhibit_pre_post_conversion = 0;
5423 /* Discard the unwind protect. */
5424 specpdl_ptr--;
5425
5426 if (current_buffer != prev)
5427 {
5428 len = ZV - BEGV;
5429 new = Fcurrent_buffer ();
5430 set_buffer_internal_1 (prev);
5431 del_range_2 (from, from_byte, to, to_byte, 0);
5432 TEMP_SET_PT_BOTH (from, from_byte);
5433 insert_from_buffer (XBUFFER (new), 1, len, 0);
5434 Fkill_buffer (new);
5435 if (orig_point >= to)
5436 orig_point += len - orig_len;
5437 else if (orig_point > from)
5438 orig_point = from;
5439 orig_len = len;
5440 to = from + len;
5441 from_byte = CHAR_TO_BYTE (from);
5442 to_byte = CHAR_TO_BYTE (to);
5443 len_byte = to_byte - from_byte;
5444 TEMP_SET_PT_BOTH (from, from_byte);
5445 }
5446 }
5447
5448 if (replace)
5449 {
5450 if (! EQ (current_buffer->undo_list, Qt))
5451 deletion = make_buffer_string_both (from, from_byte, to, to_byte, 1);
5452 else
5453 {
5454 nchars_del = to - from;
5455 nbytes_del = to_byte - from_byte;
5456 }
5457 }
5458
5459 if (coding->composing != COMPOSITION_DISABLED)
5460 {
5461 if (encodep)
5462 coding_save_composition (coding, from, to, Fcurrent_buffer ());
5463 else
5464 coding_allocate_composition_data (coding, from);
5465 }
5466
5467 /* Try to skip the heading and tailing ASCIIs. */
5468 if (coding->type != coding_type_ccl)
5469 {
5470 int from_byte_orig = from_byte, to_byte_orig = to_byte;
5471
5472 if (from < GPT && GPT < to)
5473 move_gap_both (from, from_byte);
5474 SHRINK_CONVERSION_REGION (&from_byte, &to_byte, coding, NULL, encodep);
5475 if (from_byte == to_byte
5476 && (encodep || NILP (coding->post_read_conversion))
5477 && ! CODING_REQUIRE_FLUSHING (coding))
5478 {
5479 coding->produced = len_byte;
5480 coding->produced_char = len;
5481 if (!replace)
5482 /* We must record and adjust for this new text now. */
5483 adjust_after_insert (from, from_byte_orig, to, to_byte_orig, len);
5484 return 0;
5485 }
5486
5487 head_skip = from_byte - from_byte_orig;
5488 tail_skip = to_byte_orig - to_byte;
5489 total_skip = head_skip + tail_skip;
5490 from += head_skip;
5491 to -= tail_skip;
5492 len -= total_skip; len_byte -= total_skip;
5493 }
5494
5495 /* For conversion, we must put the gap before the text in addition to
5496 making the gap larger for efficient decoding. The required gap
5497 size starts from 2000 which is the magic number used in make_gap.
5498 But, after one batch of conversion, it will be incremented if we
5499 find that it is not enough . */
5500 require = 2000;
5501
5502 if (GAP_SIZE < require)
5503 make_gap (require - GAP_SIZE);
5504 move_gap_both (from, from_byte);
5505
5506 inserted = inserted_byte = 0;
5507
5508 GAP_SIZE += len_byte;
5509 ZV -= len;
5510 Z -= len;
5511 ZV_BYTE -= len_byte;
5512 Z_BYTE -= len_byte;
5513
5514 if (GPT - BEG < BEG_UNCHANGED)
5515 BEG_UNCHANGED = GPT - BEG;
5516 if (Z - GPT < END_UNCHANGED)
5517 END_UNCHANGED = Z - GPT;
5518
5519 if (!encodep && coding->src_multibyte)
5520 {
5521 /* Decoding routines expects that the source text is unibyte.
5522 We must convert 8-bit characters of multibyte form to
5523 unibyte. */
5524 int len_byte_orig = len_byte;
5525 len_byte = str_as_unibyte (GAP_END_ADDR - len_byte, len_byte);
5526 if (len_byte < len_byte_orig)
5527 safe_bcopy (GAP_END_ADDR - len_byte_orig, GAP_END_ADDR - len_byte,
5528 len_byte);
5529 coding->src_multibyte = 0;
5530 }
5531
5532 for (;;)
5533 {
5534 int result;
5535
5536 /* The buffer memory is now:
5537 +--------+converted-text+---------+-------original-text-------+---+
5538 |<-from->|<--inserted-->|---------|<--------len_byte--------->|---|
5539 |<---------------------- GAP ----------------------->| */
5540 src = GAP_END_ADDR - len_byte;
5541 dst = GPT_ADDR + inserted_byte;
5542
5543 if (encodep)
5544 result = encode_coding (coding, src, dst, len_byte, 0);
5545 else
5546 {
5547 if (coding->composing != COMPOSITION_DISABLED)
5548 coding->cmp_data->char_offset = from + inserted;
5549 result = decode_coding (coding, src, dst, len_byte, 0);
5550 }
5551
5552 /* The buffer memory is now:
5553 +--------+-------converted-text----+--+------original-text----+---+
5554 |<-from->|<-inserted->|<-produced->|--|<-(len_byte-consumed)->|---|
5555 |<---------------------- GAP ----------------------->| */
5556
5557 inserted += coding->produced_char;
5558 inserted_byte += coding->produced;
5559 len_byte -= coding->consumed;
5560
5561 if (result == CODING_FINISH_INSUFFICIENT_CMP)
5562 {
5563 coding_allocate_composition_data (coding, from + inserted);
5564 continue;
5565 }
5566
5567 src += coding->consumed;
5568 dst += coding->produced;
5569
5570 if (result == CODING_FINISH_NORMAL)
5571 {
5572 src += len_byte;
5573 break;
5574 }
5575 if (! encodep && result == CODING_FINISH_INCONSISTENT_EOL)
5576 {
5577 unsigned char *pend = dst, *p = pend - inserted_byte;
5578 Lisp_Object eol_type;
5579
5580 /* Encode LFs back to the original eol format (CR or CRLF). */
5581 if (coding->eol_type == CODING_EOL_CR)
5582 {
5583 while (p < pend) if (*p++ == '\n') p[-1] = '\r';
5584 }
5585 else
5586 {
5587 int count = 0;
5588
5589 while (p < pend) if (*p++ == '\n') count++;
5590 if (src - dst < count)
5591 {
5592 /* We don't have sufficient room for encoding LFs
5593 back to CRLF. We must record converted and
5594 not-yet-converted text back to the buffer
5595 content, enlarge the gap, then record them out of
5596 the buffer contents again. */
5597 int add = len_byte + inserted_byte;
5598
5599 GAP_SIZE -= add;
5600 ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
5601 GPT += inserted_byte; GPT_BYTE += inserted_byte;
5602 make_gap (count - GAP_SIZE);
5603 GAP_SIZE += add;
5604 ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
5605 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
5606 /* Don't forget to update SRC, DST, and PEND. */
5607 src = GAP_END_ADDR - len_byte;
5608 dst = GPT_ADDR + inserted_byte;
5609 pend = dst;
5610 }
5611 inserted += count;
5612 inserted_byte += count;
5613 coding->produced += count;
5614 p = dst = pend + count;
5615 while (count)
5616 {
5617 *--p = *--pend;
5618 if (*p == '\n') count--, *--p = '\r';
5619 }
5620 }
5621
5622 /* Suppress eol-format conversion in the further conversion. */
5623 coding->eol_type = CODING_EOL_LF;
5624
5625 /* Set the coding system symbol to that for Unix-like EOL. */
5626 eol_type = Fget (saved_coding_symbol, Qeol_type);
5627 if (VECTORP (eol_type)
5628 && XVECTOR (eol_type)->size == 3
5629 && SYMBOLP (XVECTOR (eol_type)->contents[CODING_EOL_LF]))
5630 coding->symbol = XVECTOR (eol_type)->contents[CODING_EOL_LF];
5631 else
5632 coding->symbol = saved_coding_symbol;
5633
5634 continue;
5635 }
5636 if (len_byte <= 0)
5637 {
5638 if (coding->type != coding_type_ccl
5639 || coding->mode & CODING_MODE_LAST_BLOCK)
5640 break;
5641 coding->mode |= CODING_MODE_LAST_BLOCK;
5642 continue;
5643 }
5644 if (result == CODING_FINISH_INSUFFICIENT_SRC)
5645 {
5646 /* The source text ends in invalid codes. Let's just
5647 make them valid buffer contents, and finish conversion. */
5648 if (multibyte_p)
5649 {
5650 unsigned char *start = dst;
5651
5652 inserted += len_byte;
5653 while (len_byte--)
5654 {
5655 int c = *src++;
5656 dst += CHAR_STRING (c, dst);
5657 }
5658
5659 inserted_byte += dst - start;
5660 }
5661 else
5662 {
5663 inserted += len_byte;
5664 inserted_byte += len_byte;
5665 while (len_byte--)
5666 *dst++ = *src++;
5667 }
5668 break;
5669 }
5670 if (result == CODING_FINISH_INTERRUPT)
5671 {
5672 /* The conversion procedure was interrupted by a user. */
5673 break;
5674 }
5675 /* Now RESULT == CODING_FINISH_INSUFFICIENT_DST */
5676 if (coding->consumed < 1)
5677 {
5678 /* It's quite strange to require more memory without
5679 consuming any bytes. Perhaps CCL program bug. */
5680 break;
5681 }
5682 if (first)
5683 {
5684 /* We have just done the first batch of conversion which was
5685 stopped because of insufficient gap. Let's reconsider the
5686 required gap size (i.e. SRT - DST) now.
5687
5688 We have converted ORIG bytes (== coding->consumed) into
5689 NEW bytes (coding->produced). To convert the remaining
5690 LEN bytes, we may need REQUIRE bytes of gap, where:
5691 REQUIRE + LEN_BYTE = LEN_BYTE * (NEW / ORIG)
5692 REQUIRE = LEN_BYTE * (NEW - ORIG) / ORIG
5693 Here, we are sure that NEW >= ORIG. */
5694 float ratio = coding->produced - coding->consumed;
5695 ratio /= coding->consumed;
5696 require = len_byte * ratio;
5697 first = 0;
5698 }
5699 if ((src - dst) < (require + 2000))
5700 {
5701 /* See the comment above the previous call of make_gap. */
5702 int add = len_byte + inserted_byte;
5703
5704 GAP_SIZE -= add;
5705 ZV += add; Z += add; ZV_BYTE += add; Z_BYTE += add;
5706 GPT += inserted_byte; GPT_BYTE += inserted_byte;
5707 make_gap (require + 2000);
5708 GAP_SIZE += add;
5709 ZV -= add; Z -= add; ZV_BYTE -= add; Z_BYTE -= add;
5710 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
5711 }
5712 }
5713 if (src - dst > 0) *dst = 0; /* Put an anchor. */
5714
5715 if (encodep && coding->dst_multibyte)
5716 {
5717 /* The output is unibyte. We must convert 8-bit characters to
5718 multibyte form. */
5719 if (inserted_byte * 2 > GAP_SIZE)
5720 {
5721 GAP_SIZE -= inserted_byte;
5722 ZV += inserted_byte; Z += inserted_byte;
5723 ZV_BYTE += inserted_byte; Z_BYTE += inserted_byte;
5724 GPT += inserted_byte; GPT_BYTE += inserted_byte;
5725 make_gap (inserted_byte - GAP_SIZE);
5726 GAP_SIZE += inserted_byte;
5727 ZV -= inserted_byte; Z -= inserted_byte;
5728 ZV_BYTE -= inserted_byte; Z_BYTE -= inserted_byte;
5729 GPT -= inserted_byte; GPT_BYTE -= inserted_byte;
5730 }
5731 inserted_byte = str_to_multibyte (GPT_ADDR, GAP_SIZE, inserted_byte);
5732 }
5733
5734 /* If we shrank the conversion area, adjust it now. */
5735 if (total_skip > 0)
5736 {
5737 if (tail_skip > 0)
5738 safe_bcopy (GAP_END_ADDR, GPT_ADDR + inserted_byte, tail_skip);
5739 inserted += total_skip; inserted_byte += total_skip;
5740 GAP_SIZE += total_skip;
5741 GPT -= head_skip; GPT_BYTE -= head_skip;
5742 ZV -= total_skip; ZV_BYTE -= total_skip;
5743 Z -= total_skip; Z_BYTE -= total_skip;
5744 from -= head_skip; from_byte -= head_skip;
5745 to += tail_skip; to_byte += tail_skip;
5746 }
5747
5748 prev_Z = Z;
5749 if (! EQ (current_buffer->undo_list, Qt))
5750 adjust_after_replace (from, from_byte, deletion, inserted, inserted_byte);
5751 else
5752 adjust_after_replace_noundo (from, from_byte, nchars_del, nbytes_del,
5753 inserted, inserted_byte);
5754 inserted = Z - prev_Z;
5755
5756 if (!encodep && coding->cmp_data && coding->cmp_data->used)
5757 coding_restore_composition (coding, Fcurrent_buffer ());
5758 coding_free_composition_data (coding);
5759
5760 if (! inhibit_pre_post_conversion
5761 && ! encodep && ! NILP (coding->post_read_conversion))
5762 {
5763 Lisp_Object val;
5764
5765 if (from != PT)
5766 TEMP_SET_PT_BOTH (from, from_byte);
5767 prev_Z = Z;
5768 record_unwind_protect (code_convert_region_unwind, Qnil);
5769 /* We should not call any more pre-write/post-read-conversion
5770 functions while this post-read-conversion is running. */
5771 inhibit_pre_post_conversion = 1;
5772 val = call1 (coding->post_read_conversion, make_number (inserted));
5773 inhibit_pre_post_conversion = 0;
5774 /* Discard the unwind protect. */
5775 specpdl_ptr--;
5776 CHECK_NUMBER (val);
5777 inserted += Z - prev_Z;
5778 }
5779
5780 if (orig_point >= from)
5781 {
5782 if (orig_point >= from + orig_len)
5783 orig_point += inserted - orig_len;
5784 else
5785 orig_point = from;
5786 TEMP_SET_PT (orig_point);
5787 }
5788
5789 if (replace)
5790 {
5791 signal_after_change (from, to - from, inserted);
5792 update_compositions (from, from + inserted, CHECK_BORDER);
5793 }
5794
5795 {
5796 coding->consumed = to_byte - from_byte;
5797 coding->consumed_char = to - from;
5798 coding->produced = inserted_byte;
5799 coding->produced_char = inserted;
5800 }
5801
5802 return 0;
5803 }
5804
5805 Lisp_Object
5806 run_pre_post_conversion_on_str (str, coding, encodep)
5807 Lisp_Object str;
5808 struct coding_system *coding;
5809 int encodep;
5810 {
5811 int count = SPECPDL_INDEX ();
5812 struct gcpro gcpro1;
5813 int multibyte = STRING_MULTIBYTE (str);
5814 Lisp_Object buffer;
5815 struct buffer *buf;
5816
5817 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
5818 record_unwind_protect (code_convert_region_unwind, Qnil);
5819 GCPRO1 (str);
5820
5821 buffer = Fget_buffer_create (build_string (" *code-converting-work*"));
5822 buf = XBUFFER (buffer);
5823
5824 buf->directory = current_buffer->directory;
5825 buf->read_only = Qnil;
5826 buf->filename = Qnil;
5827 buf->undo_list = Qt;
5828 buf->overlays_before = Qnil;
5829 buf->overlays_after = Qnil;
5830
5831 set_buffer_internal (buf);
5832 /* We must insert the contents of STR as is without
5833 unibyte<->multibyte conversion. For that, we adjust the
5834 multibyteness of the working buffer to that of STR. */
5835 Ferase_buffer ();
5836 buf->enable_multibyte_characters = multibyte ? Qt : Qnil;
5837
5838 insert_from_string (str, 0, 0,
5839 SCHARS (str), SBYTES (str), 0);
5840 UNGCPRO;
5841 inhibit_pre_post_conversion = 1;
5842 if (encodep)
5843 call2 (coding->pre_write_conversion, make_number (BEG), make_number (Z));
5844 else
5845 {
5846 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
5847 call1 (coding->post_read_conversion, make_number (Z - BEG));
5848 }
5849 inhibit_pre_post_conversion = 0;
5850 str = make_buffer_string (BEG, Z, 1);
5851 return unbind_to (count, str);
5852 }
5853
5854 Lisp_Object
5855 decode_coding_string (str, coding, nocopy)
5856 Lisp_Object str;
5857 struct coding_system *coding;
5858 int nocopy;
5859 {
5860 int len;
5861 struct conversion_buffer buf;
5862 int from, to_byte;
5863 Lisp_Object saved_coding_symbol;
5864 int result;
5865 int require_decoding;
5866 int shrinked_bytes = 0;
5867 Lisp_Object newstr;
5868 int consumed, consumed_char, produced, produced_char;
5869
5870 from = 0;
5871 to_byte = SBYTES (str);
5872
5873 saved_coding_symbol = coding->symbol;
5874 coding->src_multibyte = STRING_MULTIBYTE (str);
5875 coding->dst_multibyte = 1;
5876 if (CODING_REQUIRE_DETECTION (coding))
5877 {
5878 /* See the comments in code_convert_region. */
5879 if (coding->type == coding_type_undecided)
5880 {
5881 detect_coding (coding, SDATA (str), to_byte);
5882 if (coding->type == coding_type_undecided)
5883 {
5884 coding->type = coding_type_emacs_mule;
5885 coding->category_idx = CODING_CATEGORY_IDX_EMACS_MULE;
5886 /* As emacs-mule decoder will handle composition, we
5887 need this setting to allocate coding->cmp_data
5888 later. */
5889 coding->composing = COMPOSITION_NO;
5890 }
5891 }
5892 if (coding->eol_type == CODING_EOL_UNDECIDED
5893 && coding->type != coding_type_ccl)
5894 {
5895 saved_coding_symbol = coding->symbol;
5896 detect_eol (coding, SDATA (str), to_byte);
5897 if (coding->eol_type == CODING_EOL_UNDECIDED)
5898 coding->eol_type = CODING_EOL_LF;
5899 /* We had better recover the original eol format if we
5900 encounter an inconsistent eol format while decoding. */
5901 coding->mode |= CODING_MODE_INHIBIT_INCONSISTENT_EOL;
5902 }
5903 }
5904
5905 if (coding->type == coding_type_no_conversion
5906 || coding->type == coding_type_raw_text)
5907 coding->dst_multibyte = 0;
5908
5909 require_decoding = CODING_REQUIRE_DECODING (coding);
5910
5911 if (STRING_MULTIBYTE (str))
5912 {
5913 /* Decoding routines expect the source text to be unibyte. */
5914 str = Fstring_as_unibyte (str);
5915 to_byte = SBYTES (str);
5916 nocopy = 1;
5917 coding->src_multibyte = 0;
5918 }
5919
5920 /* Try to skip the heading and tailing ASCIIs. */
5921 if (require_decoding && coding->type != coding_type_ccl)
5922 {
5923 SHRINK_CONVERSION_REGION (&from, &to_byte, coding, SDATA (str),
5924 0);
5925 if (from == to_byte)
5926 require_decoding = 0;
5927 shrinked_bytes = from + (SBYTES (str) - to_byte);
5928 }
5929
5930 if (!require_decoding)
5931 {
5932 coding->consumed = SBYTES (str);
5933 coding->consumed_char = SCHARS (str);
5934 if (coding->dst_multibyte)
5935 {
5936 str = Fstring_as_multibyte (str);
5937 nocopy = 1;
5938 }
5939 coding->produced = SBYTES (str);
5940 coding->produced_char = SCHARS (str);
5941 return (nocopy ? str : Fcopy_sequence (str));
5942 }
5943
5944 if (coding->composing != COMPOSITION_DISABLED)
5945 coding_allocate_composition_data (coding, from);
5946 len = decoding_buffer_size (coding, to_byte - from);
5947 allocate_conversion_buffer (buf, len);
5948
5949 consumed = consumed_char = produced = produced_char = 0;
5950 while (1)
5951 {
5952 result = decode_coding (coding, SDATA (str) + from + consumed,
5953 buf.data + produced, to_byte - from - consumed,
5954 buf.size - produced);
5955 consumed += coding->consumed;
5956 consumed_char += coding->consumed_char;
5957 produced += coding->produced;
5958 produced_char += coding->produced_char;
5959 if (result == CODING_FINISH_NORMAL
5960 || (result == CODING_FINISH_INSUFFICIENT_SRC
5961 && coding->consumed == 0))
5962 break;
5963 if (result == CODING_FINISH_INSUFFICIENT_CMP)
5964 coding_allocate_composition_data (coding, from + produced_char);
5965 else if (result == CODING_FINISH_INSUFFICIENT_DST)
5966 extend_conversion_buffer (&buf);
5967 else if (result == CODING_FINISH_INCONSISTENT_EOL)
5968 {
5969 Lisp_Object eol_type;
5970
5971 /* Recover the original EOL format. */
5972 if (coding->eol_type == CODING_EOL_CR)
5973 {
5974 unsigned char *p;
5975 for (p = buf.data; p < buf.data + produced; p++)
5976 if (*p == '\n') *p = '\r';
5977 }
5978 else if (coding->eol_type == CODING_EOL_CRLF)
5979 {
5980 int num_eol = 0;
5981 unsigned char *p0, *p1;
5982 for (p0 = buf.data, p1 = p0 + produced; p0 < p1; p0++)
5983 if (*p0 == '\n') num_eol++;
5984 if (produced + num_eol >= buf.size)
5985 extend_conversion_buffer (&buf);
5986 for (p0 = buf.data + produced, p1 = p0 + num_eol; p0 > buf.data;)
5987 {
5988 *--p1 = *--p0;
5989 if (*p0 == '\n') *--p1 = '\r';
5990 }
5991 produced += num_eol;
5992 produced_char += num_eol;
5993 }
5994 /* Suppress eol-format conversion in the further conversion. */
5995 coding->eol_type = CODING_EOL_LF;
5996
5997 /* Set the coding system symbol to that for Unix-like EOL. */
5998 eol_type = Fget (saved_coding_symbol, Qeol_type);
5999 if (VECTORP (eol_type)
6000 && XVECTOR (eol_type)->size == 3
6001 && SYMBOLP (XVECTOR (eol_type)->contents[CODING_EOL_LF]))
6002 coding->symbol = XVECTOR (eol_type)->contents[CODING_EOL_LF];
6003 else
6004 coding->symbol = saved_coding_symbol;
6005
6006
6007 }
6008 }
6009
6010 coding->consumed = consumed;
6011 coding->consumed_char = consumed_char;
6012 coding->produced = produced;
6013 coding->produced_char = produced_char;
6014
6015 if (coding->dst_multibyte)
6016 newstr = make_uninit_multibyte_string (produced_char + shrinked_bytes,
6017 produced + shrinked_bytes);
6018 else
6019 newstr = make_uninit_string (produced + shrinked_bytes);
6020 if (from > 0)
6021 STRING_COPYIN (newstr, 0, SDATA (str), from);
6022 STRING_COPYIN (newstr, from, buf.data, produced);
6023 if (shrinked_bytes > from)
6024 STRING_COPYIN (newstr, from + produced,
6025 SDATA (str) + to_byte,
6026 shrinked_bytes - from);
6027 free_conversion_buffer (&buf);
6028
6029 if (coding->cmp_data && coding->cmp_data->used)
6030 coding_restore_composition (coding, newstr);
6031 coding_free_composition_data (coding);
6032
6033 if (SYMBOLP (coding->post_read_conversion)
6034 && !NILP (Ffboundp (coding->post_read_conversion)))
6035 newstr = run_pre_post_conversion_on_str (newstr, coding, 0);
6036
6037 return newstr;
6038 }
6039
6040 Lisp_Object
6041 encode_coding_string (str, coding, nocopy)
6042 Lisp_Object str;
6043 struct coding_system *coding;
6044 int nocopy;
6045 {
6046 int len;
6047 struct conversion_buffer buf;
6048 int from, to, to_byte;
6049 int result;
6050 int shrinked_bytes = 0;
6051 Lisp_Object newstr;
6052 int consumed, consumed_char, produced, produced_char;
6053
6054 if (SYMBOLP (coding->pre_write_conversion)
6055 && !NILP (Ffboundp (coding->pre_write_conversion)))
6056 str = run_pre_post_conversion_on_str (str, coding, 1);
6057
6058 from = 0;
6059 to = SCHARS (str);
6060 to_byte = SBYTES (str);
6061
6062 /* Encoding routines determine the multibyteness of the source text
6063 by coding->src_multibyte. */
6064 coding->src_multibyte = STRING_MULTIBYTE (str);
6065 coding->dst_multibyte = 0;
6066 if (! CODING_REQUIRE_ENCODING (coding))
6067 {
6068 coding->consumed = SBYTES (str);
6069 coding->consumed_char = SCHARS (str);
6070 if (STRING_MULTIBYTE (str))
6071 {
6072 str = Fstring_as_unibyte (str);
6073 nocopy = 1;
6074 }
6075 coding->produced = SBYTES (str);
6076 coding->produced_char = SCHARS (str);
6077 return (nocopy ? str : Fcopy_sequence (str));
6078 }
6079
6080 if (coding->composing != COMPOSITION_DISABLED)
6081 coding_save_composition (coding, from, to, str);
6082
6083 /* Try to skip the heading and tailing ASCIIs. */
6084 if (coding->type != coding_type_ccl)
6085 {
6086 SHRINK_CONVERSION_REGION (&from, &to_byte, coding, SDATA (str),
6087 1);
6088 if (from == to_byte)
6089 return (nocopy ? str : Fcopy_sequence (str));
6090 shrinked_bytes = from + (SBYTES (str) - to_byte);
6091 }
6092
6093 len = encoding_buffer_size (coding, to_byte - from);
6094 allocate_conversion_buffer (buf, len);
6095
6096 consumed = consumed_char = produced = produced_char = 0;
6097 while (1)
6098 {
6099 result = encode_coding (coding, SDATA (str) + from + consumed,
6100 buf.data + produced, to_byte - from - consumed,
6101 buf.size - produced);
6102 consumed += coding->consumed;
6103 consumed_char += coding->consumed_char;
6104 produced += coding->produced;
6105 produced_char += coding->produced_char;
6106 if (result == CODING_FINISH_NORMAL
6107 || (result == CODING_FINISH_INSUFFICIENT_SRC
6108 && coding->consumed == 0))
6109 break;
6110 /* Now result should be CODING_FINISH_INSUFFICIENT_DST. */
6111 extend_conversion_buffer (&buf);
6112 }
6113
6114 coding->consumed = consumed;
6115 coding->consumed_char = consumed_char;
6116 coding->produced = produced;
6117 coding->produced_char = produced_char;
6118
6119 newstr = make_uninit_string (produced + shrinked_bytes);
6120 if (from > 0)
6121 STRING_COPYIN (newstr, 0, SDATA (str), from);
6122 STRING_COPYIN (newstr, from, buf.data, produced);
6123 if (shrinked_bytes > from)
6124 STRING_COPYIN (newstr, from + produced,
6125 SDATA (str) + to_byte,
6126 shrinked_bytes - from);
6127
6128 free_conversion_buffer (&buf);
6129 coding_free_composition_data (coding);
6130
6131 return newstr;
6132 }
6133
6134 \f
6135 #ifdef emacs
6136 /*** 8. Emacs Lisp library functions ***/
6137
6138 DEFUN ("coding-system-p", Fcoding_system_p, Scoding_system_p, 1, 1, 0,
6139 doc: /* Return t if OBJECT is nil or a coding-system.
6140 See the documentation of `make-coding-system' for information
6141 about coding-system objects. */)
6142 (obj)
6143 Lisp_Object obj;
6144 {
6145 if (NILP (obj))
6146 return Qt;
6147 if (!SYMBOLP (obj))
6148 return Qnil;
6149 /* Get coding-spec vector for OBJ. */
6150 obj = Fget (obj, Qcoding_system);
6151 return ((VECTORP (obj) && XVECTOR (obj)->size == 5)
6152 ? Qt : Qnil);
6153 }
6154
6155 DEFUN ("read-non-nil-coding-system", Fread_non_nil_coding_system,
6156 Sread_non_nil_coding_system, 1, 1, 0,
6157 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT. */)
6158 (prompt)
6159 Lisp_Object prompt;
6160 {
6161 Lisp_Object val;
6162 do
6163 {
6164 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
6165 Qt, Qnil, Qcoding_system_history, Qnil, Qnil);
6166 }
6167 while (SCHARS (val) == 0);
6168 return (Fintern (val, Qnil));
6169 }
6170
6171 DEFUN ("read-coding-system", Fread_coding_system, Sread_coding_system, 1, 2, 0,
6172 doc: /* Read a coding system from the minibuffer, prompting with string PROMPT.
6173 If the user enters null input, return second argument DEFAULT-CODING-SYSTEM. */)
6174 (prompt, default_coding_system)
6175 Lisp_Object prompt, default_coding_system;
6176 {
6177 Lisp_Object val;
6178 if (SYMBOLP (default_coding_system))
6179 default_coding_system = SYMBOL_NAME (default_coding_system);
6180 val = Fcompleting_read (prompt, Vcoding_system_alist, Qnil,
6181 Qt, Qnil, Qcoding_system_history,
6182 default_coding_system, Qnil);
6183 return (SCHARS (val) == 0 ? Qnil : Fintern (val, Qnil));
6184 }
6185
6186 DEFUN ("check-coding-system", Fcheck_coding_system, Scheck_coding_system,
6187 1, 1, 0,
6188 doc: /* Check validity of CODING-SYSTEM.
6189 If valid, return CODING-SYSTEM, else signal a `coding-system-error' error.
6190 It is valid if it is a symbol with a non-nil `coding-system' property.
6191 The value of property should be a vector of length 5. */)
6192 (coding_system)
6193 Lisp_Object coding_system;
6194 {
6195 CHECK_SYMBOL (coding_system);
6196 if (!NILP (Fcoding_system_p (coding_system)))
6197 return coding_system;
6198 while (1)
6199 Fsignal (Qcoding_system_error, Fcons (coding_system, Qnil));
6200 }
6201 \f
6202 Lisp_Object
6203 detect_coding_system (src, src_bytes, highest, multibytep)
6204 const unsigned char *src;
6205 int src_bytes, highest;
6206 int multibytep;
6207 {
6208 int coding_mask, eol_type;
6209 Lisp_Object val, tmp;
6210 int dummy;
6211
6212 coding_mask = detect_coding_mask (src, src_bytes, NULL, &dummy, multibytep);
6213 eol_type = detect_eol_type (src, src_bytes, &dummy);
6214 if (eol_type == CODING_EOL_INCONSISTENT)
6215 eol_type = CODING_EOL_UNDECIDED;
6216
6217 if (!coding_mask)
6218 {
6219 val = Qundecided;
6220 if (eol_type != CODING_EOL_UNDECIDED)
6221 {
6222 Lisp_Object val2;
6223 val2 = Fget (Qundecided, Qeol_type);
6224 if (VECTORP (val2))
6225 val = XVECTOR (val2)->contents[eol_type];
6226 }
6227 return (highest ? val : Fcons (val, Qnil));
6228 }
6229
6230 /* At first, gather possible coding systems in VAL. */
6231 val = Qnil;
6232 for (tmp = Vcoding_category_list; CONSP (tmp); tmp = XCDR (tmp))
6233 {
6234 Lisp_Object category_val, category_index;
6235
6236 category_index = Fget (XCAR (tmp), Qcoding_category_index);
6237 category_val = Fsymbol_value (XCAR (tmp));
6238 if (!NILP (category_val)
6239 && NATNUMP (category_index)
6240 && (coding_mask & (1 << XFASTINT (category_index))))
6241 {
6242 val = Fcons (category_val, val);
6243 if (highest)
6244 break;
6245 }
6246 }
6247 if (!highest)
6248 val = Fnreverse (val);
6249
6250 /* Then, replace the elements with subsidiary coding systems. */
6251 for (tmp = val; CONSP (tmp); tmp = XCDR (tmp))
6252 {
6253 if (eol_type != CODING_EOL_UNDECIDED
6254 && eol_type != CODING_EOL_INCONSISTENT)
6255 {
6256 Lisp_Object eol;
6257 eol = Fget (XCAR (tmp), Qeol_type);
6258 if (VECTORP (eol))
6259 XSETCAR (tmp, XVECTOR (eol)->contents[eol_type]);
6260 }
6261 }
6262 return (highest ? XCAR (val) : val);
6263 }
6264
6265 DEFUN ("detect-coding-region", Fdetect_coding_region, Sdetect_coding_region,
6266 2, 3, 0,
6267 doc: /* Detect coding system of the text in the region between START and END.
6268 Return a list of possible coding systems ordered by priority.
6269
6270 If only ASCII characters are found, it returns a list of single element
6271 `undecided' or its subsidiary coding system according to a detected
6272 end-of-line format.
6273
6274 If optional argument HIGHEST is non-nil, return the coding system of
6275 highest priority. */)
6276 (start, end, highest)
6277 Lisp_Object start, end, highest;
6278 {
6279 int from, to;
6280 int from_byte, to_byte;
6281 int include_anchor_byte = 0;
6282
6283 CHECK_NUMBER_COERCE_MARKER (start);
6284 CHECK_NUMBER_COERCE_MARKER (end);
6285
6286 validate_region (&start, &end);
6287 from = XINT (start), to = XINT (end);
6288 from_byte = CHAR_TO_BYTE (from);
6289 to_byte = CHAR_TO_BYTE (to);
6290
6291 if (from < GPT && to >= GPT)
6292 move_gap_both (to, to_byte);
6293 /* If we an anchor byte `\0' follows the region, we include it in
6294 the detecting source. Then code detectors can handle the tailing
6295 byte sequence more accurately.
6296
6297 Fix me: This is not an perfect solution. It is better that we
6298 add one more argument, say LAST_BLOCK, to all detect_coding_XXX.
6299 */
6300 if (to == Z || (to == GPT && GAP_SIZE > 0))
6301 include_anchor_byte = 1;
6302 return detect_coding_system (BYTE_POS_ADDR (from_byte),
6303 to_byte - from_byte + include_anchor_byte,
6304 !NILP (highest),
6305 !NILP (current_buffer
6306 ->enable_multibyte_characters));
6307 }
6308
6309 DEFUN ("detect-coding-string", Fdetect_coding_string, Sdetect_coding_string,
6310 1, 2, 0,
6311 doc: /* Detect coding system of the text in STRING.
6312 Return a list of possible coding systems ordered by priority.
6313
6314 If only ASCII characters are found, it returns a list of single element
6315 `undecided' or its subsidiary coding system according to a detected
6316 end-of-line format.
6317
6318 If optional argument HIGHEST is non-nil, return the coding system of
6319 highest priority. */)
6320 (string, highest)
6321 Lisp_Object string, highest;
6322 {
6323 CHECK_STRING (string);
6324
6325 return detect_coding_system (SDATA (string),
6326 /* "+ 1" is to include the anchor byte
6327 `\0'. With this, code detectors can
6328 handle the tailing bytes more
6329 accurately. */
6330 SBYTES (string) + 1,
6331 !NILP (highest),
6332 STRING_MULTIBYTE (string));
6333 }
6334
6335 /* Return an intersection of lists L1 and L2. */
6336
6337 static Lisp_Object
6338 intersection (l1, l2)
6339 Lisp_Object l1, l2;
6340 {
6341 Lisp_Object val = Fcons (Qnil, Qnil), tail;
6342
6343 for (tail = val; CONSP (l1); l1 = XCDR (l1))
6344 {
6345 if (!NILP (Fmemq (XCAR (l1), l2)))
6346 {
6347 XSETCDR (tail, Fcons (XCAR (l1), Qnil));
6348 tail = XCDR (tail);
6349 }
6350 }
6351 return XCDR (val);
6352 }
6353
6354
6355 /* Subroutine for Fsafe_coding_systems_region_internal.
6356
6357 Return a list of coding systems that safely encode the multibyte
6358 text between P and PEND. SAFE_CODINGS, if non-nil, is a list of
6359 possible coding systems. If it is nil, it means that we have not
6360 yet found any coding systems.
6361
6362 WORK_TABLE is a copy of the char-table Vchar_coding_system_table. An
6363 element of WORK_TABLE is set to t once the element is looked up.
6364
6365 If a non-ASCII single byte char is found, set
6366 *single_byte_char_found to 1. */
6367
6368 static Lisp_Object
6369 find_safe_codings (p, pend, safe_codings, work_table, single_byte_char_found)
6370 unsigned char *p, *pend;
6371 Lisp_Object safe_codings, work_table;
6372 int *single_byte_char_found;
6373 {
6374 int c, len, idx;
6375 Lisp_Object val;
6376
6377 while (p < pend)
6378 {
6379 c = STRING_CHAR_AND_LENGTH (p, pend - p, len);
6380 p += len;
6381 if (ASCII_BYTE_P (c))
6382 /* We can ignore ASCII characters here. */
6383 continue;
6384 if (SINGLE_BYTE_CHAR_P (c))
6385 *single_byte_char_found = 1;
6386 if (NILP (safe_codings))
6387 continue;
6388 /* Check the safe coding systems for C. */
6389 val = char_table_ref_and_index (work_table, c, &idx);
6390 if (EQ (val, Qt))
6391 /* This element was already checked. Ignore it. */
6392 continue;
6393 /* Remember that we checked this element. */
6394 CHAR_TABLE_SET (work_table, make_number (idx), Qt);
6395
6396 /* If there are some safe coding systems for C and we have
6397 already found the other set of coding systems for the
6398 different characters, get the intersection of them. */
6399 if (!EQ (safe_codings, Qt) && !NILP (val))
6400 val = intersection (safe_codings, val);
6401 safe_codings = val;
6402 }
6403 return safe_codings;
6404 }
6405
6406
6407 /* Return a list of coding systems that safely encode the text between
6408 START and END. If the text contains only ASCII or is unibyte,
6409 return t. */
6410
6411 DEFUN ("find-coding-systems-region-internal",
6412 Ffind_coding_systems_region_internal,
6413 Sfind_coding_systems_region_internal, 2, 2, 0,
6414 doc: /* Internal use only. */)
6415 (start, end)
6416 Lisp_Object start, end;
6417 {
6418 Lisp_Object work_table, safe_codings;
6419 int non_ascii_p = 0;
6420 int single_byte_char_found = 0;
6421 const unsigned char *p1, *p1end, *p2, *p2end, *p;
6422
6423 if (STRINGP (start))
6424 {
6425 if (!STRING_MULTIBYTE (start))
6426 return Qt;
6427 p1 = SDATA (start), p1end = p1 + SBYTES (start);
6428 p2 = p2end = p1end;
6429 if (SCHARS (start) != SBYTES (start))
6430 non_ascii_p = 1;
6431 }
6432 else
6433 {
6434 int from, to, stop;
6435
6436 CHECK_NUMBER_COERCE_MARKER (start);
6437 CHECK_NUMBER_COERCE_MARKER (end);
6438 if (XINT (start) < BEG || XINT (end) > Z || XINT (start) > XINT (end))
6439 args_out_of_range (start, end);
6440 if (NILP (current_buffer->enable_multibyte_characters))
6441 return Qt;
6442 from = CHAR_TO_BYTE (XINT (start));
6443 to = CHAR_TO_BYTE (XINT (end));
6444 stop = from < GPT_BYTE && GPT_BYTE < to ? GPT_BYTE : to;
6445 p1 = BYTE_POS_ADDR (from), p1end = p1 + (stop - from);
6446 if (stop == to)
6447 p2 = p2end = p1end;
6448 else
6449 p2 = BYTE_POS_ADDR (stop), p2end = p2 + (to - stop);
6450 if (XINT (end) - XINT (start) != to - from)
6451 non_ascii_p = 1;
6452 }
6453
6454 if (!non_ascii_p)
6455 {
6456 /* We are sure that the text contains no multibyte character.
6457 Check if it contains eight-bit-graphic. */
6458 p = p1;
6459 for (p = p1; p < p1end && ASCII_BYTE_P (*p); p++);
6460 if (p == p1end)
6461 {
6462 for (p = p2; p < p2end && ASCII_BYTE_P (*p); p++);
6463 if (p == p2end)
6464 return Qt;
6465 }
6466 }
6467
6468 /* The text contains non-ASCII characters. */
6469 work_table = Fcopy_sequence (Vchar_coding_system_table);
6470 safe_codings = find_safe_codings (p1, p1end, Qt, work_table,
6471 &single_byte_char_found);
6472 if (p2 < p2end)
6473 safe_codings = find_safe_codings (p2, p2end, safe_codings, work_table,
6474 &single_byte_char_found);
6475
6476 if (EQ (safe_codings, Qt))
6477 ; /* Nothing to be done. */
6478 else if (!single_byte_char_found)
6479 {
6480 /* Append generic coding systems. */
6481 Lisp_Object args[2];
6482 args[0] = safe_codings;
6483 args[1] = Fchar_table_extra_slot (Vchar_coding_system_table,
6484 make_number (0));
6485 safe_codings = Fappend (2, args);
6486 }
6487 else
6488 safe_codings = Fcons (Qraw_text,
6489 Fcons (Qemacs_mule,
6490 Fcons (Qno_conversion, safe_codings)));
6491 return safe_codings;
6492 }
6493
6494
6495 Lisp_Object
6496 code_convert_region1 (start, end, coding_system, encodep)
6497 Lisp_Object start, end, coding_system;
6498 int encodep;
6499 {
6500 struct coding_system coding;
6501 int from, to;
6502
6503 CHECK_NUMBER_COERCE_MARKER (start);
6504 CHECK_NUMBER_COERCE_MARKER (end);
6505 CHECK_SYMBOL (coding_system);
6506
6507 validate_region (&start, &end);
6508 from = XFASTINT (start);
6509 to = XFASTINT (end);
6510
6511 if (NILP (coding_system))
6512 return make_number (to - from);
6513
6514 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0)
6515 error ("Invalid coding system: %s", SDATA (SYMBOL_NAME (coding_system)));
6516
6517 coding.mode |= CODING_MODE_LAST_BLOCK;
6518 coding.src_multibyte = coding.dst_multibyte
6519 = !NILP (current_buffer->enable_multibyte_characters);
6520 code_convert_region (from, CHAR_TO_BYTE (from), to, CHAR_TO_BYTE (to),
6521 &coding, encodep, 1);
6522 Vlast_coding_system_used = coding.symbol;
6523 return make_number (coding.produced_char);
6524 }
6525
6526 DEFUN ("decode-coding-region", Fdecode_coding_region, Sdecode_coding_region,
6527 3, 3, "r\nzCoding system: ",
6528 doc: /* Decode the current region from the specified coding system.
6529 When called from a program, takes three arguments:
6530 START, END, and CODING-SYSTEM. START and END are buffer positions.
6531 This function sets `last-coding-system-used' to the precise coding system
6532 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
6533 not fully specified.)
6534 It returns the length of the decoded text. */)
6535 (start, end, coding_system)
6536 Lisp_Object start, end, coding_system;
6537 {
6538 return code_convert_region1 (start, end, coding_system, 0);
6539 }
6540
6541 DEFUN ("encode-coding-region", Fencode_coding_region, Sencode_coding_region,
6542 3, 3, "r\nzCoding system: ",
6543 doc: /* Encode the current region into the specified coding system.
6544 When called from a program, takes three arguments:
6545 START, END, and CODING-SYSTEM. START and END are buffer positions.
6546 This function sets `last-coding-system-used' to the precise coding system
6547 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
6548 not fully specified.)
6549 It returns the length of the encoded text. */)
6550 (start, end, coding_system)
6551 Lisp_Object start, end, coding_system;
6552 {
6553 return code_convert_region1 (start, end, coding_system, 1);
6554 }
6555
6556 Lisp_Object
6557 code_convert_string1 (string, coding_system, nocopy, encodep)
6558 Lisp_Object string, coding_system, nocopy;
6559 int encodep;
6560 {
6561 struct coding_system coding;
6562
6563 CHECK_STRING (string);
6564 CHECK_SYMBOL (coding_system);
6565
6566 if (NILP (coding_system))
6567 return (NILP (nocopy) ? Fcopy_sequence (string) : string);
6568
6569 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0)
6570 error ("Invalid coding system: %s", SDATA (SYMBOL_NAME (coding_system)));
6571
6572 coding.mode |= CODING_MODE_LAST_BLOCK;
6573 string = (encodep
6574 ? encode_coding_string (string, &coding, !NILP (nocopy))
6575 : decode_coding_string (string, &coding, !NILP (nocopy)));
6576 Vlast_coding_system_used = coding.symbol;
6577
6578 return string;
6579 }
6580
6581 DEFUN ("decode-coding-string", Fdecode_coding_string, Sdecode_coding_string,
6582 2, 3, 0,
6583 doc: /* Decode STRING which is encoded in CODING-SYSTEM, and return the result.
6584 Optional arg NOCOPY non-nil means it is OK to return STRING itself
6585 if the decoding operation is trivial.
6586 This function sets `last-coding-system-used' to the precise coding system
6587 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
6588 not fully specified.) */)
6589 (string, coding_system, nocopy)
6590 Lisp_Object string, coding_system, nocopy;
6591 {
6592 return code_convert_string1 (string, coding_system, nocopy, 0);
6593 }
6594
6595 DEFUN ("encode-coding-string", Fencode_coding_string, Sencode_coding_string,
6596 2, 3, 0,
6597 doc: /* Encode STRING to CODING-SYSTEM, and return the result.
6598 Optional arg NOCOPY non-nil means it is OK to return STRING itself
6599 if the encoding operation is trivial.
6600 This function sets `last-coding-system-used' to the precise coding system
6601 used (which may be different from CODING-SYSTEM if CODING-SYSTEM is
6602 not fully specified.) */)
6603 (string, coding_system, nocopy)
6604 Lisp_Object string, coding_system, nocopy;
6605 {
6606 return code_convert_string1 (string, coding_system, nocopy, 1);
6607 }
6608
6609 /* Encode or decode STRING according to CODING_SYSTEM.
6610 Do not set Vlast_coding_system_used.
6611
6612 This function is called only from macros DECODE_FILE and
6613 ENCODE_FILE, thus we ignore character composition. */
6614
6615 Lisp_Object
6616 code_convert_string_norecord (string, coding_system, encodep)
6617 Lisp_Object string, coding_system;
6618 int encodep;
6619 {
6620 struct coding_system coding;
6621
6622 CHECK_STRING (string);
6623 CHECK_SYMBOL (coding_system);
6624
6625 if (NILP (coding_system))
6626 return string;
6627
6628 if (setup_coding_system (Fcheck_coding_system (coding_system), &coding) < 0)
6629 error ("Invalid coding system: %s", SDATA (SYMBOL_NAME (coding_system)));
6630
6631 coding.composing = COMPOSITION_DISABLED;
6632 coding.mode |= CODING_MODE_LAST_BLOCK;
6633 return (encodep
6634 ? encode_coding_string (string, &coding, 1)
6635 : decode_coding_string (string, &coding, 1));
6636 }
6637 \f
6638 DEFUN ("decode-sjis-char", Fdecode_sjis_char, Sdecode_sjis_char, 1, 1, 0,
6639 doc: /* Decode a Japanese character which has CODE in shift_jis encoding.
6640 Return the corresponding character. */)
6641 (code)
6642 Lisp_Object code;
6643 {
6644 unsigned char c1, c2, s1, s2;
6645 Lisp_Object val;
6646
6647 CHECK_NUMBER (code);
6648 s1 = (XFASTINT (code)) >> 8, s2 = (XFASTINT (code)) & 0xFF;
6649 if (s1 == 0)
6650 {
6651 if (s2 < 0x80)
6652 XSETFASTINT (val, s2);
6653 else if (s2 >= 0xA0 || s2 <= 0xDF)
6654 XSETFASTINT (val, MAKE_CHAR (charset_katakana_jisx0201, s2, 0));
6655 else
6656 error ("Invalid Shift JIS code: %x", XFASTINT (code));
6657 }
6658 else
6659 {
6660 if ((s1 < 0x80 || (s1 > 0x9F && s1 < 0xE0) || s1 > 0xEF)
6661 || (s2 < 0x40 || s2 == 0x7F || s2 > 0xFC))
6662 error ("Invalid Shift JIS code: %x", XFASTINT (code));
6663 DECODE_SJIS (s1, s2, c1, c2);
6664 XSETFASTINT (val, MAKE_CHAR (charset_jisx0208, c1, c2));
6665 }
6666 return val;
6667 }
6668
6669 DEFUN ("encode-sjis-char", Fencode_sjis_char, Sencode_sjis_char, 1, 1, 0,
6670 doc: /* Encode a Japanese character CHAR to shift_jis encoding.
6671 Return the corresponding code in SJIS. */)
6672 (ch)
6673 Lisp_Object ch;
6674 {
6675 int charset, c1, c2, s1, s2;
6676 Lisp_Object val;
6677
6678 CHECK_NUMBER (ch);
6679 SPLIT_CHAR (XFASTINT (ch), charset, c1, c2);
6680 if (charset == CHARSET_ASCII)
6681 {
6682 val = ch;
6683 }
6684 else if (charset == charset_jisx0208
6685 && c1 > 0x20 && c1 < 0x7F && c2 > 0x20 && c2 < 0x7F)
6686 {
6687 ENCODE_SJIS (c1, c2, s1, s2);
6688 XSETFASTINT (val, (s1 << 8) | s2);
6689 }
6690 else if (charset == charset_katakana_jisx0201
6691 && c1 > 0x20 && c2 < 0xE0)
6692 {
6693 XSETFASTINT (val, c1 | 0x80);
6694 }
6695 else
6696 error ("Can't encode to shift_jis: %d", XFASTINT (ch));
6697 return val;
6698 }
6699
6700 DEFUN ("decode-big5-char", Fdecode_big5_char, Sdecode_big5_char, 1, 1, 0,
6701 doc: /* Decode a Big5 character which has CODE in BIG5 coding system.
6702 Return the corresponding character. */)
6703 (code)
6704 Lisp_Object code;
6705 {
6706 int charset;
6707 unsigned char b1, b2, c1, c2;
6708 Lisp_Object val;
6709
6710 CHECK_NUMBER (code);
6711 b1 = (XFASTINT (code)) >> 8, b2 = (XFASTINT (code)) & 0xFF;
6712 if (b1 == 0)
6713 {
6714 if (b2 >= 0x80)
6715 error ("Invalid BIG5 code: %x", XFASTINT (code));
6716 val = code;
6717 }
6718 else
6719 {
6720 if ((b1 < 0xA1 || b1 > 0xFE)
6721 || (b2 < 0x40 || (b2 > 0x7E && b2 < 0xA1) || b2 > 0xFE))
6722 error ("Invalid BIG5 code: %x", XFASTINT (code));
6723 DECODE_BIG5 (b1, b2, charset, c1, c2);
6724 XSETFASTINT (val, MAKE_CHAR (charset, c1, c2));
6725 }
6726 return val;
6727 }
6728
6729 DEFUN ("encode-big5-char", Fencode_big5_char, Sencode_big5_char, 1, 1, 0,
6730 doc: /* Encode the Big5 character CHAR to BIG5 coding system.
6731 Return the corresponding character code in Big5. */)
6732 (ch)
6733 Lisp_Object ch;
6734 {
6735 int charset, c1, c2, b1, b2;
6736 Lisp_Object val;
6737
6738 CHECK_NUMBER (ch);
6739 SPLIT_CHAR (XFASTINT (ch), charset, c1, c2);
6740 if (charset == CHARSET_ASCII)
6741 {
6742 val = ch;
6743 }
6744 else if ((charset == charset_big5_1
6745 && (XFASTINT (ch) >= 0x250a1 && XFASTINT (ch) <= 0x271ec))
6746 || (charset == charset_big5_2
6747 && XFASTINT (ch) >= 0x290a1 && XFASTINT (ch) <= 0x2bdb2))
6748 {
6749 ENCODE_BIG5 (charset, c1, c2, b1, b2);
6750 XSETFASTINT (val, (b1 << 8) | b2);
6751 }
6752 else
6753 error ("Can't encode to Big5: %d", XFASTINT (ch));
6754 return val;
6755 }
6756 \f
6757 DEFUN ("set-terminal-coding-system-internal",
6758 Fset_terminal_coding_system_internal,
6759 Sset_terminal_coding_system_internal, 1, 1, 0,
6760 doc: /* Internal use only. */)
6761 (coding_system)
6762 Lisp_Object coding_system;
6763 {
6764 CHECK_SYMBOL (coding_system);
6765 setup_coding_system (Fcheck_coding_system (coding_system), &terminal_coding);
6766 /* We had better not send unsafe characters to terminal. */
6767 terminal_coding.flags |= CODING_FLAG_ISO_SAFE;
6768 /* Character composition should be disabled. */
6769 terminal_coding.composing = COMPOSITION_DISABLED;
6770 /* Error notification should be suppressed. */
6771 terminal_coding.suppress_error = 1;
6772 terminal_coding.src_multibyte = 1;
6773 terminal_coding.dst_multibyte = 0;
6774 return Qnil;
6775 }
6776
6777 DEFUN ("set-safe-terminal-coding-system-internal",
6778 Fset_safe_terminal_coding_system_internal,
6779 Sset_safe_terminal_coding_system_internal, 1, 1, 0,
6780 doc: /* Internal use only. */)
6781 (coding_system)
6782 Lisp_Object coding_system;
6783 {
6784 CHECK_SYMBOL (coding_system);
6785 setup_coding_system (Fcheck_coding_system (coding_system),
6786 &safe_terminal_coding);
6787 /* Character composition should be disabled. */
6788 safe_terminal_coding.composing = COMPOSITION_DISABLED;
6789 /* Error notification should be suppressed. */
6790 terminal_coding.suppress_error = 1;
6791 safe_terminal_coding.src_multibyte = 1;
6792 safe_terminal_coding.dst_multibyte = 0;
6793 return Qnil;
6794 }
6795
6796 DEFUN ("terminal-coding-system",
6797 Fterminal_coding_system, Sterminal_coding_system, 0, 0, 0,
6798 doc: /* Return coding system specified for terminal output. */)
6799 ()
6800 {
6801 return terminal_coding.symbol;
6802 }
6803
6804 DEFUN ("set-keyboard-coding-system-internal",
6805 Fset_keyboard_coding_system_internal,
6806 Sset_keyboard_coding_system_internal, 1, 1, 0,
6807 doc: /* Internal use only. */)
6808 (coding_system)
6809 Lisp_Object coding_system;
6810 {
6811 CHECK_SYMBOL (coding_system);
6812 setup_coding_system (Fcheck_coding_system (coding_system), &keyboard_coding);
6813 /* Character composition should be disabled. */
6814 keyboard_coding.composing = COMPOSITION_DISABLED;
6815 return Qnil;
6816 }
6817
6818 DEFUN ("keyboard-coding-system",
6819 Fkeyboard_coding_system, Skeyboard_coding_system, 0, 0, 0,
6820 doc: /* Return coding system specified for decoding keyboard input. */)
6821 ()
6822 {
6823 return keyboard_coding.symbol;
6824 }
6825
6826 \f
6827 DEFUN ("find-operation-coding-system", Ffind_operation_coding_system,
6828 Sfind_operation_coding_system, 1, MANY, 0,
6829 doc: /* Choose a coding system for an operation based on the target name.
6830 The value names a pair of coding systems: (DECODING-SYSTEM . ENCODING-SYSTEM).
6831 DECODING-SYSTEM is the coding system to use for decoding
6832 \(in case OPERATION does decoding), and ENCODING-SYSTEM is the coding system
6833 for encoding (in case OPERATION does encoding).
6834
6835 The first argument OPERATION specifies an I/O primitive:
6836 For file I/O, `insert-file-contents' or `write-region'.
6837 For process I/O, `call-process', `call-process-region', or `start-process'.
6838 For network I/O, `open-network-stream'.
6839
6840 The remaining arguments should be the same arguments that were passed
6841 to the primitive. Depending on which primitive, one of those arguments
6842 is selected as the TARGET. For example, if OPERATION does file I/O,
6843 whichever argument specifies the file name is TARGET.
6844
6845 TARGET has a meaning which depends on OPERATION:
6846 For file I/O, TARGET is a file name.
6847 For process I/O, TARGET is a process name.
6848 For network I/O, TARGET is a service name or a port number
6849
6850 This function looks up what specified for TARGET in,
6851 `file-coding-system-alist', `process-coding-system-alist',
6852 or `network-coding-system-alist' depending on OPERATION.
6853 They may specify a coding system, a cons of coding systems,
6854 or a function symbol to call.
6855 In the last case, we call the function with one argument,
6856 which is a list of all the arguments given to this function.
6857
6858 usage: (find-operation-coding-system OPERATION ARGUMENTS ...) */)
6859 (nargs, args)
6860 int nargs;
6861 Lisp_Object *args;
6862 {
6863 Lisp_Object operation, target_idx, target, val;
6864 register Lisp_Object chain;
6865
6866 if (nargs < 2)
6867 error ("Too few arguments");
6868 operation = args[0];
6869 if (!SYMBOLP (operation)
6870 || !INTEGERP (target_idx = Fget (operation, Qtarget_idx)))
6871 error ("Invalid first argument");
6872 if (nargs < 1 + XINT (target_idx))
6873 error ("Too few arguments for operation: %s",
6874 SDATA (SYMBOL_NAME (operation)));
6875 target = args[XINT (target_idx) + 1];
6876 if (!(STRINGP (target)
6877 || (EQ (operation, Qopen_network_stream) && INTEGERP (target))))
6878 error ("Invalid argument %d", XINT (target_idx) + 1);
6879
6880 chain = ((EQ (operation, Qinsert_file_contents)
6881 || EQ (operation, Qwrite_region))
6882 ? Vfile_coding_system_alist
6883 : (EQ (operation, Qopen_network_stream)
6884 ? Vnetwork_coding_system_alist
6885 : Vprocess_coding_system_alist));
6886 if (NILP (chain))
6887 return Qnil;
6888
6889 for (; CONSP (chain); chain = XCDR (chain))
6890 {
6891 Lisp_Object elt;
6892 elt = XCAR (chain);
6893
6894 if (CONSP (elt)
6895 && ((STRINGP (target)
6896 && STRINGP (XCAR (elt))
6897 && fast_string_match (XCAR (elt), target) >= 0)
6898 || (INTEGERP (target) && EQ (target, XCAR (elt)))))
6899 {
6900 val = XCDR (elt);
6901 /* Here, if VAL is both a valid coding system and a valid
6902 function symbol, we return VAL as a coding system. */
6903 if (CONSP (val))
6904 return val;
6905 if (! SYMBOLP (val))
6906 return Qnil;
6907 if (! NILP (Fcoding_system_p (val)))
6908 return Fcons (val, val);
6909 if (! NILP (Ffboundp (val)))
6910 {
6911 val = call1 (val, Flist (nargs, args));
6912 if (CONSP (val))
6913 return val;
6914 if (SYMBOLP (val) && ! NILP (Fcoding_system_p (val)))
6915 return Fcons (val, val);
6916 }
6917 return Qnil;
6918 }
6919 }
6920 return Qnil;
6921 }
6922
6923 DEFUN ("update-coding-systems-internal", Fupdate_coding_systems_internal,
6924 Supdate_coding_systems_internal, 0, 0, 0,
6925 doc: /* Update internal database for ISO2022 and CCL based coding systems.
6926 When values of any coding categories are changed, you must
6927 call this function. */)
6928 ()
6929 {
6930 int i;
6931
6932 for (i = CODING_CATEGORY_IDX_EMACS_MULE; i < CODING_CATEGORY_IDX_MAX; i++)
6933 {
6934 Lisp_Object val;
6935
6936 val = SYMBOL_VALUE (XVECTOR (Vcoding_category_table)->contents[i]);
6937 if (!NILP (val))
6938 {
6939 if (! coding_system_table[i])
6940 coding_system_table[i] = ((struct coding_system *)
6941 xmalloc (sizeof (struct coding_system)));
6942 setup_coding_system (val, coding_system_table[i]);
6943 }
6944 else if (coding_system_table[i])
6945 {
6946 xfree (coding_system_table[i]);
6947 coding_system_table[i] = NULL;
6948 }
6949 }
6950
6951 return Qnil;
6952 }
6953
6954 DEFUN ("set-coding-priority-internal", Fset_coding_priority_internal,
6955 Sset_coding_priority_internal, 0, 0, 0,
6956 doc: /* Update internal database for the current value of `coding-category-list'.
6957 This function is internal use only. */)
6958 ()
6959 {
6960 int i = 0, idx;
6961 Lisp_Object val;
6962
6963 val = Vcoding_category_list;
6964
6965 while (CONSP (val) && i < CODING_CATEGORY_IDX_MAX)
6966 {
6967 if (! SYMBOLP (XCAR (val)))
6968 break;
6969 idx = XFASTINT (Fget (XCAR (val), Qcoding_category_index));
6970 if (idx >= CODING_CATEGORY_IDX_MAX)
6971 break;
6972 coding_priorities[i++] = (1 << idx);
6973 val = XCDR (val);
6974 }
6975 /* If coding-category-list is valid and contains all coding
6976 categories, `i' should be CODING_CATEGORY_IDX_MAX now. If not,
6977 the following code saves Emacs from crashing. */
6978 while (i < CODING_CATEGORY_IDX_MAX)
6979 coding_priorities[i++] = CODING_CATEGORY_MASK_RAW_TEXT;
6980
6981 return Qnil;
6982 }
6983
6984 #endif /* emacs */
6985
6986 \f
6987 /*** 9. Post-amble ***/
6988
6989 void
6990 init_coding_once ()
6991 {
6992 int i;
6993
6994 /* Emacs' internal format specific initialize routine. */
6995 for (i = 0; i <= 0x20; i++)
6996 emacs_code_class[i] = EMACS_control_code;
6997 emacs_code_class[0x0A] = EMACS_linefeed_code;
6998 emacs_code_class[0x0D] = EMACS_carriage_return_code;
6999 for (i = 0x21 ; i < 0x7F; i++)
7000 emacs_code_class[i] = EMACS_ascii_code;
7001 emacs_code_class[0x7F] = EMACS_control_code;
7002 for (i = 0x80; i < 0xFF; i++)
7003 emacs_code_class[i] = EMACS_invalid_code;
7004 emacs_code_class[LEADING_CODE_PRIVATE_11] = EMACS_leading_code_3;
7005 emacs_code_class[LEADING_CODE_PRIVATE_12] = EMACS_leading_code_3;
7006 emacs_code_class[LEADING_CODE_PRIVATE_21] = EMACS_leading_code_4;
7007 emacs_code_class[LEADING_CODE_PRIVATE_22] = EMACS_leading_code_4;
7008
7009 /* ISO2022 specific initialize routine. */
7010 for (i = 0; i < 0x20; i++)
7011 iso_code_class[i] = ISO_control_0;
7012 for (i = 0x21; i < 0x7F; i++)
7013 iso_code_class[i] = ISO_graphic_plane_0;
7014 for (i = 0x80; i < 0xA0; i++)
7015 iso_code_class[i] = ISO_control_1;
7016 for (i = 0xA1; i < 0xFF; i++)
7017 iso_code_class[i] = ISO_graphic_plane_1;
7018 iso_code_class[0x20] = iso_code_class[0x7F] = ISO_0x20_or_0x7F;
7019 iso_code_class[0xA0] = iso_code_class[0xFF] = ISO_0xA0_or_0xFF;
7020 iso_code_class[ISO_CODE_CR] = ISO_carriage_return;
7021 iso_code_class[ISO_CODE_SO] = ISO_shift_out;
7022 iso_code_class[ISO_CODE_SI] = ISO_shift_in;
7023 iso_code_class[ISO_CODE_SS2_7] = ISO_single_shift_2_7;
7024 iso_code_class[ISO_CODE_ESC] = ISO_escape;
7025 iso_code_class[ISO_CODE_SS2] = ISO_single_shift_2;
7026 iso_code_class[ISO_CODE_SS3] = ISO_single_shift_3;
7027 iso_code_class[ISO_CODE_CSI] = ISO_control_sequence_introducer;
7028
7029 setup_coding_system (Qnil, &keyboard_coding);
7030 setup_coding_system (Qnil, &terminal_coding);
7031 setup_coding_system (Qnil, &safe_terminal_coding);
7032 setup_coding_system (Qnil, &default_buffer_file_coding);
7033
7034 bzero (coding_system_table, sizeof coding_system_table);
7035
7036 bzero (ascii_skip_code, sizeof ascii_skip_code);
7037 for (i = 0; i < 128; i++)
7038 ascii_skip_code[i] = 1;
7039
7040 #if defined (MSDOS) || defined (WINDOWSNT)
7041 system_eol_type = CODING_EOL_CRLF;
7042 #else
7043 system_eol_type = CODING_EOL_LF;
7044 #endif
7045
7046 inhibit_pre_post_conversion = 0;
7047 }
7048
7049 #ifdef emacs
7050
7051 void
7052 syms_of_coding ()
7053 {
7054 Qtarget_idx = intern ("target-idx");
7055 staticpro (&Qtarget_idx);
7056
7057 Qcoding_system_history = intern ("coding-system-history");
7058 staticpro (&Qcoding_system_history);
7059 Fset (Qcoding_system_history, Qnil);
7060
7061 /* Target FILENAME is the first argument. */
7062 Fput (Qinsert_file_contents, Qtarget_idx, make_number (0));
7063 /* Target FILENAME is the third argument. */
7064 Fput (Qwrite_region, Qtarget_idx, make_number (2));
7065
7066 Qcall_process = intern ("call-process");
7067 staticpro (&Qcall_process);
7068 /* Target PROGRAM is the first argument. */
7069 Fput (Qcall_process, Qtarget_idx, make_number (0));
7070
7071 Qcall_process_region = intern ("call-process-region");
7072 staticpro (&Qcall_process_region);
7073 /* Target PROGRAM is the third argument. */
7074 Fput (Qcall_process_region, Qtarget_idx, make_number (2));
7075
7076 Qstart_process = intern ("start-process");
7077 staticpro (&Qstart_process);
7078 /* Target PROGRAM is the third argument. */
7079 Fput (Qstart_process, Qtarget_idx, make_number (2));
7080
7081 Qopen_network_stream = intern ("open-network-stream");
7082 staticpro (&Qopen_network_stream);
7083 /* Target SERVICE is the fourth argument. */
7084 Fput (Qopen_network_stream, Qtarget_idx, make_number (3));
7085
7086 Qcoding_system = intern ("coding-system");
7087 staticpro (&Qcoding_system);
7088
7089 Qeol_type = intern ("eol-type");
7090 staticpro (&Qeol_type);
7091
7092 Qbuffer_file_coding_system = intern ("buffer-file-coding-system");
7093 staticpro (&Qbuffer_file_coding_system);
7094
7095 Qpost_read_conversion = intern ("post-read-conversion");
7096 staticpro (&Qpost_read_conversion);
7097
7098 Qpre_write_conversion = intern ("pre-write-conversion");
7099 staticpro (&Qpre_write_conversion);
7100
7101 Qno_conversion = intern ("no-conversion");
7102 staticpro (&Qno_conversion);
7103
7104 Qundecided = intern ("undecided");
7105 staticpro (&Qundecided);
7106
7107 Qcoding_system_p = intern ("coding-system-p");
7108 staticpro (&Qcoding_system_p);
7109
7110 Qcoding_system_error = intern ("coding-system-error");
7111 staticpro (&Qcoding_system_error);
7112
7113 Fput (Qcoding_system_error, Qerror_conditions,
7114 Fcons (Qcoding_system_error, Fcons (Qerror, Qnil)));
7115 Fput (Qcoding_system_error, Qerror_message,
7116 build_string ("Invalid coding system"));
7117
7118 Qcoding_category = intern ("coding-category");
7119 staticpro (&Qcoding_category);
7120 Qcoding_category_index = intern ("coding-category-index");
7121 staticpro (&Qcoding_category_index);
7122
7123 Vcoding_category_table
7124 = Fmake_vector (make_number (CODING_CATEGORY_IDX_MAX), Qnil);
7125 staticpro (&Vcoding_category_table);
7126 {
7127 int i;
7128 for (i = 0; i < CODING_CATEGORY_IDX_MAX; i++)
7129 {
7130 XVECTOR (Vcoding_category_table)->contents[i]
7131 = intern (coding_category_name[i]);
7132 Fput (XVECTOR (Vcoding_category_table)->contents[i],
7133 Qcoding_category_index, make_number (i));
7134 }
7135 }
7136
7137 Qtranslation_table = intern ("translation-table");
7138 staticpro (&Qtranslation_table);
7139 Fput (Qtranslation_table, Qchar_table_extra_slots, make_number (1));
7140
7141 Qtranslation_table_id = intern ("translation-table-id");
7142 staticpro (&Qtranslation_table_id);
7143
7144 Qtranslation_table_for_decode = intern ("translation-table-for-decode");
7145 staticpro (&Qtranslation_table_for_decode);
7146
7147 Qtranslation_table_for_encode = intern ("translation-table-for-encode");
7148 staticpro (&Qtranslation_table_for_encode);
7149
7150 Qsafe_chars = intern ("safe-chars");
7151 staticpro (&Qsafe_chars);
7152
7153 Qchar_coding_system = intern ("char-coding-system");
7154 staticpro (&Qchar_coding_system);
7155
7156 /* Intern this now in case it isn't already done.
7157 Setting this variable twice is harmless.
7158 But don't staticpro it here--that is done in alloc.c. */
7159 Qchar_table_extra_slots = intern ("char-table-extra-slots");
7160 Fput (Qsafe_chars, Qchar_table_extra_slots, make_number (0));
7161 Fput (Qchar_coding_system, Qchar_table_extra_slots, make_number (2));
7162
7163 Qvalid_codes = intern ("valid-codes");
7164 staticpro (&Qvalid_codes);
7165
7166 Qemacs_mule = intern ("emacs-mule");
7167 staticpro (&Qemacs_mule);
7168
7169 Qraw_text = intern ("raw-text");
7170 staticpro (&Qraw_text);
7171
7172 defsubr (&Scoding_system_p);
7173 defsubr (&Sread_coding_system);
7174 defsubr (&Sread_non_nil_coding_system);
7175 defsubr (&Scheck_coding_system);
7176 defsubr (&Sdetect_coding_region);
7177 defsubr (&Sdetect_coding_string);
7178 defsubr (&Sfind_coding_systems_region_internal);
7179 defsubr (&Sdecode_coding_region);
7180 defsubr (&Sencode_coding_region);
7181 defsubr (&Sdecode_coding_string);
7182 defsubr (&Sencode_coding_string);
7183 defsubr (&Sdecode_sjis_char);
7184 defsubr (&Sencode_sjis_char);
7185 defsubr (&Sdecode_big5_char);
7186 defsubr (&Sencode_big5_char);
7187 defsubr (&Sset_terminal_coding_system_internal);
7188 defsubr (&Sset_safe_terminal_coding_system_internal);
7189 defsubr (&Sterminal_coding_system);
7190 defsubr (&Sset_keyboard_coding_system_internal);
7191 defsubr (&Skeyboard_coding_system);
7192 defsubr (&Sfind_operation_coding_system);
7193 defsubr (&Supdate_coding_systems_internal);
7194 defsubr (&Sset_coding_priority_internal);
7195
7196 DEFVAR_LISP ("coding-system-list", &Vcoding_system_list,
7197 doc: /* List of coding systems.
7198
7199 Do not alter the value of this variable manually. This variable should be
7200 updated by the functions `make-coding-system' and
7201 `define-coding-system-alias'. */);
7202 Vcoding_system_list = Qnil;
7203
7204 DEFVAR_LISP ("coding-system-alist", &Vcoding_system_alist,
7205 doc: /* Alist of coding system names.
7206 Each element is one element list of coding system name.
7207 This variable is given to `completing-read' as TABLE argument.
7208
7209 Do not alter the value of this variable manually. This variable should be
7210 updated by the functions `make-coding-system' and
7211 `define-coding-system-alias'. */);
7212 Vcoding_system_alist = Qnil;
7213
7214 DEFVAR_LISP ("coding-category-list", &Vcoding_category_list,
7215 doc: /* List of coding-categories (symbols) ordered by priority.
7216
7217 On detecting a coding system, Emacs tries code detection algorithms
7218 associated with each coding-category one by one in this order. When
7219 one algorithm agrees with a byte sequence of source text, the coding
7220 system bound to the corresponding coding-category is selected. */);
7221 {
7222 int i;
7223
7224 Vcoding_category_list = Qnil;
7225 for (i = CODING_CATEGORY_IDX_MAX - 1; i >= 0; i--)
7226 Vcoding_category_list
7227 = Fcons (XVECTOR (Vcoding_category_table)->contents[i],
7228 Vcoding_category_list);
7229 }
7230
7231 DEFVAR_LISP ("coding-system-for-read", &Vcoding_system_for_read,
7232 doc: /* Specify the coding system for read operations.
7233 It is useful to bind this variable with `let', but do not set it globally.
7234 If the value is a coding system, it is used for decoding on read operation.
7235 If not, an appropriate element is used from one of the coding system alists:
7236 There are three such tables, `file-coding-system-alist',
7237 `process-coding-system-alist', and `network-coding-system-alist'. */);
7238 Vcoding_system_for_read = Qnil;
7239
7240 DEFVAR_LISP ("coding-system-for-write", &Vcoding_system_for_write,
7241 doc: /* Specify the coding system for write operations.
7242 Programs bind this variable with `let', but you should not set it globally.
7243 If the value is a coding system, it is used for encoding of output,
7244 when writing it to a file and when sending it to a file or subprocess.
7245
7246 If this does not specify a coding system, an appropriate element
7247 is used from one of the coding system alists:
7248 There are three such tables, `file-coding-system-alist',
7249 `process-coding-system-alist', and `network-coding-system-alist'.
7250 For output to files, if the above procedure does not specify a coding system,
7251 the value of `buffer-file-coding-system' is used. */);
7252 Vcoding_system_for_write = Qnil;
7253
7254 DEFVAR_LISP ("last-coding-system-used", &Vlast_coding_system_used,
7255 doc: /* Coding system used in the latest file or process I/O. */);
7256 Vlast_coding_system_used = Qnil;
7257
7258 DEFVAR_BOOL ("inhibit-eol-conversion", &inhibit_eol_conversion,
7259 doc: /* *Non-nil means always inhibit code conversion of end-of-line format.
7260 See info node `Coding Systems' and info node `Text and Binary' concerning
7261 such conversion. */);
7262 inhibit_eol_conversion = 0;
7263
7264 DEFVAR_BOOL ("inherit-process-coding-system", &inherit_process_coding_system,
7265 doc: /* Non-nil means process buffer inherits coding system of process output.
7266 Bind it to t if the process output is to be treated as if it were a file
7267 read from some filesystem. */);
7268 inherit_process_coding_system = 0;
7269
7270 DEFVAR_LISP ("file-coding-system-alist", &Vfile_coding_system_alist,
7271 doc: /* Alist to decide a coding system to use for a file I/O operation.
7272 The format is ((PATTERN . VAL) ...),
7273 where PATTERN is a regular expression matching a file name,
7274 VAL is a coding system, a cons of coding systems, or a function symbol.
7275 If VAL is a coding system, it is used for both decoding and encoding
7276 the file contents.
7277 If VAL is a cons of coding systems, the car part is used for decoding,
7278 and the cdr part is used for encoding.
7279 If VAL is a function symbol, the function must return a coding system
7280 or a cons of coding systems which are used as above. The function gets
7281 the arguments with which `find-operation-coding-system' was called.
7282
7283 See also the function `find-operation-coding-system'
7284 and the variable `auto-coding-alist'. */);
7285 Vfile_coding_system_alist = Qnil;
7286
7287 DEFVAR_LISP ("process-coding-system-alist", &Vprocess_coding_system_alist,
7288 doc: /* Alist to decide a coding system to use for a process I/O operation.
7289 The format is ((PATTERN . VAL) ...),
7290 where PATTERN is a regular expression matching a program name,
7291 VAL is a coding system, a cons of coding systems, or a function symbol.
7292 If VAL is a coding system, it is used for both decoding what received
7293 from the program and encoding what sent to the program.
7294 If VAL is a cons of coding systems, the car part is used for decoding,
7295 and the cdr part is used for encoding.
7296 If VAL is a function symbol, the function must return a coding system
7297 or a cons of coding systems which are used as above.
7298
7299 See also the function `find-operation-coding-system'. */);
7300 Vprocess_coding_system_alist = Qnil;
7301
7302 DEFVAR_LISP ("network-coding-system-alist", &Vnetwork_coding_system_alist,
7303 doc: /* Alist to decide a coding system to use for a network I/O operation.
7304 The format is ((PATTERN . VAL) ...),
7305 where PATTERN is a regular expression matching a network service name
7306 or is a port number to connect to,
7307 VAL is a coding system, a cons of coding systems, or a function symbol.
7308 If VAL is a coding system, it is used for both decoding what received
7309 from the network stream and encoding what sent to the network stream.
7310 If VAL is a cons of coding systems, the car part is used for decoding,
7311 and the cdr part is used for encoding.
7312 If VAL is a function symbol, the function must return a coding system
7313 or a cons of coding systems which are used as above.
7314
7315 See also the function `find-operation-coding-system'. */);
7316 Vnetwork_coding_system_alist = Qnil;
7317
7318 DEFVAR_LISP ("locale-coding-system", &Vlocale_coding_system,
7319 doc: /* Coding system to use with system messages.
7320 Also used for decoding keyboard input on X Window system. */);
7321 Vlocale_coding_system = Qnil;
7322
7323 /* The eol mnemonics are reset in startup.el system-dependently. */
7324 DEFVAR_LISP ("eol-mnemonic-unix", &eol_mnemonic_unix,
7325 doc: /* *String displayed in mode line for UNIX-like (LF) end-of-line format. */);
7326 eol_mnemonic_unix = build_string (":");
7327
7328 DEFVAR_LISP ("eol-mnemonic-dos", &eol_mnemonic_dos,
7329 doc: /* *String displayed in mode line for DOS-like (CRLF) end-of-line format. */);
7330 eol_mnemonic_dos = build_string ("\\");
7331
7332 DEFVAR_LISP ("eol-mnemonic-mac", &eol_mnemonic_mac,
7333 doc: /* *String displayed in mode line for MAC-like (CR) end-of-line format. */);
7334 eol_mnemonic_mac = build_string ("/");
7335
7336 DEFVAR_LISP ("eol-mnemonic-undecided", &eol_mnemonic_undecided,
7337 doc: /* *String displayed in mode line when end-of-line format is not yet determined. */);
7338 eol_mnemonic_undecided = build_string (":");
7339
7340 DEFVAR_LISP ("enable-character-translation", &Venable_character_translation,
7341 doc: /* *Non-nil enables character translation while encoding and decoding. */);
7342 Venable_character_translation = Qt;
7343
7344 DEFVAR_LISP ("standard-translation-table-for-decode",
7345 &Vstandard_translation_table_for_decode,
7346 doc: /* Table for translating characters while decoding. */);
7347 Vstandard_translation_table_for_decode = Qnil;
7348
7349 DEFVAR_LISP ("standard-translation-table-for-encode",
7350 &Vstandard_translation_table_for_encode,
7351 doc: /* Table for translating characters while encoding. */);
7352 Vstandard_translation_table_for_encode = Qnil;
7353
7354 DEFVAR_LISP ("charset-revision-table", &Vcharset_revision_alist,
7355 doc: /* Alist of charsets vs revision numbers.
7356 While encoding, if a charset (car part of an element) is found,
7357 designate it with the escape sequence identifying revision (cdr part of the element). */);
7358 Vcharset_revision_alist = Qnil;
7359
7360 DEFVAR_LISP ("default-process-coding-system",
7361 &Vdefault_process_coding_system,
7362 doc: /* Cons of coding systems used for process I/O by default.
7363 The car part is used for decoding a process output,
7364 the cdr part is used for encoding a text to be sent to a process. */);
7365 Vdefault_process_coding_system = Qnil;
7366
7367 DEFVAR_LISP ("latin-extra-code-table", &Vlatin_extra_code_table,
7368 doc: /* Table of extra Latin codes in the range 128..159 (inclusive).
7369 This is a vector of length 256.
7370 If Nth element is non-nil, the existence of code N in a file
7371 \(or output of subprocess) doesn't prevent it to be detected as
7372 a coding system of ISO 2022 variant which has a flag
7373 `accept-latin-extra-code' t (e.g. iso-latin-1) on reading a file
7374 or reading output of a subprocess.
7375 Only 128th through 159th elements has a meaning. */);
7376 Vlatin_extra_code_table = Fmake_vector (make_number (256), Qnil);
7377
7378 DEFVAR_LISP ("select-safe-coding-system-function",
7379 &Vselect_safe_coding_system_function,
7380 doc: /* Function to call to select safe coding system for encoding a text.
7381
7382 If set, this function is called to force a user to select a proper
7383 coding system which can encode the text in the case that a default
7384 coding system used in each operation can't encode the text.
7385
7386 The default value is `select-safe-coding-system' (which see). */);
7387 Vselect_safe_coding_system_function = Qnil;
7388
7389 DEFVAR_LISP ("char-coding-system-table", &Vchar_coding_system_table,
7390 doc: /* Char-table containing safe coding systems of each characters.
7391 Each element doesn't include such generic coding systems that can
7392 encode any characters. They are in the first extra slot. */);
7393 Vchar_coding_system_table = Fmake_char_table (Qchar_coding_system, Qnil);
7394
7395 DEFVAR_BOOL ("inhibit-iso-escape-detection",
7396 &inhibit_iso_escape_detection,
7397 doc: /* If non-nil, Emacs ignores ISO2022's escape sequence on code detection.
7398
7399 By default, on reading a file, Emacs tries to detect how the text is
7400 encoded. This code detection is sensitive to escape sequences. If
7401 the sequence is valid as ISO2022, the code is determined as one of
7402 the ISO2022 encodings, and the file is decoded by the corresponding
7403 coding system (e.g. `iso-2022-7bit').
7404
7405 However, there may be a case that you want to read escape sequences in
7406 a file as is. In such a case, you can set this variable to non-nil.
7407 Then, as the code detection ignores any escape sequences, no file is
7408 detected as encoded in some ISO2022 encoding. The result is that all
7409 escape sequences become visible in a buffer.
7410
7411 The default value is nil, and it is strongly recommended not to change
7412 it. That is because many Emacs Lisp source files that contain
7413 non-ASCII characters are encoded by the coding system `iso-2022-7bit'
7414 in Emacs's distribution, and they won't be decoded correctly on
7415 reading if you suppress escape sequence detection.
7416
7417 The other way to read escape sequences in a file without decoding is
7418 to explicitly specify some coding system that doesn't use ISO2022's
7419 escape sequence (e.g `latin-1') on reading by \\[universal-coding-system-argument]. */);
7420 inhibit_iso_escape_detection = 0;
7421 }
7422
7423 char *
7424 emacs_strerror (error_number)
7425 int error_number;
7426 {
7427 char *str;
7428
7429 synchronize_system_messages_locale ();
7430 str = strerror (error_number);
7431
7432 if (! NILP (Vlocale_coding_system))
7433 {
7434 Lisp_Object dec = code_convert_string_norecord (build_string (str),
7435 Vlocale_coding_system,
7436 0);
7437 str = (char *) SDATA (dec);
7438 }
7439
7440 return str;
7441 }
7442
7443 #endif /* emacs */
7444