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