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.115 2015/05/27 00:57:40 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 (OkIndex(np->nte_index, check) \
160 && check[np->nte_index]) \
161 return (result[np->nte_index])
164 nametrans(const char *name)
165 /* translate a capability name from termcap to 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);
188 dump_init(const char *version,
196 /* set up for entry display */
205 tversion = V_ALLCAPS;
206 else if (!strcmp(version, "SVr1") || !strcmp(version, "SVR1")
207 || !strcmp(version, "Ultrix"))
209 else if (!strcmp(version, "HP"))
211 else if (!strcmp(version, "AIX"))
213 else if (!strcmp(version, "BSD"))
216 tversion = V_ALLCAPS;
218 /* implement display modes */
219 switch (outform = mode) {
222 bool_names = boolnames;
223 num_names = numnames;
224 str_names = strnames;
225 separator = (twidth > 0 && theight > 1) ? ", " : ",";
230 bool_names = boolfnames;
231 num_names = numfnames;
232 str_names = strfnames;
233 separator = (twidth > 0 && theight > 1) ? ", " : ",";
239 bool_names = boolcodes;
240 num_names = numcodes;
241 str_names = strcodes;
247 /* implement sort modes */
248 switch (sortmode = sort) {
251 (void) fprintf(stderr,
252 "%s: sorting by term structure order\n", _nc_progname);
257 (void) fprintf(stderr,
258 "%s: sorting by terminfo name order\n", _nc_progname);
259 bool_indirect = bool_terminfo_sort;
260 num_indirect = num_terminfo_sort;
261 str_indirect = str_terminfo_sort;
266 (void) fprintf(stderr,
267 "%s: sorting by C variable order\n", _nc_progname);
268 bool_indirect = bool_variable_sort;
269 num_indirect = num_variable_sort;
270 str_indirect = str_variable_sort;
275 (void) fprintf(stderr,
276 "%s: sorting by termcap name order\n", _nc_progname);
277 bool_indirect = bool_termcap_sort;
278 num_indirect = num_termcap_sort;
279 str_indirect = str_termcap_sort;
284 (void) fprintf(stderr,
285 "%s: width = %d, tversion = %d, outform = %d\n",
286 _nc_progname, width, tversion, outform);
289 static TERMTYPE *cur_type;
292 dump_predicate(PredType type, PredIdx idx)
293 /* predicate function to use for ordinary decompilation */
297 return (cur_type->Booleans[idx] == FALSE)
298 ? FAIL : cur_type->Booleans[idx];
301 return (cur_type->Numbers[idx] == ABSENT_NUMERIC)
302 ? FAIL : cur_type->Numbers[idx];
305 return (cur_type->Strings[idx] != ABSENT_STRING)
309 return (FALSE); /* pacify compiler */
312 static void set_obsolete_termcaps(TERMTYPE *tp);
314 /* is this the index of a function key string? */
316 (((i) >= STR_IDX(key_f0) && \
317 (i) <= STR_IDX(key_f9)) || \
318 ((i) >= STR_IDX(key_f11) && \
319 (i) <= STR_IDX(key_f63)))
322 * If we configure with a different Caps file, the offsets into the arrays
323 * will change. So we use an address expression.
325 #define BOOL_IDX(name) (PredType) (&(name) - &(CUR Booleans[0]))
326 #define NUM_IDX(name) (PredType) (&(name) - &(CUR Numbers[0]))
327 #define STR_IDX(name) (PredType) (&(name) - &(CUR Strings[0]))
330 version_filter(PredType type, PredIdx idx)
331 /* filter out capabilities we may want to suppress */
334 case V_ALLCAPS: /* SVr4, XSI Curses */
337 case V_SVR1: /* System V Release 1, Ultrix */
340 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
342 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
344 return ((idx <= STR_IDX(prtr_non)) ? TRUE : FALSE);
348 case V_HPUX: /* Hewlett-Packard */
351 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
353 return ((idx <= NUM_IDX(label_width)) ? TRUE : FALSE);
355 if (idx <= STR_IDX(prtr_non))
357 else if (FNKEY(idx)) /* function keys */
359 else if (idx == STR_IDX(plab_norm)
360 || idx == STR_IDX(label_on)
361 || idx == STR_IDX(label_off))
368 case V_AIX: /* AIX */
371 return ((idx <= BOOL_IDX(xon_xoff)) ? TRUE : FALSE);
373 return ((idx <= NUM_IDX(width_status_line)) ? TRUE : FALSE);
375 if (idx <= STR_IDX(prtr_non))
377 else if (FNKEY(idx)) /* function keys */
384 #define is_termcap(type) (OkIndex(idx, type##_from_termcap) && \
385 type##_from_termcap[idx])
387 case V_BSD: /* BSD */
390 return is_termcap(bool);
392 return is_termcap(num);
394 return is_termcap(str);
399 return (FALSE); /* pacify the compiler */
405 while (outbuf.used > 0 && outbuf.text[outbuf.used - 1] == ' ')
406 outbuf.text[--outbuf.used] = '\0';
414 strcpy_DYN(&outbuf, trailer);
419 wrap_concat(const char *src)
421 size_t need = strlen(src);
422 size_t want = strlen(separator) + need;
425 && column + (int) want > width) {
428 strcpy_DYN(&outbuf, src);
429 strcpy_DYN(&outbuf, separator);
430 column += (int) need;
433 #define IGNORE_SEP_TRAIL(first,last,sep_trail) \
434 if ((size_t)(last - first) > sizeof(sep_trail)-1 \
435 && !strncmp(first, sep_trail, sizeof(sep_trail)-1)) \
436 first += sizeof(sep_trail)-2
438 /* Returns the nominal length of the buffer assuming it is termcap format,
439 * i.e., the continuation sequence is treated as a single character ":".
441 * There are several implementations of termcap which read the text into a
442 * fixed-size buffer. Generally they strip the newlines from the text, but may
443 * not do it until after the buffer is read. Also, "tc=" resolution may be
444 * expanded in the same buffer. This function is useful for measuring the size
445 * of the best fixed-buffer implementation; the worst case may be much worse.
447 #ifdef TEST_TERMCAP_LENGTH
449 termcap_length(const char *src)
451 static const char pattern[] = ":\\\n\t:";
454 const char *const t = src + strlen(src);
456 while (*src != '\0') {
457 IGNORE_SEP_TRAIL(src, t, pattern);
464 #define termcap_length(src) strlen(src)
468 indent_DYN(DYNBUF * buffer, int level)
472 for (n = 0; n < level; n++)
473 strncpy_DYN(buffer, "\t", (size_t) 1);
477 has_params(const char *src)
480 int len = (int) strlen(src);
485 for (n = 0; n < len - 1; ++n) {
486 if (!strncmp(src + n, "%p", (size_t) 2)) {
488 } else if (!strncmp(src + n, "%;", (size_t) 2)) {
495 result = ((len > 50) && params);
501 fmt_complex(TERMTYPE *tterm, const char *capability, char *src, int level)
503 bool percent = FALSE;
504 bool params = has_params(src);
506 while (*src != '\0') {
510 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
516 case 't': /* "then" */
517 case 'e': /* "else" */
520 tmpbuf.text[tmpbuf.used - 1] = '\n';
521 /* treat a "%e" as else-if, on the same level */
523 indent_DYN(&tmpbuf, level);
524 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
525 strncpy_DYN(&tmpbuf, src, (size_t) 1);
527 params = has_params(src);
528 if (!params && *src != '\0' && *src != '%') {
529 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
530 indent_DYN(&tmpbuf, level + 1);
533 indent_DYN(&tmpbuf, level + 1);
534 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
535 strncpy_DYN(&tmpbuf, src, (size_t) 1);
537 src = fmt_complex(tterm, capability, src, level + 1);
538 if (*src != '\0' && *src != '%') {
539 strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
540 indent_DYN(&tmpbuf, level + 1);
542 } else if (level == 1) {
544 _nc_warning("%s: %%%c without %%? in %s",
545 _nc_first_name(tterm->term_names),
552 case ';': /* "endif" */
556 tmpbuf.text[tmpbuf.used - 1] = '\n';
557 indent_DYN(&tmpbuf, level);
558 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
559 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
562 && (strchr("?e;", src[1])) == 0) {
563 tmpbuf.text[tmpbuf.used++] = '\n';
564 indent_DYN(&tmpbuf, level);
569 _nc_warning("%s: %%; without %%? in %s",
570 _nc_first_name(tterm->term_names),
575 if (percent && params) {
576 tmpbuf.text[tmpbuf.used - 1] = '\n';
577 indent_DYN(&tmpbuf, level + 1);
578 strncpy_DYN(&tmpbuf, "%", (size_t) 1);
584 strncpy_DYN(&tmpbuf, "\\s", (size_t) 2);
591 strncpy_DYN(&tmpbuf, src++, (size_t) 1);
596 #define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap)
600 fmt_entry(TERMTYPE *tterm,
603 int suppress_untranslatable,
608 char buffer[MAX_TERMINFO_LENGTH + EXTRA_CAP];
610 NCURSES_CONST char *name;
612 PredIdx num_bools = 0;
613 PredIdx num_values = 0;
614 PredIdx num_strings = 0;
617 #define WRAP_CONCAT \
618 wrap_concat(buffer); \
621 len = 12; /* terminfo file-header */
625 pred = dump_predicate;
628 strcpy_DYN(&outbuf, 0);
630 column = INDENT; /* FIXME: workaround to prevent empty lines */
632 strcpy_DYN(&outbuf, tterm->term_names);
635 * Colon is legal in terminfo descriptions, but not in termcap.
638 char *p = outbuf.text;
646 strcpy_DYN(&outbuf, separator);
647 column = (int) outbuf.used;
652 for_each_boolean(j, tterm) {
654 name = ExtBoolname(tterm, (int) i, bool_names);
655 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
657 if (!version_filter(BOOLEAN, i))
659 else if (isObsolete(outform, name))
662 predval = pred(BOOLEAN, i);
663 if (predval != FAIL) {
664 _nc_STRCPY(buffer, name, sizeof(buffer));
666 _nc_STRCAT(buffer, "@", sizeof(buffer));
667 else if (i + 1 > num_bools)
673 if (column != INDENT && height > 1)
676 for_each_number(j, tterm) {
678 name = ExtNumname(tterm, (int) i, num_names);
679 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
681 if (!version_filter(NUMBER, i))
683 else if (isObsolete(outform, name))
686 predval = pred(NUMBER, i);
687 if (predval != FAIL) {
688 if (tterm->Numbers[i] < 0) {
689 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
692 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
693 "%s#%d", name, tterm->Numbers[i]);
694 if (i + 1 > num_values)
701 if (column != INDENT && height > 1)
704 len += (int) (num_bools
706 + strlen(tterm->term_names) + 1);
712 if (outform == F_TERMCAP) {
713 if (termcap_reset != ABSENT_STRING) {
714 if (init_3string != ABSENT_STRING
715 && !strcmp(init_3string, termcap_reset))
716 DISCARD(init_3string);
718 if (reset_2string != ABSENT_STRING
719 && !strcmp(reset_2string, termcap_reset))
720 DISCARD(reset_2string);
724 for_each_string(j, tterm) {
726 name = ExtStrname(tterm, (int) i, str_names);
727 assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
729 capability = tterm->Strings[i];
731 if (!version_filter(STRING, i))
733 else if (isObsolete(outform, name))
738 * Extended names can be longer than 2 characters, but termcap programs
739 * cannot read those (filter them out).
741 if (outform == F_TERMCAP && (strlen(name) > 2))
745 if (outform == F_TERMCAP) {
747 * Some older versions of vi want rmir/smir to be defined
748 * for ich/ich1 to work. If they're not defined, force
749 * them to be output as defined and empty.
751 if (PRESENT(insert_character) || PRESENT(parm_ich)) {
752 if (SAME_CAP(i, enter_insert_mode)
753 && enter_insert_mode == ABSENT_STRING) {
754 _nc_STRCPY(buffer, "im=", sizeof(buffer));
759 if (SAME_CAP(i, exit_insert_mode)
760 && exit_insert_mode == ABSENT_STRING) {
761 _nc_STRCPY(buffer, "ei=", sizeof(buffer));
767 * termcap applications such as screen will be confused if sgr0
768 * is translated to a string containing rmacs. Filter that out.
770 if (PRESENT(exit_attribute_mode)) {
771 if (SAME_CAP(i, exit_attribute_mode)) {
773 char *my_sgr = set_attributes;
775 set_attributes = save_sgr;
777 trimmed_sgr0 = _nc_trim_sgr0(tterm);
778 if (strcmp(capability, trimmed_sgr0))
779 capability = trimmed_sgr0;
781 set_attributes = my_sgr;
786 predval = pred(STRING, i);
789 if (predval != FAIL) {
790 if (capability != ABSENT_STRING
791 && i + 1 > num_strings)
794 if (!VALID_STRING(capability)) {
795 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
798 } else if (outform == F_TERMCAP || outform == F_TCONVERR) {
799 char *srccap = _nc_tic_expand(capability, TRUE, numbers);
800 int params = (((i < (int) SIZEOF(parametrized)) &&
805 : has_params(srccap)));
806 char *cv = _nc_infotocap(name, srccap, params);
809 if (outform == F_TCONVERR) {
810 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
811 "%s=!!! %s WILL NOT CONVERT !!!",
813 } else if (suppress_untranslatable) {
816 char *s = srccap, *d = buffer;
817 _nc_SPRINTF(d, _nc_SLIMIT(sizeof(buffer)) "..%s=", name);
819 while ((*d = *s++) != 0) {
823 } else if (*d == '\\') {
830 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
833 len += (int) strlen(capability) + 1;
836 char *src = _nc_tic_expand(capability,
837 outform == F_TERMINFO, numbers);
839 strcpy_DYN(&tmpbuf, 0);
840 strcpy_DYN(&tmpbuf, name);
841 strcpy_DYN(&tmpbuf, "=");
843 && (outform == F_TERMINFO
844 || outform == F_VARIABLE)) {
845 fmt_complex(tterm, name, src, 1);
847 strcpy_DYN(&tmpbuf, src);
849 len += (int) strlen(capability) + 1;
850 wrap_concat(tmpbuf.text);
854 /* e.g., trimmed_sgr0 */
855 if (capability != ABSENT_STRING &&
856 capability != CANCELLED_STRING &&
857 capability != tterm->Strings[i])
860 len += (int) (num_strings * 2);
863 * This piece of code should be an effective inverse of the functions
864 * postprocess_terminfo() and postprocess_terminfo() in parse_entry.c.
865 * Much more work should be done on this to support dumping termcaps.
867 if (tversion == V_HPUX) {
868 if (VALID_STRING(memory_lock)) {
869 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
870 "meml=%s", memory_lock);
873 if (VALID_STRING(memory_unlock)) {
874 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
875 "memu=%s", memory_unlock);
878 } else if (tversion == V_AIX) {
879 if (VALID_STRING(acs_chars)) {
881 const char *acstrans = "lqkxjmwuvtn";
883 char *tp, *sp, boxchars[11];
886 for (cp = acstrans; *cp; cp++) {
887 sp = (strchr) (acs_chars, *cp);
898 char *tmp = _nc_tic_expand(boxchars,
899 (outform == F_TERMINFO),
901 _nc_STRCPY(buffer, "box1=", sizeof(buffer));
902 while (*tmp != '\0') {
903 size_t have = strlen(buffer);
904 size_t next = strlen(tmp);
905 size_t want = have + next + 1;
910 * If the expanded string is too long for the buffer,
911 * chop it off and save the location where we chopped it.
913 if (want >= sizeof(buffer)) {
917 _nc_STRCAT(buffer, tmp, sizeof(buffer));
920 * If we chopped the buffer, replace the missing piece and
921 * shift everything to append the remainder.
926 while ((tmp[next] = tmp[last + next]) != '\0') {
939 * kludge: trim off trailer to avoid an extra blank line
940 * in infocmp -u output when there are no string differences
943 bool trimmed = FALSE;
944 j = (PredIdx) outbuf.used;
946 && outbuf.text[j - 1] == '\t'
947 && outbuf.text[j - 2] == '\n') {
951 && outbuf.text[j - 1] == ':'
952 && outbuf.text[j - 2] == '\t'
953 && outbuf.text[j - 3] == '\n'
954 && outbuf.text[j - 4] == '\\') {
959 outbuf.text[outbuf.used] = '\0';
961 strcpy_DYN(&outbuf, " ");
965 fprintf(stderr, "num_bools = %d\n", num_bools);
966 fprintf(stderr, "num_values = %d\n", num_values);
967 fprintf(stderr, "num_strings = %d\n", num_strings);
968 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
969 tterm->term_names, len, outbuf.used, outbuf.text);
972 * Here's where we use infodump to trigger a more stringent length check
973 * for termcap-translation purposes.
974 * Return the length of the raw entry, without tc= expansions,
975 * It gives an idea of which entries are deadly to even *scan past*,
976 * as opposed to *use*.
978 return (infodump ? len : (int) termcap_length(outbuf.text));
982 kill_string(TERMTYPE *tterm, char *cap)
985 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
986 if (cap == tterm->Strings[n]) {
987 tterm->Strings[n] = ABSENT_STRING;
995 find_string(TERMTYPE *tterm, char *name)
998 for (n = 0; n < NUM_STRINGS(tterm); ++n) {
999 if (version_filter(STRING, n)
1000 && !strcmp(name, strnames[n])) {
1001 char *cap = tterm->Strings[n];
1002 if (VALID_STRING(cap)) {
1008 return ABSENT_STRING;
1012 * This is used to remove function-key labels from a termcap entry to
1016 kill_labels(TERMTYPE *tterm, int target)
1023 for (n = 0; n <= 10; ++n) {
1024 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "lf%d", n);
1025 if ((cap = find_string(tterm, name)) != ABSENT_STRING
1026 && kill_string(tterm, cap)) {
1027 target -= (int) (strlen(cap) + 5);
1037 * This is used to remove function-key definitions from a termcap entry to
1041 kill_fkeys(TERMTYPE *tterm, int target)
1048 for (n = 60; n >= 0; --n) {
1049 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n);
1050 if ((cap = find_string(tterm, name)) != ABSENT_STRING
1051 && kill_string(tterm, cap)) {
1052 target -= (int) (strlen(cap) + 5);
1062 * Check if the given acsc string is a 1-1 mapping, i.e., just-like-vt100.
1063 * Also, since this is for termcap, we only care about the line-drawing map.
1065 #define isLine(c) (strchr("lmkjtuvwqxn", c) != 0)
1068 one_one_mapping(const char *mapping)
1072 if (mapping != ABSENT_STRING) {
1074 while (mapping[n] != '\0') {
1075 if (isLine(mapping[n]) &&
1076 mapping[n] != mapping[n + 1]) {
1086 #define FMT_ENTRY() \
1087 fmt_entry(tterm, pred, \
1089 suppress_untranslatable, \
1092 #define SHOW_WHY PRINTF
1095 purged_acs(TERMTYPE *tterm)
1097 bool result = FALSE;
1099 if (VALID_STRING(acs_chars)) {
1100 if (!one_one_mapping(acs_chars)) {
1101 enter_alt_charset_mode = ABSENT_STRING;
1102 exit_alt_charset_mode = ABSENT_STRING;
1103 SHOW_WHY("# (rmacs/smacs removed for consistency)\n");
1111 * Dump a single entry.
1114 dump_entry(TERMTYPE *tterm,
1115 int suppress_untranslatable,
1120 TERMTYPE save_tterm;
1125 if (outform == F_TERMCAP || outform == F_TCONVERR) {
1126 critlen = MAX_TERMCAP_LENGTH;
1127 legend = "older termcap";
1129 set_obsolete_termcaps(tterm);
1131 critlen = MAX_TERMINFO_LENGTH;
1132 legend = "terminfo";
1136 save_sgr = set_attributes;
1138 if ((FMT_ENTRY() > critlen)
1141 save_tterm = *tterm;
1142 if (!suppress_untranslatable) {
1143 SHOW_WHY("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
1145 suppress_untranslatable = TRUE;
1147 if (FMT_ENTRY() > critlen) {
1149 * We pick on sgr because it's a nice long string capability that
1150 * is really just an optimization hack. Another good candidate is
1151 * acsc since it is both long and unused by BSD termcap.
1153 bool changed = FALSE;
1157 * Extended names are most likely function-key definitions. Drop
1161 for (n = STRCOUNT; n < NUM_STRINGS(tterm); n++) {
1162 const char *name = ExtStrname(tterm, (int) n, strnames);
1164 if (VALID_STRING(tterm->Strings[n])) {
1165 set_attributes = ABSENT_STRING;
1166 /* we remove long names anyway - only report the short */
1167 if (strlen(name) <= 2) {
1168 SHOW_WHY("# (%s removed to fit entry within %d bytes)\n",
1173 if (FMT_ENTRY() <= critlen)
1178 if (VALID_STRING(set_attributes)) {
1179 set_attributes = ABSENT_STRING;
1180 SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n",
1184 if (!changed || (FMT_ENTRY() > critlen)) {
1185 if (purged_acs(tterm)) {
1186 acs_chars = ABSENT_STRING;
1187 SHOW_WHY("# (acsc removed to fit entry within %d bytes)\n",
1192 if (!changed || (FMT_ENTRY() > critlen)) {
1193 int oldversion = tversion;
1196 SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
1201 && kill_labels(tterm, len - critlen)) {
1202 SHOW_WHY("# (some labels capabilities suppressed to fit entry within %d bytes)\n",
1207 && kill_fkeys(tterm, len - critlen)) {
1208 SHOW_WHY("# (some function-key capabilities suppressed to fit entry within %d bytes)\n",
1212 if (len > critlen) {
1213 (void) fprintf(stderr,
1214 "warning: %s entry is %d bytes long\n",
1215 _nc_first_name(tterm->term_names),
1217 SHOW_WHY("# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
1220 tversion = oldversion;
1222 set_attributes = save_sgr;
1223 *tterm = save_tterm;
1225 } else if (!version_filter(STRING, STR_IDX(acs_chars))) {
1226 save_tterm = *tterm;
1227 if (purged_acs(tterm)) {
1230 *tterm = save_tterm;
1235 dump_uses(const char *name, bool infodump)
1236 /* dump "use=" clauses in the appropriate format */
1238 char buffer[MAX_TERMINFO_LENGTH];
1240 if (outform == F_TERMCAP || outform == F_TCONVERR)
1242 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
1243 "%s%s", infodump ? "use=" : "tc=", name);
1244 wrap_concat(buffer);
1251 * Trim any remaining whitespace.
1253 if (outbuf.used != 0) {
1254 bool infodump = (outform != F_TERMCAP && outform != F_TCONVERR);
1255 char delim = (char) (infodump ? ',' : ':');
1258 for (j = (int) outbuf.used - 1; j > 0; --j) {
1259 char ch = outbuf.text[j];
1262 } else if (isspace(UChar(ch))) {
1263 outbuf.used = (size_t) j;
1264 } else if (!infodump && ch == '\\') {
1265 outbuf.used = (size_t) j;
1266 } else if (ch == delim && (j == 0 || outbuf.text[j - 1] != '\\')) {
1267 outbuf.used = (size_t) (j + 1);
1272 outbuf.text[outbuf.used] = '\0';
1274 (void) fputs(outbuf.text, stdout);
1276 return (int) outbuf.used;
1280 compare_entry(PredHook hook,
1281 TERMTYPE *tp GCC_UNUSED,
1283 /* compare two entries */
1286 NCURSES_CONST char *name;
1289 fputs(" comparing booleans.\n", stdout);
1290 for_each_boolean(j, tp) {
1291 i = BoolIndirect(j);
1292 name = ExtBoolname(tp, (int) i, bool_names);
1294 if (isObsolete(outform, name))
1297 (*hook) (CMP_BOOLEAN, i, name);
1301 fputs(" comparing numbers.\n", stdout);
1302 for_each_number(j, tp) {
1304 name = ExtNumname(tp, (int) i, num_names);
1306 if (isObsolete(outform, name))
1309 (*hook) (CMP_NUMBER, i, name);
1313 fputs(" comparing strings.\n", stdout);
1314 for_each_string(j, tp) {
1316 name = ExtStrname(tp, (int) i, str_names);
1318 if (isObsolete(outform, name))
1321 (*hook) (CMP_STRING, i, name);
1324 /* (void) fputs(" comparing use entries.\n", stdout); */
1325 (*hook) (CMP_USE, 0, "use");
1329 #define NOTSET(s) ((s) == 0)
1332 * This bit of legerdemain turns all the terminfo variable names into
1333 * references to locations in the arrays Booleans, Numbers, and Strings ---
1334 * precisely what's needed.
1340 set_obsolete_termcaps(TERMTYPE *tp)
1342 #include "capdefaults.c"
1346 * Convert an alternate-character-set string to canonical form: sorted and
1350 repair_acsc(TERMTYPE *tp)
1352 if (VALID_STRING(acs_chars)) {
1358 bool fix_needed = FALSE;
1360 for (n = 0, source = 0; acs_chars[n] != 0; n++) {
1361 target = UChar(acs_chars[n]);
1362 if (source >= target) {
1367 if (acs_chars[n + 1])
1371 memset(mapped, 0, sizeof(mapped));
1372 for (n = 0; acs_chars[n] != 0; n++) {
1373 source = UChar(acs_chars[n]);
1374 if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
1375 mapped[source] = (char) target;
1378 extra = (char) source;
1381 for (n = m = 0; n < sizeof(mapped); n++) {
1383 acs_chars[m++] = (char) n;
1384 acs_chars[m++] = mapped[n];
1388 acs_chars[m++] = extra; /* garbage in, garbage out */