]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - progs/dump_entry.c
ncurses 6.0 - patch 20170204
[ncurses.git] / progs / dump_entry.c
index d9c309a5bb15e2425856bc846c50fafc147724c8..3be09f8671cbb44cd0eb862357944798d657f5de 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2015,2016 Free Software Foundation, Inc.              *
+ * Copyright (c) 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            *
 #include "termsort.c"          /* this C file is generated */
 #include <parametrized.h>      /* so is this */
 
-MODULE_ID("$Id: dump_entry.c,v 1.141 2016/10/02 01:28:04 tom Exp $")
+MODULE_ID("$Id: dump_entry.c,v 1.148 2017/02/04 16:55:42 tom Exp $")
 
 #define DISCARD(string) string = ABSENT_STRING
 #define PRINTF (void) printf
+#define WRAPPED 32
 
 #define OkIndex(index,array) ((int)(index) >= 0 && (int)(index) < (int) SIZEOF(array))
 #define TcOutput() (outform == F_TERMCAP || outform == F_TCONVERR)
@@ -62,6 +63,7 @@ static int column;            /* current column, limited by 'width' */
 static int oldcol;             /* last value of column before wrap */
 static bool pretty;            /* true if we format if-then-else strings */
 static bool wrapped;           /* true if we wrap too-long strings */
+static bool did_wrap;          /* true if last wrap_concat did wrapping */
 static bool checking;          /* true if we are checking for tic */
 static int quickdump;          /* true if we are dumping compiled data */
 
@@ -123,7 +125,7 @@ strncpy_DYN(DYNBUF * dst, const char *src, size_t need)
        if (dst->text == 0)
            failed("strncpy_DYN");
     }
-    _nc_STRNCPY(dst->text + dst->used, src, need);
+    _nc_STRNCPY(dst->text + dst->used, src, need + 1);
     dst->used += need;
     dst->text[dst->used] = 0;
 }
@@ -208,6 +210,8 @@ dump_init(const char *version,
     checking = check;
     quickdump = (quick & 3);
 
+    did_wrap = (width <= 0);
+
     /* versions */
     if (version == 0)
        tversion = V_ALLCAPS;
@@ -556,6 +560,7 @@ wrap_concat(const char *src)
     int gaps = (int) strlen(separator);
     int want = gaps + need;
 
+    did_wrap = (width <= 0);
     if (column > indent
        && column + want > width) {
        force_wrap();
@@ -565,14 +570,15 @@ wrap_concat(const char *src)
        (column + want) > width &&
        (!TcOutput() || strncmp(src, "..", 2))) {
        int step = 0;
-       int used = width > 32 ? width : 32;
+       int used = width > WRAPPED ? width : WRAPPED;
        int size = used;
        int base = 0;
        char *p, align[9];
        const char *my_t = trailer;
        char *fill = fill_spaces(src);
+       int last = (int) strlen(fill);
 
-       need = (int) strlen(fill);
+       need = last;
 
        if (TcOutput())
            trailer = "\\\n\t ";
@@ -581,25 +587,30 @@ wrap_concat(const char *src)
            base = (int) (p + 1 - fill);
            if (base > 8)
                base = 8;
-           sprintf(align, "%*s", base, " ");
+           _nc_SPRINTF(align, _nc_SLIMIT(align) "%*s", base, " ");
        } else {
            align[base] = '\0';
        }
-       while ((column + (need + gaps)) > used) {
-           size = used;
-           if (size > ((int) strlen(fill) - step)) {
-               size = ((int) strlen(fill) - step);
-           }
-           if (step) {
-               strcpy_DYN(&outbuf, align);
-               size -= base;
+       /* "pretty" overrides wrapping if it already split the line */
+       if (!pretty || strchr(fill, '\n') == 0) {
+           while ((column + (need + gaps)) > used) {
+               size = used;
+               if (step) {
+                   strcpy_DYN(&outbuf, align);
+                   size -= base;
+               }
+               if (size > (last - step)) {
+                   size = (last - step);
+               }
+               size = find_split(fill, step, size);
+               strncpy_DYN(&outbuf, fill + step, (size_t) size);
+               step += size;
+               need -= size;
+               if (need > 0) {
+                   force_wrap();
+                   did_wrap = TRUE;
+               }
            }
-           size = find_split(fill, step, size);
-           strncpy_DYN(&outbuf, fill + step, (size_t) size);
-           step += size;
-           need -= size;
-           if (need > 0)
-               force_wrap();
        }
        if (need > 0) {
            if (step)
@@ -1138,9 +1149,11 @@ fmt_entry(TERMTYPE *tterm,
     if (outcount) {
        bool trimmed = FALSE;
        j = (PredIdx) outbuf.used;
-       if (j >= 2
-           && outbuf.text[j - 1] == '\t'
-           && outbuf.text[j - 2] == '\n') {
+       if (wrapped && did_wrap) {
+           /* EMPTY */ ;
+       } else if (j >= 2
+                  && outbuf.text[j - 1] == '\t'
+                  && outbuf.text[j - 2] == '\n') {
            outbuf.used -= 2;
            trimmed = TRUE;
        } else if (j >= 4
@@ -1315,16 +1328,16 @@ encode_b64(char *target, char *source, unsigned state, int *saved)
 
     switch (state % 3) {
     case 0:
-       *target++ = data[ch & 077];
-       *saved = (ch >> 6) & 3;
+       *target++ = data[(ch >> 2) & 077];
+       *saved = (ch << 4);
        break;
     case 1:
-       *target++ = data[((ch << 2) | *saved) & 077];
-       *saved = (ch >> 4) & 017;
+       *target++ = data[((ch >> 4) | *saved) & 077];
+       *saved = (ch << 2);
        break;
     case 2:
-       *target++ = data[((ch << 4) | *saved) & 077];
-       *target++ = data[(ch >> 2) & 077];
+       *target++ = data[((ch >> 6) | *saved) & 077];
+       *target++ = data[ch & 077];
        *saved = 0;
        break;
     }
@@ -1366,6 +1379,8 @@ dump_entry(TERMTYPE *tterm,
                }
            }
            if (quickdump & 2) {
+               static char padding[] =
+               {0, 0};
                int value = 0;
                if (outbuf.used)
                    wrap_concat("\n");
@@ -1378,10 +1393,14 @@ dump_entry(TERMTYPE *tterm,
                case 0:
                    break;
                case 1:
-                   wrap_concat("===");
+                   encode_b64(numbuf, padding, 1, &value);
+                   wrap_concat(numbuf);
+                   wrap_concat("==");
                    break;
                case 2:
-                   wrap_concat("==");
+                   encode_b64(numbuf, padding, 1, &value);
+                   wrap_concat(numbuf);
+                   wrap_concat("=");
                    break;
                }
            }