1 /****************************************************************************
2 * Copyright (c) 1998-2015,2016 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.143 2016/10/09 01:30:14 tom Exp $")
44 #define DISCARD(string) string = ABSENT_STRING
45 #define PRINTF (void) printf
47 #define OkIndex(index,array) ((int)(index) >= 0 && (int)(index) < (int) SIZEOF(array))
48 #define TcOutput() (outform == F_TERMCAP || outform == F_TCONVERR)
56 static int tversion; /* terminfo version */
57 static int outform; /* output format to use */
58 static int sortmode; /* sort mode to use */
59 static int width = 60; /* max line width for listings */
60 static int height = 65535; /* max number of lines for listings */
61 static int column; /* current column, limited by 'width' */
62 static int oldcol; /* last value of column before wrap */
63 static bool pretty; /* true if we format if-then-else strings */
64 static bool wrapped; /* true if we wrap too-long strings */
65 static bool checking; /* true if we are checking for tic */
66 static int quickdump; /* true if we are dumping compiled data */
68 static char *save_sgr;
73 /* indirection pointers for implementing sort and display modes */
74 static const PredIdx *bool_indirect, *num_indirect, *str_indirect;
75 static NCURSES_CONST char *const *bool_names;
76 static NCURSES_CONST char *const *num_names;
77 static NCURSES_CONST char *const *str_names;
79 static const char *separator = "", *trailer = "";
80 static int indent = 8;
82 /* cover various ports and variants of terminfo */
83 #define V_ALLCAPS 0 /* all capabilities (SVr4, XSI, ncurses) */
84 #define V_SVR1 1 /* SVR1, Ultrix */
85 #define V_HPUX 2 /* HP/UX */
86 #define V_AIX 3 /* AIX */
87 #define V_BSD 4 /* BSD */
90 #define OBSOLETE(n) (!_nc_user_definable && (n[0] == 'O' && n[1] == 'T'))
92 #define OBSOLETE(n) (n[0] == 'O' && n[1] == 'T')
95 #define isObsolete(f,n) ((f == F_TERMINFO || f == F_VARIABLE) && OBSOLETE(n))
98 #define BoolIndirect(j) ((j >= BOOLCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : bool_indirect[j]))
99 #define NumIndirect(j) ((j >= NUMCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : num_indirect[j]))
100 #define StrIndirect(j) ((j >= STRCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : str_indirect[j]))
102 #define BoolIndirect(j) ((sortmode == S_NOSORT) ? (j) : bool_indirect[j])
103 #define NumIndirect(j) ((sortmode == S_NOSORT) ? (j) : num_indirect[j])
104 #define StrIndirect(j) ((sortmode == S_NOSORT) ? (j) : str_indirect[j])
107 static void failed(const char *) GCC_NORETURN;
110 failed(const char *s)
113 ExitProgram(EXIT_FAILURE);
117 strncpy_DYN(DYNBUF * dst, const char *src, size_t need)
119 size_t want = need + dst->used + 1;
120 if (want > dst->size) {
121 dst->size += (want + 1024); /* be generous */
122 dst->text = typeRealloc(char, dst->size, dst->text);
124 failed("strncpy_DYN");
126 _nc_STRNCPY(dst->text + dst->used, src, need + 1);
128 dst->text[dst->used] = 0;
132 strcpy_DYN(DYNBUF * dst, const char *src)
138 strncpy_DYN(dst, src, strlen(src));
154 _nc_leaks_dump_entry(void)
161 #define NameTrans(check,result) \
162 if ((np->nte_index <= OK_ ## check) \
163 && check[np->nte_index]) \
164 return (result[np->nte_index])
167 nametrans(const char *name)
168 /* translate a capability name to termcap from terminfo */
170 const struct name_table_entry *np;
172 if ((np = _nc_find_entry(name, _nc_get_hash_table(0))) != 0) {
173 switch (np->nte_type) {
175 NameTrans(bool_from_termcap, boolcodes);
179 NameTrans(num_from_termcap, numcodes);
183 NameTrans(str_from_termcap, strcodes);
192 dump_init(const char *version,
202 /* set up for entry display */
207 wrapped = wrap_strings;
209 quickdump = (quick & 3);
213 tversion = V_ALLCAPS;
214 else if (!strcmp(version, "SVr1") || !strcmp(version, "SVR1")
215 || !strcmp(version, "Ultrix"))
217 else if (!strcmp(version, "HP"))
219 else if (!strcmp(version, "AIX"))
221 else if (!strcmp(version, "BSD"))
224 tversion = V_ALLCAPS;
226 /* implement display modes */
227 switch (outform = mode) {
230 bool_names = boolnames;
231 num_names = numnames;
232 str_names = strnames;
233 separator = (twidth > 0 && theight > 1) ? ", " : ",";
238 bool_names = boolfnames;
239 num_names = numfnames;
240 str_names = strfnames;
241 separator = (twidth > 0 && theight > 1) ? ", " : ",";
247 bool_names = boolcodes;
248 num_names = numcodes;
249 str_names = strcodes;
256 /* implement sort modes */
257 switch (sortmode = sort) {
260 (void) fprintf(stderr,
261 "%s: sorting by term structure order\n", _nc_progname);
266 (void) fprintf(stderr,
267 "%s: sorting by terminfo name order\n", _nc_progname);
268 bool_indirect = bool_terminfo_sort;
269 num_indirect = num_terminfo_sort;
270 str_indirect = str_terminfo_sort;
275 (void) fprintf(stderr,
276 "%s: sorting by C variable order\n", _nc_progname);
277 bool_indirect = bool_variable_sort;
278 num_indirect = num_variable_sort;
279 str_indirect = str_variable_sort;
284 (void) fprintf(stderr,
285 "%s: sorting by termcap name order\n", _nc_progname);
286 bool_indirect = bool_termcap_sort;
287 num_indirect = num_termcap_sort;
288 str_indirect = str_termcap_sort;
293 (void) fprintf(stderr,
294 "%s: width = %d, tversion = %d, outform = %d\n",
295 _nc_progname, width, tversion, outform);
298 static TERMTYPE *cur_type;
301 dump_predicate(PredType type, PredIdx idx)
302 /* predicate function to use for ordinary decompilation */
306 return (cur_type->Booleans[idx] == FALSE)
307 ? FAIL : cur_type->Booleans[idx];
310 return (cur_type->Numbers[idx] == ABSENT_NUMERIC)
311 ? FAIL : cur_type->Numbers[idx];
314 return (cur_type->Strings[idx] != ABSENT_STRING)
318 return (FALSE); /* pacify compiler */
321 static void set_obsolete_termcaps(TERMTYPE *tp);
323 /* is this the index of a function key string? */
325 (((i) >= STR_IDX(key_f0) && \
326 (i) <= STR_IDX(key_f9)) || \
327 ((i) >= STR_IDX(key_f11) && \
328 (i) <= STR_IDX(key_f63)))
331 * If we configure with a different Caps file, the offsets into the arrays
332 * will change. So we use an address expression.
334 #define BOOL_IDX(name) (PredType) (&(name) - &(CUR Booleans[0]))
335 #define NUM_IDX(name) (PredType) (&(name) - &(CUR Numbers[0]))
336 #define STR_IDX(name) (PredType) (&(name) - &(CUR Strings[0]))
339 version_filter(PredType type, PredIdx idx)
340 /* filter out capabilities we may want to suppress */
343 case V_ALLCAPS: /* SVr4, XSI Curses */
346 case V_SVR1: /* System V Release 1, Ultrix */
349 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
351 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
353 return ((idx <= STR_IDX(prtr_non)) ? TRUE : FALSE);
357 case V_HPUX: /* Hewlett-Packard */
360 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
362 return ((idx <= NUM_IDX(label_width)) ? TRUE : FALSE);
364 if (idx <= STR_IDX(prtr_non))
366 else if (FNKEY(idx)) /* function keys */
368 else if (idx == STR_IDX(plab_norm)
369 || idx == STR_IDX(label_on)
370 || idx == STR_IDX(label_off))
377 case V_AIX: /* AIX */
380 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
382 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
384 if (idx <= STR_IDX(prtr_non))
386 else if (FNKEY(idx)) /* function keys */
393 #define is_termcap(type) (OkIndex(idx, type##_from_termcap) && \
394 type##_from_termcap[idx])
396 case V_BSD: /* BSD */
399 return is_termcap(bool);
401 return is_termcap(num);
403 return is_termcap(str);
408 return (FALSE); /* pacify the compiler */
414 while (outbuf.used > 0 && outbuf.text[outbuf.used - 1] == ' ')
415 outbuf.text[--outbuf.used] = '\0';
423 strcpy_DYN(&outbuf, trailer);
428 op_length(const char *src, int offset)
432 if (offset > 0 && src[offset - 1] == '\\') {
435 result++; /* for '%' mark */
436 ch = src[offset + result];
440 } else if (ch == '+') {
445 } else if (ch == '\'') {
447 } else if (ch == L_CURL[0]) {
449 while ((ch = src[offset + n]) != '\0') {
450 if (ch == R_CURL[0]) {
456 } else if (strchr("pPg", ch) != 0) {
459 result++; /* ordinary operator */
466 * When wrapping too-long strings, avoid splitting a backslash sequence, or
467 * a terminfo '%' operator. That will leave things a little ragged, but avoids
468 * a stray backslash at the end of the line, as well as making the result a
469 * little more readable.
472 find_split(const char *src, int step, int size)
477 /* check if that would split a backslash-sequence */
479 for (n = size - 1; n > 0; --n) {
480 int ch = UChar(src[step + n]);
482 if (n > 0 && src[step + n - 1] == ch)
486 } else if (!isalnum(ch)) {
493 /* check if that would split a backslash-sequence */
494 for (n = size - 1; n > 0; --n) {
495 int ch = UChar(src[step + n]);
497 int need = op_length(src, step + n);
498 if ((n + need) > size)
512 * If we are going to wrap lines, we cannot leave literal spaces because that
513 * would be ambiguous if we split on that space.
516 fill_spaces(const char *src)
518 const char *fill = "\\s";
519 size_t need = strlen(src);
520 size_t size = strlen(fill);
524 for (pass = 0; pass < 2; ++pass) {
525 for (s = d = 0; src[s] != '\0'; ++s) {
528 strcpy(&result[d], fill);
535 result[d++] = src[s];
544 result = malloc(need + 1);
546 failed("fill_spaces");
553 wrap_concat(const char *src)
555 int need = (int) strlen(src);
556 int gaps = (int) strlen(separator);
557 int want = gaps + need;
560 && column + want > width) {
565 (column + want) > width &&
566 (!TcOutput() || strncmp(src, "..", 2))) {
568 int used = width > 32 ? width : 32;
572 const char *my_t = trailer;
573 char *fill = fill_spaces(src);
575 need = (int) strlen(fill);
580 if ((p = strchr(fill, '=')) != 0) {
581 base = (int) (p + 1 - fill);
584 _nc_SPRINTF(align, _nc_SLIMIT(align) "%*s", base, " ");
588 while ((column + (need + gaps)) > used) {
590 if (size > ((int) strlen(fill) - step)) {
591 size = ((int) strlen(fill) - step);
594 strcpy_DYN(&outbuf, align);
597 size = find_split(fill, step, size);
598 strncpy_DYN(&outbuf, fill + step, (size_t) size);
606 strcpy_DYN(&outbuf, align);
607 strcpy_DYN(&outbuf, fill + step);
609 strcpy_DYN(&outbuf, separator);
615 strcpy_DYN(&outbuf, src);
616 strcpy_DYN(&outbuf, separator);
621 #define IGNORE_SEP_TRAIL(first,last,sep_trail) \
622 if ((size_t)(last - first) > sizeof(sep_trail)-1 \
623 && !strncmp(first, sep_trail, sizeof(sep_trail)-1)) \
624 first += sizeof(sep_trail)-2
626 /* Returns the nominal length of the buffer assuming it is termcap format,
627 * i.e., the continuation sequence is treated as a single character ":".
629 * There are several implementations of termcap which read the text into a
630 * fixed-size buffer. Generally they strip the newlines from the text, but may
631 * not do it until after the buffer is read. Also, "tc=" resolution may be
632 * expanded in the same buffer. This function is useful for measuring the size
633 * of the best fixed-buffer implementation; the worst case may be much worse.
635 #ifdef TEST_TERMCAP_LENGTH
637 termcap_length(const char *src)
639 static const char pattern[] = ":\\\n\t:";
642 const char *const t = src + strlen(src);
644 while (*src != '\0') {
645 IGNORE_SEP_TRAIL(src, t, pattern);
652 #define termcap_length(src) strlen(src)
656 indent_DYN(DYNBUF * buffer, int level)
660 for (n = 0; n < level; n++)
661 strncpy_DYN(buffer, "\t", (size_t) 1);
665 has_params(const char *src)
668 int len = (int) strlen(src);
673 for (n = 0; n < len - 1; ++n) {
674 if (!strncmp(src + n, "%p", (size_t) 2)) {
676 } else if (!strncmp(src + n, "%;", (size_t) 2)) {
683 result = ((len > 50) && params);
689 fmt_complex(TERMTYPE *tterm, const char *capability, char *src, int level)
691 bool percent = FALSE;
692 bool params = has_params(src);
694 while (*src != '\0') {
698 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
702 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
708 case 't': /* "then" */
709 case 'e': /* "else" */
712 tmpbuf.text[tmpbuf.used - 1] = '\n';
713 /* treat a "%e" as else-if, on the same level */
715 indent_DYN(&tmpbuf, level);
716 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
717 strncpy_DYN(&tmpbuf, src, (size_t) 1);
719 params = has_params(src);
720 if (!params && *src != '\0' && *src != '%') {
721 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
722 indent_DYN(&tmpbuf, level + 1);
725 indent_DYN(&tmpbuf, level + 1);
726 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
727 strncpy_DYN(&tmpbuf, src, (size_t) 1);
729 src = fmt_complex(tterm, capability, src, level + 1);
730 if (*src != '\0' && *src != '%') {
731 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
732 indent_DYN(&tmpbuf, level + 1);
734 } else if (level == 1) {
736 _nc_warning("%s: %%%c without %%? in %s",
737 _nc_first_name(tterm->term_names),
744 case ';': /* "endif" */
748 tmpbuf.text[tmpbuf.used - 1] = '\n';
749 indent_DYN(&tmpbuf, level);
750 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
751 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
754 && (strchr("?e;", src[1])) == 0) {
755 tmpbuf.text[tmpbuf.used++] = '\n';
756 indent_DYN(&tmpbuf, level);
761 _nc_warning("%s: %%; without %%? in %s",
762 _nc_first_name(tterm->term_names),
767 if (percent && params) {
768 tmpbuf.text[tmpbuf.used - 1] = '\n';
769 indent_DYN(&tmpbuf, level + 1);
770 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
776 strncpy_DYN(&tmpbuf, "\\s", (size_t) 2);
783 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
788 #define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap)
792 fmt_entry(TERMTYPE *tterm,
795 int suppress_untranslatable,
800 char buffer[MAX_TERMINFO_LENGTH + EXTRA_CAP];
802 NCURSES_CONST char *name;
804 PredIdx num_bools = 0;
805 PredIdx num_values = 0;
806 PredIdx num_strings = 0;
809 #define WRAP_CONCAT \
810 wrap_concat(buffer); \
813 len = 12; /* terminfo file-header */
817 pred = dump_predicate;
820 strcpy_DYN(&outbuf, 0);
822 column = indent; /* FIXME: workaround to prevent empty lines */
824 strcpy_DYN(&outbuf, tterm->term_names);
827 * Colon is legal in terminfo descriptions, but not in termcap.
830 char *p = outbuf.text;
838 strcpy_DYN(&outbuf, separator);
839 column = (int) outbuf.used;
844 for_each_boolean(j, tterm) {
846 name = ExtBoolname(tterm, (int) i, bool_names);
847 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
849 if (!version_filter(BOOLEAN, i))
851 else if (isObsolete(outform, name))
854 predval = pred(BOOLEAN, i);
855 if (predval != FAIL) {
856 _nc_STRCPY(buffer, name, sizeof(buffer));
858 _nc_STRCAT(buffer, "@", sizeof(buffer));
859 else if (i + 1 > num_bools)
865 if (column != indent && height > 1)
868 for_each_number(j, tterm) {
870 name = ExtNumname(tterm, (int) i, num_names);
871 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
873 if (!version_filter(NUMBER, i))
875 else if (isObsolete(outform, name))
878 predval = pred(NUMBER, i);
879 if (predval != FAIL) {
880 if (tterm->Numbers[i] < 0) {
881 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
884 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
885 "%s#%d", name, tterm->Numbers[i]);
886 if (i + 1 > num_values)
893 if (column != indent && height > 1)
896 len += (int) (num_bools
898 + strlen(tterm->term_names) + 1);
904 if (outform == F_TERMCAP) {
905 if (termcap_reset != ABSENT_STRING) {
906 if (init_3string != ABSENT_STRING
907 && !strcmp(init_3string, termcap_reset))
908 DISCARD(init_3string);
910 if (reset_2string != ABSENT_STRING
911 && !strcmp(reset_2string, termcap_reset))
912 DISCARD(reset_2string);
916 for_each_string(j, tterm) {
918 name = ExtStrname(tterm, (int) i, str_names);
919 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
921 capability = tterm->Strings[i];
923 if (!version_filter(STRING, i))
925 else if (isObsolete(outform, name))
930 * Extended names can be longer than 2 characters, but termcap programs
931 * cannot read those (filter them out).
933 if (outform == F_TERMCAP && (strlen(name) > 2))
937 if (outform == F_TERMCAP) {
939 * Some older versions of vi want rmir/smir to be defined
940 * for ich/ich1 to work. If they're not defined, force
941 * them to be output as defined and empty.
943 if (PRESENT(insert_character) || PRESENT(parm_ich)) {
944 if (SAME_CAP(i, enter_insert_mode)
945 && enter_insert_mode == ABSENT_STRING) {
946 _nc_STRCPY(buffer, "im=", sizeof(buffer));
951 if (SAME_CAP(i, exit_insert_mode)
952 && exit_insert_mode == ABSENT_STRING) {
953 _nc_STRCPY(buffer, "ei=", sizeof(buffer));
959 * termcap applications such as screen will be confused if sgr0
960 * is translated to a string containing rmacs. Filter that out.
962 if (PRESENT(exit_attribute_mode)) {
963 if (SAME_CAP(i, exit_attribute_mode)) {
965 char *my_sgr = set_attributes;
967 set_attributes = save_sgr;
969 trimmed_sgr0 = _nc_trim_sgr0(tterm);
970 if (strcmp(capability, trimmed_sgr0))
971 capability = trimmed_sgr0;
973 if (trimmed_sgr0 != exit_attribute_mode)
977 set_attributes = my_sgr;
982 predval = pred(STRING, i);
985 if (predval != FAIL) {
986 if (capability != ABSENT_STRING
987 && i + 1 > num_strings)
990 if (!VALID_STRING(capability)) {
991 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
994 } else if (TcOutput()) {
995 char *srccap = _nc_tic_expand(capability, TRUE, numbers);
996 int params = (((i < (int) SIZEOF(parametrized)) &&
1001 : has_params(srccap)));
1002 char *cv = _nc_infotocap(name, srccap, params);
1005 if (outform == F_TCONVERR) {
1006 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1007 "%s=!!! %s WILL NOT CONVERT !!!",
1009 } else if (suppress_untranslatable) {
1012 char *s = srccap, *d = buffer;
1013 _nc_SPRINTF(d, _nc_SLIMIT(sizeof(buffer)) "..%s=", name);
1015 while ((*d = *s++) != 0) {
1019 } else if (*d == '\\') {
1026 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1029 len += (int) strlen(capability) + 1;
1032 char *src = _nc_tic_expand(capability,
1033 outform == F_TERMINFO, numbers);
1035 strcpy_DYN(&tmpbuf, 0);
1036 strcpy_DYN(&tmpbuf, name);
1037 strcpy_DYN(&tmpbuf, "=");
1039 && (outform == F_TERMINFO
1040 || outform == F_VARIABLE)) {
1041 fmt_complex(tterm, name, src, 1);
1043 strcpy_DYN(&tmpbuf, src);
1045 len += (int) strlen(capability) + 1;
1046 wrap_concat(tmpbuf.text);
1050 /* e.g., trimmed_sgr0 */
1051 if (capability != ABSENT_STRING &&
1052 capability != CANCELLED_STRING &&
1053 capability != tterm->Strings[i])
1056 len += (int) (num_strings * 2);
1059 * This piece of code should be an effective inverse of the functions
1060 * postprocess_terminfo() and postprocess_terminfo() in parse_entry.c.
1061 * Much more work should be done on this to support dumping termcaps.
1063 if (tversion == V_HPUX) {
1064 if (VALID_STRING(memory_lock)) {
1065 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1066 "meml=%s", memory_lock);
1069 if (VALID_STRING(memory_unlock)) {
1070 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1071 "memu=%s", memory_unlock);
1074 } else if (tversion == V_AIX) {
1075 if (VALID_STRING(acs_chars)) {
1077 const char *acstrans = "lqkxjmwuvtn";
1079 char *tp, *sp, boxchars[11];
1082 for (cp = acstrans; *cp; cp++) {
1083 sp = (strchr) (acs_chars, *cp);
1094 char *tmp = _nc_tic_expand(boxchars,
1095 (outform == F_TERMINFO),
1097 _nc_STRCPY(buffer, "box1=", sizeof(buffer));
1098 while (*tmp != '\0') {
1099 size_t have = strlen(buffer);
1100 size_t next = strlen(tmp);
1101 size_t want = have + next + 1;
1106 * If the expanded string is too long for the buffer,
1107 * chop it off and save the location where we chopped it.
1109 if (want >= sizeof(buffer)) {
1113 _nc_STRCAT(buffer, tmp, sizeof(buffer));
1116 * If we chopped the buffer, replace the missing piece and
1117 * shift everything to append the remainder.
1122 while ((tmp[next] = tmp[last + next]) != '\0') {
1135 * kludge: trim off trailer to avoid an extra blank line
1136 * in infocmp -u output when there are no string differences
1139 bool trimmed = FALSE;
1140 j = (PredIdx) outbuf.used;
1142 && outbuf.text[j - 1] == '\t'
1143 && outbuf.text[j - 2] == '\n') {
1147 && outbuf.text[j - 1] == ':'
1148 && outbuf.text[j - 2] == '\t'
1149 && outbuf.text[j - 3] == '\n'
1150 && outbuf.text[j - 4] == '\\') {
1155 outbuf.text[outbuf.used] = '\0';
1157 strcpy_DYN(&outbuf, " ");
1161 fprintf(stderr, "num_bools = %d\n", num_bools);
1162 fprintf(stderr, "num_values = %d\n", num_values);
1163 fprintf(stderr, "num_strings = %d\n", num_strings);
1164 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
1165 tterm->term_names, len, outbuf.used, outbuf.text);
1168 * Here's where we use infodump to trigger a more stringent length check
1169 * for termcap-translation purposes.
1170 * Return the length of the raw entry, without tc= expansions,
1171 * It gives an idea of which entries are deadly to even *scan past*,
1172 * as opposed to *use*.
1174 return (infodump ? len : (int) termcap_length(outbuf.text));
1178 kill_string(TERMTYPE *tterm, char *cap)
1181 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1182 if (cap == tterm->Strings[n]) {
1183 tterm->Strings[n] = ABSENT_STRING;
1191 find_string(TERMTYPE *tterm, char *name)
1194 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1195 if (version_filter(STRING, n)
1196 && !strcmp(name, strnames[n])) {
1197 char *cap = tterm->Strings[n];
1198 if (VALID_STRING(cap)) {
1204 return ABSENT_STRING;
1208 * This is used to remove function-key labels from a termcap entry to
1212 kill_labels(TERMTYPE *tterm, int target)
1219 for (n = 0; n <= 10; ++n) {
1220 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "lf%d", n);
1221 if ((cap = find_string(tterm, name)) != ABSENT_STRING
1222 && kill_string(tterm, cap)) {
1223 target -= (int) (strlen(cap) + 5);
1233 * This is used to remove function-key definitions from a termcap entry to
1237 kill_fkeys(TERMTYPE *tterm, int target)
1244 for (n = 60; n >= 0; --n) {
1245 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n);
1246 if ((cap = find_string(tterm, name)) != ABSENT_STRING
1247 && kill_string(tterm, cap)) {
1248 target -= (int) (strlen(cap) + 5);
1258 * Check if the given acsc string is a 1-1 mapping, i.e., just-like-vt100.
1259 * Also, since this is for termcap, we only care about the line-drawing map.
1261 #define isLine(c) (strchr("lmkjtuvwqxn", c) != 0)
1264 one_one_mapping(const char *mapping)
1268 if (mapping != ABSENT_STRING) {
1270 while (mapping[n] != '\0') {
1271 if (isLine(mapping[n]) &&
1272 mapping[n] != mapping[n + 1]) {
1282 #define FMT_ENTRY() \
1283 fmt_entry(tterm, pred, \
1285 suppress_untranslatable, \
1288 #define SHOW_WHY PRINTF
1291 purged_acs(TERMTYPE *tterm)
1293 bool result = FALSE;
1295 if (VALID_STRING(acs_chars)) {
1296 if (!one_one_mapping(acs_chars)) {
1297 enter_alt_charset_mode = ABSENT_STRING;
1298 exit_alt_charset_mode = ABSENT_STRING;
1299 SHOW_WHY("# (rmacs/smacs removed for consistency)\n");
1307 encode_b64(char *target, char *source, unsigned state, int *saved)
1310 static const char data[] =
1311 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1312 "abcdefghijklmnopqrstuvwxyz"
1314 int ch = UChar(source[state]);
1316 switch (state % 3) {
1318 *target++ = data[ch & 077];
1319 *saved = (ch >> 6) & 3;
1322 *target++ = data[((ch << 2) | *saved) & 077];
1323 *saved = (ch >> 4) & 017;
1326 *target++ = data[((ch << 4) | *saved) & 077];
1327 *target++ = data[(ch >> 2) & 077];
1335 * Dump a single entry.
1338 dump_entry(TERMTYPE *tterm,
1339 int suppress_untranslatable,
1344 TERMTYPE save_tterm;
1352 unsigned offset = 0;
1356 if (_nc_write_object(tterm, bigbuf, &offset, sizeof(bigbuf)) == OK) {
1358 if (quickdump & 1) {
1361 wrap_concat("hex:");
1362 for (n = 0; n < offset; ++n) {
1363 _nc_SPRINTF(numbuf, _nc_SLIMIT(sizeof(numbuf))
1364 "%02X", UChar(bigbuf[n]));
1365 wrap_concat(numbuf);
1368 if (quickdump & 2) {
1372 wrap_concat("b64:");
1373 for (n = 0; n < offset; ++n) {
1374 encode_b64(numbuf, bigbuf, n, &value);
1375 wrap_concat(numbuf);
1393 critlen = MAX_TERMCAP_LENGTH;
1394 legend = "older termcap";
1396 set_obsolete_termcaps(tterm);
1398 critlen = MAX_TERMINFO_LENGTH;
1399 legend = "terminfo";
1403 save_sgr = set_attributes;
1405 if ((FMT_ENTRY() > critlen)
1408 save_tterm = *tterm;
1409 if (!suppress_untranslatable) {
1410 SHOW_WHY("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1412 suppress_untranslatable = TRUE;
1414 if (FMT_ENTRY() > critlen) {
1416 * We pick on sgr because it's a nice long string capability that
1417 * is really just an optimization hack. Another good candidate is
1418 * acsc since it is both long and unused by BSD termcap.
1420 bool changed = FALSE;
1424 * Extended names are most likely function-key definitions. Drop
1428 for (n = STRCOUNT; n < NUM_STRINGS(tterm); n++) {
1429 const char *name = ExtStrname(tterm, (int) n, strnames);
1431 if (VALID_STRING(tterm->Strings[n])) {
1432 set_attributes = ABSENT_STRING;
1433 /* we remove long names anyway - only report the short */
1434 if (strlen(name) <= 2) {
1435 SHOW_WHY("# (%s removed to fit entry within %d bytes)\n",
1440 if (FMT_ENTRY() <= critlen)
1445 if (VALID_STRING(set_attributes)) {
1446 set_attributes = ABSENT_STRING;
1447 SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n",
1451 if (!changed || (FMT_ENTRY() > critlen)) {
1452 if (purged_acs(tterm)) {
1453 acs_chars = ABSENT_STRING;
1454 SHOW_WHY("# (acsc removed to fit entry within %d bytes)\n",
1459 if (!changed || (FMT_ENTRY() > critlen)) {
1460 int oldversion = tversion;
1463 SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1468 && kill_labels(tterm, len - critlen)) {
1469 SHOW_WHY("# (some labels capabilities suppressed to fit entry within %d bytes)\n",
1474 && kill_fkeys(tterm, len - critlen)) {
1475 SHOW_WHY("# (some function-key capabilities suppressed to fit entry within %d bytes)\n",
1479 if (len > critlen) {
1480 (void) fprintf(stderr,
1481 "warning: %s entry is %d bytes long\n",
1482 _nc_first_name(tterm->term_names),
1484 SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
1487 tversion = oldversion;
1489 set_attributes = save_sgr;
1490 *tterm = save_tterm;
1492 } else if (!version_filter(STRING, STR_IDX(acs_chars))) {
1493 save_tterm = *tterm;
1494 if (purged_acs(tterm)) {
1497 *tterm = save_tterm;
1502 dump_uses(const char *name, bool infodump)
1503 /* dump "use=" clauses in the appropriate format */
1505 char buffer[MAX_TERMINFO_LENGTH];
1509 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1510 "%s%s", infodump ? "use=" : "tc=", name);
1511 wrap_concat(buffer);
1518 * Trim any remaining whitespace.
1520 if (outbuf.used != 0) {
1521 bool infodump = !TcOutput();
1522 char delim = (char) (infodump ? ',' : ':');
1525 for (j = (int) outbuf.used - 1; j > 0; --j) {
1526 char ch = outbuf.text[j];
1529 } else if (isspace(UChar(ch))) {
1530 outbuf.used = (size_t) j;
1531 } else if (!infodump && ch == '\\') {
1532 outbuf.used = (size_t) j;
1533 } else if (ch == delim && (j == 0 || outbuf.text[j - 1] != '\\')) {
1534 outbuf.used = (size_t) (j + 1);
1539 outbuf.text[outbuf.used] = '\0';
1541 if (outbuf.text != 0) {
1542 (void) fputs(outbuf.text, stdout);
1545 return (int) outbuf.used;
1549 compare_entry(PredHook hook,
1550 TERMTYPE *tp GCC_UNUSED,
1552 /* compare two entries */
1555 NCURSES_CONST char *name;
1558 fputs(" comparing booleans.\n", stdout);
1559 for_each_boolean(j, tp) {
1560 i = BoolIndirect(j);
1561 name = ExtBoolname(tp, (int) i, bool_names);
1563 if (isObsolete(outform, name))
1566 (*hook) (CMP_BOOLEAN, i, name);
1570 fputs(" comparing numbers.\n", stdout);
1571 for_each_number(j, tp) {
1573 name = ExtNumname(tp, (int) i, num_names);
1575 if (isObsolete(outform, name))
1578 (*hook) (CMP_NUMBER, i, name);
1582 fputs(" comparing strings.\n", stdout);
1583 for_each_string(j, tp) {
1585 name = ExtStrname(tp, (int) i, str_names);
1587 if (isObsolete(outform, name))
1590 (*hook) (CMP_STRING, i, name);
1593 /* (void) fputs(" comparing use entries.\n", stdout); */
1594 (*hook) (CMP_USE, 0, "use");
1598 #define NOTSET(s) ((s) == 0)
1601 * This bit of legerdemain turns all the terminfo variable names into
1602 * references to locations in the arrays Booleans, Numbers, and Strings ---
1603 * precisely what's needed.
1609 set_obsolete_termcaps(TERMTYPE *tp)
1611 #include "capdefaults.c"
1615 * Convert an alternate-character-set string to canonical form: sorted and
1619 repair_acsc(TERMTYPE *tp)
1621 if (VALID_STRING(acs_chars)) {
1627 bool fix_needed = FALSE;
1629 for (n = 0, source = 0; acs_chars[n] != 0; n++) {
1630 target = UChar(acs_chars[n]);
1631 if (source >= target) {
1636 if (acs_chars[n + 1])
1640 memset(mapped, 0, sizeof(mapped));
1641 for (n = 0; acs_chars[n] != 0; n++) {
1642 source = UChar(acs_chars[n]);
1643 if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
1644 mapped[source] = (char) target;
1647 extra = (char) source;
1650 for (n = m = 0; n < sizeof(mapped); n++) {
1652 acs_chars[m++] = (char) n;
1653 acs_chars[m++] = mapped[n];
1657 acs_chars[m++] = extra; /* garbage in, garbage out */