]> git.proxmox.com Git - mirror_frr.git/blobdiff - lib/termtable.c
Merge pull request #5793 from ton31337/fix/formatting_show_bgp_summary_failed
[mirror_frr.git] / lib / termtable.c
index 283fa173d8eaad5c707625e0a3ab2235887e6a61..b22a1ad38778e4ca18cd4d8e2693961b436907aa 100644 (file)
 #include <zebra.h>
 #include <stdio.h>
 
+#include "printfrr.h"
 #include "memory.h"
 #include "termtable.h"
 
 DEFINE_MTYPE_STATIC(LIB, TTABLE, "ASCII table")
 
 /* clang-format off */
-struct ttable_style ttable_styles[] = {
+const struct ttable_style ttable_styles[] = {
        {       // default ascii
                .corner = '+',
                .rownums_on = false,
                .indent = 1,
-               .border.top = '-',
-               .border.bottom = '-',
-               .border.left = '|',
-               .border.right = '|',
-               .border.top_on = true,
-               .border.bottom_on = true,
-               .border.left_on = true,
-               .border.right_on = true,
-               .cell.lpad = 1,
-               .cell.rpad = 1,
-               .cell.align = LEFT,
-               .cell.border.bottom = '-',
-               .cell.border.bottom_on = true,
-               .cell.border.top = '-',
-               .cell.border.top_on = false,
-               .cell.border.right = '|',
-               .cell.border.right_on = true,
-               .cell.border.left = '|',
-               .cell.border.left_on = false,
+               .border = {
+                       .top = '-',
+                       .bottom = '-',
+                       .left = '|',
+                       .right = '|',
+                       .top_on = true,
+                       .bottom_on = true,
+                       .left_on = true,
+                       .right_on = true,
+               },
+               .cell = {
+                       .lpad = 1,
+                       .rpad = 1,
+                       .align = LEFT,
+                       .border = {
+                               .bottom = '-',
+                               .bottom_on = true,
+                               .top = '-',
+                               .top_on = false,
+                               .right = '|',
+                               .right_on = true,
+                               .left = '|',
+                               .left_on = false,
+                       },
+               },
        }, {    // blank, suitable for plaintext alignment
                .corner = ' ',
                .rownums_on = false,
                .indent = 1,
-               .border.top = ' ',
-               .border.bottom = ' ',
-               .border.left = ' ',
-               .border.right = ' ',
-               .border.top_on = false,
-               .border.bottom_on = false,
-               .border.left_on = false,
-               .border.right_on = false,
-               .cell.lpad = 0,
-               .cell.rpad = 3,
-               .cell.align = LEFT,
-               .cell.border.bottom = ' ',
-               .cell.border.bottom_on = false,
-               .cell.border.top = ' ',
-               .cell.border.top_on = false,
-               .cell.border.right = ' ',
-               .cell.border.right_on = false,
-               .cell.border.left = ' ',
-               .cell.border.left_on = false,
+               .border = {
+                       .top = ' ',
+                       .bottom = ' ',
+                       .left = ' ',
+                       .right = ' ',
+                       .top_on = false,
+                       .bottom_on = false,
+                       .left_on = false,
+                       .right_on = false,
+               },
+               .cell = {
+                       .lpad = 0,
+                       .rpad = 3,
+                       .align = LEFT,
+                       .border = {
+                               .bottom = ' ',
+                               .bottom_on = false,
+                               .top = ' ',
+                               .top_on = false,
+                               .right = ' ',
+                               .right_on = false,
+                               .left = ' ',
+                               .left_on = false,
+                       },
+               }
    }
 };
 /* clang-format on */
@@ -86,7 +99,7 @@ void ttable_del(struct ttable *tt)
        XFREE(MTYPE_TTABLE, tt);
 }
 
