Debian Codegraph @

texk/cjkutils/cjkutils-src/CEFconv/cef5conv.w

raw
1 % This is the cweb file cef5conv.w of the CJK Package Ver. 4.8.5 16-Oct-2021
2
3 % Copyright (C) 1994-2021 Werner Lemberg <wl@@gnu.org>
4 %
5 % This program is free software; you can redistribute it and/or modify
6 % it under the terms of the GNU General Public License as published by
7 % the Free Software Foundation; either version 2 of the License, or
8 % (at your option) any later version.
9 %
10 % This program is distributed in the hope that it will be useful,
11 % but WITHOUT ANY WARRANTY; without even the implied warranty of
12 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 % GNU General Public License for more details.
14 %
15 % You should have received a copy of the GNU General Public License
16 % along with this program in doc/COPYING; if not, write to the Free
17 % Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18 % MA 02110-1301 USA
19
20 % To print this CWEB file you should (but not must) use the CWEAVE of the
21 % c2cweb-package (found at the CTAN archives, e.g. ftp.dante.de) and then say
22 %
23 % cweave +a cef5conv.w
24 %
25 % This (fully compatible) CWEAVE can transform CWEB-files with alternative
26 % output rules (look at the position of braces below!) the author (it's me
27 % too :-) prefer. Otherwise this file will be formatted traditionally.
28
29 \def\title{cef5conv (CJK Version 4.8.5)}
30
31 \def\topofcontents{
32 \null\vfill
33 \centerline{\titlefont The {\ttitlefont cef5conv} program}
34 \vskip 20pt
35 \centerline{(CJK Version 4.8.5)}
36 \vfill}
37
38 \def\botofcontents{
39 \vfill
40 \noindent
41 Copyright \copyright\ 1996, 1998, 2002 by Werner Lemberg
42 \bigskip\noindent
43 Permission is granted to make and distribute verbatim copies of this
44 document provided that the copyright notice and this permission notice
45 are preserved on all copies.
46
47 \smallskip\noindent
48 Permission is granted to copy and distribute modified versions of this
49 document under the conditions for verbatim copying, provided that the
50 entire resulting derived work is distributed under the terms of a
51 permission notice identical to this one.}
52
53 \pageno=\contentspagenumber \advance\pageno by 1
54 \let\maybe=\iftrue
55 \fullpageheight=240mm
56 \pageheight=223mm
57 \pagewidth=158mm
58 \setpage
59 \frenchspacing
60
61 \noinx
62 \nosecs
63 \nocon
64
65 @* Function and Use.
66 This small program will convert Big~5 encoding with CNS encoded Chinese
67 characters using the {\it Chinese Encoding Framework (CEF)} into a
68 `preprocessed' form. The need of this program arises from the fact that
69 Big~5 encoding uses the characters `\.{\\}', `\.{\{}', and `\.{\}}' which
70 have special meanings in \TeX.
71
72 Use this program as a filter:
73
74 \hskip 2em \.{cef5conv < input\_file > output\_file}
75
76
77 @* The program.
78 In contrast to \.{cefconv} two tasks will be executed:
79
80 Replacing all occurrences of Big~5 encoded characters \.{XY} (\.{X} and
81 \.{Y} are the first and the second byte of the character) with
82 \.{\char94 \char94 7fX\char94 \char94 7fZZZ\char94 \char94 7f}, where
83 \.{ZZZ} represents the second byte as a decimal number. \.{0x7F} is used as
84 a control character and a delimiter.
85
86 Replacing CEF macros of the form \.{\&xx-yyzz;} (\.{xx} can be C1--C7 for
87 the CNS planes 1--7, C0 for Big~5 encoding, an encoding CX reserved for
88 IRIZ, a private encoding CY, and U for Unicode encoding; \.{yyzz} is a
89 hexadecimal representation of the code point in this plane) with
90 \medskip
91 \quad\.{\char94 \char94 7f72\char94 \char94 7fXX\char94 \char94 7f%
92 \char94 \char94 7f"0yy\char94 \char94 7f"0zz\char94 \char94 7f}\quad.
93 \medskip
94 \.{XX} is the corresponding CJK encoding of \.{xx}; the number `72'
95 specifies a macro in the file \.{MULEenc.sty} which further processes this
96 representation -- it is automatically loaded by the \.{CJK} package.
97
98 Additionally we define a \TeX\ macro at the very beginning to signal a
99 preprocessed file.
100
101 The following code is very simple. No error detection is done because \TeX\
102 which will see the output of \.{cef5conv} complains loudly if something is
103 wrong.
104
105 @d banner
106 "cef5conv (CJK ver. 4.8.5)"
107
108 @c
109 #include <ctype.h>
110 #include <stdio.h>
111 #include <stdlib.h>
112 #ifdef WIN32
113 #include <fcntl.h>
114 #include <io.h>
115 #endif@#
116
117
118 int main(int argc, char *argv[])
119 {int ch, i;
120 unsigned char in[16];
121 unsigned char out[32];
122 unsigned char *inp, *outp;
123
124 #ifdef WIN32
125 setmode(fileno(stdout), _O_BINARY);
126 #endif
127 fprintf(stdout, "\\def\\CNSpreproc{%s}", banner);@#
128
129 ch = fgetc(stdin);@#
130
131 while(!feof(stdin))
132 {if(ch >= 0xA1 && ch <= 0xFE)
133 {fprintf(stdout, "\177%c\177", ch);@#
134
135 ch = fgetc(stdin);
136 if(!feof(stdin))
137 fprintf(stdout, "%d\177", ch);
138 }
139 else if(ch == '&')
140 /* the macro test is hardcoded to make things simple */
141 {inp = in;
142 outp = out;
143 *inp = ch;
144 *(++inp) = fgetc(stdin);@#
145
146 if(*inp == 'C' && !feof(stdin))
147 {*(++inp) = fgetc(stdin);
148 if(*inp == '0' && !feof(stdin))
149 {*(outp++) = 'B';
150 *(outp++) = 'g';
151 *(outp++) = '5';
152 }
153 else if(*inp >= '1' && *inp <= '7' && !feof(stdin))
154 {*(outp++) = 'C';
155 *(outp++) = 'N';
156 *(outp++) = 'S';
157 *(outp++) = *inp;
158 }
159 else if((*inp == 'X' || *inp == 'Y') && !feof(stdin))
160 {*(outp++) = 'C';
161 *(outp++) = 'E';
162 *(outp++) = 'F';
163 *(outp++) = *inp;
164 }
165 else
166 goto no_macro;
167 }
168 else if(*inp == 'U' && !feof(stdin))
169 {*(outp++) = 'U';
170 *(outp++) = 'T';
171 *(outp++) = 'F';
172 *(outp++) = '8';
173 }
174 else
175 goto no_macro;@#
176
177 *(++inp) = fgetc(stdin);
178 if(*inp != '-' || feof(stdin))
179 goto no_macro;@#
180
181 *(outp++) = '\177';
182 *(outp++) = '\177';
183 *(outp++) = '\"';
184 *(outp++) = '0';@#
185
186 *(++inp) = fgetc(stdin);
187 if(isxdigit(*inp) && *inp < 0x80 && !feof(stdin))
188 *(outp++) = toupper(*inp);
189 else
190 goto no_macro;@#
191
192 *(++inp) = fgetc(stdin);
193 if(isxdigit(*inp) && *inp < 0x80 && !feof(stdin))
194 *(outp++) = toupper(*inp);
195 else
196 goto no_macro;@#
197
198 *(outp++) = '\177';
199 *(outp++) = '\"';
200 *(outp++) = '0';@#
201
202 *(++inp) = fgetc(stdin);
203 if(isxdigit(*inp) && *inp < 0x80 && !feof(stdin))
204 *(outp++) = toupper(*inp);
205 else
206 goto no_macro;@#
207
208 *(++inp) = fgetc(stdin);
209 if(isxdigit(*inp) && *inp < 0x80 && !feof(stdin))
210 *(outp++) = toupper(*inp);
211 else
212 goto no_macro;@#
213
214 *(outp++) = '\177';
215 *outp = '\0';@#
216
217 *(++inp) = fgetc(stdin);
218 if(*inp != ';' || feof(stdin))
219 goto no_macro;@#
220
221 outp = out;
222 fprintf(stdout, "\17772\177");
223 while(*outp)
224 fputc(*(outp++), stdout);@#
225
226 ch = fgetc(stdin);
227 continue;@#
228
229 no_macro:
230 ch = *inp;
231 i = inp - in;
232 inp = in;
233 while(i--)
234 fputc(*(inp++), stdout);
235 continue;
236 }
237 else
238 fputc(ch, stdout);@#
239
240 ch = fgetc(stdin);
241 }
242 exit(EXIT_SUCCESS);
243 return 0; /* never reached */
244 }