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"; 389b934d09SEd Schouten 399b934d09SEd Schouten return str; 409b934d09SEd Schouten} 419b934d09SEd Schouten 429b934d09SEd SchoutenBEGIN { 439b934d09SEd SchoutenFS = "\t+" 449b934d09SEd Schouten 459b934d09SEd Schoutenwhile (getline > 0) { 469b934d09SEd Schouten if (NF == 0 || $1 ~ /^#/) 479b934d09SEd Schouten continue; 489b934d09SEd Schouten 499b934d09SEd Schouten if (NF != 3 && NF != 4) 509b934d09SEd Schouten die("Invalid line layout: " NF " columns"); 519b934d09SEd Schouten 529b934d09SEd Schouten split($3, sequence, " +"); 539b934d09SEd Schouten nsequences = 0; 549b934d09SEd Schouten for (s in sequence) 559b934d09SEd Schouten nsequences++; 569b934d09SEd Schouten 579b934d09SEd Schouten prefix = ""; 589b934d09SEd Schouten l_prefix_name[""] = "teken_state_init"; 599b934d09SEd Schouten for (i = 1; i < nsequences; i++) { 609b934d09SEd Schouten n = prefix sequence[i]; 619b934d09SEd Schouten l_prefix_parent[n] = prefix; 629b934d09SEd Schouten l_prefix_suffix[n] = sequence[i]; 639b934d09SEd Schouten if (!l_prefix_name[n]) 649b934d09SEd Schouten l_prefix_name[n] = "teken_state_" ++npr; 659b934d09SEd Schouten prefix = n; 669b934d09SEd Schouten } 679b934d09SEd Schouten 689b934d09SEd Schouten suffix = sequence[nsequences]; 699b934d09SEd Schouten cmd = prefix suffix; 709b934d09SEd Schouten 719b934d09SEd Schouten # Fill lists 729b934d09SEd Schouten if (l_cmd_name[cmd] != "") 739b934d09SEd Schouten die(cmd " already exists"); 749b934d09SEd Schouten l_cmd_prefix[cmd] = prefix; 759b934d09SEd Schouten l_cmd_suffix[cmd] = suffix; 769b934d09SEd Schouten l_cmd_args[cmd] = $4; 779b934d09SEd Schouten l_cmd_abbr[cmd] = $1; 789b934d09SEd Schouten l_cmd_name[cmd] = $2; 799b934d09SEd Schouten l_cmd_c_name[cmd] = "teken_subr_" tolower($2); 809b934d09SEd Schouten gsub(" ", "_", l_cmd_c_name[cmd]); 819b934d09SEd Schouten 829b934d09SEd Schouten if ($4 != "") 839b934d09SEd Schouten l_prefix_numbercmds[prefix]++; 849b934d09SEd Schouten} 859b934d09SEd Schouten 869b934d09SEd Schoutenprint "/* Generated file. Do not edit. */"; 879b934d09SEd Schoutenprint ""; 889b934d09SEd Schouten 899b934d09SEd Schoutenfor (p in l_prefix_name) { 909b934d09SEd Schouten if (l_prefix_name[p] != "teken_state_init") 919b934d09SEd Schouten print "static teken_state_t " l_prefix_name[p] ";"; 929b934d09SEd Schouten} 939b934d09SEd Schouten 949b934d09SEd Schoutenfor (p in l_prefix_name) { 959b934d09SEd Schouten print ""; 969b934d09SEd Schouten print "/* '" p "' */"; 979b934d09SEd Schouten print "static void"; 989b934d09SEd Schouten print l_prefix_name[p] "(teken_t *t, teken_char_t c)"; 999b934d09SEd Schouten print "{"; 1009b934d09SEd Schouten 1019b934d09SEd Schouten if (l_prefix_numbercmds[p] > 0) { 1029b934d09SEd Schouten print ""; 1039b934d09SEd Schouten print "\tif (teken_state_numbers(t, c))"; 1049b934d09SEd Schouten print "\t\treturn;"; 1059b934d09SEd Schouten } 1069b934d09SEd Schouten 1079b934d09SEd Schouten print ""; 1089b934d09SEd Schouten print "\tswitch (c) {"; 1099b934d09SEd Schouten for (c in l_cmd_prefix) { 1109b934d09SEd Schouten if (l_cmd_prefix[c] != p) 1119b934d09SEd Schouten continue; 1129b934d09SEd Schouten 1139b934d09SEd Schouten print "\tcase '" cchar(l_cmd_suffix[c]) "': /* " l_cmd_abbr[c] ": " l_cmd_name[c] " */"; 1149b934d09SEd Schouten 1159b934d09SEd Schouten if (l_cmd_args[c] == "v") { 1169b934d09SEd Schouten print "\t\t" l_cmd_c_name[c] "(t, t->t_curnum, t->t_nums);"; 1179b934d09SEd Schouten } else { 1189b934d09SEd Schouten printf "\t\t%s(t", l_cmd_c_name[c]; 1199b934d09SEd Schouten split(l_cmd_args[c], args, " "); 1209b934d09SEd Schouten for (a = 1; args[a] != ""; a++) { 1219b934d09SEd Schouten if (args[a] == "n") 1229b934d09SEd Schouten printf ", (t->t_curnum < %d || t->t_nums[%d] == 0) ? 1 : t->t_nums[%d]", a, (a - 1), (a - 1); 1239b934d09SEd Schouten else if (args[a] == "r") 1249b934d09SEd Schouten printf ", t->t_curnum < %d ? 0 : t->t_nums[%d]", a, (a - 1); 1259b934d09SEd Schouten else 1269b934d09SEd Schouten die("Invalid argument type: " args[a]); 1279b934d09SEd Schouten } 1289b934d09SEd Schouten print ");"; 1299b934d09SEd Schouten } 1309b934d09SEd Schouten print "\t\tbreak;"; 1319b934d09SEd Schouten } 1329b934d09SEd Schouten for (pc in l_prefix_parent) { 1339b934d09SEd Schouten if (l_prefix_parent[pc] != p) 1349b934d09SEd Schouten continue; 1359b934d09SEd Schouten print "\tcase '" cchar(l_prefix_suffix[pc]) "':"; 1369b934d09SEd Schouten print "\t\tteken_state_switch(t, " l_prefix_name[pc] ");"; 1379b934d09SEd Schouten print "\t\treturn;"; 1389b934d09SEd Schouten } 1399b934d09SEd Schouten 1409b934d09SEd Schouten print "\tdefault:"; 1419b934d09SEd Schouten if (l_prefix_name[p] == "teken_state_init") { 1429b934d09SEd Schouten print "\t\tteken_subr_regular_character(t, c);"; 1439b934d09SEd Schouten } else { 1449b934d09SEd Schouten print "\t\tteken_printf(\"Unsupported sequence in " l_prefix_name[p] ": %u\\n\", (unsigned int)c);"; 1459b934d09SEd Schouten } 1469b934d09SEd Schouten print "\t\tbreak;"; 1479b934d09SEd Schouten 1489b934d09SEd Schouten print "\t}"; 1499b934d09SEd Schouten 1509b934d09SEd Schouten if (l_prefix_name[p] != "teken_state_init") { 1519b934d09SEd Schouten print ""; 1529b934d09SEd Schouten print "\tteken_state_switch(t, teken_state_init);"; 1539b934d09SEd Schouten } 1549b934d09SEd Schouten print "}"; 1559b934d09SEd Schouten} 1569b934d09SEd Schouten 1579b934d09SEd Schouten} 158