]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - progs/infocmp.c
ncurses 5.6 - patch 20080105
[ncurses.git] / progs / infocmp.c
index d3353f26415e0c594fda1787d0d59722fa2ddc80..99db6bbb057f41e1d92a7fa2a3dbd70b115b8a05 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
 /****************************************************************************
- * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2007,2008 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            *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
 /*
  *     infocmp.c -- decompile an entry, or compare two entries
  *             written by Eric S. Raymond
 /*
  *     infocmp.c -- decompile an entry, or compare two entries
  *             written by Eric S. Raymond
+ *             and Thomas E Dickey
  */
 
 #include <progs.priv.h>
 
 #include <dump_entry.h>
 
  */
 
 #include <progs.priv.h>
 
 #include <dump_entry.h>
 
-MODULE_ID("$Id: infocmp.c,v 1.85 2006/08/19 21:20:37 tom Exp $")
+MODULE_ID("$Id: infocmp.c,v 1.95 2008/01/05 20:53:20 tom Exp $")
 
 #define L_CURL "{"
 #define R_CURL "}"
 
 
 #define L_CURL "{"
 #define R_CURL "}"
 
-#define MAXTERMS       32      /* max # terminal arguments we can handle */
 #define MAX_STRING     1024    /* maximum formatted string */
 
 const char *_nc_progname = "infocmp";
 #define MAX_STRING     1024    /* maximum formatted string */
 
 const char *_nc_progname = "infocmp";
@@ -60,8 +60,7 @@ typedef char path[PATH_MAX];
  *
  ***************************************************************************/
 
  *
  ***************************************************************************/
 
-static char *tname[MAXTERMS];  /* terminal type names */
-static ENTRY entries[MAXTERMS];        /* terminfo entries */
+static ENTRY *entries;         /* terminfo entries */
 static int termcount;          /* count of terminal entries */
 
 static bool limited = TRUE;    /* "-r" option is not set */
 static int termcount;          /* count of terminal entries */
 
 static bool limited = TRUE;    /* "-r" option is not set */
@@ -96,7 +95,8 @@ ExitProgram(int code)
     while (termcount-- > 0)
        _nc_free_termtype(&entries[termcount].tterm);
     _nc_leaks_dump_entry();
     while (termcount-- > 0)
        _nc_free_termtype(&entries[termcount].tterm);
     _nc_leaks_dump_entry();
-    _nc_free_and_exit(code);
+    free(entries);
+    _nc_free_tic(code);
 }
 #endif
 
 }
 #endif
 
@@ -932,7 +932,8 @@ file_comparison(int argc, char *argv[])
                case C_DIFFERENCE:
                    if (itrace)
                        (void) fprintf(stderr,
                case C_DIFFERENCE:
                    if (itrace)
                        (void) fprintf(stderr,
-                                      "infocmp: dumping differences\n");
+                                      "%s: dumping differences\n",
+                                      _nc_progname);
                    (void) printf("comparing %s to %s.\n", name1, name2);
                    compare_entry(compare_predicate, &entries->tterm, quiet);
                    break;
                    (void) printf("comparing %s to %s.\n", name1, name2);
                    compare_entry(compare_predicate, &entries->tterm, quiet);
                    break;
@@ -940,7 +941,8 @@ file_comparison(int argc, char *argv[])
                case C_COMMON:
                    if (itrace)
                        (void) fprintf(stderr,
                case C_COMMON:
                    if (itrace)
                        (void) fprintf(stderr,
-                                      "infocmp: dumping common capabilities\n");
+                                      "%s: dumping common capabilities\n",
+                                      _nc_progname);
                    (void) printf("comparing %s to %s.\n", name1, name2);
                    compare_entry(compare_predicate, &entries->tterm, quiet);
                    break;
                    (void) printf("comparing %s to %s.\n", name1, name2);
                    compare_entry(compare_predicate, &entries->tterm, quiet);
                    break;
