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