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