1 /****************************************************************************
2 * Copyright (c) 1998-2002,2003 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.71 2003/10/18 18:01:54 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(unsigned 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
];
716 dump_init((char *) 0, F_LITERAL
, S_TERMINFO
, 0, itrace
, FALSE
);
718 for (n
= 0; n
< argc
&& n
< MAXCOMPARE
; n
++) {
719 if (freopen(argv
[n
], "r", stdin
) == 0)
720 _nc_err_abort("Can't open %s", argv
[n
]);
722 _nc_head
= _nc_tail
= 0;
724 /* parse entries out of the source file */
725 _nc_set_source(argv
[n
]);
726 _nc_read_entry_source(stdin
, NULL
, TRUE
, FALSE
, NULLHOOK
);
729 (void) fprintf(stderr
, "Resolving file %d...\n", n
- 0);
731 /* maybe do use resolution */
732 if (!_nc_resolve_uses(!limited
)) {
733 (void) fprintf(stderr
,
734 "There are unresolved use entries in %s:\n",
738 (void) fputs(qp
->tterm
.term_names
, stderr
);
739 (void) fputc('\n', stderr
);
742 ExitProgram(EXIT_FAILURE
);
745 heads
[filecount
] = _nc_head
;
749 /* OK, all entries are in core. Ready to do the comparison */
751 (void) fprintf(stderr
, "Entries are now in core...\n");
753 /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
754 for (qp
= heads
[0]; qp
; qp
= qp
->next
) {
755 for (rp
= heads
[1]; rp
; rp
= rp
->next
)
756 if (_nc_entry_match(qp
->tterm
.term_names
, rp
->tterm
.term_names
)) {
757 if (qp
->ncrosslinks
< MAX_CROSSLINKS
)
758 qp
->crosslinks
[qp
->ncrosslinks
] = rp
;
761 if (rp
->ncrosslinks
< MAX_CROSSLINKS
)
762 rp
->crosslinks
[rp
->ncrosslinks
] = qp
;
767 /* now we have two circular lists with crosslinks */
769 (void) fprintf(stderr
, "Name matches are done...\n");
771 for (qp
= heads
[0]; qp
; qp
= qp
->next
) {
772 if (qp
->ncrosslinks
> 1) {
773 (void) fprintf(stderr
,
774 "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
775 _nc_first_name(qp
->tterm
.term_names
),
779 for (i
= 0; i
< qp
->ncrosslinks
; i
++)
780 (void) fprintf(stderr
,
782 _nc_first_name((qp
->crosslinks
[i
])->tterm
.term_names
));
786 for (rp
= heads
[1]; rp
; rp
= rp
->next
) {
787 if (rp
->ncrosslinks
> 1) {
788 (void) fprintf(stderr
,
789 "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
790 _nc_first_name(rp
->tterm
.term_names
),
794 for (i
= 0; i
< rp
->ncrosslinks
; i
++)
795 (void) fprintf(stderr
,
797 _nc_first_name((rp
->crosslinks
[i
])->tterm
.term_names
));
801 (void) printf("In file 1 (%s) only:\n", argv
[0]);
802 for (qp
= heads
[0]; qp
; qp
= qp
->next
)
803 if (qp
->ncrosslinks
== 0)
804 (void) printf("\t%s\n",
805 _nc_first_name(qp
->tterm
.term_names
));
807 (void) printf("In file 2 (%s) only:\n", argv
[1]);
808 for (rp
= heads
[1]; rp
; rp
= rp
->next
)
809 if (rp
->ncrosslinks
== 0)
810 (void) printf("\t%s\n",
811 _nc_first_name(rp
->tterm
.term_names
));
813 (void) printf("The following entries are equivalent:\n");
814 for (qp
= heads
[0]; qp
; qp
= qp
->next
) {
815 rp
= qp
->crosslinks
[0];
817 if (qp
->ncrosslinks
== 1) {
818 rp
= qp
->crosslinks
[0];
820 repair_acsc(&qp
->tterm
);
821 repair_acsc(&rp
->tterm
);
823 _nc_align_termtype(&qp
->tterm
, &rp
->tterm
);
825 if (entryeq(&qp
->tterm
, &rp
->tterm
) && useeq(qp
, rp
)) {
826 char name1
[NAMESIZE
], name2
[NAMESIZE
];
828 (void) canonical_name(qp
->tterm
.term_names
, name1
);
829 (void) canonical_name(rp
->tterm
.term_names
, name2
);
831 (void) printf("%s = %s\n", name1
, name2
);
836 (void) printf("Differing entries:\n");
838 for (qp
= heads
[0]; qp
; qp
= qp
->next
) {
840 if (qp
->ncrosslinks
== 1) {
841 rp
= qp
->crosslinks
[0];
843 /* sorry - we have to do this on each pass */
844 _nc_align_termtype(&qp
->tterm
, &rp
->tterm
);
846 if (!(entryeq(&qp
->tterm
, &rp
->tterm
) && useeq(qp
, rp
))) {
847 char name1
[NAMESIZE
], name2
[NAMESIZE
];
852 (void) canonical_name(qp
->tterm
.term_names
, name1
);
853 (void) canonical_name(rp
->tterm
.term_names
, name2
);
858 (void) fprintf(stderr
,
859 "infocmp: dumping differences\n");
860 (void) printf("comparing %s to %s.\n", name1
, name2
);
861 compare_entry(compare_predicate
, &entries
->tterm
, quiet
);
866 (void) fprintf(stderr
,
867 "infocmp: dumping common capabilities\n");
868 (void) printf("comparing %s to %s.\n", name1
, name2
);
869 compare_entry(compare_predicate
, &entries
->tterm
, quiet
);
874 (void) fprintf(stderr
,
875 "infocmp: dumping differences\n");
876 (void) printf("comparing %s to %s.\n", name1
, name2
);
877 compare_entry(compare_predicate
, &entries
->tterm
, quiet
);
889 static const char *tbl
[] =
891 "Usage: infocmp [options] [-A directory] [-B directory] [termname...]"
894 ," -1 print single-column"
895 ," -C use termcap-names"
896 ," -F compare terminfo-files"
897 ," -I use terminfo-names"
898 ," -L use long names"
899 ," -R subset (see manpage)"
900 ," -T eliminate size limits (test)"
903 ," -a with -F, list commented-out caps"
905 ," -c list common capabilities"
906 ," -d list different capabilities"
907 ," -e format output for C initializer"
908 ," -E format output as C tables"
909 ," -f with -1, format complex strings"
910 ," -G format %{number} to %'char'"
911 ," -g format %'char' to %{number}"
912 ," -i analyze initialization/reset"
913 ," -l output terminfo names"
914 ," -n list capabilities in neither"
915 ," -p ignore padding specifiers"
916 ," -q brief listing, removes headers"
917 ," -r with -C, output in termcap form"
918 ," -r with -F, resolve use-references"
919 ," -s [d|i|l|c] sort fields"
921 ," -t suppress commented-out capabilities"
923 ," -u produce source with 'use='"
924 ," -v number (verbose)"
925 ," -w number (width)"
927 const size_t first
= 3;
928 const size_t last
= SIZEOF(tbl
);
929 const size_t left
= (last
- first
+ 1) / 2 + first
;
932 for (n
= 0; n
< left
; n
++) {
933 size_t m
= (n
< first
) ? last
: n
+ left
- first
;
935 fprintf(stderr
, "%-40.40s%s\n", tbl
[n
], tbl
[m
]);
937 fprintf(stderr
, "%s\n", tbl
[n
]);
939 ExitProgram(EXIT_FAILURE
);
943 any_initializer(const char *fmt
, const char *type
)
945 static char *initializer
;
948 if (initializer
== 0)
949 initializer
= (char *) malloc(strlen(entries
->tterm
.term_names
) +
950 strlen(type
) + strlen(fmt
));
952 (void) strcpy(initializer
, entries
->tterm
.term_names
);
953 for (s
= initializer
; *s
!= 0 && *s
!= '|'; s
++) {
954 if (!isalnum(UChar(*s
)))
958 (void) sprintf(s
, fmt
, type
);
963 name_initializer(const char *type
)
965 return any_initializer("_%s_data", type
);
969 string_variable(const char *type
)
971 return any_initializer("_s_%s", type
);
974 /* dump C initializers for the terminal type */
976 dump_initializers(TERMTYPE
* term
)
982 printf("\nstatic char %s[] = \"%s\";\n\n",
983 name_initializer("alias"), entries
->tterm
.term_names
);
985 for_each_string(n
, term
) {
986 char buf
[MAX_STRING
], *sp
, *tp
;
988 if (VALID_STRING(term
->Strings
[n
])) {
991 for (sp
= term
->Strings
[n
];
992 *sp
!= 0 && (tp
- buf
) < MAX_STRING
- 6;
994 if (isascii(UChar(*sp
))
995 && isprint(UChar(*sp
))
1000 (void) sprintf(tp
, "\\%03o", UChar(*sp
));
1006 size
+= (strlen(term
->Strings
[n
]) + 1);
1007 (void) printf("static char %-20s[] = %s;\n",
1008 string_variable(ExtStrname(term
, n
, strnames
)), buf
);
1013 (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL
);
1015 for_each_boolean(n
, term
) {
1016 switch ((int) (term
->Booleans
[n
])) {
1025 case ABSENT_BOOLEAN
:
1026 str
= "ABSENT_BOOLEAN";
1029 case CANCELLED_BOOLEAN
:
1030 str
= "CANCELLED_BOOLEAN";
1033 (void) printf("\t/* %3d: %-8s */\t%s,\n",
1034 n
, ExtBoolname(term
, n
, boolnames
), str
);
1036 (void) printf("%s;\n", R_CURL
);
1038 (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL
);
1040 for_each_number(n
, term
) {
1042 switch (term
->Numbers
[n
]) {
1043 case ABSENT_NUMERIC
:
1044 str
= "ABSENT_NUMERIC";
1046 case CANCELLED_NUMERIC
:
1047 str
= "CANCELLED_NUMERIC";
1050 sprintf(buf
, "%d", term
->Numbers
[n
]);
1054 (void) printf("\t/* %3d: %-8s */\t%s,\n", n
,
1055 ExtNumname(term
, n
, numnames
), str
);
1057 (void) printf("%s;\n", R_CURL
);
1059 size
= sizeof(TERMTYPE
)
1060 + (NUM_BOOLEANS(term
) * sizeof(term
->Booleans
[0]))
1061 + (NUM_NUMBERS(term
) * sizeof(term
->Numbers
[0]));
1063 (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL
);
1065 for_each_string(n
, term
) {
1067 if (term
->Strings
[n
] == ABSENT_STRING
)
1068 str
= "ABSENT_STRING";
1069 else if (term
->Strings
[n
] == CANCELLED_STRING
)
1070 str
= "CANCELLED_STRING";
1072 str
= string_variable(ExtStrname(term
, n
, strnames
));
1074 (void) printf("\t/* %3d: %-8s */\t%s,\n", n
,
1075 ExtStrname(term
, n
, strnames
), str
);
1077 (void) printf("%s;\n", R_CURL
);
1080 if ((NUM_BOOLEANS(term
) != BOOLCOUNT
)
1081 || (NUM_NUMBERS(term
) != NUMCOUNT
)
1082 || (NUM_STRINGS(term
) != STRCOUNT
)) {
1083 (void) printf("static char * %s[] = %s\n",
1084 name_initializer("string_ext"), L_CURL
);
1085 for (n
= BOOLCOUNT
; n
< NUM_BOOLEANS(term
); ++n
) {
1086 (void) printf("\t/* %3d: bool */\t\"%s\",\n",
1087 n
, ExtBoolname(term
, n
, boolnames
));
1089 for (n
= NUMCOUNT
; n
< NUM_NUMBERS(term
); ++n
) {
1090 (void) printf("\t/* %3d: num */\t\"%s\",\n",
1091 n
, ExtNumname(term
, n
, numnames
));
1093 for (n
= STRCOUNT
; n
< NUM_STRINGS(term
); ++n
) {
1094 (void) printf("\t/* %3d: str */\t\"%s\",\n",
1095 n
, ExtStrname(term
, n
, strnames
));
1097 (void) printf("%s;\n", R_CURL
);
1102 /* dump C initializers for the terminal type */
1104 dump_termtype(TERMTYPE
* term
)
1106 (void) printf("\t%s\n\t\t%s,\n", L_CURL
, name_initializer("alias"));
1107 (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
1109 (void) printf("\t\t%s,\n", name_initializer("bool"));
1110 (void) printf("\t\t%s,\n", name_initializer("number"));
1112 (void) printf("\t\t%s,\n", name_initializer("string"));
1115 (void) printf("#if NCURSES_XNAMES\n");
1116 (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
1117 (void) printf("\t\t%s,\t/* ...corresponding names */\n",
1118 ((NUM_BOOLEANS(term
) != BOOLCOUNT
)
1119 || (NUM_NUMBERS(term
) != NUMCOUNT
)
1120 || (NUM_STRINGS(term
) != STRCOUNT
))
1121 ? name_initializer("string_ext")
1124 (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term
));
1125 (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term
));
1126 (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term
));
1128 (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
1129 NUM_BOOLEANS(term
) - BOOLCOUNT
);
1130 (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
1131 NUM_NUMBERS(term
) - NUMCOUNT
);
1132 (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
1133 NUM_STRINGS(term
) - STRCOUNT
);
1135 (void) printf("#endif /* NCURSES_XNAMES */\n");
1136 #endif /* NCURSES_XNAMES */
1137 (void) printf("\t%s\n", R_CURL
);
1141 optarg_to_number(void)
1144 long value
= strtol(optarg
, &temp
, 0);
1146 if (temp
== 0 || temp
== optarg
|| *temp
!= 0) {
1147 fprintf(stderr
, "Expected a number, not \"%s\"\n", optarg
);
1148 ExitProgram(EXIT_FAILURE
);
1158 if ((terminal
= getenv("TERM")) == 0) {
1159 (void) fprintf(stderr
,
1160 "infocmp: environment variable TERM not set\n");
1166 /***************************************************************************
1170 ***************************************************************************/
1173 main(int argc
, char *argv
[])
1175 char *firstdir
, *restdir
;
1176 /* Avoid "local data >32k" error with mwcc */
1177 /* Also avoid overflowing smaller stacks on systems like AmigaOS */
1178 path
*tfile
= (path
*) malloc(sizeof(path
) * MAXTERMS
);
1180 bool formatted
= FALSE
;
1181 bool filecompare
= FALSE
;
1183 bool init_analyze
= FALSE
;
1184 bool suppress_untranslatable
= FALSE
;
1186 /* where is the terminfo database location going to default to? */
1187 restdir
= firstdir
= 0;
1189 while ((c
= getopt(argc
,
1191 "1A:aB:CcdEeFfGgIiLlnpqR:rs:TtuVv:w:")) != EOF
)
1203 _nc_disable_period
= TRUE
;
1204 use_extended_names(TRUE
);
1212 outform
= F_TERMCAP
;
1214 if (sortmode
== S_DEFAULT
)
1215 sortmode
= S_TERMCAP
;
1223 compare
= C_DIFFERENCE
;
1251 outform
= F_TERMINFO
;
1252 if (sortmode
== S_DEFAULT
)
1253 sortmode
= S_VARIABLE
;
1258 init_analyze
= TRUE
;
1262 outform
= F_VARIABLE
;
1263 if (sortmode
== S_DEFAULT
)
1264 sortmode
= S_VARIABLE
;
1268 outform
= F_TERMINFO
;
1297 sortmode
= S_NOSORT
;
1298 else if (*optarg
== 'i')
1299 sortmode
= S_TERMINFO
;
1300 else if (*optarg
== 'l')
1301 sortmode
= S_VARIABLE
;
1302 else if (*optarg
== 'c')
1303 sortmode
= S_TERMCAP
;
1305 (void) fprintf(stderr
,
1306 "infocmp: unknown sort mode\n");
1307 return EXIT_FAILURE
;
1317 _nc_disable_period
= FALSE
;
1318 suppress_untranslatable
= TRUE
;
1327 puts(curses_version());
1328 ExitProgram(EXIT_SUCCESS
);
1331 itrace
= optarg_to_number();
1332 set_trace_level(itrace
);
1336 mwidth
= optarg_to_number();
1343 /* by default, sort by terminfo name */
1344 if (sortmode
== S_DEFAULT
)
1345 sortmode
= S_TERMINFO
;
1347 /* set up for display */
1348 dump_init(tversion
, outform
, sortmode
, mwidth
, itrace
, formatted
);
1350 /* make sure we have at least one terminal name to work with */
1352 argv
[argc
++] = terminal_env();
1354 /* if user is after a comparison, make sure we have two entries */
1355 if (compare
!= C_DEFAULT
&& optind
>= argc
- 1)
1356 argv
[argc
++] = terminal_env();
1358 /* exactly two terminal names with no options means do -d */
1359 if (argc
- optind
== 2 && compare
== C_DEFAULT
)
1360 compare
= C_DIFFERENCE
;
1363 /* grab the entries */
1365 for (; optind
< argc
; optind
++) {
1366 if (termcount
>= MAXTERMS
) {
1367 (void) fprintf(stderr
,
1368 "infocmp: too many terminal type arguments\n");
1369 return EXIT_FAILURE
;
1371 const char *directory
= termcount
? restdir
: firstdir
;
1374 tname
[termcount
] = argv
[optind
];
1377 (void) sprintf(tfile
[termcount
], "%s/%c/%s",
1379 *argv
[optind
], argv
[optind
]);
1381 (void) fprintf(stderr
,
1382 "infocmp: reading entry %s from file %s\n",
1383 argv
[optind
], tfile
[termcount
]);
1385 status
= _nc_read_file_entry(tfile
[termcount
],
1386 &entries
[termcount
].tterm
);
1389 (void) fprintf(stderr
,
1390 "infocmp: reading entry %s from system directories %s\n",
1391 argv
[optind
], tname
[termcount
]);
1393 status
= _nc_read_entry(tname
[termcount
],
1395 &entries
[termcount
].tterm
);
1396 directory
= TERMINFO
; /* for error message */
1400 (void) fprintf(stderr
,
1401 "infocmp: couldn't open terminfo file %s.\n",
1403 return EXIT_FAILURE
;
1405 repair_acsc(&entries
[termcount
].tterm
);
1412 _nc_align_termtype(&entries
[0].tterm
, &entries
[1].tterm
);
1415 /* dump as C initializer for the terminal type */
1418 dump_termtype(&entries
[0].tterm
);
1420 dump_initializers(&entries
[0].tterm
);
1421 ExitProgram(EXIT_SUCCESS
);
1424 /* analyze the init strings */
1427 #define CUR entries[0].tterm.
1428 analyze_string("is1", init_1string
, &entries
[0].tterm
);
1429 analyze_string("is2", init_2string
, &entries
[0].tterm
);
1430 analyze_string("is3", init_3string
, &entries
[0].tterm
);
1431 analyze_string("rs1", reset_1string
, &entries
[0].tterm
);
1432 analyze_string("rs2", reset_2string
, &entries
[0].tterm
);
1433 analyze_string("rs3", reset_3string
, &entries
[0].tterm
);
1434 analyze_string("smcup", enter_ca_mode
, &entries
[0].tterm
);
1435 analyze_string("rmcup", exit_ca_mode
, &entries
[0].tterm
);
1437 ExitProgram(EXIT_SUCCESS
);
1441 * Here's where the real work gets done
1446 (void) fprintf(stderr
,
1447 "infocmp: about to dump %s\n",
1449 (void) printf("#\tReconstructed via infocmp from file: %s\n",
1451 len
= dump_entry(&entries
[0].tterm
,
1452 suppress_untranslatable
,
1459 (void) fprintf(stderr
, "infocmp: length %d\n", len
);
1464 (void) fprintf(stderr
, "infocmp: dumping differences\n");
1465 (void) printf("comparing %s to %s.\n", tname
[0], tname
[1]);
1466 compare_entry(compare_predicate
, &entries
->tterm
, quiet
);
1471 (void) fprintf(stderr
,
1472 "infocmp: dumping common capabilities\n");
1473 (void) printf("comparing %s to %s.\n", tname
[0], tname
[1]);
1474 compare_entry(compare_predicate
, &entries
->tterm
, quiet
);
1479 (void) fprintf(stderr
,
1480 "infocmp: dumping differences\n");
1481 (void) printf("comparing %s to %s.\n", tname
[0], tname
[1]);
1482 compare_entry(compare_predicate
, &entries
->tterm
, quiet
);
1487 (void) fprintf(stderr
, "infocmp: dumping use entry\n");
1488 len
= dump_entry(&entries
[0].tterm
,
1489 suppress_untranslatable
,
1494 for (i
= 1; i
< termcount
; i
++)
1495 len
+= dump_uses(tname
[i
], !(outform
== F_TERMCAP
1496 || outform
== F_TCONVERR
));
1499 (void) fprintf(stderr
, "infocmp: length %d\n", len
);
1502 } else if (compare
== C_USEALL
)
1503 (void) fprintf(stderr
, "Sorry, -u doesn't work with -F\n");
1504 else if (compare
== C_DEFAULT
)
1505 (void) fprintf(stderr
, "Use `tic -[CI] <file>' for this.\n");
1506 else if (argc
- optind
!= 2)
1507 (void) fprintf(stderr
,
1508 "File comparison needs exactly two file arguments.\n");
1510 file_comparison(argc
- optind
, argv
+ optind
);
1512 ExitProgram(EXIT_SUCCESS
);
1515 /* infocmp.c ends here */