1 /****************************************************************************
2 * Copyright 2018-2020,2021 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.112 2021/02/27 21:01:21 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 */
62 ENTRY *newp = _nc_copy_entry(ep);
65 _nc_err_abort(MSG_NO_MEMORY);
67 newp->last = _nc_tail;
72 newp->last->next = newp;
75 #define NAMEBUFFER_SIZE (MAX_NAME_SIZE + 2)
78 force_bar(char *dst, char *src)
80 if (strchr(src, '|') == 0) {
81 size_t len = strlen(src);
82 if (len > MAX_NAME_SIZE)
84 _nc_STRNCPY(dst, src, MAX_NAME_SIZE);
85 _nc_STRCPY(dst + len, "|", NAMEBUFFER_SIZE - len);
90 #define ForceBar(dst, src) ((strchr(src, '|') == 0) ? force_bar(dst, src) : src)
92 #if NCURSES_USE_TERMCAP && NCURSES_XNAMES
94 skip_index(char *name)
96 char *bar = strchr(name, '|');
98 if (bar != 0 && (bar - name) == 2)
106 check_collisions(char *n1, char *n2, int counter)
108 char *pstart, *qstart, *pend, *qend;
109 char nc1[NAMEBUFFER_SIZE];
110 char nc2[NAMEBUFFER_SIZE];
112 n1 = ForceBar(nc1, n1);
113 n2 = ForceBar(nc2, n2);
115 #if NCURSES_USE_TERMCAP && NCURSES_XNAMES
116 if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
122 for (pstart = n1; (pend = strchr(pstart, '|')); pstart = pend + 1) {
123 for (qstart = n2; (qend = strchr(qstart, '|')); qstart = qend + 1) {
124 if ((pend - pstart == qend - qstart)
125 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0) {
127 (void) fprintf(stderr, "Name collision '%.*s' between\n",
128 (int) (pend - pstart), pstart);
138 next_name(char *name)
146 name_ending(char *name)
151 while (*name != '\0' && *name != '|')
158 * Essentially, find the conflict reported in check_collisions() and remove
159 * it from the second name, unless that happens to be the last alias.
162 remove_collision(char *n1, char *n2)
165 char *pstart, *qstart, *pend, *qend;
166 bool removed = FALSE;
168 #if NCURSES_USE_TERMCAP && NCURSES_XNAMES
169 if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
171 p2 = n2 = skip_index(n2);
175 for (pstart = n1; (pend = name_ending(pstart)); pstart = next_name(pend)) {
176 for (qstart = n2; (qend = name_ending(qstart)); qstart = next_name(qend)) {
177 if ((pend - pstart == qend - qstart)
178 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0) {
179 if (qstart != p2 || *qend == '|') {
182 while ((*qstart++ = *qend++) != '\0') ;
183 fprintf(stderr, "...now\t%s\n", p2);
186 fprintf(stderr, "Cannot remove alias '%.*s'\n",
187 (int) (qend - qstart), qstart);
197 /* do any of the aliases in a pair of terminal names match? */
199 _nc_entry_match(char *n1, char *n2)
201 return check_collisions(n1, n2, 0);
204 /****************************************************************************
206 * Entry compiler and resolution logic
208 ****************************************************************************/
211 _nc_read_entry_source(FILE *fp, char *buf,
212 int literal, bool silent,
213 bool(*hook) (ENTRY *))
214 /* slurp all entries in the given file into core */
217 bool oldsuppress = _nc_suppress_warnings;
221 _nc_suppress_warnings = TRUE; /* shut the lexer up, too */
223 _nc_reset_input(fp, buf);
225 memset(&thisentry, 0, sizeof(thisentry));
226 if (_nc_parse_entry(&thisentry, literal, silent) == ERR)
228 if (!isalnum(UChar(thisentry.tterm.term_names[0])))
229 _nc_err_abort("terminal names must start with letter or digit");
232 * This can be used for immediate compilation of entries with no "use="
233 * references to disk. That avoids consuming a lot of memory when the
234 * resolution code could fetch entries off disk.
236 if (hook != NULLHOOK && (*hook) (&thisentry)) {
241 * The enqueued entry is copied with _nc_copy_termtype(), so we can
242 * free some of the data from thisentry, i.e., the arrays.
244 FreeIfNeeded(thisentry.tterm.Booleans);
245 FreeIfNeeded(thisentry.tterm.Numbers);
246 FreeIfNeeded(thisentry.tterm.Strings);
248 FreeIfNeeded(thisentry.tterm.ext_Names);
254 /* set up the head pointer */
255 for (_nc_head = _nc_tail; _nc_head->last; _nc_head = _nc_head->last)
258 DEBUG(1, ("head = %s", _nc_head->tterm.term_names));
259 DEBUG(1, ("tail = %s", _nc_tail->tterm.term_names));
263 DEBUG(1, ("no entries parsed"));
266 _nc_suppress_warnings = oldsuppress;
271 find_capname(TERMTYPE2 *p, const char *name)
273 unsigned num_names = NUM_EXT_NAMES(p);
276 for (n = 0; n < num_names; ++n) {
277 if (!strcmp(p->ext_Names[n], name))
287 extended_captype(TERMTYPE2 *p, unsigned which)
291 limit += p->ext_Booleans;
292 if (limit != 0 && which < limit) {
295 limit += p->ext_Numbers;
296 if (limit != 0 && which < limit) {
299 limit += p->ext_Strings;
300 if (limit != 0 && which < limit) {
302 } else if (which >= limit) {
311 name_of_captype(int which)
313 const char *result = "?";
328 #define valid_TERMTYPE2(p) \
330 (p)->term_names != 0 && \
334 * Disallow changing the type of an extended capability when doing a "use"
335 * if one or the other is a string.
338 invalid_merge(TERMTYPE2 *to, TERMTYPE2 *from)
341 if (valid_TERMTYPE2(to)
342 && valid_TERMTYPE2(from)) {
343 char *to_name = _nc_first_name(to->term_names);
344 char *from_name = strdup(_nc_first_name(from->term_names));
345 unsigned num_names = NUM_EXT_NAMES(from);
348 for (n = 0; n < num_names; ++n) {
349 const char *capname = from->ext_Names[n];
350 int tt = extended_captype(to, find_capname(to, capname));
351 int tf = extended_captype(from, n);
355 && (tt == STRING) != (tf == STRING)) {
356 if (from_name != 0 && strcmp(to_name, from_name)) {
358 ("merge of %s to %s changes type of %s from %s to %s",
363 name_of_captype(tt)));
365 DEBUG(2, ("merge of %s changes type of %s from %s to %s",
369 name_of_captype(tt)));
371 _nc_warning("merge changes type of %s from %s to %s",
374 name_of_captype(tt));
382 #define validate_merge(p, q) \
383 if (invalid_merge(&((p)->tterm), &((q)->tterm))) \
386 #define validate_merge(p, q) /* nothing */
390 _nc_resolve_uses2(bool fullresolve, bool literal)
391 /* try to resolve all use capabilities */
393 ENTRY *qp, *rp, *lastread = 0;
396 int unresolved, total_unresolved, multiples;
398 DEBUG(2, ("RESOLUTION BEGINNING"));
401 * Check for multiple occurrences of the same name.
409 && check_collisions(qp->tterm.term_names,
410 rp->tterm.term_names,
413 (void) fprintf(stderr, "\t%s\n", rp->tterm.term_names);
415 (void) fprintf(stderr, "and\t%s\n", qp->tterm.term_names);
416 if (!remove_collision(rp->tterm.term_names,
417 qp->tterm.term_names)) {
426 DEBUG(2, ("NO MULTIPLE NAME OCCURRENCES"));
429 * First resolution stage: compute link pointers corresponding to names.
431 total_unresolved = 0;
435 for (i = 0; i < qp->nuses; i++) {
437 char *child = _nc_first_name(qp->tterm.term_names);
438 char *lookfor = qp->uses[i].name;
439 long lookline = qp->uses[i].line;
448 /* first, try to resolve from in-core records */
451 && _nc_name_match(rp->tterm.term_names, lookfor, "|")) {
452 DEBUG(2, ("%s: resolving use=%s (in core)",
455 qp->uses[i].link = rp;
458 /* verify that there are no earlier uses */
459 for (j = 0; j < i; ++j) {
460 if (!strcmp(qp->uses[j].link->tterm.term_names,
461 rp->tterm.term_names)) {
462 _nc_warning("duplicate use=%s", lookfor);
469 /* if that didn't work, try to merge in a compiled entry */
472 char filename[PATH_MAX];
474 memset(&thisterm, 0, sizeof(thisterm));
475 if (_nc_read_entry2(lookfor, filename, &thisterm) == 1) {
476 DEBUG(2, ("%s: resolving use=%s (compiled)",
479 TYPE_MALLOC(ENTRY, 1, rp);
480 rp->tterm = thisterm;
485 qp->uses[i].link = rp;
488 /* verify that there are no earlier uses */
489 for (j = 0; j < i; ++j) {
490 if (!strcmp(qp->uses[j].link->tterm.term_names,
491 rp->tterm.term_names)) {
492 _nc_warning("duplicate use=%s", lookfor);
499 /* no good, mark this one unresolvable and complain */
504 _nc_curr_line = (int) lookline;
505 _nc_warning("resolution of use=%s failed", lookfor);
506 qp->uses[i].link = 0;
510 if (total_unresolved) {
511 /* free entries read in off disk */
512 _nc_free_entries(lastread);
516 DEBUG(2, ("NAME RESOLUTION COMPLETED OK"));
519 * OK, at this point all (char *) references in `name' members
520 * have been successfully converted to (ENTRY *) pointers in
521 * `link' members. Time to do the actual merges.
531 DEBUG(2, ("%s: attempting merge",
532 _nc_first_name(qp->tterm.term_names)));
534 * If any of the use entries we're looking for is
535 * incomplete, punt. We'll catch this entry on a
538 for (i = 0; i < qp->nuses; i++)
540 && qp->uses[i].link->nuses) {
541 DEBUG(2, ("%s: use entry %d unresolved",
542 _nc_first_name(qp->tterm.term_names), i));
547 * First, make sure there is no garbage in the
548 * merge block. As a side effect, copy into
549 * the merged entry the name field and string
552 _nc_copy_termtype2(&(merged.tterm), &(qp->tterm));
555 * Now merge in each use entry in the proper
558 for (; qp->nuses; qp->nuses--) {
559 validate_merge(&merged,
560 qp->uses[qp->nuses - 1].link);
561 _nc_merge_entry(&merged,
562 qp->uses[qp->nuses - 1].link);
566 * Now merge in the original entry.
568 validate_merge(&merged, qp);
569 _nc_merge_entry(&merged, qp);
572 * Replace the original entry with the merged one.
574 FreeIfNeeded(qp->tterm.Booleans);
575 FreeIfNeeded(qp->tterm.Numbers);
576 FreeIfNeeded(qp->tterm.Strings);
578 FreeIfNeeded(qp->tterm.ext_Names);
580 qp->tterm = merged.tterm;
581 _nc_wrap_entry(qp, TRUE);
584 * We know every entry is resolvable because name resolution
585 * didn't bomb. So go back for another pass.
595 DEBUG(2, ("MERGES COMPLETED OK"));
599 * We'd like to free entries read in off disk at this point, but can't.
600 * The merge_entry() code doesn't copy the strings in the use entries,
601 * it just aliases them. If this ever changes, do a
602 * free_entries(lastread) here.
605 DEBUG(2, ("RESOLUTION FINISHED"));
610 _nc_curr_line = (int) qp->startline;
611 _nc_set_type(_nc_first_name(qp->tterm.term_names));
613 * tic overrides this function pointer to provide more verbose
616 if (_nc_check_termtype2 != sanity_check2) {
617 SCREEN *save_SP = SP;
620 TERMINAL *save_tm = cur_term;
623 * Setup so that tic can use ordinary terminfo interface to
624 * obtain capability information.
626 memset(&fake_sp, 0, sizeof(fake_sp));
627 memset(&fake_tm, 0, sizeof(fake_tm));
628 fake_sp._term = &fake_tm;
629 TerminalType(&fake_tm) = qp->tterm;
630 _nc_set_screen(&fake_sp);
631 set_curterm(&fake_tm);
633 _nc_check_termtype2(&qp->tterm, literal);
635 _nc_set_screen(save_SP);
636 set_curterm(save_tm);
638 fixup_acsc(&qp->tterm, literal);
641 DEBUG(2, ("SANITY CHECK FINISHED"));
648 * This bit of legerdemain turns all the terminfo variable names into
649 * references to locations in the arrays Booleans, Numbers, and Strings ---
650 * precisely what's needed.
657 fixup_acsc(TERMTYPE2 *tp, int literal)
660 if (acs_chars == ABSENT_STRING
661 && PRESENT(enter_alt_charset_mode)
662 && PRESENT(exit_alt_charset_mode))
663 acs_chars = strdup(VT_ACSC);
668 sanity_check2(TERMTYPE2 *tp, bool literal)
670 if (!PRESENT(exit_attribute_mode)) {
671 #ifdef __UNUSED__ /* this casts too wide a net */
672 bool terminal_entry = !strchr(tp->term_names, '+');
673 if (terminal_entry &&
674 (PRESENT(set_attributes)
675 || PRESENT(enter_standout_mode)
676 || PRESENT(enter_underline_mode)
677 || PRESENT(enter_blink_mode)
678 || PRESENT(enter_bold_mode)
679 || PRESENT(enter_dim_mode)
680 || PRESENT(enter_secure_mode)
681 || PRESENT(enter_protected_mode)
682 || PRESENT(enter_reverse_mode)))
683 _nc_warning("no exit_attribute_mode");
684 #endif /* __UNUSED__ */
685 PAIRED(enter_standout_mode, exit_standout_mode);
686 PAIRED(enter_underline_mode, exit_underline_mode);
687 #if defined(enter_italics_mode) && defined(exit_italics_mode)
688 PAIRED(enter_italics_mode, exit_italics_mode);
692 /* we do this check/fix in postprocess_termcap(), but some packagers
693 * prefer to bypass it...
696 fixup_acsc(tp, literal);
697 ANDMISSING(enter_alt_charset_mode, acs_chars);
698 ANDMISSING(exit_alt_charset_mode, acs_chars);
701 /* listed in structure-member order of first argument */
702 PAIRED(enter_alt_charset_mode, exit_alt_charset_mode);
703 ANDMISSING(enter_blink_mode, exit_attribute_mode);
704 ANDMISSING(enter_bold_mode, exit_attribute_mode);
705 PAIRED(exit_ca_mode, enter_ca_mode);
706 PAIRED(enter_delete_mode, exit_delete_mode);
707 ANDMISSING(enter_dim_mode, exit_attribute_mode);
708 PAIRED(enter_insert_mode, exit_insert_mode);
709 ANDMISSING(enter_secure_mode, exit_attribute_mode);
710 ANDMISSING(enter_protected_mode, exit_attribute_mode);
711 ANDMISSING(enter_reverse_mode, exit_attribute_mode);
712 PAIRED(from_status_line, to_status_line);
713 PAIRED(meta_off, meta_on);
715 PAIRED(prtr_on, prtr_off);
716 PAIRED(save_cursor, restore_cursor);
717 PAIRED(enter_xon_mode, exit_xon_mode);
718 PAIRED(enter_am_mode, exit_am_mode);
719 ANDMISSING(label_off, label_on);
720 #if defined(display_clock) && defined(remove_clock)
721 PAIRED(display_clock, remove_clock);
723 ANDMISSING(set_color_pair, initialize_pair);
730 T((T_CALLED("_nc_free_tic()")));
731 _nc_globals.leak_checking = TRUE;
732 _nc_alloc_entry_leaks();
733 _nc_captoinfo_leaks();
734 _nc_comp_scan_leaks();
735 #if BROKEN_LINKER || USE_REENTRANT
739 _nc_tic_expand(0, FALSE, 0);
743 _nc_free_tic(int code)