237 lines
5.0 KiB
Plaintext
237 lines
5.0 KiB
Plaintext
# Generate the dense JIS X 0208 lookup arrays used by lib/jis0208.c
|
|
#
|
|
# Usage:
|
|
# hawk -f tools/gen-jis0208-tabs.hawk -- tools/JIS0208.TXT
|
|
# hawk -f tools/gen-jis0208-tabs.hawk -- --forward tools/JIS0208.TXT
|
|
# hawk -f tools/gen-jis0208-tabs.hawk -- --reverse tools/JIS0208.TXT
|
|
#
|
|
|
|
BEGIN {
|
|
MODE = "all";
|
|
bad = 0;
|
|
|
|
init_forward_pages();
|
|
init_reverse_pages();
|
|
parse_args();
|
|
}
|
|
|
|
/^[ \t]*0x[0-9A-Fa-f]+[ \t]+0x[0-9A-Fa-f]+[ \t]+0x[0-9A-Fa-f]+/ {
|
|
@local jis, uc;
|
|
|
|
jis = parse_hex($2);
|
|
uc = parse_hex($3);
|
|
|
|
if (jis < 0 || uc < 0) next;
|
|
|
|
add_forward_mapping(jis, uc);
|
|
add_reverse_mapping(jis, uc);
|
|
}
|
|
|
|
END {
|
|
@local i, first;
|
|
|
|
if (bad) exit 1;
|
|
|
|
printf("/* generated by tools/gen-jis0208-tabs.hawk from %s */\n\n", input_name());
|
|
|
|
first = 1;
|
|
|
|
if (MODE == "all" || MODE == "forward")
|
|
{
|
|
for (i = 1; i <= fwd_page_count; i++)
|
|
{
|
|
if (!first) printf("\n");
|
|
dump_forward_page(i);
|
|
first = 0;
|
|
}
|
|
}
|
|
|
|
if (MODE == "all" || MODE == "reverse")
|
|
{
|
|
for (i = 1; i <= rev_page_count; i++)
|
|
{
|
|
if (!first) printf("\n");
|
|
dump_reverse_page(i);
|
|
first = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
function parse_args()
|
|
{
|
|
@local i, a;
|
|
|
|
for (i = 1; i < ARGC; i++)
|
|
{
|
|
a = ARGV[i];
|
|
if (a == "" || substr(a, 1, 2) != "--") continue;
|
|
|
|
if (a == "--all")
|
|
MODE = "all";
|
|
else if (a == "--forward")
|
|
MODE = "forward";
|
|
else if (a == "--reverse")
|
|
MODE = "reverse";
|
|
else
|
|
{
|
|
printf("unknown option %s\n", a) > "/dev/stderr";
|
|
bad = 1;
|
|
}
|
|
|
|
ARGV[i] = "";
|
|
}
|
|
}
|
|
|
|
function input_name()
|
|
{
|
|
@local i, sep, out;
|
|
|
|
sep = "";
|
|
out = "";
|
|
|
|
for (i = 1; i < ARGC; i++)
|
|
{
|
|
if (ARGV[i] == "") continue;
|
|
out = out sep ARGV[i];
|
|
sep = ", ";
|
|
}
|
|
|
|
return (out != "")? out: "<stdin>";
|
|
}
|
|
|
|
function hex_digit(ch)
|
|
{
|
|
if (ch >= "0" && ch <= "9") return ch + 0;
|
|
if (ch >= "A" && ch <= "F") return 10 + index("ABCDEF", ch) - 1;
|
|
if (ch >= "a" && ch <= "f") return 10 + index("abcdef", ch) - 1;
|
|
return -1;
|
|
}
|
|
|
|
function parse_hex(s)
|
|
{
|
|
@local i, n, d;
|
|
n = 0;
|
|
|
|
if (substr(s, 1, 2) == "0x" || substr(s, 1, 2) == "0X")
|
|
s = substr(s, 3);
|
|
|
|
for (i = 1; i <= length(s); i++)
|
|
{
|
|
d = hex_digit(substr(s, i, 1));
|
|
if (d < 0) return -1;
|
|
n = n * 16 + d;
|
|
}
|
|
|
|
return n;
|
|
}
|
|
|
|
function init_forward_pages()
|
|
{
|
|
fwd_page_count = 1;
|
|
fwd_page_no[1] = 0;
|
|
fwd_name[1] = "jis0208_to_uc_0";
|
|
fwd_from[1] = 0x2121;
|
|
fwd_to[1] = 0x7426;
|
|
fwd_len[1] = fwd_to[1] - fwd_from[1] + 1;
|
|
}
|
|
|
|
function init_reverse_pages()
|
|
{
|
|
rev_page_count = 10;
|
|
|
|
rev_page_no[1] = 0; rev_name[1] = "uc_to_jis0208_0"; rev_from[1] = 0x00A1; rev_to[1] = 0x00FF;
|
|
rev_page_no[2] = 1; rev_name[2] = "uc_to_jis0208_1"; rev_from[2] = 0x0391; rev_to[2] = 0x0451;
|
|
rev_page_no[3] = 2; rev_name[3] = "uc_to_jis0208_2"; rev_from[3] = 0x2010; rev_to[3] = 0x2FFF;
|
|
rev_page_no[4] = 3; rev_name[4] = "uc_to_jis0208_3"; rev_from[4] = 0x3000; rev_to[4] = 0x3FFF;
|
|
rev_page_no[5] = 4; rev_name[5] = "uc_to_jis0208_4"; rev_from[5] = 0x4E00; rev_to[5] = 0x5FFF;
|
|
rev_page_no[6] = 5; rev_name[6] = "uc_to_jis0208_5"; rev_from[6] = 0x6000; rev_to[6] = 0x6FFF;
|
|
rev_page_no[7] = 6; rev_name[7] = "uc_to_jis0208_6"; rev_from[7] = 0x7000; rev_to[7] = 0x7FFF;
|
|
rev_page_no[8] = 7; rev_name[8] = "uc_to_jis0208_7"; rev_from[8] = 0x8000; rev_to[8] = 0x8FFF;
|
|
rev_page_no[9] = 8; rev_name[9] = "uc_to_jis0208_8"; rev_from[9] = 0x9000; rev_to[9] = 0x9FA0;
|
|
rev_page_no[10] = 9; rev_name[10] = "uc_to_jis0208_9"; rev_from[10] = 0xFF01; rev_to[10] = 0xFFE5;
|
|
|
|
for (i = 1; i <= rev_page_count; i++)
|
|
rev_len[i] = rev_to[i] - rev_from[i] + 1;
|
|
}
|
|
|
|
function add_forward_mapping(jis, uc)
|
|
{
|
|
@local i, idx, old;
|
|
|
|
for (i = 1; i <= fwd_page_count; i++)
|
|
{
|
|
if (jis < fwd_from[i] || jis > fwd_to[i]) continue;
|
|
|
|
idx = jis - fwd_from[i];
|
|
old = fwd_data[i][idx] + 0;
|
|
if (old != 0 && old != uc)
|
|
{
|
|
printf("duplicate forward mapping for 0x%04x: 0x%04x vs 0x%04x\n", jis, old, uc) > "/dev/stderr";
|
|
bad = 1;
|
|
return;
|
|
}
|
|
|
|
fwd_data[i][idx] = uc;
|
|
return;
|
|
}
|
|
}
|
|
|
|
function add_reverse_mapping(jis, uc)
|
|
{
|
|
@local i, idx, old;
|
|
|
|
for (i = 1; i <= rev_page_count; i++)
|
|
{
|
|
if (uc < rev_from[i] || uc > rev_to[i]) continue;
|
|
|
|
idx = uc - rev_from[i];
|
|
old = rev_data[i][idx] + 0;
|
|
if (old != 0 && old != jis)
|
|
{
|
|
printf("duplicate reverse mapping for U+%04x: 0x%04x vs 0x%04x\n", uc, old, jis) > "/dev/stderr";
|
|
bad = 1;
|
|
return;
|
|
}
|
|
|
|
rev_data[i][idx] = jis;
|
|
return;
|
|
}
|
|
}
|
|
|
|
function dump_dense_array(kind, page, total)
|
|
{
|
|
@local i, v;
|
|
|
|
for (i = 0; i < total; i++)
|
|
{
|
|
if ((i % 9) == 0) printf("\t");
|
|
|
|
if (kind == "forward")
|
|
v = fwd_data[page][i] + 0;
|
|
else
|
|
v = rev_data[page][i] + 0;
|
|
|
|
printf("0x%04x", v);
|
|
if (i + 1 < total) printf(", ");
|
|
|
|
if ((i % 9) == 8 || i + 1 >= total)
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
function dump_forward_page(i)
|
|
{
|
|
printf("/* page %d 0x%04x-0x%04x */\n", fwd_page_no[i], fwd_from[i], fwd_to[i]);
|
|
printf("static const hawk_uint16_t %s[] = {\n", fwd_name[i]);
|
|
dump_dense_array("forward", i, fwd_len[i]);
|
|
printf("};\n");
|
|
}
|
|
|
|
function dump_reverse_page(i)
|
|
{
|
|
printf("/* page %d 0x%04x-0x%04x */\n", rev_page_no[i], rev_from[i], rev_to[i]);
|
|
printf("static const hawk_uint16_t %s[] = {\n", rev_name[i]);
|
|
dump_dense_array("reverse", i, rev_len[i]);
|
|
printf("};\n");
|
|
}
|