xref: /freebsd/contrib/less/mkutable (revision 252d6dde57d5dd0184929d1f8fb65e7713f51c6d)
1#!/usr/bin/env perl
2use strict;
3
4my $USAGE = <<__EOF__;
5   usage: mkutable [-n] [-f#] type... [--] [<] UnicodeData.txt
6          -n = take non-matching types
7          -f = zero-based type field (default 2)
8__EOF__
9
10use Getopt::Std;
11use vars qw( $opt_f $opt_n );
12
13my $type_field = 2;
14
15# Override Unicode tables for certain control chars
16# that are expected to be found in normal text files.
17my %force_space = (
18    0x08 => 1, # backspace
19    0x09 => 1, # tab
20    0x0a => 1, # newline
21    0x0c => 1, # form feed
22    0x0d => 1, # carriage return
23);
24
25# Hangul Jamo medial vowels and final consonants should be zero width.
26my @force_compose = (
27    [0x1160, 0x11ff],
28    [0xd7b0, 0xd7c6],
29    [0xd7cb, 0xd7fb]
30);
31
32exit (main() ? 0 : 1);
33
34sub main {
35    my $args = join ' ', @ARGV;
36    die $USAGE if not getopts('f:n');
37    $type_field = $opt_f if $opt_f;
38
39    my %types;
40    my $arg;
41    while ($arg = shift @ARGV) {
42        last if $arg eq '--';
43        $types{$arg} = 1;
44    }
45    my %out = ( 'types' => \%types );
46
47    my %force_compose;
48    foreach my $comp (@force_compose) {
49        my ($lo,$hi) = @$comp;
50        for (my $ch = $lo; $ch <= $hi; ++$ch) {
51            $force_compose{$ch} = 1;
52        }
53    }
54
55    my ($sec,$min,$hour,$mday,$mon,$year) = gmtime($ENV{SOURCE_DATE_EPOCH} // time());
56    my @month = ( "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" );
57    printf "/* Generated by \"%s %s\" on %s %2d %2d:%02d:%02d GMT %d */\n",
58        $0, $args, $month[$mon], $mday, $hour, $min, $sec, $year+1900;
59
60    my $last_code = 0;
61    my $start_range = 0;
62    while (<>) {
63        chomp;
64        s/#.*//;
65        my @fields = split /;/;
66        next if not @fields;
67        my ($lo_code, $hi_code);
68        my $codes = $fields[0];
69        if ($codes =~ /(\w+)\.\.(\w+)/) {
70            $lo_code = hex $1;
71            $hi_code = hex $2;
72        } else {
73            $lo_code = $hi_code = hex $codes;
74        }
75        if ($fields[1] =~ /, First>$/) {
76            die "invalid Unicode data: First with range" if $hi_code != $lo_code;
77            $start_range = $lo_code;
78            next;
79        }
80        if ($fields[1] =~ /, Last>$/) {
81            die "invalid Unicode data: Last without First" if not $start_range;
82            $lo_code = $start_range;
83            $start_range = 0;
84        } elsif ($start_range) {
85            die "invalid Unicode data: First without Last";
86        }
87        my $type = $fields[$type_field];
88        $type =~ s/\s//g;
89        for ($last_code = $lo_code; $last_code <= $hi_code; ++$last_code) {
90            output(\%out, $last_code,
91                $force_space{$last_code} ? 'Zs' : $force_compose{$last_code} ? 'Mn' : $type);
92        }
93    }
94    output(\%out, $last_code);
95    return 1;
96}
97
98sub output {
99    my ($out, $code, $type) = @_;
100    my $type_ok = ($type and ${${$out}{types}}{$type});
101    $type_ok = not $type_ok if $opt_n;
102    my $prev_code = $$out{prev_code};
103
104    if (not $type_ok) {
105        end_run($out, $prev_code);
106    } elsif (not $$out{in_run} or $type ne $$out{run_type} or $code != $prev_code+1) {
107        end_run($out, $prev_code);
108        start_run($out, $code, $type);
109    }
110    $$out{prev_code} = $code;
111}
112
113sub start_run {
114    my ($out, $code, $type) = @_;
115    $$out{start_code} = $code;
116    $$out{prev_code} = $code;
117    $$out{run_type} = $type;
118    $$out{in_run} = 1;
119}
120
121sub end_run {
122    my ($out, $code) = @_;
123    return if not $$out{in_run};
124    printf "\t{ 0x%04x, 0x%04x }, /* %s */\n", $$out{start_code}, $code, $$out{run_type};
125    $$out{in_run} = 0;
126}
127