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# $FreeBSD$ 299b934d09SEd Schouten 309b934d09SEd Schoutenfunction die(msg) { 319b934d09SEd Schouten print msg; 329b934d09SEd Schouten exit 1; 339b934d09SEd Schouten} 349b934d09SEd Schouten 359b934d09SEd Schoutenfunction cchar(str) { 369b934d09SEd Schouten if (str == "^[") 379b934d09SEd Schouten return "\\x1B"; 388dcd2ed3SJean-Sébastien Pédron if (str == "SP") 398dcd2ed3SJean-Sébastien Pédron return " "; 408dcd2ed3SJean-Sébastien Pédron 418dcd2ed3SJean-Sébastien Pédron return str; 428dcd2ed3SJean-Sébastien Pédron} 438dcd2ed3SJean-Sébastien Pédron 448dcd2ed3SJean-Sébastien Pédronfunction csequence(str) { 458dcd2ed3SJean-Sébastien Pédron if (str == "SP") 468dcd2ed3SJean-Sébastien Pédron return " "; 479b934d09SEd Schouten 489b934d09SEd Schouten return str; 499b934d09SEd Schouten} 509b934d09SEd Schouten 519b934d09SEd SchoutenBEGIN { 529b934d09SEd SchoutenFS = "\t+" 539b934d09SEd Schouten 549b934d09SEd Schoutenwhile (getline > 0) { 559b934d09SEd Schouten if (NF == 0 || $1 ~ /^#/) 569b934d09SEd Schouten continue; 579b934d09SEd Schouten 589b934d09SEd Schouten if (NF != 3 && NF != 4) 599b934d09SEd Schouten die("Invalid line layout: " NF " columns"); 609b934d09SEd Schouten 619b934d09SEd Schouten split($3, sequence, " +"); 629b934d09SEd Schouten nsequences = 0; 639b934d09SEd Schouten for (s in sequence) 649b934d09SEd Schouten nsequences++; 659b934d09SEd Schouten 669b934d09SEd Schouten prefix = ""; 679b934d09SEd Schouten l_prefix_name[""] = "teken_state_init"; 689b934d09SEd Schouten for (i = 1; i < nsequences; i++) { 698dcd2ed3SJean-Sébastien Pédron n = prefix csequence(sequence[i]); 709b934d09SEd Schouten l_prefix_parent[n] = prefix; 719b934d09SEd Schouten l_prefix_suffix[n] = sequence[i]; 729b934d09SEd Schouten if (!l_prefix_name[n]) 739b934d09SEd Schouten l_prefix_name[n] = "teken_state_" ++npr; 749b934d09SEd Schouten prefix = n; 759b934d09SEd Schouten } 769b934d09SEd Schouten 779b934d09SEd Schouten suffix = sequence[nsequences]; 789b934d09SEd Schouten cmd = prefix suffix; 799b934d09SEd Schouten 809b934d09SEd Schouten # Fill lists 819b934d09SEd Schouten if (l_cmd_name[cmd] != "") 829b934d09SEd Schouten die(cmd " already exists"); 839b934d09SEd Schouten l_cmd_prefix[cmd] = prefix; 849b934d09SEd Schouten l_cmd_suffix[cmd] = suffix; 859b934d09SEd Schouten l_cmd_args[cmd] = $4; 869b934d09SEd Schouten l_cmd_abbr[cmd] = $1; 879b934d09SEd Schouten l_cmd_name[cmd] = $2; 889b934d09SEd Schouten l_cmd_c_name[cmd] = "teken_subr_" tolower($2); 899b934d09SEd Schouten gsub(" ", "_", l_cmd_c_name[cmd]); 909b934d09SEd Schouten 919b934d09SEd Schouten if ($4 != "") 929b934d09SEd Schouten l_prefix_numbercmds[prefix]++; 939b934d09SEd Schouten} 949b934d09SEd Schouten 959b934d09SEd Schoutenprint "/* Generated file. Do not edit. */"; 969b934d09SEd Schoutenprint ""; 979b934d09SEd Schouten 989b934d09SEd Schoutenfor (p in l_prefix_name) { 999b934d09SEd Schouten if (l_prefix_name[p] != "teken_state_init") 1009b934d09SEd Schouten print "static teken_state_t " l_prefix_name[p] ";"; 1019b934d09SEd Schouten} 1029b934d09SEd Schouten 1039b934d09SEd Schoutenfor (p in l_prefix_name) { 1049b934d09SEd Schouten print ""; 1059b934d09SEd Schouten print "/* '" p "' */"; 1069b934d09SEd Schouten print "static void"; 1079b934d09SEd Schouten print l_prefix_name[p] "(teken_t *t, teken_char_t c)"; 1089b934d09SEd Schouten print "{"; 1099b934d09SEd Schouten 1109b934d09SEd Schouten if (l_prefix_numbercmds[p] > 0) { 1119b934d09SEd Schouten print ""; 1129b934d09SEd Schouten print "\tif (teken_state_numbers(t, c))"; 1139b934d09SEd Schouten print "\t\treturn;"; 1149b934d09SEd Schouten } 1159b934d09SEd Schouten 1169b934d09SEd Schouten print ""; 1179b934d09SEd Schouten print "\tswitch (c) {"; 1189b934d09SEd Schouten for (c in l_cmd_prefix) { 1199b934d09SEd Schouten if (l_cmd_prefix[c] != p) 1209b934d09SEd Schouten continue; 1219b934d09SEd Schouten 1229b934d09SEd Schouten print "\tcase '" cchar(l_cmd_suffix[c]) "': /* " l_cmd_abbr[c] ": " l_cmd_name[c] " */"; 1239b934d09SEd Schouten 1249b934d09SEd Schouten if (l_cmd_args[c] == "v") { 1259b934d09SEd Schouten print "\t\t" l_cmd_c_name[c] "(t, t->t_curnum, t->t_nums);"; 1269b934d09SEd Schouten } else { 1279b934d09SEd Schouten printf "\t\t%s(t", l_cmd_c_name[c]; 1289b934d09SEd Schouten split(l_cmd_args[c], args, " "); 1299b934d09SEd Schouten for (a = 1; args[a] != ""; a++) { 1309b934d09SEd Schouten if (args[a] == "n") 1319b934d09SEd Schouten printf ", (t->t_curnum < %d || t->t_nums[%d] == 0) ? 1 : t->t_nums[%d]", a, (a - 1), (a - 1); 1329b934d09SEd Schouten else if (args[a] == "r") 1339b934d09SEd Schouten printf ", t->t_curnum < %d ? 0 : t->t_nums[%d]", a, (a - 1); 1349b934d09SEd Schouten else 1359b934d09SEd Schouten die("Invalid argument type: " args[a]); 1369b934d09SEd Schouten } 1379b934d09SEd Schouten print ");"; 1389b934d09SEd Schouten } 1399b934d09SEd Schouten print "\t\tbreak;"; 1409b934d09SEd Schouten } 1419b934d09SEd Schouten for (pc in l_prefix_parent) { 1429b934d09SEd Schouten if (l_prefix_parent[pc] != p) 1439b934d09SEd Schouten continue; 1449b934d09SEd Schouten print "\tcase '" cchar(l_prefix_suffix[pc]) "':"; 1459b934d09SEd Schouten print "\t\tteken_state_switch(t, " l_prefix_name[pc] ");"; 1469b934d09SEd Schouten print "\t\treturn;"; 1479b934d09SEd Schouten } 1489b934d09SEd Schouten 1499b934d09SEd Schouten print "\tdefault:"; 1509b934d09SEd Schouten if (l_prefix_name[p] == "teken_state_init") { 1519b934d09SEd Schouten print "\t\tteken_subr_regular_character(t, c);"; 1529b934d09SEd Schouten } else { 1539b934d09SEd Schouten print "\t\tteken_printf(\"Unsupported sequence in " l_prefix_name[p] ": %u\\n\", (unsigned int)c);"; 1549b934d09SEd Schouten } 1559b934d09SEd Schouten print "\t\tbreak;"; 1569b934d09SEd Schouten 1579b934d09SEd Schouten print "\t}"; 1589b934d09SEd Schouten 1599b934d09SEd Schouten if (l_prefix_name[p] != "teken_state_init") { 1609b934d09SEd Schouten print ""; 161*3eb27bf0SPoul-Henning Kamp print "\tt->t_last = 0;"; 1629b934d09SEd Schouten print "\tteken_state_switch(t, teken_state_init);"; 1639b934d09SEd Schouten } 1649b934d09SEd Schouten print "}"; 1659b934d09SEd Schouten} 1669b934d09SEd Schouten 1679b934d09SEd Schouten} 168