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