]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/alloc_entry.c
ncurses 6.3 - patch 20220430
[ncurses.git] / ncurses / tinfo / alloc_entry.c
1 /****************************************************************************
2  * Copyright 2018-2021,2022 Thomas E. Dickey                                *
3  * Copyright 1998-2013,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /****************************************************************************
31  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  *     and: Thomas E. Dickey                        1996-on                 *
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
51 MODULE_ID("$Id: alloc_entry.c,v 1.71 2022/04/30 18:36:01 tom Exp $")
52
53 #define ABSENT_OFFSET    -1
54 #define CANCELLED_OFFSET -2
55
56 static char *stringbuf;         /* buffer for string capabilities */
57 static size_t next_free;        /* next free character in stringbuf */
58
59 NCURSES_EXPORT(void)
60 _nc_init_entry(ENTRY * const tp)
61 /* initialize a terminal type data block */
62 {
63     DEBUG(2, (T_CALLED("_nc_init_entry(tp=%p)"), tp));
64
65     if (tp == NULL) {
66 #if NO_LEAKS
67         if (stringbuf != NULL) {
68             FreeAndNull(stringbuf);
69         }
70         return;
71 #else
72         _nc_err_abort("_nc_init_entry called without initialization");
73 #endif
74     }
75
76     if (stringbuf == NULL)
77         TYPE_CALLOC(char, (size_t) MAX_ENTRY_SIZE, stringbuf);
78
79     next_free = 0;
80
81     _nc_init_termtype(&(tp->tterm));
82
83     DEBUG(2, (T_RETURN("")));
84 }
85
86 NCURSES_EXPORT(ENTRY *)
87 _nc_copy_entry(ENTRY * oldp)
88 {
89     ENTRY *newp;
90
91     DEBUG(2, (T_CALLED("_nc_copy_entry(oldp=%p)"), oldp));
92
93     newp = typeCalloc(ENTRY, 1);
94     if (newp != NULL) {
95         *newp = *oldp;
96         _nc_copy_termtype2(&(newp->tterm), &(oldp->tterm));
97     }
98
99     DEBUG(2, (T_RETURN("%p"), newp));
100     return (newp);
101 }
102
103 /* save a copy of string in the string buffer */
104 NCURSES_EXPORT(char *)
105 _nc_save_str(const char *string)
106 {
107     char *result = 0;
108     size_t old_next_free = next_free;
109     size_t len;
110
111     if (stringbuf != NULL) {
112         if (!VALID_STRING(string))
113             string = "";
114         len = strlen(string) + 1;
115
116         if (len == 1 && next_free != 0) {
117             /*
118              * Cheat a little by making an empty string point to the end of the
119              * previous string.
120              */
121             if (next_free < MAX_ENTRY_SIZE) {
122                 result = (stringbuf + next_free - 1);
123             }
124         } else if (next_free + len < MAX_ENTRY_SIZE) {
125             _nc_STRCPY(&stringbuf[next_free], string, MAX_ENTRY_SIZE);
126             DEBUG(7, ("Saved string %s", _nc_visbuf(string)));
127             DEBUG(7, ("at location %d", (int) next_free));
128             next_free += len;
129             result = (stringbuf + old_next_free);
130         } else {
131             _nc_warning("Too much data, some is lost: %s", string);
132         }
133     }
134     return result;
135 }
136
137 NCURSES_EXPORT(void)
138 _nc_wrap_entry(ENTRY * const ep, bool copy_strings)
139 /* copy the string parts to allocated storage, preserving pointers to it */
140 {
141     int offsets[MAX_ENTRY_SIZE / sizeof(short)];
142     int useoffsets[MAX_USES];
143     unsigned i, n;
144     unsigned nuses;
145     TERMTYPE2 *tp;
146
147     if (ep == NULL || stringbuf == NULL)
148         _nc_err_abort("_nc_wrap_entry called without initialization");
149
150     nuses = ep->nuses;
151     tp = &(ep->tterm);
152     if (copy_strings) {
153         next_free = 0;          /* clear static storage */
154
155         /* copy term_names, Strings, uses */
156         tp->term_names = _nc_save_str(tp->term_names);
157         for_each_string(i, tp) {
158             if (tp->Strings[i] != ABSENT_STRING &&
159                 tp->Strings[i] != CANCELLED_STRING) {
160                 tp->Strings[i] = _nc_save_str(tp->Strings[i]);
161             }
162         }
163
164         for (i = 0; i < nuses; i++) {
165             if (ep->uses[i].name == 0) {
166                 ep->uses[i].name = _nc_save_str(ep->uses[i].name);
167             }
168         }
169
170         free(tp->str_table);
171     }
172
173     assert(tp->term_names >= stringbuf);
174     n = (unsigned) (tp->term_names - stringbuf);
175     for_each_string(i, &(ep->tterm)) {
176         if (i < SIZEOF(offsets)) {
177             if (tp->Strings[i] == ABSENT_STRING) {
178                 offsets[i] = ABSENT_OFFSET;
179             } else if (tp->Strings[i] == CANCELLED_STRING) {
180                 offsets[i] = CANCELLED_OFFSET;
181             } else {
182                 offsets[i] = (int) (tp->Strings[i] - stringbuf);
183             }
184         }
185     }
186
187     for (i = 0; i < nuses; i++) {
188         if (ep->uses[i].name == 0)
189             useoffsets[i] = ABSENT_OFFSET;
190         else
191             useoffsets[i] = (int) (ep->uses[i].name - stringbuf);
192     }
193
194     TYPE_MALLOC(char, next_free, tp->str_table);
195     (void) memcpy(tp->str_table, stringbuf, next_free);
196
197     tp->term_names = tp->str_table + n;
198     for_each_string(i, &(ep->tterm)) {
199         if (i < SIZEOF(offsets)) {
200             if (offsets[i] == ABSENT_OFFSET) {
201                 tp->Strings[i] = ABSENT_STRING;
202             } else if (offsets[i] == CANCELLED_OFFSET) {
203                 tp->Strings[i] = CANCELLED_STRING;
204             } else {
205                 tp->Strings[i] = tp->str_table + offsets[i];
206             }
207         }
208     }
209
210 #if NCURSES_XNAMES
211     if (!copy_strings) {
212         if ((n = (unsigned) NUM_EXT_NAMES(tp)) != 0) {
213             if (n < SIZEOF(offsets)) {
214                 size_t length = 0;
215                 size_t offset;
216                 for (i = 0; i < n; i++) {
217                     length += strlen(tp->ext_Names[i]) + 1;
218                     offsets[i] = (int) (tp->ext_Names[i] - stringbuf);
219                 }
220                 TYPE_MALLOC(char, length, tp->ext_str_table);
221                 for (i = 0, offset = 0; i < n; i++) {
222                     tp->ext_Names[i] = tp->ext_str_table + offset;
223                     _nc_STRCPY(tp->ext_Names[i],
224                                stringbuf + offsets[i],
225                                length - offset);
226                     offset += 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(ENTRY * const target, ENTRY * const source)
243 /* merge capabilities from `from' entry into `to' entry */
244 {
245     TERMTYPE2 *to = &(target->tterm);
246     TERMTYPE2 *from = &(source->tterm);
247 #if NCURSES_XNAMES
248     TERMTYPE2 copy;
249 #endif
250     unsigned i;
251
252     if (source == 0 || from == 0 || target == 0 || to == 0)
253         return;
254
255 #if NCURSES_XNAMES
256     _nc_copy_termtype2(&copy, from);
257     from = &copy;
258     _nc_align_termtype(to, from);
259 #endif
260     for_each_boolean(i, from) {
261         if (to->Booleans[i] != (NCURSES_SBOOL) CANCELLED_BOOLEAN) {
262             int mergebool = from->Booleans[i];
263
264             if (mergebool == CANCELLED_BOOLEAN)
265                 to->Booleans[i] = FALSE;
266             else if (mergebool == TRUE)
267                 to->Booleans[i] = (NCURSES_SBOOL) mergebool;
268         }
269     }
270
271     for_each_number(i, from) {
272         if (to->Numbers[i] != CANCELLED_NUMERIC) {
273             int mergenum = from->Numbers[i];
274
275             if (mergenum == CANCELLED_NUMERIC)
276                 to->Numbers[i] = ABSENT_NUMERIC;
277             else if (mergenum != ABSENT_NUMERIC)
278                 to->Numbers[i] = (NCURSES_INT2) mergenum;
279         }
280     }
281
282     /*
283      * Note: the copies of strings this makes don't have their own
284      * storage.  This is OK right now, but will be a problem if we
285      * we ever want to deallocate entries.
286      */
287     for_each_string(i, from) {
288         if (to->Strings[i] != CANCELLED_STRING) {
289             char *mergestring = from->Strings[i];
290
291             if (mergestring == CANCELLED_STRING)
292                 to->Strings[i] = ABSENT_STRING;
293             else if (mergestring != ABSENT_STRING)
294                 to->Strings[i] = mergestring;
295         }
296     }
297 #if NCURSES_XNAMES
298     /* Discard the data allocated in _nc_copy_termtype2, but do not use
299      * _nc_free_termtype2 because that frees the string-table (which is
300      * not allocated by _nc_copy_termtype2).
301      */
302     free(copy.Booleans);
303     free(copy.Numbers);
304     free(copy.Strings);
305     free(copy.ext_Names);
306 #endif
307 }
308
309 #if NO_LEAKS
310 NCURSES_EXPORT(void)
311 _nc_alloc_entry_leaks(void)
312 {
313     if (stringbuf != NULL) {
314         FreeAndNull(stringbuf);
315     }
316     next_free = 0;
317 }
318 #endif