Debian Codegraph @

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

raw
1 % This is the cweb file cefsconv.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 cefsconv.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{cefsconv (CJK Version 4.8.5)}
30
31 \def\topofcontents{
32 \null\vfill
33 \centerline{\titlefont The {\ttitlefont cefsconv} 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 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 SJIS 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 SJIS encoding uses the characters `\.{\\}', `\.{\{}', and `\.{\}}' which
70 have special meanings in \TeX.
71
72 Use this program as a filter:
73
74 \hskip 2em \.{cefsconv < 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 two byte SJIS encoded characters \.{XY} with
81 \.{\char94 \char94 7fX\char94 \char94 7fZZZ\char94 \char94 7f} (\.{X} and
82 \.{Y} are the first and the second byte of the character; \.{ZZZ} represents
83 the second byte as a decimal number).
84
85 Replacing CEF macros of the form \.{\&xx-yyzz;} (\.{xx} can be C1--C7 for
86 the CNS planes 1--7, C0 for Big~5 encoding, an encoding CX reserved for
87 IRIZ, a private encoding CY, and U for Unicode encoding; \.{yyzz} is a
88 hexadecimal representation of the code point in this plane) with
89 \medskip
90 \quad\.{\char94 \char94 7f72\char94 \char94 7fXX\char94 \char94 7f%
91 \char94 \char94 7f"0yy\char94 \char94 7f"0zz\char94 \char94 7f}\quad.
92 \medskip
93 \.{XX} is the corresponding CJK encoding of \.{xx}; the number `72'
94 specifies a macro in the file \.{MULEenc.sty} which further processes this
95 representation -- it is necessary to explicitly load this file with
96 \.{\\usepackage}.
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 \.{cefsconv} complains loudly if something is
103 wrong.
104
105 Note that the user-defined character area of SJIS (with the first bytes in
106 the range 0xF0--0xFC) is not supported because it is not portable.
107
108 @d banner
109 "cefsconv (CJK ver. 4.8.5)"
110
111 @c
112 #include <ctype.h>
113 #include <stdio.h>
114 #include <stdlib.h>
115 #ifdef WIN32
116 #include <fcntl.h>
117 #include <io.h>
118 #endif@#
119
120
121 int main(int argc, char *argv[])
122 {int ch, i;
123 unsigned char in[16];
124 unsigned char out[32];
125 unsigned char *inp, *outp;
126
127 #ifdef WIN32
128 setmode(fileno(stdout), _O_BINARY);
129 #endif
130 fprintf(stdout, "\\def\\CNSpreproc{%s}", banner);@#
131
132 ch = fgetc(stdin);@#
133
134 while(!feof(stdin))
135 {if((ch >= 0x81 && ch <= 0x9F) || (ch >= 0xE0 && ch <= 0xEF))
136 {fprintf(stdout, "\177%c\177", ch);@#
137
138 ch = fgetc(stdin);
139 if(!feof(stdin))
140 fprintf(stdout, "%d\177", ch);
141 }
142 else if(ch == '&')
143 /* the macro test is hardcoded to make things simple */
144 {inp = in;
145 outp = out;
146 *inp = ch;
147 *(++inp) = fgetc(stdin);@#
148
149 if(*inp == 'C' && !feof(stdin))
150 {*(++inp) = fgetc(stdin);
151 if(*inp == '0' && !feof(stdin))
152 {*(outp++) = 'B';
153 *(outp++) = 'g';
154 *(outp++) = '5';
155 }
156 else if(*inp >= '1' && *inp <= '7' && !feof(stdin))
157 {*(outp++) = 'C';
158 *(outp++) = 'N';
159 *(outp++) = 'S';
160 *(outp++) = *inp;
161 }
162 else if((*inp == 'X' || *inp == 'Y') && !feof(stdin))
163 {*(outp++) = 'C';
164 *(outp++) = 'E';
165 *(outp++) = 'F';
166 *(outp++) = *inp;
167 }
168 else
169 goto no_macro;
170 }
171 else if(*inp == 'U' && !feof(stdin))
172 {*(outp++) = 'U';
173 *(outp++) = 'T';
174 *(outp++) = 'F';
175 *(outp++) = '8';
176 }
177 else
178 goto no_macro;@#
179
180 *(++inp) = fgetc(stdin);
181 if(*inp != '-' || feof(stdin))
182 goto no_macro;@#
183
184 *(outp++) = '\177';
185 *(outp++) = '\177';
186 *(outp++) = '\"';
187 *(outp++) = '0';@#
188
189 *(++inp) = fgetc(stdin);
190 if(isxdigit(*inp) && *inp < 0x80 && !feof(stdin))
191 *(outp++) = toupper(*inp);
192 else
193 goto no_macro;@#
194
195 *(++inp) = fgetc(stdin);
196 if(isxdigit(*inp) && *inp < 0x80 && !feof(stdin))
197 *(outp++) = toupper(*inp);
198 else
199 goto no_macro;@#
200
201 *(outp++) = '\177';
202 *(outp++) = '\"';
203 *(outp++) = '0';@#
204
205 *(++inp) = fgetc(stdin);
206 if(isxdigit(*inp) && *inp < 0x80 && !feof(stdin))
207 *(outp++) = toupper(*inp);
208 else
209 goto no_macro;@#
210
211 *(++inp) = fgetc(stdin);
212 if(isxdigit(*inp) && *inp < 0x80 && !feof(stdin))
213 *(outp++) = toupper(*inp);
214 else
215 goto no_macro;@#
216
217 *(outp++) = '\177';
218 *outp = '\0';@#
219
220 *(++inp) = fgetc(stdin);
221 if(*inp != ';' || feof(stdin))
222 goto no_macro;@#
223
224 outp = out;
225 fprintf(stdout, "\17772\177");
226 while(*outp)
227 fputc(*(outp++), stdout);@#
228
229 ch = fgetc(stdin);
230 continue;@#
231
232 no_macro:
233 ch = *inp;
234 i = inp - in;
235 inp = in;
236 while(i--)
237 fputc(*(inp++), stdout);
238 continue;
239 }
240 else
241 fputc(ch, stdout);@#
242
243 ch = fgetc(stdin);
244 }
245 exit(EXIT_SUCCESS);
246 return 0; /* never reached */
247 }