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