1/****************************************************************************
2 * Copyright (c) 1998-2006,2008 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 *     and: Thomas E. Dickey                        1996-on                 *
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
51MODULE_ID("$Id: alloc_entry.c,v 1.48 2008/08/16 16:25:31 tom Exp $")
52
53#define ABSENT_OFFSET    -1
54#define CANCELLED_OFFSET -2
55
56#define MAX_STRTAB	4096	/* documented maximum entry size */
57
58static char *stringbuf;		/* buffer for string capabilities */
59static size_t next_free;	/* next free character in stringbuf */
60
61NCURSES_EXPORT(void)
62_nc_init_entry(TERMTYPE *const tp)
63/* initialize a terminal type data block */
64{
65    unsigned i;
66
67#if NO_LEAKS
68    if (tp == 0 && stringbuf != 0) {
69	FreeAndNull(stringbuf);
70	return;
71    }
72#endif
73
74    if (stringbuf == 0)
75	stringbuf = (char *) malloc(MAX_STRTAB);
76
77#if NCURSES_XNAMES
78    tp->num_Booleans = BOOLCOUNT;
79    tp->num_Numbers = NUMCOUNT;
80    tp->num_Strings = STRCOUNT;
81    tp->ext_Booleans = 0;
82    tp->ext_Numbers = 0;
83    tp->ext_Strings = 0;
84#endif
85    if (tp->Booleans == 0)
86	tp->Booleans = typeMalloc(NCURSES_SBOOL, BOOLCOUNT);
87    if (tp->Numbers == 0)
88	tp->Numbers = typeMalloc(short, NUMCOUNT);
89    if (tp->Strings == 0)
90	tp->Strings = typeMalloc(char *, STRCOUNT);
91
92    for_each_boolean(i, tp)
93	tp->Booleans[i] = FALSE;
94
95    for_each_number(i, tp)
96	tp->Numbers[i] = ABSENT_NUMERIC;
97
98    for_each_string(i, tp)
99	tp->Strings[i] = ABSENT_STRING;
100
101    next_free = 0;
102}
103
104NCURSES_EXPORT(ENTRY *)
105_nc_copy_entry(ENTRY * oldp)
106{
107    ENTRY *newp = typeCalloc(ENTRY, 1);
108
109    if (newp != 0) {
110	*newp = *oldp;
111	_nc_copy_termtype(&(newp->tterm), &(oldp->tterm));
112    }
113    return newp;
114}
115
116/* save a copy of string in the string buffer */
117NCURSES_EXPORT(char *)
118_nc_save_str(const char *const string)
119{
120    char *result = 0;
121    size_t old_next_free = next_free;
122    size_t len = strlen(string) + 1;
123
124    if (len == 1 && next_free != 0) {
125	/*
126	 * Cheat a little by making an empty string point to the end of the
127	 * previous string.
128	 */
129	if (next_free < MAX_STRTAB) {
130	    result = (stringbuf + next_free - 1);
131	}
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));
136	next_free += len;
137	result = (stringbuf + old_next_free);
138    } else {
139	_nc_warning("Too much data, some is lost");
140    }
141    return result;
142}
143
144NCURSES_EXPORT(void)
145_nc_wrap_entry(ENTRY * const ep, bool copy_strings)
146/* copy the string parts to allocated storage, preserving pointers to it */
147{
148    int offsets[MAX_ENTRY_SIZE / sizeof(short)];
149    int useoffsets[MAX_USES];
150    unsigned i, n;
151    unsigned nuses = ep->nuses;
152    TERMTYPE *tp = &(ep->tterm);
153
154    if (copy_strings) {
155	next_free = 0;		/* clear static storage */
156
157	/* copy term_names, Strings, uses */
158	tp->term_names = _nc_save_str(tp->term_names);
159	for_each_string(i, tp) {
160	    if (tp->Strings[i] != ABSENT_STRING &&
161		tp->Strings[i] != CANCELLED_STRING) {
162		tp->Strings[i] = _nc_save_str(tp->Strings[i]);
163	    }
164	}
165
166	for (i = 0; i < nuses; i++) {
167	    if (ep->uses[i].name == 0) {
168		ep->uses[i].name = _nc_save_str(ep->uses[i].name);
169	    }
170	}
171
172	free(tp->str_table);
173    }
174
175    assert(tp->term_names >= stringbuf);
176    n = (unsigned) (tp->term_names - stringbuf);
177    for_each_string(i, &(ep->tterm)) {
178	if (i < SIZEOF(offsets)) {
179	    if (tp->Strings[i] == ABSENT_STRING) {
180		offsets[i] = ABSENT_OFFSET;
181	    } else if (tp->Strings[i] == CANCELLED_STRING) {
182		offsets[i] = CANCELLED_OFFSET;
183	    } else {
184		offsets[i] = tp->Strings[i] - stringbuf;
185	    }
186	}
187    }
188
189    for (i = 0; i < nuses; i++) {
190	if (ep->uses[i].name == 0)
191	    useoffsets[i] = ABSENT_OFFSET;
192	else
193	    useoffsets[i] = ep->uses[i].name - stringbuf;
194    }
195
196    if ((tp->str_table = typeMalloc(char, next_free)) == (char *) 0)
197	  _nc_err_abort(MSG_NO_MEMORY);
198    (void) memcpy(tp->str_table, stringbuf, next_free);
199
200    tp->term_names = tp->str_table + n;
201    for_each_string(i, &(ep->tterm)) {
202	if (i < SIZEOF(offsets)) {
203	    if (offsets[i] == ABSENT_OFFSET) {
204		tp->Strings[i] = ABSENT_STRING;
205	    } else if (offsets[i] == CANCELLED_OFFSET) {
206		tp->Strings[i] = CANCELLED_STRING;
207	    } else {
208		tp->Strings[i] = tp->str_table + offsets[i];
209	    }
210	}
211    }
212
213#if NCURSES_XNAMES
214    if (!copy_strings) {
215	if ((n = (unsigned) NUM_EXT_NAMES(tp)) != 0) {
216	    if (n < SIZEOF(offsets)) {
217		unsigned length = 0;
218		for (i = 0; i < n; i++) {
219		    length += strlen(tp->ext_Names[i]) + 1;
220		    offsets[i] = tp->ext_Names[i] - stringbuf;
221		}
222		if ((tp->ext_str_table = typeMalloc(char, length)) == 0)
223		      _nc_err_abort(MSG_NO_MEMORY);
224		for (i = 0, length = 0; i < n; i++) {
225		    tp->ext_Names[i] = tp->ext_str_table + length;
226		    strcpy(tp->ext_Names[i], stringbuf + offsets[i]);
227		    length += strlen(tp->ext_Names[i]) + 1;
228		}
229	    }
230	}
231    }
232#endif
233
234    for (i = 0; i < nuses; i++) {
235	if (useoffsets[i] == ABSENT_OFFSET)
236	    ep->uses[i].name = 0;
237	else
238	    ep->uses[i].name = (tp->str_table + useoffsets[i]);
239    }
240}
241
242NCURSES_EXPORT(void)
243_nc_merge_entry(TERMTYPE *const to, TERMTYPE *const from)
244/* merge capabilities from `from' entry into `to' entry */
245{
246    unsigned i;
247
248#if NCURSES_XNAMES
249    _nc_align_termtype(to, from);
250#endif
251    for_each_boolean(i, from) {
252	if (to->Booleans[i] != (char) CANCELLED_BOOLEAN) {
253	    int mergebool = from->Booleans[i];
254
255	    if (mergebool == CANCELLED_BOOLEAN)
256		to->Booleans[i] = FALSE;
257	    else if (mergebool == TRUE)
258		to->Booleans[i] = (char) mergebool;
259	}
260    }
261
262    for_each_number(i, from) {
263	if (to->Numbers[i] != CANCELLED_NUMERIC) {
264	    short mergenum = from->Numbers[i];
265
266	    if (mergenum == CANCELLED_NUMERIC)
267		to->Numbers[i] = ABSENT_NUMERIC;
268	    else if (mergenum != ABSENT_NUMERIC)
269		to->Numbers[i] = mergenum;
270	}
271    }
272
273    /*
274     * Note: the copies of strings this makes don't have their own
275     * storage.  This is OK right now, but will be a problem if we
276     * we ever want to deallocate entries.
277     */
278    for_each_string(i, from) {
279	if (to->Strings[i] != CANCELLED_STRING) {
280	    char *mergestring = from->Strings[i];
281
282	    if (mergestring == CANCELLED_STRING)
283		to->Strings[i] = ABSENT_STRING;
284	    else if (mergestring != ABSENT_STRING)
285		to->Strings[i] = mergestring;
286	}
287    }
288}
289
290#if NO_LEAKS
291NCURSES_EXPORT(void)
292_nc_alloc_entry_leaks(void)
293{
294    if (stringbuf != 0) {
295	FreeAndNull(stringbuf);
296    }
297    next_free = 0;
298}
299#endif
300