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.148 2017/02/04 16:55:42 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 TERMTYPE *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(TERMTYPE *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)
516 * If we are going to wrap lines, we cannot leave literal spaces because that
517 * would be ambiguous if we split on that space.
520 fill_spaces(const char *src)
522 const char *fill = "\\s";
523 size_t need = strlen(src);
524 size_t size = strlen(fill);
528 for (pass = 0; pass < 2; ++pass) {
529 for (s = d = 0; src[s] != '\0'; ++s) {
532 strcpy(&result[d], fill);
539 result[d++] = src[s];
548 result = malloc(need + 1);
550 failed("fill_spaces");
557 wrap_concat(const char *src)
559 int need = (int) strlen(src);
560 int gaps = (int) strlen(separator);
561 int want = gaps + need;
563 did_wrap = (width <= 0);
565 && column + want > width) {
570 (column + want) > width &&
571 (!TcOutput() || strncmp(src, "..", 2))) {
573 int used = width > WRAPPED ? width : WRAPPED;
577 const char *my_t = trailer;
578 char *fill = fill_spaces(src);
579 int last = (int) strlen(fill);
586 if ((p = strchr(fill, '=')) != 0) {
587 base = (int) (p + 1 - fill);
590 _nc_SPRINTF(align, _nc_SLIMIT(align) "%*s", base, " ");
594 /* "pretty" overrides wrapping if it already split the line */
595 if (!pretty || strchr(fill, '\n') == 0) {
596 while ((column + (need + gaps)) > used) {
599 strcpy_DYN(&outbuf, align);
602 if (size > (last - step)) {
603 size = (last - step);
605 size = find_split(fill, step, size);
606 strncpy_DYN(&outbuf, fill + step, (size_t) size);
617 strcpy_DYN(&outbuf, align);
618 strcpy_DYN(&outbuf, fill + step);
620 strcpy_DYN(&outbuf, separator);
626 strcpy_DYN(&outbuf, src);
627 strcpy_DYN(&outbuf, separator);
632 #define IGNORE_SEP_TRAIL(first,last,sep_trail) \
633 if ((size_t)(last - first) > sizeof(sep_trail)-1 \
634 && !strncmp(first, sep_trail, sizeof(sep_trail)-1)) \
635 first += sizeof(sep_trail)-2
637 /* Returns the nominal length of the buffer assuming it is termcap format,
638 * i.e., the continuation sequence is treated as a single character ":".
640 * There are several implementations of termcap which read the text into a
641 * fixed-size buffer. Generally they strip the newlines from the text, but may
642 * not do it until after the buffer is read. Also, "tc=" resolution may be
643 * expanded in the same buffer. This function is useful for measuring the size
644 * of the best fixed-buffer implementation; the worst case may be much worse.
646 #ifdef TEST_TERMCAP_LENGTH
648 termcap_length(const char *src)
650 static const char pattern[] = ":\\\n\t:";
653 const char *const t = src + strlen(src);
655 while (*src != '\0') {
656 IGNORE_SEP_TRAIL(src, t, pattern);
663 #define termcap_length(src) strlen(src)
667 indent_DYN(DYNBUF * buffer, int level)
671 for (n = 0; n < level; n++)
672 strncpy_DYN(buffer, "\t", (size_t) 1);
676 has_params(const char *src)
679 int len = (int) strlen(src);
684 for (n = 0; n < len - 1; ++n) {
685 if (!strncmp(src + n, "%p", (size_t) 2)) {
687 } else if (!strncmp(src + n, "%;", (size_t) 2)) {
694 result = ((len > 50) && params);
700 fmt_complex(TERMTYPE *tterm, const char *capability, char *src, int level)
702 bool percent = FALSE;
703 bool params = has_params(src);
705 while (*src != '\0') {
709 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
713 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
719 case 't': /* "then" */
720 case 'e': /* "else" */
723 tmpbuf.text[tmpbuf.used - 1] = '\n';
724 /* treat a "%e" as else-if, on the same level */
726 indent_DYN(&tmpbuf, level);
727 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
728 strncpy_DYN(&tmpbuf, src, (size_t) 1);
730 params = has_params(src);
731 if (!params && *src != '\0' && *src != '%') {
732 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
733 indent_DYN(&tmpbuf, level + 1);
736 indent_DYN(&tmpbuf, level + 1);
737 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
738 strncpy_DYN(&tmpbuf, src, (size_t) 1);
740 src = fmt_complex(tterm, capability, src, level + 1);
741 if (*src != '\0' && *src != '%') {
742 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
743 indent_DYN(&tmpbuf, level + 1);
745 } else if (level == 1) {
747 _nc_warning("%s: %%%c without %%? in %s",
748 _nc_first_name(tterm->term_names),
755 case ';': /* "endif" */
759 tmpbuf.text[tmpbuf.used - 1] = '\n';
760 indent_DYN(&tmpbuf, level);
761 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
762 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
765 && (strchr("?e;", src[1])) == 0) {
766 tmpbuf.text[tmpbuf.used++] = '\n';
767 indent_DYN(&tmpbuf, level);
772 _nc_warning("%s: %%; without %%? in %s",
773 _nc_first_name(tterm->term_names),
778 if (percent && params) {
779 tmpbuf.text[tmpbuf.used - 1] = '\n';
780 indent_DYN(&tmpbuf, level + 1);
781 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
787 strncpy_DYN(&tmpbuf, "\\s", (size_t) 2);
794 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
799 #define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap)
803 fmt_entry(TERMTYPE *tterm,
806 int suppress_untranslatable,
811 char buffer[MAX_TERMINFO_LENGTH + EXTRA_CAP];
813 NCURSES_CONST char *name;
815 PredIdx num_bools = 0;
816 PredIdx num_values = 0;
817 PredIdx num_strings = 0;
820 #define WRAP_CONCAT \
821 wrap_concat(buffer); \
824 len = 12; /* terminfo file-header */
828 pred = dump_predicate;
831 strcpy_DYN(&outbuf, 0);
833 column = indent; /* FIXME: workaround to prevent empty lines */
835 strcpy_DYN(&outbuf, tterm->term_names);
838 * Colon is legal in terminfo descriptions, but not in termcap.
841 char *p = outbuf.text;
849 strcpy_DYN(&outbuf, separator);
850 column = (int) outbuf.used;
855 for_each_boolean(j, tterm) {
857 name = ExtBoolname(tterm, (int) i, bool_names);
858 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
860 if (!version_filter(BOOLEAN, i))
862 else if (isObsolete(outform, name))
865 predval = pred(BOOLEAN, i);
866 if (predval != FAIL) {
867 _nc_STRCPY(buffer, name, sizeof(buffer));
869 _nc_STRCAT(buffer, "@", sizeof(buffer));
870 else if (i + 1 > num_bools)
876 if (column != indent && height > 1)
879 for_each_number(j, tterm) {
881 name = ExtNumname(tterm, (int) i, num_names);
882 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
884 if (!version_filter(NUMBER, i))
886 else if (isObsolete(outform, name))
889 predval = pred(NUMBER, i);
890 if (predval != FAIL) {
891 if (tterm->Numbers[i] < 0) {
892 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
895 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
896 "%s#%d", name, tterm->Numbers[i]);
897 if (i + 1 > num_values)
904 if (column != indent && height > 1)
907 len += (int) (num_bools
909 + strlen(tterm->term_names) + 1);
915 if (outform == F_TERMCAP) {
916 if (termcap_reset != ABSENT_STRING) {
917 if (init_3string != ABSENT_STRING
918 && !strcmp(init_3string, termcap_reset))
919 DISCARD(init_3string);
921 if (reset_2string != ABSENT_STRING
922 && !strcmp(reset_2string, termcap_reset))
923 DISCARD(reset_2string);
927 for_each_string(j, tterm) {
929 name = ExtStrname(tterm, (int) i, str_names);
930 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
932 capability = tterm->Strings[i];
934 if (!version_filter(STRING, i))
936 else if (isObsolete(outform, name))
941 * Extended names can be longer than 2 characters, but termcap programs
942 * cannot read those (filter them out).
944 if (outform == F_TERMCAP && (strlen(name) > 2))
948 if (outform == F_TERMCAP) {
950 * Some older versions of vi want rmir/smir to be defined
951 * for ich/ich1 to work. If they're not defined, force
952 * them to be output as defined and empty.
954 if (PRESENT(insert_character) || PRESENT(parm_ich)) {
955 if (SAME_CAP(i, enter_insert_mode)
956 && enter_insert_mode == ABSENT_STRING) {
957 _nc_STRCPY(buffer, "im=", sizeof(buffer));
962 if (SAME_CAP(i, exit_insert_mode)
963 && exit_insert_mode == ABSENT_STRING) {
964 _nc_STRCPY(buffer, "ei=", sizeof(buffer));
970 * termcap applications such as screen will be confused if sgr0
971 * is translated to a string containing rmacs. Filter that out.
973 if (PRESENT(exit_attribute_mode)) {
974 if (SAME_CAP(i, exit_attribute_mode)) {
976 char *my_sgr = set_attributes;
978 set_attributes = save_sgr;
980 trimmed_sgr0 = _nc_trim_sgr0(tterm);
981 if (strcmp(capability, trimmed_sgr0))
982 capability = trimmed_sgr0;
984 if (trimmed_sgr0 != exit_attribute_mode)
988 set_attributes = my_sgr;
993 predval = pred(STRING, i);
996 if (predval != FAIL) {
997 if (capability != ABSENT_STRING
998 && i + 1 > num_strings)
1001 if (!VALID_STRING(capability)) {
1002 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1005 } else if (TcOutput()) {
1006 char *srccap = _nc_tic_expand(capability, TRUE, numbers);
1007 int params = (((i < (int) SIZEOF(parametrized)) &&
1012 : has_params(srccap)));
1013 char *cv = _nc_infotocap(name, srccap, params);
1016 if (outform == F_TCONVERR) {
1017 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1018 "%s=!!! %s WILL NOT CONVERT !!!",
1020 } else if (suppress_untranslatable) {
1023 char *s = srccap, *d = buffer;
1024 _nc_SPRINTF(d, _nc_SLIMIT(sizeof(buffer)) "..%s=", name);
1026 while ((*d = *s++) != 0) {
1030 } else if (*d == '\\') {
1037 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1040 len += (int) strlen(capability) + 1;
1043 char *src = _nc_tic_expand(capability,
1044 outform == F_TERMINFO, numbers);
1046 strcpy_DYN(&tmpbuf, 0);
1047 strcpy_DYN(&tmpbuf, name);
1048 strcpy_DYN(&tmpbuf, "=");
1050 && (outform == F_TERMINFO
1051 || outform == F_VARIABLE)) {
1052 fmt_complex(tterm, name, src, 1);
1054 strcpy_DYN(&tmpbuf, src);
1056 len += (int) strlen(capability) + 1;
1057 wrap_concat(tmpbuf.text);
1061 /* e.g., trimmed_sgr0 */
1062 if (capability != ABSENT_STRING &&
1063 capability != CANCELLED_STRING &&
1064 capability != tterm->Strings[i])
1067 len += (int) (num_strings * 2);
1070 * This piece of code should be an effective inverse of the functions
1071 * postprocess_terminfo() and postprocess_terminfo() in parse_entry.c.
1072 * Much more work should be done on this to support dumping termcaps.
1074 if (tversion == V_HPUX) {
1075 if (VALID_STRING(memory_lock)) {
1076 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1077 "meml=%s", memory_lock);
1080 if (VALID_STRING(memory_unlock)) {
1081 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1082 "memu=%s", memory_unlock);
1085 } else if (tversion == V_AIX) {
1086 if (VALID_STRING(acs_chars)) {
1088 const char *acstrans = "lqkxjmwuvtn";
1090 char *tp, *sp, boxchars[11];
1093 for (cp = acstrans; *cp; cp++) {
1094 sp = (strchr) (acs_chars, *cp);
1105 char *tmp = _nc_tic_expand(boxchars,
1106 (outform == F_TERMINFO),
1108 _nc_STRCPY(buffer, "box1=", sizeof(buffer));
1109 while (*tmp != '\0') {
1110 size_t have = strlen(buffer);
1111 size_t next = strlen(tmp);
1112 size_t want = have + next + 1;
1117 * If the expanded string is too long for the buffer,
1118 * chop it off and save the location where we chopped it.
1120 if (want >= sizeof(buffer)) {
1124 _nc_STRCAT(buffer, tmp, sizeof(buffer));
1127 * If we chopped the buffer, replace the missing piece and
1128 * shift everything to append the remainder.
1133 while ((tmp[next] = tmp[last + next]) != '\0') {
1146 * kludge: trim off trailer to avoid an extra blank line
1147 * in infocmp -u output when there are no string differences
1150 bool trimmed = FALSE;
1151 j = (PredIdx) outbuf.used;
1152 if (wrapped && did_wrap) {
1155 && outbuf.text[j - 1] == '\t'
1156 && outbuf.text[j - 2] == '\n') {
1160 && outbuf.text[j - 1] == ':'
1161 && outbuf.text[j - 2] == '\t'
1162 && outbuf.text[j - 3] == '\n'
1163 && outbuf.text[j - 4] == '\\') {
1168 outbuf.text[outbuf.used] = '\0';
1170 strcpy_DYN(&outbuf, " ");
1174 fprintf(stderr, "num_bools = %d\n", num_bools);
1175 fprintf(stderr, "num_values = %d\n", num_values);
1176 fprintf(stderr, "num_strings = %d\n", num_strings);
1177 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
1178 tterm->term_names, len, outbuf.used, outbuf.text);
1181 * Here's where we use infodump to trigger a more stringent length check
1182 * for termcap-translation purposes.
1183 * Return the length of the raw entry, without tc= expansions,
1184 * It gives an idea of which entries are deadly to even *scan past*,
1185 * as opposed to *use*.
1187 return (infodump ? len : (int) termcap_length(outbuf.text));
1191 kill_string(TERMTYPE *tterm, char *cap)
1194 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1195 if (cap == tterm->Strings[n]) {
1196 tterm->Strings[n] = ABSENT_STRING;
1204 find_string(TERMTYPE *tterm, char *name)
1207 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1208 if (version_filter(STRING, n)
1209 && !strcmp(name, strnames[n])) {
1210 char *cap = tterm->Strings[n];
1211 if (VALID_STRING(cap)) {
1217 return ABSENT_STRING;
1221 * This is used to remove function-key labels from a termcap entry to
1225 kill_labels(TERMTYPE *tterm, int target)
1232 for (n = 0; n <= 10; ++n) {
1233 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "lf%d", n);
1234 if ((cap = find_string(tterm, name)) != ABSENT_STRING
1235 && kill_string(tterm, cap)) {
1236 target -= (int) (strlen(cap) + 5);
1246 * This is used to remove function-key definitions from a termcap entry to
1250 kill_fkeys(TERMTYPE *tterm, int target)
1257 for (n = 60; n >= 0; --n) {
1258 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n);
1259 if ((cap = find_string(tterm, name)) != ABSENT_STRING
1260 && kill_string(tterm, cap)) {
1261 target -= (int) (strlen(cap) + 5);
1271 * Check if the given acsc string is a 1-1 mapping, i.e., just-like-vt100.
1272 * Also, since this is for termcap, we only care about the line-drawing map.
1274 #define isLine(c) (strchr("lmkjtuvwqxn", c) != 0)
1277 one_one_mapping(const char *mapping)
1281 if (mapping != ABSENT_STRING) {
1283 while (mapping[n] != '\0') {
1284 if (isLine(mapping[n]) &&
1285 mapping[n] != mapping[n + 1]) {
1295 #define FMT_ENTRY() \
1296 fmt_entry(tterm, pred, \
1298 suppress_untranslatable, \
1301 #define SHOW_WHY PRINTF
1304 purged_acs(TERMTYPE *tterm)
1306 bool result = FALSE;
1308 if (VALID_STRING(acs_chars)) {
1309 if (!one_one_mapping(acs_chars)) {
1310 enter_alt_charset_mode = ABSENT_STRING;
1311 exit_alt_charset_mode = ABSENT_STRING;
1312 SHOW_WHY("# (rmacs/smacs removed for consistency)\n");
1320 encode_b64(char *target, char *source, unsigned state, int *saved)
1323 static const char data[] =
1324 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1325 "abcdefghijklmnopqrstuvwxyz"
1327 int ch = UChar(source[state]);
1329 switch (state % 3) {
1331 *target++ = data[(ch >> 2) & 077];
1335 *target++ = data[((ch >> 4) | *saved) & 077];
1339 *target++ = data[((ch >> 6) | *saved) & 077];
1340 *target++ = data[ch & 077];
1348 * Dump a single entry.
1351 dump_entry(TERMTYPE *tterm,
1352 int suppress_untranslatable,
1357 TERMTYPE save_tterm;
1365 unsigned offset = 0;
1369 if (_nc_write_object(tterm, bigbuf, &offset, sizeof(bigbuf)) == OK) {
1371 if (quickdump & 1) {
1374 wrap_concat("hex:");
1375 for (n = 0; n < offset; ++n) {
1376 _nc_SPRINTF(numbuf, _nc_SLIMIT(sizeof(numbuf))
1377 "%02X", UChar(bigbuf[n]));
1378 wrap_concat(numbuf);
1381 if (quickdump & 2) {
1382 static char padding[] =
1387 wrap_concat("b64:");
1388 for (n = 0; n < offset; ++n) {
1389 encode_b64(numbuf, bigbuf, n, &value);
1390 wrap_concat(numbuf);
1396 encode_b64(numbuf, padding, 1, &value);
1397 wrap_concat(numbuf);
1401 encode_b64(numbuf, padding, 1, &value);
1402 wrap_concat(numbuf);
1412 critlen = MAX_TERMCAP_LENGTH;
1413 legend = "older termcap";
1415 set_obsolete_termcaps(tterm);
1417 critlen = MAX_TERMINFO_LENGTH;
1418 legend = "terminfo";
1422 save_sgr = set_attributes;
1424 if ((FMT_ENTRY() > critlen)
1427 save_tterm = *tterm;
1428 if (!suppress_untranslatable) {
1429 SHOW_WHY("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1431 suppress_untranslatable = TRUE;
1433 if (FMT_ENTRY() > critlen) {
1435 * We pick on sgr because it's a nice long string capability that
1436 * is really just an optimization hack. Another good candidate is
1437 * acsc since it is both long and unused by BSD termcap.
1439 bool changed = FALSE;
1443 * Extended names are most likely function-key definitions. Drop
1447 for (n = STRCOUNT; n < NUM_STRINGS(tterm); n++) {
1448 const char *name = ExtStrname(tterm, (int) n, strnames);
1450 if (VALID_STRING(tterm->Strings[n])) {
1451 set_attributes = ABSENT_STRING;
1452 /* we remove long names anyway - only report the short */
1453 if (strlen(name) <= 2) {
1454 SHOW_WHY("# (%s removed to fit entry within %d bytes)\n",
1459 if (FMT_ENTRY() <= critlen)
1464 if (VALID_STRING(set_attributes)) {
1465 set_attributes = ABSENT_STRING;
1466 SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n",
1470 if (!changed || (FMT_ENTRY() > critlen)) {
1471 if (purged_acs(tterm)) {
1472 acs_chars = ABSENT_STRING;
1473 SHOW_WHY("# (acsc removed to fit entry within %d bytes)\n",
1478 if (!changed || (FMT_ENTRY() > critlen)) {
1479 int oldversion = tversion;
1482 SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1487 && kill_labels(tterm, len - critlen)) {
1488 SHOW_WHY("# (some labels capabilities suppressed to fit entry within %d bytes)\n",
1493 && kill_fkeys(tterm, len - critlen)) {
1494 SHOW_WHY("# (some function-key capabilities suppressed to fit entry within %d bytes)\n",
1498 if (len > critlen) {
1499 (void) fprintf(stderr,
1500 "warning: %s entry is %d bytes long\n",
1501 _nc_first_name(tterm->term_names),
1503 SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
1506 tversion = oldversion;
1508 set_attributes = save_sgr;
1509 *tterm = save_tterm;
1511 } else if (!version_filter(STRING, STR_IDX(acs_chars))) {
1512 save_tterm = *tterm;
1513 if (purged_acs(tterm)) {
1516 *tterm = save_tterm;
1521 dump_uses(const char *name, bool infodump)
1522 /* dump "use=" clauses in the appropriate format */
1524 char buffer[MAX_TERMINFO_LENGTH];
1528 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1529 "%s%s", infodump ? "use=" : "tc=", name);
1530 wrap_concat(buffer);
1537 * Trim any remaining whitespace.
1539 if (outbuf.used != 0) {
1540 bool infodump = !TcOutput();
1541 char delim = (char) (infodump ? ',' : ':');
1544 for (j = (int) outbuf.used - 1; j > 0; --j) {
1545 char ch = outbuf.text[j];
1548 } else if (isspace(UChar(ch))) {
1549 outbuf.used = (size_t) j;
1550 } else if (!infodump && ch == '\\') {
1551 outbuf.used = (size_t) j;
1552 } else if (ch == delim && (j == 0 || outbuf.text[j - 1] != '\\')) {
1553 outbuf.used = (size_t) (j + 1);
1558 outbuf.text[outbuf.used] = '\0';
1560 if (outbuf.text != 0) {
1561 (void) fputs(outbuf.text, stdout);
1564 return (int) outbuf.used;
1568 compare_entry(PredHook hook,
1569 TERMTYPE *tp GCC_UNUSED,
1571 /* compare two entries */
1574 NCURSES_CONST char *name;
1577 fputs(" comparing booleans.\n", stdout);
1578 for_each_boolean(j, tp) {
1579 i = BoolIndirect(j);
1580 name = ExtBoolname(tp, (int) i, bool_names);
1582 if (isObsolete(outform, name))
1585 (*hook) (CMP_BOOLEAN, i, name);
1589 fputs(" comparing numbers.\n", stdout);
1590 for_each_number(j, tp) {
1592 name = ExtNumname(tp, (int) i, num_names);
1594 if (isObsolete(outform, name))
1597 (*hook) (CMP_NUMBER, i, name);
1601 fputs(" comparing strings.\n", stdout);
1602 for_each_string(j, tp) {
1604 name = ExtStrname(tp, (int) i, str_names);
1606 if (isObsolete(outform, name))
1609 (*hook) (CMP_STRING, i, name);
1612 /* (void) fputs(" comparing use entries.\n", stdout); */
1613 (*hook) (CMP_USE, 0, "use");
1617 #define NOTSET(s) ((s) == 0)
1620 * This bit of legerdemain turns all the terminfo variable names into
1621 * references to locations in the arrays Booleans, Numbers, and Strings ---
1622 * precisely what's needed.
1628 set_obsolete_termcaps(TERMTYPE *tp)
1630 #include "capdefaults.c"
1634 * Convert an alternate-character-set string to canonical form: sorted and
1638 repair_acsc(TERMTYPE *tp)
1640 if (VALID_STRING(acs_chars)) {
1646 bool fix_needed = FALSE;
1648 for (n = 0, source = 0; acs_chars[n] != 0; n++) {
1649 target = UChar(acs_chars[n]);
1650 if (source >= target) {
1655 if (acs_chars[n + 1])
1659 memset(mapped, 0, sizeof(mapped));
1660 for (n = 0; acs_chars[n] != 0; n++) {
1661 source = UChar(acs_chars[n]);
1662 if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
1663 mapped[source] = (char) target;
1666 extra = (char) source;
1669 for (n = m = 0; n < sizeof(mapped); n++) {
1671 acs_chars[m++] = (char) n;
1672 acs_chars[m++] = mapped[n];
1676 acs_chars[m++] = extra; /* garbage in, garbage out */