1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 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 * _nc_read_entry_source(FILE *, literal, bool, bool (*hook)())
39 * _nc_resolve_uses2(void)
40 * _nc_free_entries(void)
42 * Use this code by calling _nc_read_entry_source() on as many source
43 * files as you like (either terminfo or termcap syntax). If you
44 * want use-resolution, call _nc_resolve_uses2(). To free the list
45 * storage, do _nc_free_entries().
49 #include <curses.priv.h>
54 #include <term_entry.h>
56 MODULE_ID("$Id: comp_parse.c,v 1.69 2008/08/16 21:58:16 tom Exp $")
58 static void sanity_check2(TERMTYPE *, bool);
59 NCURSES_IMPEXP void NCURSES_API(*_nc_check_termtype2) (TERMTYPE *, bool) = sanity_check2;
61 /* obsolete: 20040705 */
62 static void sanity_check(TERMTYPE *);
63 NCURSES_IMPEXP void NCURSES_API(*_nc_check_termtype) (TERMTYPE *) = sanity_check;
67 /* add an entry to the in-core list */
69 ENTRY *newp = _nc_copy_entry(ep);
72 _nc_err_abort(MSG_NO_MEMORY);
74 newp->last = _nc_tail;
79 newp->last->next = newp;
83 force_bar(char *dst, char *src)
85 if (strchr(src, '|') == 0) {
86 size_t len = strlen(src);
87 if (len > MAX_NAME_SIZE)
89 (void) strncpy(dst, src, len);
90 (void) strcpy(dst + len, "|");
97 _nc_entry_match(char *n1, char *n2)
98 /* do any of the aliases in a pair of terminal names match? */
100 char *pstart, *qstart, *pend, *qend;
101 char nc1[MAX_NAME_SIZE + 2], nc2[MAX_NAME_SIZE + 2];
103 n1 = force_bar(nc1, n1);
104 n2 = force_bar(nc2, n2);
106 for (pstart = n1; (pend = strchr(pstart, '|')); pstart = pend + 1)
107 for (qstart = n2; (qend = strchr(qstart, '|')); qstart = qend + 1)
108 if ((pend - pstart == qend - qstart)
109 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0)
115 /****************************************************************************
117 * Entry compiler and resolution logic
119 ****************************************************************************/
122 _nc_read_entry_source(FILE *fp, char *buf,
123 int literal, bool silent,
124 bool(*hook) (ENTRY *))
125 /* slurp all entries in the given file into core */
128 bool oldsuppress = _nc_suppress_warnings;
132 _nc_suppress_warnings = TRUE; /* shut the lexer up, too */
134 _nc_reset_input(fp, buf);
136 memset(&thisentry, 0, sizeof(thisentry));
137 if (_nc_parse_entry(&thisentry, literal, silent) == ERR)
139 if (!isalnum(UChar(thisentry.tterm.term_names[0])))
140 _nc_err_abort("terminal names must start with letter or digit");
143 * This can be used for immediate compilation of entries with no "use="
144 * references to disk. That avoids consuming a lot of memory when the
145 * resolution code could fetch entries off disk.
147 if (hook != NULLHOOK && (*hook) (&thisentry)) {
152 * The enqueued entry is copied with _nc_copy_termtype(), so we can
153 * free some of the data from thisentry, i.e., the arrays.
155 FreeIfNeeded(thisentry.tterm.Booleans);
156 FreeIfNeeded(thisentry.tterm.Numbers);
157 FreeIfNeeded(thisentry.tterm.Strings);
159 FreeIfNeeded(thisentry.tterm.ext_Names);
165 /* set up the head pointer */
166 for (_nc_head = _nc_tail; _nc_head->last; _nc_head = _nc_head->last)
169 DEBUG(1, ("head = %s", _nc_head->tterm.term_names));
170 DEBUG(1, ("tail = %s", _nc_tail->tterm.term_names));
174 DEBUG(1, ("no entries parsed"));
177 _nc_suppress_warnings = oldsuppress;
181 _nc_resolve_uses2(bool fullresolve, bool literal)
182 /* try to resolve all use capabilities */
184 ENTRY *qp, *rp, *lastread = 0;
187 int unresolved, total_unresolved, multiples;
189 DEBUG(2, ("RESOLUTION BEGINNING"));
192 * Check for multiple occurrences of the same name.
200 && _nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
202 if (matchcount == 1) {
203 (void) fprintf(stderr, "Name collision between %s",
204 _nc_first_name(qp->tterm.term_names));
208 (void) fprintf(stderr, " %s", _nc_first_name(rp->tterm.term_names));
212 (void) putc('\n', stderr);
217 DEBUG(2, ("NO MULTIPLE NAME OCCURRENCES"));
220 * First resolution stage: compute link pointers corresponding to names.
222 total_unresolved = 0;
226 for (i = 0; i < qp->nuses; i++) {
228 char *child = _nc_first_name(qp->tterm.term_names);
229 char *lookfor = qp->uses[i].name;
230 long lookline = qp->uses[i].line;
236 /* first, try to resolve from in-core records */
239 && _nc_name_match(rp->tterm.term_names, lookfor, "|")) {
240 DEBUG(2, ("%s: resolving use=%s (in core)",
243 qp->uses[i].link = rp;
248 /* if that didn't work, try to merge in a compiled entry */
251 char filename[PATH_MAX];
253 memset(&thisterm, 0, sizeof(thisterm));
254 if (_nc_read_entry(lookfor, filename, &thisterm) == 1) {
255 DEBUG(2, ("%s: resolving use=%s (compiled)",
258 rp = typeMalloc(ENTRY, 1);
260 _nc_err_abort(MSG_NO_MEMORY);
261 rp->tterm = thisterm;
266 qp->uses[i].link = rp;
271 /* no good, mark this one unresolvable and complain */
276 _nc_curr_line = lookline;
277 _nc_warning("resolution of use=%s failed", lookfor);
278 qp->uses[i].link = 0;
282 if (total_unresolved) {
283 /* free entries read in off disk */
284 _nc_free_entries(lastread);
288 DEBUG(2, ("NAME RESOLUTION COMPLETED OK"));
291 * OK, at this point all (char *) references in `name' members
292 * have been successfully converted to (ENTRY *) pointers in
293 * `link' members. Time to do the actual merges.
303 DEBUG(2, ("%s: attempting merge",
304 _nc_first_name(qp->tterm.term_names)));
306 * If any of the use entries we're looking for is
307 * incomplete, punt. We'll catch this entry on a
310 for (i = 0; i < qp->nuses; i++)
311 if (qp->uses[i].link->nuses) {
312 DEBUG(2, ("%s: use entry %d unresolved",
313 _nc_first_name(qp->tterm.term_names), i));
318 * First, make sure there is no garbage in the
319 * merge block. As a side effect, copy into
320 * the merged entry the name field and string
323 _nc_copy_termtype(&merged, &(qp->tterm));
326 * Now merge in each use entry in the proper
329 for (; qp->nuses; qp->nuses--)
330 _nc_merge_entry(&merged,
331 &qp->uses[qp->nuses - 1].link->tterm);
334 * Now merge in the original entry.
336 _nc_merge_entry(&merged, &qp->tterm);
339 * Replace the original entry with the merged one.
341 FreeIfNeeded(qp->tterm.Booleans);
342 FreeIfNeeded(qp->tterm.Numbers);
343 FreeIfNeeded(qp->tterm.Strings);
345 FreeIfNeeded(qp->tterm.ext_Names);
348 _nc_wrap_entry(qp, TRUE);
351 * We know every entry is resolvable because name resolution
352 * didn't bomb. So go back for another pass.
362 DEBUG(2, ("MERGES COMPLETED OK"));
366 * We'd like to free entries read in off disk at this point, but can't.
367 * The merge_entry() code doesn't copy the strings in the use entries,
368 * it just aliases them. If this ever changes, do a
369 * free_entries(lastread) here.
372 DEBUG(2, ("RESOLUTION FINISHED"));
375 if (_nc_check_termtype != 0) {
378 _nc_curr_line = qp->startline;
379 _nc_set_type(_nc_first_name(qp->tterm.term_names));
380 _nc_check_termtype2(&qp->tterm, literal);
382 DEBUG(2, ("SANITY CHECK FINISHED"));
388 /* obsolete: 20040705 */
390 _nc_resolve_uses(bool fullresolve)
392 return _nc_resolve_uses2(fullresolve, FALSE);
396 * This bit of legerdemain turns all the terminfo variable names into
397 * references to locations in the arrays Booleans, Numbers, and Strings ---
398 * precisely what's needed.
405 sanity_check2(TERMTYPE *tp, bool literal)
407 if (!PRESENT(exit_attribute_mode)) {
408 #ifdef __UNUSED__ /* this casts too wide a net */
409 bool terminal_entry = !strchr(tp->term_names, '+');
410 if (terminal_entry &&
411 (PRESENT(set_attributes)
412 || PRESENT(enter_standout_mode)
413 || PRESENT(enter_underline_mode)
414 || PRESENT(enter_blink_mode)
415 || PRESENT(enter_bold_mode)
416 || PRESENT(enter_dim_mode)
417 || PRESENT(enter_secure_mode)
418 || PRESENT(enter_protected_mode)
419 || PRESENT(enter_reverse_mode)))
420 _nc_warning("no exit_attribute_mode");
421 #endif /* __UNUSED__ */
422 PAIRED(enter_standout_mode, exit_standout_mode);
423 PAIRED(enter_underline_mode, exit_underline_mode);
426 /* we do this check/fix in postprocess_termcap(), but some packagers
427 * prefer to bypass it...
431 && enter_alt_charset_mode != 0
432 && exit_alt_charset_mode != 0)
433 acs_chars = strdup(VT_ACSC);
434 ANDMISSING(enter_alt_charset_mode, acs_chars);
435 ANDMISSING(exit_alt_charset_mode, acs_chars);
438 /* listed in structure-member order of first argument */
439 PAIRED(enter_alt_charset_mode, exit_alt_charset_mode);
440 ANDMISSING(enter_blink_mode, exit_attribute_mode);
441 ANDMISSING(enter_bold_mode, exit_attribute_mode);
442 PAIRED(exit_ca_mode, enter_ca_mode);
443 PAIRED(enter_delete_mode, exit_delete_mode);
444 ANDMISSING(enter_dim_mode, exit_attribute_mode);
445 PAIRED(enter_insert_mode, exit_insert_mode);
446 ANDMISSING(enter_secure_mode, exit_attribute_mode);
447 ANDMISSING(enter_protected_mode, exit_attribute_mode);
448 ANDMISSING(enter_reverse_mode, exit_attribute_mode);
449 PAIRED(from_status_line, to_status_line);
450 PAIRED(meta_off, meta_on);
452 PAIRED(prtr_on, prtr_off);
453 PAIRED(save_cursor, restore_cursor);
454 PAIRED(enter_xon_mode, exit_xon_mode);
455 PAIRED(enter_am_mode, exit_am_mode);
456 ANDMISSING(label_off, label_on);
458 PAIRED(display_clock, remove_clock);
460 ANDMISSING(set_color_pair, initialize_pair);
463 /* obsolete: 20040705 */
465 sanity_check(TERMTYPE *tp)
467 sanity_check2(tp, FALSE);
474 _nc_alloc_entry_leaks();
475 _nc_captoinfo_leaks();
476 _nc_comp_captab_leaks();
477 _nc_comp_scan_leaks();
478 #if BROKEN_LINKER || USE_REENTRANT
482 _nc_tic_expand(0, FALSE, 0);
486 _nc_free_tic(int code)
489 _nc_free_tinfo(code);