| 1 |
#!/usr/bin/env perl |
| 2 |
# $XTermId: annotate-enc,v 1.12 2021/02/18 10:41:35 tom Exp $ |
| 3 |
# ----------------------------------------------------------------------------- |
| 4 |
# Copyright 2021 by Thomas E. Dickey |
| 5 |
# |
| 6 |
# All Rights Reserved |
| 7 |
# |
| 8 |
# Permission to use, copy, modify, and distribute this software and its |
| 9 |
# documentation for any purpose and without fee is hereby granted, |
| 10 |
# provided that the above copyright notice appear in all copies and that |
| 11 |
# both that copyright notice and this permission notice appear in |
| 12 |
# supporting documentation, and that the name of the above listed |
| 13 |
# copyright holder(s) not be used in advertising or publicity pertaining |
| 14 |
# to distribution of the software without specific, written prior |
| 15 |
# permission. |
| 16 |
# |
| 17 |
# THE ABOVE LISTED COPYRIGHT HOLDER(S) DISCLAIM ALL WARRANTIES WITH REGARD |
| 18 |
# TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 19 |
# AND FITNESS, IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE |
| 20 |
# LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 21 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 22 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 23 |
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 24 |
# ----------------------------------------------------------------------------- |
| 25 |
# Annotate ".enc" files using the descriptions from the Unicode data file. |
| 26 |
# Also, check that the mapping is unique. |
| 27 |
|
| 28 |
use strict; |
| 29 |
use warnings; |
| 30 |
|
| 31 |
use Getopt::Std; |
| 32 |
|
| 33 |
$| = 1; |
| 34 |
|
| 35 |
our ( $opt_d, $opt_l, $opt_v ); |
| 36 |
our $UnicodeData = "UnicodeData.txt"; |
| 37 |
our %annotations; |
| 38 |
|
| 39 |
sub read_file($) { |
| 40 |
my $path = shift; |
| 41 |
open( FP, $path ) || die "cannot open $path"; |
| 42 |
my (@input) = <FP>; |
| 43 |
close(FP); |
| 44 |
chomp @input; |
| 45 |
return @input; |
| 46 |
} |
| 47 |
|
| 48 |
sub codepoint($) { |
| 49 |
my $code = shift; |
| 50 |
$code =~ s/^0x//i; |
| 51 |
$code = uc $code; |
| 52 |
$code = "0" . $code while ( length($code) < 6 ); |
| 53 |
$code = "0x" . $code; |
| 54 |
return $code; |
| 55 |
} |
| 56 |
|
| 57 |
sub parse_unicode() { |
| 58 |
my @data = &read_file($UnicodeData); |
| 59 |
for my $n ( 0 .. $#data ) { |
| 60 |
next unless ( $data[$n] =~ /^[[:xdigit:]]+;.*;/ ); |
| 61 |
my $code = $data[$n]; |
| 62 |
$code =~ s/;.*//; |
| 63 |
$code = &codepoint($code); |
| 64 |
my $text = $data[$n]; |
| 65 |
$text =~ s/^[^;]*;//; |
| 66 |
$text =~ s/;.*//; |
| 67 |
$text = lc $text if ($opt_l); |
| 68 |
$annotations{$code} = $text; |
| 69 |
printf "'%s' ->%s\n", $code, $text if ($opt_d); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
sub private_use($) { |
| 74 |
my $value = shift; |
| 75 |
my $result = 0; |
| 76 |
my $lo = hex '0xE000'; |
| 77 |
my $hi = hex '0xF8FF'; |
| 78 |
$value = hex $value; |
| 79 |
$result = 1 if ( $value >= $lo and $value <= $hi ); |
| 80 |
return $result; |
| 81 |
} |
| 82 |
|
| 83 |
sub expected($$$$) { |
| 84 |
my $path = shift; |
| 85 |
my $line = shift; |
| 86 |
my $text = shift; |
| 87 |
my $want = shift; |
| 88 |
printf STDERR "expected %s\n", $want; |
| 89 |
printf STDERR "%s:%d:%s\n", $path, 1 + $line, $text; |
| 90 |
} |
| 91 |
|
| 92 |
sub parse_encoding($) { |
| 93 |
my $path = shift; |
| 94 |
my @data = &read_file($path); |
| 95 |
my $state = 0; |
| 96 |
my $ignore = 0; |
| 97 |
my %mapped; |
| 98 |
for my $n ( 0 .. $#data ) { |
| 99 |
my $data = $data[$n]; |
| 100 |
$data =~ s/^\s+//; |
| 101 |
$data =~ s/\s+$//; |
| 102 |
if ( $data =~ /^#/ ) { |
| 103 |
printf "[%d:%d]", $state, $n + 1 if ($opt_v); |
| 104 |
printf "%s\n", $data; |
| 105 |
next; |
| 106 |
} |
| 107 |
if ( $state == 0 ) { |
| 108 |
if ( $data =~ /^STARTENCODING\s+.*$/ ) { |
| 109 |
$state++; |
| 110 |
} |
| 111 |
else { |
| 112 |
&expected( $path, $n, $data, "STARTENCODING" ); |
| 113 |
} |
| 114 |
} |
| 115 |
elsif ( $state == 1 ) { |
| 116 |
if ( $data =~ /^SIZE\s+(0x)?[[:xdigit:]]+$/ ) { |
| 117 |
|
| 118 |
# ignore |
| 119 |
} |
| 120 |
elsif ( $data =~ /^ALIAS\s+[[:alnum:].-]+$/ ) { |
| 121 |
|
| 122 |
# ignore |
| 123 |
} |
| 124 |
elsif ( $data =~ /^STARTMAPPING\s+unicode$/ ) { |
| 125 |
$state++; |
| 126 |
$ignore = 0; |
| 127 |
} |
| 128 |
elsif ( $data =~ /^STARTMAPPING\s+[[:alnum:].-]+$/ ) { |
| 129 |
$state++; |
| 130 |
$ignore = 1; |
| 131 |
} |
| 132 |
else { |
| 133 |
&expected( $path, $n, $data, "STARTMAPPING" ); |
| 134 |
} |
| 135 |
} |
| 136 |
elsif ( $state == 2 ) { |
| 137 |
if ( $data =~ /^ENDMAPPING\b/ ) { |
| 138 |
$state++; |
| 139 |
} |
| 140 |
elsif ($ignore) { |
| 141 |
} |
| 142 |
elsif ( $data =~ /^UNDEFINE(\s+0x[[:xdigit:]]){1,2}/ ) { |
| 143 |
|
| 144 |
# ignore |
| 145 |
} |
| 146 |
elsif ( $data =~ /^0x[[:xdigit:]]+\s+0x[[:xdigit:]]+.*/ ) { |
| 147 |
my $source = $data; |
| 148 |
$source =~ s/\s.*//; |
| 149 |
$source = hex $source; |
| 150 |
my $target = $data; |
| 151 |
$target =~ s/^[^\s]+\s+//; |
| 152 |
$target =~ s/[\s#].*//; |
| 153 |
$target = &codepoint($target); |
| 154 |
if ( $target !~ /^0x[[:xdigit:]]+$/ ) { |
| 155 |
&expected( $path, $n, $data, "hex target, not $target" ); |
| 156 |
next; |
| 157 |
} |
| 158 |
my $expect = ""; |
| 159 |
if ( &private_use($target) ) { |
| 160 |
|
| 161 |
# ignore |
| 162 |
} |
| 163 |
elsif ( $annotations{$target} ) { |
| 164 |
$expect = $annotations{$target}; |
| 165 |
} |
| 166 |
else { |
| 167 |
&expected( $path, $n, $data, "Unicode for \"$target\"" ); |
| 168 |
next; |
| 169 |
} |
| 170 |
my $actual = ""; |
| 171 |
if ( $data =~ /#/ ) { |
| 172 |
$actual = $data; |
| 173 |
$actual =~ s/^[^#]*#\s*//; |
| 174 |
$actual = "" if ( $actual eq "XXX" ); |
| 175 |
} |
| 176 |
if ( $actual ne "" and index( $actual, $expect ) != 0 ) { |
| 177 |
&expected( $path, $n, $data, "Description: $expect" ); |
| 178 |
next; |
| 179 |
} |
| 180 |
$target = hex $target; |
| 181 |
next if ( $target == 0 ); |
| 182 |
if ( $mapped{$target} ) { |
| 183 |
&expected( $path, $n, $data, |
| 184 |
"distinct mapping of $target" ); |
| 185 |
} |
| 186 |
$mapped{$target} = $source; |
| 187 |
$data =~ s/\s*#.*//; |
| 188 |
$data .= "\t# $expect" if ( $expect ne "" ); |
| 189 |
} |
| 190 |
elsif ( $data =~ /^0x[[:xdigit:]]+(\s+0\b)?\s*(#.*)?$/ ) { |
| 191 |
|
| 192 |
#ignore |
| 193 |
} |
| 194 |
else { |
| 195 |
&expected( $path, $n, $data, "mapping" ); |
| 196 |
} |
| 197 |
} |
| 198 |
elsif ( $state == 4 ) { |
| 199 |
if ( $data =~ /^ENDENCODING\b/ ) { |
| 200 |
$state++; |
| 201 |
} |
| 202 |
} |
| 203 |
printf "[%d:%d]", $state, $n + 1 if ($opt_v); |
| 204 |
printf "%s\n", $data; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
sub main::HELP_MESSAGE() { |
| 209 |
printf STDERR <<EOF |
| 210 |
Usage: $0 [options] |
| 211 |
|
| 212 |
Options: |
| 213 |
-d dump $UnicodeData codepoints and descriptions |
| 214 |
-l use lowercase for descriptions from $UnicodeData |
| 215 |
-v print state information on each line |
| 216 |
EOF |
| 217 |
; |
| 218 |
exit 1; |
| 219 |
} |
| 220 |
$Getopt::Std::STANDARD_HELP_VERSION = 1; |
| 221 |
&getopts('dlv') || main::HELP_MESSAGE; |
| 222 |
|
| 223 |
&parse_unicode; |
| 224 |
|
| 225 |
while ( $#ARGV >= 0 ) { |
| 226 |
&parse_encoding( shift @ARGV ); |
| 227 |
} |
| 228 |
|
| 229 |
1; |