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