xref: /openbsd-src/lib/libcurses/tinfo/alloc_entry.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: alloc_entry.c,v 1.4 2001/01/22 18:01:50 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 = strlen(string) + 1;
113 
114     if (next_free + len < MAX_STRTAB) {
115 	strcpy(&stringbuf[next_free], string);
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 	    unsigned length = 0;
187 	    for (i = 0; i < n; i++) {
188 		length += strlen(tp->ext_Names[i]) + 1;
189 		offsets[i] = tp->ext_Names[i] - stringbuf;
190 	    }
191 	    if ((tp->ext_str_table = typeMalloc(char, length)) == 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 		strcpy(tp->ext_Names[i], stringbuf + offsets[i]);
196 		length += strlen(tp->ext_Names[i]) + 1;
197 	    }
198 	}
199     }
200 #endif
201 
202     for (i = 0; i < ep->nuses; i++) {
203 	if (useoffsets[i] == ABSENT_OFFSET)
204 	    ep->uses[i].name = 0;
205 	else
206 	    ep->uses[i].name = (tp->str_table + useoffsets[i]);
207     }
208 }
209 
210 NCURSES_EXPORT(void)
211 _nc_merge_entry
212 (TERMTYPE * const to, TERMTYPE * const from)
213 /* merge capabilities from `from' entry into `to' entry */
214 {
215     int i;
216 
217 #if NCURSES_XNAMES
218     _nc_align_termtype(to, from);
219 #endif
220     for_each_boolean(i, from) {
221 	int mergebool = from->Booleans[i];
222 
223 	if (mergebool == CANCELLED_BOOLEAN)
224 	    to->Booleans[i] = FALSE;
225 	else if (mergebool == TRUE)
226 	    to->Booleans[i] = mergebool;
227     }
228 
229     for_each_number(i, from) {
230 	int mergenum = from->Numbers[i];
231 
232 	if (mergenum == CANCELLED_NUMERIC)
233 	    to->Numbers[i] = ABSENT_NUMERIC;
234 	else if (mergenum != ABSENT_NUMERIC)
235 	    to->Numbers[i] = mergenum;
236     }
237 
238     /*
239      * Note: the copies of strings this makes don't have their own
240      * storage.  This is OK right now, but will be a problem if we
241      * we ever want to deallocate entries.
242      */
243     for_each_string(i, from) {
244 	char *mergestring = from->Strings[i];
245 
246 	if (mergestring == CANCELLED_STRING)
247 	    to->Strings[i] = ABSENT_STRING;
248 	else if (mergestring != ABSENT_STRING)
249 	    to->Strings[i] = mergestring;
250     }
251 }
252