1 /****************************************************************************
2 * Copyright (c) 1998-2011,2012 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.104 2012/12/30 00:51:13 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 */
65 static char *save_sgr;
70 /* indirection pointers for implementing sort and display modes */
71 static const PredIdx *bool_indirect, *num_indirect, *str_indirect;
72 static NCURSES_CONST char *const *bool_names;
73 static NCURSES_CONST char *const *num_names;
74 static NCURSES_CONST char *const *str_names;
76 static const char *separator, *trailer;
78 /* cover various ports and variants of terminfo */
79 #define V_ALLCAPS 0 /* all capabilities (SVr4, XSI, ncurses) */
80 #define V_SVR1 1 /* SVR1, Ultrix */
81 #define V_HPUX 2 /* HP/UX */
82 #define V_AIX 3 /* AIX */
83 #define V_BSD 4 /* BSD */
86 #define OBSOLETE(n) (!_nc_user_definable && (n[0] == 'O' && n[1] == 'T'))
88 #define OBSOLETE(n) (n[0] == 'O' && n[1] == 'T')
91 #define isObsolete(f,n) ((f == F_TERMINFO || f == F_VARIABLE) && OBSOLETE(n))
94 #define BoolIndirect(j) ((j >= BOOLCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : bool_indirect[j]))
95 #define NumIndirect(j) ((j >= NUMCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : num_indirect[j]))
96 #define StrIndirect(j) ((j >= STRCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : str_indirect[j]))
98 #define BoolIndirect(j) ((sortmode == S_NOSORT) ? (j) : bool_indirect[j])
99 #define NumIndirect(j) ((sortmode == S_NOSORT) ? (j) : num_indirect[j])
100 #define StrIndirect(j) ((sortmode == S_NOSORT) ? (j) : str_indirect[j])
103 static void failed(const char *) GCC_NORETURN;
106 failed(const char *s)
109 ExitProgram(EXIT_FAILURE);
113 strncpy_DYN(DYNBUF * dst, const char *src, size_t need)
115 size_t want = need + dst->used + 1;
116 if (want > dst->size) {
117 dst->size += (want + 1024); /* be generous */
118 dst->text = typeRealloc(char, dst->size, dst->text);
120 failed("strncpy_DYN");
122 (void) strncpy(dst->text + dst->used, src, need);
124 dst->text[dst->used] = 0;
128 strcpy_DYN(DYNBUF * dst, const char *src)
134 strncpy_DYN(dst, src, strlen(src));
150 _nc_leaks_dump_entry(void)
157 #define NameTrans(check,result) \
158 if (OkIndex(np->nte_index, check) \
159 && check[np->nte_index]) \
160 return (result[np->nte_index])
163 nametrans(const char *name)
164 /* translate a capability name from termcap to terminfo */
166 const struct name_table_entry *np;
168 if ((np = _nc_find_entry(name, _nc_get_hash_table(0))) != 0)
169 switch (np->nte_type) {
171 NameTrans(bool_from_termcap, boolcodes);
175 NameTrans(num_from_termcap, numcodes);
179 NameTrans(str_from_termcap, strcodes);
187 dump_init(const char *version,
194 /* set up for entry display */
202 tversion = V_ALLCAPS;
203 else if (!strcmp(version, "SVr1") || !strcmp(version, "SVR1")
204 || !strcmp(version, "Ultrix"))
206 else if (!strcmp(version, "HP"))
208 else if (!strcmp(version, "AIX"))
210 else if (!strcmp(version, "BSD"))
213 tversion = V_ALLCAPS;
215 /* implement display modes */
216 switch (outform = mode) {
219 bool_names = boolnames;
220 num_names = numnames;
221 str_names = strnames;
222 separator = (twidth > 0 && theight > 1) ? ", " : ",";
227 bool_names = boolfnames;
228 num_names = numfnames;
229 str_names = strfnames;
230 separator = (twidth > 0 && theight > 1) ? ", " : ",";
236 bool_names = boolcodes;
237 num_names = numcodes;
238 str_names = strcodes;
244 /* implement sort modes */
245 switch (sortmode = sort) {
248 (void) fprintf(stderr,
249 "%s: sorting by term structure order\n", _nc_progname);
254 (void) fprintf(stderr,
255 "%s: sorting by terminfo name order\n", _nc_progname);
256 bool_indirect = bool_terminfo_sort;
257 num_indirect = num_terminfo_sort;
258 str_indirect = str_terminfo_sort;
263 (void) fprintf(stderr,
264 "%s: sorting by C variable order\n", _nc_progname);
265 bool_indirect = bool_variable_sort;
266 num_indirect = num_variable_sort;
267 str_indirect = str_variable_sort;
272 (void) fprintf(stderr,
273 "%s: sorting by termcap name order\n", _nc_progname);
274 bool_indirect = bool_termcap_sort;
275 num_indirect = num_termcap_sort;
276 str_indirect = str_termcap_sort;
281 (void) fprintf(stderr,
282 "%s: width = %d, tversion = %d, outform = %d\n",
283 _nc_progname, width, tversion, outform);
286 static TERMTYPE *cur_type;
289 dump_predicate(PredType type, PredIdx idx)
290 /* predicate function to use for ordinary decompilation */
294 return (cur_type->Booleans[idx] == FALSE)
295 ? FAIL : cur_type->Booleans[idx];
298 return (cur_type->Numbers[idx] == ABSENT_NUMERIC)
299 ? FAIL : cur_type->Numbers[idx];
302 return (cur_type->Strings[idx] != ABSENT_STRING)
306 return (FALSE); /* pacify compiler */
309 static void set_obsolete_termcaps(TERMTYPE *tp);
311 /* is this the index of a function key string? */
312 #define FNKEY(i) (((i)<= 65 && (i)>= 75) || ((i)<= 216 && (i)>= 268))
315 * If we configure with a different Caps file, the offsets into the arrays
316 * will change. So we use an address expression.
318 #define BOOL_IDX(name) (PredType) (&(name) - &(CUR Booleans[0]))
319 #define NUM_IDX(name) (PredType) (&(name) - &(CUR Numbers[0]))
320 #define STR_IDX(name) (PredType) (&(name) - &(CUR Strings[0]))
323 version_filter(PredType type, PredIdx idx)
324 /* filter out capabilities we may want to suppress */
327 case V_ALLCAPS: /* SVr4, XSI Curses */
330 case V_SVR1: /* System V Release 1, Ultrix */
333 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
335 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
337 return ((idx <= STR_IDX(prtr_non)) ? TRUE : FALSE);
341 case V_HPUX: /* Hewlett-Packard */
344 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
346 return ((idx <= NUM_IDX(label_width)) ? TRUE : FALSE);
348 if (idx <= STR_IDX(prtr_non))
350 else if (FNKEY(idx)) /* function keys */
352 else if (idx == STR_IDX(plab_norm)
353 || idx == STR_IDX(label_on)
354 || idx == STR_IDX(label_off))
361 case V_AIX: /* AIX */
364 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
366 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
368 if (idx <= STR_IDX(prtr_non))
370 else if (FNKEY(idx)) /* function keys */
377 #define is_termcap(type) (OkIndex(idx, type##_from_termcap) && \
378 type##_from_termcap[idx])
380 case V_BSD: /* BSD */
383 return is_termcap(bool);
385 return is_termcap(num);
387 return is_termcap(str);
392 return (FALSE); /* pacify the compiler */
398 while (outbuf.used > 0 && outbuf.text[outbuf.used - 1] == ' ')
399 outbuf.text[--outbuf.used] = '\0';
407 strcpy_DYN(&outbuf, trailer);
412 wrap_concat(const char *src)
414 size_t need = strlen(src);
415 size_t want = strlen(separator) + need;
418 && column + (int) want > width) {
421 strcpy_DYN(&outbuf, src);
422 strcpy_DYN(&outbuf, separator);
423 column += (int) need;
426 #define IGNORE_SEP_TRAIL(first,last,sep_trail) \
427 if ((size_t)(last - first) > sizeof(sep_trail)-1 \
428 && !strncmp(first, sep_trail, sizeof(sep_trail)-1)) \
429 first += sizeof(sep_trail)-2
431 /* Returns the nominal length of the buffer assuming it is termcap format,
432 * i.e., the continuation sequence is treated as a single character ":".
434 * There are several implementations of termcap which read the text into a
435 * fixed-size buffer. Generally they strip the newlines from the text, but may
436 * not do it until after the buffer is read. Also, "tc=" resolution may be
437 * expanded in the same buffer. This function is useful for measuring the size
438 * of the best fixed-buffer implementation; the worst case may be much worse.
440 #ifdef TEST_TERMCAP_LENGTH
442 termcap_length(const char *src)
444 static const char pattern[] = ":\\\n\t:";
447 const char *const t = src + strlen(src);
449 while (*src != '\0') {
450 IGNORE_SEP_TRAIL(src, t, pattern);
457 #define termcap_length(src) strlen(src)
461 indent_DYN(DYNBUF * buffer, int level)
465 for (n = 0; n < level; n++)
466 strncpy_DYN(buffer, "\t", 1);
470 has_params(const char *src)
473 int len = (int) strlen(src);
478 for (n = 0; n < len - 1; ++n) {
479 if (!strncmp(src + n, "%p", 2)) {
481 } else if (!strncmp(src + n, "%;", 2)) {
488 result = ((len > 50) && params);
494 fmt_complex(char *src, int level)
496 bool percent = FALSE;
497 bool params = has_params(src);
499 while (*src != '\0') {
503 strncpy_DYN(&tmpbuf, src++, 1);
509 case 't': /* "then" */
510 case 'e': /* "else" */
513 tmpbuf.text[tmpbuf.used - 1] = '\n';
514 /* treat a "%e" as else-if, on the same level */
516 indent_DYN(&tmpbuf, level);
517 strncpy_DYN(&tmpbuf, "%", 1);
518 strncpy_DYN(&tmpbuf, src, 1);
520 params = has_params(src);
521 if (!params && *src != '\0' && *src != '%') {
522 strncpy_DYN(&tmpbuf, "\n", 1);
523 indent_DYN(&tmpbuf, level + 1);
526 indent_DYN(&tmpbuf, level + 1);
527 strncpy_DYN(&tmpbuf, "%", 1);
528 strncpy_DYN(&tmpbuf, src, 1);
530 src = fmt_complex(src, level + 1);
531 if (*src != '\0' && *src != '%') {
532 strncpy_DYN(&tmpbuf, "\n", 1);
533 indent_DYN(&tmpbuf, level + 1);
535 } else if (level == 1) {
536 _nc_warning("%%%c without %%?", *src);
542 case ';': /* "endif" */
546 tmpbuf.text[tmpbuf.used - 1] = '\n';
547 indent_DYN(&tmpbuf, level);
548 strncpy_DYN(&tmpbuf, "%", 1);
549 strncpy_DYN(&tmpbuf, src++, 1);
552 _nc_warning("%%; without %%?");
556 if (percent && params) {
557 tmpbuf.text[tmpbuf.used - 1] = '\n';
558 indent_DYN(&tmpbuf, level + 1);
559 strncpy_DYN(&tmpbuf, "%", 1);
565 strncpy_DYN(&tmpbuf, "\\s", 2);
572 strncpy_DYN(&tmpbuf, src++, 1);
577 #define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap)
581 fmt_entry(TERMTYPE *tterm,
584 bool suppress_untranslatable,
589 char buffer[MAX_TERMINFO_LENGTH + EXTRA_CAP];
591 NCURSES_CONST char *name;
593 PredIdx num_bools = 0;
594 PredIdx num_values = 0;
595 PredIdx num_strings = 0;
598 #define WRAP_CONCAT \
599 wrap_concat(buffer); \
602 len = 12; /* terminfo file-header */
606 pred = dump_predicate;
609 strcpy_DYN(&outbuf, 0);
611 column = INDENT; /* FIXME: workaround to prevent empty lines */
613 strcpy_DYN(&outbuf, tterm->term_names);
616 * Colon is legal in terminfo descriptions, but not in termcap.
619 char *p = outbuf.text;
627 strcpy_DYN(&outbuf, separator);
628 column = (int) outbuf.used;
633 for_each_boolean(j, tterm) {
635 name = ExtBoolname(tterm, (int) i, bool_names);
636 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
638 if (!version_filter(BOOLEAN, i))
640 else if (isObsolete(outform, name))
643 predval = pred(BOOLEAN, i);
644 if (predval != FAIL) {
645 _nc_STRCPY(buffer, name, sizeof(buffer));
647 _nc_STRCAT(buffer, "@", sizeof(buffer));
648 else if (i + 1 > num_bools)
654 if (column != INDENT && height > 1)
657 for_each_number(j, tterm) {
659 name = ExtNumname(tterm, (int) i, num_names);
660 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
662 if (!version_filter(NUMBER, i))
664 else if (isObsolete(outform, name))
667 predval = pred(NUMBER, i);
668 if (predval != FAIL) {
669 if (tterm->Numbers[i] < 0) {
670 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
673 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
674 "%s#%d", name, tterm->Numbers[i]);
675 if (i + 1 > num_values)
682 if (column != INDENT && height > 1)
685 len += (int) (num_bools
687 + strlen(tterm->term_names) + 1);
693 if (outform == F_TERMCAP) {
694 if (termcap_reset != ABSENT_STRING) {
695 if (init_3string != ABSENT_STRING
696 && !strcmp(init_3string, termcap_reset))
697 DISCARD(init_3string);
699 if (reset_2string != ABSENT_STRING
700 && !strcmp(reset_2string, termcap_reset))
701 DISCARD(reset_2string);
705 for_each_string(j, tterm) {
707 name = ExtStrname(tterm, (int) i, str_names);
708 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
710 capability = tterm->Strings[i];
712 if (!version_filter(STRING, i))
714 else if (isObsolete(outform, name))
719 * Extended names can be longer than 2 characters, but termcap programs
720 * cannot read those (filter them out).
722 if (outform == F_TERMCAP && (strlen(name) > 2))
726 if (outform == F_TERMCAP) {
728 * Some older versions of vi want rmir/smir to be defined
729 * for ich/ich1 to work. If they're not defined, force
730 * them to be output as defined and empty.
732 if (PRESENT(insert_character) || PRESENT(parm_ich)) {
733 if (SAME_CAP(i, enter_insert_mode)
734 && enter_insert_mode == ABSENT_STRING) {
735 _nc_STRCPY(buffer, "im=", sizeof(buffer));
740 if (SAME_CAP(i, exit_insert_mode)
741 && exit_insert_mode == ABSENT_STRING) {
742 _nc_STRCPY(buffer, "ei=", sizeof(buffer));
748 * termcap applications such as screen will be confused if sgr0
749 * is translated to a string containing rmacs. Filter that out.
751 if (PRESENT(exit_attribute_mode)) {
752 if (SAME_CAP(i, exit_attribute_mode)) {
754 char *my_sgr = set_attributes;
756 set_attributes = save_sgr;
758 trimmed_sgr0 = _nc_trim_sgr0(tterm);
759 if (strcmp(capability, trimmed_sgr0))
760 capability = trimmed_sgr0;
762 set_attributes = my_sgr;
767 predval = pred(STRING, i);
770 if (predval != FAIL) {
771 if (capability != ABSENT_STRING
772 && i + 1 > num_strings)
775 if (!VALID_STRING(capability)) {
776 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
779 } else if (outform == F_TERMCAP || outform == F_TCONVERR) {
780 int params = ((i < (int) SIZEOF(parametrized))
783 char *srccap = _nc_tic_expand(capability, TRUE, numbers);
784 char *cv = _nc_infotocap(name, srccap, params);
787 if (outform == F_TCONVERR) {
788 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
789 "%s=!!! %s WILL NOT CONVERT !!!",
791 } else if (suppress_untranslatable) {
794 char *s = srccap, *d = buffer;
795 _nc_SPRINTF(d, _nc_SLIMIT(sizeof(buffer)) "..%s=", name);
797 while ((*d = *s++) != 0) {
801 } else if (*d == '\\') {
808 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
811 len += (int) strlen(capability) + 1;
814 char *src = _nc_tic_expand(capability,
815 outform == F_TERMINFO, numbers);
817 strcpy_DYN(&tmpbuf, 0);
818 strcpy_DYN(&tmpbuf, name);
819 strcpy_DYN(&tmpbuf, "=");
821 && (outform == F_TERMINFO
822 || outform == F_VARIABLE)) {
825 strcpy_DYN(&tmpbuf, src);
827 len += (int) strlen(capability) + 1;
828 wrap_concat(tmpbuf.text);
832 /* e.g., trimmed_sgr0 */
833 if (capability != ABSENT_STRING &&
834 capability != CANCELLED_STRING &&
835 capability != tterm->Strings[i])
838 len += (int) (num_strings * 2);
841 * This piece of code should be an effective inverse of the functions
842 * postprocess_terminfo() and postprocess_terminfo() in parse_entry.c.
843 * Much more work should be done on this to support dumping termcaps.
845 if (tversion == V_HPUX) {
846 if (VALID_STRING(memory_lock)) {
847 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
848 "meml=%s", memory_lock);
851 if (VALID_STRING(memory_unlock)) {
852 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
853 "memu=%s", memory_unlock);
856 } else if (tversion == V_AIX) {
857 if (VALID_STRING(acs_chars)) {
859 const char *acstrans = "lqkxjmwuvtn";
861 char *tp, *sp, boxchars[11];
864 for (cp = acstrans; *cp; cp++) {
865 sp = strchr(acs_chars, *cp);
876 char *tmp = _nc_tic_expand(boxchars,
877 (outform == F_TERMINFO),
879 _nc_STRCPY(buffer, "box1=", sizeof(buffer));
880 while (*tmp != '\0') {
881 size_t have = strlen(buffer);
882 size_t next = strlen(tmp);
883 size_t want = have + next + 1;
888 * If the expanded string is too long for the buffer,
889 * chop it off and save the location where we chopped it.
891 if (want >= sizeof(buffer)) {
895 _nc_STRCAT(buffer, tmp, sizeof(buffer));
898 * If we chopped the buffer, replace the missing piece and
899 * shift everything to append the remainder.
904 while ((tmp[next] = tmp[last + next]) != '\0') {
917 * kludge: trim off trailer to avoid an extra blank line
918 * in infocmp -u output when there are no string differences
921 bool trimmed = FALSE;
922 j = (PredIdx) outbuf.used;
924 && outbuf.text[j - 1] == '\t'
925 && outbuf.text[j - 2] == '\n') {
929 && outbuf.text[j - 1] == ':'
930 && outbuf.text[j - 2] == '\t'
931 && outbuf.text[j - 3] == '\n'
932 && outbuf.text[j - 4] == '\\') {
937 outbuf.text[outbuf.used] = '\0';
939 strcpy_DYN(&outbuf, " ");
943 fprintf(stderr, "num_bools = %d\n", num_bools);
944 fprintf(stderr, "num_values = %d\n", num_values);
945 fprintf(stderr, "num_strings = %d\n", num_strings);
946 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
947 tterm->term_names, len, outbuf.used, outbuf.text);
950 * Here's where we use infodump to trigger a more stringent length check
951 * for termcap-translation purposes.
952 * Return the length of the raw entry, without tc= expansions,
953 * It gives an idea of which entries are deadly to even *scan past*,
954 * as opposed to *use*.
956 return (infodump ? len : (int) termcap_length(outbuf.text));
960 kill_string(TERMTYPE *tterm, char *cap)
963 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
964 if (cap == tterm->Strings[n]) {
965 tterm->Strings[n] = ABSENT_STRING;
973 find_string(TERMTYPE *tterm, char *name)
976 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
977 if (version_filter(STRING, n)
978 && !strcmp(name, strnames[n])) {
979 char *cap = tterm->Strings[n];
980 if (VALID_STRING(cap)) {
986 return ABSENT_STRING;
990 * This is used to remove function-key labels from a termcap entry to
994 kill_labels(TERMTYPE *tterm, int target)
1001 for (n = 0; n <= 10; ++n) {
1002 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "lf%d", n);
1003 if ((cap = find_string(tterm, name)) != ABSENT_STRING
1004 && kill_string(tterm, cap)) {
1005 target -= (int) (strlen(cap) + 5);
1015 * This is used to remove function-key definitions from a termcap entry to
1019 kill_fkeys(TERMTYPE *tterm, int target)
1026 for (n = 60; n >= 0; --n) {
1027 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n);
1028 if ((cap = find_string(tterm, name)) != ABSENT_STRING
1029 && kill_string(tterm, cap)) {
1030 target -= (int) (strlen(cap) + 5);
1040 * Check if the given acsc string is a 1-1 mapping, i.e., just-like-vt100.
1041 * Also, since this is for termcap, we only care about the line-drawing map.
1043 #define isLine(c) (strchr("lmkjtuvwqxn", c) != 0)
1046 one_one_mapping(const char *mapping)
1050 if (mapping != ABSENT_STRING) {
1052 while (mapping[n] != '\0') {
1053 if (isLine(mapping[n]) &&
1054 mapping[n] != mapping[n + 1]) {
1064 #define FMT_ENTRY() \
1065 fmt_entry(tterm, pred, \
1067 suppress_untranslatable, \
1070 #define SHOW_WHY PRINTF
1073 purged_acs(TERMTYPE *tterm)
1075 bool result = FALSE;
1077 if (VALID_STRING(acs_chars)) {
1078 if (!one_one_mapping(acs_chars)) {
1079 enter_alt_charset_mode = ABSENT_STRING;
1080 exit_alt_charset_mode = ABSENT_STRING;
1081 SHOW_WHY("# (rmacs/smacs removed for consistency)\n");
1089 * Dump a single entry.
1092 dump_entry(TERMTYPE *tterm,
1093 bool suppress_untranslatable,
1098 TERMTYPE save_tterm;
1103 if (outform == F_TERMCAP || outform == F_TCONVERR) {
1104 critlen = MAX_TERMCAP_LENGTH;
1105 legend = "older termcap";
1107 set_obsolete_termcaps(tterm);
1109 critlen = MAX_TERMINFO_LENGTH;
1110 legend = "terminfo";
1114 save_sgr = set_attributes;
1116 if ((FMT_ENTRY() > critlen)
1119 save_tterm = *tterm;
1120 if (!suppress_untranslatable) {
1121 SHOW_WHY("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1123 suppress_untranslatable = TRUE;
1125 if (FMT_ENTRY() > critlen) {
1127 * We pick on sgr because it's a nice long string capability that
1128 * is really just an optimization hack. Another good candidate is
1129 * acsc since it is both long and unused by BSD termcap.
1131 bool changed = FALSE;
1135 * Extended names are most likely function-key definitions. Drop
1139 for (n = STRCOUNT; n < NUM_STRINGS(tterm); n++) {
1140 const char *name = ExtStrname(tterm, (int) n, strnames);
1142 if (VALID_STRING(tterm->Strings[n])) {
1143 set_attributes = ABSENT_STRING;
1144 /* we remove long names anyway - only report the short */
1145 if (strlen(name) <= 2) {
1146 SHOW_WHY("# (%s removed to fit entry within %d bytes)\n",
1151 if (FMT_ENTRY() <= critlen)
1156 if (VALID_STRING(set_attributes)) {
1157 set_attributes = ABSENT_STRING;
1158 SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n",
1162 if (!changed || (FMT_ENTRY() > critlen)) {
1163 if (purged_acs(tterm)) {
1164 acs_chars = ABSENT_STRING;
1165 SHOW_WHY("# (acsc removed to fit entry within %d bytes)\n",
1170 if (!changed || (FMT_ENTRY() > critlen)) {
1171 int oldversion = tversion;
1174 SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1179 && kill_labels(tterm, len - critlen)) {
1180 SHOW_WHY("# (some labels capabilities suppressed to fit entry within %d bytes)\n",
1185 && kill_fkeys(tterm, len - critlen)) {
1186 SHOW_WHY("# (some function-key capabilities suppressed to fit entry within %d bytes)\n",
1190 if (len > critlen) {
1191 (void) fprintf(stderr,
1192 "warning: %s entry is %d bytes long\n",
1193 _nc_first_name(tterm->term_names),
1195 SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
1198 tversion = oldversion;
1200 set_attributes = save_sgr;
1201 *tterm = save_tterm;
1203 } else if (!version_filter(STRING, STR_IDX(acs_chars))) {
1204 save_tterm = *tterm;
1205 if (purged_acs(tterm)) {
1208 *tterm = save_tterm;
1213 dump_uses(const char *name, bool infodump)
1214 /* dump "use=" clauses in the appropriate format */
1216 char buffer[MAX_TERMINFO_LENGTH];
1218 if (outform == F_TERMCAP || outform == F_TCONVERR)
1220 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1221 "%s%s", infodump ? "use=" : "tc=", name);
1222 wrap_concat(buffer);
1229 * Trim any remaining whitespace.
1231 if (outbuf.used != 0) {
1232 bool infodump = (outform != F_TERMCAP && outform != F_TCONVERR);
1233 char delim = (char) (infodump ? ',' : ':');
1236 for (j = (int) outbuf.used - 1; j > 0; --j) {
1237 char ch = outbuf.text[j];
1240 } else if (isspace(UChar(ch))) {
1241 outbuf.used = (size_t) j;
1242 } else if (!infodump && ch == '\\') {
1243 outbuf.used = (size_t) j;
1244 } else if (ch == delim && (j == 0 || outbuf.text[j - 1] != '\\')) {
1245 outbuf.used = (size_t) (j + 1);
1250 outbuf.text[outbuf.used] = '\0';
1252 (void) fputs(outbuf.text, stdout);
1254 return (int) outbuf.used;
1258 compare_entry(PredHook hook,
1259 TERMTYPE *tp GCC_UNUSED,
1261 /* compare two entries */
1264 NCURSES_CONST char *name;
1267 fputs(" comparing booleans.\n", stdout);
1268 for_each_boolean(j, tp) {
1269 i = BoolIndirect(j);
1270 name = ExtBoolname(tp, (int) i, bool_names);
1272 if (isObsolete(outform, name))
1275 (*hook) (CMP_BOOLEAN, i, name);
1279 fputs(" comparing numbers.\n", stdout);
1280 for_each_number(j, tp) {
1282 name = ExtNumname(tp, (int) i, num_names);
1284 if (isObsolete(outform, name))
1287 (*hook) (CMP_NUMBER, i, name);
1291 fputs(" comparing strings.\n", stdout);
1292 for_each_string(j, tp) {
1294 name = ExtStrname(tp, (int) i, str_names);
1296 if (isObsolete(outform, name))
1299 (*hook) (CMP_STRING, i, name);
1302 /* (void) fputs(" comparing use entries.\n", stdout); */
1303 (*hook) (CMP_USE, 0, "use");
1307 #define NOTSET(s) ((s) == 0)
1310 * This bit of legerdemain turns all the terminfo variable names into
1311 * references to locations in the arrays Booleans, Numbers, and Strings ---
1312 * precisely what's needed.
1318 set_obsolete_termcaps(TERMTYPE *tp)
1320 #include "capdefaults.c"
1324 * Convert an alternate-character-set string to canonical form: sorted and
1328 repair_acsc(TERMTYPE *tp)
1330 if (VALID_STRING(acs_chars)) {
1336 bool fix_needed = FALSE;
1338 for (n = 0, source = 0; acs_chars[n] != 0; n++) {
1339 target = UChar(acs_chars[n]);
1340 if (source >= target) {
1345 if (acs_chars[n + 1])
1349 memset(mapped, 0, sizeof(mapped));
1350 for (n = 0; acs_chars[n] != 0; n++) {
1351 source = UChar(acs_chars[n]);
1352 if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
1353 mapped[source] = (char) target;
1356 extra = (char) source;
1359 for (n = m = 0; n < sizeof(mapped); n++) {
1361 acs_chars[m++] = (char) n;
1362 acs_chars[m++] = mapped[n];
1366 acs_chars[m++] = extra; /* garbage in, garbage out */