]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/parse_entry.c
ncurses 6.0 - patch 20170701
[ncurses.git] / ncurses / tinfo / parse_entry.c
1 /****************************************************************************
2  * Copyright (c) 1998-2016,2017 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  *      parse_entry.c -- compile one terminfo or termcap entry
37  *
38  *      Get an exact in-core representation of an entry.  Don't
39  *      try to resolve use or tc capabilities, that is someone
40  *      else's job.  Depends on the lexical analyzer to get tokens
41  *      from the input stream.
42  */
43
44 #define __INTERNAL_CAPS_VISIBLE
45 #include <curses.priv.h>
46
47 #include <ctype.h>
48 #include <tic.h>
49
50 MODULE_ID("$Id: parse_entry.c,v 1.86 2017/06/28 00:53:12 tom Exp $")
51
52 #ifdef LINT
53 static short const parametrized[] =
54 {0};
55 #else
56 #include <parametrized.h>
57 #endif
58
59 static void postprocess_termcap(TERMTYPE2 *, bool);
60 static void postprocess_terminfo(TERMTYPE2 *);
61 static struct name_table_entry const *lookup_fullname(const char *name);
62
63 #if NCURSES_XNAMES
64
65 static struct name_table_entry const *
66 _nc_extend_names(ENTRY * entryp, char *name, int token_type)
67 {
68     static struct name_table_entry temp;
69     TERMTYPE2 *tp = &(entryp->tterm);
70     unsigned offset = 0;
71     unsigned actual;
72     unsigned tindex;
73     unsigned first, last, n;
74     bool found;
75
76     switch (token_type) {
77     case BOOLEAN:
78         first = 0;
79         last = tp->ext_Booleans;
80         offset = tp->ext_Booleans;
81         tindex = tp->num_Booleans;
82         break;
83     case NUMBER:
84         first = tp->ext_Booleans;
85         last = tp->ext_Numbers + first;
86         offset = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
87         tindex = tp->num_Numbers;
88         break;
89     case STRING:
90         first = (unsigned) (tp->ext_Booleans + tp->ext_Numbers);
91         last = tp->ext_Strings + first;
92         offset = (unsigned) (tp->ext_Booleans + tp->ext_Numbers + tp->ext_Strings);
93         tindex = tp->num_Strings;
94         break;
95     case CANCEL:
96         actual = NUM_EXT_NAMES(tp);
97         for (n = 0; n < actual; n++) {
98             if (!strcmp(name, tp->ext_Names[n])) {
99                 if (n > (unsigned) (tp->ext_Booleans + tp->ext_Numbers)) {
100                     token_type = STRING;
101                 } else if (n > tp->ext_Booleans) {
102                     token_type = NUMBER;
103                 } else {
104                     token_type = BOOLEAN;
105                 }
106                 return _nc_extend_names(entryp, name, token_type);
107             }
108         }
109         /* Well, we are given a cancel for a name that we don't recognize */
110         return _nc_extend_names(entryp, name, STRING);
111     default:
112         return 0;
113     }
114
115     /* Adjust the 'offset' (insertion-point) to keep the lists of extended
116      * names sorted.
117      */
118     for (n = first, found = FALSE; n < last; n++) {
119         int cmp = strcmp(tp->ext_Names[n], name);
120         if (cmp == 0)
121             found = TRUE;
122         if (cmp >= 0) {
123             offset = n;
124             tindex = n - first;
125             switch (token_type) {
126             case BOOLEAN:
127                 tindex += BOOLCOUNT;
128                 break;
129             case NUMBER:
130                 tindex += NUMCOUNT;
131                 break;
132             case STRING:
133                 tindex += STRCOUNT;
134                 break;
135             }
136             break;
137         }
138     }
139
140 #define for_each_value(max) \
141         for (last = (unsigned) (max - 1); last > tindex; last--)
142
143     if (!found) {
144         switch (token_type) {
145         case BOOLEAN:
146             tp->ext_Booleans++;
147             tp->num_Booleans++;
148             TYPE_REALLOC(NCURSES_SBOOL, tp->num_Booleans, tp->Booleans);
149             for_each_value(tp->num_Booleans)
150                 tp->Booleans[last] = tp->Booleans[last - 1];
151             break;
152         case NUMBER:
153             tp->ext_Numbers++;
154             tp->num_Numbers++;
155             TYPE_REALLOC(NCURSES_INT2, tp->num_Numbers, tp->Numbers);
156             for_each_value(tp->num_Numbers)
157                 tp->Numbers[last] = tp->Numbers[last - 1];
158             break;
159         case STRING:
160             tp->ext_Strings++;
161             tp->num_Strings++;
162             TYPE_REALLOC(char *, tp->num_Strings, tp->Strings);
163             for_each_value(tp->num_Strings)
164                 tp->Strings[last] = tp->Strings[last - 1];
165             break;
166         }
167         actual = NUM_EXT_NAMES(tp);
168         TYPE_REALLOC(char *, actual, tp->ext_Names);
169         while (--actual > offset)
170             tp->ext_Names[actual] = tp->ext_Names[actual - 1];
171         tp->ext_Names[offset] = _nc_save_str(name);
172     }
173
174     temp.nte_name = tp->ext_Names[offset];
175     temp.nte_type = token_type;
176     temp.nte_index = (short) tindex;
177     temp.nte_link = -1;
178
179     return &temp;
180 }
181 #endif /* NCURSES_XNAMES */
182
183 /*
184  *      int
185  *      _nc_parse_entry(entry, literal, silent)
186  *
187  *      Compile one entry.  Doesn't try to resolve use or tc capabilities.
188  *
189  *      found-forward-use = FALSE
190  *      re-initialise internal arrays
191  *      get_token();
192  *      if the token was not a name in column 1, complain and die
193  *      save names in entry's string table
194  *      while (get_token() is not EOF and not NAMES)
195  *              check for existence and type-correctness
196  *              enter cap into structure
197  *              if STRING
198  *                  save string in entry's string table
199  *      push back token
200  */
201
202 #define BAD_TC_USAGE if (!bad_tc_usage) \
203         { bad_tc_usage = TRUE; \
204          _nc_warning("Legacy termcap allows only a trailing tc= clause"); }
205
206 #define MAX_NUMBER 0x7fff       /* positive shorts only */
207
208 NCURSES_EXPORT(int)
209 _nc_parse_entry(ENTRY * entryp, int literal, bool silent)
210 {
211     int token_type;
212     struct name_table_entry const *entry_ptr;
213     char *ptr, *base;
214     bool bad_tc_usage = FALSE;
215
216     token_type = _nc_get_token(silent);
217
218     if (token_type == EOF)
219         return (EOF);
220     if (token_type != NAMES)
221         _nc_err_abort("Entry does not start with terminal names in column one");
222
223     _nc_init_entry(entryp);
224
225     entryp->cstart = _nc_comment_start;
226     entryp->cend = _nc_comment_end;
227     entryp->startline = _nc_start_line;
228     DEBUG(2, ("Comment range is %ld to %ld", entryp->cstart, entryp->cend));
229
230     /*
231      * Strip off the 2-character termcap name, if present.  Originally termcap
232      * used that as an indexing aid.  We can retain 2-character terminfo names,
233      * but note that they would be lost if we translate to/from termcap.  This
234      * feature is supposedly obsolete since "newer" BSD implementations do not
235      * use it; however our reference for this feature is SunOS 4.x, which
236      * implemented it.  Note that the resulting terminal type was never the
237      * 2-character name, but was instead the first alias after that.
238      */
239 #define ok_TC2(s) (isgraph(UChar(s)) && (s) != '|')
240     ptr = _nc_curr_token.tk_name;
241     if (_nc_syntax == SYN_TERMCAP
242 #if NCURSES_XNAMES
243         && !_nc_user_definable
244 #endif
245         ) {
246         if (ok_TC2(ptr[0]) && ok_TC2(ptr[1]) && (ptr[2] == '|')) {
247             ptr += 3;
248             _nc_curr_token.tk_name[2] = '\0';
249         }
250     }
251
252     entryp->tterm.str_table = entryp->tterm.term_names = _nc_save_str(ptr);
253
254     if (entryp->tterm.str_table == 0)
255         return (ERR);
256
257     DEBUG(1, ("Starting '%s'", ptr));
258
259     /*
260      * We do this because the one-token lookahead in the parse loop
261      * results in the terminal type getting prematurely set to correspond
262      * to that of the next entry.
263      */
264     _nc_set_type(_nc_first_name(entryp->tterm.term_names));
265
266     /* check for overly-long names and aliases */
267     for (base = entryp->tterm.term_names; (ptr = strchr(base, '|')) != 0;
268          base = ptr + 1) {
269         if (ptr - base > MAX_ALIAS) {
270             _nc_warning("%s `%.*s' may be too long",
271                         (base == entryp->tterm.term_names)
272                         ? "primary name"
273                         : "alias",
274                         (int) (ptr - base), base);
275         }
276     }
277
278     entryp->nuses = 0;
279
280     for (token_type = _nc_get_token(silent);
281          token_type != EOF && token_type != NAMES;
282          token_type = _nc_get_token(silent)) {
283         bool is_use = (strcmp(_nc_curr_token.tk_name, "use") == 0);
284         bool is_tc = !is_use && (strcmp(_nc_curr_token.tk_name, "tc") == 0);
285         if (is_use || is_tc) {
286             entryp->uses[entryp->nuses].name = _nc_save_str(_nc_curr_token.tk_valstring);
287             entryp->uses[entryp->nuses].line = _nc_curr_line;
288             if (VALID_STRING(entryp->uses[entryp->nuses].name)) {
289                 entryp->nuses++;
290                 if (entryp->nuses > 1 && is_tc) {
291                     BAD_TC_USAGE
292                 }
293             }
294         } else {
295             /* normal token lookup */
296             entry_ptr = _nc_find_entry(_nc_curr_token.tk_name,
297                                        _nc_get_hash_table(_nc_syntax));
298
299             /*
300              * Our kluge to handle aliasing.  The reason it's done
301              * this ugly way, with a linear search, is so the hashing
302              * machinery doesn't have to be made really complicated
303              * (also we get better warnings this way).  No point in
304              * making this case fast, aliased caps aren't common now
305              * and will get rarer.
306              */
307             if (entry_ptr == NOTFOUND) {
308                 const struct alias *ap;
309
310                 if (_nc_syntax == SYN_TERMCAP) {
311                     if (entryp->nuses != 0) {
312                         BAD_TC_USAGE
313                     }
314                     for (ap = _nc_get_alias_table(TRUE); ap->from; ap++)
315                         if (strcmp(ap->from, _nc_curr_token.tk_name) == 0) {
316                             if (ap->to == (char *) 0) {
317                                 _nc_warning("%s (%s termcap extension) ignored",
318                                             ap->from, ap->source);
319                                 goto nexttok;
320                             }
321
322                             entry_ptr = _nc_find_entry(ap->to,
323                                                        _nc_get_hash_table(TRUE));
324                             if (entry_ptr && !silent)
325                                 _nc_warning("%s (%s termcap extension) aliased to %s",
326                                             ap->from, ap->source, ap->to);
327                             break;
328                         }
329                 } else {        /* if (_nc_syntax == SYN_TERMINFO) */
330                     for (ap = _nc_get_alias_table(FALSE); ap->from; ap++)
331                         if (strcmp(ap->from, _nc_curr_token.tk_name) == 0) {
332                             if (ap->to == (char *) 0) {
333                                 _nc_warning("%s (%s terminfo extension) ignored",
334                                             ap->from, ap->source);
335                                 goto nexttok;
336                             }
337
338                             entry_ptr = _nc_find_entry(ap->to,
339                                                        _nc_get_hash_table(FALSE));
340                             if (entry_ptr && !silent)
341                                 _nc_warning("%s (%s terminfo extension) aliased to %s",
342                                             ap->from, ap->source, ap->to);
343                             break;
344                         }
345
346                     if (entry_ptr == NOTFOUND) {
347                         entry_ptr = lookup_fullname(_nc_curr_token.tk_name);
348                     }
349                 }
350             }
351 #if NCURSES_XNAMES
352             /*
353              * If we have extended-names active, we will automatically
354              * define a name based on its context.
355              */
356             if (entry_ptr == NOTFOUND
357                 && _nc_user_definable
358                 && (entry_ptr = _nc_extend_names(entryp,
359                                                  _nc_curr_token.tk_name,
360                                                  token_type)) != 0) {
361                 if (_nc_tracing >= DEBUG_LEVEL(1))
362                     _nc_warning("extended capability '%s'", _nc_curr_token.tk_name);
363             }
364 #endif /* NCURSES_XNAMES */
365
366             /* can't find this cap name, not even as an alias */
367             if (entry_ptr == NOTFOUND) {
368                 if (!silent)
369                     _nc_warning("unknown capability '%s'",
370                                 _nc_curr_token.tk_name);
371                 continue;
372             }
373
374             /* deal with bad type/value combinations. */
375             if (token_type == CANCEL) {
376                 /*
377                  * Prefer terminfo in this (long-obsolete) ambiguity:
378                  */
379                 if (!strcmp("ma", _nc_curr_token.tk_name)) {
380                     entry_ptr = _nc_find_type_entry("ma", NUMBER,
381                                                     _nc_syntax != 0);
382                     assert(entry_ptr != 0);
383                 }
384             } else if (entry_ptr->nte_type != token_type) {
385                 /*
386                  * Nasty special cases here handle situations in which type
387                  * information can resolve name clashes.  Normal lookup
388                  * finds the last instance in the capability table of a
389                  * given name, regardless of type.  find_type_entry looks
390                  * for a first matching instance with given type.  So as
391                  * long as all ambiguous names occur in pairs of distinct
392                  * type, this will do the job.
393                  */
394
395                 if (token_type == NUMBER
396                     && !strcmp("ma", _nc_curr_token.tk_name)) {
397                     /* tell max_attributes from arrow_key_map */
398                     entry_ptr = _nc_find_type_entry("ma", NUMBER,
399                                                     _nc_syntax != 0);
400                     assert(entry_ptr != 0);
401
402                 } else if (token_type == STRING
403                            && !strcmp("MT", _nc_curr_token.tk_name)) {
404                     /* map terminfo's string MT to MT */
405                     entry_ptr = _nc_find_type_entry("MT", STRING,
406                                                     _nc_syntax != 0);
407                     assert(entry_ptr != 0);
408
409                 } else if (token_type == BOOLEAN
410                            && entry_ptr->nte_type == STRING) {
411                     /* treat strings without following "=" as empty strings */
412                     token_type = STRING;
413                 } else {
414                     /* we couldn't recover; skip this token */
415                     if (!silent) {
416                         const char *type_name;
417                         switch (entry_ptr->nte_type) {
418                         case BOOLEAN:
419                             type_name = "boolean";
420                             break;
421                         case STRING:
422                             type_name = "string";
423                             break;
424                         case NUMBER:
425                             type_name = "numeric";
426                             break;
427                         default:
428                             type_name = "unknown";
429                             break;
430                         }
431                         _nc_warning("wrong type used for %s capability '%s'",
432                                     type_name, _nc_curr_token.tk_name);
433                     }
434                     continue;
435                 }
436             }
437
438             /* now we know that the type/value combination is OK */
439             switch (token_type) {
440             case CANCEL:
441                 switch (entry_ptr->nte_type) {
442                 case BOOLEAN:
443                     entryp->tterm.Booleans[entry_ptr->nte_index] = CANCELLED_BOOLEAN;
444                     break;
445
446                 case NUMBER:
447                     entryp->tterm.Numbers[entry_ptr->nte_index] = CANCELLED_NUMERIC;
448                     break;
449
450                 case STRING:
451                     entryp->tterm.Strings[entry_ptr->nte_index] = CANCELLED_STRING;
452                     break;
453                 }
454                 break;
455
456             case BOOLEAN:
457                 entryp->tterm.Booleans[entry_ptr->nte_index] = TRUE;
458                 break;
459
460             case NUMBER:
461                 if (_nc_curr_token.tk_valnumber > MAX_NUMBER) {
462                     entryp->tterm.Numbers[entry_ptr->nte_index] = MAX_NUMBER;
463                 } else {
464                     entryp->tterm.Numbers[entry_ptr->nte_index] =
465                         (short) _nc_curr_token.tk_valnumber;
466                 }
467                 break;
468
469             case STRING:
470                 ptr = _nc_curr_token.tk_valstring;
471                 if (_nc_syntax == SYN_TERMCAP)
472                     ptr = _nc_captoinfo(_nc_curr_token.tk_name,
473                                         ptr,
474                                         parametrized[entry_ptr->nte_index]);
475                 entryp->tterm.Strings[entry_ptr->nte_index] = _nc_save_str(ptr);
476                 break;
477
478             default:
479                 if (!silent)
480                     _nc_warning("unknown token type");
481                 _nc_panic_mode((char) ((_nc_syntax == SYN_TERMCAP) ? ':' : ','));
482                 continue;
483             }
484         }                       /* end else cur_token.name != "use" */
485       nexttok:
486         continue;               /* cannot have a label w/o statement */
487     }                           /* endwhile (not EOF and not NAMES) */
488
489     _nc_push_token(token_type);
490     _nc_set_type(_nc_first_name(entryp->tterm.term_names));
491
492     /*
493      * Try to deduce as much as possible from extension capabilities
494      * (this includes obsolete BSD capabilities).  Sigh...it would be more
495      * space-efficient to call this after use resolution, but it has
496      * to be done before entry allocation is wrapped up.
497      */
498     if (!literal) {
499         if (_nc_syntax == SYN_TERMCAP) {
500             bool has_base_entry = FALSE;
501
502             /*
503              * Don't insert defaults if this is a `+' entry meant only
504              * for inclusion in other entries (not sure termcap ever
505              * had these, actually).
506              */
507             if (strchr(entryp->tterm.term_names, '+')) {
508                 has_base_entry = TRUE;
509             } else {
510                 unsigned i;
511                 /*
512                  * Otherwise, look for a base entry that will already
513                  * have picked up defaults via translation.
514                  */
515                 for (i = 0; i < entryp->nuses; i++)
516                     if (!strchr((char *) entryp->uses[i].name, '+'))
517                         has_base_entry = TRUE;
518             }
519
520             postprocess_termcap(&entryp->tterm, has_base_entry);
521         } else
522             postprocess_terminfo(&entryp->tterm);
523     }
524     _nc_wrap_entry(entryp, FALSE);
525
526     return (OK);
527 }
528
529 NCURSES_EXPORT(int)
530 _nc_capcmp(const char *s, const char *t)
531 /* compare two string capabilities, stripping out padding */
532 {
533     bool ok_s = VALID_STRING(s);
534     bool ok_t = VALID_STRING(t);
535
536     if (ok_s && ok_t) {
537         for (;;) {
538             if (s[0] == '$' && s[1] == '<') {
539                 for (s += 2;; s++) {
540                     if (!(isdigit(UChar(*s))
541                           || *s == '.'
542                           || *s == '*'
543                           || *s == '/'
544                           || *s == '>')) {
545                         break;
546                     }
547                 }
548             }
549
550             if (t[0] == '$' && t[1] == '<') {
551                 for (t += 2;; t++) {
552                     if (!(isdigit(UChar(*t))
553                           || *t == '.'
554                           || *t == '*'
555                           || *t == '/'
556                           || *t == '>')) {
557                         break;
558                     }
559                 }
560             }
561
562             /* we've now pushed s and t past any padding they pointed at */
563
564             if (*s == '\0' && *t == '\0')
565                 return (0);
566
567             if (*s != *t)
568                 return (*t - *s);
569
570             /* else *s == *t but one is not NUL, so continue */
571             s++, t++;
572         }
573     } else if (ok_s || ok_t) {
574         return 1;
575     }
576     return 0;
577 }
578
579 static void
580 append_acs0(string_desc * dst, int code, int src)
581 {
582     if (src != 0) {
583         char temp[3];
584         temp[0] = (char) code;
585         temp[1] = (char) src;
586         temp[2] = 0;
587         _nc_safe_strcat(dst, temp);
588     }
589 }
590
591 static void
592 append_acs(string_desc * dst, int code, char *src)
593 {
594     if (VALID_STRING(src) && strlen(src) == 1) {
595         append_acs0(dst, code, *src);
596     }
597 }
598
599 /*
600  * The ko capability, if present, consists of a comma-separated capability
601  * list.  For each capability, we may assume there is a keycap that sends the
602  * string which is the value of that capability.
603  */
604 #define DATA(from, to) { { from }, { to } }
605 typedef struct {
606     const char from[3];
607     const char to[6];
608 } assoc;
609 static assoc const ko_xlate[] =
610 {
611     DATA("al", "kil1"),         /* insert line key  -> KEY_IL    */
612     DATA("bt", "kcbt"),         /* back tab         -> KEY_BTAB  */
613     DATA("cd", "ked"),          /* clear-to-eos key -> KEY_EOL   */
614     DATA("ce", "kel"),          /* clear-to-eol key -> KEY_EOS   */
615     DATA("cl", "kclr"),         /* clear key        -> KEY_CLEAR */
616     DATA("ct", "tbc"),          /* clear all tabs   -> KEY_CATAB */
617     DATA("dc", "kdch1"),        /* delete char      -> KEY_DC    */
618     DATA("dl", "kdl1"),         /* delete line      -> KEY_DL    */
619     DATA("do", "kcud1"),        /* down key         -> KEY_DOWN  */
620     DATA("ei", "krmir"),        /* exit insert key  -> KEY_EIC   */
621     DATA("ho", "khome"),        /* home key         -> KEY_HOME  */
622     DATA("ic", "kich1"),        /* insert char key  -> KEY_IC    */
623     DATA("im", "kIC"),          /* insert-mode key  -> KEY_SIC   */
624     DATA("le", "kcub1"),        /* le key           -> KEY_LEFT  */
625     DATA("nd", "kcuf1"),        /* nd key           -> KEY_RIGHT */
626     DATA("nl", "kent"),         /* new line key     -> KEY_ENTER */
627     DATA("st", "khts"),         /* set-tab key      -> KEY_STAB  */
628     DATA("ta", ""),
629     DATA("up", "kcuu1"),        /* up-arrow key     -> KEY_UP    */
630 };
631
632 /*
633  * This routine fills in string caps that either had defaults under
634  * termcap or can be manufactured from obsolete termcap capabilities.
635  * It was lifted from Ross Ridge's mytinfo package.
636  */
637
638 static const char C_CR[] = "\r";
639 static const char C_LF[] = "\n";
640 static const char C_BS[] = "\b";
641 static const char C_HT[] = "\t";
642
643 /*
644  * Note that WANTED and PRESENT are not simple inverses!  If a capability
645  * has been explicitly cancelled, it's not considered WANTED.
646  */
647 #define WANTED(s)       ((s) == ABSENT_STRING)
648 #define PRESENT(s)      (((s) != ABSENT_STRING) && ((s) != CANCELLED_STRING))
649
650 /*
651  * This bit of legerdemain turns all the terminfo variable names into
652  * references to locations in the arrays Booleans, Numbers, and Strings ---
653  * precisely what's needed.
654  */
655
656 #undef CUR
657 #define CUR tp->
658
659 static void
660 postprocess_termcap(TERMTYPE2 *tp, bool has_base)
661 {
662     char buf[MAX_LINE * 2 + 2];
663     string_desc result;
664
665     /*
666      * TERMCAP DEFAULTS AND OBSOLETE-CAPABILITY TRANSLATIONS
667      *
668      * This first part of the code is the functional inverse of the
669      * fragment in capdefaults.c.
670      * ----------------------------------------------------------------------
671      */
672
673     /* if there was a tc entry, assume we picked up defaults via that */
674     if (!has_base) {
675         if (WANTED(init_3string) && termcap_init2)
676             init_3string = _nc_save_str(termcap_init2);
677
678         if (WANTED(reset_2string) && termcap_reset)
679             reset_2string = _nc_save_str(termcap_reset);
680
681         if (WANTED(carriage_return)) {
682             if (carriage_return_delay > 0) {
683                 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
684                             "%s$<%d>", C_CR, carriage_return_delay);
685                 carriage_return = _nc_save_str(buf);
686             } else
687                 carriage_return = _nc_save_str(C_CR);
688         }
689         if (WANTED(cursor_left)) {
690             if (backspace_delay > 0) {
691                 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
692                             "%s$<%d>", C_BS, backspace_delay);
693                 cursor_left = _nc_save_str(buf);
694             } else if (backspaces_with_bs == 1)
695                 cursor_left = _nc_save_str(C_BS);
696             else if (PRESENT(backspace_if_not_bs))
697                 cursor_left = backspace_if_not_bs;
698         }
699         /* vi doesn't use "do", but it does seem to use nl (or '\n') instead */
700         if (WANTED(cursor_down)) {
701             if (PRESENT(linefeed_if_not_lf))
702                 cursor_down = linefeed_if_not_lf;
703             else if (linefeed_is_newline != 1) {
704                 if (new_line_delay > 0) {
705                     _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
706                                 "%s$<%d>", C_LF, new_line_delay);
707                     cursor_down = _nc_save_str(buf);
708                 } else
709                     cursor_down = _nc_save_str(C_LF);
710             }
711         }
712         if (WANTED(scroll_forward) && crt_no_scrolling != 1) {
713             if (PRESENT(linefeed_if_not_lf))
714                 cursor_down = linefeed_if_not_lf;
715             else if (linefeed_is_newline != 1) {
716                 if (new_line_delay > 0) {
717                     _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
718                                 "%s$<%d>", C_LF, new_line_delay);
719                     scroll_forward = _nc_save_str(buf);
720                 } else
721                     scroll_forward = _nc_save_str(C_LF);
722             }
723         }
724         if (WANTED(newline)) {
725             if (linefeed_is_newline == 1) {
726                 if (new_line_delay > 0) {
727                     _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
728                                 "%s$<%d>", C_LF, new_line_delay);
729                     newline = _nc_save_str(buf);
730                 } else
731                     newline = _nc_save_str(C_LF);
732             } else if (PRESENT(carriage_return) && PRESENT(scroll_forward)) {
733                 _nc_str_init(&result, buf, sizeof(buf));
734                 if (_nc_safe_strcat(&result, carriage_return)
735                     && _nc_safe_strcat(&result, scroll_forward))
736                     newline = _nc_save_str(buf);
737             } else if (PRESENT(carriage_return) && PRESENT(cursor_down)) {
738                 _nc_str_init(&result, buf, sizeof(buf));
739                 if (_nc_safe_strcat(&result, carriage_return)
740                     && _nc_safe_strcat(&result, cursor_down))
741                     newline = _nc_save_str(buf);
742             }
743         }
744     }
745
746     /*
747      * Inverse of capdefaults.c code ends here.
748      * ----------------------------------------------------------------------
749      *
750      * TERMCAP-TO TERMINFO MAPPINGS FOR SOURCE TRANSLATION
751      *
752      * These translations will *not* be inverted by tgetent().
753      */
754
755     if (!has_base) {
756         /*
757          * We wait until now to decide if we've got a working cr because even
758          * one that doesn't work can be used for newline. Unfortunately the
759          * space allocated for it is wasted.
760          */
761         if (return_does_clr_eol == 1 || no_correctly_working_cr == 1)
762             carriage_return = ABSENT_STRING;
763
764         /*
765          * Supposedly most termcap entries have ta now and '\t' is no longer a
766          * default, but it doesn't seem to be true...
767          */
768         if (WANTED(tab)) {
769             if (horizontal_tab_delay > 0) {
770                 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf))
771                             "%s$<%d>", C_HT, horizontal_tab_delay);
772                 tab = _nc_save_str(buf);
773             } else
774                 tab = _nc_save_str(C_HT);
775         }
776         if (init_tabs == ABSENT_NUMERIC && has_hardware_tabs == TRUE)
777             init_tabs = 8;
778
779         /*
780          * Assume we can beep with ^G unless we're given bl@.
781          */
782         if (WANTED(bell))
783             bell = _nc_save_str("\007");
784     }
785
786     /*
787      * Translate the old termcap :pt: capability to it#8 + ht=\t
788      */
789     if (has_hardware_tabs == TRUE) {
790         if (init_tabs != 8 && init_tabs != ABSENT_NUMERIC)
791             _nc_warning("hardware tabs with a width other than 8: %d", init_tabs);
792         else {
793             if (tab && _nc_capcmp(tab, C_HT))
794                 _nc_warning("hardware tabs with a non-^I tab string %s",
795                             _nc_visbuf(tab));
796             else {
797                 if (WANTED(tab))
798                     tab = _nc_save_str(C_HT);
799                 init_tabs = 8;
800             }
801         }
802     }
803     /*
804      * Now translate the ko capability, if there is one.  This
805      * isn't from mytinfo...
806      */
807     if (PRESENT(other_non_function_keys)) {
808         char *base;
809         char *bp, *cp, *dp;
810         struct name_table_entry const *from_ptr;
811         struct name_table_entry const *to_ptr;
812         char buf2[MAX_TERMINFO_LENGTH];
813         bool foundim;
814
815         /* we're going to use this for a special case later */
816         dp = strchr(other_non_function_keys, 'i');
817         foundim = (dp != 0) && (dp[1] == 'm');
818
819         /* look at each comma-separated capability in the ko string... */
820         for (base = other_non_function_keys;
821              (cp = strchr(base, ',')) != 0;
822              base = cp + 1) {
823             size_t len = (unsigned) (cp - base);
824             size_t n;
825             assoc const *ap = 0;
826
827             for (n = 0; n < SIZEOF(ko_xlate); ++n) {
828                 if (len == strlen(ko_xlate[n].from)
829                     && strncmp(ko_xlate[n].from, base, len) == 0) {
830                     ap = ko_xlate + n;
831                     break;
832                 }
833             }
834             if (ap == 0) {
835                 _nc_warning("unknown capability `%.*s' in ko string",
836                             (int) len, base);
837                 continue;
838             } else if (ap->to[0] == '\0')       /* ignore it */
839                 continue;
840
841             /* now we know we found a match in ko_table, so... */
842
843             from_ptr = _nc_find_entry(ap->from, _nc_get_hash_table(TRUE));
844             to_ptr = _nc_find_entry(ap->to, _nc_get_hash_table(FALSE));
845
846             if (!from_ptr || !to_ptr)   /* should never happen! */
847                 _nc_err_abort("ko translation table is invalid, I give up");
848
849             if (WANTED(tp->Strings[from_ptr->nte_index])) {
850                 _nc_warning("no value for ko capability %s", ap->from);
851                 continue;
852             }
853
854             if (tp->Strings[to_ptr->nte_index]) {
855                 const char *s = tp->Strings[from_ptr->nte_index];
856                 const char *t = tp->Strings[to_ptr->nte_index];
857                 /* There's no point in warning about it if it's the same
858                  * string; that's just an inefficiency.
859                  */
860                 if (VALID_STRING(s) && VALID_STRING(t) && strcmp(s, t) != 0)
861                     _nc_warning("%s (%s) already has an explicit value %s, ignoring ko",
862                                 ap->to, ap->from, t);
863                 continue;
864             }
865
866             /*
867              * The magic moment -- copy the mapped key string over,
868              * stripping out padding.
869              */
870             for (dp = buf2, bp = tp->Strings[from_ptr->nte_index]; *bp; bp++) {
871                 if (bp[0] == '$' && bp[1] == '<') {
872                     while (*bp && *bp != '>') {
873                         ++bp;
874                     }
875                 } else
876                     *dp++ = *bp;
877             }
878             *dp = '\0';
879
880             tp->Strings[to_ptr->nte_index] = _nc_save_str(buf2);
881         }
882
883         /*
884          * Note: ko=im and ko=ic both want to grab the `Insert'
885          * keycap.  There's a kich1 but no ksmir, so the ic capability
886          * got mapped to kich1 and im to kIC to avoid a collision.
887          * If the description has im but not ic, hack kIC back to kich1.
888          */
889         if (foundim && WANTED(key_ic) && key_sic) {
890             key_ic = key_sic;
891             key_sic = ABSENT_STRING;
892         }
893     }
894
895     if (!has_base) {
896         if (!hard_copy) {
897             if (WANTED(key_backspace))
898                 key_backspace = _nc_save_str(C_BS);
899             if (WANTED(key_left))
900                 key_left = _nc_save_str(C_BS);
901             if (WANTED(key_down))
902                 key_down = _nc_save_str(C_LF);
903         }
904     }
905
906     /*
907      * Translate XENIX forms characters.
908      */
909     if (PRESENT(acs_ulcorner) ||
910         PRESENT(acs_llcorner) ||
911         PRESENT(acs_urcorner) ||
912         PRESENT(acs_lrcorner) ||
913         PRESENT(acs_ltee) ||
914         PRESENT(acs_rtee) ||
915         PRESENT(acs_btee) ||
916         PRESENT(acs_ttee) ||
917         PRESENT(acs_hline) ||
918         PRESENT(acs_vline) ||
919         PRESENT(acs_plus)) {
920         char buf2[MAX_TERMCAP_LENGTH];
921
922         _nc_str_init(&result, buf2, sizeof(buf2));
923         _nc_safe_strcat(&result, acs_chars);
924
925         append_acs(&result, 'j', acs_lrcorner);
926         append_acs(&result, 'k', acs_urcorner);
927         append_acs(&result, 'l', acs_ulcorner);
928         append_acs(&result, 'm', acs_llcorner);
929         append_acs(&result, 'n', acs_plus);
930         append_acs(&result, 'q', acs_hline);
931         append_acs(&result, 't', acs_ltee);
932         append_acs(&result, 'u', acs_rtee);
933         append_acs(&result, 'v', acs_btee);
934         append_acs(&result, 'w', acs_ttee);
935         append_acs(&result, 'x', acs_vline);
936
937         if (buf2[0]) {
938             acs_chars = _nc_save_str(buf2);
939             _nc_warning("acsc string synthesized from XENIX capabilities");
940         }
941     } else if (acs_chars == 0
942                && enter_alt_charset_mode != 0
943                && exit_alt_charset_mode != 0) {
944         acs_chars = _nc_save_str(VT_ACSC);
945     }
946 }
947
948 static void
949 postprocess_terminfo(TERMTYPE2 *tp)
950 {
951     /*
952      * TERMINFO-TO-TERMINFO MAPPINGS FOR SOURCE TRANSLATION
953      * ----------------------------------------------------------------------
954      */
955
956     /*
957      * Translate AIX forms characters.
958      */
959     if (PRESENT(box_chars_1)) {
960         char buf2[MAX_TERMCAP_LENGTH];
961         string_desc result;
962
963         _nc_str_init(&result, buf2, sizeof(buf2));
964         _nc_safe_strcat(&result, acs_chars);
965
966         append_acs0(&result, 'l', box_chars_1[0]);      /* ACS_ULCORNER */
967         append_acs0(&result, 'q', box_chars_1[1]);      /* ACS_HLINE */
968         append_acs0(&result, 'k', box_chars_1[2]);      /* ACS_URCORNER */
969         append_acs0(&result, 'x', box_chars_1[3]);      /* ACS_VLINE */
970         append_acs0(&result, 'j', box_chars_1[4]);      /* ACS_LRCORNER */
971         append_acs0(&result, 'm', box_chars_1[5]);      /* ACS_LLCORNER */
972         append_acs0(&result, 'w', box_chars_1[6]);      /* ACS_TTEE */
973         append_acs0(&result, 'u', box_chars_1[7]);      /* ACS_RTEE */
974         append_acs0(&result, 'v', box_chars_1[8]);      /* ACS_BTEE */
975         append_acs0(&result, 't', box_chars_1[9]);      /* ACS_LTEE */
976         append_acs0(&result, 'n', box_chars_1[10]);     /* ACS_PLUS */
977
978         if (buf2[0]) {
979             acs_chars = _nc_save_str(buf2);
980             _nc_warning("acsc string synthesized from AIX capabilities");
981             box_chars_1 = ABSENT_STRING;
982         }
983     }
984     /*
985      * ----------------------------------------------------------------------
986      */
987 }
988
989 /*
990  * Do a linear search through the terminfo tables to find a given full-name.
991  * We don't expect to do this often, so there's no hashing function.
992  *
993  * In effect, this scans through the 3 lists of full-names, and looks them
994  * up in _nc_info_table, which is organized so that the nte_index fields are
995  * sorted, but the nte_type fields are not necessarily grouped together.
996  */
997 static struct name_table_entry const *
998 lookup_fullname(const char *find)
999 {
1000     int state = -1;
1001
1002     for (;;) {
1003         int count = 0;
1004         NCURSES_CONST char *const *names;
1005
1006         switch (++state) {
1007         case BOOLEAN:
1008             names = boolfnames;
1009             break;
1010         case STRING:
1011             names = strfnames;
1012             break;
1013         case NUMBER:
1014             names = numfnames;
1015             break;
1016         default:
1017             return NOTFOUND;
1018         }
1019
1020         for (count = 0; names[count] != 0; count++) {
1021             if (!strcmp(names[count], find)) {
1022                 struct name_table_entry const *entry_ptr = _nc_get_table(FALSE);
1023                 while (entry_ptr->nte_type != state
1024                        || entry_ptr->nte_index != count)
1025                     entry_ptr++;
1026                 return entry_ptr;
1027             }
1028         }
1029     }
1030 }
1031
1032 /* parse_entry.c ends here */