1 /****************************************************************************
2 * Copyright (c) 1998,1999,2000 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 ****************************************************************************/
35 * infocmp.c -- decompile an entry, or compare two entries
36 * written by Eric S. Raymond
39 #include <progs.priv.h>
41 #include <term_entry.h>
42 #include <dump_entry.h>
44 MODULE_ID("$Id: infocmp.c,v 1.57 2000/10/01 01:26:25 tom Exp $")
49 #define MAXTERMS 32 /* max # terminal arguments we can handle */
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 char *tname[MAXTERMS]; /* terminal type names */
64 static ENTRY entries[MAXTERMS]; /* terminfo entries */
65 static int termcount; /* count of terminal entries */
67 static bool limited = TRUE; /* "-r" option is not set */
68 static bool quiet = 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 int itrace; /* trace flag for debugging */
74 static int mwidth = 60;
75 static int numbers = 0; /* format "%'char'" to/from "%{number}" */
76 static int outform = F_TERMINFO; /* output format */
77 static int sortmode; /* sort_mode */
79 /* main comparison mode */
81 #define C_DEFAULT 0 /* don't force comparison mode */
82 #define C_DIFFERENCE 1 /* list differences between two terminals */
83 #define C_COMMON 2 /* list common capabilities */
84 #define C_NAND 3 /* list capabilities in neither terminal */
85 #define C_USEALL 4 /* generate relative use-form entry */
86 static bool ignorepads; /* ignore pad prefixes when diffing */
91 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();
99 _nc_free_and_exit(code);
104 canonical_name(char *ptr, char *buf)
105 /* extract the terminal type's primary name */
109 (void) strcpy(buf, ptr);
110 if ((bp = strchr(buf, '|')) != 0)
116 /***************************************************************************
118 * Predicates for dump function
120 ***************************************************************************/
123 capcmp(int idx, const char *s, const char *t)
124 /* capability comparison function */
126 if (!VALID_STRING(s) && !VALID_STRING(t))
128 else if (!VALID_STRING(s) || !VALID_STRING(t))
131 if ((idx == acs_chars_index) || !ignorepads)
132 return (strcmp(s, t));
134 return (_nc_capcmp(s, t));
138 use_predicate(int type, int idx)
139 /* predicate function to use for use decompilation */
149 * This assumes that multiple use entries are supposed
150 * to contribute the logical or of their boolean capabilities.
151 * This is true if we take the semantics of multiple uses to
152 * be 'each capability gets the first non-default value found
153 * in the sequence of use entries'.
155 * Note that cancelled or absent booleans are stored as FALSE,
156 * unlike numbers and strings, whose cancelled/absent state is
157 * recorded in the terminfo database.
159 for (ep = &entries[1]; ep < entries + termcount; ep++)
160 if (ep->tterm.Booleans[idx] == TRUE) {
161 is_set = entries[0].tterm.Booleans[idx];
164 if (is_set != entries[0].tterm.Booleans[idx])
172 int value = ABSENT_NUMERIC;
175 * We take the semantics of multiple uses to be 'each
176 * capability gets the first non-default value found
177 * in the sequence of use entries'.
179 for (ep = &entries[1]; ep < entries + termcount; ep++)
180 if (VALID_NUMERIC(ep->tterm.Numbers[idx])) {
181 value = ep->tterm.Numbers[idx];
185 if (value != entries[0].tterm.Numbers[idx])
186 return (value != ABSENT_NUMERIC);
193 char *termstr, *usestr = ABSENT_STRING;
195 termstr = entries[0].tterm.Strings[idx];
198 * We take the semantics of multiple uses to be 'each
199 * capability gets the first non-default value found
200 * in the sequence of use entries'.
202 for (ep = &entries[1]; ep < entries + termcount; ep++)
203 if (ep->tterm.Strings[idx]) {
204 usestr = ep->tterm.Strings[idx];
208 if (usestr == ABSENT_STRING && termstr == ABSENT_STRING)
210 else if (!usestr || !termstr || capcmp(idx, usestr, termstr))
217 return (FALSE); /* pacify compiler */
221 useeq(ENTRY * e1, ENTRY * e2)
222 /* are the use references in two entries equivalent? */
226 if (e1->nuses != e2->nuses)
229 /* Ugh...this is quadratic again */
230 for (i = 0; i < e1->nuses; i++) {
231 bool foundmatch = FALSE;
233 /* search second entry for given use reference */
234 for (j = 0; j < e2->nuses; j++)
235 if (!strcmp(e1->uses[i].name, e2->uses[j].name)) {
248 entryeq(TERMTYPE * t1, TERMTYPE * t2)
249 /* are two entries equivalent? */
253 for (i = 0; i < NUM_BOOLEANS(t1); i++)
254 if (t1->Booleans[i] != t2->Booleans[i])
257 for (i = 0; i < NUM_NUMBERS(t1); i++)
258 if (t1->Numbers[i] != t2->Numbers[i])
261 for (i = 0; i < NUM_STRINGS(t1); i++)
262 if (capcmp(i, t1->Strings[i], t2->Strings[i]))
268 #define TIC_EXPAND(result) _nc_tic_expand(result, outform==F_TERMINFO, numbers)
271 print_uses(ENTRY * ep, FILE * fp)
272 /* print an entry's use references */
279 for (i = 0; i < ep->nuses; i++) {
280 fputs(ep->uses[i].name, fp);
281 if (i < ep->nuses - 1)
287 dump_boolean(int val)
288 /* display the value of a boolean capability */
293 case CANCELLED_BOOLEAN:
305 dump_numeric(int val, char *buf)
306 /* display the value of a boolean capability */
310 strcpy(buf, s_absent);
312 case CANCELLED_NUMERIC:
313 strcpy(buf, s_cancel);
316 sprintf(buf, "%d", val);
322 dump_string(char *val, char *buf)
323 /* display the value of a string capability */
325 if (val == ABSENT_STRING)
326 strcpy(buf, s_absent);
327 else if (val == CANCELLED_STRING)
328 strcpy(buf, s_cancel);
330 sprintf(buf, "'%.*s'", MAX_STRING - 3, TIC_EXPAND(val));
335 compare_predicate(int type, int idx, const char *name)
336 /* predicate function to use for entry difference reports */
338 register ENTRY *e1 = &entries[0];
339 register ENTRY *e2 = &entries[1];
340 char buf1[MAX_STRING], buf2[MAX_STRING];
347 b1 = e1->tterm.Booleans[idx];
348 b2 = e2->tterm.Booleans[idx];
351 if (!(b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN) && b1 != b2)
352 (void) printf("\t%s: %s%s%s.\n",
360 if (b1 == b2 && b1 != ABSENT_BOOLEAN)
361 (void) printf("\t%s= %s.\n", name, dump_boolean(b1));
365 if (b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN)
366 (void) printf("\t!%s.\n", name);
372 n1 = e1->tterm.Numbers[idx];
373 n2 = e2->tterm.Numbers[idx];
374 dump_numeric(n1, buf1);
375 dump_numeric(n2, buf2);
378 if (!((n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)) && n1 != n2)
379 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
383 if (n1 != ABSENT_NUMERIC && n2 != ABSENT_NUMERIC && n1 == n2)
384 (void) printf("\t%s= %s.\n", name, buf1);
388 if (n1 == ABSENT_NUMERIC && n2 == ABSENT_NUMERIC)
389 (void) printf("\t!%s.\n", name);
395 s1 = e1->tterm.Strings[idx];
396 s2 = e2->tterm.Strings[idx];
399 if (capcmp(idx, s1, s2)) {
400 dump_string(s1, buf1);
401 dump_string(s2, buf2);
402 if (strcmp(buf1, buf2))
403 (void) printf("\t%s: %s, %s.\n", name, buf1, buf2);
408 if (s1 && s2 && !capcmp(idx, s1, s2))
409 (void) printf("\t%s= '%s'.\n", name, TIC_EXPAND(s1));
414 (void) printf("\t!%s.\n", name);
420 /* unlike the other modes, this compares *all* use entries */
423 if (!useeq(e1, e2)) {
424 (void) fputs("\tuse: ", stdout);
425 print_uses(e1, stdout);
427 print_uses(e2, stdout);
428 fputs(".\n", stdout);
433 if (e1->nuses && e2->nuses && useeq(e1, e2)) {
434 (void) fputs("\tuse: ", stdout);
435 print_uses(e1, stdout);
436 fputs(".\n", stdout);
441 if (!e1->nuses && !e2->nuses)
442 (void) printf("\t!use.\n");
448 /***************************************************************************
450 * Init string analysis
452 ***************************************************************************/
459 static const assoc std_caps[] =
461 /* these are specified by X.364 and iBCS2 */
462 {"\033c", "RIS"}, /* full reset */
463 {"\0337", "SC"}, /* save cursor */
464 {"\0338", "RC"}, /* restore cursor */
465 {"\033[r", "RSR"}, /* not an X.364 mnemonic */
466 {"\033[m", "SGR0"}, /* not an X.364 mnemonic */
467 {"\033[2J", "ED2"}, /* clear page */
469 /* this group is specified by ISO 2022 */
470 {"\033(0", "ISO DEC G0"}, /* enable DEC graphics for G0 */
471 {"\033(A", "ISO UK G0"}, /* enable UK chars for G0 */
472 {"\033(B", "ISO US G0"}, /* enable US chars for G0 */
473 {"\033)0", "ISO DEC G1"}, /* enable DEC graphics for G1 */
474 {"\033)A", "ISO UK G1"}, /* enable UK chars for G1 */
475 {"\033)B", "ISO US G1"}, /* enable US chars for G1 */
477 /* these are DEC private modes widely supported by emulators */
478 {"\033=", "DECPAM"}, /* application keypad mode */
479 {"\033>", "DECPNM"}, /* normal keypad mode */
480 {"\033<", "DECANSI"}, /* enter ANSI mode */
482 {(char *) 0, (char *) 0}
485 static const assoc private_modes[] =
486 /* DEC \E[ ... [hl] modes recognized by many emulators */
488 {"1", "CKM"}, /* application cursor keys */
489 {"2", "ANM"}, /* set VT52 mode */
490 {"3", "COLM"}, /* 132-column mode */
491 {"4", "SCLM"}, /* smooth scroll */
492 {"5", "SCNM"}, /* reverse video mode */
493 {"6", "OM"}, /* origin mode */
494 {"7", "AWM"}, /* wraparound mode */
495 {"8", "ARM"}, /* auto-repeat mode */
496 {(char *) 0, (char *) 0}
499 static const assoc ecma_highlights[] =
500 /* recognize ECMA attribute sequences */
502 {"0", "NORMAL"}, /* normal */
503 {"1", "+BOLD"}, /* bold on */
504 {"2", "+DIM"}, /* dim on */
505 {"3", "+ITALIC"}, /* italic on */
506 {"4", "+UNDERLINE"}, /* underline on */
507 {"5", "+BLINK"}, /* blink on */
508 {"6", "+FASTBLINK"}, /* fastblink on */
509 {"7", "+REVERSE"}, /* reverse on */
510 {"8", "+INVISIBLE"}, /* invisible on */
511 {"9", "+DELETED"}, /* deleted on */
512 {"10", "MAIN-FONT"}, /* select primary font */
513 {"11", "ALT-FONT-1"}, /* select alternate font 1 */
514 {"12", "ALT-FONT-2"}, /* select alternate font 2 */
515 {"13", "ALT-FONT-3"}, /* select alternate font 3 */
516 {"14", "ALT-FONT-4"}, /* select alternate font 4 */
517 {"15", "ALT-FONT-5"}, /* select alternate font 5 */
518 {"16", "ALT-FONT-6"}, /* select alternate font 6 */
519 {"17", "ALT-FONT-7"}, /* select alternate font 7 */
520 {"18", "ALT-FONT-1"}, /* select alternate font 1 */
521 {"19", "ALT-FONT-1"}, /* select alternate font 1 */
522 {"20", "FRAKTUR"}, /* Fraktur font */
523 {"21", "DOUBLEUNDER"}, /* double underline */
524 {"22", "-DIM"}, /* dim off */
525 {"23", "-ITALIC"}, /* italic off */
526 {"24", "-UNDERLINE"}, /* underline off */
527 {"25", "-BLINK"}, /* blink off */
528 {"26", "-FASTBLINK"}, /* fastblink off */
529 {"27", "-REVERSE"}, /* reverse off */
530 {"28", "-INVISIBLE"}, /* invisible off */
531 {"29", "-DELETED"}, /* deleted off */
532 {(char *) 0, (char *) 0}
536 analyze_string(const char *name, const char *cap, TERMTYPE * tp)
538 char buf[MAX_TERMINFO_LENGTH];
539 char buf2[MAX_TERMINFO_LENGTH];
543 if (cap == ABSENT_STRING || cap == CANCELLED_STRING)
545 (void) printf("%s: ", name);
548 for (sp = cap; *sp; sp++) {
551 const char *expansion = 0;
553 /* first, check other capabilities in this entry */
554 for (i = 0; i < STRCOUNT; i++) {
555 char *cp = tp->Strings[i];
557 /* don't use soft-key capabilities */
558 if (strnames[i][0] == 'k' && strnames[i][0] == 'f')
561 if (cp != ABSENT_STRING && cp != CANCELLED_STRING && cp[0] && cp
564 (void) strncpy(buf2, sp, len);
567 if (_nc_capcmp(cp, buf2))
570 #define ISRS(s) (!strncmp((s), "is", 2) || !strncmp((s), "rs", 2))
572 * Theoretically we just passed the test for translation
573 * (equality once the padding is stripped). However, there
574 * are a few more hoops that need to be jumped so that
575 * identical pairs of initialization and reset strings
576 * don't just refer to each other.
578 if (ISRS(name) || ISRS(strnames[i]))
583 expansion = strnames[i];
588 /* now check the standard capabilities */
590 for (ap = std_caps; ap->from; ap++) {
591 len = strlen(ap->from);
593 if (strncmp(ap->from, sp, len) == 0) {
599 /* now check for private-mode sequences */
601 && sp[0] == '\033' && sp[1] == '[' && sp[2] == '?'
602 && (len = strspn(sp + 3, "0123456789;"))
603 && ((sp[3 + len] == 'h') || (sp[3 + len] == 'l'))) {
604 char buf3[MAX_TERMINFO_LENGTH];
606 (void) strcpy(buf2, (sp[3 + len] == 'h') ? "DEC+" : "DEC-");
607 (void) strncpy(buf3, sp + 3, len);
611 ep = strtok(buf3, ";");
615 for (ap = private_modes; ap->from; ap++) {
616 size_t tlen = strlen(ap->from);
618 if (strncmp(ap->from, ep, tlen) == 0) {
619 (void) strcat(buf2, ap->to);
626 (void) strcat(buf2, ep);
627 (void) strcat(buf2, ";");
629 ((ep = strtok((char *) 0, ";")));
630 buf2[strlen(buf2) - 1] = '\0';
634 /* now check for ECMA highlight sequences */
636 && sp[0] == '\033' && sp[1] == '['
637 && (len = strspn(sp + 2, "0123456789;"))
638 && sp[2 + len] == 'm') {
639 char buf3[MAX_TERMINFO_LENGTH];
641 (void) strcpy(buf2, "SGR:");
642 (void) strncpy(buf3, sp + 2, len);
646 ep = strtok(buf3, ";");
650 for (ap = ecma_highlights; ap->from; ap++) {
651 size_t tlen = strlen(ap->from);
653 if (strncmp(ap->from, ep, tlen) == 0) {
654 (void) strcat(buf2, ap->to);
661 (void) strcat(buf2, ep);
662 (void) strcat(buf2, ";");
664 ((ep = strtok((char *) 0, ";")));
666 buf2[strlen(buf2) - 1] = '\0';
669 /* now check for scroll region reset */
671 (void) sprintf(buf2, "\033[1;%dr", tp->Numbers[2]);
673 if (strncmp(buf2, sp, len) == 0)
677 /* now check for home-down */
679 (void) sprintf(buf2, "\033[%d;1H", tp->Numbers[2]);
681 if (strncmp(buf2, sp, len) == 0)
685 /* now look at the expansion we got, if any */
687 (void) sprintf(buf + strlen(buf), "{%s}", expansion);
691 /* couldn't match anything */
694 (void) strcat(buf, TIC_EXPAND(buf2));
697 (void) printf("%s\n", buf);
700 /***************************************************************************
704 ***************************************************************************/
707 file_comparison(int argc, char *argv[])
710 /* someday we may allow comparisons on more files */
712 ENTRY *heads[MAXCOMPARE];
713 ENTRY *tails[MAXCOMPARE];
717 dump_init((char *) 0, F_LITERAL, S_TERMINFO, 0, itrace, FALSE);
719 for (n = 0; n < argc && n < MAXCOMPARE; n++) {
720 if (freopen(argv[n], "r", stdin) == 0)
721 _nc_err_abort("Can't open %s", argv[n]);
723 _nc_head = _nc_tail = 0;
725 /* parse entries out of the source file */
726 _nc_set_source(argv[n]);
727 _nc_read_entry_source(stdin, NULL, TRUE, FALSE, NULLHOOK);
730 (void) fprintf(stderr, "Resolving file %d...\n", n - 0);
732 /* maybe do use resolution */
733 if (!_nc_resolve_uses(!limited)) {
734 (void) fprintf(stderr,
735 "There are unresolved use entries in %s:\n",
739 (void) fputs(qp->tterm.term_names, stderr);
740 (void) fputc('\n', stderr);
746 heads[filecount] = _nc_head;
747 tails[filecount] = _nc_tail;
751 /* OK, all entries are in core. Ready to do the comparison */
753 (void) fprintf(stderr, "Entries are now in core...\n");
755 /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
756 for (qp = heads[0]; qp; qp = qp->next) {
757 for (rp = heads[1]; rp; rp = rp->next)
758 if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
759 if (qp->ncrosslinks < MAX_CROSSLINKS)
760 qp->crosslinks[qp->ncrosslinks] = rp;
763 if (rp->ncrosslinks < MAX_CROSSLINKS)
764 rp->crosslinks[rp->ncrosslinks] = qp;
769 /* now we have two circular lists with crosslinks */
771 (void) fprintf(stderr, "Name matches are done...\n");
773 for (qp = heads[0]; qp; qp = qp->next) {
774 if (qp->ncrosslinks > 1) {
775 (void) fprintf(stderr,
776 "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
777 _nc_first_name(qp->tterm.term_names),
781 for (i = 0; i < qp->ncrosslinks; i++)
782 (void) fprintf(stderr,
784 _nc_first_name((qp->crosslinks[i])->tterm.term_names));
788 for (rp = heads[1]; rp; rp = rp->next) {
789 if (rp->ncrosslinks > 1) {
790 (void) fprintf(stderr,
791 "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
792 _nc_first_name(rp->tterm.term_names),
796 for (i = 0; i < rp->ncrosslinks; i++)
797 (void) fprintf(stderr,
799 _nc_first_name((rp->crosslinks[i])->tterm.term_names));
803 (void) printf("In file 1 (%s) only:\n", argv[0]);
804 for (qp = heads[0]; qp; qp = qp->next)
805 if (qp->ncrosslinks == 0)
806 (void) printf("\t%s\n",
807 _nc_first_name(qp->tterm.term_names));
809 (void) printf("In file 2 (%s) only:\n", argv[1]);
810 for (rp = heads[1]; rp; rp = rp->next)
811 if (rp->ncrosslinks == 0)
812 (void) printf("\t%s\n",
813 _nc_first_name(rp->tterm.term_names));
815 (void) printf("The following entries are equivalent:\n");
816 for (qp = heads[0]; qp; qp = qp->next) {
817 rp = qp->crosslinks[0];
819 if (qp->ncrosslinks == 1) {
820 rp = qp->crosslinks[0];
822 repair_acsc(&qp->tterm);
823 repair_acsc(&rp->tterm);
825 _nc_align_termtype(&qp->tterm, &rp->tterm);
827 if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
828 char name1[NAMESIZE], name2[NAMESIZE];
830 (void) canonical_name(qp->tterm.term_names, name1);
831 (void) canonical_name(rp->tterm.term_names, name2);
833 (void) printf("%s = %s\n", name1, name2);
838 (void) printf("Differing entries:\n");
840 for (qp = heads[0]; qp; qp = qp->next) {
842 if (qp->ncrosslinks == 1) {
843 rp = qp->crosslinks[0];
845 /* sorry - we have to do this on each pass */
846 _nc_align_termtype(&qp->tterm, &rp->tterm);
848 if (!(entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp))) {
849 char name1[NAMESIZE], name2[NAMESIZE];
854 (void) canonical_name(qp->tterm.term_names, name1);
855 (void) canonical_name(rp->tterm.term_names, name2);
860 (void) fprintf(stderr,
861 "infocmp: dumping differences\n");
862 (void) printf("comparing %s to %s.\n", name1, name2);
863 compare_entry(compare_predicate, &entries->tterm, quiet);
868 (void) fprintf(stderr,
869 "infocmp: dumping common capabilities\n");
870 (void) printf("comparing %s to %s.\n", name1, name2);
871 compare_entry(compare_predicate, &entries->tterm, quiet);
876 (void) fprintf(stderr,
877 "infocmp: dumping differences\n");
878 (void) printf("comparing %s to %s.\n", name1, name2);
879 compare_entry(compare_predicate, &entries->tterm, quiet);
891 static const char *tbl[] =
893 "Usage: infocmp [options] [-A directory] [-B directory] [termname...]"
896 ," -1 print single-column"
897 ," -C use termcap-names"
898 ," -F compare terminfo-files"
899 ," -I use terminfo-names"
900 ," -L use long names"
901 ," -R subset (see manpage)"
902 ," -T eliminate size limits (test)"
905 ," -a with -F, list commented-out caps"
907 ," -c list common capabilities"
908 ," -d list different capabilities"
909 ," -e format output for C initializer"
910 ," -E format output as C tables"
911 ," -f with -1, format complex strings"
912 ," -G format %{number} to %'char'"
913 ," -g format %'char' to %{number}"
914 ," -i analyze initialization/reset"
915 ," -l output terminfo names"
916 ," -n list capabilities in neither"
917 ," -p ignore padding specifiers"
918 ," -q brief listing, removes headers"
919 ," -r with -C, output in termcap form"
920 ," -r with -F, resolve use-references"
921 ," -s [d|i|l|c] sort fields"
922 ," -u produce source with 'use='"
923 ," -v number (verbose)"
924 ," -w number (width)"
926 const size_t first = 3;
927 const size_t last = SIZEOF(tbl);
928 const size_t left = (last - first + 1) / 2 + first;
931 for (n = 0; n < left; n++) {
932 size_t m = (n < first) ? last : n + left - first;
934 fprintf(stderr, "%-40.40s%s\n", tbl[n], tbl[m]);
936 fprintf(stderr, "%s\n", tbl[n]);
942 name_initializer(const char *type)
944 static char *initializer;
947 if (initializer == 0)
948 initializer = (char *) malloc(strlen(entries->tterm.term_names) + 20);
950 (void) sprintf(initializer, "%s_data_%s", type, entries->tterm.term_names);
951 for (s = initializer; *s != 0 && *s != '|'; s++) {
959 /* dump C initializers for the terminal type */
961 dump_initializers(TERMTYPE * term)
967 (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
969 for_each_boolean(n, term) {
970 switch ((int) (term->Booleans[n])) {
980 str = "ABSENT_BOOLEAN";
983 case CANCELLED_BOOLEAN:
984 str = "CANCELLED_BOOLEAN";
987 (void) printf("\t/* %3d: %-8s */\t%s,\n",
988 n, ExtBoolname(term, n, boolnames), str);
990 (void) printf("%s;\n", R_CURL);
992 (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
994 for_each_number(n, term) {
996 switch (term->Numbers[n]) {
998 str = "ABSENT_NUMERIC";
1000 case CANCELLED_NUMERIC:
1001 str = "CANCELLED_NUMERIC";
1004 sprintf(buf, "%d", term->Numbers[n]);
1008 (void) printf("\t/* %3d: %-8s */\t%s,\n", n,
1009 ExtNumname(term, n, numnames), str);
1011 (void) printf("%s;\n", R_CURL);
1013 size = sizeof(TERMTYPE)
1014 + (NUM_BOOLEANS(term) * sizeof(term->Booleans[0]))
1015 + (NUM_NUMBERS(term) * sizeof(term->Numbers[0]));
1017 (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
1019 for_each_string(n, term) {
1020 char buf[MAX_STRING], *sp, *tp;
1022 if (term->Strings[n] == ABSENT_STRING)
1023 str = "ABSENT_STRING";
1024 else if (term->Strings[n] == CANCELLED_STRING)
1025 str = "CANCELLED_STRING";
1029 for (sp = term->Strings[n];
1030 *sp != 0 && (tp - buf) < MAX_STRING - 6;
1032 if (isascii(*sp) && isprint(*sp) && *sp != '\\' && *sp != '"')
1035 (void) sprintf(tp, "\\%03o", *sp & 0xff);
1041 size += (strlen(term->Strings[n]) + 1);
1045 if (n == STRCOUNT) {
1046 (void) printf("%s;\n", R_CURL);
1048 (void) printf("static char * %s[] = %s\n",
1049 name_initializer("string_ext"), L_CURL);
1052 (void) printf("\t/* %3d: %-8s */\t%s,\n", n,
1053 ExtStrname(term, n, strnames), str);
1055 (void) printf("%s;\n", R_CURL);
1058 /* dump C initializers for the terminal type */
1060 dump_termtype(TERMTYPE * term)
1062 (void) printf("\t%s\n\t\t\"%s\",\n", L_CURL, term->term_names);
1063 (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
1065 (void) printf("\t\t%s,\n", name_initializer("bool"));
1066 (void) printf("\t\t%s,\n", name_initializer("number"));
1068 (void) printf("\t\t%s,\n", name_initializer("string"));
1071 (void) printf("#if NCURSES_XNAMES\n");
1072 (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
1073 (void) printf("\t\t%s,\t/* ...corresponding names */\n",
1074 (NUM_STRINGS(term) != STRCOUNT)
1075 ? name_initializer("string_ext")
1078 (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
1079 (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
1080 (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
1082 (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
1083 NUM_BOOLEANS(term) - BOOLCOUNT);
1084 (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
1085 NUM_NUMBERS(term) - NUMCOUNT);
1086 (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
1087 NUM_STRINGS(term) - STRCOUNT);
1089 (void) printf("#endif /* NCURSES_XNAMES */\n");
1090 #endif /* NCURSES_XNAMES */
1091 (void) printf("\t%s\n", R_CURL);
1095 optarg_to_number(void)
1098 long value = strtol(optarg, &temp, 0);
1100 if (temp == 0 || temp == optarg || *temp != 0) {
1101 fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
1107 /***************************************************************************
1111 ***************************************************************************/
1114 main(int argc, char *argv[])
1116 char *terminal, *firstdir, *restdir;
1117 /* Avoid "local data >32k" error with mwcc */
1118 /* Also avoid overflowing smaller stacks on systems like AmigaOS */
1119 path *tfile = (path *) malloc(sizeof(path) * MAXTERMS);
1121 bool formatted = FALSE;
1122 bool filecompare = FALSE;
1124 bool init_analyze = FALSE;
1126 if ((terminal = getenv("TERM")) == 0) {
1127 (void) fprintf(stderr,
1128 "infocmp: environment variable TERM not set\n");
1129 return EXIT_FAILURE;
1132 /* where is the terminfo database location going to default to? */
1133 restdir = firstdir = 0;
1135 while ((c = getopt(argc, argv, "adeEcCfFGgIinlLpqrR:s:uv:Vw:A:B:1T")) != EOF)
1139 _nc_disable_period = TRUE;
1140 use_extended_names(TRUE);
1144 compare = C_DIFFERENCE;
1160 outform = F_TERMCAP;
1162 if (sortmode == S_DEFAULT)
1163 sortmode = S_TERMCAP;
1183 outform = F_TERMINFO;
1184 if (sortmode == S_DEFAULT)
1185 sortmode = S_VARIABLE;
1190 init_analyze = TRUE;
1194 outform = F_TERMINFO;
1198 outform = F_VARIABLE;
1199 if (sortmode == S_DEFAULT)
1200 sortmode = S_VARIABLE;
1229 sortmode = S_NOSORT;
1230 else if (*optarg == 'i')
1231 sortmode = S_TERMINFO;
1232 else if (*optarg == 'l')
1233 sortmode = S_VARIABLE;
1234 else if (*optarg == 'c')
1235 sortmode = S_TERMCAP;
1237 (void) fprintf(stderr,
1238 "infocmp: unknown sort mode\n");
1239 return EXIT_FAILURE;
1248 itrace = optarg_to_number();
1249 set_trace_level(itrace);
1253 puts(curses_version());
1254 ExitProgram(EXIT_SUCCESS);
1257 mwidth = optarg_to_number();
1279 /* by default, sort by terminfo name */
1280 if (sortmode == S_DEFAULT)
1281 sortmode = S_TERMINFO;
1283 /* set up for display */
1284 dump_init(tversion, outform, sortmode, mwidth, itrace, formatted);
1286 /* make sure we have at least one terminal name to work with */
1288 argv[argc++] = terminal;
1290 /* if user is after a comparison, make sure we have two entries */
1291 if (compare != C_DEFAULT && optind >= argc - 1)
1292 argv[argc++] = terminal;
1294 /* exactly two terminal names with no options means do -d */
1295 if (argc - optind == 2 && compare == C_DEFAULT)
1296 compare = C_DIFFERENCE;
1299 /* grab the entries */
1301 for (; optind < argc; optind++) {
1302 if (termcount >= MAXTERMS) {
1303 (void) fprintf(stderr,
1304 "infocmp: too many terminal type arguments\n");
1305 return EXIT_FAILURE;
1307 const char *directory = termcount ? restdir : firstdir;
1310 tname[termcount] = argv[optind];
1313 (void) sprintf(tfile[termcount], "%s/%c/%s",
1315 *argv[optind], argv[optind]);
1317 (void) fprintf(stderr,
1318 "infocmp: reading entry %s from file %s\n",
1319 argv[optind], tfile[termcount]);
1321 status = _nc_read_file_entry(tfile[termcount],
1322 &entries[termcount].tterm);
1325 (void) fprintf(stderr,
1326 "infocmp: reading entry %s from system directories %s\n",
1327 argv[optind], tname[termcount]);
1329 status = _nc_read_entry(tname[termcount],
1331 &entries[termcount].tterm);
1332 directory = TERMINFO; /* for error message */
1336 (void) fprintf(stderr,
1337 "infocmp: couldn't open terminfo file %s.\n",
1339 return EXIT_FAILURE;
1341 repair_acsc(&entries[termcount].tterm);
1348 _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
1351 /* dump as C initializer for the terminal type */
1354 dump_termtype(&entries[0].tterm);
1356 dump_initializers(&entries[0].tterm);
1357 ExitProgram(EXIT_SUCCESS);
1360 /* analyze the init strings */
1363 #define CUR entries[0].tterm.
1364 analyze_string("is1", init_1string, &entries[0].tterm);
1365 analyze_string("is2", init_2string, &entries[0].tterm);
1366 analyze_string("is3", init_3string, &entries[0].tterm);
1367 analyze_string("rs1", reset_1string, &entries[0].tterm);
1368 analyze_string("rs2", reset_2string, &entries[0].tterm);
1369 analyze_string("rs3", reset_3string, &entries[0].tterm);
1370 analyze_string("smcup", enter_ca_mode, &entries[0].tterm);
1371 analyze_string("rmcup", exit_ca_mode, &entries[0].tterm);
1373 ExitProgram(EXIT_SUCCESS);
1377 * Here's where the real work gets done
1382 (void) fprintf(stderr,
1383 "infocmp: about to dump %s\n",
1385 (void) printf("#\tReconstructed via infocmp from file: %s\n",
1387 len = dump_entry(&entries[0].tterm, limited, numbers, NULL);
1390 (void) fprintf(stderr, "infocmp: length %d\n", len);
1395 (void) fprintf(stderr, "infocmp: dumping differences\n");
1396 (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1397 compare_entry(compare_predicate, &entries->tterm, quiet);
1402 (void) fprintf(stderr,
1403 "infocmp: dumping common capabilities\n");
1404 (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1405 compare_entry(compare_predicate, &entries->tterm, quiet);
1410 (void) fprintf(stderr,
1411 "infocmp: dumping differences\n");
1412 (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1413 compare_entry(compare_predicate, &entries->tterm, quiet);
1418 (void) fprintf(stderr, "infocmp: dumping use entry\n");
1419 len = dump_entry(&entries[0].tterm, limited, numbers, use_predicate);
1420 for (i = 1; i < termcount; i++)
1421 len += dump_uses(tname[i], !(outform == F_TERMCAP || outform
1425 (void) fprintf(stderr, "infocmp: length %d\n", len);
1428 } else if (compare == C_USEALL)
1429 (void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
1430 else if (compare == C_DEFAULT)
1431 (void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
1432 else if (argc - optind != 2)
1433 (void) fprintf(stderr,
1434 "File comparison needs exactly two file arguments.\n");
1436 file_comparison(argc - optind, argv + optind);
1438 ExitProgram(EXIT_SUCCESS);
1441 /* infocmp.c ends here */