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