xref: /freebsd/contrib/ncurses/progs/dump_entry.c (revision 68ad2b0d7af2a3571c4abac9afa712f9b09b721c)
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 #define __INTERNAL_CAPS_VISIBLE
37 #include <progs.priv.h>
38 
39 #include <dump_entry.h>
40 #include <termsort.h>		/* this C file is generated */
41 #include <parametrized.h>	/* so is this */
42 
43 MODULE_ID("$Id: dump_entry.c,v 1.199 2025/02/08 21:53:40 tom Exp $")
44 
45 #define DISCARD(string) string = ABSENT_STRING
46 #define PRINTF (void) printf
47 #define WRAPPED 32
48 
49 #define OkIndex(index,array) ((int)(index) >= 0 && (int)(index) < (int) SIZEOF(array))
50 #define TcOutput() (outform == F_TERMCAP || outform == F_TCONVERR)
51 
52 typedef struct {
53     char *text;
54     size_t used;
55     size_t size;
56 } DYNBUF;
57 
58 static int tversion;		/* terminfo version */
59 static int outform;		/* output format to use */
60 static int sortmode;		/* sort mode to use */
61 static int width = 60;		/* max line width for listings */
62 static int height = 65535;	/* max number of lines for listings */
63 static int column;		/* current column, limited by 'width' */
64 static int oldcol;		/* last value of column before wrap */
65 static bool pretty;		/* true if we format if-then-else strings */
66 static bool wrapped;		/* true if we wrap too-long strings */
67 static bool did_wrap;		/* true if last wrap_concat did wrapping */
68 static bool checking;		/* true if we are checking for tic */
69 static int quickdump;		/* true if we are dumping compiled data */
70 
71 static char *save_sgr;
72 
73 static DYNBUF outbuf;
74 static DYNBUF tmpbuf;
75 
76 /* indirection pointers for implementing sort and display modes */
77 static const PredIdx *bool_indirect, *num_indirect, *str_indirect;
78 static NCURSES_CONST char *const *bool_names;
79 static NCURSES_CONST char *const *num_names;
80 static NCURSES_CONST char *const *str_names;
81 
82 static const char *separator = "", *trailer = "";
83 static int indent = 8;
84 
85 /* cover various ports and variants of terminfo */
86 #define V_ALLCAPS	0	/* all capabilities (SVr4, XSI, ncurses) */
87 #define V_SVR1		1	/* SVR1, Ultrix */
88 #define V_HPUX		2	/* HP-UX */
89 #define V_AIX		3	/* AIX */
90 #define V_BSD		4	/* BSD */
91 
92 #if NCURSES_XNAMES
93 #define OBSOLETE(n) (!_nc_user_definable && (n[0] == 'O' && n[1] == 'T'))
94 #else
95 #define OBSOLETE(n) (n[0] == 'O' && n[1] == 'T')
96 #endif
97 
98 #define isObsolete(f,n) ((f == F_TERMINFO || f == F_VARIABLE) && (sortmode != S_VARIABLE) && OBSOLETE(n))
99 
100 #if NCURSES_XNAMES
101 #define BoolIndirect(j) ((j >= BOOLCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : bool_indirect[j]))
102 #define NumIndirect(j)  ((j >= NUMCOUNT)  ? (j) : ((sortmode == S_NOSORT) ? j : num_indirect[j]))
103 #define StrIndirect(j)  ((j >= STRCOUNT)  ? (j) : ((sortmode == S_NOSORT) ? j : str_indirect[j]))
104 #else
105 #define BoolIndirect(j) ((sortmode == S_NOSORT) ? (j) : bool_indirect[j])
106 #define NumIndirect(j)  ((sortmode == S_NOSORT) ? (j) : num_indirect[j])
107 #define StrIndirect(j)  ((sortmode == S_NOSORT) ? (j) : str_indirect[j])
108 #endif
109 
110 static GCC_NORETURN void
failed(const char * s)111 failed(const char *s)
112 {
113     perror(s);
114     ExitProgram(EXIT_FAILURE);
115 }
116 
117 static void
strncpy_DYN(DYNBUF * dst,const char * src,size_t need)118 strncpy_DYN(DYNBUF * dst, const char *src, size_t need)
119 {
120     size_t want = need + dst->used + 1;
121     if (want > dst->size) {
122 	dst->size += (want + 1024);	/* be generous */
123 	dst->text = typeRealloc(char, dst->size, dst->text);
124 	if (dst->text == NULL)
125 	    failed("strncpy_DYN");
126     }
127     _nc_STRNCPY(dst->text + dst->used, src, need + 1);
128     dst->used += need;
129     dst->text[dst->used] = 0;
130 }
131 
132 static void
strcpy_DYN(DYNBUF * dst,const char * src)133 strcpy_DYN(DYNBUF * dst, const char *src)
134 {
135     if (src == NULL) {
136 	dst->used = 0;
137 	strcpy_DYN(dst, "");
138     } else {
139 	strncpy_DYN(dst, src, strlen(src));
140     }
141 }
142 
143 #if NO_LEAKS
144 static void
free_DYN(DYNBUF * p)145 free_DYN(DYNBUF * p)
146 {
147     if (p->text != NULL)
148 	free(p->text);
149     p->text = NULL;
150     p->size = 0;
151     p->used = 0;
152 }
153 
154 void
_nc_leaks_dump_entry(void)155 _nc_leaks_dump_entry(void)
156 {
157     free_DYN(&outbuf);
158     free_DYN(&tmpbuf);
159 }
160 #endif
161 
162 #define NameTrans(check,result) \
163 	    if ((np->nte_index <= OK_ ## check) \
164 		&& check[np->nte_index]) \
165 		return (result[np->nte_index])
166 
167 NCURSES_CONST char *
nametrans(const char * name)168 nametrans(const char *name)
169 /* translate a capability name to termcap from terminfo */
170 {
171     const struct name_table_entry *np;
172 
173     if ((np = _nc_find_entry(name, _nc_get_hash_table(0))) != NULL) {
174 	switch (np->nte_type) {
175 	case BOOLEAN:
176 	    NameTrans(bool_from_termcap, boolcodes);
177 	    break;
178 
179 	case NUMBER:
180 	    NameTrans(num_from_termcap, numcodes);
181 	    break;
182 
183 	case STRING:
184 	    NameTrans(str_from_termcap, strcodes);
185 	    break;
186 	}
187     }
188 
189     return (NULL);
190 }
191 
192 void
dump_init(const char * version,int mode,int sort,bool wrap_strings,int twidth,int theight,unsigned traceval,bool formatted,bool check,int quick)193 dump_init(const char *version,
194 	  int mode,
195 	  int sort,
196 	  bool wrap_strings,
197 	  int twidth,
198 	  int theight,
199 	  unsigned traceval,
200 	  bool formatted,
201 	  bool check,
202 	  int quick)
203 /* set up for entry display */
204 {
205     width = twidth;
206     height = theight;
207     pretty = formatted;
208     wrapped = wrap_strings;
209     checking = check;
210     quickdump = (quick & 3);
211 
212     did_wrap = (width <= 0);
213 
214     /* versions */
215     if (version == NULL)
216 	tversion = V_ALLCAPS;
217     else if (!strcmp(version, "SVr1") || !strcmp(version, "SVR1")
218 	     || !strcmp(version, "Ultrix"))
219 	tversion = V_SVR1;
220     else if (!strcmp(version, "HP"))
221 	tversion = V_HPUX;
222     else if (!strcmp(version, "AIX"))
223 	tversion = V_AIX;
224     else if (!strcmp(version, "BSD"))
225 	tversion = V_BSD;
226     else
227 	tversion = V_ALLCAPS;
228 
229     /* implement display modes */
230     switch (outform = mode) {
231     case F_LITERAL:
232     case F_TERMINFO:
233 	bool_names = boolnames;
234 	num_names = numnames;
235 	str_names = strnames;
236 	separator = (twidth > 0 && theight > 1) ? ", " : ",";
237 	trailer = "\n\t";
238 	break;
239 
240     case F_VARIABLE:
241 	bool_names = boolfnames;
242 	num_names = numfnames;
243 	str_names = strfnames;
244 	separator = (twidth > 0 && theight > 1) ? ", " : ",";
245 	trailer = "\n\t";
246 	break;
247 
248     case F_TERMCAP:
249     case F_TCONVERR:
250 	bool_names = boolcodes;
251 	num_names = numcodes;
252 	str_names = strcodes;
253 	separator = ":";
254 	trailer = "\\\n\t:";
255 	break;
256     }
257     indent = 8;
258 
259     /* implement sort modes */
260     switch (sortmode = sort) {
261     case S_NOSORT:
262 	if (traceval)
263 	    (void) fprintf(stderr,
264 			   "%s: sorting by term structure order\n", _nc_progname);
265 	break;
266 
267     case S_TERMINFO:
268 	if (traceval)
269 	    (void) fprintf(stderr,
270 			   "%s: sorting by terminfo name order\n", _nc_progname);
271 	bool_indirect = bool_terminfo_sort;
272 	num_indirect = num_terminfo_sort;
273 	str_indirect = str_terminfo_sort;
274 	break;
275 
276     case S_VARIABLE:
277 	if (traceval)
278 	    (void) fprintf(stderr,
279 			   "%s: sorting by C variable order\n", _nc_progname);
280 	bool_indirect = bool_variable_sort;
281 	num_indirect = num_variable_sort;
282 	str_indirect = str_variable_sort;
283 	break;
284 
285     case S_TERMCAP:
286 	if (traceval)
287 	    (void) fprintf(stderr,
288 			   "%s: sorting by termcap name order\n", _nc_progname);
289 	bool_indirect = bool_termcap_sort;
290 	num_indirect = num_termcap_sort;
291 	str_indirect = str_termcap_sort;
292 	break;
293     }
294 
295     if (traceval)
296 	(void) fprintf(stderr,
297 		       "%s: width = %d, tversion = %d, outform = %d\n",
298 		       _nc_progname, width, tversion, outform);
299 }
300 
301 static TERMTYPE2 *cur_type;
302 
303 static int
dump_predicate(PredType type,PredIdx idx)304 dump_predicate(PredType type, PredIdx idx)
305 /* predicate function to use for ordinary decompilation */
306 {
307     switch (type) {
308     case BOOLEAN:
309 	return (cur_type->Booleans[idx] == FALSE)
310 	    ? FAIL : cur_type->Booleans[idx];
311 
312     case NUMBER:
313 	return (cur_type->Numbers[idx] == ABSENT_NUMERIC)
314 	    ? FAIL : cur_type->Numbers[idx];
315 
316     case STRING:
317 	return (cur_type->Strings[idx] != ABSENT_STRING)
318 	    ? (int) TRUE : FAIL;
319     }
320 
321     return (FALSE);		/* pacify compiler */
322 }
323 
324 static void set_obsolete_termcaps(TERMTYPE2 *tp);
325 
326 /* is this the index of a function key string? */
327 #define FNKEY(i) \
328     (((i) >= STR_IDX(key_f0) && \
329       (i) <= STR_IDX(key_f9)) || \
330      ((i) >= STR_IDX(key_f11) && \
331       (i) <= STR_IDX(key_f63)))
332 
333 /*
334  * If we configure with a different Caps file, the offsets into the arrays
335  * will change.  So we use an address expression.
336  */
337 #define BOOL_IDX(name) (PredType) (&(name) - &(CUR Booleans[0]))
338 #define NUM_IDX(name)  (PredType) (&(name) - &(CUR Numbers[0]))
339 #define STR_IDX(name)  (PredType) (&(name) - &(CUR Strings[0]))
340 
341 static bool
version_filter(PredType type,PredIdx idx)342 version_filter(PredType type, PredIdx idx)
343 /* filter out capabilities we may want to suppress */
344 {
345     switch (tversion) {
346     case V_ALLCAPS:		/* SVr4, XSI Curses */
347 	return (TRUE);
348 
349     case V_SVR1:		/* System V Release 1, Ultrix */
350 	switch (type) {
351 	case BOOLEAN:
352 	    return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
353 	case NUMBER:
354 	    return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
355 	case STRING:
356 	    return ((idx <= STR_IDX(prtr_non)) ? TRUE : FALSE);
357 	}
358 	break;
359 
360     case V_HPUX:		/* Hewlett-Packard */
361 	switch (type) {
362 	case BOOLEAN:
363 	    return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
364 	case NUMBER:
365 	    return ((idx <= NUM_IDX(label_width)) ? TRUE : FALSE);
366 	case STRING:
367 	    if (idx <= STR_IDX(prtr_non))
368 		return (TRUE);
369 	    else if (FNKEY(idx))	/* function keys */
370 		return (TRUE);
371 	    else if (idx == STR_IDX(plab_norm)
372 		     || idx == STR_IDX(label_on)
373 		     || idx == STR_IDX(label_off))
374 		return (TRUE);
375 	    else
376 		return (FALSE);
377 	}
378 	break;
379 
380     case V_AIX:		/* AIX */
381 	switch (type) {
382 	case BOOLEAN:
383 	    return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
384 	case NUMBER:
385 	    return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
386 	case STRING:
387 	    if (idx <= STR_IDX(prtr_non))
388 		return (TRUE);
389 	    else if (FNKEY(idx))	/* function keys */
390 		return (TRUE);
391 	    else
392 		return (FALSE);
393 	}
394 	break;
395 
396 #define is_termcap(type) (OkIndex(idx, type##_from_termcap) && \
397 			  type##_from_termcap[idx])
398 
399     case V_BSD:		/* BSD */
400 	switch (type) {
401 	case BOOLEAN:
402 	    return is_termcap(bool);
403 	case NUMBER:
404 	    return is_termcap(num);
405 	case STRING:
406 	    return is_termcap(str);
407 	}
408 	break;
409     }
410 
411     return (FALSE);		/* pacify the compiler */
412 }
413 
414 static void
trim_trailing(void)415 trim_trailing(void)
416 {
417     while (outbuf.used > 0 && outbuf.text[outbuf.used - 1] == ' ')
418 	outbuf.text[--outbuf.used] = '\0';
419 }
420 
421 static void
force_wrap(void)422 force_wrap(void)
423 {
424     oldcol = column;
425     trim_trailing();
426     strcpy_DYN(&outbuf, trailer);
427     column = indent;
428 }
429 
430 static int
op_length(const char * src,int offset)431 op_length(const char *src, int offset)
432 {
433     int result = 0;
434 
435     if (offset > 0 && src[offset - 1] == '\\') {
436 	result = 0;
437     } else {
438 	int ch;
439 
440 	result++;		/* for '%' mark */
441 	ch = src[offset + result];
442 	if (TcOutput()) {
443 	    if (ch == '>') {
444 		result += 3;
445 	    } else if (ch == '+') {
446 		result += 2;
447 	    } else {
448 		result++;
449 	    }
450 	} else if (ch == '\'') {
451 	    result += 3;
452 	} else if (ch == L_CURL[0]) {
453 	    int n = result;
454 	    while ((ch = src[offset + n]) != '\0') {
455 		if (ch == R_CURL[0]) {
456 		    result = ++n;
457 		    break;
458 		}
459 		n++;
460 	    }
461 	} else if (strchr("pPg", ch) != NULL) {
462 	    result += 2;
463 	} else {
464 	    result++;		/* ordinary operator */
465 	}
466     }
467     return result;
468 }
469 
470 /*
471  * When wrapping too-long strings, avoid splitting a backslash sequence, or
472  * a terminfo '%' operator.  That will leave things a little ragged, but avoids
473  * a stray backslash at the end of the line, as well as making the result a
474  * little more readable.
475  */
476 static int
find_split(const char * src,int step,int size)477 find_split(const char *src, int step, int size)
478 {
479     int result = size;
480 
481     if (size > 0) {
482 	/* check if that would split a backslash-sequence */
483 	int mark = size;
484 	int n;
485 
486 	for (n = size - 1; n > 0; --n) {
487 	    int ch = UChar(src[step + n]);
488 	    if (ch == '\\') {
489 		if (n > 0 && src[step + n - 1] == ch)
490 		    --n;
491 		mark = n;
492 		break;
493 	    } else if (!isalnum(ch)) {
494 		break;
495 	    }
496 	}
497 	if (mark < size) {
498 	    result = mark;
499 	} else {
500 	    /* check if that would split a backslash-sequence */
501 	    for (n = size - 1; n > 0; --n) {
502 		int ch = UChar(src[step + n]);
503 		if (ch == '%') {
504 		    int need = op_length(src, step + n);
505 		    if ((n + need) > size) {
506 			mark = n;
507 		    }
508 		    break;
509 		}
510 	    }
511 	    if (mark < size) {
512 		result = mark;
513 	    }
514 	}
515     }
516     return result;
517 }
518 
519 /*
520  * If we are going to wrap lines, we cannot leave literal spaces because that
521  * would be ambiguous if we split on that space.
522  */
523 static char *
fill_spaces(const char * src)524 fill_spaces(const char *src)
525 {
526     const char *fill = "\\s";
527     size_t need = strlen(src);
528     size_t size = strlen(fill);
529     char *result = NULL;
530     int pass;
531     size_t s, d;
532     for (pass = 0; pass < 2; ++pass) {
533 	for (s = d = 0; src[s] != '\0'; ++s) {
534 	    if (src[s] == ' ') {
535 		if (pass) {
536 		    _nc_STRCPY(&result[d], fill, need + 1 - d);
537 		    d += size;
538 		} else {
539 		    need += size;
540 		}
541 	    } else {
542 		if (pass) {
543 		    result[d++] = src[s];
544 		} else {
545 		    ++d;
546 		}
547 	    }
548 	}
549 	if (pass) {
550 	    result[d] = '\0';
551 	} else {
552 	    result = calloc(need + 1, sizeof(char));
553 	    if (result == NULL)
554 		failed("fill_spaces");
555 	}
556     }
557     return result;
558 }
559 
560 typedef enum {
561     wOFF = 0
562     ,w1ST = 1
563     ,w2ND = 2
564     ,wEND = 4
565     ,wERR = 8
566 } WRAPMODE;
567 
568 #define wrap_1ST(mode) ((mode)&w1ST)
569 #define wrap_END(mode) ((mode)&wEND)
570 #define wrap_ERR(mode) ((mode)&wERR)
571 
572 static void
wrap_concat(const char * src,int need,unsigned mode)573 wrap_concat(const char *src, int need, unsigned mode)
574 {
575     int gaps = (int) strlen(separator);
576     int want = gaps + need;
577 
578     did_wrap = (width <= 0);
579     if (wrap_1ST(mode)
580 	&& column > indent
581 	&& column + want > width) {
582 	force_wrap();
583     }
584     if ((wrap_END(mode) && !wrap_ERR(mode)) &&
585 	wrapped &&
586 	(width >= 0) &&
587 	(column + want) > width) {
588 	int step = 0;
589 	int used = width > WRAPPED ? width : WRAPPED;
590 	int base = 0;
591 	char *p, align[9];
592 	const char *my_t = trailer;
593 	char *fill = fill_spaces(src);
594 	int last = (int) strlen(fill);
595 
596 	need = last;
597 
598 	if (TcOutput())
599 	    trailer = "\\\n\t ";
600 
601 	if (!TcOutput() && (p = strchr(fill, '=')) != NULL) {
602 	    base = (int) (p + 1 - fill);
603 	    if (base > 8)
604 		base = 8;
605 	    _nc_SPRINTF(align, _nc_SLIMIT(align) "%*s", base, " ");
606 	} else if (column > 8) {
607 	    base = column - 8;
608 	    if (base > 8)
609 		base = 8;
610 	    _nc_SPRINTF(align, _nc_SLIMIT(align) "%*s", base, " ");
611 	} else {
612 	    align[base] = '\0';
613 	}
614 	/* "pretty" overrides wrapping if it already split the line */
615 	if (!pretty || strchr(fill, '\n') == NULL) {
616 	    int tag = 0;
617 
618 	    if (TcOutput() && outbuf.used && !wrap_1ST(mode)) {
619 		tag = 3;
620 	    }
621 
622 	    while ((column + (need + gaps)) > used) {
623 		int size = used - tag;
624 		if (step) {
625 		    strcpy_DYN(&outbuf, align);
626 		    size -= base;
627 		}
628 		if (size > (last - step)) {
629 		    size = (last - step);
630 		}
631 		size = find_split(fill, step, size);
632 		strncpy_DYN(&outbuf, fill + step, (size_t) size);
633 		step += size;
634 		need -= size;
635 		if (need > 0) {
636 		    force_wrap();
637 		    did_wrap = TRUE;
638 		    tag = 0;
639 		}
640 	    }
641 	}
642 	if (need > 0) {
643 	    if (step)
644 		strcpy_DYN(&outbuf, align);
645 	    strcpy_DYN(&outbuf, fill + step);
646 	}
647 	if (wrap_END(mode))
648 	    strcpy_DYN(&outbuf, separator);
649 	trailer = my_t;
650 	force_wrap();
651 
652 	free(fill);
653     } else {
654 	strcpy_DYN(&outbuf, src);
655 	if (wrap_END(mode))
656 	    strcpy_DYN(&outbuf, separator);
657 	column += (int) strlen(src);
658     }
659 }
660 
661 static void
wrap_concat1(const char * src)662 wrap_concat1(const char *src)
663 {
664     int need = (int) strlen(src);
665     wrap_concat(src, need, w1ST | wEND);
666 }
667 
668 static void
wrap_concat3(const char * name,const char * eqls,const char * value)669 wrap_concat3(const char *name, const char *eqls, const char *value)
670 {
671     int nlen = (int) strlen(name);
672     int elen = (int) strlen(eqls);
673     int vlen = (int) strlen(value);
674 
675     wrap_concat(name, nlen + elen + vlen, w1ST);
676     wrap_concat(eqls, elen + vlen, w2ND);
677     wrap_concat(value, vlen, wEND);
678 }
679 
680 #define IGNORE_SEP_TRAIL(first,last,sep_trail) \
681 	if ((size_t)(last - first) > sizeof(sep_trail)-1 \
682 	 && !strncmp(first, sep_trail, sizeof(sep_trail)-1)) \
683 		first += sizeof(sep_trail)-2
684 
685 /* Returns the nominal length of the buffer assuming it is termcap format,
686  * i.e., the continuation sequence is treated as a single character ":".
687  *
688  * There are several implementations of termcap which read the text into a
689  * fixed-size buffer.  Generally they strip the newlines from the text, but may
690  * not do it until after the buffer is read.  Also, "tc=" resolution may be
691  * expanded in the same buffer.  This function is useful for measuring the size
692  * of the best fixed-buffer implementation; the worst case may be much worse.
693  */
694 #ifdef TEST_TERMCAP_LENGTH
695 static int
termcap_length(const char * src)696 termcap_length(const char *src)
697 {
698     static const char pattern[] = ":\\\n\t:";
699 
700     int len = 0;
701     const char *const t = src + strlen(src);
702 
703     while (*src != '\0') {
704 	IGNORE_SEP_TRAIL(src, t, pattern);
705 	src++;
706 	len++;
707     }
708     return len;
709 }
710 #else
711 #define termcap_length(src) strlen(src)
712 #endif
713 
714 static void
indent_DYN(DYNBUF * buffer,int level)715 indent_DYN(DYNBUF * buffer, int level)
716 {
717     int n;
718 
719     for (n = 0; n < level; n++)
720 	strncpy_DYN(buffer, "\t", (size_t) 1);
721 }
722 
723 /*
724  * Check if the current line which was begun consists only of a tab and the
725  * given leading text.
726  */
727 static bool
leading_DYN(const DYNBUF * buffer,const char * leading)728 leading_DYN(const DYNBUF * buffer, const char *leading)
729 {
730     bool result = FALSE;
731     size_t need = strlen(leading);
732     if (buffer->used > need) {
733 	need = buffer->used - need;
734 	if (!strcmp(buffer->text + need, leading)) {
735 	    result = TRUE;
736 	    while (--need != 0) {
737 		if (buffer->text[need] == '\n') {
738 		    break;
739 		}
740 		if (buffer->text[need] != '\t') {
741 		    result = FALSE;
742 		    break;
743 		}
744 	    }
745 	}
746     }
747     return result;
748 }
749 
750 bool
has_params(const char * src,bool formatting)751 has_params(const char *src, bool formatting)
752 {
753     bool result = FALSE;
754     int len = (int) strlen(src);
755     int n;
756     bool ifthen = FALSE;
757     bool params = FALSE;
758 
759     for (n = 0; n < len - 1; ++n) {
760 	if (!strncmp(src + n, "%p", (size_t) 2)) {
761 	    params = TRUE;
762 	} else if (!strncmp(src + n, "%;", (size_t) 2)) {
763 	    ifthen = TRUE;
764 	    result = params;
765 	    break;
766 	}
767     }
768     if (!ifthen) {
769 	if (formatting) {
770 	    result = ((len > 50) && params);
771 	} else {
772 	    result = params;
773 	}
774     }
775     return result;
776 }
777 
778 static char *
fmt_complex(TERMTYPE2 * tterm,const char * capability,char * src,int level)779 fmt_complex(TERMTYPE2 *tterm, const char *capability, char *src, int level)
780 {
781     bool percent = FALSE;
782     bool params = has_params(src, TRUE);
783 
784     while (*src != '\0') {
785 	switch (*src) {
786 	case '^':
787 	    percent = FALSE;
788 	    strncpy_DYN(&tmpbuf, src++, (size_t) 1);
789 	    break;
790 	case '\\':
791 	    percent = FALSE;
792 	    strncpy_DYN(&tmpbuf, src++, (size_t) 1);
793 	    break;
794 	case '%':
795 	    percent = TRUE;
796 	    break;
797 	case '?':		/* "if" */
798 	case 't':		/* "then" */
799 	case 'e':		/* "else" */
800 	    if (percent) {
801 		percent = FALSE;
802 		tmpbuf.text[tmpbuf.used - 1] = '\n';
803 		/* treat a "%e" as else-if, on the same level */
804 		if (*src == 'e') {
805 		    indent_DYN(&tmpbuf, level);
806 		    strncpy_DYN(&tmpbuf, "%", (size_t) 1);
807 		    strncpy_DYN(&tmpbuf, src, (size_t) 1);
808 		    src++;
809 		    params = has_params(src, TRUE);
810 		    if (!params && *src != '\0' && *src != '%') {
811 			strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
812 			indent_DYN(&tmpbuf, level + 1);
813 		    }
814 		} else {
815 		    indent_DYN(&tmpbuf, level + 1);
816 		    strncpy_DYN(&tmpbuf, "%", (size_t) 1);
817 		    strncpy_DYN(&tmpbuf, src, (size_t) 1);
818 		    if (*src++ == '?') {
819 			src = fmt_complex(tterm, capability, src, level + 1);
820 			if (*src != '\0' && *src != '%') {
821 			    strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
822 			    indent_DYN(&tmpbuf, level + 1);
823 			}
824 		    } else if (level == 1) {
825 			if (checking)
826 			    _nc_warning("%s: %%%c without %%? in %s",
827 					_nc_first_name(tterm->term_names),
828 					*src, capability);
829 		    }
830 		}
831 		continue;
832 	    }
833 	    break;
834 	case ';':		/* "endif" */
835 	    if (percent) {
836 		percent = FALSE;
837 		if (level > 1) {
838 		    tmpbuf.text[tmpbuf.used - 1] = '\n';
839 		    indent_DYN(&tmpbuf, level);
840 		    strncpy_DYN(&tmpbuf, "%", (size_t) 1);
841 		    strncpy_DYN(&tmpbuf, src++, (size_t) 1);
842 		    if (src[0] == '%'
843 			&& src[1] != '\0'
844 			&& (strchr("?e;", src[1])) == NULL) {
845 			tmpbuf.text[tmpbuf.used++] = '\n';
846 			indent_DYN(&tmpbuf, level);
847 		    }
848 		    return src;
849 		}
850 		if (checking)
851 		    _nc_warning("%s: %%; without %%? in %s",
852 				_nc_first_name(tterm->term_names),
853 				capability);
854 	    }
855 	    break;
856 	case 'p':
857 	    if (percent && params && !leading_DYN(&tmpbuf, "%")) {
858 		tmpbuf.text[tmpbuf.used - 1] = '\n';
859 		indent_DYN(&tmpbuf, level + 1);
860 		strncpy_DYN(&tmpbuf, "%", (size_t) 1);
861 	    }
862 	    percent = FALSE;
863 	    break;
864 	case ' ':
865 	    strncpy_DYN(&tmpbuf, "\\s", (size_t) 2);
866 	    ++src;
867 	    continue;
868 	default:
869 	    percent = FALSE;
870 	    break;
871 	}
872 	strncpy_DYN(&tmpbuf, src++, (size_t) 1);
873     }
874     return src;
875 }
876 
877 /*
878  * Make "large" numbers a little easier to read by showing them in hexadecimal
879  * if they are "close" to a power of two.
880  */
881 static const char *
number_format(int value)882 number_format(int value)
883 {
884     const char *result = "%d";
885 
886     if ((outform != F_TERMCAP) && (value > 255)) {
887 	unsigned long lv = (unsigned long) value;
888 	int bits = sizeof(unsigned long) * 8;
889 	int nn;
890 
891 	for (nn = 8; nn < bits; ++nn) {
892 	    unsigned long mm;
893 
894 	    mm = 1UL << nn;
895 	    if ((mm - 16) <= lv && (mm + 16) > lv) {
896 		result = "%#x";
897 		break;
898 	    }
899 	}
900     }
901     return result;
902 }
903 
904 #define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap)
905 #define EXTRA_CAP 20
906 
907 int
fmt_entry(TERMTYPE2 * tterm,PredFunc pred,int content_only,int suppress_untranslatable,int infodump,int numbers)908 fmt_entry(TERMTYPE2 *tterm,
909 	  PredFunc pred,
910 	  int content_only,
911 	  int suppress_untranslatable,
912 	  int infodump,
913 	  int numbers)
914 {
915     PredIdx i, j;
916     char buffer[MAX_TERMINFO_LENGTH + EXTRA_CAP];
917     NCURSES_CONST char *name;
918     int predval, len;
919     PredIdx num_bools = 0;
920     PredIdx num_values = 0;
921     PredIdx num_strings = 0;
922     bool outcount = 0;
923 
924 #define WRAP_CONCAT1(s)		wrap_concat1(s); outcount = TRUE
925 #define WRAP_CONCAT		WRAP_CONCAT1(buffer)
926 
927     len = 12;			/* terminfo file-header */
928 
929     if (pred == NULL) {
930 	cur_type = tterm;
931 	pred = dump_predicate;
932     }
933 
934     strcpy_DYN(&outbuf, NULL);
935     if (content_only) {
936 	column = indent;	/* workaround to prevent empty lines */
937     } else {
938 	strcpy_DYN(&outbuf, tterm->term_names);
939 
940 	/*
941 	 * Colon is legal in terminfo descriptions, but not in termcap.
942 	 */
943 	if (!infodump) {
944 	    char *p = outbuf.text;
945 	    while (*p) {
946 		if (*p == ':') {
947 		    *p = '=';
948 		}
949 		++p;
950 	    }
951 	}
952 	strcpy_DYN(&outbuf, separator);
953 	column = (int) outbuf.used;
954 	if (height > 1)
955 	    force_wrap();
956     }
957 
958     for_each_boolean(j, tterm) {
959 	i = BoolIndirect(j);
960 	name = ExtBoolname(tterm, (int) i, bool_names);
961 	assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
962 
963 	if (!version_filter(BOOLEAN, i))
964 	    continue;
965 	else if (isObsolete(outform, name))
966 	    continue;
967 
968 	predval = pred(BOOLEAN, i);
969 	if (predval != FAIL) {
970 	    _nc_STRCPY(buffer, name, sizeof(buffer));
971 	    if (predval <= 0)
972 		_nc_STRCAT(buffer, "@", sizeof(buffer));
973 	    else if (i + 1 > num_bools)
974 		num_bools = i + 1;
975 	    WRAP_CONCAT;
976 	}
977     }
978 
979     if (column != indent && height > 1)
980 	force_wrap();
981 
982     for_each_number(j, tterm) {
983 	i = NumIndirect(j);
984 	name = ExtNumname(tterm, (int) i, num_names);
985 	assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
986 
987 	if (!version_filter(NUMBER, i))
988 	    continue;
989 	else if (isObsolete(outform, name))
990 	    continue;
991 
992 	predval = pred(NUMBER, i);
993 	if (predval != FAIL) {
994 	    if (tterm->Numbers[i] < 0) {
995 		_nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
996 			    "%s@", name);
997 	    } else {
998 		size_t nn;
999 		_nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1000 			    "%s#", name);
1001 		nn = strlen(buffer);
1002 		_nc_SPRINTF(buffer + nn, _nc_SLIMIT(sizeof(buffer) - nn)
1003 			    number_format(tterm->Numbers[i]),
1004 			    tterm->Numbers[i]);
1005 		if (i + 1 > num_values)
1006 		    num_values = i + 1;
1007 	    }
1008 	    WRAP_CONCAT;
1009 	}
1010     }
1011 
1012     if (column != indent && height > 1)
1013 	force_wrap();
1014 
1015     len += (int) (num_bools
1016 		  + num_values * 2
1017 		  + strlen(tterm->term_names) + 1);
1018     if (len & 1)
1019 	len++;
1020 
1021 #undef CUR
1022 #define CUR tterm->
1023     if (outform == F_TERMCAP) {
1024 	if (VALID_STRING(termcap_reset)) {
1025 	    if (VALID_STRING(init_3string)
1026 		&& !strcmp(init_3string, termcap_reset))
1027 		DISCARD(init_3string);
1028 
1029 	    if (VALID_STRING(reset_2string)
1030 		&& !strcmp(reset_2string, termcap_reset))
1031 		DISCARD(reset_2string);
1032 	}
1033     }
1034 
1035     for_each_string(j, tterm) {
1036 	char *capability;
1037 	i = StrIndirect(j);
1038 	name = ExtStrname(tterm, (int) i, str_names);
1039 	assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
1040 
1041 	capability = tterm->Strings[i];
1042 
1043 	if (!version_filter(STRING, i))
1044 	    continue;
1045 	else if (isObsolete(outform, name))
1046 	    continue;
1047 
1048 #if NCURSES_XNAMES
1049 	/*
1050 	 * Extended names can be longer than 2 characters, but termcap programs
1051 	 * cannot read those (filter them out).
1052 	 */
1053 	if (outform == F_TERMCAP && (strlen(name) > 2))
1054 	    continue;
1055 #endif
1056 
1057 	if (outform == F_TERMCAP) {
1058 	    /*
1059 	     * Some older versions of vi want rmir/smir to be defined
1060 	     * for ich/ich1 to work.  If they're not defined, force
1061 	     * them to be output as defined and empty.
1062 	     */
1063 	    if (PRESENT(insert_character) || PRESENT(parm_ich)) {
1064 		if (SAME_CAP(i, enter_insert_mode)
1065 		    && enter_insert_mode == ABSENT_STRING) {
1066 		    _nc_STRCPY(buffer, "im=", sizeof(buffer));
1067 		    WRAP_CONCAT;
1068 		    continue;
1069 		}
1070 
1071 		if (SAME_CAP(i, exit_insert_mode)
1072 		    && exit_insert_mode == ABSENT_STRING) {
1073 		    _nc_STRCPY(buffer, "ei=", sizeof(buffer));
1074 		    WRAP_CONCAT;
1075 		    continue;
1076 		}
1077 	    }
1078 	    /*
1079 	     * termcap applications such as screen will be confused if sgr0
1080 	     * is translated to a string containing rmacs.  Filter that out.
1081 	     */
1082 	    if (PRESENT(exit_attribute_mode)) {
1083 		if (SAME_CAP(i, exit_attribute_mode)) {
1084 		    char *trimmed_sgr0;
1085 		    char *my_sgr = set_attributes;
1086 
1087 		    set_attributes = save_sgr;
1088 
1089 		    trimmed_sgr0 = _nc_trim_sgr0(tterm);
1090 		    if (strcmp(capability, trimmed_sgr0)) {
1091 			capability = trimmed_sgr0;
1092 		    } else {
1093 			if (trimmed_sgr0 != exit_attribute_mode)
1094 			    free(trimmed_sgr0);
1095 		    }
1096 
1097 		    set_attributes = my_sgr;
1098 		}
1099 	    }
1100 	}
1101 
1102 	predval = pred(STRING, i);
1103 	buffer[0] = '\0';
1104 
1105 	if (predval != FAIL) {
1106 	    if (VALID_STRING(capability)
1107 		&& i + 1 > num_strings)
1108 		num_strings = i + 1;
1109 
1110 	    if (!VALID_STRING(capability)) {
1111 		_nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1112 			    "%s@", name);
1113 		WRAP_CONCAT;
1114 	    } else if (TcOutput()) {
1115 		char *srccap = _nc_tic_expand(capability, TRUE, numbers);
1116 		int params = ((i < (int) SIZEOF(parametrized))
1117 			      ? parametrized[i]
1118 			      : ((*srccap == 'k')
1119 				 ? 0
1120 				 : has_params(srccap, FALSE)));
1121 		const char *cv = _nc_infotocap(name, srccap, params);
1122 
1123 		if (cv == NULL) {
1124 		    if (outform == F_TCONVERR) {
1125 			_nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1126 				    "%s=!!! %s WILL NOT CONVERT !!!",
1127 				    name, srccap);
1128 			WRAP_CONCAT;
1129 		    } else if (suppress_untranslatable) {
1130 			continue;
1131 		    } else {
1132 			const char *s = srccap;
1133 			char *d = buffer;
1134 			int need = 3 + (int) strlen(name);
1135 			while ((*d = *s++) != 0) {
1136 			    if ((d - buffer + 2) >= (int) sizeof(buffer)) {
1137 				fprintf(stderr,
1138 					"%s: value for %s is too long\n",
1139 					_nc_progname,
1140 					name);
1141 				*d = '\0';
1142 				break;
1143 			    }
1144 			    if (*d == ':') {
1145 				*d++ = '\\';
1146 				*d = ':';
1147 			    } else if (*d == '\\') {
1148 				if ((*++d = *s++) == '\0')
1149 				    break;
1150 			    }
1151 			    d++;
1152 			    *d = '\0';
1153 			}
1154 			need += (int) (d - buffer);
1155 			wrap_concat("..", need, w1ST | wERR);
1156 			need -= 2;
1157 			wrap_concat(name, need, wOFF | wERR);
1158 			need -= (int) strlen(name);
1159 			wrap_concat("=", need, w2ND | wERR);
1160 			need -= 1;
1161 			wrap_concat(buffer, need, wEND | wERR);
1162 			outcount = TRUE;
1163 		    }
1164 		} else {
1165 		    wrap_concat3(name, "=", cv);
1166 		}
1167 		len += (int) strlen(capability) + 1;
1168 	    } else {
1169 		char *src = _nc_tic_expand(capability,
1170 					   outform == F_TERMINFO, numbers);
1171 
1172 		strcpy_DYN(&tmpbuf, NULL);
1173 		strcpy_DYN(&tmpbuf, name);
1174 		strcpy_DYN(&tmpbuf, "=");
1175 		if (pretty
1176 		    && (outform == F_TERMINFO
1177 			|| outform == F_VARIABLE)) {
1178 		    fmt_complex(tterm, name, src, 1);
1179 		} else {
1180 		    strcpy_DYN(&tmpbuf, src);
1181 		}
1182 		len += (int) strlen(capability) + 1;
1183 		WRAP_CONCAT1(tmpbuf.text);
1184 	    }
1185 	}
1186 	/* e.g., trimmed_sgr0 */
1187 	if (VALID_STRING(capability) &&
1188 	    capability != tterm->Strings[i])
1189 	    free(capability);
1190     }
1191     len += (int) (num_strings * 2);
1192 
1193     /*
1194      * This piece of code should be an effective inverse of the functions
1195      * postprocess_terminfo() and postprocess_terminfo() in parse_entry.c.
1196      * Much more work should be done on this to support dumping termcaps.
1197      */
1198     if (tversion == V_HPUX) {
1199 	if (VALID_STRING(memory_lock)) {
1200 	    _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1201 			"meml=%s", memory_lock);
1202 	    WRAP_CONCAT;
1203 	}
1204 	if (VALID_STRING(memory_unlock)) {
1205 	    _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1206 			"memu=%s", memory_unlock);
1207 	    WRAP_CONCAT;
1208 	}
1209     } else if (tversion == V_AIX) {
1210 	if (VALID_STRING(acs_chars)) {
1211 	    bool box_ok = TRUE;
1212 	    static const char acstrans[] = "lqkxjmwuvtn";
1213 	    const char *cp;
1214 	    const char *sp;
1215 	    char *tp, boxchars[sizeof(acstrans)];
1216 
1217 	    tp = boxchars;
1218 	    for (cp = acstrans; *cp; cp++) {
1219 		sp = (strchr) (acs_chars, *cp);
1220 		if (sp)
1221 		    *tp++ = sp[1];
1222 		else {
1223 		    box_ok = FALSE;
1224 		    break;
1225 		}
1226 	    }
1227 	    *tp = '\0';
1228 
1229 	    if (box_ok) {
1230 		char *tmp = _nc_tic_expand(boxchars,
1231 					   (outform == F_TERMINFO),
1232 					   numbers);
1233 		_nc_STRCPY(buffer, "box1=", sizeof(buffer));
1234 		while (*tmp != '\0') {
1235 		    size_t have = strlen(buffer);
1236 		    size_t next = strlen(tmp);
1237 		    size_t want = have + next + 1;
1238 		    size_t last = next;
1239 		    char save = '\0';
1240 
1241 		    /*
1242 		     * If the expanded string is too long for the buffer,
1243 		     * chop it off and save the location where we chopped it.
1244 		     */
1245 		    if (want >= sizeof(buffer)) {
1246 			save = tmp[last];
1247 			tmp[last] = '\0';
1248 		    }
1249 		    _nc_STRCAT(buffer, tmp, sizeof(buffer));
1250 
1251 		    /*
1252 		     * If we chopped the buffer, replace the missing piece and
1253 		     * shift everything to append the remainder.
1254 		     */
1255 		    if (save != '\0') {
1256 			next = 0;
1257 			tmp[last] = save;
1258 			while ((tmp[next] = tmp[last + next]) != '\0') {
1259 			    ++next;
1260 			}
1261 		    } else {
1262 			break;
1263 		    }
1264 		}
1265 		WRAP_CONCAT;
1266 	    }
1267 	}
1268     }
1269 
1270     /*
1271      * kludge: trim off trailer to avoid an extra blank line
1272      * in infocmp -u output when there are no string differences
1273      */
1274     if (outcount) {
1275 	bool trimmed = FALSE;
1276 	j = (PredIdx) outbuf.used;
1277 	if (wrapped && did_wrap) {
1278 	    /* EMPTY */ ;
1279 	} else if (j >= 2
1280 		   && outbuf.text[j - 1] == '\t'
1281 		   && outbuf.text[j - 2] == '\n') {
1282 	    outbuf.used -= 2;
1283 	    trimmed = TRUE;
1284 	} else if (j >= 4
1285 		   && outbuf.text[j - 1] == ':'
1286 		   && outbuf.text[j - 2] == '\t'
1287 		   && outbuf.text[j - 3] == '\n'
1288 		   && outbuf.text[j - 4] == '\\') {
1289 	    outbuf.used -= 4;
1290 	    trimmed = TRUE;
1291 	}
1292 	if (trimmed) {
1293 	    outbuf.text[outbuf.used] = '\0';
1294 	    column = oldcol;
1295 	    strcpy_DYN(&outbuf, " ");
1296 	}
1297     }
1298 #if 0
1299     fprintf(stderr, "num_bools = %d\n", num_bools);
1300     fprintf(stderr, "num_values = %d\n", num_values);
1301     fprintf(stderr, "num_strings = %d\n", num_strings);
1302     fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
1303 	    tterm->term_names, len, outbuf.used, outbuf.text);
1304 #endif
1305     /*
1306      * Here's where we use infodump to trigger a more stringent length check
1307      * for termcap-translation purposes.
1308      * Return the length of the raw entry, without tc= expansions,
1309      * It gives an idea of which entries are deadly to even *scan past*,
1310      * as opposed to *use*.
1311      */
1312     return (infodump ? len : (int) termcap_length(outbuf.text));
1313 }
1314 
1315 static bool
kill_string(TERMTYPE2 * tterm,const char * const cap)1316 kill_string(TERMTYPE2 *tterm, const char *const cap)
1317 {
1318     unsigned n;
1319     for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1320 	if (cap == tterm->Strings[n]) {
1321 	    tterm->Strings[n] = ABSENT_STRING;
1322 	    return TRUE;
1323 	}
1324     }
1325     return FALSE;
1326 }
1327 
1328 static char *
find_string(TERMTYPE2 * tterm,const char * name)1329 find_string(TERMTYPE2 *tterm, const char *name)
1330 {
1331     PredIdx n;
1332     for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1333 	if (version_filter(STRING, n)
1334 	    && !strcmp(name, strnames[n])) {
1335 	    char *cap = tterm->Strings[n];
1336 	    if (VALID_STRING(cap)) {
1337 		return cap;
1338 	    }
1339 	    break;
1340 	}
1341     }
1342     return ABSENT_STRING;
1343 }
1344 
1345 /*
1346  * This is used to remove function-key labels from a termcap entry to
1347  * make it smaller.
1348  */
1349 static int
kill_labels(TERMTYPE2 * tterm,int target)1350 kill_labels(TERMTYPE2 *tterm, int target)
1351 {
1352     int n;
1353     int result = 0;
1354     char name[20];
1355 
1356     for (n = 0; n <= 10; ++n) {
1357 	const char *cap;
1358 
1359 	_nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "lf%d", n);
1360 	cap = find_string(tterm, name);
1361 	if (VALID_STRING(cap)
1362 	    && kill_string(tterm, cap)) {
1363 	    target -= (int) (strlen(cap) + 5);
1364 	    ++result;
1365 	    if (target < 0)
1366 		break;
1367 	}
1368     }
1369     return result;
1370 }
1371 
1372 /*
1373  * This is used to remove function-key definitions from a termcap entry to
1374  * make it smaller.
1375  */
1376 static int
kill_fkeys(TERMTYPE2 * tterm,int target)1377 kill_fkeys(TERMTYPE2 *tterm, int target)
1378 {
1379     int n;
1380     int result = 0;
1381     char name[20];
1382 
1383     for (n = 60; n >= 0; --n) {
1384 	const char *cap;
1385 
1386 	_nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n);
1387 	cap = find_string(tterm, name);
1388 	if (VALID_STRING(cap)
1389 	    && kill_string(tterm, cap)) {
1390 	    target -= (int) (strlen(cap) + 5);
1391 	    ++result;
1392 	    if (target < 0)
1393 		break;
1394 	}
1395     }
1396     return result;
1397 }
1398 
1399 /*
1400  * Check if the given acsc string is a 1-1 mapping, i.e., just-like-vt100.
1401  * Also, since this is for termcap, we only care about the line-drawing map.
1402  */
1403 #define isLine(c) (strchr("lmkjtuvwqxn", c) != NULL)
1404 
1405 static bool
one_one_mapping(const char * mapping)1406 one_one_mapping(const char *mapping)
1407 {
1408     bool result = TRUE;
1409 
1410     if (VALID_STRING(mapping)) {
1411 	int n = 0;
1412 	while (mapping[n] != '\0' && mapping[n + 1] != '\0') {
1413 	    if (isLine(mapping[n]) &&
1414 		mapping[n] != mapping[n + 1]) {
1415 		result = FALSE;
1416 		break;
1417 	    }
1418 	    n += 2;
1419 	}
1420     }
1421     return result;
1422 }
1423 
1424 #define FMT_ENTRY() \
1425 		fmt_entry(tterm, pred, \
1426 			0, \
1427 			suppress_untranslatable, \
1428 			infodump, numbers)
1429 
1430 #define SHOW_WHY PRINTF
1431 
1432 static bool
purged_acs(TERMTYPE2 * tterm)1433 purged_acs(TERMTYPE2 *tterm)
1434 {
1435     bool result = FALSE;
1436 
1437     if (VALID_STRING(acs_chars)) {
1438 	if (!one_one_mapping(acs_chars)) {
1439 	    enter_alt_charset_mode = ABSENT_STRING;
1440 	    exit_alt_charset_mode = ABSENT_STRING;
1441 	    SHOW_WHY("# (rmacs/smacs removed for consistency)\n");
1442 	}
1443 	result = TRUE;
1444     }
1445     return result;
1446 }
1447 
1448 static void
encode_b64(char * target,const char * source,unsigned state,int * saved)1449 encode_b64(char *target, const char *source, unsigned state, int *saved)
1450 {
1451     /* RFC-4648 */
1452     static const char data[] =
1453     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1454     "abcdefghijklmnopqrstuvwxyz"
1455     "0123456789" "-_";
1456     int ch = UChar(source[state]);
1457 
1458     switch (state % 3) {
1459     case 0:
1460 	*target++ = data[(ch >> 2) & 077];
1461 	*saved = (ch << 4);
1462 	break;
1463     case 1:
1464 	*target++ = data[((ch >> 4) | *saved) & 077];
1465 	*saved = (ch << 2);
1466 	break;
1467     case 2:
1468 	*target++ = data[((ch >> 6) | *saved) & 077];
1469 	*target++ = data[ch & 077];
1470 	*saved = 0;
1471 	break;
1472     }
1473     *target = '\0';
1474 }
1475 
1476 /*
1477  * Dump a single entry.
1478  */
1479 void
dump_entry(TERMTYPE2 * tterm,int suppress_untranslatable,int limited,int numbers,PredFunc pred)1480 dump_entry(TERMTYPE2 *tterm,
1481 	   int suppress_untranslatable,
1482 	   int limited,
1483 	   int numbers,
1484 	   PredFunc pred)
1485 {
1486     TERMTYPE2 save_tterm;
1487     int critlen;
1488     const char *legend;
1489     bool infodump;
1490 
1491     if (quickdump) {
1492 	char bigbuf[65536];
1493 	unsigned offset = 0;
1494 
1495 	separator = "";
1496 	trailer = "\n";
1497 	indent = 0;
1498 
1499 	if (_nc_write_object(tterm, bigbuf, &offset, sizeof(bigbuf)) == OK) {
1500 	    char numbuf[80];
1501 	    unsigned n;
1502 
1503 	    if (quickdump & 1) {
1504 		if (outbuf.used)
1505 		    wrap_concat1("\n");
1506 		wrap_concat1("hex:");
1507 		for (n = 0; n < offset; ++n) {
1508 		    _nc_SPRINTF(numbuf, _nc_SLIMIT(sizeof(numbuf))
1509 				"%02X", UChar(bigbuf[n]));
1510 		    wrap_concat1(numbuf);
1511 		}
1512 	    }
1513 	    if (quickdump & 2) {
1514 		static const char padding[] =
1515 		{0, 0};
1516 		int value = 0;
1517 
1518 		if (outbuf.used)
1519 		    wrap_concat1("\n");
1520 		wrap_concat1("b64:");
1521 		for (n = 0; n < offset; ++n) {
1522 		    encode_b64(numbuf, bigbuf, n, &value);
1523 		    wrap_concat1(numbuf);
1524 		}
1525 		switch (n % 3) {
1526 		case 0:
1527 		    break;
1528 		case 1:
1529 		    encode_b64(numbuf, padding, 1, &value);
1530 		    wrap_concat1(numbuf);
1531 		    wrap_concat1("==");
1532 		    break;
1533 		case 2:
1534 		    encode_b64(numbuf, padding, 1, &value);
1535 		    wrap_concat1(numbuf);
1536 		    wrap_concat1("=");
1537 		    break;
1538 		}
1539 	    }
1540 	}
1541 	return;
1542     }
1543 
1544     if (TcOutput()) {
1545 	critlen = MAX_TERMCAP_LENGTH;
1546 	legend = "older termcap";
1547 	infodump = FALSE;
1548 	set_obsolete_termcaps(tterm);
1549     } else {
1550 	critlen = MAX_TERMINFO_LENGTH;
1551 	legend = "terminfo";
1552 	infodump = TRUE;
1553     }
1554 
1555     save_sgr = set_attributes;
1556 
1557     if ((FMT_ENTRY() > critlen)
1558 	&& TcOutput()
1559 	&& limited) {
1560 
1561 	save_tterm = *tterm;
1562 	if (!suppress_untranslatable) {
1563 	    SHOW_WHY("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1564 		     critlen);
1565 	    suppress_untranslatable = TRUE;
1566 	}
1567 	if (FMT_ENTRY() > critlen) {
1568 	    /*
1569 	     * We pick on sgr because it is a nice long string capability that
1570 	     * is really just an optimization hack.  Another good candidate is
1571 	     * acsc since it is both long and unused by BSD termcap.
1572 	     */
1573 	    bool changed = FALSE;
1574 
1575 #if NCURSES_XNAMES
1576 	    /*
1577 	     * Extended names are most likely function-key definitions.  Drop
1578 	     * those first.
1579 	     */
1580 	    unsigned n;
1581 	    for (n = STRCOUNT; n < NUM_STRINGS(tterm); n++) {
1582 		const char *name = ExtStrname(tterm, (int) n, strnames);
1583 
1584 		if (VALID_STRING(tterm->Strings[n])) {
1585 		    set_attributes = ABSENT_STRING;
1586 		    /* we remove long names anyway - only report the short */
1587 		    if (strlen(name) <= 2) {
1588 			SHOW_WHY("# (%s removed to fit entry within %d bytes)\n",
1589 				 name,
1590 				 critlen);
1591 		    }
1592 		    changed = TRUE;
1593 		    if (FMT_ENTRY() <= critlen)
1594 			break;
1595 		}
1596 	    }
1597 #endif
1598 	    if (VALID_STRING(set_attributes)) {
1599 		set_attributes = ABSENT_STRING;
1600 		SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n",
1601 			 critlen);
1602 		changed = TRUE;
1603 	    }
1604 	    if (!changed || (FMT_ENTRY() > critlen)) {
1605 		if (purged_acs(tterm)) {
1606 		    acs_chars = ABSENT_STRING;
1607 		    SHOW_WHY("# (acsc removed to fit entry within %d bytes)\n",
1608 			     critlen);
1609 		    changed = TRUE;
1610 		}
1611 	    }
1612 	    if (!changed || (FMT_ENTRY() > critlen)) {
1613 		int oldversion = tversion;
1614 		int len;
1615 
1616 		tversion = V_BSD;
1617 		SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1618 			 critlen);
1619 
1620 		len = FMT_ENTRY();
1621 		if (len > critlen
1622 		    && kill_labels(tterm, len - critlen)) {
1623 		    SHOW_WHY("# (some labels capabilities suppressed to fit entry within %d bytes)\n",
1624 			     critlen);
1625 		    len = FMT_ENTRY();
1626 		}
1627 		if (len > critlen
1628 		    && kill_fkeys(tterm, len - critlen)) {
1629 		    SHOW_WHY("# (some function-key capabilities suppressed to fit entry within %d bytes)\n",
1630 			     critlen);
1631 		    len = FMT_ENTRY();
1632 		}
1633 		if (len > critlen) {
1634 		    (void) fprintf(stderr,
1635 				   "%s: %s entry is %d bytes long\n",
1636 				   _nc_progname,
1637 				   _nc_first_name(tterm->term_names),
1638 				   len);
1639 		    SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
1640 			     len, legend);
1641 		}
1642 		tversion = oldversion;
1643 	    }
1644 	    set_attributes = save_sgr;
1645 	    *tterm = save_tterm;
1646 	}
1647     } else if (!version_filter(STRING, STR_IDX(acs_chars))) {
1648 	save_tterm = *tterm;
1649 	if (purged_acs(tterm)) {
1650 	    (void) FMT_ENTRY();
1651 	}
1652 	*tterm = save_tterm;
1653     }
1654 }
1655 
1656 void
dump_uses(const char * value,bool infodump)1657 dump_uses(const char *value, bool infodump)
1658 /* dump "use=" clauses in the appropriate format */
1659 {
1660     char buffer[MAX_TERMINFO_LENGTH + EXTRA_CAP];
1661     int limit = (VALID_STRING(value) ? (int) strlen(value) : 0);
1662     const char *cap = infodump ? "use" : "tc";
1663 
1664     if (TcOutput())
1665 	trim_trailing();
1666     if (limit == 0) {
1667 	_nc_warning("empty \"%s\" field", cap);
1668 	value = "";
1669     } else if (limit > MAX_ALIAS) {
1670 	_nc_warning("\"%s\" field too long (%d), limit to %d",
1671 		    cap, limit, MAX_ALIAS);
1672 	limit = MAX_ALIAS;
1673     }
1674     _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1675 		"%s=%.*s", cap, limit, value);
1676     wrap_concat1(buffer);
1677 }
1678 
1679 int
show_entry(void)1680 show_entry(void)
1681 {
1682     /*
1683      * Trim any remaining whitespace.
1684      */
1685     if (outbuf.used != 0) {
1686 	bool infodump = !TcOutput();
1687 	char delim = (char) (infodump ? ',' : ':');
1688 	int j;
1689 
1690 	for (j = (int) outbuf.used - 1; j > 0; --j) {
1691 	    char ch = outbuf.text[j];
1692 	    if (ch == '\n') {
1693 		;
1694 	    } else if (isspace(UChar(ch))) {
1695 		outbuf.used = (size_t) j;
1696 	    } else if (!infodump && ch == '\\') {
1697 		outbuf.used = (size_t) j;
1698 	    } else if (ch == delim && (outbuf.text[j - 1] != '\\')) {
1699 		outbuf.used = (size_t) (j + 1);
1700 	    } else {
1701 		break;
1702 	    }
1703 	}
1704 	outbuf.text[outbuf.used] = '\0';
1705     }
1706     if (outbuf.text != NULL) {
1707 	(void) fputs(outbuf.text, stdout);
1708 	putchar('\n');
1709     }
1710     return (int) outbuf.used;
1711 }
1712 
1713 void
compare_entry(PredHook hook,TERMTYPE2 * tp GCC_UNUSED,bool quiet)1714 compare_entry(PredHook hook,
1715 	      TERMTYPE2 *tp GCC_UNUSED,
1716 	      bool quiet)
1717 /* compare two entries */
1718 {
1719     PredIdx i, j;
1720     NCURSES_CONST char *name;
1721 
1722     if (!quiet)
1723 	fputs("    comparing booleans.\n", stdout);
1724     for_each_boolean(j, tp) {
1725 	i = BoolIndirect(j);
1726 	name = ExtBoolname(tp, (int) i, bool_names);
1727 
1728 	if (isObsolete(outform, name))
1729 	    continue;
1730 
1731 	(*hook) (CMP_BOOLEAN, i, name);
1732     }
1733 
1734     if (!quiet)
1735 	fputs("    comparing numbers.\n", stdout);
1736     for_each_number(j, tp) {
1737 	i = NumIndirect(j);
1738 	name = ExtNumname(tp, (int) i, num_names);
1739 
1740 	if (isObsolete(outform, name))
1741 	    continue;
1742 
1743 	(*hook) (CMP_NUMBER, i, name);
1744     }
1745 
1746     if (!quiet)
1747 	fputs("    comparing strings.\n", stdout);
1748     for_each_string(j, tp) {
1749 	i = StrIndirect(j);
1750 	name = ExtStrname(tp, (int) i, str_names);
1751 
1752 	if (isObsolete(outform, name))
1753 	    continue;
1754 
1755 	(*hook) (CMP_STRING, i, name);
1756     }
1757 
1758     /* (void) fputs("    comparing use entries.\n", stdout); */
1759     (*hook) (CMP_USE, 0, "use");
1760 
1761 }
1762 
1763 #define NOTSET(s)	((s) == 0)
1764 
1765 /*
1766  * This bit of legerdemain turns all the terminfo variable names into
1767  * references to locations in the arrays Booleans, Numbers, and Strings ---
1768  * precisely what's needed.
1769  */
1770 #undef CUR
1771 #define CUR tp->
1772 
1773 static void
set_obsolete_termcaps(TERMTYPE2 * tp)1774 set_obsolete_termcaps(TERMTYPE2 *tp)
1775 {
1776 #include "capdefaults.c"
1777 }
1778 
1779 /*
1780  * Convert an alternate-character-set string to canonical form: sorted and
1781  * unique.
1782  */
1783 void
repair_acsc(TERMTYPE2 * tp)1784 repair_acsc(TERMTYPE2 *tp)
1785 {
1786     if (VALID_STRING(acs_chars)) {
1787 	size_t n;
1788 	char mapped[256];
1789 	unsigned source;
1790 	unsigned target;
1791 	bool fix_needed = FALSE;
1792 
1793 	for (n = 0, source = 0; acs_chars[n] != 0; n++) {
1794 	    target = UChar(acs_chars[n]);
1795 	    if (source >= target) {
1796 		fix_needed = TRUE;
1797 		break;
1798 	    }
1799 	    source = target;
1800 	    if (acs_chars[n + 1])
1801 		n++;
1802 	}
1803 
1804 	if (fix_needed) {
1805 	    size_t m;
1806 	    char extra = 0;
1807 
1808 	    memset(mapped, 0, sizeof(mapped));
1809 	    for (n = 0; acs_chars[n] != 0; n++) {
1810 		source = UChar(acs_chars[n]);
1811 		if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
1812 		    mapped[source] = (char) target;
1813 		    n++;
1814 		} else {
1815 		    extra = (char) source;
1816 		}
1817 	    }
1818 	    for (n = m = 0; n < sizeof(mapped); n++) {
1819 		if (mapped[n]) {
1820 		    acs_chars[m++] = (char) n;
1821 		    acs_chars[m++] = mapped[n];
1822 		}
1823 	    }
1824 	    if (extra)
1825 		acs_chars[m++] = extra;		/* garbage in, garbage out */
1826 	    acs_chars[m] = 0;
1827 	}
1828     }
1829 }
1830