blob: dec4b925625efe1cd364cdd3829929eb92dbb541 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2023,2024 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
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 * comp_parse.c -- parser driver loop and use handling.
38 *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053039 * Use this code by calling _nc_read_entry_source() on as many source
40 * files as you like (either terminfo or termcap syntax). If you
41 * want use-resolution, call _nc_resolve_uses2(). To free the list
42 * storage, do _nc_free_entries().
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053043 */
44
45#include <curses.priv.h>
46
47#include <ctype.h>
48
49#include <tic.h>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053050
micky3879b9f5e72025-07-08 18:04:53 -040051MODULE_ID("$Id: comp_parse.c,v 1.134 2024/02/10 15:52:11 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053052
micky3879b9f5e72025-07-08 18:04:53 -040053static void sanity_check2(TERMTYPE2 *, bool);
54NCURSES_IMPEXP void (NCURSES_API *_nc_check_termtype2) (TERMTYPE2 *, bool) = sanity_check2;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053055
micky3879b9f5e72025-07-08 18:04:53 -040056static void fixup_acsc(TERMTYPE2 *, int);
Steve Kondikae271bc2015-11-15 02:50:53 +010057
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053058static void
59enqueue(ENTRY * ep)
60/* add an entry to the in-core list */
61{
micky3879b9f5e72025-07-08 18:04:53 -040062 ENTRY *newp;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053063
micky3879b9f5e72025-07-08 18:04:53 -040064 DEBUG(2, (T_CALLED("enqueue(ep=%p)"), (void *) ep));
65
66 newp = _nc_copy_entry(ep);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053067 if (newp == 0)
68 _nc_err_abort(MSG_NO_MEMORY);
69
70 newp->last = _nc_tail;
71 _nc_tail = newp;
72
73 newp->next = 0;
74 if (newp->last)
75 newp->last->next = newp;
micky3879b9f5e72025-07-08 18:04:53 -040076 DEBUG(2, (T_RETURN("")));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053077}
78
micky3879b9f5e72025-07-08 18:04:53 -040079#define NAMEBUFFER_SIZE (MAX_NAME_SIZE + 2)
80
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053081static char *
82force_bar(char *dst, char *src)
83{
84 if (strchr(src, '|') == 0) {
85 size_t len = strlen(src);
86 if (len > MAX_NAME_SIZE)
87 len = MAX_NAME_SIZE;
micky3879b9f5e72025-07-08 18:04:53 -040088 _nc_STRNCPY(dst, src, MAX_NAME_SIZE);
89 _nc_STRCPY(dst + len, "|", NAMEBUFFER_SIZE - len);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053090 src = dst;
91 }
92 return src;
93}
Steve Kondikae271bc2015-11-15 02:50:53 +010094#define ForceBar(dst, src) ((strchr(src, '|') == 0) ? force_bar(dst, src) : src)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053095
Steve Kondikae271bc2015-11-15 02:50:53 +010096#if NCURSES_USE_TERMCAP && NCURSES_XNAMES
97static char *
98skip_index(char *name)
99{
100 char *bar = strchr(name, '|');
101
102 if (bar != 0 && (bar - name) == 2)
103 name = bar + 1;
104
105 return name;
106}
107#endif
108
109static bool
110check_collisions(char *n1, char *n2, int counter)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530111{
112 char *pstart, *qstart, *pend, *qend;
micky3879b9f5e72025-07-08 18:04:53 -0400113 char nc1[NAMEBUFFER_SIZE];
114 char nc2[NAMEBUFFER_SIZE];
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530115
Steve Kondikae271bc2015-11-15 02:50:53 +0100116 n1 = ForceBar(nc1, n1);
117 n2 = ForceBar(nc2, n2);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530118
Steve Kondikae271bc2015-11-15 02:50:53 +0100119#if NCURSES_USE_TERMCAP && NCURSES_XNAMES
120 if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
121 n1 = skip_index(n1);
122 n2 = skip_index(n2);
123 }
124#endif
125
126 for (pstart = n1; (pend = strchr(pstart, '|')); pstart = pend + 1) {
127 for (qstart = n2; (qend = strchr(qstart, '|')); qstart = qend + 1) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530128 if ((pend - pstart == qend - qstart)
Steve Kondikae271bc2015-11-15 02:50:53 +0100129 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0) {
130 if (counter > 0)
131 (void) fprintf(stderr, "Name collision '%.*s' between\n",
132 (int) (pend - pstart), pstart);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530133 return (TRUE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100134 }
135 }
136 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530137
138 return (FALSE);
139}
140
Steve Kondikae271bc2015-11-15 02:50:53 +0100141static char *
142next_name(char *name)
143{
144 if (*name != '\0')
145 ++name;
146 return name;
147}
148
149static char *
150name_ending(char *name)
151{
152 if (*name == '\0') {
153 name = 0;
154 } else {
155 while (*name != '\0' && *name != '|')
156 ++name;
157 }
158 return name;
159}
160
161/*
162 * Essentially, find the conflict reported in check_collisions() and remove
163 * it from the second name, unless that happens to be the last alias.
164 */
165static bool
166remove_collision(char *n1, char *n2)
167{
168 char *p2 = n2;
169 char *pstart, *qstart, *pend, *qend;
170 bool removed = FALSE;
171
172#if NCURSES_USE_TERMCAP && NCURSES_XNAMES
173 if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
174 n1 = skip_index(n1);
175 p2 = n2 = skip_index(n2);
176 }
177#endif
178
179 for (pstart = n1; (pend = name_ending(pstart)); pstart = next_name(pend)) {
180 for (qstart = n2; (qend = name_ending(qstart)); qstart = next_name(qend)) {
181 if ((pend - pstart == qend - qstart)
182 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0) {
183 if (qstart != p2 || *qend == '|') {
184 if (*qend == '|')
185 ++qend;
186 while ((*qstart++ = *qend++) != '\0') ;
187 fprintf(stderr, "...now\t%s\n", p2);
micky3879b9f5e72025-07-08 18:04:53 -0400188 removed = TRUE;
Steve Kondikae271bc2015-11-15 02:50:53 +0100189 } else {
190 fprintf(stderr, "Cannot remove alias '%.*s'\n",
191 (int) (qend - qstart), qstart);
192 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100193 break;
194 }
195 }
196 }
197
198 return removed;
199}
200
201/* do any of the aliases in a pair of terminal names match? */
202NCURSES_EXPORT(bool)
203_nc_entry_match(char *n1, char *n2)
204{
205 return check_collisions(n1, n2, 0);
206}
207
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530208/****************************************************************************
209 *
210 * Entry compiler and resolution logic
211 *
212 ****************************************************************************/
213
214NCURSES_EXPORT(void)
215_nc_read_entry_source(FILE *fp, char *buf,
216 int literal, bool silent,
217 bool(*hook) (ENTRY *))
218/* slurp all entries in the given file into core */
219{
220 ENTRY thisentry;
221 bool oldsuppress = _nc_suppress_warnings;
222 int immediate = 0;
223
micky3879b9f5e72025-07-08 18:04:53 -0400224 DEBUG(2,
225 (T_CALLED("_nc_read_entry_source("
226 "file=%p, buf=%p, literal=%d, silent=%d, hook=%#"
227 PRIxPTR ")"),
228 (void *) fp, buf, literal, silent, CASTxPTR(hook)));
229
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530230 if (silent)
231 _nc_suppress_warnings = TRUE; /* shut the lexer up, too */
232
233 _nc_reset_input(fp, buf);
234 for (;;) {
235 memset(&thisentry, 0, sizeof(thisentry));
236 if (_nc_parse_entry(&thisentry, literal, silent) == ERR)
237 break;
238 if (!isalnum(UChar(thisentry.tterm.term_names[0])))
239 _nc_err_abort("terminal names must start with letter or digit");
240
241 /*
242 * This can be used for immediate compilation of entries with no "use="
243 * references to disk. That avoids consuming a lot of memory when the
244 * resolution code could fetch entries off disk.
245 */
246 if (hook != NULLHOOK && (*hook) (&thisentry)) {
247 immediate++;
248 } else {
249 enqueue(&thisentry);
250 /*
251 * The enqueued entry is copied with _nc_copy_termtype(), so we can
252 * free some of the data from thisentry, i.e., the arrays.
253 */
254 FreeIfNeeded(thisentry.tterm.Booleans);
255 FreeIfNeeded(thisentry.tterm.Numbers);
256 FreeIfNeeded(thisentry.tterm.Strings);
micky3879b9f5e72025-07-08 18:04:53 -0400257 FreeIfNeeded(thisentry.tterm.str_table);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530258#if NCURSES_XNAMES
259 FreeIfNeeded(thisentry.tterm.ext_Names);
micky3879b9f5e72025-07-08 18:04:53 -0400260 FreeIfNeeded(thisentry.tterm.ext_str_table);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530261#endif
262 }
263 }
264
265 if (_nc_tail) {
266 /* set up the head pointer */
micky3879b9f5e72025-07-08 18:04:53 -0400267 for (_nc_head = _nc_tail; _nc_head->last; _nc_head = _nc_head->last) {
268 /* EMPTY */ ;
269 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530270
micky3879b9f5e72025-07-08 18:04:53 -0400271 DEBUG(2, ("head = %s", _nc_head->tterm.term_names));
272 DEBUG(2, ("tail = %s", _nc_tail->tterm.term_names));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530273 }
274#ifdef TRACE
275 else if (!immediate)
micky3879b9f5e72025-07-08 18:04:53 -0400276 DEBUG(2, ("no entries parsed"));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530277#endif
278
279 _nc_suppress_warnings = oldsuppress;
micky3879b9f5e72025-07-08 18:04:53 -0400280 DEBUG(2, (T_RETURN("")));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530281}
282
micky3879b9f5e72025-07-08 18:04:53 -0400283#if 0 && NCURSES_XNAMES
284static unsigned
285find_capname(TERMTYPE2 *p, const char *name)
286{
287 unsigned num_names = NUM_EXT_NAMES(p);
288 unsigned n;
289 if (name != 0) {
290 for (n = 0; n < num_names; ++n) {
291 if (!strcmp(p->ext_Names[n], name))
292 break;
293 }
294 } else {
295 n = num_names + 1;
296 }
297 return n;
298}
299
300static int
301extended_captype(TERMTYPE2 *p, unsigned which)
302{
303 int result = UNDEF;
304 unsigned limit = 0;
305 limit += p->ext_Booleans;
306 if (limit != 0 && which < limit) {
307 result = BOOLEAN;
308 } else {
309 limit += p->ext_Numbers;
310 if (limit != 0 && which < limit) {
311 result = NUMBER;
312 } else {
313 limit += p->ext_Strings;
314 if (limit != 0 && which < limit) {
315 result = ((p->Strings[STRCOUNT + which] != CANCELLED_STRING)
316 ? STRING
317 : CANCEL);
318 } else if (which >= limit) {
319 result = CANCEL;
320 }
321 }
322 }
323 return result;
324}
325
326static const char *
327name_of_captype(int which)
328{
329 const char *result = "?";
330 switch (which) {
331 case BOOLEAN:
332 result = "boolean";
333 break;
334 case NUMBER:
335 result = "number";
336 break;
337 case STRING:
338 result = "string";
339 break;
340 }
341 return result;
342}
343
344#define valid_TERMTYPE2(p) \
345 ((p) != 0 && \
346 (p)->term_names != 0 && \
347 (p)->ext_Names != 0)
348
349/*
350 * Disallow changing the type of an extended capability when doing a "use"
351 * if one or the other is a string.
352 */
353static int
354invalid_merge(TERMTYPE2 *to, TERMTYPE2 *from)
355{
356 int rc = FALSE;
357 if (valid_TERMTYPE2(to)
358 && valid_TERMTYPE2(from)) {
359 char *to_name = _nc_first_name(to->term_names);
360 char *from_name = strdup(_nc_first_name(from->term_names));
361 unsigned num_names = NUM_EXT_NAMES(from);
362 unsigned n;
363
364 for (n = 0; n < num_names; ++n) {
365 const char *capname = from->ext_Names[n];
366 int tt = extended_captype(to, find_capname(to, capname));
367 int tf = extended_captype(from, n);
368
369 if (tt <= STRING
370 && tf <= STRING
371 && (tt == STRING) != (tf == STRING)) {
372 if (from_name != 0 && strcmp(to_name, from_name)) {
373 _nc_warning("merge of %s to %s changes type of %s from %s to %s",
374 from_name,
375 to_name,
376 from->ext_Names[n],
377 name_of_captype(tf),
378 name_of_captype(tt));
379 } else {
380 _nc_warning("merge of %s changes type of %s from %s to %s",
381 to_name,
382 from->ext_Names[n],
383 name_of_captype(tf),
384 name_of_captype(tt));
385 }
386 rc = TRUE;
387 }
388 }
389 free(from_name);
390 }
391 return rc;
392}
393#define validate_merge(p, q) \
394 if (invalid_merge(&((p)->tterm), &((q)->tterm))) \
395 return FALSE
396#else
397#define validate_merge(p, q) /* nothing */
398#endif
399
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530400NCURSES_EXPORT(int)
401_nc_resolve_uses2(bool fullresolve, bool literal)
402/* try to resolve all use capabilities */
403{
404 ENTRY *qp, *rp, *lastread = 0;
405 bool keepgoing;
micky3879b9f5e72025-07-08 18:04:53 -0400406 unsigned i, j;
407 int total_unresolved, multiples;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530408
micky3879b9f5e72025-07-08 18:04:53 -0400409 DEBUG(2, (T_CALLED("_nc_resolve_uses2")));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530410
411 /*
412 * Check for multiple occurrences of the same name.
413 */
414 multiples = 0;
415 for_entry_list(qp) {
416 int matchcount = 0;
417
micky3879b9f5e72025-07-08 18:04:53 -0400418 for_entry_list2(rp, qp->next) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530419 if (qp > rp
Steve Kondikae271bc2015-11-15 02:50:53 +0100420 && check_collisions(qp->tterm.term_names,
421 rp->tterm.term_names,
422 matchcount + 1)) {
423 if (!matchcount++) {
424 (void) fprintf(stderr, "\t%s\n", rp->tterm.term_names);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530425 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100426 (void) fprintf(stderr, "and\t%s\n", qp->tterm.term_names);
427 if (!remove_collision(rp->tterm.term_names,
428 qp->tterm.term_names)) {
429 ++multiples;
430 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530431 }
432 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530433 }
micky3879b9f5e72025-07-08 18:04:53 -0400434 if (multiples > 0) {
435 DEBUG(2, (T_RETURN("false")));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530436 return (FALSE);
micky3879b9f5e72025-07-08 18:04:53 -0400437 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530438
439 DEBUG(2, ("NO MULTIPLE NAME OCCURRENCES"));
440
441 /*
442 * First resolution stage: compute link pointers corresponding to names.
443 */
444 total_unresolved = 0;
445 _nc_curr_col = -1;
446 for_entry_list(qp) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530447 for (i = 0; i < qp->nuses; i++) {
448 bool foundit;
449 char *child = _nc_first_name(qp->tterm.term_names);
450 char *lookfor = qp->uses[i].name;
451 long lookline = qp->uses[i].line;
452
micky3879b9f5e72025-07-08 18:04:53 -0400453 if (lookfor == 0)
454 continue;
455
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530456 foundit = FALSE;
457
458 _nc_set_type(child);
459
460 /* first, try to resolve from in-core records */
461 for_entry_list(rp) {
462 if (rp != qp
463 && _nc_name_match(rp->tterm.term_names, lookfor, "|")) {
micky3879b9f5e72025-07-08 18:04:53 -0400464 DEBUG(2, ("%s: resolving use=%s %p (in core)",
465 child, lookfor, lookfor));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530466
467 qp->uses[i].link = rp;
468 foundit = TRUE;
micky3879b9f5e72025-07-08 18:04:53 -0400469
470 /* verify that there are no earlier uses */
471 for (j = 0; j < i; ++j) {
472 if (qp->uses[j].link != NULL
473 && !strcmp(qp->uses[j].link->tterm.term_names,
474 rp->tterm.term_names)) {
475 _nc_warning("duplicate use=%s", lookfor);
476 break;
477 }
478 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530479 }
480 }
481
482 /* if that didn't work, try to merge in a compiled entry */
483 if (!foundit) {
micky3879b9f5e72025-07-08 18:04:53 -0400484 TERMTYPE2 thisterm;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530485 char filename[PATH_MAX];
486
487 memset(&thisterm, 0, sizeof(thisterm));
micky3879b9f5e72025-07-08 18:04:53 -0400488 if (_nc_read_entry2(lookfor, filename, &thisterm) == 1) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530489 DEBUG(2, ("%s: resolving use=%s (compiled)",
490 child, lookfor));
491
Steve Kondikae271bc2015-11-15 02:50:53 +0100492 TYPE_MALLOC(ENTRY, 1, rp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530493 rp->tterm = thisterm;
494 rp->nuses = 0;
495 rp->next = lastread;
496 lastread = rp;
497
498 qp->uses[i].link = rp;
499 foundit = TRUE;
micky3879b9f5e72025-07-08 18:04:53 -0400500
501 /* verify that there are no earlier uses */
502 for (j = 0; j < i; ++j) {
503 if (qp->uses[j].link != NULL
504 && !strcmp(qp->uses[j].link->tterm.term_names,
505 rp->tterm.term_names)) {
506 _nc_warning("duplicate use=%s", lookfor);
507 break;
508 }
509 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530510 }
511 }
512
513 /* no good, mark this one unresolvable and complain */
514 if (!foundit) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530515 total_unresolved++;
516
Steve Kondikae271bc2015-11-15 02:50:53 +0100517 _nc_curr_line = (int) lookline;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530518 _nc_warning("resolution of use=%s failed", lookfor);
519 qp->uses[i].link = 0;
520 }
521 }
522 }
523 if (total_unresolved) {
524 /* free entries read in off disk */
525 _nc_free_entries(lastread);
micky3879b9f5e72025-07-08 18:04:53 -0400526 DEBUG(2, (T_RETURN("false")));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530527 return (FALSE);
528 }
529
530 DEBUG(2, ("NAME RESOLUTION COMPLETED OK"));
531
532 /*
533 * OK, at this point all (char *) references in `name' members
534 * have been successfully converted to (ENTRY *) pointers in
535 * `link' members. Time to do the actual merges.
536 */
537 if (fullresolve) {
538 do {
micky3879b9f5e72025-07-08 18:04:53 -0400539 ENTRY merged;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530540
541 keepgoing = FALSE;
542
543 for_entry_list(qp) {
544 if (qp->nuses > 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400545 DEBUG(2, ("%s: attempting merge of %d entries",
546 _nc_first_name(qp->tterm.term_names),
547 qp->nuses));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530548 /*
549 * If any of the use entries we're looking for is
550 * incomplete, punt. We'll catch this entry on a
551 * subsequent pass.
552 */
micky3879b9f5e72025-07-08 18:04:53 -0400553 for (i = 0; i < qp->nuses; i++) {
554 if (qp->uses[i].link
555 && qp->uses[i].link->nuses) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530556 DEBUG(2, ("%s: use entry %d unresolved",
557 _nc_first_name(qp->tterm.term_names), i));
558 goto incomplete;
559 }
micky3879b9f5e72025-07-08 18:04:53 -0400560 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530561
562 /*
563 * First, make sure there is no garbage in the
564 * merge block. As a side effect, copy into
565 * the merged entry the name field and string
566 * table pointer.
567 */
micky3879b9f5e72025-07-08 18:04:53 -0400568 _nc_copy_termtype2(&(merged.tterm), &(qp->tterm));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530569
570 /*
571 * Now merge in each use entry in the proper
572 * (reverse) order.
573 */
micky3879b9f5e72025-07-08 18:04:53 -0400574 for (; qp->nuses; qp->nuses--) {
575 int n = (int) (qp->nuses - 1);
576 validate_merge(&merged, qp->uses[n].link);
577 _nc_merge_entry(&merged, qp->uses[n].link);
578 free(qp->uses[n].name);
579 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530580
581 /*
582 * Now merge in the original entry.
583 */
micky3879b9f5e72025-07-08 18:04:53 -0400584 validate_merge(&merged, qp);
585 _nc_merge_entry(&merged, qp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530586
587 /*
588 * Replace the original entry with the merged one.
589 */
590 FreeIfNeeded(qp->tterm.Booleans);
591 FreeIfNeeded(qp->tterm.Numbers);
592 FreeIfNeeded(qp->tterm.Strings);
micky3879b9f5e72025-07-08 18:04:53 -0400593 FreeIfNeeded(qp->tterm.str_table);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530594#if NCURSES_XNAMES
595 FreeIfNeeded(qp->tterm.ext_Names);
micky3879b9f5e72025-07-08 18:04:53 -0400596 FreeIfNeeded(qp->tterm.ext_str_table);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530597#endif
micky3879b9f5e72025-07-08 18:04:53 -0400598 qp->tterm = merged.tterm;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530599 _nc_wrap_entry(qp, TRUE);
600
601 /*
602 * We know every entry is resolvable because name resolution
603 * didn't bomb. So go back for another pass.
604 */
605 /* FALLTHRU */
606 incomplete:
607 keepgoing = TRUE;
608 }
609 }
610 } while
611 (keepgoing);
612
613 DEBUG(2, ("MERGES COMPLETED OK"));
614 }
615
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530616 DEBUG(2, ("RESOLUTION FINISHED"));
617
micky3879b9f5e72025-07-08 18:04:53 -0400618 if (fullresolve) {
619 _nc_curr_col = -1;
620 for_entry_list(qp) {
621 _nc_curr_line = (int) qp->startline;
622 _nc_set_type(_nc_first_name(qp->tterm.term_names));
623 /*
624 * tic overrides this function pointer to provide more verbose
625 * checking.
626 */
627 if (_nc_check_termtype2 != sanity_check2) {
628 SCREEN *save_SP = SP;
629 SCREEN fake_sp;
630 TERMINAL fake_tm;
631 TERMINAL *save_tm = cur_term;
632
Steve Kondikae271bc2015-11-15 02:50:53 +0100633 /*
micky3879b9f5e72025-07-08 18:04:53 -0400634 * Setup so that tic can use ordinary terminfo interface to
635 * obtain capability information.
Steve Kondikae271bc2015-11-15 02:50:53 +0100636 */
micky3879b9f5e72025-07-08 18:04:53 -0400637 memset(&fake_sp, 0, sizeof(fake_sp));
638 memset(&fake_tm, 0, sizeof(fake_tm));
639 fake_sp._term = &fake_tm;
640 TerminalType(&fake_tm) = qp->tterm;
641 _nc_set_screen(&fake_sp);
642 set_curterm(&fake_tm);
Steve Kondikae271bc2015-11-15 02:50:53 +0100643
micky3879b9f5e72025-07-08 18:04:53 -0400644 _nc_check_termtype2(&qp->tterm, literal);
Steve Kondikae271bc2015-11-15 02:50:53 +0100645
micky3879b9f5e72025-07-08 18:04:53 -0400646 /*
647 * Checking calls tparm, which can allocate memory. Fix leaks.
648 */
649#define TPS(name) fake_tm.tparm_state.name
650 FreeAndNull(TPS(out_buff));
651 FreeAndNull(TPS(fmt_buff));
652#undef TPS
Steve Kondikae271bc2015-11-15 02:50:53 +0100653
micky3879b9f5e72025-07-08 18:04:53 -0400654 _nc_set_screen(save_SP);
655 set_curterm(save_tm);
656 } else {
657 fixup_acsc(&qp->tterm, literal);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530658 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530659 }
micky3879b9f5e72025-07-08 18:04:53 -0400660 DEBUG(2, ("SANITY CHECK FINISHED"));
661 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530662
micky3879b9f5e72025-07-08 18:04:53 -0400663 DEBUG(2, (T_RETURN("true")));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530664 return (TRUE);
665}
666
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530667/*
668 * This bit of legerdemain turns all the terminfo variable names into
669 * references to locations in the arrays Booleans, Numbers, and Strings ---
670 * precisely what's needed.
671 */
672
673#undef CUR
674#define CUR tp->
675
676static void
micky3879b9f5e72025-07-08 18:04:53 -0400677fixup_acsc(TERMTYPE2 *tp, int literal)
Steve Kondikae271bc2015-11-15 02:50:53 +0100678{
679 if (!literal) {
micky3879b9f5e72025-07-08 18:04:53 -0400680 if (acs_chars == ABSENT_STRING
681 && PRESENT(enter_alt_charset_mode)
682 && PRESENT(exit_alt_charset_mode))
Steve Kondikae271bc2015-11-15 02:50:53 +0100683 acs_chars = strdup(VT_ACSC);
684 }
685}
686
687static void
micky3879b9f5e72025-07-08 18:04:53 -0400688sanity_check2(TERMTYPE2 *tp, bool literal)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530689{
690 if (!PRESENT(exit_attribute_mode)) {
691#ifdef __UNUSED__ /* this casts too wide a net */
692 bool terminal_entry = !strchr(tp->term_names, '+');
693 if (terminal_entry &&
694 (PRESENT(set_attributes)
695 || PRESENT(enter_standout_mode)
696 || PRESENT(enter_underline_mode)
697 || PRESENT(enter_blink_mode)
698 || PRESENT(enter_bold_mode)
699 || PRESENT(enter_dim_mode)
700 || PRESENT(enter_secure_mode)
701 || PRESENT(enter_protected_mode)
702 || PRESENT(enter_reverse_mode)))
703 _nc_warning("no exit_attribute_mode");
704#endif /* __UNUSED__ */
705 PAIRED(enter_standout_mode, exit_standout_mode);
706 PAIRED(enter_underline_mode, exit_underline_mode);
micky3879b9f5e72025-07-08 18:04:53 -0400707#if defined(enter_italics_mode) && defined(exit_italics_mode)
Steve Kondikae271bc2015-11-15 02:50:53 +0100708 PAIRED(enter_italics_mode, exit_italics_mode);
micky3879b9f5e72025-07-08 18:04:53 -0400709#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530710 }
711
712 /* we do this check/fix in postprocess_termcap(), but some packagers
713 * prefer to bypass it...
714 */
715 if (!literal) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100716 fixup_acsc(tp, literal);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530717 ANDMISSING(enter_alt_charset_mode, acs_chars);
718 ANDMISSING(exit_alt_charset_mode, acs_chars);
719 }
720
721 /* listed in structure-member order of first argument */
722 PAIRED(enter_alt_charset_mode, exit_alt_charset_mode);
723 ANDMISSING(enter_blink_mode, exit_attribute_mode);
724 ANDMISSING(enter_bold_mode, exit_attribute_mode);
725 PAIRED(exit_ca_mode, enter_ca_mode);
726 PAIRED(enter_delete_mode, exit_delete_mode);
727 ANDMISSING(enter_dim_mode, exit_attribute_mode);
728 PAIRED(enter_insert_mode, exit_insert_mode);
729 ANDMISSING(enter_secure_mode, exit_attribute_mode);
730 ANDMISSING(enter_protected_mode, exit_attribute_mode);
731 ANDMISSING(enter_reverse_mode, exit_attribute_mode);
732 PAIRED(from_status_line, to_status_line);
733 PAIRED(meta_off, meta_on);
734
735 PAIRED(prtr_on, prtr_off);
736 PAIRED(save_cursor, restore_cursor);
737 PAIRED(enter_xon_mode, exit_xon_mode);
738 PAIRED(enter_am_mode, exit_am_mode);
739 ANDMISSING(label_off, label_on);
micky3879b9f5e72025-07-08 18:04:53 -0400740#if defined(display_clock) && defined(remove_clock)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530741 PAIRED(display_clock, remove_clock);
742#endif
743 ANDMISSING(set_color_pair, initialize_pair);
744}
745
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530746#if NO_LEAKS
747NCURSES_EXPORT(void)
748_nc_leaks_tic(void)
749{
micky3879b9f5e72025-07-08 18:04:53 -0400750 T((T_CALLED("_nc_leaks_tic()")));
751 _nc_globals.leak_checking = TRUE;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530752 _nc_alloc_entry_leaks();
753 _nc_captoinfo_leaks();
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530754 _nc_comp_scan_leaks();
755#if BROKEN_LINKER || USE_REENTRANT
756 _nc_names_leaks();
757 _nc_codes_leaks();
758#endif
759 _nc_tic_expand(0, FALSE, 0);
micky3879b9f5e72025-07-08 18:04:53 -0400760 T((T_RETURN("")));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530761}
762
763NCURSES_EXPORT(void)
764_nc_free_tic(int code)
765{
micky3879b9f5e72025-07-08 18:04:53 -0400766 T((T_CALLED("_nc_free_tic(%d)"), code));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530767 _nc_leaks_tic();
micky3879b9f5e72025-07-08 18:04:53 -0400768 exit_terminfo(code);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530769}
770#endif