1 /**************************************************************************** 2 * Copyright (c) 1998 Free Software Foundation, Inc. * 3 * * 4 * Permission is hereby granted, free of charge, to any person obtaining a * 5 * copy of this software and associated documentation files (the * 6 * "Software"), to deal in the Software without restriction, including * 7 * without limitation the rights to use, copy, modify, merge, publish, * 8 * distribute, distribute with modifications, sublicense, and/or sell * 9 * copies of the Software, and to permit persons to whom the Software is * 10 * furnished to do so, subject to the following conditions: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 22 * * 23 * Except as contained in this notice, the name(s) of the above copyright * 24 * holders shall not be used in advertising or otherwise to promote the * 25 * sale, use or other dealings in this Software without prior written * 26 * authorization. * 27 ****************************************************************************/ 28 29 /**************************************************************************** 30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 31 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 32 ****************************************************************************/ 33 34 35 /* 36 * alloc_entry.c -- allocation functions for terminfo entries 37 * 38 * _nc_copy_entry() 39 * _nc_init_entry() 40 * _nc_merge_entry() 41 * _nc_save_str() 42 * _nc_wrap_entry() 43 * 44 */ 45 46 #include <curses.priv.h> 47 48 #include <tic.h> 49 #include <term_entry.h> 50 51 MODULE_ID("$Id: alloc_entry.c,v 1.30 1999/03/01 02:03:45 tom Exp $") 52 53 #define ABSENT_OFFSET -1 54 #define CANCELLED_OFFSET -2 55 56 #define MAX_STRTAB 4096 /* documented maximum entry size */ 57 58 static char stringbuf[MAX_STRTAB]; /* buffer for string capabilities */ 59 static size_t next_free; /* next free character in stringbuf */ 60 61 void _nc_init_entry(TERMTYPE *const tp) 62 /* initialize a terminal type data block */ 63 { 64 int i; 65 66 #if NCURSES_XNAMES 67 tp->num_Booleans = BOOLCOUNT; 68 tp->num_Numbers = NUMCOUNT; 69 tp->num_Strings = STRCOUNT; 70 tp->ext_Booleans = 0; 71 tp->ext_Numbers = 0; 72 tp->ext_Strings = 0; 73 #endif 74 if (tp->Booleans == 0) 75 tp->Booleans = typeMalloc(char,BOOLCOUNT); 76 if (tp->Numbers == 0) 77 tp->Numbers = typeMalloc(short,NUMCOUNT); 78 if (tp->Strings == 0) 79 tp->Strings = typeMalloc(char *,STRCOUNT); 80 81 for_each_boolean(i,tp) 82 tp->Booleans[i] = FALSE; 83 84 for_each_number(i,tp) 85 tp->Numbers[i] = ABSENT_NUMERIC; 86 87 for_each_string(i,tp) 88 tp->Strings[i] = ABSENT_STRING; 89 90 next_free = 0; 91 } 92 93 ENTRY *_nc_copy_entry(ENTRY *oldp) 94 { 95 ENTRY *newp = typeCalloc(ENTRY,1); 96 97 if (newp != 0) { 98 *newp = *oldp; 99 _nc_copy_termtype(&(newp->tterm), &(oldp->tterm)); 100 } 101 return newp; 102 } 103 104 char *_nc_save_str(const char *const string) 105 /* save a copy of string in the string buffer */ 106 { 107 size_t old_next_free = next_free; 108 size_t len = strlen(string) + 1; 109 110 if (next_free + len < MAX_STRTAB) 111 { 112 strcpy(&stringbuf[next_free], string); 113 DEBUG(7, ("Saved string %s", _nc_visbuf(string))); 114 DEBUG(7, ("at location %d", (int) next_free)); 115 next_free += len; 116 } 117 return(stringbuf + old_next_free); 118 } 119 120 void _nc_wrap_entry(ENTRY *const ep) 121 /* copy the string parts to allocated storage, preserving pointers to it */ 122 { 123 int offsets[MAX_ENTRY_SIZE/2], useoffsets[MAX_USES]; 124 int i, n; 125 TERMTYPE *tp = &(ep->tterm); 126 127 n = tp->term_names - stringbuf; 128 for_each_string(i, &(ep->tterm)) { 129 if (tp->Strings[i] == ABSENT_STRING) 130 offsets[i] = ABSENT_OFFSET; 131 else if (tp->Strings[i] == CANCELLED_STRING) 132 offsets[i] = CANCELLED_OFFSET; 133 else 134 offsets[i] = tp->Strings[i] - stringbuf; 135 } 136 137 for (i=0; i < ep->nuses; i++) { 138 if (ep->uses[i].parent == (void *)0) 139 useoffsets[i] = ABSENT_OFFSET; 140 else 141 useoffsets[i] = (char *)(ep->uses[i].parent) - stringbuf; 142 } 143 144 if ((tp->str_table = typeMalloc(char, next_free)) == (char *)0) 145 _nc_err_abort("Out of memory"); 146 (void) memcpy(tp->str_table, stringbuf, next_free); 147 148 tp->term_names = tp->str_table + n; 149 for_each_string(i, &(ep->tterm)) { 150 if (offsets[i] == ABSENT_OFFSET) 151 tp->Strings[i] = ABSENT_STRING; 152 else if (offsets[i] == CANCELLED_OFFSET) 153 tp->Strings[i] = CANCELLED_STRING; 154 else 155 tp->Strings[i] = tp->str_table + offsets[i]; 156 } 157 158 #if NCURSES_XNAMES 159 if ((n = NUM_EXT_NAMES(tp)) != 0) { 160 unsigned length = 0; 161 for (i = 0; i < n; i++) { 162 length += strlen(tp->ext_Names[i]) + 1; 163 offsets[i] = tp->ext_Names[i] - stringbuf; 164 } 165 if ((tp->ext_str_table = typeMalloc(char, length)) == 0) 166 _nc_err_abort("Out of memory"); 167 for (i = 0, length = 0; i < n; i++) { 168 tp->ext_Names[i] = tp->ext_str_table + length; 169 strcpy(tp->ext_Names[i], stringbuf + offsets[i]); 170 length += strlen(tp->ext_Names[i]) + 1; 171 } 172 } 173 #endif 174 175 for (i=0; i < ep->nuses; i++) { 176 if (useoffsets[i] == ABSENT_OFFSET) 177 ep->uses[i].parent = (void *)0; 178 else 179 ep->uses[i].parent = (char *)(tp->str_table + useoffsets[i]); 180 } 181 } 182 183 void _nc_merge_entry(TERMTYPE *const to, TERMTYPE *const from) 184 /* merge capabilities from `from' entry into `to' entry */ 185 { 186 int i; 187 188 #if NCURSES_XNAMES 189 _nc_align_termtype(to, from); 190 #endif 191 for_each_boolean(i, from) 192 { 193 int mergebool = from->Booleans[i]; 194 195 if (mergebool == CANCELLED_BOOLEAN) 196 to->Booleans[i] = FALSE; 197 else if (mergebool == TRUE) 198 to->Booleans[i] = mergebool; 199 } 200 201 for_each_number(i, from) 202 { 203 int mergenum = from->Numbers[i]; 204 205 if (mergenum == CANCELLED_NUMERIC) 206 to->Numbers[i] = ABSENT_NUMERIC; 207 else if (mergenum != ABSENT_NUMERIC) 208 to->Numbers[i] = mergenum; 209 } 210 211 /* 212 * Note: the copies of strings this makes don't have their own 213 * storage. This is OK right now, but will be a problem if we 214 * we ever want to deallocate entries. 215 */ 216 for_each_string(i, from) 217 { 218 char *mergestring = from->Strings[i]; 219 220 if (mergestring == CANCELLED_STRING) 221 to->Strings[i] = ABSENT_STRING; 222 else if (mergestring != ABSENT_STRING) 223 to->Strings[i] = mergestring; 224 } 225 } 226