]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - progs/infocmp.c
ncurses 6.4 - patch 20240420
[ncurses.git] / progs / infocmp.c
index d2f5eabe8847c81b4752b5a82d7fda4be0eeb3ad..7932203ac2b1aa189f37e5dd7ffbdd662f6ad988 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright 2020-2021,2022 Thomas E. Dickey                                *
+ * Copyright 2020-2022,2023 Thomas E. Dickey                                *
  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
@@ -43,7 +43,7 @@
 
 #include <dump_entry.h>
 
-MODULE_ID("$Id: infocmp.c,v 1.152 2022/02/26 23:25:55 tom Exp $")
+MODULE_ID("$Id: infocmp.c,v 1.163 2023/12/16 17:27:47 tom Exp $")
 
 #define MAX_STRING     1024    /* maximum formatted string */
 
@@ -124,7 +124,7 @@ failed(const char *s)
     ExitProgram(EXIT_FAILURE);
 }
 
-static char *
+static void
 canonical_name(char *source, char *target)
 /* extract the terminal type's primary name */
 {
@@ -137,8 +137,6 @@ canonical_name(char *source, char *target)
        *target++ = ch;
     }
     *target = '\0';
-
-    return (target);
 }
 
 static bool
@@ -160,7 +158,7 @@ no_numeric(int value)
 }
 
 static bool
-no_string(char *value)
+no_string(const char *const value)
 {
     bool result = (value == ABSENT_STRING);
     if (!strcmp(s_absent, s_cancel))
@@ -189,10 +187,21 @@ capcmp(PredIdx idx, const char *s, const char *t)
        return (_nc_capcmp(s, t));
 }
 
+/*
+ * Predicate function to use for "use=" decompilation.
+ *
+ * Return value is used in fmt_entry:
+ *   FAIL  show nothing for this capability.
+ *   FALSE show cancel for booleans (a compromise)
+ *   TRUE  show capability
+ *
+ * The only difference between FALSE/TRUE returns is in the treatment of
+ * booleans.
+ */
 static int
 use_predicate(unsigned type, PredIdx idx)
-/* predicate function to use for use decompilation */
 {
+    int result = FAIL;
     ENTRY *ep;
 
     switch (type) {
@@ -211,16 +220,18 @@ use_predicate(unsigned type, PredIdx idx)
             * unlike numbers and strings, whose cancelled/absent state is
             * recorded in the terminfo database.
             */
-           for (ep = &entries[1]; ep < entries + termcount; ep++)
-               if (ep->tterm.Booleans[idx] == TRUE) {
-                   is_set = entries[0].tterm.Booleans[idx];
-                   break;
+           if (idx < NUM_BOOLEANS(&(entries[0].tterm))) {
+               for (ep = &entries[1]; ep < entries + termcount; ep++) {
+                   if (idx < NUM_BOOLEANS(&(ep->tterm))
+                       && (is_set = ep->tterm.Booleans[idx])) {
+                       break;
+                   }
                }
-           if (is_set != entries[0].tterm.Booleans[idx])
-               return (!is_set);
-           else
-               return (FAIL);
+               if (is_set != entries[0].tterm.Booleans[idx])
+                   result = (!is_set);
+           }
        }
+       break;
 
     case NUMBER:
        {
@@ -231,45 +242,56 @@ use_predicate(unsigned type, PredIdx idx)
             * capability gets the first non-default value found
             * in the sequence of use entries'.
             */
-           for (ep = &entries[1]; ep < entries + termcount; ep++)
-               if (VALID_NUMERIC(ep->tterm.Numbers[idx])) {
-                   value = ep->tterm.Numbers[idx];
-                   break;
-               }
+           if (idx < NUM_NUMBERS(&(entries[0].tterm))) {
+               for (ep = &entries[1]; ep < entries + termcount; ep++)
+                   if (idx < NUM_NUMBERS(&(ep->tterm))
+                       && VALID_NUMERIC(ep->tterm.Numbers[idx])) {
+                       value = ep->tterm.Numbers[idx];
+                       break;
+                   }
 
-           if (value != entries[0].tterm.Numbers[idx])
-               return (value != ABSENT_NUMERIC);
-           else
-               return (FAIL);
+               if (value != entries[0].tterm.Numbers[idx])
+                   result = (value != ABSENT_NUMERIC);
+           }
        }
+       break;
 
     case STRING:
        {
-           char *termstr, *usestr = ABSENT_STRING;
-
-           termstr = entries[0].tterm.Strings[idx];
+           char *termstr = entries[0].tterm.Strings[idx];
+           char *usestr = ABSENT_STRING;
 
            /*
             * We take the semantics of multiple uses to be 'each
             * capability gets the first non-default value found
             * in the sequence of use entries'.
             */
-           for (ep = &entries[1]; ep < entries + termcount; ep++)
-               if (ep->tterm.Strings[idx]) {
-                   usestr = ep->tterm.Strings[idx];
-                   break;
-               }
+           if (idx < NUM_STRINGS(&(entries[0].tterm))) {
+               for (ep = &entries[1]; ep < entries + termcount; ep++)
+                   if (idx < NUM_STRINGS(&(ep->tterm))
+                       && ep->tterm.Strings[idx]) {
+                       usestr = ep->tterm.Strings[idx];
+                       break;
+                   }
 
-           if (usestr == ABSENT_STRING && termstr == ABSENT_STRING)
-               return (FAIL);
-           else if (!usestr || !termstr || capcmp(idx, usestr, termstr))
-               return (TRUE);
-           else
-               return (FAIL);
+               if (usestr == CANCELLED_STRING && termstr == ABSENT_STRING)
+                   result = (FAIL);
+               else if (usestr == CANCELLED_STRING && termstr == CANCELLED_STRING)
+                   result = (TRUE);
+               else if (usestr == ABSENT_STRING && termstr == ABSENT_STRING)
+                   result = (FAIL);
+               else if (!usestr || !termstr || capcmp(idx, usestr, termstr))
+                   result = (TRUE);
+           }
        }
+       break;
+
+    default:
+       result = FALSE;
+       break;
     }
 
-    return (FALSE);            /* pacify compiler */
+    return (result);
 }
 
 static bool
