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