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