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.120 2012/06/08 23:05:25 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 canonical_name(char *ptr, char *buf)
124 /* extract the terminal type's primary name */
128 _nc_STRCPY(buf, ptr, NAMESIZE);
129 if ((bp = strchr(buf, '|')) != 0)
135 /***************************************************************************
137 * Predicates for dump function
139 ***************************************************************************/
142 capcmp(PredIdx idx, const char *s, const char *t)
143 /* capability comparison function */
145 if (!VALID_STRING(s) && !VALID_STRING(t))
147 else if (!VALID_STRING(s) || !VALID_STRING(t))
150 if ((idx == acs_chars_index) || !ignorepads)
151 return (strcmp(s, t));
153 return (_nc_capcmp(s, t));
157 use_predicate(unsigned type, PredIdx idx)
158 /* predicate function to use for use decompilation */
168 * This assumes that multiple use entries are supposed
169 * to contribute the logical or of their boolean capabilities.
170 * This is true if we take the semantics of multiple uses to
171 * be 'each capability gets the first non-default value found
172 * in the sequence of use entries'.
174 * Note that cancelled or absent booleans are stored as FALSE,
175 * unlike numbers and strings, whose cancelled/absent state is
176 * recorded in the terminfo database.
178 for (ep = &entries[1]; ep < entries + termcount; ep++)
179 if (ep->tterm.Booleans[idx] == TRUE) {
180 is_set = entries[0].tterm.Booleans[idx];
183 if (is_set != entries[0].tterm.Booleans[idx])
191 int value = ABSENT_NUMERIC;
194 * We take the semantics of multiple uses to be 'each
195 * capability gets the first non-default value found
196 * in the sequence of use entries'.
198 for (ep = &entries[1]; ep < entries + termcount; ep++)
199 if (VALID_NUMERIC(ep->tterm.Numbers[idx])) {
200 value = ep->tterm.Numbers[idx];
204 if (value != entries[0].tterm.Numbers[idx])
205 return (value != ABSENT_NUMERIC);
212 char *termstr, *usestr = ABSENT_STRING;
214 termstr = entries[0].tterm.Strings[idx];
217 * We take the semantics of multiple uses to be 'each
218 * capability gets the first non-default value found
219 * in the sequence of use entries'.
221 for (ep = &entries[1]; ep < entries + termcount; ep++)
222 if (ep->tterm.Strings[idx]) {
223 usestr = ep->tterm.Strings[idx];
227 if (usestr == ABSENT_STRING && termstr == ABSENT_STRING)
229 else if (!usestr || !termstr || capcmp(idx, usestr, termstr))
236 return (FALSE); /* pacify compiler */
240 useeq(ENTRY * e1, ENTRY * e2)
241 /* are the use references in two entries equivalent? */
245 if (e1->nuses != e2->nuses)
248 /* Ugh...this is quadratic again */
249 for (i = 0; i < e1->nuses; i++) {
250 bool foundmatch = FALSE;
252 /* search second entry for given use reference */
253 for (j = 0; j < e2->nuses; j++)
254 if (!strcmp(e1->uses[i].name, e2->uses[j].name)) {
267 entryeq(TERMTYPE *t1, TERMTYPE *t2)
268 /* are two entries equivalent? */
272 for (i = 0; i < NUM_BOOLEANS(t1); i++)
273 if (t1->Booleans[i] != t2->Booleans[i])
276 for (i = 0; i < NUM_NUMBERS(t1); i++)
277 if (t1->Numbers[i] != t2->Numbers[i])
280 for (i = 0; i < NUM_STRINGS(t1); i++)
281 if (capcmp((PredIdx) i, t1->Strings[i], t2->Strings[i]))
287 #define TIC_EXPAND(result) _nc_tic_expand(result, outform==F_TERMINFO, numbers)
290 print_uses(ENTRY * ep, FILE *fp)
291 /* print an entry's use references */
298 for (i = 0; i < ep->nuses; i++) {
299 fputs(ep->uses[i].name, fp);
300 if (i < ep->nuses - 1)
306 dump_boolean(int val)
307 /* display the value of a boolean capability */
312 case CANCELLED_BOOLEAN:
324 dump_numeric(int val, char *buf)
325 /* display the value of a boolean capability */
329 _nc_STRCPY(buf, s_absent, MAX_STRING);
331 case CANCELLED_NUMERIC:
332 _nc_STRCPY(buf, s_cancel, MAX_STRING);
335 _nc_SPRINTF(buf, _nc_SLIMIT(MAX_STRING) "%d", val);
341 dump_string(char *val, char *buf)
342 /* display the value of a string capability */
344 if (val == ABSENT_STRING)
345 _nc_STRCPY(buf, s_absent, MAX_STRING);
346 else if (val == CANCELLED_STRING)
347 _nc_STRCPY(buf, s_cancel, MAX_STRING);
349 _nc_SPRINTF(buf, _nc_SLIMIT(MAX_STRING)
350 "'%.*s'", MAX_STRING - 3, TIC_EXPAND(val));
355 * Show "comparing..." message for the given terminal names.
358 show_comparing(char **names)
363 (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
367 (void) fprintf(stderr, "%s: dumping common capabilities\n", _nc_progname);
371 (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
376 printf("comparing %s", *names++);
378 printf(" to %s", *names++);
380 printf(", %s", *names++);
388 * ncurses stores two types of non-standard capabilities:
389 * a) capabilities listed past the "STOP-HERE" comment in the Caps file.
390 * These are used in the terminfo source file to provide data for termcaps,
391 * e.g., when there is no equivalent capability in terminfo, as well as for
392 * widely-used non-standard capabilities.
393 * b) user-definable capabilities, via "tic -x".
395 * However, if "-x" is omitted from the tic command, both types of
396 * non-standard capability are not loaded into the terminfo database. This
397 * macro is used for limit-checks against the symbols that tic uses to omit
398 * the two types of non-standard entry.
400 #define check_user_definable(n,limit) if (!_nc_user_definable && (n) > (limit)) break
403 * Use these macros to simplify loops on C_COMMON and C_NAND:
405 #define for_each_entry() while (entries[extra].tterm.term_names)
406 #define next_entry (&(entries[extra++].tterm))
409 compare_predicate(PredType type, PredIdx idx, const char *name)
410 /* predicate function to use for entry difference reports */
412 ENTRY *e1 = &entries[0];
413 ENTRY *e2 = &entries[1];
414 char buf1[MAX_STRING];
415 char buf2[MAX_STRING];
424 check_user_definable(idx, BOOLWRITE);
425 b1 = e1->tterm.Booleans[idx];
428 b2 = next_entry->Booleans[idx];
429 if (!(b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN) && b1 != b2)
430 (void) printf("\t%s: %s%s%s.\n",
438 if (b1 != ABSENT_BOOLEAN) {
441 b2 = next_entry->Booleans[idx];
448 (void) printf("\t%s= %s.\n", name, dump_boolean(b1));
454 if (b1 == ABSENT_BOOLEAN) {
457 b2 = next_entry->Booleans[idx];
464 (void) printf("\t!%s.\n", name);
472 check_user_definable(idx, NUMWRITE);
473 n1 = e1->tterm.Numbers[idx];
476 n2 = next_entry->Numbers[idx];
477 if (!((n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)) && n1 != n2) {
478 dump_numeric(n1, buf1);
479 dump_numeric(n2, buf2);
480 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
485 if (n1 != ABSENT_NUMERIC) {
488 n2 = next_entry->Numbers[idx];
495 dump_numeric(n1, buf1);
496 (void) printf("\t%s= %s.\n", name, buf1);
502 if (n1 == ABSENT_NUMERIC) {
505 n2 = next_entry->Numbers[idx];
512 (void) printf("\t!%s.\n", name);
520 check_user_definable(idx, STRWRITE);
521 s1 = e1->tterm.Strings[idx];
524 s2 = next_entry->Strings[idx];
525 if (capcmp(idx, s1, s2)) {
526 dump_string(s1, buf1);
527 dump_string(s2, buf2);
528 if (strcmp(buf1, buf2))
529 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
534 if (s1 != ABSENT_STRING) {
537 s2 = next_entry->Strings[idx];
538 if (capcmp(idx, s1, s2) != 0) {
544 (void) printf("\t%s= '%s'.\n", name, TIC_EXPAND(s1));
550 if (s1 == ABSENT_STRING) {
553 s2 = next_entry->Strings[idx];
560 (void) printf("\t!%s.\n", name);
568 /* unlike the other modes, this compares *all* use entries */
571 if (!useeq(e1, e2)) {
572 (void) fputs("\tuse: ", stdout);
573 print_uses(e1, stdout);
575 print_uses(e2, stdout);
576 fputs(".\n", stdout);
584 e2 = &entries[extra++];
585 if (e2->nuses != e1->nuses || !useeq(e1, e2)) {
591 (void) fputs("\tuse: ", stdout);
592 print_uses(e1, stdout);
593 fputs(".\n", stdout);
602 e2 = &entries[extra++];
603 if (e2->nuses != e1->nuses) {
609 (void) printf("\t!use.\n");
617 /***************************************************************************
619 * Init string analysis
621 ***************************************************************************/
628 static const assoc std_caps[] =
630 /* these are specified by X.364 and iBCS2 */
631 {"\033c", "RIS"}, /* full reset */
632 {"\0337", "SC"}, /* save cursor */
633 {"\0338", "RC"}, /* restore cursor */
634 {"\033[r", "RSR"}, /* not an X.364 mnemonic */
635 {"\033[m", "SGR0"}, /* not an X.364 mnemonic */
636 {"\033[2J", "ED2"}, /* clear page */
638 /* this group is specified by ISO 2022 */
639 {"\033(0", "ISO DEC G0"}, /* enable DEC graphics for G0 */
640 {"\033(A", "ISO UK G0"}, /* enable UK chars for G0 */
641 {"\033(B", "ISO US G0"}, /* enable US chars for G0 */
642 {"\033)0", "ISO DEC G1"}, /* enable DEC graphics for G1 */
643 {"\033)A", "ISO UK G1"}, /* enable UK chars for G1 */
644 {"\033)B", "ISO US G1"}, /* enable US chars for G1 */
646 /* these are DEC private controls widely supported by emulators */
647 {"\033=", "DECPAM"}, /* application keypad mode */
648 {"\033>", "DECPNM"}, /* normal keypad mode */
649 {"\033<", "DECANSI"}, /* enter ANSI mode */
650 {"\033[!p", "DECSTR"}, /* soft reset */
651 {"\033 F", "S7C1T"}, /* 7-bit controls */
653 {(char *) 0, (char *) 0}
656 static const assoc std_modes[] =
657 /* ECMA \E[ ... [hl] modes recognized by many emulators */
659 {"2", "AM"}, /* keyboard action mode */
660 {"4", "IRM"}, /* insert/replace mode */
661 {"12", "SRM"}, /* send/receive mode */
662 {"20", "LNM"}, /* linefeed mode */
663 {(char *) 0, (char *) 0}
666 static const assoc private_modes[] =
667 /* DEC \E[ ... [hl] modes recognized by many emulators */
669 {"1", "CKM"}, /* application cursor keys */
670 {"2", "ANM"}, /* set VT52 mode */
671 {"3", "COLM"}, /* 132-column mode */
672 {"4", "SCLM"}, /* smooth scroll */
673 {"5", "SCNM"}, /* reverse video mode */
674 {"6", "OM"}, /* origin mode */
675 {"7", "AWM"}, /* wraparound mode */
676 {"8", "ARM"}, /* auto-repeat mode */
677 {(char *) 0, (char *) 0}
680 static const assoc ecma_highlights[] =
681 /* recognize ECMA attribute sequences */
683 {"0", "NORMAL"}, /* normal */
684 {"1", "+BOLD"}, /* bold on */
685 {"2", "+DIM"}, /* dim on */
686 {"3", "+ITALIC"}, /* italic on */
687 {"4", "+UNDERLINE"}, /* underline on */
688 {"5", "+BLINK"}, /* blink on */
689 {"6", "+FASTBLINK"}, /* fastblink on */
690 {"7", "+REVERSE"}, /* reverse on */
691 {"8", "+INVISIBLE"}, /* invisible on */
692 {"9", "+DELETED"}, /* deleted on */
693 {"10", "MAIN-FONT"}, /* select primary font */
694 {"11", "ALT-FONT-1"}, /* select alternate font 1 */
695 {"12", "ALT-FONT-2"}, /* select alternate font 2 */
696 {"13", "ALT-FONT-3"}, /* select alternate font 3 */
697 {"14", "ALT-FONT-4"}, /* select alternate font 4 */
698 {"15", "ALT-FONT-5"}, /* select alternate font 5 */
699 {"16", "ALT-FONT-6"}, /* select alternate font 6 */
700 {"17", "ALT-FONT-7"}, /* select alternate font 7 */
701 {"18", "ALT-FONT-1"}, /* select alternate font 1 */
702 {"19", "ALT-FONT-1"}, /* select alternate font 1 */
703 {"20", "FRAKTUR"}, /* Fraktur font */
704 {"21", "DOUBLEUNDER"}, /* double underline */
705 {"22", "-DIM"}, /* dim off */
706 {"23", "-ITALIC"}, /* italic off */
707 {"24", "-UNDERLINE"}, /* underline off */
708 {"25", "-BLINK"}, /* blink off */
709 {"26", "-FASTBLINK"}, /* fastblink off */
710 {"27", "-REVERSE"}, /* reverse off */
711 {"28", "-INVISIBLE"}, /* invisible off */
712 {"29", "-DELETED"}, /* deleted off */
713 {(char *) 0, (char *) 0}
717 skip_csi(const char *cap)
720 if (cap[0] == '\033' && cap[1] == '[')
722 else if (UChar(cap[0]) == 0233)
728 same_param(const char *table, const char *param, size_t length)
731 if (strncmp(table, param, length) == 0) {
732 result = !isdigit(UChar(param[length]));
738 lookup_params(const assoc * table, char *dst, char *src)
741 const char *ep = strtok(src, ";");
749 for (ap = table; ap->from; ap++) {
750 size_t tlen = strlen(ap->from);
752 if (same_param(ap->from, ep, tlen)) {
753 _nc_STRCAT(dst, ap->to, MAX_TERMINFO_LENGTH);
760 _nc_STRCAT(dst, ep, MAX_TERMINFO_LENGTH);
761 _nc_STRCAT(dst, ";", MAX_TERMINFO_LENGTH);
763 ((ep = strtok((char *) 0, ";")));
765 dst[strlen(dst) - 1] = '\0';
773 analyze_string(const char *name, const char *cap, TERMTYPE *tp)
775 char buf2[MAX_TERMINFO_LENGTH];
778 int tp_lines = tp->Numbers[2];
780 if (cap == ABSENT_STRING || cap == CANCELLED_STRING)
782 (void) printf("%s: ", name);
784 for (sp = cap; *sp; sp++) {
789 const char *expansion = 0;
790 char buf3[MAX_TERMINFO_LENGTH];
792 /* first, check other capabilities in this entry */
793 for (i = 0; i < STRCOUNT; i++) {
794 char *cp = tp->Strings[i];
796 /* don't use soft-key capabilities */
797 if (strnames[i][0] == 'k' && strnames[i][0] == 'f')
800 if (cp != ABSENT_STRING && cp != CANCELLED_STRING && cp[0] && cp
803 (void) strncpy(buf2, sp, len);
806 if (_nc_capcmp(cp, buf2))
809 #define ISRS(s) (!strncmp((s), "is", 2) || !strncmp((s), "rs", 2))
811 * Theoretically we just passed the test for translation
812 * (equality once the padding is stripped). However, there
813 * are a few more hoops that need to be jumped so that
814 * identical pairs of initialization and reset strings
815 * don't just refer to each other.
817 if (ISRS(name) || ISRS(strnames[i]))
822 expansion = strnames[i];
827 /* now check the standard capabilities */
830 for (ap = std_caps; ap->from; ap++) {
831 size_t adj = (size_t) (csi ? 2 : 0);
833 len = strlen(ap->from);
834 if (csi && skip_csi(ap->from) != csi)
837 && strncmp(ap->from + adj, sp + csi, len - adj) == 0) {
846 /* now check for standard-mode sequences */
848 && (csi = skip_csi(sp)) != 0
849 && (len = strspn(sp + csi, "0123456789;"))
850 && (len < sizeof(buf3))
851 && (next = (size_t) csi + len)
852 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
859 (void) strncpy(buf3, sp + csi, len);
861 len += (size_t) csi + 1;
863 expansion = lookup_params(std_modes, buf2, buf3);
866 /* now check for private-mode sequences */
868 && (csi = skip_csi(sp)) != 0
870 && (len = strspn(sp + csi + 1, "0123456789;"))
871 && (len < sizeof(buf3))
872 && (next = (size_t) csi + 1 + len)
873 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
880 (void) strncpy(buf3, sp + csi + 1, len);
882 len += (size_t) csi + 2;
884 expansion = lookup_params(private_modes, buf2, buf3);
887 /* now check for ECMA highlight sequences */
889 && (csi = skip_csi(sp)) != 0
890 && (len = strspn(sp + csi, "0123456789;")) != 0
891 && (len < sizeof(buf3))
892 && (next = (size_t) csi + len)
893 && sp[next] == 'm') {
895 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
896 (void) strncpy(buf3, sp + csi, len);
898 len += (size_t) csi + 1;
900 expansion = lookup_params(ecma_highlights, buf2, buf3);
904 && (csi = skip_csi(sp)) != 0
906 len = (size_t) csi + 1;
907 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
908 _nc_STRCAT(buf2, ecma_highlights[0].to, sizeof(buf2));
912 /* now check for scroll region reset */
914 && (csi = skip_csi(sp)) != 0) {
915 if (sp[csi] == 'r') {
919 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "1;%dr", tp_lines);
921 if (strncmp(buf2, sp + csi, len) == 0)
927 /* now check for home-down */
929 && (csi = skip_csi(sp)) != 0) {
930 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%d;1H", tp_lines);
932 if (strncmp(buf2, sp + csi, len) == 0) {
935 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%dH", tp_lines);
937 if (strncmp(buf2, sp + csi, len) == 0) {
944 /* now look at the expansion we got, if any */
946 printf("{%s}", expansion);
949 /* couldn't match anything */
952 fputs(TIC_EXPAND(buf2), stdout);
958 /***************************************************************************
962 ***************************************************************************/
965 file_comparison(int argc, char *argv[])
968 /* someday we may allow comparisons on more files */
970 ENTRY *heads[MAXCOMPARE];
974 memset(heads, 0, sizeof(heads));
975 dump_init((char *) 0, F_LITERAL, S_TERMINFO, 0, 65535, itrace, FALSE);
977 for (n = 0; n < argc && n < MAXCOMPARE; n++) {
978 if (freopen(argv[n], "r", stdin) == 0)
979 _nc_err_abort("Can't open %s", argv[n]);
982 entered[n].head = _nc_head;
983 entered[n].tail = _nc_tail;
985 _nc_head = _nc_tail = 0;
987 /* parse entries out of the source file */
988 _nc_set_source(argv[n]);
989 _nc_read_entry_source(stdin, NULL, TRUE, literal, NULLHOOK);
992 (void) fprintf(stderr, "Resolving file %d...\n", n - 0);
994 /* maybe do use resolution */
995 if (!_nc_resolve_uses2(!limited, literal)) {
996 (void) fprintf(stderr,
997 "There are unresolved use entries in %s:\n",
1001 (void) fputs(qp->tterm.term_names, stderr);
1002 (void) fputc('\n', stderr);
1005 ExitProgram(EXIT_FAILURE);
1008 heads[filecount] = _nc_head;
1012 /* OK, all entries are in core. Ready to do the comparison */
1014 (void) fprintf(stderr, "Entries are now in core...\n");
1016 /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
1017 for (qp = heads[0]; qp; qp = qp->next) {
1018 for (rp = heads[1]; rp; rp = rp->next)
1019 if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
1020 if (qp->ncrosslinks < MAX_CROSSLINKS)
1021 qp->crosslinks[qp->ncrosslinks] = rp;
1024 if (rp->ncrosslinks < MAX_CROSSLINKS)
1025 rp->crosslinks[rp->ncrosslinks] = qp;
1030 /* now we have two circular lists with crosslinks */
1032 (void) fprintf(stderr, "Name matches are done...\n");
1034 for (qp = heads[0]; qp; qp = qp->next) {
1035 if (qp->ncrosslinks > 1) {
1036 (void) fprintf(stderr,
1037 "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
1038 _nc_first_name(qp->tterm.term_names),
1042 for (i = 0; i < qp->ncrosslinks; i++)
1043 (void) fprintf(stderr,
1045 _nc_first_name((qp->crosslinks[i])->tterm.term_names));
1049 for (rp = heads[1]; rp; rp = rp->next) {
1050 if (rp->ncrosslinks > 1) {
1051 (void) fprintf(stderr,
1052 "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
1053 _nc_first_name(rp->tterm.term_names),
1057 for (i = 0; i < rp->ncrosslinks; i++)
1058 (void) fprintf(stderr,
1060 _nc_first_name((rp->crosslinks[i])->tterm.term_names));
1064 (void) printf("In file 1 (%s) only:\n", argv[0]);
1065 for (qp = heads[0]; qp; qp = qp->next)
1066 if (qp->ncrosslinks == 0)
1067 (void) printf("\t%s\n",
1068 _nc_first_name(qp->tterm.term_names));
1070 (void) printf("In file 2 (%s) only:\n", argv[1]);
1071 for (rp = heads[1]; rp; rp = rp->next)
1072 if (rp->ncrosslinks == 0)
1073 (void) printf("\t%s\n",
1074 _nc_first_name(rp->tterm.term_names));
1076 (void) printf("The following entries are equivalent:\n");
1077 for (qp = heads[0]; qp; qp = qp->next) {
1078 if (qp->ncrosslinks == 1) {
1079 rp = qp->crosslinks[0];
1081 repair_acsc(&qp->tterm);
1082 repair_acsc(&rp->tterm);
1084 _nc_align_termtype(&qp->tterm, &rp->tterm);
1086 if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
1087 char name1[NAMESIZE], name2[NAMESIZE];
1089 (void) canonical_name(qp->tterm.term_names, name1);
1090 (void) canonical_name(rp->tterm.term_names, name2);
1092 (void) printf("%s = %s\n", name1, name2);
1097 (void) printf("Differing entries:\n");
1099 for (qp = heads[0]; qp; qp = qp->next) {
1101 if (qp->ncrosslinks == 1) {
1102 rp = qp->crosslinks[0];
1104 /* sorry - we have to do this on each pass */
1105 _nc_align_termtype(&qp->tterm, &rp->tterm);
1107 if (!(entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp))) {
1108 char name1[NAMESIZE], name2[NAMESIZE];
1118 (void) canonical_name(qp->tterm.term_names, name1);
1119 (void) canonical_name(rp->tterm.term_names, name2);
1123 show_comparing(names);
1124 compare_entry(compare_predicate, &entries->tterm, quiet);
1128 show_comparing(names);
1129 compare_entry(compare_predicate, &entries->tterm, quiet);
1133 show_comparing(names);
1134 compare_entry(compare_predicate, &entries->tterm, quiet);
1146 static const char *tbl[] =
1148 "Usage: infocmp [options] [-A directory] [-B directory] [termname...]"
1151 ," -0 print single-row"
1152 ," -1 print single-column"
1153 ," -K use termcap-names and BSD syntax"
1154 ," -C use termcap-names"
1155 ," -F compare terminfo-files"
1156 ," -I use terminfo-names"
1157 ," -L use long names"
1158 ," -R subset (see manpage)"
1159 ," -T eliminate size limits (test)"
1160 ," -U eliminate post-processing of entries"
1161 ," -V print version"
1163 ," -a with -F, list commented-out caps"
1165 ," -c list common capabilities"
1166 ," -d list different capabilities"
1167 ," -e format output for C initializer"
1168 ," -E format output as C tables"
1169 ," -f with -1, format complex strings"
1170 ," -G format %{number} to %'char'"
1171 ," -g format %'char' to %{number}"
1172 ," -i analyze initialization/reset"
1173 ," -l output terminfo names"
1174 ," -n list capabilities in neither"
1175 ," -p ignore padding specifiers"
1176 ," -q brief listing, removes headers"
1177 ," -r with -C, output in termcap form"
1178 ," -r with -F, resolve use-references"
1179 ," -s [d|i|l|c] sort fields"
1181 ," -t suppress commented-out capabilities"
1183 ," -u produce source with 'use='"
1184 ," -v number (verbose)"
1185 ," -w number (width)"
1187 ," -x treat unknown capabilities as user-defined"
1190 const size_t first = 3;
1191 const size_t last = SIZEOF(tbl);
1192 const size_t left = (last - first + 1) / 2 + first;
1195 for (n = 0; n < left; n++) {
1196 size_t m = (n < first) ? last : n + left - first;
1198 fprintf(stderr, "%-40.40s%s\n", tbl[n], tbl[m]);
1200 fprintf(stderr, "%s\n", tbl[n]);
1202 ExitProgram(EXIT_FAILURE);
1206 any_initializer(const char *fmt, const char *type)
1208 static char *initializer;
1212 if (initializer == 0) {
1213 need = (strlen(entries->tterm.term_names)
1216 initializer = (char *) malloc(need);
1219 _nc_STRCPY(initializer, entries->tterm.term_names, need);
1220 for (s = initializer; *s != 0 && *s != '|'; s++) {
1221 if (!isalnum(UChar(*s)))
1225 _nc_SPRINTF(s, _nc_SLIMIT(need) fmt, type);
1230 name_initializer(const char *type)
1232 return any_initializer("_%s_data", type);
1236 string_variable(const char *type)
1238 return any_initializer("_s_%s", type);
1241 /* dump C initializers for the terminal type */
1243 dump_initializers(TERMTYPE *term)
1246 const char *str = 0;
1248 printf("\nstatic char %s[] = \"%s\";\n\n",
1249 name_initializer("alias"), entries->tterm.term_names);
1251 for_each_string(n, term) {
1252 char buf[MAX_STRING], *sp, *tp;
1254 if (VALID_STRING(term->Strings[n])) {
1256 #define TP_LIMIT ((MAX_STRING - 5) - (size_t)(tp - buf))
1258 for (sp = term->Strings[n];
1259 *sp != 0 && TP_LIMIT > 2;
1261 if (isascii(UChar(*sp))
1262 && isprint(UChar(*sp))
1267 _nc_SPRINTF(tp, _nc_SLIMIT(TP_LIMIT) "\\%03o", UChar(*sp));
1273 (void) printf("static char %-20s[] = %s;\n",
1274 string_variable(ExtStrname(term, (int) n, strnames)),
1280 (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
1282 for_each_boolean(n, term) {
1283 switch ((int) (term->Booleans[n])) {
1292 case ABSENT_BOOLEAN:
1293 str = "ABSENT_BOOLEAN";
1296 case CANCELLED_BOOLEAN:
1297 str = "CANCELLED_BOOLEAN";
1300 (void) printf("\t/* %3u: %-8s */\t%s,\n",
1301 n, ExtBoolname(term, (int) n, boolnames), str);
1303 (void) printf("%s;\n", R_CURL);
1305 (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
1307 for_each_number(n, term) {
1309 switch (term->Numbers[n]) {
1310 case ABSENT_NUMERIC:
1311 str = "ABSENT_NUMERIC";
1313 case CANCELLED_NUMERIC:
1314 str = "CANCELLED_NUMERIC";
1317 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) "%d", term->Numbers[n]);
1321 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1322 ExtNumname(term, (int) n, numnames), str);
1324 (void) printf("%s;\n", R_CURL);
1326 (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
1328 for_each_string(n, term) {
1330 if (term->Strings[n] == ABSENT_STRING)
1331 str = "ABSENT_STRING";
1332 else if (term->Strings[n] == CANCELLED_STRING)
1333 str = "CANCELLED_STRING";
1335 str = string_variable(ExtStrname(term, (int) n, strnames));
1337 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1338 ExtStrname(term, (int) n, strnames), str);
1340 (void) printf("%s;\n", R_CURL);
1343 if ((NUM_BOOLEANS(term) != BOOLCOUNT)
1344 || (NUM_NUMBERS(term) != NUMCOUNT)
1345 || (NUM_STRINGS(term) != STRCOUNT)) {
1346 (void) printf("static char * %s[] = %s\n",
1347 name_initializer("string_ext"), L_CURL);
1348 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
1349 (void) printf("\t/* %3u: bool */\t\"%s\",\n",
1350 n, ExtBoolname(term, (int) n, boolnames));
1352 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
1353 (void) printf("\t/* %3u: num */\t\"%s\",\n",
1354 n, ExtNumname(term, (int) n, numnames));
1356 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
1357 (void) printf("\t/* %3u: str */\t\"%s\",\n",
1358 n, ExtStrname(term, (int) n, strnames));
1360 (void) printf("%s;\n", R_CURL);
1365 /* dump C initializers for the terminal type */
1367 dump_termtype(TERMTYPE *term)
1369 (void) printf("\t%s\n\t\t%s,\n", L_CURL, name_initializer("alias"));
1370 (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
1372 (void) printf("\t\t%s,\n", name_initializer("bool"));
1373 (void) printf("\t\t%s,\n", name_initializer("number"));
1375 (void) printf("\t\t%s,\n", name_initializer("string"));
1378 (void) printf("#if NCURSES_XNAMES\n");
1379 (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
1380 (void) printf("\t\t%s,\t/* ...corresponding names */\n",
1381 ((NUM_BOOLEANS(term) != BOOLCOUNT)
1382 || (NUM_NUMBERS(term) != NUMCOUNT)
1383 || (NUM_STRINGS(term) != STRCOUNT))
1384 ? name_initializer("string_ext")
1387 (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
1388 (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
1389 (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
1391 (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
1392 NUM_BOOLEANS(term) - BOOLCOUNT);
1393 (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
1394 NUM_NUMBERS(term) - NUMCOUNT);
1395 (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
1396 NUM_STRINGS(term) - STRCOUNT);
1398 (void) printf("#endif /* NCURSES_XNAMES */\n");
1401 #endif /* NCURSES_XNAMES */
1402 (void) printf("\t%s\n", R_CURL);
1406 optarg_to_number(void)
1409 long value = strtol(optarg, &temp, 0);
1411 if (temp == 0 || temp == optarg || *temp != 0) {
1412 fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
1413 ExitProgram(EXIT_FAILURE);
1423 if ((terminal = getenv("TERM")) == 0) {
1424 (void) fprintf(stderr,
1425 "%s: environment variable TERM not set\n",
1433 * Show the databases that infocmp knows about. The location to which it writes is
1436 show_databases(void)
1442 _nc_first_db(&state, &offset);
1443 while ((path2 = _nc_next_db(&state, &offset)) != 0) {
1444 printf("%s\n", path2);
1449 /***************************************************************************
1453 ***************************************************************************/
1456 #define MAIN_LEAKS() \
1461 #define MAIN_LEAKS() /* nothing */
1465 main(int argc, char *argv[])
1467 /* Avoid "local data >32k" error with mwcc */
1468 /* Also avoid overflowing smaller stacks on systems like AmigaOS */
1475 char *firstdir, *restdir;
1477 bool formatted = FALSE;
1478 bool filecompare = FALSE;
1480 bool init_analyze = FALSE;
1481 bool suppress_untranslatable = FALSE;
1483 /* where is the terminfo database location going to default to? */
1484 restdir = firstdir = 0;
1487 use_extended_names(FALSE);
1491 _nc_progname = _nc_rootname(argv[0]);
1493 /* make sure we have enough space to add two terminal entries */
1494 myargv = typeCalloc(char *, (size_t) (argc + 3));
1495 memcpy(myargv, argv, (sizeof(char *) * (size_t) argc));
1498 while ((c = getopt(argc,
1500 "01A:aB:CcDdEeFfGgIiKLlnpqR:rs:TtUuVv:w:x")) != -1) {
1517 _nc_disable_period = TRUE;
1518 use_extended_names(TRUE);
1529 outform = F_TERMCAP;
1531 if (sortmode == S_DEFAULT)
1532 sortmode = S_TERMCAP;
1537 ExitProgram(EXIT_SUCCESS);
1545 compare = C_DIFFERENCE;
1573 outform = F_TERMINFO;
1574 if (sortmode == S_DEFAULT)
1575 sortmode = S_VARIABLE;
1580 init_analyze = TRUE;
1584 outform = F_VARIABLE;
1585 if (sortmode == S_DEFAULT)
1586 sortmode = S_VARIABLE;
1590 outform = F_TERMINFO;
1618 sortmode = S_NOSORT;
1619 else if (*optarg == 'i')
1620 sortmode = S_TERMINFO;
1621 else if (*optarg == 'l')
1622 sortmode = S_VARIABLE;
1623 else if (*optarg == 'c')
1624 sortmode = S_TERMCAP;
1626 (void) fprintf(stderr,
1627 "%s: unknown sort mode\n",
1629 ExitProgram(EXIT_FAILURE);
1639 _nc_disable_period = FALSE;
1640 suppress_untranslatable = TRUE;
1653 puts(curses_version());
1654 ExitProgram(EXIT_SUCCESS);
1657 itrace = (unsigned) optarg_to_number();
1658 set_trace_level(itrace);
1662 mwidth = optarg_to_number();
1667 use_extended_names(TRUE);
1676 maxterms = (size_t) (argc + 2 - optind);
1677 tfile = typeMalloc(path, maxterms);
1678 tname = typeCalloc(char *, maxterms);
1679 entries = typeCalloc(ENTRY, maxterms);
1681 entered = typeCalloc(ENTERED, maxterms);
1687 fprintf(stderr, "%s: not enough memory\n", _nc_progname);
1688 ExitProgram(EXIT_FAILURE);
1691 /* by default, sort by terminfo name */
1692 if (sortmode == S_DEFAULT)
1693 sortmode = S_TERMINFO;
1695 /* make sure we have at least one terminal name to work with */
1697 argv[argc++] = terminal_env();
1699 /* if user is after a comparison, make sure we have two entries */
1700 if (compare != C_DEFAULT && optind >= argc - 1)
1701 argv[argc++] = terminal_env();
1703 /* exactly one terminal name with no options means display it */
1704 /* exactly two terminal names with no options means do -d */
1705 if (compare == C_DEFAULT) {
1706 switch (argc - optind) {
1708 fprintf(stderr, "%s: too many names to compare\n", _nc_progname);
1709 ExitProgram(EXIT_FAILURE);
1713 compare = C_DIFFERENCE;
1718 /* set up for display */
1719 dump_init(tversion, outform, sortmode, mwidth, mheight, itrace, formatted);
1722 /* grab the entries */
1724 for (; optind < argc; optind++) {
1725 const char *directory = termcount ? restdir : firstdir;
1728 tname[termcount] = argv[optind];
1732 #if MIXEDCASE_FILENAMES
1733 #define LEAF_FMT "%c"
1735 #define LEAF_FMT "%02x"
1737 _nc_SPRINTF(tfile[termcount],
1738 _nc_SLIMIT(sizeof(path))
1739 "%s/" LEAF_FMT "/%s",
1741 UChar(*argv[optind]), argv[optind]);
1743 (void) fprintf(stderr,
1744 "%s: reading entry %s from file %s\n",
1746 argv[optind], tfile[termcount]);
1748 status = _nc_read_file_entry(tfile[termcount],
1749 &entries[termcount].tterm);
1751 (void) fprintf(stderr, "%s: terminfo files not supported\n",
1754 ExitProgram(EXIT_FAILURE);
1758 (void) fprintf(stderr,
1759 "%s: reading entry %s from database\n",
1763 status = _nc_read_entry(tname[termcount],
1765 &entries[termcount].tterm);
1769 (void) fprintf(stderr,
1770 "%s: couldn't open terminfo file %s.\n",
1774 ExitProgram(EXIT_FAILURE);
1776 repair_acsc(&entries[termcount].tterm);
1782 _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
1785 /* dump as C initializer for the terminal type */
1788 dump_termtype(&entries[0].tterm);
1790 dump_initializers(&entries[0].tterm);
1793 /* analyze the init strings */
1794 else if (init_analyze) {
1796 #define CUR entries[0].tterm.
1797 analyze_string("is1", init_1string, &entries[0].tterm);
1798 analyze_string("is2", init_2string, &entries[0].tterm);
1799 analyze_string("is3", init_3string, &entries[0].tterm);
1800 analyze_string("rs1", reset_1string, &entries[0].tterm);
1801 analyze_string("rs2", reset_2string, &entries[0].tterm);
1802 analyze_string("rs3", reset_3string, &entries[0].tterm);
1803 analyze_string("smcup", enter_ca_mode, &entries[0].tterm);
1804 analyze_string("rmcup", exit_ca_mode, &entries[0].tterm);
1809 * Here's where the real work gets done
1814 (void) fprintf(stderr,
1815 "%s: about to dump %s\n",
1818 (void) printf("#\tReconstructed via infocmp from file: %s\n",
1820 dump_entry(&entries[0].tterm,
1821 suppress_untranslatable,
1827 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1831 show_comparing(tname);
1832 compare_entry(compare_predicate, &entries->tterm, quiet);
1836 show_comparing(tname);
1837 compare_entry(compare_predicate, &entries->tterm, quiet);
1841 show_comparing(tname);
1842 compare_entry(compare_predicate, &entries->tterm, quiet);
1847 (void) fprintf(stderr, "%s: dumping use entry\n", _nc_progname);
1848 dump_entry(&entries[0].tterm,
1849 suppress_untranslatable,
1853 for (i = 1; i < termcount; i++)
1854 dump_uses(tname[i], !(outform == F_TERMCAP
1855 || outform == F_TCONVERR));
1858 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1862 } else if (compare == C_USEALL) {
1863 (void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
1864 } else if (compare == C_DEFAULT) {
1865 (void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
1866 } else if (argc - optind != 2) {
1867 (void) fprintf(stderr,
1868 "File comparison needs exactly two file arguments.\n");
1870 file_comparison(argc - optind, argv + optind);
1874 ExitProgram(EXIT_SUCCESS);
1877 /* infocmp.c ends here */