1##############################################################################
2# Copyright 2019,2020 Thomas E. Dickey                                       #
3# Copyright 1998-2008,2009 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: MKnames.awk,v 1.24 2020/02/02 23:34:34 tom Exp $
30function large_item(value) {
31	result = sprintf("%d,", offset);
32	offset = offset + length(value) + 1;
33	offcol = offcol + length(result) + 2;
34	if (offcol > 70) {
35		result = result "\n";
36		offcol = 0;
37	} else {
38		result = result " ";
39	}
40	bigstr = bigstr sprintf("\"%s\\0\" ", value);
41	bigcol = bigcol + length(value) + 5;
42	if (bigcol > 70) {
43		bigstr = bigstr "\\\n";
44		bigcol = 0;
45	}
46	return result;
47}
48
49function small_item(value) {
50	return sprintf("\t\t\"%s\",\n", value);
51}
52
53function print_strings(name,value) {
54	printf  "DCL(%s) = {\n", name
55	print  value
56	print  "\t\t(NCURSES_CONST char *)0,"
57	print  "};"
58	print  ""
59}
60
61function print_offsets(name,value) {
62	printf  "static const short _nc_offset_%s[] = {\n", name
63	printf "%s",  value
64	print  "};"
65	print  ""
66	printf "static NCURSES_CONST char ** ptr_%s = 0;\n", name
67	print  ""
68}
69
70BEGIN	{
71		print  "/* This file was generated by MKnames.awk */"
72		print  ""
73		print  "#include <curses.priv.h>"
74		print  ""
75		print  "#define IT NCURSES_CONST char * const"
76		print  ""
77		offset = 0;
78		offcol = 0;
79		bigcol = 0;
80	}
81
82$1 ~ /^#/		{next;}
83$1 ~ /^(cap|info)alias/	{next;}
84
85$1 == "userdef"		{next;}
86$1 == "SKIPWARN"	{next;}
87
88$3 == "bool"	{
89			small_boolnames = small_boolnames small_item($2);
90			large_boolnames = large_boolnames large_item($2);
91			small_boolfnames = small_boolfnames small_item($1);
92			large_boolfnames = large_boolfnames large_item($1);
93		}
94
95$3 == "num"	{
96			small_numnames = small_numnames small_item($2);
97			large_numnames = large_numnames large_item($2);
98			small_numfnames = small_numfnames small_item($1);
99			large_numfnames = large_numfnames large_item($1);
100		}
101
102$3 == "str"	{
103			small_strnames = small_strnames small_item($2);
104			large_strnames = large_strnames large_item($2);
105			small_strfnames = small_strfnames small_item($1);
106			large_strfnames = large_strfnames large_item($1);
107		}
108
109END	{
110		print  ""
111		print  "#if BROKEN_LINKER || USE_REENTRANT"
112		print  ""
113		if (bigstrings) {
114			printf "static const char _nc_name_blob[] = \n"
115			printf "%s;\n", bigstr;
116			print_offsets("boolfnames", large_boolfnames);
117			print_offsets("boolnames", large_boolnames);
118			print_offsets("numfnames", large_numfnames);
119			print_offsets("numnames", large_numnames);
120			print_offsets("strfnames", large_strfnames);
121			print_offsets("strnames", large_strnames);
122			print  ""
123			print  "static IT *"
124			print  "alloc_array(NCURSES_CONST char ***value, const short *offsets, unsigned size)"
125			print  "{"
126			print  "	if (*value == 0) {"
127			print  "		if ((*value = typeCalloc(NCURSES_CONST char *, size + 1)) != 0) {"
128			print  "			unsigned n;"
129			print  "			for (n = 0; n < size; ++n) {"
130			print  "				(*value)[n] = (NCURSES_CONST char *) _nc_name_blob + offsets[n];"
131			print  "			}"
132			print  "		}"
133			print  "	}"
134			print  "	return *value;"
135			print  "}"
136			print  ""
137			print  "#define FIX(it) NCURSES_IMPEXP IT * NCURSES_API NCURSES_PUBLIC_VAR(it)(void) { return alloc_array(&ptr_##it, _nc_offset_##it, SIZEOF(_nc_offset_##it)); }"
138		} else {
139			print  "#define DCL(it) static IT data##it[]"
140			print  ""
141			print_strings("boolnames", small_boolnames);
142			print_strings("boolfnames", small_boolfnames);
143			print_strings("numnames", small_numnames);
144			print_strings("numfnames", small_numfnames);
145			print_strings("strnames", small_strnames);
146			print_strings("strfnames", small_strfnames);
147			print  "#define FIX(it) NCURSES_IMPEXP IT * NCURSES_API NCURSES_PUBLIC_VAR(it)(void) { return data##it; }"
148		}
149		print  ""
150		print  "/* remove public definition which conflicts with FIX() */"
151		print  "#undef boolnames"
152		print  "#undef boolfnames"
153		print  "#undef numnames"
154		print  "#undef numfnames"
155		print  "#undef strnames"
156		print  "#undef strfnames"
157		print  ""
158		print  "/* add local definition */"
159		print  "FIX(boolnames)"
160		print  "FIX(boolfnames)"
161		print  "FIX(numnames)"
162		print  "FIX(numfnames)"
163		print  "FIX(strnames)"
164		print  "FIX(strfnames)"
165		print  ""
166		print  "/* restore the public definition */"
167		print  "#define boolnames  NCURSES_PUBLIC_VAR(boolnames())"
168		print  "#define boolfnames NCURSES_PUBLIC_VAR(boolfnames())"
169		print  "#define numnames   NCURSES_PUBLIC_VAR(numnames())"
170		print  "#define numfnames  NCURSES_PUBLIC_VAR(numfnames())"
171		print  "#define strnames   NCURSES_PUBLIC_VAR(strnames())"
172		print  "#define strfnames  NCURSES_PUBLIC_VAR(strfnames())"
173		print  ""
174		print  "#define FREE_FIX(it) if (ptr_##it) { FreeAndNull(ptr_##it); }"
175		print  ""
176		print  "#if NO_LEAKS"
177		print  "NCURSES_EXPORT(void)"
178		print  "_nc_names_leaks(void)"
179		print  "{"
180		if (bigstrings) {
181		print  "FREE_FIX(boolnames)"
182		print  "FREE_FIX(boolfnames)"
183		print  "FREE_FIX(numnames)"
184		print  "FREE_FIX(numfnames)"
185		print  "FREE_FIX(strnames)"
186		print  "FREE_FIX(strfnames)"
187		}
188		print  "}"
189		print  "#endif"
190		print  ""
191		print  "#else"
192		print  ""
193		print  "#define DCL(it) NCURSES_EXPORT_VAR(IT) it[]"
194		print  ""
195		print_strings("boolnames", small_boolnames);
196		print_strings("boolfnames", small_boolfnames);
197		print_strings("numnames", small_numnames);
198		print_strings("numfnames", small_numfnames);
199		print_strings("strnames", small_strnames);
200		print_strings("strfnames", small_strfnames);
201		print  ""
202		print  "#endif /* BROKEN_LINKER */"
203	}
204