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