1 /* $OpenBSD: add_tries.c,v 1.3 2001/01/22 18:01:50 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998,2000 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> 1998 * 33 ****************************************************************************/ 34 35 /* 36 ** add_tries.c 37 ** 38 ** Add keycode/string to tries-tree. 39 ** 40 */ 41 42 #include <curses.priv.h> 43 44 MODULE_ID("$From: add_tries.c,v 1.4 2000/12/10 02:55:07 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 49 NCURSES_EXPORT(void) 50 _nc_add_to_try(struct tries **tree, const char *str, unsigned short code) 51 { 52 static bool out_of_memory = FALSE; 53 struct tries *ptr, *savedptr; 54 unsigned const char *txt = (unsigned const char *) str; 55 56 if (txt == 0 || *txt == '\0' || out_of_memory || code == 0) 57 return; 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 = code; 72 return; 73 } 74 if (ptr->child != 0) 75 ptr = ptr->child; 76 else 77 break; 78 } else { 79 if ((ptr->sibling = typeCalloc(struct tries, 1)) == 0) { 80 out_of_memory = TRUE; 81 return; 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(struct tries, 1); 93 94 if (ptr == 0) { 95 out_of_memory = TRUE; 96 return; 97 } 98 99 SET_TRY(ptr, txt); 100 ptr->value = 0; 101 } 102 103 /* at this point, we are adding to the try. ptr->child == 0 */ 104 105 while (*txt) { 106 ptr->child = typeCalloc(struct tries, 1); 107 108 ptr = ptr->child; 109 110 if (ptr == 0) { 111 out_of_memory = TRUE; 112 113 while ((ptr = savedptr) != 0) { 114 savedptr = ptr->child; 115 free(ptr); 116 } 117 118 return; 119 } 120 121 SET_TRY(ptr, txt); 122 ptr->value = 0; 123 } 124 125 ptr->value = code; 126 return; 127 } 128