]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/alloc_entry.c
ncurses 6.4 - patch 20230909
[ncurses.git] / ncurses / tinfo / alloc_entry.c
1 /****************************************************************************
2  * Copyright 2018-2022,2023 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.78 2023/09/09 16:06:00 Nicholas.Marriott 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)"), (void *) 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)"), (void *) 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"), (void *) 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
110     if (stringbuf != NULL) {
111         size_t len;
112
113         if (!VALID_STRING(string))
114             string = "";
115         len = strlen(string) + 1;
116
117         if (len == 1 && next_free != 0) {
118             /*
119              * Cheat a little by making an empty string point to the end of the
120              * previous string.
121              */
122             if (next_free < MAX_ENTRY_SIZE) {
123                 result = (stringbuf + next_free - 1);
124             }
125         } else if (next_free + len < MAX_ENTRY_SIZE) {
126             _nc_STRCPY(&stringbuf[next_free], string, MAX_ENTRY_SIZE);
127             DEBUG(7, ("Saved string %s", _nc_visbuf(string)));
128             DEBUG(7, ("at location %d", (int) next_free));
129             next_free += len;
130             result = (stringbuf + old_next_free);
131         } else {
132             _nc_warning("Too much data, some is lost: %s", string);
133         }
134     }
135     return result;
136 }
137
138 NCURSES_EXPORT(void)
139 _nc_wrap_entry(ENTRY * const ep, bool copy_strings)
140 /* copy the string parts to allocated storage, preserving pointers to it */
141 {
142     int offsets[MAX_ENTRY_SIZE / sizeof(short)];
143     int useoffsets[MAX_USES];
144     unsigned i, n;
145     unsigned nuses;
146     TERMTYPE2 *tp;
147
148     DEBUG(2, (T_CALLED("_nc_wrap_entry(ep=%p, copy_strings=%d)"), (void *)
149               ep, copy_strings));
150     if (ep == NULL || stringbuf == NULL)
151         _nc_err_abort("_nc_wrap_entry called without initialization");
152
153     nuses = ep->nuses;
154     tp = &(ep->tterm);
155     if (copy_strings) {
156         next_free = 0;          /* clear static storage */
157
158         /* copy term_names, Strings, uses */
159         tp->term_names = _nc_save_str(tp->term_names);
160         for_each_string(i, tp) {
161             if (tp->Strings[i] != ABSENT_STRING &&
162                 tp->Strings[i] != CANCELLED_STRING) {
163                 tp->Strings[i] = _nc_save_str(tp->Strings[i]);
164             }
165         }
166
167         for (i = 0; i < nuses; i++) {
168             if (ep->uses[i].name == 0) {
169                 ep->uses[i].name = _nc_save_str(ep->uses[i].name);
170             }
171         }
172
173         free(tp->str_table);
174     }
175
176     assert(tp->term_names >= stringbuf);
177     n = (unsigned) (tp->term_names - stringbuf);
178     for_each_string(i, &(ep->tterm)) {
179         if (i < SIZEOF(offsets)) {
180             if (tp->Strings[i] == ABSENT_STRING) {
181                 offsets[i] = ABSENT_OFFSET;
182             } else if (tp->Strings[i] == CANCELLED_STRING) {
183                 offsets[i] = CANCELLED_OFFSET;
184             } else {
185                 offsets[i] = (int) (tp->Strings[i] - stringbuf);
186             }
187         }
188     }
189
190     for (i = 0; i < nuses; i++) {
191         if (ep->uses[i].name == 0)
192             useoffsets[i] = ABSENT_OFFSET;
193         else
194             useoffsets[i] = (int) (ep->uses[i].name - stringbuf);
195     }
196
197     TYPE_MALLOC(char, next_free, tp->str_table);
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                 size_t length = 0;
218                 size_t offset;
219                 for (i = 0; i < n; i++) {
220                     length += strlen(tp->ext_Names[i]) + 1;
221                     offsets[i] = (int) (tp->ext_Names[i] - stringbuf);
222                 }
223                 TYPE_MALLOC(char, length, tp->ext_str_table);
224                 for (i = 0, offset = 0; i < n; i++) {
225                     tp->ext_Names[i] = tp->ext_str_table + offset;
226                     _nc_STRCPY(tp->ext_Names[i],
227                                stringbuf + offsets[i],
228                                length - offset);
229                     offset += strlen(tp->ext_Names[i]) + 1;
230                 }
231             }
232         }
233     }
234 #endif
235
236     for (i = 0; i < nuses; i++) {
237         if (useoffsets[i] == ABSENT_OFFSET) {
238             ep->uses[i].name = 0;
239         } else {
240             ep->uses[i].name = strdup(tp->str_table + useoffsets[i]);
241         }
242     }
243     DEBUG(2, (T_RETURN("")));
244 }
245
246 NCURSES_EXPORT(void)
247 _nc_merge_entry(ENTRY * const target, ENTRY * const source)
248 /* merge capabilities from `from' entry into `to' entry */
249 {
250     TERMTYPE2 *to = &(target->tterm);
251     TERMTYPE2 *from = &(source->tterm);
252 #if NCURSES_XNAMES
253     TERMTYPE2 copy;
254     size_t str_size, copy_size;
255     char *str_table;
256 #endif
257     unsigned i;
258
259     if (source == 0 || from == 0 || target == 0 || to == 0)
260         return;
261
262 #if NCURSES_XNAMES
263     _nc_copy_termtype2(&copy, from);
264     from = &copy;
265     _nc_align_termtype(to, from);
266     /*
267      * compute the maximum size of the string-table.
268      */
269     str_size = strlen(to->term_names) + 1;
270     for_each_string(i, from) {
271         if (VALID_STRING(from->Strings[i]))
272             str_size += strlen(from->Strings[i]) + 1;
273     }
274     for_each_string(i, to) {
275         if (VALID_STRING(to->Strings[i]))
276             str_size += strlen(to->Strings[i]) + 1;
277     }
278     /* allocate a string-table large enough for both source/target, and
279      * copy all of the strings into that table.  In the merge, we will
280      * select from the original source/target lists to construct a new
281      * target list.
282      */
283     if (str_size != 0) {
284         char *str_copied;
285         if ((str_table = malloc(str_size)) == NULL)
286             _nc_err_abort(MSG_NO_MEMORY);
287         str_copied = str_table;
288         _nc_STRCPY(str_copied, to->term_names, str_size);
289         to->term_names = str_copied;
290         copy_size = strlen(str_copied) + 1;
291         str_copied += copy_size;
292         str_size -= copy_size;
293         for_each_string(i, from) {
294             if (VALID_STRING(from->Strings[i])) {
295                 _nc_STRCPY(str_copied, from->Strings[i], str_size);
296                 from->Strings[i] = str_copied;
297                 copy_size = strlen(str_copied) + 1;
298                 str_copied += copy_size;
299                 str_size -= copy_size;
300             }
301         }
302         for_each_string(i, to) {
303             if (VALID_STRING(to->Strings[i])) {
304                 _nc_STRCPY(str_copied, to->Strings[i], str_size);
305                 to->Strings[i] = str_copied;
306                 copy_size = strlen(str_copied) + 1;
307                 str_copied += copy_size;
308                 str_size -= copy_size;
309             }
310         }
311         free(to->str_table);
312         to->str_table = str_table;
313         free(from->str_table);
314     }
315     /*
316      * Do the same for the extended-strings (i.e., lists of capabilities).
317      */
318     str_size = 0;
319     for (i = 0; i < NUM_EXT_NAMES(from); ++i) {
320         if (VALID_STRING(from->ext_Names[i]))
321             str_size += strlen(from->ext_Names[i]) + 1;
322     }
323     for (i = 0; i < NUM_EXT_NAMES(to); ++i) {
324         if (VALID_STRING(to->ext_Names[i]))
325             str_size += strlen(to->ext_Names[i]) + 1;
326     }
327     /* allocate a string-table large enough for both source/target, and
328      * copy all of the strings into that table.  In the merge, we will
329      * select from the original source/target lists to construct a new
330      * target list.
331      */
332     if (str_size != 0) {
333         char *str_copied;
334         if ((str_table = malloc(str_size)) == NULL)
335             _nc_err_abort(MSG_NO_MEMORY);
336         str_copied = str_table;
337         for (i = 0; i < NUM_EXT_NAMES(from); ++i) {
338             if (VALID_STRING(from->ext_Names[i])) {
339                 _nc_STRCPY(str_copied, from->ext_Names[i], str_size);
340                 from->ext_Names[i] = str_copied;
341                 copy_size = strlen(str_copied) + 1;
342                 str_copied += copy_size;
343                 str_size -= copy_size;
344             }
345         }
346         for (i = 0; i < NUM_EXT_NAMES(to); ++i) {
347             if (VALID_STRING(to->ext_Names[i])) {
348                 _nc_STRCPY(str_copied, to->ext_Names[i], str_size);
349                 to->ext_Names[i] = str_copied;
350                 copy_size = strlen(str_copied) + 1;
351                 str_copied += copy_size;
352                 str_size -= copy_size;
353             }
354         }
355         free(to->ext_str_table);
356         to->ext_str_table = str_table;
357         free(from->ext_str_table);
358     }
359 #endif
360     for_each_boolean(i, from) {
361         if (to->Booleans[i] != (NCURSES_SBOOL) CANCELLED_BOOLEAN) {
362             int mergebool = from->Booleans[i];
363
364             if (mergebool == CANCELLED_BOOLEAN)
365                 to->Booleans[i] = FALSE;
366             else if (mergebool == TRUE)
367                 to->Booleans[i] = (NCURSES_SBOOL) mergebool;
368         }
369     }
370
371     for_each_number(i, from) {
372         if (to->Numbers[i] != CANCELLED_NUMERIC) {
373             int mergenum = from->Numbers[i];
374
375             if (mergenum == CANCELLED_NUMERIC)
376                 to->Numbers[i] = ABSENT_NUMERIC;
377             else if (mergenum != ABSENT_NUMERIC)
378                 to->Numbers[i] = (NCURSES_INT2) mergenum;
379         }
380     }
381
382     /*
383      * Note: the copies of strings this makes don't have their own
384      * storage.  This is OK right now, but will be a problem if we
385      * we ever want to deallocate entries.
386      */
387     for_each_string(i, from) {
388         if (to->Strings[i] != CANCELLED_STRING) {
389             char *mergestring = from->Strings[i];
390
391             if (mergestring == CANCELLED_STRING)
392                 to->Strings[i] = ABSENT_STRING;
393             else if (mergestring != ABSENT_STRING)
394                 to->Strings[i] = mergestring;
395         }
396     }
397 #if NCURSES_XNAMES
398     /* cleanup */
399     free(copy.Booleans);
400     free(copy.Numbers);
401     free(copy.Strings);
402     free(copy.ext_Names);
403 #endif
404 }
405
406 #if NO_LEAKS
407 NCURSES_EXPORT(void)
408 _nc_alloc_entry_leaks(void)
409 {
410     if (stringbuf != NULL) {
411         FreeAndNull(stringbuf);
412     }
413     next_free = 0;
414 }
415 #endif