1 /* $OpenBSD: alloc_entry.c,v 1.5 2003/03/18 16:55:54 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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 34 ****************************************************************************/ 35 36 /* 37 * alloc_entry.c -- allocation functions for terminfo entries 38 * 39 * _nc_copy_entry() 40 * _nc_init_entry() 41 * _nc_merge_entry() 42 * _nc_save_str() 43 * _nc_wrap_entry() 44 * 45 */ 46 47 #include <curses.priv.h> 48 49 #include <tic.h> 50 #include <term_entry.h> 51 52 MODULE_ID("$From: alloc_entry.c,v 1.35 2001/01/13 22:40:17 tom Exp $") 53 54 #define ABSENT_OFFSET -1 55 #define CANCELLED_OFFSET -2 56 57 #define MAX_STRTAB 4096 /* documented maximum entry size */ 58 59 static char stringbuf[MAX_STRTAB]; /* buffer for string capabilities */ 60 static size_t next_free; /* next free character in stringbuf */ 61 62 NCURSES_EXPORT(void) 63 _nc_init_entry(TERMTYPE * const tp) 64 /* initialize a terminal type data block */ 65 { 66 int i; 67 68 #if NCURSES_XNAMES 69 tp->num_Booleans = BOOLCOUNT; 70 tp->num_Numbers = NUMCOUNT; 71 tp->num_Strings = STRCOUNT; 72 tp->ext_Booleans = 0; 73 tp->ext_Numbers = 0; 74 tp->ext_Strings = 0; 75 #endif 76 if (tp->Booleans == 0) 77 tp->Booleans = typeMalloc(char, BOOLCOUNT); 78 if (tp->Numbers == 0) 79 tp->Numbers = typeMalloc(short, NUMCOUNT); 80 if (tp->Strings == 0) 81 tp->Strings = typeMalloc(char *, STRCOUNT); 82 83 for_each_boolean(i, tp) 84 tp->Booleans[i] = FALSE; 85 86 for_each_number(i, tp) 87 tp->Numbers[i] = ABSENT_NUMERIC; 88 89 for_each_string(i, tp) 90 tp->Strings[i] = ABSENT_STRING; 91 92 next_free = 0; 93 } 94 95 NCURSES_EXPORT(ENTRY *) 96 _nc_copy_entry(ENTRY * oldp) 97 { 98 ENTRY *newp = typeCalloc(ENTRY, 1); 99 100 if (newp != 0) { 101 *newp = *oldp; 102 _nc_copy_termtype(&(newp->tterm), &(oldp->tterm)); 103 } 104 return newp; 105 } 106 107 NCURSES_EXPORT(char *) 108 _nc_save_str(const char *const string) 109 /* save a copy of string in the string buffer */ 110 { 111 size_t old_next_free = next_free; 112 size_t len; 113 114 len = strlcpy(stringbuf + next_free, string, sizeof(stringbuf) - next_free); 115 if (++len < sizeof(stringbuf) - next_free) { 116 DEBUG(7, ("Saved string %s", _nc_visbuf(string))); 117 DEBUG(7, ("at location %d", (int) next_free)); 118 next_free += len; 119 } 120 return (stringbuf + old_next_free); 121 } 122 123 NCURSES_EXPORT(void) 124 _nc_wrap_entry(ENTRY * const ep, bool copy_strings) 125 /* copy the string parts to allocated storage, preserving pointers to it */ 126 { 127 int offsets[MAX_ENTRY_SIZE / 2], useoffsets[MAX_USES]; 128 int i, n; 129 TERMTYPE *tp = &(ep->tterm); 130 131 if (copy_strings) { 132 next_free = 0; /* clear static storage */ 133 134 /* copy term_names, Strings, uses */ 135 tp->term_names = _nc_save_str(tp->term_names); 136 for_each_string(i, tp) { 137 if (tp->Strings[i] != ABSENT_STRING && 138 tp->Strings[i] != CANCELLED_STRING) { 139 tp->Strings[i] = _nc_save_str(tp->Strings[i]); 140 } 141 } 142 143 for (i = 0; i < ep->nuses; i++) { 144 if (ep->uses[i].name == 0) { 145 ep->uses[i].name = _nc_save_str(ep->uses[i].name); 146 } 147 } 148 149 free(tp->str_table); 150 } 151 152 n = tp->term_names - stringbuf; 153 for_each_string(i, &(ep->tterm)) { 154 if (tp->Strings[i] == ABSENT_STRING) 155 offsets[i] = ABSENT_OFFSET; 156 else if (tp->Strings[i] == CANCELLED_STRING) 157 offsets[i] = CANCELLED_OFFSET; 158 else 159 offsets[i] = tp->Strings[i] - stringbuf; 160 } 161 162 for (i = 0; i < ep->nuses; i++) { 163 if (ep->uses[i].name == 0) 164 useoffsets[i] = ABSENT_OFFSET; 165 else 166 useoffsets[i] = ep->uses[i].name - stringbuf; 167 } 168 169 if ((tp->str_table = typeMalloc(char, next_free)) == (char *) 0) 170 _nc_err_abort("Out of memory"); 171 (void) memcpy(tp->str_table, stringbuf, next_free); 172 173 tp->term_names = tp->str_table + n; 174 for_each_string(i, &(ep->tterm)) { 175 if (offsets[i] == ABSENT_OFFSET) 176 tp->Strings[i] = ABSENT_STRING; 177 else if (offsets[i] == CANCELLED_OFFSET) 178 tp->Strings[i] = CANCELLED_STRING; 179 else 180 tp->Strings[i] = tp->str_table + offsets[i]; 181 } 182 183 #if NCURSES_XNAMES 184 if (!copy_strings) { 185 if ((n = NUM_EXT_NAMES(tp)) != 0) { 186 size_t copied, length, strtabsize = 0; 187 for (i = 0; i < n; i++) { 188 strtabsize += strlen(tp->ext_Names[i]) + 1; 189 offsets[i] = tp->ext_Names[i] - stringbuf; 190 } 191 if ((tp->ext_str_table = typeMalloc(char, strtabsize)) == 0) 192 _nc_err_abort("Out of memory"); 193 for (i = 0, length = 0; i < n; i++) { 194 tp->ext_Names[i] = tp->ext_str_table + length; 195 copied = strlcpy(tp->ext_Names[i], stringbuf + offsets[i], 196 strtabsize) + 1; 197 if (copied > strtabsize) 198 _nc_err_abort("Buffer overflow"); 199 length += copied; 200 strtabsize -= copied; 201 } 202 } 203 } 204 #endif 205 206 for (i = 0; i < ep->nuses; i++) { 207 if (useoffsets[i] == ABSENT_OFFSET) 208 ep->uses[i].name = 0; 209 else 210 ep->uses[i].name = (tp->str_table + useoffsets[i]); 211 } 212 } 213 214 NCURSES_EXPORT(void) 215 _nc_merge_entry 216 (TERMTYPE * const to, TERMTYPE * const from) 217 /* merge capabilities from `from' entry into `to' entry */ 218 { 219 int i; 220 221 #if NCURSES_XNAMES 222 _nc_align_termtype(to, from); 223 #endif 224 for_each_boolean(i, from) { 225 int mergebool = from->Booleans[i]; 226 227 if (mergebool == CANCELLED_BOOLEAN) 228 to->Booleans[i] = FALSE; 229 else if (mergebool == TRUE) 230 to->Booleans[i] = mergebool; 231 } 232 233 for_each_number(i, from) { 234 int mergenum = from->Numbers[i]; 235 236 if (mergenum == CANCELLED_NUMERIC) 237 to->Numbers[i] = ABSENT_NUMERIC; 238 else if (mergenum != ABSENT_NUMERIC) 239 to->Numbers[i] = mergenum; 240 } 241 242 /* 243 * Note: the copies of strings this makes don't have their own 244 * storage. This is OK right now, but will be a problem if we 245 * we ever want to deallocate entries. 246 */ 247 for_each_string(i, from) { 248 char *mergestring = from->Strings[i]; 249 250 if (mergestring == CANCELLED_STRING) 251 to->Strings[i] = ABSENT_STRING; 252 else if (mergestring != ABSENT_STRING) 253 to->Strings[i] = mergestring; 254 } 255 } 256