1 /****************************************************************************
2 * Copyright 2018-2019,2020 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.c" /* this C file is generated */
41 #include <parametrized.h> /* so is this */
43 MODULE_ID("$Id: dump_entry.c,v 1.181 2020/12/26 18:25:18 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 void failed(const char *) GCC_NORETURN;
113 failed(const char *s)
116 ExitProgram(EXIT_FAILURE);
120 strncpy_DYN(DYNBUF * dst, const char *src, size_t need)
122 size_t want = need + dst->used + 1;
123 if (want > dst->size) {
124 dst->size += (want + 1024); /* be generous */
125 dst->text = typeRealloc(char, dst->size, dst->text);
127 failed("strncpy_DYN");
129 _nc_STRNCPY(dst->text + dst->used, src, need + 1);
131 dst->text[dst->used] = 0;
135 strcpy_DYN(DYNBUF * dst, const char *src)
141 strncpy_DYN(dst, src, strlen(src));
157 _nc_leaks_dump_entry(void)
164 #define NameTrans(check,result) \
165 if ((np->nte_index <= OK_ ## check) \
166 && check[np->nte_index]) \
167 return (result[np->nte_index])
170 nametrans(const char *name)
171 /* translate a capability name to termcap from terminfo */
173 const struct name_table_entry *np;
175 if ((np = _nc_find_entry(name, _nc_get_hash_table(0))) != 0) {
176 switch (np->nte_type) {
178 NameTrans(bool_from_termcap, boolcodes);
182 NameTrans(num_from_termcap, numcodes);
186 NameTrans(str_from_termcap, strcodes);
195 dump_init(const char *version,
205 /* set up for entry display */
210 wrapped = wrap_strings;
212 quickdump = (quick & 3);
214 did_wrap = (width <= 0);
218 tversion = V_ALLCAPS;
219 else if (!strcmp(version, "SVr1") || !strcmp(version, "SVR1")
220 || !strcmp(version, "Ultrix"))
222 else if (!strcmp(version, "HP"))
224 else if (!strcmp(version, "AIX"))
226 else if (!strcmp(version, "BSD"))
229 tversion = V_ALLCAPS;
231 /* implement display modes */
232 switch (outform = mode) {
235 bool_names = boolnames;
236 num_names = numnames;
237 str_names = strnames;
238 separator = (twidth > 0 && theight > 1) ? ", " : ",";
243 bool_names = boolfnames;
244 num_names = numfnames;
245 str_names = strfnames;
246 separator = (twidth > 0 && theight > 1) ? ", " : ",";
252 bool_names = boolcodes;
253 num_names = numcodes;
254 str_names = strcodes;
261 /* implement sort modes */
262 switch (sortmode = sort) {
265 (void) fprintf(stderr,
266 "%s: sorting by term structure order\n", _nc_progname);
271 (void) fprintf(stderr,
272 "%s: sorting by terminfo name order\n", _nc_progname);
273 bool_indirect = bool_terminfo_sort;
274 num_indirect = num_terminfo_sort;
275 str_indirect = str_terminfo_sort;
280 (void) fprintf(stderr,
281 "%s: sorting by C variable order\n", _nc_progname);
282 bool_indirect = bool_variable_sort;
283 num_indirect = num_variable_sort;
284 str_indirect = str_variable_sort;
289 (void) fprintf(stderr,
290 "%s: sorting by termcap name order\n", _nc_progname);
291 bool_indirect = bool_termcap_sort;
292 num_indirect = num_termcap_sort;
293 str_indirect = str_termcap_sort;
298 (void) fprintf(stderr,
299 "%s: width = %d, tversion = %d, outform = %d\n",
300 _nc_progname, width, tversion, outform);
303 static TERMTYPE2 *cur_type;
306 dump_predicate(PredType type, PredIdx idx)
307 /* predicate function to use for ordinary decompilation */
311 return (cur_type->Booleans[idx] == FALSE)
312 ? FAIL : cur_type->Booleans[idx];
315 return (cur_type->Numbers[idx] == ABSENT_NUMERIC)
316 ? FAIL : cur_type->Numbers[idx];
319 return (cur_type->Strings[idx] != ABSENT_STRING)
323 return (FALSE); /* pacify compiler */
326 static void set_obsolete_termcaps(TERMTYPE2 *tp);
328 /* is this the index of a function key string? */
330 (((i) >= STR_IDX(key_f0) && \
331 (i) <= STR_IDX(key_f9)) || \
332 ((i) >= STR_IDX(key_f11) && \
333 (i) <= STR_IDX(key_f63)))
336 * If we configure with a different Caps file, the offsets into the arrays
337 * will change. So we use an address expression.
339 #define BOOL_IDX(name) (PredType) (&(name) - &(CUR Booleans[0]))
340 #define NUM_IDX(name) (PredType) (&(name) - &(CUR Numbers[0]))
341 #define STR_IDX(name) (PredType) (&(name) - &(CUR Strings[0]))
344 version_filter(PredType type, PredIdx idx)
345 /* filter out capabilities we may want to suppress */
348 case V_ALLCAPS: /* SVr4, XSI Curses */
351 case V_SVR1: /* System V Release 1, Ultrix */
354 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
356 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
358 return ((idx <= STR_IDX(prtr_non)) ? TRUE : FALSE);
362 case V_HPUX: /* Hewlett-Packard */
365 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
367 return ((idx <= NUM_IDX(label_width)) ? TRUE : FALSE);
369 if (idx <= STR_IDX(prtr_non))
371 else if (FNKEY(idx)) /* function keys */
373 else if (idx == STR_IDX(plab_norm)
374 || idx == STR_IDX(label_on)
375 || idx == STR_IDX(label_off))
382 case V_AIX: /* AIX */
385 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
387 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
389 if (idx <= STR_IDX(prtr_non))
391 else if (FNKEY(idx)) /* function keys */
398 #define is_termcap(type) (OkIndex(idx, type##_from_termcap) && \
399 type##_from_termcap[idx])
401 case V_BSD: /* BSD */
404 return is_termcap(bool);
406 return is_termcap(num);
408 return is_termcap(str);
413 return (FALSE); /* pacify the compiler */
419 while (outbuf.used > 0 && outbuf.text[outbuf.used - 1] == ' ')
420 outbuf.text[--outbuf.used] = '\0';
428 strcpy_DYN(&outbuf, trailer);
433 op_length(const char *src, int offset)
437 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 */
484 for (n = size - 1; n > 0; --n) {
485 int ch = UChar(src[step + n]);
487 if (n > 0 && src[step + n - 1] == ch)
491 } else if (!isalnum(ch)) {
498 /* check if that would split a backslash-sequence */
499 for (n = size - 1; n > 0; --n) {
500 int ch = UChar(src[step + n]);
502 int need = op_length(src, step + n);
503 if ((n + need) > size) {
518 * If we are going to wrap lines, we cannot leave literal spaces because that
519 * would be ambiguous if we split on that space.
522 fill_spaces(const char *src)
524 const char *fill = "\\s";
525 size_t need = strlen(src);
526 size_t size = strlen(fill);
530 for (pass = 0; pass < 2; ++pass) {
531 for (s = d = 0; src[s] != '\0'; ++s) {
534 _nc_STRCPY(&result[d], fill, need + 1 - d);
541 result[d++] = src[s];
550 result = malloc(need + 1);
552 failed("fill_spaces");
566 #define wrap_1ST(mode) ((mode)&w1ST)
567 #define wrap_END(mode) ((mode)&wEND)
568 #define wrap_ERR(mode) ((mode)&wERR)
571 wrap_concat(const char *src, int need, unsigned mode)
573 int gaps = (int) strlen(separator);
574 int want = gaps + need;
576 did_wrap = (width <= 0);
579 && column + want > width) {
582 if ((wrap_END(mode) && !wrap_ERR(mode)) &&
585 (column + want) > width) {
587 int used = width > WRAPPED ? width : WRAPPED;
591 const char *my_t = trailer;
592 char *fill = fill_spaces(src);
593 int last = (int) strlen(fill);
600 if (!TcOutput() && (p = strchr(fill, '=')) != 0) {
601 base = (int) (p + 1 - fill);
604 _nc_SPRINTF(align, _nc_SLIMIT(align) "%*s", base, " ");
605 } else if (column > 8) {
609 _nc_SPRINTF(align, _nc_SLIMIT(align) "%*s", base, " ");
613 /* "pretty" overrides wrapping if it already split the line */
614 if (!pretty || strchr(fill, '\n') == 0) {
617 if (TcOutput() && outbuf.used && !wrap_1ST(mode)) {
621 while ((column + (need + gaps)) > used) {
624 strcpy_DYN(&outbuf, align);
627 if (size > (last - step)) {
628 size = (last - step);
630 size = find_split(fill, step, size);
631 strncpy_DYN(&outbuf, fill + step, (size_t) size);
643 strcpy_DYN(&outbuf, align);
644 strcpy_DYN(&outbuf, fill + step);
647 strcpy_DYN(&outbuf, separator);
653 strcpy_DYN(&outbuf, src);
655 strcpy_DYN(&outbuf, separator);
656 column += (int) strlen(src);
661 wrap_concat1(const char *src)
663 int need = (int) strlen(src);
664 wrap_concat(src, need, w1ST | wEND);
668 wrap_concat3(const char *name, const char *eqls, const char *value)
670 int nlen = (int) strlen(name);
671 int elen = (int) strlen(eqls);
672 int vlen = (int) strlen(value);
674 wrap_concat(name, nlen + elen + vlen, w1ST);
675 wrap_concat(eqls, elen + vlen, w2ND);
676 wrap_concat(value, vlen, wEND);
679 #define IGNORE_SEP_TRAIL(first,last,sep_trail) \
680 if ((size_t)(last - first) > sizeof(sep_trail)-1 \
681 && !strncmp(first, sep_trail, sizeof(sep_trail)-1)) \
682 first += sizeof(sep_trail)-2
684 /* Returns the nominal length of the buffer assuming it is termcap format,
685 * i.e., the continuation sequence is treated as a single character ":".
687 * There are several implementations of termcap which read the text into a
688 * fixed-size buffer. Generally they strip the newlines from the text, but may
689 * not do it until after the buffer is read. Also, "tc=" resolution may be
690 * expanded in the same buffer. This function is useful for measuring the size
691 * of the best fixed-buffer implementation; the worst case may be much worse.
693 #ifdef TEST_TERMCAP_LENGTH
695 termcap_length(const char *src)
697 static const char pattern[] = ":\\\n\t:";
700 const char *const t = src + strlen(src);
702 while (*src != '\0') {
703 IGNORE_SEP_TRAIL(src, t, pattern);
710 #define termcap_length(src) strlen(src)
714 indent_DYN(DYNBUF * buffer, int level)
718 for (n = 0; n < level; n++)
719 strncpy_DYN(buffer, "\t", (size_t) 1);
723 * Check if the current line which was begun consists only of a tab and the
724 * given leading text.
727 leading_DYN(DYNBUF * buffer, const char *leading)
730 size_t need = strlen(leading);
731 if (buffer->used > need) {
732 need = buffer->used - need;
733 if (!strcmp(buffer->text + need, leading)) {
735 while (--need != 0) {
736 if (buffer->text[need] == '\n') {
739 if (buffer->text[need] != '\t') {
750 has_params(const char *src, bool formatting)
753 int len = (int) strlen(src);
758 for (n = 0; n < len - 1; ++n) {
759 if (!strncmp(src + n, "%p", (size_t) 2)) {
761 } else if (!strncmp(src + n, "%;", (size_t) 2)) {
769 result = ((len > 50) && params);
778 fmt_complex(TERMTYPE2 *tterm, const char *capability, char *src, int level)
780 bool percent = FALSE;
781 bool params = has_params(src, TRUE);
783 while (*src != '\0') {
787 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
791 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
797 case 't': /* "then" */
798 case 'e': /* "else" */
801 tmpbuf.text[tmpbuf.used - 1] = '\n';
802 /* treat a "%e" as else-if, on the same level */
804 indent_DYN(&tmpbuf, level);
805 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
806 strncpy_DYN(&tmpbuf, src, (size_t) 1);
808 params = has_params(src, TRUE);
809 if (!params && *src != '\0' && *src != '%') {
810 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
811 indent_DYN(&tmpbuf, level + 1);
814 indent_DYN(&tmpbuf, level + 1);
815 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
816 strncpy_DYN(&tmpbuf, src, (size_t) 1);
818 src = fmt_complex(tterm, capability, src, level + 1);
819 if (*src != '\0' && *src != '%') {
820 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
821 indent_DYN(&tmpbuf, level + 1);
823 } else if (level == 1) {
825 _nc_warning("%s: %%%c without %%? in %s",
826 _nc_first_name(tterm->term_names),
833 case ';': /* "endif" */
837 tmpbuf.text[tmpbuf.used - 1] = '\n';
838 indent_DYN(&tmpbuf, level);
839 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
840 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
843 && (strchr("?e;", src[1])) == 0) {
844 tmpbuf.text[tmpbuf.used++] = '\n';
845 indent_DYN(&tmpbuf, level);
850 _nc_warning("%s: %%; without %%? in %s",
851 _nc_first_name(tterm->term_names),
856 if (percent && params && !leading_DYN(&tmpbuf, "%")) {
857 tmpbuf.text[tmpbuf.used - 1] = '\n';
858 indent_DYN(&tmpbuf, level + 1);
859 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";
885 if ((outform != F_TERMCAP) && (value > 255)) {
886 unsigned long lv = (unsigned long) value;
888 int bits = sizeof(unsigned long) * 8;
890 for (nn = 8; nn < bits; ++nn) {
892 if ((mm - 16) <= lv && (mm + 16) > lv) {
901 #define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap)
905 fmt_entry(TERMTYPE2 *tterm,
908 int suppress_untranslatable,
913 char buffer[MAX_TERMINFO_LENGTH + EXTRA_CAP];
915 NCURSES_CONST char *name;
917 PredIdx num_bools = 0;
918 PredIdx num_values = 0;
919 PredIdx num_strings = 0;
922 #define WRAP_CONCAT1(s) wrap_concat1(s); outcount = TRUE
923 #define WRAP_CONCAT WRAP_CONCAT1(buffer)
925 len = 12; /* terminfo file-header */
929 pred = dump_predicate;
932 strcpy_DYN(&outbuf, 0);
934 column = indent; /* FIXME: workaround to prevent empty lines */
936 strcpy_DYN(&outbuf, tterm->term_names);
939 * Colon is legal in terminfo descriptions, but not in termcap.
942 char *p = outbuf.text;
950 strcpy_DYN(&outbuf, separator);
951 column = (int) outbuf.used;
956 for_each_boolean(j, tterm) {
958 name = ExtBoolname(tterm, (int) i, bool_names);
959 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
961 if (!version_filter(BOOLEAN, i))
963 else if (isObsolete(outform, name))
966 predval = pred(BOOLEAN, i);
967 if (predval != FAIL) {
968 _nc_STRCPY(buffer, name, sizeof(buffer));
970 _nc_STRCAT(buffer, "@", sizeof(buffer));
971 else if (i + 1 > num_bools)
977 if (column != indent && height > 1)
980 for_each_number(j, tterm) {
982 name = ExtNumname(tterm, (int) i, num_names);
983 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
985 if (!version_filter(NUMBER, i))
987 else if (isObsolete(outform, name))
990 predval = pred(NUMBER, i);
991 if (predval != FAIL) {
992 if (tterm->Numbers[i] < 0) {
993 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
997 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1000 _nc_SPRINTF(buffer + nn, _nc_SLIMIT(sizeof(buffer) - nn)
1001 number_format(tterm->Numbers[i]),
1003 if (i + 1 > num_values)
1010 if (column != indent && height > 1)
1013 len += (int) (num_bools
1015 + strlen(tterm->term_names) + 1);
1021 if (outform == F_TERMCAP) {
1022 if (VALID_STRING(termcap_reset)) {
1023 if (VALID_STRING(init_3string)
1024 && !strcmp(init_3string, termcap_reset))
1025 DISCARD(init_3string);
1027 if (VALID_STRING(reset_2string)
1028 && !strcmp(reset_2string, termcap_reset))
1029 DISCARD(reset_2string);
1033 for_each_string(j, tterm) {
1035 name = ExtStrname(tterm, (int) i, str_names);
1036 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
1038 capability = tterm->Strings[i];
1040 if (!version_filter(STRING, i))
1042 else if (isObsolete(outform, name))
1047 * Extended names can be longer than 2 characters, but termcap programs
1048 * cannot read those (filter them out).
1050 if (outform == F_TERMCAP && (strlen(name) > 2))
1054 if (outform == F_TERMCAP) {
1056 * Some older versions of vi want rmir/smir to be defined
1057 * for ich/ich1 to work. If they're not defined, force
1058 * them to be output as defined and empty.
1060 if (PRESENT(insert_character) || PRESENT(parm_ich)) {
1061 if (SAME_CAP(i, enter_insert_mode)
1062 && enter_insert_mode == ABSENT_STRING) {
1063 _nc_STRCPY(buffer, "im=", sizeof(buffer));
1068 if (SAME_CAP(i, exit_insert_mode)
1069 && exit_insert_mode == ABSENT_STRING) {
1070 _nc_STRCPY(buffer, "ei=", sizeof(buffer));
1076 * termcap applications such as screen will be confused if sgr0
1077 * is translated to a string containing rmacs. Filter that out.
1079 if (PRESENT(exit_attribute_mode)) {
1080 if (SAME_CAP(i, exit_attribute_mode)) {
1082 char *my_sgr = set_attributes;
1084 set_attributes = save_sgr;
1086 trimmed_sgr0 = _nc_trim_sgr0(tterm);
1087 if (strcmp(capability, trimmed_sgr0)) {
1088 capability = trimmed_sgr0;
1090 if (trimmed_sgr0 != exit_attribute_mode)
1094 set_attributes = my_sgr;
1099 predval = pred(STRING, i);
1102 if (predval != FAIL) {
1103 if (VALID_STRING(capability)
1104 && i + 1 > num_strings)
1105 num_strings = i + 1;
1107 if (!VALID_STRING(capability)) {
1108 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1111 } else if (TcOutput()) {
1112 char *srccap = _nc_tic_expand(capability, TRUE, numbers);
1113 int params = ((i < (int) SIZEOF(parametrized))
1117 : has_params(srccap, FALSE)));
1118 char *cv = _nc_infotocap(name, srccap, params);
1121 if (outform == F_TCONVERR) {
1122 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1123 "%s=!!! %s WILL NOT CONVERT !!!",
1126 } else if (suppress_untranslatable) {
1129 char *s = srccap, *d = buffer;
1130 int need = 3 + (int) strlen(name);
1131 while ((*d = *s++) != 0) {
1132 if ((d - buffer + 2) >= (int) sizeof(buffer)) {
1134 "%s: value for %s is too long\n",
1143 } else if (*d == '\\') {
1144 if ((*++d = *s++) == '\0')
1150 need += (int) (d - buffer);
1151 wrap_concat("..", need, w1ST | wERR);
1153 wrap_concat(name, need, wOFF | wERR);
1154 need -= (int) strlen(name);
1155 wrap_concat("=", need, w2ND | wERR);
1157 wrap_concat(buffer, need, wEND | wERR);
1161 wrap_concat3(name, "=", cv);
1163 len += (int) strlen(capability) + 1;
1165 char *src = _nc_tic_expand(capability,
1166 outform == F_TERMINFO, numbers);
1168 strcpy_DYN(&tmpbuf, 0);
1169 strcpy_DYN(&tmpbuf, name);
1170 strcpy_DYN(&tmpbuf, "=");
1172 && (outform == F_TERMINFO
1173 || outform == F_VARIABLE)) {
1174 fmt_complex(tterm, name, src, 1);
1176 strcpy_DYN(&tmpbuf, src);
1178 len += (int) strlen(capability) + 1;
1179 WRAP_CONCAT1(tmpbuf.text);
1182 /* e.g., trimmed_sgr0 */
1183 if (VALID_STRING(capability) &&
1184 capability != tterm->Strings[i])
1187 len += (int) (num_strings * 2);
1190 * This piece of code should be an effective inverse of the functions
1191 * postprocess_terminfo() and postprocess_terminfo() in parse_entry.c.
1192 * Much more work should be done on this to support dumping termcaps.
1194 if (tversion == V_HPUX) {
1195 if (VALID_STRING(memory_lock)) {
1196 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1197 "meml=%s", memory_lock);
1200 if (VALID_STRING(memory_unlock)) {
1201 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1202 "memu=%s", memory_unlock);
1205 } else if (tversion == V_AIX) {
1206 if (VALID_STRING(acs_chars)) {
1208 const char *acstrans = "lqkxjmwuvtn";
1210 char *tp, *sp, boxchars[11];
1213 for (cp = acstrans; *cp; cp++) {
1214 sp = (strchr) (acs_chars, *cp);
1225 char *tmp = _nc_tic_expand(boxchars,
1226 (outform == F_TERMINFO),
1228 _nc_STRCPY(buffer, "box1=", sizeof(buffer));
1229 while (*tmp != '\0') {
1230 size_t have = strlen(buffer);
1231 size_t next = strlen(tmp);
1232 size_t want = have + next + 1;
1237 * If the expanded string is too long for the buffer,
1238 * chop it off and save the location where we chopped it.
1240 if (want >= sizeof(buffer)) {
1244 _nc_STRCAT(buffer, tmp, sizeof(buffer));
1247 * If we chopped the buffer, replace the missing piece and
1248 * shift everything to append the remainder.
1253 while ((tmp[next] = tmp[last + next]) != '\0') {
1266 * kludge: trim off trailer to avoid an extra blank line
1267 * in infocmp -u output when there are no string differences
1270 bool trimmed = FALSE;
1271 j = (PredIdx) outbuf.used;
1272 if (wrapped && did_wrap) {
1275 && outbuf.text[j - 1] == '\t'
1276 && outbuf.text[j - 2] == '\n') {
1280 && outbuf.text[j - 1] == ':'
1281 && outbuf.text[j - 2] == '\t'
1282 && outbuf.text[j - 3] == '\n'
1283 && outbuf.text[j - 4] == '\\') {
1288 outbuf.text[outbuf.used] = '\0';
1290 strcpy_DYN(&outbuf, " ");
1294 fprintf(stderr, "num_bools = %d\n", num_bools);
1295 fprintf(stderr, "num_values = %d\n", num_values);
1296 fprintf(stderr, "num_strings = %d\n", num_strings);
1297 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
1298 tterm->term_names, len, outbuf.used, outbuf.text);
1301 * Here's where we use infodump to trigger a more stringent length check
1302 * for termcap-translation purposes.
1303 * Return the length of the raw entry, without tc= expansions,
1304 * It gives an idea of which entries are deadly to even *scan past*,
1305 * as opposed to *use*.
1307 return (infodump ? len : (int) termcap_length(outbuf.text));
1311 kill_string(TERMTYPE2 *tterm, char *cap)
1314 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1315 if (cap == tterm->Strings[n]) {
1316 tterm->Strings[n] = ABSENT_STRING;
1324 find_string(TERMTYPE2 *tterm, char *name)
1327 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1328 if (version_filter(STRING, n)
1329 && !strcmp(name, strnames[n])) {
1330 char *cap = tterm->Strings[n];
1331 if (VALID_STRING(cap)) {
1337 return ABSENT_STRING;
1341 * This is used to remove function-key labels from a termcap entry to
1345 kill_labels(TERMTYPE2 *tterm, int target)
1352 for (n = 0; n <= 10; ++n) {
1353 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "lf%d", n);
1354 cap = find_string(tterm, name);
1355 if (VALID_STRING(cap)
1356 && kill_string(tterm, cap)) {
1357 target -= (int) (strlen(cap) + 5);
1367 * This is used to remove function-key definitions from a termcap entry to
1371 kill_fkeys(TERMTYPE2 *tterm, int target)
1378 for (n = 60; n >= 0; --n) {
1379 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n);
1380 cap = find_string(tterm, name);
1381 if (VALID_STRING(cap)
1382 && kill_string(tterm, cap)) {
1383 target -= (int) (strlen(cap) + 5);
1393 * Check if the given acsc string is a 1-1 mapping, i.e., just-like-vt100.
1394 * Also, since this is for termcap, we only care about the line-drawing map.
1396 #define isLine(c) (strchr("lmkjtuvwqxn", c) != 0)
1399 one_one_mapping(const char *mapping)
1403 if (VALID_STRING(mapping)) {
1405 while (mapping[n] != '\0' && mapping[n + 1] != '\0') {
1406 if (isLine(mapping[n]) &&
1407 mapping[n] != mapping[n + 1]) {
1417 #define FMT_ENTRY() \
1418 fmt_entry(tterm, pred, \
1420 suppress_untranslatable, \
1423 #define SHOW_WHY PRINTF
1426 purged_acs(TERMTYPE2 *tterm)
1428 bool result = FALSE;
1430 if (VALID_STRING(acs_chars)) {
1431 if (!one_one_mapping(acs_chars)) {
1432 enter_alt_charset_mode = ABSENT_STRING;
1433 exit_alt_charset_mode = ABSENT_STRING;
1434 SHOW_WHY("# (rmacs/smacs removed for consistency)\n");
1442 encode_b64(char *target, char *source, unsigned state, int *saved)
1445 static const char data[] =
1446 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1447 "abcdefghijklmnopqrstuvwxyz"
1449 int ch = UChar(source[state]);
1451 switch (state % 3) {
1453 *target++ = data[(ch >> 2) & 077];
1457 *target++ = data[((ch >> 4) | *saved) & 077];
1461 *target++ = data[((ch >> 6) | *saved) & 077];
1462 *target++ = data[ch & 077];
1470 * Dump a single entry.
1473 dump_entry(TERMTYPE2 *tterm,
1474 int suppress_untranslatable,
1479 TERMTYPE2 save_tterm;
1487 unsigned offset = 0;
1491 if (_nc_write_object(tterm, bigbuf, &offset, sizeof(bigbuf)) == OK) {
1493 if (quickdump & 1) {
1496 wrap_concat1("hex:");
1497 for (n = 0; n < offset; ++n) {
1498 _nc_SPRINTF(numbuf, _nc_SLIMIT(sizeof(numbuf))
1499 "%02X", UChar(bigbuf[n]));
1500 wrap_concat1(numbuf);
1503 if (quickdump & 2) {
1504 static char padding[] =
1509 wrap_concat1("b64:");
1510 for (n = 0; n < offset; ++n) {
1511 encode_b64(numbuf, bigbuf, n, &value);
1512 wrap_concat1(numbuf);
1518 encode_b64(numbuf, padding, 1, &value);
1519 wrap_concat1(numbuf);
1523 encode_b64(numbuf, padding, 1, &value);
1524 wrap_concat1(numbuf);
1534 critlen = MAX_TERMCAP_LENGTH;
1535 legend = "older termcap";
1537 set_obsolete_termcaps(tterm);
1539 critlen = MAX_TERMINFO_LENGTH;
1540 legend = "terminfo";
1544 save_sgr = set_attributes;
1546 if ((FMT_ENTRY() > critlen)
1549 save_tterm = *tterm;
1550 if (!suppress_untranslatable) {
1551 SHOW_WHY("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1553 suppress_untranslatable = TRUE;
1555 if (FMT_ENTRY() > critlen) {
1557 * We pick on sgr because it's a nice long string capability that
1558 * is really just an optimization hack. Another good candidate is
1559 * acsc since it is both long and unused by BSD termcap.
1561 bool changed = FALSE;
1565 * Extended names are most likely function-key definitions. Drop
1569 for (n = STRCOUNT; n < NUM_STRINGS(tterm); n++) {
1570 const char *name = ExtStrname(tterm, (int) n, strnames);
1572 if (VALID_STRING(tterm->Strings[n])) {
1573 set_attributes = ABSENT_STRING;
1574 /* we remove long names anyway - only report the short */
1575 if (strlen(name) <= 2) {
1576 SHOW_WHY("# (%s removed to fit entry within %d bytes)\n",
1581 if (FMT_ENTRY() <= critlen)
1586 if (VALID_STRING(set_attributes)) {
1587 set_attributes = ABSENT_STRING;
1588 SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n",
1592 if (!changed || (FMT_ENTRY() > critlen)) {
1593 if (purged_acs(tterm)) {
1594 acs_chars = ABSENT_STRING;
1595 SHOW_WHY("# (acsc removed to fit entry within %d bytes)\n",
1600 if (!changed || (FMT_ENTRY() > critlen)) {
1601 int oldversion = tversion;
1604 SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1609 && kill_labels(tterm, len - critlen)) {
1610 SHOW_WHY("# (some labels capabilities suppressed to fit entry within %d bytes)\n",
1615 && kill_fkeys(tterm, len - critlen)) {
1616 SHOW_WHY("# (some function-key capabilities suppressed to fit entry within %d bytes)\n",
1620 if (len > critlen) {
1621 (void) fprintf(stderr,
1622 "%s: %s entry is %d bytes long\n",
1624 _nc_first_name(tterm->term_names),
1626 SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
1629 tversion = oldversion;
1631 set_attributes = save_sgr;
1632 *tterm = save_tterm;
1634 } else if (!version_filter(STRING, STR_IDX(acs_chars))) {
1635 save_tterm = *tterm;
1636 if (purged_acs(tterm)) {
1639 *tterm = save_tterm;
1644 dump_uses(const char *name, bool infodump)
1645 /* dump "use=" clauses in the appropriate format */
1647 char buffer[MAX_TERMINFO_LENGTH];
1651 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1652 "%s%s", infodump ? "use=" : "tc=", name);
1653 wrap_concat1(buffer);
1660 * Trim any remaining whitespace.
1662 if (outbuf.used != 0) {
1663 bool infodump = !TcOutput();
1664 char delim = (char) (infodump ? ',' : ':');
1667 for (j = (int) outbuf.used - 1; j > 0; --j) {
1668 char ch = outbuf.text[j];
1671 } else if (isspace(UChar(ch))) {
1672 outbuf.used = (size_t) j;
1673 } else if (!infodump && ch == '\\') {
1674 outbuf.used = (size_t) j;
1675 } else if (ch == delim && (j == 0 || outbuf.text[j - 1] != '\\')) {
1676 outbuf.used = (size_t) (j + 1);
1681 outbuf.text[outbuf.used] = '\0';
1683 if (outbuf.text != 0) {
1684 (void) fputs(outbuf.text, stdout);
1687 return (int) outbuf.used;
1691 compare_entry(PredHook hook,
1692 TERMTYPE2 *tp GCC_UNUSED,
1694 /* compare two entries */
1697 NCURSES_CONST char *name;
1700 fputs(" comparing booleans.\n", stdout);
1701 for_each_boolean(j, tp) {
1702 i = BoolIndirect(j);
1703 name = ExtBoolname(tp, (int) i, bool_names);
1705 if (isObsolete(outform, name))
1708 (*hook) (CMP_BOOLEAN, i, name);
1712 fputs(" comparing numbers.\n", stdout);
1713 for_each_number(j, tp) {
1715 name = ExtNumname(tp, (int) i, num_names);
1717 if (isObsolete(outform, name))
1720 (*hook) (CMP_NUMBER, i, name);
1724 fputs(" comparing strings.\n", stdout);
1725 for_each_string(j, tp) {
1727 name = ExtStrname(tp, (int) i, str_names);
1729 if (isObsolete(outform, name))
1732 (*hook) (CMP_STRING, i, name);
1735 /* (void) fputs(" comparing use entries.\n", stdout); */
1736 (*hook) (CMP_USE, 0, "use");
1740 #define NOTSET(s) ((s) == 0)
1743 * This bit of legerdemain turns all the terminfo variable names into
1744 * references to locations in the arrays Booleans, Numbers, and Strings ---
1745 * precisely what's needed.
1751 set_obsolete_termcaps(TERMTYPE2 *tp)
1753 #include "capdefaults.c"
1757 * Convert an alternate-character-set string to canonical form: sorted and
1761 repair_acsc(TERMTYPE2 *tp)
1763 if (VALID_STRING(acs_chars)) {
1769 bool fix_needed = FALSE;
1771 for (n = 0, source = 0; acs_chars[n] != 0; n++) {
1772 target = UChar(acs_chars[n]);
1773 if (source >= target) {
1778 if (acs_chars[n + 1])
1782 memset(mapped, 0, sizeof(mapped));
1783 for (n = 0; acs_chars[n] != 0; n++) {
1784 source = UChar(acs_chars[n]);
1785 if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
1786 mapped[source] = (char) target;
1789 extra = (char) source;
1792 for (n = m = 0; n < sizeof(mapped); n++) {
1794 acs_chars[m++] = (char) n;
1795 acs_chars[m++] = mapped[n];
1799 acs_chars[m++] = extra; /* garbage in, garbage out */