2 /***************************************************************************
4 ****************************************************************************
5 * ncurses is copyright (C) 1992-1995 *
7 * zmbenhal@netcom.com *
9 * esr@snark.thyrsus.com *
11 * Permission is hereby granted to reproduce and distribute ncurses *
12 * by any means and for any fee, whether alone or as part of a *
13 * larger distribution, in source or in binary form, PROVIDED *
14 * this notice is included with any such distribution, and is not *
15 * removed from any of its header files. Mention of ncurses in any *
16 * applications linked with it is highly appreciated. *
18 * ncurses comes AS IS with no warranty, implied or expressed. *
20 ***************************************************************************/
24 * alloc_entry.c -- allocation functions for terminfo entries
33 #include <curses.priv.h>
37 #include <term_entry.h>
39 MODULE_ID("$Id: alloc_entry.c,v 1.12 1997/02/01 22:59:47 tom Exp $")
41 #define MAX_STRTAB 4096 /* documented maximum entry size */
43 static char stringbuf[MAX_STRTAB]; /* buffer for string capabilities */
44 static size_t next_free; /* next free character in stringbuf */
46 void _nc_init_entry(TERMTYPE *const tp)
47 /* initialize a terminal type data block */
51 for (i=0; i < BOOLCOUNT; i++)
52 tp->Booleans[i] = FALSE;
54 for (i=0; i < NUMCOUNT; i++)
57 for (i=0; i < STRCOUNT; i++)
58 tp->Strings[i] = (char *)NULL;
63 char *_nc_save_str(const char *const string)
64 /* save a copy of string in the string buffer */
66 size_t old_next_free = next_free;
67 size_t len = strlen(string) + 1;
69 if (next_free + len < MAX_STRTAB)
71 strcpy(&stringbuf[next_free], string);
72 DEBUG(7, ("Saved string %s", _nc_visbuf(string)));
73 DEBUG(7, ("at location %d", (int) next_free));
76 return(stringbuf + old_next_free);
79 void _nc_wrap_entry(ENTRY *const ep)
80 /* copy the string parts to allocated storage, preserving pointers to it */
82 int offsets[STRCOUNT], useoffsets[MAX_USES];
85 n = ep->tterm.term_names - stringbuf;
86 for (i=0; i < STRCOUNT; i++)
87 if (ep->tterm.Strings[i] == (char *)NULL)
89 else if (ep->tterm.Strings[i] == CANCELLED_STRING)
92 offsets[i] = ep->tterm.Strings[i] - stringbuf;
94 for (i=0; i < ep->nuses; i++)
95 if (ep->uses[i].parent == (void *)NULL)
98 useoffsets[i] = (char *)(ep->uses[i].parent) - stringbuf;
100 if ((ep->tterm.str_table = (char *)malloc(next_free)) == (char *)NULL)
101 _nc_err_abort("Out of memory");
102 (void) memcpy(ep->tterm.str_table, stringbuf, next_free);
104 ep->tterm.term_names = ep->tterm.str_table + n;
105 for (i=0; i < STRCOUNT; i++)
106 if (offsets[i] == -1)
107 ep->tterm.Strings[i] = (char *)NULL;
108 else if (offsets[i] == -2)
109 ep->tterm.Strings[i] = CANCELLED_STRING;
111 ep->tterm.Strings[i] = ep->tterm.str_table + offsets[i];
113 for (i=0; i < ep->nuses; i++)
114 if (useoffsets[i] == -1)
115 ep->uses[i].parent = (void *)NULL;
117 ep->uses[i].parent = (char *)(ep->tterm.str_table + useoffsets[i]);
120 void _nc_merge_entry(TERMTYPE *const to, TERMTYPE *const from)
121 /* merge capabilities from `from' entry into `to' entry */
125 for (i=0; i < BOOLCOUNT; i++)
127 int mergebool = from->Booleans[i];
129 if (mergebool == CANCELLED_BOOLEAN)
130 to->Booleans[i] = FALSE;
131 else if (mergebool == TRUE)
132 to->Booleans[i] = mergebool;
135 for (i=0; i < NUMCOUNT; i++)
137 int mergenum = from->Numbers[i];
139 if (mergenum == CANCELLED_NUMERIC)
140 to->Numbers[i] = ABSENT_NUMERIC;
141 else if (mergenum != ABSENT_NUMERIC)
142 to->Numbers[i] = mergenum;
146 * Note: the copies of strings this makes don't have their own
147 * storage. This is OK right now, but will be a problem if we
148 * we ever want to deallocate entries.
150 for (i=0; i < STRCOUNT; i++)
152 char *mergestring = from->Strings[i];
154 if (mergestring == CANCELLED_STRING)
155 to->Strings[i] = ABSENT_STRING;
156 else if (mergestring != ABSENT_STRING)
157 to->Strings[i] = mergestring;