1 /****************************************************************************
2 * Copyright (c) 1998 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 ****************************************************************************/
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.30 1999/03/01 02:03:45 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[MAX_STRTAB]; /* buffer for string capabilities */
59 static size_t next_free; /* next free character in stringbuf */
61 void _nc_init_entry(TERMTYPE *const tp)
62 /* initialize a terminal type data block */
67 tp->num_Booleans = BOOLCOUNT;
68 tp->num_Numbers = NUMCOUNT;
69 tp->num_Strings = STRCOUNT;
74 if (tp->Booleans == 0)
75 tp->Booleans = typeMalloc(char,BOOLCOUNT);
77 tp->Numbers = typeMalloc(short,NUMCOUNT);
79 tp->Strings = typeMalloc(char *,STRCOUNT);
81 for_each_boolean(i,tp)
82 tp->Booleans[i] = FALSE;
85 tp->Numbers[i] = ABSENT_NUMERIC;
88 tp->Strings[i] = ABSENT_STRING;
93 ENTRY *_nc_copy_entry(ENTRY *oldp)
95 ENTRY *newp = typeCalloc(ENTRY,1);
99 _nc_copy_termtype(&(newp->tterm), &(oldp->tterm));
104 char *_nc_save_str(const char *const string)
105 /* save a copy of string in the string buffer */
107 size_t old_next_free = next_free;
108 size_t len = strlen(string) + 1;
110 if (next_free + len < MAX_STRTAB)
112 strcpy(&stringbuf[next_free], string);
113 DEBUG(7, ("Saved string %s", _nc_visbuf(string)));
114 DEBUG(7, ("at location %d", (int) next_free));
117 return(stringbuf + old_next_free);
120 void _nc_wrap_entry(ENTRY *const ep)
121 /* copy the string parts to allocated storage, preserving pointers to it */
123 int offsets[MAX_ENTRY_SIZE/2], useoffsets[MAX_USES];
125 TERMTYPE *tp = &(ep->tterm);
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;
134 offsets[i] = tp->Strings[i] - stringbuf;
137 for (i=0; i < ep->nuses; i++) {
138 if (ep->uses[i].parent == (void *)0)
139 useoffsets[i] = ABSENT_OFFSET;
141 useoffsets[i] = (char *)(ep->uses[i].parent) - stringbuf;
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);
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;
155 tp->Strings[i] = tp->str_table + offsets[i];
159 if ((n = NUM_EXT_NAMES(tp)) != 0) {
161 for (i = 0; i < n; i++) {
162 length += strlen(tp->ext_Names[i]) + 1;
163 offsets[i] = tp->ext_Names[i] - stringbuf;
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;
175 for (i=0; i < ep->nuses; i++) {
176 if (useoffsets[i] == ABSENT_OFFSET)
177 ep->uses[i].parent = (void *)0;
179 ep->uses[i].parent = (char *)(tp->str_table + useoffsets[i]);
183 void _nc_merge_entry(TERMTYPE *const to, TERMTYPE *const from)
184 /* merge capabilities from `from' entry into `to' entry */
189 _nc_align_termtype(to, from);
191 for_each_boolean(i, from)
193 int mergebool = from->Booleans[i];
195 if (mergebool == CANCELLED_BOOLEAN)
196 to->Booleans[i] = FALSE;
197 else if (mergebool == TRUE)
198 to->Booleans[i] = mergebool;
201 for_each_number(i, from)
203 int mergenum = from->Numbers[i];
205 if (mergenum == CANCELLED_NUMERIC)
206 to->Numbers[i] = ABSENT_NUMERIC;
207 else if (mergenum != ABSENT_NUMERIC)
208 to->Numbers[i] = mergenum;
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.
216 for_each_string(i, from)
218 char *mergestring = from->Strings[i];
220 if (mergestring == CANCELLED_STRING)
221 to->Strings[i] = ABSENT_STRING;
222 else if (mergestring != ABSENT_STRING)
223 to->Strings[i] = mergestring;