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