]> git.saurik.com Git - apple/icu.git/blame - icuSources/tools/gensprep/filterRFC3454.pl
ICU-6.2.10.tar.gz
[apple/icu.git] / icuSources / tools / gensprep / filterRFC3454.pl
CommitLineData
374ca955
A
1#/usr/bin/perl
2# Copyright (c) 2001-2004 International Business Machines
3# Corporation and others. All Rights Reserved.
4
5####################################################################################
6# filterRFC3454.pl:
7# This tool filters the RFC-3454 txt file for StringPrep tables and creates a table
8# to be used in NamePrepProfile
9#
10# Author: Ram Viswanadha
11#
12####################################################################################
13
14use File::Find;
15use File::Basename;
16use IO::File;
17use Cwd;
18use File::Copy;
19use Getopt::Long;
20use File::Path;
21use File::Copy;
22
23$copyright = "#################\n# This file was generated from RFC 3454 (http://www.ietf.org/rfc/rfc3454.txt)\n# Copyright (C) The Internet Society (2002). All Rights Reserved. \n###################\n\n";
24$warning = "###################\n# WARNING: This table is generated by filterRFC3454.pl tool. DO NOT EDIT \n###################\n\n";
25#run the program)
26main();
27
28#---------------------------------------------------------------------
29# The main program
30
31sub main(){
32 GetOptions(
33 "--sourcedir=s" => \$sourceDir,
34 "--destdir=s" => \$destDir,
35 "--src-filename=s" => \$srcFileName,
36 "--dest-filename=s" => \$destFileName,
37 "--A1" => \$a1,
38 "--B1" => \$b1,
39 "--B2" => \$b2,
40 "--B3" => \$b3,
41 "--C11" => \$c11,
42 "--C12" => \$c12,
43 "--C21" => \$c21,
44 "--C22" => \$c22,
45 "--C3" => \$c3,
46 "--C4" => \$c4,
47 "--C5" => \$c5,
48 "--C6" => \$c6,
49 "--C7" => \$c7,
50 "--C8" => \$c8,
51 "--C9" => \$c9,
52 "--ldh-chars" => \$writeLDHChars,
53 "--iscsi" => \$writeISCSIChars,
54 );
55 usage() unless defined $sourceDir;
56 usage() unless defined $destDir;
57 usage() unless defined $srcFileName;
58 usage() unless defined $destFileName;
59
60 $infile = $sourceDir."/".$srcFileName;
61 $inFH = IO::File->new($infile,"r")
62 or die "could not open the file $infile for reading: $! \n";
63 $outfile = $destDir."/".$destFileName;
64
65 unlink($outfile);
66 $outFH = IO::File->new($outfile,"a")
67 or die "could not open the file $outfile for writing: $! \n";
68 print $outFH $copyright;
69 print $outFH $warning;
70 close($outFH);
71
72 if(defined $b2 && defined $b3){
73 die "ERROR: --B2 and --B3 are both specified\!\n";
74 }
75
76 while(defined ($line=<$inFH>)){
77 next unless $line=~ /Start\sTable/;
78 if($line =~ /A.1/){
79 createUnassignedTable($inFH,$outfile);
80 }
81 if($line =~ /B.1/ && defined $b1){
82 createMapToNothing($inFH,$outfile);
83 }
84 if($line =~ /B.2/ && defined $b2){
85 createCaseMapNorm($inFH,$outfile);
86 }
87 if($line =~ /B.3/ && defined $b3){
88 createCaseMapNoNorm($inFH,$outfile);
89 }
90 if($line =~ /C.1.1/ && defined $c11 ){
91 createProhibitedTable($inFH,$outfile,$line);
92 }
93 if($line =~ /C.1.2/ && defined $c12 ){
94 createProhibitedTable($inFH,$outfile,$line);
95 }
96 if($line =~ /C.2.1/ && defined $c21 ){
97 createProhibitedTable($inFH,$outfile,$line);
98 }
99 if($line =~ /C.2.2/ && defined $c22 ){
100 createProhibitedTable($inFH,$outfile,$line);
101 }
102 if($line =~ /C.3/ && defined $c3 ){
103 createProhibitedTable($inFH,$outfile,$line);
104 }
105 if($line =~ /C.4/ && defined $c4 ){
106 createProhibitedTable($inFH,$outfile,$line);
107 }
108 if($line =~ /C.5/ && defined $c5 ){
109 createProhibitedTable($inFH,$outfile,$line);
110 }
111 if($line =~ /C.6/ && defined $c6 ){
112 createProhibitedTable($inFH,$outfile,$line);
113 }
114 if($line =~ /C.7/ && defined $c7 ){
115 createProhibitedTable($inFH,$outfile,$line);
116 }
117 if($line =~ /C.8/ && defined $c8 ){
118 createProhibitedTable($inFH,$outfile,$line);
119 }
120 if($line =~ /C.9/ && defined $c9 ){
121 createProhibitedTable($inFH,$outfile,$line);
122 }
123 }
124 if( defined $writeISCSIChars){
125 create_iSCSIExtraProhibitedTable($inFH, $outfile);
126 }
127 close($inFH);
128}
129
130#-----------------------------------------------------------------------
131sub readPrint{
132 local ($inFH, $outFH,$comment, $table) = @_;
133 $count = 0;
134 print $outFH $comment."\n";
135 while(defined ($line = <$inFH>)){
136 next if $line =~ /Hoffman\s\&\sBlanchet/; # ignore heading
137 next if $line =~ /RFC\s3454/; # ignore heading
138 next if $line =~ /\f/; # ignore form feed
139 next if $line eq "\n"; # ignore blank lines
140 # break if "End Table" is found
141 if( $line =~ /End\sTable/){
142 print $outFH "\n# Total code points $count\n\n";
143 return;
144 }
145 if($print==1){
146 print $line;
147 }
148 $line =~ s/-/../;
149 $line =~ s/^\s+//;
150 if($line =~ /\;/){
151 }else{
152 $line =~ s/$/;/;
153 }
154 if($table =~ /A/ ){
155 ($code, $noise) = split /;/ , $line;
156 $line = $code."; ; UNASSIGNED\n";
157 }elsif ( $table =~ /B\.1/ ){
158 $line =~ s/Map to nothing/MAP/;
159 }elsif ( $table =~ /B\.[23]/ ){
160 $line =~ s/Case map/MAP/;
161 $line =~ s/Additional folding/MAP/;
162 }elsif ( $table =~ /C/ ) {
163 ($code, $noise) = split /;/ , $line;
164 $line = $code."; ; PROHIBITED\n";
165 }
166 if($line =~ /\.\./){
167 ($code, $noise) = split /;/ , $line;
168 ($startStr, $endStr ) = split /\.\./, $code;
169 $start = atoi($startStr);
170 $end = atoi($endStr);
171 #print $start." ".$end."\n";
172 while($start <= $end){
173 $count++;
174 $start++;
175 }
176 }else{
177 $count++;
178 }
179 print $outFH $line;
180 }
181}
182#-----------------------------------------------------------------------
183sub atoi {
184 my $t;
185 foreach my $d (split(//, shift())) {
186 $t = $t * 16 + $d;
187 }
188 return $t;
189}
190#-----------------------------------------------------------------------
191sub createUnassignedTable{
192 ($inFH,$outfile) = @_;
193 $outFH = IO::File->new($outfile,"a")
194 or die "could not open the file $outfile for writing: $! \n";
195 $comment = "# This table contains code points from Table A.1 from RFC 3454\n";
196 readPrint($inFH,$outFH, $comment, "A");
197 close($outFH);
198}
199#-----------------------------------------------------------------------
200sub createMapToNothing{
201 ($inFH,$outfile) = @_;
202 $outFH = IO::File->new($outfile,"a")
203 or die "could not open the file $outfile for writing: $! \n";
204 $comment = "# This table contains code points from Table B.1 from RFC 3454\n";
205 readPrint($inFH,$outFH,$comment, "B.1");
206 close($outFH);
207}
208#-----------------------------------------------------------------------
209sub createCaseMapNorm{
210 ($inFH,$outfile) = @_;
211 $outFH = IO::File->new($outfile,"a")
212 or die "could not open the file $outfile for writing: $! \n";
213 $comment = $warning."# This table contains code points from Table B.2 from RFC 3454\n";
214 readPrint($inFH,$outFH,$comment, "B.2");
215 close($outFH);
216}
217#-----------------------------------------------------------------------
218sub createCaseMapNoNorm{
219 ($inFH,$outfile) = @_;
220 $outFH = IO::File->new($outfile,"a")
221 or die "could not open the file $outfile for writing: $! \n";
222 $comment = $warning."# This table contains code points from Table B.3 from RFC 3454\n";
223 readPrint($inFH,$outFH,$comment, "B.3");
224 close($outFH);
225}
226#-----------------------------------------------------------------------
227sub createProhibitedTable{
228 ($inFH,$outfile,$line) = @_;
229 $line =~ s/Start//;
230 $line =~ s/-//g;
231 $comment = "# code points from $line";
232
233 $outFH = IO::File->new($outfile, "a")
234 or die "could not open the file $outfile for writing: $! \n";
235 readPrint($inFH,$outFH,$comment, "C");
236 close($outFH);
237}
238
239#-----------------------------------------------------------------------
240sub create_iSCSIExtraProhibitedTable{
241 ($inFH,$outfile,$line) = @_;
242 $comment ="# Additional prohibitions from draft-ietf-ips-iscsi-string-prep-06.txt\n";
243
244 $outFH = IO::File->new($outfile, "a")
245 or die "could not open the file $outfile for writing: $! \n";
246 print $outFH $comment;
247 print $outFH "0021..002C; ; PROHIBITED\n";
248 print $outFH "002F; ; PROHIBITED\n";
249 print $outFH "003B..0040; ; PROHIBITED\n";
250 print $outFH "005B..0060; ; PROHIBITED\n";
251 print $outFH "007B..007E; ; PROHIBITED\n";
252 print $outFH "3002; ; PROHIBITED\n";
253 print $outFH "\n# Total code points 30\n";
254 close($outFH);
255}
256#-----------------------------------------------------------------------
257sub usage {
258 print << "END";
259Usage:
260filterRFC3454.pl
261Options:
262 --sourcedir=<directory>
263 --destdir=<directory>
264 --src-filename=<name of RFC file>
265 --dest-filename=<name of destination file>
266 --A1 Generate data for table A.1
267 --B1 Generate data for table B.1
268 --B2 Generate data for table B.2
269 --B3 Generate data for table B.3
270 --C11 Generate data for table C.1.1
271 --C12 Generate data for table C.1.2
272 --C21 Generate data for table C.2.1
273 --C22 Generate data for table C.2.2
274 --C3 Generate data for table C.3
275 --C4 Generate data for table C.4
276 --C5 Generate data for table C.5
277 --C6 Generate data for table C.6
278 --C7 Generate data for table C.7
279 --C8 Generate data for table C.8
280 --C9 Generate data for table C.9
281 --iscsi Generate data for extra prohibited iSCSI chars
282
283Note, --B2 and --B3 are mutually exclusive.
284
285e.g.: filterRFC3454.pl --sourcedir=. --destdir=./output --src-filename=rfc3454.txt --dest-filename=NamePrepProfile.txt --A1 --B1 --B2 --C12 --C22 --C3 --C4 --C5 --C6 --C7 --C8 --C9
286
287filterRFC3454.pl filters the RFC file and creates String prep table files.
288The RFC text can be downloaded from ftp://ftp.rfc-editor.org/in-notes/rfc3454.txt
289
290END
291 exit(0);
292}
293
294