Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2019-2020,2023 Thomas E. Dickey * |
| 3 | * Copyright 1998-2008,2009 Free Software Foundation, Inc. * |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 48 | MODULE_ID("$Id: comp_hash.c,v 1.55 2023/06/24 13:29:43 tom Exp $") |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 49 | |
| 50 | /* |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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. |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 53 | */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 54 | NCURSES_EXPORT(struct name_table_entry const *) |
| 55 | _nc_find_entry(const char *string, |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 56 | const HashValue * hash_table) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 57 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 58 | bool termcap = (hash_table != _nc_get_hash_table(FALSE)); |
| 59 | const HashData *data = _nc_get_hash_info(termcap); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 60 | int hashvalue; |
| 61 | struct name_table_entry const *ptr = 0; |
| 62 | struct name_table_entry const *real_table; |
| 63 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 64 | hashvalue = data->hash_of(string); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 65 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 66 | if (hashvalue >= 0 |
| 67 | && (unsigned) hashvalue < data->table_size |
| 68 | && data->table_data[hashvalue] >= 0) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 69 | |
| 70 | real_table = _nc_get_table(termcap); |
| 71 | ptr = real_table + data->table_data[hashvalue]; |
| 72 | while (!data->compare_names(ptr->nte_name, string)) { |
| 73 | if (ptr->nte_link < 0) { |
| 74 | ptr = 0; |
| 75 | break; |
| 76 | } |
| 77 | ptr = real_table + (ptr->nte_link |
| 78 | + data->table_data[data->table_size]); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | return (ptr); |
| 83 | } |
| 84 | |
| 85 | /* |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 86 | * Finds the entry for the given name with the given type in the given table if |
| 87 | * present (as distinct from _nc_find_entry, which finds the last entry |
| 88 | * regardless of type). |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 89 | * |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 90 | * Returns a pointer to the entry in the table or 0 if not found. |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 91 | */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 92 | NCURSES_EXPORT(struct name_table_entry const *) |
| 93 | _nc_find_type_entry(const char *string, |
| 94 | int type, |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 95 | bool termcap) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 96 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 97 | struct name_table_entry const *ptr = NULL; |
| 98 | const HashData *data = _nc_get_hash_info(termcap); |
| 99 | int hashvalue = data->hash_of(string); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 100 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 101 | if (hashvalue >= 0 |
| 102 | && (unsigned) hashvalue < data->table_size |
| 103 | && data->table_data[hashvalue] >= 0) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 104 | const struct name_table_entry *const table = _nc_get_table(termcap); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 105 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 106 | if (table != NULL) { |
| 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]); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 115 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 116 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 117 | } |
| 118 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 119 | return ptr; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 120 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 121 | |
| 122 | #if NCURSES_XNAMES |
| 123 | NCURSES_EXPORT(struct user_table_entry const *) |
| 124 | _nc_find_user_entry(const char *string) |
| 125 | { |
| 126 | const HashData *data = _nc_get_hash_user(); |
| 127 | int hashvalue; |
| 128 | struct user_table_entry const *ptr = 0; |
| 129 | struct user_table_entry const *real_table; |
| 130 | |
| 131 | hashvalue = data->hash_of(string); |
| 132 | |
| 133 | if (hashvalue >= 0 |
| 134 | && (unsigned) hashvalue < data->table_size |
| 135 | && data->table_data[hashvalue] >= 0) { |
| 136 | |
| 137 | real_table = _nc_get_userdefs_table(); |
| 138 | ptr = real_table + data->table_data[hashvalue]; |
| 139 | while (!data->compare_names(ptr->ute_name, string)) { |
| 140 | if (ptr->ute_link < 0) { |
| 141 | ptr = 0; |
| 142 | break; |
| 143 | } |
| 144 | ptr = real_table + (ptr->ute_link |
| 145 | + data->table_data[data->table_size]); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return (ptr); |
| 150 | } |
| 151 | #endif /* NCURSES_XNAMES */ |