home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / e20313sr.zip / emacs / 20.3.1 / src / ccl.c < prev    next >
C/C++ Source or Header  |  1999-07-31  |  56KB  |  1,971 lines

  1. /* CCL (Code Conversion Language) interpreter.
  2.    Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
  3.    Licensed to the Free Software Foundation.
  4.  
  5. This file is part of GNU Emacs.
  6.  
  7. GNU Emacs is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. GNU Emacs is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU Emacs; see the file COPYING.  If not, write to
  19. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.  */
  21.  
  22. #include <stdio.h>
  23.  
  24. #ifdef emacs
  25.  
  26. #include <config.h>
  27.  
  28. #ifdef STDC_HEADERS
  29. #include <stdlib.h>
  30. #endif
  31.  
  32. #include "lisp.h"
  33. #include "charset.h"
  34. #include "ccl.h"
  35. #include "coding.h"
  36.  
  37. #else  /* not emacs */
  38.  
  39. #include "mulelib.h"
  40.  
  41. #endif /* not emacs */
  42.  
  43. /* This contains all code conversion map available to CCL.  */
  44. Lisp_Object Vcode_conversion_map_vector;
  45.  
  46. /* Alist of fontname patterns vs corresponding CCL program.  */
  47. Lisp_Object Vfont_ccl_encoder_alist;
  48.  
  49. /* This symbol is a property which assocates with ccl program vector.
  50.    Ex: (get 'ccl-big5-encoder 'ccl-program) returns ccl program vector.  */
  51. Lisp_Object Qccl_program;
  52.  
  53. /* These symbols are properties which associate with code conversion
  54.    map and their ID respectively.  */
  55. Lisp_Object Qcode_conversion_map;
  56. Lisp_Object Qcode_conversion_map_id;
  57.  
  58. /* Symbols of ccl program have this property, a value of the property
  59.    is an index for Vccl_protram_table. */
  60. Lisp_Object Qccl_program_idx;
  61.  
  62. /* Vector of CCL program names vs corresponding program data.  */
  63. Lisp_Object Vccl_program_table;
  64.  
  65. /* CCL (Code Conversion Language) is a simple language which has
  66.    operations on one input buffer, one output buffer, and 7 registers.
  67.    The syntax of CCL is described in `ccl.el'.  Emacs Lisp function
  68.    `ccl-compile' compiles a CCL program and produces a CCL code which
  69.    is a vector of integers.  The structure of this vector is as
  70.    follows: The 1st element: buffer-magnification, a factor for the
  71.    size of output buffer compared with the size of input buffer.  The
  72.    2nd element: address of CCL code to be executed when encountered
  73.    with end of input stream.  The 3rd and the remaining elements: CCL
  74.    codes.  */
  75.  
  76. /* Header of CCL compiled code */
  77. #define CCL_HEADER_BUF_MAG    0
  78. #define CCL_HEADER_EOF        1
  79. #define CCL_HEADER_MAIN        2
  80.  
  81. /* CCL code is a sequence of 28-bit non-negative integers (i.e. the
  82.    MSB is always 0), each contains CCL command and/or arguments in the
  83.    following format:
  84.  
  85.     |----------------- integer (28-bit) ------------------|
  86.     |------- 17-bit ------|- 3-bit --|- 3-bit --|- 5-bit -|
  87.     |--constant argument--|-register-|-register-|-command-|
  88.        ccccccccccccccccc      RRR        rrr       XXXXX
  89.   or
  90.     |------- relative address -------|-register-|-command-|
  91.            cccccccccccccccccccc          rrr       XXXXX
  92.   or
  93.     |------------- constant or other args ----------------|
  94.                      cccccccccccccccccccccccccccc
  95.  
  96.    where, `cc...c' is a non-negative integer indicating constant value
  97.    (the left most `c' is always 0) or an absolute jump address, `RRR'
  98.    and `rrr' are CCL register number, `XXXXX' is one of the following
  99.    CCL commands.  */
  100.  
  101. /* CCL commands
  102.  
  103.    Each comment fields shows one or more lines for command syntax and
  104.    the following lines for semantics of the command.  In semantics, IC
  105.    stands for Instruction Counter.  */
  106.  
  107. #define CCL_SetRegister        0x00 /* Set register a register value:
  108.                     1:00000000000000000RRRrrrXXXXX
  109.                     ------------------------------
  110.                     reg[rrr] = reg[RRR];
  111.                     */
  112.  
  113. #define CCL_SetShortConst    0x01 /* Set register a short constant value:
  114.                     1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
  115.                     ------------------------------
  116.                     reg[rrr] = CCCCCCCCCCCCCCCCCCC;
  117.                     */
  118.  
  119. #define CCL_SetConst        0x02 /* Set register a constant value:
  120.                     1:00000000000000000000rrrXXXXX
  121.                     2:CONSTANT
  122.                     ------------------------------
  123.                     reg[rrr] = CONSTANT;
  124.                     IC++;
  125.                     */
  126.  
  127. #define CCL_SetArray        0x03 /* Set register an element of array:
  128.                     1:CCCCCCCCCCCCCCCCCRRRrrrXXXXX
  129.                     2:ELEMENT[0]
  130.                     3:ELEMENT[1]
  131.                     ...
  132.                     ------------------------------
  133.                     if (0 <= reg[RRR] < CC..C)
  134.                       reg[rrr] = ELEMENT[reg[RRR]];
  135.                     IC += CC..C;
  136.                     */
  137.  
  138. #define CCL_Jump        0x04 /* Jump:
  139.                     1:A--D--D--R--E--S--S-000XXXXX
  140.                     ------------------------------
  141.                     IC += ADDRESS;
  142.                     */
  143.  
  144. /* Note: If CC..C is greater than 0, the second code is omitted.  */
  145.  
  146. #define CCL_JumpCond        0x05 /* Jump conditional:
  147.                     1:A--D--D--R--E--S--S-rrrXXXXX
  148.                     ------------------------------
  149.                     if (!reg[rrr])
  150.                       IC += ADDRESS;
  151.                     */
  152.  
  153.  
  154. #define CCL_WriteRegisterJump    0x06 /* Write register and jump:
  155.                     1:A--D--D--R--E--S--S-rrrXXXXX
  156.                     ------------------------------
  157.                     write (reg[rrr]);
  158.                     IC += ADDRESS;
  159.                     */
  160.  
  161. #define CCL_WriteRegisterReadJump 0x07 /* Write register, read, and jump:
  162.                     1:A--D--D--R--E--S--S-rrrXXXXX
  163.                     2:A--D--D--R--E--S--S-rrrYYYYY
  164.                     -----------------------------
  165.                     write (reg[rrr]);
  166.                     IC++;
  167.                     read (reg[rrr]);
  168.                     IC += ADDRESS;
  169.                     */
  170. /* Note: If read is suspended, the resumed execution starts from the
  171.    second code (YYYYY == CCL_ReadJump).  */
  172.  
  173. #define CCL_WriteConstJump    0x08 /* Write constant and jump:
  174.                     1:A--D--D--R--E--S--S-000XXXXX
  175.                     2:CONST
  176.                     ------------------------------
  177.                     write (CONST);
  178.                     IC += ADDRESS;
  179.                     */
  180.  
  181. #define CCL_WriteConstReadJump    0x09 /* Write constant, read, and jump:
  182.                     1:A--D--D--R--E--S--S-rrrXXXXX
  183.                     2:CONST
  184.                     3:A--D--D--R--E--S--S-rrrYYYYY
  185.                     -----------------------------
  186.                     write (CONST);
  187.                     IC += 2;
  188.                     read (reg[rrr]);
  189.                     IC += ADDRESS;
  190.                     */
  191. /* Note: If read is suspended, the resumed execution starts from the
  192.    second code (YYYYY == CCL_ReadJump).  */
  193.  
  194. #define CCL_WriteStringJump    0x0A /* Write string and jump:
  195.                     1:A--D--D--R--E--S--S-000XXXXX
  196.                     2:LENGTH
  197.                     3:0000STRIN[0]STRIN[1]STRIN[2]
  198.                     ...
  199.                     ------------------------------
  200.                     write_string (STRING, LENGTH);
  201.                     IC += ADDRESS;
  202.                     */
  203.  
  204. #define CCL_WriteArrayReadJump    0x0B /* Write an array element, read, and jump:
  205.                     1:A--D--D--R--E--S--S-rrrXXXXX
  206.                     2:LENGTH
  207.                     3:ELEMENET[0]
  208.                     4:ELEMENET[1]
  209.                     ...
  210.                     N:A--D--D--R--E--S--S-rrrYYYYY
  211.                     ------------------------------
  212.                     if (0 <= reg[rrr] < LENGTH)
  213.                       write (ELEMENT[reg[rrr]]);
  214.                     IC += LENGTH + 2; (... pointing at N+1)
  215.                     read (reg[rrr]);
  216.                     IC += ADDRESS;
  217.                     */
  218. /* Note: If read is suspended, the resumed execution starts from the
  219.    Nth code (YYYYY == CCL_ReadJump).  */
  220.  
  221. #define CCL_ReadJump        0x0C /* Read and jump:
  222.                     1:A--D--D--R--E--S--S-rrrYYYYY
  223.                     -----------------------------
  224.                     read (reg[rrr]);
  225.                     IC += ADDRESS;
  226.                     */
  227.  
  228. #define CCL_Branch        0x0D /* Jump by branch table:
  229.                     1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
  230.                     2:A--D--D--R--E-S-S[0]000XXXXX
  231.                     3:A--D--D--R--E-S-S[1]000XXXXX
  232.                     ...
  233.                     ------------------------------
  234.                     if (0 <= reg[rrr] < CC..C)
  235.                       IC += ADDRESS[reg[rrr]];
  236.                     else
  237.                       IC += ADDRESS[CC..C];
  238.                     */
  239.  
  240. #define CCL_ReadRegister    0x0E /* Read bytes into registers:
  241.                     1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
  242.                     2:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
  243.                     ...
  244.                     ------------------------------
  245.                     while (CCC--)
  246.                       read (reg[rrr]);
  247.                     */
  248.  
  249. #define CCL_WriteExprConst    0x0F  /* write result of expression:
  250.                     1:00000OPERATION000RRR000XXXXX
  251.                     2:CONSTANT
  252.                     ------------------------------
  253.                     write (reg[RRR] OPERATION CONSTANT);
  254.                     IC++;
  255.                     */
  256.  
  257. /* Note: If the Nth read is suspended, the resumed execution starts
  258.    from the Nth code.  */
  259.  
  260. #define CCL_ReadBranch        0x10 /* Read one byte into a register,
  261.                     and jump by branch table:
  262.                     1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
  263.                     2:A--D--D--R--E-S-S[0]000XXXXX
  264.                     3:A--D--D--R--E-S-S[1]000XXXXX
  265.                     ...
  266.                     ------------------------------
  267.                     read (read[rrr]);
  268.                     if (0 <= reg[rrr] < CC..C)
  269.                       IC += ADDRESS[reg[rrr]];
  270.                     else
  271.                       IC += ADDRESS[CC..C];
  272.                     */
  273.  
  274. #define CCL_WriteRegister    0x11 /* Write registers:
  275.                     1:CCCCCCCCCCCCCCCCCCCrrrXXXXX
  276.                     2:CCCCCCCCCCCCCCCCCCCrrrXXXXX
  277.                     ...
  278.                     ------------------------------
  279.                     while (CCC--)
  280.                       write (reg[rrr]);
  281.                     ...
  282.                     */
  283.  
  284. /* Note: If the Nth write is suspended, the resumed execution
  285.    starts from the Nth code.  */
  286.  
  287. #define CCL_WriteExprRegister    0x12 /* Write result of expression
  288.                     1:00000OPERATIONRrrRRR000XXXXX
  289.                     ------------------------------
  290.                     write (reg[RRR] OPERATION reg[Rrr]);
  291.                     */
  292.  
  293. #define CCL_Call        0x13 /* Call the CCL program whose ID is
  294.                     (CC..C).
  295.                     1:CCCCCCCCCCCCCCCCCCCC000XXXXX
  296.                     ------------------------------
  297.                     call (CC..C)
  298.                     */
  299.  
  300. #define CCL_WriteConstString    0x14 /* Write a constant or a string:
  301.                     1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
  302.                     [2:0000STRIN[0]STRIN[1]STRIN[2]]
  303.                     [...]
  304.                     -----------------------------
  305.                     if (!rrr)
  306.                       write (CC..C)
  307.                     else
  308.                       write_string (STRING, CC..C);
  309.                       IC += (CC..C + 2) / 3;
  310.                     */
  311.  
  312. #define CCL_WriteArray        0x15 /* Write an element of array:
  313.                     1:CCCCCCCCCCCCCCCCCCCCrrrXXXXX
  314.                     2:ELEMENT[0]
  315.                     3:ELEMENT[1]
  316.                     ...
  317.                     ------------------------------
  318.                     if (0 <= reg[rrr] < CC..C)
  319.                       write (ELEMENT[reg[rrr]]);
  320.                     IC += CC..C;
  321.                     */
  322.  
  323. #define CCL_End            0x16 /* Terminate:
  324.                     1:00000000000000000000000XXXXX
  325.                     ------------------------------
  326.                     terminate ();
  327.                     */
  328.  
  329. /* The following two codes execute an assignment arithmetic/logical
  330.    operation.  The form of the operation is like REG OP= OPERAND.  */
  331.  
  332. #define CCL_ExprSelfConst    0x17 /* REG OP= constant:
  333.                     1:00000OPERATION000000rrrXXXXX
  334.                     2:CONSTANT
  335.                     ------------------------------
  336.                     reg[rrr] OPERATION= CONSTANT;
  337.                     */
  338.  
  339. #define CCL_ExprSelfReg        0x18 /* REG1 OP= REG2:
  340.                     1:00000OPERATION000RRRrrrXXXXX
  341.                     ------------------------------
  342.                     reg[rrr] OPERATION= reg[RRR];
  343.                     */
  344.  
  345. /* The following codes execute an arithmetic/logical operation.  The
  346.    form of the operation is like REG_X = REG_Y OP OPERAND2.  */
  347.  
  348. #define CCL_SetExprConst    0x19 /* REG_X = REG_Y OP constant:
  349.                     1:00000OPERATION000RRRrrrXXXXX
  350.                     2:CONSTANT
  351.                     ------------------------------
  352.                     reg[rrr] = reg[RRR] OPERATION CONSTANT;
  353.                     IC++;
  354.                     */
  355.  
  356. #define CCL_SetExprReg        0x1A /* REG1 = REG2 OP REG3:
  357.                     1:00000OPERATIONRrrRRRrrrXXXXX
  358.                     ------------------------------
  359.                     reg[rrr] = reg[RRR] OPERATION reg[Rrr];
  360.                     */
  361.  
  362. #define CCL_JumpCondExprConst    0x1B /* Jump conditional according to
  363.                     an operation on constant:
  364.                     1:A--D--D--R--E--S--S-rrrXXXXX
  365.                     2:OPERATION
  366.                     3:CONSTANT
  367.                     -----------------------------
  368.                     reg[7] = reg[rrr] OPERATION CONSTANT;
  369.                     if (!(reg[7]))
  370.                       IC += ADDRESS;
  371.                     else
  372.                       IC += 2
  373.                     */
  374.  
  375. #define CCL_JumpCondExprReg    0x1C /* Jump conditional according to
  376.                     an operation on register:
  377.                     1:A--D--D--R--E--S--S-rrrXXXXX
  378.                     2:OPERATION
  379.                     3:RRR
  380.                     -----------------------------
  381.                     reg[7] = reg[rrr] OPERATION reg[RRR];
  382.                     if (!reg[7])
  383.                       IC += ADDRESS;
  384.                     else
  385.                       IC += 2;
  386.                     */
  387.  
  388. #define CCL_ReadJumpCondExprConst 0x1D /* Read and jump conditional according
  389.                       to an operation on constant:
  390.                     1:A--D--D--R--E--S--S-rrrXXXXX
  391.                     2:OPERATION
  392.                     3:CONSTANT
  393.                     -----------------------------
  394.                     read (reg[rrr]);
  395.                     reg[7] = reg[rrr] OPERATION CONSTANT;
  396.                     if (!reg[7])
  397.                       IC += ADDRESS;
  398.                     else
  399.                       IC += 2;
  400.                     */
  401.  
  402. #define CCL_ReadJumpCondExprReg    0x1E /* Read and jump conditional according
  403.                     to an operation on register:
  404.                     1:A--D--D--R--E--S--S-rrrXXXXX
  405.                     2:OPERATION
  406.                     3:RRR
  407.                     -----------------------------
  408.                     read (reg[rrr]);
  409.                     reg[7] = reg[rrr] OPERATION reg[RRR];
  410.                     if (!reg[7])
  411.                       IC += ADDRESS;
  412.                     else
  413.                       IC += 2;
  414.                     */
  415.  
  416. #define CCL_Extention        0x1F /* Extended CCL code
  417.                     1:ExtendedCOMMNDRrrRRRrrrXXXXX
  418.                     2:ARGUEMENT
  419.                     3:...
  420.                     ------------------------------
  421.                     extended_command (rrr,RRR,Rrr,ARGS)
  422.                       */
  423.  
  424. /* 
  425.    Here after, Extended CCL Instructions.
  426.    Bit length of extended command is 14.
  427.    Therefore, the instruction code range is 0..16384(0x3fff).
  428.  */
  429.  
  430. /* Read a multibyte characeter.
  431.    A code point is stored into reg[rrr].  A charset ID is stored into
  432.    reg[RRR].  */
  433.  
  434. #define CCL_ReadMultibyteChar2    0x00 /* Read Multibyte Character
  435.                     1:ExtendedCOMMNDRrrRRRrrrXXXXX  */
  436.  
  437. /* Write a multibyte character.
  438.    Write a character whose code point is reg[rrr] and the charset ID
  439.    is reg[RRR].  */
  440.  
  441. #define CCL_WriteMultibyteChar2    0x01 /* Write Multibyte Character
  442.                     1:ExtendedCOMMNDRrrRRRrrrXXXXX  */
  443.  
  444. /* Translate a character whose code point is reg[rrr] and the charset
  445.    ID is reg[RRR] by a translation table whose ID is reg[Rrr].
  446.  
  447.    A translated character is set in reg[rrr] (code point) and reg[RRR]
  448.    (charset ID).  */
  449.  
  450. #define CCL_TranslateCharacter    0x02 /* Translate a multibyte character
  451.                     1:ExtendedCOMMNDRrrRRRrrrXXXXX  */
  452.  
  453. /* Translate a character whose code point is reg[rrr] and the charset
  454.    ID is reg[RRR] by a translation table whose ID is ARGUMENT.
  455.  
  456.    A translated character is set in reg[rrr] (code point) and reg[RRR]
  457.    (charset ID).  */
  458.  
  459. #define CCL_TranslateCharacterConstTbl 0x03 /* Translate a multibyte character
  460.                            1:ExtendedCOMMNDRrrRRRrrrXXXXX
  461.                            2:ARGUMENT(Translation Table ID)
  462.                         */
  463.  
  464. /* Iterate looking up MAPs for reg[rrr] starting from the Nth (N =
  465.    reg[RRR]) MAP until some value is found.
  466.  
  467.    Each MAP is a Lisp vector whose element is number, nil, t, or
  468.    lambda.
  469.    If the element is nil, ignore the map and proceed to the next map.
  470.    If the element is t or lambda, finish without changing reg[rrr].
  471.    If the element is a number, set reg[rrr] to the number and finish.
  472.  
  473.    Detail of the map structure is descibed in the comment for
  474.    CCL_MapMultiple below.  */
  475.  
  476. #define CCL_IterateMultipleMap    0x10 /* Iterate multiple maps
  477.                     1:ExtendedCOMMNDXXXRRRrrrXXXXX
  478.                     2:NUMBER of MAPs
  479.                     3:MAP-ID1
  480.                     4:MAP-ID2
  481.                     ...
  482.                      */ 
  483.  
  484. /* Map the code in reg[rrr] by MAPs starting from the Nth (N =
  485.    reg[RRR]) map.
  486.  
  487.    MAPs are supplied in the succeeding CCL codes as follows:
  488.  
  489.    When CCL program gives this nested structure of map to this command:
  490.     ((MAP-ID11
  491.       MAP-ID12
  492.       (MAP-ID121 MAP-ID122 MAP-ID123)
  493.       MAP-ID13)
  494.      (MAP-ID21
  495.       (MAP-ID211 (MAP-ID2111) MAP-ID212)
  496.       MAP-ID22)),
  497.    the compiled CCL codes has this sequence:
  498.     CCL_MapMultiple (CCL code of this command)
  499.     16 (total number of MAPs and SEPARATORs)
  500.     -7 (1st SEPARATOR)
  501.     MAP-ID11
  502.     MAP-ID12
  503.     -3 (2nd SEPARATOR)
  504.     MAP-ID121
  505.     MAP-ID122
  506.     MAP-ID123
  507.     MAP-ID13
  508.     -7 (3rd SEPARATOR)
  509.     MAP-ID21
  510.     -4 (4th SEPARATOR)
  511.     MAP-ID211
  512.     -1 (5th SEPARATOR)
  513.     MAP_ID2111
  514.     MAP-ID212
  515.     MAP-ID22
  516.  
  517.    A value of each SEPARATOR follows this rule:
  518.     MAP-SET := SEPARATOR [(MAP-ID | MAP-SET)]+
  519.     SEPARATOR := -(number of MAP-IDs and SEPARATORs in the MAP-SET)
  520.  
  521.    (*)....Nest level of MAP-SET must not be over than MAX_MAP_SET_LEVEL.
  522.  
  523.    When some map fails to map (i.e. it doesn't have a value for
  524.    reg[rrr]), the mapping is treated as identity.
  525.  
  526.    The mapping is iterated for all maps in each map set (set of maps
  527.    separated by SEPARATOR) except in the case that lambda is
  528.    encountered.  More precisely, the mapping proceeds as below:
  529.  
  530.    At first, VAL0 is set to reg[rrr], and it is translated by the
  531.    first map to VAL1.  Then, VAL1 is translated by the next map to
  532.    VAL2.  This mapping is iterated until the last map is used.  The
  533.    result of the mapping is the last value of VAL?.
  534.  
  535.    But, when VALm is mapped to VALn and VALn is not a number, the
  536.    mapping proceed as below:
  537.  
  538.    If VALn is nil, the lastest map is ignored and the mapping of VALm
  539.    proceed to the next map.
  540.  
  541.    In VALn is t, VALm is reverted to reg[rrr] and the mapping of VALm
  542.    proceed to the next map.
  543.  
  544.    If VALn is lambda, the whole mapping process terminates, and VALm
  545.    is the result of this mapping.
  546.  
  547.    Each map is a Lisp vector of the following format (a) or (b):
  548.     (a)......[STARTPOINT VAL1 VAL2 ...]
  549.     (b)......[t VAL STARTPOINT ENDPOINT],
  550.    where
  551.     STARTPOINT is an offset to be used for indexing a map,
  552.     ENDPOINT is a maximum index number of a map,
  553.     VAL and VALn is a number, nil, t, or lambda.  
  554.  
  555.    Valid index range of a map of type (a) is:
  556.     STARTPOINT <= index < STARTPOINT + map_size - 1
  557.    Valid index range of a map of type (b) is:
  558.     STARTPOINT <= index < ENDPOINT    */
  559.  
  560. #define CCL_MapMultiple 0x11    /* Mapping by multiple code conversion maps
  561.                      1:ExtendedCOMMNDXXXRRRrrrXXXXX
  562.                      2:N-2
  563.                      3:SEPARATOR_1 (< 0)
  564.                      4:MAP-ID_1
  565.                      5:MAP-ID_2
  566.                      ...
  567.                      M:SEPARATOR_x (< 0)
  568.                      M+1:MAP-ID_y
  569.                      ...
  570.                      N:SEPARATOR_z (< 0)
  571.                       */
  572.  
  573. #define MAX_MAP_SET_LEVEL 20
  574.  
  575. typedef struct
  576. {
  577.   int rest_length;
  578.   int orig_val;
  579. } tr_stack;
  580.  
  581. static tr_stack mapping_stack[MAX_MAP_SET_LEVEL];
  582. static tr_stack *mapping_stack_pointer;
  583.  
  584. #define PUSH_MAPPING_STACK(restlen, orig)                 \
  585. {                                                           \
  586.   mapping_stack_pointer->rest_length = (restlen);         \
  587.   mapping_stack_pointer->orig_val = (orig);               \
  588.   mapping_stack_pointer++;                                \
  589. }
  590.  
  591. #define POP_MAPPING_STACK(restlen, orig)                  \
  592. {                                                           \
  593.   mapping_stack_pointer--;                                \
  594.   (restlen) = mapping_stack_pointer->rest_length;         \
  595.   (orig) = mapping_stack_pointer->orig_val;               \
  596. }                                                           \
  597.  
  598. #define CCL_MapSingle        0x12 /* Map by single code conversion map
  599.                     1:ExtendedCOMMNDXXXRRRrrrXXXXX
  600.                     2:MAP-ID
  601.                     ------------------------------
  602.                     Map reg[rrr] by MAP-ID.
  603.                     If some valid mapping is found,
  604.                       set reg[rrr] to the result,
  605.                     else
  606.                       set reg[RRR] to -1.
  607.                      */
  608.  
  609. /* CCL arithmetic/logical operators. */
  610. #define CCL_PLUS    0x00    /* X = Y + Z */
  611. #define CCL_MINUS    0x01    /* X = Y - Z */
  612. #define CCL_MUL        0x02    /* X = Y * Z */
  613. #define CCL_DIV        0x03    /* X = Y / Z */
  614. #define CCL_MOD        0x04    /* X = Y % Z */
  615. #define CCL_AND        0x05    /* X = Y & Z */
  616. #define CCL_OR        0x06    /* X = Y | Z */
  617. #define CCL_XOR        0x07    /* X = Y ^ Z */
  618. #define CCL_LSH        0x08    /* X = Y << Z */
  619. #define CCL_RSH        0x09    /* X = Y >> Z */
  620. #define CCL_LSH8    0x0A    /* X = (Y << 8) | Z */
  621. #define CCL_RSH8    0x0B    /* X = Y >> 8, r[7] = Y & 0xFF  */
  622. #define CCL_DIVMOD    0x0C    /* X = Y / Z, r[7] = Y % Z */
  623. #define CCL_LS        0x10    /* X = (X < Y) */
  624. #define CCL_GT        0x11    /* X = (X > Y) */
  625. #define CCL_EQ        0x12    /* X = (X == Y) */
  626. #define CCL_LE        0x13    /* X = (X <= Y) */
  627. #define CCL_GE        0x14    /* X = (X >= Y) */
  628. #define CCL_NE        0x15    /* X = (X != Y) */
  629.  
  630. #define CCL_ENCODE_SJIS 0x16    /* X = HIGHER_BYTE (SJIS (Y, Z))
  631.                    r[7] = LOWER_BYTE (SJIS (Y, Z) */
  632. #define CCL_DECODE_SJIS 0x17    /* X = HIGHER_BYTE (DE-SJIS (Y, Z))
  633.                    r[7] = LOWER_BYTE (DE-SJIS (Y, Z)) */
  634.  
  635. /* Terminate CCL program successfully.  */
  636. #define CCL_SUCCESS               \
  637.   do {                       \
  638.     ccl->status = CCL_STAT_SUCCESS;    \
  639.     ccl->ic = CCL_HEADER_MAIN;        \
  640.     goto ccl_finish;               \
  641.   } while (0)
  642.  
  643. /* Suspend CCL program because of reading from empty input buffer or
  644.    writing to full output buffer.  When this program is resumed, the
  645.    same I/O command is executed.  */
  646. #define CCL_SUSPEND(stat)    \
  647.   do {                \
  648.     ic--;            \
  649.     ccl->status = stat;        \
  650.     goto ccl_finish;        \
  651.   } while (0)
  652.  
  653. /* Terminate CCL program because of invalid command.  Should not occur
  654.    in the normal case.  */
  655. #define CCL_INVALID_CMD                 \
  656.   do {                         \
  657.     ccl->status = CCL_STAT_INVALID_CMD;    \
  658.     goto ccl_error_handler;             \
  659.   } while (0)
  660.  
  661. /* Encode one character CH to multibyte form and write to the current
  662.    output buffer.  If CH is less than 256, CH is written as is.  */
  663. #define CCL_WRITE_CHAR(ch)                \
  664.   do {                            \
  665.     if (!dst)                        \
  666.       CCL_INVALID_CMD;                    \
  667.     else                        \
  668.       {                            \
  669.     unsigned char work[4], *str;            \
  670.     int len = CHAR_STRING (ch, work, str);        \
  671.     if (dst + len <= (dst_bytes ? dst_end : src))    \
  672.       {                        \
  673.         bcopy (str, dst, len);            \
  674.         dst += len;                    \
  675.       }                        \
  676.     else                        \
  677.       CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST);    \
  678.       }                            \
  679.   } while (0)
  680.  
  681. /* Write a string at ccl_prog[IC] of length LEN to the current output
  682.    buffer.  */
  683. #define CCL_WRITE_STRING(len)                \
  684.   do {                            \
  685.     if (!dst)                        \
  686.       CCL_INVALID_CMD;                    \
  687.     else if (dst + len <= (dst_bytes ? dst_end : src))    \
  688.       for (i = 0; i < len; i++)                \
  689.     *dst++ = ((XFASTINT (ccl_prog[ic + (i / 3)]))    \
  690.           >> ((2 - (i % 3)) * 8)) & 0xFF;    \
  691.     else                        \
  692.       CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST);        \
  693.   } while (0)
  694.  
  695. /* Read one byte from the current input buffer into Rth register.  */
  696. #define CCL_READ_CHAR(r)            \
  697.   do {                        \
  698.     if (!src)                    \
  699.       CCL_INVALID_CMD;                \
  700.     else if (src < src_end)            \
  701.       r = *src++;                \
  702.     else if (ccl->last_block)            \
  703.       {                        \
  704.         ic = ccl->eof_ic;            \
  705.         goto ccl_repeat;            \
  706.       }                        \
  707.     else                    \
  708.       CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC);    \
  709.   } while (0)
  710.  
  711.  
  712. /* Execute CCL code on SRC_BYTES length text at SOURCE.  The resulting
  713.    text goes to a place pointed by DESTINATION, the length of which
  714.    should not exceed DST_BYTES.  The bytes actually processed is
  715.    returned as *CONSUMED.  The return value is the length of the
  716.    resulting text.  As a side effect, the contents of CCL registers
  717.    are updated.  If SOURCE or DESTINATION is NULL, only operations on
  718.    registers are permitted.  */
  719.  
  720. #ifdef CCL_DEBUG
  721. #define CCL_DEBUG_BACKTRACE_LEN 256
  722. int ccl_backtrace_table[CCL_BACKTRACE_TABLE];
  723. int ccl_backtrace_idx;
  724. #endif
  725.  
  726. struct ccl_prog_stack
  727.   {
  728.     Lisp_Object *ccl_prog;    /* Pointer to an array of CCL code.  */
  729.     int ic;            /* Instruction Counter.  */
  730.   };
  731.  
  732. int
  733. ccl_driver (ccl, source, destination, src_bytes, dst_bytes, consumed)
  734.      struct ccl_program *ccl;
  735.      unsigned char *source, *destination;
  736.      int src_bytes, dst_bytes;
  737.      int *consumed;
  738. {
  739.   register int *reg = ccl->reg;
  740.   register int ic = ccl->ic;
  741.   register int code, field1, field2;
  742.   register Lisp_Object *ccl_prog = ccl->prog;
  743.   unsigned char *src = source, *src_end = src + src_bytes;
  744.   unsigned char *dst = destination, *dst_end = dst + dst_bytes;
  745.   int jump_address;
  746.   int i, j, op;
  747.   int stack_idx = 0;
  748.   /* For the moment, we only support depth 256 of stack.  */ 
  749.   struct ccl_prog_stack ccl_prog_stack_struct[256];
  750.  
  751.   if (ic >= ccl->eof_ic)
  752.     ic = CCL_HEADER_MAIN;
  753.  
  754. #ifdef CCL_DEBUG
  755.   ccl_backtrace_idx = 0;
  756. #endif
  757.  
  758.   for (;;)
  759.     {
  760.     ccl_repeat:
  761. #ifdef CCL_DEBUG
  762.       ccl_backtrace_table[ccl_backtrace_idx++] = ic;
  763.       if (ccl_backtrace_idx >= CCL_DEBUG_BACKTRACE_LEN)
  764.     ccl_backtrace_idx = 0;
  765.       ccl_backtrace_table[ccl_backtrace_idx] = 0;
  766. #endif
  767.  
  768.       if (!NILP (Vquit_flag) && NILP (Vinhibit_quit))
  769.     {
  770.       /* We can't just signal Qquit, instead break the loop as if
  771.              the whole data is processed.  Don't reset Vquit_flag, it
  772.              must be handled later at a safer place.  */
  773.       if (consumed)
  774.         src = source + src_bytes;
  775.       ccl->status = CCL_STAT_QUIT;
  776.       break;
  777.     }
  778.  
  779.       code = XINT (ccl_prog[ic]); ic++;
  780.       field1 = code >> 8;
  781.       field2 = (code & 0xFF) >> 5;
  782.  
  783. #define rrr field2
  784. #define RRR (field1 & 7)
  785. #define Rrr ((field1 >> 3) & 7)
  786. #define ADDR field1
  787. #define EXCMD (field1 >> 6)
  788.  
  789.       switch (code & 0x1F)
  790.     {
  791.     case CCL_SetRegister:    /* 00000000000000000RRRrrrXXXXX */
  792.       reg[rrr] = reg[RRR];
  793.       break;
  794.  
  795.     case CCL_SetShortConst:    /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
  796.       reg[rrr] = field1;
  797.       break;
  798.  
  799.     case CCL_SetConst:    /* 00000000000000000000rrrXXXXX */
  800.       reg[rrr] = XINT (ccl_prog[ic]);
  801.       ic++;
  802.       break;
  803.  
  804.     case CCL_SetArray:    /* CCCCCCCCCCCCCCCCCCCCRRRrrrXXXXX */
  805.       i = reg[RRR];
  806.       j = field1 >> 3;
  807.       if ((unsigned int) i < j)
  808.         reg[rrr] = XINT (ccl_prog[ic + i]);
  809.       ic += j;
  810.       break;
  811.  
  812.     case CCL_Jump:        /* A--D--D--R--E--S--S-000XXXXX */
  813.       ic += ADDR;
  814.       break;
  815.  
  816.     case CCL_JumpCond:    /* A--D--D--R--E--S--S-rrrXXXXX */
  817.       if (!reg[rrr])
  818.         ic += ADDR;
  819.       break;
  820.  
  821.     case CCL_WriteRegisterJump: /* A--D--D--R--E--S--S-rrrXXXXX */
  822.       i = reg[rrr];
  823.       CCL_WRITE_CHAR (i);
  824.       ic += ADDR;
  825.       break;
  826.  
  827.     case CCL_WriteRegisterReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
  828.       i = reg[rrr];
  829.       CCL_WRITE_CHAR (i);
  830.       ic++;
  831.       CCL_READ_CHAR (reg[rrr]);
  832.       ic += ADDR - 1;
  833.       break;
  834.  
  835.     case CCL_WriteConstJump: /* A--D--D--R--E--S--S-000XXXXX */
  836.       i = XINT (ccl_prog[ic]);
  837.       CCL_WRITE_CHAR (i);
  838.       ic += ADDR;
  839.       break;
  840.  
  841.     case CCL_WriteConstReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
  842.       i = XINT (ccl_prog[ic]);
  843.       CCL_WRITE_CHAR (i);
  844.       ic++;
  845.       CCL_READ_CHAR (reg[rrr]);
  846.       ic += ADDR - 1;
  847.       break;
  848.  
  849.     case CCL_WriteStringJump: /* A--D--D--R--E--S--S-000XXXXX */
  850.       j = XINT (ccl_prog[ic]);
  851.       ic++;
  852.       CCL_WRITE_STRING (j);
  853.       ic += ADDR - 1;
  854.       break;
  855.  
  856.     case CCL_WriteArrayReadJump: /* A--D--D--R--E--S--S-rrrXXXXX */
  857.       i = reg[rrr];
  858.       j = XINT (ccl_prog[ic]);
  859.       if ((unsigned int) i < j)
  860.         {
  861.           i = XINT (ccl_prog[ic + 1 + i]);
  862.           CCL_WRITE_CHAR (i);
  863.         }
  864.       ic += j + 2;
  865.       CCL_READ_CHAR (reg[rrr]);
  866.       ic += ADDR - (j + 2);
  867.       break;
  868.  
  869.     case CCL_ReadJump:    /* A--D--D--R--E--S--S-rrrYYYYY */
  870.       CCL_READ_CHAR (reg[rrr]);
  871.       ic += ADDR;
  872.       break;
  873.  
  874.     case CCL_ReadBranch:    /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
  875.       CCL_READ_CHAR (reg[rrr]);
  876.       /* fall through ... */
  877.     case CCL_Branch:    /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
  878.       if ((unsigned int) reg[rrr] < field1)
  879.         ic += XINT (ccl_prog[ic + reg[rrr]]);
  880.       else
  881.         ic += XINT (ccl_prog[ic + field1]);
  882.       break;
  883.  
  884.     case CCL_ReadRegister:    /* CCCCCCCCCCCCCCCCCCCCrrXXXXX */
  885.       while (1)
  886.         {
  887.           CCL_READ_CHAR (reg[rrr]);
  888.           if (!field1) break;
  889.           code = XINT (ccl_prog[ic]); ic++;
  890.           field1 = code >> 8;
  891.           field2 = (code & 0xFF) >> 5;
  892.         }
  893.       break;
  894.  
  895.     case CCL_WriteExprConst:  /* 1:00000OPERATION000RRR000XXXXX */
  896.       rrr = 7;
  897.       i = reg[RRR];
  898.       j = XINT (ccl_prog[ic]);
  899.       op = field1 >> 6;
  900.       ic++;
  901.       goto ccl_set_expr;
  902.  
  903.     case CCL_WriteRegister:    /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
  904.       while (1)
  905.         {
  906.           i = reg[rrr];
  907.           CCL_WRITE_CHAR (i);
  908.           if (!field1) break;
  909.           code = XINT (ccl_prog[ic]); ic++;
  910.           field1 = code >> 8;
  911.           field2 = (code & 0xFF) >> 5;
  912.         }
  913.       break;
  914.  
  915.     case CCL_WriteExprRegister: /* 1:00000OPERATIONRrrRRR000XXXXX */
  916.       rrr = 7;
  917.       i = reg[RRR];
  918.       j = reg[Rrr];
  919.       op = field1 >> 6;
  920.       goto ccl_set_expr;
  921.  
  922.     case CCL_Call:        /* CCCCCCCCCCCCCCCCCCCC000XXXXX */
  923.       {
  924.         Lisp_Object slot;
  925.  
  926.         if (stack_idx >= 256
  927.         || field1 < 0
  928.         || field1 >= XVECTOR (Vccl_program_table)->size
  929.         || (slot = XVECTOR (Vccl_program_table)->contents[field1],
  930.             !CONSP (slot))
  931.         || !VECTORP (XCONS (slot)->cdr))
  932.           {
  933.         if (stack_idx > 0)
  934.           {
  935.             ccl_prog = ccl_prog_stack_struct[0].ccl_prog;
  936.             ic = ccl_prog_stack_struct[0].ic;
  937.           }
  938.         CCL_INVALID_CMD;
  939.           }
  940.         
  941.         ccl_prog_stack_struct[stack_idx].ccl_prog = ccl_prog;
  942.         ccl_prog_stack_struct[stack_idx].ic = ic;
  943.         stack_idx++;
  944.         ccl_prog = XVECTOR (XCONS (slot)->cdr)->contents;
  945.         ic = CCL_HEADER_MAIN;
  946.       }
  947.       break;
  948.  
  949.     case CCL_WriteConstString: /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
  950.       if (!rrr)
  951.         CCL_WRITE_CHAR (field1);
  952.       else
  953.         {
  954.           CCL_WRITE_STRING (field1);
  955.           ic += (field1 + 2) / 3;
  956.         }
  957.       break;
  958.  
  959.     case CCL_WriteArray:    /* CCCCCCCCCCCCCCCCCCCCrrrXXXXX */
  960.       i = reg[rrr];
  961.       if ((unsigned int) i < field1)
  962.         {
  963.           j = XINT (ccl_prog[ic + i]);
  964.           CCL_WRITE_CHAR (j);
  965.         }
  966.       ic += field1;
  967.       break;
  968.  
  969.     case CCL_End:        /* 0000000000000000000000XXXXX */
  970.       if (stack_idx-- > 0)
  971.         {
  972.           ccl_prog = ccl_prog_stack_struct[stack_idx].ccl_prog;
  973.           ic = ccl_prog_stack_struct[stack_idx].ic;
  974.           break;
  975.         }
  976.       CCL_SUCCESS;
  977.  
  978.     case CCL_ExprSelfConst: /* 00000OPERATION000000rrrXXXXX */
  979.       i = XINT (ccl_prog[ic]);
  980.       ic++;
  981.       op = field1 >> 6;
  982.       goto ccl_expr_self;
  983.  
  984.     case CCL_ExprSelfReg:    /* 00000OPERATION000RRRrrrXXXXX */
  985.       i = reg[RRR];
  986.       op = field1 >> 6;
  987.  
  988.     ccl_expr_self:
  989.       switch (op)
  990.         {
  991.         case CCL_PLUS: reg[rrr] += i; break;
  992.         case CCL_MINUS: reg[rrr] -= i; break;
  993.         case CCL_MUL: reg[rrr] *= i; break;
  994.         case CCL_DIV: reg[rrr] /= i; break;
  995.         case CCL_MOD: reg[rrr] %= i; break;
  996.         case CCL_AND: reg[rrr] &= i; break;
  997.         case CCL_OR: reg[rrr] |= i; break;
  998.         case CCL_XOR: reg[rrr] ^= i; break;
  999.         case CCL_LSH: reg[rrr] <<= i; break;
  1000.         case CCL_RSH: reg[rrr] >>= i; break;
  1001.         case CCL_LSH8: reg[rrr] <<= 8; reg[rrr] |= i; break;
  1002.         case CCL_RSH8: reg[7] = reg[rrr] & 0xFF; reg[rrr] >>= 8; break;
  1003.         case CCL_DIVMOD: reg[7] = reg[rrr] % i; reg[rrr] /= i; break;
  1004.         case CCL_LS: reg[rrr] = reg[rrr] < i; break;
  1005.         case CCL_GT: reg[rrr] = reg[rrr] > i; break;
  1006.         case CCL_EQ: reg[rrr] = reg[rrr] == i; break;
  1007.         case CCL_LE: reg[rrr] = reg[rrr] <= i; break;
  1008.         case CCL_GE: reg[rrr] = reg[rrr] >= i; break;
  1009.         case CCL_NE: reg[rrr] = reg[rrr] != i; break;
  1010.         default: CCL_INVALID_CMD;
  1011.         }
  1012.       break;
  1013.  
  1014.     case CCL_SetExprConst:    /* 00000OPERATION000RRRrrrXXXXX */
  1015.       i = reg[RRR];
  1016.       j = XINT (ccl_prog[ic]);
  1017.       op = field1 >> 6;
  1018.       jump_address = ++ic;
  1019.       goto ccl_set_expr;
  1020.  
  1021.     case CCL_SetExprReg:    /* 00000OPERATIONRrrRRRrrrXXXXX */
  1022.       i = reg[RRR];
  1023.       j = reg[Rrr];
  1024.       op = field1 >> 6;
  1025.       jump_address = ic;
  1026.       goto ccl_set_expr;
  1027.  
  1028.     case CCL_ReadJumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
  1029.       CCL_READ_CHAR (reg[rrr]);
  1030.     case CCL_JumpCondExprConst: /* A--D--D--R--E--S--S-rrrXXXXX */
  1031.       i = reg[rrr];
  1032.       op = XINT (ccl_prog[ic]);
  1033.       jump_address = ic++ + ADDR;
  1034.       j = XINT (ccl_prog[ic]);
  1035.       ic++;
  1036.       rrr = 7;
  1037.       goto ccl_set_expr;
  1038.  
  1039.     case CCL_ReadJumpCondExprReg: /* A--D--D--R--E--S--S-rrrXXXXX */
  1040.       CCL_READ_CHAR (reg[rrr]);
  1041.     case CCL_JumpCondExprReg:
  1042.       i = reg[rrr];
  1043.       op = XINT (ccl_prog[ic]);
  1044.       jump_address = ic++ + ADDR;
  1045.       j = reg[XINT (ccl_prog[ic])];
  1046.       ic++;
  1047.       rrr = 7;
  1048.  
  1049.     ccl_set_expr:
  1050.       switch (op)
  1051.         {
  1052.         case CCL_PLUS: reg[rrr] = i + j; break;
  1053.         case CCL_MINUS: reg[rrr] = i - j; break;
  1054.         case CCL_MUL: reg[rrr] = i * j; break;
  1055.         case CCL_DIV: reg[rrr] = i / j; break;
  1056.         case CCL_MOD: reg[rrr] = i % j; break;
  1057.         case CCL_AND: reg[rrr] = i & j; break;
  1058.         case CCL_OR: reg[rrr] = i | j; break;
  1059.         case CCL_XOR: reg[rrr] = i ^ j;; break;
  1060.         case CCL_LSH: reg[rrr] = i << j; break;
  1061.         case CCL_RSH: reg[rrr] = i >> j; break;
  1062.         case CCL_LSH8: reg[rrr] = (i << 8) | j; break;
  1063.         case CCL_RSH8: reg[rrr] = i >> 8; reg[7] = i & 0xFF; break;
  1064.         case CCL_DIVMOD: reg[rrr] = i / j; reg[7] = i % j; break;
  1065.         case CCL_LS: reg[rrr] = i < j; break;
  1066.         case CCL_GT: reg[rrr] = i > j; break;
  1067.         case CCL_EQ: reg[rrr] = i == j; break;
  1068.         case CCL_LE: reg[rrr] = i <= j; break;
  1069.         case CCL_GE: reg[rrr] = i >= j; break;
  1070.         case CCL_NE: reg[rrr] = i != j; break;
  1071.         case CCL_ENCODE_SJIS: ENCODE_SJIS (i, j, reg[rrr], reg[7]); break;
  1072.         case CCL_DECODE_SJIS: DECODE_SJIS (i, j, reg[rrr], reg[7]); break;
  1073.         default: CCL_INVALID_CMD;
  1074.         }
  1075.       code &= 0x1F;
  1076.       if (code == CCL_WriteExprConst || code == CCL_WriteExprRegister)
  1077.         {
  1078.           i = reg[rrr];
  1079.           CCL_WRITE_CHAR (i);
  1080.         }
  1081.       else if (!reg[rrr])
  1082.         ic = jump_address;
  1083.       break;
  1084.  
  1085.     case CCL_Extention:
  1086.       switch (EXCMD)
  1087.         {
  1088.         case CCL_ReadMultibyteChar2:
  1089.           if (!src)
  1090.         CCL_INVALID_CMD;
  1091.           do {
  1092.         if (src >= src_end)
  1093.           {
  1094.             src++;
  1095.             goto ccl_read_multibyte_character_suspend;
  1096.           }
  1097.           
  1098.         i = *src++;
  1099.         if (i == LEADING_CODE_COMPOSITION)
  1100.           {
  1101.             if (src >= src_end)
  1102.               goto ccl_read_multibyte_character_suspend;
  1103.             if (*src == 0xFF)
  1104.               {
  1105.             ccl->private_state = COMPOSING_WITH_RULE_HEAD;
  1106.             src++;
  1107.               }
  1108.             else
  1109.               ccl->private_state = COMPOSING_NO_RULE_HEAD;
  1110.           }
  1111.         if (ccl->private_state != 0)
  1112.           {
  1113.             /* composite character */
  1114.             if (*src < 0xA0)
  1115.               ccl->private_state = 0;
  1116.             else
  1117.               {
  1118.             if (i == 0xA0)
  1119.               {
  1120.                 if (src >= src_end)
  1121.                   goto ccl_read_multibyte_character_suspend;
  1122.                 i = *src++ & 0x7F;
  1123.               }
  1124.             else
  1125.               i -= 0x20;
  1126.  
  1127.             if (COMPOSING_WITH_RULE_RULE == ccl->private_state)
  1128.               {
  1129.                 ccl->private_state = COMPOSING_WITH_RULE_HEAD;
  1130.                 continue;
  1131.               }
  1132.             else if (COMPOSING_WITH_RULE_HEAD == ccl->private_state)
  1133.               ccl->private_state = COMPOSING_WITH_RULE_RULE;
  1134.               }
  1135.           }
  1136.         if (i < 0x80)
  1137.           {
  1138.             /* ASCII */
  1139.             reg[rrr] = i;
  1140.             reg[RRR] = CHARSET_ASCII;
  1141.           }
  1142.         else if (i <= MAX_CHARSET_OFFICIAL_DIMENSION1)
  1143.           {
  1144.             if (src >= src_end)
  1145.               goto ccl_read_multibyte_character_suspend;
  1146.             reg[RRR] = i;
  1147.             reg[rrr] = (*src++ & 0x7F);
  1148.           }
  1149.         else if (i <= MAX_CHARSET_OFFICIAL_DIMENSION2)
  1150.           {
  1151.             if ((src + 1) >= src_end)
  1152.               goto ccl_read_multibyte_character_suspend;
  1153.             reg[RRR] = i;
  1154.             i = (*src++ & 0x7F);
  1155.             reg[rrr] = ((i << 7) | (*src & 0x7F));
  1156.             src++;
  1157.           }
  1158.         else if ((i == LEADING_CODE_PRIVATE_11)
  1159.              || (i == LEADING_CODE_PRIVATE_12))
  1160.           {
  1161.             if ((src + 1) >= src_end)
  1162.               goto ccl_read_multibyte_character_suspend;
  1163.             reg[RRR] = *src++;
  1164.             reg[rrr] = (*src++ & 0x7F);
  1165.           }
  1166.         else if ((i == LEADING_CODE_PRIVATE_21)
  1167.              || (i == LEADING_CODE_PRIVATE_22))
  1168.           {
  1169.             if ((src + 2) >= src_end)
  1170.               goto ccl_read_multibyte_character_suspend;
  1171.             reg[RRR] = *src++;
  1172.             i = (*src++ & 0x7F);
  1173.             reg[rrr] = ((i << 7) | (*src & 0x7F));
  1174.             src++;
  1175.           }
  1176.         else
  1177.           {
  1178.             /* INVALID CODE
  1179.                Returned charset is -1.  */
  1180.             reg[RRR] = -1;
  1181.           }
  1182.           } while (0);
  1183.           break;
  1184.  
  1185.         ccl_read_multibyte_character_suspend:
  1186.           src--;
  1187.           if (ccl->last_block)
  1188.         {
  1189.           ic = ccl->eof_ic;
  1190.           goto ccl_repeat;
  1191.         }
  1192.           else
  1193.         CCL_SUSPEND (CCL_STAT_SUSPEND_BY_SRC);
  1194.  
  1195.           break;
  1196.  
  1197.         case CCL_WriteMultibyteChar2:
  1198.           i = reg[RRR]; /* charset */
  1199.           if (i == CHARSET_ASCII)
  1200.         i = reg[rrr] & 0x7F;
  1201.           else if (i == CHARSET_COMPOSITION)
  1202.         i = MAKE_COMPOSITE_CHAR (reg[rrr]);
  1203.           else if (CHARSET_DIMENSION (i) == 1)
  1204.         i = ((i - 0x70) << 7) | (reg[rrr] & 0x7F);
  1205.           else if (i < MIN_CHARSET_PRIVATE_DIMENSION2)
  1206.         i = ((i - 0x8F) << 14) | reg[rrr];
  1207.           else
  1208.         i = ((i - 0xE0) << 14) | reg[rrr];
  1209.  
  1210.           CCL_WRITE_CHAR (i);
  1211.  
  1212.           break;
  1213.  
  1214.         case CCL_TranslateCharacter:
  1215.           i = reg[RRR]; /* charset */
  1216.           if (i == CHARSET_ASCII)
  1217.         i = reg[rrr] & 0x7F;
  1218.           else if (i == CHARSET_COMPOSITION)
  1219.         {
  1220.           reg[RRR] = -1;
  1221.           break;
  1222.         }
  1223.           else if (CHARSET_DIMENSION (i) == 1)
  1224.         i = ((i - 0x70) << 7) | (reg[rrr] & 0x7F);
  1225.           else if (i < MIN_CHARSET_PRIVATE_DIMENSION2)
  1226.         i = ((i - 0x8F) << 14) | (reg[rrr] & 0x3FFF);
  1227.           else
  1228.         i = ((i - 0xE0) << 14) | (reg[rrr] & 0x3FFF);
  1229.  
  1230.           op = translate_char (GET_TRANSLATION_TABLE (reg[Rrr]),
  1231.                    i, -1, 0, 0);
  1232.           SPLIT_CHAR (op, reg[RRR], i, j);
  1233.           if (j != -1)
  1234.         i = (i << 7) | j;
  1235.           
  1236.           reg[rrr] = i;
  1237.           break;
  1238.  
  1239.         case CCL_TranslateCharacterConstTbl:
  1240.           op = XINT (ccl_prog[ic]); /* table */
  1241.           ic++;
  1242.           i = reg[RRR]; /* charset */
  1243.           if (i == CHARSET_ASCII)
  1244.         i = reg[rrr] & 0x7F;
  1245.           else if (i == CHARSET_COMPOSITION)
  1246.         {
  1247.           reg[RRR] = -1;
  1248.           break;
  1249.         }
  1250.           else if (CHARSET_DIMENSION (i) == 1)
  1251.         i = ((i - 0x70) << 7) | (reg[rrr] & 0x7F);
  1252.           else if (i < MIN_CHARSET_PRIVATE_DIMENSION2)
  1253.         i = ((i - 0x8F) << 14) | (reg[rrr] & 0x3FFF);
  1254.           else
  1255.         i = ((i - 0xE0) << 14) | (reg[rrr] & 0x3FFF);
  1256.  
  1257.           op = translate_char (GET_TRANSLATION_TABLE (op), i, -1, 0, 0);
  1258.           SPLIT_CHAR (op, reg[RRR], i, j);
  1259.           if (j != -1)
  1260.         i = (i << 7) | j;
  1261.           
  1262.           reg[rrr] = i;
  1263.           break;
  1264.  
  1265.         case CCL_IterateMultipleMap:
  1266.           {
  1267.         Lisp_Object map, content, attrib, value;
  1268.         int point, size, fin_ic;
  1269.  
  1270.         j = XINT (ccl_prog[ic++]); /* number of maps. */
  1271.         fin_ic = ic + j;
  1272.         op = reg[rrr];
  1273.         if ((j > reg[RRR]) && (j >= 0))
  1274.           {
  1275.             ic += reg[RRR];
  1276.             i = reg[RRR];
  1277.           }
  1278.         else
  1279.           {
  1280.             reg[RRR] = -1;
  1281.             ic = fin_ic;
  1282.             break;
  1283.           }
  1284.  
  1285.         for (;i < j;i++)
  1286.           {
  1287.  
  1288.             size = XVECTOR (Vcode_conversion_map_vector)->size;
  1289.             point = XINT (ccl_prog[ic++]);
  1290.             if (point >= size) continue;
  1291.             map =
  1292.               XVECTOR (Vcode_conversion_map_vector)->contents[point];
  1293.  
  1294.             /* Check map varidity.  */
  1295.             if (!CONSP (map)) continue;
  1296.             map = XCONS(map)->cdr;
  1297.             if (!VECTORP (map)) continue;
  1298.             size = XVECTOR (map)->size;
  1299.             if (size <= 1) continue;
  1300.  
  1301.             content = XVECTOR (map)->contents[0];
  1302.  
  1303.             /* check map type,
  1304.                [STARTPOINT VAL1 VAL2 ...] or
  1305.                [t ELELMENT STARTPOINT ENDPOINT]  */
  1306.             if (NUMBERP (content))
  1307.               {
  1308.             point = XUINT (content);
  1309.             point = op - point + 1;
  1310.             if (!((point >= 1) && (point < size))) continue;
  1311.             content = XVECTOR (map)->contents[point];
  1312.               }
  1313.             else if (EQ (content, Qt))
  1314.               {
  1315.             if (size != 4) continue;
  1316.             if ((op >= XUINT (XVECTOR (map)->contents[2]))
  1317.                 && (op < XUINT (XVECTOR (map)->contents[3])))
  1318.               content = XVECTOR (map)->contents[1];
  1319.             else
  1320.               continue;
  1321.               }
  1322.             else 
  1323.               continue;
  1324.  
  1325.             if (NILP (content))
  1326.               continue;
  1327.             else if (NUMBERP (content))
  1328.               {
  1329.             reg[RRR] = i;
  1330.             reg[rrr] = XINT(content);
  1331.             break;
  1332.               }
  1333.             else if (EQ (content, Qt) || EQ (content, Qlambda))
  1334.               {
  1335.             reg[RRR] = i;
  1336.             break;
  1337.               }
  1338.             else if (CONSP (content))
  1339.               {
  1340.             attrib = XCONS (content)->car;
  1341.             value = XCONS (content)->cdr;
  1342.             if (!NUMBERP (attrib) || !NUMBERP (value))
  1343.               continue;
  1344.             reg[RRR] = i;
  1345.             reg[rrr] = XUINT (value);
  1346.             break;
  1347.               }
  1348.           }
  1349.         if (i == j)
  1350.           reg[RRR] = -1;
  1351.         ic = fin_ic;
  1352.           }
  1353.           break;
  1354.           
  1355.         case CCL_MapMultiple:
  1356.           {
  1357.         Lisp_Object map, content, attrib, value;
  1358.         int point, size, map_vector_size;
  1359.         int map_set_rest_length, fin_ic;
  1360.  
  1361.         map_set_rest_length =
  1362.           XINT (ccl_prog[ic++]); /* number of maps and separators. */
  1363.         fin_ic = ic + map_set_rest_length;
  1364.         if ((map_set_rest_length > reg[RRR]) && (reg[RRR] >= 0))
  1365.           {
  1366.             ic += reg[RRR];
  1367.             i = reg[RRR];
  1368.             map_set_rest_length -= i;
  1369.           }
  1370.         else
  1371.           {
  1372.             ic = fin_ic;
  1373.             reg[RRR] = -1;
  1374.             break;
  1375.           }
  1376.         mapping_stack_pointer = mapping_stack;
  1377.         op = reg[rrr];
  1378.         PUSH_MAPPING_STACK (0, op);
  1379.         reg[RRR] = -1;
  1380.         map_vector_size = XVECTOR (Vcode_conversion_map_vector)->size;
  1381.         for (;map_set_rest_length > 0;i++, map_set_rest_length--)
  1382.           {
  1383.             point = XINT(ccl_prog[ic++]);
  1384.             if (point < 0)
  1385.               {
  1386.             point = -point;
  1387.             if (mapping_stack_pointer
  1388.                 >= &mapping_stack[MAX_MAP_SET_LEVEL])
  1389.               {
  1390.                 CCL_INVALID_CMD;
  1391.               }
  1392.             PUSH_MAPPING_STACK (map_set_rest_length - point,
  1393.                         reg[rrr]);
  1394.             map_set_rest_length = point + 1;
  1395.             reg[rrr] = op;
  1396.             continue;
  1397.               }
  1398.  
  1399.             if (point >= map_vector_size) continue;
  1400.             map = (XVECTOR (Vcode_conversion_map_vector)
  1401.                ->contents[point]);
  1402.  
  1403.             /* Check map varidity.  */
  1404.             if (!CONSP (map)) continue;
  1405.             map = XCONS (map)->cdr;
  1406.             if (!VECTORP (map)) continue;
  1407.             size = XVECTOR (map)->size;
  1408.             if (size <= 1) continue;
  1409.  
  1410.             content = XVECTOR (map)->contents[0];
  1411.  
  1412.             /* check map type,
  1413.                [STARTPOINT VAL1 VAL2 ...] or
  1414.                [t ELEMENT STARTPOINT ENDPOINT]  */
  1415.             if (NUMBERP (content))
  1416.               {
  1417.             point = XUINT (content);
  1418.             point = op - point + 1;
  1419.             if (!((point >= 1) && (point < size))) continue;
  1420.             content = XVECTOR (map)->contents[point];
  1421.               }
  1422.             else if (EQ (content, Qt))
  1423.               {
  1424.             if (size != 4) continue;
  1425.             if ((op >= XUINT (XVECTOR (map)->contents[2])) &&
  1426.                 (op < XUINT (XVECTOR (map)->contents[3])))
  1427.               content = XVECTOR (map)->contents[1];
  1428.             else
  1429.               continue;
  1430.               }
  1431.             else 
  1432.               continue;
  1433.  
  1434.             if (NILP (content))
  1435.               continue;
  1436.             else if (NUMBERP (content))
  1437.               {
  1438.             op = XINT (content);
  1439.             reg[RRR] = i;
  1440.             i += map_set_rest_length;
  1441.             POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
  1442.               }
  1443.             else if (CONSP (content))
  1444.               {
  1445.             attrib = XCONS (content)->car;
  1446.             value = XCONS (content)->cdr;
  1447.             if (!NUMBERP (attrib) || !NUMBERP (value))
  1448.               continue;
  1449.             reg[RRR] = i;
  1450.             op = XUINT (value);
  1451.             i += map_set_rest_length;
  1452.             POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
  1453.               }
  1454.             else if (EQ (content, Qt))
  1455.               {
  1456.             reg[RRR] = i;
  1457.             op = reg[rrr];
  1458.             i += map_set_rest_length;
  1459.             POP_MAPPING_STACK (map_set_rest_length, reg[rrr]);
  1460.               }
  1461.             else if (EQ (content, Qlambda))
  1462.               {
  1463.             break;
  1464.               }
  1465.             else
  1466.               CCL_INVALID_CMD;
  1467.           }
  1468.         ic = fin_ic;
  1469.           }
  1470.           reg[rrr] = op;
  1471.           break;
  1472.  
  1473.         case CCL_MapSingle:
  1474.           {
  1475.         Lisp_Object map, attrib, value, content;
  1476.         int size, point;
  1477.         j = XINT (ccl_prog[ic++]); /* map_id */
  1478.         op = reg[rrr];
  1479.         if (j >= XVECTOR (Vcode_conversion_map_vector)->size)
  1480.           {
  1481.             reg[RRR] = -1;
  1482.             break;
  1483.           }
  1484.         map = XVECTOR (Vcode_conversion_map_vector)->contents[j];
  1485.         if (!CONSP (map))
  1486.           {
  1487.             reg[RRR] = -1;
  1488.             break;
  1489.           }
  1490.         map = XCONS(map)->cdr;
  1491.         if (!VECTORP (map))
  1492.           {
  1493.             reg[RRR] = -1;
  1494.             break;
  1495.           }
  1496.         size = XVECTOR (map)->size;
  1497.         point = XUINT (XVECTOR (map)->contents[0]);
  1498.         point = op - point + 1;
  1499.         reg[RRR] = 0;
  1500.         if ((size <= 1) ||
  1501.             (!((point >= 1) && (point < size))))
  1502.           reg[RRR] = -1;
  1503.         else
  1504.           {
  1505.             content = XVECTOR (map)->contents[point];
  1506.             if (NILP (content))
  1507.               reg[RRR] = -1;
  1508.             else if (NUMBERP (content))
  1509.               reg[rrr] = XINT (content);
  1510.             else if (EQ (content, Qt))
  1511.               reg[RRR] = i;
  1512.             else if (CONSP (content))
  1513.               {
  1514.             attrib = XCONS (content)->car;
  1515.             value = XCONS (content)->cdr;
  1516.             if (!NUMBERP (attrib) || !NUMBERP (value))
  1517.               continue;
  1518.             reg[rrr] = XUINT(value);
  1519.             break;
  1520.               }
  1521.             else
  1522.               reg[RRR] = -1;
  1523.           }
  1524.           }
  1525.           break;
  1526.           
  1527.         default:
  1528.           CCL_INVALID_CMD;
  1529.         }
  1530.       break;
  1531.  
  1532.     default:
  1533.       CCL_INVALID_CMD;
  1534.     }
  1535.     }
  1536.  
  1537.  ccl_error_handler:
  1538.   if (destination)
  1539.     {
  1540.       /* We can insert an error message only if DESTINATION is
  1541.          specified and we still have a room to store the message
  1542.          there.  */
  1543.       char msg[256];
  1544.       int msglen;
  1545.  
  1546.       switch (ccl->status)
  1547.     {
  1548.     case CCL_STAT_INVALID_CMD:
  1549.       sprintf(msg, "\nCCL: Invalid command %x (ccl_code = %x) at %d.",
  1550.           code & 0x1F, code, ic);
  1551. #ifdef CCL_DEBUG
  1552.       {
  1553.         int i = ccl_backtrace_idx - 1;
  1554.         int j;
  1555.  
  1556.         msglen = strlen (msg);
  1557.         if (dst + msglen <= dst_end)
  1558.           {
  1559.         bcopy (msg, dst, msglen);
  1560.         dst += msglen;
  1561.           }
  1562.  
  1563.         for (j = 0; j < CCL_DEBUG_BACKTRACE_LEN; j++, i--)
  1564.           {
  1565.         if (i < 0) i = CCL_DEBUG_BACKTRACE_LEN - 1;
  1566.         if (ccl_backtrace_table[i] == 0)
  1567.           break;
  1568.         sprintf(msg, " %d", ccl_backtrace_table[i]);
  1569.         msglen = strlen (msg);
  1570.         if (dst + msglen > dst_end)
  1571.           break;
  1572.         bcopy (msg, dst, msglen);
  1573.         dst += msglen;
  1574.           }
  1575.       }
  1576. #endif
  1577.       goto ccl_finish;
  1578.  
  1579.     case CCL_STAT_QUIT:
  1580.       sprintf(msg, "\nCCL: Quited.");
  1581.       break;
  1582.  
  1583.     default:
  1584.       sprintf(msg, "\nCCL: Unknown error type (%d).", ccl->status);
  1585.     }
  1586.  
  1587.       msglen = strlen (msg);
  1588.       if (dst + msglen <= dst_end)
  1589.     {
  1590.       bcopy (msg, dst, msglen);
  1591.       dst += msglen;
  1592.     }
  1593.     }
  1594.  
  1595.  ccl_finish:
  1596.   ccl->ic = ic;
  1597.   if (consumed) *consumed = src - source;
  1598.   return dst - destination;
  1599. }
  1600.  
  1601. /* Setup fields of the structure pointed by CCL appropriately for the
  1602.    execution of compiled CCL code in VEC (vector of integer).  */
  1603. void
  1604. setup_ccl_program (ccl, vec)
  1605.      struct ccl_program *ccl;
  1606.      Lisp_Object vec;
  1607. {
  1608.   int i;
  1609.  
  1610.   ccl->size = XVECTOR (vec)->size;
  1611.   ccl->prog = XVECTOR (vec)->contents;
  1612.   ccl->ic = CCL_HEADER_MAIN;
  1613.   ccl->eof_ic = XINT (XVECTOR (vec)->contents[CCL_HEADER_EOF]);
  1614.   ccl->buf_magnification = XINT (XVECTOR (vec)->contents[CCL_HEADER_BUF_MAG]);
  1615.   for (i = 0; i < 8; i++)
  1616.     ccl->reg[i] = 0;
  1617.   ccl->last_block = 0;
  1618.   ccl->private_state = 0;
  1619.   ccl->status = 0;
  1620. }
  1621.  
  1622. /* Resolve symbols in the specified CCL code (Lisp vector).  This
  1623.    function converts symbols of code conversion maps and character
  1624.    translation tables embeded in the CCL code into their ID numbers.  */
  1625.  
  1626. Lisp_Object
  1627. resolve_symbol_ccl_program (ccl)
  1628.      Lisp_Object ccl;
  1629. {
  1630.   int i, veclen;
  1631.   Lisp_Object result, contents, prop;
  1632.  
  1633.   result = ccl;
  1634.   veclen = XVECTOR (result)->size;
  1635.  
  1636.   /* Set CCL program's table ID */
  1637.   for (i = 0; i < veclen; i++)
  1638.     {
  1639.       contents = XVECTOR (result)->contents[i];
  1640.       if (SYMBOLP (contents))
  1641.     {
  1642.       if (EQ(result, ccl))
  1643.         result = Fcopy_sequence (ccl);
  1644.  
  1645.       prop = Fget (contents, Qtranslation_table_id);
  1646.       if (NUMBERP (prop))
  1647.         {
  1648.           XVECTOR (result)->contents[i] = prop;
  1649.           continue;
  1650.         }
  1651.       prop = Fget (contents, Qcode_conversion_map_id);
  1652.       if (NUMBERP (prop))
  1653.         {
  1654.           XVECTOR (result)->contents[i] = prop;
  1655.           continue;
  1656.         }
  1657.       prop = Fget (contents, Qccl_program_idx);
  1658.       if (NUMBERP (prop))
  1659.         {
  1660.           XVECTOR (result)->contents[i] = prop;
  1661.           continue;
  1662.         }
  1663.     }
  1664.     }
  1665.  
  1666.   return result;
  1667. }
  1668.  
  1669.  
  1670. #ifdef emacs
  1671.  
  1672. DEFUN ("ccl-execute", Fccl_execute, Sccl_execute, 2, 2, 0,
  1673.   "Execute CCL-PROGRAM with registers initialized by REGISTERS.\n\
  1674. \n\
  1675. CCL-PROGRAM is a symbol registered by register-ccl-program,\n\
  1676. or a compiled code generated by `ccl-compile' (for backward compatibility,\n\
  1677. in this case, the execution is slower).\n\
  1678. No I/O commands should appear in CCL-PROGRAM.\n\
  1679. \n\
  1680. REGISTERS is a vector of [R0 R1 ... R7] where RN is an initial value\n\
  1681.  of Nth register.\n\
  1682. \n\
  1683. As side effect, each element of REGISTERS holds the value of\n\
  1684.  corresponding register after the execution.")
  1685.   (ccl_prog, reg)
  1686.      Lisp_Object ccl_prog, reg;
  1687. {
  1688.   struct ccl_program ccl;
  1689.   int i;
  1690.   Lisp_Object ccl_id;
  1691.  
  1692.   if ((SYMBOLP (ccl_prog)) &&
  1693.       (!NILP (ccl_id = Fget (ccl_prog, Qccl_program_idx))))
  1694.     {
  1695.       ccl_prog = XVECTOR (Vccl_program_table)->contents[XUINT (ccl_id)];
  1696.       CHECK_LIST (ccl_prog, 0);
  1697.       ccl_prog = XCONS (ccl_prog)->cdr;
  1698.       CHECK_VECTOR (ccl_prog, 1);
  1699.     }
  1700.   else
  1701.     {
  1702.       CHECK_VECTOR (ccl_prog, 1);
  1703.       ccl_prog = resolve_symbol_ccl_program (ccl_prog);
  1704.     }
  1705.  
  1706.   CHECK_VECTOR (reg, 2);
  1707.   if (XVECTOR (reg)->size != 8)
  1708.     error ("Invalid length of vector REGISTERS");
  1709.  
  1710.   setup_ccl_program (&ccl, ccl_prog);
  1711.   for (i = 0; i < 8; i++)
  1712.     ccl.reg[i] = (INTEGERP (XVECTOR (reg)->contents[i])
  1713.           ? XINT (XVECTOR (reg)->contents[i])
  1714.           : 0);
  1715.  
  1716.   ccl_driver (&ccl, (char *)0, (char *)0, 0, 0, (int *)0);
  1717.   QUIT;
  1718.   if (ccl.status != CCL_STAT_SUCCESS)
  1719.     error ("Error in CCL program at %dth code", ccl.ic);
  1720.  
  1721.   for (i = 0; i < 8; i++)
  1722.     XSETINT (XVECTOR (reg)->contents[i], ccl.reg[i]);
  1723.   return Qnil;
  1724. }
  1725.  
  1726. DEFUN ("ccl-execute-on-string", Fccl_execute_on_string, Sccl_execute_on_string,
  1727.        3, 5, 0,
  1728.   "Execute CCL-PROGRAM with initial STATUS on STRING.\n\
  1729. \n\
  1730. CCL-PROGRAM is a symbol registered by register-ccl-program,\n\
  1731. or a compiled code generated by `ccl-compile' (for backward compatibility,\n\
  1732. in this case, the execution is slower).\n\
  1733. \n\
  1734. Read buffer is set to STRING, and write buffer is allocated automatically.\n\
  1735. \n\
  1736. STATUS is a vector of [R0 R1 ... R7 IC], where\n\
  1737.  R0..R7 are initial values of corresponding registers,\n\
  1738.  IC is the instruction counter specifying from where to start the program.\n\
  1739. If R0..R7 are nil, they are initialized to 0.\n\
  1740. If IC is nil, it is initialized to head of the CCL program.\n\
  1741. \n\
  1742. If optional 4th arg CONTINUE is non-nil, keep IC on read operation\n\
  1743. when read buffer is exausted, else, IC is always set to the end of\n\
  1744. CCL-PROGRAM on exit.\n\
  1745. \n\
  1746. It returns the contents of write buffer as a string,\n\
  1747.  and as side effect, STATUS is updated.\n\
  1748. If the optional 5th arg UNIBYTE-P is non-nil, the returned string\n\
  1749. is a unibyte string.  By default it is a multibyte string.")
  1750.   (ccl_prog, status, str, contin, unibyte_p)
  1751.      Lisp_Object ccl_prog, status, str, contin, unibyte_p;
  1752. {
  1753.   Lisp_Object val;
  1754.   struct ccl_program ccl;
  1755.   int i, produced;
  1756.   int outbufsize;
  1757.   char *outbuf;
  1758.   struct gcpro gcpro1, gcpro2, gcpro3;
  1759.   Lisp_Object ccl_id;
  1760.  
  1761.   if ((SYMBOLP (ccl_prog)) &&
  1762.       (!NILP (ccl_id = Fget (ccl_prog, Qccl_program_idx))))
  1763.     {
  1764.       ccl_prog = XVECTOR (Vccl_program_table)->contents[XUINT (ccl_id)];
  1765.       CHECK_LIST (ccl_prog, 0);
  1766.       ccl_prog = XCONS (ccl_prog)->cdr;
  1767.       CHECK_VECTOR (ccl_prog, 1);
  1768.     }
  1769.   else
  1770.     {
  1771.       CHECK_VECTOR (ccl_prog, 1);
  1772.       ccl_prog = resolve_symbol_ccl_program (ccl_prog);
  1773.     }
  1774.  
  1775.   CHECK_VECTOR (status, 1);
  1776.   if (XVECTOR (status)->size != 9)
  1777.     error ("Invalid length of vector STATUS");
  1778.   CHECK_STRING (str, 2);
  1779.   GCPRO3 (ccl_prog, status, str);
  1780.  
  1781.   setup_ccl_program (&ccl, ccl_prog);
  1782.   for (i = 0; i < 8; i++)
  1783.     {
  1784.       if (NILP (XVECTOR (status)->contents[i]))
  1785.     XSETINT (XVECTOR (status)->contents[i], 0);
  1786.       if (INTEGERP (XVECTOR (status)->contents[i]))
  1787.     ccl.reg[i] = XINT (XVECTOR (status)->contents[i]);
  1788.     }
  1789.   if (INTEGERP (XVECTOR (status)->contents[i]))
  1790.     {
  1791.       i = XFASTINT (XVECTOR (status)->contents[8]);
  1792.       if (ccl.ic < i && i < ccl.size)
  1793.     ccl.ic = i;
  1794.     }
  1795.   outbufsize = STRING_BYTES (XSTRING (str)) * ccl.buf_magnification + 256;
  1796.   outbuf = (char *) xmalloc (outbufsize);
  1797.   if (!outbuf)
  1798.     error ("Not enough memory");
  1799.   ccl.last_block = NILP (contin);
  1800.   produced = ccl_driver (&ccl, XSTRING (str)->data, outbuf,
  1801.              STRING_BYTES (XSTRING (str)), outbufsize, (int *)0);
  1802.   for (i = 0; i < 8; i++)
  1803.     XSET (XVECTOR (status)->contents[i], Lisp_Int, ccl.reg[i]);
  1804.   XSETINT (XVECTOR (status)->contents[8], ccl.ic);
  1805.   UNGCPRO;
  1806.  
  1807.   if (NILP (unibyte_p))
  1808.     val = make_string (outbuf, produced);
  1809.   else
  1810.     val = make_unibyte_string (outbuf, produced);
  1811.   free (outbuf);
  1812.   QUIT;
  1813.   if (ccl.status != CCL_STAT_SUCCESS
  1814.       && ccl.status != CCL_STAT_SUSPEND_BY_SRC
  1815.       && ccl.status != CCL_STAT_SUSPEND_BY_DST)
  1816.     error ("Error in CCL program at %dth code", ccl.ic);
  1817.  
  1818.   return val;
  1819. }
  1820.  
  1821. DEFUN ("register-ccl-program", Fregister_ccl_program, Sregister_ccl_program,
  1822.        2, 2, 0,
  1823.   "Register CCL program PROGRAM of NAME in `ccl-program-table'.\n\
  1824. PROGRAM should be a compiled code of CCL program, or nil.\n\
  1825. Return index number of the registered CCL program.")
  1826.   (name, ccl_prog)
  1827.      Lisp_Object name, ccl_prog;
  1828. {
  1829.   int len = XVECTOR (Vccl_program_table)->size;
  1830.   int i;
  1831.  
  1832.   CHECK_SYMBOL (name, 0);
  1833.   if (!NILP (ccl_prog))
  1834.     {
  1835.       CHECK_VECTOR (ccl_prog, 1);
  1836.       ccl_prog = resolve_symbol_ccl_program (ccl_prog);
  1837.     }
  1838.   
  1839.   for (i = 0; i < len; i++)
  1840.     {
  1841.       Lisp_Object slot = XVECTOR (Vccl_program_table)->contents[i];
  1842.  
  1843.       if (!CONSP (slot))
  1844.     break;
  1845.  
  1846.       if (EQ (name, XCONS (slot)->car))
  1847.     {
  1848.       XCONS (slot)->cdr = ccl_prog;
  1849.       return make_number (i);
  1850.     }
  1851.     }
  1852.  
  1853.   if (i == len)
  1854.     {
  1855.       Lisp_Object new_table = Fmake_vector (make_number (len * 2), Qnil);
  1856.       int j;
  1857.  
  1858.       for (j = 0; j < len; j++)
  1859.     XVECTOR (new_table)->contents[j]
  1860.       = XVECTOR (Vccl_program_table)->contents[j];
  1861.       Vccl_program_table = new_table;
  1862.     }
  1863.  
  1864.   XVECTOR (Vccl_program_table)->contents[i] = Fcons (name, ccl_prog);
  1865.   Fput (name, Qccl_program_idx, make_number (i));
  1866.   return make_number (i);
  1867. }
  1868.  
  1869. /* Register code conversion map.
  1870.    A code conversion map consists of numbers, Qt, Qnil, and Qlambda.
  1871.    The first element is start code point.
  1872.    The rest elements are mapped numbers.
  1873.    Symbol t means to map to an original number before mapping.
  1874.    Symbol nil means that the corresponding element is empty.
  1875.    Symbol lambda menas to terminate mapping here.
  1876. */
  1877.  
  1878. DEFUN ("register-code-conversion-map", Fregister_code_conversion_map,
  1879.        Sregister_code_conversion_map,
  1880.        2, 2, 0,
  1881.   "Register SYMBOL as code conversion map MAP.\n\
  1882. Return index number of the registered map.")
  1883.   (symbol, map)
  1884.      Lisp_Object symbol, map;
  1885. {
  1886.   int len = XVECTOR (Vcode_conversion_map_vector)->size;
  1887.   int i;
  1888.   Lisp_Object index;
  1889.  
  1890.   CHECK_SYMBOL (symbol, 0);
  1891.   CHECK_VECTOR (map, 1);
  1892.   
  1893.   for (i = 0; i < len; i++)
  1894.     {
  1895.       Lisp_Object slot = XVECTOR (Vcode_conversion_map_vector)->contents[i];
  1896.  
  1897.       if (!CONSP (slot))
  1898.     break;
  1899.  
  1900.       if (EQ (symbol, XCONS (slot)->car))
  1901.     {
  1902.       index = make_number (i);
  1903.       XCONS (slot)->cdr = map;
  1904.       Fput (symbol, Qcode_conversion_map, map);
  1905.       Fput (symbol, Qcode_conversion_map_id, index);
  1906.       return index;
  1907.     }
  1908.     }
  1909.  
  1910.   if (i == len)
  1911.     {
  1912.       Lisp_Object new_vector = Fmake_vector (make_number (len * 2), Qnil);
  1913.       int j;
  1914.  
  1915.       for (j = 0; j < len; j++)
  1916.     XVECTOR (new_vector)->contents[j]
  1917.       = XVECTOR (Vcode_conversion_map_vector)->contents[j];
  1918.       Vcode_conversion_map_vector = new_vector;
  1919.     }
  1920.  
  1921.   index = make_number (i);
  1922.   Fput (symbol, Qcode_conversion_map, map);
  1923.   Fput (symbol, Qcode_conversion_map_id, index);
  1924.   XVECTOR (Vcode_conversion_map_vector)->contents[i] = Fcons (symbol, map);
  1925.   return index;
  1926. }
  1927.  
  1928.  
  1929. void
  1930. syms_of_ccl ()
  1931. {
  1932.   staticpro (&Vccl_program_table);
  1933.   Vccl_program_table = Fmake_vector (make_number (32), Qnil);
  1934.  
  1935.   Qccl_program = intern ("ccl-program");
  1936.   staticpro (&Qccl_program);
  1937.  
  1938.   Qccl_program_idx = intern ("ccl-program-idx");
  1939.   staticpro (&Qccl_program_idx);
  1940.  
  1941.   Qcode_conversion_map = intern ("code-conversion-map");
  1942.   staticpro (&Qcode_conversion_map);
  1943.  
  1944.   Qcode_conversion_map_id = intern ("code-conversion-map-id");
  1945.   staticpro (&Qcode_conversion_map_id);
  1946.  
  1947.   DEFVAR_LISP ("code-conversion-map-vector", &Vcode_conversion_map_vector,
  1948.     "Vector of code conversion maps.");
  1949.   Vcode_conversion_map_vector = Fmake_vector (make_number (16), Qnil);
  1950.  
  1951.   DEFVAR_LISP ("font-ccl-encoder-alist", &Vfont_ccl_encoder_alist,
  1952.     "Alist of fontname patterns vs corresponding CCL program.\n\
  1953. Each element looks like (REGEXP . CCL-CODE),\n\
  1954.  where CCL-CODE is a compiled CCL program.\n\
  1955. When a font whose name matches REGEXP is used for displaying a character,\n\
  1956.  CCL-CODE is executed to calculate the code point in the font\n\
  1957.  from the charset number and position code(s) of the character which are set\n\
  1958.  in CCL registers R0, R1, and R2 before the execution.\n\
  1959. The code point in the font is set in CCL registers R1 and R2\n\
  1960.  when the execution terminated.\n\
  1961. If the font is single-byte font, the register R2 is not used.");
  1962.   Vfont_ccl_encoder_alist = Qnil;
  1963.  
  1964.   defsubr (&Sccl_execute);
  1965.   defsubr (&Sccl_execute_on_string);
  1966.   defsubr (&Sregister_ccl_program);
  1967.   defsubr (&Sregister_code_conversion_map);
  1968. }
  1969.  
  1970. #endif  /* emacs */
  1971.