1 /****************************************************************************
2 * Copyright (c) 1998-2017,2018 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
36 * comp_parse.c -- parser driver loop and use handling.
38 * Use this code by calling _nc_read_entry_source() on as many source
39 * files as you like (either terminfo or termcap syntax). If you
40 * want use-resolution, call _nc_resolve_uses2(). To free the list
41 * storage, do _nc_free_entries().
44 #include <curses.priv.h>
50 MODULE_ID("$Id: comp_parse.c,v 1.101 2018/02/24 22:33:40 tom Exp $")
52 static void sanity_check2(TERMTYPE2 *, bool);
53 NCURSES_IMPEXP void NCURSES_API(*_nc_check_termtype2) (TERMTYPE2 *, bool) = sanity_check2;
55 static void fixup_acsc(TERMTYPE2 *, int);
59 /* add an entry to the in-core list */
61 ENTRY *newp = _nc_copy_entry(ep);
64 _nc_err_abort(MSG_NO_MEMORY);
66 newp->last = _nc_tail;
71 newp->last->next = newp;
74 #define NAMEBUFFER_SIZE (MAX_NAME_SIZE + 2)
77 force_bar(char *dst, char *src)
79 if (strchr(src, '|') == 0) {
80 size_t len = strlen(src);
81 if (len > MAX_NAME_SIZE)
83 _nc_STRNCPY(dst, src, len);
84 _nc_STRCPY(dst + len, "|", NAMEBUFFER_SIZE - len);
89 #define ForceBar(dst, src) ((strchr(src, '|') == 0) ? force_bar(dst, src) : src)
91 #if NCURSES_USE_TERMCAP && NCURSES_XNAMES
93 skip_index(char *name)
95 char *bar = strchr(name, '|');
97 if (bar != 0 && (bar - name) == 2)
105 check_collisions(char *n1, char *n2, int counter)
107 char *pstart, *qstart, *pend, *qend;
108 char nc1[NAMEBUFFER_SIZE];
109 char nc2[NAMEBUFFER_SIZE];
111 n1 = ForceBar(nc1, n1);
112 n2 = ForceBar(nc2, n2);
114 #if NCURSES_USE_TERMCAP && NCURSES_XNAMES
115 if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
121 for (pstart = n1; (pend = strchr(pstart, '|')); pstart = pend + 1) {
122 for (qstart = n2; (qend = strchr(qstart, '|')); qstart = qend + 1) {
123 if ((pend - pstart == qend - qstart)
124 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0) {
126 (void) fprintf(stderr, "Name collision '%.*s' between\n",
127 (int) (pend - pstart), pstart);
137 next_name(char *name)
145 name_ending(char *name)
150 while (*name != '\0' && *name != '|')
157 * Essentially, find the conflict reported in check_collisions() and remove
158 * it from the second name, unless that happens to be the last alias.
161 remove_collision(char *n1, char *n2)
164 char *pstart, *qstart, *pend, *qend;
165 bool removed = FALSE;
167 #if NCURSES_USE_TERMCAP && NCURSES_XNAMES
168 if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
170 p2 = n2 = skip_index(n2);
174 for (pstart = n1; (pend = name_ending(pstart)); pstart = next_name(pend)) {
175 for (qstart = n2; (qend = name_ending(qstart)); qstart = next_name(qend)) {
176 if ((pend - pstart == qend - qstart)
177 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0) {
178 if (qstart != p2 || *qend == '|') {
181 while ((*qstart++ = *qend++) != '\0') ;
182 fprintf(stderr, "...now\t%s\n", p2);
184 fprintf(stderr, "Cannot remove alias '%.*s'\n",
185 (int) (qend - qstart), qstart);
196 /* do any of the aliases in a pair of terminal names match? */
198 _nc_entry_match(char *n1, char *n2)
200 return check_collisions(n1, n2, 0);
203 /****************************************************************************
205 * Entry compiler and resolution logic
207 ****************************************************************************/
210 _nc_read_entry_source(FILE *fp, char *buf,
211 int literal, bool silent,
212 bool(*hook) (ENTRY *))
213 /* slurp all entries in the given file into core */
216 bool oldsuppress = _nc_suppress_warnings;
220 _nc_suppress_warnings = TRUE; /* shut the lexer up, too */
222 _nc_reset_input(fp, buf);
224 memset(&thisentry, 0, sizeof(thisentry));
225 if (_nc_parse_entry(&thisentry, literal, silent) == ERR)
227 if (!isalnum(UChar(thisentry.tterm.term_names[0])))
228 _nc_err_abort("terminal names must start with letter or digit");
231 * This can be used for immediate compilation of entries with no "use="
232 * references to disk. That avoids consuming a lot of memory when the
233 * resolution code could fetch entries off disk.
235 if (hook != NULLHOOK && (*hook) (&thisentry)) {
240 * The enqueued entry is copied with _nc_copy_termtype(), so we can
241 * free some of the data from thisentry, i.e., the arrays.
243 FreeIfNeeded(thisentry.tterm.Booleans);
244 FreeIfNeeded(thisentry.tterm.Numbers);
245 FreeIfNeeded(thisentry.tterm.Strings);
247 FreeIfNeeded(thisentry.tterm.ext_Names);
253 /* set up the head pointer */
254 for (_nc_head = _nc_tail; _nc_head->last; _nc_head = _nc_head->last)
257 DEBUG(1, ("head = %s", _nc_head->tterm.term_names));
258 DEBUG(1, ("tail = %s", _nc_tail->tterm.term_names));
262 DEBUG(1, ("no entries parsed"));
265 _nc_suppress_warnings = oldsuppress;
270 find_capname(TERMTYPE2 *p, const char *name)
272 unsigned num_names = NUM_EXT_NAMES(p);
274 for (n = 0; n < num_names; ++n) {
275 if (!strcmp(p->ext_Names[n], name))
282 extended_captype(TERMTYPE2 *p, unsigned which)
286 limit += p->ext_Booleans;
287 if (limit != 0 && which < limit) {
290 limit += p->ext_Numbers;
291 if (limit != 0 && which < limit) {
294 limit += p->ext_Strings;
295 if (limit != 0 && which < limit) {
297 } else if (which >= limit) {
306 name_of_captype(int which)
308 const char *result = "?";
324 * Disallow changing the type of an extended capability when doing a "use"
325 * if one or the other is a string.
328 invalid_merge(TERMTYPE2 *to, TERMTYPE2 *from)
331 char *to_name = _nc_first_name(to->term_names);
332 char *from_name = strdup(_nc_first_name(from->term_names));
333 unsigned num_names = NUM_EXT_NAMES(from);
336 for (n = 0; n < num_names; ++n) {
337 const char *capname = from->ext_Names[n];
338 int tt = extended_captype(to, find_capname(to, capname));
339 int tf = extended_captype(from, n);
343 && (tt == STRING) != (tf == STRING)) {
344 if (from_name != 0 && strcmp(to_name, from_name)) {
346 ("merge of %s to %s changes type of %s from %s to %s",
351 name_of_captype(tt)));
353 DEBUG(2, ("merge of %s changes type of %s from %s to %s",
357 name_of_captype(tt)));
359 _nc_warning("merge changes type of %s from %s to %s",
362 name_of_captype(tt));
369 #define validate_merge(p, q) \
370 if (invalid_merge(&((p)->tterm), &((q)->tterm))) \
373 #define validate_merge(p, q) /* nothing */
377 _nc_resolve_uses2(bool fullresolve, bool literal)
378 /* try to resolve all use capabilities */
380 ENTRY *qp, *rp, *lastread = 0;
383 int unresolved, total_unresolved, multiples;
385 DEBUG(2, ("RESOLUTION BEGINNING"));
388 * Check for multiple occurrences of the same name.
396 && check_collisions(qp->tterm.term_names,
397 rp->tterm.term_names,
400 (void) fprintf(stderr, "\t%s\n", rp->tterm.term_names);
402 (void) fprintf(stderr, "and\t%s\n", qp->tterm.term_names);
403 if (!remove_collision(rp->tterm.term_names,
404 qp->tterm.term_names)) {
413 DEBUG(2, ("NO MULTIPLE NAME OCCURRENCES"));
416 * First resolution stage: compute link pointers corresponding to names.
418 total_unresolved = 0;
422 for (i = 0; i < qp->nuses; i++) {
424 char *child = _nc_first_name(qp->tterm.term_names);
425 char *lookfor = qp->uses[i].name;
426 long lookline = qp->uses[i].line;
432 /* first, try to resolve from in-core records */
435 && _nc_name_match(rp->tterm.term_names, lookfor, "|")) {
436 DEBUG(2, ("%s: resolving use=%s (in core)",
439 qp->uses[i].link = rp;
444 /* if that didn't work, try to merge in a compiled entry */
447 char filename[PATH_MAX];
449 memset(&thisterm, 0, sizeof(thisterm));
450 if (_nc_read_entry2(lookfor, filename, &thisterm) == 1) {
451 DEBUG(2, ("%s: resolving use=%s (compiled)",
454 TYPE_MALLOC(ENTRY, 1, rp);
455 rp->tterm = thisterm;
460 qp->uses[i].link = rp;
465 /* no good, mark this one unresolvable and complain */
470 _nc_curr_line = (int) lookline;
471 _nc_warning("resolution of use=%s failed", lookfor);
472 qp->uses[i].link = 0;
476 if (total_unresolved) {
477 /* free entries read in off disk */
478 _nc_free_entries(lastread);
482 DEBUG(2, ("NAME RESOLUTION COMPLETED OK"));
485 * OK, at this point all (char *) references in `name' members
486 * have been successfully converted to (ENTRY *) pointers in
487 * `link' members. Time to do the actual merges.
497 DEBUG(2, ("%s: attempting merge",
498 _nc_first_name(qp->tterm.term_names)));
500 * If any of the use entries we're looking for is
501 * incomplete, punt. We'll catch this entry on a
504 for (i = 0; i < qp->nuses; i++)
505 if (qp->uses[i].link->nuses) {
506 DEBUG(2, ("%s: use entry %d unresolved",
507 _nc_first_name(qp->tterm.term_names), i));
512 * First, make sure there is no garbage in the
513 * merge block. As a side effect, copy into
514 * the merged entry the name field and string
517 _nc_copy_termtype2(&(merged.tterm), &(qp->tterm));
520 * Now merge in each use entry in the proper
523 for (; qp->nuses; qp->nuses--) {
524 validate_merge(&merged,
525 qp->uses[qp->nuses - 1].link);
526 _nc_merge_entry(&merged,
527 qp->uses[qp->nuses - 1].link);
531 * Now merge in the original entry.
533 validate_merge(&merged, qp);
534 _nc_merge_entry(&merged, qp);
537 * Replace the original entry with the merged one.
539 FreeIfNeeded(qp->tterm.Booleans);
540 FreeIfNeeded(qp->tterm.Numbers);
541 FreeIfNeeded(qp->tterm.Strings);
543 FreeIfNeeded(qp->tterm.ext_Names);
545 qp->tterm = merged.tterm;
546 _nc_wrap_entry(qp, TRUE);
549 * We know every entry is resolvable because name resolution
550 * didn't bomb. So go back for another pass.
560 DEBUG(2, ("MERGES COMPLETED OK"));
564 * We'd like to free entries read in off disk at this point, but can't.
565 * The merge_entry() code doesn't copy the strings in the use entries,
566 * it just aliases them. If this ever changes, do a
567 * free_entries(lastread) here.
570 DEBUG(2, ("RESOLUTION FINISHED"));
575 _nc_curr_line = (int) qp->startline;
576 _nc_set_type(_nc_first_name(qp->tterm.term_names));
578 * tic overrides this function pointer to provide more verbose
581 if (_nc_check_termtype2 != sanity_check2) {
582 SCREEN *save_SP = SP;
585 TERMINAL *save_tm = cur_term;
588 * Setup so that tic can use ordinary terminfo interface to
589 * obtain capability information.
591 memset(&fake_sp, 0, sizeof(fake_sp));
592 memset(&fake_tm, 0, sizeof(fake_tm));
593 fake_sp._term = &fake_tm;
594 TerminalType(&fake_tm) = qp->tterm;
595 _nc_set_screen(&fake_sp);
596 set_curterm(&fake_tm);
598 _nc_check_termtype2(&qp->tterm, literal);
600 _nc_set_screen(save_SP);
601 set_curterm(save_tm);
603 fixup_acsc(&qp->tterm, literal);
606 DEBUG(2, ("SANITY CHECK FINISHED"));
613 * This bit of legerdemain turns all the terminfo variable names into
614 * references to locations in the arrays Booleans, Numbers, and Strings ---
615 * precisely what's needed.
622 fixup_acsc(TERMTYPE2 *tp, int literal)
625 if (acs_chars == ABSENT_STRING
626 && PRESENT(enter_alt_charset_mode)
627 && PRESENT(exit_alt_charset_mode))
628 acs_chars = strdup(VT_ACSC);
633 sanity_check2(TERMTYPE2 *tp, bool literal)
635 if (!PRESENT(exit_attribute_mode)) {
636 #ifdef __UNUSED__ /* this casts too wide a net */
637 bool terminal_entry = !strchr(tp->term_names, '+');
638 if (terminal_entry &&
639 (PRESENT(set_attributes)
640 || PRESENT(enter_standout_mode)
641 || PRESENT(enter_underline_mode)
642 || PRESENT(enter_blink_mode)
643 || PRESENT(enter_bold_mode)
644 || PRESENT(enter_dim_mode)
645 || PRESENT(enter_secure_mode)
646 || PRESENT(enter_protected_mode)
647 || PRESENT(enter_reverse_mode)))
648 _nc_warning("no exit_attribute_mode");
649 #endif /* __UNUSED__ */
650 PAIRED(enter_standout_mode, exit_standout_mode);
651 PAIRED(enter_underline_mode, exit_underline_mode);
652 #if defined(enter_italics_mode) && defined(exit_italics_mode)
653 PAIRED(enter_italics_mode, exit_italics_mode);
657 /* we do this check/fix in postprocess_termcap(), but some packagers
658 * prefer to bypass it...
661 fixup_acsc(tp, literal);
662 ANDMISSING(enter_alt_charset_mode, acs_chars);
663 ANDMISSING(exit_alt_charset_mode, acs_chars);
666 /* listed in structure-member order of first argument */
667 PAIRED(enter_alt_charset_mode, exit_alt_charset_mode);
668 ANDMISSING(enter_blink_mode, exit_attribute_mode);
669 ANDMISSING(enter_bold_mode, exit_attribute_mode);
670 PAIRED(exit_ca_mode, enter_ca_mode);
671 PAIRED(enter_delete_mode, exit_delete_mode);
672 ANDMISSING(enter_dim_mode, exit_attribute_mode);
673 PAIRED(enter_insert_mode, exit_insert_mode);
674 ANDMISSING(enter_secure_mode, exit_attribute_mode);
675 ANDMISSING(enter_protected_mode, exit_attribute_mode);
676 ANDMISSING(enter_reverse_mode, exit_attribute_mode);
677 PAIRED(from_status_line, to_status_line);
678 PAIRED(meta_off, meta_on);
680 PAIRED(prtr_on, prtr_off);
681 PAIRED(save_cursor, restore_cursor);
682 PAIRED(enter_xon_mode, exit_xon_mode);
683 PAIRED(enter_am_mode, exit_am_mode);
684 ANDMISSING(label_off, label_on);
685 #if defined(display_clock) && defined(remove_clock)
686 PAIRED(display_clock, remove_clock);
688 ANDMISSING(set_color_pair, initialize_pair);
695 T((T_CALLED("_nc_free_tic()")));
696 _nc_globals.leak_checking = TRUE;
697 _nc_alloc_entry_leaks();
698 _nc_captoinfo_leaks();
699 _nc_comp_scan_leaks();
700 #if BROKEN_LINKER || USE_REENTRANT
704 _nc_tic_expand(0, FALSE, 0);
708 _nc_free_tic(int code)
711 _nc_free_tinfo(code);