]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - progs/dump_entry.c
ncurses 6.3
[ncurses.git] / progs / dump_entry.c
index 60a3d95670359da88dbf660c21a63328c1281ac5..860eccc9a37736df6d1aefd2354558af33d528ed 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 1998-2018,2019 Free Software Foundation, Inc.              *
+ * Copyright 2018-2020,2021 Thomas E. Dickey                                *
+ * Copyright 1998-2016,2017 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            *
 #define __INTERNAL_CAPS_VISIBLE
 #include <progs.priv.h>
 
-#include "dump_entry.h"
-#include "termsort.c"          /* this C file is generated */
+#include <dump_entry.h>
+#include <termsort.h>          /* this C file is generated */
 #include <parametrized.h>      /* so is this */
 
-MODULE_ID("$Id: dump_entry.c,v 1.171 2019/01/21 15:11:51 tom Exp $")
+MODULE_ID("$Id: dump_entry.c,v 1.189 2021/09/04 10:29:59 tom Exp $")
 
 #define DISCARD(string) string = ABSENT_STRING
 #define PRINTF (void) printf
@@ -84,7 +85,7 @@ static int indent = 8;
 /* cover various ports and variants of terminfo */
 #define V_ALLCAPS      0       /* all capabilities (SVr4, XSI, ncurses) */
 #define V_SVR1         1       /* SVR1, Ultrix */
-#define V_HPUX         2       /* HP/UX */
+#define V_HPUX         2       /* HP-UX */
 #define V_AIX          3       /* AIX */
 #define V_BSD          4       /* BSD */
 
@@ -106,9 +107,7 @@ static int indent = 8;
 #define StrIndirect(j)  ((sortmode == S_NOSORT) ? (j) : str_indirect[j])
 #endif
 
