summaryrefslogtreecommitdiff
path: root/usr/src/data/locale/convert_map.pl
blob: 628a7efa301010044206992c847025edbbf3a3c5 (plain)
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
#! /usr/perl5/bin/perl
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy is of the CDDL is also available via the Internet
# at http://www.illumos.org/license/CDDL.
#

#
# Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
#

# This converts MAPPING files to localedef character maps
# suitable for use with the UTF-8 derived localedef data.

sub ucs_to_utf8
{
    my $ucs = shift;
    my $utf8;

    if ($ucs <= 0x7f) {
	$utf8 = sprintf("\\x%02X", $ucs).$utf8;
    } elsif ($ucs <= 0x7ff) {
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", $ucs | 0xc0).$utf8;

    } elsif ($ucs <= 0xffff) {
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", $ucs | 0xe0).$utf8;

    } elsif ($ucs <= 0x1fffff) {
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", $ucs | 0xf0).$utf8;

    } elsif ($ucs <= 0x03ffffff) {
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", $ucs | 0xf8).$utf8;

    } else {
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", ($ucs & 0x3f) | 0x80).$utf8;
	$ucs >>= 6;
	$utf8 = sprintf("\\x%02X", $ucs | 0xf8).$utf8;
    }

    return ($utf8);
}

my %unames;
my %uvalues;

#
# This is not a general purpose Character Map parser, but its good enough
# for the stock one supplied with CLDR.
#
sub load_utf8_cm
{
    my $file = shift;

    open(UTF8, "$file") || die "open";

    while (<UTF8>) {
	next if (/^#/);
	next if (/^\s*$/);
	next if (/^\s*CHARMAP\s*$/);
	next if (/^\s*END\s*CHARMAP\s*$/);
	chomp;
	@words = split /\s+/;
	$name = $words[0];
	$utf8val = $words[1];

	if (defined($unames{$utf8val})) {
	    $unames{$utf8val} .= "\n" .$name;
	} else {
	    $unames{$utf8val} = $name;
	}
	$uvalues{$name} = $utf8val;
    }
    close(UTF8);
}

my %map;

sub load_map
{
    my $file = shift;

    open(MAP, "$file") || die "open";

    while (<MAP>) {
	next if (/^#/);
	next if (/^\s*$/);
	chomp;
	@words = split /\s+/;
	$utf8 = $words[1];
	$utf8 =~ s/^\\x[0]*//;
	$utf8 = ucs_to_utf8(hex($utf8));
	$val = $words[0];
	if (defined ($map{$val})) {
	    $map{$val} .= " ".$utf8;
	} else {
	    $map{$val} = $utf8;
	}
    }
}

sub mb_str
{
    my $val = shift;
    my $str = "";
    $val = hex($val);

    if ($val == 0) {
	return ("\\x00");
    }
    while ($val) {
	$str = sprintf("\\x%02x", $val & 0xff).$str;
	$val >>= 8;
    }
    return ($str);
}

$mf = shift(@ARGV);

load_utf8_cm("data/UTF-8.cm");
load_map($mf);

print("CHARMAP\n");
foreach $val (sort (keys (%map))) {
    #$utf8 = $map{$val};
    foreach $utf8 (split / /, $map{$val}) {
	$ref = $unames{$utf8};
	foreach $name (sort (split /\n/, $ref)) {
	    print "$name";
	    my $nt = int((64 - length($name) + 7) / 8);
	    while ($nt) {
		print "\t";
		$nt--;
	    }
	    print mb_str($val)."\n";
	}
    }
}
print "END CHARMAP\n";