-struct ttable *ttable_new(struct ttable_style *style)
+struct ttable *ttable_new(const struct ttable_style *style)
 {
        struct ttable *tt;
 
@@ -122,14 +135,15 @@ static struct ttable_cell *ttable_insert_row_va(struct ttable *tt, int i,
 {
        assert(i >= -1 && i < tt->nrows);
 
+       char shortbuf[256];
        char *res, *orig, *section;
        struct ttable_cell *row;
        int col = 0;
        int ncols = 0;
 
        /* count how many columns we have */
-       for (int i = 0; format[i]; i++)
-               ncols += !!(format[i] == '|');
+       for (int j = 0; format[j]; j++)
+               ncols += !!(format[j] == '|');
        ncols++;
 
        if (tt->ncols == 0)
@@ -146,19 +160,18 @@ static struct ttable_cell *ttable_insert_row_va(struct ttable *tt, int i,
        /* CALLOC a block of cells */
        row = XCALLOC(MTYPE_TTABLE, tt->ncols * sizeof(struct ttable_cell));
 
-       res = NULL;
-       vasprintf(&res, format, ap);
-
+       res = vasnprintfrr(MTYPE_TMP, shortbuf, sizeof(shortbuf), format, ap);
        orig = res;
 
-       while (res) {
+       while (res && col < tt->ncols) {
                section = strsep(&res, "|");
                row[col].text = XSTRDUP(MTYPE_TTABLE, section);
                row[col].style = tt->style.cell;
                col++;
        }
 
-       free(orig);
+       if (orig != shortbuf)
+               XFREE(MTYPE_TMP, orig);
 
        /* insert row */
        if (i == -1 || i == tt->nrows)
@@ -349,7 +362,7 @@ char *ttable_dump(struct ttable *tt, const char *newline)
        rsize = nl_len + (tt->style.border.right_on ? 1 : 0);
        right = XCALLOC(MTYPE_TTABLE, rsize);
 
-       memset (left, ' ', lsize);
+       memset(left, ' ', lsize);
 
        if (tt->style.border.left_on)
                left[lsize - 1] = tt->style.border.left;
@@ -383,7 +396,7 @@ char *ttable_dump(struct ttable *tt, const char *newline)
                        memcpy(&buf[pos], left, lsize);
                        pos += lsize;
 
-                       for (size_t i = 0; i < width - lsize - rsize; i++)
+                       for (size_t l = 0; l < width - lsize - rsize; l++)
                                buf[pos++] = row[0].style.border.top;
 
                        pos -= width - lsize - rsize;
@@ -409,7 +422,7 @@ char *ttable_dump(struct ttable *tt, const char *newline)
                                buf[pos++] = row[j].style.border.left;
 
                        /* print left padding */
-                       for (int i = 0; i < row[j].style.lpad; i++)
+                       for (int k = 0; k < row[j].style.lpad; k++)
                                buf[pos++] = ' ';
 
                        /* calculate padding for sprintf */
@@ -431,7 +444,7 @@ char *ttable_dump(struct ttable *tt, const char *newline)
                        pos += sprintf(&buf[pos], fmt, abspad, row[j].text);
 
                        /* print right padding */
-                       for (int i = 0; i < row[j].style.rpad; i++)
+                       for (int k = 0; k < row[j].style.rpad; k++)
                                buf[pos++] = ' ';
 
                        /* if right border && not last col print right border */
@@ -447,7 +460,7 @@ char *ttable_dump(struct ttable *tt, const char *newline)
                        memcpy(&buf[pos], left, lsize);
                        pos += lsize;
 
-                       for (size_t i = 0; i < width - lsize - rsize; i++)
+                       for (size_t l = 0; l < width - lsize - rsize; l++)
                                buf[pos++] = row[0].style.border.bottom;
 
                        pos -= width - lsize - rsize;
@@ -471,7 +484,7 @@ char *ttable_dump(struct ttable *tt, const char *newline)
                memcpy(&buf[pos], left, lsize);
                pos += lsize;
 
-               for (size_t i = 0; i < width - lsize - rsize; i++)
+               for (size_t l = 0; l < width - lsize - rsize; l++)
                        buf[pos++] = tt->style.border.bottom;
 
                memcpy(&buf[pos], right, rsize);