1 /****************************************************************************
2 * Copyright (c) 1998-2015,2016 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 ****************************************************************************/
36 * infocmp.c -- decompile an entry, or compare two entries
37 * written by Eric S. Raymond
41 #include <progs.priv.h>
43 #include <dump_entry.h>
45 MODULE_ID("$Id: infocmp.c,v 1.140 2016/10/01 19:09:22 tom Exp $")
47 #define MAX_STRING 1024 /* maximum formatted string */
49 const char *_nc_progname = "infocmp";
51 typedef char path[PATH_MAX];
53 /***************************************************************************
55 * The following control variables, together with the contents of the
56 * terminfo entries, completely determine the actions of the program.
58 ***************************************************************************/
60 static ENTRY *entries; /* terminfo entries */
61 static int termcount; /* count of terminal entries */
63 static bool limited = TRUE; /* "-r" option is not set */
64 static bool quiet = FALSE;
65 static bool literal = FALSE;
66 static const char *bool_sep = ":";
67 static const char *s_absent = "NULL";
68 static const char *s_cancel = "NULL";
69 static const char *tversion; /* terminfo version selected */
70 static unsigned itrace; /* trace flag for debugging */
71 static int mwidth = 60;
72 static int mheight = 65535;
73 static int numbers = 0; /* format "%'char'" to/from "%{number}" */
74 static int outform = F_TERMINFO; /* output format */
75 static int sortmode; /* sort_mode */
77 /* main comparison mode */
79 #define C_DEFAULT 0 /* don't force comparison mode */
80 #define C_DIFFERENCE 1 /* list differences between two terminals */
81 #define C_COMMON 2 /* list common capabilities */
82 #define C_NAND 3 /* list capabilities in neither terminal */
83 #define C_USEALL 4 /* generate relative use-form entry */
84 static bool ignorepads; /* ignore pad prefixes when diffing */
93 static ENTERED *entered;
96 static void ExitProgram(int code) GCC_NORETURN;
97 /* prototype is to get gcc to accept the noreturn attribute */
103 for (n = 0; n < termcount; ++n) {
104 ENTRY *new_head = _nc_head;
105 ENTRY *new_tail = _nc_tail;
106 _nc_head = entered[n].head;
107 _nc_tail = entered[n].tail;
108 _nc_free_entries(entered[n].head);
112 _nc_leaks_dump_entry();
120 failed(const char *s)
123 ExitProgram(EXIT_FAILURE);
127 canonical_name(char *ptr, char *buf)
128 /* extract the terminal type's primary name */
132 _nc_STRCPY(buf, ptr, NAMESIZE);
133 if ((bp = strchr(buf, '|')) != 0)
139 /***************************************************************************
141 * Predicates for dump function
143 ***************************************************************************/
146 capcmp(PredIdx idx, const char *s, const char *t)
147 /* capability comparison function */
149 if (!VALID_STRING(s) && !VALID_STRING(t))
151 else if (!VALID_STRING(s) || !VALID_STRING(t))
154 if ((idx == acs_chars_index) || !ignorepads)
155 return (strcmp(s, t));
157 return (_nc_capcmp(s, t));
161 use_predicate(unsigned type, PredIdx idx)
162 /* predicate function to use for use decompilation */
172 * This assumes that multiple use entries are supposed
173 * to contribute the logical or of their boolean capabilities.
174 * This is true if we take the semantics of multiple uses to
175 * be 'each capability gets the first non-default value found
176 * in the sequence of use entries'.
178 * Note that cancelled or absent booleans are stored as FALSE,
179 * unlike numbers and strings, whose cancelled/absent state is
180 * recorded in the terminfo database.
182 for (ep = &entries[1]; ep < entries + termcount; ep++)
183 if (ep->tterm.Booleans[idx] == TRUE) {
184 is_set = entries[0].tterm.Booleans[idx];
187 if (is_set != entries[0].tterm.Booleans[idx])
195 int value = ABSENT_NUMERIC;
198 * We take the semantics of multiple uses to be 'each
199 * capability gets the first non-default value found
200 * in the sequence of use entries'.
202 for (ep = &entries[1]; ep < entries + termcount; ep++)
203 if (VALID_NUMERIC(ep->tterm.Numbers[idx])) {
204 value = ep->tterm.Numbers[idx];
208 if (value != entries[0].tterm.Numbers[idx])
209 return (value != ABSENT_NUMERIC);
216 char *termstr, *usestr = ABSENT_STRING;
218 termstr = entries[0].tterm.Strings[idx];
221 * We take the semantics of multiple uses to be 'each
222 * capability gets the first non-default value found
223 * in the sequence of use entries'.
225 for (ep = &entries[1]; ep < entries + termcount; ep++)
226 if (ep->tterm.Strings[idx]) {
227 usestr = ep->tterm.Strings[idx];
231 if (usestr == ABSENT_STRING && termstr == ABSENT_STRING)
233 else if (!usestr || !termstr || capcmp(idx, usestr, termstr))
240 return (FALSE); /* pacify compiler */
244 useeq(ENTRY * e1, ENTRY * e2)
245 /* are the use references in two entries equivalent? */
249 if (e1->nuses != e2->nuses)
252 /* Ugh...this is quadratic again */
253 for (i = 0; i < e1->nuses; i++) {
254 bool foundmatch = FALSE;
256 /* search second entry for given use reference */
257 for (j = 0; j < e2->nuses; j++)
258 if (!strcmp(e1->uses[i].name, e2->uses[j].name)) {
271 entryeq(TERMTYPE *t1, TERMTYPE *t2)
272 /* are two entries equivalent? */
276 for (i = 0; i < NUM_BOOLEANS(t1); i++)
277 if (t1->Booleans[i] != t2->Booleans[i])
280 for (i = 0; i < NUM_NUMBERS(t1); i++)
281 if (t1->Numbers[i] != t2->Numbers[i])
284 for (i = 0; i < NUM_STRINGS(t1); i++)
285 if (capcmp((PredIdx) i, t1->Strings[i], t2->Strings[i]))
291 #define TIC_EXPAND(result) _nc_tic_expand(result, outform==F_TERMINFO, numbers)
294 print_uses(ENTRY * ep, FILE *fp)
295 /* print an entry's use references */
302 for (i = 0; i < ep->nuses; i++) {
303 fputs(ep->uses[i].name, fp);
304 if (i < ep->nuses - 1)
310 dump_boolean(int val)
311 /* display the value of a boolean capability */
316 case CANCELLED_BOOLEAN:
328 dump_numeric(int val, char *buf)
329 /* display the value of a boolean capability */
333 _nc_STRCPY(buf, s_absent, MAX_STRING);
335 case CANCELLED_NUMERIC:
336 _nc_STRCPY(buf, s_cancel, MAX_STRING);
339 _nc_SPRINTF(buf, _nc_SLIMIT(MAX_STRING) "%d", val);
345 dump_string(char *val, char *buf)
346 /* display the value of a string capability */
348 if (val == ABSENT_STRING)
349 _nc_STRCPY(buf, s_absent, MAX_STRING);
350 else if (val == CANCELLED_STRING)
351 _nc_STRCPY(buf, s_cancel, MAX_STRING);
353 _nc_SPRINTF(buf, _nc_SLIMIT(MAX_STRING)
354 "'%.*s'", MAX_STRING - 3, TIC_EXPAND(val));
359 * Show "comparing..." message for the given terminal names.
362 show_comparing(char **names)
367 (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
371 (void) fprintf(stderr, "%s: dumping common capabilities\n", _nc_progname);
375 (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
380 printf("comparing %s", *names++);
382 printf(" to %s", *names++);
384 printf(", %s", *names++);
392 * ncurses stores two types of non-standard capabilities:
393 * a) capabilities listed past the "STOP-HERE" comment in the Caps file.
394 * These are used in the terminfo source file to provide data for termcaps,
395 * e.g., when there is no equivalent capability in terminfo, as well as for
396 * widely-used non-standard capabilities.
397 * b) user-definable capabilities, via "tic -x".
399 * However, if "-x" is omitted from the tic command, both types of
400 * non-standard capability are not loaded into the terminfo database. This
401 * macro is used for limit-checks against the symbols that tic uses to omit
402 * the two types of non-standard entry.
405 #define check_user_definable(n,limit) if (!_nc_user_definable && (n) > (limit)) break
407 #define check_user_definable(n,limit) if ((n) > (limit)) break
411 * Use these macros to simplify loops on C_COMMON and C_NAND:
413 #define for_each_entry() while (entries[extra].tterm.term_names)
414 #define next_entry (&(entries[extra++].tterm))
417 compare_predicate(PredType type, PredIdx idx, const char *name)
418 /* predicate function to use for entry difference reports */
420 ENTRY *e1 = &entries[0];
421 ENTRY *e2 = &entries[1];
422 char buf1[MAX_STRING];
423 char buf2[MAX_STRING];
432 check_user_definable(idx, BOOLWRITE);
433 b1 = e1->tterm.Booleans[idx];
436 b2 = next_entry->Booleans[idx];
437 if (!(b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN) && b1 != b2)
438 (void) printf("\t%s: %s%s%s.\n",
446 if (b1 != ABSENT_BOOLEAN) {
449 b2 = next_entry->Booleans[idx];
456 (void) printf("\t%s= %s.\n", name, dump_boolean(b1));
462 if (b1 == ABSENT_BOOLEAN) {
465 b2 = next_entry->Booleans[idx];
472 (void) printf("\t!%s.\n", name);
480 check_user_definable(idx, NUMWRITE);
481 n1 = e1->tterm.Numbers[idx];
484 n2 = next_entry->Numbers[idx];
485 if (!((n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)) && n1 != n2) {
486 dump_numeric(n1, buf1);
487 dump_numeric(n2, buf2);
488 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
493 if (n1 != ABSENT_NUMERIC) {
496 n2 = next_entry->Numbers[idx];
503 dump_numeric(n1, buf1);
504 (void) printf("\t%s= %s.\n", name, buf1);
510 if (n1 == ABSENT_NUMERIC) {
513 n2 = next_entry->Numbers[idx];
520 (void) printf("\t!%s.\n", name);
528 check_user_definable(idx, STRWRITE);
529 s1 = e1->tterm.Strings[idx];
532 s2 = next_entry->Strings[idx];
533 if (capcmp(idx, s1, s2)) {
534 dump_string(s1, buf1);
535 dump_string(s2, buf2);
536 if (strcmp(buf1, buf2))
537 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
542 if (s1 != ABSENT_STRING) {
545 s2 = next_entry->Strings[idx];
546 if (capcmp(idx, s1, s2) != 0) {
552 (void) printf("\t%s= '%s'.\n", name, TIC_EXPAND(s1));
558 if (s1 == ABSENT_STRING) {
561 s2 = next_entry->Strings[idx];
568 (void) printf("\t!%s.\n", name);
576 /* unlike the other modes, this compares *all* use entries */
579 if (!useeq(e1, e2)) {
580 (void) fputs("\tuse: ", stdout);
581 print_uses(e1, stdout);
583 print_uses(e2, stdout);
584 fputs(".\n", stdout);
592 e2 = &entries[extra++];
593 if (e2->nuses != e1->nuses || !useeq(e1, e2)) {
599 (void) fputs("\tuse: ", stdout);
600 print_uses(e1, stdout);
601 fputs(".\n", stdout);
610 e2 = &entries[extra++];
611 if (e2->nuses != e1->nuses) {
617 (void) printf("\t!use.\n");
625 /***************************************************************************
627 * Init string analysis
629 ***************************************************************************/
631 #define DATA(from, to) { { from }, { to } }
632 #define DATAX() DATA("", "")
639 static const assoc std_caps[] =
641 /* these are specified by X.364 and iBCS2 */
642 DATA("\033c", "RIS"), /* full reset */
643 DATA("\0337", "SC"), /* save cursor */
644 DATA("\0338", "RC"), /* restore cursor */
645 DATA("\033[r", "RSR"), /* not an X.364 mnemonic */
646 DATA("\033[m", "SGR0"), /* not an X.364 mnemonic */
647 DATA("\033[2J", "ED2"), /* clear page */
649 /* this group is specified by ISO 2022 */
650 DATA("\033(0", "ISO DEC G0"), /* enable DEC graphics for G0 */
651 DATA("\033(A", "ISO UK G0"), /* enable UK chars for G0 */
652 DATA("\033(B", "ISO US G0"), /* enable US chars for G0 */
653 DATA("\033)0", "ISO DEC G1"), /* enable DEC graphics for G1 */
654 DATA("\033)A", "ISO UK G1"), /* enable UK chars for G1 */
655 DATA("\033)B", "ISO US G1"), /* enable US chars for G1 */
657 /* these are DEC private controls widely supported by emulators */
658 DATA("\033=", "DECPAM"), /* application keypad mode */
659 DATA("\033>", "DECPNM"), /* normal keypad mode */
660 DATA("\033<", "DECANSI"), /* enter ANSI mode */
661 DATA("\033[!p", "DECSTR"), /* soft reset */
662 DATA("\033 F", "S7C1T"), /* 7-bit controls */
667 static const assoc std_modes[] =
668 /* ECMA \E[ ... [hl] modes recognized by many emulators */
670 DATA("2", "AM"), /* keyboard action mode */
671 DATA("4", "IRM"), /* insert/replace mode */
672 DATA("12", "SRM"), /* send/receive mode */
673 DATA("20", "LNM"), /* linefeed mode */
677 static const assoc private_modes[] =
678 /* DEC \E[ ... [hl] modes recognized by many emulators */
680 DATA("1", "CKM"), /* application cursor keys */
681 DATA("2", "ANM"), /* set VT52 mode */
682 DATA("3", "COLM"), /* 132-column mode */
683 DATA("4", "SCLM"), /* smooth scroll */
684 DATA("5", "SCNM"), /* reverse video mode */
685 DATA("6", "OM"), /* origin mode */
686 DATA("7", "AWM"), /* wraparound mode */
687 DATA("8", "ARM"), /* auto-repeat mode */
691 static const assoc ecma_highlights[] =
692 /* recognize ECMA attribute sequences */
694 DATA("0", "NORMAL"), /* normal */
695 DATA("1", "+BOLD"), /* bold on */
696 DATA("2", "+DIM"), /* dim on */
697 DATA("3", "+ITALIC"), /* italic on */
698 DATA("4", "+UNDERLINE"), /* underline on */
699 DATA("5", "+BLINK"), /* blink on */
700 DATA("6", "+FASTBLINK"), /* fastblink on */
701 DATA("7", "+REVERSE"), /* reverse on */
702 DATA("8", "+INVISIBLE"), /* invisible on */
703 DATA("9", "+DELETED"), /* deleted on */
704 DATA("10", "MAIN-FONT"), /* select primary font */
705 DATA("11", "ALT-FONT-1"), /* select alternate font 1 */
706 DATA("12", "ALT-FONT-2"), /* select alternate font 2 */
707 DATA("13", "ALT-FONT-3"), /* select alternate font 3 */
708 DATA("14", "ALT-FONT-4"), /* select alternate font 4 */
709 DATA("15", "ALT-FONT-5"), /* select alternate font 5 */
710 DATA("16", "ALT-FONT-6"), /* select alternate font 6 */
711 DATA("17", "ALT-FONT-7"), /* select alternate font 7 */
712 DATA("18", "ALT-FONT-1"), /* select alternate font 1 */
713 DATA("19", "ALT-FONT-1"), /* select alternate font 1 */
714 DATA("20", "FRAKTUR"), /* Fraktur font */
715 DATA("21", "DOUBLEUNDER"), /* double underline */
716 DATA("22", "-DIM"), /* dim off */
717 DATA("23", "-ITALIC"), /* italic off */
718 DATA("24", "-UNDERLINE"), /* underline off */
719 DATA("25", "-BLINK"), /* blink off */
720 DATA("26", "-FASTBLINK"), /* fastblink off */
721 DATA("27", "-REVERSE"), /* reverse off */
722 DATA("28", "-INVISIBLE"), /* invisible off */
723 DATA("29", "-DELETED"), /* deleted off */
730 skip_csi(const char *cap)
733 if (cap[0] == '\033' && cap[1] == '[')
735 else if (UChar(cap[0]) == 0233)
741 same_param(const char *table, const char *param, size_t length)
744 if (strncmp(table, param, length) == 0) {
745 result = !isdigit(UChar(param[length]));
751 lookup_params(const assoc * table, char *dst, char *src)
754 const char *ep = strtok(src, ";");
762 for (ap = table; ap->from[0]; ap++) {
763 size_t tlen = strlen(ap->from);
765 if (same_param(ap->from, ep, tlen)) {
766 _nc_STRCAT(dst, ap->to, MAX_TERMINFO_LENGTH);
773 _nc_STRCAT(dst, ep, MAX_TERMINFO_LENGTH);
774 _nc_STRCAT(dst, ";", MAX_TERMINFO_LENGTH);
776 ((ep = strtok((char *) 0, ";")));
778 dst[strlen(dst) - 1] = '\0';
786 analyze_string(const char *name, const char *cap, TERMTYPE *tp)
788 char buf2[MAX_TERMINFO_LENGTH];
791 int tp_lines = tp->Numbers[2];
793 if (!VALID_STRING(cap))
795 (void) printf("%s: ", name);
797 for (sp = cap; *sp; sp++) {
802 const char *expansion = 0;
803 char buf3[MAX_TERMINFO_LENGTH];
805 /* first, check other capabilities in this entry */
806 for (i = 0; i < STRCOUNT; i++) {
807 char *cp = tp->Strings[i];
809 /* don't use function-key capabilities */
810 if (strnames[i][0] == 'k' && strnames[i][1] == 'f')
813 if (VALID_STRING(cp) &&
817 _nc_STRNCPY(buf2, sp, len);
820 if (_nc_capcmp(cp, buf2))
823 #define ISRS(s) (!strncmp((s), "is", (size_t) 2) || !strncmp((s), "rs", (size_t) 2))
825 * Theoretically we just passed the test for translation
826 * (equality once the padding is stripped). However, there
827 * are a few more hoops that need to be jumped so that
828 * identical pairs of initialization and reset strings
829 * don't just refer to each other.
831 if (ISRS(name) || ISRS(strnames[i]))
836 expansion = strnames[i];
841 /* now check the standard capabilities */
844 for (ap = std_caps; ap->from[0]; ap++) {
845 size_t adj = (size_t) (csi ? 2 : 0);
847 len = strlen(ap->from);
848 if (csi && skip_csi(ap->from) != csi)
851 && strncmp(ap->from + adj, sp + csi, len - adj) == 0) {
860 /* now check for standard-mode sequences */
862 && (csi = skip_csi(sp)) != 0
863 && (len = (strspn) (sp + csi, "0123456789;"))
864 && (len < sizeof(buf3))
865 && (next = (size_t) csi + len)
866 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
873 _nc_STRNCPY(buf3, sp + csi, len);
875 len += (size_t) csi + 1;
877 expansion = lookup_params(std_modes, buf2, buf3);
880 /* now check for private-mode sequences */
882 && (csi = skip_csi(sp)) != 0
884 && (len = (strspn) (sp + csi + 1, "0123456789;"))
885 && (len < sizeof(buf3))
886 && (next = (size_t) csi + 1 + len)
887 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
894 _nc_STRNCPY(buf3, sp + csi + 1, len);
896 len += (size_t) csi + 2;
898 expansion = lookup_params(private_modes, buf2, buf3);
901 /* now check for ECMA highlight sequences */
903 && (csi = skip_csi(sp)) != 0
904 && (len = (strspn) (sp + csi, "0123456789;")) != 0
905 && (len < sizeof(buf3))
906 && (next = (size_t) csi + len)
907 && sp[next] == 'm') {
909 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
910 _nc_STRNCPY(buf3, sp + csi, len);
912 len += (size_t) csi + 1;
914 expansion = lookup_params(ecma_highlights, buf2, buf3);
918 && (csi = skip_csi(sp)) != 0
920 len = (size_t) csi + 1;
921 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
922 _nc_STRCAT(buf2, ecma_highlights[0].to, sizeof(buf2));
926 /* now check for scroll region reset */
928 && (csi = skip_csi(sp)) != 0) {
929 if (sp[csi] == 'r') {
933 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "1;%dr", tp_lines);
935 if (strncmp(buf2, sp + csi, len) == 0)
941 /* now check for home-down */
943 && (csi = skip_csi(sp)) != 0) {
944 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%d;1H", tp_lines);
946 if (strncmp(buf2, sp + csi, len) == 0) {
949 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%dH", tp_lines);
951 if (strncmp(buf2, sp + csi, len) == 0) {
958 /* now look at the expansion we got, if any */
960 printf("{%s}", expansion);
963 /* couldn't match anything */
966 fputs(TIC_EXPAND(buf2), stdout);
972 /***************************************************************************
976 ***************************************************************************/
979 file_comparison(int argc, char *argv[])
982 /* someday we may allow comparisons on more files */
984 ENTRY *heads[MAXCOMPARE];
988 memset(heads, 0, sizeof(heads));
989 dump_init((char *) 0, F_LITERAL, S_TERMINFO,
990 FALSE, 0, 65535, itrace, FALSE, FALSE, FALSE);
992 for (n = 0; n < argc && n < MAXCOMPARE; n++) {
993 if (freopen(argv[n], "r", stdin) == 0)
994 _nc_err_abort("Can't open %s", argv[n]);
997 entered[n].head = _nc_head;
998 entered[n].tail = _nc_tail;
1000 _nc_head = _nc_tail = 0;
1002 /* parse entries out of the source file */
1003 _nc_set_source(argv[n]);
1004 _nc_read_entry_source(stdin, NULL, TRUE, literal, NULLHOOK);
1007 (void) fprintf(stderr, "Resolving file %d...\n", n - 0);
1009 /* maybe do use resolution */
1010 if (!_nc_resolve_uses2(!limited, literal)) {
1011 (void) fprintf(stderr,
1012 "There are unresolved use entries in %s:\n",
1014 for_entry_list(qp) {
1016 (void) fputs(qp->tterm.term_names, stderr);
1017 (void) fputc('\n', stderr);
1020 ExitProgram(EXIT_FAILURE);
1023 heads[filecount] = _nc_head;
1027 /* OK, all entries are in core. Ready to do the comparison */
1029 (void) fprintf(stderr, "Entries are now in core...\n");
1031 /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
1032 for (qp = heads[0]; qp; qp = qp->next) {
1033 for (rp = heads[1]; rp; rp = rp->next)
1034 if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
1035 if (qp->ncrosslinks < MAX_CROSSLINKS)
1036 qp->crosslinks[qp->ncrosslinks] = rp;
1039 if (rp->ncrosslinks < MAX_CROSSLINKS)
1040 rp->crosslinks[rp->ncrosslinks] = qp;
1045 /* now we have two circular lists with crosslinks */
1047 (void) fprintf(stderr, "Name matches are done...\n");
1049 for (qp = heads[0]; qp; qp = qp->next) {
1050 if (qp->ncrosslinks > 1) {
1051 (void) fprintf(stderr,
1052 "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
1053 _nc_first_name(qp->tterm.term_names),
1057 for (i = 0; i < qp->ncrosslinks; i++)
1058 (void) fprintf(stderr,
1060 _nc_first_name((qp->crosslinks[i])->tterm.term_names));
1064 for (rp = heads[1]; rp; rp = rp->next) {
1065 if (rp->ncrosslinks > 1) {
1066 (void) fprintf(stderr,
1067 "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
1068 _nc_first_name(rp->tterm.term_names),
1072 for (i = 0; i < rp->ncrosslinks; i++)
1073 (void) fprintf(stderr,
1075 _nc_first_name((rp->crosslinks[i])->tterm.term_names));
1079 (void) printf("In file 1 (%s) only:\n", argv[0]);
1080 for (qp = heads[0]; qp; qp = qp->next)
1081 if (qp->ncrosslinks == 0)
1082 (void) printf("\t%s\n",
1083 _nc_first_name(qp->tterm.term_names));
1085 (void) printf("In file 2 (%s) only:\n", argv[1]);
1086 for (rp = heads[1]; rp; rp = rp->next)
1087 if (rp->ncrosslinks == 0)
1088 (void) printf("\t%s\n",
1089 _nc_first_name(rp->tterm.term_names));
1091 (void) printf("The following entries are equivalent:\n");
1092 for (qp = heads[0]; qp; qp = qp->next) {
1093 if (qp->ncrosslinks == 1) {
1094 rp = qp->crosslinks[0];
1096 repair_acsc(&qp->tterm);
1097 repair_acsc(&rp->tterm);
1099 _nc_align_termtype(&qp->tterm, &rp->tterm);
1101 if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
1102 char name1[NAMESIZE], name2[NAMESIZE];
1104 (void) canonical_name(qp->tterm.term_names, name1);
1105 (void) canonical_name(rp->tterm.term_names, name2);
1107 (void) printf("%s = %s\n", name1, name2);
1112 (void) printf("Differing entries:\n");
1114 for (qp = heads[0]; qp; qp = qp->next) {
1116 if (qp->ncrosslinks == 1) {
1117 rp = qp->crosslinks[0];
1119 /* sorry - we have to do this on each pass */
1120 _nc_align_termtype(&qp->tterm, &rp->tterm);
1122 if (!(entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp))) {
1123 char name1[NAMESIZE], name2[NAMESIZE];
1133 (void) canonical_name(qp->tterm.term_names, name1);
1134 (void) canonical_name(rp->tterm.term_names, name2);
1138 show_comparing(names);
1139 compare_entry(compare_predicate, &entries->tterm, quiet);
1143 show_comparing(names);
1144 compare_entry(compare_predicate, &entries->tterm, quiet);
1148 show_comparing(names);
1149 compare_entry(compare_predicate, &entries->tterm, quiet);
1161 #define DATA(s) s "\n"
1162 static const char head[] =
1164 DATA("Usage: infocmp [options] [-A directory] [-B directory] [termname...]")
1170 static const char options[][45] =
1172 " -0 print single-row"
1173 ," -1 print single-column"
1174 ," -C use termcap-names"
1175 ," -D print database locations"
1176 ," -E format output as C tables"
1177 ," -F compare terminfo-files"
1178 ," -G format %{number} to %'char'"
1179 ," -I use terminfo-names"
1180 ," -K use termcap-names and BSD syntax"
1181 ," -L use long names"
1182 ," -R subset (see manpage)"
1183 ," -T eliminate size limits (test)"
1184 ," -U do not post-process entries"
1185 ," -V print version"
1186 ," -W wrap long strings per -w[n]"
1188 ," -a with -F, list commented-out caps"
1190 ," -c list common capabilities"
1191 ," -d list different capabilities"
1192 ," -e format output for C initializer"
1193 ," -f with -1, format complex strings"
1194 ," -g format %'char' to %{number}"
1195 ," -i analyze initialization/reset"
1196 ," -l output terminfo names"
1197 ," -n list capabilities in neither"
1198 ," -p ignore padding specifiers"
1199 ," -Q number dump compiled description"
1200 ," -q brief listing, removes headers"
1201 ," -r with -C, output in termcap form"
1202 ," -r with -F, resolve use-references"
1203 ," -s [d|i|l|c] sort fields"
1205 ," -t suppress commented-out capabilities"
1207 ," -u produce source with 'use='"
1208 ," -v number (verbose)"
1209 ," -w number (width)"
1211 ," -x unknown capabilities are user-defined"
1215 const size_t last = SIZEOF(options);
1216 const size_t left = (last + 1) / 2;
1219 fputs(head, stderr);
1220 for (n = 0; n < left; n++) {
1221 size_t m = n + left;
1223 fprintf(stderr, "%-40.40s%s\n", options[n], options[m]);
1225 fprintf(stderr, "%s\n", options[n]);
1227 ExitProgram(EXIT_FAILURE);
1231 any_initializer(const char *fmt, const char *type)
1233 static char *initializer;
1237 if (initializer == 0) {
1238 need = (strlen(entries->tterm.term_names)
1241 initializer = (char *) malloc(need + 1);
1242 if (initializer == 0)
1243 failed("any_initializer");
1246 _nc_STRCPY(initializer, entries->tterm.term_names, need);
1247 for (s = initializer; *s != 0 && *s != '|'; s++) {
1248 if (!isalnum(UChar(*s)))
1252 _nc_SPRINTF(s, _nc_SLIMIT(need) fmt, type);
1257 name_initializer(const char *type)
1259 return any_initializer("_%s_data", type);
1263 string_variable(const char *type)
1265 return any_initializer("_s_%s", type);
1268 /* dump C initializers for the terminal type */
1270 dump_initializers(TERMTYPE *term)
1273 const char *str = 0;
1275 printf("\nstatic char %s[] = \"%s\";\n\n",
1276 name_initializer("alias"), entries->tterm.term_names);
1278 for_each_string(n, term) {
1279 char buf[MAX_STRING], *sp, *tp;
1281 if (VALID_STRING(term->Strings[n])) {
1283 #define TP_LIMIT ((MAX_STRING - 5) - (size_t)(tp - buf))
1285 for (sp = term->Strings[n];
1286 *sp != 0 && TP_LIMIT > 2;
1288 if (isascii(UChar(*sp))
1289 && isprint(UChar(*sp))
1294 _nc_SPRINTF(tp, _nc_SLIMIT(TP_LIMIT) "\\%03o", UChar(*sp));
1300 (void) printf("static char %-20s[] = %s;\n",
1301 string_variable(ExtStrname(term, (int) n, strnames)),
1307 (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
1309 for_each_boolean(n, term) {
1310 switch ((int) (term->Booleans[n])) {
1319 case ABSENT_BOOLEAN:
1320 str = "ABSENT_BOOLEAN";
1323 case CANCELLED_BOOLEAN:
1324 str = "CANCELLED_BOOLEAN";
1327 (void) printf("\t/* %3u: %-8s */\t%s,\n",
1328 n, ExtBoolname(term, (int) n, boolnames), str);
1330 (void) printf("%s;\n", R_CURL);
1332 (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
1334 for_each_number(n, term) {
1336 switch (term->Numbers[n]) {
1337 case ABSENT_NUMERIC:
1338 str = "ABSENT_NUMERIC";
1340 case CANCELLED_NUMERIC:
1341 str = "CANCELLED_NUMERIC";
1344 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) "%d", term->Numbers[n]);
1348 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1349 ExtNumname(term, (int) n, numnames), str);
1351 (void) printf("%s;\n", R_CURL);
1353 (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
1355 for_each_string(n, term) {
1357 if (term->Strings[n] == ABSENT_STRING)
1358 str = "ABSENT_STRING";
1359 else if (term->Strings[n] == CANCELLED_STRING)
1360 str = "CANCELLED_STRING";
1362 str = string_variable(ExtStrname(term, (int) n, strnames));
1364 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1365 ExtStrname(term, (int) n, strnames), str);
1367 (void) printf("%s;\n", R_CURL);
1370 if ((NUM_BOOLEANS(term) != BOOLCOUNT)
1371 || (NUM_NUMBERS(term) != NUMCOUNT)
1372 || (NUM_STRINGS(term) != STRCOUNT)) {
1373 (void) printf("static char * %s[] = %s\n",
1374 name_initializer("string_ext"), L_CURL);
1375 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
1376 (void) printf("\t/* %3u: bool */\t\"%s\",\n",
1377 n, ExtBoolname(term, (int) n, boolnames));
1379 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
1380 (void) printf("\t/* %3u: num */\t\"%s\",\n",
1381 n, ExtNumname(term, (int) n, numnames));
1383 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
1384 (void) printf("\t/* %3u: str */\t\"%s\",\n",
1385 n, ExtStrname(term, (int) n, strnames));
1387 (void) printf("%s;\n", R_CURL);
1392 /* dump C initializers for the terminal type */
1394 dump_termtype(TERMTYPE *term)
1396 (void) printf("\t%s\n\t\t%s,\n", L_CURL, name_initializer("alias"));
1397 (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
1399 (void) printf("\t\t%s,\n", name_initializer("bool"));
1400 (void) printf("\t\t%s,\n", name_initializer("number"));
1402 (void) printf("\t\t%s,\n", name_initializer("string"));
1405 (void) printf("#if NCURSES_XNAMES\n");
1406 (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
1407 (void) printf("\t\t%s,\t/* ...corresponding names */\n",
1408 ((NUM_BOOLEANS(term) != BOOLCOUNT)
1409 || (NUM_NUMBERS(term) != NUMCOUNT)
1410 || (NUM_STRINGS(term) != STRCOUNT))
1411 ? name_initializer("string_ext")
1414 (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
1415 (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
1416 (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
1418 (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
1419 NUM_BOOLEANS(term) - BOOLCOUNT);
1420 (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
1421 NUM_NUMBERS(term) - NUMCOUNT);
1422 (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
1423 NUM_STRINGS(term) - STRCOUNT);
1425 (void) printf("#endif /* NCURSES_XNAMES */\n");
1428 #endif /* NCURSES_XNAMES */
1429 (void) printf("\t%s\n", R_CURL);
1433 optarg_to_number(void)
1436 long value = strtol(optarg, &temp, 0);
1438 if (temp == 0 || temp == optarg || *temp != 0) {
1439 fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
1440 ExitProgram(EXIT_FAILURE);
1450 if ((terminal = getenv("TERM")) == 0) {
1451 (void) fprintf(stderr,
1452 "%s: environment variable TERM not set\n",
1460 * Show the databases that infocmp knows about. The location to which it writes is
1463 show_databases(void)
1469 _nc_first_db(&state, &offset);
1470 while ((path2 = _nc_next_db(&state, &offset)) != 0) {
1471 printf("%s\n", path2);
1476 /***************************************************************************
1480 ***************************************************************************/
1483 #define MAIN_LEAKS() \
1488 #define MAIN_LEAKS() /* nothing */
1492 main(int argc, char *argv[])
1494 /* Avoid "local data >32k" error with mwcc */
1495 /* Also avoid overflowing smaller stacks on systems like AmigaOS */
1502 char *firstdir, *restdir;
1504 bool formatted = FALSE;
1505 bool filecompare = FALSE;
1507 bool init_analyze = FALSE;
1508 bool suppress_untranslatable = FALSE;
1510 bool wrap_strings = FALSE;
1512 /* where is the terminfo database location going to default to? */
1513 restdir = firstdir = 0;
1516 use_extended_names(FALSE);
1520 _nc_progname = _nc_rootname(argv[0]);
1522 /* make sure we have enough space to add two terminal entries */
1523 myargv = typeCalloc(char *, (size_t) (argc + 3));
1527 memcpy(myargv, argv, (sizeof(char *) * (size_t) argc));
1530 while ((c = getopt(argc,
1532 "01A:aB:CcDdEeFfGgIiKLlnpQ:qR:rs:TtUuVv:Ww:x")) != -1) {
1549 _nc_disable_period = TRUE;
1550 use_extended_names(TRUE);
1561 outform = F_TERMCAP;
1563 if (sortmode == S_DEFAULT)
1564 sortmode = S_TERMCAP;
1569 ExitProgram(EXIT_SUCCESS);
1577 compare = C_DIFFERENCE;
1605 outform = F_TERMINFO;
1606 if (sortmode == S_DEFAULT)
1607 sortmode = S_VARIABLE;
1612 init_analyze = TRUE;
1616 outform = F_VARIABLE;
1617 if (sortmode == S_DEFAULT)
1618 sortmode = S_VARIABLE;
1622 outform = F_TERMINFO;
1634 quickdump = optarg_to_number();
1654 sortmode = S_NOSORT;
1655 else if (*optarg == 'i')
1656 sortmode = S_TERMINFO;
1657 else if (*optarg == 'l')
1658 sortmode = S_VARIABLE;
1659 else if (*optarg == 'c')
1660 sortmode = S_TERMCAP;
1662 (void) fprintf(stderr,
1663 "%s: unknown sort mode\n",
1665 ExitProgram(EXIT_FAILURE);
1675 _nc_disable_period = FALSE;
1676 suppress_untranslatable = TRUE;
1689 puts(curses_version());
1690 ExitProgram(EXIT_SUCCESS);
1693 itrace = (unsigned) optarg_to_number();
1694 set_trace_level(itrace);
1698 wrap_strings = TRUE;
1702 mwidth = optarg_to_number();
1707 use_extended_names(TRUE);
1716 maxterms = (size_t) (argc + 2 - optind);
1717 if ((tfile = typeMalloc(path, maxterms)) == 0)
1719 if ((tname = typeCalloc(char *, maxterms)) == 0)
1721 if ((entries = typeCalloc(ENTRY, maxterms)) == 0)
1724 if ((entered = typeCalloc(ENTERED, maxterms)) == 0)
1731 fprintf(stderr, "%s: not enough memory\n", _nc_progname);
1732 ExitProgram(EXIT_FAILURE);
1735 /* by default, sort by terminfo name */
1736 if (sortmode == S_DEFAULT)
1737 sortmode = S_TERMINFO;
1739 /* make sure we have at least one terminal name to work with */
1741 argv[argc++] = terminal_env();
1743 /* if user is after a comparison, make sure we have two entries */
1744 if (compare != C_DEFAULT && optind >= argc - 1)
1745 argv[argc++] = terminal_env();
1747 /* exactly one terminal name with no options means display it */
1748 /* exactly two terminal names with no options means do -d */
1749 if (compare == C_DEFAULT) {
1750 switch (argc - optind) {
1752 fprintf(stderr, "%s: too many names to compare\n", _nc_progname);
1753 ExitProgram(EXIT_FAILURE);
1757 compare = C_DIFFERENCE;
1762 /* set up for display */
1763 dump_init(tversion, outform, sortmode,
1764 wrap_strings, mwidth, mheight, itrace,
1765 formatted, FALSE, quickdump);
1768 /* grab the entries */
1770 for (; optind < argc; optind++) {
1771 const char *directory = termcount ? restdir : firstdir;
1774 tname[termcount] = argv[optind];
1777 #if NCURSES_USE_DATABASE
1778 #if MIXEDCASE_FILENAMES
1779 #define LEAF_FMT "%c"
1781 #define LEAF_FMT "%02x"
1783 _nc_SPRINTF(tfile[termcount],
1784 _nc_SLIMIT(sizeof(path))
1785 "%s/" LEAF_FMT "/%s",
1787 UChar(*argv[optind]), argv[optind]);
1789 (void) fprintf(stderr,
1790 "%s: reading entry %s from file %s\n",
1792 argv[optind], tfile[termcount]);
1794 status = _nc_read_file_entry(tfile[termcount],
1795 &entries[termcount].tterm);
1797 (void) fprintf(stderr, "%s: terminfo files not supported\n",
1800 ExitProgram(EXIT_FAILURE);
1804 (void) fprintf(stderr,
1805 "%s: reading entry %s from database\n",
1809 status = _nc_read_entry(tname[termcount],
1811 &entries[termcount].tterm);
1815 (void) fprintf(stderr,
1816 "%s: couldn't open terminfo file %s.\n",
1820 ExitProgram(EXIT_FAILURE);
1822 repair_acsc(&entries[termcount].tterm);
1828 _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
1831 /* dump as C initializer for the terminal type */
1834 dump_termtype(&entries[0].tterm);
1836 dump_initializers(&entries[0].tterm);
1839 /* analyze the init strings */
1840 else if (init_analyze) {
1842 #define CUR entries[0].tterm.
1843 analyze_string("is1", init_1string, &entries[0].tterm);
1844 analyze_string("is2", init_2string, &entries[0].tterm);
1845 analyze_string("is3", init_3string, &entries[0].tterm);
1846 analyze_string("rs1", reset_1string, &entries[0].tterm);
1847 analyze_string("rs2", reset_2string, &entries[0].tterm);
1848 analyze_string("rs3", reset_3string, &entries[0].tterm);
1849 analyze_string("smcup", enter_ca_mode, &entries[0].tterm);
1850 analyze_string("rmcup", exit_ca_mode, &entries[0].tterm);
1851 analyze_string("smkx", keypad_xmit, &entries[0].tterm);
1852 analyze_string("rmkx", keypad_local, &entries[0].tterm);
1857 * Here's where the real work gets done
1862 (void) fprintf(stderr,
1863 "%s: about to dump %s\n",
1868 printf("#\tReconstructed via infocmp from file: %s\n",
1870 dump_entry(&entries[0].tterm,
1871 suppress_untranslatable,
1877 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1881 show_comparing(tname);
1882 compare_entry(compare_predicate, &entries->tterm, quiet);
1886 show_comparing(tname);
1887 compare_entry(compare_predicate, &entries->tterm, quiet);
1891 show_comparing(tname);
1892 compare_entry(compare_predicate, &entries->tterm, quiet);
1897 (void) fprintf(stderr, "%s: dumping use entry\n", _nc_progname);
1898 dump_entry(&entries[0].tterm,
1899 suppress_untranslatable,
1903 for (i = 1; i < termcount; i++)
1904 dump_uses(tname[i], !(outform == F_TERMCAP
1905 || outform == F_TCONVERR));
1908 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1912 } else if (compare == C_USEALL) {
1913 (void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
1914 } else if (compare == C_DEFAULT) {
1915 (void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
1916 } else if (argc - optind != 2) {
1917 (void) fprintf(stderr,
1918 "File comparison needs exactly two file arguments.\n");
1920 file_comparison(argc - optind, argv + optind);
1924 ExitProgram(EXIT_SUCCESS);
1927 /* infocmp.c ends here */