1 /****************************************************************************
2 * Copyright (c) 1998-2014,2015 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.117 2015/07/05 00:01:04 tom Exp $")
45 #define DISCARD(string) string = ABSENT_STRING
46 #define PRINTF (void) printf
48 #define OkIndex(index,array) ((int)(index) >= 0 && (int)(index) < (int) SIZEOF(array))
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 checking; /* true if we are checking for tic */
66 static char *save_sgr;
71 /* indirection pointers for implementing sort and display modes */
72 static const PredIdx *bool_indirect, *num_indirect, *str_indirect;
73 static NCURSES_CONST char *const *bool_names;
74 static NCURSES_CONST char *const *num_names;
75 static NCURSES_CONST char *const *str_names;
77 static const char *separator = "", *trailer = "";
79 /* cover various ports and variants of terminfo */
80 #define V_ALLCAPS 0 /* all capabilities (SVr4, XSI, ncurses) */
81 #define V_SVR1 1 /* SVR1, Ultrix */
82 #define V_HPUX 2 /* HP/UX */
83 #define V_AIX 3 /* AIX */
84 #define V_BSD 4 /* BSD */
87 #define OBSOLETE(n) (!_nc_user_definable && (n[0] == 'O' && n[1] == 'T'))
89 #define OBSOLETE(n) (n[0] == 'O' && n[1] == 'T')
92 #define isObsolete(f,n) ((f == F_TERMINFO || f == F_VARIABLE) && OBSOLETE(n))
95 #define BoolIndirect(j) ((j >= BOOLCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : bool_indirect[j]))
96 #define NumIndirect(j) ((j >= NUMCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : num_indirect[j]))
97 #define StrIndirect(j) ((j >= STRCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : str_indirect[j]))
99 #define BoolIndirect(j) ((sortmode == S_NOSORT) ? (j) : bool_indirect[j])
100 #define NumIndirect(j) ((sortmode == S_NOSORT) ? (j) : num_indirect[j])
101 #define StrIndirect(j) ((sortmode == S_NOSORT) ? (j) : str_indirect[j])
104 static void failed(const char *) GCC_NORETURN;
107 failed(const char *s)
110 ExitProgram(EXIT_FAILURE);
114 strncpy_DYN(DYNBUF * dst, const char *src, size_t need)
116 size_t want = need + dst->used + 1;
117 if (want > dst->size) {
118 dst->size += (want + 1024); /* be generous */
119 dst->text = typeRealloc(char, dst->size, dst->text);
121 failed("strncpy_DYN");
123 (void) strncpy(dst->text + dst->used, src, need);
125 dst->text[dst->used] = 0;
129 strcpy_DYN(DYNBUF * dst, const char *src)
135 strncpy_DYN(dst, src, strlen(src));
151 _nc_leaks_dump_entry(void)
158 #define NameTrans(check,result) \
159 if ((np->nte_index <= OK_ ## check) \
160 && check[np->nte_index]) \
161 return (result[np->nte_index])
164 nametrans(const char *name)
165 /* translate a capability name to termcap from terminfo */
167 const struct name_table_entry *np;
169 if ((np = _nc_find_entry(name, _nc_get_hash_table(0))) != 0) {
170 switch (np->nte_type) {
172 NameTrans(bool_from_termcap, boolcodes);
176 NameTrans(num_from_termcap, numcodes);
180 NameTrans(str_from_termcap, strcodes);
189 dump_init(const char *version,
197 /* set up for entry display */
206 tversion = V_ALLCAPS;
207 else if (!strcmp(version, "SVr1") || !strcmp(version, "SVR1")
208 || !strcmp(version, "Ultrix"))
210 else if (!strcmp(version, "HP"))
212 else if (!strcmp(version, "AIX"))
214 else if (!strcmp(version, "BSD"))
217 tversion = V_ALLCAPS;
219 /* implement display modes */
220 switch (outform = mode) {
223 bool_names = boolnames;
224 num_names = numnames;
225 str_names = strnames;
226 separator = (twidth > 0 && theight > 1) ? ", " : ",";
231 bool_names = boolfnames;
232 num_names = numfnames;
233 str_names = strfnames;
234 separator = (twidth > 0 && theight > 1) ? ", " : ",";
240 bool_names = boolcodes;
241 num_names = numcodes;
242 str_names = strcodes;
248 /* implement sort modes */
249 switch (sortmode = sort) {
252 (void) fprintf(stderr,
253 "%s: sorting by term structure order\n", _nc_progname);
258 (void) fprintf(stderr,
259 "%s: sorting by terminfo name order\n", _nc_progname);
260 bool_indirect = bool_terminfo_sort;
261 num_indirect = num_terminfo_sort;
262 str_indirect = str_terminfo_sort;
267 (void) fprintf(stderr,
268 "%s: sorting by C variable order\n", _nc_progname);
269 bool_indirect = bool_variable_sort;
270 num_indirect = num_variable_sort;
271 str_indirect = str_variable_sort;
276 (void) fprintf(stderr,
277 "%s: sorting by termcap name order\n", _nc_progname);
278 bool_indirect = bool_termcap_sort;
279 num_indirect = num_termcap_sort;
280 str_indirect = str_termcap_sort;
285 (void) fprintf(stderr,
286 "%s: width = %d, tversion = %d, outform = %d\n",
287 _nc_progname, width, tversion, outform);
290 static TERMTYPE *cur_type;
293 dump_predicate(PredType type, PredIdx idx)
294 /* predicate function to use for ordinary decompilation */
298 return (cur_type->Booleans[idx] == FALSE)
299 ? FAIL : cur_type->Booleans[idx];
302 return (cur_type->Numbers[idx] == ABSENT_NUMERIC)
303 ? FAIL : cur_type->Numbers[idx];
306 return (cur_type->Strings[idx] != ABSENT_STRING)
310 return (FALSE); /* pacify compiler */
313 static void set_obsolete_termcaps(TERMTYPE *tp);
315 /* is this the index of a function key string? */
317 (((i) >= STR_IDX(key_f0) && \
318 (i) <= STR_IDX(key_f9)) || \
319 ((i) >= STR_IDX(key_f11) && \
320 (i) <= STR_IDX(key_f63)))
323 * If we configure with a different Caps file, the offsets into the arrays
324 * will change. So we use an address expression.
326 #define BOOL_IDX(name) (PredType) (&(name) - &(CUR Booleans[0]))
327 #define NUM_IDX(name) (PredType) (&(name) - &(CUR Numbers[0]))
328 #define STR_IDX(name) (PredType) (&(name) - &(CUR Strings[0]))
331 version_filter(PredType type, PredIdx idx)
332 /* filter out capabilities we may want to suppress */
335 case V_ALLCAPS: /* SVr4, XSI Curses */
338 case V_SVR1: /* System V Release 1, Ultrix */
341 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
343 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
345 return ((idx <= STR_IDX(prtr_non)) ? TRUE : FALSE);
349 case V_HPUX: /* Hewlett-Packard */
352 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
354 return ((idx <= NUM_IDX(label_width)) ? TRUE : FALSE);
356 if (idx <= STR_IDX(prtr_non))
358 else if (FNKEY(idx)) /* function keys */
360 else if (idx == STR_IDX(plab_norm)
361 || idx == STR_IDX(label_on)
362 || idx == STR_IDX(label_off))
369 case V_AIX: /* AIX */
372 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
374 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
376 if (idx <= STR_IDX(prtr_non))
378 else if (FNKEY(idx)) /* function keys */
385 #define is_termcap(type) (OkIndex(idx, type##_from_termcap) && \
386 type##_from_termcap[idx])
388 case V_BSD: /* BSD */
391 return is_termcap(bool);
393 return is_termcap(num);
395 return is_termcap(str);
400 return (FALSE); /* pacify the compiler */
406 while (outbuf.used > 0 && outbuf.text[outbuf.used - 1] == ' ')
407 outbuf.text[--outbuf.used] = '\0';
415 strcpy_DYN(&outbuf, trailer);
420 wrap_concat(const char *src)
422 size_t need = strlen(src);
423 size_t want = strlen(separator) + need;
426 && column + (int) want > width) {
429 strcpy_DYN(&outbuf, src);
430 strcpy_DYN(&outbuf, separator);
431 column += (int) need;
434 #define IGNORE_SEP_TRAIL(first,last,sep_trail) \
435 if ((size_t)(last - first) > sizeof(sep_trail)-1 \
436 && !strncmp(first, sep_trail, sizeof(sep_trail)-1)) \
437 first += sizeof(sep_trail)-2
439 /* Returns the nominal length of the buffer assuming it is termcap format,
440 * i.e., the continuation sequence is treated as a single character ":".
442 * There are several implementations of termcap which read the text into a
443 * fixed-size buffer. Generally they strip the newlines from the text, but may
444 * not do it until after the buffer is read. Also, "tc=" resolution may be
445 * expanded in the same buffer. This function is useful for measuring the size
446 * of the best fixed-buffer implementation; the worst case may be much worse.
448 #ifdef TEST_TERMCAP_LENGTH
450 termcap_length(const char *src)
452 static const char pattern[] = ":\\\n\t:";
455 const char *const t = src + strlen(src);
457 while (*src != '\0') {
458 IGNORE_SEP_TRAIL(src, t, pattern);
465 #define termcap_length(src) strlen(src)
469 indent_DYN(DYNBUF * buffer, int level)
473 for (n = 0; n < level; n++)
474 strncpy_DYN(buffer, "\t", (size_t) 1);
478 has_params(const char *src)
481 int len = (int) strlen(src);
486 for (n = 0; n < len - 1; ++n) {
487 if (!strncmp(src + n, "%p", (size_t) 2)) {
489 } else if (!strncmp(src + n, "%;", (size_t) 2)) {
496 result = ((len > 50) && params);
502 fmt_complex(TERMTYPE *tterm, const char *capability, char *src, int level)
504 bool percent = FALSE;
505 bool params = has_params(src);
507 while (*src != '\0') {
511 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
517 case 't': /* "then" */
518 case 'e': /* "else" */
521 tmpbuf.text[tmpbuf.used - 1] = '\n';
522 /* treat a "%e" as else-if, on the same level */
524 indent_DYN(&tmpbuf, level);
525 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
526 strncpy_DYN(&tmpbuf, src, (size_t) 1);
528 params = has_params(src);
529 if (!params && *src != '\0' && *src != '%') {
530 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
531 indent_DYN(&tmpbuf, level + 1);
534 indent_DYN(&tmpbuf, level + 1);
535 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
536 strncpy_DYN(&tmpbuf, src, (size_t) 1);
538 src = fmt_complex(tterm, capability, src, level + 1);
539 if (*src != '\0' && *src != '%') {
540 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
541 indent_DYN(&tmpbuf, level + 1);
543 } else if (level == 1) {
545 _nc_warning("%s: %%%c without %%? in %s",
546 _nc_first_name(tterm->term_names),
553 case ';': /* "endif" */
557 tmpbuf.text[tmpbuf.used - 1] = '\n';
558 indent_DYN(&tmpbuf, level);
559 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
560 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
563 && (strchr("?e;", src[1])) == 0) {
564 tmpbuf.text[tmpbuf.used++] = '\n';
565 indent_DYN(&tmpbuf, level);
570 _nc_warning("%s: %%; without %%? in %s",
571 _nc_first_name(tterm->term_names),
576 if (percent && params) {
577 tmpbuf.text[tmpbuf.used - 1] = '\n';
578 indent_DYN(&tmpbuf, level + 1);
579 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
585 strncpy_DYN(&tmpbuf, "\\s", (size_t) 2);
592 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
597 #define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap)
601 fmt_entry(TERMTYPE *tterm,
604 int suppress_untranslatable,
609 char buffer[MAX_TERMINFO_LENGTH + EXTRA_CAP];
611 NCURSES_CONST char *name;
613 PredIdx num_bools = 0;
614 PredIdx num_values = 0;
615 PredIdx num_strings = 0;
618 #define WRAP_CONCAT \
619 wrap_concat(buffer); \
622 len = 12; /* terminfo file-header */
626 pred = dump_predicate;
629 strcpy_DYN(&outbuf, 0);
631 column = INDENT; /* FIXME: workaround to prevent empty lines */
633 strcpy_DYN(&outbuf, tterm->term_names);
636 * Colon is legal in terminfo descriptions, but not in termcap.
639 char *p = outbuf.text;
647 strcpy_DYN(&outbuf, separator);
648 column = (int) outbuf.used;
653 for_each_boolean(j, tterm) {
655 name = ExtBoolname(tterm, (int) i, bool_names);
656 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
658 if (!version_filter(BOOLEAN, i))
660 else if (isObsolete(outform, name))
663 predval = pred(BOOLEAN, i);
664 if (predval != FAIL) {
665 _nc_STRCPY(buffer, name, sizeof(buffer));
667 _nc_STRCAT(buffer, "@", sizeof(buffer));
668 else if (i + 1 > num_bools)
674 if (column != INDENT && height > 1)
677 for_each_number(j, tterm) {
679 name = ExtNumname(tterm, (int) i, num_names);
680 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
682 if (!version_filter(NUMBER, i))
684 else if (isObsolete(outform, name))
687 predval = pred(NUMBER, i);
688 if (predval != FAIL) {
689 if (tterm->Numbers[i] < 0) {
690 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
693 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
694 "%s#%d", name, tterm->Numbers[i]);
695 if (i + 1 > num_values)
702 if (column != INDENT && height > 1)
705 len += (int) (num_bools
707 + strlen(tterm->term_names) + 1);
713 if (outform == F_TERMCAP) {
714 if (termcap_reset != ABSENT_STRING) {
715 if (init_3string != ABSENT_STRING
716 && !strcmp(init_3string, termcap_reset))
717 DISCARD(init_3string);
719 if (reset_2string != ABSENT_STRING
720 && !strcmp(reset_2string, termcap_reset))
721 DISCARD(reset_2string);
725 for_each_string(j, tterm) {
727 name = ExtStrname(tterm, (int) i, str_names);
728 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
730 capability = tterm->Strings[i];
732 if (!version_filter(STRING, i))
734 else if (isObsolete(outform, name))
739 * Extended names can be longer than 2 characters, but termcap programs
740 * cannot read those (filter them out).
742 if (outform == F_TERMCAP && (strlen(name) > 2))
746 if (outform == F_TERMCAP) {
748 * Some older versions of vi want rmir/smir to be defined
749 * for ich/ich1 to work. If they're not defined, force
750 * them to be output as defined and empty.
752 if (PRESENT(insert_character) || PRESENT(parm_ich)) {
753 if (SAME_CAP(i, enter_insert_mode)
754 && enter_insert_mode == ABSENT_STRING) {
755 _nc_STRCPY(buffer, "im=", sizeof(buffer));
760 if (SAME_CAP(i, exit_insert_mode)
761 && exit_insert_mode == ABSENT_STRING) {
762 _nc_STRCPY(buffer, "ei=", sizeof(buffer));
768 * termcap applications such as screen will be confused if sgr0
769 * is translated to a string containing rmacs. Filter that out.
771 if (PRESENT(exit_attribute_mode)) {
772 if (SAME_CAP(i, exit_attribute_mode)) {
774 char *my_sgr = set_attributes;
776 set_attributes = save_sgr;
778 trimmed_sgr0 = _nc_trim_sgr0(tterm);
779 if (strcmp(capability, trimmed_sgr0))
780 capability = trimmed_sgr0;
784 set_attributes = my_sgr;
789 predval = pred(STRING, i);
792 if (predval != FAIL) {
793 if (capability != ABSENT_STRING
794 && i + 1 > num_strings)
797 if (!VALID_STRING(capability)) {
798 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
801 } else if (outform == F_TERMCAP || outform == F_TCONVERR) {
802 char *srccap = _nc_tic_expand(capability, TRUE, numbers);
803 int params = (((i < (int) SIZEOF(parametrized)) &&
808 : has_params(srccap)));
809 char *cv = _nc_infotocap(name, srccap, params);
812 if (outform == F_TCONVERR) {
813 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
814 "%s=!!! %s WILL NOT CONVERT !!!",
816 } else if (suppress_untranslatable) {
819 char *s = srccap, *d = buffer;
820 _nc_SPRINTF(d, _nc_SLIMIT(sizeof(buffer)) "..%s=", name);
822 while ((*d = *s++) != 0) {
826 } else if (*d == '\\') {
833 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
836 len += (int) strlen(capability) + 1;
839 char *src = _nc_tic_expand(capability,
840 outform == F_TERMINFO, numbers);
842 strcpy_DYN(&tmpbuf, 0);
843 strcpy_DYN(&tmpbuf, name);
844 strcpy_DYN(&tmpbuf, "=");
846 && (outform == F_TERMINFO
847 || outform == F_VARIABLE)) {
848 fmt_complex(tterm, name, src, 1);
850 strcpy_DYN(&tmpbuf, src);
852 len += (int) strlen(capability) + 1;
853 wrap_concat(tmpbuf.text);
857 /* e.g., trimmed_sgr0 */
858 if (capability != ABSENT_STRING &&
859 capability != CANCELLED_STRING &&
860 capability != tterm->Strings[i])
863 len += (int) (num_strings * 2);
866 * This piece of code should be an effective inverse of the functions
867 * postprocess_terminfo() and postprocess_terminfo() in parse_entry.c.
868 * Much more work should be done on this to support dumping termcaps.
870 if (tversion == V_HPUX) {
871 if (VALID_STRING(memory_lock)) {
872 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
873 "meml=%s", memory_lock);
876 if (VALID_STRING(memory_unlock)) {
877 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
878 "memu=%s", memory_unlock);
881 } else if (tversion == V_AIX) {
882 if (VALID_STRING(acs_chars)) {
884 const char *acstrans = "lqkxjmwuvtn";
886 char *tp, *sp, boxchars[11];
889 for (cp = acstrans; *cp; cp++) {
890 sp = (strchr) (acs_chars, *cp);
901 char *tmp = _nc_tic_expand(boxchars,
902 (outform == F_TERMINFO),
904 _nc_STRCPY(buffer, "box1=", sizeof(buffer));
905 while (*tmp != '\0') {
906 size_t have = strlen(buffer);
907 size_t next = strlen(tmp);
908 size_t want = have + next + 1;
913 * If the expanded string is too long for the buffer,
914 * chop it off and save the location where we chopped it.
916 if (want >= sizeof(buffer)) {
920 _nc_STRCAT(buffer, tmp, sizeof(buffer));
923 * If we chopped the buffer, replace the missing piece and
924 * shift everything to append the remainder.
929 while ((tmp[next] = tmp[last + next]) != '\0') {
942 * kludge: trim off trailer to avoid an extra blank line
943 * in infocmp -u output when there are no string differences
946 bool trimmed = FALSE;
947 j = (PredIdx) outbuf.used;
949 && outbuf.text[j - 1] == '\t'
950 && outbuf.text[j - 2] == '\n') {
954 && outbuf.text[j - 1] == ':'
955 && outbuf.text[j - 2] == '\t'
956 && outbuf.text[j - 3] == '\n'
957 && outbuf.text[j - 4] == '\\') {
962 outbuf.text[outbuf.used] = '\0';
964 strcpy_DYN(&outbuf, " ");
968 fprintf(stderr, "num_bools = %d\n", num_bools);
969 fprintf(stderr, "num_values = %d\n", num_values);
970 fprintf(stderr, "num_strings = %d\n", num_strings);
971 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
972 tterm->term_names, len, outbuf.used, outbuf.text);
975 * Here's where we use infodump to trigger a more stringent length check
976 * for termcap-translation purposes.
977 * Return the length of the raw entry, without tc= expansions,
978 * It gives an idea of which entries are deadly to even *scan past*,
979 * as opposed to *use*.
981 return (infodump ? len : (int) termcap_length(outbuf.text));
985 kill_string(TERMTYPE *tterm, char *cap)
988 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
989 if (cap == tterm->Strings[n]) {
990 tterm->Strings[n] = ABSENT_STRING;
998 find_string(TERMTYPE *tterm, char *name)
1001 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1002 if (version_filter(STRING, n)
1003 && !strcmp(name, strnames[n])) {
1004 char *cap = tterm->Strings[n];
1005 if (VALID_STRING(cap)) {
1011 return ABSENT_STRING;
1015 * This is used to remove function-key labels from a termcap entry to
1019 kill_labels(TERMTYPE *tterm, int target)
1026 for (n = 0; n <= 10; ++n) {
1027 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "lf%d", n);
1028 if ((cap = find_string(tterm, name)) != ABSENT_STRING
1029 && kill_string(tterm, cap)) {
1030 target -= (int) (strlen(cap) + 5);
1040 * This is used to remove function-key definitions from a termcap entry to
1044 kill_fkeys(TERMTYPE *tterm, int target)
1051 for (n = 60; n >= 0; --n) {
1052 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n);
1053 if ((cap = find_string(tterm, name)) != ABSENT_STRING
1054 && kill_string(tterm, cap)) {
1055 target -= (int) (strlen(cap) + 5);
1065 * Check if the given acsc string is a 1-1 mapping, i.e., just-like-vt100.
1066 * Also, since this is for termcap, we only care about the line-drawing map.
1068 #define isLine(c) (strchr("lmkjtuvwqxn", c) != 0)
1071 one_one_mapping(const char *mapping)
1075 if (mapping != ABSENT_STRING) {
1077 while (mapping[n] != '\0') {
1078 if (isLine(mapping[n]) &&
1079 mapping[n] != mapping[n + 1]) {
1089 #define FMT_ENTRY() \
1090 fmt_entry(tterm, pred, \
1092 suppress_untranslatable, \
1095 #define SHOW_WHY PRINTF
1098 purged_acs(TERMTYPE *tterm)
1100 bool result = FALSE;
1102 if (VALID_STRING(acs_chars)) {
1103 if (!one_one_mapping(acs_chars)) {
1104 enter_alt_charset_mode = ABSENT_STRING;
1105 exit_alt_charset_mode = ABSENT_STRING;
1106 SHOW_WHY("# (rmacs/smacs removed for consistency)\n");
1114 * Dump a single entry.
1117 dump_entry(TERMTYPE *tterm,
1118 int suppress_untranslatable,
1123 TERMTYPE save_tterm;
1128 if (outform == F_TERMCAP || outform == F_TCONVERR) {
1129 critlen = MAX_TERMCAP_LENGTH;
1130 legend = "older termcap";
1132 set_obsolete_termcaps(tterm);
1134 critlen = MAX_TERMINFO_LENGTH;
1135 legend = "terminfo";
1139 save_sgr = set_attributes;
1141 if ((FMT_ENTRY() > critlen)
1144 save_tterm = *tterm;
1145 if (!suppress_untranslatable) {
1146 SHOW_WHY("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1148 suppress_untranslatable = TRUE;
1150 if (FMT_ENTRY() > critlen) {
1152 * We pick on sgr because it's a nice long string capability that
1153 * is really just an optimization hack. Another good candidate is
1154 * acsc since it is both long and unused by BSD termcap.
1156 bool changed = FALSE;
1160 * Extended names are most likely function-key definitions. Drop
1164 for (n = STRCOUNT; n < NUM_STRINGS(tterm); n++) {
1165 const char *name = ExtStrname(tterm, (int) n, strnames);
1167 if (VALID_STRING(tterm->Strings[n])) {
1168 set_attributes = ABSENT_STRING;
1169 /* we remove long names anyway - only report the short */
1170 if (strlen(name) <= 2) {
1171 SHOW_WHY("# (%s removed to fit entry within %d bytes)\n",
1176 if (FMT_ENTRY() <= critlen)
1181 if (VALID_STRING(set_attributes)) {
1182 set_attributes = ABSENT_STRING;
1183 SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n",
1187 if (!changed || (FMT_ENTRY() > critlen)) {
1188 if (purged_acs(tterm)) {
1189 acs_chars = ABSENT_STRING;
1190 SHOW_WHY("# (acsc removed to fit entry within %d bytes)\n",
1195 if (!changed || (FMT_ENTRY() > critlen)) {
1196 int oldversion = tversion;
1199 SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1204 && kill_labels(tterm, len - critlen)) {
1205 SHOW_WHY("# (some labels capabilities suppressed to fit entry within %d bytes)\n",
1210 && kill_fkeys(tterm, len - critlen)) {
1211 SHOW_WHY("# (some function-key capabilities suppressed to fit entry within %d bytes)\n",
1215 if (len > critlen) {
1216 (void) fprintf(stderr,
1217 "warning: %s entry is %d bytes long\n",
1218 _nc_first_name(tterm->term_names),
1220 SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
1223 tversion = oldversion;
1225 set_attributes = save_sgr;
1226 *tterm = save_tterm;
1228 } else if (!version_filter(STRING, STR_IDX(acs_chars))) {
1229 save_tterm = *tterm;
1230 if (purged_acs(tterm)) {
1233 *tterm = save_tterm;
1238 dump_uses(const char *name, bool infodump)
1239 /* dump "use=" clauses in the appropriate format */
1241 char buffer[MAX_TERMINFO_LENGTH];
1243 if (outform == F_TERMCAP || outform == F_TCONVERR)
1245 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1246 "%s%s", infodump ? "use=" : "tc=", name);
1247 wrap_concat(buffer);
1254 * Trim any remaining whitespace.
1256 if (outbuf.used != 0) {
1257 bool infodump = (outform != F_TERMCAP && outform != F_TCONVERR);
1258 char delim = (char) (infodump ? ',' : ':');
1261 for (j = (int) outbuf.used - 1; j > 0; --j) {
1262 char ch = outbuf.text[j];
1265 } else if (isspace(UChar(ch))) {
1266 outbuf.used = (size_t) j;
1267 } else if (!infodump && ch == '\\') {
1268 outbuf.used = (size_t) j;
1269 } else if (ch == delim && (j == 0 || outbuf.text[j - 1] != '\\')) {
1270 outbuf.used = (size_t) (j + 1);
1275 outbuf.text[outbuf.used] = '\0';
1277 (void) fputs(outbuf.text, stdout);
1279 return (int) outbuf.used;
1283 compare_entry(PredHook hook,
1284 TERMTYPE *tp GCC_UNUSED,
1286 /* compare two entries */
1289 NCURSES_CONST char *name;
1292 fputs(" comparing booleans.\n", stdout);
1293 for_each_boolean(j, tp) {
1294 i = BoolIndirect(j);
1295 name = ExtBoolname(tp, (int) i, bool_names);
1297 if (isObsolete(outform, name))
1300 (*hook) (CMP_BOOLEAN, i, name);
1304 fputs(" comparing numbers.\n", stdout);
1305 for_each_number(j, tp) {
1307 name = ExtNumname(tp, (int) i, num_names);
1309 if (isObsolete(outform, name))
1312 (*hook) (CMP_NUMBER, i, name);
1316 fputs(" comparing strings.\n", stdout);
1317 for_each_string(j, tp) {
1319 name = ExtStrname(tp, (int) i, str_names);
1321 if (isObsolete(outform, name))
1324 (*hook) (CMP_STRING, i, name);
1327 /* (void) fputs(" comparing use entries.\n", stdout); */
1328 (*hook) (CMP_USE, 0, "use");
1332 #define NOTSET(s) ((s) == 0)
1335 * This bit of legerdemain turns all the terminfo variable names into
1336 * references to locations in the arrays Booleans, Numbers, and Strings ---
1337 * precisely what's needed.
1343 set_obsolete_termcaps(TERMTYPE *tp)
1345 #include "capdefaults.c"
1349 * Convert an alternate-character-set string to canonical form: sorted and
1353 repair_acsc(TERMTYPE *tp)
1355 if (VALID_STRING(acs_chars)) {
1361 bool fix_needed = FALSE;
1363 for (n = 0, source = 0; acs_chars[n] != 0; n++) {
1364 target = UChar(acs_chars[n]);
1365 if (source >= target) {
1370 if (acs_chars[n + 1])
1374 memset(mapped, 0, sizeof(mapped));
1375 for (n = 0; acs_chars[n] != 0; n++) {
1376 source = UChar(acs_chars[n]);
1377 if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
1378 mapped[source] = (char) target;
1381 extra = (char) source;
1384 for (n = m = 0; n < sizeof(mapped); n++) {
1386 acs_chars[m++] = (char) n;
1387 acs_chars[m++] = mapped[n];
1391 acs_chars[m++] = extra; /* garbage in, garbage out */