1 /****************************************************************************
2 * Copyright (c) 1998-2013,2014 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
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.129 2014/02/01 22:11:03 tom Exp $")
50 #define MAX_STRING 1024 /* maximum formatted string */
52 const char *_nc_progname = "infocmp";
54 typedef char path[PATH_MAX];
56 /***************************************************************************
58 * The following control variables, together with the contents of the
59 * terminfo entries, completely determine the actions of the program.
61 ***************************************************************************/
63 static ENTRY *entries; /* terminfo entries */
64 static int termcount; /* count of terminal entries */
66 static bool limited = TRUE; /* "-r" option is not set */
67 static bool quiet = FALSE;
68 static bool literal = FALSE;
69 static const char *bool_sep = ":";
70 static const char *s_absent = "NULL";
71 static const char *s_cancel = "NULL";
72 static const char *tversion; /* terminfo version selected */
73 static unsigned itrace; /* trace flag for debugging */
74 static int mwidth = 60;
75 static int mheight = 65535;
76 static int numbers = 0; /* format "%'char'" to/from "%{number}" */
77 static int outform = F_TERMINFO; /* output format */
78 static int sortmode; /* sort_mode */
80 /* main comparison mode */
82 #define C_DEFAULT 0 /* don't force comparison mode */
83 #define C_DIFFERENCE 1 /* list differences between two terminals */
84 #define C_COMMON 2 /* list common capabilities */
85 #define C_NAND 3 /* list capabilities in neither terminal */
86 #define C_USEALL 4 /* generate relative use-form entry */
87 static bool ignorepads; /* ignore pad prefixes when diffing */
96 static ENTERED *entered;
99 static void ExitProgram(int code) GCC_NORETURN;
100 /* prototype is to get gcc to accept the noreturn attribute */
102 ExitProgram(int code)
106 for (n = 0; n < termcount; ++n) {
107 ENTRY *new_head = _nc_head;
108 ENTRY *new_tail = _nc_tail;
109 _nc_head = entered[n].head;
110 _nc_tail = entered[n].tail;
111 _nc_free_entries(entered[n].head);
115 _nc_leaks_dump_entry();
123 failed(const char *s)
126 ExitProgram(EXIT_FAILURE);
130 canonical_name(char *ptr, char *buf)
131 /* extract the terminal type's primary name */
135 _nc_STRCPY(buf, ptr, NAMESIZE);
136 if ((bp = strchr(buf, '|')) != 0)
142 /***************************************************************************
144 * Predicates for dump function
146 ***************************************************************************/
149 capcmp(PredIdx idx, const char *s, const char *t)
150 /* capability comparison function */
152 if (!VALID_STRING(s) && !VALID_STRING(t))
154 else if (!VALID_STRING(s) || !VALID_STRING(t))
157 if ((idx == acs_chars_index) || !ignorepads)
158 return (strcmp(s, t));
160 return (_nc_capcmp(s, t));
164 use_predicate(unsigned type, PredIdx idx)
165 /* predicate function to use for use decompilation */
175 * This assumes that multiple use entries are supposed
176 * to contribute the logical or of their boolean capabilities.
177 * This is true if we take the semantics of multiple uses to
178 * be 'each capability gets the first non-default value found
179 * in the sequence of use entries'.
181 * Note that cancelled or absent booleans are stored as FALSE,
182 * unlike numbers and strings, whose cancelled/absent state is
183 * recorded in the terminfo database.
185 for (ep = &entries[1]; ep < entries + termcount; ep++)
186 if (ep->tterm.Booleans[idx] == TRUE) {
187 is_set = entries[0].tterm.Booleans[idx];
190 if (is_set != entries[0].tterm.Booleans[idx])
198 int value = ABSENT_NUMERIC;
201 * We take the semantics of multiple uses to be 'each
202 * capability gets the first non-default value found
203 * in the sequence of use entries'.
205 for (ep = &entries[1]; ep < entries + termcount; ep++)
206 if (VALID_NUMERIC(ep->tterm.Numbers[idx])) {
207 value = ep->tterm.Numbers[idx];
211 if (value != entries[0].tterm.Numbers[idx])
212 return (value != ABSENT_NUMERIC);
219 char *termstr, *usestr = ABSENT_STRING;
221 termstr = entries[0].tterm.Strings[idx];
224 * We take the semantics of multiple uses to be 'each
225 * capability gets the first non-default value found
226 * in the sequence of use entries'.
228 for (ep = &entries[1]; ep < entries + termcount; ep++)
229 if (ep->tterm.Strings[idx]) {
230 usestr = ep->tterm.Strings[idx];
234 if (usestr == ABSENT_STRING && termstr == ABSENT_STRING)
236 else if (!usestr || !termstr || capcmp(idx, usestr, termstr))
243 return (FALSE); /* pacify compiler */
247 useeq(ENTRY * e1, ENTRY * e2)
248 /* are the use references in two entries equivalent? */
252 if (e1->nuses != e2->nuses)
255 /* Ugh...this is quadratic again */
256 for (i = 0; i < e1->nuses; i++) {
257 bool foundmatch = FALSE;
259 /* search second entry for given use reference */
260 for (j = 0; j < e2->nuses; j++)
261 if (!strcmp(e1->uses[i].name, e2->uses[j].name)) {
274 entryeq(TERMTYPE *t1, TERMTYPE *t2)
275 /* are two entries equivalent? */
279 for (i = 0; i < NUM_BOOLEANS(t1); i++)
280 if (t1->Booleans[i] != t2->Booleans[i])
283 for (i = 0; i < NUM_NUMBERS(t1); i++)
284 if (t1->Numbers[i] != t2->Numbers[i])
287 for (i = 0; i < NUM_STRINGS(t1); i++)
288 if (capcmp((PredIdx) i, t1->Strings[i], t2->Strings[i]))
294 #define TIC_EXPAND(result) _nc_tic_expand(result, outform==F_TERMINFO, numbers)
297 print_uses(ENTRY * ep, FILE *fp)
298 /* print an entry's use references */
305 for (i = 0; i < ep->nuses; i++) {
306 fputs(ep->uses[i].name, fp);
307 if (i < ep->nuses - 1)
313 dump_boolean(int val)
314 /* display the value of a boolean capability */
319 case CANCELLED_BOOLEAN:
331 dump_numeric(int val, char *buf)
332 /* display the value of a boolean capability */
336 _nc_STRCPY(buf, s_absent, MAX_STRING);
338 case CANCELLED_NUMERIC:
339 _nc_STRCPY(buf, s_cancel, MAX_STRING);
342 _nc_SPRINTF(buf, _nc_SLIMIT(MAX_STRING) "%d", val);
348 dump_string(char *val, char *buf)
349 /* display the value of a string capability */
351 if (val == ABSENT_STRING)
352 _nc_STRCPY(buf, s_absent, MAX_STRING);
353 else if (val == CANCELLED_STRING)
354 _nc_STRCPY(buf, s_cancel, MAX_STRING);
356 _nc_SPRINTF(buf, _nc_SLIMIT(MAX_STRING)
357 "'%.*s'", MAX_STRING - 3, TIC_EXPAND(val));
362 * Show "comparing..." message for the given terminal names.
365 show_comparing(char **names)
370 (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
374 (void) fprintf(stderr, "%s: dumping common capabilities\n", _nc_progname);
378 (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
383 printf("comparing %s", *names++);
385 printf(" to %s", *names++);
387 printf(", %s", *names++);
395 * ncurses stores two types of non-standard capabilities:
396 * a) capabilities listed past the "STOP-HERE" comment in the Caps file.
397 * These are used in the terminfo source file to provide data for termcaps,
398 * e.g., when there is no equivalent capability in terminfo, as well as for
399 * widely-used non-standard capabilities.
400 * b) user-definable capabilities, via "tic -x".
402 * However, if "-x" is omitted from the tic command, both types of
403 * non-standard capability are not loaded into the terminfo database. This
404 * macro is used for limit-checks against the symbols that tic uses to omit
405 * the two types of non-standard entry.
408 #define check_user_definable(n,limit) if (!_nc_user_definable && (n) > (limit)) break
410 #define check_user_definable(n,limit) if ((n) > (limit)) break
414 * Use these macros to simplify loops on C_COMMON and C_NAND:
416 #define for_each_entry() while (entries[extra].tterm.term_names)
417 #define next_entry (&(entries[extra++].tterm))
420 compare_predicate(PredType type, PredIdx idx, const char *name)
421 /* predicate function to use for entry difference reports */
423 ENTRY *e1 = &entries[0];
424 ENTRY *e2 = &entries[1];
425 char buf1[MAX_STRING];
426 char buf2[MAX_STRING];
435 check_user_definable(idx, BOOLWRITE);
436 b1 = e1->tterm.Booleans[idx];
439 b2 = next_entry->Booleans[idx];
440 if (!(b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN) && b1 != b2)
441 (void) printf("\t%s: %s%s%s.\n",
449 if (b1 != ABSENT_BOOLEAN) {
452 b2 = next_entry->Booleans[idx];
459 (void) printf("\t%s= %s.\n", name, dump_boolean(b1));
465 if (b1 == ABSENT_BOOLEAN) {
468 b2 = next_entry->Booleans[idx];
475 (void) printf("\t!%s.\n", name);
483 check_user_definable(idx, NUMWRITE);
484 n1 = e1->tterm.Numbers[idx];
487 n2 = next_entry->Numbers[idx];
488 if (!((n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)) && n1 != n2) {
489 dump_numeric(n1, buf1);
490 dump_numeric(n2, buf2);
491 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
496 if (n1 != ABSENT_NUMERIC) {
499 n2 = next_entry->Numbers[idx];
506 dump_numeric(n1, buf1);
507 (void) printf("\t%s= %s.\n", name, buf1);
513 if (n1 == ABSENT_NUMERIC) {
516 n2 = next_entry->Numbers[idx];
523 (void) printf("\t!%s.\n", name);
531 check_user_definable(idx, STRWRITE);
532 s1 = e1->tterm.Strings[idx];
535 s2 = next_entry->Strings[idx];
536 if (capcmp(idx, s1, s2)) {
537 dump_string(s1, buf1);
538 dump_string(s2, buf2);
539 if (strcmp(buf1, buf2))
540 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
545 if (s1 != ABSENT_STRING) {
548 s2 = next_entry->Strings[idx];
549 if (capcmp(idx, s1, s2) != 0) {
555 (void) printf("\t%s= '%s'.\n", name, TIC_EXPAND(s1));
561 if (s1 == ABSENT_STRING) {
564 s2 = next_entry->Strings[idx];
571 (void) printf("\t!%s.\n", name);
579 /* unlike the other modes, this compares *all* use entries */
582 if (!useeq(e1, e2)) {
583 (void) fputs("\tuse: ", stdout);
584 print_uses(e1, stdout);
586 print_uses(e2, stdout);
587 fputs(".\n", stdout);
595 e2 = &entries[extra++];
596 if (e2->nuses != e1->nuses || !useeq(e1, e2)) {
602 (void) fputs("\tuse: ", stdout);
603 print_uses(e1, stdout);
604 fputs(".\n", stdout);
613 e2 = &entries[extra++];
614 if (e2->nuses != e1->nuses) {
620 (void) printf("\t!use.\n");
628 /***************************************************************************
630 * Init string analysis
632 ***************************************************************************/
639 static const assoc std_caps[] =
641 /* these are specified by X.364 and iBCS2 */
642 {"\033c", "RIS"}, /* full reset */
643 {"\0337", "SC"}, /* save cursor */
644 {"\0338", "RC"}, /* restore cursor */
645 {"\033[r", "RSR"}, /* not an X.364 mnemonic */
646 {"\033[m", "SGR0"}, /* not an X.364 mnemonic */
647 {"\033[2J", "ED2"}, /* clear page */
649 /* this group is specified by ISO 2022 */
650 {"\033(0", "ISO DEC G0"}, /* enable DEC graphics for G0 */
651 {"\033(A", "ISO UK G0"}, /* enable UK chars for G0 */
652 {"\033(B", "ISO US G0"}, /* enable US chars for G0 */
653 {"\033)0", "ISO DEC G1"}, /* enable DEC graphics for G1 */
654 {"\033)A", "ISO UK G1"}, /* enable UK chars for G1 */
655 {"\033)B", "ISO US G1"}, /* enable US chars for G1 */
657 /* these are DEC private controls widely supported by emulators */
658 {"\033=", "DECPAM"}, /* application keypad mode */
659 {"\033>", "DECPNM"}, /* normal keypad mode */
660 {"\033<", "DECANSI"}, /* enter ANSI mode */
661 {"\033[!p", "DECSTR"}, /* soft reset */
662 {"\033 F", "S7C1T"}, /* 7-bit controls */
664 {(char *) 0, (char *) 0}
667 static const assoc std_modes[] =
668 /* ECMA \E[ ... [hl] modes recognized by many emulators */
670 {"2", "AM"}, /* keyboard action mode */
671 {"4", "IRM"}, /* insert/replace mode */
672 {"12", "SRM"}, /* send/receive mode */
673 {"20", "LNM"}, /* linefeed mode */
674 {(char *) 0, (char *) 0}
677 static const assoc private_modes[] =
678 /* DEC \E[ ... [hl] modes recognized by many emulators */
680 {"1", "CKM"}, /* application cursor keys */
681 {"2", "ANM"}, /* set VT52 mode */
682 {"3", "COLM"}, /* 132-column mode */
683 {"4", "SCLM"}, /* smooth scroll */
684 {"5", "SCNM"}, /* reverse video mode */
685 {"6", "OM"}, /* origin mode */
686 {"7", "AWM"}, /* wraparound mode */
687 {"8", "ARM"}, /* auto-repeat mode */
688 {(char *) 0, (char *) 0}
691 static const assoc ecma_highlights[] =
692 /* recognize ECMA attribute sequences */
694 {"0", "NORMAL"}, /* normal */
695 {"1", "+BOLD"}, /* bold on */
696 {"2", "+DIM"}, /* dim on */
697 {"3", "+ITALIC"}, /* italic on */
698 {"4", "+UNDERLINE"}, /* underline on */
699 {"5", "+BLINK"}, /* blink on */
700 {"6", "+FASTBLINK"}, /* fastblink on */
701 {"7", "+REVERSE"}, /* reverse on */
702 {"8", "+INVISIBLE"}, /* invisible on */
703 {"9", "+DELETED"}, /* deleted on */
704 {"10", "MAIN-FONT"}, /* select primary font */
705 {"11", "ALT-FONT-1"}, /* select alternate font 1 */
706 {"12", "ALT-FONT-2"}, /* select alternate font 2 */
707 {"13", "ALT-FONT-3"}, /* select alternate font 3 */
708 {"14", "ALT-FONT-4"}, /* select alternate font 4 */
709 {"15", "ALT-FONT-5"}, /* select alternate font 5 */
710 {"16", "ALT-FONT-6"}, /* select alternate font 6 */
711 {"17", "ALT-FONT-7"}, /* select alternate font 7 */
712 {"18", "ALT-FONT-1"}, /* select alternate font 1 */
713 {"19", "ALT-FONT-1"}, /* select alternate font 1 */
714 {"20", "FRAKTUR"}, /* Fraktur font */
715 {"21", "DOUBLEUNDER"}, /* double underline */
716 {"22", "-DIM"}, /* dim off */
717 {"23", "-ITALIC"}, /* italic off */
718 {"24", "-UNDERLINE"}, /* underline off */
719 {"25", "-BLINK"}, /* blink off */
720 {"26", "-FASTBLINK"}, /* fastblink off */
721 {"27", "-REVERSE"}, /* reverse off */
722 {"28", "-INVISIBLE"}, /* invisible off */
723 {"29", "-DELETED"}, /* deleted off */
724 {(char *) 0, (char *) 0}
728 skip_csi(const char *cap)
731 if (cap[0] == '\033' && cap[1] == '[')
733 else if (UChar(cap[0]) == 0233)
739 same_param(const char *table, const char *param, size_t length)
742 if (strncmp(table, param, length) == 0) {
743 result = !isdigit(UChar(param[length]));
749 lookup_params(const assoc * table, char *dst, char *src)
752 const char *ep = strtok(src, ";");
760 for (ap = table; ap->from; ap++) {
761 size_t tlen = strlen(ap->from);
763 if (same_param(ap->from, ep, tlen)) {
764 _nc_STRCAT(dst, ap->to, MAX_TERMINFO_LENGTH);
771 _nc_STRCAT(dst, ep, MAX_TERMINFO_LENGTH);
772 _nc_STRCAT(dst, ";", MAX_TERMINFO_LENGTH);
774 ((ep = strtok((char *) 0, ";")));
776 dst[strlen(dst) - 1] = '\0';
784 analyze_string(const char *name, const char *cap, TERMTYPE *tp)
786 char buf2[MAX_TERMINFO_LENGTH];
789 int tp_lines = tp->Numbers[2];
791 if (!VALID_STRING(cap))
793 (void) printf("%s: ", name);
795 for (sp = cap; *sp; sp++) {
800 const char *expansion = 0;
801 char buf3[MAX_TERMINFO_LENGTH];
803 /* first, check other capabilities in this entry */
804 for (i = 0; i < STRCOUNT; i++) {
805 char *cp = tp->Strings[i];
807 /* don't use function-key capabilities */
808 if (strnames[i][0] == 'k' && strnames[i][1] == 'f')
811 if (VALID_STRING(cp) &&
815 (void) strncpy(buf2, sp, len);
818 if (_nc_capcmp(cp, buf2))
821 #define ISRS(s) (!strncmp((s), "is", (size_t) 2) || !strncmp((s), "rs", (size_t) 2))
823 * Theoretically we just passed the test for translation
824 * (equality once the padding is stripped). However, there
825 * are a few more hoops that need to be jumped so that
826 * identical pairs of initialization and reset strings
827 * don't just refer to each other.
829 if (ISRS(name) || ISRS(strnames[i]))
834 expansion = strnames[i];
839 /* now check the standard capabilities */
842 for (ap = std_caps; ap->from; ap++) {
843 size_t adj = (size_t) (csi ? 2 : 0);
845 len = strlen(ap->from);
846 if (csi && skip_csi(ap->from) != csi)
849 && strncmp(ap->from + adj, sp + csi, len - adj) == 0) {
858 /* now check for standard-mode sequences */
860 && (csi = skip_csi(sp)) != 0
861 && (len = (strspn) (sp + csi, "0123456789;"))
862 && (len < sizeof(buf3))
863 && (next = (size_t) csi + len)
864 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
871 (void) strncpy(buf3, sp + csi, len);
873 len += (size_t) csi + 1;
875 expansion = lookup_params(std_modes, buf2, buf3);
878 /* now check for private-mode sequences */
880 && (csi = skip_csi(sp)) != 0
882 && (len = (strspn) (sp + csi + 1, "0123456789;"))
883 && (len < sizeof(buf3))
884 && (next = (size_t) csi + 1 + len)
885 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
892 (void) strncpy(buf3, sp + csi + 1, len);
894 len += (size_t) csi + 2;
896 expansion = lookup_params(private_modes, buf2, buf3);
899 /* now check for ECMA highlight sequences */
901 && (csi = skip_csi(sp)) != 0
902 && (len = (strspn) (sp + csi, "0123456789;")) != 0
903 && (len < sizeof(buf3))
904 && (next = (size_t) csi + len)
905 && sp[next] == 'm') {
907 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
908 (void) strncpy(buf3, sp + csi, len);
910 len += (size_t) csi + 1;
912 expansion = lookup_params(ecma_highlights, buf2, buf3);
916 && (csi = skip_csi(sp)) != 0
918 len = (size_t) csi + 1;
919 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
920 _nc_STRCAT(buf2, ecma_highlights[0].to, sizeof(buf2));
924 /* now check for scroll region reset */
926 && (csi = skip_csi(sp)) != 0) {
927 if (sp[csi] == 'r') {
931 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "1;%dr", tp_lines);
933 if (strncmp(buf2, sp + csi, len) == 0)
939 /* now check for home-down */
941 && (csi = skip_csi(sp)) != 0) {
942 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%d;1H", tp_lines);
944 if (strncmp(buf2, sp + csi, len) == 0) {
947 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%dH", tp_lines);
949 if (strncmp(buf2, sp + csi, len) == 0) {
956 /* now look at the expansion we got, if any */
958 printf("{%s}", expansion);
961 /* couldn't match anything */
964 fputs(TIC_EXPAND(buf2), stdout);
970 /***************************************************************************
974 ***************************************************************************/
977 file_comparison(int argc, char *argv[])
980 /* someday we may allow comparisons on more files */
982 ENTRY *heads[MAXCOMPARE];
986 memset(heads, 0, sizeof(heads));
987 dump_init((char *) 0, F_LITERAL, S_TERMINFO, 0, 65535, itrace, FALSE);
989 for (n = 0; n < argc && n < MAXCOMPARE; n++) {
990 if (freopen(argv[n], "r", stdin) == 0)
991 _nc_err_abort("Can't open %s", argv[n]);
994 entered[n].head = _nc_head;
995 entered[n].tail = _nc_tail;
997 _nc_head = _nc_tail = 0;
999 /* parse entries out of the source file */
1000 _nc_set_source(argv[n]);
1001 _nc_read_entry_source(stdin, NULL, TRUE, literal, NULLHOOK);
1004 (void) fprintf(stderr, "Resolving file %d...\n", n - 0);
1006 /* maybe do use resolution */
1007 if (!_nc_resolve_uses2(!limited, literal)) {
1008 (void) fprintf(stderr,
1009 "There are unresolved use entries in %s:\n",
1011 for_entry_list(qp) {
1013 (void) fputs(qp->tterm.term_names, stderr);
1014 (void) fputc('\n', stderr);
1017 ExitProgram(EXIT_FAILURE);
1020 heads[filecount] = _nc_head;
1024 /* OK, all entries are in core. Ready to do the comparison */
1026 (void) fprintf(stderr, "Entries are now in core...\n");
1028 /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
1029 for (qp = heads[0]; qp; qp = qp->next) {
1030 for (rp = heads[1]; rp; rp = rp->next)
1031 if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
1032 if (qp->ncrosslinks < MAX_CROSSLINKS)
1033 qp->crosslinks[qp->ncrosslinks] = rp;
1036 if (rp->ncrosslinks < MAX_CROSSLINKS)
1037 rp->crosslinks[rp->ncrosslinks] = qp;
1042 /* now we have two circular lists with crosslinks */
1044 (void) fprintf(stderr, "Name matches are done...\n");
1046 for (qp = heads[0]; qp; qp = qp->next) {
1047 if (qp->ncrosslinks > 1) {
1048 (void) fprintf(stderr,
1049 "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
1050 _nc_first_name(qp->tterm.term_names),
1054 for (i = 0; i < qp->ncrosslinks; i++)
1055 (void) fprintf(stderr,
1057 _nc_first_name((qp->crosslinks[i])->tterm.term_names));
1061 for (rp = heads[1]; rp; rp = rp->next) {
1062 if (rp->ncrosslinks > 1) {
1063 (void) fprintf(stderr,
1064 "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
1065 _nc_first_name(rp->tterm.term_names),
1069 for (i = 0; i < rp->ncrosslinks; i++)
1070 (void) fprintf(stderr,
1072 _nc_first_name((rp->crosslinks[i])->tterm.term_names));
1076 (void) printf("In file 1 (%s) only:\n", argv[0]);
1077 for (qp = heads[0]; qp; qp = qp->next)
1078 if (qp->ncrosslinks == 0)
1079 (void) printf("\t%s\n",
1080 _nc_first_name(qp->tterm.term_names));
1082 (void) printf("In file 2 (%s) only:\n", argv[1]);
1083 for (rp = heads[1]; rp; rp = rp->next)
1084 if (rp->ncrosslinks == 0)
1085 (void) printf("\t%s\n",
1086 _nc_first_name(rp->tterm.term_names));
1088 (void) printf("The following entries are equivalent:\n");
1089 for (qp = heads[0]; qp; qp = qp->next) {
1090 if (qp->ncrosslinks == 1) {
1091 rp = qp->crosslinks[0];
1093 repair_acsc(&qp->tterm);
1094 repair_acsc(&rp->tterm);
1096 _nc_align_termtype(&qp->tterm, &rp->tterm);
1098 if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
1099 char name1[NAMESIZE], name2[NAMESIZE];
1101 (void) canonical_name(qp->tterm.term_names, name1);
1102 (void) canonical_name(rp->tterm.term_names, name2);
1104 (void) printf("%s = %s\n", name1, name2);
1109 (void) printf("Differing entries:\n");
1111 for (qp = heads[0]; qp; qp = qp->next) {
1113 if (qp->ncrosslinks == 1) {
1114 rp = qp->crosslinks[0];
1116 /* sorry - we have to do this on each pass */
1117 _nc_align_termtype(&qp->tterm, &rp->tterm);
1119 if (!(entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp))) {
1120 char name1[NAMESIZE], name2[NAMESIZE];
1130 (void) canonical_name(qp->tterm.term_names, name1);
1131 (void) canonical_name(rp->tterm.term_names, name2);
1135 show_comparing(names);
1136 compare_entry(compare_predicate, &entries->tterm, quiet);
1140 show_comparing(names);
1141 compare_entry(compare_predicate, &entries->tterm, quiet);
1145 show_comparing(names);
1146 compare_entry(compare_predicate, &entries->tterm, quiet);
1158 static const char *tbl[] =
1160 "Usage: infocmp [options] [-A directory] [-B directory] [termname...]"
1163 ," -0 print single-row"
1164 ," -1 print single-column"
1165 ," -K use termcap-names and BSD syntax"
1166 ," -C use termcap-names"
1167 ," -F compare terminfo-files"
1168 ," -I use terminfo-names"
1169 ," -L use long names"
1170 ," -R subset (see manpage)"
1171 ," -T eliminate size limits (test)"
1172 ," -U eliminate post-processing of entries"
1173 ," -D print database locations"
1174 ," -V print version"
1176 ," -a with -F, list commented-out caps"
1178 ," -c list common capabilities"
1179 ," -d list different capabilities"
1180 ," -e format output for C initializer"
1181 ," -E format output as C tables"
1182 ," -f with -1, format complex strings"
1183 ," -G format %{number} to %'char'"
1184 ," -g format %'char' to %{number}"
1185 ," -i analyze initialization/reset"
1186 ," -l output terminfo names"
1187 ," -n list capabilities in neither"
1188 ," -p ignore padding specifiers"
1189 ," -q brief listing, removes headers"
1190 ," -r with -C, output in termcap form"
1191 ," -r with -F, resolve use-references"
1192 ," -s [d|i|l|c] sort fields"
1194 ," -t suppress commented-out capabilities"
1196 ," -u produce source with 'use='"
1197 ," -v number (verbose)"
1198 ," -w number (width)"
1200 ," -x treat unknown capabilities as user-defined"
1203 const size_t first = 3;
1204 const size_t last = SIZEOF(tbl);
1205 const size_t left = (last - first + 1) / 2 + first;
1208 for (n = 0; n < left; n++) {
1209 size_t m = (n < first) ? last : n + left - first;
1211 fprintf(stderr, "%-40.40s%s\n", tbl[n], tbl[m]);
1213 fprintf(stderr, "%s\n", tbl[n]);
1215 ExitProgram(EXIT_FAILURE);
1219 any_initializer(const char *fmt, const char *type)
1221 static char *initializer;
1225 if (initializer == 0) {
1226 need = (strlen(entries->tterm.term_names)
1229 initializer = (char *) malloc(need + 1);
1230 if (initializer == 0)
1231 failed("any_initializer");
1234 _nc_STRCPY(initializer, entries->tterm.term_names, need);
1235 for (s = initializer; *s != 0 && *s != '|'; s++) {
1236 if (!isalnum(UChar(*s)))
1240 _nc_SPRINTF(s, _nc_SLIMIT(need) fmt, type);
1245 name_initializer(const char *type)
1247 return any_initializer("_%s_data", type);
1251 string_variable(const char *type)
1253 return any_initializer("_s_%s", type);
1256 /* dump C initializers for the terminal type */
1258 dump_initializers(TERMTYPE *term)
1261 const char *str = 0;
1263 printf("\nstatic char %s[] = \"%s\";\n\n",
1264 name_initializer("alias"), entries->tterm.term_names);
1266 for_each_string(n, term) {
1267 char buf[MAX_STRING], *sp, *tp;
1269 if (VALID_STRING(term->Strings[n])) {
1271 #define TP_LIMIT ((MAX_STRING - 5) - (size_t)(tp - buf))
1273 for (sp = term->Strings[n];
1274 *sp != 0 && TP_LIMIT > 2;
1276 if (isascii(UChar(*sp))
1277 && isprint(UChar(*sp))
1282 _nc_SPRINTF(tp, _nc_SLIMIT(TP_LIMIT) "\\%03o", UChar(*sp));
1288 (void) printf("static char %-20s[] = %s;\n",
1289 string_variable(ExtStrname(term, (int) n, strnames)),
1295 (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
1297 for_each_boolean(n, term) {
1298 switch ((int) (term->Booleans[n])) {
1307 case ABSENT_BOOLEAN:
1308 str = "ABSENT_BOOLEAN";
1311 case CANCELLED_BOOLEAN:
1312 str = "CANCELLED_BOOLEAN";
1315 (void) printf("\t/* %3u: %-8s */\t%s,\n",
1316 n, ExtBoolname(term, (int) n, boolnames), str);
1318 (void) printf("%s;\n", R_CURL);
1320 (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
1322 for_each_number(n, term) {
1324 switch (term->Numbers[n]) {
1325 case ABSENT_NUMERIC:
1326 str = "ABSENT_NUMERIC";
1328 case CANCELLED_NUMERIC:
1329 str = "CANCELLED_NUMERIC";
1332 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) "%d", term->Numbers[n]);
1336 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1337 ExtNumname(term, (int) n, numnames), str);
1339 (void) printf("%s;\n", R_CURL);
1341 (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
1343 for_each_string(n, term) {
1345 if (term->Strings[n] == ABSENT_STRING)
1346 str = "ABSENT_STRING";
1347 else if (term->Strings[n] == CANCELLED_STRING)
1348 str = "CANCELLED_STRING";
1350 str = string_variable(ExtStrname(term, (int) n, strnames));
1352 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1353 ExtStrname(term, (int) n, strnames), str);
1355 (void) printf("%s;\n", R_CURL);
1358 if ((NUM_BOOLEANS(term) != BOOLCOUNT)
1359 || (NUM_NUMBERS(term) != NUMCOUNT)
1360 || (NUM_STRINGS(term) != STRCOUNT)) {
1361 (void) printf("static char * %s[] = %s\n",
1362 name_initializer("string_ext"), L_CURL);
1363 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
1364 (void) printf("\t/* %3u: bool */\t\"%s\",\n",
1365 n, ExtBoolname(term, (int) n, boolnames));
1367 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
1368 (void) printf("\t/* %3u: num */\t\"%s\",\n",
1369 n, ExtNumname(term, (int) n, numnames));
1371 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
1372 (void) printf("\t/* %3u: str */\t\"%s\",\n",
1373 n, ExtStrname(term, (int) n, strnames));
1375 (void) printf("%s;\n", R_CURL);
1380 /* dump C initializers for the terminal type */
1382 dump_termtype(TERMTYPE *term)
1384 (void) printf("\t%s\n\t\t%s,\n", L_CURL, name_initializer("alias"));
1385 (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
1387 (void) printf("\t\t%s,\n", name_initializer("bool"));
1388 (void) printf("\t\t%s,\n", name_initializer("number"));
1390 (void) printf("\t\t%s,\n", name_initializer("string"));
1393 (void) printf("#if NCURSES_XNAMES\n");
1394 (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
1395 (void) printf("\t\t%s,\t/* ...corresponding names */\n",
1396 ((NUM_BOOLEANS(term) != BOOLCOUNT)
1397 || (NUM_NUMBERS(term) != NUMCOUNT)
1398 || (NUM_STRINGS(term) != STRCOUNT))
1399 ? name_initializer("string_ext")
1402 (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
1403 (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
1404 (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
1406 (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
1407 NUM_BOOLEANS(term) - BOOLCOUNT);
1408 (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
1409 NUM_NUMBERS(term) - NUMCOUNT);
1410 (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
1411 NUM_STRINGS(term) - STRCOUNT);
1413 (void) printf("#endif /* NCURSES_XNAMES */\n");
1416 #endif /* NCURSES_XNAMES */
1417 (void) printf("\t%s\n", R_CURL);
1421 optarg_to_number(void)
1424 long value = strtol(optarg, &temp, 0);
1426 if (temp == 0 || temp == optarg || *temp != 0) {
1427 fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
1428 ExitProgram(EXIT_FAILURE);
1438 if ((terminal = getenv("TERM")) == 0) {
1439 (void) fprintf(stderr,
1440 "%s: environment variable TERM not set\n",
1448 * Show the databases that infocmp knows about. The location to which it writes is
1451 show_databases(void)
1457 _nc_first_db(&state, &offset);
1458 while ((path2 = _nc_next_db(&state, &offset)) != 0) {
1459 printf("%s\n", path2);
1464 /***************************************************************************
1468 ***************************************************************************/
1471 #define MAIN_LEAKS() \
1476 #define MAIN_LEAKS() /* nothing */
1480 main(int argc, char *argv[])
1482 /* Avoid "local data >32k" error with mwcc */
1483 /* Also avoid overflowing smaller stacks on systems like AmigaOS */
1490 char *firstdir, *restdir;
1492 bool formatted = FALSE;
1493 bool filecompare = FALSE;
1495 bool init_analyze = FALSE;
1496 bool suppress_untranslatable = FALSE;
1498 /* where is the terminfo database location going to default to? */
1499 restdir = firstdir = 0;
1502 use_extended_names(FALSE);
1506 _nc_progname = _nc_rootname(argv[0]);
1508 /* make sure we have enough space to add two terminal entries */
1509 myargv = typeCalloc(char *, (size_t) (argc + 3));
1513 memcpy(myargv, argv, (sizeof(char *) * (size_t) argc));
1516 while ((c = getopt(argc,
1518 "01A:aB:CcDdEeFfGgIiKLlnpqR:rs:TtUuVv:w:x")) != -1) {
1535 _nc_disable_period = TRUE;
1536 use_extended_names(TRUE);
1547 outform = F_TERMCAP;
1549 if (sortmode == S_DEFAULT)
1550 sortmode = S_TERMCAP;
1555 ExitProgram(EXIT_SUCCESS);
1563 compare = C_DIFFERENCE;
1591 outform = F_TERMINFO;
1592 if (sortmode == S_DEFAULT)
1593 sortmode = S_VARIABLE;
1598 init_analyze = TRUE;
1602 outform = F_VARIABLE;
1603 if (sortmode == S_DEFAULT)
1604 sortmode = S_VARIABLE;
1608 outform = F_TERMINFO;
1636 sortmode = S_NOSORT;
1637 else if (*optarg == 'i')
1638 sortmode = S_TERMINFO;
1639 else if (*optarg == 'l')
1640 sortmode = S_VARIABLE;
1641 else if (*optarg == 'c')
1642 sortmode = S_TERMCAP;
1644 (void) fprintf(stderr,
1645 "%s: unknown sort mode\n",
1647 ExitProgram(EXIT_FAILURE);
1657 _nc_disable_period = FALSE;
1658 suppress_untranslatable = TRUE;
1671 puts(curses_version());
1672 ExitProgram(EXIT_SUCCESS);
1675 itrace = (unsigned) optarg_to_number();
1676 set_trace_level(itrace);
1680 mwidth = optarg_to_number();
1685 use_extended_names(TRUE);
1694 maxterms = (size_t) (argc + 2 - optind);
1695 if ((tfile = typeMalloc(path, maxterms)) == 0)
1697 if ((tname = typeCalloc(char *, maxterms)) == 0)
1699 if ((entries = typeCalloc(ENTRY, maxterms)) == 0)
1702 if ((entered = typeCalloc(ENTERED, maxterms)) == 0)
1709 fprintf(stderr, "%s: not enough memory\n", _nc_progname);
1710 ExitProgram(EXIT_FAILURE);
1713 /* by default, sort by terminfo name */
1714 if (sortmode == S_DEFAULT)
1715 sortmode = S_TERMINFO;
1717 /* make sure we have at least one terminal name to work with */
1719 argv[argc++] = terminal_env();
1721 /* if user is after a comparison, make sure we have two entries */
1722 if (compare != C_DEFAULT && optind >= argc - 1)
1723 argv[argc++] = terminal_env();
1725 /* exactly one terminal name with no options means display it */
1726 /* exactly two terminal names with no options means do -d */
1727 if (compare == C_DEFAULT) {
1728 switch (argc - optind) {
1730 fprintf(stderr, "%s: too many names to compare\n", _nc_progname);
1731 ExitProgram(EXIT_FAILURE);
1735 compare = C_DIFFERENCE;
1740 /* set up for display */
1741 dump_init(tversion, outform, sortmode, mwidth, mheight, itrace, formatted);
1744 /* grab the entries */
1746 for (; optind < argc; optind++) {
1747 const char *directory = termcount ? restdir : firstdir;
1750 tname[termcount] = argv[optind];
1753 #if NCURSES_USE_DATABASE
1754 #if MIXEDCASE_FILENAMES
1755 #define LEAF_FMT "%c"
1757 #define LEAF_FMT "%02x"
1759 _nc_SPRINTF(tfile[termcount],
1760 _nc_SLIMIT(sizeof(path))
1761 "%s/" LEAF_FMT "/%s",
1763 UChar(*argv[optind]), argv[optind]);
1765 (void) fprintf(stderr,
1766 "%s: reading entry %s from file %s\n",
1768 argv[optind], tfile[termcount]);
1770 status = _nc_read_file_entry(tfile[termcount],
1771 &entries[termcount].tterm);
1773 (void) fprintf(stderr, "%s: terminfo files not supported\n",
1776 ExitProgram(EXIT_FAILURE);
1780 (void) fprintf(stderr,
1781 "%s: reading entry %s from database\n",
1785 status = _nc_read_entry(tname[termcount],
1787 &entries[termcount].tterm);
1791 (void) fprintf(stderr,
1792 "%s: couldn't open terminfo file %s.\n",
1796 ExitProgram(EXIT_FAILURE);
1798 repair_acsc(&entries[termcount].tterm);
1804 _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
1807 /* dump as C initializer for the terminal type */
1810 dump_termtype(&entries[0].tterm);
1812 dump_initializers(&entries[0].tterm);
1815 /* analyze the init strings */
1816 else if (init_analyze) {
1818 #define CUR entries[0].tterm.
1819 analyze_string("is1", init_1string, &entries[0].tterm);
1820 analyze_string("is2", init_2string, &entries[0].tterm);
1821 analyze_string("is3", init_3string, &entries[0].tterm);
1822 analyze_string("rs1", reset_1string, &entries[0].tterm);
1823 analyze_string("rs2", reset_2string, &entries[0].tterm);
1824 analyze_string("rs3", reset_3string, &entries[0].tterm);
1825 analyze_string("smcup", enter_ca_mode, &entries[0].tterm);
1826 analyze_string("rmcup", exit_ca_mode, &entries[0].tterm);
1831 * Here's where the real work gets done
1836 (void) fprintf(stderr,
1837 "%s: about to dump %s\n",
1840 (void) printf("#\tReconstructed via infocmp from file: %s\n",
1842 dump_entry(&entries[0].tterm,
1843 suppress_untranslatable,
1849 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1853 show_comparing(tname);
1854 compare_entry(compare_predicate, &entries->tterm, quiet);
1858 show_comparing(tname);
1859 compare_entry(compare_predicate, &entries->tterm, quiet);
1863 show_comparing(tname);
1864 compare_entry(compare_predicate, &entries->tterm, quiet);
1869 (void) fprintf(stderr, "%s: dumping use entry\n", _nc_progname);
1870 dump_entry(&entries[0].tterm,
1871 suppress_untranslatable,
1875 for (i = 1; i < termcount; i++)
1876 dump_uses(tname[i], !(outform == F_TERMCAP
1877 || outform == F_TCONVERR));
1880 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1884 } else if (compare == C_USEALL) {
1885 (void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
1886 } else if (compare == C_DEFAULT) {
1887 (void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
1888 } else if (argc - optind != 2) {
1889 (void) fprintf(stderr,
1890 "File comparison needs exactly two file arguments.\n");
1892 file_comparison(argc - optind, argv + optind);
1896 ExitProgram(EXIT_SUCCESS);
1899 /* infocmp.c ends here */