+ static char *initializer;
+ char *s;
+
+ if (initializer == 0)
+ initializer = (char *) malloc(strlen(entries->tterm.term_names) +
+ strlen(type) + strlen(fmt));
+
+ (void) strcpy(initializer, entries->tterm.term_names);
+ for (s = initializer; *s != 0 && *s != '|'; 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)
+{
+ 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';
+ (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);
+
+ for_each_boolean(n, term) {
+ switch ((int) (term->Booleans[n])) {
+ case TRUE:
+ str = "TRUE";
+ break;
+
+ case FALSE:
+ str = "FALSE";
+ break;
+
+ case ABSENT_BOOLEAN:
+ str = "ABSENT_BOOLEAN";
+ break;
+
+ case CANCELLED_BOOLEAN:
+ str = "CANCELLED_BOOLEAN";
+ break;
+ }
+ (void) printf("\t/* %3u: %-8s */\t%s,\n",
+ n, ExtBoolname(term, n, boolnames), str);
+ }
+ (void) printf("%s;\n", R_CURL);
+
+ (void) printf("static short %s[] = %s\n", name_initializer("number"), L_CURL);
+
+ for_each_number(n, term) {
+ char buf[BUFSIZ];
+ switch (term->Numbers[n]) {
+ case ABSENT_NUMERIC:
+ str = "ABSENT_NUMERIC";
+ break;
+ case CANCELLED_NUMERIC:
+ str = "CANCELLED_NUMERIC";
+ break;
+ default:
+ sprintf(buf, "%d", term->Numbers[n]);
+ str = buf;
+ break;
+ }
+ (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
+ ExtNumname(term, n, numnames), str);
+ }
+ (void) printf("%s;\n", R_CURL);
+
+ size = (sizeof(TERMTYPE)
+ + (NUM_BOOLEANS(term) * sizeof(term->Booleans[0]))
+ + (NUM_NUMBERS(term) * sizeof(term->Numbers[0])));
+
+ (void) printf("static char * %s[] = %s\n", name_initializer("string"), L_CURL);
+
+ for_each_string(n, term) {
+
+ if (term->Strings[n] == ABSENT_STRING)
+ str = "ABSENT_STRING";
+ else if (term->Strings[n] == CANCELLED_STRING)
+ str = "CANCELLED_STRING";
+ else {
+ str = string_variable(ExtStrname(term, n, strnames));
+ }
+ (void) printf("\t/* %3u: %-8s */\t%s,\n", n,
+ ExtStrname(term, n, strnames), str);
+ }
+ (void) printf("%s;\n", R_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/* %3u: bool */\t\"%s\",\n",
+ n, ExtBoolname(term, n, boolnames));
+ }
+ for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
+ (void) printf("\t/* %3u: num */\t\"%s\",\n",
+ n, ExtNumname(term, n, numnames));
+ }
+ for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
+ (void) printf("\t/* %3u: str */\t\"%s\",\n",
+ n, ExtStrname(term, n, strnames));
+ }
+ (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, 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("\t\t%s,\n", name_initializer("number"));
+
+ (void) printf("\t\t%s,\n", name_initializer("string"));
+
+#if NCURSES_XNAMES
+ (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_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);
+ (void) printf("\t\t%d,\t\t/* count extensions to Numbers */\n",
+ NUM_NUMBERS(term) - NUMCOUNT);
+ (void) printf("\t\t%d,\t\t/* count extensions to Strings */\n",
+ 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");