*** empty log message ***
[m17n/m17n-db.git] / SCRIPT.awk
1 # SCRIPT.awk -- awk script to generate SCRIPT.tab
2 # Copyright (C) 2007
3 #   National Institute of Advanced Industrial Science and Technology (AIST)
4 #   Registration Number H15PRO112
5
6 # This file is part of the m17n database; a sub-part of the m17n
7 # library.
8
9 # The m17n library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1 of
12 # the License, or (at your option) any later version.
13
14 # The m17n library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with the m17n library; if not, write to the Free
21 # Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 # Boston, MA 02110-1301, USA.
23
24 BEGIN {
25   tohex["0"] = 1;
26   tohex["1"] = 2;
27   tohex["2"] = 3;
28   tohex["3"] = 4;
29   tohex["4"] = 5;
30   tohex["5"] = 6;
31   tohex["6"] = 7;
32   tohex["7"] = 8;
33   tohex["8"] = 9;
34   tohex["9"] = 10;
35   tohex["A"] = 11;
36   tohex["B"] = 12;
37   tohex["C"] = 13;
38   tohex["D"] = 14;
39   tohex["E"] = 15;
40   tohex["F"] = 16;
41   tohex["a"] = 11;
42   tohex["b"] = 12;
43   tohex["c"] = 13;
44   tohex["d"] = 14;
45   tohex["e"] = 15;
46   tohex["f"] = 16;
47
48   FS = "[ \t]*[;#][ \t]*";
49   initialized = 0;
50   charcount = 0;
51   range_index = 0;
52 }
53
54 function decode_hex(str, idx) {
55     n = 0;
56     len = length(str);
57     for (i = idx; i <= len; i++) {
58         c = tohex[substr(str, i, 1)];
59         if (c == 0)
60             break;
61         n = n * 16 + c - 1;
62     }
63     return n;
64 }
65
66 function initialize() {
67     first = -1;
68     while (getline line < "UNIDATA/UnicodeData.txt" > 0) {
69         if (line ~ /^[0-9A-F][0-9A-F]*/) {
70             last = decode_hex(line, 1);
71             if (first >= 0) {
72                 range[range_index++] = first;
73                 range[range_index++] = last;
74                 first = -1;
75             } else if (line ~ /First>/) {
76                 first = last;
77             } else {
78                 exists[last] = 1;
79             }
80         }
81     }
82 }
83
84 function char_exist_p(c) {
85     if (exists[c] == 1)
86         return 1;
87     for (i = 0; i < range_index; i += 2)
88         if (range[i] >= c && range[i + 1] <= c)
89             return 1;
90     return 0;
91 }
92
93 function maybe_emit(ch1, ch2, this_script) {
94     if (initialized == 0) {
95         initialize();
96         initialized = 1;
97         print "# Ranges may contain non-existing character codes.";
98         print "0x0-0x3FFFFF common";
99         first = ch1;
100         last = ch2;
101         script = this_script;
102     } else {
103         if (script == this_script) {
104             for (j = last + 1; j < ch1; j++)
105                 if (char_exist_p(j))
106                     break;
107             if (j == ch1) {
108                 last = ch2;
109                 return;
110             }
111         }
112         if (script != "Common") {
113             if (first == last)
114                 printf "0x%04X %s\n", first, tolower(script);
115             else
116                 printf "0x%04X-0x%04X %s\n", first, last, tolower(script);
117         }
118         first = ch1;
119         last = ch2;
120         script = this_script;
121     }
122 }
123
124 /^[0-9A-F]+\.\./ {
125     maybe_emit(decode_hex($0, 1), decode_hex($0, match($0, "\\.\\.") + 2), $2);
126     next;
127 }
128
129 /^[0-9A-F]/ {
130     ch = decode_hex($0, 1);
131     maybe_emit(ch, ch, $2);
132     next;
133 }
134
135 END {
136     if (script != "Common") {
137         if (first == last)
138             printf "0x%04X %s\n", first, tolower (script);
139         else
140             printf "0x%04X-0x%04X %s\n", first, last, tolower (script);
141     }
142     while (getline < "SCRIPT.ext" > 0) {
143         if ($0 ~ /^[0-9A-F][0-9A-F]*/) {
144             print;
145         }
146     }
147
148 }