Debian Codegraph @

bin/make_encmap

raw ◀ ▶
1 Debian patch touch this file
1 #!perl
2 #
3 # make_encmap
4 #
5 # Copyright (C) 1998 Clark Cooper. All rights reserved.
6 # Copyright (C) 2008-2009, 2014 Steve Hay. All rights reserved.
7 #
8 # This script is free software; you can redistribute it and/or modify it under
9 # the same terms as Perl itself, i.e. under the terms of either the GNU General
10 # Public License or the Artistic License, as specified in the LICENCE file.
11 #
12
13 use 5.008001;
14
15 use strict;
16 use warnings;
17
18 my $name = shift;
19 my $file = shift;
20
21 my $except_str = '$@\^`{}~';
22 my %Exceptions;
23
24 foreach (unpack('c*', $except_str)) {
25 $Exceptions{$_} = 1;
26 }
27
28 if (not defined($name) or not defined($file)) {
29 $! = 2;
30 die "Usage is:\n\tmake_encmap name file\n";
31 }
32
33 my $map;
34 open($map, '<', $file) or do {
35 $! = 3;
36 die "Couldn't open $file";
37 };
38
39 my @byte1;
40 my $minpos = 256;
41
42 while (<$map>) {
43 next if /^\#/;
44
45 next unless /0x([\da-f]{2,4})\s+0x([\da-f]{4})\s*\#\s*(.*)\s*$/i;
46
47 my ($from, $to, $name) = ($1, $2, $3);
48
49 my $flen = length($from);
50 if ($flen == 3) {
51 $! = 4;
52 die "Bad line at $., from must be either 2 or 4 digits:\n$_";
53 }
54
55 my $toval = hex($to);
56 my $f1 = substr($from, 0, 2);
57 my $f1val = hex($f1);
58
59 if ($flen == 2) {
60 if ($f1val < 128) {
61 next if $f1val == $toval;
62
63 warn "The byte '0x$f1' mapped to 0x$to\n"
64 unless defined($Exceptions{$f1val});
65 }
66
67 if (defined($byte1[$f1val])) {
68 if ($byte1[$f1val] != $toval) {
69 $! = 5;
70 die "Multiple mappings for 0x$f1val: $to & " . sprintf("0x%x",
71 $byte1[$f1val]);
72 }
73 }
74 else {
75 $byte1[$f1val] = $toval;
76 $minpos = $f1val
77 if $f1val < $minpos;
78 }
79 }
80 else {
81 my $b1 = $byte1[$f1val];
82
83 if (defined($b1)) {
84 if (not ref($b1)) {
85 $! = 6;
86 die "The 1st byte of '$from' overlaps a single byte definition.";
87 }
88 }
89 else {
90 $b1 = $byte1[$f1val] = [];
91 $minpos = $f1val
92 if $f1val < $minpos;
93 }
94
95 my $f2 = substr($from, 2, 2);
96 my $f2val = hex($f2);
97
98 $b1->[$f2val] = $toval;
99 }
100 }
101
102 close($map);
103
104 if (not $minpos < 256) {
105 $! = 7;
106 die "Minpos never set";
107 }
108
109 print "<encmap name='$name'>\n";
110
111 process_byte(2, $minpos, \@byte1);
112
113 print "</encmap>\n";
114
115 exit 0;
116
117 ####
118 ## End main
119 ####
120
121 sub emit {
122 my ($pre, $start, $lim, $val) = @_;
123
124 my $len = $lim - $start;
125
126 if ($len == 1) {
127 printf("$pre<ch byte='x%02x' uni='x%04x'/>\n", $start, $val);
128 }
129 else {
130 printf("$pre<range byte='x%02x' len='%d' uni='x%04x'/>\n",
131 $start, $len, $val);
132 }
133 } # End emit
134
135 sub process_byte {
136 my ($lead, $minpos, $aref) = @_;
137
138 my $rngstrt;
139 my $rngval;
140 my $i;
141
142 my $prefix = ' ' x $lead;
143
144 for ($i = $minpos; $i <= $#{$aref}; $i++) {
145 my $v = $ {$aref}[$i];
146
147 if (defined($v)) {
148 if (ref($v)) {
149 emit($prefix, $rngstrt, $i, $rngval)
150 if defined($rngstrt);
151
152 $rngstrt = undef;
153 printf "$prefix<prefix byte='x%02x'>\n", $i;
154 process_byte($lead + 2, 0, $v);
155 print "$prefix</prefix>\n";
156 }
157 else {
158 next if (defined($rngstrt) and ($v - $rngval == $i - $rngstrt));
159
160 emit($prefix, $rngstrt, $i, $rngval)
161 if defined($rngstrt);
162
163 $rngstrt = $i;
164 $rngval = $v;
165 }
166 }
167 else {
168 emit($prefix, $rngstrt, $i, $rngval)
169 if defined($rngstrt);
170
171 $rngstrt = undef;
172 }
173 }
174
175 emit($prefix, $rngstrt, $i, $rngval)
176 if defined($rngstrt);
177
178 } # End process_byte
179
180 __END__
181
182 =head1 NAME
183
184 make_encmap - create an XML representation from an Unicode mapping file
185
186 =head1 SYNOPSIS
187
188 make_encmap <name> <file>
189
190 =head1 DESCRIPTION
191
192 B<make_encmap> creates a XML encmap file with a given name from an Unicode
193 mapping file, received e.g. from L<ftp://ftp.unicode.org>. The result by
194 default is output to F<stdout>.
195
196 =head1 ARGUMENTS
197
198 =over 4
199
200 =item E<lt>nameE<gt>
201
202 The name to set in the XML encmap file created.
203
204 =item E<lt>fileE<gt>
205
206 The Unicode mapping file to create the XML encmap file from.
207
208 =back
209
210 =head1 OPTIONS
211
212 I<None>.
213
214 =head1 EXAMPLES
215
216 The following example shows the usage of B<make_encmap> for the ISO/IEC 8859-15
217 table.
218
219 wget ftp://ftp.unicode.org/Public/MAPPINGS/ISO8859/8859-15.TXT
220 make_encmap 8859-15 8859-15.TXT E<gt> 8859-15.encmap
221
222 =head1 EXIT STATUS
223
224 0 The script exited normally.
225
226 2 Invalid command-line arguments.
227
228 >2 An error occurred.
229
230 =head1 KNOWN BUGS
231
232 I<None>.
233
234 =head1 SEE ALSO
235
•236 L<compile_encoding(1p)>,
237 L<XML::Encoding(3pm)>.
238
239 =head1 AUTHOR
240
241 Clark Cooper E<lt>L<[email protected]|mailto:[email protected]>E<gt>.
242
243 Steve Hay E<lt>L<[email protected]|mailto:[email protected]>E<gt> is now maintaining
244 XML::Encoding as of version 2.00.
245
246 This manual page was written by Daniel Leidert
247 E<lt>L<[email protected]|mailto:[email protected]>E<gt> for the Debian
248 project (but may be used by others).
249
250 =head1 COPYRIGHT
251
252 Copyright (C) 1998 Clark Cooper. All rights reserved.
253 Copyright (C) 2008-2009, 2014 Steve Hay. All rights reserved.
254
255 =head1 LICENCE
256
257 This script is free software; you can redistribute it and/or modify it under the
258 same terms as Perl itself, i.e. under the terms of either the GNU General Public
259 License or the Artistic License, as specified in the F<LICENCE> file.
260
261 =head1 VERSION
262
263 Version 2.11
264
265 =head1 DATE
266
267 08 Dec 2020
268
269 =head1 HISTORY
270
271 See the F<Changes> file.
272
273 =cut