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