1 /****************************************************************************
2 * Copyright 2018-2021,2022 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
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 ****************************************************************************/
37 * comp_parse.c -- parser driver loop and use handling.
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().
45 #include <curses.priv.h>
51 MODULE_ID("$Id: comp_parse.c,v 1.127 2022/09/24 15:24:15 tom Exp $")
53 static void sanity_check2(TERMTYPE2 *, bool);
54 NCURSES_IMPEXP void (NCURSES_API *_nc_check_termtype2) (TERMTYPE2 *, bool) = sanity_check2;
56 static void fixup_acsc(TERMTYPE2 *, int);
60 /* add an entry to the in-core list */
64 DEBUG(2, (T_CALLED("enqueue(ep=%p)"), (void *) ep));
66 newp = _nc_copy_entry(ep);
68 _nc_err_abort(MSG_NO_MEMORY);
70 newp->last = _nc_tail;
75 newp->last->next = newp;
76 DEBUG(2, (T_RETURN("")));
79 #define NAMEBUFFER_SIZE (MAX_NAME_SIZE + 2)
82 force_bar(char *dst, char *src)
84 if (strchr(src, '|') == 0) {
85 size_t len = strlen(src);
86 if (len > MAX_NAME_SIZE)
88 _nc_STRNCPY(dst, src, MAX_NAME_SIZE);
89 _nc_STRCPY(dst + len, "|", NAMEBUFFER_SIZE - len);
94 #define ForceBar(dst, src) ((strchr(src, '|') == 0) ? force_bar(dst, src) : src)
96 #if NCURSES_USE_TERMCAP && NCURSES_XNAMES
98 skip_index(char *name)
100 char *bar = strchr(name, '|');
102 if (bar != 0 && (bar - name) == 2)
110 check_collisions(char *n1, char *n2, int counter)
112 char *pstart, *qstart, *pend, *qend;
113 char nc1[NAMEBUFFER_SIZE];
114 char nc2[NAMEBUFFER_SIZE];
116 n1 = ForceBar(nc1, n1);
117 n2 = ForceBar(nc2, n2);
119 #if NCURSES_USE_TERMCAP && NCURSES_XNAMES
120 if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
126 for (pstart = n1; (pend = strchr(pstart, '|')); pstart = pend + 1) {
127 for (qstart = n2; (qend = strchr(qstart, '|')); qstart = qend + 1) {
128 if ((pend - pstart == qend - qstart)
129 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0) {
131 (void) fprintf(stderr, "Name collision '%.*s' between\n",
132 (int) (pend - pstart), pstart);
142 next_name(char *name)
150 name_ending(char *name)
155 while (*name != '\0' && *name != '|')
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.
166 remove_collision(char *n1, char *n2)
169 char *pstart, *qstart, *pend, *qend;
170 bool removed = FALSE;
172 #if NCURSES_USE_TERMCAP && NCURSES_XNAMES
173 if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
175 p2 = n2 = skip_index(n2);
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 == '|') {
186 while ((*qstart++ = *qend++) != '\0') ;
187 fprintf(stderr, "...now\t%s\n", p2);
190 fprintf(stderr, "Cannot remove alias '%.*s'\n",
191 (int) (qend - qstart), qstart);
201 /* do any of the aliases in a pair of terminal names match? */
203 _nc_entry_match(char *n1, char *n2)
205 return check_collisions(n1, n2, 0);
208 /****************************************************************************
210 * Entry compiler and resolution logic
212 ****************************************************************************/
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 */
221 bool oldsuppress = _nc_suppress_warnings;
225 (T_CALLED("_nc_read_entry_source(file=%p, buf=%p, literal=%d, silent=%d, hook=%p)"),
226 (void *) fp, buf, literal, silent, (void *) hook));
229 _nc_suppress_warnings = TRUE; /* shut the lexer up, too */
231 _nc_reset_input(fp, buf);
233 memset(&thisentry, 0, sizeof(thisentry));
234 if (_nc_parse_entry(&thisentry, literal, silent) == ERR)
236 if (!isalnum(UChar(thisentry.tterm.term_names[0])))
237 _nc_err_abort("terminal names must start with letter or digit");
240 * This can be used for immediate compilation of entries with no "use="
241 * references to disk. That avoids consuming a lot of memory when the
242 * resolution code could fetch entries off disk.
244 if (hook != NULLHOOK && (*hook) (&thisentry)) {
249 * The enqueued entry is copied with _nc_copy_termtype(), so we can
250 * free some of the data from thisentry, i.e., the arrays.
252 FreeIfNeeded(thisentry.tterm.Booleans);
253 FreeIfNeeded(thisentry.tterm.Numbers);
254 FreeIfNeeded(thisentry.tterm.Strings);
255 FreeIfNeeded(thisentry.tterm.str_table);
257 FreeIfNeeded(thisentry.tterm.ext_Names);
258 FreeIfNeeded(thisentry.tterm.ext_str_table);
264 /* set up the head pointer */
265 for (_nc_head = _nc_tail; _nc_head->last; _nc_head = _nc_head->last)
268 DEBUG(2, ("head = %s", _nc_head->tterm.term_names));
269 DEBUG(2, ("tail = %s", _nc_tail->tterm.term_names));
273 DEBUG(2, ("no entries parsed"));
276 _nc_suppress_warnings = oldsuppress;
277 DEBUG(2, (T_RETURN("")));
280 #if 0 && NCURSES_XNAMES
282 find_capname(TERMTYPE2 *p, const char *name)
284 unsigned num_names = NUM_EXT_NAMES(p);
287 for (n = 0; n < num_names; ++n) {
288 if (!strcmp(p->ext_Names[n], name))
298 extended_captype(TERMTYPE2 *p, unsigned which)
302 limit += p->ext_Booleans;
303 if (limit != 0 && which < limit) {
306 limit += p->ext_Numbers;
307 if (limit != 0 && which < limit) {
310 limit += p->ext_Strings;
311 if (limit != 0 && which < limit) {
312 result = ((p->Strings[STRCOUNT + which] != CANCELLED_STRING)
315 } else if (which >= limit) {
324 name_of_captype(int which)
326 const char *result = "?";
341 #define valid_TERMTYPE2(p) \
343 (p)->term_names != 0 && \
347 * Disallow changing the type of an extended capability when doing a "use"
348 * if one or the other is a string.
351 invalid_merge(TERMTYPE2 *to, TERMTYPE2 *from)
354 if (valid_TERMTYPE2(to)
355 && valid_TERMTYPE2(from)) {
356 char *to_name = _nc_first_name(to->term_names);
357 char *from_name = strdup(_nc_first_name(from->term_names));
358 unsigned num_names = NUM_EXT_NAMES(from);
361 for (n = 0; n < num_names; ++n) {
362 const char *capname = from->ext_Names[n];
363 int tt = extended_captype(to, find_capname(to, capname));
364 int tf = extended_captype(from, n);
368 && (tt == STRING) != (tf == STRING)) {
369 if (from_name != 0 && strcmp(to_name, from_name)) {
370 _nc_warning("merge of %s to %s changes type of %s from %s to %s",
375 name_of_captype(tt));
377 _nc_warning("merge of %s changes type of %s from %s to %s",
381 name_of_captype(tt));
390 #define validate_merge(p, q) \
391 if (invalid_merge(&((p)->tterm), &((q)->tterm))) \
394 #define validate_merge(p, q) /* nothing */
398 _nc_resolve_uses2(bool fullresolve, bool literal)
399 /* try to resolve all use capabilities */
401 ENTRY *qp, *rp, *lastread = 0;
404 int unresolved, total_unresolved, multiples;
406 DEBUG(2, (T_CALLED("_nc_resolve_uses2")));
409 * Check for multiple occurrences of the same name.
415 for_entry_list2(rp, qp->next) {
417 && check_collisions(qp->tterm.term_names,
418 rp->tterm.term_names,
421 (void) fprintf(stderr, "\t%s\n", rp->tterm.term_names);
423 (void) fprintf(stderr, "and\t%s\n", qp->tterm.term_names);
424 if (!remove_collision(rp->tterm.term_names,
425 qp->tterm.term_names)) {
432 DEBUG(2, (T_RETURN("false")));
436 DEBUG(2, ("NO MULTIPLE NAME OCCURRENCES"));
439 * First resolution stage: compute link pointers corresponding to names.
441 total_unresolved = 0;
445 for (i = 0; i < qp->nuses; i++) {
447 char *child = _nc_first_name(qp->tterm.term_names);
448 char *lookfor = qp->uses[i].name;
449 long lookline = qp->uses[i].line;
458 /* first, try to resolve from in-core records */
461 && _nc_name_match(rp->tterm.term_names, lookfor, "|")) {
462 DEBUG(2, ("%s: resolving use=%s %p (in core)",
463 child, lookfor, lookfor));
465 qp->uses[i].link = rp;
468 /* verify that there are no earlier uses */
469 for (j = 0; j < i; ++j) {
470 if (qp->uses[j].link != NULL
471 && !strcmp(qp->uses[j].link->tterm.term_names,
472 rp->tterm.term_names)) {
473 _nc_warning("duplicate use=%s", lookfor);
480 /* if that didn't work, try to merge in a compiled entry */
483 char filename[PATH_MAX];
485 memset(&thisterm, 0, sizeof(thisterm));
486 if (_nc_read_entry2(lookfor, filename, &thisterm) == 1) {
487 DEBUG(2, ("%s: resolving use=%s (compiled)",
490 TYPE_MALLOC(ENTRY, 1, rp);
491 rp->tterm = thisterm;
496 qp->uses[i].link = rp;
499 /* verify that there are no earlier uses */
500 for (j = 0; j < i; ++j) {
501 if (qp->uses[j].link != NULL
502 && !strcmp(qp->uses[j].link->tterm.term_names,
503 rp->tterm.term_names)) {
504 _nc_warning("duplicate use=%s", lookfor);
511 /* no good, mark this one unresolvable and complain */
516 _nc_curr_line = (int) lookline;
517 _nc_warning("resolution of use=%s failed", lookfor);
518 qp->uses[i].link = 0;
522 if (total_unresolved) {
523 /* free entries read in off disk */
524 _nc_free_entries(lastread);
525 DEBUG(2, (T_RETURN("false")));
529 DEBUG(2, ("NAME RESOLUTION COMPLETED OK"));
532 * OK, at this point all (char *) references in `name' members
533 * have been successfully converted to (ENTRY *) pointers in
534 * `link' members. Time to do the actual merges.
544 DEBUG(2, ("%s: attempting merge of %d entries",
545 _nc_first_name(qp->tterm.term_names),
548 * If any of the use entries we're looking for is
549 * incomplete, punt. We'll catch this entry on a
552 for (i = 0; i < qp->nuses; i++) {
554 && qp->uses[i].link->nuses) {
555 DEBUG(2, ("%s: use entry %d unresolved",
556 _nc_first_name(qp->tterm.term_names), i));
562 * First, make sure there is no garbage in the
563 * merge block. As a side effect, copy into
564 * the merged entry the name field and string
567 _nc_copy_termtype2(&(merged.tterm), &(qp->tterm));
570 * Now merge in each use entry in the proper
573 for (; qp->nuses; qp->nuses--) {
574 validate_merge(&merged,
575 qp->uses[qp->nuses - 1].link);
576 _nc_merge_entry(&merged,
577 qp->uses[qp->nuses - 1].link);
578 free(qp->uses[qp->nuses - 1].name);
582 * Now merge in the original entry.
584 validate_merge(&merged, qp);
585 _nc_merge_entry(&merged, qp);
588 * Replace the original entry with the merged one.
590 FreeIfNeeded(qp->tterm.Booleans);
591 FreeIfNeeded(qp->tterm.Numbers);
592 FreeIfNeeded(qp->tterm.Strings);
593 FreeIfNeeded(qp->tterm.str_table);
595 FreeIfNeeded(qp->tterm.ext_Names);
596 FreeIfNeeded(qp->tterm.ext_str_table);
598 qp->tterm = merged.tterm;
599 _nc_wrap_entry(qp, TRUE);
602 * We know every entry is resolvable because name resolution
603 * didn't bomb. So go back for another pass.
613 DEBUG(2, ("MERGES COMPLETED OK"));
616 DEBUG(2, ("RESOLUTION FINISHED"));
621 _nc_curr_line = (int) qp->startline;
622 _nc_set_type(_nc_first_name(qp->tterm.term_names));
624 * tic overrides this function pointer to provide more verbose
627 if (_nc_check_termtype2 != sanity_check2) {
628 SCREEN *save_SP = SP;
631 TERMINAL *save_tm = cur_term;
634 * Setup so that tic can use ordinary terminfo interface to
635 * obtain capability information.
637 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);
644 _nc_check_termtype2(&qp->tterm, literal);
647 * Checking calls tparm, which can allocate memory. Fix leaks.
649 #define TPS(name) fake_tm.tparm_state.name
650 FreeAndNull(TPS(out_buff));
651 FreeAndNull(TPS(fmt_buff));
654 _nc_set_screen(save_SP);
655 set_curterm(save_tm);
657 fixup_acsc(&qp->tterm, literal);
660 DEBUG(2, ("SANITY CHECK FINISHED"));
663 DEBUG(2, (T_RETURN("true")));
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.
677 fixup_acsc(TERMTYPE2 *tp, int literal)
680 if (acs_chars == ABSENT_STRING
681 && PRESENT(enter_alt_charset_mode)
682 && PRESENT(exit_alt_charset_mode))
683 acs_chars = strdup(VT_ACSC);
688 sanity_check2(TERMTYPE2 *tp, bool literal)
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);
707 #if defined(enter_italics_mode) && defined(exit_italics_mode)
708 PAIRED(enter_italics_mode, exit_italics_mode);
712 /* we do this check/fix in postprocess_termcap(), but some packagers
713 * prefer to bypass it...
716 fixup_acsc(tp, literal);
717 ANDMISSING(enter_alt_charset_mode, acs_chars);
718 ANDMISSING(exit_alt_charset_mode, acs_chars);
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);
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);
740 #if defined(display_clock) && defined(remove_clock)
741 PAIRED(display_clock, remove_clock);
743 ANDMISSING(set_color_pair, initialize_pair);
750 T((T_CALLED("_nc_leaks_tic()")));
751 _nc_globals.leak_checking = TRUE;
752 _nc_alloc_entry_leaks();
753 _nc_captoinfo_leaks();
754 _nc_comp_scan_leaks();
755 #if BROKEN_LINKER || USE_REENTRANT
759 _nc_tic_expand(0, FALSE, 0);
764 _nc_free_tic(int code)
766 T((T_CALLED("_nc_free_tic(%d)"), code));