1##############################################################################
2# Copyright 2020 Thomas E. Dickey                                            #
3# Copyright 1998-2006,2007 Free Software Foundation, Inc.                    #
4#                                                                            #
5# Permission is hereby granted, free of charge, to any person obtaining a    #
6# copy of this software and associated documentation files (the "Software"), #
7# to deal in the Software without restriction, including without limitation  #
8# the rights to use, copy, modify, merge, publish, distribute, distribute    #
9# with modifications, sublicense, and/or sell copies of the Software, and to #
10# permit persons to whom the Software is furnished to do so, subject to the  #
11# following conditions:                                                      #
12#                                                                            #
13# The above copyright notice and this permission notice shall be included in #
14# all copies or substantial portions of the Software.                        #
15#                                                                            #
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
19# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
21# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
22# DEALINGS IN THE SOFTWARE.                                                  #
23#                                                                            #
24# Except as contained in this notice, the name(s) of the above copyright     #
25# holders shall not be used in advertising or otherwise to promote the sale, #
26# use or other dealings in this Software without prior written               #
27# authorization.                                                             #
28##############################################################################
29# $Id: MKcaptab.awk,v 1.21 2020/02/02 23:34:34 tom Exp $
30function add_string(text) {
31    if (text != "IGNORE") {
32	offsets[num_strings] = offset;
33	offset = offset + length(text) + 1;
34	printf "%s\\0", text;
35    } else {
36	offsets[num_strings] = -1;
37    }
38    num_strings = num_strings + 1;
39    if ((num_strings % 3) == 0) {
40	printf "\\\n";
41    }
42    return offsets[num_strings - 1];
43}
44BEGIN {
45	first = 1;
46	num_aliases = 0;
47	num_strings = 0;
48	offset = 0;
49}
50
51/^[^#]/ {
52	    if (first) {
53		printf "/* generated by MKcaptab.awk %s(%d) */\n", tablename, bigstrings;
54		print ""
55		if (bigstrings) {
56		    printf "static struct alias *_nc_%s_table = 0;\n", tablename;
57		    print "";
58		    printf "static const char %s_text[] = \"\\\n", tablename;
59		} else {
60		    printf "static const struct alias _nc_%s_table[] =\n", tablename;
61		    printf "{\n";
62		}
63		first = 0;
64	    }
65	    if ($1 == tablename) {
66		if ($3 == "IGNORE") {
67		    to = "(char *)NULL";
68		} else {
69		    to = "\"" $3 "\"";
70		}
71		if (bigstrings) {
72		    c1 = add_string($2);
73		    c2 = add_string($3);
74		    c3 = add_string($4);
75		    aliases[num_aliases] = sprintf("\t{%5d, %5d, %5d},\t /* %s */", c1, c2, c3, $5);
76		    num_aliases = num_aliases + 1;
77		} else {
78		    printf "\t{\"%s\", %s, \"%s\"},\t /* %s */\n", $2, to, $4, $5;
79		}
80	    }
81	}
82END	{
83	    if (bigstrings) {
84		printf "\";\n\n";
85		printf "static const alias_table_data %s_data[] = {\n", tablename;
86		for (n = 0; n < num_aliases; ++n) {
87		    printf "%s\n", aliases[n];
88		}
89		printf "};\n\n";
90	    } else {
91		printf "\t{(char *)NULL, (char *)NULL, (char *)NULL}\n";
92		printf "};\n\n";
93	    }
94	}
95# vile:sw=4:
96