1##############################################################################
2# Copyright (c) 2007-2008,2009 Free Software Foundation, Inc.                #
3#                                                                            #
4# Permission is hereby granted, free of charge, to any person obtaining a    #
5# copy of this software and associated documentation files (the "Software"), #
6# to deal in the Software without restriction, including without limitation  #
7# the rights to use, copy, modify, merge, publish, distribute, distribute    #
8# with modifications, sublicense, and/or sell copies of the Software, and to #
9# permit persons to whom the Software is furnished to do so, subject to the  #
10# following conditions:                                                      #
11#                                                                            #
12# The above copyright notice and this permission notice shall be included in #
13# all copies or substantial portions of the Software.                        #
14#                                                                            #
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
18# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
20# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
21# DEALINGS IN THE SOFTWARE.                                                  #
22#                                                                            #
23# Except as contained in this notice, the name(s) of the above copyright     #
24# holders shall not be used in advertising or otherwise to promote the sale, #
25# use or other dealings in this Software without prior written               #
26# authorization.                                                             #
27##############################################################################
28# $Id: MKnames.awk,v 1.22 2009/03/21 21:03:39 tom Exp $
29function large_item(value) {
30	result = sprintf("%d,", offset);
31	offset = offset + length(value) + 1;
32	offcol = offcol + length(result) + 2;
33	if (offcol > 70) {
34		result = result "\n";
35		offcol = 0;
36	} else {
37		result = result " ";
38	}
39	bigstr = bigstr sprintf("\"%s\\0\" ", value);
40	bigcol = bigcol + length(value) + 5;
41	if (bigcol > 70) {
42		bigstr = bigstr "\\\n";
43		bigcol = 0;
44	}
45	return result;
46}
47
48function small_item(value) {
49	return sprintf("\t\t\"%s\",\n", value);
50}
51
52function print_strings(name,value) {
53	printf  "DCL(%s) = {\n", name
54	print  value
55	print  "\t\t(NCURSES_CONST char *)0,"
56	print  "};"
57	print  ""
58}
59
60function print_offsets(name,value) {
61	printf  "static const short _nc_offset_%s[] = {\n", name
62	printf "%s",  value
63	print  "};"
64	print  ""
65	printf "static NCURSES_CONST char ** ptr_%s = 0;\n", name
66	print  ""
67}
68
69BEGIN	{
70		print  "/* This file was generated by MKnames.awk */"
71		print  ""
72		print  "#include <curses.priv.h>"
73		print  ""
74		print  "#define IT NCURSES_CONST char * const"
75		print  ""
76		offset = 0;
77		offcol = 0;
78		bigcol = 0;
79	}
80
81$1 ~ /^#/		{next;}
82
83$1 == "SKIPWARN"	{next;}
84
85$3 == "bool"	{
86			small_boolnames = small_boolnames small_item($2);
87			large_boolnames = large_boolnames large_item($2);
88			small_boolfnames = small_boolfnames small_item($1);
89			large_boolfnames = large_boolfnames large_item($1);
90		}
91
92$3 == "num"	{
93			small_numnames = small_numnames small_item($2);
94			large_numnames = large_numnames large_item($2);
95			small_numfnames = small_numfnames small_item($1);
96			large_numfnames = large_numfnames large_item($1);
97		}
98
99$3 == "str"	{
100			small_strnames = small_strnames small_item($2);
101			large_strnames = large_strnames large_item($2);
102			small_strfnames = small_strfnames small_item($1);
103			large_strfnames = large_strfnames large_item($1);
104		}
105
106END	{
107		print  ""
108		print  "#if BROKEN_LINKER || USE_REENTRANT"
109		print  ""
110		if (bigstrings) {
111			printf "static const char _nc_name_blob[] = \n"
112			printf "%s;\n", bigstr;
113			print_offsets("boolfnames", large_boolfnames);
114			print_offsets("boolnames", large_boolnames);
115			print_offsets("numfnames", large_numfnames);
116			print_offsets("numnames", large_numnames);
117			print_offsets("strfnames", large_strfnames);
118			print_offsets("strnames", large_strnames);
119			print  ""
120			print  "static IT *"
121			print  "alloc_array(NCURSES_CONST char ***value, const short *offsets, unsigned size)"
122			print  "{"
123			print  "	if (*value == 0) {"
124			print  "		if ((*value = typeCalloc(NCURSES_CONST char *, size + 1)) != 0) {"
125			print  "			unsigned n;"
126			print  "			for (n = 0; n < size; ++n) {"
127			print  "				(*value)[n] = (NCURSES_CONST char *) _nc_name_blob + offsets[n];"
128			print  "			}"
129			print  "		}"
130			print  "	}"
131			print  "	return *value;"
132			print  "}"
133			print  ""
134			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)); }"
135		} else {
136			print  "#define DCL(it) static IT data##it[]"
137			print  ""
138			print_strings("boolnames", small_boolnames);
139			print_strings("boolfnames", small_boolfnames);
140			print_strings("numnames", small_numnames);
141			print_strings("numfnames", small_numfnames);
142			print_strings("strnames", small_strnames);
143			print_strings("strfnames", small_strfnames);
144			print  "#define FIX(it) NCURSES_IMPEXP IT * NCURSES_API NCURSES_PUBLIC_VAR(it)(void) { return data##it; }"
145		}
146		print  ""
147		print  "/* remove public definition which conflicts with FIX() */"
148		print  "#undef boolnames"
149		print  "#undef boolfnames"
150		print  "#undef numnames"
151		print  "#undef numfnames"
152		print  "#undef strnames"
153		print  "#undef strfnames"
154		print  ""
155		print  "/* add local definition */"
156		print  "FIX(boolnames)"
157		print  "FIX(boolfnames)"
158		print  "FIX(numnames)"
159		print  "FIX(numfnames)"
160		print  "FIX(strnames)"
161		print  "FIX(strfnames)"
162		print  ""
163		print  "/* restore the public definition */"
164		print  "#define boolnames  NCURSES_PUBLIC_VAR(boolnames())"
165		print  "#define boolfnames NCURSES_PUBLIC_VAR(boolfnames())"
166		print  "#define numnames   NCURSES_PUBLIC_VAR(numnames())"
167		print  "#define numfnames  NCURSES_PUBLIC_VAR(numfnames())"
168		print  "#define strnames   NCURSES_PUBLIC_VAR(strnames())"
169		print  "#define strfnames  NCURSES_PUBLIC_VAR(strfnames())"
170		print  ""
171		print  "#define FREE_FIX(it) if (ptr_##it) { FreeAndNull(ptr_##it); }"
172		print  ""
173		print  "#if NO_LEAKS"
174		print  "NCURSES_EXPORT(void)"
175		print  "_nc_names_leaks(void)"
176		print  "{"
177		if (bigstrings) {
178		print  "FREE_FIX(boolnames)"
179		print  "FREE_FIX(boolfnames)"
180		print  "FREE_FIX(numnames)"
181		print  "FREE_FIX(numfnames)"
182		print  "FREE_FIX(strnames)"
183		print  "FREE_FIX(strfnames)"
184		}
185		print  "}"
186		print  "#endif"
187		print  ""
188		print  "#else"
189		print  ""
190		print  "#define DCL(it) NCURSES_EXPORT_VAR(IT) it[]"
191		print  ""
192		print_strings("boolnames", small_boolnames);
193		print_strings("boolfnames", small_boolfnames);
194		print_strings("numnames", small_numnames);
195		print_strings("numfnames", small_numfnames);
196		print_strings("strnames", small_strnames);
197		print_strings("strfnames", small_strfnames);
198		print  ""
199		print  "#endif /* BROKEN_LINKER */"
200	}
201