@@ -948,7 +950,8 @@ file_comparison(int argc, char *argv[])
                case C_NAND:
                    if (itrace)
                        (void) fprintf(stderr,
                case C_NAND:
                    if (itrace)
                        (void) fprintf(stderr,
-                                      "infocmp: dumping differences\n");
+                                      "%s: dumping differences\n",
+                                      _nc_progname);
                    (void) printf("comparing %s to %s.\n", name1, name2);
                    compare_entry(compare_predicate, &entries->tterm, quiet);
                    break;
                    (void) printf("comparing %s to %s.\n", name1, name2);
                    compare_entry(compare_predicate, &entries->tterm, quiet);
                    break;
@@ -1083,7 +1086,6 @@ dump_initializers(TERMTYPE *term)
            }
            *tp++ = '"';
            *tp = '\0';
            }
            *tp++ = '"';
            *tp = '\0';
-           size += (strlen(term->Strings[n]) + 1);
            (void) printf("static char %-20s[] = %s;\n",
                          string_variable(ExtStrname(term, n, strnames)), buf);
        }
            (void) printf("static char %-20s[] = %s;\n",
                          string_variable(ExtStrname(term, n, strnames)), buf);
        }
@@ -1237,7 +1239,8 @@ terminal_env(void)
 
     if ((terminal = getenv("TERM")) == 0) {
        (void) fprintf(stderr,
 
     if ((terminal = getenv("TERM")) == 0) {
        (void) fprintf(stderr,
-                      "infocmp: environment variable TERM not set\n");
+                      "%s: environment variable TERM not set\n",
+                      _nc_progname);
        exit(EXIT_FAILURE);
     }
     return terminal;
        exit(EXIT_FAILURE);
     }
     return terminal;
