/****************************************************************************
- * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. *
+ * Copyright (c) 1998-2002,2003 Free Software Foundation, Inc. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
#include <term_entry.h>
#include <dump_entry.h>
-MODULE_ID("$Id: infocmp.c,v 1.54 2000/03/19 02:56:14 tom Exp $")
+MODULE_ID("$Id: infocmp.c,v 1.71 2003/10/18 18:01:54 tom Exp $")
#define L_CURL "{"
#define R_CURL "}"
static int itrace; /* trace flag for debugging */
static int mwidth = 60;
static int numbers = 0; /* format "%'char'" to/from "%{number}" */
-static int outform = F_TERMINFO;/* output format */
+static int outform = F_TERMINFO; /* output format */
static int sortmode; /* sort_mode */
/* main comparison mode */
static void
ExitProgram(int code) GCC_NORETURN;
/* prototype is to get gcc to accept the noreturn attribute */
-static void
-ExitProgram(int code)
+ static void
+ ExitProgram(int code)
{
while (termcount-- > 0)
_nc_free_termtype(&entries[termcount].tterm);
***************************************************************************/
static int
-capcmp(int idx, const char *s, const char *t)
+capcmp(unsigned idx, const char *s, const char *t)
/* capability comparison function */
{
if (!VALID_STRING(s) && !VALID_STRING(t))
entryeq(TERMTYPE * t1, TERMTYPE * t2)
/* are two entries equivalent? */
{
- int i;
+ unsigned i;
for (i = 0; i < NUM_BOOLEANS(t1); i++)
if (t1->Booleans[i] != t2->Booleans[i])
#define TIC_EXPAND(result) _nc_tic_expand(result, outform==F_TERMINFO, numbers)
static void
-print_uses(ENTRY * ep, FILE * fp)
+print_uses(ENTRY * ep, FILE *fp)
/* print an entry's use references */
{
int i;
case C_DIFFERENCE:
if (!(b1 == ABSENT_BOOLEAN && b2 == ABSENT_BOOLEAN) && b1 != b2)
(void) printf("\t%s: %s%s%s.\n",
- name,
- dump_boolean(b1),
- bool_sep,
- dump_boolean(b2));
+ name,
+ dump_boolean(b1),
+ bool_sep,
+ dump_boolean(b2));
break;
case C_COMMON:
/* someday we may allow comparisons on more files */
int filecount = 0;
ENTRY *heads[MAXCOMPARE];
- ENTRY *tails[MAXCOMPARE];
ENTRY *qp, *rp;
int i, n;
/* maybe do use resolution */
if (!_nc_resolve_uses(!limited)) {
(void) fprintf(stderr,
- "There are unresolved use entries in %s:\n",
- argv[n]);
+ "There are unresolved use entries in %s:\n",
+ argv[n]);
for_entry_list(qp) {
if (qp->nuses) {
(void) fputs(qp->tterm.term_names, stderr);
(void) fputc('\n', stderr);
}
}
- exit(EXIT_FAILURE);
+ ExitProgram(EXIT_FAILURE);
}
heads[filecount] = _nc_head;
- tails[filecount] = _nc_tail;
filecount++;
}
for (qp = heads[0]; qp; qp = qp->next) {
if (qp->ncrosslinks > 1) {
(void) fprintf(stderr,
- "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
- _nc_first_name(qp->tterm.term_names),
- argv[0],
- qp->ncrosslinks,
- argv[1]);
+ "%s in file 1 (%s) has %d matches in file 2 (%s):\n",
+ _nc_first_name(qp->tterm.term_names),
+ argv[0],
+ qp->ncrosslinks,
+ argv[1]);
for (i = 0; i < qp->ncrosslinks; i++)
(void) fprintf(stderr,
- "\t%s\n",
- _nc_first_name((qp->crosslinks[i])->tterm.term_names));
+ "\t%s\n",
+ _nc_first_name((qp->crosslinks[i])->tterm.term_names));
}
}
for (rp = heads[1]; rp; rp = rp->next) {
if (rp->ncrosslinks > 1) {
(void) fprintf(stderr,
- "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
- _nc_first_name(rp->tterm.term_names),
- argv[1],
- rp->ncrosslinks,
- argv[0]);
+ "%s in file 2 (%s) has %d matches in file 1 (%s):\n",
+ _nc_first_name(rp->tterm.term_names),
+ argv[1],
+ rp->ncrosslinks,
+ argv[0]);
for (i = 0; i < rp->ncrosslinks; i++)
(void) fprintf(stderr,
- "\t%s\n",
- _nc_first_name((rp->crosslinks[i])->tterm.term_names));
+ "\t%s\n",
+ _nc_first_name((rp->crosslinks[i])->tterm.term_names));
}
}
for (qp = heads[0]; qp; qp = qp->next)
if (qp->ncrosslinks == 0)
(void) printf("\t%s\n",
- _nc_first_name(qp->tterm.term_names));
+ _nc_first_name(qp->tterm.term_names));
(void) printf("In file 2 (%s) only:\n", argv[1]);
for (rp = heads[1]; rp; rp = rp->next)
if (rp->ncrosslinks == 0)
(void) printf("\t%s\n",
- _nc_first_name(rp->tterm.term_names));
+ _nc_first_name(rp->tterm.term_names));
(void) printf("The following entries are equivalent:\n");
for (qp = heads[0]; qp; qp = qp->next) {
case C_DIFFERENCE:
if (itrace)
(void) fprintf(stderr,
- "infocmp: dumping differences\n");
+ "infocmp: dumping differences\n");
(void) printf("comparing %s to %s.\n", name1, name2);
compare_entry(compare_predicate, &entries->tterm, quiet);
break;
case C_COMMON:
if (itrace)
(void) fprintf(stderr,
- "infocmp: dumping common capabilities\n");
+ "infocmp: dumping common capabilities\n");
(void) printf("comparing %s to %s.\n", name1, name2);
compare_entry(compare_predicate, &entries->tterm, quiet);
break;
case C_NAND:
if (itrace)
(void) fprintf(stderr,
- "infocmp: dumping differences\n");
+ "infocmp: dumping differences\n");
(void) printf("comparing %s to %s.\n", name1, name2);
compare_entry(compare_predicate, &entries->tterm, quiet);
break;
," -r with -C, output in termcap form"
," -r with -F, resolve use-references"
," -s [d|i|l|c] sort fields"
+#if NCURSES_XNAMES
+ ," -t suppress commented-out capabilities"
+#endif
," -u produce source with 'use='"
," -v number (verbose)"
," -w number (width)"
};
const size_t first = 3;
- const size_t last = sizeof(tbl) / sizeof(tbl[0]);
+ const size_t last = SIZEOF(tbl);
const size_t left = (last - first + 1) / 2 + first;
size_t n;
else
fprintf(stderr, "%s\n", tbl[n]);
}
- exit(EXIT_FAILURE);
+ ExitProgram(EXIT_FAILURE);
}
static char *
-name_initializer(const char *type)
+any_initializer(const char *fmt, const char *type)
{
static char *initializer;
char *s;
if (initializer == 0)
- initializer = (char *) malloc(strlen(entries->tterm.term_names) + 20);
+ initializer = (char *) malloc(strlen(entries->tterm.term_names) +
+ strlen(type) + strlen(fmt));
- (void) sprintf(initializer, "%s_data_%s", type, entries->tterm.term_names);
+ (void) strcpy(initializer, entries->tterm.term_names);
for (s = initializer; *s != 0 && *s != '|'; s++) {
- if (!isalnum(*s))
+ if (!isalnum(UChar(*s)))
*s = '_';
}
*s = 0;
+ (void) sprintf(s, fmt, type);
return initializer;
}
+static char *
+name_initializer(const char *type)
+{
+ return any_initializer("_%s_data", type);
+}
+
+static char *
+string_variable(const char *type)
+{
+ return any_initializer("_s_%s", type);
+}
+
/* dump C initializers for the terminal type */
static void
dump_initializers(TERMTYPE * term)
{
- int n;
- const char *str = 0;
+ unsigned n;
int size;
+ const char *str = 0;
+
+ printf("\nstatic char %s[] = \"%s\";\n\n",
+ name_initializer("alias"), entries->tterm.term_names);
+
+ for_each_string(n, term) {
+ char buf[MAX_STRING], *sp, *tp;
+
+ if (VALID_STRING(term->Strings[n])) {
+ tp = buf;
+ *tp++ = '"';
+ for (sp = term->Strings[n];
+ *sp != 0 && (tp - buf) < MAX_STRING - 6;
+ sp++) {
+ if (isascii(UChar(*sp))
+ && isprint(UChar(*sp))
+ && *sp != '\\'
+ && *sp != '"')
+ *tp++ = *sp;
+ else {
+ (void) sprintf(tp, "\\%03o", UChar(*sp));
+ tp += 4;
+ }
+ }
+ *tp++ = '"';
+ *tp = '\0';
+ size += (strlen(term->Strings[n]) + 1);
+ (void) printf("static char %-20s[] = %s;\n",
+ string_variable(ExtStrname(term, n, strnames)), buf);
+ }
+ }
+ printf("\n");
(void) printf("static char %s[] = %s\n", name_initializer("bool"), L_CURL);
break;
}
(void) printf("\t/* %3d: %-8s */\t%s,\n",
- n, ExtBoolname(term, n, boolnames), str);
+ n, ExtBoolname(term, n, boolnames), str);
}
(void) printf("%s;\n", R_CURL);
str = buf;
break;
}
- (void) printf("\t/* %3d: %-8s */\t%s,\n", n, ExtNumname(term, n,
- numnames), str);
+ (void) printf("\t/* %3d: %-8s */\t%s,\n", n,
+ ExtNumname(term, n, numnames), str);
}
(void) printf("%s;\n", R_CURL);
(void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
for_each_string(n, term) {
- char buf[MAX_STRING], *sp, *tp;
if (term->Strings[n] == ABSENT_STRING)
str = "ABSENT_STRING";
else if (term->Strings[n] == CANCELLED_STRING)
str = "CANCELLED_STRING";
else {
- tp = buf;
- *tp++ = '"';
- for (sp = term->Strings[n];
- *sp != 0 && (tp - buf) < MAX_STRING - 6;
- sp++) {
- if (isascii(*sp) && isprint(*sp) && *sp != '\\' && *sp != '"')
- *tp++ = *sp;
- else {
- (void) sprintf(tp, "\\%03o", *sp & 0xff);
- tp += 4;
- }
- }
- *tp++ = '"';
- *tp = '\0';
- size += (strlen(term->Strings[n]) + 1);
- str = buf;
+ str = string_variable(ExtStrname(term, n, strnames));
}
-#if NCURSES_XNAMES
- if (n == STRCOUNT) {
- (void) printf("%s;\n", R_CURL);
+ (void) printf("\t/* %3d: %-8s */\t%s,\n", n,
+ ExtStrname(term, n, strnames), str);
+ }
+ (void) printf("%s;\n", R_CURL);
- (void) printf("static char * %s[] = %s\n",
- name_initializer("string_ext"), L_CURL);
+#if NCURSES_XNAMES
+ if ((NUM_BOOLEANS(term) != BOOLCOUNT)
+ || (NUM_NUMBERS(term) != NUMCOUNT)
+ || (NUM_STRINGS(term) != STRCOUNT)) {
+ (void) printf("static char * %s[] = %s\n",
+ name_initializer("string_ext"), L_CURL);
+ for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
+ (void) printf("\t/* %3d: bool */\t\"%s\",\n",
+ n, ExtBoolname(term, n, boolnames));
}
-#endif
- (void) printf("\t/* %3d: %-8s */\t%s,\n", n, ExtStrname(term, n,
- strnames), str);
+ for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
+ (void) printf("\t/* %3d: num */\t\"%s\",\n",
+ n, ExtNumname(term, n, numnames));
+ }
+ for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
+ (void) printf("\t/* %3d: str */\t\"%s\",\n",
+ n, ExtStrname(term, n, strnames));
+ }
+ (void) printf("%s;\n", R_CURL);
}
- (void) printf("%s;\n", R_CURL);
+#endif
}
/* dump C initializers for the terminal type */
static void
dump_termtype(TERMTYPE * term)
{
- (void) printf("\t%s\n\t\t\"%s\",\n", L_CURL, term->term_names);
+ (void) printf("\t%s\n\t\t%s,\n", L_CURL, name_initializer("alias"));
(void) printf("\t\t(char *)0,\t/* pointer to string table */\n");
(void) printf("\t\t%s,\n", name_initializer("bool"));
(void) printf("#if NCURSES_XNAMES\n");
(void) printf("\t\t(char *)0,\t/* pointer to extended string table */\n");
(void) printf("\t\t%s,\t/* ...corresponding names */\n",
- (NUM_STRINGS(term) != STRCOUNT)
- ? name_initializer("string_ext")
- : "(char **)0");
+ ((NUM_BOOLEANS(term) != BOOLCOUNT)
+ || (NUM_NUMBERS(term) != NUMCOUNT)
+ || (NUM_STRINGS(term) != STRCOUNT))
+ ? name_initializer("string_ext")
+ : "(char **)0");
(void) printf("\t\t%d,\t\t/* count total Booleans */\n", NUM_BOOLEANS(term));
(void) printf("\t\t%d,\t\t/* count total Numbers */\n", NUM_NUMBERS(term));
(void) printf("\t\t%d,\t\t/* count total Strings */\n", NUM_STRINGS(term));
(void) printf("\t\t%d,\t\t/* count extensions to Booleans */\n",
- NUM_BOOLEANS(term) - BOOLCOUNT);
+ NUM_BOOLEANS(term) - BOOLCOUNT);
(void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
- NUM_NUMBERS(term) - NUMCOUNT);
+ NUM_NUMBERS(term) - NUMCOUNT);
(void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
- NUM_STRINGS(term) - STRCOUNT);
+ NUM_STRINGS(term) - STRCOUNT);
(void) printf("#endif /* NCURSES_XNAMES */\n");
#endif /* NCURSES_XNAMES */
(void) printf("\t%s\n", R_CURL);
}
+static int
+optarg_to_number(void)
+{
+ char *temp = 0;
+ long value = strtol(optarg, &temp, 0);
+
+ if (temp == 0 || temp == optarg || *temp != 0) {
+ fprintf(stderr, "Expected a number, not \"%s\"\n", optarg);
+ ExitProgram(EXIT_FAILURE);
+ }
+ return (int) value;
+}
+
+static char *
+terminal_env(void)
+{
+ char *terminal;
+
+ if ((terminal = getenv("TERM")) == 0) {
+ (void) fprintf(stderr,
+ "infocmp: environment variable TERM not set\n");
+ exit(EXIT_FAILURE);
+ }
+ return terminal;
+}
+
/***************************************************************************
*
* Main sequence
int
main(int argc, char *argv[])
{
- char *terminal, *firstdir, *restdir;
+ char *firstdir, *restdir;
/* Avoid "local data >32k" error with mwcc */
/* Also avoid overflowing smaller stacks on systems like AmigaOS */
path *tfile = (path *) malloc(sizeof(path) * MAXTERMS);
bool filecompare = FALSE;
int initdump = 0;
bool init_analyze = FALSE;
-
- if ((terminal = getenv("TERM")) == 0) {
- (void) fprintf(stderr,
- "infocmp: environment variable TERM not set\n");
- return EXIT_FAILURE;
- }
+ bool suppress_untranslatable = FALSE;
/* where is the terminfo database location going to default to? */
restdir = firstdir = 0;
- while ((c = getopt(argc, argv, "adeEcCfFGgIinlLpqrR:s:uv:Vw:A:B:1T")) != EOF)
+ while ((c = getopt(argc,
+ argv,
+ "1A:aB:CcdEeFfGgIiLlnpqR:rs:TtuVv:w:")) != EOF)
switch (c) {
+ case '1':
+ mwidth = 0;
+ break;
+
+ case 'A':
+ firstdir = optarg;
+ break;
+
#if NCURSES_XNAMES
case 'a':
_nc_disable_period = TRUE;
use_extended_names(TRUE);
break;
#endif
- case 'd':
- compare = C_DIFFERENCE;
+ case 'B':
+ restdir = optarg;
break;
- case 'e':
- initdump |= 1;
+ case 'C':
+ outform = F_TERMCAP;
+ tversion = "BSD";
+ if (sortmode == S_DEFAULT)
+ sortmode = S_TERMCAP;
+ break;
+
+ case 'c':
+ compare = C_COMMON;
+ break;
+
+ case 'd':
+ compare = C_DIFFERENCE;
break;
case 'E':
initdump |= 2;
break;
- case 'c':
- compare = C_COMMON;
+ case 'e':
+ initdump |= 1;
break;
- case 'C':
- outform = F_TERMCAP;
- tversion = "BSD";
- if (sortmode == S_DEFAULT)
- sortmode = S_TERMCAP;
+ case 'F':
+ filecompare = TRUE;
break;
case 'f':
numbers = -1;
break;
- case 'F':
- filecompare = TRUE;
- break;
-
case 'I':
outform = F_TERMINFO;
if (sortmode == S_DEFAULT)
init_analyze = TRUE;
break;
- case 'l':
- outform = F_TERMINFO;
- break;
-
case 'L':
outform = F_VARIABLE;
if (sortmode == S_DEFAULT)
sortmode = S_VARIABLE;
break;
+ case 'l':
+ outform = F_TERMINFO;
+ break;
+
case 'n':
compare = C_NAND;
break;
bool_sep = ", ";
break;
+ case 'R':
+ tversion = optarg;
+ break;
+
case 'r':
tversion = 0;
limited = FALSE;
break;
- case 'R':
- tversion = optarg;
- break;
-
case 's':
if (*optarg == 'd')
sortmode = S_NOSORT;
sortmode = S_TERMCAP;
else {
(void) fprintf(stderr,
- "infocmp: unknown sort mode\n");
+ "infocmp: unknown sort mode\n");
return EXIT_FAILURE;
}
break;
- case 'u':
- compare = C_USEALL;
+ case 'T':
+ limited = FALSE;
break;
- case 'v':
- itrace = atoi(optarg);
- set_trace_level(itrace);
+#if NCURSES_XNAMES
+ case 't':
+ _nc_disable_period = FALSE;
+ suppress_untranslatable = TRUE;
break;
+#endif
- case 'V':
- (void) fputs(NCURSES_VERSION, stdout);
- putchar('\n');
- ExitProgram(EXIT_SUCCESS);
-
- case 'w':
- mwidth = atoi(optarg);
+ case 'u':
+ compare = C_USEALL;
break;
- case 'A':
- firstdir = optarg;
- break;
+ case 'V':
+ puts(curses_version());
+ ExitProgram(EXIT_SUCCESS);
- case 'B':
- restdir = optarg;
+ case 'v':
+ itrace = optarg_to_number();
+ set_trace_level(itrace);
break;
- case '1':
- mwidth = 0;
+ case 'w':
+ mwidth = optarg_to_number();
break;
- case 'T':
- limited = FALSE;
- break;
default:
usage();
}
/* make sure we have at least one terminal name to work with */
if (optind >= argc)
- argv[argc++] = terminal;
+ argv[argc++] = terminal_env();
/* if user is after a comparison, make sure we have two entries */
if (compare != C_DEFAULT && optind >= argc - 1)
- argv[argc++] = terminal;
+ argv[argc++] = terminal_env();
/* exactly two terminal names with no options means do -d */
if (argc - optind == 2 && compare == C_DEFAULT)
for (; optind < argc; optind++) {
if (termcount >= MAXTERMS) {
(void) fprintf(stderr,
- "infocmp: too many terminal type arguments\n");
+ "infocmp: too many terminal type arguments\n");
return EXIT_FAILURE;
} else {
const char *directory = termcount ? restdir : firstdir;
if (directory) {
(void) sprintf(tfile[termcount], "%s/%c/%s",
- directory,
- *argv[optind], argv[optind]);
+ directory,
+ *argv[optind], argv[optind]);
if (itrace)
(void) fprintf(stderr,
- "infocmp: reading entry %s from file %s\n",
- argv[optind], tfile[termcount]);
+ "infocmp: reading entry %s from file %s\n",
+ argv[optind], tfile[termcount]);
status = _nc_read_file_entry(tfile[termcount],
- &entries[termcount].tterm);
+ &entries[termcount].tterm);
} else {
if (itrace)
(void) fprintf(stderr,
- "infocmp: reading entry %s from system directories %s\n",
- argv[optind], tname[termcount]);
+ "infocmp: reading entry %s from system directories %s\n",
+ argv[optind], tname[termcount]);
status = _nc_read_entry(tname[termcount],
- tfile[termcount],
- &entries[termcount].tterm);
+ tfile[termcount],
+ &entries[termcount].tterm);
directory = TERMINFO; /* for error message */
}
if (status <= 0) {
(void) fprintf(stderr,
- "infocmp: couldn't open terminfo file %s.\n",
- tfile[termcount]);
+ "infocmp: couldn't open terminfo file %s.\n",
+ tfile[termcount]);
return EXIT_FAILURE;
}
repair_acsc(&entries[termcount].tterm);
case C_DEFAULT:
if (itrace)
(void) fprintf(stderr,
- "infocmp: about to dump %s\n",
- tname[0]);
+ "infocmp: about to dump %s\n",
+ tname[0]);
(void) printf("#\tReconstructed via infocmp from file: %s\n",
- tfile[0]);
- len = dump_entry(&entries[0].tterm, limited, numbers, NULL);
+ tfile[0]);
+ len = dump_entry(&entries[0].tterm,
+ suppress_untranslatable,
+ limited,
+ 0,
+ numbers,
+ NULL);
putchar('\n');
if (itrace)
(void) fprintf(stderr, "infocmp: length %d\n", len);
case C_COMMON:
if (itrace)
(void) fprintf(stderr,
- "infocmp: dumping common capabilities\n");
+ "infocmp: dumping common capabilities\n");
(void) printf("comparing %s to %s.\n", tname[0], tname[1]);
compare_entry(compare_predicate, &entries->tterm, quiet);
break;
case C_NAND:
if (itrace)
(void) fprintf(stderr,
- "infocmp: dumping differences\n");
+ "infocmp: dumping differences\n");
(void) printf("comparing %s to %s.\n", tname[0], tname[1]);
compare_entry(compare_predicate, &entries->tterm, quiet);
break;
case C_USEALL:
if (itrace)
(void) fprintf(stderr, "infocmp: dumping use entry\n");
- len = dump_entry(&entries[0].tterm, limited, numbers, use_predicate);
+ len = dump_entry(&entries[0].tterm,
+ suppress_untranslatable,
+ limited,
+ 0,
+ numbers,
+ use_predicate);
for (i = 1; i < termcount; i++)
- len += dump_uses(tname[i], !(outform == F_TERMCAP || outform
- == F_TCONVERR));
+ len += dump_uses(tname[i], !(outform == F_TERMCAP
+ || outform == F_TCONVERR));
putchar('\n');
if (itrace)
(void) fprintf(stderr, "infocmp: length %d\n", len);
(void) fprintf(stderr, "Use `tic -[CI] <file>' for this.\n");
else if (argc - optind != 2)
(void) fprintf(stderr,
- "File comparison needs exactly two file arguments.\n");
+ "File comparison needs exactly two file arguments.\n");
else
file_comparison(argc - optind, argv + optind);