1 /****************************************************************************
2 * Copyright 2018-2022,2023 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
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 ****************************************************************************/
36 #define __INTERNAL_CAPS_VISIBLE
37 #include <progs.priv.h>
39 #include <dump_entry.h>
40 #include <termsort.h> /* this C file is generated */
41 #include <parametrized.h> /* so is this */
43 MODULE_ID("$Id: dump_entry.c,v 1.195 2023/01/21 21:25:21 tom Exp $")
45 #define DISCARD(string) string = ABSENT_STRING
46 #define PRINTF (void) printf
49 #define OkIndex(index,array) ((int)(index) >= 0 && (int)(index) < (int) SIZEOF(array))
50 #define TcOutput() (outform == F_TERMCAP || outform == F_TCONVERR)
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 */
71 static char *save_sgr;
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;
82 static const char *separator = "", *trailer = "";
83 static int indent = 8;
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 */
93 #define OBSOLETE(n) (!_nc_user_definable && (n[0] == 'O' && n[1] == 'T'))
95 #define OBSOLETE(n) (n[0] == 'O' && n[1] == 'T')
98 #define isObsolete(f,n) ((f == F_TERMINFO || f == F_VARIABLE) && (sortmode != S_VARIABLE) && OBSOLETE(n))
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]))
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])
110 static GCC_NORETURN void
111 failed(const char *s)
114 ExitProgram(EXIT_FAILURE);
118 strncpy_DYN(DYNBUF * dst, const char *src, size_t need)
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);
125 failed("strncpy_DYN");
127 _nc_STRNCPY(dst->text + dst->used, src, need + 1);
129 dst->text[dst->used] = 0;
133 strcpy_DYN(DYNBUF * dst, const char *src)
139 strncpy_DYN(dst, src, strlen(src));
155 _nc_leaks_dump_entry(void)
162 #define NameTrans(check,result) \
163 if ((np->nte_index <= OK_ ## check) \
164 && check[np->nte_index]) \
165 return (result[np->nte_index])
168 nametrans(const char *name)
169 /* translate a capability name to termcap from terminfo */
171 const struct name_table_entry *np;
173 if ((np = _nc_find_entry(name, _nc_get_hash_table(0))) != 0) {
174 switch (np->nte_type) {
176 NameTrans(bool_from_termcap, boolcodes);
180 NameTrans(num_from_termcap, numcodes);
184 NameTrans(str_from_termcap, strcodes);
193 dump_init(const char *version,
203 /* set up for entry display */
208 wrapped = wrap_strings;
210 quickdump = (quick & 3);
212 did_wrap = (width <= 0);
216 tversion = V_ALLCAPS;
217 else if (!strcmp(version, "SVr1") || !strcmp(version, "SVR1")
218 || !strcmp(version, "Ultrix"))
220 else if (!strcmp(version, "HP"))
222 else if (!strcmp(version, "AIX"))
224 else if (!strcmp(version, "BSD"))
227 tversion = V_ALLCAPS;
229 /* implement display modes */
230 switch (outform = mode) {
233 bool_names = boolnames;
234 num_names = numnames;
235 str_names = strnames;
236 separator = (twidth > 0 && theight > 1) ? ", " : ",";
241 bool_names = boolfnames;
242 num_names = numfnames;
243 str_names = strfnames;
244 separator = (twidth > 0 && theight > 1) ? ", " : ",";
250 bool_names = boolcodes;
251 num_names = numcodes;
252 str_names = strcodes;
259 /* implement sort modes */
260 switch (sortmode = sort) {
263 (void) fprintf(stderr,
264 "%s: sorting by term structure order\n", _nc_progname);
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;
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;
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;
296 (void) fprintf(stderr,
297 "%s: width = %d, tversion = %d, outform = %d\n",
298 _nc_progname, width, tversion, outform);
301 static TERMTYPE2 *cur_type;
304 dump_predicate(PredType type, PredIdx idx)
305 /* predicate function to use for ordinary decompilation */
309 return (cur_type->Booleans[idx] == FALSE)
310 ? FAIL : cur_type->Booleans[idx];
313 return (cur_type->Numbers[idx] == ABSENT_NUMERIC)
314 ? FAIL : cur_type->Numbers[idx];
317 return (cur_type->Strings[idx] != ABSENT_STRING)
321 return (FALSE); /* pacify compiler */
324 static void set_obsolete_termcaps(TERMTYPE2 *tp);
326 /* is this the index of a function key string? */
328 (((i) >= STR_IDX(key_f0) && \
329 (i) <= STR_IDX(key_f9)) || \
330 ((i) >= STR_IDX(key_f11) && \
331 (i) <= STR_IDX(key_f63)))
334 * If we configure with a different Caps file, the offsets into the arrays
335 * will change. So we use an address expression.
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]))
342 version_filter(PredType type, PredIdx idx)
343 /* filter out capabilities we may want to suppress */
346 case V_ALLCAPS: /* SVr4, XSI Curses */
349 case V_SVR1: /* System V Release 1, Ultrix */
352 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
354 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
356 return ((idx <= STR_IDX(prtr_non)) ? TRUE : FALSE);
360 case V_HPUX: /* Hewlett-Packard */
363 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
365 return ((idx <= NUM_IDX(label_width)) ? TRUE : FALSE);
367 if (idx <= STR_IDX(prtr_non))
369 else if (FNKEY(idx)) /* function keys */
371 else if (idx == STR_IDX(plab_norm)
372 || idx == STR_IDX(label_on)
373 || idx == STR_IDX(label_off))
380 case V_AIX: /* AIX */
383 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
385 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
387 if (idx <= STR_IDX(prtr_non))
389 else if (FNKEY(idx)) /* function keys */
396 #define is_termcap(type) (OkIndex(idx, type##_from_termcap) && \
397 type##_from_termcap[idx])
399 case V_BSD: /* BSD */
402 return is_termcap(bool);
404 return is_termcap(num);
406 return is_termcap(str);
411 return (FALSE); /* pacify the compiler */
417 while (outbuf.used > 0 && outbuf.text[outbuf.used - 1] == ' ')
418 outbuf.text[--outbuf.used] = '\0';
426 strcpy_DYN(&outbuf, trailer);
431 op_length(const char *src, int offset)
435 if (offset > 0 && src[offset - 1] == '\\') {
440 result++; /* for '%' mark */
441 ch = src[offset + result];
445 } else if (ch == '+') {
450 } else if (ch == '\'') {
452 } else if (ch == L_CURL[0]) {
454 while ((ch = src[offset + n]) != '\0') {
455 if (ch == R_CURL[0]) {
461 } else if (strchr("pPg", ch) != 0) {
464 result++; /* ordinary operator */
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.
477 find_split(const char *src, int step, int size)
482 /* check if that would split a backslash-sequence */
486 for (n = size - 1; n > 0; --n) {
487 int ch = UChar(src[step + n]);
489 if (n > 0 && src[step + n - 1] == ch)
493 } else if (!isalnum(ch)) {
500 /* check if that would split a backslash-sequence */
501 for (n = size - 1; n > 0; --n) {
502 int ch = UChar(src[step + n]);
504 int need = op_length(src, step + n);
505 if ((n + need) > size) {
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.
524 fill_spaces(const char *src)
526 const char *fill = "\\s";
527 size_t need = strlen(src);
528 size_t size = strlen(fill);
532 for (pass = 0; pass < 2; ++pass) {
533 for (s = d = 0; src[s] != '\0'; ++s) {
536 _nc_STRCPY(&result[d], fill, need + 1 - d);
543 result[d++] = src[s];
552 result = calloc(need + 1, sizeof(char));
554 failed("fill_spaces");
568 #define wrap_1ST(mode) ((mode)&w1ST)
569 #define wrap_END(mode) ((mode)&wEND)
570 #define wrap_ERR(mode) ((mode)&wERR)
573 wrap_concat(const char *src, int need, unsigned mode)
575 int gaps = (int) strlen(separator);
576 int want = gaps + need;
578 did_wrap = (width <= 0);
581 && column + want > width) {
584 if ((wrap_END(mode) && !wrap_ERR(mode)) &&
587 (column + want) > width) {
589 int used = width > WRAPPED ? width : WRAPPED;
592 const char *my_t = trailer;
593 char *fill = fill_spaces(src);
594 int last = (int) strlen(fill);
601 if (!TcOutput() && (p = strchr(fill, '=')) != 0) {
602 base = (int) (p + 1 - fill);
605 _nc_SPRINTF(align, _nc_SLIMIT(align) "%*s", base, " ");
606 } else if (column > 8) {
610 _nc_SPRINTF(align, _nc_SLIMIT(align) "%*s", base, " ");
614 /* "pretty" overrides wrapping if it already split the line */
615 if (!pretty || strchr(fill, '\n') == 0) {
618 if (TcOutput() && outbuf.used && !wrap_1ST(mode)) {
622 while ((column + (need + gaps)) > used) {
623 int size = used - tag;
625 strcpy_DYN(&outbuf, align);
628 if (size > (last - step)) {
629 size = (last - step);
631 size = find_split(fill, step, size);
632 strncpy_DYN(&outbuf, fill + step, (size_t) size);
644 strcpy_DYN(&outbuf, align);
645 strcpy_DYN(&outbuf, fill + step);
648 strcpy_DYN(&outbuf, separator);
654 strcpy_DYN(&outbuf, src);
656 strcpy_DYN(&outbuf, separator);
657 column += (int) strlen(src);
662 wrap_concat1(const char *src)
664 int need = (int) strlen(src);
665 wrap_concat(src, need, w1ST | wEND);
669 wrap_concat3(const char *name, const char *eqls, const char *value)
671 int nlen = (int) strlen(name);
672 int elen = (int) strlen(eqls);
673 int vlen = (int) strlen(value);
675 wrap_concat(name, nlen + elen + vlen, w1ST);
676 wrap_concat(eqls, elen + vlen, w2ND);
677 wrap_concat(value, vlen, wEND);
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
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 ":".
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.
694 #ifdef TEST_TERMCAP_LENGTH
696 termcap_length(const char *src)
698 static const char pattern[] = ":\\\n\t:";
701 const char *const t = src + strlen(src);
703 while (*src != '\0') {
704 IGNORE_SEP_TRAIL(src, t, pattern);
711 #define termcap_length(src) strlen(src)
715 indent_DYN(DYNBUF * buffer, int level)
719 for (n = 0; n < level; n++)
720 strncpy_DYN(buffer, "\t", (size_t) 1);
724 * Check if the current line which was begun consists only of a tab and the
725 * given leading text.
728 leading_DYN(DYNBUF * buffer, const char *leading)
731 size_t need = strlen(leading);
732 if (buffer->used > need) {
733 need = buffer->used - need;
734 if (!strcmp(buffer->text + need, leading)) {
736 while (--need != 0) {
737 if (buffer->text[need] == '\n') {
740 if (buffer->text[need] != '\t') {
751 has_params(const char *src, bool formatting)
754 int len = (int) strlen(src);
759 for (n = 0; n < len - 1; ++n) {
760 if (!strncmp(src + n, "%p", (size_t) 2)) {
762 } else if (!strncmp(src + n, "%;", (size_t) 2)) {
770 result = ((len > 50) && params);
779 fmt_complex(TERMTYPE2 *tterm, const char *capability, char *src, int level)
781 bool percent = FALSE;
782 bool params = has_params(src, TRUE);
784 while (*src != '\0') {
788 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
792 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
798 case 't': /* "then" */
799 case 'e': /* "else" */
802 tmpbuf.text[tmpbuf.used - 1] = '\n';
803 /* treat a "%e" as else-if, on the same level */
805 indent_DYN(&tmpbuf, level);
806 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
807 strncpy_DYN(&tmpbuf, src, (size_t) 1);
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);
815 indent_DYN(&tmpbuf, level + 1);
816 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
817 strncpy_DYN(&tmpbuf, src, (size_t) 1);
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);
824 } else if (level == 1) {
826 _nc_warning("%s: %%%c without %%? in %s",
827 _nc_first_name(tterm->term_names),
834 case ';': /* "endif" */
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);
844 && (strchr("?e;", src[1])) == 0) {
845 tmpbuf.text[tmpbuf.used++] = '\n';
846 indent_DYN(&tmpbuf, level);
851 _nc_warning("%s: %%; without %%? in %s",
852 _nc_first_name(tterm->term_names),
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);
865 strncpy_DYN(&tmpbuf, "\\s", (size_t) 2);
872 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
878 * Make "large" numbers a little easier to read by showing them in hexadecimal
879 * if they are "close" to a power of two.
882 number_format(int value)
884 const char *result = "%d";
886 if ((outform != F_TERMCAP) && (value > 255)) {
887 unsigned long lv = (unsigned long) value;
888 int bits = sizeof(unsigned long) * 8;
891 for (nn = 8; nn < bits; ++nn) {
895 if ((mm - 16) <= lv && (mm + 16) > lv) {
904 #define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap)
908 fmt_entry(TERMTYPE2 *tterm,
911 int suppress_untranslatable,
916 char buffer[MAX_TERMINFO_LENGTH + EXTRA_CAP];
917 NCURSES_CONST char *name;
919 PredIdx num_bools = 0;
920 PredIdx num_values = 0;
921 PredIdx num_strings = 0;
924 #define WRAP_CONCAT1(s) wrap_concat1(s); outcount = TRUE
925 #define WRAP_CONCAT WRAP_CONCAT1(buffer)
927 len = 12; /* terminfo file-header */
931 pred = dump_predicate;
934 strcpy_DYN(&outbuf, 0);
936 column = indent; /* workaround to prevent empty lines */
938 strcpy_DYN(&outbuf, tterm->term_names);
941 * Colon is legal in terminfo descriptions, but not in termcap.
944 char *p = outbuf.text;
952 strcpy_DYN(&outbuf, separator);
953 column = (int) outbuf.used;
958 for_each_boolean(j, tterm) {
960 name = ExtBoolname(tterm, (int) i, bool_names);
961 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
963 if (!version_filter(BOOLEAN, i))
965 else if (isObsolete(outform, name))
968 predval = pred(BOOLEAN, i);
969 if (predval != FAIL) {
970 _nc_STRCPY(buffer, name, sizeof(buffer));
972 _nc_STRCAT(buffer, "@", sizeof(buffer));
973 else if (i + 1 > num_bools)
979 if (column != indent && height > 1)
982 for_each_number(j, tterm) {
984 name = ExtNumname(tterm, (int) i, num_names);
985 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
987 if (!version_filter(NUMBER, i))
989 else if (isObsolete(outform, name))
992 predval = pred(NUMBER, i);
993 if (predval != FAIL) {
994 if (tterm->Numbers[i] < 0) {
995 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
999 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1001 nn = strlen(buffer);
1002 _nc_SPRINTF(buffer + nn, _nc_SLIMIT(sizeof(buffer) - nn)
1003 number_format(tterm->Numbers[i]),
1005 if (i + 1 > num_values)
1012 if (column != indent && height > 1)
1015 len += (int) (num_bools
1017 + strlen(tterm->term_names) + 1);
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);
1029 if (VALID_STRING(reset_2string)
1030 && !strcmp(reset_2string, termcap_reset))
1031 DISCARD(reset_2string);
1035 for_each_string(j, tterm) {
1038 name = ExtStrname(tterm, (int) i, str_names);
1039 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
1041 capability = tterm->Strings[i];
1043 if (!version_filter(STRING, i))
1045 else if (isObsolete(outform, name))
1050 * Extended names can be longer than 2 characters, but termcap programs
1051 * cannot read those (filter them out).
1053 if (outform == F_TERMCAP && (strlen(name) > 2))
1057 if (outform == F_TERMCAP) {
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.
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));
1071 if (SAME_CAP(i, exit_insert_mode)
1072 && exit_insert_mode == ABSENT_STRING) {
1073 _nc_STRCPY(buffer, "ei=", sizeof(buffer));
1079 * termcap applications such as screen will be confused if sgr0
1080 * is translated to a string containing rmacs. Filter that out.
1082 if (PRESENT(exit_attribute_mode)) {
1083 if (SAME_CAP(i, exit_attribute_mode)) {
1085 char *my_sgr = set_attributes;
1087 set_attributes = save_sgr;
1089 trimmed_sgr0 = _nc_trim_sgr0(tterm);
1090 if (strcmp(capability, trimmed_sgr0)) {
1091 capability = trimmed_sgr0;
1093 if (trimmed_sgr0 != exit_attribute_mode)
1097 set_attributes = my_sgr;
1102 predval = pred(STRING, i);
1105 if (predval != FAIL) {
1106 if (VALID_STRING(capability)
1107 && i + 1 > num_strings)
1108 num_strings = i + 1;
1110 if (!VALID_STRING(capability)) {
1111 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1114 } else if (TcOutput()) {
1115 char *srccap = _nc_tic_expand(capability, TRUE, numbers);
1116 int params = ((i < (int) SIZEOF(parametrized))
1120 : has_params(srccap, FALSE)));
1121 char *cv = _nc_infotocap(name, srccap, params);
1124 if (outform == F_TCONVERR) {
1125 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1126 "%s=!!! %s WILL NOT CONVERT !!!",
1129 } else if (suppress_untranslatable) {
1132 char *s = srccap, *d = buffer;
1133 int need = 3 + (int) strlen(name);
1134 while ((*d = *s++) != 0) {
1135 if ((d - buffer + 2) >= (int) sizeof(buffer)) {
1137 "%s: value for %s is too long\n",
1146 } else if (*d == '\\') {
1147 if ((*++d = *s++) == '\0')
1153 need += (int) (d - buffer);
1154 wrap_concat("..", need, w1ST | wERR);
1156 wrap_concat(name, need, wOFF | wERR);
1157 need -= (int) strlen(name);
1158 wrap_concat("=", need, w2ND | wERR);
1160 wrap_concat(buffer, need, wEND | wERR);
1164 wrap_concat3(name, "=", cv);
1166 len += (int) strlen(capability) + 1;
1168 char *src = _nc_tic_expand(capability,
1169 outform == F_TERMINFO, numbers);
1171 strcpy_DYN(&tmpbuf, 0);
1172 strcpy_DYN(&tmpbuf, name);
1173 strcpy_DYN(&tmpbuf, "=");
1175 && (outform == F_TERMINFO
1176 || outform == F_VARIABLE)) {
1177 fmt_complex(tterm, name, src, 1);
1179 strcpy_DYN(&tmpbuf, src);
1181 len += (int) strlen(capability) + 1;
1182 WRAP_CONCAT1(tmpbuf.text);
1185 /* e.g., trimmed_sgr0 */
1186 if (VALID_STRING(capability) &&
1187 capability != tterm->Strings[i])
1190 len += (int) (num_strings * 2);
1193 * This piece of code should be an effective inverse of the functions
1194 * postprocess_terminfo() and postprocess_terminfo() in parse_entry.c.
1195 * Much more work should be done on this to support dumping termcaps.
1197 if (tversion == V_HPUX) {
1198 if (VALID_STRING(memory_lock)) {
1199 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1200 "meml=%s", memory_lock);
1203 if (VALID_STRING(memory_unlock)) {
1204 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1205 "memu=%s", memory_unlock);
1208 } else if (tversion == V_AIX) {
1209 if (VALID_STRING(acs_chars)) {
1211 const char *acstrans = "lqkxjmwuvtn";
1213 char *tp, *sp, boxchars[11];
1216 for (cp = acstrans; *cp; cp++) {
1217 sp = (strchr) (acs_chars, *cp);
1228 char *tmp = _nc_tic_expand(boxchars,
1229 (outform == F_TERMINFO),
1231 _nc_STRCPY(buffer, "box1=", sizeof(buffer));
1232 while (*tmp != '\0') {
1233 size_t have = strlen(buffer);
1234 size_t next = strlen(tmp);
1235 size_t want = have + next + 1;
1240 * If the expanded string is too long for the buffer,
1241 * chop it off and save the location where we chopped it.
1243 if (want >= sizeof(buffer)) {
1247 _nc_STRCAT(buffer, tmp, sizeof(buffer));
1250 * If we chopped the buffer, replace the missing piece and
1251 * shift everything to append the remainder.
1256 while ((tmp[next] = tmp[last + next]) != '\0') {
1269 * kludge: trim off trailer to avoid an extra blank line
1270 * in infocmp -u output when there are no string differences
1273 bool trimmed = FALSE;
1274 j = (PredIdx) outbuf.used;
1275 if (wrapped && did_wrap) {
1278 && outbuf.text[j - 1] == '\t'
1279 && outbuf.text[j - 2] == '\n') {
1283 && outbuf.text[j - 1] == ':'
1284 && outbuf.text[j - 2] == '\t'
1285 && outbuf.text[j - 3] == '\n'
1286 && outbuf.text[j - 4] == '\\') {
1291 outbuf.text[outbuf.used] = '\0';
1293 strcpy_DYN(&outbuf, " ");
1297 fprintf(stderr, "num_bools = %d\n", num_bools);
1298 fprintf(stderr, "num_values = %d\n", num_values);
1299 fprintf(stderr, "num_strings = %d\n", num_strings);
1300 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
1301 tterm->term_names, len, outbuf.used, outbuf.text);
1304 * Here's where we use infodump to trigger a more stringent length check
1305 * for termcap-translation purposes.
1306 * Return the length of the raw entry, without tc= expansions,
1307 * It gives an idea of which entries are deadly to even *scan past*,
1308 * as opposed to *use*.
1310 return (infodump ? len : (int) termcap_length(outbuf.text));
1314 kill_string(TERMTYPE2 *tterm, char *cap)
1317 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1318 if (cap == tterm->Strings[n]) {
1319 tterm->Strings[n] = ABSENT_STRING;
1327 find_string(TERMTYPE2 *tterm, char *name)
1330 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1331 if (version_filter(STRING, n)
1332 && !strcmp(name, strnames[n])) {
1333 char *cap = tterm->Strings[n];
1334 if (VALID_STRING(cap)) {
1340 return ABSENT_STRING;
1344 * This is used to remove function-key labels from a termcap entry to
1348 kill_labels(TERMTYPE2 *tterm, int target)
1354 for (n = 0; n <= 10; ++n) {
1357 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "lf%d", n);
1358 cap = find_string(tterm, name);
1359 if (VALID_STRING(cap)
1360 && kill_string(tterm, cap)) {
1361 target -= (int) (strlen(cap) + 5);
1371 * This is used to remove function-key definitions from a termcap entry to
1375 kill_fkeys(TERMTYPE2 *tterm, int target)
1381 for (n = 60; n >= 0; --n) {
1384 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n);
1385 cap = find_string(tterm, name);
1386 if (VALID_STRING(cap)
1387 && kill_string(tterm, cap)) {
1388 target -= (int) (strlen(cap) + 5);
1398 * Check if the given acsc string is a 1-1 mapping, i.e., just-like-vt100.
1399 * Also, since this is for termcap, we only care about the line-drawing map.
1401 #define isLine(c) (strchr("lmkjtuvwqxn", c) != 0)
1404 one_one_mapping(const char *mapping)
1408 if (VALID_STRING(mapping)) {
1410 while (mapping[n] != '\0' && mapping[n + 1] != '\0') {
1411 if (isLine(mapping[n]) &&
1412 mapping[n] != mapping[n + 1]) {
1422 #define FMT_ENTRY() \
1423 fmt_entry(tterm, pred, \
1425 suppress_untranslatable, \
1428 #define SHOW_WHY PRINTF
1431 purged_acs(TERMTYPE2 *tterm)
1433 bool result = FALSE;
1435 if (VALID_STRING(acs_chars)) {
1436 if (!one_one_mapping(acs_chars)) {
1437 enter_alt_charset_mode = ABSENT_STRING;
1438 exit_alt_charset_mode = ABSENT_STRING;
1439 SHOW_WHY("# (rmacs/smacs removed for consistency)\n");
1447 encode_b64(char *target, char *source, unsigned state, int *saved)
1450 static const char data[] =
1451 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1452 "abcdefghijklmnopqrstuvwxyz"
1454 int ch = UChar(source[state]);
1456 switch (state % 3) {
1458 *target++ = data[(ch >> 2) & 077];
1462 *target++ = data[((ch >> 4) | *saved) & 077];
1466 *target++ = data[((ch >> 6) | *saved) & 077];
1467 *target++ = data[ch & 077];
1475 * Dump a single entry.
1478 dump_entry(TERMTYPE2 *tterm,
1479 int suppress_untranslatable,
1484 TERMTYPE2 save_tterm;
1491 unsigned offset = 0;
1497 if (_nc_write_object(tterm, bigbuf, &offset, sizeof(bigbuf)) == OK) {
1501 if (quickdump & 1) {
1504 wrap_concat1("hex:");
1505 for (n = 0; n < offset; ++n) {
1506 _nc_SPRINTF(numbuf, _nc_SLIMIT(sizeof(numbuf))
1507 "%02X", UChar(bigbuf[n]));
1508 wrap_concat1(numbuf);
1511 if (quickdump & 2) {
1512 static char padding[] =
1518 wrap_concat1("b64:");
1519 for (n = 0; n < offset; ++n) {
1520 encode_b64(numbuf, bigbuf, n, &value);
1521 wrap_concat1(numbuf);
1527 encode_b64(numbuf, padding, 1, &value);
1528 wrap_concat1(numbuf);
1532 encode_b64(numbuf, padding, 1, &value);
1533 wrap_concat1(numbuf);
1543 critlen = MAX_TERMCAP_LENGTH;
1544 legend = "older termcap";
1546 set_obsolete_termcaps(tterm);
1548 critlen = MAX_TERMINFO_LENGTH;
1549 legend = "terminfo";
1553 save_sgr = set_attributes;
1555 if ((FMT_ENTRY() > critlen)
1559 save_tterm = *tterm;
1560 if (!suppress_untranslatable) {
1561 SHOW_WHY("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1563 suppress_untranslatable = TRUE;
1565 if (FMT_ENTRY() > critlen) {
1567 * We pick on sgr because it is a nice long string capability that
1568 * is really just an optimization hack. Another good candidate is
1569 * acsc since it is both long and unused by BSD termcap.
1571 bool changed = FALSE;
1575 * Extended names are most likely function-key definitions. Drop
1579 for (n = STRCOUNT; n < NUM_STRINGS(tterm); n++) {
1580 const char *name = ExtStrname(tterm, (int) n, strnames);
1582 if (VALID_STRING(tterm->Strings[n])) {
1583 set_attributes = ABSENT_STRING;
1584 /* we remove long names anyway - only report the short */
1585 if (strlen(name) <= 2) {
1586 SHOW_WHY("# (%s removed to fit entry within %d bytes)\n",
1591 if (FMT_ENTRY() <= critlen)
1596 if (VALID_STRING(set_attributes)) {
1597 set_attributes = ABSENT_STRING;
1598 SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n",
1602 if (!changed || (FMT_ENTRY() > critlen)) {
1603 if (purged_acs(tterm)) {
1604 acs_chars = ABSENT_STRING;
1605 SHOW_WHY("# (acsc removed to fit entry within %d bytes)\n",
1610 if (!changed || (FMT_ENTRY() > critlen)) {
1611 int oldversion = tversion;
1615 SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1620 && kill_labels(tterm, len - critlen)) {
1621 SHOW_WHY("# (some labels capabilities suppressed to fit entry within %d bytes)\n",
1626 && kill_fkeys(tterm, len - critlen)) {
1627 SHOW_WHY("# (some function-key capabilities suppressed to fit entry within %d bytes)\n",
1631 if (len > critlen) {
1632 (void) fprintf(stderr,
1633 "%s: %s entry is %d bytes long\n",
1635 _nc_first_name(tterm->term_names),
1637 SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
1640 tversion = oldversion;
1642 set_attributes = save_sgr;
1643 *tterm = save_tterm;
1645 } else if (!version_filter(STRING, STR_IDX(acs_chars))) {
1646 save_tterm = *tterm;
1647 if (purged_acs(tterm)) {
1650 *tterm = save_tterm;
1655 dump_uses(const char *value, bool infodump)
1656 /* dump "use=" clauses in the appropriate format */
1658 char buffer[MAX_TERMINFO_LENGTH + EXTRA_CAP];
1659 int limit = (VALID_STRING(value) ? (int) strlen(value) : 0);
1660 const char *cap = infodump ? "use" : "tc";
1665 _nc_warning("empty \"%s\" field", cap);
1667 } else if (limit > MAX_ALIAS) {
1668 _nc_warning("\"%s\" field too long (%d), limit to %d",
1669 cap, limit, MAX_ALIAS);
1672 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1673 "%s=%.*s", cap, limit, value);
1674 wrap_concat1(buffer);
1681 * Trim any remaining whitespace.
1683 if (outbuf.used != 0) {
1684 bool infodump = !TcOutput();
1685 char delim = (char) (infodump ? ',' : ':');
1688 for (j = (int) outbuf.used - 1; j > 0; --j) {
1689 char ch = outbuf.text[j];
1692 } else if (isspace(UChar(ch))) {
1693 outbuf.used = (size_t) j;
1694 } else if (!infodump && ch == '\\') {
1695 outbuf.used = (size_t) j;
1696 } else if (ch == delim && (j == 0 || outbuf.text[j - 1] != '\\')) {
1697 outbuf.used = (size_t) (j + 1);
1702 outbuf.text[outbuf.used] = '\0';
1704 if (outbuf.text != 0) {
1705 (void) fputs(outbuf.text, stdout);
1708 return (int) outbuf.used;
1712 compare_entry(PredHook hook,
1713 TERMTYPE2 *tp GCC_UNUSED,
1715 /* compare two entries */
1718 NCURSES_CONST char *name;
1721 fputs(" comparing booleans.\n", stdout);
1722 for_each_boolean(j, tp) {
1723 i = BoolIndirect(j);
1724 name = ExtBoolname(tp, (int) i, bool_names);
1726 if (isObsolete(outform, name))
1729 (*hook) (CMP_BOOLEAN, i, name);
1733 fputs(" comparing numbers.\n", stdout);
1734 for_each_number(j, tp) {
1736 name = ExtNumname(tp, (int) i, num_names);
1738 if (isObsolete(outform, name))
1741 (*hook) (CMP_NUMBER, i, name);
1745 fputs(" comparing strings.\n", stdout);
1746 for_each_string(j, tp) {
1748 name = ExtStrname(tp, (int) i, str_names);
1750 if (isObsolete(outform, name))
1753 (*hook) (CMP_STRING, i, name);
1756 /* (void) fputs(" comparing use entries.\n", stdout); */
1757 (*hook) (CMP_USE, 0, "use");
1761 #define NOTSET(s) ((s) == 0)
1764 * This bit of legerdemain turns all the terminfo variable names into
1765 * references to locations in the arrays Booleans, Numbers, and Strings ---
1766 * precisely what's needed.
1772 set_obsolete_termcaps(TERMTYPE2 *tp)
1774 #include "capdefaults.c"
1778 * Convert an alternate-character-set string to canonical form: sorted and
1782 repair_acsc(TERMTYPE2 *tp)
1784 if (VALID_STRING(acs_chars)) {
1789 bool fix_needed = FALSE;
1791 for (n = 0, source = 0; acs_chars[n] != 0; n++) {
1792 target = UChar(acs_chars[n]);
1793 if (source >= target) {
1798 if (acs_chars[n + 1])
1806 memset(mapped, 0, sizeof(mapped));
1807 for (n = 0; acs_chars[n] != 0; n++) {
1808 source = UChar(acs_chars[n]);
1809 if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
1810 mapped[source] = (char) target;
1813 extra = (char) source;
1816 for (n = m = 0; n < sizeof(mapped); n++) {
1818 acs_chars[m++] = (char) n;
1819 acs_chars[m++] = mapped[n];
1823 acs_chars[m++] = extra; /* garbage in, garbage out */