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