]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/alloc_entry.c
ncurses 5.4
[ncurses.git] / ncurses / tinfo / alloc_entry.c
1 /****************************************************************************
2  * Copyright (c) 1998-2002,2003 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-2003               *
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 #include <term_entry.h>
50
51 MODULE_ID("$Id: alloc_entry.c,v 1.40 2003/11/08 21:29:54 tom Exp $")
52
53 #define ABSENT_OFFSET    -1
54 #define CANCELLED_OFFSET -2
55
56 #define MAX_STRTAB      4096    /* documented maximum entry size */
57
58 static char *stringbuf;         /* buffer for string capabilities */
59 static size_t next_free;        /* next free character in stringbuf */
60
61 NCURSES_EXPORT(void)
62 _nc_init_entry(TERMTYPE * const tp)
63 /* initialize a terminal type data block */
64 {
65     unsigned i;
66
67     if (stringbuf == 0)
68         stringbuf = (char *) malloc(MAX_STRTAB);
69
70 #if NCURSES_XNAMES
71     tp->num_Booleans = BOOLCOUNT;
72     tp->num_Numbers = NUMCOUNT;
73     tp->num_Strings = STRCOUNT;
74     tp->ext_Booleans = 0;
75     tp->ext_Numbers = 0;
76     tp->ext_Strings = 0;
77 #endif
78     if (tp->Booleans == 0)
79         tp->Booleans = typeMalloc(char, BOOLCOUNT);
80     if (tp->Numbers == 0)
81         tp->Numbers = typeMalloc(short, NUMCOUNT);
82     if (tp->Strings == 0)
83         tp->Strings = typeMalloc(char *, STRCOUNT);
84
85     for_each_boolean(i, tp)
86         tp->Booleans[i] = FALSE;
87
88     for_each_number(i, tp)
89         tp->Numbers[i] = ABSENT_NUMERIC;
90
91     for_each_string(i, tp)
92         tp->Strings[i] = ABSENT_STRING;
93
94     next_free = 0;
95 }
96
97 NCURSES_EXPORT(ENTRY *)
98 _nc_copy_entry(ENTRY * oldp)
99 {
100     ENTRY *newp = typeCalloc(ENTRY, 1);
101
102     if (newp != 0) {
103         *newp = *oldp;
104         _nc_copy_termtype(&(newp->tterm), &(oldp->tterm));
105     }
106     return newp;
107 }
108
109 NCURSES_EXPORT(char *)
110 _nc_save_str(const char *const string)
111 /* save a copy of string in the string buffer */
112 {
113     size_t old_next_free = next_free;
114     size_t len = strlen(string) + 1;
115
116     if (next_free + len < MAX_STRTAB) {
117         strcpy(&stringbuf[next_free], string);
118         DEBUG(7, ("Saved string %s", _nc_visbuf(string)));
119         DEBUG(7, ("at location %d", (int) next_free));
120         next_free += len;
121     }
122     return (stringbuf + old_next_free);
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 / 2], useoffsets[MAX_USES];
130     unsigned i, n;
131     unsigned nuses = ep->nuses;
132     TERMTYPE *tp = &(ep->tterm);
133
134     if (copy_strings) {
135         next_free = 0;          /* clear static storage */
136
137         /* copy term_names, Strings, uses */
138         tp->term_names = _nc_save_str(tp->term_names);
139         for_each_string(i, tp) {
140             if (tp->Strings[i] != ABSENT_STRING &&
141                 tp->Strings[i] != CANCELLED_STRING) {
142                 tp->Strings[i] = _nc_save_str(tp->Strings[i]);
143             }
144         }
145
146         for (i = 0; i < nuses; i++) {
147             if (ep->uses[i].name == 0) {
148                 ep->uses[i].name = _nc_save_str(ep->uses[i].name);
149             }
150         }
151
152         free(tp->str_table);
153     }
154
155     n = tp->term_names - stringbuf;
156     for_each_string(i, &(ep->tterm)) {
157         if (tp->Strings[i] == ABSENT_STRING)
158             offsets[i] = ABSENT_OFFSET;
159         else if (tp->Strings[i] == CANCELLED_STRING)
160             offsets[i] = CANCELLED_OFFSET;
161         else
162             offsets[i] = tp->Strings[i] - stringbuf;
163     }
164
165     for (i = 0; i < nuses; i++) {
166         if (ep->uses[i].name == 0)
167             useoffsets[i] = ABSENT_OFFSET;
168         else
169             useoffsets[i] = ep->uses[i].name - stringbuf;
170     }
171
172     if ((tp->str_table = typeMalloc(char, next_free)) == (char *) 0)
173           _nc_err_abort(MSG_NO_MEMORY);
174     (void) memcpy(tp->str_table, stringbuf, next_free);
175
176     tp->term_names = tp->str_table + n;
177     for_each_string(i, &(ep->tterm)) {
178         if (offsets[i] == ABSENT_OFFSET)
179             tp->Strings[i] = ABSENT_STRING;
180         else if (offsets[i] == CANCELLED_OFFSET)
181             tp->Strings[i] = CANCELLED_STRING;
182         else
183             tp->Strings[i] = tp->str_table + offsets[i];
184     }
185
186 #if NCURSES_XNAMES
187     if (!copy_strings) {
188         if ((n = NUM_EXT_NAMES(tp)) != 0) {
189             unsigned length = 0;
190             for (i = 0; i < n; i++) {
191                 length += strlen(tp->ext_Names[i]) + 1;
192                 offsets[i] = tp->ext_Names[i] - stringbuf;
193             }
194             if ((tp->ext_str_table = typeMalloc(char, length)) == 0)
195                   _nc_err_abort(MSG_NO_MEMORY);
196             for (i = 0, length = 0; i < n; i++) {
197                 tp->ext_Names[i] = tp->ext_str_table + length;
198                 strcpy(tp->ext_Names[i], stringbuf + offsets[i]);
199                 length += strlen(tp->ext_Names[i]) + 1;
200             }
201         }
202     }
203 #endif
204
205     for (i = 0; i < nuses; i++) {
206         if (useoffsets[i] == ABSENT_OFFSET)
207             ep->uses[i].name = 0;
208         else
209             ep->uses[i].name = (tp->str_table + useoffsets[i]);
210     }
211 }
212
213 NCURSES_EXPORT(void)
214 _nc_merge_entry(TERMTYPE * const to, TERMTYPE * const from)
215 /* merge capabilities from `from' entry into `to' entry */
216 {
217     unsigned i;
218
219 #if NCURSES_XNAMES
220     _nc_align_termtype(to, from);
221 #endif
222     for_each_boolean(i, from) {
223         if (to->Booleans[i] != CANCELLED_BOOLEAN) {
224             int mergebool = from->Booleans[i];
225
226             if (mergebool == CANCELLED_BOOLEAN)
227                 to->Booleans[i] = FALSE;
228             else if (mergebool == TRUE)
229                 to->Booleans[i] = mergebool;
230         }
231     }
232
233     for_each_number(i, from) {
234         if (to->Numbers[i] != CANCELLED_NUMERIC) {
235             int mergenum = from->Numbers[i];
236
237             if (mergenum == CANCELLED_NUMERIC)
238                 to->Numbers[i] = ABSENT_NUMERIC;
239             else if (mergenum != ABSENT_NUMERIC)
240                 to->Numbers[i] = mergenum;
241         }
242     }
243
244     /*
245      * Note: the copies of strings this makes don't have their own
246      * storage.  This is OK right now, but will be a problem if we
247      * we ever want to deallocate entries.
248      */
249     for_each_string(i, from) {
250         if (to->Strings[i] != CANCELLED_STRING) {
251             char *mergestring = from->Strings[i];
252
253             if (mergestring == CANCELLED_STRING)
254                 to->Strings[i] = ABSENT_STRING;
255             else if (mergestring != ABSENT_STRING)
256                 to->Strings[i] = mergestring;
257         }
258     }
259 }