xref: /dflybsd-src/contrib/ncurses/ncurses/tinfo/comp_hash.c (revision 0cadad7e49c6219b0de0675ef6a6f44683d177d4)
1fdd4e1e0SJan Lentfer /****************************************************************************
2*32bb5217SDaniel Fojt  * Copyright 2019,2020 Thomas E. Dickey                                     *
3*32bb5217SDaniel Fojt  * Copyright 1998-2008,2009 Free Software Foundation, Inc.                  *
4fdd4e1e0SJan Lentfer  *                                                                          *
5fdd4e1e0SJan Lentfer  * Permission is hereby granted, free of charge, to any person obtaining a  *
6fdd4e1e0SJan Lentfer  * copy of this software and associated documentation files (the            *
7fdd4e1e0SJan Lentfer  * "Software"), to deal in the Software without restriction, including      *
8fdd4e1e0SJan Lentfer  * without limitation the rights to use, copy, modify, merge, publish,      *
9fdd4e1e0SJan Lentfer  * distribute, distribute with modifications, sublicense, and/or sell       *
10fdd4e1e0SJan Lentfer  * copies of the Software, and to permit persons to whom the Software is    *
11fdd4e1e0SJan Lentfer  * furnished to do so, subject to the following conditions:                 *
12fdd4e1e0SJan Lentfer  *                                                                          *
13fdd4e1e0SJan Lentfer  * The above copyright notice and this permission notice shall be included  *
14fdd4e1e0SJan Lentfer  * in all copies or substantial portions of the Software.                   *
15fdd4e1e0SJan Lentfer  *                                                                          *
16fdd4e1e0SJan Lentfer  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17fdd4e1e0SJan Lentfer  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18fdd4e1e0SJan Lentfer  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19fdd4e1e0SJan Lentfer  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20fdd4e1e0SJan Lentfer  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21fdd4e1e0SJan Lentfer  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22fdd4e1e0SJan Lentfer  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23fdd4e1e0SJan Lentfer  *                                                                          *
24fdd4e1e0SJan Lentfer  * Except as contained in this notice, the name(s) of the above copyright   *
25fdd4e1e0SJan Lentfer  * holders shall not be used in advertising or otherwise to promote the     *
26fdd4e1e0SJan Lentfer  * sale, use or other dealings in this Software without prior written       *
27fdd4e1e0SJan Lentfer  * authorization.                                                           *
28fdd4e1e0SJan Lentfer  ****************************************************************************/
29fdd4e1e0SJan Lentfer 
30fdd4e1e0SJan Lentfer /****************************************************************************
31fdd4e1e0SJan Lentfer  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32fdd4e1e0SJan Lentfer  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
331d102085SJan Lentfer  *     and: Thomas E. Dickey                        1996-on                 *
34fdd4e1e0SJan Lentfer  ****************************************************************************/
35fdd4e1e0SJan Lentfer 
36fdd4e1e0SJan Lentfer /*
37fdd4e1e0SJan Lentfer  *	comp_hash.c --- Routines to deal with the hashtable of capability
38fdd4e1e0SJan Lentfer  *			names.
39fdd4e1e0SJan Lentfer  *
40fdd4e1e0SJan Lentfer  */
41fdd4e1e0SJan Lentfer 
421d102085SJan Lentfer #define USE_TERMLIB 1
43fdd4e1e0SJan Lentfer #include <curses.priv.h>
44fdd4e1e0SJan Lentfer 
45fdd4e1e0SJan Lentfer #include <tic.h>
46fdd4e1e0SJan Lentfer #include <hashsize.h>
47fdd4e1e0SJan Lentfer 
48*32bb5217SDaniel Fojt MODULE_ID("$Id: comp_hash.c,v 1.53 2020/02/02 23:34:34 tom Exp $")
49fdd4e1e0SJan Lentfer 
50fdd4e1e0SJan Lentfer /*
51fdd4e1e0SJan Lentfer  * Finds the entry for the given string in the hash table if present.
52fdd4e1e0SJan Lentfer  * Returns a pointer to the entry in the table or 0 if not found.
53fdd4e1e0SJan Lentfer  */
54*32bb5217SDaniel Fojt /* entrypoint used by tack 1.07 */
NCURSES_EXPORT(struct name_table_entry const *)55fdd4e1e0SJan Lentfer NCURSES_EXPORT(struct name_table_entry const *)
561d102085SJan Lentfer _nc_find_entry(const char *string,
5700d8f3c4SJohn Marino 	       const HashValue * hash_table)
58fdd4e1e0SJan Lentfer {
5900d8f3c4SJohn Marino     bool termcap = (hash_table != _nc_get_hash_table(FALSE));
6000d8f3c4SJohn Marino     const HashData *data = _nc_get_hash_info(termcap);
61fdd4e1e0SJan Lentfer     int hashvalue;
621d102085SJan Lentfer     struct name_table_entry const *ptr = 0;
631d102085SJan Lentfer     struct name_table_entry const *real_table;
64fdd4e1e0SJan Lentfer 
6500d8f3c4SJohn Marino     hashvalue = data->hash_of(string);
66fdd4e1e0SJan Lentfer 
67*32bb5217SDaniel Fojt     if (hashvalue >= 0
68*32bb5217SDaniel Fojt 	&& (unsigned) hashvalue < data->table_size
69*32bb5217SDaniel Fojt 	&& data->table_data[hashvalue] >= 0) {
7000d8f3c4SJohn Marino 
7100d8f3c4SJohn Marino 	real_table = _nc_get_table(termcap);
7200d8f3c4SJohn Marino 	ptr = real_table + data->table_data[hashvalue];
7300d8f3c4SJohn Marino 	while (!data->compare_names(ptr->nte_name, string)) {
7400d8f3c4SJohn Marino 	    if (ptr->nte_link < 0) {
7500d8f3c4SJohn Marino 		ptr = 0;
7600d8f3c4SJohn Marino 		break;
7700d8f3c4SJohn Marino 	    }
7800d8f3c4SJohn Marino 	    ptr = real_table + (ptr->nte_link
7900d8f3c4SJohn Marino 				+ data->table_data[data->table_size]);
80fdd4e1e0SJan Lentfer 	}
81fdd4e1e0SJan Lentfer     }
82fdd4e1e0SJan Lentfer 
83fdd4e1e0SJan Lentfer     return (ptr);
84fdd4e1e0SJan Lentfer }
85fdd4e1e0SJan Lentfer 
86fdd4e1e0SJan Lentfer /*
8700d8f3c4SJohn Marino  * Finds the entry for the given name with the given type in the given table if
8800d8f3c4SJohn Marino  * present (as distinct from _nc_find_entry, which finds the last entry
8900d8f3c4SJohn Marino  * regardless of type).
90fdd4e1e0SJan Lentfer  *
9100d8f3c4SJohn Marino  * Returns a pointer to the entry in the table or 0 if not found.
92fdd4e1e0SJan Lentfer  */
93fdd4e1e0SJan Lentfer NCURSES_EXPORT(struct name_table_entry const *)
_nc_find_type_entry(const char * string,int type,bool termcap)941d102085SJan Lentfer _nc_find_type_entry(const char *string,
95fdd4e1e0SJan Lentfer 		    int type,
9600d8f3c4SJohn Marino 		    bool termcap)
97fdd4e1e0SJan Lentfer {
9800d8f3c4SJohn Marino     struct name_table_entry const *ptr = NULL;
9900d8f3c4SJohn Marino     const HashData *data = _nc_get_hash_info(termcap);
10000d8f3c4SJohn Marino     int hashvalue = data->hash_of(string);
101fdd4e1e0SJan Lentfer 
102*32bb5217SDaniel Fojt     if (hashvalue >= 0
103*32bb5217SDaniel Fojt 	&& (unsigned) hashvalue < data->table_size
104*32bb5217SDaniel Fojt 	&& data->table_data[hashvalue] >= 0) {
10500d8f3c4SJohn Marino 	const struct name_table_entry *const table = _nc_get_table(termcap);
106fdd4e1e0SJan Lentfer 
10700d8f3c4SJohn Marino 	ptr = table + data->table_data[hashvalue];
10800d8f3c4SJohn Marino 	while (ptr->nte_type != type
10900d8f3c4SJohn Marino 	       || !data->compare_names(ptr->nte_name, string)) {
11000d8f3c4SJohn Marino 	    if (ptr->nte_link < 0) {
11100d8f3c4SJohn Marino 		ptr = 0;
112fdd4e1e0SJan Lentfer 		break;
113fdd4e1e0SJan Lentfer 	    }
11400d8f3c4SJohn Marino 	    ptr = table + (ptr->nte_link + data->table_data[data->table_size]);
115fdd4e1e0SJan Lentfer 	}
116fdd4e1e0SJan Lentfer     }
117fdd4e1e0SJan Lentfer 
11800d8f3c4SJohn Marino     return ptr;
119fdd4e1e0SJan Lentfer }
120*32bb5217SDaniel Fojt 
121*32bb5217SDaniel Fojt #if NCURSES_XNAMES
122*32bb5217SDaniel Fojt NCURSES_EXPORT(struct user_table_entry const *)
_nc_find_user_entry(const char * string)123*32bb5217SDaniel Fojt _nc_find_user_entry(const char *string)
124*32bb5217SDaniel Fojt {
125*32bb5217SDaniel Fojt     const HashData *data = _nc_get_hash_user();
126*32bb5217SDaniel Fojt     int hashvalue;
127*32bb5217SDaniel Fojt     struct user_table_entry const *ptr = 0;
128*32bb5217SDaniel Fojt     struct user_table_entry const *real_table;
129*32bb5217SDaniel Fojt 
130*32bb5217SDaniel Fojt     hashvalue = data->hash_of(string);
131*32bb5217SDaniel Fojt 
132*32bb5217SDaniel Fojt     if (hashvalue >= 0
133*32bb5217SDaniel Fojt 	&& (unsigned) hashvalue < data->table_size
134*32bb5217SDaniel Fojt 	&& data->table_data[hashvalue] >= 0) {
135*32bb5217SDaniel Fojt 
136*32bb5217SDaniel Fojt 	real_table = _nc_get_userdefs_table();
137*32bb5217SDaniel Fojt 	ptr = real_table + data->table_data[hashvalue];
138*32bb5217SDaniel Fojt 	while (!data->compare_names(ptr->ute_name, string)) {
139*32bb5217SDaniel Fojt 	    if (ptr->ute_link < 0) {
140*32bb5217SDaniel Fojt 		ptr = 0;
141*32bb5217SDaniel Fojt 		break;
142*32bb5217SDaniel Fojt 	    }
143*32bb5217SDaniel Fojt 	    ptr = real_table + (ptr->ute_link
144*32bb5217SDaniel Fojt 				+ data->table_data[data->table_size]);
145*32bb5217SDaniel Fojt 	}
146*32bb5217SDaniel Fojt     }
147*32bb5217SDaniel Fojt 
148*32bb5217SDaniel Fojt     return (ptr);
149*32bb5217SDaniel Fojt }
150*32bb5217SDaniel Fojt #endif /* NCURSES_XNAMES */
151