19b934d09SEd Schouten#!/usr/bin/awk -f 29b934d09SEd Schouten 39b934d09SEd Schouten#- 49b934d09SEd Schouten# Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org> 59b934d09SEd Schouten# All rights reserved. 69b934d09SEd Schouten# 79b934d09SEd Schouten# Redistribution and use in source and binary forms, with or without 89b934d09SEd Schouten# modification, are permitted provided that the following conditions 99b934d09SEd Schouten# are met: 109b934d09SEd Schouten# 1. Redistributions of source code must retain the above copyright 119b934d09SEd Schouten# notice, this list of conditions and the following disclaimer. 129b934d09SEd Schouten# 2. Redistributions in binary form must reproduce the above copyright 139b934d09SEd Schouten# notice, this list of conditions and the following disclaimer in the 149b934d09SEd Schouten# documentation and/or other materials provided with the distribution. 159b934d09SEd Schouten# 169b934d09SEd Schouten# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 179b934d09SEd Schouten# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 189b934d09SEd Schouten# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 199b934d09SEd Schouten# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 209b934d09SEd Schouten# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 219b934d09SEd Schouten# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 229b934d09SEd Schouten# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 239b934d09SEd Schouten# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 249b934d09SEd Schouten# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 259b934d09SEd Schouten# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 269b934d09SEd Schouten# SUCH DAMAGE. 279b934d09SEd Schouten# 289b934d09SEd Schouten 299b934d09SEd Schoutenfunction die(msg) { 309b934d09SEd Schouten print msg; 319b934d09SEd Schouten exit 1; 329b934d09SEd Schouten} 339b934d09SEd Schouten 349b934d09SEd Schoutenfunction cchar(str) { 359b934d09SEd Schouten if (str == "^[") 369b934d09SEd Schouten return "\\x1B"; 378dcd2ed3SJean-Sébastien Pédron if (str == "SP") 388dcd2ed3SJean-Sébastien Pédron return " "; 398dcd2ed3SJean-Sébastien Pédron 408dcd2ed3SJean-Sébastien Pédron return str; 418dcd2ed3SJean-Sébastien Pédron} 428dcd2ed3SJean-Sébastien Pédron 438dcd2ed3SJean-Sébastien Pédronfunction csequence(str) { 448dcd2ed3SJean-Sébastien Pédron if (str == "SP") 458dcd2ed3SJean-Sébastien Pédron return " "; 469b934d09SEd Schouten 479b934d09SEd Schouten return str; 489b934d09SEd Schouten} 499b934d09SEd Schouten 509b934d09SEd SchoutenBEGIN { 519b934d09SEd SchoutenFS = "\t+" 529b934d09SEd Schouten 539b934d09SEd Schoutenwhile (getline > 0) { 549b934d09SEd Schouten if (NF == 0 || $1 ~ /^#/) 559b934d09SEd Schouten continue; 569b934d09SEd Schouten 579b934d09SEd Schouten if (NF != 3 && NF != 4) 589b934d09SEd Schouten die("Invalid line layout: " NF " columns"); 599b934d09SEd Schouten 609b934d09SEd Schouten split($3, sequence, " +"); 619b934d09SEd Schouten nsequences = 0; 629b934d09SEd Schouten for (s in sequence) 639b934d09SEd Schouten nsequences++; 649b934d09SEd Schouten 659b934d09SEd Schouten prefix = ""; 669b934d09SEd Schouten l_prefix_name[""] = "teken_state_init"; 679b934d09SEd Schouten for (i = 1; i < nsequences; i++) { 688dcd2ed3SJean-Sébastien Pédron n = prefix csequence(sequence[i]); 699b934d09SEd Schouten l_prefix_parent[n] = prefix; 709b934d09SEd Schouten l_prefix_suffix[n] = sequence[i]; 719b934d09SEd Schouten if (!l_prefix_name[n]) 729b934d09SEd Schouten l_prefix_name[n] = "teken_state_" ++npr; 739b934d09SEd Schouten prefix = n; 749b934d09SEd Schouten } 759b934d09SEd Schouten 769b934d09SEd Schouten suffix = sequence[nsequences]; 779b934d09SEd Schouten cmd = prefix suffix; 789b934d09SEd Schouten 799b934d09SEd Schouten # Fill lists 809b934d09SEd Schouten if (l_cmd_name[cmd] != "") 819b934d09SEd Schouten die(cmd " already exists"); 829b934d09SEd Schouten l_cmd_prefix[cmd] = prefix; 839b934d09SEd Schouten l_cmd_suffix[cmd] = suffix; 849b934d09SEd Schouten l_cmd_args[cmd] = $4; 859b934d09SEd Schouten l_cmd_abbr[cmd] = $1; 869b934d09SEd Schouten l_cmd_name[cmd] = $2; 879b934d09SEd Schouten l_cmd_c_name[cmd] = "teken_subr_" tolower($2); 889b934d09SEd Schouten gsub(" ", "_", l_cmd_c_name[cmd]); 899b934d09SEd Schouten 909b934d09SEd Schouten if ($4 != "") 919b934d09SEd Schouten l_prefix_numbercmds[prefix]++; 929b934d09SEd Schouten} 939b934d09SEd Schouten 949b934d09SEd Schoutenprint "/* Generated file. Do not edit. */"; 959b934d09SEd Schoutenprint ""; 969b934d09SEd Schouten 979b934d09SEd Schoutenfor (p in l_prefix_name) { 989b934d09SEd Schouten if (l_prefix_name[p] != "teken_state_init") 999b934d09SEd Schouten print "static teken_state_t " l_prefix_name[p] ";"; 1009b934d09SEd Schouten} 1019b934d09SEd Schouten 1029b934d09SEd Schoutenfor (p in l_prefix_name) { 1039b934d09SEd Schouten print ""; 1049b934d09SEd Schouten print "/* '" p "' */"; 1059b934d09SEd Schouten print "static void"; 1069b934d09SEd Schouten print l_prefix_name[p] "(teken_t *t, teken_char_t c)"; 1079b934d09SEd Schouten print "{"; 1089b934d09SEd Schouten 1099b934d09SEd Schouten if (l_prefix_numbercmds[p] > 0) { 1109b934d09SEd Schouten print ""; 1119b934d09SEd Schouten print "\tif (teken_state_numbers(t, c))"; 1129b934d09SEd Schouten print "\t\treturn;"; 1139b934d09SEd Schouten } 1149b934d09SEd Schouten 1159b934d09SEd Schouten print ""; 1169b934d09SEd Schouten print "\tswitch (c) {"; 1179b934d09SEd Schouten for (c in l_cmd_prefix) { 1189b934d09SEd Schouten if (l_cmd_prefix[c] != p) 1199b934d09SEd Schouten continue; 1209b934d09SEd Schouten 1219b934d09SEd Schouten print "\tcase '" cchar(l_cmd_suffix[c]) "': /* " l_cmd_abbr[c] ": " l_cmd_name[c] " */"; 1229b934d09SEd Schouten 1239b934d09SEd Schouten if (l_cmd_args[c] == "v") { 1249b934d09SEd Schouten print "\t\t" l_cmd_c_name[c] "(t, t->t_curnum, t->t_nums);"; 1259b934d09SEd Schouten } else { 1269b934d09SEd Schouten printf "\t\t%s(t", l_cmd_c_name[c]; 1279b934d09SEd Schouten split(l_cmd_args[c], args, " "); 1289b934d09SEd Schouten for (a = 1; args[a] != ""; a++) { 1299b934d09SEd Schouten if (args[a] == "n") 1309b934d09SEd Schouten printf ", (t->t_curnum < %d || t->t_nums[%d] == 0) ? 1 : t->t_nums[%d]", a, (a - 1), (a - 1); 1319b934d09SEd Schouten else if (args[a] == "r") 1329b934d09SEd Schouten printf ", t->t_curnum < %d ? 0 : t->t_nums[%d]", a, (a - 1); 1339b934d09SEd Schouten else 1349b934d09SEd Schouten die("Invalid argument type: " args[a]); 1359b934d09SEd Schouten } 1369b934d09SEd Schouten print ");"; 1379b934d09SEd Schouten } 1389b934d09SEd Schouten print "\t\tbreak;"; 1399b934d09SEd Schouten } 1409b934d09SEd Schouten for (pc in l_prefix_parent) { 1419b934d09SEd Schouten if (l_prefix_parent[pc] != p) 1429b934d09SEd Schouten continue; 1439b934d09SEd Schouten print "\tcase '" cchar(l_prefix_suffix[pc]) "':"; 1449b934d09SEd Schouten print "\t\tteken_state_switch(t, " l_prefix_name[pc] ");"; 1459b934d09SEd Schouten print "\t\treturn;"; 1469b934d09SEd Schouten } 1479b934d09SEd Schouten 1489b934d09SEd Schouten print "\tdefault:"; 1499b934d09SEd Schouten if (l_prefix_name[p] == "teken_state_init") { 1509b934d09SEd Schouten print "\t\tteken_subr_regular_character(t, c);"; 1519b934d09SEd Schouten } else { 1529b934d09SEd Schouten print "\t\tteken_printf(\"Unsupported sequence in " l_prefix_name[p] ": %u\\n\", (unsigned int)c);"; 1539b934d09SEd Schouten } 1549b934d09SEd Schouten print "\t\tbreak;"; 1559b934d09SEd Schouten 1569b934d09SEd Schouten print "\t}"; 1579b934d09SEd Schouten 1589b934d09SEd Schouten if (l_prefix_name[p] != "teken_state_init") { 1599b934d09SEd Schouten print ""; 160*3eb27bf0SPoul-Henning Kamp print "\tt->t_last = 0;"; 1619b934d09SEd Schouten print "\tteken_state_switch(t, teken_state_init);"; 1629b934d09SEd Schouten } 1639b934d09SEd Schouten print "}"; 1649b934d09SEd Schouten} 1659b934d09SEd Schouten 1669b934d09SEd Schouten} 167