1 /****************************************************************************
2 * Copyright 2020,2023 Thomas E. Dickey *
3 * Copyright 1998-2009,2010 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: Thomas E. Dickey <dickey@clark.net> 1997 *
32 ****************************************************************************/
33
34 /*
35 ** tries.c
36 **
37 ** Functions to manage the tree of partial-completions for keycodes.
38 **
39 */
40
41 #include <curses.priv.h>
42 #include <tic.h>
43
44 MODULE_ID("$Id: tries.c,v 1.32 2023/06/24 15:36:23 tom Exp $")
45
46 /*
47 * Expand a keycode into the string that it corresponds to, returning null if
48 * no match was found, otherwise allocating a string of the result.
49 */
NCURSES_EXPORT(char *)50 NCURSES_EXPORT(char *)
51 _nc_expand_try(TRIES * tree, unsigned code, int *count, size_t len)
52 {
53 TRIES *ptr = tree;
54 char *result = 0;
55
56 if (code != 0) {
57 while (ptr != 0) {
58 if ((result = _nc_expand_try(ptr->child, code, count, len + 1))
59 != 0) {
60 break;
61 }
62 if (ptr->value == code) {
63 *count -= 1;
64 if (*count == -1) {
65 result = typeCalloc(char, len + 2);
66 break;
67 }
68 }
69 ptr = ptr->sibling;
70 }
71 }
72 if (result != 0) {
73 if (ptr != 0 && (result[len] = (char) ptr->ch) == 0)
74 *((unsigned char *) (result + len)) = 128;
75 #ifdef TRACE
76 if (len == 0 && USE_TRACEF(TRACE_MAXIMUM)) {
77 _tracef("expand_key %s %s",
78 _nc_tracechar(CURRENT_SCREEN, (int) code),
79 _nc_visbuf(result));
80 _nc_unlock_global(tracef);
81 }
82 #endif
83 }
84 return result;
85 }
86
87 /*
88 * Remove a code from the specified tree, freeing the unused nodes. Returns
89 * true if the code was found/removed.
90 */
91 NCURSES_EXPORT(int)
_nc_remove_key(TRIES ** tree,unsigned code)92 _nc_remove_key(TRIES ** tree, unsigned code)
93 {
94 T((T_CALLED("_nc_remove_key(%p,%d)"), (void *) tree, code));
95
96 if (code == 0)
97 returnCode(FALSE);
98
99 while (*tree != 0) {
100 if (_nc_remove_key(&(*tree)->child, code)) {
101 returnCode(TRUE);
102 }
103 if ((*tree)->value == code) {
104 if ((*tree)->child) {
105 /* don't cut the whole sub-tree */
106 (*tree)->value = 0;
107 } else {
108 TRIES *to_free = *tree;
109 *tree = (*tree)->sibling;
110 free(to_free);
111 }
112 returnCode(TRUE);
113 }
114 tree = &(*tree)->sibling;
115 }
116 returnCode(FALSE);
117 }
118
119 /*
120 * Remove a string from the specified tree, freeing the unused nodes. Returns
121 * true if the string was found/removed.
122 */
123 NCURSES_EXPORT(int)
_nc_remove_string(TRIES ** tree,const char * string)124 _nc_remove_string(TRIES ** tree, const char *string)
125 {
126 T((T_CALLED("_nc_remove_string(%p,%s)"), (void *) tree, _nc_visbuf(string)));
127
128 if (!VALID_STRING(string) || *string == 0)
129 returnCode(FALSE);
130
131 while (*tree != 0) {
132 if (UChar((*tree)->ch) == UChar(*string)) {
133 if (string[1] != 0)
134 returnCode(_nc_remove_string(&(*tree)->child, string + 1));
135 if ((*tree)->child == 0) {
136 TRIES *to_free = *tree;
137 *tree = (*tree)->sibling;
138 free(to_free);
139 returnCode(TRUE);
140 } else {
141 returnCode(FALSE);
142 }
143 }
144 tree = &(*tree)->sibling;
145 }
146 returnCode(FALSE);
147 }
148