1 /****************************************************************************
2 * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
36 * alloc_entry.c -- allocation functions for terminfo entries
46 #include <curses.priv.h>
49 #include <term_entry.h>
51 MODULE_ID("$Id: alloc_entry.c,v 1.47 2006/12/16 19:06:58 tom Exp $")
53 #define ABSENT_OFFSET -1
54 #define CANCELLED_OFFSET -2
56 #define MAX_STRTAB 4096 /* documented maximum entry size */
58 static char *stringbuf; /* buffer for string capabilities */
59 static size_t next_free; /* next free character in stringbuf */
62 _nc_init_entry(TERMTYPE *const tp)
63 /* initialize a terminal type data block */
68 if (tp == 0 && stringbuf != 0) {
69 FreeAndNull(stringbuf);
75 stringbuf = (char *) malloc(MAX_STRTAB);
78 tp->num_Booleans = BOOLCOUNT;
79 tp->num_Numbers = NUMCOUNT;
80 tp->num_Strings = STRCOUNT;
85 if (tp->Booleans == 0)
86 tp->Booleans = typeMalloc(NCURSES_SBOOL, BOOLCOUNT);
88 tp->Numbers = typeMalloc(short, NUMCOUNT);
90 tp->Strings = typeMalloc(char *, STRCOUNT);
92 for_each_boolean(i, tp)
93 tp->Booleans[i] = FALSE;
95 for_each_number(i, tp)
96 tp->Numbers[i] = ABSENT_NUMERIC;
98 for_each_string(i, tp)
99 tp->Strings[i] = ABSENT_STRING;
104 NCURSES_EXPORT(ENTRY *)
105 _nc_copy_entry(ENTRY * oldp)
107 ENTRY *newp = typeCalloc(ENTRY, 1);
111 _nc_copy_termtype(&(newp->tterm), &(oldp->tterm));
116 /* save a copy of string in the string buffer */
117 NCURSES_EXPORT(char *)
118 _nc_save_str(const char *const string)
121 size_t old_next_free = next_free;
122 size_t len = strlen(string) + 1;
124 if (len == 1 && next_free != 0) {
126 * Cheat a little by making an empty string point to the end of the
129 if (next_free < MAX_STRTAB) {
130 result = (stringbuf + next_free - 1);
132 } else if (next_free + len < MAX_STRTAB) {
133 strcpy(&stringbuf[next_free], string);
134 DEBUG(7, ("Saved string %s", _nc_visbuf(string)));
135 DEBUG(7, ("at location %d", (int) next_free));
137 result = (stringbuf + old_next_free);
139 _nc_warning("Too much data, some is lost");
145 _nc_wrap_entry(ENTRY * const ep, bool copy_strings)
146 /* copy the string parts to allocated storage, preserving pointers to it */
148 int offsets[MAX_ENTRY_SIZE / 2], useoffsets[MAX_USES];
150 unsigned nuses = ep->nuses;
151 TERMTYPE *tp = &(ep->tterm);
154 next_free = 0; /* clear static storage */
156 /* copy term_names, Strings, uses */
157 tp->term_names = _nc_save_str(tp->term_names);
158 for_each_string(i, tp) {
159 if (tp->Strings[i] != ABSENT_STRING &&
160 tp->Strings[i] != CANCELLED_STRING) {
161 tp->Strings[i] = _nc_save_str(tp->Strings[i]);
165 for (i = 0; i < nuses; i++) {
166 if (ep->uses[i].name == 0) {
167 ep->uses[i].name = _nc_save_str(ep->uses[i].name);
174 n = tp->term_names - stringbuf;
175 for_each_string(i, &(ep->tterm)) {
176 if (tp->Strings[i] == ABSENT_STRING)
177 offsets[i] = ABSENT_OFFSET;
178 else if (tp->Strings[i] == CANCELLED_STRING)
179 offsets[i] = CANCELLED_OFFSET;
181 offsets[i] = tp->Strings[i] - stringbuf;
184 for (i = 0; i < nuses; i++) {
185 if (ep->uses[i].name == 0)
186 useoffsets[i] = ABSENT_OFFSET;
188 useoffsets[i] = ep->uses[i].name - stringbuf;
191 if ((tp->str_table = typeMalloc(char, next_free)) == (char *) 0)
192 _nc_err_abort(MSG_NO_MEMORY);
193 (void) memcpy(tp->str_table, stringbuf, next_free);
195 tp->term_names = tp->str_table + n;
196 for_each_string(i, &(ep->tterm)) {
197 if (offsets[i] == ABSENT_OFFSET)
198 tp->Strings[i] = ABSENT_STRING;
199 else if (offsets[i] == CANCELLED_OFFSET)
200 tp->Strings[i] = CANCELLED_STRING;
202 tp->Strings[i] = tp->str_table + offsets[i];
207 if ((n = NUM_EXT_NAMES(tp)) != 0) {
209 for (i = 0; i < n; i++) {
210 length += strlen(tp->ext_Names[i]) + 1;
211 offsets[i] = tp->ext_Names[i] - stringbuf;
213 if ((tp->ext_str_table = typeMalloc(char, length)) == 0)
214 _nc_err_abort(MSG_NO_MEMORY);
215 for (i = 0, length = 0; i < n; i++) {
216 tp->ext_Names[i] = tp->ext_str_table + length;
217 strcpy(tp->ext_Names[i], stringbuf + offsets[i]);
218 length += strlen(tp->ext_Names[i]) + 1;
224 for (i = 0; i < nuses; i++) {
225 if (useoffsets[i] == ABSENT_OFFSET)
226 ep->uses[i].name = 0;
228 ep->uses[i].name = (tp->str_table + useoffsets[i]);
233 _nc_merge_entry(TERMTYPE *const to, TERMTYPE *const from)
234 /* merge capabilities from `from' entry into `to' entry */
239 _nc_align_termtype(to, from);
241 for_each_boolean(i, from) {
242 if (to->Booleans[i] != (char) CANCELLED_BOOLEAN) {
243 int mergebool = from->Booleans[i];
245 if (mergebool == CANCELLED_BOOLEAN)
246 to->Booleans[i] = FALSE;
247 else if (mergebool == TRUE)
248 to->Booleans[i] = mergebool;
252 for_each_number(i, from) {
253 if (to->Numbers[i] != CANCELLED_NUMERIC) {
254 int mergenum = from->Numbers[i];
256 if (mergenum == CANCELLED_NUMERIC)
257 to->Numbers[i] = ABSENT_NUMERIC;
258 else if (mergenum != ABSENT_NUMERIC)
259 to->Numbers[i] = mergenum;
264 * Note: the copies of strings this makes don't have their own
265 * storage. This is OK right now, but will be a problem if we
266 * we ever want to deallocate entries.
268 for_each_string(i, from) {
269 if (to->Strings[i] != CANCELLED_STRING) {
270 char *mergestring = from->Strings[i];
272 if (mergestring == CANCELLED_STRING)
273 to->Strings[i] = ABSENT_STRING;
274 else if (mergestring != ABSENT_STRING)
275 to->Strings[i] = mergestring;
282 _nc_alloc_entry_leaks(void)
284 if (stringbuf != 0) {
285 FreeAndNull(stringbuf);