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