]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/MKcaptab.sh
d333301a0b377eeded747bb76f80f8c3fcec4b92
[ncurses.git] / ncurses / tinfo / MKcaptab.sh
1 #!/bin/sh
2 ##############################################################################
3 # Copyright (c) 2007,2009 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: MKcaptab.sh,v 1.10 2009/08/08 17:19:25 tom Exp $
30 AWK=${1-awk}
31 OPT1=${2-0}
32 OPT2=${3-tinfo/MKcaptab.awk}
33 DATA=${4-../include/Caps}
34
35 cat <<EOF
36 /*
37  * generated by $0
38  */
39
40 EOF
41
42 cat <<'EOF'
43 /*
44  *      comp_captab.c -- The names of the capabilities indexed via a hash
45  *                       table for the compiler.
46  *
47  */
48
49 #include <curses.priv.h>
50 #include <tic.h>
51 #include <hashsize.h>
52
53 EOF
54
55 ./make_hash 1 info $OPT1 <$DATA
56 ./make_hash 3 cap  $OPT1 <$DATA
57
58 $AWK -f $OPT2 bigstrings=$OPT1 tablename=capalias <$DATA 
59
60 $AWK -f $OPT2 bigstrings=$OPT1 tablename=infoalias <$DATA
61
62 cat <<EOF
63
64 #if $OPT1
65 static void
66 next_string(const char *strings, unsigned *offset)
67 {
68     *offset += strlen(strings + *offset) + 1;
69 }
70
71 static const struct name_table_entry *
72 _nc_build_names(struct name_table_entry **actual,
73                 const name_table_data *source,
74                 const char *strings)
75 {
76     if (*actual == 0) {
77         *actual = typeCalloc(struct name_table_entry, CAPTABSIZE);
78         if (*actual != 0) {
79             unsigned n;
80             unsigned len = 0;
81             for (n = 0; n < CAPTABSIZE; ++n) {
82                 (*actual)[n].nte_name = strings + len;
83                 (*actual)[n].nte_type = source[n].nte_type;
84                 (*actual)[n].nte_index = source[n].nte_index;
85                 (*actual)[n].nte_link = source[n].nte_link;
86                 next_string(strings, &len);
87             }
88         }
89     }
90     return *actual;
91 }
92
93 #define add_alias(field) \\
94         if (source[n].field >= 0) { \\
95                 (*actual)[n].field = strings + source[n].field; \\
96         }
97
98 static const struct alias *
99 _nc_build_alias(struct alias **actual,
100                 const alias_table_data *source,
101                 const char *strings,
102                 unsigned tablesize)
103 {
104     if (*actual == 0) {
105         *actual = typeCalloc(struct alias, tablesize + 1);
106         if (*actual != 0) {
107             unsigned n;
108             for (n = 0; n < tablesize; ++n) {
109                 add_alias(from);
110                 add_alias(to);
111                 add_alias(source);
112             }
113         }
114     }
115     return *actual;
116 }
117
118 #define build_names(root) _nc_build_names(&_nc_##root##_table, \\
119                                           root##_names_data, \\
120                                           root##_names_text)
121 #define build_alias(root) _nc_build_alias(&_nc_##root##alias_table, \\
122                                           root##alias_data, \\
123                                           root##alias_text, \\
124                                           SIZEOF(root##alias_data))
125 #else
126 #define build_names(root) _nc_ ## root ## _table
127 #define build_alias(root) _nc_ ## root ## alias_table
128 #endif
129
130 NCURSES_EXPORT(const struct name_table_entry *) _nc_get_table (bool termcap)
131 {
132     return termcap ? build_names(cap) : build_names(info) ;
133 }
134
135 /* entrypoint used by tack (do not alter) */
136 NCURSES_EXPORT(const HashValue *) _nc_get_hash_table (bool termcap)
137 {
138     return termcap ? _nc_cap_hash_table: _nc_info_hash_table ;
139 }
140
141 NCURSES_EXPORT(const struct alias *) _nc_get_alias_table (bool termcap)
142 {
143     return termcap ? build_alias(cap) : build_alias(info) ;
144 }
145
146 static HashValue
147 hash_function(const char *string)
148 {
149     long sum = 0;
150
151     DEBUG(9, ("hashing %s", string));
152     while (*string) {
153         sum += (long) (*string + (*(string + 1) << 8));
154         string++;
155     }
156
157     DEBUG(9, ("sum is %ld", sum));
158     return (HashValue) (sum % HASHTABSIZE);
159 }
160
161 static int
162 compare_cap_names(const char *a, const char *b)
163 {
164     return !strncmp(a, b, 2);
165 }
166
167 static int
168 compare_info_names(const char *a, const char *b)
169 {
170     return !strcmp(a, b);
171 }
172
173 static const HashData hash_data[2] = {
174     { HASHTABSIZE, _nc_info_hash_table, hash_function, compare_info_names },
175     { HASHTABSIZE, _nc_cap_hash_table, hash_function, compare_cap_names }
176 };
177
178 NCURSES_EXPORT(const HashData *) _nc_get_hash_info (bool termcap)
179 {
180     return &hash_data[(termcap != FALSE)];
181 }
182
183 #if NO_LEAKS
184 NCURSES_EXPORT(void) _nc_comp_captab_leaks(void)
185 {
186 #if $OPT1
187     FreeIfNeeded(_nc_cap_table);
188     FreeIfNeeded(_nc_info_table);
189     FreeIfNeeded(_nc_capalias_table);
190     FreeIfNeeded(_nc_infoalias_table);
191 #endif
192 }
193 #endif /* NO_LEAKS */
194 EOF