1 /****************************************************************************
2 * Copyright 2020-2021,2022 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 ****************************************************************************/
37 * infocmp.c -- decompile an entry, or compare two entries
38 * written by Eric S. Raymond
42 #include <progs.priv.h>
44 #include <dump_entry.h>
46 MODULE_ID("$Id: infocmp.c,v 1.153 2022/03/05 16:15:48 tom Exp $")
48 #define MAX_STRING 1024 /* maximum formatted string */
50 const char *_nc_progname = "infocmp";
52 typedef char path[PATH_MAX];
54 /***************************************************************************
56 * The following control variables, together with the contents of the
57 * terminfo entries, completely determine the actions of the program.
59 ***************************************************************************/
61 static ENTRY *entries; /* terminfo entries */
62 static int termcount; /* count of terminal entries */
64 static bool limited = TRUE; /* "-r" option is not set */
65 static bool quiet = FALSE;
66 static bool literal = FALSE;
67 static const char *bool_sep = ":";
68 static const char *s_absent = "NULL";
69 static const char *s_cancel = "NULL";
70 static const char *tversion; /* terminfo version selected */
71 static unsigned itrace; /* trace flag for debugging */
72 static int mwidth = 60;
73 static int mheight = 65535;
74 static int numbers = 0; /* format "%'char'" to/from "%{number}" */
75 static int outform = F_TERMINFO; /* output format */
76 static int sortmode; /* sort_mode */
78 /* main comparison mode */
80 #define C_DEFAULT 0 /* don't force comparison mode */
81 #define C_DIFFERENCE 1 /* list differences between two terminals */
82 #define C_COMMON 2 /* list common capabilities */
83 #define C_NAND 3 /* list capabilities in neither terminal */
84 #define C_USEALL 4 /* generate relative use-form entry */
85 static bool ignorepads; /* ignore pad prefixes when diffing */
94 static ENTERED *entered;
97 static GCC_NORETURN void ExitProgram(int code);
98 /* prototype is to get gcc to accept the noreturn attribute */
100 ExitProgram(int code)
104 for (n = 0; n < termcount; ++n) {
105 ENTRY *new_head = _nc_head;
106 ENTRY *new_tail = _nc_tail;
107 _nc_head = entered[n].head;
108 _nc_tail = entered[n].tail;
109 _nc_free_entries(entered[n].head);
113 _nc_leaks_dump_entry();
121 failed(const char *s)
124 ExitProgram(EXIT_FAILURE);
128 canonical_name(char *source, char *target)
129 /* extract the terminal type's primary name */
131 int limit = NAMESIZE;
133 while (--limit > 0) {
143 no_boolean(int value)
145 bool result = (value == ABSENT_BOOLEAN);
146 if (!strcmp(s_absent, s_cancel))
147 result = !VALID_BOOLEAN(value);
152 no_numeric(int value)
154 bool result = (value == ABSENT_NUMERIC);
155 if (!strcmp(s_absent, s_cancel))
156 result = !VALID_NUMERIC(value);
161 no_string(char *value)
163 bool result = (value == ABSENT_STRING);
164 if (!strcmp(s_absent, s_cancel))
165 result = !VALID_STRING(value);
169 /***************************************************************************
171 * Predicates for dump function
173 ***************************************************************************/
176 capcmp(PredIdx idx, const char *s, const char *t)
177 /* capability comparison function */
179 if (!VALID_STRING(s) && !VALID_STRING(t))
181 else if (!VALID_STRING(s) || !VALID_STRING(t))
184 if ((idx == acs_chars_index) || !ignorepads)
185 return (strcmp(s, t));
187 return (_nc_capcmp(s, t));
191 use_predicate(unsigned type, PredIdx idx)
192 /* predicate function to use for use decompilation */
202 * This assumes that multiple use entries are supposed
203 * to contribute the logical or of their boolean capabilities.
204 * This is true if we take the semantics of multiple uses to
205 * be 'each capability gets the first non-default value found
206 * in the sequence of use entries'.
208 * Note that cancelled or absent booleans are stored as FALSE,
209 * unlike numbers and strings, whose cancelled/absent state is
210 * recorded in the terminfo database.
212 for (ep = &entries[1]; ep < entries + termcount; ep++)
213 if (ep->tterm.Booleans[idx] == TRUE) {
214 is_set = entries[0].tterm.Booleans[idx];
217 if (is_set != entries[0].tterm.Booleans[idx])
225 int value = ABSENT_NUMERIC;
228 * We take the semantics of multiple uses to be 'each
229 * capability gets the first non-default value found
230 * in the sequence of use entries'.
232 for (ep = &entries[1]; ep < entries + termcount; ep++)
233 if (VALID_NUMERIC(ep->tterm.Numbers[idx])) {
234 value = ep->tterm.Numbers[idx];
238 if (value != entries[0].tterm.Numbers[idx])
239 return (value != ABSENT_NUMERIC);
246 char *termstr, *usestr = ABSENT_STRING;
248 termstr = entries[0].tterm.Strings[idx];
251 * We take the semantics of multiple uses to be 'each
252 * capability gets the first non-default value found
253 * in the sequence of use entries'.
255 for (ep = &entries[1]; ep < entries + termcount; ep++)
256 if (ep->tterm.Strings[idx]) {
257 usestr = ep->tterm.Strings[idx];
261 if (usestr == ABSENT_STRING && termstr == ABSENT_STRING)
263 else if (!usestr || !termstr || capcmp(idx, usestr, termstr))
270 return (FALSE); /* pacify compiler */
274 useeq(ENTRY * e1, ENTRY * e2)
275 /* are the use references in two entries equivalent? */
279 if (e1->nuses != e2->nuses)
282 /* Ugh...this is quadratic again */
283 for (i = 0; i < e1->nuses; i++) {
284 bool foundmatch = FALSE;
286 /* search second entry for given use reference */
287 for (j = 0; j < e2->nuses; j++)
288 if (!strcmp(e1->uses[i].name, e2->uses[j].name)) {
301 entryeq(TERMTYPE2 *t1, TERMTYPE2 *t2)
302 /* are two entries equivalent? */
306 for (i = 0; i < NUM_BOOLEANS(t1); i++)
307 if (t1->Booleans[i] != t2->Booleans[i])
310 for (i = 0; i < NUM_NUMBERS(t1); i++)
311 if (t1->Numbers[i] != t2->Numbers[i])
314 for (i = 0; i < NUM_STRINGS(t1); i++)
315 if (capcmp((PredIdx) i, t1->Strings[i], t2->Strings[i]))
321 #define TIC_EXPAND(result) _nc_tic_expand(result, outform==F_TERMINFO, numbers)
324 print_uses(ENTRY * ep, FILE *fp)
325 /* print an entry's use references */
332 for (i = 0; i < ep->nuses; i++) {
333 fputs(ep->uses[i].name, fp);
334 if (i < ep->nuses - 1)
341 dump_boolean(int val)
342 /* display the value of a boolean capability */
347 case CANCELLED_BOOLEAN:
359 dump_numeric(int val, char *buf)
360 /* display the value of a numeric capability */
364 _nc_STRCPY(buf, s_absent, MAX_STRING);
366 case CANCELLED_NUMERIC:
367 _nc_STRCPY(buf, s_cancel, MAX_STRING);
370 _nc_SPRINTF(buf, _nc_SLIMIT(MAX_STRING) "%d", val);
376 dump_string(char *val, char *buf)
377 /* display the value of a string capability */
379 if (val == ABSENT_STRING)
380 _nc_STRCPY(buf, s_absent, MAX_STRING);
381 else if (val == CANCELLED_STRING)
382 _nc_STRCPY(buf, s_cancel, MAX_STRING);
384 _nc_SPRINTF(buf, _nc_SLIMIT(MAX_STRING)
385 "'%.*s'", MAX_STRING - 3, TIC_EXPAND(val));
390 * Show "comparing..." message for the given terminal names.
393 show_comparing(char **names)
398 (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
402 (void) fprintf(stderr, "%s: dumping common capabilities\n", _nc_progname);
406 (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
411 printf("comparing %s", *names++);
413 printf(" to %s", *names++);
415 printf(", %s", *names++);
423 * ncurses stores two types of non-standard capabilities:
424 * a) capabilities listed past the "STOP-HERE" comment in the Caps file.
425 * These are used in the terminfo source file to provide data for termcaps,
426 * e.g., when there is no equivalent capability in terminfo, as well as for
427 * widely-used non-standard capabilities.
428 * b) user-definable capabilities, via "tic -x".
430 * However, if "-x" is omitted from the tic command, both types of
431 * non-standard capability are not loaded into the terminfo database. This
432 * macro is used for limit-checks against the symbols that tic uses to omit
433 * the two types of non-standard entry.
436 #define check_user_definable(n,limit) if (!_nc_user_definable && (n) > (limit)) break
438 #define check_user_definable(n,limit) if ((n) > (limit)) break
442 * Use these macros to simplify loops on C_COMMON and C_NAND:
444 #define for_each_entry() while (entries[extra].tterm.term_names)
445 #define next_entry (&(entries[extra++].tterm))
448 compare_predicate(PredType type, PredIdx idx, const char *name)
449 /* predicate function to use for entry difference reports */
451 ENTRY *e1 = &entries[0];
452 ENTRY *e2 = &entries[1];
453 char buf1[MAX_STRING];
454 char buf2[MAX_STRING];
463 check_user_definable(idx, BOOLWRITE);
464 b1 = e1->tterm.Booleans[idx];
467 b2 = next_entry->Booleans[idx];
468 if (!(no_boolean(b1) && no_boolean(b2)) && (b1 != b2))
469 (void) printf("\t%s: %s%s%s.\n",
477 if (b1 != ABSENT_BOOLEAN) {
480 b2 = next_entry->Booleans[idx];
487 (void) printf("\t%s= %s.\n", name, dump_boolean(b1));
493 if (b1 == ABSENT_BOOLEAN) {
496 b2 = next_entry->Booleans[idx];
503 (void) printf("\t!%s.\n", name);
511 check_user_definable(idx, NUMWRITE);
512 n1 = e1->tterm.Numbers[idx];
515 n2 = next_entry->Numbers[idx];
516 if (!(no_numeric(n1) && no_numeric(n2)) && n1 != n2) {
517 dump_numeric(n1, buf1);
518 dump_numeric(n2, buf2);
519 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
524 if (n1 != ABSENT_NUMERIC) {
527 n2 = next_entry->Numbers[idx];
534 dump_numeric(n1, buf1);
535 (void) printf("\t%s= %s.\n", name, buf1);
541 if (n1 == ABSENT_NUMERIC) {
544 n2 = next_entry->Numbers[idx];
551 (void) printf("\t!%s.\n", name);
559 check_user_definable(idx, STRWRITE);
560 s1 = e1->tterm.Strings[idx];
563 s2 = next_entry->Strings[idx];
564 if (!(no_string(s1) && no_string(s2)) && capcmp(idx, s1, s2)) {
565 dump_string(s1, buf1);
566 dump_string(s2, buf2);
567 if (strcmp(buf1, buf2))
568 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
573 if (s1 != ABSENT_STRING) {
576 s2 = next_entry->Strings[idx];
577 if (capcmp(idx, s1, s2) != 0) {
583 (void) printf("\t%s= '%s'.\n", name, TIC_EXPAND(s1));
589 if (s1 == ABSENT_STRING) {
592 s2 = next_entry->Strings[idx];
599 (void) printf("\t!%s.\n", name);
607 /* unlike the other modes, this compares *all* use entries */
610 if (!useeq(e1, e2)) {
611 (void) fputs("\tuse: ", stdout);
612 print_uses(e1, stdout);
614 print_uses(e2, stdout);
615 fputs(".\n", stdout);
623 e2 = &entries[extra++];
624 if (e2->nuses != e1->nuses || !useeq(e1, e2)) {
630 (void) fputs("\tuse: ", stdout);
631 print_uses(e1, stdout);
632 fputs(".\n", stdout);
641 e2 = &entries[extra++];
642 if (e2->nuses != e1->nuses) {
648 (void) printf("\t!use.\n");
656 /***************************************************************************
658 * Init string analysis
660 ***************************************************************************/
662 #define DATA(from, to) { { from }, { to } }
663 #define DATAX() DATA("", "")
670 static const assoc std_caps[] =
672 /* these are specified by X.364 and iBCS2 */
673 DATA("\033c", "RIS"), /* full reset */
674 DATA("\0337", "SC"), /* save cursor */
675 DATA("\0338", "RC"), /* restore cursor */
676 DATA("\033[r", "RSR"), /* not an X.364 mnemonic */
677 DATA("\033[m", "SGR0"), /* not an X.364 mnemonic */
678 DATA("\033[2J", "ED2"), /* clear page */
680 /* this group is specified by ISO 2022 */
681 DATA("\033(0", "ISO DEC G0"), /* enable DEC graphics for G0 */
682 DATA("\033(A", "ISO UK G0"), /* enable UK chars for G0 */
683 DATA("\033(B", "ISO US G0"), /* enable US chars for G0 */
684 DATA("\033)0", "ISO DEC G1"), /* enable DEC graphics for G1 */
685 DATA("\033)A", "ISO UK G1"), /* enable UK chars for G1 */
686 DATA("\033)B", "ISO US G1"), /* enable US chars for G1 */
688 /* these are DEC private controls widely supported by emulators */
689 DATA("\033=", "DECPAM"), /* application keypad mode */
690 DATA("\033>", "DECPNM"), /* normal keypad mode */
691 DATA("\033<", "DECANSI"), /* enter ANSI mode */
692 DATA("\033[!p", "DECSTR"), /* soft reset */
693 DATA("\033 F", "S7C1T"), /* 7-bit controls */
698 static const assoc std_modes[] =
699 /* ECMA \E[ ... [hl] modes recognized by many emulators */
701 DATA("2", "AM"), /* keyboard action mode */
702 DATA("4", "IRM"), /* insert/replace mode */
703 DATA("12", "SRM"), /* send/receive mode */
704 DATA("20", "LNM"), /* linefeed mode */
708 static const assoc private_modes[] =
709 /* DEC \E[ ... [hl] modes recognized by many emulators */
711 DATA("1", "CKM"), /* application cursor keys */
712 DATA("2", "ANM"), /* set VT52 mode */
713 DATA("3", "COLM"), /* 132-column mode */
714 DATA("4", "SCLM"), /* smooth scroll */
715 DATA("5", "SCNM"), /* reverse video mode */
716 DATA("6", "OM"), /* origin mode */
717 DATA("7", "AWM"), /* wraparound mode */
718 DATA("8", "ARM"), /* auto-repeat mode */
722 static const assoc ecma_highlights[] =
723 /* recognize ECMA attribute sequences */
725 DATA("0", "NORMAL"), /* normal */
726 DATA("1", "+BOLD"), /* bold on */
727 DATA("2", "+DIM"), /* dim on */
728 DATA("3", "+ITALIC"), /* italic on */
729 DATA("4", "+UNDERLINE"), /* underline on */
730 DATA("5", "+BLINK"), /* blink on */
731 DATA("6", "+FASTBLINK"), /* fastblink on */
732 DATA("7", "+REVERSE"), /* reverse on */
733 DATA("8", "+INVISIBLE"), /* invisible on */
734 DATA("9", "+DELETED"), /* deleted on */
735 DATA("10", "MAIN-FONT"), /* select primary font */
736 DATA("11", "ALT-FONT-1"), /* select alternate font 1 */
737 DATA("12", "ALT-FONT-2"), /* select alternate font 2 */
738 DATA("13", "ALT-FONT-3"), /* select alternate font 3 */
739 DATA("14", "ALT-FONT-4"), /* select alternate font 4 */
740 DATA("15", "ALT-FONT-5"), /* select alternate font 5 */
741 DATA("16", "ALT-FONT-6"), /* select alternate font 6 */
742 DATA("17", "ALT-FONT-7"), /* select alternate font 7 */
743 DATA("18", "ALT-FONT-1"), /* select alternate font 1 */
744 DATA("19", "ALT-FONT-1"), /* select alternate font 1 */
745 DATA("20", "FRAKTUR"), /* Fraktur font */
746 DATA("21", "DOUBLEUNDER"), /* double underline */
747 DATA("22", "-DIM"), /* dim off */
748 DATA("23", "-ITALIC"), /* italic off */
749 DATA("24", "-UNDERLINE"), /* underline off */
750 DATA("25", "-BLINK"), /* blink off */
751 DATA("26", "-FASTBLINK"), /* fastblink off */
752 DATA("27", "-REVERSE"), /* reverse off */
753 DATA("28", "-INVISIBLE"), /* invisible off */
754 DATA("29", "-DELETED"), /* deleted off */
761 skip_csi(const char *cap)
764 if (cap[0] == '\033' && cap[1] == '[')
766 else if (UChar(cap[0]) == 0233)
772 same_param(const char *table, const char *param, size_t length)
775 if (strncmp(table, param, length) == 0) {
776 result = !isdigit(UChar(param[length]));
782 lookup_params(const assoc * table, char *dst, char *src)
785 const char *ep = strtok(src, ";");
793 for (ap = table; ap->from[0]; ap++) {
794 size_t tlen = strlen(ap->from);
796 if (same_param(ap->from, ep, tlen)) {
797 _nc_STRCAT(dst, ap->to, MAX_TERMINFO_LENGTH);
804 _nc_STRCAT(dst, ep, MAX_TERMINFO_LENGTH);
805 _nc_STRCAT(dst, ";", MAX_TERMINFO_LENGTH);
807 ((ep = strtok((char *) 0, ";")));
809 dst[strlen(dst) - 1] = '\0';
817 analyze_string(const char *name, const char *cap, TERMTYPE2 *tp)
819 char buf2[MAX_TERMINFO_LENGTH];
822 int tp_lines = tp->Numbers[2];
824 if (!VALID_STRING(cap))
826 (void) printf("%s: ", name);
828 for (sp = cap; *sp; sp++) {
833 const char *expansion = 0;
834 char buf3[MAX_TERMINFO_LENGTH];
836 /* first, check other capabilities in this entry */
837 for (i = 0; i < STRCOUNT; i++) {
838 char *cp = tp->Strings[i];
840 /* don't use function-key capabilities */
841 if (strnames[i] == NULL)
843 if (strnames[i][0] == 'k' && strnames[i][1] == 'f')
846 if (VALID_STRING(cp) &&
850 _nc_STRNCPY(buf2, sp, len);
853 if (_nc_capcmp(cp, buf2))
856 #define ISRS(s) (!strncmp((s), "is", (size_t) 2) || !strncmp((s), "rs", (size_t) 2))
858 * Theoretically we just passed the test for translation
859 * (equality once the padding is stripped). However, there
860 * are a few more hoops that need to be jumped so that
861 * identical pairs of initialization and reset strings
862 * don't just refer to each other.
864 if (ISRS(name) || ISRS(strnames[i]))
869 expansion = strnames[i];
874 /* now check the standard capabilities */
877 for (ap = std_caps; ap->from[0]; ap++) {
878 size_t adj = (size_t) (csi ? 2 : 0);
880 len = strlen(ap->from);
881 if (csi && skip_csi(ap->from) != csi)
884 && strncmp(ap->from + adj, sp + csi, len - adj) == 0) {
893 /* now check for standard-mode sequences */
895 && (csi = skip_csi(sp)) != 0
896 && (len = (strspn) (sp + csi, "0123456789;"))
897 && (len < sizeof(buf3))
898 && (next = (size_t) csi + len)
899 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
906 _nc_STRNCPY(buf3, sp + csi, len);
908 len += (size_t) csi + 1;
910 expansion = lookup_params(std_modes, buf2, buf3);
913 /* now check for private-mode sequences */
915 && (csi = skip_csi(sp)) != 0
917 && (len = (strspn) (sp + csi + 1, "0123456789;"))
918 && (len < sizeof(buf3))
919 && (next = (size_t) csi + 1 + len)
920 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
927 _nc_STRNCPY(buf3, sp + csi + 1, len);
929 len += (size_t) csi + 2;
931 expansion = lookup_params(private_modes, buf2, buf3);
934 /* now check for ECMA highlight sequences */
936 && (csi = skip_csi(sp)) != 0
937 && (len = (strspn) (sp + csi, "0123456789;")) != 0
938 && (len < sizeof(buf3))
939 && (next = (size_t) csi + len)
940 && sp[next] == 'm') {
942 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
943 _nc_STRNCPY(buf3, sp + csi, len);
945 len += (size_t) csi + 1;
947 expansion = lookup_params(ecma_highlights, buf2, buf3);
951 && (csi = skip_csi(sp)) != 0
953 len = (size_t) csi + 1;
954 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
955 _nc_STRCAT(buf2, ecma_highlights[0].to, sizeof(buf2));
959 /* now check for scroll region reset */
961 && (csi = skip_csi(sp)) != 0) {
962 if (sp[csi] == 'r') {
966 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "1;%dr", tp_lines);
968 if (strncmp(buf2, sp + csi, len) == 0)
974 /* now check for home-down */
976 && (csi = skip_csi(sp)) != 0) {
977 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%d;1H", tp_lines);
979 if (strncmp(buf2, sp + csi, len) == 0) {
982 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%dH", tp_lines);
984 if (strncmp(buf2, sp + csi, len) == 0) {
991 /* now look at the expansion we got, if any */
993 printf("{%s}", expansion);
996 /* couldn't match anything */
999 fputs(TIC_EXPAND(buf2), stdout);
1005 /***************************************************************************
1009 ***************************************************************************/
1012 file_comparison(int argc, char *argv[])
1014 #define MAXCOMPARE 2
1015 /* someday we may allow comparisons on more files */
1017 ENTRY *heads[MAXCOMPARE];
1021 memset(heads, 0, sizeof(heads));
1022 dump_init((char *) 0, F_LITERAL, S_TERMINFO,
1023 FALSE, 0, 65535, itrace, FALSE, FALSE, FALSE);
1025 for (n = 0; n < argc && n < MAXCOMPARE; n++) {
1026 if (freopen(argv[n], "r", stdin) == 0)
1027 _nc_err_abort("Can't open %s", argv[n]);
1030 entered[n].head = _nc_head;
1031 entered[n].tail = _nc_tail;
1033 _nc_head = _nc_tail = 0;
1035 /* parse entries out of the source file */
1036 _nc_set_source(argv[n]);
1037 _nc_read_entry_source(stdin, NULL, TRUE, literal, NULLHOOK);
1040 (void) fprintf(stderr, "Resolving file %d...\n", n - 0);
1042 /* maybe do use resolution */
1043 if (!_nc_resolve_uses2(!limited, literal)) {
1044 (void) fprintf(stderr,
1045 "There are unresolved use entries in %s:\n",
1047 for_entry_list(qp) {
1049 (void) fputs(qp->tterm.term_names, stderr);
1050 (void) fputc('\n', stderr);
1053 ExitProgram(EXIT_FAILURE);
1056 heads[filecount] = _nc_head;
1060 /* OK, all entries are in core. Ready to do the comparison */
1062 (void) fprintf(stderr, "Entries are now in core...\n");
1064 /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
1065 for (qp = heads[0]; qp; qp = qp->next) {
1066 for (rp = heads[1]; rp; rp = rp->next)
1067 if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
1068 if (qp->ncrosslinks < MAX_CROSSLINKS)
1069 qp->crosslinks[qp->ncrosslinks] = rp;
1072 if (rp->ncrosslinks < MAX_CROSSLINKS)
1073 rp->crosslinks[rp->ncrosslinks] = qp;
1078 /* now we have two circular lists with crosslinks */
1080 (void) fprintf(stderr, "Name matches are done...\n");
1082 for (qp = heads[0]; qp; qp = qp->next) {
1083 if (qp->ncrosslinks > 1) {
1084 (void) fprintf(stderr,
1085 "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
1086 _nc_first_name(qp->tterm.term_names),
1090 for (i = 0; i < qp->ncrosslinks; i++)
1091 (void) fprintf(stderr,
1093 _nc_first_name((qp->crosslinks[i])->tterm.term_names));
1097 for (rp = heads[1]; rp; rp = rp->next) {
1098 if (rp->ncrosslinks > 1) {
1099 (void) fprintf(stderr,
1100 "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
1101 _nc_first_name(rp->tterm.term_names),
1105 for (i = 0; i < rp->ncrosslinks; i++)
1106 (void) fprintf(stderr,
1108 _nc_first_name((rp->crosslinks[i])->tterm.term_names));
1112 (void) printf("In file 1 (%s) only:\n", argv[0]);
1113 for (qp = heads[0]; qp; qp = qp->next)
1114 if (qp->ncrosslinks == 0)
1115 (void) printf("\t%s\n",
1116 _nc_first_name(qp->tterm.term_names));
1118 (void) printf("In file 2 (%s) only:\n", argv[1]);
1119 for (rp = heads[1]; rp; rp = rp->next)
1120 if (rp->ncrosslinks == 0)
1121 (void) printf("\t%s\n",
1122 _nc_first_name(rp->tterm.term_names));
1124 (void) printf("The following entries are equivalent:\n");
1125 for (qp = heads[0]; qp; qp = qp->next) {
1126 if (qp->ncrosslinks == 1) {
1127 rp = qp->crosslinks[0];
1129 repair_acsc(&qp->tterm);
1130 repair_acsc(&rp->tterm);
1132 _nc_align_termtype(&qp->tterm, &rp->tterm);
1134 if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
1135 char name1[NAMESIZE], name2[NAMESIZE];
1137 canonical_name(qp->tterm.term_names, name1);
1138 canonical_name(rp->tterm.term_names, name2);
1140 (void) printf("%s = %s\n", name1, name2);
1145 (void) printf("Differing entries:\n");
1147 for (qp = heads[0]; qp; qp = qp->next) {
1149 if (qp->ncrosslinks == 1) {
1150 rp = qp->crosslinks[0];
1152 /* sorry - we have to do this on each pass */
1153 _nc_align_termtype(&qp->tterm, &rp->tterm);
1155 if (!(entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp))) {
1156 char name1[NAMESIZE], name2[NAMESIZE];
1166 canonical_name(qp->tterm.term_names, name1);
1167 canonical_name(rp->tterm.term_names, name2);
1171 show_comparing(names);
1172 compare_entry(compare_predicate, &entries->tterm, quiet);
1176 show_comparing(names);
1177 compare_entry(compare_predicate, &entries->tterm, quiet);
1181 show_comparing(names);
1182 compare_entry(compare_predicate, &entries->tterm, quiet);
1194 #define DATA(s) s "\n"
1195 static const char head[] =
1197 DATA("Usage: infocmp [options] [-A directory] [-B directory] [termname...]")
1202 /* length is given here so the compiler can make everything readonly */
1204 static const char options[][46] =
1206 " -0 print single-row"
1207 ," -1 print single-column"
1208 ," -C use termcap-names"
1209 ," -D print database locations"
1210 ," -E format output as C tables"
1211 ," -F compare terminfo-files"
1212 ," -G format %{number} to %'char'"
1213 ," -I use terminfo-names"
1214 ," -K use termcap-names and BSD syntax"
1215 ," -L use long names"
1216 ," -R subset (see manpage)"
1217 ," -T eliminate size limits (test)"
1218 ," -U do not post-process entries"
1219 ," -V print version"
1220 ," -W wrap long strings per -w[n]"
1222 ," -a with -F, list commented-out caps"
1224 ," -c list common capabilities"
1225 ," -d list different capabilities"
1226 ," -e format output for C initializer"
1227 ," -f with -1, format complex strings"
1228 ," -g format %'char' to %{number}"
1229 ," -i analyze initialization/reset"
1230 ," -l output terminfo names"
1231 ," -n list capabilities in neither"
1232 ," -p ignore padding specifiers"
1233 ," -Q number dump compiled description"
1234 ," -q brief listing, removes headers"
1235 ," -r with -C, output in termcap form"
1236 ," -r with -F, resolve use-references"
1237 ," -s [d|i|l|c] sort fields"
1239 ," -t suppress commented-out capabilities"
1241 ," -u produce source with 'use='"
1242 ," -v number (verbose)"
1243 ," -w number (width)"
1245 ," -x unknown capabilities are user-defined"
1249 const size_t last = SIZEOF(options);
1250 const size_t left = (last + 1) / 2;
1253 fputs(head, stderr);
1254 for (n = 0; n < left; n++) {
1255 size_t m = n + left;
1257 fprintf(stderr, "%-40.40s%s\n", options[n], options[m]);
1259 fprintf(stderr, "%s\n", options[n]);
1261 ExitProgram(EXIT_FAILURE);
1265 any_initializer(const char *fmt, const char *type)
1267 static char *initializer;
1271 if (initializer == 0) {
1272 need = (strlen(entries->tterm.term_names)
1275 initializer = (char *) malloc(need + 1);
1276 if (initializer == 0)
1277 failed("any_initializer");
1280 _nc_STRCPY(initializer, entries->tterm.term_names, need);
1281 for (s = initializer; *s != 0 && *s != '|'; s++) {
1282 if (!isalnum(UChar(*s)))
1286 _nc_SPRINTF(s, _nc_SLIMIT(need) fmt, type);
1291 name_initializer(const char *type)
1293 return any_initializer("_%s_data", type);
1297 string_variable(const char *type)
1299 return any_initializer("_s_%s", type);
1302 /* dump C initializers for the terminal type */
1304 dump_initializers(TERMTYPE2 *term)
1307 const char *str = 0;
1309 printf("\nstatic char %s[] = \"%s\";\n\n",
1310 name_initializer("alias"), entries->tterm.term_names);
1312 for_each_string(n, term) {
1313 if (VALID_STRING(term->Strings[n])) {
1314 char buf[MAX_STRING], *sp, *tp;
1317 #define TP_LIMIT ((MAX_STRING - 5) - (size_t)(tp - buf))
1319 for (sp = term->Strings[n];
1320 *sp != 0 && TP_LIMIT > 2;
1322 if (isascii(UChar(*sp))
1323 && isprint(UChar(*sp))
1328 _nc_SPRINTF(tp, _nc_SLIMIT(TP_LIMIT) "\\%03o", UChar(*sp));
1334 (void) printf("static char %-20s[] = %s;\n",
1335 string_variable(ExtStrname(term, (int) n, strnames)),
1341 (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
1343 for_each_boolean(n, term) {
1344 switch ((int) (term->Booleans[n])) {
1353 case ABSENT_BOOLEAN:
1354 str = "ABSENT_BOOLEAN";
1357 case CANCELLED_BOOLEAN:
1358 str = "CANCELLED_BOOLEAN";
1361 (void) printf("\t/* %3u: %-8s */\t%s,\n",
1362 n, ExtBoolname(term, (int) n, boolnames), str);
1364 (void) printf("%s;\n", R_CURL);
1366 (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
1368 for_each_number(n, term) {
1370 switch (term->Numbers[n]) {
1371 case ABSENT_NUMERIC:
1372 str = "ABSENT_NUMERIC";
1374 case CANCELLED_NUMERIC:
1375 str = "CANCELLED_NUMERIC";
1378 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) "%d", term->Numbers[n]);
1382 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1383 ExtNumname(term, (int) n, numnames), str);
1385 (void) printf("%s;\n", R_CURL);
1387 (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
1389 for_each_string(n, term) {
1391 if (term->Strings[n] == ABSENT_STRING)
1392 str = "ABSENT_STRING";
1393 else if (term->Strings[n] == CANCELLED_STRING)
1394 str = "CANCELLED_STRING";
1396 str = string_variable(ExtStrname(term, (int) n, strnames));
1398 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1399 ExtStrname(term, (int) n, strnames), str);
1401 (void) printf("%s;\n", R_CURL);
1404 if ((NUM_BOOLEANS(term) != BOOLCOUNT)
1405 || (NUM_NUMBERS(term) != NUMCOUNT)
1406 || (NUM_STRINGS(term) != STRCOUNT)) {
1407 (void) printf("static char * %s[] = %s\n",
1408 name_initializer("string_ext"), L_CURL);
1409 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
1410 (void) printf("\t/* %3u: bool */\t\"%s\",\n",
1411 n, ExtBoolname(term, (int) n, boolnames));
1413 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
1414 (void) printf("\t/* %3u: num */\t\"%s\",\n",
1415 n, ExtNumname(term, (int) n, numnames));
1417 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
1418 (void) printf("\t/* %3u: str */\t\"%s\",\n",
1419 n, ExtStrname(term, (int) n, strnames));
1421 (void) printf("%s;\n", R_CURL);
1426 /* dump C initializers for the terminal type */
1428 dump_termtype(TERMTYPE2 *term)
1430 (void) printf("\t%s\n\t\t%s,\n", L_CURL, name_initializer("alias"));
1431 (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
1433 (void) printf("\t\t%s,\n", name_initializer("bool"));
1434 (void) printf("\t\t%s,\n", name_initializer("number"));
1436 (void) printf("\t\t%s,\n", name_initializer("string"));
1439 (void) printf("#if NCURSES_XNAMES\n");
1440 (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
1441 (void) printf("\t\t%s,\t/* ...corresponding names */\n",
1442 ((NUM_BOOLEANS(term) != BOOLCOUNT)
1443 || (NUM_NUMBERS(term) != NUMCOUNT)
1444 || (NUM_STRINGS(term) != STRCOUNT))
1445 ? name_initializer("string_ext")
1448 (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
1449 (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
1450 (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
1452 (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
1453 NUM_BOOLEANS(term) - BOOLCOUNT);
1454 (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
1455 NUM_NUMBERS(term) - NUMCOUNT);
1456 (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
1457 NUM_STRINGS(term) - STRCOUNT);
1459 (void) printf("#endif /* NCURSES_XNAMES */\n");
1462 #endif /* NCURSES_XNAMES */
1463 (void) printf("\t%s\n", R_CURL);
1467 optarg_to_number(void)
1470 long value = strtol(optarg, &temp, 0);
1472 if (temp == 0 || temp == optarg || *temp != 0) {
1473 fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
1474 ExitProgram(EXIT_FAILURE);
1484 if ((terminal = getenv("TERM")) == 0) {
1485 (void) fprintf(stderr,
1486 "%s: environment variable TERM not set\n",
1494 * Show the databases that infocmp knows about. The location to which it writes is
1497 show_databases(void)
1503 _nc_first_db(&state, &offset);
1504 while ((path2 = _nc_next_db(&state, &offset)) != 0) {
1505 printf("%s\n", path2);
1510 /***************************************************************************
1514 ***************************************************************************/
1517 #define MAIN_LEAKS() \
1518 _nc_free_termtype2(&entries[0].tterm); \
1519 _nc_free_termtype2(&entries[1].tterm); \
1524 #define MAIN_LEAKS() /* nothing */
1528 main(int argc, char *argv[])
1530 /* Avoid "local data >32k" error with mwcc */
1531 /* Also avoid overflowing smaller stacks on systems like AmigaOS */
1538 char *firstdir, *restdir;
1540 bool formatted = FALSE;
1541 bool filecompare = FALSE;
1543 bool init_analyze = FALSE;
1544 bool suppress_untranslatable = FALSE;
1546 bool wrap_strings = FALSE;
1548 /* where is the terminfo database location going to default to? */
1549 restdir = firstdir = 0;
1552 use_extended_names(FALSE);
1556 _nc_progname = _nc_rootname(argv[0]);
1558 /* make sure we have enough space to add two terminal entries */
1559 myargv = typeCalloc(char *, (size_t) (argc + 3));
1563 memcpy(myargv, argv, (sizeof(char *) * (size_t) argc));
1566 while ((c = getopt(argc,
1568 "01A:aB:CcDdEeFfGgIiKLlnpQ:qR:rs:TtUuVv:Ww:x")) != -1) {
1585 _nc_disable_period = TRUE;
1586 use_extended_names(TRUE);
1597 outform = F_TERMCAP;
1599 if (sortmode == S_DEFAULT)
1600 sortmode = S_TERMCAP;
1605 ExitProgram(EXIT_SUCCESS);
1613 compare = C_DIFFERENCE;
1641 outform = F_TERMINFO;
1642 if (sortmode == S_DEFAULT)
1643 sortmode = S_VARIABLE;
1648 init_analyze = TRUE;
1652 outform = F_VARIABLE;
1653 if (sortmode == S_DEFAULT)
1654 sortmode = S_VARIABLE;
1658 outform = F_TERMINFO;
1670 quickdump = optarg_to_number();
1690 sortmode = S_NOSORT;
1691 else if (*optarg == 'i')
1692 sortmode = S_TERMINFO;
1693 else if (*optarg == 'l')
1694 sortmode = S_VARIABLE;
1695 else if (*optarg == 'c')
1696 sortmode = S_TERMCAP;
1698 (void) fprintf(stderr,
1699 "%s: unknown sort mode\n",
1701 ExitProgram(EXIT_FAILURE);
1711 _nc_disable_period = FALSE;
1712 suppress_untranslatable = TRUE;
1725 puts(curses_version());
1726 ExitProgram(EXIT_SUCCESS);
1729 itrace = (unsigned) optarg_to_number();
1730 set_trace_level(itrace);
1734 wrap_strings = TRUE;
1738 mwidth = optarg_to_number();
1743 use_extended_names(TRUE);
1752 maxterms = (size_t) (argc + 2 - optind);
1753 if ((tfile = typeMalloc(path, maxterms)) == 0)
1755 if ((tname = typeCalloc(char *, maxterms)) == 0)
1757 if ((entries = typeCalloc(ENTRY, maxterms)) == 0)
1760 if ((entered = typeCalloc(ENTERED, maxterms)) == 0)
1767 fprintf(stderr, "%s: not enough memory\n", _nc_progname);
1768 ExitProgram(EXIT_FAILURE);
1771 /* by default, sort by terminfo name */
1772 if (sortmode == S_DEFAULT)
1773 sortmode = S_TERMINFO;
1775 /* make sure we have at least one terminal name to work with */
1777 argv[argc++] = terminal_env();
1779 /* if user is after a comparison, make sure we have two entries */
1780 if (compare != C_DEFAULT && optind >= argc - 1)
1781 argv[argc++] = terminal_env();
1783 /* exactly one terminal name with no options means display it */
1784 /* exactly two terminal names with no options means do -d */
1785 if (compare == C_DEFAULT) {
1786 switch (argc - optind) {
1788 fprintf(stderr, "%s: too many names to compare\n", _nc_progname);
1789 ExitProgram(EXIT_FAILURE);
1793 compare = C_DIFFERENCE;
1798 /* set up for display */
1799 dump_init(tversion, outform, sortmode,
1800 wrap_strings, mwidth, mheight, itrace,
1801 formatted, FALSE, quickdump);
1804 /* grab the entries */
1806 for (; optind < argc; optind++) {
1807 const char *directory = termcount ? restdir : firstdir;
1810 tname[termcount] = argv[optind];
1813 #if NCURSES_USE_DATABASE
1814 #if MIXEDCASE_FILENAMES
1815 #define LEAF_FMT "%c"
1817 #define LEAF_FMT "%02x"
1819 _nc_SPRINTF(tfile[termcount],
1820 _nc_SLIMIT(sizeof(path))
1821 "%s/" LEAF_FMT "/%s",
1823 UChar(*argv[optind]), argv[optind]);
1825 (void) fprintf(stderr,
1826 "%s: reading entry %s from file %s\n",
1828 argv[optind], tfile[termcount]);
1830 status = _nc_read_file_entry(tfile[termcount],
1831 &entries[termcount].tterm);
1833 (void) fprintf(stderr, "%s: terminfo files not supported\n",
1836 ExitProgram(EXIT_FAILURE);
1840 (void) fprintf(stderr,
1841 "%s: reading entry %s from database\n",
1845 status = _nc_read_entry2(tname[termcount],
1847 &entries[termcount].tterm);
1851 (void) fprintf(stderr,
1852 "%s: couldn't open terminfo file %s.\n",
1856 ExitProgram(EXIT_FAILURE);
1858 repair_acsc(&entries[termcount].tterm);
1864 _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
1867 /* dump as C initializer for the terminal type */
1870 dump_termtype(&entries[0].tterm);
1872 dump_initializers(&entries[0].tterm);
1875 /* analyze the init strings */
1876 else if (init_analyze) {
1878 #define CUR entries[0].tterm.
1879 analyze_string("is1", init_1string, &entries[0].tterm);
1880 analyze_string("is2", init_2string, &entries[0].tterm);
1881 analyze_string("is3", init_3string, &entries[0].tterm);
1882 analyze_string("rs1", reset_1string, &entries[0].tterm);
1883 analyze_string("rs2", reset_2string, &entries[0].tterm);
1884 analyze_string("rs3", reset_3string, &entries[0].tterm);
1885 analyze_string("smcup", enter_ca_mode, &entries[0].tterm);
1886 analyze_string("rmcup", exit_ca_mode, &entries[0].tterm);
1887 analyze_string("smkx", keypad_xmit, &entries[0].tterm);
1888 analyze_string("rmkx", keypad_local, &entries[0].tterm);
1895 * Here's where the real work gets done
1900 (void) fprintf(stderr,
1901 "%s: about to dump %s\n",
1906 printf("#\tReconstructed via infocmp from file: %s\n",
1908 dump_entry(&entries[0].tterm,
1909 suppress_untranslatable,
1915 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1919 show_comparing(tname);
1920 compare_entry(compare_predicate, &entries->tterm, quiet);
1924 show_comparing(tname);
1925 compare_entry(compare_predicate, &entries->tterm, quiet);
1929 show_comparing(tname);
1930 compare_entry(compare_predicate, &entries->tterm, quiet);
1935 (void) fprintf(stderr, "%s: dumping use entry\n", _nc_progname);
1936 dump_entry(&entries[0].tterm,
1937 suppress_untranslatable,
1941 for (i = 1; i < termcount; i++)
1942 dump_uses(tname[i], !(outform == F_TERMCAP
1943 || outform == F_TCONVERR));
1946 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1950 } else if (compare == C_USEALL) {
1951 (void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
1952 } else if (compare == C_DEFAULT) {
1953 (void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
1954 } else if (argc - optind != 2) {
1955 (void) fprintf(stderr,
1956 "File comparison needs exactly two file arguments.\n");
1958 file_comparison(argc - optind, argv + optind);
1962 ExitProgram(EXIT_SUCCESS);
1965 /* infocmp.c ends here */