2 # I'm assuming that you're running this on some kind of ASCII system, but
3 # it will generate EDCDIC too. (TODO)
7 my @lines = grep {!/^#/} <DATA>;
10 my ($arrays, $chrmap, $letter, $arrayname, $noone, $nocsum, $size,
12 my $line = "/* $letter */ $size";
13 $line .= " | PACK_SIZE_CANNOT_ONLY_ONE" if $noone;
14 $line .= " | PACK_SIZE_CANNOT_CSUM" if $nocsum;
17 $line = [$condition, $line] if $condition;
18 $arrays->{$arrayname}->[ord $chrmap->{$letter}] = $line;
19 # print ord $chrmap->{$letter}, " $line\n";
27 my ($letter, $shriek, $noone, $nocsum, $size, $condition)
28 = /^([A-Za-z])(!?)\t(\S*)\t(\S*)\t([^\t\n]+)(?:\t+(.*))?$/;
29 die "Can't parse '$_'" unless $size;
31 if (defined $condition) {
32 $condition = join " && ", map {"defined($_)"} split ' ', $condition;
34 unless ($size =~ s/^=//) {
35 $size = "sizeof($size)";
38 addline (\%arrays, $chrmap, $letter, $shriek ? 'shrieking' : 'normal',
39 $noone, $nocsum, $size, $condition);
43 foreach my $arrayname (sort keys %arrays) {
44 my $array = $arrays{$arrayname};
45 die "No defined entries in $arrayname" unless $array->[$#$array];
46 # Find the first used entry
48 $earliest++ while (!$array->[$earliest]);
49 # Remove all the empty elements.
50 splice @$array, 0, $earliest;
51 print "unsigned char size_${arrayname}[", scalar @$array, "] = {\n";
54 # There is an assumption here that the last entry isn't conditonal
56 push @lines, "#if $_->[0]", " $_->[1]", "#else", " 0,", "#endif";
58 push @lines, $_ ? " $_" : " 0,";
61 # remove the last, annoying, comma
62 die "Last entry was a conditional: '$lines[$#lines]'"
63 unless $lines[$#lines] =~ s/,$//;
64 print "$_\n" foreach @lines;
66 $earliest{$arrayname} = $earliest;
69 print "struct packsize_t packsize[2] = {\n";
72 foreach (qw(normal shrieking)) {
73 my $array = $arrays{$_};
74 push @lines, " {size_$_, $earliest{$_}, " . (scalar @$array) . "},";
76 # remove the last, annoying, comma
78 print "$_\n" foreach @lines;
82 my %asciimap = (map {chr $_, chr $_} 0..255);
83 my %ebcdicmap = (map {chr $_, Encode::encode ("posix-bc", chr $_)} 0..255);
89 output_tables (\%asciimap, @lines);
92 /* EBCDIC (or bust) */
94 output_tables (\%ebcdicmap, @lines);
98 #Symbol nooone nocsum size
131 D =LONG_DOUBLESIZE HAS_LONG_DOUBLE USE_LONG_DOUBLE