xref: /freebsd/contrib/ncurses/ncurses/tinfo/add_tries.c (revision 21817992b3314c908ab50f0bb88d2ee750b9c4ac)
1 /****************************************************************************
2  * Copyright 2019-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            1998-on                             *
32  ****************************************************************************/
33 
34 /*
35 **	add_tries.c
36 **
37 **	Add keycode/string to tries-tree.
38 **
39 */
40 
41 #include <curses.priv.h>
42 #include <tic.h>
43 
44 MODULE_ID("$Id: add_tries.c,v 1.13 2023/06/24 15:36:13 tom Exp $")
45 
46 #define SET_TRY(dst,src) if ((dst->ch = *src++) == 128) dst->ch = '\0'
47 #define CMP_TRY(a,b) ((a)? (a == b) : (b == 128))
48 
NCURSES_EXPORT(int)49 NCURSES_EXPORT(int)
50 _nc_add_to_try(TRIES ** tree, const char *str, unsigned code)
51 {
52     TRIES *ptr, *savedptr;
53     unsigned const char *txt = (unsigned const char *) str;
54 
55     T((T_CALLED("_nc_add_to_try(%p, %s, %u)"),
56        (void *) *tree, _nc_visbuf(str), code));
57     if (!VALID_STRING(str) || *txt == '\0' || code == 0)
58 	returnCode(ERR);
59 
60     if ((*tree) != 0) {
61 	ptr = savedptr = (*tree);
62 
63 	for (;;) {
64 	    unsigned char cmp = *txt;
65 
66 	    while (!CMP_TRY(ptr->ch, cmp)
67 		   && ptr->sibling != 0)
68 		ptr = ptr->sibling;
69 
70 	    if (CMP_TRY(ptr->ch, cmp)) {
71 		if (*(++txt) == '\0') {
72 		    ptr->value = (unsigned short) code;
73 		    returnCode(OK);
74 		}
75 		if (ptr->child != 0)
76 		    ptr = ptr->child;
77 		else
78 		    break;
79 	    } else {
80 		if ((ptr->sibling = typeCalloc(TRIES, 1)) == 0) {
81 		    returnCode(ERR);
82 		}
83 
84 		savedptr = ptr = ptr->sibling;
85 		SET_TRY(ptr, txt);
86 		ptr->value = 0;
87 
88 		break;
89 	    }
90 	}			/* end for (;;) */
91     } else {			/* (*tree) == 0 :: First sequence to be added */
92 	savedptr = ptr = (*tree) = typeCalloc(TRIES, 1);
93 
94 	if (ptr == 0) {
95 	    returnCode(ERR);
96 	}
97 
98 	SET_TRY(ptr, txt);
99 	ptr->value = 0;
100     }
101 
102     /* at this point, we are adding to the try.  ptr->child == 0 */
103 
104     while (*txt) {
105 	ptr->child = typeCalloc(TRIES, 1);
106 
107 	ptr = ptr->child;
108 
109 	if (ptr == 0) {
110 	    while ((ptr = savedptr) != 0) {
111 		savedptr = ptr->child;
112 		free(ptr);
113 	    }
114 	    *tree = NULL;
115 	    returnCode(ERR);
116 	}
117 
118 	SET_TRY(ptr, txt);
119 	ptr->value = 0;
120     }
121 
122     ptr->value = (unsigned short) code;
123     returnCode(OK);
124 }
125