xref: /freebsd/contrib/less/mkutable (revision b2ea244070ff84eab79e04befb7aa30c982fc84d)
1a15691bfSXin LI#! /usr/bin/perl
2a15691bfSXin LIuse strict;
3a15691bfSXin LI
4a15691bfSXin LImy $USAGE = <<__EOF__;
5a15691bfSXin LI   usage: mkutable [-n] [-f#] type... [--] [<] UnicodeData.txt
6a15691bfSXin LI          -n = take non-matching types
7a15691bfSXin LI      -f = zero-based type field (default 2)
8a15691bfSXin LI__EOF__
9a15691bfSXin LI
10a15691bfSXin LIuse vars qw( $opt_f $opt_n );
11a15691bfSXin LIuse Getopt::Std;
12a15691bfSXin LImy $type_field = 2;
13a15691bfSXin LI
14*b2ea2440SXin LI# Override Unicode tables for certain control chars
15*b2ea2440SXin LI# that are expected to be found in normal text files.
16*b2ea2440SXin LImy %force_space = (
17*b2ea2440SXin LI    0x08 => 1, # backspace
18*b2ea2440SXin LI    0x09 => 1, # tab
19*b2ea2440SXin LI    0x0a => 1, # newline
20*b2ea2440SXin LI    0x0c => 1, # form feed
21*b2ea2440SXin LI    0x0d => 1, # carriage return
22*b2ea2440SXin LI);
23*b2ea2440SXin LI
24f6b74a7dSXin LIexit (main() ? 0 : 1);
25a15691bfSXin LI
26a15691bfSXin LIsub main {
27a15691bfSXin LI    my $date = `date`;
28a15691bfSXin LI    chomp $date;
29a15691bfSXin LI    my $args = join ' ', @ARGV;
30a15691bfSXin LI    my $header = "/* Generated by \"$0 $args\" on $date */\n";
31a15691bfSXin LI
32a15691bfSXin LI    die $USAGE if not getopts('f:n');
33a15691bfSXin LI    $type_field = $opt_f if $opt_f;
34a15691bfSXin LI    my %types;
35a15691bfSXin LI    my $arg;
36a15691bfSXin LI    while ($arg = shift @ARGV) {
37a15691bfSXin LI        last if $arg eq '--';
38a15691bfSXin LI        $types{$arg} = 1;
39a15691bfSXin LI    }
40a15691bfSXin LI    my %out = ( 'types' => \%types );
41a15691bfSXin LI
42a15691bfSXin LI    print $header;
43f6b74a7dSXin LI    my $last_code = 0;
44a15691bfSXin LI    while (<>) {
45a15691bfSXin LI        chomp;
46a15691bfSXin LI        s/#.*//;
47a15691bfSXin LI        my @fields = split /;/;
48a15691bfSXin LI        next if not @fields;
49f6b74a7dSXin LI        my ($lo_code, $hi_code);
50f6b74a7dSXin LI        my $codes = $fields[0];
51f6b74a7dSXin LI        if ($codes =~ /(\w+)\.\.(\w+)/) {
52f6b74a7dSXin LI            $lo_code = hex $1;
53f6b74a7dSXin LI            $hi_code = hex $2;
54f6b74a7dSXin LI        } else {
55f6b74a7dSXin LI            $lo_code = $hi_code = hex $fields[0];
56f6b74a7dSXin LI        }
57a15691bfSXin LI        my $type = $fields[$type_field];
58a15691bfSXin LI        $type =~ s/\s//g;
59f6b74a7dSXin LI        for ($last_code = $lo_code; $last_code <= $hi_code; ++$last_code) {
60*b2ea2440SXin LI            $type = 'Zs' if $force_space{$last_code};
61f6b74a7dSXin LI            output(\%out, $last_code, $type);
62a15691bfSXin LI        }
63a15691bfSXin LI    }
64f6b74a7dSXin LI    output(\%out, $last_code);
65f6b74a7dSXin LI    return 1;
66a15691bfSXin LI}
67a15691bfSXin LI
68a15691bfSXin LIsub output {
69a15691bfSXin LI    my ($out, $code, $type) = @_;
70f6b74a7dSXin LI    my $type_ok = ($type and ${${$out}{types}}{$type});
71f6b74a7dSXin LI    $type_ok = not $type_ok if $opt_n;
72f6b74a7dSXin LI    my $prev_code = $$out{prev_code};
73f6b74a7dSXin LI
74f6b74a7dSXin LI    if (not $type_ok) {
75f6b74a7dSXin LI        end_run($out, $prev_code);
76f6b74a7dSXin LI    } elsif (not $$out{in_run} or $type ne $$out{run_type} or $code != $prev_code+1) {
77f6b74a7dSXin LI        end_run($out, $prev_code);
78a15691bfSXin LI        start_run($out, $code, $type);
79a15691bfSXin LI    }
80f6b74a7dSXin LI    $$out{prev_code} = $code;
81a15691bfSXin LI}
82a15691bfSXin LI
83a15691bfSXin LIsub start_run {
84a15691bfSXin LI    my ($out, $code, $type) = @_;
85a15691bfSXin LI    $$out{start_code} = $code;
86f6b74a7dSXin LI    $$out{prev_code} = $code;
87f6b74a7dSXin LI    $$out{run_type} = $type;
88a15691bfSXin LI    $$out{in_run} = 1;
89a15691bfSXin LI}
90a15691bfSXin LI
91a15691bfSXin LIsub end_run {
92a15691bfSXin LI    my ($out, $code) = @_;
93a15691bfSXin LI    return if not $$out{in_run};
94f6b74a7dSXin LI    printf "\t{ 0x%04x, 0x%04x }, /* %s */\n", $$out{start_code}, $code, $$out{run_type};
95a15691bfSXin LI    $$out{in_run} = 0;
96a15691bfSXin LI}
97