1 /****************************************************************************
2 * Copyright (c) 1998-2011,2012 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.85 2012/04/14 23:30:10 tom Exp $")
52 static void sanity_check2(TERMTYPE *, bool);
53 NCURSES_IMPEXP void NCURSES_API(*_nc_check_termtype2) (TERMTYPE *, bool) = sanity_check2;
55 /* obsolete: 20040705 */
56 static void sanity_check(TERMTYPE *);
57 NCURSES_IMPEXP void NCURSES_API(*_nc_check_termtype) (TERMTYPE *) = sanity_check;
59 static void fixup_acsc(TERMTYPE *, int);
63 /* add an entry to the in-core list */
65 ENTRY *newp = _nc_copy_entry(ep);
68 _nc_err_abort(MSG_NO_MEMORY);
70 newp->last = _nc_tail;
75 newp->last->next = newp;
79 force_bar(char *dst, char *src)
81 if (strchr(src, '|') == 0) {
82 size_t len = strlen(src);
83 if (len > MAX_NAME_SIZE)
85 (void) strncpy(dst, src, len);
86 _nc_STRCPY(dst + len, "|", MAX_NAME_SIZE);
91 #define ForceBar(dst, src) ((strchr(src, '|') == 0) ? force_bar(dst, src) : src)
93 #if USE_TERMCAP && NCURSES_XNAMES
95 skip_index(char *name)
97 char *bar = strchr(name, '|');
99 if (bar != 0 && (bar - name) == 2)
107 check_collisions(char *n1, char *n2, int counter)
109 char *pstart, *qstart, *pend, *qend;
110 char nc1[MAX_NAME_SIZE + 2];
111 char nc2[MAX_NAME_SIZE + 2];
113 n1 = ForceBar(nc1, n1);
114 n2 = ForceBar(nc2, n2);
116 #if USE_TERMCAP && NCURSES_XNAMES
117 if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
123 for (pstart = n1; (pend = strchr(pstart, '|')); pstart = pend + 1) {
124 for (qstart = n2; (qend = strchr(qstart, '|')); qstart = qend + 1) {
125 if ((pend - pstart == qend - qstart)
126 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0) {
128 (void) fprintf(stderr, "Name collision '%.*s' between\n",
129 (int) (pend - pstart), pstart);
139 next_name(char *name)
147 name_ending(char *name)
152 while (*name != '\0' && *name != '|')
159 * Essentially, find the conflict reported in check_collisions() and remove
160 * it from the second name, unless that happens to be the last alias.
163 remove_collision(char *n1, char *n2)
167 char *pstart, *qstart, *pend, *qend;
168 bool removed = FALSE;
170 #if USE_TERMCAP && NCURSES_XNAMES
171 if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
172 p1 = n1 = skip_index(n1);
173 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);
189 fprintf(stderr, "Cannot remove alias '%.*s'\n",
190 (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 _nc_suppress_warnings = TRUE; /* shut the lexer up, too */
227 _nc_reset_input(fp, buf);
229 memset(&thisentry, 0, sizeof(thisentry));
230 if (_nc_parse_entry(&thisentry, literal, silent) == ERR)
232 if (!isalnum(UChar(thisentry.tterm.term_names[0])))
233 _nc_err_abort("terminal names must start with letter or digit");
236 * This can be used for immediate compilation of entries with no "use="
237 * references to disk. That avoids consuming a lot of memory when the
238 * resolution code could fetch entries off disk.
240 if (hook != NULLHOOK && (*hook) (&thisentry)) {
245 * The enqueued entry is copied with _nc_copy_termtype(), so we can
246 * free some of the data from thisentry, i.e., the arrays.
248 FreeIfNeeded(thisentry.tterm.Booleans);
249 FreeIfNeeded(thisentry.tterm.Numbers);
250 FreeIfNeeded(thisentry.tterm.Strings);
252 FreeIfNeeded(thisentry.tterm.ext_Names);
258 /* set up the head pointer */
259 for (_nc_head = _nc_tail; _nc_head->last; _nc_head = _nc_head->last)
262 DEBUG(1, ("head = %s", _nc_head->tterm.term_names));
263 DEBUG(1, ("tail = %s", _nc_tail->tterm.term_names));
267 DEBUG(1, ("no entries parsed"));
270 _nc_suppress_warnings = oldsuppress;
274 _nc_resolve_uses2(bool fullresolve, bool literal)
275 /* try to resolve all use capabilities */
277 ENTRY *qp, *rp, *lastread = 0;
280 int unresolved, total_unresolved, multiples;
282 DEBUG(2, ("RESOLUTION BEGINNING"));
285 * Check for multiple occurrences of the same name.
293 && check_collisions(qp->tterm.term_names,
294 rp->tterm.term_names,
297 (void) fprintf(stderr, "\t%s\n", rp->tterm.term_names);
299 (void) fprintf(stderr, "and\t%s\n", qp->tterm.term_names);
300 if (!remove_collision(rp->tterm.term_names,
301 qp->tterm.term_names)) {
310 DEBUG(2, ("NO MULTIPLE NAME OCCURRENCES"));
313 * First resolution stage: compute link pointers corresponding to names.
315 total_unresolved = 0;
319 for (i = 0; i < qp->nuses; i++) {
321 char *child = _nc_first_name(qp->tterm.term_names);
322 char *lookfor = qp->uses[i].name;
323 long lookline = qp->uses[i].line;
329 /* first, try to resolve from in-core records */
332 && _nc_name_match(rp->tterm.term_names, lookfor, "|")) {
333 DEBUG(2, ("%s: resolving use=%s (in core)",
336 qp->uses[i].link = rp;
341 /* if that didn't work, try to merge in a compiled entry */
344 char filename[PATH_MAX];
346 memset(&thisterm, 0, sizeof(thisterm));
347 if (_nc_read_entry(lookfor, filename, &thisterm) == 1) {
348 DEBUG(2, ("%s: resolving use=%s (compiled)",
351 rp = typeMalloc(ENTRY, 1);
353 _nc_err_abort(MSG_NO_MEMORY);
354 rp->tterm = thisterm;
359 qp->uses[i].link = rp;
364 /* no good, mark this one unresolvable and complain */
369 _nc_curr_line = (int) lookline;
370 _nc_warning("resolution of use=%s failed", lookfor);
371 qp->uses[i].link = 0;
375 if (total_unresolved) {
376 /* free entries read in off disk */
377 _nc_free_entries(lastread);
381 DEBUG(2, ("NAME RESOLUTION COMPLETED OK"));
384 * OK, at this point all (char *) references in `name' members
385 * have been successfully converted to (ENTRY *) pointers in
386 * `link' members. Time to do the actual merges.
396 DEBUG(2, ("%s: attempting merge",
397 _nc_first_name(qp->tterm.term_names)));
399 * If any of the use entries we're looking for is
400 * incomplete, punt. We'll catch this entry on a
403 for (i = 0; i < qp->nuses; i++)
404 if (qp->uses[i].link->nuses) {
405 DEBUG(2, ("%s: use entry %d unresolved",
406 _nc_first_name(qp->tterm.term_names), i));
411 * First, make sure there is no garbage in the
412 * merge block. As a side effect, copy into
413 * the merged entry the name field and string
416 _nc_copy_termtype(&merged, &(qp->tterm));
419 * Now merge in each use entry in the proper
422 for (; qp->nuses; qp->nuses--)
423 _nc_merge_entry(&merged,
424 &qp->uses[qp->nuses - 1].link->tterm);
427 * Now merge in the original entry.
429 _nc_merge_entry(&merged, &qp->tterm);
432 * Replace the original entry with the merged one.
434 FreeIfNeeded(qp->tterm.Booleans);
435 FreeIfNeeded(qp->tterm.Numbers);
436 FreeIfNeeded(qp->tterm.Strings);
438 FreeIfNeeded(qp->tterm.ext_Names);
441 _nc_wrap_entry(qp, TRUE);
444 * We know every entry is resolvable because name resolution
445 * didn't bomb. So go back for another pass.
455 DEBUG(2, ("MERGES COMPLETED OK"));
459 * We'd like to free entries read in off disk at this point, but can't.
460 * The merge_entry() code doesn't copy the strings in the use entries,
461 * it just aliases them. If this ever changes, do a
462 * free_entries(lastread) here.
465 DEBUG(2, ("RESOLUTION FINISHED"));
468 if (_nc_check_termtype != 0) {
471 _nc_curr_line = (int) qp->startline;
472 _nc_set_type(_nc_first_name(qp->tterm.term_names));
474 * tic overrides this function pointer to provide more verbose
477 if (_nc_check_termtype2 != sanity_check2) {
478 SCREEN *save_SP = SP;
481 TERMINAL *save_tm = cur_term;
484 * Setup so that tic can use ordinary terminfo interface
485 * to obtain capability information.
487 memset(&fake_sp, 0, sizeof(fake_sp));
488 memset(&fake_tm, 0, sizeof(fake_tm));
489 fake_sp._term = &fake_tm;
490 fake_tm.type = qp->tterm;
492 set_curterm(&fake_tm);
494 _nc_check_termtype2(&qp->tterm, literal);
497 set_curterm(save_tm);
499 fixup_acsc(&qp->tterm, literal);
502 DEBUG(2, ("SANITY CHECK FINISHED"));
508 /* obsolete: 20040705 */
510 _nc_resolve_uses(bool fullresolve)
512 return _nc_resolve_uses2(fullresolve, FALSE);
516 * This bit of legerdemain turns all the terminfo variable names into
517 * references to locations in the arrays Booleans, Numbers, and Strings ---
518 * precisely what's needed.
525 fixup_acsc(TERMTYPE *tp, int literal)
529 && enter_alt_charset_mode != 0
530 && exit_alt_charset_mode != 0)
531 acs_chars = strdup(VT_ACSC);
536 sanity_check2(TERMTYPE *tp, bool literal)
538 if (!PRESENT(exit_attribute_mode)) {
539 #ifdef __UNUSED__ /* this casts too wide a net */
540 bool terminal_entry = !strchr(tp->term_names, '+');
541 if (terminal_entry &&
542 (PRESENT(set_attributes)
543 || PRESENT(enter_standout_mode)
544 || PRESENT(enter_underline_mode)
545 || PRESENT(enter_blink_mode)
546 || PRESENT(enter_bold_mode)
547 || PRESENT(enter_dim_mode)
548 || PRESENT(enter_secure_mode)
549 || PRESENT(enter_protected_mode)
550 || PRESENT(enter_reverse_mode)))
551 _nc_warning("no exit_attribute_mode");
552 #endif /* __UNUSED__ */
553 PAIRED(enter_standout_mode, exit_standout_mode);
554 PAIRED(enter_underline_mode, exit_underline_mode);
557 /* we do this check/fix in postprocess_termcap(), but some packagers
558 * prefer to bypass it...
561 fixup_acsc(tp, literal);
562 ANDMISSING(enter_alt_charset_mode, acs_chars);
563 ANDMISSING(exit_alt_charset_mode, acs_chars);
566 /* listed in structure-member order of first argument */
567 PAIRED(enter_alt_charset_mode, exit_alt_charset_mode);
568 ANDMISSING(enter_blink_mode, exit_attribute_mode);
569 ANDMISSING(enter_bold_mode, exit_attribute_mode);
570 PAIRED(exit_ca_mode, enter_ca_mode);
571 PAIRED(enter_delete_mode, exit_delete_mode);
572 ANDMISSING(enter_dim_mode, exit_attribute_mode);
573 PAIRED(enter_insert_mode, exit_insert_mode);
574 ANDMISSING(enter_secure_mode, exit_attribute_mode);
575 ANDMISSING(enter_protected_mode, exit_attribute_mode);
576 ANDMISSING(enter_reverse_mode, exit_attribute_mode);
577 PAIRED(from_status_line, to_status_line);
578 PAIRED(meta_off, meta_on);
580 PAIRED(prtr_on, prtr_off);
581 PAIRED(save_cursor, restore_cursor);
582 PAIRED(enter_xon_mode, exit_xon_mode);
583 PAIRED(enter_am_mode, exit_am_mode);
584 ANDMISSING(label_off, label_on);
586 PAIRED(display_clock, remove_clock);
588 ANDMISSING(set_color_pair, initialize_pair);
591 /* obsolete: 20040705 */
593 sanity_check(TERMTYPE *tp)
595 sanity_check2(tp, FALSE);
602 _nc_alloc_entry_leaks();
603 _nc_captoinfo_leaks();
604 _nc_comp_scan_leaks();
605 #if BROKEN_LINKER || USE_REENTRANT
609 _nc_tic_expand(0, FALSE, 0);
613 _nc_free_tic(int code)
616 _nc_free_tinfo(code);