-static void failed(const char *) GCC_NORETURN;
-
-static void
+static GCC_NORETURN void
 failed(const char *s)
 {
     perror(s);
@@ -432,10 +431,12 @@ static int
 op_length(const char *src, int offset)
 {
     int result = 0;
-    int ch;
+
     if (offset > 0 && src[offset - 1] == '\\') {
        result = 0;
     } else {
+       int ch;
+
        result++;               /* for '%' mark */
        ch = src[offset + result];
        if (TcOutput()) {
@@ -476,10 +477,12 @@ static int
 find_split(const char *src, int step, int size)
 {
     int result = size;
-    int n;
+
     if (size > 0) {
        /* check if that would split a backslash-sequence */
        int mark = size;
+       int n;
+
        for (n = size - 1; n > 0; --n) {
            int ch = UChar(src[step + n]);
            if (ch == '\\') {
@@ -525,13 +528,13 @@ fill_spaces(const char *src)
     size_t size = strlen(fill);
     char *result = 0;
     int pass;
-    int s, d;
+    size_t s, d;
     for (pass = 0; pass < 2; ++pass) {
        for (s = d = 0; src[s] != '\0'; ++s) {
            if (src[s] == ' ') {
                if (pass) {
                    _nc_STRCPY(&result[d], fill, need + 1 - d);
-                   d += (int) size;
+                   d += size;
                } else {
                    need += size;
                }
@@ -546,7 +549,7 @@ fill_spaces(const char *src)
        if (pass) {
            result[d] = '\0';
        } else {
-           result = malloc(need + 1);
+           result = calloc(need + 1, sizeof(char));
            if (result == 0)
                failed("fill_spaces");
        }
@@ -584,7 +587,6 @@ wrap_concat(const char *src, int need, unsigned mode)
        (column + want) > width) {
        int step = 0;
        int used = width > WRAPPED ? width : WRAPPED;
-       int size;
        int base = 0;
        char *p, align[9];
        const char *my_t = trailer;
@@ -618,7 +620,7 @@ wrap_concat(const char *src, int need, unsigned mode)
            }
 
            while ((column + (need + gaps)) > used) {
-               size = used - tag;
+               int size = used - tag;
                if (step) {
                    strcpy_DYN(&outbuf, align);
                    size -= base;
@@ -718,8 +720,35 @@ indent_DYN(DYNBUF * buffer, int level)
        strncpy_DYN(buffer, "\t", (size_t) 1);
 }
 
+/*
+ * Check if the current line which was begun consists only of a tab and the
+ * given leading text.
+ */
+static bool
+leading_DYN(DYNBUF * buffer, const char *leading)
+{
+    bool result = FALSE;
+    size_t need = strlen(leading);
+    if (buffer->used > need) {
+       need = buffer->used - need;
+       if (!strcmp(buffer->text + need, leading)) {
+           result = TRUE;
+           while (--need != 0) {
+               if (buffer->text[need] == '\n') {
+                   break;
+               }
+               if (buffer->text[need] != '\t') {
+                   result = FALSE;
+                   break;
+               }
+           }
+       }
+    }
+    return result;
+}
+
 bool
-has_params(const char *src)
+has_params(const char *src, bool formatting)
 {
     bool result = FALSE;
     int len = (int) strlen(src);
@@ -737,7 +766,11 @@ has_params(const char *src)
        }
     }
     if (!ifthen) {
-       result = ((len > 50) && params);
+       if (formatting) {
+           result = ((len > 50) && params);
+       } else {
+           result = params;
+       }
     }
     return result;
 }
@@ -746,7 +779,7 @@ static char *
 fmt_complex(TERMTYPE2 *tterm, const char *capability, char *src, int level)
 {
     bool percent = FALSE;
-    bool params = has_params(src);
+    bool params = has_params(src, TRUE);
 
     while (*src != '\0') {
        switch (*src) {
@@ -773,7 +806,7 @@ fmt_complex(TERMTYPE2 *tterm, const char *capability, char *src, int level)
                    strncpy_DYN(&tmpbuf, "%", (size_t) 1);
                    strncpy_DYN(&tmpbuf, src, (size_t) 1);
                    src++;
-                   params = has_params(src);
+                   params = has_params(src, TRUE);
                    if (!params && *src != '\0' && *src != '%') {
                        strncpy_DYN(&tmpbuf, "\n", (size_t) 1);
                        indent_DYN(&tmpbuf, level + 1);
@@ -821,7 +854,7 @@ fmt_complex(TERMTYPE2 *tterm, const char *capability, char *src, int level)
            }
            break;
        case 'p':
-           if (percent && params) {
+           if (percent && params && !leading_DYN(&tmpbuf, "%")) {
                tmpbuf.text[tmpbuf.used - 1] = '\n';
                indent_DYN(&tmpbuf, level + 1);
                strncpy_DYN(&tmpbuf, "%", (size_t) 1);
@@ -850,12 +883,15 @@ static const char *
 number_format(int value)
 {
     const char *result = "%d";
+
     if ((outform != F_TERMCAP) && (value > 255)) {
        unsigned long lv = (unsigned long) value;
-       unsigned long mm;
        int bits = sizeof(unsigned long) * 8;
        int nn;
+
        for (nn = 8; nn < bits; ++nn) {
+           unsigned long mm;
+
            mm = 1UL << nn;
            if ((mm - 16) <= lv && (mm + 16) > lv) {
                result = "%#x";
@@ -879,7 +915,6 @@ fmt_entry(TERMTYPE2 *tterm,
 {
     PredIdx i, j;
     char buffer[MAX_TERMINFO_LENGTH + EXTRA_CAP];
-    char *capability;
     NCURSES_CONST char *name;
     int predval, len;
     PredIdx num_bools = 0;
@@ -999,6 +1034,7 @@ fmt_entry(TERMTYPE2 *tterm,
     }
 
     for_each_string(j, tterm) {
+       char *capability;
        i = StrIndirect(j);
        name = ExtStrname(tterm, (int) i, str_names);
        assert(strlen(name) < sizeof(buffer) - EXTRA_CAP);
@@ -1078,12 +1114,11 @@ fmt_entry(TERMTYPE2 *tterm,
                WRAP_CONCAT;
            } else if (TcOutput()) {
                char *srccap = _nc_tic_expand(capability, TRUE, numbers);
-               int params = (((i < (int) SIZEOF(parametrized)) &&
-                              (i < STRCOUNT))
+               int params = ((i < (int) SIZEOF(parametrized))
                              ? parametrized[i]
                              : ((*srccap == 'k')
                                 ? 0
-                                : has_params(srccap)));
+                                : has_params(srccap, FALSE)));
                char *cv = _nc_infotocap(name, srccap, params);
 
                if (cv == 0) {
@@ -1098,7 +1133,7 @@ fmt_entry(TERMTYPE2 *tterm,
                        char *s = srccap, *d = buffer;
                        int need = 3 + (int) strlen(name);
                        while ((*d = *s++) != 0) {
-                           if ((d - buffer + 1) >= (int) sizeof(buffer)) {
+                           if ((d - buffer + 2) >= (int) sizeof(buffer)) {
                                fprintf(stderr,
                                        "%s: value for %s is too long\n",
                                        _nc_progname,
@@ -1110,7 +1145,8 @@ fmt_entry(TERMTYPE2 *tterm,
                                *d++ = '\\';
                                *d = ':';
                            } else if (*d == '\\') {
-                               *++d = *s++;
+                               if ((*++d = *s++) == '\0')
+                                   break;
                            }
                            d++;
                            *d = '\0';
@@ -1314,10 +1350,11 @@ kill_labels(TERMTYPE2 *tterm, int target)
 {
     int n;
     int result = 0;
-    char *cap;
-    char name[10];
+    char name[20];
 
     for (n = 0; n <= 10; ++n) {
+       char *cap;
+
        _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "lf%d", n);
        cap = find_string(tterm, name);
        if (VALID_STRING(cap)
@@ -1340,10 +1377,11 @@ kill_fkeys(TERMTYPE2 *tterm, int target)
 {
     int n;
     int result = 0;
-    char *cap;
-    char name[10];
+    char name[20];
 
     for (n = 60; n >= 0; --n) {
+       char *cap;
+
        _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n);
        cap = find_string(tterm, name);
        if (VALID_STRING(cap)
@@ -1370,7 +1408,7 @@ one_one_mapping(const char *mapping)
 
     if (VALID_STRING(mapping)) {
        int n = 0;
-       while (mapping[n] != '\0') {
+       while (mapping[n] != '\0' && mapping[n + 1] != '\0') {
            if (isLine(mapping[n]) &&
                mapping[n] != mapping[n + 1]) {
                result = FALSE;
@@ -1445,19 +1483,22 @@ dump_entry(TERMTYPE2 *tterm,
           PredFunc pred)
 {
     TERMTYPE2 save_tterm;
-    int len, critlen;
+    int critlen;
     const char *legend;
     bool infodump;
 
     if (quickdump) {
        char bigbuf[65536];
-       unsigned n;
        unsigned offset = 0;
+
        separator = "";
        trailer = "\n";
        indent = 0;
+
        if (_nc_write_object(tterm, bigbuf, &offset, sizeof(bigbuf)) == OK) {
            char numbuf[80];
+           unsigned n;
+
            if (quickdump & 1) {
                if (outbuf.used)
                    wrap_concat1("\n");
@@ -1472,6 +1513,7 @@ dump_entry(TERMTYPE2 *tterm,
                static char padding[] =
                {0, 0};
                int value = 0;
+
                if (outbuf.used)
                    wrap_concat1("\n");
                wrap_concat1("b64:");
@@ -1522,7 +1564,7 @@ dump_entry(TERMTYPE2 *tterm,
        }
        if (FMT_ENTRY() > critlen) {
            /*
-            * We pick on sgr because it's a nice long string capability that
+            * We pick on sgr because it is a nice long string capability that
             * is really just an optimization hack.  Another good candidate is
             * acsc since it is both long and unused by BSD termcap.
             */
@@ -1567,6 +1609,7 @@ dump_entry(TERMTYPE2 *tterm,
            }
            if (!changed || (FMT_ENTRY() > critlen)) {
                int oldversion = tversion;
+               int len;
 
                tversion = V_BSD;
                SHOW_WHY("# (terminfo-only capabilities suppressed to fit entry within %d bytes)\n",
@@ -1729,9 +1772,8 @@ void
 repair_acsc(TERMTYPE2 *tp)
 {
     if (VALID_STRING(acs_chars)) {
-       size_t n, m;
+       size_t n;
        char mapped[256];
-       char extra = 0;
        unsigned source;
        unsigned target;
        bool fix_needed = FALSE;
@@ -1746,7 +1788,11 @@ repair_acsc(TERMTYPE2 *tp)
            if (acs_chars[n + 1])
                n++;
        }
+
        if (fix_needed) {
+           size_t m;
+           char extra = 0;
+
            memset(mapped, 0, sizeof(mapped));
            for (n = 0; acs_chars[n] != 0; n++) {
                source = UChar(acs_chars[n]);