]> code.delx.au - gnu-emacs/blob - src/ccl.c
(HASH_VALUE, HASH_KEY): Remove (it's in lisp.h now).
[gnu-emacs] / src / ccl.c
1 /* CCL (Code Conversion Language) interpreter.
2 Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
3 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
4 Licensed to the Free Software Foundation.
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 #ifdef emacs
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28
29 #ifdef emacs
30
31 #include "lisp.h"
32 #include "charset.h"
33 #include "ccl.h"
34 #include "coding.h"
35
36 #else /* not emacs */
37
38 #include "mulelib.h"
39
40 #endif /* not emacs */
41
42 /* This contains all code conversion map available to CCL. */
43 Lisp_Object Vcode_conversion_map_vector;
44
45 /* Alist of fontname patterns vs corresponding CCL program. */
46 Lisp_Object Vfont_ccl_encoder_alist;
47
48 /* This symbol is a property which assocates with ccl program vector.
49 Ex: (get 'ccl-big5-encoder 'ccl-program) returns ccl program vector. */
50 Lisp_Object Qccl_program;
51
52 /* These symbols are properties which associate with code conversion
53 map and their ID respectively. */
54 Lisp_Object Qcode_conversion_map;
55 Lisp_Object Qcode_conversion_map_id;
56
57 /* Symbols of ccl program have this property, a value of the property
58 is an index for Vccl_protram_table. */
59 Lisp_Object Qccl_program_idx;
60
61 /* Table of registered CCL programs. Each element is a vector of
62 NAME, CCL_PROG, and RESOLVEDP where NAME (symbol) is the name of
63 the program, CCL_PROG (vector) is the compiled code of the program,
64 RESOLVEDP (t or nil) is the flag to tell if symbols in CCL_PROG is
65 already resolved to index numbers or not. */
66 Lisp_Object Vccl_program_table;
67
68 /* Vector of registered hash tables for translation. */
69 Lisp_Object Vtranslation_hash_table_vector;
70
71 /* Return a hash table of id number ID. */
72 #define GET_HASH_TABLE(id) \
73 (XHASH_TABLE (XCDR(XVECTOR(Vtranslation_hash_table_vector)->contents[(id)])))
74
75 /* CCL (Code Conversion Language) is a simple language which has
76 operations on one input buffer, one output buffer, and 7 registers.
77 The syntax of CCL is described in `ccl.el'. Emacs Lisp function
78 `ccl-compile' compiles a CCL program and produces a CCL code which
79 is a vector of integers. The structure of this vector is as
80 follows: The 1st element: buffer-magnification, a factor for the
81 size of output buffer compared with the size of input buffer. The
82 2nd element: address of CCL code to be executed when encountered
83 with end of input stream. The 3rd and the remaining elements: CCL
84 codes. */
85
86 /* Header of CCL compiled code */
87 #define CCL_HEADER_BUF_MAG 0
88 #define CCL_HEADER_EOF 1
89 #define CCL_HEADER_MAIN 2
90
91 /* CCL code is a sequence of 28-bit non-negative integers (i.e. the
92 MSB is always 0), each contains CCL command and/or arguments in the
93 following format:
94
95 |----------------- integer (28-bit) ------------------|
96 |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
97 |--constant argument--|-register-|-register-|-command-|
98 ccccccccccccccccc RRR rrr XXXXX
99 or
100 |------- relative address -------|-register-|-command-|
101 cccccccccccccccccccc rrr XXXXX
102 or
103 |------------- constant or other args ----------------|
104 cccccccccccccccccccccccccccc
105
106 where, `cc...c' is a non-negative integer indicating constant value
107 (the left most `c' is always 0) or an absolute jump address, `RRR'
108 and `rrr' are CCL register number, `XXXXX' is one of the following
109 CCL commands. */
110
111 /* CCL commands
112
113 Each comment fields shows one or more lines for command syntax and
114 the following lines for semantics of the command. In semantics, IC
115 stands for Instruction Counter. */
116
117 #define CCL_SetRegister 0x00 /* Set register a register value:
118 1:00000000000000000RRRrrrXXXXX
119 ------------------------------
120 reg[rrr] = reg[RRR];
121 */
122
123 #define CCL_SetShortConst 0x01 /* Set register a short constant value:
124 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
125 ------------------------------
126 reg[rrr] = CCCCCCCCCCCCCCCCCCC;
127 */
128
129 #define CCL_SetConst 0x02 /* Set register a constant value:
130 1:00000000000000000000rrrXXXXX
131 2:CONSTANT
132 ------------------------------
133 reg[rrr] = CONSTANT;
134 IC++;
135 */
136
137 #define CCL_SetArray 0x03 /* Set register an element of array:
138 1:CCCCCCCCCCCCCCCCCRRRrrrXXXXX
139 2:ELEMENT[0]
140 3:ELEMENT[1]
141 ...
142 ------------------------------
143 if (0 <= reg[RRR] < CC..C)
144 reg[rrr] = ELEMENT[reg[RRR]];
145 IC += CC..C;
146 */
147
148 #define CCL_Jump 0x04 /* Jump:
149 1:A--D--D--R--E--S--S-000XXXXX
150 ------------------------------
151 IC += ADDRESS;
152 */
153
154 /* Note: If CC..C is greater than 0, the second code is omitted. */
155
156 #define CCL_JumpCond 0x05 /* Jump conditional:
157 1:A--D--D--R--E--S--S-rrrXXXXX
158 ------------------------------
159 if (!reg[rrr])
160 IC += ADDRESS;
161 */
162
163
164 #define CCL_WriteRegisterJump 0x06 /* Write register and jump:
165 1:A--D--D--R--E--S--S-rrrXXXXX
166 ------------------------------
167 write (reg[rrr]);
168 IC += ADDRESS;
169 */
170
171 #define CCL_WriteRegisterReadJump 0x07 /* Write register, read, and jump:
172 1:A--D--D--R--E--S--S-rrrXXXXX
173 2:A--D--D--R--E--S--S-rrrYYYYY
174 -----------------------------
175 write (reg[rrr]);
176 IC++;
177 read (reg[rrr]);
178 IC += ADDRESS;
179 */
180 /* Note: If read is suspended, the resumed execution starts from the
181 second code (YYYYY == CCL_ReadJump). */
182
183 #define CCL_WriteConstJump 0x08 /* Write constant and jump:
184 1:A--D--D--R--E--S--S-000XXXXX
185 2:CONST
186 ------------------------------
187 write (CONST);
188 IC += ADDRESS;
189 */
190
191 #define CCL_WriteConstReadJump 0x09 /* Write constant, read, and jump:
192 1:A--D--D--R--E--S--S-rrrXXXXX
193 2:CONST
194 3:A--D--D--R--E--S--S-rrrYYYYY
195 -----------------------------
196 write (CONST);
197 IC += 2;
198 read (reg[rrr]);
199 IC += ADDRESS;
200 */
201 /* Note: If read is suspended, the resumed execution starts from the
202 second code (YYYYY == CCL_ReadJump). */
203
204 #define CCL_WriteStringJump 0x0A /* Write string and jump:
205 1:A--D--D--R--E--S--S-000XXXXX
206 2:LENGTH
207 3:0000STRIN[0]STRIN[1]STRIN[2]
208 ...
209 ------------------------------
210 write_string (STRING, LENGTH);
211 IC += ADDRESS;
212 */
213
214 #define CCL_WriteArrayReadJump 0x0B /* Write an array element, read, and jump:
215 1:A--D--D--R--E--S--S-rrrXXXXX
216 2:LENGTH
217 3:ELEMENET[0]
218 4:ELEMENET[1]
219 ...
220 N:A--D--D--R--E--S--S-rrrYYYYY
221 ------------------------------
222 if (0 <= reg[rrr] < LENGTH)
223 write (ELEMENT[reg[rrr]]);
224 IC += LENGTH + 2; (... pointing at N+1)
225 read (reg[rrr]);
226 IC += ADDRESS;
227 */
228 /* Note: If read is suspended, the resumed execution starts from the
229 Nth code (YYYYY == CCL_ReadJump). */
230
231 #define CCL_ReadJump 0x0C /* Read and jump:
232 1:A--D--D--R--E--S--S-rrrYYYYY
233 -----------------------------
234 read (reg[rrr]);
235 IC += ADDRESS;
236 */
237
238 #define CCL_Branch 0x0D /* Jump by branch table:
239 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
240 2:A--D--D--R--E-S-S[0]000XXXXX
241 3:A--D--D--R--E-S-S[1]000XXXXX
242 ...
243 ------------------------------
244 if (0 <= reg[rrr] < CC..C)
245 IC += ADDRESS[reg[rrr]];
246 else
247 IC += ADDRESS[CC..C];
248 */
249
250 #define CCL_ReadRegister 0x0E /* Read bytes into registers:
251 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
252 2:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
253 ...
254 ------------------------------
255 while (CCC--)
256 read (reg[rrr]);
257 */
258
259 #define CCL_WriteExprConst 0x0F /* write result of expression:
260 1:00000OPERATION000RRR000XXXXX
261 2:CONSTANT
262 ------------------------------
263 write (reg[RRR] OPERATION CONSTANT);
264 IC++;
265 */
266
267 /* Note: If the Nth read is suspended, the resumed execution starts
268 from the Nth code. */
269
270 #define CCL_ReadBranch 0x10 /* Read one byte into a register,
271 and jump by branch table:
272 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
273 2:A--D--D--R--E-S-S[0]000XXXXX
274 3:A--D--D--R--E-S-S[1]000XXXXX
275 ...
276 ------------------------------
277 read (read[rrr]);
278 if (0 <= reg[rrr] < CC..C)
279 IC += ADDRESS[reg[rrr]];
280 else
281 IC += ADDRESS[CC..C];
282 */
283
284 #define CCL_WriteRegister 0x11 /* Write registers:
285 1:CCCCCCCCCCCCCCCCCCCrrrXXXXX
286 2:CCCCCCCCCCCCCCCCCCCrrrXXXXX
287 ...
288 ------------------------------
289 while (CCC--)
290 write (reg[rrr]);
291 ...
292 */
293
294 /* Note: If the Nth write is suspended, the resumed execution
295 starts from the Nth code. */
296
297 #define CCL_WriteExprRegister 0x12 /* Write result of expression
298 1:00000OPERATIONRrrRRR000XXXXX
299 ------------------------------
300 write (reg[RRR] OPERATION reg[Rrr]);
301 */
302
303 #define CCL_Call 0x13 /* Call the CCL program whose ID is
304 CC..C or cc..c.
305 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX
306 [2:00000000cccccccccccccccccccc]
307 ------------------------------
308 if (FFF)
309 call (cc..c)
310 IC++;
311 else
312 call (CC..C)
313 */
314
315 #define CCL_WriteConstString 0x14 /* Write a constant or a string:
316 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
317 [2:0000STRIN[0]STRIN[1]STRIN[2]]
318 [...]
319 -----------------------------
320 if (!rrr)
321 write (CC..C)
322 else
323 write_string (STRING, CC..C);
324 IC += (CC..C + 2) / 3;
325 */
326
327 #define CCL_WriteArray 0x15 /* Write an element of array:
328 1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
329 2:ELEMENT[0]
330 3:ELEMENT[1]
331 ...
332 ------------------------------
333 if (0 <= reg[rrr] < CC..C)
334 write (ELEMENT[reg[rrr]]);
335 IC += CC..C;
336 */
337
338 #define CCL_End 0x16 /* Terminate:
339 1:00000000000000000000000XXXXX
340 ------------------------------
341 terminate ();
342 */
343
344 /* The following two codes execute an assignment arithmetic/logical
345 operation. The form of the operation is like REG OP= OPERAND. */
346
347 #define CCL_ExprSelfConst 0x17 /* REG OP= constant:
348 1:00000OPERATION000000rrrXXXXX
349 2:CONSTANT
350 ------------------------------
351 reg[rrr] OPERATION= CONSTANT;
352 */
353
354 #define CCL_ExprSelfReg 0x18 /* REG1 OP= REG2:
355 1:00000OPERATION000RRRrrrXXXXX
356 ------------------------------
357 reg[rrr] OPERATION= reg[RRR];
358 */
359
360 /* The following codes execute an arithmetic/logical operation. The
361 form of the operation is like REG_X = REG_Y OP OPERAND2. */
362
363 #define CCL_SetExprConst 0x19 /* REG_X = REG_Y OP constant:
364 1:00000OPERATION000RRRrrrXXXXX
365 2:CONSTANT
366 ------------------------------
367 reg[rrr] = reg[RRR] OPERATION CONSTANT;
368 IC++;
369 */
370
371 #define CCL_SetExprReg 0x1A /* REG1 = REG2 OP REG3:
372 1:00000OPERATIONRrrRRRrrrXXXXX
373 ------------------------------
374 reg[rrr] = reg[RRR] OPERATION reg[Rrr];
375 */
376
377 #define CCL_JumpCondExprConst 0x1B /* Jump conditional according to
378 an operation on constant:
379 1:A--D--D--R--E--S--S-rrrXXXXX
380 2:OPERATION
381 3:CONSTANT
382 -----------------------------
383 reg[7] = reg[rrr] OPERATION CONSTANT;
384 if (!(reg[7]))
385 IC += ADDRESS;
386 else
387 IC += 2
388 */
389
390 #define CCL_JumpCondExprReg 0x1C /* Jump conditional according to
391 an operation on register:
392 1:A--D--D--R--E--S--S-rrrXXXXX
393 2:OPERATION
394 3:RRR
395 -----------------------------
396 reg[7] = reg[rrr] OPERATION reg[RRR];
397 if (!reg[7])
398 IC += ADDRESS;
399 else
400 IC += 2;
401 */
402
403 #define CCL_ReadJumpCondExprConst 0x1D /* Read and jump conditional according
404 to an operation on constant:
405 1:A--D--D--R--E--S--S-rrrXXXXX
406 2:OPERATION
407 3:CONSTANT
408 -----------------------------
409 read (reg[rrr]);
410 reg[7] = reg[rrr] OPERATION CONSTANT;
411 if (!reg[7])
412 IC += ADDRESS;
413 else
414 IC += 2;
415 */
416
417 #define CCL_ReadJumpCondExprReg 0x1E /* Read and jump conditional according
418 to an operation on register:
419 1:A--D--D--R--E--S--S-rrrXXXXX
420 2:OPERATION
421 3:RRR
422 -----------------------------
423 read (reg[rrr]);
424 reg[7] = reg[rrr] OPERATION reg[RRR];
425 if (!reg[7])
426 IC += ADDRESS;
427 else
428 IC += 2;
429 */
430
431 #define CCL_Extension 0x1F /* Extended CCL code
432 1:ExtendedCOMMNDRrrRRRrrrXXXXX
433 2:ARGUEMENT
434 3:...
435 ------------------------------
436 extended_command (rrr,RRR,Rrr,ARGS)
437 */
438
439 /*
440 Here after, Extended CCL Instructions.
441 Bit length of extended command is 14.
442 Therefore, the instruction code range is 0..16384(0x3fff).
443 */
444
445 /* Read a multibyte characeter.
446 A code point is stored into reg[rrr]. A charset ID is stored into
447 reg[RRR]. */
448
449 #define CCL_ReadMultibyteChar2 0x00 /* Read Multibyte Character
450 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
451
452 /* Write a multibyte character.
453 Write a character whose code point is reg[rrr] and the charset ID
454 is reg[RRR]. */
455
456 #define CCL_WriteMultibyteChar2 0x01 /* Write Multibyte Character
457 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
458
459 /* Translate a character whose code point is reg[rrr] and the charset
460 ID is reg[RRR] by a translation table whose ID is reg[Rrr].
461
462 A translated character is set in reg[rrr] (code point) and reg[RRR]
463 (charset ID). */
464
465 #define CCL_TranslateCharacter 0x02 /* Translate a multibyte character
466 1:ExtendedCOMMNDRrrRRRrrrXXXXX */
467
468 /* Translate a character whose code point is reg[rrr] and the charset
469 ID is reg[RRR] by a translation table whose ID is ARGUMENT.
470
471 A translated character is set in reg[rrr] (code point) and reg[RRR]
472 (charset ID). */
473
474 #define CCL_TranslateCharacterConstTbl 0x03 /* Translate a multibyte character
475 1:ExtendedCOMMNDRrrRRRrrrXXXXX
476 2:ARGUMENT(Translation Table ID)
477 */
478
479 /* Iterate looking up MAPs for reg[rrr] starting from the Nth (N =
480 reg[RRR]) MAP until some value is found.
481
482 Each MAP is a Lisp vector whose element is number, nil, t, or
483 lambda.
484 If the element is nil, ignore the map and proceed to the next map.
485 If the element is t or lambda, finish without changing reg[rrr].
486 If the element is a number, set reg[rrr] to the number and finish.
487
488 Detail of the map structure is descibed in the comment for
489 CCL_MapMultiple below. */
490
491 #define CCL_IterateMultipleMap 0x10 /* Iterate multiple maps
492 1:ExtendedCOMMNDXXXRRRrrrXXXXX
493 2:NUMBER of MAPs
494 3:MAP-ID1
495 4:MAP-ID2
496 ...
497 */
498
499 /* Map the code in reg[rrr] by MAPs starting from the Nth (N =
500 reg[RRR]) map.
501
502 MAPs are supplied in the succeeding CCL codes as follows:
503
504 When CCL program gives this nested structure of map to this command:
505 ((MAP-ID11
506 MAP-ID12
507 (MAP-ID121 MAP-ID122 MAP-ID123)
508 MAP-ID13)
509 (MAP-ID21
510 (MAP-ID211 (MAP-ID2111) MAP-ID212)
511 MAP-ID22)),
512 the compiled CCL codes has this sequence:
513 CCL_MapMultiple (CCL code of this command)
514 16 (total number of MAPs and SEPARATORs)
515 -7 (1st SEPARATOR)
516 MAP-ID11
517 MAP-ID12
518 -3 (2nd SEPARATOR)
519 MAP-ID121
520 MAP-ID122
521 MAP-ID123
522 MAP-ID13
523 -7 (3rd SEPARATOR)
524 MAP-ID21
525 -4 (4th SEPARATOR)
526 MAP-ID211
527 -1 (5th SEPARATOR)
528 MAP_ID2111
529 MAP-ID212
530 MAP-ID22
531
532 A value of each SEPARATOR follows this rule:
533 MAP-SET := SEPARATOR [(MAP-ID | MAP-SET)]+
534 SEPARATOR := -(number of MAP-IDs and SEPARATORs in the MAP-SET)
535
536 (*)....Nest level of MAP-SET must not be over than MAX_MAP_SET_LEVEL.
537
538 When some map fails to map (i.e. it doesn't have a value for
539 reg[rrr]), the mapping is treated as identity.
540
541 The mapping is iterated for all maps in each map set (set of maps
542 separated by SEPARATOR) except in the case that lambda is
543 encountered. More precisely, the mapping proceeds as below:
544
545 At first, VAL0 is set to reg[rrr], and it is translated by the
546 first map to VAL1. Then, VAL1 is translated by the next map to
547 VAL2. This mapping is iterated until the last map is used. The
548 result of the mapping is the last value of VAL?. When the mapping
549 process reached to the end of the map set, it moves to the next
550 map set. If the next does not exit, the mapping process terminates,
551 and regard the last value as a result.
552
553 But, when VALm is mapped to VALn and VALn is not a number, the
554 mapping proceed as below:
555
556 If VALn is nil, the lastest map is ignored and the mapping of VALm
557 proceed to the next map.
558
559 In VALn is t, VALm is reverted to reg[rrr] and the mapping of VALm
560 proceed to the next map.
561
562 If VALn is lambda, move to the next map set like reaching to the
563 end of the current map set.
564
565 If VALn is a symbol, call the CCL program refered by it.
566 Then, use reg[rrr] as a mapped value except for -1, -2 and -3.
567 Such special values are regarded as nil, t, and lambda respectively.
568
569 Each map is a Lisp vector of the following format (a) or (b):
570 (a)......[STARTPOINT VAL1 VAL2 ...]
571 (b)......[t VAL STARTPOINT ENDPOINT],
572 where
573 STARTPOINT is an offset to be used for indexing a map,
574 ENDPOINT is a maximum index number of a map,
575 VAL and VALn is a number, nil, t, or lambda.
576
577 Valid index range of a map of type (a) is:
578 STARTPOINT <= index < STARTPOINT + map_size - 1
579 Valid index range of a map of type (b) is:
580 STARTPOINT <= index < ENDPOINT */
581
582 #define CCL_MapMultiple 0x11 /* Mapping by multiple code conversion maps
583 1:ExtendedCOMMNDXXXRRRrrrXXXXX
584 2:N-2
585 3:SEPARATOR_1 (< 0)
586 4:MAP-ID_1
587 5:MAP-ID_2
588 ...
589 M:SEPARATOR_x (< 0)
590 M+1:MAP-ID_y
591 ...
592 N:SEPARATOR_z (< 0)
593 */
594
595 #define MAX_MAP_SET_LEVEL 30
596
597 typedef struct
598 {
599 int rest_length;
600 int orig_val;
601 } tr_stack;
602
603 static tr_stack mapping_stack[MAX_MAP_SET_LEVEL];
604 static tr_stack *mapping_stack_pointer;
605
606 /* If this variable is non-zero, it indicates the stack_idx
607 of immediately called by CCL_MapMultiple. */
608 static int stack_idx_of_map_multiple;
609
610 #define PUSH_MAPPING_STACK(restlen, orig) \
611 do \
612 { \
613 mapping_stack_pointer->rest_length = (restlen); \
614 mapping_stack_pointer->orig_val = (orig); \
615 mapping_stack_pointer++; \
616 } \
617 while (0)
618
619 #define POP_MAPPING_STACK(restlen, orig) \
620 do \
621 { \
622 mapping_stack_pointer--; \
623 (restlen) = mapping_stack_pointer->rest_length; \
624 (orig) = mapping_stack_pointer->orig_val; \
625 } \
626 while (0)
627
628 #define CCL_CALL_FOR_MAP_INSTRUCTION(symbol, ret_ic) \
629 do \
630 { \
631 struct ccl_program called_ccl; \
632 if (stack_idx >= 256 \
633 || (setup_ccl_program (&called_ccl, (symbol)) != 0)) \
634 { \
635 if (stack_idx > 0) \
636 { \
637 ccl_prog = ccl_prog_stack_struct[0].ccl_prog; \
638 ic = ccl_prog_stack_struct[0].ic; \
639 } \
640 CCL_INVALID_CMD; \
641 } \
642 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog; \
643 ccl_prog_stack_struct[stack_idx].ic = (ret_ic); \
644 stack_idx++; \
645 ccl_prog = called_ccl.prog; \
646 ic = CCL_HEADER_MAIN; \
647 goto ccl_repeat; \
648 } \
649 while (0)
650
651 #define CCL_MapSingle 0x12 /* Map by single code conversion map
652 1:ExtendedCOMMNDXXXRRRrrrXXXXX
653 2:MAP-ID
654 ------------------------------
655 Map reg[rrr] by MAP-ID.
656 If some valid mapping is found,
657 set reg[rrr] to the result,
658 else
659 set reg[RRR] to -1.
660 */
661
662 #define CCL_LookupIntConstTbl 0x13 /* Lookup multibyte character by
663 integer key. Afterwards R7 set
664 to 1 iff lookup succeeded.
665 1:ExtendedCOMMNDRrrRRRXXXXXXXX
666 2:ARGUMENT(Hash table ID) */
667
668 #define CCL_LookupCharConstTbl 0x14 /* Lookup integer by multibyte
669 character key. Afterwards R7 set
670 to 1 iff lookup succeeded.
671 1:ExtendedCOMMNDRrrRRRrrrXXXXX
672 2:ARGUMENT(Hash table ID) */
673
674 /* CCL arithmetic/logical operators. */
675 #define CCL_PLUS 0x00 /* X = Y + Z */
676 #define CCL_MINUS 0x01 /* X = Y - Z */
677 #define CCL_MUL 0x02 /* X = Y * Z */
678 #define CCL_DIV 0x03 /* X = Y / Z */
679 #define CCL_MOD 0x04 /* X = Y % Z */
680 #define CCL_AND 0x05 /* X = Y & Z */
681 #define CCL_OR 0x06 /* X = Y | Z */
682 #define CCL_XOR 0x07 /* X = Y ^ Z */
683 #define CCL_LSH 0x08 /* X = Y << Z */
684 #define CCL_RSH 0x09 /* X = Y >> Z */
685 #define CCL_LSH8 0x0A /* X = (Y << 8) | Z */
686 #define CCL_RSH8 0x0B /* X = Y >> 8, r[7] = Y & 0xFF */
687 #define CCL_DIVMOD 0x0C /* X = Y / Z, r[7] = Y % Z */
688 #define CCL_LS 0x10 /* X = (X < Y) */
689 #define CCL_GT 0x11 /* X = (X > Y) */
690 #define CCL_EQ 0x12 /* X = (X == Y) */
691 #define CCL_LE 0x13 /* X = (X <= Y) */
692 #define CCL_GE 0x14 /* X = (X >= Y) */
693 #define CCL_NE 0x15 /* X = (X != Y) */
694
695 #define CCL_DECODE_SJIS 0x16 /* X = HIGHER_BYTE (DE-SJIS (Y, Z))
696 r[7] = LOWER_BYTE (DE-SJIS (Y, Z)) */
697 #define CCL_ENCODE_SJIS 0x17 /* X = HIGHER_BYTE (SJIS (Y, Z))
698 r[7] = LOWER_BYTE (SJIS (Y, Z) */
699
700 /* Terminate CCL program successfully. */
701 #define CCL_SUCCESS \
702 do \
703 { \
704 ccl->status = CCL_STAT_SUCCESS; \
705 goto ccl_finish; \
706 } \
707 while(0)
708
709 /* Suspend CCL program because of reading from empty input buffer or
710 writing to full output buffer. When this program is resumed, the
711 same I/O command is executed. */
712 #define CCL_SUSPEND(stat) \
713 do \
714 { \
715 ic--; \
716 ccl->status = stat; \
717 goto ccl_finish; \
718 } \
719 while (0)
720
721 /* Terminate CCL program because of invalid command. Should not occur
722 in the normal case. */
723 #define CCL_INVALID_CMD \
724 do \
725 { \
726 ccl->status = CCL_STAT_INVALID_CMD; \
727 goto ccl_error_handler; \
728 } \
729 while(0)
730
731 /* Encode one character CH to multibyte form and write to the current
732 output buffer. If CH is less than 256, CH is written as is. */
733 #define CCL_WRITE_CHAR(ch) \
734 do { \
735 int bytes = SINGLE_BYTE_CHAR_P (ch) ? 1: CHAR_BYTES (ch); \
736 if (!dst) \
737 CCL_INVALID_CMD; \
738 else if (dst + bytes + extra_bytes < (dst_bytes ? dst_end : src)) \
739 { \
740 if (bytes == 1) \
741 { \
742 *dst++ = (ch); \
743 if ((ch) >= 0x80 && (ch) < 0xA0) \
744 /* We may have to convert this eight-bit char to \
745 multibyte form later. */ \
746 extra_bytes++; \
747 } \
748 else if (CHAR_VALID_P (ch, 0)) \
749 dst += CHAR_STRING (ch, dst); \
750 else \
751 CCL_INVALID_CMD; \
752 } \
753 else \
754 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
755 } while (0)
756
757 /* Encode one character CH to multibyte form and write to the current
758 output buffer. The output bytes always forms a valid multibyte
759 sequence. */
760 #define CCL_WRITE_MULTIBYTE_CHAR(ch) \
761 do { \
762 int bytes = CHAR_BYTES (ch); \
763 if (!dst) \
764 CCL_INVALID_CMD; \
765 else if (dst + bytes + extra_bytes < (dst_bytes ? dst_end : src)) \
766 { \
767 if (CHAR_VALID_P ((ch), 0)) \
768 dst += CHAR_STRING ((ch), dst); \
769 else \
770 CCL_INVALID_CMD; \
771 } \
772 else \
773 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
774 } while (0)
775
776 /* Write a string at ccl_prog[IC] of length LEN to the current output
777 buffer. */
778 #define CCL_WRITE_STRING(len) \
779 do { \
780 if (!dst) \
781 CCL_INVALID_CMD; \
782 else if (dst + len <= (dst_bytes ? dst_end : src)) \
783 for (i = 0; i < len; i++) \
784 *dst++ = ((XFASTINT (ccl_prog[ic + (i / 3)])) \
785 >> ((2 - (i % 3)) * 8)) & 0xFF; \
786 else \
787 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST); \
788 } while (0)
789
790 /* Read one byte from the current input buffer into REGth register. */
791 #define CCL_READ_CHAR(REG) \
792 do { \
793 if (!src) \
794 CCL_INVALID_CMD; \
795 else if (src < src_end) \
796 { \
797 REG = *src++; \
798 if (REG == '\n' \
799 && ccl->eol_type != CODING_EOL_LF) \
800 { \
801 /* We are encoding. */ \
802 if (ccl->eol_type == CODING_EOL_CRLF) \
803 { \
804 if (ccl->cr_consumed) \
805 ccl->cr_consumed = 0; \
806 else \
807 { \
808 ccl->cr_consumed = 1; \
809 REG = '\r'; \
810 src--; \
811 } \
812 } \
813 else \
814 REG = '\r'; \
815 } \
816 if (REG == LEADING_CODE_8_BIT_CONTROL \
817 && ccl->multibyte) \
818 REG = *src++ - 0x20; \
819 } \
820 else if (ccl->last_block) \
821 { \
822 ic = ccl->eof_ic; \
823 goto ccl_repeat; \
824 } \
825 else \
826 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC); \
827 } while (0)
828
829
830 /* Set C to the character code made from CHARSET and CODE. This is
831 like MAKE_CHAR but check the validity of CHARSET and CODE. If they
832 are not valid, set C to (CODE & 0xFF) because that is usually the
833 case that CCL_ReadMultibyteChar2 read an invalid code and it set
834 CODE to that invalid byte. */
835
836 #define CCL_MAKE_CHAR(charset, code, c) \
837 do { \
838 if (charset == CHARSET_ASCII) \
839 c = code & 0xFF; \
840 else if (CHARSET_DEFINED_P (charset) \
841 && (code & 0x7F) >= 32 \
842 && (code < 256 || ((code >> 7) & 0x7F) >= 32)) \
843 { \
844 int c1 = code & 0x7F, c2 = 0; \
845 \
846 if (code >= 256) \
847 c2 = c1, c1 = (code >> 7) & 0x7F; \
848 c = MAKE_CHAR (charset, c1, c2); \
849 } \
850 else \
851 c = code & 0xFF; \
852 } while (0)
853
854
855 /* Execute CCL code on SRC_BYTES length text at SOURCE. The resulting
856 text goes to a place pointed by DESTINATION, the length of which
857 should not exceed DST_BYTES. The bytes actually processed is
858 returned as *CONSUMED. The return value is the length of the
859 resulting text. As a side effect, the contents of CCL registers
860 are updated. If SOURCE or DESTINATION is NULL, only operations on
861 registers are permitted. */
862
863 #ifdef CCL_DEBUG
864 #define CCL_DEBUG_BACKTRACE_LEN 256
865 int ccl_backtrace_table[CCL_BACKTRACE_TABLE];
866 int ccl_backtrace_idx;
867 #endif
868
869 struct ccl_prog_stack
870 {
871 Lisp_Object *ccl_prog; /* Pointer to an array of CCL code. */
872 int ic; /* Instruction Counter. */
873 };
874
875 /* For the moment, we only support depth 256 of stack. */
876 static struct ccl_prog_stack ccl_prog_stack_struct[256];
877
878 int
879 ccl_driver (ccl, source, destination, src_bytes, dst_bytes, consumed)
880 struct ccl_program *ccl;
881 unsigned char *source, *destination;
882 int src_bytes, dst_bytes;
883 int *consumed;
884 {
885 register int *reg = ccl->reg;
886 register int ic = ccl->ic;
887 register int code = 0, field1, field2;
888 register Lisp_Object *ccl_prog = ccl->prog;
889 unsigned char *src = source, *src_end = src + src_bytes;
890 unsigned char *dst = destination, *dst_end = dst + dst_bytes;
891 int jump_address;
892 int i = 0, j, op;
893 int stack_idx = ccl->stack_idx;
894 /* Instruction counter of the current CCL code. */
895 int this_ic = 0;
896 /* CCL_WRITE_CHAR will produce 8-bit code of range 0x80..0x9F. But,
897 each of them will be converted to multibyte form of 2-byte
898 sequence. For that conversion, we remember how many more bytes
899 we must keep in DESTINATION in this variable. */
900 int extra_bytes = 0;
901
902 if (ic >= ccl->eof_ic)
903 ic = CCL_HEADER_MAIN;
904
905 if (ccl->buf_magnification == 0) /* We can't produce any bytes. */
906 dst = NULL;
907
908 /* Set mapping stack pointer. */
909 mapping_stack_pointer = mapping_stack;
910
911 #ifdef CCL_DEBUG
912 ccl_backtrace_idx = 0;
913 #endif
914
915 for (;;)
916 {
917 ccl_repeat:
918 #ifdef CCL_DEBUG
919 ccl_backtrace_table[ccl_backtrace_idx++] = ic;
920 if (ccl_backtrace_idx >= CCL_DEBUG_BACKTRACE_LEN)
921 ccl_backtrace_idx = 0;
922 ccl_backtrace_table[ccl_backtrace_idx] = 0;
923 #endif
924
925 if (!NILP (Vquit_flag) && NILP (Vinhibit_quit))
926 {
927 /* We can't just signal Qquit, instead break the loop as if
928 the whole data is processed. Don't reset Vquit_flag, it
929 must be handled later at a safer place. */
930 if (consumed)
931 src = source + src_bytes;
932 ccl->status = CCL_STAT_QUIT;
933 break;
934 }
935
936 this_ic = ic;
937 code = XINT (ccl_prog[ic]); ic++;
938 field1 = code >> 8;
939 field2 = (code & 0xFF) >> 5;
940
941 #define rrr field2
942 #define RRR (field1 & 7)
943 #define Rrr ((field1 >> 3) & 7)
944 #define ADDR field1
945 #define EXCMD (field1 >> 6)
946
947 switch (code & 0x1F)
948 {
949 case CCL_SetRegister: /* 00000000000000000RRRrrrXXXXX */
950 reg[rrr] = reg[RRR];
951 break;
952
953 case CCL_SetShortConst: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
954 reg[rrr] = field1;
955 break;
956
957 case CCL_SetConst: /* 00000000000000000000rrrXXXXX */
958 reg[rrr] = XINT (ccl_prog[ic]);
959 ic++;
960 break;
961
962 case CCL_SetArray: /* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
963 i = reg[RRR];
964 j = field1 >> 3;
965 if ((unsigned int) i < j)
966 reg[rrr] = XINT (ccl_prog[ic + i]);
967 ic += j;
968 break;
969
970 case CCL_Jump: /* A--D--D--R--E--S--S-000XXXXX */
971 ic += ADDR;
972 break;
973
974 case CCL_JumpCond: /* A--D--D--R--E--S--S-rrrXXXXX */
975 if (!reg[rrr])
976 ic += ADDR;
977 break;
978
979 case CCL_WriteRegisterJump: /* A--D--D--R--E--S--S-rrrXXXXX */
980 i = reg[rrr];
981 CCL_WRITE_CHAR (i);
982 ic += ADDR;
983 break;
984
985 case CCL_WriteRegisterReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
986 i = reg[rrr];
987 CCL_WRITE_CHAR (i);
988 ic++;
989 CCL_READ_CHAR (reg[rrr]);
990 ic += ADDR - 1;
991 break;
992
993 case CCL_WriteConstJump: /* A--D--D--R--E--S--S-000XXXXX */
994 i = XINT (ccl_prog[ic]);
995 CCL_WRITE_CHAR (i);
996 ic += ADDR;
997 break;
998
999 case CCL_WriteConstReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
1000 i = XINT (ccl_prog[ic]);
1001 CCL_WRITE_CHAR (i);
1002 ic++;
1003 CCL_READ_CHAR (reg[rrr]);
1004 ic += ADDR - 1;
1005 break;
1006
1007 case CCL_WriteStringJump: /* A--D--D--R--E--S--S-000XXXXX */
1008 j = XINT (ccl_prog[ic]);
1009 ic++;
1010 CCL_WRITE_STRING (j);
1011 ic += ADDR - 1;
1012 break;
1013
1014 case CCL_WriteArrayReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
1015 i = reg[rrr];
1016 j = XINT (ccl_prog[ic]);
1017 if ((unsigned int) i < j)
1018 {
1019 i = XINT (ccl_prog[ic + 1 + i]);
1020 CCL_WRITE_CHAR (i);
1021 }
1022 ic += j + 2;
1023 CCL_READ_CHAR (reg[rrr]);
1024 ic += ADDR - (j + 2);
1025 break;
1026
1027 case CCL_ReadJump: /* A--D--D--R--E--S--S-rrrYYYYY */
1028 CCL_READ_CHAR (reg[rrr]);
1029 ic += ADDR;
1030 break;
1031
1032 case CCL_ReadBranch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1033 CCL_READ_CHAR (reg[rrr]);
1034 /* fall through ... */
1035 case CCL_Branch: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1036 if ((unsigned int) reg[rrr] < field1)
1037 ic += XINT (ccl_prog[ic + reg[rrr]]);
1038 else
1039 ic += XINT (ccl_prog[ic + field1]);
1040 break;
1041
1042 case CCL_ReadRegister: /* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
1043 while (1)
1044 {
1045 CCL_READ_CHAR (reg[rrr]);
1046 if (!field1) break;
1047 code = XINT (ccl_prog[ic]); ic++;
1048 field1 = code >> 8;
1049 field2 = (code & 0xFF) >> 5;
1050 }
1051 break;
1052
1053 case CCL_WriteExprConst: /* 1:00000OPERATION000RRR000XXXXX */
1054 rrr = 7;
1055 i = reg[RRR];
1056 j = XINT (ccl_prog[ic]);
1057 op = field1 >> 6;
1058 jump_address = ic + 1;
1059 goto ccl_set_expr;
1060
1061 case CCL_WriteRegister: /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
1062 while (1)
1063 {
1064 i = reg[rrr];
1065 CCL_WRITE_CHAR (i);
1066 if (!field1) break;
1067 code = XINT (ccl_prog[ic]); ic++;
1068 field1 = code >> 8;
1069 field2 = (code & 0xFF) >> 5;
1070 }
1071 break;
1072
1073 case CCL_WriteExprRegister: /* 1:00000OPERATIONRrrRRR000XXXXX */
1074 rrr = 7;
1075 i = reg[RRR];
1076 j = reg[Rrr];
1077 op = field1 >> 6;
1078 jump_address = ic;
1079 goto ccl_set_expr;
1080
1081 case CCL_Call: /* 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX */
1082 {
1083 Lisp_Object slot;
1084 int prog_id;
1085
1086 /* If FFF is nonzero, the CCL program ID is in the
1087 following code. */
1088 if (rrr)
1089 {
1090 prog_id = XINT (ccl_prog[ic]);
1091 ic++;
1092 }
1093 else
1094 prog_id = field1;
1095
1096 if (stack_idx >= 256
1097 || prog_id < 0
1098 || prog_id >= ASIZE (Vccl_program_table)
1099 || (slot = AREF (Vccl_program_table, prog_id), !VECTORP (slot))
1100 || !VECTORP (AREF (slot, 1)))
1101 {
1102 if (stack_idx > 0)
1103 {
1104 ccl_prog = ccl_prog_stack_struct[0].ccl_prog;
1105 ic = ccl_prog_stack_struct[0].ic;
1106 }
1107 CCL_INVALID_CMD;
1108 }
1109
1110 ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog;
1111 ccl_prog_stack_struct[stack_idx].ic = ic;
1112 stack_idx++;
1113 ccl_prog = XVECTOR (AREF (slot, 1))->contents;
1114 ic = CCL_HEADER_MAIN;
1115 }
1116 break;
1117
1118 case CCL_WriteConstString: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1119 if (!rrr)
1120 CCL_WRITE_CHAR (field1);
1121 else
1122 {
1123 CCL_WRITE_STRING (field1);
1124 ic += (field1 + 2) / 3;
1125 }
1126 break;
1127
1128 case CCL_WriteArray: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
1129 i = reg[rrr];
1130 if ((unsigned int) i < field1)
1131 {
1132 j = XINT (ccl_prog[ic + i]);
1133 CCL_WRITE_CHAR (j);
1134 }
1135 ic += field1;
1136 break;
1137
1138 case CCL_End: /* 0000000000000000000000XXXXX */
1139 if (stack_idx > 0)
1140 {
1141 stack_idx--;
1142 ccl_prog = ccl_prog_stack_struct[stack_idx].ccl_prog;
1143 ic = ccl_prog_stack_struct[stack_idx].ic;
1144 break;
1145 }
1146 if (src)
1147 src = src_end;
1148 /* ccl->ic should points to this command code again to
1149 suppress further processing. */
1150 ic--;
1151 CCL_SUCCESS;
1152
1153 case CCL_ExprSelfConst: /* 00000OPERATION000000rrrXXXXX */
1154 i = XINT (ccl_prog[ic]);
1155 ic++;
1156 op = field1 >> 6;
1157 goto ccl_expr_self;
1158
1159 case CCL_ExprSelfReg: /* 00000OPERATION000RRRrrrXXXXX */
1160 i = reg[RRR];
1161 op = field1 >> 6;
1162
1163 ccl_expr_self:
1164 switch (op)
1165 {
1166 case CCL_PLUS: reg[rrr] += i; break;
1167 case CCL_MINUS: reg[rrr] -= i; break;
1168 case CCL_MUL: reg[rrr] *= i; break;
1169 case CCL_DIV: reg[rrr] /= i; break;
1170 case CCL_MOD: reg[rrr] %= i; break;
1171 case CCL_AND: reg[rrr] &= i; break;
1172 case CCL_OR: reg[rrr] |= i; break;
1173 case CCL_XOR: reg[rrr] ^= i; break;
1174 case CCL_LSH: reg[rrr] <<= i; break;
1175 case CCL_RSH: reg[rrr] >>= i; break;
1176 case CCL_LSH8: reg[rrr] <<= 8; reg[rrr] |= i; break;
1177 case CCL_RSH8: reg[7] = reg[rrr] & 0xFF; reg[rrr] >>= 8; break;
1178 case CCL_DIVMOD: reg[7] = reg[rrr] % i; reg[rrr] /= i; break;
1179 case CCL_LS: reg[rrr] = reg[rrr] < i; break;
1180 case CCL_GT: reg[rrr] = reg[rrr] > i; break;
1181 case CCL_EQ: reg[rrr] = reg[rrr] == i; break;
1182 case CCL_LE: reg[rrr] = reg[rrr] <= i; break;
1183 case CCL_GE: reg[rrr] = reg[rrr] >= i; break;
1184 case CCL_NE: reg[rrr] = reg[rrr] != i; break;
1185 default: CCL_INVALID_CMD;
1186 }
1187 break;
1188
1189 case CCL_SetExprConst: /* 00000OPERATION000RRRrrrXXXXX */
1190 i = reg[RRR];
1191 j = XINT (ccl_prog[ic]);
1192 op = field1 >> 6;
1193 jump_address = ++ic;
1194 goto ccl_set_expr;
1195
1196 case CCL_SetExprReg: /* 00000OPERATIONRrrRRRrrrXXXXX */
1197 i = reg[RRR];
1198 j = reg[Rrr];
1199 op = field1 >> 6;
1200 jump_address = ic;
1201 goto ccl_set_expr;
1202
1203 case CCL_ReadJumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
1204 CCL_READ_CHAR (reg[rrr]);
1205 case CCL_JumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
1206 i = reg[rrr];
1207 op = XINT (ccl_prog[ic]);
1208 jump_address = ic++ + ADDR;
1209 j = XINT (ccl_prog[ic]);
1210 ic++;
1211 rrr = 7;
1212 goto ccl_set_expr;
1213
1214 case CCL_ReadJumpCondExprReg: /* A--D--D--R--E--S--S-rrrXXXXX */
1215 CCL_READ_CHAR (reg[rrr]);
1216 case CCL_JumpCondExprReg:
1217 i = reg[rrr];
1218 op = XINT (ccl_prog[ic]);
1219 jump_address = ic++ + ADDR;
1220 j = reg[XINT (ccl_prog[ic])];
1221 ic++;
1222 rrr = 7;
1223
1224 ccl_set_expr:
1225 switch (op)
1226 {
1227 case CCL_PLUS: reg[rrr] = i + j; break;
1228 case CCL_MINUS: reg[rrr] = i - j; break;
1229 case CCL_MUL: reg[rrr] = i * j; break;
1230 case CCL_DIV: reg[rrr] = i / j; break;
1231 case CCL_MOD: reg[rrr] = i % j; break;
1232 case CCL_AND: reg[rrr] = i & j; break;
1233 case CCL_OR: reg[rrr] = i | j; break;
1234 case CCL_XOR: reg[rrr] = i ^ j;; break;
1235 case CCL_LSH: reg[rrr] = i << j; break;
1236 case CCL_RSH: reg[rrr] = i >> j; break;
1237 case CCL_LSH8: reg[rrr] = (i << 8) | j; break;
1238 case CCL_RSH8: reg[rrr] = i >> 8; reg[7] = i & 0xFF; break;
1239 case CCL_DIVMOD: reg[rrr] = i / j; reg[7] = i % j; break;
1240 case CCL_LS: reg[rrr] = i < j; break;
1241 case CCL_GT: reg[rrr] = i > j; break;
1242 case CCL_EQ: reg[rrr] = i == j; break;
1243 case CCL_LE: reg[rrr] = i <= j; break;
1244 case CCL_GE: reg[rrr] = i >= j; break;
1245 case CCL_NE: reg[rrr] = i != j; break;
1246 case CCL_DECODE_SJIS: DECODE_SJIS (i, j, reg[rrr], reg[7]); break;
1247 case CCL_ENCODE_SJIS: ENCODE_SJIS (i, j, reg[rrr], reg[7]); break;
1248 default: CCL_INVALID_CMD;
1249 }
1250 code &= 0x1F;
1251 if (code == CCL_WriteExprConst || code == CCL_WriteExprRegister)
1252 {
1253 i = reg[rrr];
1254 CCL_WRITE_CHAR (i);
1255 ic = jump_address;
1256 }
1257 else if (!reg[rrr])
1258 ic = jump_address;
1259 break;
1260
1261 case CCL_Extension:
1262 switch (EXCMD)
1263 {
1264 case CCL_ReadMultibyteChar2:
1265 if (!src)
1266 CCL_INVALID_CMD;
1267
1268 if (src >= src_end)
1269 {
1270 src++;
1271 goto ccl_read_multibyte_character_suspend;
1272 }
1273
1274 if (!ccl->multibyte)
1275 {
1276 int bytes;
1277 if (!UNIBYTE_STR_AS_MULTIBYTE_P (src, src_end - src, bytes))
1278 {
1279 reg[RRR] = CHARSET_8_BIT_CONTROL;
1280 reg[rrr] = *src++;
1281 break;
1282 }
1283 }
1284 i = *src++;
1285 if (i == '\n' && ccl->eol_type != CODING_EOL_LF)
1286 {
1287 /* We are encoding. */
1288 if (ccl->eol_type == CODING_EOL_CRLF)
1289 {
1290 if (ccl->cr_consumed)
1291 ccl->cr_consumed = 0;
1292 else
1293 {
1294 ccl->cr_consumed = 1;
1295 i = '\r';
1296 src--;
1297 }
1298 }
1299 else
1300 i = '\r';
1301 reg[rrr] = i;
1302 reg[RRR] = CHARSET_ASCII;
1303 }
1304 else if (i < 0x80)
1305 {
1306 /* ASCII */
1307 reg[rrr] = i;
1308 reg[RRR] = CHARSET_ASCII;
1309 }
1310 else if (i <= MAX_CHARSET_OFFICIAL_DIMENSION2)
1311 {
1312 int dimension = BYTES_BY_CHAR_HEAD (i) - 1;
1313
1314 if (dimension == 0)
1315 {
1316 /* `i' is a leading code for an undefined charset. */
1317 reg[RRR] = CHARSET_8_BIT_GRAPHIC;
1318 reg[rrr] = i;
1319 }
1320 else if (src + dimension > src_end)
1321 goto ccl_read_multibyte_character_suspend;
1322 else
1323 {
1324 reg[RRR] = i;
1325 i = (*src++ & 0x7F);
1326 if (dimension == 1)
1327 reg[rrr] = i;
1328 else
1329 reg[rrr] = ((i << 7) | (*src++ & 0x7F));
1330 }
1331 }
1332 else if ((i == LEADING_CODE_PRIVATE_11)
1333 || (i == LEADING_CODE_PRIVATE_12))
1334 {
1335 if ((src + 1) >= src_end)
1336 goto ccl_read_multibyte_character_suspend;
1337 reg[RRR] = *src++;
1338 reg[rrr] = (*src++ & 0x7F);
1339 }
1340 else if ((i == LEADING_CODE_PRIVATE_21)
1341 || (i == LEADING_CODE_PRIVATE_22))
1342 {
1343 if ((src + 2) >= src_end)
1344 goto ccl_read_multibyte_character_suspend;
1345 reg[RRR] = *src++;
1346 i = (*src++ & 0x7F);
1347 reg[rrr] = ((i << 7) | (*src & 0x7F));
1348 src++;
1349 }
1350 else if (i == LEADING_CODE_8_BIT_CONTROL)
1351 {
1352 if (src >= src_end)
1353 goto ccl_read_multibyte_character_suspend;
1354 reg[RRR] = CHARSET_8_BIT_CONTROL;
1355 reg[rrr] = (*src++ - 0x20);
1356 }
1357 else if (i >= 0xA0)
1358 {
1359 reg[RRR] = CHARSET_8_BIT_GRAPHIC;
1360 reg[rrr] = i;
1361 }
1362 else
1363 {
1364 /* INVALID CODE. Return a single byte character. */
1365 reg[RRR] = CHARSET_ASCII;
1366 reg[rrr] = i;
1367 }
1368 break;
1369
1370 ccl_read_multibyte_character_suspend:
1371 if (src <= src_end && !ccl->multibyte && ccl->last_block)
1372 {
1373 reg[RRR] = CHARSET_8_BIT_CONTROL;
1374 reg[rrr] = i;
1375 break;
1376 }
1377 src--;
1378 if (ccl->last_block)
1379 {
1380 ic = ccl->eof_ic;
1381 goto ccl_repeat;
1382 }
1383 else
1384 CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC);
1385
1386 break;
1387
1388 case CCL_WriteMultibyteChar2:
1389 i = reg[RRR]; /* charset */
1390 if (i == CHARSET_ASCII
1391 || i == CHARSET_8_BIT_CONTROL
1392 || i == CHARSET_8_BIT_GRAPHIC)
1393 i = reg[rrr] & 0xFF;
1394 else if (CHARSET_DIMENSION (i) == 1)
1395 i = ((i - 0x70) << 7) | (reg[rrr] & 0x7F);
1396 else if (i < MIN_CHARSET_PRIVATE_DIMENSION2)
1397 i = ((i - 0x8F) << 14) | reg[rrr];
1398 else
1399 i = ((i - 0xE0) << 14) | reg[rrr];
1400
1401 CCL_WRITE_MULTIBYTE_CHAR (i);
1402
1403 break;
1404
1405 case CCL_TranslateCharacter:
1406 CCL_MAKE_CHAR (reg[RRR], reg[rrr], i);
1407 op = translate_char (GET_TRANSLATION_TABLE (reg[Rrr]),
1408 i, -1, 0, 0);
1409 SPLIT_CHAR (op, reg[RRR], i, j);
1410 if (j != -1)
1411 i = (i << 7) | j;
1412
1413 reg[rrr] = i;
1414 break;
1415
1416 case CCL_TranslateCharacterConstTbl:
1417 op = XINT (ccl_prog[ic]); /* table */
1418 ic++;
1419 CCL_MAKE_CHAR (reg[RRR], reg[rrr], i);
1420 op = translate_char (GET_TRANSLATION_TABLE (op), i, -1, 0, 0);
1421 SPLIT_CHAR (op, reg[RRR], i, j);
1422 if (j != -1)
1423 i = (i << 7) | j;
1424
1425 reg[rrr] = i;
1426 break;
1427
1428 case CCL_LookupIntConstTbl:
1429 op = XINT (ccl_prog[ic]); /* table */
1430 ic++;
1431 {
1432 struct Lisp_Hash_Table *h = GET_HASH_TABLE (op);
1433
1434 op = hash_lookup (h, make_number (reg[RRR]), NULL);
1435 if (op >= 0)
1436 {
1437 op = HASH_VALUE (h, op);
1438 if (!CHAR_VALID_P (op, 0))
1439 CCL_INVALID_CMD;
1440 SPLIT_CHAR (XINT (op), reg[RRR], i, j);
1441 if (j != -1)
1442 i = (i << 7) | j;
1443 reg[rrr] = i;
1444 reg[7] = 1; /* r7 true for success */
1445 }
1446 else
1447 reg[7] = 0;
1448 }
1449 break;
1450
1451 case CCL_LookupCharConstTbl:
1452 op = XINT (ccl_prog[ic]); /* table */
1453 ic++;
1454 CCL_MAKE_CHAR (reg[RRR], reg[rrr], i);
1455 {
1456 struct Lisp_Hash_Table *h = GET_HASH_TABLE (op);
1457
1458 op = hash_lookup (h, make_number (i), NULL);
1459 if (op >= 0)
1460 {
1461 op = HASH_VALUE (h, op);
1462 if (!INTEGERP (op))
1463 CCL_INVALID_CMD;
1464 reg[RRR] = XINT (op);
1465 reg[7] = 1; /* r7 true for success */
1466 }
1467 else
1468 reg[7] = 0;
1469 }
1470 break;
1471
1472 case CCL_IterateMultipleMap:
1473 {
1474 Lisp_Object map, content, attrib, value;
1475 int point, size, fin_ic;
1476
1477 j = XINT (ccl_prog[ic++]); /* number of maps. */
1478 fin_ic = ic + j;
1479 op = reg[rrr];
1480 if ((j > reg[RRR]) && (j >= 0))
1481 {
1482 ic += reg[RRR];
1483 i = reg[RRR];
1484 }
1485 else
1486 {
1487 reg[RRR] = -1;
1488 ic = fin_ic;
1489 break;
1490 }
1491
1492 for (;i < j;i++)
1493 {
1494
1495 size = ASIZE (Vcode_conversion_map_vector);
1496 point = XINT (ccl_prog[ic++]);
1497 if (point >= size) continue;
1498 map = AREF (Vcode_conversion_map_vector, point);
1499
1500 /* Check map varidity. */
1501 if (!CONSP (map)) continue;
1502 map = XCDR (map);
1503 if (!VECTORP (map)) continue;
1504 size = ASIZE (map);
1505 if (size <= 1) continue;
1506
1507 content = AREF (map, 0);
1508
1509 /* check map type,
1510 [STARTPOINT VAL1 VAL2 ...] or
1511 [t ELELMENT STARTPOINT ENDPOINT] */
1512 if (NUMBERP (content))
1513 {
1514 point = XUINT (content);
1515 point = op - point + 1;
1516 if (!((point >= 1) && (point < size))) continue;
1517 content = AREF (map, point);
1518 }
1519 else if (EQ (content, Qt))
1520 {
1521 if (size != 4) continue;
1522 if ((op >= XUINT (AREF (map, 2)))
1523 && (op < XUINT (AREF (map, 3))))
1524 content = AREF (map, 1);
1525 else
1526 continue;
1527 }
1528 else
1529 continue;
1530
1531 if (NILP (content))
1532 continue;
1533 else if (NUMBERP (content))
1534 {
1535 reg[RRR] = i;
1536 reg[rrr] = XINT(content);
1537 break;
1538 }
1539 else if (EQ (content, Qt) || EQ (content, Qlambda))
1540 {
1541 reg[RRR] = i;
1542 break;
1543 }
1544 else if (CONSP (content))
1545 {
1546 attrib = XCAR (content);
1547 value = XCDR (content);
1548 if (!NUMBERP (attrib) || !NUMBERP (value))
1549 continue;
1550 reg[RRR] = i;
1551 reg[rrr] = XUINT (value);
1552 break;
1553 }
1554 else if (SYMBOLP (content))
1555 CCL_CALL_FOR_MAP_INSTRUCTION (content, fin_ic);
1556 else
1557 CCL_INVALID_CMD;
1558 }
1559 if (i == j)
1560 reg[RRR] = -1;
1561 ic = fin_ic;
1562 }
1563 break;
1564
1565 case CCL_MapMultiple:
1566 {
1567 Lisp_Object map, content, attrib, value;
1568 int point, size, map_vector_size;
1569 int map_set_rest_length, fin_ic;
1570 int current_ic = this_ic;
1571
1572 /* inhibit recursive call on MapMultiple. */
1573 if (stack_idx_of_map_multiple > 0)
1574 {
1575 if (stack_idx_of_map_multiple <= stack_idx)
1576 {
1577 stack_idx_of_map_multiple = 0;
1578 mapping_stack_pointer = mapping_stack;
1579 CCL_INVALID_CMD;
1580 }
1581 }
1582 else
1583 mapping_stack_pointer = mapping_stack;
1584 stack_idx_of_map_multiple = 0;
1585
1586 map_set_rest_length =
1587 XINT (ccl_prog[ic++]); /* number of maps and separators. */
1588 fin_ic = ic + map_set_rest_length;
1589 op = reg[rrr];
1590
1591 if ((map_set_rest_length > reg[RRR]) && (reg[RRR] >= 0))
1592 {
1593 ic += reg[RRR];
1594 i = reg[RRR];
1595 map_set_rest_length -= i;
1596 }
1597 else
1598 {
1599 ic = fin_ic;
1600 reg[RRR] = -1;
1601 mapping_stack_pointer = mapping_stack;
1602 break;
1603 }
1604
1605 if (mapping_stack_pointer <= (mapping_stack + 1))
1606 {
1607 /* Set up initial state. */
1608 mapping_stack_pointer = mapping_stack;
1609 PUSH_MAPPING_STACK (0, op);
1610 reg[RRR] = -1;
1611 }
1612 else
1613 {
1614 /* Recover after calling other ccl program. */
1615 int orig_op;
1616
1617 POP_MAPPING_STACK (map_set_rest_length, orig_op);
1618 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1619 switch (op)
1620 {
1621 case -1:
1622 /* Regard it as Qnil. */
1623 op = orig_op;
1624 i++;
1625 ic++;
1626 map_set_rest_length--;
1627 break;
1628 case -2:
1629 /* Regard it as Qt. */
1630 op = reg[rrr];
1631 i++;
1632 ic++;
1633 map_set_rest_length--;
1634 break;
1635 case -3:
1636 /* Regard it as Qlambda. */
1637 op = orig_op;
1638 i += map_set_rest_length;
1639 ic += map_set_rest_length;
1640 map_set_rest_length = 0;
1641 break;
1642 default:
1643 /* Regard it as normal mapping. */
1644 i += map_set_rest_length;
1645 ic += map_set_rest_length;
1646 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1647 break;
1648 }
1649 }
1650 map_vector_size = ASIZE (Vcode_conversion_map_vector);
1651
1652 do {
1653 for (;map_set_rest_length > 0;i++, ic++, map_set_rest_length--)
1654 {
1655 point = XINT(ccl_prog[ic]);
1656 if (point < 0)
1657 {
1658 /* +1 is for including separator. */
1659 point = -point + 1;
1660 if (mapping_stack_pointer
1661 >= &mapping_stack[MAX_MAP_SET_LEVEL])
1662 CCL_INVALID_CMD;
1663 PUSH_MAPPING_STACK (map_set_rest_length - point,
1664 reg[rrr]);
1665 map_set_rest_length = point;
1666 reg[rrr] = op;
1667 continue;
1668 }
1669
1670 if (point >= map_vector_size) continue;
1671 map = AREF (Vcode_conversion_map_vector, point);
1672
1673 /* Check map varidity. */
1674 if (!CONSP (map)) continue;
1675 map = XCDR (map);
1676 if (!VECTORP (map)) continue;
1677 size = ASIZE (map);
1678 if (size <= 1) continue;
1679
1680 content = AREF (map, 0);
1681
1682 /* check map type,
1683 [STARTPOINT VAL1 VAL2 ...] or
1684 [t ELEMENT STARTPOINT ENDPOINT] */
1685 if (NUMBERP (content))
1686 {
1687 point = XUINT (content);
1688 point = op - point + 1;
1689 if (!((point >= 1) && (point < size))) continue;
1690 content = AREF (map, point);
1691 }
1692 else if (EQ (content, Qt))
1693 {
1694 if (size != 4) continue;
1695 if ((op >= XUINT (AREF (map, 2))) &&
1696 (op < XUINT (AREF (map, 3))))
1697 content = AREF (map, 1);
1698 else
1699 continue;
1700 }
1701 else
1702 continue;
1703
1704 if (NILP (content))
1705 continue;
1706
1707 reg[RRR] = i;
1708 if (NUMBERP (content))
1709 {
1710 op = XINT (content);
1711 i += map_set_rest_length - 1;
1712 ic += map_set_rest_length - 1;
1713 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1714 map_set_rest_length++;
1715 }
1716 else if (CONSP (content))
1717 {
1718 attrib = XCAR (content);
1719 value = XCDR (content);
1720 if (!NUMBERP (attrib) || !NUMBERP (value))
1721 continue;
1722 op = XUINT (value);
1723 i += map_set_rest_length - 1;
1724 ic += map_set_rest_length - 1;
1725 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1726 map_set_rest_length++;
1727 }
1728 else if (EQ (content, Qt))
1729 {
1730 op = reg[rrr];
1731 }
1732 else if (EQ (content, Qlambda))
1733 {
1734 i += map_set_rest_length;
1735 ic += map_set_rest_length;
1736 break;
1737 }
1738 else if (SYMBOLP (content))
1739 {
1740 if (mapping_stack_pointer
1741 >= &mapping_stack[MAX_MAP_SET_LEVEL])
1742 CCL_INVALID_CMD;
1743 PUSH_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1744 PUSH_MAPPING_STACK (map_set_rest_length, op);
1745 stack_idx_of_map_multiple = stack_idx + 1;
1746 CCL_CALL_FOR_MAP_INSTRUCTION (content, current_ic);
1747 }
1748 else
1749 CCL_INVALID_CMD;
1750 }
1751 if (mapping_stack_pointer <= (mapping_stack + 1))
1752 break;
1753 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1754 i += map_set_rest_length;
1755 ic += map_set_rest_length;
1756 POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
1757 } while (1);
1758
1759 ic = fin_ic;
1760 }
1761 reg[rrr] = op;
1762 break;
1763
1764 case CCL_MapSingle:
1765 {
1766 Lisp_Object map, attrib, value, content;
1767 int size, point;
1768 j = XINT (ccl_prog[ic++]); /* map_id */
1769 op = reg[rrr];
1770 if (j >= ASIZE (Vcode_conversion_map_vector))
1771 {
1772 reg[RRR] = -1;
1773 break;
1774 }
1775 map = AREF (Vcode_conversion_map_vector, j);
1776 if (!CONSP (map))
1777 {
1778 reg[RRR] = -1;
1779 break;
1780 }
1781 map = XCDR (map);
1782 if (!VECTORP (map))
1783 {
1784 reg[RRR] = -1;
1785 break;
1786 }
1787 size = ASIZE (map);
1788 point = XUINT (AREF (map, 0));
1789 point = op - point + 1;
1790 reg[RRR] = 0;
1791 if ((size <= 1) ||
1792 (!((point >= 1) && (point < size))))
1793 reg[RRR] = -1;
1794 else
1795 {
1796 reg[RRR] = 0;
1797 content = AREF (map, point);
1798 if (NILP (content))
1799 reg[RRR] = -1;
1800 else if (NUMBERP (content))
1801 reg[rrr] = XINT (content);
1802 else if (EQ (content, Qt));
1803 else if (CONSP (content))
1804 {
1805 attrib = XCAR (content);
1806 value = XCDR (content);
1807 if (!NUMBERP (attrib) || !NUMBERP (value))
1808 continue;
1809 reg[rrr] = XUINT(value);
1810 break;
1811 }
1812 else if (SYMBOLP (content))
1813 CCL_CALL_FOR_MAP_INSTRUCTION (content, ic);
1814 else
1815 reg[RRR] = -1;
1816 }
1817 }
1818 break;
1819
1820 default:
1821 CCL_INVALID_CMD;
1822 }
1823 break;
1824
1825 default:
1826 CCL_INVALID_CMD;
1827 }
1828 }
1829
1830 ccl_error_handler:
1831 /* The suppress_error member is set when e.g. a CCL-based coding
1832 system is used for terminal output. */
1833 if (!ccl->suppress_error && destination)
1834 {
1835 /* We can insert an error message only if DESTINATION is
1836 specified and we still have a room to store the message
1837 there. */
1838 char msg[256];
1839 int msglen;
1840
1841 if (!dst)
1842 dst = destination;
1843
1844 switch (ccl->status)
1845 {
1846 case CCL_STAT_INVALID_CMD:
1847 sprintf(msg, "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
1848 code & 0x1F, code, this_ic);
1849 #ifdef CCL_DEBUG
1850 {
1851 int i = ccl_backtrace_idx - 1;
1852 int j;
1853
1854 msglen = strlen (msg);
1855 if (dst + msglen <= (dst_bytes ? dst_end : src))
1856 {
1857 bcopy (msg, dst, msglen);
1858 dst += msglen;
1859 }
1860
1861 for (j = 0; j < CCL_DEBUG_BACKTRACE_LEN; j++, i--)
1862 {
1863 if (i < 0) i = CCL_DEBUG_BACKTRACE_LEN - 1;
1864 if (ccl_backtrace_table[i] == 0)
1865 break;
1866 sprintf(msg, " %d", ccl_backtrace_table[i]);
1867 msglen = strlen (msg);
1868 if (dst + msglen > (dst_bytes ? dst_end : src))
1869 break;
1870 bcopy (msg, dst, msglen);
1871 dst += msglen;
1872 }
1873 goto ccl_finish;
1874 }
1875 #endif
1876 break;
1877
1878 case CCL_STAT_QUIT:
1879 sprintf(msg, "\nCCL: Quited.");
1880 break;
1881
1882 default:
1883 sprintf(msg, "\nCCL: Unknown error type (%d).", ccl->status);
1884 }
1885
1886 msglen = strlen (msg);
1887 if (dst + msglen <= (dst_bytes ? dst_end : src))
1888 {
1889 bcopy (msg, dst, msglen);
1890 dst += msglen;
1891 }
1892
1893 if (ccl->status == CCL_STAT_INVALID_CMD)
1894 {
1895 #if 0 /* If the remaining bytes contain 0x80..0x9F, copying them
1896 results in an invalid multibyte sequence. */
1897
1898 /* Copy the remaining source data. */
1899 int i = src_end - src;
1900 if (dst_bytes && (dst_end - dst) < i)
1901 i = dst_end - dst;
1902 bcopy (src, dst, i);
1903 src += i;
1904 dst += i;
1905 #else
1906 /* Signal that we've consumed everything. */
1907 src = src_end;
1908 #endif
1909 }
1910 }
1911
1912 ccl_finish:
1913 ccl->ic = ic;
1914 ccl->stack_idx = stack_idx;
1915 ccl->prog = ccl_prog;
1916 ccl->eight_bit_control = (extra_bytes > 0);
1917 if (consumed)
1918 *consumed = src - source;
1919 return (dst ? dst - destination : 0);
1920 }
1921
1922 /* Resolve symbols in the specified CCL code (Lisp vector). This
1923 function converts symbols of code conversion maps and character
1924 translation tables embeded in the CCL code into their ID numbers.
1925
1926 The return value is a vector (CCL itself or a new vector in which
1927 all symbols are resolved), Qt if resolving of some symbol failed,
1928 or nil if CCL contains invalid data. */
1929
1930 static Lisp_Object
1931 resolve_symbol_ccl_program (ccl)
1932 Lisp_Object ccl;
1933 {
1934 int i, veclen, unresolved = 0;
1935 Lisp_Object result, contents, val;
1936
1937 result = ccl;
1938 veclen = ASIZE (result);
1939
1940 for (i = 0; i < veclen; i++)
1941 {
1942 contents = AREF (result, i);
1943 if (INTEGERP (contents))
1944 continue;
1945 else if (CONSP (contents)
1946 && SYMBOLP (XCAR (contents))
1947 && SYMBOLP (XCDR (contents)))
1948 {
1949 /* This is the new style for embedding symbols. The form is
1950 (SYMBOL . PROPERTY). (get SYMBOL PROPERTY) should give
1951 an index number. */
1952
1953 if (EQ (result, ccl))
1954 result = Fcopy_sequence (ccl);
1955
1956 val = Fget (XCAR (contents), XCDR (contents));
1957 if (NATNUMP (val))
1958 AREF (result, i) = val;
1959 else
1960 unresolved = 1;
1961 continue;
1962 }
1963 else if (SYMBOLP (contents))
1964 {
1965 /* This is the old style for embedding symbols. This style
1966 may lead to a bug if, for instance, a translation table
1967 and a code conversion map have the same name. */
1968 if (EQ (result, ccl))
1969 result = Fcopy_sequence (ccl);
1970
1971 val = Fget (contents, Qtranslation_table_id);
1972 if (NATNUMP (val))
1973 AREF (result, i) = val;
1974 else
1975 {
1976 val = Fget (contents, Qcode_conversion_map_id);
1977 if (NATNUMP (val))
1978 AREF (result, i) = val;
1979 else
1980 {
1981 val = Fget (contents, Qccl_program_idx);
1982 if (NATNUMP (val))
1983 AREF (result, i) = val;
1984 else
1985 unresolved = 1;
1986 }
1987 }
1988 continue;
1989 }
1990 return Qnil;
1991 }
1992
1993 return (unresolved ? Qt : result);
1994 }
1995
1996 /* Return the compiled code (vector) of CCL program CCL_PROG.
1997 CCL_PROG is a name (symbol) of the program or already compiled
1998 code. If necessary, resolve symbols in the compiled code to index
1999 numbers. If we failed to get the compiled code or to resolve
2000 symbols, return Qnil. */
2001
2002 static Lisp_Object
2003 ccl_get_compiled_code (ccl_prog)
2004 Lisp_Object ccl_prog;
2005 {
2006 Lisp_Object val, slot;
2007
2008 if (VECTORP (ccl_prog))
2009 {
2010 val = resolve_symbol_ccl_program (ccl_prog);
2011 return (VECTORP (val) ? val : Qnil);
2012 }
2013 if (!SYMBOLP (ccl_prog))
2014 return Qnil;
2015
2016 val = Fget (ccl_prog, Qccl_program_idx);
2017 if (! NATNUMP (val)
2018 || XINT (val) >= ASIZE (Vccl_program_table))
2019 return Qnil;
2020 slot = AREF (Vccl_program_table, XINT (val));
2021 if (! VECTORP (slot)
2022 || ASIZE (slot) != 3
2023 || ! VECTORP (AREF (slot, 1)))
2024 return Qnil;
2025 if (NILP (AREF (slot, 2)))
2026 {
2027 val = resolve_symbol_ccl_program (AREF (slot, 1));
2028 if (! VECTORP (val))
2029 return Qnil;
2030 AREF (slot, 1) = val;
2031 AREF (slot, 2) = Qt;
2032 }
2033 return AREF (slot, 1);
2034 }
2035
2036 /* Setup fields of the structure pointed by CCL appropriately for the
2037 execution of CCL program CCL_PROG. CCL_PROG is the name (symbol)
2038 of the CCL program or the already compiled code (vector).
2039 Return 0 if we succeed this setup, else return -1.
2040
2041 If CCL_PROG is nil, we just reset the structure pointed by CCL. */
2042 int
2043 setup_ccl_program (ccl, ccl_prog)
2044 struct ccl_program *ccl;
2045 Lisp_Object ccl_prog;
2046 {
2047 int i;
2048
2049 if (! NILP (ccl_prog))
2050 {
2051 struct Lisp_Vector *vp;
2052
2053 ccl_prog = ccl_get_compiled_code (ccl_prog);
2054 if (! VECTORP (ccl_prog))
2055 return -1;
2056 vp = XVECTOR (ccl_prog);
2057 ccl->size = vp->size;
2058 ccl->prog = vp->contents;
2059 ccl->eof_ic = XINT (vp->contents[CCL_HEADER_EOF]);
2060 ccl->buf_magnification = XINT (vp->contents[CCL_HEADER_BUF_MAG]);
2061 }
2062 ccl->ic = CCL_HEADER_MAIN;
2063 for (i = 0; i < 8; i++)
2064 ccl->reg[i] = 0;
2065 ccl->last_block = 0;
2066 ccl->private_state = 0;
2067 ccl->status = 0;
2068 ccl->stack_idx = 0;
2069 ccl->eol_type = CODING_EOL_LF;
2070 ccl->suppress_error = 0;
2071 return 0;
2072 }
2073
2074 #ifdef emacs
2075
2076 DEFUN ("ccl-program-p", Fccl_program_p, Sccl_program_p, 1, 1, 0,
2077 doc: /* Return t if OBJECT is a CCL program name or a compiled CCL program code.
2078 See the documentation of `define-ccl-program' for the detail of CCL program. */)
2079 (object)
2080 Lisp_Object object;
2081 {
2082 Lisp_Object val;
2083
2084 if (VECTORP (object))
2085 {
2086 val = resolve_symbol_ccl_program (object);
2087 return (VECTORP (val) ? Qt : Qnil);
2088 }
2089 if (!SYMBOLP (object))
2090 return Qnil;
2091
2092 val = Fget (object, Qccl_program_idx);
2093 return ((! NATNUMP (val)
2094 || XINT (val) >= ASIZE (Vccl_program_table))
2095 ? Qnil : Qt);
2096 }
2097
2098 DEFUN ("ccl-execute", Fccl_execute, Sccl_execute, 2, 2, 0,
2099 doc: /* Execute CCL-PROGRAM with registers initialized by REGISTERS.
2100
2101 CCL-PROGRAM is a CCL program name (symbol)
2102 or compiled code generated by `ccl-compile' (for backward compatibility.
2103 In the latter case, the execution overhead is bigger than in the former).
2104 No I/O commands should appear in CCL-PROGRAM.
2105
2106 REGISTERS is a vector of [R0 R1 ... R7] where RN is an initial value
2107 for the Nth register.
2108
2109 As side effect, each element of REGISTERS holds the value of
2110 the corresponding register after the execution.
2111
2112 See the documentation of `define-ccl-program' for a definition of CCL
2113 programs. */)
2114 (ccl_prog, reg)
2115 Lisp_Object ccl_prog, reg;
2116 {
2117 struct ccl_program ccl;
2118 int i;
2119
2120 if (setup_ccl_program (&ccl, ccl_prog) < 0)
2121 error ("Invalid CCL program");
2122
2123 CHECK_VECTOR (reg);
2124 if (ASIZE (reg) != 8)
2125 error ("Length of vector REGISTERS is not 8");
2126
2127 for (i = 0; i < 8; i++)
2128 ccl.reg[i] = (INTEGERP (AREF (reg, i))
2129 ? XINT (AREF (reg, i))
2130 : 0);
2131
2132 ccl_driver (&ccl, (unsigned char *)0, (unsigned char *)0, 0, 0, (int *)0);
2133 QUIT;
2134 if (ccl.status != CCL_STAT_SUCCESS)
2135 error ("Error in CCL program at %dth code", ccl.ic);
2136
2137 for (i = 0; i < 8; i++)
2138 XSETINT (AREF (reg, i), ccl.reg[i]);
2139 return Qnil;
2140 }
2141
2142 DEFUN ("ccl-execute-on-string", Fccl_execute_on_string, Sccl_execute_on_string,
2143 3, 5, 0,
2144 doc: /* Execute CCL-PROGRAM with initial STATUS on STRING.
2145
2146 CCL-PROGRAM is a symbol registered by register-ccl-program,
2147 or a compiled code generated by `ccl-compile' (for backward compatibility,
2148 in this case, the execution is slower).
2149
2150 Read buffer is set to STRING, and write buffer is allocated automatically.
2151
2152 STATUS is a vector of [R0 R1 ... R7 IC], where
2153 R0..R7 are initial values of corresponding registers,
2154 IC is the instruction counter specifying from where to start the program.
2155 If R0..R7 are nil, they are initialized to 0.
2156 If IC is nil, it is initialized to head of the CCL program.
2157
2158 If optional 4th arg CONTINUE is non-nil, keep IC on read operation
2159 when read buffer is exausted, else, IC is always set to the end of
2160 CCL-PROGRAM on exit.
2161
2162 It returns the contents of write buffer as a string,
2163 and as side effect, STATUS is updated.
2164 If the optional 5th arg UNIBYTE-P is non-nil, the returned string
2165 is a unibyte string. By default it is a multibyte string.
2166
2167 See the documentation of `define-ccl-program' for the detail of CCL program. */)
2168 (ccl_prog, status, str, contin, unibyte_p)
2169 Lisp_Object ccl_prog, status, str, contin, unibyte_p;
2170 {
2171 Lisp_Object val;
2172 struct ccl_program ccl;
2173 int i, produced;
2174 int outbufsize;
2175 char *outbuf;
2176 struct gcpro gcpro1, gcpro2;
2177
2178 if (setup_ccl_program (&ccl, ccl_prog) < 0)
2179 error ("Invalid CCL program");
2180
2181 CHECK_VECTOR (status);
2182 if (ASIZE (status) != 9)
2183 error ("Length of vector STATUS is not 9");
2184 CHECK_STRING (str);
2185
2186 GCPRO2 (status, str);
2187
2188 for (i = 0; i < 8; i++)
2189 {
2190 if (NILP (AREF (status, i)))
2191 XSETINT (AREF (status, i), 0);
2192 if (INTEGERP (AREF (status, i)))
2193 ccl.reg[i] = XINT (AREF (status, i));
2194 }
2195 if (INTEGERP (AREF (status, i)))
2196 {
2197 i = XFASTINT (AREF (status, 8));
2198 if (ccl.ic < i && i < ccl.size)
2199 ccl.ic = i;
2200 }
2201 outbufsize = STRING_BYTES (XSTRING (str)) * ccl.buf_magnification + 256;
2202 outbuf = (char *) xmalloc (outbufsize);
2203 ccl.last_block = NILP (contin);
2204 ccl.multibyte = STRING_MULTIBYTE (str);
2205 produced = ccl_driver (&ccl, XSTRING (str)->data, outbuf,
2206 STRING_BYTES (XSTRING (str)), outbufsize, (int *) 0);
2207 for (i = 0; i < 8; i++)
2208 XSET (AREF (status, i), Lisp_Int, ccl.reg[i]);
2209 XSETINT (AREF (status, 8), ccl.ic);
2210 UNGCPRO;
2211
2212 if (NILP (unibyte_p))
2213 {
2214 int nchars;
2215
2216 produced = str_as_multibyte (outbuf, outbufsize, produced, &nchars);
2217 val = make_multibyte_string (outbuf, nchars, produced);
2218 }
2219 else
2220 val = make_unibyte_string (outbuf, produced);
2221 xfree (outbuf);
2222 QUIT;
2223 if (ccl.status == CCL_STAT_SUSPEND_BY_DST)
2224 error ("Output buffer for the CCL programs overflow");
2225 if (ccl.status != CCL_STAT_SUCCESS
2226 && ccl.status != CCL_STAT_SUSPEND_BY_SRC)
2227 error ("Error in CCL program at %dth code", ccl.ic);
2228
2229 return val;
2230 }
2231
2232 DEFUN ("register-ccl-program", Fregister_ccl_program, Sregister_ccl_program,
2233 2, 2, 0,
2234 doc: /* Register CCL program CCL_PROG as NAME in `ccl-program-table'.
2235 CCL_PROG should be a compiled CCL program (vector), or nil.
2236 If it is nil, just reserve NAME as a CCL program name.
2237 Return index number of the registered CCL program. */)
2238 (name, ccl_prog)
2239 Lisp_Object name, ccl_prog;
2240 {
2241 int len = ASIZE (Vccl_program_table);
2242 int idx;
2243 Lisp_Object resolved;
2244
2245 CHECK_SYMBOL (name);
2246 resolved = Qnil;
2247 if (!NILP (ccl_prog))
2248 {
2249 CHECK_VECTOR (ccl_prog);
2250 resolved = resolve_symbol_ccl_program (ccl_prog);
2251 if (NILP (resolved))
2252 error ("Error in CCL program");
2253 if (VECTORP (resolved))
2254 {
2255 ccl_prog = resolved;
2256 resolved = Qt;
2257 }
2258 else
2259 resolved = Qnil;
2260 }
2261
2262 for (idx = 0; idx < len; idx++)
2263 {
2264 Lisp_Object slot;
2265
2266 slot = AREF (Vccl_program_table, idx);
2267 if (!VECTORP (slot))
2268 /* This is the first unsed slot. Register NAME here. */
2269 break;
2270
2271 if (EQ (name, AREF (slot, 0)))
2272 {
2273 /* Update this slot. */
2274 AREF (slot, 1) = ccl_prog;
2275 AREF (slot, 2) = resolved;
2276 return make_number (idx);
2277 }
2278 }
2279
2280 if (idx == len)
2281 {
2282 /* Extend the table. */
2283 Lisp_Object new_table;
2284 int j;
2285
2286 new_table = Fmake_vector (make_number (len * 2), Qnil);
2287 for (j = 0; j < len; j++)
2288 AREF (new_table, j)
2289 = AREF (Vccl_program_table, j);
2290 Vccl_program_table = new_table;
2291 }
2292
2293 {
2294 Lisp_Object elt;
2295
2296 elt = Fmake_vector (make_number (3), Qnil);
2297 AREF (elt, 0) = name;
2298 AREF (elt, 1) = ccl_prog;
2299 AREF (elt, 2) = resolved;
2300 AREF (Vccl_program_table, idx) = elt;
2301 }
2302
2303 Fput (name, Qccl_program_idx, make_number (idx));
2304 return make_number (idx);
2305 }
2306
2307 /* Register code conversion map.
2308 A code conversion map consists of numbers, Qt, Qnil, and Qlambda.
2309 The first element is the start code point.
2310 The other elements are mapped numbers.
2311 Symbol t means to map to an original number before mapping.
2312 Symbol nil means that the corresponding element is empty.
2313 Symbol lambda means to terminate mapping here.
2314 */
2315
2316 DEFUN ("register-code-conversion-map", Fregister_code_conversion_map,
2317 Sregister_code_conversion_map,
2318 2, 2, 0,
2319 doc: /* Register SYMBOL as code conversion map MAP.
2320 Return index number of the registered map. */)
2321 (symbol, map)
2322 Lisp_Object symbol, map;
2323 {
2324 int len = ASIZE (Vcode_conversion_map_vector);
2325 int i;
2326 Lisp_Object index;
2327
2328 CHECK_SYMBOL (symbol);
2329 CHECK_VECTOR (map);
2330
2331 for (i = 0; i < len; i++)
2332 {
2333 Lisp_Object slot = AREF (Vcode_conversion_map_vector, i);
2334
2335 if (!CONSP (slot))
2336 break;
2337
2338 if (EQ (symbol, XCAR (slot)))
2339 {
2340 index = make_number (i);
2341 XSETCDR (slot, map);
2342 Fput (symbol, Qcode_conversion_map, map);
2343 Fput (symbol, Qcode_conversion_map_id, index);
2344 return index;
2345 }
2346 }
2347
2348 if (i == len)
2349 {
2350 Lisp_Object new_vector = Fmake_vector (make_number (len * 2), Qnil);
2351 int j;
2352
2353 for (j = 0; j < len; j++)
2354 AREF (new_vector, j)
2355 = AREF (Vcode_conversion_map_vector, j);
2356 Vcode_conversion_map_vector = new_vector;
2357 }
2358
2359 index = make_number (i);
2360 Fput (symbol, Qcode_conversion_map, map);
2361 Fput (symbol, Qcode_conversion_map_id, index);
2362 AREF (Vcode_conversion_map_vector, i) = Fcons (symbol, map);
2363 return index;
2364 }
2365
2366
2367 void
2368 syms_of_ccl ()
2369 {
2370 staticpro (&Vccl_program_table);
2371 Vccl_program_table = Fmake_vector (make_number (32), Qnil);
2372
2373 Qccl_program = intern ("ccl-program");
2374 staticpro (&Qccl_program);
2375
2376 Qccl_program_idx = intern ("ccl-program-idx");
2377 staticpro (&Qccl_program_idx);
2378
2379 Qcode_conversion_map = intern ("code-conversion-map");
2380 staticpro (&Qcode_conversion_map);
2381
2382 Qcode_conversion_map_id = intern ("code-conversion-map-id");
2383 staticpro (&Qcode_conversion_map_id);
2384
2385 DEFVAR_LISP ("code-conversion-map-vector", &Vcode_conversion_map_vector,
2386 doc: /* Vector of code conversion maps. */);
2387 Vcode_conversion_map_vector = Fmake_vector (make_number (16), Qnil);
2388
2389 DEFVAR_LISP ("font-ccl-encoder-alist", &Vfont_ccl_encoder_alist,
2390 doc: /* Alist of fontname patterns vs corresponding CCL program.
2391 Each element looks like (REGEXP . CCL-CODE),
2392 where CCL-CODE is a compiled CCL program.
2393 When a font whose name matches REGEXP is used for displaying a character,
2394 CCL-CODE is executed to calculate the code point in the font
2395 from the charset number and position code(s) of the character which are set
2396 in CCL registers R0, R1, and R2 before the execution.
2397 The code point in the font is set in CCL registers R1 and R2
2398 when the execution terminated.
2399 If the font is single-byte font, the register R2 is not used. */);
2400 Vfont_ccl_encoder_alist = Qnil;
2401
2402 DEFVAR_LISP ("translation-hash-table-vector", &Vtranslation_hash_table_vector,
2403 doc: /* Vector containing all translation hash tables ever defined.
2404 Comprises pairs (SYMBOL . TABLE) where SYMBOL and TABLE were set up by calls
2405 to `define-translation-hash-table'. The vector is indexed by the table id
2406 used by CCL. */);
2407 Vtranslation_hash_table_vector = Qnil;
2408
2409 defsubr (&Sccl_program_p);
2410 defsubr (&Sccl_execute);
2411 defsubr (&Sccl_execute_on_string);
2412 defsubr (&Sregister_ccl_program);
2413 defsubr (&Sregister_code_conversion_map);
2414 }
2415
2416 #endif /* emacs */