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.122 2012/10/27 19:50:50 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.
407 #define check_user_definable(n,limit) if (!_nc_user_definable && (n) > (limit)) break
410 * Use these macros to simplify loops on C_COMMON and C_NAND:
412 #define for_each_entry() while (entries[extra].tterm.term_names)
413 #define next_entry (&(entries[extra++].tterm))
416 compare_predicate(PredType type, PredIdx idx, const char *name)
417 /* predicate function to use for entry difference reports */
419 ENTRY *e1 = &entries[0];
420 ENTRY *e2 = &entries[1];
421 char buf1[MAX_STRING];
422 char buf2[MAX_STRING];
431 check_user_definable(idx, BOOLWRITE);
432 b1 = e1->tterm.Booleans[idx];
435 b2 = next_entry->Booleans[idx];
436 if (!(b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN) && b1 != b2)
437 (void) printf("\t%s: %s%s%s.\n",
445 if (b1 != ABSENT_BOOLEAN) {
448 b2 = next_entry->Booleans[idx];
455 (void) printf("\t%s= %s.\n", name, dump_boolean(b1));
461 if (b1 == ABSENT_BOOLEAN) {
464 b2 = next_entry->Booleans[idx];
471 (void) printf("\t!%s.\n", name);
479 check_user_definable(idx, NUMWRITE);
480 n1 = e1->tterm.Numbers[idx];
483 n2 = next_entry->Numbers[idx];
484 if (!((n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)) && n1 != n2) {
485 dump_numeric(n1, buf1);
486 dump_numeric(n2, buf2);
487 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
492 if (n1 != ABSENT_NUMERIC) {
495 n2 = next_entry->Numbers[idx];
502 dump_numeric(n1, buf1);
503 (void) printf("\t%s= %s.\n", name, buf1);
509 if (n1 == ABSENT_NUMERIC) {
512 n2 = next_entry->Numbers[idx];
519 (void) printf("\t!%s.\n", name);
527 check_user_definable(idx, STRWRITE);
528 s1 = e1->tterm.Strings[idx];
531 s2 = next_entry->Strings[idx];
532 if (capcmp(idx, s1, s2)) {
533 dump_string(s1, buf1);
534 dump_string(s2, buf2);
535 if (strcmp(buf1, buf2))
536 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
541 if (s1 != ABSENT_STRING) {
544 s2 = next_entry->Strings[idx];
545 if (capcmp(idx, s1, s2) != 0) {
551 (void) printf("\t%s= '%s'.\n", name, TIC_EXPAND(s1));
557 if (s1 == ABSENT_STRING) {
560 s2 = next_entry->Strings[idx];
567 (void) printf("\t!%s.\n", name);
575 /* unlike the other modes, this compares *all* use entries */
578 if (!useeq(e1, e2)) {
579 (void) fputs("\tuse: ", stdout);
580 print_uses(e1, stdout);
582 print_uses(e2, stdout);
583 fputs(".\n", stdout);
591 e2 = &entries[extra++];
592 if (e2->nuses != e1->nuses || !useeq(e1, e2)) {
598 (void) fputs("\tuse: ", stdout);
599 print_uses(e1, stdout);
600 fputs(".\n", stdout);
609 e2 = &entries[extra++];
610 if (e2->nuses != e1->nuses) {
616 (void) printf("\t!use.\n");
624 /***************************************************************************
626 * Init string analysis
628 ***************************************************************************/
635 static const assoc std_caps[] =
637 /* these are specified by X.364 and iBCS2 */
638 {"\033c", "RIS"}, /* full reset */
639 {"\0337", "SC"}, /* save cursor */
640 {"\0338", "RC"}, /* restore cursor */
641 {"\033[r", "RSR"}, /* not an X.364 mnemonic */
642 {"\033[m", "SGR0"}, /* not an X.364 mnemonic */
643 {"\033[2J", "ED2"}, /* clear page */
645 /* this group is specified by ISO 2022 */
646 {"\033(0", "ISO DEC G0"}, /* enable DEC graphics for G0 */
647 {"\033(A", "ISO UK G0"}, /* enable UK chars for G0 */
648 {"\033(B", "ISO US G0"}, /* enable US chars for G0 */
649 {"\033)0", "ISO DEC G1"}, /* enable DEC graphics for G1 */
650 {"\033)A", "ISO UK G1"}, /* enable UK chars for G1 */
651 {"\033)B", "ISO US G1"}, /* enable US chars for G1 */
653 /* these are DEC private controls widely supported by emulators */
654 {"\033=", "DECPAM"}, /* application keypad mode */
655 {"\033>", "DECPNM"}, /* normal keypad mode */
656 {"\033<", "DECANSI"}, /* enter ANSI mode */
657 {"\033[!p", "DECSTR"}, /* soft reset */
658 {"\033 F", "S7C1T"}, /* 7-bit controls */
660 {(char *) 0, (char *) 0}
663 static const assoc std_modes[] =
664 /* ECMA \E[ ... [hl] modes recognized by many emulators */
666 {"2", "AM"}, /* keyboard action mode */
667 {"4", "IRM"}, /* insert/replace mode */
668 {"12", "SRM"}, /* send/receive mode */
669 {"20", "LNM"}, /* linefeed mode */
670 {(char *) 0, (char *) 0}
673 static const assoc private_modes[] =
674 /* DEC \E[ ... [hl] modes recognized by many emulators */
676 {"1", "CKM"}, /* application cursor keys */
677 {"2", "ANM"}, /* set VT52 mode */
678 {"3", "COLM"}, /* 132-column mode */
679 {"4", "SCLM"}, /* smooth scroll */
680 {"5", "SCNM"}, /* reverse video mode */
681 {"6", "OM"}, /* origin mode */
682 {"7", "AWM"}, /* wraparound mode */
683 {"8", "ARM"}, /* auto-repeat mode */
684 {(char *) 0, (char *) 0}
687 static const assoc ecma_highlights[] =
688 /* recognize ECMA attribute sequences */
690 {"0", "NORMAL"}, /* normal */
691 {"1", "+BOLD"}, /* bold on */
692 {"2", "+DIM"}, /* dim on */
693 {"3", "+ITALIC"}, /* italic on */
694 {"4", "+UNDERLINE"}, /* underline on */
695 {"5", "+BLINK"}, /* blink on */
696 {"6", "+FASTBLINK"}, /* fastblink on */
697 {"7", "+REVERSE"}, /* reverse on */
698 {"8", "+INVISIBLE"}, /* invisible on */
699 {"9", "+DELETED"}, /* deleted on */
700 {"10", "MAIN-FONT"}, /* select primary font */
701 {"11", "ALT-FONT-1"}, /* select alternate font 1 */
702 {"12", "ALT-FONT-2"}, /* select alternate font 2 */
703 {"13", "ALT-FONT-3"}, /* select alternate font 3 */
704 {"14", "ALT-FONT-4"}, /* select alternate font 4 */
705 {"15", "ALT-FONT-5"}, /* select alternate font 5 */
706 {"16", "ALT-FONT-6"}, /* select alternate font 6 */
707 {"17", "ALT-FONT-7"}, /* select alternate font 7 */
708 {"18", "ALT-FONT-1"}, /* select alternate font 1 */
709 {"19", "ALT-FONT-1"}, /* select alternate font 1 */
710 {"20", "FRAKTUR"}, /* Fraktur font */
711 {"21", "DOUBLEUNDER"}, /* double underline */
712 {"22", "-DIM"}, /* dim off */
713 {"23", "-ITALIC"}, /* italic off */
714 {"24", "-UNDERLINE"}, /* underline off */
715 {"25", "-BLINK"}, /* blink off */
716 {"26", "-FASTBLINK"}, /* fastblink off */
717 {"27", "-REVERSE"}, /* reverse off */
718 {"28", "-INVISIBLE"}, /* invisible off */
719 {"29", "-DELETED"}, /* deleted off */
720 {(char *) 0, (char *) 0}
724 skip_csi(const char *cap)
727 if (cap[0] == '\033' && cap[1] == '[')
729 else if (UChar(cap[0]) == 0233)
735 same_param(const char *table, const char *param, size_t length)
738 if (strncmp(table, param, length) == 0) {
739 result = !isdigit(UChar(param[length]));
745 lookup_params(const assoc * table, char *dst, char *src)
748 const char *ep = strtok(src, ";");
756 for (ap = table; ap->from; ap++) {
757 size_t tlen = strlen(ap->from);
759 if (same_param(ap->from, ep, tlen)) {
760 _nc_STRCAT(dst, ap->to, MAX_TERMINFO_LENGTH);
767 _nc_STRCAT(dst, ep, MAX_TERMINFO_LENGTH);
768 _nc_STRCAT(dst, ";", MAX_TERMINFO_LENGTH);
770 ((ep = strtok((char *) 0, ";")));
772 dst[strlen(dst) - 1] = '\0';
780 analyze_string(const char *name, const char *cap, TERMTYPE *tp)
782 char buf2[MAX_TERMINFO_LENGTH];
785 int tp_lines = tp->Numbers[2];
787 if (cap == ABSENT_STRING || cap == CANCELLED_STRING)
789 (void) printf("%s: ", name);
791 for (sp = cap; *sp; sp++) {
796 const char *expansion = 0;
797 char buf3[MAX_TERMINFO_LENGTH];
799 /* first, check other capabilities in this entry */
800 for (i = 0; i < STRCOUNT; i++) {
801 char *cp = tp->Strings[i];
803 /* don't use soft-key capabilities */
804 if (strnames[i][0] == 'k' && strnames[i][0] == 'f')
807 if (cp != ABSENT_STRING && cp != CANCELLED_STRING && cp[0] && cp
810 (void) strncpy(buf2, sp, len);
813 if (_nc_capcmp(cp, buf2))
816 #define ISRS(s) (!strncmp((s), "is", 2) || !strncmp((s), "rs", 2))
818 * Theoretically we just passed the test for translation
819 * (equality once the padding is stripped). However, there
820 * are a few more hoops that need to be jumped so that
821 * identical pairs of initialization and reset strings
822 * don't just refer to each other.
824 if (ISRS(name) || ISRS(strnames[i]))
829 expansion = strnames[i];
834 /* now check the standard capabilities */
837 for (ap = std_caps; ap->from; ap++) {
838 size_t adj = (size_t) (csi ? 2 : 0);
840 len = strlen(ap->from);
841 if (csi && skip_csi(ap->from) != csi)
844 && strncmp(ap->from + adj, sp + csi, len - adj) == 0) {
853 /* now check for standard-mode sequences */
855 && (csi = skip_csi(sp)) != 0
856 && (len = strspn(sp + csi, "0123456789;"))
857 && (len < sizeof(buf3))
858 && (next = (size_t) csi + len)
859 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
866 (void) strncpy(buf3, sp + csi, len);
868 len += (size_t) csi + 1;
870 expansion = lookup_params(std_modes, buf2, buf3);
873 /* now check for private-mode sequences */
875 && (csi = skip_csi(sp)) != 0
877 && (len = strspn(sp + csi + 1, "0123456789;"))
878 && (len < sizeof(buf3))
879 && (next = (size_t) csi + 1 + len)
880 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
887 (void) strncpy(buf3, sp + csi + 1, len);
889 len += (size_t) csi + 2;
891 expansion = lookup_params(private_modes, buf2, buf3);
894 /* now check for ECMA highlight sequences */
896 && (csi = skip_csi(sp)) != 0
897 && (len = strspn(sp + csi, "0123456789;")) != 0
898 && (len < sizeof(buf3))
899 && (next = (size_t) csi + len)
900 && sp[next] == 'm') {
902 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
903 (void) strncpy(buf3, sp + csi, len);
905 len += (size_t) csi + 1;
907 expansion = lookup_params(ecma_highlights, buf2, buf3);
911 && (csi = skip_csi(sp)) != 0
913 len = (size_t) csi + 1;
914 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
915 _nc_STRCAT(buf2, ecma_highlights[0].to, sizeof(buf2));
919 /* now check for scroll region reset */
921 && (csi = skip_csi(sp)) != 0) {
922 if (sp[csi] == 'r') {
926 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "1;%dr", tp_lines);
928 if (strncmp(buf2, sp + csi, len) == 0)
934 /* now check for home-down */
936 && (csi = skip_csi(sp)) != 0) {
937 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%d;1H", tp_lines);
939 if (strncmp(buf2, sp + csi, len) == 0) {
942 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%dH", tp_lines);
944 if (strncmp(buf2, sp + csi, len) == 0) {
951 /* now look at the expansion we got, if any */
953 printf("{%s}", expansion);
956 /* couldn't match anything */
959 fputs(TIC_EXPAND(buf2), stdout);
965 /***************************************************************************
969 ***************************************************************************/
972 file_comparison(int argc, char *argv[])
975 /* someday we may allow comparisons on more files */
977 ENTRY *heads[MAXCOMPARE];
981 memset(heads, 0, sizeof(heads));
982 dump_init((char *) 0, F_LITERAL, S_TERMINFO, 0, 65535, itrace, FALSE);
984 for (n = 0; n < argc && n < MAXCOMPARE; n++) {
985 if (freopen(argv[n], "r", stdin) == 0)
986 _nc_err_abort("Can't open %s", argv[n]);
989 entered[n].head = _nc_head;
990 entered[n].tail = _nc_tail;
992 _nc_head = _nc_tail = 0;
994 /* parse entries out of the source file */
995 _nc_set_source(argv[n]);
996 _nc_read_entry_source(stdin, NULL, TRUE, literal, NULLHOOK);
999 (void) fprintf(stderr, "Resolving file %d...\n", n - 0);
1001 /* maybe do use resolution */
1002 if (!_nc_resolve_uses2(!limited, literal)) {
1003 (void) fprintf(stderr,
1004 "There are unresolved use entries in %s:\n",
1006 for_entry_list(qp) {
1008 (void) fputs(qp->tterm.term_names, stderr);
1009 (void) fputc('\n', stderr);
1012 ExitProgram(EXIT_FAILURE);
1015 heads[filecount] = _nc_head;
1019 /* OK, all entries are in core. Ready to do the comparison */
1021 (void) fprintf(stderr, "Entries are now in core...\n");
1023 /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
1024 for (qp = heads[0]; qp; qp = qp->next) {
1025 for (rp = heads[1]; rp; rp = rp->next)
1026 if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
1027 if (qp->ncrosslinks < MAX_CROSSLINKS)
1028 qp->crosslinks[qp->ncrosslinks] = rp;
1031 if (rp->ncrosslinks < MAX_CROSSLINKS)
1032 rp->crosslinks[rp->ncrosslinks] = qp;
1037 /* now we have two circular lists with crosslinks */
1039 (void) fprintf(stderr, "Name matches are done...\n");
1041 for (qp = heads[0]; qp; qp = qp->next) {
1042 if (qp->ncrosslinks > 1) {
1043 (void) fprintf(stderr,
1044 "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
1045 _nc_first_name(qp->tterm.term_names),
1049 for (i = 0; i < qp->ncrosslinks; i++)
1050 (void) fprintf(stderr,
1052 _nc_first_name((qp->crosslinks[i])->tterm.term_names));
1056 for (rp = heads[1]; rp; rp = rp->next) {
1057 if (rp->ncrosslinks > 1) {
1058 (void) fprintf(stderr,
1059 "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
1060 _nc_first_name(rp->tterm.term_names),
1064 for (i = 0; i < rp->ncrosslinks; i++)
1065 (void) fprintf(stderr,
1067 _nc_first_name((rp->crosslinks[i])->tterm.term_names));
1071 (void) printf("In file 1 (%s) only:\n", argv[0]);
1072 for (qp = heads[0]; qp; qp = qp->next)
1073 if (qp->ncrosslinks == 0)
1074 (void) printf("\t%s\n",
1075 _nc_first_name(qp->tterm.term_names));
1077 (void) printf("In file 2 (%s) only:\n", argv[1]);
1078 for (rp = heads[1]; rp; rp = rp->next)
1079 if (rp->ncrosslinks == 0)
1080 (void) printf("\t%s\n",
1081 _nc_first_name(rp->tterm.term_names));
1083 (void) printf("The following entries are equivalent:\n");
1084 for (qp = heads[0]; qp; qp = qp->next) {
1085 if (qp->ncrosslinks == 1) {
1086 rp = qp->crosslinks[0];
1088 repair_acsc(&qp->tterm);
1089 repair_acsc(&rp->tterm);
1091 _nc_align_termtype(&qp->tterm, &rp->tterm);
1093 if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
1094 char name1[NAMESIZE], name2[NAMESIZE];
1096 (void) canonical_name(qp->tterm.term_names, name1);
1097 (void) canonical_name(rp->tterm.term_names, name2);
1099 (void) printf("%s = %s\n", name1, name2);
1104 (void) printf("Differing entries:\n");
1106 for (qp = heads[0]; qp; qp = qp->next) {
1108 if (qp->ncrosslinks == 1) {
1109 rp = qp->crosslinks[0];
1111 /* sorry - we have to do this on each pass */
1112 _nc_align_termtype(&qp->tterm, &rp->tterm);
1114 if (!(entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp))) {
1115 char name1[NAMESIZE], name2[NAMESIZE];
1125 (void) canonical_name(qp->tterm.term_names, name1);
1126 (void) canonical_name(rp->tterm.term_names, name2);
1130 show_comparing(names);
1131 compare_entry(compare_predicate, &entries->tterm, quiet);
1135 show_comparing(names);
1136 compare_entry(compare_predicate, &entries->tterm, quiet);
1140 show_comparing(names);
1141 compare_entry(compare_predicate, &entries->tterm, quiet);
1153 static const char *tbl[] =
1155 "Usage: infocmp [options] [-A directory] [-B directory] [termname...]"
1158 ," -0 print single-row"
1159 ," -1 print single-column"
1160 ," -K use termcap-names and BSD syntax"
1161 ," -C use termcap-names"
1162 ," -F compare terminfo-files"
1163 ," -I use terminfo-names"
1164 ," -L use long names"
1165 ," -R subset (see manpage)"
1166 ," -T eliminate size limits (test)"
1167 ," -U eliminate post-processing of entries"
1168 ," -V print version"
1170 ," -a with -F, list commented-out caps"
1172 ," -c list common capabilities"
1173 ," -d list different capabilities"
1174 ," -e format output for C initializer"
1175 ," -E format output as C tables"
1176 ," -f with -1, format complex strings"
1177 ," -G format %{number} to %'char'"
1178 ," -g format %'char' to %{number}"
1179 ," -i analyze initialization/reset"
1180 ," -l output terminfo names"
1181 ," -n list capabilities in neither"
1182 ," -p ignore padding specifiers"
1183 ," -q brief listing, removes headers"
1184 ," -r with -C, output in termcap form"
1185 ," -r with -F, resolve use-references"
1186 ," -s [d|i|l|c] sort fields"
1188 ," -t suppress commented-out capabilities"
1190 ," -u produce source with 'use='"
1191 ," -v number (verbose)"
1192 ," -w number (width)"
1194 ," -x treat unknown capabilities as user-defined"
1197 const size_t first = 3;
1198 const size_t last = SIZEOF(tbl);
1199 const size_t left = (last - first + 1) / 2 + first;
1202 for (n = 0; n < left; n++) {
1203 size_t m = (n < first) ? last : n + left - first;
1205 fprintf(stderr, "%-40.40s%s\n", tbl[n], tbl[m]);
1207 fprintf(stderr, "%s\n", tbl[n]);
1209 ExitProgram(EXIT_FAILURE);
1213 any_initializer(const char *fmt, const char *type)
1215 static char *initializer;
1219 if (initializer == 0) {
1220 need = (strlen(entries->tterm.term_names)
1223 initializer = (char *) malloc(need);
1224 if (initializer == 0)
1225 failed("any_initializer");
1228 _nc_STRCPY(initializer, entries->tterm.term_names, need);
1229 for (s = initializer; *s != 0 && *s != '|'; s++) {
1230 if (!isalnum(UChar(*s)))
1234 _nc_SPRINTF(s, _nc_SLIMIT(need) fmt, type);
1239 name_initializer(const char *type)
1241 return any_initializer("_%s_data", type);
1245 string_variable(const char *type)
1247 return any_initializer("_s_%s", type);
1250 /* dump C initializers for the terminal type */
1252 dump_initializers(TERMTYPE *term)
1255 const char *str = 0;
1257 printf("\nstatic char %s[] = \"%s\";\n\n",
1258 name_initializer("alias"), entries->tterm.term_names);
1260 for_each_string(n, term) {
1261 char buf[MAX_STRING], *sp, *tp;
1263 if (VALID_STRING(term->Strings[n])) {
1265 #define TP_LIMIT ((MAX_STRING - 5) - (size_t)(tp - buf))
1267 for (sp = term->Strings[n];
1268 *sp != 0 && TP_LIMIT > 2;
1270 if (isascii(UChar(*sp))
1271 && isprint(UChar(*sp))
1276 _nc_SPRINTF(tp, _nc_SLIMIT(TP_LIMIT) "\\%03o", UChar(*sp));
1282 (void) printf("static char %-20s[] = %s;\n",
1283 string_variable(ExtStrname(term, (int) n, strnames)),
1289 (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
1291 for_each_boolean(n, term) {
1292 switch ((int) (term->Booleans[n])) {
1301 case ABSENT_BOOLEAN:
1302 str = "ABSENT_BOOLEAN";
1305 case CANCELLED_BOOLEAN:
1306 str = "CANCELLED_BOOLEAN";
1309 (void) printf("\t/* %3u: %-8s */\t%s,\n",
1310 n, ExtBoolname(term, (int) n, boolnames), str);
1312 (void) printf("%s;\n", R_CURL);
1314 (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
1316 for_each_number(n, term) {
1318 switch (term->Numbers[n]) {
1319 case ABSENT_NUMERIC:
1320 str = "ABSENT_NUMERIC";
1322 case CANCELLED_NUMERIC:
1323 str = "CANCELLED_NUMERIC";
1326 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) "%d", term->Numbers[n]);
1330 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1331 ExtNumname(term, (int) n, numnames), str);
1333 (void) printf("%s;\n", R_CURL);
1335 (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
1337 for_each_string(n, term) {
1339 if (term->Strings[n] == ABSENT_STRING)
1340 str = "ABSENT_STRING";
1341 else if (term->Strings[n] == CANCELLED_STRING)
1342 str = "CANCELLED_STRING";
1344 str = string_variable(ExtStrname(term, (int) n, strnames));
1346 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1347 ExtStrname(term, (int) n, strnames), str);
1349 (void) printf("%s;\n", R_CURL);
1352 if ((NUM_BOOLEANS(term) != BOOLCOUNT)
1353 || (NUM_NUMBERS(term) != NUMCOUNT)
1354 || (NUM_STRINGS(term) != STRCOUNT)) {
1355 (void) printf("static char * %s[] = %s\n",
1356 name_initializer("string_ext"), L_CURL);
1357 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
1358 (void) printf("\t/* %3u: bool */\t\"%s\",\n",
1359 n, ExtBoolname(term, (int) n, boolnames));
1361 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
1362 (void) printf("\t/* %3u: num */\t\"%s\",\n",
1363 n, ExtNumname(term, (int) n, numnames));
1365 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
1366 (void) printf("\t/* %3u: str */\t\"%s\",\n",
1367 n, ExtStrname(term, (int) n, strnames));
1369 (void) printf("%s;\n", R_CURL);
1374 /* dump C initializers for the terminal type */
1376 dump_termtype(TERMTYPE *term)
1378 (void) printf("\t%s\n\t\t%s,\n", L_CURL, name_initializer("alias"));
1379 (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
1381 (void) printf("\t\t%s,\n", name_initializer("bool"));
1382 (void) printf("\t\t%s,\n", name_initializer("number"));
1384 (void) printf("\t\t%s,\n", name_initializer("string"));
1387 (void) printf("#if NCURSES_XNAMES\n");
1388 (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
1389 (void) printf("\t\t%s,\t/* ...corresponding names */\n",
1390 ((NUM_BOOLEANS(term) != BOOLCOUNT)
1391 || (NUM_NUMBERS(term) != NUMCOUNT)
1392 || (NUM_STRINGS(term) != STRCOUNT))
1393 ? name_initializer("string_ext")
1396 (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
1397 (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
1398 (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
1400 (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
1401 NUM_BOOLEANS(term) - BOOLCOUNT);
1402 (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
1403 NUM_NUMBERS(term) - NUMCOUNT);
1404 (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
1405 NUM_STRINGS(term) - STRCOUNT);
1407 (void) printf("#endif /* NCURSES_XNAMES */\n");
1410 #endif /* NCURSES_XNAMES */
1411 (void) printf("\t%s\n", R_CURL);
1415 optarg_to_number(void)
1418 long value = strtol(optarg, &temp, 0);
1420 if (temp == 0 || temp == optarg || *temp != 0) {
1421 fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
1422 ExitProgram(EXIT_FAILURE);
1432 if ((terminal = getenv("TERM")) == 0) {
1433 (void) fprintf(stderr,
1434 "%s: environment variable TERM not set\n",
1442 * Show the databases that infocmp knows about. The location to which it writes is
1445 show_databases(void)
1451 _nc_first_db(&state, &offset);
1452 while ((path2 = _nc_next_db(&state, &offset)) != 0) {
1453 printf("%s\n", path2);
1458 /***************************************************************************
1462 ***************************************************************************/
1465 #define MAIN_LEAKS() \
1470 #define MAIN_LEAKS() /* nothing */
1474 main(int argc, char *argv[])
1476 /* Avoid "local data >32k" error with mwcc */
1477 /* Also avoid overflowing smaller stacks on systems like AmigaOS */
1484 char *firstdir, *restdir;
1486 bool formatted = FALSE;
1487 bool filecompare = FALSE;
1489 bool init_analyze = FALSE;
1490 bool suppress_untranslatable = FALSE;
1492 /* where is the terminfo database location going to default to? */
1493 restdir = firstdir = 0;
1496 use_extended_names(FALSE);
1500 _nc_progname = _nc_rootname(argv[0]);
1502 /* make sure we have enough space to add two terminal entries */
1503 myargv = typeCalloc(char *, (size_t) (argc + 3));
1507 memcpy(myargv, argv, (sizeof(char *) * (size_t) argc));
1510 while ((c = getopt(argc,
1512 "01A:aB:CcDdEeFfGgIiKLlnpqR:rs:TtUuVv:w:x")) != -1) {
1529 _nc_disable_period = TRUE;
1530 use_extended_names(TRUE);
1541 outform = F_TERMCAP;
1543 if (sortmode == S_DEFAULT)
1544 sortmode = S_TERMCAP;
1549 ExitProgram(EXIT_SUCCESS);
1557 compare = C_DIFFERENCE;
1585 outform = F_TERMINFO;
1586 if (sortmode == S_DEFAULT)
1587 sortmode = S_VARIABLE;
1592 init_analyze = TRUE;
1596 outform = F_VARIABLE;
1597 if (sortmode == S_DEFAULT)
1598 sortmode = S_VARIABLE;
1602 outform = F_TERMINFO;
1630 sortmode = S_NOSORT;
1631 else if (*optarg == 'i')
1632 sortmode = S_TERMINFO;
1633 else if (*optarg == 'l')
1634 sortmode = S_VARIABLE;
1635 else if (*optarg == 'c')
1636 sortmode = S_TERMCAP;
1638 (void) fprintf(stderr,
1639 "%s: unknown sort mode\n",
1641 ExitProgram(EXIT_FAILURE);
1651 _nc_disable_period = FALSE;
1652 suppress_untranslatable = TRUE;
1665 puts(curses_version());
1666 ExitProgram(EXIT_SUCCESS);
1669 itrace = (unsigned) optarg_to_number();
1670 set_trace_level(itrace);
1674 mwidth = optarg_to_number();
1679 use_extended_names(TRUE);
1688 maxterms = (size_t) (argc + 2 - optind);
1689 if ((tfile = typeMalloc(path, maxterms)) == 0)
1691 if ((tname = typeCalloc(char *, maxterms)) == 0)
1693 if ((entries = typeCalloc(ENTRY, maxterms)) == 0)
1696 if ((entered = typeCalloc(ENTERED, maxterms)) == 0)
1703 fprintf(stderr, "%s: not enough memory\n", _nc_progname);
1704 ExitProgram(EXIT_FAILURE);
1707 /* by default, sort by terminfo name */
1708 if (sortmode == S_DEFAULT)
1709 sortmode = S_TERMINFO;
1711 /* make sure we have at least one terminal name to work with */
1713 argv[argc++] = terminal_env();
1715 /* if user is after a comparison, make sure we have two entries */
1716 if (compare != C_DEFAULT && optind >= argc - 1)
1717 argv[argc++] = terminal_env();
1719 /* exactly one terminal name with no options means display it */
1720 /* exactly two terminal names with no options means do -d */
1721 if (compare == C_DEFAULT) {
1722 switch (argc - optind) {
1724 fprintf(stderr, "%s: too many names to compare\n", _nc_progname);
1725 ExitProgram(EXIT_FAILURE);
1729 compare = C_DIFFERENCE;
1734 /* set up for display */
1735 dump_init(tversion, outform, sortmode, mwidth, mheight, itrace, formatted);
1738 /* grab the entries */
1740 for (; optind < argc; optind++) {
1741 const char *directory = termcount ? restdir : firstdir;
1744 tname[termcount] = argv[optind];
1748 #if MIXEDCASE_FILENAMES
1749 #define LEAF_FMT "%c"
1751 #define LEAF_FMT "%02x"
1753 _nc_SPRINTF(tfile[termcount],
1754 _nc_SLIMIT(sizeof(path))
1755 "%s/" LEAF_FMT "/%s",
1757 UChar(*argv[optind]), argv[optind]);
1759 (void) fprintf(stderr,
1760 "%s: reading entry %s from file %s\n",
1762 argv[optind], tfile[termcount]);
1764 status = _nc_read_file_entry(tfile[termcount],
1765 &entries[termcount].tterm);
1767 (void) fprintf(stderr, "%s: terminfo files not supported\n",
1770 ExitProgram(EXIT_FAILURE);
1774 (void) fprintf(stderr,
1775 "%s: reading entry %s from database\n",
1779 status = _nc_read_entry(tname[termcount],
1781 &entries[termcount].tterm);
1785 (void) fprintf(stderr,
1786 "%s: couldn't open terminfo file %s.\n",
1790 ExitProgram(EXIT_FAILURE);
1792 repair_acsc(&entries[termcount].tterm);
1798 _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
1801 /* dump as C initializer for the terminal type */
1804 dump_termtype(&entries[0].tterm);
1806 dump_initializers(&entries[0].tterm);
1809 /* analyze the init strings */
1810 else if (init_analyze) {
1812 #define CUR entries[0].tterm.
1813 analyze_string("is1", init_1string, &entries[0].tterm);
1814 analyze_string("is2", init_2string, &entries[0].tterm);
1815 analyze_string("is3", init_3string, &entries[0].tterm);
1816 analyze_string("rs1", reset_1string, &entries[0].tterm);
1817 analyze_string("rs2", reset_2string, &entries[0].tterm);
1818 analyze_string("rs3", reset_3string, &entries[0].tterm);
1819 analyze_string("smcup", enter_ca_mode, &entries[0].tterm);
1820 analyze_string("rmcup", exit_ca_mode, &entries[0].tterm);
1825 * Here's where the real work gets done
1830 (void) fprintf(stderr,
1831 "%s: about to dump %s\n",
1834 (void) printf("#\tReconstructed via infocmp from file: %s\n",
1836 dump_entry(&entries[0].tterm,
1837 suppress_untranslatable,
1843 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1847 show_comparing(tname);
1848 compare_entry(compare_predicate, &entries->tterm, quiet);
1852 show_comparing(tname);
1853 compare_entry(compare_predicate, &entries->tterm, quiet);
1857 show_comparing(tname);
1858 compare_entry(compare_predicate, &entries->tterm, quiet);
1863 (void) fprintf(stderr, "%s: dumping use entry\n", _nc_progname);
1864 dump_entry(&entries[0].tterm,
1865 suppress_untranslatable,
1869 for (i = 1; i < termcount; i++)
1870 dump_uses(tname[i], !(outform == F_TERMCAP
1871 || outform == F_TCONVERR));
1874 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1878 } else if (compare == C_USEALL) {
1879 (void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
1880 } else if (compare == C_DEFAULT) {
1881 (void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
1882 } else if (argc - optind != 2) {
1883 (void) fprintf(stderr,
1884 "File comparison needs exactly two file arguments.\n");
1886 file_comparison(argc - optind, argv + optind);
1890 ExitProgram(EXIT_SUCCESS);
1893 /* infocmp.c ends here */