ncurses 5.3
[ncurses.git] / progs / infocmp.c
index bf7ff1c54b07927b669232be416500c15e3f1948..5d80511cca323c6e9e06f100293a76477e50af53 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2001,2002 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            *
@@ -41,7 +41,7 @@
 #include <term_entry.h>
 #include <dump_entry.h>
 
-MODULE_ID("$Id: infocmp.c,v 1.57 2000/10/01 01:26:25 tom Exp $")
+MODULE_ID("$Id: infocmp.c,v 1.68 2002/10/06 01:13:04 tom Exp $")
 
 #define L_CURL "{"
 #define R_CURL "}"
@@ -710,7 +710,6 @@ file_comparison(int argc, char *argv[])
     /* someday we may allow comparisons on more files */
     int filecount = 0;
     ENTRY *heads[MAXCOMPARE];
-    ENTRY *tails[MAXCOMPARE];
     ENTRY *qp, *rp;
     int i, n;
 
@@ -744,7 +743,6 @@ file_comparison(int argc, char *argv[])
        }
 
        heads[filecount] = _nc_head;
-       tails[filecount] = _nc_tail;
        filecount++;
     }
 
@@ -919,6 +917,9 @@ usage(void)
        ,"  -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)"
@@ -939,30 +940,75 @@ usage(void)
 }
 
 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;
     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);
 
@@ -1017,49 +1063,47 @@ dump_initializers(TERMTYPE * term)
     (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("static char * %s[] = %s\n",
-                         name_initializer("string_ext"), L_CURL);
-       }
-#endif
        (void) printf("\t/* %3d: %-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/* %3d: bool */\t\"%s\",\n",
+                         n, ExtBoolname(term, n, boolnames));
+       }
+       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);
+    }
+#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"));
@@ -1071,7 +1115,9 @@ dump_termtype(TERMTYPE * term)
     (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)
+                 ((NUM_BOOLEANS(term) != BOOLCOUNT)
+                  || (NUM_NUMBERS(term) != NUMCOUNT)
+                  || (NUM_STRINGS(term) != STRCOUNT))
                  ? name_initializer("string_ext")
                  : "(char **)0");
 
@@ -1122,6 +1168,7 @@ main(int argc, char *argv[])
     bool filecompare = FALSE;
     int initdump = 0;
     bool init_analyze = FALSE;
+    bool suppress_untranslatable = FALSE;
 
     if ((terminal = getenv("TERM")) == 0) {
        (void) fprintf(stderr,
@@ -1132,35 +1179,53 @@ main(int argc, char *argv[])
     /* 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':
@@ -1175,10 +1240,6 @@ main(int argc, char *argv[])
            numbers = -1;
            break;
 
-       case 'F':
-           filecompare = TRUE;
-           break;
-
        case 'I':
            outform = F_TERMINFO;
            if (sortmode == S_DEFAULT)
@@ -1190,16 +1251,16 @@ main(int argc, char *argv[])
            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;
@@ -1215,15 +1276,15 @@ main(int argc, char *argv[])
            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;
@@ -1240,38 +1301,34 @@ main(int argc, char *argv[])
            }
            break;
 
+       case 'T':
+           limited = FALSE;
+           break;
+
+#if NCURSES_XNAMES
+       case 't':
+           _nc_disable_period = FALSE;
+           suppress_untranslatable = TRUE;
+           break;
+#endif
+
        case 'u':
            compare = C_USEALL;
            break;
 
+       case 'V':
+           puts(curses_version());
+           ExitProgram(EXIT_SUCCESS);
+
        case 'v':
            itrace = optarg_to_number();
            set_trace_level(itrace);
            break;
 
-       case 'V':
-           puts(curses_version());
-           ExitProgram(EXIT_SUCCESS);
-
        case 'w':
            mwidth = optarg_to_number();
            break;
 
-       case 'A':
-           firstdir = optarg;
-           break;
-
-       case 'B':
-           restdir = optarg;
-           break;
-
-       case '1':
-           mwidth = 0;
-           break;
-
-       case 'T':
-           limited = FALSE;
-           break;
        default:
            usage();
        }
@@ -1384,7 +1441,12 @@ main(int argc, char *argv[])
                               tname[0]);
            (void) printf("#\tReconstructed via infocmp from file: %s\n",
                          tfile[0]);
-           len = dump_entry(&entries[0].tterm, limited, numbers, NULL);
+           len = dump_entry(&entries[0].tterm,
+                            suppress_untranslatable,
+                            limited,
+                            0,
+                            numbers,
+                            NULL);
            putchar('\n');
            if (itrace)
                (void) fprintf(stderr, "infocmp: length %d\n", len);
@@ -1416,10 +1478,15 @@ main(int argc, char *argv[])
        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);