@@ -1252,10 +1255,15 @@ terminal_env(void)
 int
 main(int argc, char *argv[])
 {
 int
 main(int argc, char *argv[])
 {
-    char *firstdir, *restdir;
     /* Avoid "local data >32k" error with mwcc */
     /* Also avoid overflowing smaller stacks on systems like AmigaOS */
     /* Avoid "local data >32k" error with mwcc */
     /* Also avoid overflowing smaller stacks on systems like AmigaOS */
-    path *tfile = (path *) malloc(sizeof(path) * MAXTERMS);
+    path *tfile = 0;
+    char **tname = 0;
+    int maxterms;
+
+    char **myargv;
+
+    char *firstdir, *restdir;
     int c, i, len;
     bool formatted = FALSE;
     bool filecompare = FALSE;
     int c, i, len;
     bool formatted = FALSE;
     bool filecompare = FALSE;
@@ -1270,9 +1278,16 @@ main(int argc, char *argv[])
     use_extended_names(FALSE);
 #endif
 
     use_extended_names(FALSE);
 #endif
 
+    _nc_progname = _nc_rootname(argv[0]);
+
+    /* make sure we have enough space to add two terminal entries */
+    myargv = typeCalloc(char *, argc + 3);
+    memcpy(myargv, argv, sizeof(char *) * argc);
+    argv = myargv;
+
     while ((c = getopt(argc,
                       argv,
     while ((c = getopt(argc,
                       argv,
-                      "1A:aB:CcdEeFfGgIiLlnpqR:rs:TtUuVv:w:x")) != EOF) {
+                      "1A:aB:CcdEeFfGgIiLlnpqR:rs:TtUuVv:w:x")) != -1) {
        switch (c) {
        case '1':
            mwidth = 0;
        switch (c) {
        case '1':
            mwidth = 0;
@@ -1373,7 +1388,6 @@ main(int argc, char *argv[])
 
        case 'r':
            tversion = 0;
 
        case 'r':
            tversion = 0;
-           limited = FALSE;
            break;
 
        case 's':
            break;
 
        case 's':
@@ -1387,7 +1401,8 @@ main(int argc, char *argv[])
                sortmode = S_TERMCAP;
            else {
                (void) fprintf(stderr,
                sortmode = S_TERMCAP;
            else {
                (void) fprintf(stderr,
-                              "infocmp: unknown sort mode\n");
+                              "%s: unknown sort mode\n",
+                              _nc_progname);
                ExitProgram(EXIT_FAILURE);
            }
            break;
                ExitProgram(EXIT_FAILURE);
            }
            break;
@@ -1435,6 +1450,18 @@ main(int argc, char *argv[])
        }
     }
 
        }
     }
 
+    maxterms = (argc + 2 - optind);
+    tfile = typeMalloc(path, maxterms);
+    tname = typeCalloc(char *, maxterms);
+    entries = typeCalloc(ENTRY, maxterms);
+
+    if (tfile == 0
+       || tname == 0
+       || entries == 0) {
+       fprintf(stderr, "%s: not enough memory\n", _nc_progname);
+       ExitProgram(EXIT_FAILURE);
+    }
+
     /* by default, sort by terminfo name */
     if (sortmode == S_DEFAULT)
        sortmode = S_TERMINFO;
     /* by default, sort by terminfo name */
     if (sortmode == S_DEFAULT)
        sortmode = S_TERMINFO;
@@ -1458,53 +1485,56 @@ main(int argc, char *argv[])
        /* grab the entries */
        termcount = 0;
        for (; optind < argc; optind++) {
        /* grab the entries */
        termcount = 0;
        for (; optind < argc; optind++) {
-           if (termcount >= MAXTERMS) {
-               (void) fprintf(stderr,
-                              "infocmp: too many terminal type arguments\n");
-               ExitProgram(EXIT_FAILURE);
-           } else {
-               const char *directory = termcount ? restdir : firstdir;
-               int status;
+           const char *directory = termcount ? restdir : firstdir;
+           int status;
 
 
-               tname[termcount] = argv[optind];
+           tname[termcount] = argv[optind];
 
 
-               if (directory) {
+           if (directory) {
 #if USE_DATABASE
 #if USE_DATABASE
-                   (void) sprintf(tfile[termcount], "%s/%c/%s",
-                                  directory,
-                                  *argv[optind], argv[optind]);
-                   if (itrace)
-                       (void) fprintf(stderr,
-                                      "infocmp: reading entry %s from file %s\n",
-                                      argv[optind], tfile[termcount]);
-
-                   status = _nc_read_file_entry(tfile[termcount],
-                                                &entries[termcount].tterm);
+#if MIXEDCASE_FILENAMES
+#define LEAF_FMT "%c"
 #else
 #else
-                   (void) fprintf(stderr, "terminfo files not supported\n");
-                   ExitProgram(EXIT_FAILURE);
+#define LEAF_FMT "%02x"
 #endif
 #endif
-               } else {
-                   if (itrace)
-                       (void) fprintf(stderr,
-                                      "infocmp: reading entry %s from database\n",
-                                      tname[termcount]);
-
-                   status = _nc_read_entry(tname[termcount],
-                                           tfile[termcount],
-                                           &entries[termcount].tterm);
-                   directory = TERMINFO;       /* for error message */
-               }
+               (void) sprintf(tfile[termcount], "%s/" LEAF_FMT "/%s",
+                              directory,
+                              UChar(*argv[optind]), argv[optind]);
+               if (itrace)
+                   (void) fprintf(stderr,
+                                  "%s: reading entry %s from file %s\n",
+                                  _nc_progname,
+                                  argv[optind], tfile[termcount]);
 
 
-               if (status <= 0) {
+               status = _nc_read_file_entry(tfile[termcount],
+                                            &entries[termcount].tterm);
+#else
+               (void) fprintf(stderr, "%s: terminfo files not supported\n",
+                              _nc_progname);
+               ExitProgram(EXIT_FAILURE);
+#endif
+           } else {
+               if (itrace)
                    (void) fprintf(stderr,
                    (void) fprintf(stderr,
-                                  "infocmp: couldn't open terminfo file %s.\n",
-                                  tfile[termcount]);
-                   ExitProgram(EXIT_FAILURE);
-               }
-               repair_acsc(&entries[termcount].tterm);
-               termcount++;
+                                  "%s: reading entry %s from database\n",
+                                  _nc_progname,
+                                  tname[termcount]);
+
+               status = _nc_read_entry(tname[termcount],
+                                       tfile[termcount],
+                                       &entries[termcount].tterm);
+               directory = TERMINFO;   /* for error message */
+           }
+
+           if (status <= 0) {
+               (void) fprintf(stderr,
+                              "%s: couldn't open terminfo file %s.\n",
+                              _nc_progname,
+                              tfile[termcount]);
+               ExitProgram(EXIT_FAILURE);
            }
            }
+           repair_acsc(&entries[termcount].tterm);
+           termcount++;
        }
 
 #if NCURSES_XNAMES
        }
 
 #if NCURSES_XNAMES
@@ -1542,7 +1572,8 @@ main(int argc, char *argv[])
            case C_DEFAULT:
                if (itrace)
                    (void) fprintf(stderr,
            case C_DEFAULT:
                if (itrace)
                    (void) fprintf(stderr,
-                                  "infocmp: about to dump %s\n",
+                                  "%s: about to dump %s\n",
+                                  _nc_progname,
                                   tname[0]);
                (void) printf("#\tReconstructed via infocmp from file: %s\n",
                              tfile[0]);
                                   tname[0]);
                (void) printf("#\tReconstructed via infocmp from file: %s\n",
                              tfile[0]);
@@ -1553,12 +1584,12 @@ main(int argc, char *argv[])
                           NULL);
                len = show_entry();
                if (itrace)
                           NULL);
                len = show_entry();
                if (itrace)
