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