1 /* $OpenBSD: tries.c,v 1.2 1999/02/24 06:31:08 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Thomas E. Dickey <dickey@clark.net> 1997 * 33 ****************************************************************************/ 34 35 /* 36 ** tries.c 37 ** 38 ** Functions to manage the tree of partial-completions for keycodes. 39 ** 40 */ 41 42 #include <curses.priv.h> 43 44 MODULE_ID("$From: tries.c,v 1.10 1999/02/19 11:52:11 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 */ 50 char *_nc_expand_try(struct tries *tree, unsigned short code, int *count, size_t len) 51 { 52 struct tries *ptr = tree; 53 char *result = 0; 54 55 if (code != 0) { 56 while (ptr != 0) { 57 if ((result = _nc_expand_try(ptr->child, code, count, len + 1)) != 0) { 58 break; 59 } 60 if (ptr->value == code) { 61 *count -= 1; 62 if (*count == -1) { 63 result = typeCalloc(char, len+2); 64 break; 65 } 66 } 67 ptr = ptr->sibling; 68 } 69 } 70 if (result != 0) { 71 if ((result[len] = ptr->ch) == 0) 72 *((unsigned char *)(result+len)) = 128; 73 #ifdef TRACE 74 if (len == 0) 75 _tracef("expand_key %s %s", _trace_key(code), _nc_visbuf(result)); 76 #endif 77 } 78 return result; 79 } 80 81 /* 82 * Remove a code from the specified tree, freeing the unused nodes. Returns 83 * true if the code was found/removed. 84 */ 85 int _nc_remove_key(struct tries **tree, unsigned short code) 86 { 87 if (code == 0) 88 return FALSE; 89 90 while (*tree != 0) { 91 if (_nc_remove_key(&(*tree)->child, code)) { 92 return TRUE; 93 } 94 if ((*tree)->value == code) { 95 if((*tree)->child) { 96 /* don't cut the whole sub-tree */ 97 (*tree)->value = 0; 98 } else { 99 struct tries *to_free = *tree; 100 *tree = (*tree)->sibling; 101 free(to_free); 102 } 103 return TRUE; 104 } 105 tree = &(*tree)->sibling; 106 } 107 return FALSE; 108 } 109 110 /* 111 * Remove a string from the specified tree, freeing the unused nodes. Returns 112 * true if the string was found/removed. 113 */ 114 int _nc_remove_string(struct tries **tree, char *string) 115 { 116 if (string == 0 || *string == 0) 117 return FALSE; 118 119 while (*tree != 0) { 120 if (_nc_remove_string(&(*tree)->child, string+1)) { 121 return TRUE; 122 } 123 if ((unsigned char)(*tree)->ch == (unsigned char)*string) { 124 if((*tree)->child) { 125 /* don't cut the whole sub-tree */ 126 (*tree)->value = 0; 127 } else { 128 struct tries *to_free = *tree; 129 *tree = (*tree)->sibling; 130 free(to_free); 131 } 132 return TRUE; 133 } 134 tree = &(*tree)->sibling; 135 } 136 return FALSE; 137 } 138