1 /****************************************************************************
2 * Copyright (c) 1998 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 ****************************************************************************/
37 * comp_parse.c -- parser driver loop and use handling.
39 * _nc_read_entry_source(FILE *, literal, bool, bool (*hook)())
40 * _nc_resolve_uses(void)
41 * _nc_free_entries(void)
43 * Use this code by calling _nc_read_entry_source() on as many source
44 * files as you like (either terminfo or termcap syntax). If you
45 * want use-resolution, call _nc_resolve_uses(). To free the list
46 * storage, do _nc_free_entries().
50 #include <curses.priv.h>
56 #include <term_entry.h>
58 MODULE_ID("$Id: comp_parse.c,v 1.21 1998/02/11 12:14:00 tom Exp $")
60 static void sanity_check(TERMTYPE *);
62 /****************************************************************************
64 * Entry queue handling
66 ****************************************************************************/
68 * The entry list is a doubly linked list with NULLs terminating the lists:
70 * --------- --------- ---------
72 * |-------| |-------| |-------|
73 * | ----+-->| ----+-->| NULL | next
74 * |-------| |-------| |-------|
75 * | NULL |<--+---- |<--+---- | last
76 * --------- --------- ---------
83 ENTRY *_nc_head, *_nc_tail;
85 static void enqueue(ENTRY *ep)
86 /* add an entry to the in-core list */
88 ENTRY *newp = (ENTRY *)malloc(sizeof(ENTRY));
91 _nc_err_abort("Out of memory");
93 (void) memcpy(newp, ep, sizeof(ENTRY));
95 newp->last = _nc_tail;
98 newp->next = (ENTRY *)NULL;
100 newp->last->next = newp;
103 void _nc_free_entries(ENTRY *head)
104 /* free the allocated storage consumed by list entries */
108 for (ep = head; ep; ep = next)
111 * This conditional lets us disconnect storage from the list.
112 * To do this, copy an entry out of the list, then null out
113 * the string-table member in the original and any use entries
116 FreeIfNeeded(ep->tterm.str_table);
121 if (ep == _nc_head) _nc_head = 0;
122 if (ep == _nc_tail) _nc_tail = 0;
126 bool _nc_entry_match(char *n1, char *n2)
127 /* do any of the aliases in a pair of terminal names match? */
129 char *pstart, *qstart, *pend, *qend;
130 char nc1[MAX_NAME_SIZE+1], nc2[MAX_NAME_SIZE+1];
132 if (strchr(n1, '|') == NULL)
134 (void) strcpy(nc1, n1);
135 (void) strcat(nc1, "|");
139 if (strchr(n2, '|') == NULL)
141 (void) strcpy(nc2, n2);
142 (void) strcat(nc2, "|");
146 for (pstart = n1; (pend = strchr(pstart, '|')); pstart = pend + 1)
147 for (qstart = n2; (qend = strchr(qstart, '|')); qstart = qend + 1)
148 if ((pend-pstart == qend-qstart)
149 && memcmp(pstart, qstart, (size_t)(pend-pstart)) == 0)
155 /****************************************************************************
157 * Entry compiler and resolution logic
159 ****************************************************************************/
161 void _nc_read_entry_source(FILE *fp, char *buf,
162 int literal, bool silent,
163 bool (*hook)(ENTRY *))
164 /* slurp all entries in the given file into core */
167 bool oldsuppress = _nc_suppress_warnings;
171 _nc_suppress_warnings = TRUE; /* shut the lexer up, too */
173 for (_nc_reset_input(fp, buf); _nc_parse_entry(&thisentry, literal, silent) != ERR; )
175 if (!isalnum(thisentry.tterm.term_names[0]))
176 _nc_err_abort("terminal names must start with letter or digit");
179 * This can be used for immediate compilation of entries with no
180 * use references to disk, so as to avoid chewing up a lot of
181 * core when the resolution code could fetch entries off disk.
183 if (hook != NULLHOOK && (*hook)(&thisentry))
191 /* set up the head pointer */
192 for (_nc_head = _nc_tail; _nc_head->last; _nc_head = _nc_head->last)
195 DEBUG(1, ("head = %s", _nc_head->tterm.term_names));
196 DEBUG(1, ("tail = %s", _nc_tail->tterm.term_names));
199 DEBUG(1, ("no entries parsed"));
201 _nc_suppress_warnings = oldsuppress;
204 int _nc_resolve_uses(void)
205 /* try to resolve all use capabilities */
207 ENTRY *qp, *rp, *lastread = NULL;
209 int i, j, unresolved, total_unresolved, multiples;
211 DEBUG(2, ("RESOLUTION BEGINNING"));
214 * Check for multiple occurrences of the same name.
223 && _nc_entry_match(qp->tterm.term_names, rp->tterm.term_names))
228 (void) fprintf(stderr, "Name collision between %s",
229 _nc_first_name(qp->tterm.term_names));
233 (void) fprintf(stderr, " %s", _nc_first_name(rp->tterm.term_names));
236 (void) putc('\n', stderr);
241 DEBUG(2, ("NO MULTIPLE NAME OCCURRENCES"));
244 * First resolution stage: replace names in use arrays with entry
245 * pointers. By doing this, we avoid having to do the same name
246 * match once for each time a use entry is itself unresolved.
248 total_unresolved = 0;
253 for (i = 0; i < qp->nuses; i++)
256 char *child = _nc_first_name(qp->tterm.term_names);
257 char *lookfor = (char *)(qp->uses[i].parent);
258 long lookline = qp->uses[i].line;
264 /* first, try to resolve from in-core records */
267 && _nc_name_match(rp->tterm.term_names, lookfor, "|"))
269 DEBUG(2, ("%s: resolving use=%s (in core)",
272 qp->uses[i].parent = rp;
276 /* if that didn't work, try to merge in a compiled entry */
280 char filename[PATH_MAX];
282 if (_nc_read_entry(lookfor, filename, &thisterm) == 1)
284 DEBUG(2, ("%s: resolving use=%s (compiled)",
287 rp = (ENTRY *)malloc(sizeof(ENTRY));
288 memcpy(&rp->tterm, &thisterm, sizeof(TERMTYPE));
293 qp->uses[i].parent = rp;
298 /* no good, mark this one unresolvable and complain */
304 _nc_curr_line = lookline;
305 _nc_warning("resolution of use=%s failed", lookfor);
306 qp->uses[i].parent = (ENTRY *)NULL;
310 if (total_unresolved)
312 /* free entries read in off disk */
313 _nc_free_entries(lastread);
317 DEBUG(2, ("NAME RESOLUTION COMPLETED OK"));
320 * OK, at this point all (char *) references have been successfully
321 * replaced by (ENTRY *) pointers. Time to do the actual merges.
331 DEBUG(2, ("%s: attempting merge", _nc_first_name(qp->tterm.term_names)));
333 * If any of the use entries we're looking for is
334 * incomplete, punt. We'll catch this entry on a
337 for (i = 0; i < qp->nuses; i++)
338 if (((ENTRY *)qp->uses[i].parent)->nuses)
340 DEBUG(2, ("%s: use entry %d unresolved",
341 _nc_first_name(qp->tterm.term_names), i));
346 * First, make sure there's no garbage in the merge block.
347 * as a side effect, copy into the merged entry the name
348 * field and string table pointer.
350 memcpy(&merged, &qp->tterm, sizeof(TERMTYPE));
353 * Now merge in each use entry in the proper
356 for (; qp->nuses; qp->nuses--)
357 _nc_merge_entry(&merged,
358 &((ENTRY *)qp->uses[qp->nuses-1].parent)->tterm);
361 * Now merge in the original entry.
363 _nc_merge_entry(&merged, &qp->tterm);
366 * Replace the original entry with the merged one.
368 memcpy(&qp->tterm, &merged, sizeof(TERMTYPE));
371 * We know every entry is resolvable because name resolution
372 * didn't bomb. So go back for another pass.
381 DEBUG(2, ("MERGES COMPLETED OK"));
384 * The exit condition of the loop above is such that all entries
385 * must now be resolved. Now handle cancellations. In a resolved
386 * entry there should be no cancellation markers.
390 for (j = 0; j < BOOLCOUNT; j++)
391 if (qp->tterm.Booleans[j] == CANCELLED_BOOLEAN)
392 qp->tterm.Booleans[j] = FALSE;
393 for (j = 0; j < NUMCOUNT; j++)
394 if (qp->tterm.Numbers[j] == CANCELLED_NUMERIC)
395 qp->tterm.Numbers[j] = ABSENT_NUMERIC;
396 for (j = 0; j < STRCOUNT; j++)
397 if (qp->tterm.Strings[j] == CANCELLED_STRING)
398 qp->tterm.Strings[j] = ABSENT_STRING;
402 * We'd like to free entries read in off disk at this point, but can't.
403 * The merge_entry() code doesn't copy the strings in the use entries,
404 * it just aliases them. If this ever changes, do a
405 * free_entries(lastread) here.
408 DEBUG(2, ("RESOLUTION FINISHED"));
413 _nc_curr_line = qp->startline;
414 _nc_set_type(_nc_first_name(qp->tterm.term_names));
415 sanity_check(&qp->tterm);
418 DEBUG(2, ("SANITY CHECK FINISHED"));
424 * This bit of legerdemain turns all the terminfo variable names into
425 * references to locations in the arrays Booleans, Numbers, and Strings ---
426 * precisely what's needed.
433 * Note that WANTED and PRESENT are not simple inverses! If a capability
434 * has been explicitly cancelled, it's not considered WANTED.
436 #define WANTED(s) ((s) == ABSENT_STRING)
437 #define PRESENT(s) (((s) != ABSENT_STRING) && ((s) != CANCELLED_STRING))
439 #define ANDMISSING(p,q) \
440 {if (PRESENT(p) && !PRESENT(q)) _nc_warning(#p " but no " #q);}
442 #define PAIRED(p,q) \
444 if (PRESENT(q) && !PRESENT(p)) \
445 _nc_warning(#q " but no " #p); \
446 if (PRESENT(p) && !PRESENT(q)) \
447 _nc_warning(#p " but no " #q); \
450 static void sanity_check(TERMTYPE *tp)
452 #ifdef __UNUSED__ /* this casts too wide a net */
453 bool terminal_entry = !strchr(tp->term_names, '+');
456 if (!PRESENT(exit_attribute_mode))
458 #ifdef __UNUSED__ /* this casts too wide a net */
459 if (terminal_entry &&
460 (PRESENT(set_attributes)
461 || PRESENT(enter_standout_mode)
462 || PRESENT(enter_underline_mode)
463 || PRESENT(enter_blink_mode)
464 || PRESENT(enter_bold_mode)
465 || PRESENT(enter_dim_mode)
466 || PRESENT(enter_secure_mode)
467 || PRESENT(enter_protected_mode)
468 || PRESENT(enter_reverse_mode)))
469 _nc_warning("no exit_attribute_mode");
470 #endif /* __UNUSED__ */
471 PAIRED(enter_standout_mode, exit_standout_mode)
472 PAIRED(enter_underline_mode, exit_underline_mode)
475 /* listed in structure-member order of first argument */
477 ANDMISSING(cursor_invisible, cursor_normal)
478 ANDMISSING(cursor_visible, cursor_normal)
479 #endif /* __UNUSED__ */
480 PAIRED(enter_alt_charset_mode, exit_alt_charset_mode)
481 ANDMISSING(enter_alt_charset_mode, acs_chars)
482 ANDMISSING(exit_alt_charset_mode, acs_chars)
483 ANDMISSING(enter_blink_mode, exit_attribute_mode)
484 ANDMISSING(enter_bold_mode, exit_attribute_mode)
485 PAIRED(exit_ca_mode, enter_ca_mode)
486 PAIRED(enter_delete_mode, exit_delete_mode)
487 ANDMISSING(enter_dim_mode, exit_attribute_mode)
488 PAIRED(enter_insert_mode, exit_insert_mode)
489 ANDMISSING(enter_secure_mode, exit_attribute_mode)
490 ANDMISSING(enter_protected_mode, exit_attribute_mode)
491 ANDMISSING(enter_reverse_mode, exit_attribute_mode)
492 PAIRED(from_status_line, to_status_line)
493 PAIRED(meta_off, meta_on)
495 PAIRED(prtr_on, prtr_off)
496 PAIRED(save_cursor, restore_cursor)
497 PAIRED(enter_xon_mode, exit_xon_mode)
498 PAIRED(enter_am_mode, exit_am_mode)
499 ANDMISSING(label_off, label_on)
500 PAIRED(display_clock, remove_clock)
501 ANDMISSING(set_color_pair, initialize_pair)
503 /* Some checks that we should make, but don't want to confuse people
504 * with. Put those under the tic -v option so we can still get them.
509 * From XSI & O'Reilly, we gather that sc/rc are required if csr is
510 * given, because the cursor position after the scrolling operation is
511 * performed is undefined.
513 ANDMISSING(change_scroll_region, save_cursor)
514 ANDMISSING(change_scroll_region, restore_cursor)
517 * Some non-curses applications (e.g., jove) get confused if we have
518 * both ich/ich1 and smir/rmir. Let's be nice and warn about that,
519 * too, even though ncurses handles it.
521 if ((PRESENT(enter_insert_mode) || PRESENT(exit_insert_mode))
522 && (PRESENT(insert_character) || PRESENT(parm_ich))) {
523 _nc_warning("non-curses applications may be confused by ich/ich1 with smir/rmir");