1 /****************************************************************************
2 * Copyright (c) 1998-2011,2012 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.118 2012/05/26 21:11:32 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 */
91 static void ExitProgram(int code) GCC_NORETURN;
92 /* prototype is to get gcc to accept the noreturn attribute */
96 while (termcount-- > 0)
97 _nc_free_termtype(&entries[termcount].tterm);
98 _nc_leaks_dump_entry();
105 canonical_name(char *ptr, char *buf)
106 /* extract the terminal type's primary name */
110 _nc_STRCPY(buf, ptr, NAMESIZE);
111 if ((bp = strchr(buf, '|')) != 0)
117 /***************************************************************************
119 * Predicates for dump function
121 ***************************************************************************/
124 capcmp(PredIdx idx, const char *s, const char *t)
125 /* capability comparison function */
127 if (!VALID_STRING(s) && !VALID_STRING(t))
129 else if (!VALID_STRING(s) || !VALID_STRING(t))
132 if ((idx == acs_chars_index) || !ignorepads)
133 return (strcmp(s, t));
135 return (_nc_capcmp(s, t));
139 use_predicate(unsigned type, PredIdx idx)
140 /* predicate function to use for use decompilation */
150 * This assumes that multiple use entries are supposed
151 * to contribute the logical or of their boolean capabilities.
152 * This is true if we take the semantics of multiple uses to
153 * be 'each capability gets the first non-default value found
154 * in the sequence of use entries'.
156 * Note that cancelled or absent booleans are stored as FALSE,
157 * unlike numbers and strings, whose cancelled/absent state is
158 * recorded in the terminfo database.
160 for (ep = &entries[1]; ep < entries + termcount; ep++)
161 if (ep->tterm.Booleans[idx] == TRUE) {
162 is_set = entries[0].tterm.Booleans[idx];
165 if (is_set != entries[0].tterm.Booleans[idx])
173 int value = ABSENT_NUMERIC;
176 * We take the semantics of multiple uses to be 'each
177 * capability gets the first non-default value found
178 * in the sequence of use entries'.
180 for (ep = &entries[1]; ep < entries + termcount; ep++)
181 if (VALID_NUMERIC(ep->tterm.Numbers[idx])) {
182 value = ep->tterm.Numbers[idx];
186 if (value != entries[0].tterm.Numbers[idx])
187 return (value != ABSENT_NUMERIC);
194 char *termstr, *usestr = ABSENT_STRING;
196 termstr = entries[0].tterm.Strings[idx];
199 * We take the semantics of multiple uses to be 'each
200 * capability gets the first non-default value found
201 * in the sequence of use entries'.
203 for (ep = &entries[1]; ep < entries + termcount; ep++)
204 if (ep->tterm.Strings[idx]) {
205 usestr = ep->tterm.Strings[idx];
209 if (usestr == ABSENT_STRING && termstr == ABSENT_STRING)
211 else if (!usestr || !termstr || capcmp(idx, usestr, termstr))
218 return (FALSE); /* pacify compiler */
222 useeq(ENTRY * e1, ENTRY * e2)
223 /* are the use references in two entries equivalent? */
227 if (e1->nuses != e2->nuses)
230 /* Ugh...this is quadratic again */
231 for (i = 0; i < e1->nuses; i++) {
232 bool foundmatch = FALSE;
234 /* search second entry for given use reference */
235 for (j = 0; j < e2->nuses; j++)
236 if (!strcmp(e1->uses[i].name, e2->uses[j].name)) {
249 entryeq(TERMTYPE *t1, TERMTYPE *t2)
250 /* are two entries equivalent? */
254 for (i = 0; i < NUM_BOOLEANS(t1); i++)
255 if (t1->Booleans[i] != t2->Booleans[i])
258 for (i = 0; i < NUM_NUMBERS(t1); i++)
259 if (t1->Numbers[i] != t2->Numbers[i])
262 for (i = 0; i < NUM_STRINGS(t1); i++)
263 if (capcmp((PredIdx) i, t1->Strings[i], t2->Strings[i]))
269 #define TIC_EXPAND(result) _nc_tic_expand(result, outform==F_TERMINFO, numbers)
272 print_uses(ENTRY * ep, FILE *fp)
273 /* print an entry's use references */
280 for (i = 0; i < ep->nuses; i++) {
281 fputs(ep->uses[i].name, fp);
282 if (i < ep->nuses - 1)
288 dump_boolean(int val)
289 /* display the value of a boolean capability */
294 case CANCELLED_BOOLEAN:
306 dump_numeric(int val, char *buf)
307 /* display the value of a boolean capability */
311 _nc_STRCPY(buf, s_absent, MAX_STRING);
313 case CANCELLED_NUMERIC:
314 _nc_STRCPY(buf, s_cancel, MAX_STRING);
317 _nc_SPRINTF(buf, _nc_SLIMIT(MAX_STRING) "%d", val);
323 dump_string(char *val, char *buf)
324 /* display the value of a string capability */
326 if (val == ABSENT_STRING)
327 _nc_STRCPY(buf, s_absent, MAX_STRING);
328 else if (val == CANCELLED_STRING)
329 _nc_STRCPY(buf, s_cancel, MAX_STRING);
331 _nc_SPRINTF(buf, _nc_SLIMIT(MAX_STRING)
332 "'%.*s'", MAX_STRING - 3, TIC_EXPAND(val));
337 * Show "comparing..." message for the given terminal names.
340 show_comparing(char **names)
345 (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
349 (void) fprintf(stderr, "%s: dumping common capabilities\n", _nc_progname);
353 (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
358 printf("comparing %s", *names++);
360 printf(" to %s", *names++);
362 printf(", %s", *names++);
370 * ncurses stores two types of non-standard capabilities:
371 * a) capabilities listed past the "STOP-HERE" comment in the Caps file.
372 * These are used in the terminfo source file to provide data for termcaps,
373 * e.g., when there is no equivalent capability in terminfo, as well as for
374 * widely-used non-standard capabilities.
375 * b) user-definable capabilities, via "tic -x".
377 * However, if "-x" is omitted from the tic command, both types of
378 * non-standard capability are not loaded into the terminfo database. This
379 * macro is used for limit-checks against the symbols that tic uses to omit
380 * the two types of non-standard entry.
382 #define check_user_definable(n,limit) if (!_nc_user_definable && (n) > (limit)) break
385 * Use these macros to simplify loops on C_COMMON and C_NAND:
387 #define for_each_entry() while (entries[extra].tterm.term_names)
388 #define next_entry (&(entries[extra++].tterm))
391 compare_predicate(PredType type, PredIdx idx, const char *name)
392 /* predicate function to use for entry difference reports */
394 ENTRY *e1 = &entries[0];
395 ENTRY *e2 = &entries[1];
396 char buf1[MAX_STRING];
397 char buf2[MAX_STRING];
406 check_user_definable(idx, BOOLWRITE);
407 b1 = e1->tterm.Booleans[idx];
410 b2 = next_entry->Booleans[idx];
411 if (!(b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN) && b1 != b2)
412 (void) printf("\t%s: %s%s%s.\n",
420 if (b1 != ABSENT_BOOLEAN) {
423 b2 = next_entry->Booleans[idx];
430 (void) printf("\t%s= %s.\n", name, dump_boolean(b1));
436 if (b1 == ABSENT_BOOLEAN) {
439 b2 = next_entry->Booleans[idx];
446 (void) printf("\t!%s.\n", name);
454 check_user_definable(idx, NUMWRITE);
455 n1 = e1->tterm.Numbers[idx];
458 n2 = next_entry->Numbers[idx];
459 if (!((n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)) && n1 != n2) {
460 dump_numeric(n1, buf1);
461 dump_numeric(n2, buf2);
462 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
467 if (n1 != ABSENT_NUMERIC) {
470 n2 = next_entry->Numbers[idx];
477 dump_numeric(n1, buf1);
478 (void) printf("\t%s= %s.\n", name, buf1);
484 if (n1 == ABSENT_NUMERIC) {
487 n2 = next_entry->Numbers[idx];
494 (void) printf("\t!%s.\n", name);
502 check_user_definable(idx, STRWRITE);
503 s1 = e1->tterm.Strings[idx];
506 s2 = next_entry->Strings[idx];
507 if (capcmp(idx, s1, s2)) {
508 dump_string(s1, buf1);
509 dump_string(s2, buf2);
510 if (strcmp(buf1, buf2))
511 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
516 if (s1 != ABSENT_STRING) {
519 s2 = next_entry->Strings[idx];
520 if (capcmp(idx, s1, s2) != 0) {
526 (void) printf("\t%s= '%s'.\n", name, TIC_EXPAND(s1));
532 if (s1 == ABSENT_STRING) {
535 s2 = next_entry->Strings[idx];
542 (void) printf("\t!%s.\n", name);
550 /* unlike the other modes, this compares *all* use entries */
553 if (!useeq(e1, e2)) {
554 (void) fputs("\tuse: ", stdout);
555 print_uses(e1, stdout);
557 print_uses(e2, stdout);
558 fputs(".\n", stdout);
566 e2 = &entries[extra++];
567 if (e2->nuses != e1->nuses || !useeq(e1, e2)) {
573 (void) fputs("\tuse: ", stdout);
574 print_uses(e1, stdout);
575 fputs(".\n", stdout);
583 e2 = &entries[extra++];
584 if (e2->nuses != e1->nuses) {
590 (void) printf("\t!use.\n");
598 /***************************************************************************
600 * Init string analysis
602 ***************************************************************************/
609 static const assoc std_caps[] =
611 /* these are specified by X.364 and iBCS2 */
612 {"\033c", "RIS"}, /* full reset */
613 {"\0337", "SC"}, /* save cursor */
614 {"\0338", "RC"}, /* restore cursor */
615 {"\033[r", "RSR"}, /* not an X.364 mnemonic */
616 {"\033[m", "SGR0"}, /* not an X.364 mnemonic */
617 {"\033[2J", "ED2"}, /* clear page */
619 /* this group is specified by ISO 2022 */
620 {"\033(0", "ISO DEC G0"}, /* enable DEC graphics for G0 */
621 {"\033(A", "ISO UK G0"}, /* enable UK chars for G0 */
622 {"\033(B", "ISO US G0"}, /* enable US chars for G0 */
623 {"\033)0", "ISO DEC G1"}, /* enable DEC graphics for G1 */
624 {"\033)A", "ISO UK G1"}, /* enable UK chars for G1 */
625 {"\033)B", "ISO US G1"}, /* enable US chars for G1 */
627 /* these are DEC private controls widely supported by emulators */
628 {"\033=", "DECPAM"}, /* application keypad mode */
629 {"\033>", "DECPNM"}, /* normal keypad mode */
630 {"\033<", "DECANSI"}, /* enter ANSI mode */
631 {"\033[!p", "DECSTR"}, /* soft reset */
632 {"\033 F", "S7C1T"}, /* 7-bit controls */
634 {(char *) 0, (char *) 0}
637 static const assoc std_modes[] =
638 /* ECMA \E[ ... [hl] modes recognized by many emulators */
640 {"2", "AM"}, /* keyboard action mode */
641 {"4", "IRM"}, /* insert/replace mode */
642 {"12", "SRM"}, /* send/receive mode */
643 {"20", "LNM"}, /* linefeed mode */
644 {(char *) 0, (char *) 0}
647 static const assoc private_modes[] =
648 /* DEC \E[ ... [hl] modes recognized by many emulators */
650 {"1", "CKM"}, /* application cursor keys */
651 {"2", "ANM"}, /* set VT52 mode */
652 {"3", "COLM"}, /* 132-column mode */
653 {"4", "SCLM"}, /* smooth scroll */
654 {"5", "SCNM"}, /* reverse video mode */
655 {"6", "OM"}, /* origin mode */
656 {"7", "AWM"}, /* wraparound mode */
657 {"8", "ARM"}, /* auto-repeat mode */
658 {(char *) 0, (char *) 0}
661 static const assoc ecma_highlights[] =
662 /* recognize ECMA attribute sequences */
664 {"0", "NORMAL"}, /* normal */
665 {"1", "+BOLD"}, /* bold on */
666 {"2", "+DIM"}, /* dim on */
667 {"3", "+ITALIC"}, /* italic on */
668 {"4", "+UNDERLINE"}, /* underline on */
669 {"5", "+BLINK"}, /* blink on */
670 {"6", "+FASTBLINK"}, /* fastblink on */
671 {"7", "+REVERSE"}, /* reverse on */
672 {"8", "+INVISIBLE"}, /* invisible on */
673 {"9", "+DELETED"}, /* deleted on */
674 {"10", "MAIN-FONT"}, /* select primary font */
675 {"11", "ALT-FONT-1"}, /* select alternate font 1 */
676 {"12", "ALT-FONT-2"}, /* select alternate font 2 */
677 {"13", "ALT-FONT-3"}, /* select alternate font 3 */
678 {"14", "ALT-FONT-4"}, /* select alternate font 4 */
679 {"15", "ALT-FONT-5"}, /* select alternate font 5 */
680 {"16", "ALT-FONT-6"}, /* select alternate font 6 */
681 {"17", "ALT-FONT-7"}, /* select alternate font 7 */
682 {"18", "ALT-FONT-1"}, /* select alternate font 1 */
683 {"19", "ALT-FONT-1"}, /* select alternate font 1 */
684 {"20", "FRAKTUR"}, /* Fraktur font */
685 {"21", "DOUBLEUNDER"}, /* double underline */
686 {"22", "-DIM"}, /* dim off */
687 {"23", "-ITALIC"}, /* italic off */
688 {"24", "-UNDERLINE"}, /* underline off */
689 {"25", "-BLINK"}, /* blink off */
690 {"26", "-FASTBLINK"}, /* fastblink off */
691 {"27", "-REVERSE"}, /* reverse off */
692 {"28", "-INVISIBLE"}, /* invisible off */
693 {"29", "-DELETED"}, /* deleted off */
694 {(char *) 0, (char *) 0}
698 skip_csi(const char *cap)
701 if (cap[0] == '\033' && cap[1] == '[')
703 else if (UChar(cap[0]) == 0233)
709 same_param(const char *table, const char *param, size_t length)
712 if (strncmp(table, param, length) == 0) {
713 result = !isdigit(UChar(param[length]));
719 lookup_params(const assoc * table, char *dst, char *src)
722 const char *ep = strtok(src, ";");
730 for (ap = table; ap->from; ap++) {
731 size_t tlen = strlen(ap->from);
733 if (same_param(ap->from, ep, tlen)) {
734 _nc_STRCAT(dst, ap->to, MAX_TERMINFO_LENGTH);
741 _nc_STRCAT(dst, ep, MAX_TERMINFO_LENGTH);
742 _nc_STRCAT(dst, ";", MAX_TERMINFO_LENGTH);
744 ((ep = strtok((char *) 0, ";")));
746 dst[strlen(dst) - 1] = '\0';
754 analyze_string(const char *name, const char *cap, TERMTYPE *tp)
756 char buf2[MAX_TERMINFO_LENGTH];
759 int tp_lines = tp->Numbers[2];
761 if (cap == ABSENT_STRING || cap == CANCELLED_STRING)
763 (void) printf("%s: ", name);
765 for (sp = cap; *sp; sp++) {
770 const char *expansion = 0;
771 char buf3[MAX_TERMINFO_LENGTH];
773 /* first, check other capabilities in this entry */
774 for (i = 0; i < STRCOUNT; i++) {
775 char *cp = tp->Strings[i];
777 /* don't use soft-key capabilities */
778 if (strnames[i][0] == 'k' && strnames[i][0] == 'f')
781 if (cp != ABSENT_STRING && cp != CANCELLED_STRING && cp[0] && cp
784 (void) strncpy(buf2, sp, len);
787 if (_nc_capcmp(cp, buf2))
790 #define ISRS(s) (!strncmp((s), "is", 2) || !strncmp((s), "rs", 2))
792 * Theoretically we just passed the test for translation
793 * (equality once the padding is stripped). However, there
794 * are a few more hoops that need to be jumped so that
795 * identical pairs of initialization and reset strings
796 * don't just refer to each other.
798 if (ISRS(name) || ISRS(strnames[i]))
803 expansion = strnames[i];
808 /* now check the standard capabilities */
811 for (ap = std_caps; ap->from; ap++) {
812 size_t adj = (size_t) (csi ? 2 : 0);
814 len = strlen(ap->from);
815 if (csi && skip_csi(ap->from) != csi)
818 && strncmp(ap->from + adj, sp + csi, len - adj) == 0) {
827 /* now check for standard-mode sequences */
829 && (csi = skip_csi(sp)) != 0
830 && (len = strspn(sp + csi, "0123456789;"))
831 && (len < sizeof(buf3))
832 && (next = (size_t) csi + len)
833 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
840 (void) strncpy(buf3, sp + csi, len);
842 len += (size_t) csi + 1;
844 expansion = lookup_params(std_modes, buf2, buf3);
847 /* now check for private-mode sequences */
849 && (csi = skip_csi(sp)) != 0
851 && (len = strspn(sp + csi + 1, "0123456789;"))
852 && (len < sizeof(buf3))
853 && (next = (size_t) csi + 1 + len)
854 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
861 (void) strncpy(buf3, sp + csi + 1, len);
863 len += (size_t) csi + 2;
865 expansion = lookup_params(private_modes, buf2, buf3);
868 /* now check for ECMA highlight sequences */
870 && (csi = skip_csi(sp)) != 0
871 && (len = strspn(sp + csi, "0123456789;")) != 0
872 && (len < sizeof(buf3))
873 && (next = (size_t) csi + len)
874 && sp[next] == 'm') {
876 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
877 (void) strncpy(buf3, sp + csi, len);
879 len += (size_t) csi + 1;
881 expansion = lookup_params(ecma_highlights, buf2, buf3);
885 && (csi = skip_csi(sp)) != 0
887 len = (size_t) csi + 1;
888 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
889 _nc_STRCAT(buf2, ecma_highlights[0].to, sizeof(buf2));
893 /* now check for scroll region reset */
895 && (csi = skip_csi(sp)) != 0) {
896 if (sp[csi] == 'r') {
900 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "1;%dr", tp_lines);
902 if (strncmp(buf2, sp + csi, len) == 0)
908 /* now check for home-down */
910 && (csi = skip_csi(sp)) != 0) {
911 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%d;1H", tp_lines);
913 if (strncmp(buf2, sp + csi, len) == 0) {
916 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%dH", tp_lines);
918 if (strncmp(buf2, sp + csi, len) == 0) {
925 /* now look at the expansion we got, if any */
927 printf("{%s}", expansion);
930 /* couldn't match anything */
933 fputs(TIC_EXPAND(buf2), stdout);
939 /***************************************************************************
943 ***************************************************************************/
946 file_comparison(int argc, char *argv[])
949 /* someday we may allow comparisons on more files */
951 ENTRY *heads[MAXCOMPARE];
955 memset(heads, 0, sizeof(heads));
956 dump_init((char *) 0, F_LITERAL, S_TERMINFO, 0, 65535, itrace, FALSE);
958 for (n = 0; n < argc && n < MAXCOMPARE; n++) {
959 if (freopen(argv[n], "r", stdin) == 0)
960 _nc_err_abort("Can't open %s", argv[n]);
962 _nc_head = _nc_tail = 0;
964 /* parse entries out of the source file */
965 _nc_set_source(argv[n]);
966 _nc_read_entry_source(stdin, NULL, TRUE, literal, NULLHOOK);
969 (void) fprintf(stderr, "Resolving file %d...\n", n - 0);
971 /* maybe do use resolution */
972 if (!_nc_resolve_uses2(!limited, literal)) {
973 (void) fprintf(stderr,
974 "There are unresolved use entries in %s:\n",
978 (void) fputs(qp->tterm.term_names, stderr);
979 (void) fputc('\n', stderr);
982 ExitProgram(EXIT_FAILURE);
985 heads[filecount] = _nc_head;
989 /* OK, all entries are in core. Ready to do the comparison */
991 (void) fprintf(stderr, "Entries are now in core...\n");
993 /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
994 for (qp = heads[0]; qp; qp = qp->next) {
995 for (rp = heads[1]; rp; rp = rp->next)
996 if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
997 if (qp->ncrosslinks < MAX_CROSSLINKS)
998 qp->crosslinks[qp->ncrosslinks] = rp;
1001 if (rp->ncrosslinks < MAX_CROSSLINKS)
1002 rp->crosslinks[rp->ncrosslinks] = qp;
1007 /* now we have two circular lists with crosslinks */
1009 (void) fprintf(stderr, "Name matches are done...\n");
1011 for (qp = heads[0]; qp; qp = qp->next) {
1012 if (qp->ncrosslinks > 1) {
1013 (void) fprintf(stderr,
1014 "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
1015 _nc_first_name(qp->tterm.term_names),
1019 for (i = 0; i < qp->ncrosslinks; i++)
1020 (void) fprintf(stderr,
1022 _nc_first_name((qp->crosslinks[i])->tterm.term_names));
1026 for (rp = heads[1]; rp; rp = rp->next) {
1027 if (rp->ncrosslinks > 1) {
1028 (void) fprintf(stderr,
1029 "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
1030 _nc_first_name(rp->tterm.term_names),
1034 for (i = 0; i < rp->ncrosslinks; i++)
1035 (void) fprintf(stderr,
1037 _nc_first_name((rp->crosslinks[i])->tterm.term_names));
1041 (void) printf("In file 1 (%s) only:\n", argv[0]);
1042 for (qp = heads[0]; qp; qp = qp->next)
1043 if (qp->ncrosslinks == 0)
1044 (void) printf("\t%s\n",
1045 _nc_first_name(qp->tterm.term_names));
1047 (void) printf("In file 2 (%s) only:\n", argv[1]);
1048 for (rp = heads[1]; rp; rp = rp->next)
1049 if (rp->ncrosslinks == 0)
1050 (void) printf("\t%s\n",
1051 _nc_first_name(rp->tterm.term_names));
1053 (void) printf("The following entries are equivalent:\n");
1054 for (qp = heads[0]; qp; qp = qp->next) {
1055 if (qp->ncrosslinks == 1) {
1056 rp = qp->crosslinks[0];
1058 repair_acsc(&qp->tterm);
1059 repair_acsc(&rp->tterm);
1061 _nc_align_termtype(&qp->tterm, &rp->tterm);
1063 if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
1064 char name1[NAMESIZE], name2[NAMESIZE];
1066 (void) canonical_name(qp->tterm.term_names, name1);
1067 (void) canonical_name(rp->tterm.term_names, name2);
1069 (void) printf("%s = %s\n", name1, name2);
1074 (void) printf("Differing entries:\n");
1076 for (qp = heads[0]; qp; qp = qp->next) {
1078 if (qp->ncrosslinks == 1) {
1079 rp = qp->crosslinks[0];
1081 /* sorry - we have to do this on each pass */
1082 _nc_align_termtype(&qp->tterm, &rp->tterm);
1084 if (!(entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp))) {
1085 char name1[NAMESIZE], name2[NAMESIZE];
1095 (void) canonical_name(qp->tterm.term_names, name1);
1096 (void) canonical_name(rp->tterm.term_names, name2);
1100 show_comparing(names);
1101 compare_entry(compare_predicate, &entries->tterm, quiet);
1105 show_comparing(names);
1106 compare_entry(compare_predicate, &entries->tterm, quiet);
1110 show_comparing(names);
1111 compare_entry(compare_predicate, &entries->tterm, quiet);
1123 static const char *tbl[] =
1125 "Usage: infocmp [options] [-A directory] [-B directory] [termname...]"
1128 ," -0 print single-row"
1129 ," -1 print single-column"
1130 ," -K use termcap-names and BSD syntax"
1131 ," -C use termcap-names"
1132 ," -F compare terminfo-files"
1133 ," -I use terminfo-names"
1134 ," -L use long names"
1135 ," -R subset (see manpage)"
1136 ," -T eliminate size limits (test)"
1137 ," -U eliminate post-processing of entries"
1138 ," -V print version"
1140 ," -a with -F, list commented-out caps"
1142 ," -c list common capabilities"
1143 ," -d list different capabilities"
1144 ," -e format output for C initializer"
1145 ," -E format output as C tables"
1146 ," -f with -1, format complex strings"
1147 ," -G format %{number} to %'char'"
1148 ," -g format %'char' to %{number}"
1149 ," -i analyze initialization/reset"
1150 ," -l output terminfo names"
1151 ," -n list capabilities in neither"
1152 ," -p ignore padding specifiers"
1153 ," -q brief listing, removes headers"
1154 ," -r with -C, output in termcap form"
1155 ," -r with -F, resolve use-references"
1156 ," -s [d|i|l|c] sort fields"
1158 ," -t suppress commented-out capabilities"
1160 ," -u produce source with 'use='"
1161 ," -v number (verbose)"
1162 ," -w number (width)"
1164 ," -x treat unknown capabilities as user-defined"
1167 const size_t first = 3;
1168 const size_t last = SIZEOF(tbl);
1169 const size_t left = (last - first + 1) / 2 + first;
1172 for (n = 0; n < left; n++) {
1173 size_t m = (n < first) ? last : n + left - first;
1175 fprintf(stderr, "%-40.40s%s\n", tbl[n], tbl[m]);
1177 fprintf(stderr, "%s\n", tbl[n]);
1179 ExitProgram(EXIT_FAILURE);
1183 any_initializer(const char *fmt, const char *type)
1185 static char *initializer;
1189 if (initializer == 0) {
1190 need = (strlen(entries->tterm.term_names)
1193 initializer = (char *) malloc(need);
1196 _nc_STRCPY(initializer, entries->tterm.term_names, need);
1197 for (s = initializer; *s != 0 && *s != '|'; s++) {
1198 if (!isalnum(UChar(*s)))
1202 _nc_SPRINTF(s, _nc_SLIMIT(need) fmt, type);
1207 name_initializer(const char *type)
1209 return any_initializer("_%s_data", type);
1213 string_variable(const char *type)
1215 return any_initializer("_s_%s", type);
1218 /* dump C initializers for the terminal type */
1220 dump_initializers(TERMTYPE *term)
1223 const char *str = 0;
1225 printf("\nstatic char %s[] = \"%s\";\n\n",
1226 name_initializer("alias"), entries->tterm.term_names);
1228 for_each_string(n, term) {
1229 char buf[MAX_STRING], *sp, *tp;
1231 if (VALID_STRING(term->Strings[n])) {
1233 #define TP_LIMIT ((MAX_STRING - 5) - (size_t)(tp - buf))
1235 for (sp = term->Strings[n];
1236 *sp != 0 && TP_LIMIT > 2;
1238 if (isascii(UChar(*sp))
1239 && isprint(UChar(*sp))
1244 _nc_SPRINTF(tp, _nc_SLIMIT(TP_LIMIT) "\\%03o", UChar(*sp));
1250 (void) printf("static char %-20s[] = %s;\n",
1251 string_variable(ExtStrname(term, (int) n, strnames)),
1257 (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
1259 for_each_boolean(n, term) {
1260 switch ((int) (term->Booleans[n])) {
1269 case ABSENT_BOOLEAN:
1270 str = "ABSENT_BOOLEAN";
1273 case CANCELLED_BOOLEAN:
1274 str = "CANCELLED_BOOLEAN";
1277 (void) printf("\t/* %3u: %-8s */\t%s,\n",
1278 n, ExtBoolname(term, (int) n, boolnames), str);
1280 (void) printf("%s;\n", R_CURL);
1282 (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
1284 for_each_number(n, term) {
1286 switch (term->Numbers[n]) {
1287 case ABSENT_NUMERIC:
1288 str = "ABSENT_NUMERIC";
1290 case CANCELLED_NUMERIC:
1291 str = "CANCELLED_NUMERIC";
1294 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) "%d", term->Numbers[n]);
1298 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1299 ExtNumname(term, (int) n, numnames), str);
1301 (void) printf("%s;\n", R_CURL);
1303 (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
1305 for_each_string(n, term) {
1307 if (term->Strings[n] == ABSENT_STRING)
1308 str = "ABSENT_STRING";
1309 else if (term->Strings[n] == CANCELLED_STRING)
1310 str = "CANCELLED_STRING";
1312 str = string_variable(ExtStrname(term, (int) n, strnames));
1314 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1315 ExtStrname(term, (int) n, strnames), str);
1317 (void) printf("%s;\n", R_CURL);
1320 if ((NUM_BOOLEANS(term) != BOOLCOUNT)
1321 || (NUM_NUMBERS(term) != NUMCOUNT)
1322 || (NUM_STRINGS(term) != STRCOUNT)) {
1323 (void) printf("static char * %s[] = %s\n",
1324 name_initializer("string_ext"), L_CURL);
1325 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
1326 (void) printf("\t/* %3u: bool */\t\"%s\",\n",
1327 n, ExtBoolname(term, (int) n, boolnames));
1329 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
1330 (void) printf("\t/* %3u: num */\t\"%s\",\n",
1331 n, ExtNumname(term, (int) n, numnames));
1333 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
1334 (void) printf("\t/* %3u: str */\t\"%s\",\n",
1335 n, ExtStrname(term, (int) n, strnames));
1337 (void) printf("%s;\n", R_CURL);
1342 /* dump C initializers for the terminal type */
1344 dump_termtype(TERMTYPE *term)
1346 (void) printf("\t%s\n\t\t%s,\n", L_CURL, name_initializer("alias"));
1347 (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
1349 (void) printf("\t\t%s,\n", name_initializer("bool"));
1350 (void) printf("\t\t%s,\n", name_initializer("number"));
1352 (void) printf("\t\t%s,\n", name_initializer("string"));
1355 (void) printf("#if NCURSES_XNAMES\n");
1356 (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
1357 (void) printf("\t\t%s,\t/* ...corresponding names */\n",
1358 ((NUM_BOOLEANS(term) != BOOLCOUNT)
1359 || (NUM_NUMBERS(term) != NUMCOUNT)
1360 || (NUM_STRINGS(term) != STRCOUNT))
1361 ? name_initializer("string_ext")
1364 (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
1365 (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
1366 (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
1368 (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
1369 NUM_BOOLEANS(term) - BOOLCOUNT);
1370 (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
1371 NUM_NUMBERS(term) - NUMCOUNT);
1372 (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
1373 NUM_STRINGS(term) - STRCOUNT);
1375 (void) printf("#endif /* NCURSES_XNAMES */\n");
1378 #endif /* NCURSES_XNAMES */
1379 (void) printf("\t%s\n", R_CURL);
1383 optarg_to_number(void)
1386 long value = strtol(optarg, &temp, 0);
1388 if (temp == 0 || temp == optarg || *temp != 0) {
1389 fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
1390 ExitProgram(EXIT_FAILURE);
1400 if ((terminal = getenv("TERM")) == 0) {
1401 (void) fprintf(stderr,
1402 "%s: environment variable TERM not set\n",
1410 * Show the databases that infocmp knows about. The location to which it writes is
1413 show_databases(void)
1419 _nc_first_db(&state, &offset);
1420 while ((path2 = _nc_next_db(&state, &offset)) != 0) {
1421 printf("%s\n", path2);
1426 /***************************************************************************
1430 ***************************************************************************/
1433 #define MAIN_LEAKS() \
1438 #define MAIN_LEAKS() /* nothing */
1442 main(int argc, char *argv[])
1444 /* Avoid "local data >32k" error with mwcc */
1445 /* Also avoid overflowing smaller stacks on systems like AmigaOS */
1452 char *firstdir, *restdir;
1454 bool formatted = FALSE;
1455 bool filecompare = FALSE;
1457 bool init_analyze = FALSE;
1458 bool suppress_untranslatable = FALSE;
1460 /* where is the terminfo database location going to default to? */
1461 restdir = firstdir = 0;
1464 use_extended_names(FALSE);
1468 _nc_progname = _nc_rootname(argv[0]);
1470 /* make sure we have enough space to add two terminal entries */
1471 myargv = typeCalloc(char *, (size_t) (argc + 3));
1472 memcpy(myargv, argv, (sizeof(char *) * (size_t) argc));
1475 while ((c = getopt(argc,
1477 "01A:aB:CcDdEeFfGgIiKLlnpqR:rs:TtUuVv:w:x")) != -1) {
1494 _nc_disable_period = TRUE;
1495 use_extended_names(TRUE);
1506 outform = F_TERMCAP;
1508 if (sortmode == S_DEFAULT)
1509 sortmode = S_TERMCAP;
1514 ExitProgram(EXIT_SUCCESS);
1522 compare = C_DIFFERENCE;
1550 outform = F_TERMINFO;
1551 if (sortmode == S_DEFAULT)
1552 sortmode = S_VARIABLE;
1557 init_analyze = TRUE;
1561 outform = F_VARIABLE;
1562 if (sortmode == S_DEFAULT)
1563 sortmode = S_VARIABLE;
1567 outform = F_TERMINFO;
1595 sortmode = S_NOSORT;
1596 else if (*optarg == 'i')
1597 sortmode = S_TERMINFO;
1598 else if (*optarg == 'l')
1599 sortmode = S_VARIABLE;
1600 else if (*optarg == 'c')
1601 sortmode = S_TERMCAP;
1603 (void) fprintf(stderr,
1604 "%s: unknown sort mode\n",
1606 ExitProgram(EXIT_FAILURE);
1616 _nc_disable_period = FALSE;
1617 suppress_untranslatable = TRUE;
1630 puts(curses_version());
1631 ExitProgram(EXIT_SUCCESS);
1634 itrace = (unsigned) optarg_to_number();
1635 set_trace_level(itrace);
1639 mwidth = optarg_to_number();
1644 use_extended_names(TRUE);
1653 maxterms = (size_t) (argc + 2 - optind);
1654 tfile = typeMalloc(path, maxterms);
1655 tname = typeCalloc(char *, maxterms);
1656 entries = typeCalloc(ENTRY, maxterms);
1661 fprintf(stderr, "%s: not enough memory\n", _nc_progname);
1662 ExitProgram(EXIT_FAILURE);
1665 /* by default, sort by terminfo name */
1666 if (sortmode == S_DEFAULT)
1667 sortmode = S_TERMINFO;
1669 /* make sure we have at least one terminal name to work with */
1671 argv[argc++] = terminal_env();
1673 /* if user is after a comparison, make sure we have two entries */
1674 if (compare != C_DEFAULT && optind >= argc - 1)
1675 argv[argc++] = terminal_env();
1677 /* exactly one terminal name with no options means display it */
1678 /* exactly two terminal names with no options means do -d */
1679 if (compare == C_DEFAULT) {
1680 switch (argc - optind) {
1682 fprintf(stderr, "%s: too many names to compare\n", _nc_progname);
1683 ExitProgram(EXIT_FAILURE);
1687 compare = C_DIFFERENCE;
1692 /* set up for display */
1693 dump_init(tversion, outform, sortmode, mwidth, mheight, itrace, formatted);
1696 /* grab the entries */
1698 for (; optind < argc; optind++) {
1699 const char *directory = termcount ? restdir : firstdir;
1702 tname[termcount] = argv[optind];
1706 #if MIXEDCASE_FILENAMES
1707 #define LEAF_FMT "%c"
1709 #define LEAF_FMT "%02x"
1711 _nc_SPRINTF(tfile[termcount],
1712 _nc_SLIMIT(sizeof(path))
1713 "%s/" LEAF_FMT "/%s",
1715 UChar(*argv[optind]), argv[optind]);
1717 (void) fprintf(stderr,
1718 "%s: reading entry %s from file %s\n",
1720 argv[optind], tfile[termcount]);
1722 status = _nc_read_file_entry(tfile[termcount],
1723 &entries[termcount].tterm);
1725 (void) fprintf(stderr, "%s: terminfo files not supported\n",
1728 ExitProgram(EXIT_FAILURE);
1732 (void) fprintf(stderr,
1733 "%s: reading entry %s from database\n",
1737 status = _nc_read_entry(tname[termcount],
1739 &entries[termcount].tterm);
1743 (void) fprintf(stderr,
1744 "%s: couldn't open terminfo file %s.\n",
1748 ExitProgram(EXIT_FAILURE);
1750 repair_acsc(&entries[termcount].tterm);
1756 _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
1759 /* dump as C initializer for the terminal type */
1762 dump_termtype(&entries[0].tterm);
1764 dump_initializers(&entries[0].tterm);
1767 /* analyze the init strings */
1768 else if (init_analyze) {
1770 #define CUR entries[0].tterm.
1771 analyze_string("is1", init_1string, &entries[0].tterm);
1772 analyze_string("is2", init_2string, &entries[0].tterm);
1773 analyze_string("is3", init_3string, &entries[0].tterm);
1774 analyze_string("rs1", reset_1string, &entries[0].tterm);
1775 analyze_string("rs2", reset_2string, &entries[0].tterm);
1776 analyze_string("rs3", reset_3string, &entries[0].tterm);
1777 analyze_string("smcup", enter_ca_mode, &entries[0].tterm);
1778 analyze_string("rmcup", exit_ca_mode, &entries[0].tterm);
1783 * Here's where the real work gets done
1788 (void) fprintf(stderr,
1789 "%s: about to dump %s\n",
1792 (void) printf("#\tReconstructed via infocmp from file: %s\n",
1794 dump_entry(&entries[0].tterm,
1795 suppress_untranslatable,
1801 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1805 show_comparing(tname);
1806 compare_entry(compare_predicate, &entries->tterm, quiet);
1810 show_comparing(tname);
1811 compare_entry(compare_predicate, &entries->tterm, quiet);
1815 show_comparing(tname);
1816 compare_entry(compare_predicate, &entries->tterm, quiet);
1821 (void) fprintf(stderr, "%s: dumping use entry\n", _nc_progname);
1822 dump_entry(&entries[0].tterm,
1823 suppress_untranslatable,
1827 for (i = 1; i < termcount; i++)
1828 dump_uses(tname[i], !(outform == F_TERMCAP
1829 || outform == F_TCONVERR));
1832 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1836 } else if (compare == C_USEALL) {
1837 (void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
1838 } else if (compare == C_DEFAULT) {
1839 (void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
1840 } else if (argc - optind != 2) {
1841 (void) fprintf(stderr,
1842 "File comparison needs exactly two file arguments.\n");
1844 file_comparison(argc - optind, argv + optind);
1848 ExitProgram(EXIT_SUCCESS);
1851 /* infocmp.c ends here */