]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/comp_parse.c
653a37412648de6a43470ad0cc1b2ee72fd336de
[ncurses.git] / ncurses / tinfo / comp_parse.c
1 /****************************************************************************
2  * Copyright (c) 1998-2012,2013 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.88 2013/06/03 11:05:54 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 *, int);
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         _nc_STRCPY(dst + len, "|", MAX_NAME_SIZE);
87         src = dst;
88     }
89     return src;
90 }
91 #define ForceBar(dst, src) ((strchr(src, '|') == 0) ? force_bar(dst, src) : src)
92
93 #if NCURSES_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 static bool
107 check_collisions(char *n1, char *n2, int counter)
108 {
109     char *pstart, *qstart, *pend, *qend;
110     char nc1[MAX_NAME_SIZE + 2];
111     char nc2[MAX_NAME_SIZE + 2];
112
113     n1 = ForceBar(nc1, n1);
114     n2 = ForceBar(nc2, n2);
115
116 #if NCURSES_USE_TERMCAP && NCURSES_XNAMES
117     if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
118         n1 = skip_index(n1);
119         n2 = skip_index(n2);
120     }
121 #endif
122
123     for (pstart = n1; (pend = strchr(pstart, '|')); pstart = pend + 1) {
124         for (qstart = n2; (qend = strchr(qstart, '|')); qstart = qend + 1) {
125             if ((pend - pstart == qend - qstart)
126                 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0) {
127                 if (counter > 0)
128                     (void) fprintf(stderr, "Name collision '%.*s' between\n",
129                                    (int) (pend - pstart), pstart);
130                 return (TRUE);
131             }
132         }
133     }
134
135     return (FALSE);
136 }
137
138 static char *
139 next_name(char *name)
140 {
141     if (*name != '\0')
142         ++name;
143     return name;
144 }
145
146 static char *
147 name_ending(char *name)
148 {
149     if (*name == '\0') {
150         name = 0;
151     } else {
152         while (*name != '\0' && *name != '|')
153             ++name;
154     }
155     return name;
156 }
157
158 /*
159  * Essentially, find the conflict reported in check_collisions() and remove
160  * it from the second name, unless that happens to be the last alias.
161  */
162 static bool
163 remove_collision(char *n1, char *n2)
164 {
165     char *p1 = n1;
166     char *p2 = n2;
167     char *pstart, *qstart, *pend, *qend;
168     bool removed = FALSE;
169
170 #if NCURSES_USE_TERMCAP && NCURSES_XNAMES
171     if ((_nc_syntax == SYN_TERMCAP) && _nc_user_definable) {
172         p1 = n1 = skip_index(n1);
173         p2 = n2 = skip_index(n2);
174     }
175 #else
176     (void) p1;
177 #endif
178
179     for (pstart = n1; (pend = name_ending(pstart)); pstart = next_name(pend)) {
180         for (qstart = n2; (qend = name_ending(qstart)); qstart = next_name(qend)) {
181             if ((pend - pstart == qend - qstart)
182                 && memcmp(pstart, qstart, (size_t) (pend - pstart)) == 0) {
183                 if (qstart != p2 || *qend == '|') {
184                     if (*qend == '|')
185                         ++qend;
186                     while ((*qstart++ = *qend++) != '\0') ;
187                     fprintf(stderr, "...now\t%s\n", p2);
188                 } else {
189                     fprintf(stderr, "Cannot remove alias '%.*s'\n",
190                             (int) (qend - qstart), qstart);
191                 }
192                 removed = TRUE;
193                 break;
194             }
195         }
196     }
197
198     return removed;
199 }
200
201 /* do any of the aliases in a pair of terminal names match? */
202 NCURSES_EXPORT(bool)
203 _nc_entry_match(char *n1, char *n2)
204 {
205     return check_collisions(n1, n2, 0);
206 }
207
208 /****************************************************************************
209  *
210  * Entry compiler and resolution logic
211  *
212  ****************************************************************************/
213
214 NCURSES_EXPORT(void)
215 _nc_read_entry_source(FILE *fp, char *buf,
216                       int literal, bool silent,
217                       bool(*hook) (ENTRY *))
218 /* slurp all entries in the given file into core */
219 {
220     ENTRY thisentry;
221     bool oldsuppress = _nc_suppress_warnings;
222     int immediate = 0;
223
224     if (silent)
225         _nc_suppress_warnings = TRUE;   /* shut the lexer up, too */
226
227     _nc_reset_input(fp, buf);
228     for (;;) {
229         memset(&thisentry, 0, sizeof(thisentry));
230         if (_nc_parse_entry(&thisentry, literal, silent) == ERR)
231             break;
232         if (!isalnum(UChar(thisentry.tterm.term_names[0])))
233             _nc_err_abort("terminal names must start with letter or digit");
234
235         /*
236          * This can be used for immediate compilation of entries with no "use="
237          * references to disk.  That avoids consuming a lot of memory when the
238          * resolution code could fetch entries off disk.
239          */
240         if (hook != NULLHOOK && (*hook) (&thisentry)) {
241             immediate++;
242         } else {
243             enqueue(&thisentry);
244             /*
245              * The enqueued entry is copied with _nc_copy_termtype(), so we can
246              * free some of the data from thisentry, i.e., the arrays.
247              */
248             FreeIfNeeded(thisentry.tterm.Booleans);
249             FreeIfNeeded(thisentry.tterm.Numbers);
250             FreeIfNeeded(thisentry.tterm.Strings);
251 #if NCURSES_XNAMES
252             FreeIfNeeded(thisentry.tterm.ext_Names);
253 #endif
254         }
255     }
256
257     if (_nc_tail) {
258         /* set up the head pointer */
259         for (_nc_head = _nc_tail; _nc_head->last; _nc_head = _nc_head->last)
260             continue;
261
262         DEBUG(1, ("head = %s", _nc_head->tterm.term_names));
263         DEBUG(1, ("tail = %s", _nc_tail->tterm.term_names));
264     }
265 #ifdef TRACE
266     else if (!immediate)
267         DEBUG(1, ("no entries parsed"));
268 #endif
269
270     _nc_suppress_warnings = oldsuppress;
271 }
272
273 NCURSES_EXPORT(int)
274 _nc_resolve_uses2(bool fullresolve, bool literal)
275 /* try to resolve all use capabilities */
276 {
277     ENTRY *qp, *rp, *lastread = 0;
278     bool keepgoing;
279     unsigned i;
280     int unresolved, total_unresolved, multiples;
281
282     DEBUG(2, ("RESOLUTION BEGINNING"));
283
284     /*
285      * Check for multiple occurrences of the same name.
286      */
287     multiples = 0;
288     for_entry_list(qp) {
289         int matchcount = 0;
290
291         for_entry_list(rp) {
292             if (qp > rp
293                 && check_collisions(qp->tterm.term_names,
294                                     rp->tterm.term_names,
295                                     matchcount + 1)) {
296                 if (!matchcount++) {
297                     (void) fprintf(stderr, "\t%s\n", rp->tterm.term_names);
298                 }
299                 (void) fprintf(stderr, "and\t%s\n", qp->tterm.term_names);
300                 if (!remove_collision(rp->tterm.term_names,
301                                       qp->tterm.term_names)) {
302                     ++multiples;
303                 }
304             }
305         }
306     }
307     if (multiples > 0)
308         return (FALSE);
309
310     DEBUG(2, ("NO MULTIPLE NAME OCCURRENCES"));
311
312     /*
313      * First resolution stage: compute link pointers corresponding to names.
314      */
315     total_unresolved = 0;
316     _nc_curr_col = -1;
317     for_entry_list(qp) {
318         unresolved = 0;
319         for (i = 0; i < qp->nuses; i++) {
320             bool foundit;
321             char *child = _nc_first_name(qp->tterm.term_names);
322             char *lookfor = qp->uses[i].name;
323             long lookline = qp->uses[i].line;
324
325             foundit = FALSE;
326
327             _nc_set_type(child);
328
329             /* first, try to resolve from in-core records */
330             for_entry_list(rp) {
331                 if (rp != qp
332                     && _nc_name_match(rp->tterm.term_names, lookfor, "|")) {
333                     DEBUG(2, ("%s: resolving use=%s (in core)",
334                               child, lookfor));
335
336                     qp->uses[i].link = rp;
337                     foundit = TRUE;
338                 }
339             }
340
341             /* if that didn't work, try to merge in a compiled entry */
342             if (!foundit) {
343                 TERMTYPE thisterm;
344                 char filename[PATH_MAX];
345
346                 memset(&thisterm, 0, sizeof(thisterm));
347                 if (_nc_read_entry(lookfor, filename, &thisterm) == 1) {
348                     DEBUG(2, ("%s: resolving use=%s (compiled)",
349                               child, lookfor));
350
351                     TYPE_MALLOC(ENTRY, 1, rp);
352                     rp->tterm = thisterm;
353                     rp->nuses = 0;
354                     rp->next = lastread;
355                     lastread = rp;
356
357                     qp->uses[i].link = rp;
358                     foundit = TRUE;
359                 }
360             }
361
362             /* no good, mark this one unresolvable and complain */
363             if (!foundit) {
364                 unresolved++;
365                 total_unresolved++;
366
367                 _nc_curr_line = (int) lookline;
368                 _nc_warning("resolution of use=%s failed", lookfor);
369                 qp->uses[i].link = 0;
370             }
371         }
372     }
373     if (total_unresolved) {
374         /* free entries read in off disk */
375         _nc_free_entries(lastread);
376         return (FALSE);
377     }
378
379     DEBUG(2, ("NAME RESOLUTION COMPLETED OK"));
380
381     /*
382      * OK, at this point all (char *) references in `name' members
383      * have been successfully converted to (ENTRY *) pointers in
384      * `link' members.  Time to do the actual merges.
385      */
386     if (fullresolve) {
387         do {
388             TERMTYPE merged;
389
390             keepgoing = FALSE;
391
392             for_entry_list(qp) {
393                 if (qp->nuses > 0) {
394                     DEBUG(2, ("%s: attempting merge",
395                               _nc_first_name(qp->tterm.term_names)));
396                     /*
397                      * If any of the use entries we're looking for is
398                      * incomplete, punt.  We'll catch this entry on a
399                      * subsequent pass.
400                      */
401                     for (i = 0; i < qp->nuses; i++)
402                         if (qp->uses[i].link->nuses) {
403                             DEBUG(2, ("%s: use entry %d unresolved",
404                                       _nc_first_name(qp->tterm.term_names), i));
405                             goto incomplete;
406                         }
407
408                     /*
409                      * First, make sure there is no garbage in the
410                      * merge block.  As a side effect, copy into
411                      * the merged entry the name field and string
412                      * table pointer.
413                      */
414                     _nc_copy_termtype(&merged, &(qp->tterm));
415
416                     /*
417                      * Now merge in each use entry in the proper
418                      * (reverse) order.
419                      */
420                     for (; qp->nuses; qp->nuses--)
421                         _nc_merge_entry(&merged,
422                                         &qp->uses[qp->nuses - 1].link->tterm);
423
424                     /*
425                      * Now merge in the original entry.
426                      */
427                     _nc_merge_entry(&merged, &qp->tterm);
428
429                     /*
430                      * Replace the original entry with the merged one.
431                      */
432                     FreeIfNeeded(qp->tterm.Booleans);
433                     FreeIfNeeded(qp->tterm.Numbers);
434                     FreeIfNeeded(qp->tterm.Strings);
435 #if NCURSES_XNAMES
436                     FreeIfNeeded(qp->tterm.ext_Names);
437 #endif
438                     qp->tterm = merged;
439                     _nc_wrap_entry(qp, TRUE);
440
441                     /*
442                      * We know every entry is resolvable because name resolution
443                      * didn't bomb.  So go back for another pass.
444                      */
445                     /* FALLTHRU */
446                   incomplete:
447                     keepgoing = TRUE;
448                 }
449             }
450         } while
451             (keepgoing);
452
453         DEBUG(2, ("MERGES COMPLETED OK"));
454     }
455
456     /*
457      * We'd like to free entries read in off disk at this point, but can't.
458      * The merge_entry() code doesn't copy the strings in the use entries,
459      * it just aliases them.  If this ever changes, do a
460      * free_entries(lastread) here.
461      */
462
463     DEBUG(2, ("RESOLUTION FINISHED"));
464
465     if (fullresolve)
466         if (_nc_check_termtype != 0) {
467             _nc_curr_col = -1;
468             for_entry_list(qp) {
469                 _nc_curr_line = (int) qp->startline;
470                 _nc_set_type(_nc_first_name(qp->tterm.term_names));
471                 /*
472                  * tic overrides this function pointer to provide more verbose
473                  * checking.
474                  */
475                 if (_nc_check_termtype2 != sanity_check2) {
476                     SCREEN *save_SP = SP;
477                     SCREEN fake_sp;
478                     TERMINAL fake_tm;
479                     TERMINAL *save_tm = cur_term;
480
481                     /*
482                      * Setup so that tic can use ordinary terminfo interface
483                      * to obtain capability information.
484                      */
485                     memset(&fake_sp, 0, sizeof(fake_sp));
486                     memset(&fake_tm, 0, sizeof(fake_tm));
487                     fake_sp._term = &fake_tm;
488                     fake_tm.type = qp->tterm;
489                     _nc_set_screen(&fake_sp);
490                     set_curterm(&fake_tm);
491
492                     _nc_check_termtype2(&qp->tterm, literal);
493
494                     _nc_set_screen(save_SP);
495                     set_curterm(save_tm);
496                 } else {
497                     fixup_acsc(&qp->tterm, literal);
498                 }
499             }
500             DEBUG(2, ("SANITY CHECK FINISHED"));
501         }
502
503     return (TRUE);
504 }
505
506 /* obsolete: 20040705 */
507 NCURSES_EXPORT(int)
508 _nc_resolve_uses(bool fullresolve)
509 {
510     return _nc_resolve_uses2(fullresolve, FALSE);
511 }
512
513 /*
514  * This bit of legerdemain turns all the terminfo variable names into
515  * references to locations in the arrays Booleans, Numbers, and Strings ---
516  * precisely what's needed.
517  */
518
519 #undef CUR
520 #define CUR tp->
521
522 static void
523 fixup_acsc(TERMTYPE *tp, int literal)
524 {
525     if (!literal) {
526         if (acs_chars == 0
527             && enter_alt_charset_mode != 0
528             && exit_alt_charset_mode != 0)
529             acs_chars = strdup(VT_ACSC);
530     }
531 }
532
533 static void
534 sanity_check2(TERMTYPE *tp, bool literal)
535 {
536     if (!PRESENT(exit_attribute_mode)) {
537 #ifdef __UNUSED__               /* this casts too wide a net */
538         bool terminal_entry = !strchr(tp->term_names, '+');
539         if (terminal_entry &&
540             (PRESENT(set_attributes)
541              || PRESENT(enter_standout_mode)
542              || PRESENT(enter_underline_mode)
543              || PRESENT(enter_blink_mode)
544              || PRESENT(enter_bold_mode)
545              || PRESENT(enter_dim_mode)
546              || PRESENT(enter_secure_mode)
547              || PRESENT(enter_protected_mode)
548              || PRESENT(enter_reverse_mode)))
549             _nc_warning("no exit_attribute_mode");
550 #endif /* __UNUSED__ */
551         PAIRED(enter_standout_mode, exit_standout_mode);
552         PAIRED(enter_underline_mode, exit_underline_mode);
553     }
554
555     /* we do this check/fix in postprocess_termcap(), but some packagers
556      * prefer to bypass it...
557      */
558     if (!literal) {
559         fixup_acsc(tp, literal);
560         ANDMISSING(enter_alt_charset_mode, acs_chars);
561         ANDMISSING(exit_alt_charset_mode, acs_chars);
562     }
563
564     /* listed in structure-member order of first argument */
565     PAIRED(enter_alt_charset_mode, exit_alt_charset_mode);
566     ANDMISSING(enter_blink_mode, exit_attribute_mode);
567     ANDMISSING(enter_bold_mode, exit_attribute_mode);
568     PAIRED(exit_ca_mode, enter_ca_mode);
569     PAIRED(enter_delete_mode, exit_delete_mode);
570     ANDMISSING(enter_dim_mode, exit_attribute_mode);
571     PAIRED(enter_insert_mode, exit_insert_mode);
572     ANDMISSING(enter_secure_mode, exit_attribute_mode);
573     ANDMISSING(enter_protected_mode, exit_attribute_mode);
574     ANDMISSING(enter_reverse_mode, exit_attribute_mode);
575     PAIRED(from_status_line, to_status_line);
576     PAIRED(meta_off, meta_on);
577
578     PAIRED(prtr_on, prtr_off);
579     PAIRED(save_cursor, restore_cursor);
580     PAIRED(enter_xon_mode, exit_xon_mode);
581     PAIRED(enter_am_mode, exit_am_mode);
582     ANDMISSING(label_off, label_on);
583 #ifdef remove_clock
584     PAIRED(display_clock, remove_clock);
585 #endif
586     ANDMISSING(set_color_pair, initialize_pair);
587 }
588
589 /* obsolete: 20040705 */
590 static void
591 sanity_check(TERMTYPE *tp)
592 {
593     sanity_check2(tp, FALSE);
594 }
595
596 #if NO_LEAKS
597 NCURSES_EXPORT(void)
598 _nc_leaks_tic(void)
599 {
600     _nc_alloc_entry_leaks();
601     _nc_captoinfo_leaks();
602     _nc_comp_scan_leaks();
603 #if BROKEN_LINKER || USE_REENTRANT
604     _nc_names_leaks();
605     _nc_codes_leaks();
606 #endif
607     _nc_tic_expand(0, FALSE, 0);
608 }
609
610 NCURSES_EXPORT(void)
611 _nc_free_tic(int code)
612 {
613     _nc_leaks_tic();
614     _nc_free_tinfo(code);
615 }
616 #endif