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.118 2015/07/07 08:06:39 Werner.Fink 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;
782 if (trimmed_sgr0 != exit_attribute_mode)
786 set_attributes = my_sgr;
791 predval = pred(STRING, i);
794 if (predval != FAIL) {
795 if (capability != ABSENT_STRING
796 && i + 1 > num_strings)
799 if (!VALID_STRING(capability)) {
800 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
803 } else if (outform == F_TERMCAP || outform == F_TCONVERR) {
804 char *srccap = _nc_tic_expand(capability, TRUE, numbers);
805 int params = (((i < (int) SIZEOF(parametrized)) &&
810 : has_params(srccap)));
811 char *cv = _nc_infotocap(name, srccap, params);
814 if (outform == F_TCONVERR) {
815 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
816 "%s=!!! %s WILL NOT CONVERT !!!",
818 } else if (suppress_untranslatable) {
821 char *s = srccap, *d = buffer;
822 _nc_SPRINTF(d, _nc_SLIMIT(sizeof(buffer)) "..%s=", name);
824 while ((*d = *s++) != 0) {
828 } else if (*d == '\\') {
835 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
838 len += (int) strlen(capability) + 1;
841 char *src = _nc_tic_expand(capability,
842 outform == F_TERMINFO, numbers);
844 strcpy_DYN(&tmpbuf, 0);
845 strcpy_DYN(&tmpbuf, name);
846 strcpy_DYN(&tmpbuf, "=");
848 && (outform == F_TERMINFO
849 || outform == F_VARIABLE)) {
850 fmt_complex(tterm, name, src, 1);
852 strcpy_DYN(&tmpbuf, src);
854 len += (int) strlen(capability) + 1;
855 wrap_concat(tmpbuf.text);
859 /* e.g., trimmed_sgr0 */
860 if (capability != ABSENT_STRING &&
861 capability != CANCELLED_STRING &&
862 capability != tterm->Strings[i])
865 len += (int) (num_strings * 2);
868 * This piece of code should be an effective inverse of the functions
869 * postprocess_terminfo() and postprocess_terminfo() in parse_entry.c.
870 * Much more work should be done on this to support dumping termcaps.
872 if (tversion == V_HPUX) {
873 if (VALID_STRING(memory_lock)) {
874 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
875 "meml=%s", memory_lock);
878 if (VALID_STRING(memory_unlock)) {
879 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
880 "memu=%s", memory_unlock);
883 } else if (tversion == V_AIX) {
884 if (VALID_STRING(acs_chars)) {
886 const char *acstrans = "lqkxjmwuvtn";
888 char *tp, *sp, boxchars[11];
891 for (cp = acstrans; *cp; cp++) {
892 sp = (strchr) (acs_chars, *cp);
903 char *tmp = _nc_tic_expand(boxchars,
904 (outform == F_TERMINFO),
906 _nc_STRCPY(buffer, "box1=", sizeof(buffer));
907 while (*tmp != '\0') {
908 size_t have = strlen(buffer);
909 size_t next = strlen(tmp);
910 size_t want = have + next + 1;
915 * If the expanded string is too long for the buffer,
916 * chop it off and save the location where we chopped it.
918 if (want >= sizeof(buffer)) {
922 _nc_STRCAT(buffer, tmp, sizeof(buffer));
925 * If we chopped the buffer, replace the missing piece and
926 * shift everything to append the remainder.
931 while ((tmp[next] = tmp[last + next]) != '\0') {
944 * kludge: trim off trailer to avoid an extra blank line
945 * in infocmp -u output when there are no string differences
948 bool trimmed = FALSE;
949 j = (PredIdx) outbuf.used;
951 && outbuf.text[j - 1] == '\t'
952 && outbuf.text[j - 2] == '\n') {
956 && outbuf.text[j - 1] == ':'
957 && outbuf.text[j - 2] == '\t'
958 && outbuf.text[j - 3] == '\n'
959 && outbuf.text[j - 4] == '\\') {
964 outbuf.text[outbuf.used] = '\0';
966 strcpy_DYN(&outbuf, " ");
970 fprintf(stderr, "num_bools = %d\n", num_bools);
971 fprintf(stderr, "num_values = %d\n", num_values);
972 fprintf(stderr, "num_strings = %d\n", num_strings);
973 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
974 tterm->term_names, len, outbuf.used, outbuf.text);
977 * Here's where we use infodump to trigger a more stringent length check
978 * for termcap-translation purposes.
979 * Return the length of the raw entry, without tc= expansions,
980 * It gives an idea of which entries are deadly to even *scan past*,
981 * as opposed to *use*.
983 return (infodump ? len : (int) termcap_length(outbuf.text));
987 kill_string(TERMTYPE *tterm, char *cap)
990 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
991 if (cap == tterm->Strings[n]) {
992 tterm->Strings[n] = ABSENT_STRING;
1000 find_string(TERMTYPE *tterm, char *name)
1003 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
1004 if (version_filter(STRING, n)
1005 && !strcmp(name, strnames[n])) {
1006 char *cap = tterm->Strings[n];
1007 if (VALID_STRING(cap)) {
1013 return ABSENT_STRING;
1017 * This is used to remove function-key labels from a termcap entry to
1021 kill_labels(TERMTYPE *tterm, int target)
1028 for (n = 0; n <= 10; ++n) {
1029 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "lf%d", n);
1030 if ((cap = find_string(tterm, name)) != ABSENT_STRING
1031 && kill_string(tterm, cap)) {
1032 target -= (int) (strlen(cap) + 5);
1042 * This is used to remove function-key definitions from a termcap entry to
1046 kill_fkeys(TERMTYPE *tterm, int target)
1053 for (n = 60; n >= 0; --n) {
1054 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n);
1055 if ((cap = find_string(tterm, name)) != ABSENT_STRING
1056 && kill_string(tterm, cap)) {
1057 target -= (int) (strlen(cap) + 5);
1067 * Check if the given acsc string is a 1-1 mapping, i.e., just-like-vt100.
1068 * Also, since this is for termcap, we only care about the line-drawing map.
1070 #define isLine(c) (strchr("lmkjtuvwqxn", c) != 0)
1073 one_one_mapping(const char *mapping)
1077 if (mapping != ABSENT_STRING) {
1079 while (mapping[n] != '\0') {
1080 if (isLine(mapping[n]) &&
1081 mapping[n] != mapping[n + 1]) {
1091 #define FMT_ENTRY() \
1092 fmt_entry(tterm, pred, \
1094 suppress_untranslatable, \
1097 #define SHOW_WHY PRINTF
1100 purged_acs(TERMTYPE *tterm)
1102 bool result = FALSE;
1104 if (VALID_STRING(acs_chars)) {
1105 if (!one_one_mapping(acs_chars)) {
1106 enter_alt_charset_mode = ABSENT_STRING;
1107 exit_alt_charset_mode = ABSENT_STRING;
1108 SHOW_WHY("# (rmacs/smacs removed for consistency)\n");
1116 * Dump a single entry.
1119 dump_entry(TERMTYPE *tterm,
1120 int suppress_untranslatable,
1125 TERMTYPE save_tterm;
1130 if (outform == F_TERMCAP || outform == F_TCONVERR) {
1131 critlen = MAX_TERMCAP_LENGTH;
1132 legend = "older termcap";
1134 set_obsolete_termcaps(tterm);
1136 critlen = MAX_TERMINFO_LENGTH;
1137 legend = "terminfo";
1141 save_sgr = set_attributes;
1143 if ((FMT_ENTRY() > critlen)
1146 save_tterm = *tterm;
1147 if (!suppress_untranslatable) {
1148 SHOW_WHY("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1150 suppress_untranslatable = TRUE;
1152 if (FMT_ENTRY() > critlen) {
1154 * We pick on sgr because it's a nice long string capability that
1155 * is really just an optimization hack. Another good candidate is
1156 * acsc since it is both long and unused by BSD termcap.
1158 bool changed = FALSE;
1162 * Extended names are most likely function-key definitions. Drop
1166 for (n = STRCOUNT; n < NUM_STRINGS(tterm); n++) {
1167 const char *name = ExtStrname(tterm, (int) n, strnames);
1169 if (VALID_STRING(tterm->Strings[n])) {
1170 set_attributes = ABSENT_STRING;
1171 /* we remove long names anyway - only report the short */
1172 if (strlen(name) <= 2) {
1173 SHOW_WHY("# (%s removed to fit entry within %d bytes)\n",
1178 if (FMT_ENTRY() <= critlen)
1183 if (VALID_STRING(set_attributes)) {
1184 set_attributes = ABSENT_STRING;
1185 SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n",
1189 if (!changed || (FMT_ENTRY() > critlen)) {
1190 if (purged_acs(tterm)) {
1191 acs_chars = ABSENT_STRING;
1192 SHOW_WHY("# (acsc removed to fit entry within %d bytes)\n",
1197 if (!changed || (FMT_ENTRY() > critlen)) {
1198 int oldversion = tversion;
1201 SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1206 && kill_labels(tterm, len - critlen)) {
1207 SHOW_WHY("# (some labels capabilities suppressed to fit entry within %d bytes)\n",
1212 && kill_fkeys(tterm, len - critlen)) {
1213 SHOW_WHY("# (some function-key capabilities suppressed to fit entry within %d bytes)\n",
1217 if (len > critlen) {
1218 (void) fprintf(stderr,
1219 "warning: %s entry is %d bytes long\n",
1220 _nc_first_name(tterm->term_names),
1222 SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
1225 tversion = oldversion;
1227 set_attributes = save_sgr;
1228 *tterm = save_tterm;
1230 } else if (!version_filter(STRING, STR_IDX(acs_chars))) {
1231 save_tterm = *tterm;
1232 if (purged_acs(tterm)) {
1235 *tterm = save_tterm;
1240 dump_uses(const char *name, bool infodump)
1241 /* dump "use=" clauses in the appropriate format */
1243 char buffer[MAX_TERMINFO_LENGTH];
1245 if (outform == F_TERMCAP || outform == F_TCONVERR)
1247 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1248 "%s%s", infodump ? "use=" : "tc=", name);
1249 wrap_concat(buffer);
1256 * Trim any remaining whitespace.
1258 if (outbuf.used != 0) {
1259 bool infodump = (outform != F_TERMCAP && outform != F_TCONVERR);
1260 char delim = (char) (infodump ? ',' : ':');
1263 for (j = (int) outbuf.used - 1; j > 0; --j) {
1264 char ch = outbuf.text[j];
1267 } else if (isspace(UChar(ch))) {
1268 outbuf.used = (size_t) j;
1269 } else if (!infodump && ch == '\\') {
1270 outbuf.used = (size_t) j;
1271 } else if (ch == delim && (j == 0 || outbuf.text[j - 1] != '\\')) {
1272 outbuf.used = (size_t) (j + 1);
1277 outbuf.text[outbuf.used] = '\0';
1279 (void) fputs(outbuf.text, stdout);
1281 return (int) outbuf.used;
1285 compare_entry(PredHook hook,
1286 TERMTYPE *tp GCC_UNUSED,
1288 /* compare two entries */
1291 NCURSES_CONST char *name;
1294 fputs(" comparing booleans.\n", stdout);
1295 for_each_boolean(j, tp) {
1296 i = BoolIndirect(j);
1297 name = ExtBoolname(tp, (int) i, bool_names);
1299 if (isObsolete(outform, name))
1302 (*hook) (CMP_BOOLEAN, i, name);
1306 fputs(" comparing numbers.\n", stdout);
1307 for_each_number(j, tp) {
1309 name = ExtNumname(tp, (int) i, num_names);
1311 if (isObsolete(outform, name))
1314 (*hook) (CMP_NUMBER, i, name);
1318 fputs(" comparing strings.\n", stdout);
1319 for_each_string(j, tp) {
1321 name = ExtStrname(tp, (int) i, str_names);
1323 if (isObsolete(outform, name))
1326 (*hook) (CMP_STRING, i, name);
1329 /* (void) fputs(" comparing use entries.\n", stdout); */
1330 (*hook) (CMP_USE, 0, "use");
1334 #define NOTSET(s) ((s) == 0)
1337 * This bit of legerdemain turns all the terminfo variable names into
1338 * references to locations in the arrays Booleans, Numbers, and Strings ---
1339 * precisely what's needed.
1345 set_obsolete_termcaps(TERMTYPE *tp)
1347 #include "capdefaults.c"
1351 * Convert an alternate-character-set string to canonical form: sorted and
1355 repair_acsc(TERMTYPE *tp)
1357 if (VALID_STRING(acs_chars)) {
1363 bool fix_needed = FALSE;
1365 for (n = 0, source = 0; acs_chars[n] != 0; n++) {
1366 target = UChar(acs_chars[n]);
1367 if (source >= target) {
1372 if (acs_chars[n + 1])
1376 memset(mapped, 0, sizeof(mapped));
1377 for (n = 0; acs_chars[n] != 0; n++) {
1378 source = UChar(acs_chars[n]);
1379 if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
1380 mapped[source] = (char) target;
1383 extra = (char) source;
1386 for (n = m = 0; n < sizeof(mapped); n++) {
1388 acs_chars[m++] = (char) n;
1389 acs_chars[m++] = mapped[n];
1393 acs_chars[m++] = extra; /* garbage in, garbage out */