]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/alloc_entry.c
aed7394362a9f6672b9f824ecf11372501405700
[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.69 2022/04/16 22:46:53 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     if (tp == NULL) {
64 #if NO_LEAKS
65         if (stringbuf != NULL) {
66             FreeAndNull(stringbuf);
67         }
68         return;
69 #else
70         _nc_err_abort("_nc_init_entry called without initialization");
71 #endif
72     }
73
74     if (stringbuf == NULL)
75         TYPE_MALLOC(char, (size_t) MAX_ENTRY_SIZE, stringbuf);
76
77     next_free = 0;
78
79     _nc_init_termtype(&(tp->tterm));
80 }
81
82 NCURSES_EXPORT(ENTRY *)
83 _nc_copy_entry(ENTRY * oldp)
84 {
85     ENTRY *newp = typeCalloc(ENTRY, 1);
86
87     if (newp != NULL) {
88         *newp = *oldp;
89         _nc_copy_termtype2(&(newp->tterm), &(oldp->tterm));
90     }
91     return newp;
92 }
93
94 /* save a copy of string in the string buffer */
95 NCURSES_EXPORT(char *)
96 _nc_save_str(const char *string)
97 {
98     char *result = 0;
99     size_t old_next_free = next_free;
100     size_t len;
101
102     if (stringbuf != NULL) {
103         if (!VALID_STRING(string))
104             string = "";
105         len = strlen(string) + 1;
106
107         if (len == 1 && next_free != 0) {
108             /*
109              * Cheat a little by making an empty string point to the end of the
110              * previous string.
111              */
112             if (next_free < MAX_ENTRY_SIZE) {
113                 result = (stringbuf + next_free - 1);
114             }
115         } else if (next_free + len < MAX_ENTRY_SIZE) {
116             _nc_STRCPY(&stringbuf[next_free], string, MAX_ENTRY_SIZE);
117             DEBUG(7, ("Saved string %s", _nc_visbuf(string)));
118             DEBUG(7, ("at location %d", (int) next_free));
119             next_free += len;
120             result = (stringbuf + old_next_free);
121         } else {
122             _nc_warning("Too much data, some is lost: %s", string);
123         }
124     }
125     return result;
126 }
127
128 NCURSES_EXPORT(void)
129 _nc_wrap_entry(ENTRY * const ep, bool copy_strings)
130 /* copy the string parts to allocated storage, preserving pointers to it */
131 {
132     int offsets[MAX_ENTRY_SIZE / sizeof(short)];
133     int useoffsets[MAX_USES];
134     unsigned i, n;
135     unsigned nuses;
136     TERMTYPE2 *tp;
137
138     if (ep == NULL || stringbuf == NULL)
139         _nc_err_abort("_nc_wrap_entry called without initialization");
140
141     nuses = ep->nuses;
142     tp = &(ep->tterm);
143     if (copy_strings) {
144         next_free = 0;          /* clear static storage */
145
146         /* copy term_names, Strings, uses */
147         tp->term_names = _nc_save_str(tp->term_names);
148         for_each_string(i, tp) {
149             if (tp->Strings[i] != ABSENT_STRING &&
150                 tp->Strings[i] != CANCELLED_STRING) {
151                 tp->Strings[i] = _nc_save_str(tp->Strings[i]);
152             }
153         }
154
155         for (i = 0; i < nuses; i++) {
156             if (ep->uses[i].name == 0) {
157                 ep->uses[i].name = _nc_save_str(ep->uses[i].name);
158             }
159         }
160
161         free(tp->str_table);
162     }
163
164     assert(tp->term_names >= stringbuf);
165     n = (unsigned) (tp->term_names - stringbuf);
166     for_each_string(i, &(ep->tterm)) {
167         if (i < SIZEOF(offsets)) {
168             if (tp->Strings[i] == ABSENT_STRING) {
169                 offsets[i] = ABSENT_OFFSET;
170             } else if (tp->Strings[i] == CANCELLED_STRING) {
171                 offsets[i] = CANCELLED_OFFSET;
172             } else {
173                 offsets[i] = (int) (tp->Strings[i] - stringbuf);
174             }
175         }
176     }
177
178     for (i = 0; i < nuses; i++) {
179         if (ep->uses[i].name == 0)
180             useoffsets[i] = ABSENT_OFFSET;
181         else
182             useoffsets[i] = (int) (ep->uses[i].name - stringbuf);
183     }
184
185     TYPE_MALLOC(char, next_free, tp->str_table);
186     (void) memcpy(tp->str_table, stringbuf, next_free);
187
188     tp->term_names = tp->str_table + n;
189     for_each_string(i, &(ep->tterm)) {
190         if (i < SIZEOF(offsets)) {
191             if (offsets[i] == ABSENT_OFFSET) {
192                 tp->Strings[i] = ABSENT_STRING;
193             } else if (offsets[i] == CANCELLED_OFFSET) {
194                 tp->Strings[i] = CANCELLED_STRING;
195             } else {
196                 tp->Strings[i] = tp->str_table + offsets[i];
197             }
198         }
199     }
200
201 #if NCURSES_XNAMES
202     if (!copy_strings) {
203         if ((n = (unsigned) NUM_EXT_NAMES(tp)) != 0) {
204             if (n < SIZEOF(offsets)) {
205                 size_t length = 0;
206                 size_t offset;
207                 for (i = 0; i < n; i++) {
208                     length += strlen(tp->ext_Names[i]) + 1;
209                     offsets[i] = (int) (tp->ext_Names[i] - stringbuf);
210                 }
211                 TYPE_MALLOC(char, length, tp->ext_str_table);
212                 for (i = 0, offset = 0; i < n; i++) {
213                     tp->ext_Names[i] = tp->ext_str_table + offset;
214                     _nc_STRCPY(tp->ext_Names[i],
215                                stringbuf + offsets[i],
216                                length - offset);
217                     offset += strlen(tp->ext_Names[i]) + 1;
218                 }
219             }
220         }
221     }
222 #endif
223
224     for (i = 0; i < nuses; i++) {
225         if (useoffsets[i] == ABSENT_OFFSET)
226             ep->uses[i].name = 0;
227         else
228             ep->uses[i].name = (tp->str_table + useoffsets[i]);
229     }
230 }
231
232 NCURSES_EXPORT(void)
233 _nc_merge_entry(ENTRY * const target, ENTRY * const source)
234 /* merge capabilities from `from' entry into `to' entry */
235 {
236     TERMTYPE2 *to = &(target->tterm);
237     TERMTYPE2 *from = &(source->tterm);
238 #if NCURSES_XNAMES
239     TERMTYPE2 copy;
240 #endif
241     unsigned i;
242
243     if (source == 0 || from == 0 || target == 0 || to == 0)
244         return;
245
246 #if NCURSES_XNAMES
247     _nc_copy_termtype2(&copy, from);
248     from = &copy;
249     _nc_align_termtype(to, from);
250 #endif
251     for_each_boolean(i, from) {
252         if (to->Booleans[i] != (NCURSES_SBOOL) 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] = (NCURSES_SBOOL) mergebool;
259         }
260     }
261
262     for_each_number(i, from) {
263         if (to->Numbers[i] != CANCELLED_NUMERIC) {
264             int 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] = (NCURSES_INT2) 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 #if NCURSES_XNAMES
289     /* Discard the data allocated in _nc_copy_termtype2, but do not use
290      * _nc_free_termtype2 because that frees the string-table (which is
291      * not allocated by _nc_copy_termtype2).
292      */
293     free(copy.Booleans);
294     free(copy.Numbers);
295     free(copy.Strings);
296     free(copy.ext_Names);
297 #endif
298 }
299
300 #if NO_LEAKS
301 NCURSES_EXPORT(void)
302 _nc_alloc_entry_leaks(void)
303 {
304     if (stringbuf != NULL) {
305         FreeAndNull(stringbuf);
306     }
307     next_free = 0;
308 }
309 #endif