]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/comp_parse.c
938b2c224df5193521a2233213d176ac9a583380
[ncurses.git] / ncurses / tinfo / comp_parse.c
1 /****************************************************************************
2  * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
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  ****************************************************************************/
34
35 /*
36  *      comp_parse.c -- parser driver loop and use handling.
37  *
38  *      _nc_read_entry_source(FILE *, literal, bool, bool (*hook)())
39  *      _nc_resolve_uses2(void)
40  *      _nc_free_entries(void)
41  *
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().
46  *
47  */
48
49 #include <curses.priv.h>
50
51 #include <ctype.h>
52
53 #include <tic.h>
54 #include <term_entry.h>
55
56 MODULE_ID("$Id: comp_parse.c,v 1.71 2009/09/27 14:45:02 tom Exp $")
57
58 static void sanity_check2(TERMTYPE *, bool);
59 NCURSES_IMPEXP void NCURSES_API(*_nc_check_termtype2) (TERMTYPE *, bool) = sanity_check2;
60
61 /* obsolete: 20040705 */
62 static void sanity_check(TERMTYPE *);
63 NCURSES_IMPEXP void NCURSES_API(*_nc_check_termtype) (TERMTYPE *) = sanity_check;
64
65 static void
66 enqueue(ENTRY * ep)
67 /* add an entry to the in-core list */
68 {
69     ENTRY *newp = _nc_copy_entry(ep);
70
71     if (newp == 0)
72         _nc_err_abort(MSG_NO_MEMORY);
73
74     newp->last = _nc_tail;
75     _nc_tail = newp;
76
77     newp->next = 0;
78     if (newp->last)
79         newp->last->next = newp;
80 }
81
82 static char *
83 force_bar(char *dst, char *src)
84 {
85     if (strchr(src, '|') == 0) {
86         size_t len = strlen(src);
87         if (len > MAX_NAME_SIZE)
88             len = MAX_NAME_SIZE;
89         (void) strncpy(dst, src, len);
90         (void) strcpy(dst + len, "|");
91         src = dst;
92     }
93     return src;
94 }
95 #define ForceBar(dst, src) ((strchr(src, '|') == 0) ? force_bar(dst, src) : src)
96
97 NCURSES_EXPORT(bool)
98 _nc_entry_match(char *n1, char *n2)
99 /* do any of the aliases in a pair of terminal names match? */
100 {
101     char *pstart, *qstart, *pend, *qend;
102     char nc1[MAX_NAME_SIZE + 2];
103     char nc2[MAX_NAME_SIZE + 2];
104
105     n1 = ForceBar(nc1, n1);
106     n2 = ForceBar(nc2, n2);
107
108     for (pstart = n1; (pend = strchr(pstart, '|')); pstart = pend + 1)
109         for (qstart = n2; (qend = strchr(qstart, '|')); qstart = qend + 1)
110             if ((pend - pstart == qend - qstart)
111                 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0)
112                 return (TRUE);
113
114     return (FALSE);
115 }
116
117 /****************************************************************************
118  *
119  * Entry compiler and resolution logic
120  *
121  ****************************************************************************/
122
123 NCURSES_EXPORT(void)
124 _nc_read_entry_source(FILE *fp, char *buf,
125                       int literal, bool silent,
126                       bool(*hook) (ENTRY *))
127 /* slurp all entries in the given file into core */
128 {
129     ENTRY thisentry;
130     bool oldsuppress = _nc_suppress_warnings;
131     int immediate = 0;
132
133     if (silent)
134         _nc_suppress_warnings = TRUE;   /* shut the lexer up, too */
135
136     _nc_reset_input(fp, buf);
137     for (;;) {
138         memset(&thisentry, 0, sizeof(thisentry));
139         if (_nc_parse_entry(&thisentry, literal, silent) == ERR)
140             break;
141         if (!isalnum(UChar(thisentry.tterm.term_names[0])))
142             _nc_err_abort("terminal names must start with letter or digit");
143
144         /*
145          * This can be used for immediate compilation of entries with no "use="
146          * references to disk.  That avoids consuming a lot of memory when the
147          * resolution code could fetch entries off disk.
148          */
149         if (hook != NULLHOOK && (*hook) (&thisentry)) {
150             immediate++;
151         } else {
152             enqueue(&thisentry);
153             /*
154              * The enqueued entry is copied with _nc_copy_termtype(), so we can
155              * free some of the data from thisentry, i.e., the arrays.
156              */
157             FreeIfNeeded(thisentry.tterm.Booleans);
158             FreeIfNeeded(thisentry.tterm.Numbers);
159             FreeIfNeeded(thisentry.tterm.Strings);
160 #if NCURSES_XNAMES
161             FreeIfNeeded(thisentry.tterm.ext_Names);
162 #endif
163         }
164     }
165
166     if (_nc_tail) {
167         /* set up the head pointer */
168         for (_nc_head = _nc_tail; _nc_head->last; _nc_head = _nc_head->last)
169             continue;
170
171         DEBUG(1, ("head = %s", _nc_head->tterm.term_names));
172         DEBUG(1, ("tail = %s", _nc_tail->tterm.term_names));
173     }
174 #ifdef TRACE
175     else if (!immediate)
176         DEBUG(1, ("no entries parsed"));
177 #endif
178
179     _nc_suppress_warnings = oldsuppress;
180 }
181
182 NCURSES_EXPORT(int)
183 _nc_resolve_uses2(bool fullresolve, bool literal)
184 /* try to resolve all use capabilities */
185 {
186     ENTRY *qp, *rp, *lastread = 0;
187     bool keepgoing;
188     unsigned i;
189     int unresolved, total_unresolved, multiples;
190
191     DEBUG(2, ("RESOLUTION BEGINNING"));
192
193     /*
194      * Check for multiple occurrences of the same name.
195      */
196     multiples = 0;
197     for_entry_list(qp) {
198         int matchcount = 0;
199
200         for_entry_list(rp) {
201             if (qp > rp
202                 && _nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
203                 matchcount++;
204                 if (matchcount == 1) {
205                     (void) fprintf(stderr, "Name collision between %s",
206                                    _nc_first_name(qp->tterm.term_names));
207                     multiples++;
208                 }
209                 if (matchcount >= 1)
210                     (void) fprintf(stderr, " %s", _nc_first_name(rp->tterm.term_names));
211             }
212         }
213         if (matchcount >= 1)
214             (void) putc('\n', stderr);
215     }
216     if (multiples > 0)
217         return (FALSE);
218
219     DEBUG(2, ("NO MULTIPLE NAME OCCURRENCES"));
220
221     /*
222      * First resolution stage: compute link pointers corresponding to names.
223      */
224     total_unresolved = 0;
225     _nc_curr_col = -1;
226     for_entry_list(qp) {
227         unresolved = 0;
228         for (i = 0; i < qp->nuses; i++) {
229             bool foundit;
230             char *child = _nc_first_name(qp->tterm.term_names);
231             char *lookfor = qp->uses[i].name;
232             long lookline = qp->uses[i].line;
233
234             foundit = FALSE;
235
236             _nc_set_type(child);
237
238             /* first, try to resolve from in-core records */
239             for_entry_list(rp) {
240                 if (rp != qp
241                     && _nc_name_match(rp->tterm.term_names, lookfor, "|")) {
242                     DEBUG(2, ("%s: resolving use=%s (in core)",
243                               child, lookfor));
244
245                     qp->uses[i].link = rp;
246                     foundit = TRUE;
247                 }
248             }
249
250             /* if that didn't work, try to merge in a compiled entry */
251             if (!foundit) {
252                 TERMTYPE thisterm;
253                 char filename[PATH_MAX];
254
255                 memset(&thisterm, 0, sizeof(thisterm));
256                 if (_nc_read_entry(lookfor, filename, &thisterm) == 1) {
257                     DEBUG(2, ("%s: resolving use=%s (compiled)",
258                               child, lookfor));
259
260                     rp = typeMalloc(ENTRY, 1);
261                     if (rp == 0)
262                         _nc_err_abort(MSG_NO_MEMORY);
263                     rp->tterm = thisterm;
264                     rp->nuses = 0;
265                     rp->next = lastread;
266                     lastread = rp;
267
268                     qp->uses[i].link = rp;
269                     foundit = TRUE;
270                 }
271             }
272
273             /* no good, mark this one unresolvable and complain */
274             if (!foundit) {
275                 unresolved++;
276                 total_unresolved++;
277
278                 _nc_curr_line = lookline;
279                 _nc_warning("resolution of use=%s failed", lookfor);
280                 qp->uses[i].link = 0;
281             }
282         }
283     }
284     if (total_unresolved) {
285         /* free entries read in off disk */
286         _nc_free_entries(lastread);
287         return (FALSE);
288     }
289
290     DEBUG(2, ("NAME RESOLUTION COMPLETED OK"));
291
292     /*
293      * OK, at this point all (char *) references in `name' members
294      * have been successfully converted to (ENTRY *) pointers in
295      * `link' members.  Time to do the actual merges.
296      */
297     if (fullresolve) {
298         do {
299             TERMTYPE merged;
300
301             keepgoing = FALSE;
302
303             for_entry_list(qp) {
304                 if (qp->nuses > 0) {
305                     DEBUG(2, ("%s: attempting merge",
306                               _nc_first_name(qp->tterm.term_names)));
307                     /*
308                      * If any of the use entries we're looking for is
309                      * incomplete, punt.  We'll catch this entry on a
310                      * subsequent pass.
311                      */
312                     for (i = 0; i < qp->nuses; i++)
313                         if (qp->uses[i].link->nuses) {
314                             DEBUG(2, ("%s: use entry %d unresolved",
315                                       _nc_first_name(qp->tterm.term_names), i));
316                             goto incomplete;
317                         }
318
319                     /*
320                      * First, make sure there is no garbage in the
321                      * merge block.  As a side effect, copy into
322                      * the merged entry the name field and string
323                      * table pointer.
324                      */
325                     _nc_copy_termtype(&merged, &(qp->tterm));
326
327                     /*
328                      * Now merge in each use entry in the proper
329                      * (reverse) order.
330                      */
331                     for (; qp->nuses; qp->nuses--)
332                         _nc_merge_entry(&merged,
333                                         &qp->uses[qp->nuses - 1].link->tterm);
334
335                     /*
336                      * Now merge in the original entry.
337                      */
338                     _nc_merge_entry(&merged, &qp->tterm);
339
340                     /*
341                      * Replace the original entry with the merged one.
342                      */
343                     FreeIfNeeded(qp->tterm.Booleans);
344                     FreeIfNeeded(qp->tterm.Numbers);
345                     FreeIfNeeded(qp->tterm.Strings);
346 #if NCURSES_XNAMES
347                     FreeIfNeeded(qp->tterm.ext_Names);
348 #endif
349                     qp->tterm = merged;
350                     _nc_wrap_entry(qp, TRUE);
351
352                     /*
353                      * We know every entry is resolvable because name resolution
354                      * didn't bomb.  So go back for another pass.
355                      */
356                     /* FALLTHRU */
357                   incomplete:
358                     keepgoing = TRUE;
359                 }
360             }
361         } while
362             (keepgoing);
363
364         DEBUG(2, ("MERGES COMPLETED OK"));
365     }
366
367     /*
368      * We'd like to free entries read in off disk at this point, but can't.
369      * The merge_entry() code doesn't copy the strings in the use entries,
370      * it just aliases them.  If this ever changes, do a
371      * free_entries(lastread) here.
372      */
373
374     DEBUG(2, ("RESOLUTION FINISHED"));
375
376     if (fullresolve)
377         if (_nc_check_termtype != 0) {
378             _nc_curr_col = -1;
379             for_entry_list(qp) {
380                 _nc_curr_line = qp->startline;
381                 _nc_set_type(_nc_first_name(qp->tterm.term_names));
382                 _nc_check_termtype2(&qp->tterm, literal);
383             }
384             DEBUG(2, ("SANITY CHECK FINISHED"));
385         }
386
387     return (TRUE);
388 }
389
390 /* obsolete: 20040705 */
391 NCURSES_EXPORT(int)
392 _nc_resolve_uses(bool fullresolve)
393 {
394     return _nc_resolve_uses2(fullresolve, FALSE);
395 }
396
397 /*
398  * This bit of legerdemain turns all the terminfo variable names into
399  * references to locations in the arrays Booleans, Numbers, and Strings ---
400  * precisely what's needed.
401  */
402
403 #undef CUR
404 #define CUR tp->
405
406 static void
407 sanity_check2(TERMTYPE *tp, bool literal)
408 {
409     if (!PRESENT(exit_attribute_mode)) {
410 #ifdef __UNUSED__               /* this casts too wide a net */
411         bool terminal_entry = !strchr(tp->term_names, '+');
412         if (terminal_entry &&
413             (PRESENT(set_attributes)
414              || PRESENT(enter_standout_mode)
415              || PRESENT(enter_underline_mode)
416              || PRESENT(enter_blink_mode)
417              || PRESENT(enter_bold_mode)
418              || PRESENT(enter_dim_mode)
419              || PRESENT(enter_secure_mode)
420              || PRESENT(enter_protected_mode)
421              || PRESENT(enter_reverse_mode)))
422             _nc_warning("no exit_attribute_mode");
423 #endif /* __UNUSED__ */
424         PAIRED(enter_standout_mode, exit_standout_mode);
425         PAIRED(enter_underline_mode, exit_underline_mode);
426     }
427
428     /* we do this check/fix in postprocess_termcap(), but some packagers
429      * prefer to bypass it...
430      */
431     if (!literal) {
432         if (acs_chars == 0
433             && enter_alt_charset_mode != 0
434             && exit_alt_charset_mode != 0)
435             acs_chars = strdup(VT_ACSC);
436         ANDMISSING(enter_alt_charset_mode, acs_chars);
437         ANDMISSING(exit_alt_charset_mode, acs_chars);
438     }
439
440     /* listed in structure-member order of first argument */
441     PAIRED(enter_alt_charset_mode, exit_alt_charset_mode);
442     ANDMISSING(enter_blink_mode, exit_attribute_mode);
443     ANDMISSING(enter_bold_mode, exit_attribute_mode);
444     PAIRED(exit_ca_mode, enter_ca_mode);
445     PAIRED(enter_delete_mode, exit_delete_mode);
446     ANDMISSING(enter_dim_mode, exit_attribute_mode);
447     PAIRED(enter_insert_mode, exit_insert_mode);
448     ANDMISSING(enter_secure_mode, exit_attribute_mode);
449     ANDMISSING(enter_protected_mode, exit_attribute_mode);
450     ANDMISSING(enter_reverse_mode, exit_attribute_mode);
451     PAIRED(from_status_line, to_status_line);
452     PAIRED(meta_off, meta_on);
453
454     PAIRED(prtr_on, prtr_off);
455     PAIRED(save_cursor, restore_cursor);
456     PAIRED(enter_xon_mode, exit_xon_mode);
457     PAIRED(enter_am_mode, exit_am_mode);
458     ANDMISSING(label_off, label_on);
459 #ifdef remove_clock
460     PAIRED(display_clock, remove_clock);
461 #endif
462     ANDMISSING(set_color_pair, initialize_pair);
463 }
464
465 /* obsolete: 20040705 */
466 static void
467 sanity_check(TERMTYPE *tp)
468 {
469     sanity_check2(tp, FALSE);
470 }
471
472 #if NO_LEAKS
473 NCURSES_EXPORT(void)
474 _nc_leaks_tic(void)
475 {
476     _nc_alloc_entry_leaks();
477     _nc_captoinfo_leaks();
478     _nc_comp_scan_leaks();
479 #if BROKEN_LINKER || USE_REENTRANT
480     _nc_names_leaks();
481     _nc_codes_leaks();
482 #endif
483     _nc_tic_expand(0, FALSE, 0);
484 }
485
486 NCURSES_EXPORT(void)
487 _nc_free_tic(int code)
488 {
489     _nc_leaks_tic();
490     _nc_free_tinfo(code);
491 }
492 #endif