-                   (void) fprintf(stderr, "infocmp: length %d\n", len);
+                   (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
                break;
 
            case C_DIFFERENCE:
                if (itrace)
                break;
 
            case C_DIFFERENCE:
                if (itrace)
-                   (void) fprintf(stderr, "infocmp: dumping differences\n");
+                   (void) fprintf(stderr, "%s: dumping differences\n", _nc_progname);
                (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
                compare_entry(compare_predicate, &entries->tterm, quiet);
                break;
                (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
                compare_entry(compare_predicate, &entries->tterm, quiet);
                break;
@@ -1566,7 +1597,8 @@ main(int argc, char *argv[])
            case C_COMMON:
                if (itrace)
                    (void) fprintf(stderr,
            case C_COMMON:
                if (itrace)
                    (void) fprintf(stderr,
-                                  "infocmp: dumping common capabilities\n");
+                                  "%s: dumping common capabilities\n",
+                                  _nc_progname);
                (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
                compare_entry(compare_predicate, &entries->tterm, quiet);
                break;
                (void) printf("comparing %s to %s.\n", tname[0], tname[1]);
                compare_entry(compare_predicate, &entries->tterm, quiet);
                break;
@@ -1574,14 +1606,15 @@ main(int argc, char *argv[])
            case C_NAND:
                if (itrace)
                    (void) fprintf(stderr,
            case C_NAND:
                if (itrace)
                    (void) fprintf(stderr,
-                                  "infocmp: dumping differences\n");
+                                  "%s: dumping differences\n",
+                                  _nc_progname);
                (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) 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");
+                   (void) fprintf(stderr, "%s: dumping use entry\n", _nc_progname);
                dump_entry(&entries[0].tterm,
                           suppress_untranslatable,
                           limited,
                dump_entry(&entries[0].tterm,
                           suppress_untranslatable,
                           limited,
@@ -1592,7 +1625,7 @@ main(int argc, char *argv[])
                                          || outform == F_TCONVERR));
                len = show_entry();
                if (itrace)
                                          || outform == F_TCONVERR));
                len = show_entry();
                if (itrace)
-                   (void) fprintf(stderr, "infocmp: length %d\n", len);
+                   (void) fprintf(stderr, "%s: length %d\n", _nc_progname, len);
                break;
            }
        }
                break;
            }
        }
@@ -1606,7 +1639,11 @@ main(int argc, char *argv[])
     else
        file_comparison(argc - optind, argv + optind);
 
     else
        file_comparison(argc - optind, argv + optind);
 
+#if NO_LEAKS
+    free(myargv);
     free(tfile);
     free(tfile);
+    free(tname);
+#endif
     ExitProgram(EXIT_SUCCESS);
 }
 
     ExitProgram(EXIT_SUCCESS);
 }