1 /****************************************************************************
2 * Copyright (c) 1998-2005,2006 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.63 2006/07/08 18:55:14 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;
65 /****************************************************************************
67 * Entry queue handling
69 ****************************************************************************/
71 * The entry list is a doubly linked list with NULLs terminating the lists:
73 * --------- --------- ---------
75 * |-------| |-------| |-------|
76 * | ----+-->| ----+-->| NULL | next
77 * |-------| |-------| |-------|
78 * | NULL |<--+---- |<--+---- | last
79 * --------- --------- ---------
86 NCURSES_EXPORT_VAR(ENTRY *) _nc_head = 0;
87 NCURSES_EXPORT_VAR(ENTRY *) _nc_tail = 0;
91 /* add an entry to the in-core list */
93 ENTRY *newp = _nc_copy_entry(ep);
96 _nc_err_abort(MSG_NO_MEMORY);
98 newp->last = _nc_tail;
103 newp->last->next = newp;
107 _nc_free_entries(ENTRY * headp)
108 /* free the allocated storage consumed by list entries */
110 (void) headp; /* unused - _nc_head is altered here! */
112 while (_nc_head != 0) {
113 _nc_free_termtype(&(_nc_head->tterm));
117 NCURSES_EXPORT(ENTRY *)
118 _nc_delink_entry(ENTRY * headp, TERMTYPE *tterm)
119 /* delink the allocated storage for the given list entry */
123 for (last = 0, ep = headp; ep != 0; last = ep, ep = ep->next) {
124 if (&(ep->tterm) == tterm) {
126 last->next = ep->next;
128 if (ep == _nc_head) {
131 if (ep == _nc_tail) {
141 _nc_free_entry(ENTRY * headp, TERMTYPE *tterm)
142 /* free the allocated storage consumed by the given list entry */
146 if ((ep = _nc_delink_entry(headp, tterm)) != 0) {
152 force_bar(char *dst, char *src)
154 if (strchr(src, '|') == 0) {
155 size_t len = strlen(src);
156 if (len > MAX_NAME_SIZE)
158 (void) strncpy(dst, src, len);
159 (void) strcpy(dst + len, "|");
166 _nc_entry_match(char *n1, char *n2)
167 /* do any of the aliases in a pair of terminal names match? */
169 char *pstart, *qstart, *pend, *qend;
170 char nc1[MAX_NAME_SIZE + 2], nc2[MAX_NAME_SIZE + 2];
172 n1 = force_bar(nc1, n1);
173 n2 = force_bar(nc2, n2);
175 for (pstart = n1; (pend = strchr(pstart, '|')); pstart = pend + 1)
176 for (qstart = n2; (qend = strchr(qstart, '|')); qstart = qend + 1)
177 if ((pend - pstart == qend - qstart)
178 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0)
184 /****************************************************************************
186 * Entry compiler and resolution logic
188 ****************************************************************************/
191 _nc_read_entry_source(FILE *fp, char *buf,
192 int literal, bool silent,
193 bool(*hook) (ENTRY *))
194 /* slurp all entries in the given file into core */
197 bool oldsuppress = _nc_suppress_warnings;
201 _nc_suppress_warnings = TRUE; /* shut the lexer up, too */
203 _nc_reset_input(fp, buf);
205 memset(&thisentry, 0, sizeof(thisentry));
206 if (_nc_parse_entry(&thisentry, literal, silent) == ERR)
208 if (!isalnum(UChar(thisentry.tterm.term_names[0])))
209 _nc_err_abort("terminal names must start with letter or digit");
212 * This can be used for immediate compilation of entries with no "use="
213 * references to disk. That avoids consuming a lot of memory when the
214 * resolution code could fetch entries off disk.
216 if (hook != NULLHOOK && (*hook) (&thisentry)) {
221 * The enqueued entry is copied with _nc_copy_termtype(), so we can
222 * free some of the data from thisentry, i.e., the arrays.
224 FreeIfNeeded(thisentry.tterm.Booleans);
225 FreeIfNeeded(thisentry.tterm.Numbers);
226 FreeIfNeeded(thisentry.tterm.Strings);
228 FreeIfNeeded(thisentry.tterm.ext_Names);
234 /* set up the head pointer */
235 for (_nc_head = _nc_tail; _nc_head->last; _nc_head = _nc_head->last)
238 DEBUG(1, ("head = %s", _nc_head->tterm.term_names));
239 DEBUG(1, ("tail = %s", _nc_tail->tterm.term_names));
243 DEBUG(1, ("no entries parsed"));
246 _nc_suppress_warnings = oldsuppress;
250 _nc_resolve_uses2(bool fullresolve, bool literal)
251 /* try to resolve all use capabilities */
253 ENTRY *qp, *rp, *lastread = 0;
255 int i, unresolved, total_unresolved, multiples;
257 DEBUG(2, ("RESOLUTION BEGINNING"));
260 * Check for multiple occurrences of the same name.
268 && _nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
270 if (matchcount == 1) {
271 (void) fprintf(stderr, "Name collision between %s",
272 _nc_first_name(qp->tterm.term_names));
276 (void) fprintf(stderr, " %s", _nc_first_name(rp->tterm.term_names));
280 (void) putc('\n', stderr);
285 DEBUG(2, ("NO MULTIPLE NAME OCCURRENCES"));
288 * First resolution stage: compute link pointers corresponding to names.
290 total_unresolved = 0;
294 for (i = 0; i < qp->nuses; i++) {
296 char *child = _nc_first_name(qp->tterm.term_names);
297 char *lookfor = qp->uses[i].name;
298 long lookline = qp->uses[i].line;
304 /* first, try to resolve from in-core records */
307 && _nc_name_match(rp->tterm.term_names, lookfor, "|")) {
308 DEBUG(2, ("%s: resolving use=%s (in core)",
311 qp->uses[i].link = rp;
316 /* if that didn't work, try to merge in a compiled entry */
319 char filename[PATH_MAX];
321 memset(&thisterm, 0, sizeof(thisterm));
322 if (_nc_read_entry(lookfor, filename, &thisterm) == 1) {
323 DEBUG(2, ("%s: resolving use=%s (compiled)",
326 rp = typeMalloc(ENTRY, 1);
328 _nc_err_abort(MSG_NO_MEMORY);
329 rp->tterm = thisterm;
334 qp->uses[i].link = rp;
339 /* no good, mark this one unresolvable and complain */
344 _nc_curr_line = lookline;
345 _nc_warning("resolution of use=%s failed", lookfor);
346 qp->uses[i].link = 0;
350 if (total_unresolved) {
351 /* free entries read in off disk */
352 _nc_free_entries(lastread);
356 DEBUG(2, ("NAME RESOLUTION COMPLETED OK"));
359 * OK, at this point all (char *) references in `name' members
360 * have been successfully converted to (ENTRY *) pointers in
361 * `link' members. Time to do the actual merges.
371 DEBUG(2, ("%s: attempting merge",
372 _nc_first_name(qp->tterm.term_names)));
374 * If any of the use entries we're looking for is
375 * incomplete, punt. We'll catch this entry on a
378 for (i = 0; i < qp->nuses; i++)
379 if (qp->uses[i].link->nuses) {
380 DEBUG(2, ("%s: use entry %d unresolved",
381 _nc_first_name(qp->tterm.term_names), i));
386 * First, make sure there is no garbage in the
387 * merge block. As a side effect, copy into
388 * the merged entry the name field and string
391 _nc_copy_termtype(&merged, &(qp->tterm));
394 * Now merge in each use entry in the proper
397 for (; qp->nuses; qp->nuses--)
398 _nc_merge_entry(&merged,
399 &qp->uses[qp->nuses - 1].link->tterm);
402 * Now merge in the original entry.
404 _nc_merge_entry(&merged, &qp->tterm);
407 * Replace the original entry with the merged one.
409 FreeIfNeeded(qp->tterm.Booleans);
410 FreeIfNeeded(qp->tterm.Numbers);
411 FreeIfNeeded(qp->tterm.Strings);
413 FreeIfNeeded(qp->tterm.ext_Names);
416 _nc_wrap_entry(qp, TRUE);
419 * We know every entry is resolvable because name resolution
420 * didn't bomb. So go back for another pass.
430 DEBUG(2, ("MERGES COMPLETED OK"));
434 * We'd like to free entries read in off disk at this point, but can't.
435 * The merge_entry() code doesn't copy the strings in the use entries,
436 * it just aliases them. If this ever changes, do a
437 * free_entries(lastread) here.
440 DEBUG(2, ("RESOLUTION FINISHED"));
443 if (_nc_check_termtype != 0) {
446 _nc_curr_line = qp->startline;
447 _nc_set_type(_nc_first_name(qp->tterm.term_names));
448 _nc_check_termtype2(&qp->tterm, literal);
450 DEBUG(2, ("SANITY CHECK FINISHED"));
456 /* obsolete: 20040705 */
458 _nc_resolve_uses(bool fullresolve)
460 return _nc_resolve_uses2(fullresolve, FALSE);
464 * This bit of legerdemain turns all the terminfo variable names into
465 * references to locations in the arrays Booleans, Numbers, and Strings ---
466 * precisely what's needed.
473 sanity_check2(TERMTYPE *tp, bool literal)
475 if (!PRESENT(exit_attribute_mode)) {
476 #ifdef __UNUSED__ /* this casts too wide a net */
477 bool terminal_entry = !strchr(tp->term_names, '+');
478 if (terminal_entry &&
479 (PRESENT(set_attributes)
480 || PRESENT(enter_standout_mode)
481 || PRESENT(enter_underline_mode)
482 || PRESENT(enter_blink_mode)
483 || PRESENT(enter_bold_mode)
484 || PRESENT(enter_dim_mode)
485 || PRESENT(enter_secure_mode)
486 || PRESENT(enter_protected_mode)
487 || PRESENT(enter_reverse_mode)))
488 _nc_warning("no exit_attribute_mode");
489 #endif /* __UNUSED__ */
490 PAIRED(enter_standout_mode, exit_standout_mode);
491 PAIRED(enter_underline_mode, exit_underline_mode);
494 /* we do this check/fix in postprocess_termcap(), but some packagers
495 * prefer to bypass it...
499 && enter_alt_charset_mode != 0
500 && exit_alt_charset_mode != 0)
501 acs_chars = strdup(VT_ACSC);
502 ANDMISSING(enter_alt_charset_mode, acs_chars);
503 ANDMISSING(exit_alt_charset_mode, acs_chars);
506 /* listed in structure-member order of first argument */
507 PAIRED(enter_alt_charset_mode, exit_alt_charset_mode);
508 ANDMISSING(enter_blink_mode, exit_attribute_mode);
509 ANDMISSING(enter_bold_mode, exit_attribute_mode);
510 PAIRED(exit_ca_mode, enter_ca_mode);
511 PAIRED(enter_delete_mode, exit_delete_mode);
512 ANDMISSING(enter_dim_mode, exit_attribute_mode);
513 PAIRED(enter_insert_mode, exit_insert_mode);
514 ANDMISSING(enter_secure_mode, exit_attribute_mode);
515 ANDMISSING(enter_protected_mode, exit_attribute_mode);
516 ANDMISSING(enter_reverse_mode, exit_attribute_mode);
517 PAIRED(from_status_line, to_status_line);
518 PAIRED(meta_off, meta_on);
520 PAIRED(prtr_on, prtr_off);
521 PAIRED(save_cursor, restore_cursor);
522 PAIRED(enter_xon_mode, exit_xon_mode);
523 PAIRED(enter_am_mode, exit_am_mode);
524 ANDMISSING(label_off, label_on);
526 PAIRED(display_clock, remove_clock);
528 ANDMISSING(set_color_pair, initialize_pair);
531 /* obsolete: 20040705 */
533 sanity_check(TERMTYPE *tp)
535 sanity_check2(tp, FALSE);