1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 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.96 2008/01/19 21:08:07 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 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 */
90 static void ExitProgram(int code) GCC_NORETURN;
91 /* prototype is to get gcc to accept the noreturn attribute */
95 while (termcount-- > 0)
96 _nc_free_termtype(&entries[termcount].tterm);
97 _nc_leaks_dump_entry();
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(PredIdx 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(unsigned type, PredIdx 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((PredIdx) 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(PredType type, PredIdx 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 controls widely supported by emulators */
478 {"\033=", "DECPAM"}, /* application keypad mode */
479 {"\033>", "DECPNM"}, /* normal keypad mode */
480 {"\033<", "DECANSI"}, /* enter ANSI mode */
481 {"\033[!p", "DECSTR"}, /* soft reset */
482 {"\033 F", "S7C1T"}, /* 7-bit controls */
484 {(char *) 0, (char *) 0}
487 static const assoc std_modes[] =
488 /* ECMA \E[ ... [hl] modes recognized by many emulators */
490 {"2", "AM"}, /* keyboard action mode */
491 {"4", "IRM"}, /* insert/replace mode */
492 {"12", "SRM"}, /* send/receive mode */
493 {"20", "LNM"}, /* linefeed mode */
494 {(char *) 0, (char *) 0}
497 static const assoc private_modes[] =
498 /* DEC \E[ ... [hl] modes recognized by many emulators */
500 {"1", "CKM"}, /* application cursor keys */
501 {"2", "ANM"}, /* set VT52 mode */
502 {"3", "COLM"}, /* 132-column mode */
503 {"4", "SCLM"}, /* smooth scroll */
504 {"5", "SCNM"}, /* reverse video mode */
505 {"6", "OM"}, /* origin mode */
506 {"7", "AWM"}, /* wraparound mode */
507 {"8", "ARM"}, /* auto-repeat mode */
508 {(char *) 0, (char *) 0}
511 static const assoc ecma_highlights[] =
512 /* recognize ECMA attribute sequences */
514 {"0", "NORMAL"}, /* normal */
515 {"1", "+BOLD"}, /* bold on */
516 {"2", "+DIM"}, /* dim on */
517 {"3", "+ITALIC"}, /* italic on */
518 {"4", "+UNDERLINE"}, /* underline on */
519 {"5", "+BLINK"}, /* blink on */
520 {"6", "+FASTBLINK"}, /* fastblink on */
521 {"7", "+REVERSE"}, /* reverse on */
522 {"8", "+INVISIBLE"}, /* invisible on */
523 {"9", "+DELETED"}, /* deleted on */
524 {"10", "MAIN-FONT"}, /* select primary font */
525 {"11", "ALT-FONT-1"}, /* select alternate font 1 */
526 {"12", "ALT-FONT-2"}, /* select alternate font 2 */
527 {"13", "ALT-FONT-3"}, /* select alternate font 3 */
528 {"14", "ALT-FONT-4"}, /* select alternate font 4 */
529 {"15", "ALT-FONT-5"}, /* select alternate font 5 */
530 {"16", "ALT-FONT-6"}, /* select alternate font 6 */
531 {"17", "ALT-FONT-7"}, /* select alternate font 7 */
532 {"18", "ALT-FONT-1"}, /* select alternate font 1 */
533 {"19", "ALT-FONT-1"}, /* select alternate font 1 */
534 {"20", "FRAKTUR"}, /* Fraktur font */
535 {"21", "DOUBLEUNDER"}, /* double underline */
536 {"22", "-DIM"}, /* dim off */
537 {"23", "-ITALIC"}, /* italic off */
538 {"24", "-UNDERLINE"}, /* underline off */
539 {"25", "-BLINK"}, /* blink off */
540 {"26", "-FASTBLINK"}, /* fastblink off */
541 {"27", "-REVERSE"}, /* reverse off */
542 {"28", "-INVISIBLE"}, /* invisible off */
543 {"29", "-DELETED"}, /* deleted off */
544 {(char *) 0, (char *) 0}
548 skip_csi(const char *cap)
551 if (cap[0] == '\033' && cap[1] == '[')
553 else if (UChar(cap[0]) == 0233)
559 same_param(const char *table, const char *param, unsigned length)
562 if (strncmp(table, param, length) == 0) {
563 result = !isdigit(UChar(param[length]));
569 lookup_params(const assoc * table, char *dst, char *src)
571 const char *ep = strtok(src, ";");
577 for (ap = table; ap->from; ap++) {
578 size_t tlen = strlen(ap->from);
580 if (same_param(ap->from, ep, tlen)) {
581 (void) strcat(dst, ap->to);
588 (void) strcat(dst, ep);
589 (void) strcat(dst, ";");
591 ((ep = strtok((char *) 0, ";")));
593 dst[strlen(dst) - 1] = '\0';
599 analyze_string(const char *name, const char *cap, TERMTYPE *tp)
601 char buf[MAX_TERMINFO_LENGTH];
602 char buf2[MAX_TERMINFO_LENGTH];
605 int tp_lines = tp->Numbers[2];
607 if (cap == ABSENT_STRING || cap == CANCELLED_STRING)
609 (void) printf("%s: ", name);
612 for (sp = cap; *sp; sp++) {
617 const char *expansion = 0;
618 char buf3[MAX_TERMINFO_LENGTH];
620 /* first, check other capabilities in this entry */
621 for (i = 0; i < STRCOUNT; i++) {
622 char *cp = tp->Strings[i];
624 /* don't use soft-key capabilities */
625 if (strnames[i][0] == 'k' && strnames[i][0] == 'f')
628 if (cp != ABSENT_STRING && cp != CANCELLED_STRING && cp[0] && cp
631 (void) strncpy(buf2, sp, len);
634 if (_nc_capcmp(cp, buf2))
637 #define ISRS(s) (!strncmp((s), "is", 2) || !strncmp((s), "rs", 2))
639 * Theoretically we just passed the test for translation
640 * (equality once the padding is stripped). However, there
641 * are a few more hoops that need to be jumped so that
642 * identical pairs of initialization and reset strings
643 * don't just refer to each other.
645 if (ISRS(name) || ISRS(strnames[i]))
650 expansion = strnames[i];
655 /* now check the standard capabilities */
658 for (ap = std_caps; ap->from; ap++) {
659 size_t adj = csi ? 2 : 0;
661 len = strlen(ap->from);
662 if (csi && skip_csi(ap->from) != csi)
665 && strncmp(ap->from + adj, sp + csi, len - adj) == 0) {
674 /* now check for standard-mode sequences */
676 && (csi = skip_csi(sp)) != 0
677 && (len = strspn(sp + csi, "0123456789;"))
678 && (next = csi + len)
679 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
681 (void) strcpy(buf2, (sp[next] == 'h') ? "ECMA+" : "ECMA-");
682 (void) strncpy(buf3, sp + csi, len);
686 expansion = lookup_params(std_modes, buf2, buf3);
689 /* now check for private-mode sequences */
691 && (csi = skip_csi(sp)) != 0
693 && (len = strspn(sp + csi + 1, "0123456789;"))
694 && (next = csi + 1 + len)
695 && ((sp[next] == 'h') || (sp[next] == 'l'))) {
697 (void) strcpy(buf2, (sp[next] == 'h') ? "DEC+" : "DEC-");
698 (void) strncpy(buf3, sp + csi + 1, len);
702 expansion = lookup_params(private_modes, buf2, buf3);
705 /* now check for ECMA highlight sequences */
707 && (csi = skip_csi(sp)) != 0
708 && (len = strspn(sp + csi, "0123456789;")) != 0
709 && (next = csi + len)
710 && sp[next] == 'm') {
712 (void) strcpy(buf2, "SGR:");
713 (void) strncpy(buf3, sp + csi, len);
717 expansion = lookup_params(ecma_highlights, buf2, buf3);
721 && (csi = skip_csi(sp)) != 0
724 (void) strcpy(buf2, "SGR:");
725 strcat(buf2, ecma_highlights[0].to);
729 /* now check for scroll region reset */
731 && (csi = skip_csi(sp)) != 0) {
732 if (sp[csi] == 'r') {
736 (void) sprintf(buf2, "1;%dr", tp_lines);
738 if (strncmp(buf2, sp + csi, len) == 0)
744 /* now check for home-down */
746 && (csi = skip_csi(sp)) != 0) {
747 (void) sprintf(buf2, "%d;1H", tp_lines);
749 if (strncmp(buf2, sp + csi, len) == 0) {
752 (void) sprintf(buf2, "%dH", tp_lines);
754 if (strncmp(buf2, sp + csi, len) == 0) {
761 /* now look at the expansion we got, if any */
763 (void) sprintf(buf + strlen(buf), "{%s}", expansion);
767 /* couldn't match anything */
770 (void) strcat(buf, TIC_EXPAND(buf2));
773 (void) printf("%s\n", buf);
776 /***************************************************************************
780 ***************************************************************************/
783 file_comparison(int argc, char *argv[])
786 /* someday we may allow comparisons on more files */
788 ENTRY *heads[MAXCOMPARE];
792 dump_init((char *) 0, F_LITERAL, S_TERMINFO, 0, itrace, FALSE);
794 for (n = 0; n < argc && n < MAXCOMPARE; n++) {
795 if (freopen(argv[n], "r", stdin) == 0)
796 _nc_err_abort("Can't open %s", argv[n]);
798 _nc_head = _nc_tail = 0;
800 /* parse entries out of the source file */
801 _nc_set_source(argv[n]);
802 _nc_read_entry_source(stdin, NULL, TRUE, literal, NULLHOOK);
805 (void) fprintf(stderr, "Resolving file %d...\n", n - 0);
807 /* maybe do use resolution */
808 if (!_nc_resolve_uses2(!limited, literal)) {
809 (void) fprintf(stderr,
810 "There are unresolved use entries in %s:\n",
814 (void) fputs(qp->tterm.term_names, stderr);
815 (void) fputc('\n', stderr);
818 ExitProgram(EXIT_FAILURE);
821 heads[filecount] = _nc_head;
825 /* OK, all entries are in core. Ready to do the comparison */
827 (void) fprintf(stderr, "Entries are now in core...\n");
829 /* The entry-matching loop. Sigh, this is intrinsically quadratic. */
830 for (qp = heads[0]; qp; qp = qp->next) {
831 for (rp = heads[1]; rp; rp = rp->next)
832 if (_nc_entry_match(qp->tterm.term_names, rp->tterm.term_names)) {
833 if (qp->ncrosslinks < MAX_CROSSLINKS)
834 qp->crosslinks[qp->ncrosslinks] = rp;
837 if (rp->ncrosslinks < MAX_CROSSLINKS)
838 rp->crosslinks[rp->ncrosslinks] = qp;
843 /* now we have two circular lists with crosslinks */
845 (void) fprintf(stderr, "Name matches are done...\n");
847 for (qp = heads[0]; qp; qp = qp->next) {
848 if (qp->ncrosslinks > 1) {
849 (void) fprintf(stderr,
850 "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
851 _nc_first_name(qp->tterm.term_names),
855 for (i = 0; i < qp->ncrosslinks; i++)
856 (void) fprintf(stderr,
858 _nc_first_name((qp->crosslinks[i])->tterm.term_names));
862 for (rp = heads[1]; rp; rp = rp->next) {
863 if (rp->ncrosslinks > 1) {
864 (void) fprintf(stderr,
865 "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
866 _nc_first_name(rp->tterm.term_names),
870 for (i = 0; i < rp->ncrosslinks; i++)
871 (void) fprintf(stderr,
873 _nc_first_name((rp->crosslinks[i])->tterm.term_names));
877 (void) printf("In file 1 (%s) only:\n", argv[0]);
878 for (qp = heads[0]; qp; qp = qp->next)
879 if (qp->ncrosslinks == 0)
880 (void) printf("\t%s\n",
881 _nc_first_name(qp->tterm.term_names));
883 (void) printf("In file 2 (%s) only:\n", argv[1]);
884 for (rp = heads[1]; rp; rp = rp->next)
885 if (rp->ncrosslinks == 0)
886 (void) printf("\t%s\n",
887 _nc_first_name(rp->tterm.term_names));
889 (void) printf("The following entries are equivalent:\n");
890 for (qp = heads[0]; qp; qp = qp->next) {
891 rp = qp->crosslinks[0];
893 if (qp->ncrosslinks == 1) {
894 rp = qp->crosslinks[0];
896 repair_acsc(&qp->tterm);
897 repair_acsc(&rp->tterm);
899 _nc_align_termtype(&qp->tterm, &rp->tterm);
901 if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
902 char name1[NAMESIZE], name2[NAMESIZE];
904 (void) canonical_name(qp->tterm.term_names, name1);
905 (void) canonical_name(rp->tterm.term_names, name2);
907 (void) printf("%s = %s\n", name1, name2);
912 (void) printf("Differing entries:\n");
914 for (qp = heads[0]; qp; qp = qp->next) {
916 if (qp->ncrosslinks == 1) {
917 rp = qp->crosslinks[0];
919 /* sorry - we have to do this on each pass */
920 _nc_align_termtype(&qp->tterm, &rp->tterm);
922 if (!(entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp))) {
923 char name1[NAMESIZE], name2[NAMESIZE];
928 (void) canonical_name(qp->tterm.term_names, name1);
929 (void) canonical_name(rp->tterm.term_names, name2);
934 (void) fprintf(stderr,
935 "%s: dumping differences\n",
937 (void) printf("comparing %s to %s.\n", name1, name2);
938 compare_entry(compare_predicate, &entries->tterm, quiet);
943 (void) fprintf(stderr,
944 "%s: dumping common capabilities\n",
946 (void) printf("comparing %s to %s.\n", name1, name2);
947 compare_entry(compare_predicate, &entries->tterm, quiet);
952 (void) fprintf(stderr,
953 "%s: dumping differences\n",
955 (void) printf("comparing %s to %s.\n", name1, name2);
956 compare_entry(compare_predicate, &entries->tterm, quiet);
968 static const char *tbl[] =
970 "Usage: infocmp [options] [-A directory] [-B directory] [termname...]"
973 ," -1 print single-column"
974 ," -C use termcap-names"
975 ," -F compare terminfo-files"
976 ," -I use terminfo-names"
977 ," -L use long names"
978 ," -R subset (see manpage)"
979 ," -T eliminate size limits (test)"
980 ," -U eliminate post-processing of entries"
983 ," -a with -F, list commented-out caps"
985 ," -c list common capabilities"
986 ," -d list different capabilities"
987 ," -e format output for C initializer"
988 ," -E format output as C tables"
989 ," -f with -1, format complex strings"
990 ," -G format %{number} to %'char'"
991 ," -g format %'char' to %{number}"
992 ," -i analyze initialization/reset"
993 ," -l output terminfo names"
994 ," -n list capabilities in neither"
995 ," -p ignore padding specifiers"
996 ," -q brief listing, removes headers"
997 ," -r with -C, output in termcap form"
998 ," -r with -F, resolve use-references"
999 ," -s [d|i|l|c] sort fields"
1001 ," -t suppress commented-out capabilities"
1003 ," -u produce source with 'use='"
1004 ," -v number (verbose)"
1005 ," -w number (width)"
1007 ," -x treat unknown capabilities as user-defined"
1010 const size_t first = 3;
1011 const size_t last = SIZEOF(tbl);
1012 const size_t left = (last - first + 1) / 2 + first;
1015 for (n = 0; n < left; n++) {
1016 size_t m = (n < first) ? last : n + left - first;
1018 fprintf(stderr, "%-40.40s%s\n", tbl[n], tbl[m]);
1020 fprintf(stderr, "%s\n", tbl[n]);
1022 ExitProgram(EXIT_FAILURE);
1026 any_initializer(const char *fmt, const char *type)
1028 static char *initializer;
1031 if (initializer == 0)
1032 initializer = (char *) malloc(strlen(entries->tterm.term_names) +
1033 strlen(type) + strlen(fmt));
1035 (void) strcpy(initializer, entries->tterm.term_names);
1036 for (s = initializer; *s != 0 && *s != '|'; s++) {
1037 if (!isalnum(UChar(*s)))
1041 (void) sprintf(s, fmt, type);
1046 name_initializer(const char *type)
1048 return any_initializer("_%s_data", type);
1052 string_variable(const char *type)
1054 return any_initializer("_s_%s", type);
1057 /* dump C initializers for the terminal type */
1059 dump_initializers(TERMTYPE *term)
1063 const char *str = 0;
1065 printf("\nstatic char %s[] = \"%s\";\n\n",
1066 name_initializer("alias"), entries->tterm.term_names);
1068 for_each_string(n, term) {
1069 char buf[MAX_STRING], *sp, *tp;
1071 if (VALID_STRING(term->Strings[n])) {
1074 for (sp = term->Strings[n];
1075 *sp != 0 && (tp - buf) < MAX_STRING - 6;
1077 if (isascii(UChar(*sp))
1078 && isprint(UChar(*sp))
1083 (void) sprintf(tp, "\\%03o", UChar(*sp));
1089 (void) printf("static char %-20s[] = %s;\n",
1090 string_variable(ExtStrname(term, n, strnames)), buf);
1095 (void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
1097 for_each_boolean(n, term) {
1098 switch ((int) (term->Booleans[n])) {
1107 case ABSENT_BOOLEAN:
1108 str = "ABSENT_BOOLEAN";
1111 case CANCELLED_BOOLEAN:
1112 str = "CANCELLED_BOOLEAN";
1115 (void) printf("\t/* %3u: %-8s */\t%s,\n",
1116 n, ExtBoolname(term, n, boolnames), str);
1118 (void) printf("%s;\n", R_CURL);
1120 (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
1122 for_each_number(n, term) {
1124 switch (term->Numbers[n]) {
1125 case ABSENT_NUMERIC:
1126 str = "ABSENT_NUMERIC";
1128 case CANCELLED_NUMERIC:
1129 str = "CANCELLED_NUMERIC";
1132 sprintf(buf, "%d", term->Numbers[n]);
1136 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1137 ExtNumname(term, n, numnames), str);
1139 (void) printf("%s;\n", R_CURL);
1141 size = (sizeof(TERMTYPE)
1142 + (NUM_BOOLEANS(term) * sizeof(term->Booleans[0]))
1143 + (NUM_NUMBERS(term) * sizeof(term->Numbers[0])));
1145 (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
1147 for_each_string(n, term) {
1149 if (term->Strings[n] == ABSENT_STRING)
1150 str = "ABSENT_STRING";
1151 else if (term->Strings[n] == CANCELLED_STRING)
1152 str = "CANCELLED_STRING";
1154 str = string_variable(ExtStrname(term, n, strnames));
1156 (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
1157 ExtStrname(term, n, strnames), str);
1159 (void) printf("%s;\n", R_CURL);
1162 if ((NUM_BOOLEANS(term) != BOOLCOUNT)
1163 || (NUM_NUMBERS(term) != NUMCOUNT)
1164 || (NUM_STRINGS(term) != STRCOUNT)) {
1165 (void) printf("static char * %s[] = %s\n",
1166 name_initializer("string_ext"), L_CURL);
1167 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
1168 (void) printf("\t/* %3u: bool */\t\"%s\",\n",
1169 n, ExtBoolname(term, n, boolnames));
1171 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
1172 (void) printf("\t/* %3u: num */\t\"%s\",\n",
1173 n, ExtNumname(term, n, numnames));
1175 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
1176 (void) printf("\t/* %3u: str */\t\"%s\",\n",
1177 n, ExtStrname(term, n, strnames));
1179 (void) printf("%s;\n", R_CURL);
1184 /* dump C initializers for the terminal type */
1186 dump_termtype(TERMTYPE *term)
1188 (void) printf("\t%s\n\t\t%s,\n", L_CURL, name_initializer("alias"));
1189 (void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
1191 (void) printf("\t\t%s,\n", name_initializer("bool"));
1192 (void) printf("\t\t%s,\n", name_initializer("number"));
1194 (void) printf("\t\t%s,\n", name_initializer("string"));
1197 (void) printf("#if NCURSES_XNAMES\n");
1198 (void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
1199 (void) printf("\t\t%s,\t/* ...corresponding names */\n",
1200 ((NUM_BOOLEANS(term) != BOOLCOUNT)
1201 || (NUM_NUMBERS(term) != NUMCOUNT)
1202 || (NUM_STRINGS(term) != STRCOUNT))
1203 ? name_initializer("string_ext")
1206 (void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
1207 (void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
1208 (void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
1210 (void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
1211 NUM_BOOLEANS(term) - BOOLCOUNT);
1212 (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
1213 NUM_NUMBERS(term) - NUMCOUNT);
1214 (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
1215 NUM_STRINGS(term) - STRCOUNT);
1217 (void) printf("#endif /* NCURSES_XNAMES */\n");
1220 #endif /* NCURSES_XNAMES */
1221 (void) printf("\t%s\n", R_CURL);
1225 optarg_to_number(void)
1228 long value = strtol(optarg, &temp, 0);
1230 if (temp == 0 || temp == optarg || *temp != 0) {
1231 fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
1232 ExitProgram(EXIT_FAILURE);
1242 if ((terminal = getenv("TERM")) == 0) {
1243 (void) fprintf(stderr,
1244 "%s: environment variable TERM not set\n",
1251 /***************************************************************************
1255 ***************************************************************************/
1258 main(int argc, char *argv[])
1260 /* Avoid "local data >32k" error with mwcc */
1261 /* Also avoid overflowing smaller stacks on systems like AmigaOS */
1268 char *firstdir, *restdir;
1270 bool formatted = FALSE;
1271 bool filecompare = FALSE;
1273 bool init_analyze = FALSE;
1274 bool suppress_untranslatable = FALSE;
1276 /* where is the terminfo database location going to default to? */
1277 restdir = firstdir = 0;
1280 use_extended_names(FALSE);
1283 _nc_progname = _nc_rootname(argv[0]);
1285 /* make sure we have enough space to add two terminal entries */
1286 myargv = typeCalloc(char *, argc + 3);
1287 memcpy(myargv, argv, sizeof(char *) * argc);
1290 while ((c = getopt(argc,
1292 "1A:aB:CcdEeFfGgIiLlnpqR:rs:TtUuVv:w:x")) != -1) {
1304 _nc_disable_period = TRUE;
1305 use_extended_names(TRUE);
1313 outform = F_TERMCAP;
1315 if (sortmode == S_DEFAULT)
1316 sortmode = S_TERMCAP;
1324 compare = C_DIFFERENCE;
1352 outform = F_TERMINFO;
1353 if (sortmode == S_DEFAULT)
1354 sortmode = S_VARIABLE;
1359 init_analyze = TRUE;
1363 outform = F_VARIABLE;
1364 if (sortmode == S_DEFAULT)
1365 sortmode = S_VARIABLE;
1369 outform = F_TERMINFO;
1397 sortmode = S_NOSORT;
1398 else if (*optarg == 'i')
1399 sortmode = S_TERMINFO;
1400 else if (*optarg == 'l')
1401 sortmode = S_VARIABLE;
1402 else if (*optarg == 'c')
1403 sortmode = S_TERMCAP;
1405 (void) fprintf(stderr,
1406 "%s: unknown sort mode\n",
1408 ExitProgram(EXIT_FAILURE);
1418 _nc_disable_period = FALSE;
1419 suppress_untranslatable = TRUE;
1432 puts(curses_version());
1433 ExitProgram(EXIT_SUCCESS);
1436 itrace = optarg_to_number();
1437 set_trace_level(itrace);
1441 mwidth = optarg_to_number();
1446 use_extended_names(TRUE);
1455 maxterms = (argc + 2 - optind);
1456 tfile = typeMalloc(path, maxterms);
1457 tname = typeCalloc(char *, maxterms);
1458 entries = typeCalloc(ENTRY, maxterms);
1463 fprintf(stderr, "%s: not enough memory\n", _nc_progname);
1464 ExitProgram(EXIT_FAILURE);
1467 /* by default, sort by terminfo name */
1468 if (sortmode == S_DEFAULT)
1469 sortmode = S_TERMINFO;
1471 /* set up for display */
1472 dump_init(tversion, outform, sortmode, mwidth, itrace, formatted);
1474 /* make sure we have at least one terminal name to work with */
1476 argv[argc++] = terminal_env();
1478 /* if user is after a comparison, make sure we have two entries */
1479 if (compare != C_DEFAULT && optind >= argc - 1)
1480 argv[argc++] = terminal_env();
1482 /* exactly two terminal names with no options means do -d */
1483 if (argc - optind == 2 && compare == C_DEFAULT)
1484 compare = C_DIFFERENCE;
1487 /* grab the entries */
1489 for (; optind < argc; optind++) {
1490 const char *directory = termcount ? restdir : firstdir;
1493 tname[termcount] = argv[optind];
1497 #if MIXEDCASE_FILENAMES
1498 #define LEAF_FMT "%c"
1500 #define LEAF_FMT "%02x"
1502 (void) sprintf(tfile[termcount], "%s/" LEAF_FMT "/%s",
1504 UChar(*argv[optind]), argv[optind]);
1506 (void) fprintf(stderr,
1507 "%s: reading entry %s from file %s\n",
1509 argv[optind], tfile[termcount]);
1511 status = _nc_read_file_entry(tfile[termcount],
1512 &entries[termcount].tterm);
1514 (void) fprintf(stderr, "%s: terminfo files not supported\n",
1516 ExitProgram(EXIT_FAILURE);
1520 (void) fprintf(stderr,
1521 "%s: reading entry %s from database\n",
1525 status = _nc_read_entry(tname[termcount],
1527 &entries[termcount].tterm);
1528 directory = TERMINFO; /* for error message */
1532 (void) fprintf(stderr,
1533 "%s: couldn't open terminfo file %s.\n",
1536 ExitProgram(EXIT_FAILURE);
1538 repair_acsc(&entries[termcount].tterm);
1544 _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
1547 /* dump as C initializer for the terminal type */
1550 dump_termtype(&entries[0].tterm);
1552 dump_initializers(&entries[0].tterm);
1555 /* analyze the init strings */
1556 else if (init_analyze) {
1558 #define CUR entries[0].tterm.
1559 analyze_string("is1", init_1string, &entries[0].tterm);
1560 analyze_string("is2", init_2string, &entries[0].tterm);
1561 analyze_string("is3", init_3string, &entries[0].tterm);
1562 analyze_string("rs1", reset_1string, &entries[0].tterm);
1563 analyze_string("rs2", reset_2string, &entries[0].tterm);
1564 analyze_string("rs3", reset_3string, &entries[0].tterm);
1565 analyze_string("smcup", enter_ca_mode, &entries[0].tterm);
1566 analyze_string("rmcup", exit_ca_mode, &entries[0].tterm);
1571 * Here's where the real work gets done
1576 (void) fprintf(stderr,
1577 "%s: about to dump %s\n",
1580 (void) printf("#\tReconstructed via infocmp from file: %s\n",
1582 dump_entry(&entries[0].tterm,
1583 suppress_untranslatable,
1589 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1594 (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
1595 (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1596 compare_entry(compare_predicate, &entries->tterm, quiet);
1601 (void) fprintf(stderr,
1602 "%s: dumping common capabilities\n",
1604 (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1605 compare_entry(compare_predicate, &entries->tterm, quiet);
1610 (void) fprintf(stderr,
1611 "%s: dumping differences\n",
1613 (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
1614 compare_entry(compare_predicate, &entries->tterm, quiet);
1619 (void) fprintf(stderr, "%s: dumping use entry\n", _nc_progname);
1620 dump_entry(&entries[0].tterm,
1621 suppress_untranslatable,
1625 for (i = 1; i < termcount; i++)
1626 dump_uses(tname[i], !(outform == F_TERMCAP
1627 || outform == F_TCONVERR));
1630 (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
1634 } else if (compare == C_USEALL)
1635 (void) fprintf(stderr, "Sorry, -u doesn't work with -F\n");
1636 else if (compare == C_DEFAULT)
1637 (void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
1638 else if (argc - optind != 2)
1639 (void) fprintf(stderr,
1640 "File comparison needs exactly two file arguments.\n");
1642 file_comparison(argc - optind, argv + optind);
1649 ExitProgram(EXIT_SUCCESS);
1652 /* infocmp.c ends here */