-
Notifications
You must be signed in to change notification settings - Fork 1
/
tmpl2c.pl
executable file
·319 lines (298 loc) · 7.46 KB
/
tmpl2c.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/perl
# tmpl2c.pl,v 1.3 1994/02/14 10:47:08 shigeya Exp
#
# Convert template file to C string, prepared for distribute
# Copyright(c)1993 1994 Shigeya Suzuki
#
# jcode.pl included for Japanese handling.
&jcode'init();
while (<>) {
next if /^#/ || /^\s*$/;
if (/^@\s+(.*)/) {
$kitname = $1;
print "char $kitname\[\] = \"\\\n";
while (<>) {
last if /^@/;
&jcode'convert(*_, "euc");
s/"/\\"/g;
s/$/\\n\\/;
print;
}
print "\";\n\n\n";
}
elsif (/^&\s+(.*)/) {
print "struct mestab $1\[\] = {\n";
while (<>) {
last if /^&/;
($pat, $name, $conversion) = split;
$conversion = 0 if $conversion eq "" || $conversion eq "none";
print "\t{ \"$pat\", $name, $conversion}, \n";
}
print "\t{0, 0, 0}\n};\n\n";
}
}
package jcode;
;######################################################################
;#
;# jcode.pl: Japanese character code conversion library
;#
;# Copyright (c) 1991,1992 Software Research Associates, Inc.
;# Original by [email protected], Feb 1992
;#
;# Maintained by Kazumasa Utashiro <[email protected]>
;# Software Research Associates, Inc., Japan
;#
;; $rcsid = q!Id: jcode.pl,v 1.9 1994/02/14 06:16:29 utashiro Exp !;
;#
;######################################################################
;#
;# INTERFACE:
;#
;# &jcode'getcode(*line)
;# Return 'jis', 'sjis', 'euc' or undef according to
;# Japanese character code in $line.
;#
;# &jcode'convert(*line, $ocode [, $icode])
;# Convert the line in any Japanese code to specified
;# code in second argument $ocode. $ocode is one of
;# 'jis', 'sjis' or 'euc'. Input code is recognized
;# automatically from the line itself when $icode is not
;# supplied. $icode also can be specified, but xxx2yyy
;# routine is more efficient when both codes are known.
;#
;# It returns a list of pointer of convert subroutine and
;# input code. It means that this routine returns the
;# input code of the line in scalar context.
;#
;# &jcode'xxx2yyy(*line)
;# Convert Japanese code from xxx to yyy. xxx and yyy
;# are one of "jis", "sjis" or "euc". These subroutines
;# return number of converted substrings. So return
;# value 0 means the line was not converted at all.
;#
;# &jcode'jis_inout($in, $out)
;# Set or inquire JIS start and end sequences. Default
;# is "ESC-$-B" and "ESC-(-B". If you supplied only one
;# character, "ESC-$" or "ESC-(" is added as a prefix
;# for each character respectively. Acutually "ESC-(-B"
;# is not a sequence to end JIS code but a sequence to
;# start ASCII code set. So `in' and `out' are somewhat
;# misleading.
;#
;# &jcode'get_inout($string)
;# Get JIS start and end sequences from $string.
;#
;# $jcode'convf{'xxx', 'yyy'}
;# The value of this associative array is pointer to the
;# subroutine jcode'xxx2yyy().
;#
;# ---------------------------------------------------------------
;#
;# &jcode'init()
;# Initialize the variables used in other functions. You
;# don't have to call this when using jocde.pl by do or
;# require. Call it first if you embedded the jcode.pl
;# in your script.
;#
;######################################################################
;#
;# SAMPLES
;#
;# Convert any Kanji code to JIS and print each line with code name.
;#
;# while (<>) {
;# $code = &jcode'convert(*_, 'jis');
;# print $code, "\t", $_;
;# }
;#
;# Convert all lines to JIS according to the first recognized line.
;#
;# while (<>) {
;# print, next unless /[\033\200-\377]/;
;# (*f, $icode) = &jcode'convert(*_, 'jis');
;# print;
;# defined(&f) || next;
;# while (<>) { &f(*_); print; }
;# last;
;# }
;#
;# The safest way for converting to JIS
;#
;# while (<>) {
;# ($matched, $code) = &jcode'getcode(*_);
;# print, next unless (@buf || $matched);
;# push(@readahead, $_);
;# next unless $code;
;# eval "&jcode'${code}2jis(*_), print while (\$_ = shift(\@buf));";
;# eval "&jcode'${code}2jis(*_), print while (\$_ = <>);";
;# last;
;# }
;#
;######################################################################
;#
;# Call initialize function if not called yet. This sounds strange
;# but this makes easy to embed the jcode.pl in the script. Call
;# &jcode'init at the beginning of the script in that case.
;#
&init unless defined $version;
;#
;# Initialize variables.
;#
sub init {
($version) = ($rcsid =~ /,v ([\d.]+)/);
$re_sjis_c = '[\201-\237\340-\374][\100-\176\200-\374]';
$re_sjis_s = "($re_sjis_c)+";
$re_euc_c = '[\241-\376][\241-\376]';
$re_euc_s = "($re_euc_c)+";
$re_jin = '\033\$[\@B]';
$re_jout = '\033\([BJ]';
&jis_inout("\033\$B", "\033(B");
for $from ('jis', 'sjis', 'euc') {
for $to ('jis', 'sjis', 'euc') {
eval "\$convf{$from, $to} = *${from}2${to};";
}
}
}
;#
;# Set JIS in and out final characters.
;#
sub jis_inout {
$jin = shift || $jin;
$jout = shift || $jout;
$jin = "\033\$".$jin if length($jin) == 1;
$jout = "\033\(".$jout if length($jout) == 1;
($jin, $jout);
}
;#
;# Get JIS in and out sequences from the string.
;#
sub get_inout {
local($jin, $jout);
$_[$[] =~ /$re_jin/o && ($jin = $&);
$_[$[] =~ /$re_jout/o && ($jout = $&);
($jin, $jout);
}
;#
;# Character code recognition
;#
sub getcode {
local(*_) = @_;
return undef unless /[\033\200-\377]/;
return 'jis' if /$re_jin|$re_jout/o;
local($sjis, $euc);
$sjis += length($&) while /$re_sjis_s/go;
$euc += length($&) while /$re_euc_s/go;
(&max($sjis, $euc), ('euc', undef, 'sjis')[($sjis<=>$euc) + $[ + 1]);
}
sub max { $_[ $[ + ($_[$[] < $_[$[+1]) ]; }
;#
;# Convert any code to specified code
;#
sub convert {
local(*_, $ocode, $icode) = @_;
return (undef, undef) unless $icode = $icode || &getcode(*_);
$ocode = 'jis' unless $ocode;
local(*convf) = $convf{$icode, $ocode};
convf(*_);
(*convf, $icode);
}
;#
;# JIS to JIS
;#
sub jis2jis {
local(*_) = @_;
s/$re_jin/$jin/go;
s/$re_jout/$jout/go;
}
;#
;# SJIS to JIS
;#
sub sjis2jis {
local(*_) = @_;
s/$re_sjis_s/&_sjis2jis($&)/geo;
}
sub _sjis2jis {
local($_) = @_;
s/../$s2e{$&}||&s2e($&)/geo;
tr/\241-\376/\041-\176/;
$jin . $_ . $jout;
}
;#
;# EUC to JIS
;#
sub euc2jis {
local(*_) = @_;
s/$re_euc_s/&_euc2jis($&)/geo;
}
sub _euc2jis {
local($_) = @_;
tr/\241-\376/\041-\176/;
$jin . $_ . $jout;
}
;#
;# JIS to EUC
;#
sub jis2euc {
local(*_) = @_;
s/$re_jin([!-~]*)$re_jout/&_jis2euc($1)/geo;
}
sub _jis2euc {
local($_) = @_;
tr/\041-\176/\241-\376/;
$_;
}
;#
;# JIS to SJIS
;#
sub jis2sjis {
local(*_) = @_;
s/$re_jin([!-~]*)$re_jout/&_jis2sjis($1)/geo;
}
sub _jis2sjis {
local($_) = @_;
tr/\041-\176/\241-\376/;
s/../$e2s{$&}||&e2s($&)/ge;
$_;
}
;#
;# SJIS to EUC
;#
sub sjis2euc {
local(*_) = @_;
s/$re_sjis_c/$s2e{$&}||&s2e($&)/geo;
}
sub s2e {
($c1, $c2) = unpack('CC', $code = shift);
if ($c2 >= 0x9f) {
$c1 = $c1 * 2 - ($c1 >= 0xe0 ? 0xe0 : 0x60);
$c2 += 2;
} else {
$c1 = $c1 * 2 - ($c1 >= 0xe0 ? 0xe1 : 0x61);
$c2 += 0x60 + ($c2 < 0x7f);
}
$s2e{$code} = pack('CC', $c1, $c2);
}
;#
;# EUC to SJIS
;#
sub euc2sjis {
local(*_) = @_;
s/$re_euc_c/$e2s{$&}||&e2s($&)/geo;
}
sub e2s {
($c1, $c2) = unpack('CC', $code = shift);
if ($c1 % 2) {
$c1 = ($c1>>1) + ($c1 < 0xdf ? 0x31 : 0x71);
$c2 -= 0x60 + ($c2 < 0xe0);
} else {
$c1 = ($c1>>1) + ($c1 < 0xdf ? 0x30 : 0x70);
$c2 -= 2;
}
$e2s{$code} = pack('CC', $c1, $c2);
}
;#
;# SJIS to SJIS, EUC to EUC
;#
sub sjis2sjis { 0; }
sub euc2euc { 0; }
1;