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