xref: /freebsd/contrib/less/mkutable (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1#! /usr/bin/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 $date = `date`;
56    chomp $date;
57    print "/* Generated by \"$0 $args\" on $date */\n";
58
59    my $last_code = 0;
60    while (<>) {
61        chomp;
62        s/#.*//;
63        my @fields = split /;/;
64        next if not @fields;
65        my ($lo_code, $hi_code);
66        my $codes = $fields[0];
67        if ($codes =~ /(\w+)\.\.(\w+)/) {
68            $lo_code = hex $1;
69            $hi_code = hex $2;
70        } else {
71            $lo_code = $hi_code = hex $codes;
72        }
73        my $type = $fields[$type_field];
74        $type =~ s/\s//g;
75        for ($last_code = $lo_code; $last_code <= $hi_code; ++$last_code) {
76            output(\%out, $last_code,
77                $force_space{$last_code} ? 'Zs' : $force_compose{$last_code} ? 'Mn' : $type);
78        }
79    }
80    output(\%out, $last_code);
81    return 1;
82}
83
84sub output {
85    my ($out, $code, $type) = @_;
86    my $type_ok = ($type and ${${$out}{types}}{$type});
87    $type_ok = not $type_ok if $opt_n;
88    my $prev_code = $$out{prev_code};
89
90    if (not $type_ok) {
91        end_run($out, $prev_code);
92    } elsif (not $$out{in_run} or $type ne $$out{run_type} or $code != $prev_code+1) {
93        end_run($out, $prev_code);
94        start_run($out, $code, $type);
95    }
96    $$out{prev_code} = $code;
97}
98
99sub start_run {
100    my ($out, $code, $type) = @_;
101    $$out{start_code} = $code;
102    $$out{prev_code} = $code;
103    $$out{run_type} = $type;
104    $$out{in_run} = 1;
105}
106
107sub end_run {
108    my ($out, $code) = @_;
109    return if not $$out{in_run};
110    printf "\t{ 0x%04x, 0x%04x }, /* %s */\n", $$out{start_code}, $code, $$out{run_type};
111    $$out{in_run} = 0;
112}
113