1 /****************************************************************************
2 * Copyright (c) 1998-2010,2011 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.74 2011/07/25 22:46:47 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 *, bool);
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 (void) strcpy(dst + len, "|");
91 #define ForceBar(dst, src) ((strchr(src, '|') == 0) ? force_bar(dst, src) : src)
94 _nc_entry_match(char *n1, char *n2)
95 /* do any of the aliases in a pair of terminal names match? */
97 char *pstart, *qstart, *pend, *qend;
98 char nc1[MAX_NAME_SIZE + 2];
99 char nc2[MAX_NAME_SIZE + 2];
101 n1 = ForceBar(nc1, n1);
102 n2 = ForceBar(nc2, n2);
104 for (pstart = n1; (pend = strchr(pstart, '|')); pstart = pend + 1)
105 for (qstart = n2; (qend = strchr(qstart, '|')); qstart = qend + 1)
106 if ((pend - pstart == qend - qstart)
107 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0)
113 /****************************************************************************
115 * Entry compiler and resolution logic
117 ****************************************************************************/
120 _nc_read_entry_source(FILE *fp, char *buf,
121 int literal, bool silent,
122 bool(*hook) (ENTRY *))
123 /* slurp all entries in the given file into core */
126 bool oldsuppress = _nc_suppress_warnings;
130 _nc_suppress_warnings = TRUE; /* shut the lexer up, too */
132 _nc_reset_input(fp, buf);
134 memset(&thisentry, 0, sizeof(thisentry));
135 if (_nc_parse_entry(&thisentry, literal, silent) == ERR)
137 if (!isalnum(UChar(thisentry.tterm.term_names[0])))
138 _nc_err_abort("terminal names must start with letter or digit");
141 * This can be used for immediate compilation of entries with no "use="
142 * references to disk. That avoids consuming a lot of memory when the
143 * resolution code could fetch entries off disk.
145 if (hook != NULLHOOK && (*hook) (&thisentry)) {
150 * The enqueued entry is copied with _nc_copy_termtype(), so we can
151 * free some of the data from thisentry, i.e., the arrays.
153 FreeIfNeeded(thisentry.tterm.Booleans);
154 FreeIfNeeded(thisentry.tterm.Numbers);
155 FreeIfNeeded(thisentry.tterm.Strings);
157 FreeIfNeeded(thisentry.tterm.ext_Names);
163 /* set up the head pointer */
164 for (_nc_head = _nc_tail; _nc_head->last; _nc_head = _nc_head->last)
167 DEBUG(1, ("head = %s", _nc_head->tterm.term_names));
168 DEBUG(1, ("tail = %s", _nc_tail->tterm.term_names));
172 DEBUG(1, ("no entries parsed"));
175 _nc_suppress_warnings = oldsuppress;
179 _nc_resolve_uses2(bool fullresolve, bool literal)
180 /* try to resolve all use capabilities */
182 ENTRY *qp, *rp, *lastread = 0;
185 int unresolved, total_unresolved, multiples;
187 DEBUG(2, ("RESOLUTION BEGINNING"));
190 * Check for multiple occurrences of the same name.
198 && _nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
200 if (matchcount == 1) {
201 (void) fprintf(stderr, "Name collision between %s",
202 _nc_first_name(qp->tterm.term_names));
206 (void) fprintf(stderr, " %s", _nc_first_name(rp->tterm.term_names));
210 (void) putc('\n', stderr);
215 DEBUG(2, ("NO MULTIPLE NAME OCCURRENCES"));
218 * First resolution stage: compute link pointers corresponding to names.
220 total_unresolved = 0;
224 for (i = 0; i < qp->nuses; i++) {
226 char *child = _nc_first_name(qp->tterm.term_names);
227 char *lookfor = qp->uses[i].name;
228 long lookline = qp->uses[i].line;
234 /* first, try to resolve from in-core records */
237 && _nc_name_match(rp->tterm.term_names, lookfor, "|")) {
238 DEBUG(2, ("%s: resolving use=%s (in core)",
241 qp->uses[i].link = rp;
246 /* if that didn't work, try to merge in a compiled entry */
249 char filename[PATH_MAX];
251 memset(&thisterm, 0, sizeof(thisterm));
252 if (_nc_read_entry(lookfor, filename, &thisterm) == 1) {
253 DEBUG(2, ("%s: resolving use=%s (compiled)",
256 rp = typeMalloc(ENTRY, 1);
258 _nc_err_abort(MSG_NO_MEMORY);
259 rp->tterm = thisterm;
264 qp->uses[i].link = rp;
269 /* no good, mark this one unresolvable and complain */
274 _nc_curr_line = (int) lookline;
275 _nc_warning("resolution of use=%s failed", lookfor);
276 qp->uses[i].link = 0;
280 if (total_unresolved) {
281 /* free entries read in off disk */
282 _nc_free_entries(lastread);
286 DEBUG(2, ("NAME RESOLUTION COMPLETED OK"));
289 * OK, at this point all (char *) references in `name' members
290 * have been successfully converted to (ENTRY *) pointers in
291 * `link' members. Time to do the actual merges.
301 DEBUG(2, ("%s: attempting merge",
302 _nc_first_name(qp->tterm.term_names)));
304 * If any of the use entries we're looking for is
305 * incomplete, punt. We'll catch this entry on a
308 for (i = 0; i < qp->nuses; i++)
309 if (qp->uses[i].link->nuses) {
310 DEBUG(2, ("%s: use entry %d unresolved",
311 _nc_first_name(qp->tterm.term_names), i));
316 * First, make sure there is no garbage in the
317 * merge block. As a side effect, copy into
318 * the merged entry the name field and string
321 _nc_copy_termtype(&merged, &(qp->tterm));
324 * Now merge in each use entry in the proper
327 for (; qp->nuses; qp->nuses--)
328 _nc_merge_entry(&merged,
329 &qp->uses[qp->nuses - 1].link->tterm);
332 * Now merge in the original entry.
334 _nc_merge_entry(&merged, &qp->tterm);
337 * Replace the original entry with the merged one.
339 FreeIfNeeded(qp->tterm.Booleans);
340 FreeIfNeeded(qp->tterm.Numbers);
341 FreeIfNeeded(qp->tterm.Strings);
343 FreeIfNeeded(qp->tterm.ext_Names);
346 _nc_wrap_entry(qp, TRUE);
349 * We know every entry is resolvable because name resolution
350 * didn't bomb. So go back for another pass.
360 DEBUG(2, ("MERGES COMPLETED OK"));
364 * We'd like to free entries read in off disk at this point, but can't.
365 * The merge_entry() code doesn't copy the strings in the use entries,
366 * it just aliases them. If this ever changes, do a
367 * free_entries(lastread) here.
370 DEBUG(2, ("RESOLUTION FINISHED"));
373 if (_nc_check_termtype != 0) {
376 _nc_curr_line = (int) qp->startline;
377 _nc_set_type(_nc_first_name(qp->tterm.term_names));
378 fixup_acsc(&qp->tterm, literal);
380 DEBUG(2, ("SANITY CHECK FINISHED"));
386 /* obsolete: 20040705 */
388 _nc_resolve_uses(bool fullresolve)
390 return _nc_resolve_uses2(fullresolve, FALSE);
394 * This bit of legerdemain turns all the terminfo variable names into
395 * references to locations in the arrays Booleans, Numbers, and Strings ---
396 * precisely what's needed.
403 fixup_acsc(TERMTYPE *tp, bool literal)
407 && enter_alt_charset_mode != 0
408 && exit_alt_charset_mode != 0)
409 acs_chars = strdup(VT_ACSC);
414 sanity_check2(TERMTYPE *tp, bool literal)
416 if (!PRESENT(exit_attribute_mode)) {
417 #ifdef __UNUSED__ /* this casts too wide a net */
418 bool terminal_entry = !strchr(tp->term_names, '+');
419 if (terminal_entry &&
420 (PRESENT(set_attributes)
421 || PRESENT(enter_standout_mode)
422 || PRESENT(enter_underline_mode)
423 || PRESENT(enter_blink_mode)
424 || PRESENT(enter_bold_mode)
425 || PRESENT(enter_dim_mode)
426 || PRESENT(enter_secure_mode)
427 || PRESENT(enter_protected_mode)
428 || PRESENT(enter_reverse_mode)))
429 _nc_warning("no exit_attribute_mode");
430 #endif /* __UNUSED__ */
431 PAIRED(enter_standout_mode, exit_standout_mode);
432 PAIRED(enter_underline_mode, exit_underline_mode);
435 /* we do this check/fix in postprocess_termcap(), but some packagers
436 * prefer to bypass it...
439 fixup_acsc(tp, literal);
440 ANDMISSING(enter_alt_charset_mode, acs_chars);
441 ANDMISSING(exit_alt_charset_mode, acs_chars);
444 /* listed in structure-member order of first argument */
445 PAIRED(enter_alt_charset_mode, exit_alt_charset_mode);
446 ANDMISSING(enter_blink_mode, exit_attribute_mode);
447 ANDMISSING(enter_bold_mode, exit_attribute_mode);
448 PAIRED(exit_ca_mode, enter_ca_mode);
449 PAIRED(enter_delete_mode, exit_delete_mode);
450 ANDMISSING(enter_dim_mode, exit_attribute_mode);
451 PAIRED(enter_insert_mode, exit_insert_mode);
452 ANDMISSING(enter_secure_mode, exit_attribute_mode);
453 ANDMISSING(enter_protected_mode, exit_attribute_mode);
454 ANDMISSING(enter_reverse_mode, exit_attribute_mode);
455 PAIRED(from_status_line, to_status_line);
456 PAIRED(meta_off, meta_on);
458 PAIRED(prtr_on, prtr_off);
459 PAIRED(save_cursor, restore_cursor);
460 PAIRED(enter_xon_mode, exit_xon_mode);
461 PAIRED(enter_am_mode, exit_am_mode);
462 ANDMISSING(label_off, label_on);
464 PAIRED(display_clock, remove_clock);
466 ANDMISSING(set_color_pair, initialize_pair);
469 /* obsolete: 20040705 */
471 sanity_check(TERMTYPE *tp)
473 sanity_check2(tp, FALSE);
480 _nc_alloc_entry_leaks();
481 _nc_captoinfo_leaks();
482 _nc_comp_scan_leaks();
483 #if BROKEN_LINKER || USE_REENTRANT
487 _nc_tic_expand(0, FALSE, 0);
491 _nc_free_tic(int code)
494 _nc_free_tinfo(code);