@@ -907,7 +929,6 @@ analyze_string(const char *name, const char *cap, TERMTYPE2 *tp)
                       sizeof(buf2));
            _nc_STRNCPY(buf3, sp + csi, len);
            buf3[len] = '\0';
-           len += (size_t) csi + 1;
 
            expansion = lookup_params(std_modes, buf2, buf3);
        }
@@ -928,7 +949,6 @@ analyze_string(const char *name, const char *cap, TERMTYPE2 *tp)
                       sizeof(buf2));
            _nc_STRNCPY(buf3, sp + csi + 1, len);
            buf3[len] = '\0';
-           len += (size_t) csi + 2;
 
            expansion = lookup_params(private_modes, buf2, buf3);
        }
@@ -1136,8 +1156,8 @@ file_comparison(int argc, char *argv[])
            if (entryeq(&qp->tterm, &rp->tterm) && useeq(qp, rp)) {
                char name1[NAMESIZE], name2[NAMESIZE];
 
-               (void) canonical_name(qp->tterm.term_names, name1);
-               (void) canonical_name(rp->tterm.term_names, name2);
+               canonical_name(qp->tterm.term_names, name1);
+               canonical_name(rp->tterm.term_names, name2);
 
                (void) printf("%s = %s\n", name1, name2);
            }
@@ -1165,8 +1185,8 @@ file_comparison(int argc, char *argv[])
                entries[0] = *qp;
                entries[1] = *rp;
 
-               (void) canonical_name(qp->tterm.term_names, name1);
-               (void) canonical_name(rp->tterm.term_names, name2);
+               canonical_name(qp->tterm.term_names, name1);
+               canonical_name(rp->tterm.term_names, name2);
 
                switch (compare) {
                case C_DIFFERENCE:
@@ -1729,7 +1749,7 @@ main(int argc, char *argv[])
 
        case 'v':
            itrace = (unsigned) optarg_to_number();
-           set_trace_level(itrace);
+           use_verbosity(itrace);
            break;
 
        case 'W':
@@ -1862,8 +1882,16 @@ main(int argc, char *argv[])
        }
 
 #if NCURSES_XNAMES
-       if (termcount > 1)
-           _nc_align_termtype(&entries[0].tterm, &entries[1].tterm);
+       if (termcount > 1) {
+           /*
+            * User-defined capabilities in different terminal descriptions
+            * may have the same name/type but different indices.  Line up
+            * the names to use comparable indices.  We may have more than two
+            * entries to compare when processing the "-u" option.
+            */
+           for (c = 1; c < termcount; ++c)
+               _nc_align_termtype(&entries[c].tterm, &entries[0].tterm);
+       }
 #endif
 
        /* dump as C initializer for the terminal type */