1 /****************************************************************************
2 * Copyright (c) 1998-2000 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 ****************************************************************************/
34 #define __INTERNAL_CAPS_VISIBLE
35 #include <progs.priv.h>
37 #include "dump_entry.h"
38 #include "termsort.c" /* this C file is generated */
39 #include <parametrized.h> /* so is this */
41 MODULE_ID("$Id: dump_entry.c,v 1.54 2000/10/01 01:34:06 tom Exp $")
44 #define DISCARD(string) string = ABSENT_STRING
45 #define PRINTF (void) printf
53 static int tversion; /* terminfo version */
54 static int outform; /* output format to use */
55 static int sortmode; /* sort mode to use */
56 static int width = 60; /* max line width for listings */
57 static int column; /* current column, limited by 'width' */
58 static int oldcol; /* last value of column before wrap */
59 static int tracelevel; /* level of debug output */
60 static bool pretty; /* true if we format if-then-else strings */
65 /* indirection pointers for implementing sort and display modes */
66 static const int *bool_indirect, *num_indirect, *str_indirect;
67 static NCURSES_CONST char *const *bool_names;
68 static NCURSES_CONST char *const *num_names;
69 static NCURSES_CONST char *const *str_names;
71 static const char *separator, *trailer;
73 /* cover various ports and variants of terminfo */
74 #define V_ALLCAPS 0 /* all capabilities (SVr4, XSI, ncurses) */
75 #define V_SVR1 1 /* SVR1, Ultrix */
76 #define V_HPUX 2 /* HP/UX */
77 #define V_AIX 3 /* AIX */
78 #define V_BSD 4 /* BSD */
81 #define OBSOLETE(n) (!_nc_user_definable && (n[0] == 'O' && n[1] == 'T'))
83 #define OBSOLETE(n) (n[0] == 'O' && n[1] == 'T')
86 #define isObsolete(f,n) ((f == F_TERMINFO || f == F_VARIABLE) && OBSOLETE(n))
89 #define BoolIndirect(j) ((j >= BOOLCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : bool_indirect[j]))
90 #define NumIndirect(j) ((j >= NUMCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : num_indirect[j]))
91 #define StrIndirect(j) ((j >= STRCOUNT) ? (j) : ((sortmode == S_NOSORT) ? j : str_indirect[j]))
93 #define BoolIndirect(j) ((sortmode == S_NOSORT) ? (j) : bool_indirect[j])
94 #define NumIndirect(j) ((sortmode == S_NOSORT) ? (j) : num_indirect[j])
95 #define StrIndirect(j) ((sortmode == S_NOSORT) ? (j) : str_indirect[j])
99 strncpy_DYN(DYNBUF * dst, const char *src, size_t need)
101 size_t want = need + dst->used + 1;
102 if (want > dst->size) {
103 dst->size += (want + 1024); /* be generous */
104 dst->text = typeRealloc(char, dst->size, dst->text);
106 (void) strncpy(dst->text + dst->used, src, need);
108 dst->text[dst->used] = 0;
112 strcpy_DYN(DYNBUF * dst, const char *src)
118 strncpy_DYN(dst, src, strlen(src));
134 _nc_leaks_dump_entry(void)
142 nametrans(const char *name)
143 /* translate a capability name from termcap to terminfo */
145 const struct name_table_entry *np;
147 if ((np = _nc_find_entry(name, _nc_get_hash_table(0))) != 0)
148 switch (np->nte_type) {
150 if (bool_from_termcap[np->nte_index])
151 return (boolcodes[np->nte_index]);
155 if (num_from_termcap[np->nte_index])
156 return (numcodes[np->nte_index]);
160 if (str_from_termcap[np->nte_index])
161 return (strcodes[np->nte_index]);
169 dump_init(const char *version, int mode, int sort, int twidth, int traceval,
171 /* set up for entry display */
175 tracelevel = traceval;
179 tversion = V_ALLCAPS;
180 else if (!strcmp(version, "SVr1") || !strcmp(version, "SVR1")
181 || !strcmp(version, "Ultrix"))
183 else if (!strcmp(version, "HP"))
185 else if (!strcmp(version, "AIX"))
187 else if (!strcmp(version, "BSD"))
190 tversion = V_ALLCAPS;
192 /* implement display modes */
193 switch (outform = mode) {
196 bool_names = boolnames;
197 num_names = numnames;
198 str_names = strnames;
199 separator = twidth ? ", " : ",";
204 bool_names = boolfnames;
205 num_names = numfnames;
206 str_names = strfnames;
207 separator = twidth ? ", " : ",";
213 bool_names = boolcodes;
214 num_names = numcodes;
215 str_names = strcodes;
221 /* implement sort modes */
222 switch (sortmode = sort) {
225 (void) fprintf(stderr,
226 "%s: sorting by term structure order\n", _nc_progname);
231 (void) fprintf(stderr,
232 "%s: sorting by terminfo name order\n", _nc_progname);
233 bool_indirect = bool_terminfo_sort;
234 num_indirect = num_terminfo_sort;
235 str_indirect = str_terminfo_sort;
240 (void) fprintf(stderr,
241 "%s: sorting by C variable order\n", _nc_progname);
242 bool_indirect = bool_variable_sort;
243 num_indirect = num_variable_sort;
244 str_indirect = str_variable_sort;
249 (void) fprintf(stderr,
250 "%s: sorting by termcap name order\n", _nc_progname);
251 bool_indirect = bool_termcap_sort;
252 num_indirect = num_termcap_sort;
253 str_indirect = str_termcap_sort;
258 (void) fprintf(stderr,
259 "%s: width = %d, tversion = %d, outform = %d\n",
260 _nc_progname, width, tversion, outform);
263 static TERMTYPE *cur_type;
266 dump_predicate(int type, int idx)
267 /* predicate function to use for ordinary decompilation */
271 return (cur_type->Booleans[idx] == FALSE)
272 ? FAIL : cur_type->Booleans[idx];
275 return (cur_type->Numbers[idx] == ABSENT_NUMERIC)
276 ? FAIL : cur_type->Numbers[idx];
279 return (cur_type->Strings[idx] != ABSENT_STRING)
283 return (FALSE); /* pacify compiler */
286 static void set_obsolete_termcaps(TERMTYPE * tp);
288 /* is this the index of a function key string? */
289 #define FNKEY(i) (((i)<= 65 && (i)>= 75) || ((i)<= 216 && (i)>= 268))
292 version_filter(int type, int idx)
293 /* filter out capabilities we may want to suppress */
296 case V_ALLCAPS: /* SVr4, XSI Curses */
299 case V_SVR1: /* System V Release 1, Ultrix */
302 /* below and including xon_xoff */
303 return ((idx <= 20) ? TRUE : FALSE);
305 /* below and including width_status_line */
306 return ((idx <= 7) ? TRUE : FALSE);
308 /* below and including prtr_non */
309 return ((idx <= 144) ? TRUE : FALSE);
313 case V_HPUX: /* Hewlett-Packard */
316 /* below and including xon_xoff */
317 return ((idx <= 20) ? TRUE : FALSE);
319 /* below and including label_width */
320 return ((idx <= 10) ? TRUE : FALSE);
322 if (idx <= 144) /* below and including prtr_non */
324 else if (FNKEY(idx)) /* function keys */
326 else if (idx == 147 || idx == 156 || idx == 157) /* plab_norm,label_on,label_off */
333 case V_AIX: /* AIX */
336 /* below and including xon_xoff */
337 return ((idx <= 20) ? TRUE : FALSE);
339 /* below and including width_status_line */
340 return ((idx <= 7) ? TRUE : FALSE);
342 if (idx <= 144) /* below and including prtr_non */
344 else if (FNKEY(idx)) /* function keys */
351 case V_BSD: /* BSD */
354 return bool_from_termcap[idx];
356 return num_from_termcap[idx];
358 return str_from_termcap[idx];
363 return (FALSE); /* pacify the compiler */
370 strcpy_DYN(&outbuf, trailer);
375 wrap_concat(const char *src)
377 int need = strlen(src);
378 int want = strlen(separator) + need;
381 && column + want > width) {
384 strcpy_DYN(&outbuf, src);
385 strcpy_DYN(&outbuf, separator);
389 #define IGNORE_SEP_TRAIL(first,last,sep_trail) \
390 if ((size_t)(last - first) > sizeof(sep_trail)-1 \
391 && !strncmp(first, sep_trail, sizeof(sep_trail)-1)) \
392 first += sizeof(sep_trail)-2
394 /* Returns the nominal length of the buffer assuming it is termcap format,
395 * i.e., the continuation sequence is treated as a single character ":".
397 * There are several implementations of termcap which read the text into a
398 * fixed-size buffer. Generally they strip the newlines from the text, but may
399 * not do it until after the buffer is read. Also, "tc=" resolution may be
400 * expanded in the same buffer. This function is useful for measuring the size
401 * of the best fixed-buffer implementation; the worst case may be much worse.
403 #ifdef TEST_TERMCAP_LENGTH
405 termcap_length(const char *src)
407 static const char pattern[] = ":\\\n\t:";
410 const char *const t = src + strlen(src);
412 while (*src != '\0') {
413 IGNORE_SEP_TRAIL(src, t, pattern);
420 #define termcap_length(src) strlen(src)
424 fmt_complex(char *src, int level)
428 bool if_then = strstr(src, "%?") != 0;
429 bool params = !if_then && (strlen(src) > 50) && (strstr(src, "%p") != 0);
431 while (*src != '\0') {
435 strncpy_DYN(&tmpbuf, src++, 1);
441 case 't': /* "then" */
442 case 'e': /* "else" */
445 tmpbuf.text[tmpbuf.used - 1] = '\n';
446 /* treat a "%e%?" as else-if, on the same level */
447 if (!strncmp(src, "e%?", 3)) {
448 for (n = 0; n < level; n++)
449 strncpy_DYN(&tmpbuf, "\t", 1);
450 strncpy_DYN(&tmpbuf, "%", 1);
451 strncpy_DYN(&tmpbuf, src, 3);
454 for (n = 0; n <= level; n++)
455 strncpy_DYN(&tmpbuf, "\t", 1);
456 strncpy_DYN(&tmpbuf, "%", 1);
457 strncpy_DYN(&tmpbuf, src, 1);
459 src = fmt_complex(src, level + 1);
460 } else if (level == 1) {
461 _nc_warning("%%%c without %%?", *src);
467 case ';': /* "endif" */
471 tmpbuf.text[tmpbuf.used - 1] = '\n';
472 for (n = 0; n < level; n++)
473 strncpy_DYN(&tmpbuf, "\t", 1);
474 strncpy_DYN(&tmpbuf, "%", 1);
475 strncpy_DYN(&tmpbuf, src++, 1);
478 _nc_warning("%%; without %%?");
482 if (percent && params) {
483 tmpbuf.text[tmpbuf.used - 1] = '\n';
484 for (n = 0; n <= level; n++)
485 strncpy_DYN(&tmpbuf, "\t", 1);
486 strncpy_DYN(&tmpbuf, "%", 1);
494 strncpy_DYN(&tmpbuf, src++, 1);
500 fmt_entry(TERMTYPE * tterm,
501 int (*pred) (int type, int idx),
502 bool suppress_untranslatable,
507 char buffer[MAX_TERMINFO_LENGTH];
508 NCURSES_CONST char *name;
515 #define WRAP_CONCAT \
516 wrap_concat(buffer); \
519 len = 12; /* terminfo file-header */
523 pred = dump_predicate;
526 strcpy_DYN(&outbuf, 0);
527 strcpy_DYN(&outbuf, tterm->term_names);
528 strcpy_DYN(&outbuf, separator);
529 column = outbuf.used;
532 for_each_boolean(j, tterm) {
534 name = ExtBoolname(tterm, i, bool_names);
536 if (!version_filter(BOOLEAN, i))
538 else if (isObsolete(outform, name))
541 predval = pred(BOOLEAN, i);
542 if (predval != FAIL) {
543 (void) strcpy(buffer, name);
545 (void) strcat(buffer, "@");
546 else if (i + 1 > num_bools)
552 if (column != INDENT)
555 for_each_number(j, tterm) {
557 name = ExtNumname(tterm, i, num_names);
559 if (!version_filter(NUMBER, i))
561 else if (isObsolete(outform, name))
564 predval = pred(NUMBER, i);
565 if (predval != FAIL) {
566 if (tterm->Numbers[i] < 0) {
567 sprintf(buffer, "%s@", name);
569 sprintf(buffer, "%s#%d", name, tterm->Numbers[i]);
570 if (i + 1 > num_values)
577 if (column != INDENT)
582 + strlen(tterm->term_names) + 1;
588 if (outform == F_TERMCAP) {
589 if (termcap_reset != ABSENT_STRING) {
590 if (init_3string != ABSENT_STRING
591 && !strcmp(init_3string, termcap_reset))
592 DISCARD(init_3string);
594 if (reset_2string != ABSENT_STRING
595 && !strcmp(reset_2string, termcap_reset))
596 DISCARD(reset_2string);
600 for_each_string(j, tterm) {
602 name = ExtStrname(tterm, i, str_names);
604 if (!version_filter(STRING, i))
606 else if (isObsolete(outform, name))
610 * Some older versions of vi want rmir/smir to be defined
611 * for ich/ich1 to work. If they're not defined, force
612 * them to be output as defined and empty.
614 if (outform == F_TERMCAP) {
615 if (insert_character || parm_ich) {
616 if (&tterm->Strings[i] == &enter_insert_mode
617 && enter_insert_mode == ABSENT_STRING) {
618 (void) strcpy(buffer, "im=");
623 if (&tterm->Strings[i] == &exit_insert_mode
624 && exit_insert_mode == ABSENT_STRING) {
625 (void) strcpy(buffer, "ei=");
632 predval = pred(STRING, i);
635 if (predval != FAIL) {
636 if (tterm->Strings[i] != ABSENT_STRING
637 && i + 1 > num_strings)
640 if (!VALID_STRING(tterm->Strings[i])) {
641 sprintf(buffer, "%s@", name);
643 } else if (outform == F_TERMCAP || outform == F_TCONVERR) {
644 int params = (i < (int) SIZEOF(parametrized)) ? parametrized[i] : 0;
645 char *srccap = _nc_tic_expand(tterm->Strings[i], TRUE, numbers);
646 char *cv = _nc_infotocap(name, srccap, params);
649 if (outform == F_TCONVERR) {
650 sprintf(buffer, "%s=!!! %s WILL NOT CONVERT !!!",
652 } else if (suppress_untranslatable) {
655 char *s = srccap, *d = buffer;
656 sprintf(d, "..%s=", name);
658 while ((*d = *s++) != 0) {
662 } else if (*d == '\\') {
669 sprintf(buffer, "%s=%s", name, cv);
671 len += strlen(tterm->Strings[i]) + 1;
674 char *src = _nc_tic_expand(tterm->Strings[i],
675 outform == F_TERMINFO, numbers);
677 strcpy_DYN(&tmpbuf, 0);
678 strcpy_DYN(&tmpbuf, name);
679 strcpy_DYN(&tmpbuf, "=");
681 && (outform == F_TERMINFO
682 || outform == F_VARIABLE)) {
685 strcpy_DYN(&tmpbuf, src);
687 len += strlen(tterm->Strings[i]) + 1;
688 wrap_concat(tmpbuf.text);
693 len += num_strings * 2;
696 * This piece of code should be an effective inverse of the functions
697 * postprocess_terminfo and postprocess_terminfo in parse_entry.c.
698 * Much more work should be done on this to support dumping termcaps.
700 if (tversion == V_HPUX) {
702 (void) sprintf(buffer, "meml=%s", memory_lock);
706 (void) sprintf(buffer, "memu=%s", memory_unlock);
709 } else if (tversion == V_AIX) {
710 if (VALID_STRING(acs_chars)) {
712 const char *acstrans = "lqkxjmwuvtn";
714 char *tp, *sp, boxchars[11];
717 for (cp = acstrans; *cp; cp++) {
718 sp = strchr(acs_chars, *cp);
729 (void) strcpy(buffer, "box1=");
730 (void) strcat(buffer, _nc_tic_expand(boxchars,
731 outform == F_TERMINFO, numbers));
738 * kludge: trim off trailer to avoid an extra blank line
739 * in infocmp -u output when there are no string differences
742 bool trimmed = FALSE;
745 && outbuf.text[j - 1] == '\t'
746 && outbuf.text[j - 2] == '\n') {
750 && outbuf.text[j - 1] == ':'
751 && outbuf.text[j - 2] == '\t'
752 && outbuf.text[j - 3] == '\n'
753 && outbuf.text[j - 4] == '\\') {
758 outbuf.text[outbuf.used] = '\0';
763 fprintf(stderr, "num_bools = %d\n", num_bools);
764 fprintf(stderr, "num_values = %d\n", num_values);
765 fprintf(stderr, "num_strings = %d\n", num_strings);
766 fprintf(stderr, "term_names=%s, len=%d, strlen(outbuf)=%d, outbuf=%s\n",
767 tterm->term_names, len, outbuf.used, outbuf.text);
770 * Here's where we use infodump to trigger a more stringent length check
771 * for termcap-translation purposes.
772 * Return the length of the raw entry, without tc= expansions,
773 * It gives an idea of which entries are deadly to even *scan past*,
774 * as opposed to *use*.
776 return (infodump ? len : termcap_length(outbuf.text));
780 dump_entry(TERMTYPE * tterm, bool limited, int numbers, int (*pred) (int
782 /* dump a single entry */
788 if (outform == F_TERMCAP || outform == F_TCONVERR) {
789 critlen = MAX_TERMCAP_LENGTH;
790 legend = "older termcap";
792 set_obsolete_termcaps(tterm);
794 critlen = MAX_TERMINFO_LENGTH;
799 if (((len = fmt_entry(tterm, pred, FALSE, infodump, numbers)) > critlen)
801 PRINTF("# (untranslatable capabilities removed to fit entry within %d bytes)\n",
803 if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers)) > critlen) {
805 * We pick on sgr because it's a nice long string capability that
806 * is really just an optimization hack. Another good candidate is
807 * acsc since it is both long and unused by BSD termcap.
809 char *oldsgr = set_attributes;
810 char *oldacsc = acs_chars;
811 set_attributes = ABSENT_STRING;
812 PRINTF("# (sgr removed to fit entry within %d bytes)\n",
814 if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers)) > critlen) {
815 acs_chars = ABSENT_STRING;
816 PRINTF("# (acsc removed to fit entry within %d bytes)\n",
819 if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers)) > critlen) {
820 int oldversion = tversion;
823 PRINTF("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
826 if ((len = fmt_entry(tterm, pred, TRUE, infodump, numbers))
828 (void) fprintf(stderr,
829 "warning: %s entry is %d bytes long\n",
830 _nc_first_name(tterm->term_names),
833 "# WARNING: this entry, %d bytes long, may core-dump %s libraries!\n",
836 tversion = oldversion;
838 set_attributes = oldsgr;
843 (void) fputs(outbuf.text, stdout);
848 dump_uses(const char *name, bool infodump)
849 /* dump "use=" clauses in the appropriate format */
851 char buffer[MAX_TERMINFO_LENGTH];
853 strcpy_DYN(&outbuf, 0);
854 (void) sprintf(buffer, "%s%s", infodump ? "use=" : "tc=", name);
856 (void) fputs(outbuf.text, stdout);
861 compare_entry(void (*hook) (int t, int i, const char *name), TERMTYPE * tp
862 GCC_UNUSED, bool quiet)
863 /* compare two entries */
866 NCURSES_CONST char *name;
869 fputs(" comparing booleans.\n", stdout);
870 for_each_boolean(j, tp) {
872 name = ExtBoolname(tp, i, bool_names);
874 if (isObsolete(outform, name))
877 (*hook) (CMP_BOOLEAN, i, name);
881 fputs(" comparing numbers.\n", stdout);
882 for_each_number(j, tp) {
884 name = ExtNumname(tp, i, num_names);
886 if (isObsolete(outform, name))
889 (*hook) (CMP_NUMBER, i, name);
893 fputs(" comparing strings.\n", stdout);
894 for_each_string(j, tp) {
896 name = ExtStrname(tp, i, str_names);
898 if (isObsolete(outform, name))
901 (*hook) (CMP_STRING, i, name);
904 /* (void) fputs(" comparing use entries.\n", stdout); */
905 (*hook) (CMP_USE, 0, "use");
909 #define NOTSET(s) ((s) == 0)
912 * This bit of legerdemain turns all the terminfo variable names into
913 * references to locations in the arrays Booleans, Numbers, and Strings ---
914 * precisely what's needed.
920 set_obsolete_termcaps(TERMTYPE * tp)
922 #include "capdefaults.c"
926 * Convert an alternate-character-set string to canonical form: sorted and
930 repair_acsc(TERMTYPE * tp)
932 if (VALID_STRING(acs_chars)) {
938 bool fix_needed = FALSE;
940 for (n = 0, source = 0; acs_chars[n] != 0; n++) {
941 target = acs_chars[n];
942 if (source >= target) {
947 if (acs_chars[n + 1])
951 memset(mapped, 0, sizeof(mapped));
952 for (n = 0; acs_chars[n] != 0; n++) {
953 source = acs_chars[n];
954 if ((target = (unsigned char) acs_chars[n + 1]) != 0) {
955 mapped[source] = target;
961 for (n = m = 0; n < sizeof(mapped); n++) {
964 acs_chars[m++] = mapped[n];
968 acs_chars[m++] = extra; /* garbage in, garbage out */