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