xref: /openbsd/lib/libcurses/tinfo/MKcodes.awk (revision 4bdff4be)
1# $OpenBSD: MKcodes.awk,v 1.2 2023/10/17 09:52:09 nicm Exp $
2##############################################################################
3# Copyright 2019,2020 Thomas E. Dickey                                       #
4# Copyright 2007-2009,2010 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: MKcodes.awk,v 1.2 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 MKcodes.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_boolcodes = small_boolcodes small_item($4);
91			large_boolcodes = large_boolcodes large_item($4);
92		}
93
94$3 == "num"	{
95			small_numcodes = small_numcodes small_item($4);
96			large_numcodes = large_numcodes large_item($4);
97		}
98
99$3 == "str"	{
100			small_strcodes = small_strcodes small_item($4);
101			large_strcodes = large_strcodes large_item($4);
102		}
103
104END	{
105		print  ""
106		print  "#if BROKEN_LINKER || USE_REENTRANT"
107		print  ""
108		if (bigstrings) {
109			printf "static const char _nc_code_blob[] = \n"
110			printf "%s;\n", bigstr;
111			print_offsets("boolcodes", large_boolcodes);
112			print_offsets("numcodes", large_numcodes);
113			print_offsets("strcodes", large_strcodes);
114			print  ""
115			print  "static IT *"
116			print  "alloc_array(NCURSES_CONST char ***value, const short *offsets, unsigned size)"
117			print  "{"
118			print  "	if (*value == 0) {"
119			print  "		if ((*value = typeCalloc(NCURSES_CONST char *, size + 1)) != 0) {"
120			print  "			unsigned n;"
121			print  "			for (n = 0; n < size; ++n) {"
122			print  "				(*value)[n] = (NCURSES_CONST char *) _nc_code_blob + offsets[n];"
123			print  "			}"
124			print  "		}"
125			print  "	}"
126			print  "	return *value;"
127			print  "}"
128			print  ""
129			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)); }"
130		} else {
131			print  "#define DCL(it) static IT data##it[]"
132			print  ""
133			print_strings("boolcodes", small_boolcodes);
134			print_strings("numcodes", small_numcodes);
135			print_strings("strcodes", small_strcodes);
136			print  "#define FIX(it) NCURSES_IMPEXP IT * NCURSES_API NCURSES_PUBLIC_VAR(it)(void) { return data##it; }"
137		}
138		print  ""
139		print  "/* remove public definition which conflicts with FIX() */"
140		print  "#undef boolcodes"
141		print  "#undef numcodes"
142		print  "#undef strcodes"
143		print  ""
144		print  "/* add local definition */"
145		print  "FIX(boolcodes)"
146		print  "FIX(numcodes)"
147		print  "FIX(strcodes)"
148		print  ""
149		print  "/* restore the public definition */"
150		print  ""
151		print  "#define FREE_FIX(it) if (ptr_##it) { FreeAndNull(ptr_##it); }"
152		print  "#define boolcodes  NCURSES_PUBLIC_VAR(boolcodes())"
153		print  "#define numcodes   NCURSES_PUBLIC_VAR(numcodes())"
154		print  "#define strcodes   NCURSES_PUBLIC_VAR(strcodes())"
155		print  ""
156		print  "#if NO_LEAKS"
157		print  "NCURSES_EXPORT(void)"
158		print  "_nc_codes_leaks(void)"
159		print  "{"
160		if (bigstrings) {
161		print  "FREE_FIX(boolcodes)"
162		print  "FREE_FIX(numcodes)"
163		print  "FREE_FIX(strcodes)"
164		}
165		print  "}"
166		print  "#endif"
167		print  ""
168		print  "#else"
169		print  ""
170		print  "#define DCL(it) NCURSES_EXPORT_VAR(IT) it[]"
171		print  ""
172		print_strings("boolcodes", small_boolcodes);
173		print_strings("numcodes", small_numcodes);
174		print_strings("strcodes", small_strcodes);
175		print  ""
176		print  "#endif /* BROKEN_LINKER */"
177	}
178