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