File: sbcsgen.pl

package info (click to toggle)
halibut 1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,060 kB
  • sloc: ansic: 59,012; perl: 197; lisp: 76; makefile: 50; sh: 1
file content (178 lines) | stat: -rw-r--r-- 4,685 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env perl -w

# This script generates sbcsdat.c (the data for all the SBCSes) from its
# source form sbcs.dat.

$infile = "sbcs.dat";
$outfile = undef;
$outheader = undef;

my $doing_opts = 1;
my $nargs = 0;
while (@ARGV) {
    if ($doing_opts && $ARGV[0] =~ m/^-/) {
        if ($ARGV[0] =~ m/^--source=(.*)$/) {
            $outfile = $1;
            shift;
        } elsif ($ARGV[0] =~ m/^--header=(.*)$/) {
            $outheader = $1;
            shift;
        } elsif ($ARGV[0] eq "--") {
            $doing_opts = 0;
            shift;
        } else {
            die "unrecognised option '$ARGV[0]'\n";
        }
        next;
    }
    if ($nargs++ == 0) {
        $infile = $ARGV[0];
        shift;
    } else {
        die "spurious extra argument '$ARGV[0]'\n";
    }
}

die "usage: sbcsgen.pl ( --source=SRCFILE | --header=HDRFILE ) [INFILE]\n"
    unless defined $outfile or defined $outheader;

open INFH, $infile;

my $charsetname = undef;
my @vals = ();

my @charsetnames = ();
my @sortpriority = ();

if (defined $outfile) {
    open SOURCEFH, ">", $outfile;
    select SOURCEFH;

    print "/*\n";
    print " * sbcsdat.c - data definitions for single-byte character sets.\n";
    print " *\n";
    print " * Generated by sbcsgen.pl from sbcs.dat.\n";
    print " * You should edit those files rather than editing this one.\n";
    print " */\n";
    print "\n";
    print "#ifndef ENUM_CHARSETS\n";
    print "\n";
    print "#include \"charset.h\"\n";
    print "#include \"internal.h\"\n";
    print "\n";
}

while (<INFH>) {
    chomp;
    y/\r\n//; # robustness in the face of strange line endings
    if (/^(charset|tables) (.*)$/) {
        $tables_only = ($1 eq "tables");
	$charsetname = $2;
	@vals = ();
	@sortpriority = map { 0 } 0..255;
    } elsif (/^sortpriority ([^-]*)-([^-]*) (.*)$/) {
	for ($i = hex $1; $i <= hex $2; $i++) {
	    $sortpriority[$i] += $3;
	}
    } elsif (/^[0-9a-fA-FX]/) {
	push @vals, map { $_ eq "XXXX" ? -1 : hex $_ } split / +/, $_;
	if (scalar @vals > 256) {
	    die "$infile:$.: charset $charsetname has more than 256 values\n";
	} elsif (scalar @vals == 256) {
	    &outcharset($charsetname, \@vals, \@sortpriority, $tables_only)
                if defined $outfile;
	    push @charsetnames, $charsetname unless $tables_only;
	    $charsetname = undef;
	    @vals = ();
	    @sortpriority = map { 0 } 0..255;
	}
    }
}

if (defined $outfile) {
    print "#else /* ENUM_CHARSETS */\n";
    print "\n";

    foreach $i (@charsetnames) {
        print "ENUM_CHARSET($i)\n";
    }

    print "\n";
    print "#endif /* ENUM_CHARSETS */\n";

    close SOURCEFH;
}

if (defined $outheader) {
    open HEADERFH, ">", $outheader;
    select HEADERFH;

    print "/*\n";
    print " * sbcsdat.h - header file for SBCS data structures.\n";
    print " *\n";
    print " * Generated by sbcsgen.pl from sbcs.dat.\n";
    print " * You should edit those files rather than editing this one.\n";
    print " */\n";
    print "\n";
    print "#ifndef charset_sbcsdat_h\n";
    print "#define charset_sbcsdat_h\n";
    print "\n";
    print "#include \"charset.h\"\n";
    print "#include \"internal.h\"\n";
    print "\n";
    foreach $i (@charsetnames) {
        print "extern const sbcs_data sbcsdata_$i;\n";
    }
    print "\n";
    print "#endif /* charset_sbcsdat_h */\n";

    close HEADERFH;
}

sub outcharset($$$$) {
    my ($name, $vals, $sortpriority, $tables_only) = @_;
    my ($prefix, $i, @sorted);

    print "const sbcs_data sbcsdata_$name = {\n";
    print "    {\n";
    $prefix = "    ";
    @sorted = ();
    for ($i = 0; $i < 256; $i++) {
	if ($vals->[$i] < 0) {
	    printf "%sERROR ", $prefix;
	} else {
	    printf "%s0x%04x", $prefix, $vals->[$i];
	    die "ooh? $i\n" unless defined $sortpriority->[$i];
	    push @sorted, [$i, $vals->[$i], 0+$sortpriority->[$i]];
	}
	if ($i % 8 == 7) {
	    $prefix = ",\n    ";
	} else {
	    $prefix = ", ";
	}
    }
    print "\n    },\n    {\n";
    @sorted = sort { ($a->[1] == $b->[1] ?
	              $b->[2] <=> $a->[2] :
	              $a->[1] <=> $b->[1]) ||
                     $a->[0] <=> $b->[0] } @sorted;
    $prefix = "    ";
    $uval = -1;
    for ($i = $j = 0; $i < scalar @sorted; $i++) {
	next if ($uval == $sorted[$i]->[1]); # low-priority alternative
	$uval = $sorted[$i]->[1];
	printf "%s0x%02x", $prefix, $sorted[$i]->[0];
	if ($j % 8 == 7) {
	    $prefix = ",\n    ";
	} else {
	    $prefix = ", ";
	}
	$j++;
    }
    printf "\n    },\n    %d\n", $j;
    print "};\n";
    unless ($tables_only) {
        print "const charset_spec charset_$name = {\n" .
            "    $name, read_sbcs, write_sbcs, &sbcsdata_$name\n};\n\n";
    }
}