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