1 /****************************************************************************
2 * Copyright (c) 1998-2001,2002 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 ****************************************************************************/
35 * alloc_entry.c -- allocation functions for terminfo entries
45 #include <curses.priv.h>
48 #include <term_entry.h>
50 MODULE_ID("$Id: alloc_entry.c,v 1.37 2002/09/07 20:04:15 tom Exp $")
52 #define ABSENT_OFFSET -1
53 #define CANCELLED_OFFSET -2
55 #define MAX_STRTAB 4096 /* documented maximum entry size */
57 static char *stringbuf; /* buffer for string capabilities */
58 static size_t next_free; /* next free character in stringbuf */
61 _nc_init_entry(TERMTYPE * const tp)
62 /* initialize a terminal type data block */
67 stringbuf = malloc(MAX_STRTAB);
70 tp->num_Booleans = BOOLCOUNT;
71 tp->num_Numbers = NUMCOUNT;
72 tp->num_Strings = STRCOUNT;
77 if (tp->Booleans == 0)
78 tp->Booleans = typeMalloc(char, BOOLCOUNT);
80 tp->Numbers = typeMalloc(short, NUMCOUNT);
82 tp->Strings = typeMalloc(char *, STRCOUNT);
84 for_each_boolean(i, tp)
85 tp->Booleans[i] = FALSE;
87 for_each_number(i, tp)
88 tp->Numbers[i] = ABSENT_NUMERIC;
90 for_each_string(i, tp)
91 tp->Strings[i] = ABSENT_STRING;
96 NCURSES_EXPORT(ENTRY *)
97 _nc_copy_entry(ENTRY * oldp)
99 ENTRY *newp = typeCalloc(ENTRY, 1);
103 _nc_copy_termtype(&(newp->tterm), &(oldp->tterm));
108 NCURSES_EXPORT(char *)
109 _nc_save_str(const char *const string)
110 /* save a copy of string in the string buffer */
112 size_t old_next_free = next_free;
113 size_t len = strlen(string) + 1;
115 if (next_free + len < MAX_STRTAB) {
116 strcpy(&stringbuf[next_free], string);
117 DEBUG(7, ("Saved string %s", _nc_visbuf(string)));
118 DEBUG(7, ("at location %d", (int) next_free));
121 return (stringbuf + old_next_free);
125 _nc_wrap_entry(ENTRY * const ep, bool copy_strings)
126 /* copy the string parts to allocated storage, preserving pointers to it */
128 int offsets[MAX_ENTRY_SIZE / 2], useoffsets[MAX_USES];
130 TERMTYPE *tp = &(ep->tterm);
133 next_free = 0; /* clear static storage */
135 /* copy term_names, Strings, uses */
136 tp->term_names = _nc_save_str(tp->term_names);
137 for_each_string(i, tp) {
138 if (tp->Strings[i] != ABSENT_STRING &&
139 tp->Strings[i] != CANCELLED_STRING) {
140 tp->Strings[i] = _nc_save_str(tp->Strings[i]);
144 for (i = 0; i < ep->nuses; i++) {
145 if (ep->uses[i].name == 0) {
146 ep->uses[i].name = _nc_save_str(ep->uses[i].name);
153 n = tp->term_names - stringbuf;
154 for_each_string(i, &(ep->tterm)) {
155 if (tp->Strings[i] == ABSENT_STRING)
156 offsets[i] = ABSENT_OFFSET;
157 else if (tp->Strings[i] == CANCELLED_STRING)
158 offsets[i] = CANCELLED_OFFSET;
160 offsets[i] = tp->Strings[i] - stringbuf;
163 for (i = 0; i < ep->nuses; i++) {
164 if (ep->uses[i].name == 0)
165 useoffsets[i] = ABSENT_OFFSET;
167 useoffsets[i] = ep->uses[i].name - stringbuf;
170 if ((tp->str_table = typeMalloc(char, next_free)) == (char *) 0)
171 _nc_err_abort(MSG_NO_MEMORY);
172 (void) memcpy(tp->str_table, stringbuf, next_free);
174 tp->term_names = tp->str_table + n;
175 for_each_string(i, &(ep->tterm)) {
176 if (offsets[i] == ABSENT_OFFSET)
177 tp->Strings[i] = ABSENT_STRING;
178 else if (offsets[i] == CANCELLED_OFFSET)
179 tp->Strings[i] = CANCELLED_STRING;
181 tp->Strings[i] = tp->str_table + offsets[i];
186 if ((n = NUM_EXT_NAMES(tp)) != 0) {
188 for (i = 0; i < n; i++) {
189 length += strlen(tp->ext_Names[i]) + 1;
190 offsets[i] = tp->ext_Names[i] - stringbuf;
192 if ((tp->ext_str_table = typeMalloc(char, length)) == 0)
193 _nc_err_abort(MSG_NO_MEMORY);
194 for (i = 0, length = 0; i < n; i++) {
195 tp->ext_Names[i] = tp->ext_str_table + length;
196 strcpy(tp->ext_Names[i], stringbuf + offsets[i]);
197 length += strlen(tp->ext_Names[i]) + 1;
203 for (i = 0; i < ep->nuses; i++) {
204 if (useoffsets[i] == ABSENT_OFFSET)
205 ep->uses[i].name = 0;
207 ep->uses[i].name = (tp->str_table + useoffsets[i]);
213 (TERMTYPE * const to, TERMTYPE * const from)
214 /* merge capabilities from `from' entry into `to' entry */
219 _nc_align_termtype(to, from);
221 for_each_boolean(i, from) {
222 int mergebool = from->Booleans[i];
224 if (mergebool == CANCELLED_BOOLEAN)
225 to->Booleans[i] = FALSE;
226 else if (mergebool == TRUE)
227 to->Booleans[i] = mergebool;
230 for_each_number(i, from) {
231 int mergenum = from->Numbers[i];
233 if (mergenum == CANCELLED_NUMERIC)
234 to->Numbers[i] = ABSENT_NUMERIC;
235 else if (mergenum != ABSENT_NUMERIC)
236 to->Numbers[i] = mergenum;
240 * Note: the copies of strings this makes don't have their own
241 * storage. This is OK right now, but will be a problem if we
242 * we ever want to deallocate entries.
244 for_each_string(i, from) {
245 char *mergestring = from->Strings[i];
247 if (mergestring == CANCELLED_STRING)
248 to->Strings[i] = ABSENT_STRING;
249 else if (mergestring != ABSENT_STRING)
250 to->Strings[i] = mergestring;