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