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.112 2012/02/22 23:59:53 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 compare_predicate(PredType type, PredIdx idx, const char *name)
338 /* predicate function to use for entry difference reports */
340 register ENTRY *e1 = &entries[0];
341 register ENTRY *e2 = &entries[1];
342 char buf1[MAX_STRING], buf2[MAX_STRING];
349 b1 = e1->tterm.Booleans[idx];
350 b2 = e2->tterm.Booleans[idx];
353 if (!(b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN) && b1 != b2)
354 (void) printf("\t%s: %s%s%s.\n",
362 if (b1 == b2 && b1 != ABSENT_BOOLEAN)
363 (void) printf("\t%s= %s.\n", name, dump_boolean(b1));
367 if (b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN)
368 (void) printf("\t!%s.\n", name);
374 n1 = e1->tterm.Numbers[idx];
375 n2 = e2->tterm.Numbers[idx];
376 dump_numeric(n1, buf1);
377 dump_numeric(n2, buf2);
380 if (!((n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)) && n1 != n2)
381 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
385 if (n1 != ABSENT_NUMERIC && n2 != ABSENT_NUMERIC && n1 == n2)
386 (void) printf("\t%s= %s.\n", name, buf1);
390 if (n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)
391 (void) printf("\t!%s.\n", name);
397 s1 = e1->tterm.Strings[idx];
398 s2 = e2->tterm.Strings[idx];
401 if (capcmp(idx, s1, s2)) {
402 dump_string(s1, buf1);
403 dump_string(s2, buf2);
404 if (strcmp(buf1, buf2))
405 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
410 if (s1 && s2 && !capcmp(idx, s1, s2))
411 (void) printf("\t%s= '%s'.\n", name, TIC_EXPAND(s1));
416 (void) printf("\t!%s.\n", name);
422 /* unlike the other modes, this compares *all* use entries */
425 if (!useeq(e1, e2)) {
426 (void) fputs("\tuse: ", stdout);
427 print_uses(e1, stdout);
429 print_uses(e2, stdout);
430 fputs(".\n", stdout);
435 if (e1->nuses && e2->nuses && useeq(e1, e2)) {
436 (void) fputs("\tuse: ", stdout);
437 print_uses(e1, stdout);
438 fputs(".\n", stdout);
443 if (!e1->nuses && !e2->nuses)
444 (void) printf("\t!use.\n");
450 /***************************************************************************
452 * Init string analysis
454 ***************************************************************************/
461 static const assoc std_caps[] =
463 /* these are specified by X.364 and iBCS2 */
464 {"\033c", "RIS"}, /* full reset */
465 {"\0337", "SC"}, /* save cursor */
466 {"\0338", "RC"}, /* restore cursor */
467 {"\033[r", "RSR"}, /* not an X.364 mnemonic */
468 {"\033[m", "SGR0"}, /* not an X.364 mnemonic */
469 {"\033[2J", "ED2"}, /* clear page */
471 /* this group is specified by ISO 2022 */
472 {"\033(0", "ISO DEC G0"}, /* enable DEC graphics for G0 */
473 {"\033(A", "ISO UK G0"}, /* enable UK chars for G0 */
474 {"\033(B", "ISO US G0"}, /* enable US chars for G0 */
475 {"\033)0", "ISO DEC G1"}, /* enable DEC graphics for G1 */
476 {"\033)A", "ISO UK G1"}, /* enable UK chars for G1 */
477 {"\033)B", "ISO US G1"}, /* enable US chars for G1 */
479 /* these are DEC private controls widely supported by emulators */
480 {"\033=", "DECPAM"}, /* application keypad mode */
481 {"\033>", "DECPNM"}, /* normal keypad mode */
482 {"\033<", "DECANSI"}, /* enter ANSI mode */
483 {"\033[!p", "DECSTR"}, /* soft reset */
484 {"\033 F", "S7C1T"}, /* 7-bit controls */
486 {(char *) 0, (char *) 0}
489 static const assoc std_modes[] =
490 /* ECMA \E[ ... [hl] modes recognized by many emulators */
492 {"2", "AM"}, /* keyboard action mode */
493 {"4", "IRM"}, /* insert/replace mode */
494 {"12", "SRM"}, /* send/receive mode */
495 {"20", "LNM"}, /* linefeed mode */
496 {(char *) 0, (char *) 0}
499 static const assoc private_modes[] =
500 /* DEC \E[ ... [hl] modes recognized by many emulators */
502 {"1", "CKM"}, /* application cursor keys */
503 {"2", "ANM"}, /* set VT52 mode */
504 {"3", "COLM"}, /* 132-column mode */
505 {"4", "SCLM"}, /* smooth scroll */
506 {"5", "SCNM"}, /* reverse video mode */
507 {"6", "OM"}, /* origin mode */
508 {"7", "AWM"}, /* wraparound mode */
509 {"8", "ARM"}, /* auto-repeat mode */
510 {(char *) 0, (char *) 0}
513 static const assoc ecma_highlights[] =
514 /* recognize ECMA attribute sequences */
516 {"0", "NORMAL"}, /* normal */
517 {"1", "+BOLD"}, /* bold on */
518 {"2", "+DIM"}, /* dim on */
519 {"3", "+ITALIC"}, /* italic on */
520 {"4", "+UNDERLINE"}, /* underline on */
521 {"5", "+BLINK"}, /* blink on */
522 {"6", "+FASTBLINK"}, /* fastblink on */
523 {"7", "+REVERSE"}, /* reverse on */
524 {"8", "+INVISIBLE"}, /* invisible on */
525 {"9", "+DELETED"}, /* deleted on */
526 {"10", "MAIN-FONT"}, /* select primary font */
527 {"11", "ALT-FONT-1"}, /* select alternate font 1 */
528 {"12", "ALT-FONT-2"}, /* select alternate font 2 */
529 {"13", "ALT-FONT-3"}, /* select alternate font 3 */
530 {"14", "ALT-FONT-4"}, /* select alternate font 4 */
531 {"15", "ALT-FONT-5"}, /* select alternate font 5 */
532 {"16", "ALT-FONT-6"}, /* select alternate font 6 */
533 {"17", "ALT-FONT-7"}, /* select alternate font 7 */
534 {"18", "ALT-FONT-1"}, /* select alternate font 1 */
535 {"19", "ALT-FONT-1"}, /* select alternate font 1 */
536 {"20", "FRAKTUR"}, /* Fraktur font */
537 {"21", "DOUBLEUNDER"}, /* double underline */
538 {"22", "-DIM"}, /* dim off */
539 {"23", "-ITALIC"}, /* italic off */
540 {"24", "-UNDERLINE"}, /* underline off */
541 {"25", "-BLINK"}, /* blink off */
542 {"26", "-FASTBLINK"}, /* fastblink off */
543 {"27", "-REVERSE"}, /* reverse off */
544 {"28", "-INVISIBLE"}, /* invisible off */
545 {"29", "-DELETED"}, /* deleted off */
546 {(char *) 0, (char *) 0}
550 skip_csi(const char *cap)
553 if (cap[0] == '\033' && cap[1] == '[')
555 else if (UChar(cap[0]) == 0233)
561 same_param(const char *table, const char *param, size_t length)
564 if (strncmp(table, param, length) == 0) {
565 result = !isdigit(UChar(param[length]));
571 lookup_params(const assoc * table, char *dst, char *src)
574 const char *ep = strtok(src, ";");
582 for (ap = table; ap->from; ap++) {
583 size_t tlen = strlen(ap->from);
585 if (same_param(ap->from, ep, tlen)) {
586 _nc_STRCAT(dst, ap->to, MAX_TERMINFO_LENGTH);
593 _nc_STRCAT(dst, ep, MAX_TERMINFO_LENGTH);
594 _nc_STRCAT(dst, ";", MAX_TERMINFO_LENGTH);
596 ((ep = strtok((char *) 0, ";")));
598 dst[strlen(dst) - 1] = '\0';
606 analyze_string(const char *name, const char *cap, TERMTYPE *tp)
608 char buf2[MAX_TERMINFO_LENGTH];
611 int tp_lines = tp->Numbers[2];
613 if (cap == ABSENT_STRING || cap == CANCELLED_STRING)
615 (void) printf("%s: ", name);
617 for (sp = cap; *sp; sp++) {
622 const char *expansion = 0;
623 char buf3[MAX_TERMINFO_LENGTH];
625 /* first, check other capabilities in this entry */
626 for (i = 0; i < STRCOUNT; i++) {
627 char *cp = tp->Strings[i];
629 /* don't use soft-key capabilities */
630 if (strnames[i][0] == 'k' && strnames[i][0] == 'f')
633 if (cp != ABSENT_STRING && cp != CANCELLED_STRING && cp[0] && cp
636 (void) strncpy(buf2, sp, len);
639 if (_nc_capcmp(cp, buf2))
642 #define ISRS(s) (!strncmp((s), "is", 2) || !strncmp((s), "rs", 2))
644 * Theoretically we just passed the test for translation
645 * (equality once the padding is stripped). However, there
646 * are a few more hoops that need to be jumped so that
647 * identical pairs of initialization and reset strings
648 * don't just refer to each other.
650 if (ISRS(name) || ISRS(strnames[i]))
655 expansion = strnames[i];
660 /* now check the standard capabilities */
663 for (ap = std_caps; ap->from; ap++) {
664 size_t adj = (size_t) (csi ? 2 : 0);
666 len = strlen(ap->from);
667 if (csi && skip_csi(ap->from) != csi)
670 && strncmp(ap->from + adj, sp + csi, len - adj) == 0) {
679 /* now check for standard-mode sequences */
681 && (csi = skip_csi(sp)) != 0
682 && (len = strspn(sp + csi, "0123456789;"))
683 && (len < sizeof(buf3))
684 && (next = (size_t) csi + len)
685 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
692 (void) strncpy(buf3, sp + csi, len);
694 len += (size_t) csi + 1;
696 expansion = lookup_params(std_modes, buf2, buf3);
699 /* now check for private-mode sequences */
701 && (csi = skip_csi(sp)) != 0
703 && (len = strspn(sp + csi + 1, "0123456789;"))
704 && (len < sizeof(buf3))
705 && (next = (size_t) csi + 1 + len)
706 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
713 (void) strncpy(buf3, sp + csi + 1, len);
715 len += (size_t) csi + 2;
717 expansion = lookup_params(private_modes, buf2, buf3);
720 /* now check for ECMA highlight sequences */
722 && (csi = skip_csi(sp)) != 0
723 && (len = strspn(sp + csi, "0123456789;")) != 0
724 && (len < sizeof(buf3))
725 && (next = (size_t) csi + len)
726 && sp[next] == 'm') {
728 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
729 (void) strncpy(buf3, sp + csi, len);
731 len += (size_t) csi + 1;
733 expansion = lookup_params(ecma_highlights, buf2, buf3);
737 && (csi = skip_csi(sp)) != 0
739 len = (size_t) csi + 1;
740 _nc_STRCPY(buf2, "SGR:", sizeof(buf2));
741 _nc_STRCAT(buf2, ecma_highlights[0].to, sizeof(buf2));
745 /* now check for scroll region reset */
747 && (csi = skip_csi(sp)) != 0) {
748 if (sp[csi] == 'r') {
752 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "1;%dr", tp_lines);
754 if (strncmp(buf2, sp + csi, len) == 0)
760 /* now check for home-down */
762 && (csi = skip_csi(sp)) != 0) {
763 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%d;1H", tp_lines);
765 if (strncmp(buf2, sp + csi, len) == 0) {
768 _nc_SPRINTF(buf2, _nc_SLIMIT(sizeof(buf2)) "%dH", tp_lines);
770 if (strncmp(buf2, sp + csi, len) == 0) {
777 /* now look at the expansion we got, if any */
779 printf("{%s}", expansion);
782 /* couldn't match anything */
785 fputs(TIC_EXPAND(buf2), stdout);
791 /***************************************************************************
795 ***************************************************************************/
798 file_comparison(int argc, char *argv[])
801 /* someday we may allow comparisons on more files */
803 ENTRY *heads[MAXCOMPARE];
807 memset(heads, 0, sizeof(heads));
808 dump_init((char *) 0, F_LITERAL, S_TERMINFO, 0, 65535, itrace, FALSE);
810 for (n = 0; n < argc && n < MAXCOMPARE; n++) {
811 if (freopen(argv[n], "r", stdin) == 0)
812 _nc_err_abort("Can't open %s", argv[n]);
814 _nc_head = _nc_tail = 0;
816 /* parse entries out of the source file */
817 _nc_set_source(argv[n]);
818 _nc_read_entry_source(stdin, NULL, TRUE, literal, NULLHOOK);
821 (void) fprintf(stderr, "Resolving file %d...\n", n - 0);
823 /* maybe do use resolution */
824 if (!_nc_resolve_uses2(!limited, literal)) {
825 (void) fprintf(stderr,
826 "There are unresolved use entries in %s:\n",
830 (void) fputs(qp->tterm.term_names, stderr);
831 (void) fputc('\n', stderr);
834 ExitProgram(EXIT_FAILURE);
837 heads[filecount] = _nc_head;
841 /* OK, all entries are in core. Ready to do the comparison */
843 (void) fprintf(stderr, "Entries are now in core...\n");
845 /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
846 for (qp = heads[0]; qp; qp = qp->next) {
847 for (rp = heads[1]; rp; rp = rp->next)
848 if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
849 if (qp->ncrosslinks < MAX_CROSSLINKS)
850 qp->crosslinks[qp->ncrosslinks] = rp;
853 if (rp->ncrosslinks < MAX_CROSSLINKS)
854 rp->crosslinks[rp->ncrosslinks] = qp;
859 /* now we have two circular lists with crosslinks */
861 (void) fprintf(stderr, "Name matches are done...\n");
863 for (qp = heads[0]; qp; qp = qp->next) {
864 if (qp->ncrosslinks > 1) {
865 (void) fprintf(stderr,
866 "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
867 _nc_first_name(qp->tterm.term_names),
871 for (i = 0; i < qp->ncrosslinks; i++)
872 (void) fprintf(stderr,
874 _nc_first_name((qp->crosslinks[i])->tterm.term_names));
878 for (rp = heads[1]; rp; rp = rp->next) {
879 if (rp->ncrosslinks > 1) {
880 (void) fprintf(stderr,
881 "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
882 _nc_first_name(rp->tterm.term_names),
886 for (i = 0; i < rp->ncrosslinks; i++)
887 (void) fprintf(stderr,
889 _nc_first_name((rp->crosslinks[i])->tterm.term_names));
893 (void) printf("In file 1 (%s) only:\n", argv[0]);
894 for (qp = heads[0]; qp; qp = qp->next)
895 if (qp->ncrosslinks == 0)
896 (void) printf("\t%s\n",
897 _nc_first_name(qp->tterm.term_names));
899 (void) printf("In file 2 (%s) only:\n", argv[1]);
900 for (rp = heads[1]; rp; rp = rp->next)
901 if (rp->ncrosslinks == 0)
902 (void) printf("\t%s\n",
903 _nc_first_name(rp->tterm.term_names));
905 (void) printf("The following entries are equivalent:\n");
906 for (qp = heads[0]; qp; qp = qp->next) {
907 if (qp->ncrosslinks == 1) {
908 rp = qp->crosslinks[0];
910 repair_acsc(&qp->tterm);
911 repair_acsc(&rp->tterm);
913 _nc_align_termtype(&qp->tterm, &rp->tterm);
915 if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
916 char name1[NAMESIZE], name2[NAMESIZE];
918 (void) canonical_name(qp->tterm.term_names, name1);
919 (void) canonical_name(rp->tterm.term_names, name2);
921 (void) printf("%s = %s\n", name1, name2);
926 (void) printf("Differing entries:\n");
928 for (qp = heads[0]; qp; qp = qp->next) {
930 if (qp->ncrosslinks == 1) {
931 rp = qp->crosslinks[0];
933 /* sorry - we have to do this on each pass */
934 _nc_align_termtype(&qp->tterm, &rp->tterm);
936 if (!(entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp))) {
937 char name1[NAMESIZE], name2[NAMESIZE];
942 (void) canonical_name(qp->tterm.term_names, name1);
943 (void) canonical_name(rp->tterm.term_names, name2);
948 (void) fprintf(stderr,
949 "%s: dumping differences\n",
951 (void) printf("comparing %s to %s.\n", name1, name2);
952 compare_entry(compare_predicate, &entries->tterm, quiet);
957 (void) fprintf(stderr,
958 "%s: dumping common capabilities\n",
960 (void) printf("comparing %s to %s.\n", name1, name2);
961 compare_entry(compare_predicate, &entries->tterm, quiet);
966 (void) fprintf(stderr,
967 "%s: dumping differences\n",
969 (void) printf("comparing %s to %s.\n", name1, name2);
970 compare_entry(compare_predicate, &entries->tterm, quiet);
982 static const char *tbl[] =
984 "Usage: infocmp [options] [-A directory] [-B directory] [termname...]"
987 ," -0 print single-row"
988 ," -1 print single-column"
989 ," -K use termcap-names and BSD syntax"
990 ," -C use termcap-names"
991 ," -F compare terminfo-files"
992 ," -I use terminfo-names"
993 ," -L use long names"
994 ," -R subset (see manpage)"
995 ," -T eliminate size limits (test)"
996 ," -U eliminate post-processing of entries"
999 ," -a with -F, list commented-out caps"
1001 ," -c list common capabilities"
1002 ," -d list different capabilities"
1003 ," -e format output for C initializer"
1004 ," -E format output as C tables"
1005 ," -f with -1, format complex strings"
1006 ," -G format %{number} to %'char'"
1007 ," -g format %'char' to %{number}"
1008 ," -i analyze initialization/reset"
1009 ," -l output terminfo names"
1010 ," -n list capabilities in neither"
1011 ," -p ignore padding specifiers"
1012 ," -q brief listing, removes headers"
1013 ," -r with -C, output in termcap form"
1014 ," -r with -F, resolve use-references"
1015 ," -s [d|i|l|c] sort fields"
1017 ," -t suppress commented-out capabilities"
1019 ," -u produce source with 'use='"
1020 ," -v number (verbose)"
1021 ," -w number (width)"
1023 ," -x treat unknown capabilities as user-defined"
1026 const size_t first = 3;
1027 const size_t last = SIZEOF(tbl);
1028 const size_t left = (last - first + 1) / 2 + first;
1031 for (n = 0; n < left; n++) {
1032 size_t m = (n < first) ? last : n + left - first;
1034 fprintf(stderr, "%-40.40s%s\n", tbl[n], tbl[m]);
1036 fprintf(stderr, "%s\n", tbl[n]);
1038 ExitProgram(EXIT_FAILURE);
1042 any_initializer(const char *fmt, const char *type)
1044 static char *initializer;
1048 if (initializer == 0) {
1049 need = (strlen(entries->tterm.term_names)
1052 initializer = (char *) malloc(need);
1055 _nc_STRCPY(initializer, entries->tterm.term_names, need);
1056 for (s = initializer; *s != 0 && *s != '|'; s++) {
1057 if (!isalnum(UChar(*s)))
1061 _nc_SPRINTF(s, _nc_SLIMIT(need) fmt, type);
1066 name_initializer(const char *type)
1068 return any_initializer("_%s_data", type);
1072 string_variable(const char *type)
1074 return any_initializer("_s_%s", type);
1077 /* dump C initializers for the terminal type */
1079 dump_initializers(TERMTYPE *term)
1082 const char *str = 0;
1084 printf("\nstatic char %s[] = \"%s\";\n\n",
1085 name_initializer("alias"), entries->tterm.term_names);
1087 for_each_string(n, term) {
1088 char buf[MAX_STRING], *sp, *tp;
1090 if (VALID_STRING(term->Strings[n])) {
1093 for (sp = term->Strings[n];
1094 *sp != 0 && (tp - buf) < MAX_STRING - 6;
1096 if (isascii(UChar(*sp))
1097 && isprint(UChar(*sp))
1102 _nc_SPRINTF(tp, _nc_SLIMIT(MAX_STRING) "\\%03o", UChar(*sp));
1108 (void) printf("static char %-20s[] = %s;\n",
1109 string_variable(ExtStrname(term, (int) n, strnames)),
1115 (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
1117 for_each_boolean(n, term) {
1118 switch ((int) (term->Booleans[n])) {
1127 case ABSENT_BOOLEAN:
1128 str = "ABSENT_BOOLEAN";
1131 case CANCELLED_BOOLEAN:
1132 str = "CANCELLED_BOOLEAN";
1135 (void) printf("\t/* %3u: %-8s */\t%s,\n",
1136 n, ExtBoolname(term, (int) n, boolnames), str);
1138 (void) printf("%s;\n", R_CURL);
1140 (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
1142 for_each_number(n, term) {
1144 switch (term->Numbers[n]) {
1145 case ABSENT_NUMERIC:
1146 str = "ABSENT_NUMERIC";
1148 case CANCELLED_NUMERIC:
1149 str = "CANCELLED_NUMERIC";
1152 _nc_SPRINTF(buf, _nc_SLIMIT(sizeof(buf)) "%d", term->Numbers[n]);
1156 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1157 ExtNumname(term, (int) n, numnames), str);
1159 (void) printf("%s;\n", R_CURL);
1161 (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
1163 for_each_string(n, term) {
1165 if (term->Strings[n] == ABSENT_STRING)
1166 str = "ABSENT_STRING";
1167 else if (term->Strings[n] == CANCELLED_STRING)
1168 str = "CANCELLED_STRING";
1170 str = string_variable(ExtStrname(term, (int) n, strnames));
1172 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1173 ExtStrname(term, (int) n, strnames), str);
1175 (void) printf("%s;\n", R_CURL);
1178 if ((NUM_BOOLEANS(term) != BOOLCOUNT)
1179 || (NUM_NUMBERS(term) != NUMCOUNT)
1180 || (NUM_STRINGS(term) != STRCOUNT)) {
1181 (void) printf("static char * %s[] = %s\n",
1182 name_initializer("string_ext"), L_CURL);
1183 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
1184 (void) printf("\t/* %3u: bool */\t\"%s\",\n",
1185 n, ExtBoolname(term, (int) n, boolnames));
1187 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
1188 (void) printf("\t/* %3u: num */\t\"%s\",\n",
1189 n, ExtNumname(term, (int) n, numnames));
1191 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
1192 (void) printf("\t/* %3u: str */\t\"%s\",\n",
1193 n, ExtStrname(term, (int) n, strnames));
1195 (void) printf("%s;\n", R_CURL);
1200 /* dump C initializers for the terminal type */
1202 dump_termtype(TERMTYPE *term)
1204 (void) printf("\t%s\n\t\t%s,\n", L_CURL, name_initializer("alias"));
1205 (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
1207 (void) printf("\t\t%s,\n", name_initializer("bool"));
1208 (void) printf("\t\t%s,\n", name_initializer("number"));
1210 (void) printf("\t\t%s,\n", name_initializer("string"));
1213 (void) printf("#if NCURSES_XNAMES\n");
1214 (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
1215 (void) printf("\t\t%s,\t/* ...corresponding names */\n",
1216 ((NUM_BOOLEANS(term) != BOOLCOUNT)
1217 || (NUM_NUMBERS(term) != NUMCOUNT)
1218 || (NUM_STRINGS(term) != STRCOUNT))
1219 ? name_initializer("string_ext")
1222 (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
1223 (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
1224 (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
1226 (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
1227 NUM_BOOLEANS(term) - BOOLCOUNT);
1228 (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
1229 NUM_NUMBERS(term) - NUMCOUNT);
1230 (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
1231 NUM_STRINGS(term) - STRCOUNT);
1233 (void) printf("#endif /* NCURSES_XNAMES */\n");
1236 #endif /* NCURSES_XNAMES */
1237 (void) printf("\t%s\n", R_CURL);
1241 optarg_to_number(void)
1244 long value = strtol(optarg, &temp, 0);
1246 if (temp == 0 || temp == optarg || *temp != 0) {
1247 fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
1248 ExitProgram(EXIT_FAILURE);
1258 if ((terminal = getenv("TERM")) == 0) {
1259 (void) fprintf(stderr,
1260 "%s: environment variable TERM not set\n",
1268 * Show the databases that infocmp knows about. The location to which it writes is
1271 show_databases(void)
1277 _nc_first_db(&state, &offset);
1278 while ((path2 = _nc_next_db(&state, &offset)) != 0) {
1279 printf("%s\n", path2);
1284 /***************************************************************************
1288 ***************************************************************************/
1291 #define MAIN_LEAKS() \
1296 #define MAIN_LEAKS() /* nothing */
1300 main(int argc, char *argv[])
1302 /* Avoid "local data >32k" error with mwcc */
1303 /* Also avoid overflowing smaller stacks on systems like AmigaOS */
1310 char *firstdir, *restdir;
1312 bool formatted = FALSE;
1313 bool filecompare = FALSE;
1315 bool init_analyze = FALSE;
1316 bool suppress_untranslatable = FALSE;
1318 /* where is the terminfo database location going to default to? */
1319 restdir = firstdir = 0;
1322 use_extended_names(FALSE);
1326 _nc_progname = _nc_rootname(argv[0]);
1328 /* make sure we have enough space to add two terminal entries */
1329 myargv = typeCalloc(char *, (size_t) (argc + 3));
1330 memcpy(myargv, argv, (sizeof(char *) * (size_t) argc));
1333 while ((c = getopt(argc,
1335 "01A:aB:CcDdEeFfGgIiKLlnpqR:rs:TtUuVv:w:x")) != -1) {
1352 _nc_disable_period = TRUE;
1353 use_extended_names(TRUE);
1364 outform = F_TERMCAP;
1366 if (sortmode == S_DEFAULT)
1367 sortmode = S_TERMCAP;
1372 ExitProgram(EXIT_SUCCESS);
1380 compare = C_DIFFERENCE;
1408 outform = F_TERMINFO;
1409 if (sortmode == S_DEFAULT)
1410 sortmode = S_VARIABLE;
1415 init_analyze = TRUE;
1419 outform = F_VARIABLE;
1420 if (sortmode == S_DEFAULT)
1421 sortmode = S_VARIABLE;
1425 outform = F_TERMINFO;
1453 sortmode = S_NOSORT;
1454 else if (*optarg == 'i')
1455 sortmode = S_TERMINFO;
1456 else if (*optarg == 'l')
1457 sortmode = S_VARIABLE;
1458 else if (*optarg == 'c')
1459 sortmode = S_TERMCAP;
1461 (void) fprintf(stderr,
1462 "%s: unknown sort mode\n",
1464 ExitProgram(EXIT_FAILURE);
1474 _nc_disable_period = FALSE;
1475 suppress_untranslatable = TRUE;
1488 puts(curses_version());
1489 ExitProgram(EXIT_SUCCESS);
1492 itrace = (unsigned) optarg_to_number();
1493 set_trace_level(itrace);
1497 mwidth = optarg_to_number();
1502 use_extended_names(TRUE);
1511 maxterms = (size_t) (argc + 2 - optind);
1512 tfile = typeMalloc(path, maxterms);
1513 tname = typeCalloc(char *, maxterms);
1514 entries = typeCalloc(ENTRY, maxterms);
1519 fprintf(stderr, "%s: not enough memory\n", _nc_progname);
1520 ExitProgram(EXIT_FAILURE);
1523 /* by default, sort by terminfo name */
1524 if (sortmode == S_DEFAULT)
1525 sortmode = S_TERMINFO;
1527 /* set up for display */
1528 dump_init(tversion, outform, sortmode, mwidth, mheight, itrace, formatted);
1530 /* make sure we have at least one terminal name to work with */
1532 argv[argc++] = terminal_env();
1534 /* if user is after a comparison, make sure we have two entries */
1535 if (compare != C_DEFAULT && optind >= argc - 1)
1536 argv[argc++] = terminal_env();
1538 /* exactly two terminal names with no options means do -d */
1539 if (argc - optind == 2 && compare == C_DEFAULT)
1540 compare = C_DIFFERENCE;
1543 /* grab the entries */
1545 for (; optind < argc; optind++) {
1546 const char *directory = termcount ? restdir : firstdir;
1549 tname[termcount] = argv[optind];
1553 #if MIXEDCASE_FILENAMES
1554 #define LEAF_FMT "%c"
1556 #define LEAF_FMT "%02x"
1558 _nc_SPRINTF(tfile[termcount],
1559 _nc_SLIMIT(sizeof(path))
1560 "%s/" LEAF_FMT "/%s",
1562 UChar(*argv[optind]), argv[optind]);
1564 (void) fprintf(stderr,
1565 "%s: reading entry %s from file %s\n",
1567 argv[optind], tfile[termcount]);
1569 status = _nc_read_file_entry(tfile[termcount],
1570 &entries[termcount].tterm);
1572 (void) fprintf(stderr, "%s: terminfo files not supported\n",
1575 ExitProgram(EXIT_FAILURE);
1579 (void) fprintf(stderr,
1580 "%s: reading entry %s from database\n",
1584 status = _nc_read_entry(tname[termcount],
1586 &entries[termcount].tterm);
1590 (void) fprintf(stderr,
1591 "%s: couldn't open terminfo file %s.\n",
1595 ExitProgram(EXIT_FAILURE);
1597 repair_acsc(&entries[termcount].tterm);
1603 _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
1606 /* dump as C initializer for the terminal type */
1609 dump_termtype(&entries[0].tterm);
1611 dump_initializers(&entries[0].tterm);
1614 /* analyze the init strings */
1615 else if (init_analyze) {
1617 #define CUR entries[0].tterm.
1618 analyze_string("is1", init_1string, &entries[0].tterm);
1619 analyze_string("is2", init_2string, &entries[0].tterm);
1620 analyze_string("is3", init_3string, &entries[0].tterm);
1621 analyze_string("rs1", reset_1string, &entries[0].tterm);
1622 analyze_string("rs2", reset_2string, &entries[0].tterm);
1623 analyze_string("rs3", reset_3string, &entries[0].tterm);
1624 analyze_string("smcup", enter_ca_mode, &entries[0].tterm);
1625 analyze_string("rmcup", exit_ca_mode, &entries[0].tterm);
1630 * Here's where the real work gets done
1635 (void) fprintf(stderr,
1636 "%s: about to dump %s\n",
1639 (void) printf("#\tReconstructed via infocmp from file: %s\n",
1641 dump_entry(&entries[0].tterm,
1642 suppress_untranslatable,
1648 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1653 (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
1654 (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1655 compare_entry(compare_predicate, &entries->tterm, quiet);
1660 (void) fprintf(stderr,
1661 "%s: dumping common capabilities\n",
1663 (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1664 compare_entry(compare_predicate, &entries->tterm, quiet);
1669 (void) fprintf(stderr,
1670 "%s: dumping differences\n",
1672 (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1673 compare_entry(compare_predicate, &entries->tterm, quiet);
1678 (void) fprintf(stderr, "%s: dumping use entry\n", _nc_progname);
1679 dump_entry(&entries[0].tterm,
1680 suppress_untranslatable,
1684 for (i = 1; i < termcount; i++)
1685 dump_uses(tname[i], !(outform == F_TERMCAP
1686 || outform == F_TCONVERR));
1689 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1693 } else if (compare == C_USEALL)
1694 (void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
1695 else if (compare == C_DEFAULT)
1696 (void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
1697 else if (argc - optind != 2)
1698 (void) fprintf(stderr,
1699 "File comparison needs exactly two file arguments.\n");
1701 file_comparison(argc - optind, argv + optind);
1704 ExitProgram(EXIT_SUCCESS);
1707 /* infocmp.c ends here */