1 /****************************************************************************
2 * Copyright (c) 1998-2016,2017 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996 on *
33 ****************************************************************************/
35 #define __INTERNAL_CAPS_VISIBLE
36 #include <progs.priv.h>
38 #include "dump_entry.h"
39 #include "termsort.c" /* this C file is generated */
40 #include <parametrized.h> /* so is this */
42 MODULE_ID("$Id: dump_entry.c,v 1.168 2017/09/02 21:01:54 tom Exp $")
44 #define DISCARD(string) string = ABSENT_STRING
45 #define PRINTF (void) printf
48 #define OkIndex(index,array) ((int)(index) >= 0 && (int)(index) < (int) SIZEOF(array))
49 #define TcOutput() (outform == F_TERMCAP || outform == F_TCONVERR)
57 static int tversion; /* terminfo version */
58 static int outform; /* output format to use */
59 static int sortmode; /* sort mode to use */
60 static int width = 60; /* max line width for listings */
61 static int height = 65535; /* max number of lines for listings */
62 static int column; /* current column, limited by 'width' */
63 static int oldcol; /* last value of column before wrap */
64 static bool pretty; /* true if we format if-then-else strings */
65 static bool wrapped; /* true if we wrap too-long strings */
66 static bool did_wrap; /* true if last wrap_concat did wrapping */
67 static bool checking; /* true if we are checking for tic */
68 static int quickdump; /* true if we are dumping compiled data */
70 static char *save_sgr;
75 /* indirection pointers for implementing sort and display modes */
76 static const PredIdx *bool_indirect, *num_indirect, *str_indirect;
77 static NCURSES_CONST char *const *bool_names;
78 static NCURSES_CONST char *const *num_names;
79 static NCURSES_CONST char *const *str_names;
81 static const char *separator = "", *trailer = "";
82 static int indent = 8;
84 /* cover various ports and variants of terminfo */
85 #define V_ALLCAPS 0 /* all capabilities (SVr4, XSI, ncurses) */
86 #define V_SVR1 1 /* SVR1, Ultrix */
87 #define V_HPUX 2 /* HP/UX */
88 #define V_AIX 3 /* AIX */
89 #define V_BSD 4 /* BSD */
92 #define OBSOLETE(n) (!_nc_user_definable && (n[0] == 'O' && n[1] == 'T'))
94 #define OBSOLETE(n) (n[0] == 'O' && n[1] == 'T')
97 #define isObsolete(f,n) ((f == F_TERMINFO || f == F_VARIABLE) && OBSOLETE(n))
100 #define BoolIndirect(j) ((j >= BOOLCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : bool_indirect[j]))
101 #define NumIndirect(j) ((j >= NUMCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : num_indirect[j]))
102 #define StrIndirect(j) ((j >= STRCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : str_indirect[j]))
104 #define BoolIndirect(j) ((sortmode == S_NOSORT) ? (j) : bool_indirect[j])
105 #define NumIndirect(j) ((sortmode == S_NOSORT) ? (j) : num_indirect[j])
106 #define StrIndirect(j) ((sortmode == S_NOSORT) ? (j) : str_indirect[j])
109 static void failed(const char *) GCC_NORETURN;
112 failed(const char *s)
115 ExitProgram(EXIT_FAILURE);
119 strncpy_DYN(DYNBUF * dst, const char *src, size_t need)
121 size_t want = need + dst->used + 1;
122 if (want > dst->size) {
123 dst->size += (want + 1024); /* be generous */
124 dst->text = typeRealloc(char, dst->size, dst->text);
126 failed("strncpy_DYN");
128 _nc_STRNCPY(dst->text + dst->used, src, need + 1);
130 dst->text[dst->used] = 0;
134 strcpy_DYN(DYNBUF * dst, const char *src)
140 strncpy_DYN(dst, src, strlen(src));
156 _nc_leaks_dump_entry(void)
163 #define NameTrans(check,result) \
164 if ((np->nte_index <= OK_ ## check) \
165 && check[np->nte_index]) \
166 return (result[np->nte_index])
169 nametrans(const char *name)
170 /* translate a capability name to termcap from terminfo */
172 const struct name_table_entry *np;
174 if ((np = _nc_find_entry(name, _nc_get_hash_table(0))) != 0) {
175 switch (np->nte_type) {
177 NameTrans(bool_from_termcap, boolcodes);
181 NameTrans(num_from_termcap, numcodes);
185 NameTrans(str_from_termcap, strcodes);
194 dump_init(const char *version,
204 /* set up for entry display */
209 wrapped = wrap_strings;
211 quickdump = (quick & 3);
213 did_wrap = (width <= 0);
217 tversion = V_ALLCAPS;
218 else if (!strcmp(version, "SVr1") || !strcmp(version, "SVR1")
219 || !strcmp(version, "Ultrix"))
221 else if (!strcmp(version, "HP"))
223 else if (!strcmp(version, "AIX"))
225 else if (!strcmp(version, "BSD"))
228 tversion = V_ALLCAPS;
230 /* implement display modes */
231 switch (outform = mode) {
234 bool_names = boolnames;
235 num_names = numnames;
236 str_names = strnames;
237 separator = (twidth > 0 && theight > 1) ? ", " : ",";
242 bool_names = boolfnames;
243 num_names = numfnames;
244 str_names = strfnames;
245 separator = (twidth > 0 && theight > 1) ? ", " : ",";
251 bool_names = boolcodes;
252 num_names = numcodes;
253 str_names = strcodes;
260 /* implement sort modes */
261 switch (sortmode = sort) {
264 (void) fprintf(stderr,
265 "%s: sorting by term structure order\n", _nc_progname);
270 (void) fprintf(stderr,
271 "%s: sorting by terminfo name order\n", _nc_progname);
272 bool_indirect = bool_terminfo_sort;
273 num_indirect = num_terminfo_sort;
274 str_indirect = str_terminfo_sort;
279 (void) fprintf(stderr,
280 "%s: sorting by C variable order\n", _nc_progname);
281 bool_indirect = bool_variable_sort;
282 num_indirect = num_variable_sort;
283 str_indirect = str_variable_sort;
288 (void) fprintf(stderr,
289 "%s: sorting by termcap name order\n", _nc_progname);
290 bool_indirect = bool_termcap_sort;
291 num_indirect = num_termcap_sort;
292 str_indirect = str_termcap_sort;
297 (void) fprintf(stderr,
298 "%s: width = %d, tversion = %d, outform = %d\n",
299 _nc_progname, width, tversion, outform);
302 static TERMTYPE2 *cur_type;
305 dump_predicate(PredType type, PredIdx idx)
306 /* predicate function to use for ordinary decompilation */
310 return (cur_type->Booleans[idx] == FALSE)
311 ? FAIL : cur_type->Booleans[idx];
314 return (cur_type->Numbers[idx] == ABSENT_NUMERIC)
315 ? FAIL : cur_type->Numbers[idx];
318 return (cur_type->Strings[idx] != ABSENT_STRING)
322 return (FALSE); /* pacify compiler */
325 static void set_obsolete_termcaps(TERMTYPE2 *tp);
327 /* is this the index of a function key string? */
329 (((i) >= STR_IDX(key_f0) && \
330 (i) <= STR_IDX(key_f9)) || \
331 ((i) >= STR_IDX(key_f11) && \
332 (i) <= STR_IDX(key_f63)))
335 * If we configure with a different Caps file, the offsets into the arrays
336 * will change. So we use an address expression.
338 #define BOOL_IDX(name) (PredType) (&(name) - &(CUR Booleans[0]))
339 #define NUM_IDX(name) (PredType) (&(name) - &(CUR Numbers[0]))
340 #define STR_IDX(name) (PredType) (&(name) - &(CUR Strings[0]))
343 version_filter(PredType type, PredIdx idx)
344 /* filter out capabilities we may want to suppress */
347 case V_ALLCAPS: /* SVr4, XSI Curses */
350 case V_SVR1: /* System V Release 1, Ultrix */
353 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
355 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
357 return ((idx <= STR_IDX(prtr_non)) ? TRUE : FALSE);
361 case V_HPUX: /* Hewlett-Packard */
364 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
366 return ((idx <= NUM_IDX(label_width)) ? TRUE : FALSE);
368 if (idx <= STR_IDX(prtr_non))
370 else if (FNKEY(idx)) /* function keys */
372 else if (idx == STR_IDX(plab_norm)
373 || idx == STR_IDX(label_on)
374 || idx == STR_IDX(label_off))
381 case V_AIX: /* AIX */
384 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
386 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
388 if (idx <= STR_IDX(prtr_non))
390 else if (FNKEY(idx)) /* function keys */
397 #define is_termcap(type) (OkIndex(idx, type##_from_termcap) && \
398 type##_from_termcap[idx])
400 case V_BSD: /* BSD */
403 return is_termcap(bool);
405 return is_termcap(num);
407 return is_termcap(str);
412 return (FALSE); /* pacify the compiler */
418 while (outbuf.used > 0 && outbuf.text[outbuf.used - 1] == ' ')
419 outbuf.text[--outbuf.used] = '\0';
427 strcpy_DYN(&outbuf, trailer);
432 op_length(const char *src, int offset)
436 if (offset > 0 && src[offset - 1] == '\\') {
439 result++; /* for '%' mark */
440 ch = src[offset + result];
444 } else if (ch == '+') {
449 } else if (ch == '\'') {
451 } else if (ch == L_CURL[0]) {
453 while ((ch = src[offset + n]) != '\0') {
454 if (ch == R_CURL[0]) {
460 } else if (strchr("pPg", ch) != 0) {
463 result++; /* ordinary operator */
470 * When wrapping too-long strings, avoid splitting a backslash sequence, or
471 * a terminfo '%' operator. That will leave things a little ragged, but avoids
472 * a stray backslash at the end of the line, as well as making the result a
473 * little more readable.
476 find_split(const char *src, int step, int size)
481 /* check if that would split a backslash-sequence */
483 for (n = size - 1; n > 0; --n) {
484 int ch = UChar(src[step + n]);
486 if (n > 0 && src[step + n - 1] == ch)
490 } else if (!isalnum(ch)) {
497 /* check if that would split a backslash-sequence */
498 for (n = size - 1; n > 0; --n) {
499 int ch = UChar(src[step + n]);
501 int need = op_length(src, step + n);
502 if ((n + need) > size) {
517 * If we are going to wrap lines, we cannot leave literal spaces because that
518 * would be ambiguous if we split on that space.
521 fill_spaces(const char *src)
523 const char *fill = "\\s";
524 size_t need = strlen(src);
525 size_t size = strlen(fill);
529 for (pass = 0; pass < 2; ++pass) {
530 for (s = d = 0; src[s] != '\0'; ++s) {
533 strcpy(&result[d], fill);
540 result[d++] = src[s];
549 result = malloc(need + 1);
551 failed("fill_spaces");
565 #define wrap_1ST(mode) ((mode)&w1ST)
566 #define wrap_END(mode) ((mode)&wEND)
567 #define wrap_ERR(mode) ((mode)&wERR)
570 wrap_concat(const char *src, int need, unsigned mode)
572 int gaps = (int) strlen(separator);
573 int want = gaps + need;
575 did_wrap = (width <= 0);
578 && column + want > width) {
581 if ((wrap_END(mode) && !wrap_ERR(mode)) &&
584 (column + want) > width) {
586 int used = width > WRAPPED ? width : WRAPPED;
590 const char *my_t = trailer;
591 char *fill = fill_spaces(src);
592 int last = (int) strlen(fill);
599 if (!TcOutput() && (p = strchr(fill, '=')) != 0) {
600 base = (int) (p + 1 - fill);
603 _nc_SPRINTF(align, _nc_SLIMIT(align) "%*s", base, " ");
604 } else if (column > 8) {
608 _nc_SPRINTF(align, _nc_SLIMIT(align) "%*s", base, " ");
612 /* "pretty" overrides wrapping if it already split the line */
613 if (!pretty || strchr(fill, '\n') == 0) {
616 if (TcOutput() && outbuf.used && !wrap_1ST(mode)) {
620 while ((column + (need + gaps)) > used) {
623 strcpy_DYN(&outbuf, align);
626 if (size > (last - step)) {
627 size = (last - step);
629 size = find_split(fill, step, size);
630 strncpy_DYN(&outbuf, fill + step, (size_t) size);
642 strcpy_DYN(&outbuf, align);
643 strcpy_DYN(&outbuf, fill + step);
646 strcpy_DYN(&outbuf, separator);
652 strcpy_DYN(&outbuf, src);
654 strcpy_DYN(&outbuf, separator);
655 column += (int) strlen(src);
660 wrap_concat1(const char *src)
662 int need = (int) strlen(src);
663 wrap_concat(src, need, w1ST | wEND);
667 wrap_concat3(const char *name, const char *eqls, const char *value)
669 int nlen = (int) strlen(name);
670 int elen = (int) strlen(eqls);
671 int vlen = (int) strlen(value);
673 wrap_concat(name, nlen + elen + vlen, w1ST);
674 wrap_concat(eqls, elen + vlen, w2ND);
675 wrap_concat(value, vlen, wEND);
678 #define IGNORE_SEP_TRAIL(first,last,sep_trail) \
679 if ((size_t)(last - first) > sizeof(sep_trail)-1 \
680 && !strncmp(first, sep_trail, sizeof(sep_trail)-1)) \
681 first += sizeof(sep_trail)-2
683 /* Returns the nominal length of the buffer assuming it is termcap format,
684 * i.e., the continuation sequence is treated as a single character ":".
686 * There are several implementations of termcap which read the text into a
687 * fixed-size buffer. Generally they strip the newlines from the text, but may
688 * not do it until after the buffer is read. Also, "tc=" resolution may be
689 * expanded in the same buffer. This function is useful for measuring the size
690 * of the best fixed-buffer implementation; the worst case may be much worse.
692 #ifdef TEST_TERMCAP_LENGTH
694 termcap_length(const char *src)
696 static const char pattern[] = ":\\\n\t:";
699 const char *const t = src + strlen(src);
701 while (*src != '\0') {
702 IGNORE_SEP_TRAIL(src, t, pattern);
709 #define termcap_length(src) strlen(src)
713 indent_DYN(DYNBUF * buffer, int level)
717 for (n = 0; n < level; n++)
718 strncpy_DYN(buffer, "\t", (size_t) 1);
722 has_params(const char *src)
725 int len = (int) strlen(src);
730 for (n = 0; n < len - 1; ++n) {
731 if (!strncmp(src + n, "%p", (size_t) 2)) {
733 } else if (!strncmp(src + n, "%;", (size_t) 2)) {
740 result = ((len > 50) && params);
746 fmt_complex(TERMTYPE2 *tterm, const char *capability, char *src, int level)
748 bool percent = FALSE;
749 bool params = has_params(src);
751 while (*src != '\0') {
755 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
759 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
765 case 't': /* "then" */
766 case 'e': /* "else" */
769 tmpbuf.text[tmpbuf.used - 1] = '\n';
770 /* treat a "%e" as else-if, on the same level */
772 indent_DYN(&tmpbuf, level);
773 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
774 strncpy_DYN(&tmpbuf, src, (size_t) 1);
776 params = has_params(src);
777 if (!params && *src != '\0' && *src != '%') {
778 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
779 indent_DYN(&tmpbuf, level + 1);
782 indent_DYN(&tmpbuf, level + 1);
783 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
784 strncpy_DYN(&tmpbuf, src, (size_t) 1);
786 src = fmt_complex(tterm, capability, src, level + 1);
787 if (*src != '\0' && *src != '%') {
788 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
789 indent_DYN(&tmpbuf, level + 1);
791 } else if (level == 1) {
793 _nc_warning("%s: %%%c without %%? in %s",
794 _nc_first_name(tterm->term_names),
801 case ';': /* "endif" */
805 tmpbuf.text[tmpbuf.used - 1] = '\n';
806 indent_DYN(&tmpbuf, level);
807 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
808 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
811 && (strchr("?e;", src[1])) == 0) {
812 tmpbuf.text[tmpbuf.used++] = '\n';
813 indent_DYN(&tmpbuf, level);
818 _nc_warning("%s: %%; without %%? in %s",
819 _nc_first_name(tterm->term_names),
824 if (percent && params) {
825 tmpbuf.text[tmpbuf.used - 1] = '\n';
826 indent_DYN(&tmpbuf, level + 1);
827 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
833 strncpy_DYN(&tmpbuf, "\\s", (size_t) 2);
840 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
846 * Make "large" numbers a little easier to read by showing them in hexadecimal
847 * if they are "close" to a power of two.
850 number_format(int value)
852 const char *result = "%d";
853 if ((outform != F_TERMCAP) && (value > 255)) {
854 unsigned long lv = (unsigned long) value;
856 int bits = sizeof(unsigned long) * 8;
858 for (nn = 8; nn < bits; ++nn) {
860 if ((mm - 16) <= lv && (mm + 16) > lv) {
869 #define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap)
873 fmt_entry(TERMTYPE2 *tterm,
876 int suppress_untranslatable,
881 char buffer[MAX_TERMINFO_LENGTH + EXTRA_CAP];
883 NCURSES_CONST char *name;
885 PredIdx num_bools = 0;
886 PredIdx num_values = 0;
887 PredIdx num_strings = 0;
890 #define WRAP_CONCAT1(s) wrap_concat1(s); outcount = TRUE
891 #define WRAP_CONCAT WRAP_CONCAT1(buffer)
893 len = 12; /* terminfo file-header */
897 pred = dump_predicate;
900 strcpy_DYN(&outbuf, 0);
902 column = indent; /* FIXME: workaround to prevent empty lines */
904 strcpy_DYN(&outbuf, tterm->term_names);
907 * Colon is legal in terminfo descriptions, but not in termcap.
910 char *p = outbuf.text;
918 strcpy_DYN(&outbuf, separator);
919 column = (int) outbuf.used;
924 for_each_boolean(j, tterm) {
926 name = ExtBoolname(tterm, (int) i, bool_names);
927 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
929 if (!version_filter(BOOLEAN, i))
931 else if (isObsolete(outform, name))
934 predval = pred(BOOLEAN, i);
935 if (predval != FAIL) {
936 _nc_STRCPY(buffer, name, sizeof(buffer));
938 _nc_STRCAT(buffer, "@", sizeof(buffer));
939 else if (i + 1 > num_bools)
945 if (column != indent && height > 1)
948 for_each_number(j, tterm) {
950 name = ExtNumname(tterm, (int) i, num_names);
951 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
953 if (!version_filter(NUMBER, i))
955 else if (isObsolete(outform, name))
958 predval = pred(NUMBER, i);
959 if (predval != FAIL) {
960 if (tterm->Numbers[i] < 0) {
961 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
965 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
968 _nc_SPRINTF(buffer + nn, _nc_SLIMIT(sizeof(buffer) - nn)
969 number_format(tterm->Numbers[i]),
971 if (i + 1 > num_values)
978 if (column != indent && height > 1)
981 len += (int) (num_bools
983 + strlen(tterm->term_names) + 1);
989 if (outform == F_TERMCAP) {
990 if (VALID_STRING(termcap_reset)) {
991 if (VALID_STRING(init_3string)
992 && !strcmp(init_3string, termcap_reset))
993 DISCARD(init_3string);
995 if (VALID_STRING(reset_2string)
996 && !strcmp(reset_2string, termcap_reset))
997 DISCARD(reset_2string);
1001 for_each_string(j, tterm) {
1003 name = ExtStrname(tterm, (int) i, str_names);
1004 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
1006 capability = tterm->Strings[i];
1008 if (!version_filter(STRING, i))
1010 else if (isObsolete(outform, name))
1015 * Extended names can be longer than 2 characters, but termcap programs
1016 * cannot read those (filter them out).
1018 if (outform == F_TERMCAP && (strlen(name) > 2))
1022 if (outform == F_TERMCAP) {
1024 * Some older versions of vi want rmir/smir to be defined
1025 * for ich/ich1 to work. If they're not defined, force
1026 * them to be output as defined and empty.
1028 if (PRESENT(insert_character) || PRESENT(parm_ich)) {
1029 if (SAME_CAP(i, enter_insert_mode)
1030 && enter_insert_mode == ABSENT_STRING) {
1031 _nc_STRCPY(buffer, "im=", sizeof(buffer));
1036 if (SAME_CAP(i, exit_insert_mode)
1037 && exit_insert_mode == ABSENT_STRING) {
1038 _nc_STRCPY(buffer, "ei=", sizeof(buffer));
1044 * termcap applications such as screen will be confused if sgr0
1045 * is translated to a string containing rmacs. Filter that out.
1047 if (PRESENT(exit_attribute_mode)) {
1048 if (SAME_CAP(i, exit_attribute_mode)) {
1050 char *my_sgr = set_attributes;
1052 set_attributes = save_sgr;
1054 trimmed_sgr0 = _nc_trim_sgr0(tterm);
1055 if (strcmp(capability, trimmed_sgr0)) {
1056 capability = trimmed_sgr0;
1058 if (trimmed_sgr0 != exit_attribute_mode)
1062 set_attributes = my_sgr;
1067 predval = pred(STRING, i);
1070 if (predval != FAIL) {
1071 if (VALID_STRING(capability)
1072 && i + 1 > num_strings)
1073 num_strings = i + 1;
1075 if (!VALID_STRING(capability)) {
1076 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1079 } else if (TcOutput()) {
1080 char *srccap = _nc_tic_expand(capability, TRUE, numbers);
1081 int params = (((i < (int) SIZEOF(parametrized)) &&
1086 : has_params(srccap)));
1087 char *cv = _nc_infotocap(name, srccap, params);
1090 if (outform == F_TCONVERR) {
1091 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1092 "%s=!!! %s WILL NOT CONVERT !!!",
1095 } else if (suppress_untranslatable) {
1098 char *s = srccap, *d = buffer;
1099 int need = 3 + (int) strlen(name);
1100 while ((*d = *s++) != 0) {
1101 if ((d - buffer + 1) >= (int) sizeof(buffer)) {
1103 "%s: value for %s is too long\n",
1112 } else if (*d == '\\') {
1118 need += (int) (d - buffer);
1119 wrap_concat("..", need, w1ST | wERR);
1121 wrap_concat(name, need, wOFF | wERR);
1122 need -= (int) strlen(name);
1123 wrap_concat("=", need, w2ND | wERR);
1125 wrap_concat(buffer, need, wEND | wERR);
1129 wrap_concat3(name, "=", cv);
1131 len += (int) strlen(capability) + 1;
1133 char *src = _nc_tic_expand(capability,
1134 outform == F_TERMINFO, numbers);
1136 strcpy_DYN(&tmpbuf, 0);
1137 strcpy_DYN(&tmpbuf, name);
1138 strcpy_DYN(&tmpbuf, "=");
1140 && (outform == F_TERMINFO
1141 || outform == F_VARIABLE)) {
1142 fmt_complex(tterm, name, src, 1);
1144 strcpy_DYN(&tmpbuf, src);
1146 len += (int) strlen(capability) + 1;
1147 WRAP_CONCAT1(tmpbuf.text);
1150 /* e.g., trimmed_sgr0 */
1151 if (VALID_STRING(capability) &&
1152 capability != tterm->Strings[i])
1155 len += (int) (num_strings * 2);
1158 * This piece of code should be an effective inverse of the functions
1159 * postprocess_terminfo() and postprocess_terminfo() in parse_entry.c.
1160 * Much more work should be done on this to support dumping termcaps.
1162 if (tversion == V_HPUX) {
1163 if (VALID_STRING(memory_lock)) {
1164 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1165 "meml=%s", memory_lock);
1168 if (VALID_STRING(memory_unlock)) {
1169 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1170 "memu=%s", memory_unlock);
1173 } else if (tversion == V_AIX) {
1174 if (VALID_STRING(acs_chars)) {
1176 const char *acstrans = "lqkxjmwuvtn";
1178 char *tp, *sp, boxchars[11];
1181 for (cp = acstrans; *cp; cp++) {
1182 sp = (strchr) (acs_chars, *cp);
1193 char *tmp = _nc_tic_expand(boxchars,
1194 (outform == F_TERMINFO),
1196 _nc_STRCPY(buffer, "box1=", sizeof(buffer));
1197 while (*tmp != '\0') {
1198 size_t have = strlen(buffer);
1199 size_t next = strlen(tmp);
1200 size_t want = have + next + 1;
1205 * If the expanded string is too long for the buffer,
1206 * chop it off and save the location where we chopped it.
1208 if (want >= sizeof(buffer)) {
1212 _nc_STRCAT(buffer, tmp, sizeof(buffer));
1215 * If we chopped the buffer, replace the missing piece and
1216 * shift everything to append the remainder.
1221 while ((tmp[next] = tmp[last + next]) != '\0') {
1234 * kludge: trim off trailer to avoid an extra blank line
1235 * in infocmp -u output when there are no string differences
1238 bool trimmed = FALSE;
1239 j = (PredIdx) outbuf.used;
1240 if (wrapped && did_wrap) {
1243 && outbuf.text[j - 1] == '\t'
1244 && outbuf.text[j - 2] == '\n') {
1248 && outbuf.text[j - 1] == ':'
1249 && outbuf.text[j - 2] == '\t'
1250 && outbuf.text[j - 3] == '\n'
1251 && outbuf.text[j - 4] == '\\') {
1256 outbuf.text[outbuf.used] = '\0';
1258 strcpy_DYN(&outbuf, " ");
1262 fprintf(stderr, "num_bools = %d\n", num_bools);
1263 fprintf(stderr, "num_values = %d\n", num_values);
1264 fprintf(stderr, "num_strings = %d\n", num_strings);
1265 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
1266 tterm->term_names, len, outbuf.used, outbuf.text);
1269 * Here's where we use infodump to trigger a more stringent length check
1270 * for termcap-translation purposes.
1271 * Return the length of the raw entry, without tc= expansions,
1272 * It gives an idea of which entries are deadly to even *scan past*,
1273 * as opposed to *use*.
1275 return (infodump ? len : (int) termcap_length(outbuf.text));
1279 kill_string(TERMTYPE2 *tterm, char *cap)
1282 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1283 if (cap == tterm->Strings[n]) {
1284 tterm->Strings[n] = ABSENT_STRING;
1292 find_string(TERMTYPE2 *tterm, char *name)
1295 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1296 if (version_filter(STRING, n)
1297 && !strcmp(name, strnames[n])) {
1298 char *cap = tterm->Strings[n];
1299 if (VALID_STRING(cap)) {
1305 return ABSENT_STRING;
1309 * This is used to remove function-key labels from a termcap entry to
1313 kill_labels(TERMTYPE2 *tterm, int target)
1320 for (n = 0; n <= 10; ++n) {
1321 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "lf%d", n);
1322 cap = find_string(tterm, name);
1323 if (VALID_STRING(cap)
1324 && kill_string(tterm, cap)) {
1325 target -= (int) (strlen(cap) + 5);
1335 * This is used to remove function-key definitions from a termcap entry to
1339 kill_fkeys(TERMTYPE2 *tterm, int target)
1346 for (n = 60; n >= 0; --n) {
1347 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n);
1348 cap = find_string(tterm, name);
1349 if (VALID_STRING(cap)
1350 && kill_string(tterm, cap)) {
1351 target -= (int) (strlen(cap) + 5);
1361 * Check if the given acsc string is a 1-1 mapping, i.e., just-like-vt100.
1362 * Also, since this is for termcap, we only care about the line-drawing map.
1364 #define isLine(c) (strchr("lmkjtuvwqxn", c) != 0)
1367 one_one_mapping(const char *mapping)
1371 if (VALID_STRING(mapping)) {
1373 while (mapping[n] != '\0') {
1374 if (isLine(mapping[n]) &&
1375 mapping[n] != mapping[n + 1]) {
1385 #define FMT_ENTRY() \
1386 fmt_entry(tterm, pred, \
1388 suppress_untranslatable, \
1391 #define SHOW_WHY PRINTF
1394 purged_acs(TERMTYPE2 *tterm)
1396 bool result = FALSE;
1398 if (VALID_STRING(acs_chars)) {
1399 if (!one_one_mapping(acs_chars)) {
1400 enter_alt_charset_mode = ABSENT_STRING;
1401 exit_alt_charset_mode = ABSENT_STRING;
1402 SHOW_WHY("# (rmacs/smacs removed for consistency)\n");
1410 encode_b64(char *target, char *source, unsigned state, int *saved)
1413 static const char data[] =
1414 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1415 "abcdefghijklmnopqrstuvwxyz"
1417 int ch = UChar(source[state]);
1419 switch (state % 3) {
1421 *target++ = data[(ch >> 2) & 077];
1425 *target++ = data[((ch >> 4) | *saved) & 077];
1429 *target++ = data[((ch >> 6) | *saved) & 077];
1430 *target++ = data[ch & 077];
1438 * Dump a single entry.
1441 dump_entry(TERMTYPE2 *tterm,
1442 int suppress_untranslatable,
1447 TERMTYPE2 save_tterm;
1455 unsigned offset = 0;
1459 if (_nc_write_object(tterm, bigbuf, &offset, sizeof(bigbuf)) == OK) {
1461 if (quickdump & 1) {
1464 wrap_concat1("hex:");
1465 for (n = 0; n < offset; ++n) {
1466 _nc_SPRINTF(numbuf, _nc_SLIMIT(sizeof(numbuf))
1467 "%02X", UChar(bigbuf[n]));
1468 wrap_concat1(numbuf);
1471 if (quickdump & 2) {
1472 static char padding[] =
1477 wrap_concat1("b64:");
1478 for (n = 0; n < offset; ++n) {
1479 encode_b64(numbuf, bigbuf, n, &value);
1480 wrap_concat1(numbuf);
1486 encode_b64(numbuf, padding, 1, &value);
1487 wrap_concat1(numbuf);
1491 encode_b64(numbuf, padding, 1, &value);
1492 wrap_concat1(numbuf);
1502 critlen = MAX_TERMCAP_LENGTH;
1503 legend = "older termcap";
1505 set_obsolete_termcaps(tterm);
1507 critlen = MAX_TERMINFO_LENGTH;
1508 legend = "terminfo";
1512 save_sgr = set_attributes;
1514 if ((FMT_ENTRY() > critlen)
1517 save_tterm = *tterm;
1518 if (!suppress_untranslatable) {
1519 SHOW_WHY("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1521 suppress_untranslatable = TRUE;
1523 if (FMT_ENTRY() > critlen) {
1525 * We pick on sgr because it's a nice long string capability that
1526 * is really just an optimization hack. Another good candidate is
1527 * acsc since it is both long and unused by BSD termcap.
1529 bool changed = FALSE;
1533 * Extended names are most likely function-key definitions. Drop
1537 for (n = STRCOUNT; n < NUM_STRINGS(tterm); n++) {
1538 const char *name = ExtStrname(tterm, (int) n, strnames);
1540 if (VALID_STRING(tterm->Strings[n])) {
1541 set_attributes = ABSENT_STRING;
1542 /* we remove long names anyway - only report the short */
1543 if (strlen(name) <= 2) {
1544 SHOW_WHY("# (%s removed to fit entry within %d bytes)\n",
1549 if (FMT_ENTRY() <= critlen)
1554 if (VALID_STRING(set_attributes)) {
1555 set_attributes = ABSENT_STRING;
1556 SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n",
1560 if (!changed || (FMT_ENTRY() > critlen)) {
1561 if (purged_acs(tterm)) {
1562 acs_chars = ABSENT_STRING;
1563 SHOW_WHY("# (acsc removed to fit entry within %d bytes)\n",
1568 if (!changed || (FMT_ENTRY() > critlen)) {
1569 int oldversion = tversion;
1572 SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1577 && kill_labels(tterm, len - critlen)) {
1578 SHOW_WHY("# (some labels capabilities suppressed to fit entry within %d bytes)\n",
1583 && kill_fkeys(tterm, len - critlen)) {
1584 SHOW_WHY("# (some function-key capabilities suppressed to fit entry within %d bytes)\n",
1588 if (len > critlen) {
1589 (void) fprintf(stderr,
1590 "%s: %s entry is %d bytes long\n",
1592 _nc_first_name(tterm->term_names),
1594 SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
1597 tversion = oldversion;
1599 set_attributes = save_sgr;
1600 *tterm = save_tterm;
1602 } else if (!version_filter(STRING, STR_IDX(acs_chars))) {
1603 save_tterm = *tterm;
1604 if (purged_acs(tterm)) {
1607 *tterm = save_tterm;
1612 dump_uses(const char *name, bool infodump)
1613 /* dump "use=" clauses in the appropriate format */
1615 char buffer[MAX_TERMINFO_LENGTH];
1619 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1620 "%s%s", infodump ? "use=" : "tc=", name);
1621 wrap_concat1(buffer);
1628 * Trim any remaining whitespace.
1630 if (outbuf.used != 0) {
1631 bool infodump = !TcOutput();
1632 char delim = (char) (infodump ? ',' : ':');
1635 for (j = (int) outbuf.used - 1; j > 0; --j) {
1636 char ch = outbuf.text[j];
1639 } else if (isspace(UChar(ch))) {
1640 outbuf.used = (size_t) j;
1641 } else if (!infodump && ch == '\\') {
1642 outbuf.used = (size_t) j;
1643 } else if (ch == delim && (j == 0 || outbuf.text[j - 1] != '\\')) {
1644 outbuf.used = (size_t) (j + 1);
1649 outbuf.text[outbuf.used] = '\0';
1651 if (outbuf.text != 0) {
1652 (void) fputs(outbuf.text, stdout);
1655 return (int) outbuf.used;
1659 compare_entry(PredHook hook,
1660 TERMTYPE2 *tp GCC_UNUSED,
1662 /* compare two entries */
1665 NCURSES_CONST char *name;
1668 fputs(" comparing booleans.\n", stdout);
1669 for_each_boolean(j, tp) {
1670 i = BoolIndirect(j);
1671 name = ExtBoolname(tp, (int) i, bool_names);
1673 if (isObsolete(outform, name))
1676 (*hook) (CMP_BOOLEAN, i, name);
1680 fputs(" comparing numbers.\n", stdout);
1681 for_each_number(j, tp) {
1683 name = ExtNumname(tp, (int) i, num_names);
1685 if (isObsolete(outform, name))
1688 (*hook) (CMP_NUMBER, i, name);
1692 fputs(" comparing strings.\n", stdout);
1693 for_each_string(j, tp) {
1695 name = ExtStrname(tp, (int) i, str_names);
1697 if (isObsolete(outform, name))
1700 (*hook) (CMP_STRING, i, name);
1703 /* (void) fputs(" comparing use entries.\n", stdout); */
1704 (*hook) (CMP_USE, 0, "use");
1708 #define NOTSET(s) ((s) == 0)
1711 * This bit of legerdemain turns all the terminfo variable names into
1712 * references to locations in the arrays Booleans, Numbers, and Strings ---
1713 * precisely what's needed.
1719 set_obsolete_termcaps(TERMTYPE2 *tp)
1721 #include "capdefaults.c"
1725 * Convert an alternate-character-set string to canonical form: sorted and
1729 repair_acsc(TERMTYPE2 *tp)
1731 if (VALID_STRING(acs_chars)) {
1737 bool fix_needed = FALSE;
1739 for (n = 0, source = 0; acs_chars[n] != 0; n++) {
1740 target = UChar(acs_chars[n]);
1741 if (source >= target) {
1746 if (acs_chars[n + 1])
1750 memset(mapped, 0, sizeof(mapped));
1751 for (n = 0; acs_chars[n] != 0; n++) {
1752 source = UChar(acs_chars[n]);
1753 if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
1754 mapped[source] = (char) target;
1757 extra = (char) source;
1760 for (n = m = 0; n < sizeof(mapped); n++) {
1762 acs_chars[m++] = (char) n;
1763 acs_chars[m++] = mapped[n];
1767 acs_chars[m++] = extra; /* garbage in, garbage out */