1/****************************************************************************
2 * Copyright 2019,2020 Thomas E. Dickey                                     *
3 * Copyright 1998-2008,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            *
7 * "Software"), to deal in the Software without restriction, including      *
8 * without limitation the rights to use, copy, modify, merge, publish,      *
9 * distribute, distribute with modifications, sublicense, and/or sell       *
10 * copies of the Software, and to permit persons to whom the Software is    *
11 * furnished to do so, subject to the following conditions:                 *
12 *                                                                          *
13 * The above copyright notice and this permission notice shall be included  *
14 * in all copies or substantial portions of the Software.                   *
15 *                                                                          *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22 * THE USE OR OTHER 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     *
26 * sale, use or other dealings in this Software without prior written       *
27 * authorization.                                                           *
28 ****************************************************************************/
29
30/****************************************************************************
31 *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32 *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33 *     and: Thomas E. Dickey                        1996-on                 *
34 ****************************************************************************/
35
36/*
37 *	comp_hash.c --- Routines to deal with the hashtable of capability
38 *			names.
39 *
40 */
41
42#define USE_TERMLIB 1
43#include <curses.priv.h>
44
45#include <tic.h>
46#include <hashsize.h>
47
48MODULE_ID("$Id: comp_hash.c,v 1.53 2020/02/02 23:34:34 tom Exp $")
49
50/*
51 * Finds the entry for the given string in the hash table if present.
52 * Returns a pointer to the entry in the table or 0 if not found.
53 */
54/* entrypoint used by tack 1.07 */
55NCURSES_EXPORT(struct name_table_entry const *)
56_nc_find_entry(const char *string,
57	       const HashValue * hash_table)
58{
59    bool termcap = (hash_table != _nc_get_hash_table(FALSE));
60    const HashData *data = _nc_get_hash_info(termcap);
61    int hashvalue;
62    struct name_table_entry const *ptr = 0;
63    struct name_table_entry const *real_table;
64
65    hashvalue = data->hash_of(string);
66
67    if (hashvalue >= 0
68	&& (unsigned) hashvalue < data->table_size
69	&& data->table_data[hashvalue] >= 0) {
70
71	real_table = _nc_get_table(termcap);
72	ptr = real_table + data->table_data[hashvalue];
73	while (!data->compare_names(ptr->nte_name, string)) {
74	    if (ptr->nte_link < 0) {
75		ptr = 0;
76		break;
77	    }
78	    ptr = real_table + (ptr->nte_link
79				+ data->table_data[data->table_size]);
80	}
81    }
82
83    return (ptr);
84}
85
86/*
87 * Finds the entry for the given name with the given type in the given table if
88 * present (as distinct from _nc_find_entry, which finds the last entry
89 * regardless of type).
90 *
91 * Returns a pointer to the entry in the table or 0 if not found.
92 */
93NCURSES_EXPORT(struct name_table_entry const *)
94_nc_find_type_entry(const char *string,
95		    int type,
96		    bool termcap)
97{
98    struct name_table_entry const *ptr = NULL;
99    const HashData *data = _nc_get_hash_info(termcap);
100    int hashvalue = data->hash_of(string);
101
102    if (hashvalue >= 0
103	&& (unsigned) hashvalue < data->table_size
104	&& data->table_data[hashvalue] >= 0) {
105	const struct name_table_entry *const table = _nc_get_table(termcap);
106
107	ptr = table + data->table_data[hashvalue];
108	while (ptr->nte_type != type
109	       || !data->compare_names(ptr->nte_name, string)) {
110	    if (ptr->nte_link < 0) {
111		ptr = 0;
112		break;
113	    }
114	    ptr = table + (ptr->nte_link + data->table_data[data->table_size]);
115	}
116    }
117
118    return ptr;
119}
120
121#if NCURSES_XNAMES
122NCURSES_EXPORT(struct user_table_entry const *)
123_nc_find_user_entry(const char *string)
124{
125    const HashData *data = _nc_get_hash_user();
126    int hashvalue;
127    struct user_table_entry const *ptr = 0;
128    struct user_table_entry const *real_table;
129
130    hashvalue = data->hash_of(string);
131
132    if (hashvalue >= 0
133	&& (unsigned) hashvalue < data->table_size
134	&& data->table_data[hashvalue] >= 0) {
135
136	real_table = _nc_get_userdefs_table();
137	ptr = real_table + data->table_data[hashvalue];
138	while (!data->compare_names(ptr->ute_name, string)) {
139	    if (ptr->ute_link < 0) {
140		ptr = 0;
141		break;
142	    }
143	    ptr = real_table + (ptr->ute_link
144				+ data->table_data[data->table_size]);
145	}
146    }
147
148    return (ptr);
149}
150#endif /* NCURSES_XNAMES */
151