1 /* $OpenBSD: add_tries.c,v 1.4 2010/01/12 23:22:06 nicm Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998-2005,2006 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 1998-on * 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("$Id: add_tries.c,v 1.4 2010/01/12 23:22:06 nicm 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(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)"), *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 = 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 returnCode(ERR); 114 } 115 116 SET_TRY(ptr, txt); 117 ptr->value = 0; 118 } 119 120 ptr->value = code; 121 returnCode(OK); 122 } 123