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.176 2020/02/02 23:34:34 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)
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)) {
768 result = ((len > 50) && params);
774 fmt_complex(TERMTYPE2 *tterm, const char *capability, char *src, int level)
776 bool percent = FALSE;
777 bool params = has_params(src);
779 while (*src != '\0') {
783 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
787 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
793 case 't': /* "then" */
794 case 'e': /* "else" */
797 tmpbuf.text[tmpbuf.used - 1] = '\n';
798 /* treat a "%e" as else-if, on the same level */
800 indent_DYN(&tmpbuf, level);
801 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
802 strncpy_DYN(&tmpbuf, src, (size_t) 1);
804 params = has_params(src);
805 if (!params && *src != '\0' && *src != '%') {
806 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
807 indent_DYN(&tmpbuf, level + 1);
810 indent_DYN(&tmpbuf, level + 1);
811 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
812 strncpy_DYN(&tmpbuf, src, (size_t) 1);
814 src = fmt_complex(tterm, capability, src, level + 1);
815 if (*src != '\0' && *src != '%') {
816 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
817 indent_DYN(&tmpbuf, level + 1);
819 } else if (level == 1) {
821 _nc_warning("%s: %%%c without %%? in %s",
822 _nc_first_name(tterm->term_names),
829 case ';': /* "endif" */
833 tmpbuf.text[tmpbuf.used - 1] = '\n';
834 indent_DYN(&tmpbuf, level);
835 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
836 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
839 && (strchr("?e;", src[1])) == 0) {
840 tmpbuf.text[tmpbuf.used++] = '\n';
841 indent_DYN(&tmpbuf, level);
846 _nc_warning("%s: %%; without %%? in %s",
847 _nc_first_name(tterm->term_names),
852 if (percent && params && !leading_DYN(&tmpbuf, "%")) {
853 tmpbuf.text[tmpbuf.used - 1] = '\n';
854 indent_DYN(&tmpbuf, level + 1);
855 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
861 strncpy_DYN(&tmpbuf, "\\s", (size_t) 2);
868 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
874 * Make "large" numbers a little easier to read by showing them in hexadecimal
875 * if they are "close" to a power of two.
878 number_format(int value)
880 const char *result = "%d";
881 if ((outform != F_TERMCAP) && (value > 255)) {
882 unsigned long lv = (unsigned long) value;
884 int bits = sizeof(unsigned long) * 8;
886 for (nn = 8; nn < bits; ++nn) {
888 if ((mm - 16) <= lv && (mm + 16) > lv) {
897 #define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap)
901 fmt_entry(TERMTYPE2 *tterm,
904 int suppress_untranslatable,
909 char buffer[MAX_TERMINFO_LENGTH + EXTRA_CAP];
911 NCURSES_CONST char *name;
913 PredIdx num_bools = 0;
914 PredIdx num_values = 0;
915 PredIdx num_strings = 0;
918 #define WRAP_CONCAT1(s) wrap_concat1(s); outcount = TRUE
919 #define WRAP_CONCAT WRAP_CONCAT1(buffer)
921 len = 12; /* terminfo file-header */
925 pred = dump_predicate;
928 strcpy_DYN(&outbuf, 0);
930 column = indent; /* FIXME: workaround to prevent empty lines */
932 strcpy_DYN(&outbuf, tterm->term_names);
935 * Colon is legal in terminfo descriptions, but not in termcap.
938 char *p = outbuf.text;
946 strcpy_DYN(&outbuf, separator);
947 column = (int) outbuf.used;
952 for_each_boolean(j, tterm) {
954 name = ExtBoolname(tterm, (int) i, bool_names);
955 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
957 if (!version_filter(BOOLEAN, i))
959 else if (isObsolete(outform, name))
962 predval = pred(BOOLEAN, i);
963 if (predval != FAIL) {
964 _nc_STRCPY(buffer, name, sizeof(buffer));
966 _nc_STRCAT(buffer, "@", sizeof(buffer));
967 else if (i + 1 > num_bools)
973 if (column != indent && height > 1)
976 for_each_number(j, tterm) {
978 name = ExtNumname(tterm, (int) i, num_names);
979 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
981 if (!version_filter(NUMBER, i))
983 else if (isObsolete(outform, name))
986 predval = pred(NUMBER, i);
987 if (predval != FAIL) {
988 if (tterm->Numbers[i] < 0) {
989 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
993 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
996 _nc_SPRINTF(buffer + nn, _nc_SLIMIT(sizeof(buffer) - nn)
997 number_format(tterm->Numbers[i]),
999 if (i + 1 > num_values)
1006 if (column != indent && height > 1)
1009 len += (int) (num_bools
1011 + strlen(tterm->term_names) + 1);
1017 if (outform == F_TERMCAP) {
1018 if (VALID_STRING(termcap_reset)) {
1019 if (VALID_STRING(init_3string)
1020 && !strcmp(init_3string, termcap_reset))
1021 DISCARD(init_3string);
1023 if (VALID_STRING(reset_2string)
1024 && !strcmp(reset_2string, termcap_reset))
1025 DISCARD(reset_2string);
1029 for_each_string(j, tterm) {
1031 name = ExtStrname(tterm, (int) i, str_names);
1032 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
1034 capability = tterm->Strings[i];
1036 if (!version_filter(STRING, i))
1038 else if (isObsolete(outform, name))
1043 * Extended names can be longer than 2 characters, but termcap programs
1044 * cannot read those (filter them out).
1046 if (outform == F_TERMCAP && (strlen(name) > 2))
1050 if (outform == F_TERMCAP) {
1052 * Some older versions of vi want rmir/smir to be defined
1053 * for ich/ich1 to work. If they're not defined, force
1054 * them to be output as defined and empty.
1056 if (PRESENT(insert_character) || PRESENT(parm_ich)) {
1057 if (SAME_CAP(i, enter_insert_mode)
1058 && enter_insert_mode == ABSENT_STRING) {
1059 _nc_STRCPY(buffer, "im=", sizeof(buffer));
1064 if (SAME_CAP(i, exit_insert_mode)
1065 && exit_insert_mode == ABSENT_STRING) {
1066 _nc_STRCPY(buffer, "ei=", sizeof(buffer));
1072 * termcap applications such as screen will be confused if sgr0
1073 * is translated to a string containing rmacs. Filter that out.
1075 if (PRESENT(exit_attribute_mode)) {
1076 if (SAME_CAP(i, exit_attribute_mode)) {
1078 char *my_sgr = set_attributes;
1080 set_attributes = save_sgr;
1082 trimmed_sgr0 = _nc_trim_sgr0(tterm);
1083 if (strcmp(capability, trimmed_sgr0)) {
1084 capability = trimmed_sgr0;
1086 if (trimmed_sgr0 != exit_attribute_mode)
1090 set_attributes = my_sgr;
1095 predval = pred(STRING, i);
1098 if (predval != FAIL) {
1099 if (VALID_STRING(capability)
1100 && i + 1 > num_strings)
1101 num_strings = i + 1;
1103 if (!VALID_STRING(capability)) {
1104 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1107 } else if (TcOutput()) {
1108 char *srccap = _nc_tic_expand(capability, TRUE, numbers);
1109 int params = ((i < (int) SIZEOF(parametrized))
1113 : has_params(srccap)));
1114 char *cv = _nc_infotocap(name, srccap, params);
1117 if (outform == F_TCONVERR) {
1118 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1119 "%s=!!! %s WILL NOT CONVERT !!!",
1122 } else if (suppress_untranslatable) {
1125 char *s = srccap, *d = buffer;
1126 int need = 3 + (int) strlen(name);
1127 while ((*d = *s++) != 0) {
1128 if ((d - buffer + 1) >= (int) sizeof(buffer)) {
1130 "%s: value for %s is too long\n",
1139 } else if (*d == '\\') {
1140 if ((*++d = *s++) == '\0')
1146 need += (int) (d - buffer);
1147 wrap_concat("..", need, w1ST | wERR);
1149 wrap_concat(name, need, wOFF | wERR);
1150 need -= (int) strlen(name);
1151 wrap_concat("=", need, w2ND | wERR);
1153 wrap_concat(buffer, need, wEND | wERR);
1157 wrap_concat3(name, "=", cv);
1159 len += (int) strlen(capability) + 1;
1161 char *src = _nc_tic_expand(capability,
1162 outform == F_TERMINFO, numbers);
1164 strcpy_DYN(&tmpbuf, 0);
1165 strcpy_DYN(&tmpbuf, name);
1166 strcpy_DYN(&tmpbuf, "=");
1168 && (outform == F_TERMINFO
1169 || outform == F_VARIABLE)) {
1170 fmt_complex(tterm, name, src, 1);
1172 strcpy_DYN(&tmpbuf, src);
1174 len += (int) strlen(capability) + 1;
1175 WRAP_CONCAT1(tmpbuf.text);
1178 /* e.g., trimmed_sgr0 */
1179 if (VALID_STRING(capability) &&
1180 capability != tterm->Strings[i])
1183 len += (int) (num_strings * 2);
1186 * This piece of code should be an effective inverse of the functions
1187 * postprocess_terminfo() and postprocess_terminfo() in parse_entry.c.
1188 * Much more work should be done on this to support dumping termcaps.
1190 if (tversion == V_HPUX) {
1191 if (VALID_STRING(memory_lock)) {
1192 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1193 "meml=%s", memory_lock);
1196 if (VALID_STRING(memory_unlock)) {
1197 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1198 "memu=%s", memory_unlock);
1201 } else if (tversion == V_AIX) {
1202 if (VALID_STRING(acs_chars)) {
1204 const char *acstrans = "lqkxjmwuvtn";
1206 char *tp, *sp, boxchars[11];
1209 for (cp = acstrans; *cp; cp++) {
1210 sp = (strchr) (acs_chars, *cp);
1221 char *tmp = _nc_tic_expand(boxchars,
1222 (outform == F_TERMINFO),
1224 _nc_STRCPY(buffer, "box1=", sizeof(buffer));
1225 while (*tmp != '\0') {
1226 size_t have = strlen(buffer);
1227 size_t next = strlen(tmp);
1228 size_t want = have + next + 1;
1233 * If the expanded string is too long for the buffer,
1234 * chop it off and save the location where we chopped it.
1236 if (want >= sizeof(buffer)) {
1240 _nc_STRCAT(buffer, tmp, sizeof(buffer));
1243 * If we chopped the buffer, replace the missing piece and
1244 * shift everything to append the remainder.
1249 while ((tmp[next] = tmp[last + next]) != '\0') {
1262 * kludge: trim off trailer to avoid an extra blank line
1263 * in infocmp -u output when there are no string differences
1266 bool trimmed = FALSE;
1267 j = (PredIdx) outbuf.used;
1268 if (wrapped && did_wrap) {
1271 && outbuf.text[j - 1] == '\t'
1272 && outbuf.text[j - 2] == '\n') {
1276 && outbuf.text[j - 1] == ':'
1277 && outbuf.text[j - 2] == '\t'
1278 && outbuf.text[j - 3] == '\n'
1279 && outbuf.text[j - 4] == '\\') {
1284 outbuf.text[outbuf.used] = '\0';
1286 strcpy_DYN(&outbuf, " ");
1290 fprintf(stderr, "num_bools = %d\n", num_bools);
1291 fprintf(stderr, "num_values = %d\n", num_values);
1292 fprintf(stderr, "num_strings = %d\n", num_strings);
1293 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
1294 tterm->term_names, len, outbuf.used, outbuf.text);
1297 * Here's where we use infodump to trigger a more stringent length check
1298 * for termcap-translation purposes.
1299 * Return the length of the raw entry, without tc= expansions,
1300 * It gives an idea of which entries are deadly to even *scan past*,
1301 * as opposed to *use*.
1303 return (infodump ? len : (int) termcap_length(outbuf.text));
1307 kill_string(TERMTYPE2 *tterm, char *cap)
1310 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1311 if (cap == tterm->Strings[n]) {
1312 tterm->Strings[n] = ABSENT_STRING;
1320 find_string(TERMTYPE2 *tterm, char *name)
1323 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1324 if (version_filter(STRING, n)
1325 && !strcmp(name, strnames[n])) {
1326 char *cap = tterm->Strings[n];
1327 if (VALID_STRING(cap)) {
1333 return ABSENT_STRING;
1337 * This is used to remove function-key labels from a termcap entry to
1341 kill_labels(TERMTYPE2 *tterm, int target)
1348 for (n = 0; n <= 10; ++n) {
1349 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "lf%d", n);
1350 cap = find_string(tterm, name);
1351 if (VALID_STRING(cap)
1352 && kill_string(tterm, cap)) {
1353 target -= (int) (strlen(cap) + 5);
1363 * This is used to remove function-key definitions from a termcap entry to
1367 kill_fkeys(TERMTYPE2 *tterm, int target)
1374 for (n = 60; n >= 0; --n) {
1375 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n);
1376 cap = find_string(tterm, name);
1377 if (VALID_STRING(cap)
1378 && kill_string(tterm, cap)) {
1379 target -= (int) (strlen(cap) + 5);
1389 * Check if the given acsc string is a 1-1 mapping, i.e., just-like-vt100.
1390 * Also, since this is for termcap, we only care about the line-drawing map.
1392 #define isLine(c) (strchr("lmkjtuvwqxn", c) != 0)
1395 one_one_mapping(const char *mapping)
1399 if (VALID_STRING(mapping)) {
1401 while (mapping[n] != '\0' && mapping[n + 1] != '\0') {
1402 if (isLine(mapping[n]) &&
1403 mapping[n] != mapping[n + 1]) {
1413 #define FMT_ENTRY() \
1414 fmt_entry(tterm, pred, \
1416 suppress_untranslatable, \
1419 #define SHOW_WHY PRINTF
1422 purged_acs(TERMTYPE2 *tterm)
1424 bool result = FALSE;
1426 if (VALID_STRING(acs_chars)) {
1427 if (!one_one_mapping(acs_chars)) {
1428 enter_alt_charset_mode = ABSENT_STRING;
1429 exit_alt_charset_mode = ABSENT_STRING;
1430 SHOW_WHY("# (rmacs/smacs removed for consistency)\n");
1438 encode_b64(char *target, char *source, unsigned state, int *saved)
1441 static const char data[] =
1442 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1443 "abcdefghijklmnopqrstuvwxyz"
1445 int ch = UChar(source[state]);
1447 switch (state % 3) {
1449 *target++ = data[(ch >> 2) & 077];
1453 *target++ = data[((ch >> 4) | *saved) & 077];
1457 *target++ = data[((ch >> 6) | *saved) & 077];
1458 *target++ = data[ch & 077];
1466 * Dump a single entry.
1469 dump_entry(TERMTYPE2 *tterm,
1470 int suppress_untranslatable,
1475 TERMTYPE2 save_tterm;
1483 unsigned offset = 0;
1487 if (_nc_write_object(tterm, bigbuf, &offset, sizeof(bigbuf)) == OK) {
1489 if (quickdump & 1) {
1492 wrap_concat1("hex:");
1493 for (n = 0; n < offset; ++n) {
1494 _nc_SPRINTF(numbuf, _nc_SLIMIT(sizeof(numbuf))
1495 "%02X", UChar(bigbuf[n]));
1496 wrap_concat1(numbuf);
1499 if (quickdump & 2) {
1500 static char padding[] =
1505 wrap_concat1("b64:");
1506 for (n = 0; n < offset; ++n) {
1507 encode_b64(numbuf, bigbuf, n, &value);
1508 wrap_concat1(numbuf);
1514 encode_b64(numbuf, padding, 1, &value);
1515 wrap_concat1(numbuf);
1519 encode_b64(numbuf, padding, 1, &value);
1520 wrap_concat1(numbuf);
1530 critlen = MAX_TERMCAP_LENGTH;
1531 legend = "older termcap";
1533 set_obsolete_termcaps(tterm);
1535 critlen = MAX_TERMINFO_LENGTH;
1536 legend = "terminfo";
1540 save_sgr = set_attributes;
1542 if ((FMT_ENTRY() > critlen)
1545 save_tterm = *tterm;
1546 if (!suppress_untranslatable) {
1547 SHOW_WHY("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1549 suppress_untranslatable = TRUE;
1551 if (FMT_ENTRY() > critlen) {
1553 * We pick on sgr because it's a nice long string capability that
1554 * is really just an optimization hack. Another good candidate is
1555 * acsc since it is both long and unused by BSD termcap.
1557 bool changed = FALSE;
1561 * Extended names are most likely function-key definitions. Drop
1565 for (n = STRCOUNT; n < NUM_STRINGS(tterm); n++) {
1566 const char *name = ExtStrname(tterm, (int) n, strnames);
1568 if (VALID_STRING(tterm->Strings[n])) {
1569 set_attributes = ABSENT_STRING;
1570 /* we remove long names anyway - only report the short */
1571 if (strlen(name) <= 2) {
1572 SHOW_WHY("# (%s removed to fit entry within %d bytes)\n",
1577 if (FMT_ENTRY() <= critlen)
1582 if (VALID_STRING(set_attributes)) {
1583 set_attributes = ABSENT_STRING;
1584 SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n",
1588 if (!changed || (FMT_ENTRY() > critlen)) {
1589 if (purged_acs(tterm)) {
1590 acs_chars = ABSENT_STRING;
1591 SHOW_WHY("# (acsc removed to fit entry within %d bytes)\n",
1596 if (!changed || (FMT_ENTRY() > critlen)) {
1597 int oldversion = tversion;
1600 SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1605 && kill_labels(tterm, len - critlen)) {
1606 SHOW_WHY("# (some labels capabilities suppressed to fit entry within %d bytes)\n",
1611 && kill_fkeys(tterm, len - critlen)) {
1612 SHOW_WHY("# (some function-key capabilities suppressed to fit entry within %d bytes)\n",
1616 if (len > critlen) {
1617 (void) fprintf(stderr,
1618 "%s: %s entry is %d bytes long\n",
1620 _nc_first_name(tterm->term_names),
1622 SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
1625 tversion = oldversion;
1627 set_attributes = save_sgr;
1628 *tterm = save_tterm;
1630 } else if (!version_filter(STRING, STR_IDX(acs_chars))) {
1631 save_tterm = *tterm;
1632 if (purged_acs(tterm)) {
1635 *tterm = save_tterm;
1640 dump_uses(const char *name, bool infodump)
1641 /* dump "use=" clauses in the appropriate format */
1643 char buffer[MAX_TERMINFO_LENGTH];
1647 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1648 "%s%s", infodump ? "use=" : "tc=", name);
1649 wrap_concat1(buffer);
1656 * Trim any remaining whitespace.
1658 if (outbuf.used != 0) {
1659 bool infodump = !TcOutput();
1660 char delim = (char) (infodump ? ',' : ':');
1663 for (j = (int) outbuf.used - 1; j > 0; --j) {
1664 char ch = outbuf.text[j];
1667 } else if (isspace(UChar(ch))) {
1668 outbuf.used = (size_t) j;
1669 } else if (!infodump && ch == '\\') {
1670 outbuf.used = (size_t) j;
1671 } else if (ch == delim && (j == 0 || outbuf.text[j - 1] != '\\')) {
1672 outbuf.used = (size_t) (j + 1);
1677 outbuf.text[outbuf.used] = '\0';
1679 if (outbuf.text != 0) {
1680 (void) fputs(outbuf.text, stdout);
1683 return (int) outbuf.used;
1687 compare_entry(PredHook hook,
1688 TERMTYPE2 *tp GCC_UNUSED,
1690 /* compare two entries */
1693 NCURSES_CONST char *name;
1696 fputs(" comparing booleans.\n", stdout);
1697 for_each_boolean(j, tp) {
1698 i = BoolIndirect(j);
1699 name = ExtBoolname(tp, (int) i, bool_names);
1701 if (isObsolete(outform, name))
1704 (*hook) (CMP_BOOLEAN, i, name);
1708 fputs(" comparing numbers.\n", stdout);
1709 for_each_number(j, tp) {
1711 name = ExtNumname(tp, (int) i, num_names);
1713 if (isObsolete(outform, name))
1716 (*hook) (CMP_NUMBER, i, name);
1720 fputs(" comparing strings.\n", stdout);
1721 for_each_string(j, tp) {
1723 name = ExtStrname(tp, (int) i, str_names);
1725 if (isObsolete(outform, name))
1728 (*hook) (CMP_STRING, i, name);
1731 /* (void) fputs(" comparing use entries.\n", stdout); */
1732 (*hook) (CMP_USE, 0, "use");
1736 #define NOTSET(s) ((s) == 0)
1739 * This bit of legerdemain turns all the terminfo variable names into
1740 * references to locations in the arrays Booleans, Numbers, and Strings ---
1741 * precisely what's needed.
1747 set_obsolete_termcaps(TERMTYPE2 *tp)
1749 #include "capdefaults.c"
1753 * Convert an alternate-character-set string to canonical form: sorted and
1757 repair_acsc(TERMTYPE2 *tp)
1759 if (VALID_STRING(acs_chars)) {
1765 bool fix_needed = FALSE;
1767 for (n = 0, source = 0; acs_chars[n] != 0; n++) {
1768 target = UChar(acs_chars[n]);
1769 if (source >= target) {
1774 if (acs_chars[n + 1])
1778 memset(mapped, 0, sizeof(mapped));
1779 for (n = 0; acs_chars[n] != 0; n++) {
1780 source = UChar(acs_chars[n]);
1781 if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
1782 mapped[source] = (char) target;
1785 extra = (char) source;
1788 for (n = m = 0; n < sizeof(mapped); n++) {
1790 acs_chars[m++] = (char) n;
1791 acs_chars[m++] = mapped[n];
1795 acs_chars[m++] = extra; /* garbage in, garbage out */