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