xref: /freebsd/contrib/ncurses/ncurses/tinfo/comp_parse.c (revision 68ad2b0d7af2a3571c4abac9afa712f9b09b721c)
1 /****************************************************************************
2  * Copyright 2018-2024,2025 Thomas E. Dickey                                *
3  * Copyright 1998-2016,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  *	comp_parse.c -- parser driver loop and use handling.
38  *
39  *	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().
43  */
44 
45 #include <curses.priv.h>
46 
47 #include <ctype.h>
48 
49 #include <tic.h>
50 
51 MODULE_ID("$Id: comp_parse.c,v 1.141 2025/12/27 12:33:34 tom Exp $")
52 
53 static void sanity_check2(TERMTYPE2 *, bool);
54 NCURSES_IMPEXP void (NCURSES_API *_nc_check_termtype2) (TERMTYPE2 *, bool) = sanity_check2;
55 
56 static void fixup_acsc(TERMTYPE2 *, int);
57 
58 static void
enqueue(ENTRY * ep)59 enqueue(ENTRY * ep)
60 /* add an entry to the in-core list */
61 {
62     ENTRY *newp;
63 
64     DEBUG(2, (T_CALLED("enqueue(ep=%p)"), (void *) ep));
65 
66     newp = _nc_copy_entry(ep);
67     if (newp == NULL)
68 	_nc_err_abort(MSG_NO_MEMORY);
69 
70     newp->last = _nc_tail;
71     _nc_tail = newp;
72 
73     newp->next = NULL;
74     if (newp->last)
75 	newp->last->next = newp;
76     DEBUG(2, (T_RETURN("")));
77 }
78 
79 #define NAMEBUFFER_SIZE (MAX_NAME_SIZE + 2)
80 
81 static char *
force_bar(char * dst,char * src)82 force_bar(char *dst, char *src)
83 {
84     if (strchr(src, '|') == NULL) {
85 	size_t len = strlen(src);
86 	if (len > MAX_NAME_SIZE)
87 	    len = MAX_NAME_SIZE;
88 	_nc_STRNCPY(dst, src, MAX_NAME_SIZE);
89 	_nc_STRCPY(dst + len, "|", NAMEBUFFER_SIZE - len);
90 	src = dst;
91     }
92     return src;
93 }
94 #define ForceBar(dst, src) ((strchr(src, '|') == NULL) ? force_bar(dst, src) : src)
95 
96 #if NCURSES_USE_TERMCAP && NCURSES_XNAMES
97 static char *
skip_index(char * name)98 skip_index(char *name)
99 {
100     char *bar = strchr(name, '|');
101 
102     if (bar != NULL && (bar - name) == 2)
103 	name = bar + 1;
104 
105     return name;
106 }
107 #endif
108 
109 static bool
check_collisions(char * n1,char * n2,int counter)110 check_collisions(char *n1, char *n2, int counter)
111 {
112     const char *pstart;
113     const char *qstart;
114     char *pend, *qend;
115     char nc1[NAMEBUFFER_SIZE];
116     char nc2[NAMEBUFFER_SIZE];
117 
118     n1 = ForceBar(nc1, n1);
119     n2 = ForceBar(nc2, n2);
120 
121 #if NCURSES_USE_TERMCAP && NCURSES_XNAMES
122     if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
123 	n1 = skip_index(n1);
124 	n2 = skip_index(n2);
125     }
126 #endif
127 
128     for (pstart = n1; (pend = strchr(pstart, '|')); pstart = pend + 1) {
129 	for (qstart = n2; (qend = strchr(qstart, '|')); qstart = qend + 1) {
130 	    if ((pend - pstart == qend - qstart)
131 		&& memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0) {
132 		if (counter > 0)
133 		    (void) fprintf(stderr, "Name collision '%.*s' between\n",
134 				   (int) (pend - pstart), pstart);
135 		return (TRUE);
136 	    }
137 	}
138     }
139 
140     return (FALSE);
141 }
142 
143 static char *
next_name(char * name)144 next_name(char *name)
145 {
146     if (*name != '\0')
147 	++name;
148     return name;
149 }
150 
151 static char *
name_ending(char * name)152 name_ending(char *name)
153 {
154     if (*name == '\0') {
155 	name = NULL;
156     } else {
157 	while (*name != '\0' && *name != '|')
158 	    ++name;
159     }
160     return name;
161 }
162 
163 /*
164  * Essentially, find the conflict reported in check_collisions() and remove
165  * it from the second name, unless that happens to be the last alias.
166  */
167 static bool
remove_collision(char * n1,char * n2)168 remove_collision(char *n1, char *n2)
169 {
170     char *p2 = n2;
171     char *pstart, *qstart, *pend, *qend;
172     bool removed = FALSE;
173 
174 #if NCURSES_USE_TERMCAP && NCURSES_XNAMES
175     if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
176 	n1 = skip_index(n1);
177 	p2 = n2 = skip_index(n2);
178     }
179 #endif
180 
181     for (pstart = n1; (pend = name_ending(pstart)); pstart = next_name(pend)) {
182 	for (qstart = n2; (qend = name_ending(qstart)); qstart = next_name(qend)) {
183 	    if ((pend - pstart == qend - qstart)
184 		&& memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0) {
185 		if (qstart != p2 || *qend == '|') {
186 		    if (*qend == '|')
187 			++qend;
188 		    while ((*qstart++ = *qend++) != '\0') ;
189 		    fprintf(stderr, "...now\t%s\n", p2);
190 		    removed = TRUE;
191 		} else {
192 		    fprintf(stderr, "Cannot remove alias '%.*s'\n",
193 			    (int) (qend - qstart), qstart);
194 		}
195 		break;
196 	    }
197 	}
198     }
199 
200     return removed;
201 }
202 
203 /* do any of the aliases in a pair of terminal names match? */
204 NCURSES_EXPORT(bool)
_nc_entry_match(char * n1,char * n2)205 _nc_entry_match(char *n1, char *n2)
206 {
207     return check_collisions(n1, n2, 0);
208 }
209 
210 /****************************************************************************
211  *
212  * Entry compiler and resolution logic
213  *
214  ****************************************************************************/
215 
216 NCURSES_EXPORT(void)
_nc_read_entry_source(FILE * fp,char * buf,int literal,bool silent,bool (* hook)(ENTRY *))217 _nc_read_entry_source(FILE *fp, char *buf,
218 		      int literal, bool silent,
219 		      bool(*hook) (ENTRY *))
220 /* slurp all entries in the given file into core */
221 {
222     ENTRY thisentry;
223     bool oldsuppress = _nc_suppress_warnings;
224     int immediate = 0;
225 
226     DEBUG(2,
227 	  (T_CALLED("_nc_read_entry_source("
228 		    "file=%p, buf=%p, literal=%d, silent=%d, hook=%#"
229 		    PRIxPTR ")"),
230 	   (void *) fp, buf, literal, silent, CASTxPTR(hook)));
231 
232     if (silent)
233 	_nc_suppress_warnings = TRUE;	/* shut the lexer up, too */
234 
235     _nc_reset_input(fp, buf);
236     for (;;) {
237 	memset(&thisentry, 0, sizeof(thisentry));
238 	if (_nc_parse_entry(&thisentry, literal, silent) == ERR)
239 	    break;
240 	if (!isalnum(UChar(thisentry.tterm.term_names[0])))
241 	    _nc_err_abort("terminal names must start with letter or digit");
242 
243 	/*
244 	 * This can be used for immediate compilation of entries with no "use="
245 	 * references to disk.  That avoids consuming a lot of memory when the
246 	 * resolution code could fetch entries off disk.
247 	 */
248 	if (hook != NULLHOOK && (*hook) (&thisentry)) {
249 	    immediate++;
250 	} else {
251 	    enqueue(&thisentry);
252 	    /*
253 	     * The enqueued entry is copied with _nc_copy_termtype(), so we can
254 	     * free some of the data from thisentry, i.e., the arrays.
255 	     */
256 	    FreeIfNeeded(thisentry.tterm.Booleans);
257 	    FreeIfNeeded(thisentry.tterm.Numbers);
258 	    FreeIfNeeded(thisentry.tterm.Strings);
259 	    FreeIfNeeded(thisentry.tterm.str_table);
260 #if NCURSES_XNAMES
261 	    FreeIfNeeded(thisentry.tterm.ext_Names);
262 	    FreeIfNeeded(thisentry.tterm.ext_str_table);
263 #endif
264 	}
265     }
266 
267     if (_nc_tail) {
268 	/* set up the head pointer */
269 	for (_nc_head = _nc_tail; _nc_head->last; _nc_head = _nc_head->last) {
270 	    /* EMPTY */ ;
271 	}
272 
273 	DEBUG(2, ("head = %s", _nc_head->tterm.term_names));
274 	DEBUG(2, ("tail = %s", _nc_tail->tterm.term_names));
275     }
276 #ifdef TRACE
277     else if (!immediate)
278 	DEBUG(2, ("no entries parsed"));
279 #endif
280 
281     _nc_suppress_warnings = oldsuppress;
282     DEBUG(2, (T_RETURN("")));
283 }
284 
285 #if 0 && NCURSES_XNAMES
286 static unsigned
287 find_capname(TERMTYPE2 *p, const char *name)
288 {
289     unsigned num_names = NUM_EXT_NAMES(p);
290     unsigned n;
291     if (name != 0) {
292 	for (n = 0; n < num_names; ++n) {
293 	    if (!strcmp(p->ext_Names[n], name))
294 		break;
295 	}
296     } else {
297 	n = num_names + 1;
298     }
299     return n;
300 }
301 
302 static int
303 extended_captype(TERMTYPE2 *p, unsigned which)
304 {
305     int result = UNDEF;
306     unsigned limit = 0;
307     limit += p->ext_Booleans;
308     if (limit != 0 && which < limit) {
309 	result = BOOLEAN;
310     } else {
311 	limit += p->ext_Numbers;
312 	if (limit != 0 && which < limit) {
313 	    result = NUMBER;
314 	} else {
315 	    limit += p->ext_Strings;
316 	    if (limit != 0 && which < limit) {
317 		result = ((p->Strings[STRCOUNT + which] != CANCELLED_STRING)
318 			  ? STRING
319 			  : CANCEL);
320 	    } else if (which >= limit) {
321 		result = CANCEL;
322 	    }
323 	}
324     }
325     return result;
326 }
327 
328 static const char *
329 name_of_captype(int which)
330 {
331     const char *result = "?";
332     switch (which) {
333     case BOOLEAN:
334 	result = "boolean";
335 	break;
336     case NUMBER:
337 	result = "number";
338 	break;
339     case STRING:
340 	result = "string";
341 	break;
342     }
343     return result;
344 }
345 
346 #define valid_TERMTYPE2(p) \
347 	((p) != 0 && \
348 	 (p)->term_names != 0 && \
349 	 (p)->ext_Names != 0)
350 
351 /*
352  * Disallow changing the type of an extended capability when doing a "use"
353  * if one or the other is a string.
354  */
355 static int
356 invalid_merge(TERMTYPE2 *to, TERMTYPE2 *from)
357 {
358     int rc = FALSE;
359     if (valid_TERMTYPE2(to)
360 	&& valid_TERMTYPE2(from)) {
361 	char *to_name = _nc_first_name(to->term_names);
362 	char *from_name = strdup(_nc_first_name(from->term_names));
363 	unsigned num_names = NUM_EXT_NAMES(from);
364 	unsigned n;
365 
366 	for (n = 0; n < num_names; ++n) {
367 	    const char *capname = from->ext_Names[n];
368 	    int tt = extended_captype(to, find_capname(to, capname));
369 	    int tf = extended_captype(from, n);
370 
371 	    if (tt <= STRING
372 		&& tf <= STRING
373 		&& (tt == STRING) != (tf == STRING)) {
374 		if (from_name != 0 && strcmp(to_name, from_name)) {
375 		    _nc_warning("merge of %s to %s changes type of %s from %s to %s",
376 				from_name,
377 				to_name,
378 				from->ext_Names[n],
379 				name_of_captype(tf),
380 				name_of_captype(tt));
381 		} else {
382 		    _nc_warning("merge of %s changes type of %s from %s to %s",
383 				to_name,
384 				from->ext_Names[n],
385 				name_of_captype(tf),
386 				name_of_captype(tt));
387 		}
388 		rc = TRUE;
389 	    }
390 	}
391 	free(from_name);
392     }
393     return rc;
394 }
395 #define validate_merge(p, q) \
396 	if (invalid_merge(&((p)->tterm), &((q)->tterm))) \
397 		return FALSE
398 #else
399 #define validate_merge(p, q)	/* nothing */
400 #endif
401 
402 NCURSES_EXPORT(int)
_nc_resolve_uses2(bool fullresolve,bool literal)403 _nc_resolve_uses2(bool fullresolve, bool literal)
404 /* try to resolve all use capabilities */
405 {
406     ENTRY *qp, *rp, *lastread = NULL;
407     bool keepgoing;
408     unsigned i, j;
409     int total_unresolved, multiples;
410 
411     DEBUG(2, (T_CALLED("_nc_resolve_uses2")));
412 
413     /*
414      * Check for multiple occurrences of the same name.
415      */
416     multiples = 0;
417     for_entry_list(qp) {
418 	int matchcount = 0;
419 
420 	for_entry_list2(rp, qp->next) {
421 	    if (qp > rp
422 		&& check_collisions(qp->tterm.term_names,
423 				    rp->tterm.term_names,
424 				    matchcount + 1)) {
425 		if (!matchcount++) {
426 		    (void) fprintf(stderr, "\t%s\n", rp->tterm.term_names);
427 		}
428 		(void) fprintf(stderr, "and\t%s\n", qp->tterm.term_names);
429 		if (!remove_collision(rp->tterm.term_names,
430 				      qp->tterm.term_names)) {
431 		    ++multiples;
432 		}
433 	    }
434 	}
435     }
436     if (multiples > 0) {
437 	DEBUG(2, (T_RETURN("false")));
438 	return (FALSE);
439     }
440 
441     DEBUG(2, ("NO MULTIPLE NAME OCCURRENCES"));
442 
443     /*
444      * First resolution stage: compute link pointers corresponding to names.
445      */
446     total_unresolved = 0;
447     _nc_curr_col = -1;
448     for_entry_list(qp) {
449 	for (i = 0; i < qp->nuses; i++) {
450 	    bool foundit;
451 	    char *child = _nc_first_name(qp->tterm.term_names);
452 	    char *lookfor = qp->uses[i].name;
453 	    long lookline = qp->uses[i].line;
454 
455 	    if (lookfor == NULL)
456 		continue;
457 
458 	    foundit = FALSE;
459 
460 	    _nc_set_type(child);
461 
462 	    /* first, try to resolve from in-core records */
463 	    for_entry_list(rp) {
464 		if (rp != qp
465 		    && _nc_name_match(rp->tterm.term_names, lookfor, "|")) {
466 		    DEBUG(2, ("%s: resolving use=%s %p (in core)",
467 			      child, lookfor, lookfor));
468 
469 		    qp->uses[i].link = rp;
470 		    foundit = TRUE;
471 
472 		    /* verify that there are no earlier uses */
473 		    for (j = 0; j < i; ++j) {
474 			if (qp->uses[j].link != NULL
475 			    && !strcmp(qp->uses[j].link->tterm.term_names,
476 				       rp->tterm.term_names)) {
477 			    _nc_warning("duplicate use=%s", lookfor);
478 			    break;
479 			}
480 		    }
481 		}
482 	    }
483 
484 	    /* if that didn't work, try to merge in a compiled entry */
485 	    if (!foundit) {
486 		TERMTYPE2 thisterm;
487 		char filename[PATH_MAX];
488 
489 		memset(&thisterm, 0, sizeof(thisterm));
490 		if (_nc_read_entry2(lookfor, filename, &thisterm) == 1) {
491 		    DEBUG(2, ("%s: resolving use=%s (compiled)",
492 			      child, lookfor));
493 
494 		    TYPE_MALLOC(ENTRY, 1, rp);
495 		    rp->tterm = thisterm;
496 		    rp->nuses = 0;
497 		    rp->next = lastread;
498 		    lastread = rp;
499 
500 		    qp->uses[i].link = rp;
501 		    foundit = TRUE;
502 
503 		    /* verify that there are no earlier uses */
504 		    for (j = 0; j < i; ++j) {
505 			if (qp->uses[j].link != NULL
506 			    && !strcmp(qp->uses[j].link->tterm.term_names,
507 				       rp->tterm.term_names)) {
508 			    _nc_warning("duplicate use=%s", lookfor);
509 			    break;
510 			}
511 		    }
512 		}
513 	    }
514 
515 	    /* no good, mark this one unresolvable and complain */
516 	    if (!foundit) {
517 		total_unresolved++;
518 
519 		_nc_curr_line = (int) lookline;
520 		_nc_warning("resolution of use=%s failed", lookfor);
521 		qp->uses[i].link = NULL;
522 	    }
523 	}
524     }
525     if (total_unresolved) {
526 	/* free entries read in off disk */
527 	_nc_free_entries(lastread);
528 	DEBUG(2, (T_RETURN("false")));
529 	return (FALSE);
530     }
531 
532     DEBUG(2, ("NAME RESOLUTION COMPLETED OK"));
533 
534     /*
535      * OK, at this point all (char *) references in `name' members
536      * have been successfully converted to (ENTRY *) pointers in
537      * `link' members.  Time to do the actual merges.
538      */
539     if (fullresolve) {
540 	do {
541 	    bool attempts;
542 	    bool progress;
543 	    ENTRY merged;
544 
545 	    attempts = FALSE;
546 	    progress = FALSE;
547 	    keepgoing = FALSE;
548 
549 	    for_entry_list(qp) {
550 		if (qp->nuses > 0) {
551 		    attempts = TRUE;
552 
553 		    DEBUG(2, ("%s: attempting merge of %d entries",
554 			      _nc_first_name(qp->tterm.term_names),
555 			      qp->nuses));
556 		    /*
557 		     * If any of the use entries we're looking for is
558 		     * incomplete, punt.  We'll catch this entry on a
559 		     * subsequent pass.
560 		     */
561 		    for (i = 0; i < qp->nuses; i++) {
562 			if (qp->uses[i].link
563 			    && qp->uses[i].link->nuses) {
564 			    DEBUG(2, ("%s: use entry %d unresolved",
565 				      _nc_first_name(qp->tterm.term_names), i));
566 			    goto incomplete;
567 			}
568 		    }
569 
570 		    /*
571 		     * First, make sure there is no garbage in the
572 		     * merge block.  As a side effect, copy into
573 		     * the merged entry the name field and string
574 		     * table pointer.
575 		     */
576 		    _nc_copy_termtype2(&(merged.tterm), &(qp->tterm));
577 
578 		    /*
579 		     * Now merge in each use entry in the proper
580 		     * (reverse) order.
581 		     */
582 		    for (; qp->nuses; qp->nuses--) {
583 			int n = (int) (qp->nuses - 1);
584 			validate_merge(&merged, qp->uses[n].link);
585 			_nc_merge_entry(&merged, qp->uses[n].link);
586 			free(qp->uses[n].name);
587 		    }
588 
589 		    /*
590 		     * Now merge in the original entry.
591 		     */
592 		    validate_merge(&merged, qp);
593 		    _nc_merge_entry(&merged, qp);
594 
595 		    /*
596 		     * Replace the original entry with the merged one.
597 		     */
598 		    FreeIfNeeded(qp->tterm.Booleans);
599 		    FreeIfNeeded(qp->tterm.Numbers);
600 		    FreeIfNeeded(qp->tterm.Strings);
601 		    FreeIfNeeded(qp->tterm.str_table);
602 #if NCURSES_XNAMES
603 		    FreeIfNeeded(qp->tterm.ext_Names);
604 		    FreeIfNeeded(qp->tterm.ext_str_table);
605 #endif
606 		    qp->tterm = merged.tterm;
607 		    _nc_wrap_entry(qp, TRUE);
608 		    progress = TRUE;
609 
610 		    /*
611 		     * Every entry should be resolvable because name resolution
612 		     * did not fail.  Continue if we have just made a change,
613 		     * or another entry may be changeable.
614 		     */
615 		    /* FALLTHRU */
616 		  incomplete:
617 		    keepgoing = TRUE;
618 		}
619 	    }
620 
621 	    /*
622 	     * If we went all the way through the list without making any
623 	     * changes, while there were remaining use-linkages, something went
624 	     * wrong.  Give up.
625 	     */
626 	    if (!progress && attempts) {
627 		for_entry_list(qp) {
628 		    for (i = 0; i < qp->nuses; ++i) {
629 			_nc_warning("problem with use=%s", qp->uses[i].name);
630 		    }
631 		}
632 		_nc_warning("merge failed, infinite loop");
633 		DEBUG(2, (T_RETURN("false")));
634 		return FALSE;
635 	    }
636 	} while
637 	    (keepgoing);
638 
639 	DEBUG(2, ("MERGES COMPLETED OK"));
640     }
641 
642     DEBUG(2, ("RESOLUTION FINISHED"));
643 
644     if (fullresolve) {
645 	_nc_curr_col = -1;
646 	for_entry_list(qp) {
647 	    _nc_curr_line = (int) qp->startline;
648 	    _nc_set_type(_nc_first_name(qp->tterm.term_names));
649 	    /*
650 	     * tic overrides this function pointer to provide more verbose
651 	     * checking.
652 	     */
653 	    if (_nc_check_termtype2 != sanity_check2) {
654 		SCREEN *save_SP = SP;
655 		SCREEN fake_sp;
656 		TERMINAL fake_tm;
657 		TERMINAL *save_tm = cur_term;
658 
659 		/*
660 		 * Setup so that tic can use ordinary terminfo interface to
661 		 * obtain capability information.
662 		 */
663 		memset(&fake_sp, 0, sizeof(fake_sp));
664 		memset(&fake_tm, 0, sizeof(fake_tm));
665 		fake_sp._term = &fake_tm;
666 		TerminalType(&fake_tm) = qp->tterm;
667 		_nc_set_screen(&fake_sp);
668 		set_curterm(&fake_tm);
669 #if USE_TERM_DRIVER
670 		((TERMINAL_CONTROL_BLOCK *) (CurTerm))->drv = &_nc_TINFO_DRIVER;
671 #endif
672 
673 		_nc_check_termtype2(&qp->tterm, literal);
674 
675 		/*
676 		 * Checking calls tparm, which can allocate memory.  Fix leaks.
677 		 */
678 #define TPS(name) fake_tm.tparm_state.name
679 		FreeAndNull(TPS(out_buff));
680 		FreeAndNull(TPS(fmt_buff));
681 #undef TPS
682 
683 		_nc_set_screen(save_SP);
684 		set_curterm(save_tm);
685 	    } else {
686 		fixup_acsc(&qp->tterm, literal);
687 	    }
688 	}
689 	DEBUG(2, ("SANITY CHECK FINISHED"));
690     }
691 
692     DEBUG(2, (T_RETURN("true")));
693     return (TRUE);
694 }
695 
696 /*
697  * This bit of legerdemain turns all the terminfo variable names into
698  * references to locations in the arrays Booleans, Numbers, and Strings ---
699  * precisely what's needed.
700  */
701 
702 #undef CUR
703 #define CUR tp->
704 
705 static void
fixup_acsc(TERMTYPE2 * tp,int literal)706 fixup_acsc(TERMTYPE2 *tp, int literal)
707 {
708     if (!literal) {
709 	if (acs_chars == ABSENT_STRING
710 	    && PRESENT(enter_alt_charset_mode)
711 	    && PRESENT(exit_alt_charset_mode))
712 	    acs_chars = strdup(VT_ACSC);
713     }
714 }
715 
716 static void
sanity_check2(TERMTYPE2 * tp,bool literal)717 sanity_check2(TERMTYPE2 *tp, bool literal)
718 {
719     if (!PRESENT(exit_attribute_mode)) {
720 #ifdef __UNUSED__		/* this casts too wide a net */
721 	bool terminal_entry = !strchr(tp->term_names, '+');
722 	if (terminal_entry &&
723 	    (PRESENT(set_attributes)
724 	     || PRESENT(enter_standout_mode)
725 	     || PRESENT(enter_underline_mode)
726 	     || PRESENT(enter_blink_mode)
727 	     || PRESENT(enter_bold_mode)
728 	     || PRESENT(enter_dim_mode)
729 	     || PRESENT(enter_secure_mode)
730 	     || PRESENT(enter_protected_mode)
731 	     || PRESENT(enter_reverse_mode)))
732 	    _nc_warning("no exit_attribute_mode");
733 #endif /* __UNUSED__ */
734 	PAIRED(enter_standout_mode, exit_standout_mode);
735 	PAIRED(enter_underline_mode, exit_underline_mode);
736 #if defined(enter_italics_mode) && defined(exit_italics_mode)
737 	PAIRED(enter_italics_mode, exit_italics_mode);
738 #endif
739     }
740 
741     /* we do this check/fix in postprocess_termcap(), but some packagers
742      * prefer to bypass it...
743      */
744     if (!literal) {
745 	fixup_acsc(tp, literal);
746 	ANDMISSING(enter_alt_charset_mode, acs_chars);
747 	ANDMISSING(exit_alt_charset_mode, acs_chars);
748     }
749 
750     /* listed in structure-member order of first argument */
751     PAIRED(enter_alt_charset_mode, exit_alt_charset_mode);
752     ANDMISSING(enter_blink_mode, exit_attribute_mode);
753     ANDMISSING(enter_bold_mode, exit_attribute_mode);
754     PAIRED(exit_ca_mode, enter_ca_mode);
755     PAIRED(enter_delete_mode, exit_delete_mode);
756     ANDMISSING(enter_dim_mode, exit_attribute_mode);
757     PAIRED(enter_insert_mode, exit_insert_mode);
758     ANDMISSING(enter_secure_mode, exit_attribute_mode);
759     ANDMISSING(enter_protected_mode, exit_attribute_mode);
760     ANDMISSING(enter_reverse_mode, exit_attribute_mode);
761     PAIRED(from_status_line, to_status_line);
762     PAIRED(meta_off, meta_on);
763 
764     PAIRED(prtr_on, prtr_off);
765     PAIRED(save_cursor, restore_cursor);
766     PAIRED(enter_xon_mode, exit_xon_mode);
767     PAIRED(enter_am_mode, exit_am_mode);
768     ANDMISSING(label_off, label_on);
769 #if defined(display_clock) && defined(remove_clock)
770     PAIRED(display_clock, remove_clock);
771 #endif
772     ANDMISSING(set_color_pair, initialize_pair);
773 }
774 
775 #if NO_LEAKS
776 NCURSES_EXPORT(void)
_nc_leaks_tic(void)777 _nc_leaks_tic(void)
778 {
779     T((T_CALLED("_nc_leaks_tic()")));
780     _nc_globals.leak_checking = TRUE;
781     _nc_alloc_entry_leaks();
782     _nc_captoinfo_leaks();
783     _nc_comp_scan_leaks();
784 #if BROKEN_LINKER || USE_REENTRANT
785     _nc_names_leaks();
786     _nc_codes_leaks();
787 #endif
788     _nc_tic_expand(NULL, FALSE, 0);
789     T((T_RETURN("")));
790 }
791 
792 NCURSES_EXPORT(void)
_nc_free_tic(int code)793 _nc_free_tic(int code)
794 {
795     T((T_CALLED("_nc_free_tic(%d)"), code));
796     _nc_leaks_tic();
797     exit_terminfo(code);
798 }
799 #endif
800