1 /****************************************************************************
2 * Copyright (c) 1998-2013,2014 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.114 2014/10/18 09:32:54 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? */
313 (((i) >= STR_IDX(key_f0) && \
314 (i) <= STR_IDX(key_f9)) || \
315 ((i) >= STR_IDX(key_f11) && \
316 (i) <= STR_IDX(key_f63)))
319 * If we configure with a different Caps file, the offsets into the arrays
320 * will change. So we use an address expression.
322 #define BOOL_IDX(name) (PredType) (&(name) - &(CUR Booleans[0]))
323 #define NUM_IDX(name) (PredType) (&(name) - &(CUR Numbers[0]))
324 #define STR_IDX(name) (PredType) (&(name) - &(CUR Strings[0]))
327 version_filter(PredType type, PredIdx idx)
328 /* filter out capabilities we may want to suppress */
331 case V_ALLCAPS: /* SVr4, XSI Curses */
334 case V_SVR1: /* System V Release 1, Ultrix */
337 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
339 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
341 return ((idx <= STR_IDX(prtr_non)) ? TRUE : FALSE);
345 case V_HPUX: /* Hewlett-Packard */
348 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
350 return ((idx <= NUM_IDX(label_width)) ? TRUE : FALSE);
352 if (idx <= STR_IDX(prtr_non))
354 else if (FNKEY(idx)) /* function keys */
356 else if (idx == STR_IDX(plab_norm)
357 || idx == STR_IDX(label_on)
358 || idx == STR_IDX(label_off))
365 case V_AIX: /* AIX */
368 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
370 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
372 if (idx <= STR_IDX(prtr_non))
374 else if (FNKEY(idx)) /* function keys */
381 #define is_termcap(type) (OkIndex(idx, type##_from_termcap) && \
382 type##_from_termcap[idx])
384 case V_BSD: /* BSD */
387 return is_termcap(bool);
389 return is_termcap(num);
391 return is_termcap(str);
396 return (FALSE); /* pacify the compiler */
402 while (outbuf.used > 0 && outbuf.text[outbuf.used - 1] == ' ')
403 outbuf.text[--outbuf.used] = '\0';
411 strcpy_DYN(&outbuf, trailer);
416 wrap_concat(const char *src)
418 size_t need = strlen(src);
419 size_t want = strlen(separator) + need;
422 && column + (int) want > width) {
425 strcpy_DYN(&outbuf, src);
426 strcpy_DYN(&outbuf, separator);
427 column += (int) need;
430 #define IGNORE_SEP_TRAIL(first,last,sep_trail) \
431 if ((size_t)(last - first) > sizeof(sep_trail)-1 \
432 && !strncmp(first, sep_trail, sizeof(sep_trail)-1)) \
433 first += sizeof(sep_trail)-2
435 /* Returns the nominal length of the buffer assuming it is termcap format,
436 * i.e., the continuation sequence is treated as a single character ":".
438 * There are several implementations of termcap which read the text into a
439 * fixed-size buffer. Generally they strip the newlines from the text, but may
440 * not do it until after the buffer is read. Also, "tc=" resolution may be
441 * expanded in the same buffer. This function is useful for measuring the size
442 * of the best fixed-buffer implementation; the worst case may be much worse.
444 #ifdef TEST_TERMCAP_LENGTH
446 termcap_length(const char *src)
448 static const char pattern[] = ":\\\n\t:";
451 const char *const t = src + strlen(src);
453 while (*src != '\0') {
454 IGNORE_SEP_TRAIL(src, t, pattern);
461 #define termcap_length(src) strlen(src)
465 indent_DYN(DYNBUF * buffer, int level)
469 for (n = 0; n < level; n++)
470 strncpy_DYN(buffer, "\t", (size_t) 1);
474 has_params(const char *src)
477 int len = (int) strlen(src);
482 for (n = 0; n < len - 1; ++n) {
483 if (!strncmp(src + n, "%p", (size_t) 2)) {
485 } else if (!strncmp(src + n, "%;", (size_t) 2)) {
492 result = ((len > 50) && params);
498 fmt_complex(TERMTYPE *tterm, const char *capability, char *src, int level)
500 bool percent = FALSE;
501 bool params = has_params(src);
503 while (*src != '\0') {
507 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
513 case 't': /* "then" */
514 case 'e': /* "else" */
517 tmpbuf.text[tmpbuf.used - 1] = '\n';
518 /* treat a "%e" as else-if, on the same level */
520 indent_DYN(&tmpbuf, level);
521 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
522 strncpy_DYN(&tmpbuf, src, (size_t) 1);
524 params = has_params(src);
525 if (!params && *src != '\0' && *src != '%') {
526 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
527 indent_DYN(&tmpbuf, level + 1);
530 indent_DYN(&tmpbuf, level + 1);
531 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
532 strncpy_DYN(&tmpbuf, src, (size_t) 1);
534 src = fmt_complex(tterm, capability, src, level + 1);
535 if (*src != '\0' && *src != '%') {
536 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
537 indent_DYN(&tmpbuf, level + 1);
539 } else if (level == 1) {
540 _nc_warning("%s: %%%c without %%? in %s",
541 _nc_first_name(tterm->term_names),
548 case ';': /* "endif" */
552 tmpbuf.text[tmpbuf.used - 1] = '\n';
553 indent_DYN(&tmpbuf, level);
554 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
555 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
558 && (strchr("?e;", src[1])) == 0) {
559 tmpbuf.text[tmpbuf.used++] = '\n';
560 indent_DYN(&tmpbuf, level);
564 _nc_warning("%s: %%; without %%? in %s",
565 _nc_first_name(tterm->term_names),
570 if (percent && params) {
571 tmpbuf.text[tmpbuf.used - 1] = '\n';
572 indent_DYN(&tmpbuf, level + 1);
573 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
579 strncpy_DYN(&tmpbuf, "\\s", (size_t) 2);
586 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
591 #define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap)
595 fmt_entry(TERMTYPE *tterm,
598 int suppress_untranslatable,
603 char buffer[MAX_TERMINFO_LENGTH + EXTRA_CAP];
605 NCURSES_CONST char *name;
607 PredIdx num_bools = 0;
608 PredIdx num_values = 0;
609 PredIdx num_strings = 0;
612 #define WRAP_CONCAT \
613 wrap_concat(buffer); \
616 len = 12; /* terminfo file-header */
620 pred = dump_predicate;
623 strcpy_DYN(&outbuf, 0);
625 column = INDENT; /* FIXME: workaround to prevent empty lines */
627 strcpy_DYN(&outbuf, tterm->term_names);
630 * Colon is legal in terminfo descriptions, but not in termcap.
633 char *p = outbuf.text;
641 strcpy_DYN(&outbuf, separator);
642 column = (int) outbuf.used;
647 for_each_boolean(j, tterm) {
649 name = ExtBoolname(tterm, (int) i, bool_names);
650 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
652 if (!version_filter(BOOLEAN, i))
654 else if (isObsolete(outform, name))
657 predval = pred(BOOLEAN, i);
658 if (predval != FAIL) {
659 _nc_STRCPY(buffer, name, sizeof(buffer));
661 _nc_STRCAT(buffer, "@", sizeof(buffer));
662 else if (i + 1 > num_bools)
668 if (column != INDENT && height > 1)
671 for_each_number(j, tterm) {
673 name = ExtNumname(tterm, (int) i, num_names);
674 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
676 if (!version_filter(NUMBER, i))
678 else if (isObsolete(outform, name))
681 predval = pred(NUMBER, i);
682 if (predval != FAIL) {
683 if (tterm->Numbers[i] < 0) {
684 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
687 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
688 "%s#%d", name, tterm->Numbers[i]);
689 if (i + 1 > num_values)
696 if (column != INDENT && height > 1)
699 len += (int) (num_bools
701 + strlen(tterm->term_names) + 1);
707 if (outform == F_TERMCAP) {
708 if (termcap_reset != ABSENT_STRING) {
709 if (init_3string != ABSENT_STRING
710 && !strcmp(init_3string, termcap_reset))
711 DISCARD(init_3string);
713 if (reset_2string != ABSENT_STRING
714 && !strcmp(reset_2string, termcap_reset))
715 DISCARD(reset_2string);
719 for_each_string(j, tterm) {
721 name = ExtStrname(tterm, (int) i, str_names);
722 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
724 capability = tterm->Strings[i];
726 if (!version_filter(STRING, i))
728 else if (isObsolete(outform, name))
733 * Extended names can be longer than 2 characters, but termcap programs
734 * cannot read those (filter them out).
736 if (outform == F_TERMCAP && (strlen(name) > 2))
740 if (outform == F_TERMCAP) {
742 * Some older versions of vi want rmir/smir to be defined
743 * for ich/ich1 to work. If they're not defined, force
744 * them to be output as defined and empty.
746 if (PRESENT(insert_character) || PRESENT(parm_ich)) {
747 if (SAME_CAP(i, enter_insert_mode)
748 && enter_insert_mode == ABSENT_STRING) {
749 _nc_STRCPY(buffer, "im=", sizeof(buffer));
754 if (SAME_CAP(i, exit_insert_mode)
755 && exit_insert_mode == ABSENT_STRING) {
756 _nc_STRCPY(buffer, "ei=", sizeof(buffer));
762 * termcap applications such as screen will be confused if sgr0
763 * is translated to a string containing rmacs. Filter that out.
765 if (PRESENT(exit_attribute_mode)) {
766 if (SAME_CAP(i, exit_attribute_mode)) {
768 char *my_sgr = set_attributes;
770 set_attributes = save_sgr;
772 trimmed_sgr0 = _nc_trim_sgr0(tterm);
773 if (strcmp(capability, trimmed_sgr0))
774 capability = trimmed_sgr0;
776 set_attributes = my_sgr;
781 predval = pred(STRING, i);
784 if (predval != FAIL) {
785 if (capability != ABSENT_STRING
786 && i + 1 > num_strings)
789 if (!VALID_STRING(capability)) {
790 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
793 } else if (outform == F_TERMCAP || outform == F_TCONVERR) {
794 char *srccap = _nc_tic_expand(capability, TRUE, numbers);
795 int params = (((i < (int) SIZEOF(parametrized)) &&
800 : has_params(srccap)));
801 char *cv = _nc_infotocap(name, srccap, params);
804 if (outform == F_TCONVERR) {
805 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
806 "%s=!!! %s WILL NOT CONVERT !!!",
808 } else if (suppress_untranslatable) {
811 char *s = srccap, *d = buffer;
812 _nc_SPRINTF(d, _nc_SLIMIT(sizeof(buffer)) "..%s=", name);
814 while ((*d = *s++) != 0) {
818 } else if (*d == '\\') {
825 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
828 len += (int) strlen(capability) + 1;
831 char *src = _nc_tic_expand(capability,
832 outform == F_TERMINFO, numbers);
834 strcpy_DYN(&tmpbuf, 0);
835 strcpy_DYN(&tmpbuf, name);
836 strcpy_DYN(&tmpbuf, "=");
838 && (outform == F_TERMINFO
839 || outform == F_VARIABLE)) {
840 fmt_complex(tterm, name, src, 1);
842 strcpy_DYN(&tmpbuf, src);
844 len += (int) strlen(capability) + 1;
845 wrap_concat(tmpbuf.text);
849 /* e.g., trimmed_sgr0 */
850 if (capability != ABSENT_STRING &&
851 capability != CANCELLED_STRING &&
852 capability != tterm->Strings[i])
855 len += (int) (num_strings * 2);
858 * This piece of code should be an effective inverse of the functions
859 * postprocess_terminfo() and postprocess_terminfo() in parse_entry.c.
860 * Much more work should be done on this to support dumping termcaps.
862 if (tversion == V_HPUX) {
863 if (VALID_STRING(memory_lock)) {
864 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
865 "meml=%s", memory_lock);
868 if (VALID_STRING(memory_unlock)) {
869 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
870 "memu=%s", memory_unlock);
873 } else if (tversion == V_AIX) {
874 if (VALID_STRING(acs_chars)) {
876 const char *acstrans = "lqkxjmwuvtn";
878 char *tp, *sp, boxchars[11];
881 for (cp = acstrans; *cp; cp++) {
882 sp = (strchr) (acs_chars, *cp);
893 char *tmp = _nc_tic_expand(boxchars,
894 (outform == F_TERMINFO),
896 _nc_STRCPY(buffer, "box1=", sizeof(buffer));
897 while (*tmp != '\0') {
898 size_t have = strlen(buffer);
899 size_t next = strlen(tmp);
900 size_t want = have + next + 1;
905 * If the expanded string is too long for the buffer,
906 * chop it off and save the location where we chopped it.
908 if (want >= sizeof(buffer)) {
912 _nc_STRCAT(buffer, tmp, sizeof(buffer));
915 * If we chopped the buffer, replace the missing piece and
916 * shift everything to append the remainder.
921 while ((tmp[next] = tmp[last + next]) != '\0') {
934 * kludge: trim off trailer to avoid an extra blank line
935 * in infocmp -u output when there are no string differences
938 bool trimmed = FALSE;
939 j = (PredIdx) outbuf.used;
941 && outbuf.text[j - 1] == '\t'
942 && outbuf.text[j - 2] == '\n') {
946 && outbuf.text[j - 1] == ':'
947 && outbuf.text[j - 2] == '\t'
948 && outbuf.text[j - 3] == '\n'
949 && outbuf.text[j - 4] == '\\') {
954 outbuf.text[outbuf.used] = '\0';
956 strcpy_DYN(&outbuf, " ");
960 fprintf(stderr, "num_bools = %d\n", num_bools);
961 fprintf(stderr, "num_values = %d\n", num_values);
962 fprintf(stderr, "num_strings = %d\n", num_strings);
963 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
964 tterm->term_names, len, outbuf.used, outbuf.text);
967 * Here's where we use infodump to trigger a more stringent length check
968 * for termcap-translation purposes.
969 * Return the length of the raw entry, without tc= expansions,
970 * It gives an idea of which entries are deadly to even *scan past*,
971 * as opposed to *use*.
973 return (infodump ? len : (int) termcap_length(outbuf.text));
977 kill_string(TERMTYPE *tterm, char *cap)
980 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
981 if (cap == tterm->Strings[n]) {
982 tterm->Strings[n] = ABSENT_STRING;
990 find_string(TERMTYPE *tterm, char *name)
993 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
994 if (version_filter(STRING, n)
995 && !strcmp(name, strnames[n])) {
996 char *cap = tterm->Strings[n];
997 if (VALID_STRING(cap)) {
1003 return ABSENT_STRING;
1007 * This is used to remove function-key labels from a termcap entry to
1011 kill_labels(TERMTYPE *tterm, int target)
1018 for (n = 0; n <= 10; ++n) {
1019 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "lf%d", n);
1020 if ((cap = find_string(tterm, name)) != ABSENT_STRING
1021 && kill_string(tterm, cap)) {
1022 target -= (int) (strlen(cap) + 5);
1032 * This is used to remove function-key definitions from a termcap entry to
1036 kill_fkeys(TERMTYPE *tterm, int target)
1043 for (n = 60; n >= 0; --n) {
1044 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n);
1045 if ((cap = find_string(tterm, name)) != ABSENT_STRING
1046 && kill_string(tterm, cap)) {
1047 target -= (int) (strlen(cap) + 5);
1057 * Check if the given acsc string is a 1-1 mapping, i.e., just-like-vt100.
1058 * Also, since this is for termcap, we only care about the line-drawing map.
1060 #define isLine(c) (strchr("lmkjtuvwqxn", c) != 0)
1063 one_one_mapping(const char *mapping)
1067 if (mapping != ABSENT_STRING) {
1069 while (mapping[n] != '\0') {
1070 if (isLine(mapping[n]) &&
1071 mapping[n] != mapping[n + 1]) {
1081 #define FMT_ENTRY() \
1082 fmt_entry(tterm, pred, \
1084 suppress_untranslatable, \
1087 #define SHOW_WHY PRINTF
1090 purged_acs(TERMTYPE *tterm)
1092 bool result = FALSE;
1094 if (VALID_STRING(acs_chars)) {
1095 if (!one_one_mapping(acs_chars)) {
1096 enter_alt_charset_mode = ABSENT_STRING;
1097 exit_alt_charset_mode = ABSENT_STRING;
1098 SHOW_WHY("# (rmacs/smacs removed for consistency)\n");
1106 * Dump a single entry.
1109 dump_entry(TERMTYPE *tterm,
1110 int suppress_untranslatable,
1115 TERMTYPE save_tterm;
1120 if (outform == F_TERMCAP || outform == F_TCONVERR) {
1121 critlen = MAX_TERMCAP_LENGTH;
1122 legend = "older termcap";
1124 set_obsolete_termcaps(tterm);
1126 critlen = MAX_TERMINFO_LENGTH;
1127 legend = "terminfo";
1131 save_sgr = set_attributes;
1133 if ((FMT_ENTRY() > critlen)
1136 save_tterm = *tterm;
1137 if (!suppress_untranslatable) {
1138 SHOW_WHY("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1140 suppress_untranslatable = TRUE;
1142 if (FMT_ENTRY() > critlen) {
1144 * We pick on sgr because it's a nice long string capability that
1145 * is really just an optimization hack. Another good candidate is
1146 * acsc since it is both long and unused by BSD termcap.
1148 bool changed = FALSE;
1152 * Extended names are most likely function-key definitions. Drop
1156 for (n = STRCOUNT; n < NUM_STRINGS(tterm); n++) {
1157 const char *name = ExtStrname(tterm, (int) n, strnames);
1159 if (VALID_STRING(tterm->Strings[n])) {
1160 set_attributes = ABSENT_STRING;
1161 /* we remove long names anyway - only report the short */
1162 if (strlen(name) <= 2) {
1163 SHOW_WHY("# (%s removed to fit entry within %d bytes)\n",
1168 if (FMT_ENTRY() <= critlen)
1173 if (VALID_STRING(set_attributes)) {
1174 set_attributes = ABSENT_STRING;
1175 SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n",
1179 if (!changed || (FMT_ENTRY() > critlen)) {
1180 if (purged_acs(tterm)) {
1181 acs_chars = ABSENT_STRING;
1182 SHOW_WHY("# (acsc removed to fit entry within %d bytes)\n",
1187 if (!changed || (FMT_ENTRY() > critlen)) {
1188 int oldversion = tversion;
1191 SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1196 && kill_labels(tterm, len - critlen)) {
1197 SHOW_WHY("# (some labels capabilities suppressed to fit entry within %d bytes)\n",
1202 && kill_fkeys(tterm, len - critlen)) {
1203 SHOW_WHY("# (some function-key capabilities suppressed to fit entry within %d bytes)\n",
1207 if (len > critlen) {
1208 (void) fprintf(stderr,
1209 "warning: %s entry is %d bytes long\n",
1210 _nc_first_name(tterm->term_names),
1212 SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
1215 tversion = oldversion;
1217 set_attributes = save_sgr;
1218 *tterm = save_tterm;
1220 } else if (!version_filter(STRING, STR_IDX(acs_chars))) {
1221 save_tterm = *tterm;
1222 if (purged_acs(tterm)) {
1225 *tterm = save_tterm;
1230 dump_uses(const char *name, bool infodump)
1231 /* dump "use=" clauses in the appropriate format */
1233 char buffer[MAX_TERMINFO_LENGTH];
1235 if (outform == F_TERMCAP || outform == F_TCONVERR)
1237 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1238 "%s%s", infodump ? "use=" : "tc=", name);
1239 wrap_concat(buffer);
1246 * Trim any remaining whitespace.
1248 if (outbuf.used != 0) {
1249 bool infodump = (outform != F_TERMCAP && outform != F_TCONVERR);
1250 char delim = (char) (infodump ? ',' : ':');
1253 for (j = (int) outbuf.used - 1; j > 0; --j) {
1254 char ch = outbuf.text[j];
1257 } else if (isspace(UChar(ch))) {
1258 outbuf.used = (size_t) j;
1259 } else if (!infodump && ch == '\\') {
1260 outbuf.used = (size_t) j;
1261 } else if (ch == delim && (j == 0 || outbuf.text[j - 1] != '\\')) {
1262 outbuf.used = (size_t) (j + 1);
1267 outbuf.text[outbuf.used] = '\0';
1269 (void) fputs(outbuf.text, stdout);
1271 return (int) outbuf.used;
1275 compare_entry(PredHook hook,
1276 TERMTYPE *tp GCC_UNUSED,
1278 /* compare two entries */
1281 NCURSES_CONST char *name;
1284 fputs(" comparing booleans.\n", stdout);
1285 for_each_boolean(j, tp) {
1286 i = BoolIndirect(j);
1287 name = ExtBoolname(tp, (int) i, bool_names);
1289 if (isObsolete(outform, name))
1292 (*hook) (CMP_BOOLEAN, i, name);
1296 fputs(" comparing numbers.\n", stdout);
1297 for_each_number(j, tp) {
1299 name = ExtNumname(tp, (int) i, num_names);
1301 if (isObsolete(outform, name))
1304 (*hook) (CMP_NUMBER, i, name);
1308 fputs(" comparing strings.\n", stdout);
1309 for_each_string(j, tp) {
1311 name = ExtStrname(tp, (int) i, str_names);
1313 if (isObsolete(outform, name))
1316 (*hook) (CMP_STRING, i, name);
1319 /* (void) fputs(" comparing use entries.\n", stdout); */
1320 (*hook) (CMP_USE, 0, "use");
1324 #define NOTSET(s) ((s) == 0)
1327 * This bit of legerdemain turns all the terminfo variable names into
1328 * references to locations in the arrays Booleans, Numbers, and Strings ---
1329 * precisely what's needed.
1335 set_obsolete_termcaps(TERMTYPE *tp)
1337 #include "capdefaults.c"
1341 * Convert an alternate-character-set string to canonical form: sorted and
1345 repair_acsc(TERMTYPE *tp)
1347 if (VALID_STRING(acs_chars)) {
1353 bool fix_needed = FALSE;
1355 for (n = 0, source = 0; acs_chars[n] != 0; n++) {
1356 target = UChar(acs_chars[n]);
1357 if (source >= target) {
1362 if (acs_chars[n + 1])
1366 memset(mapped, 0, sizeof(mapped));
1367 for (n = 0; acs_chars[n] != 0; n++) {
1368 source = UChar(acs_chars[n]);
1369 if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
1370 mapped[source] = (char) target;
1373 extra = (char) source;
1376 for (n = m = 0; n < sizeof(mapped); n++) {
1378 acs_chars[m++] = (char) n;
1379 acs_chars[m++] = mapped[n];
1383 acs_chars[m++] = extra; /* garbage in, garbage out */