vsh-table.c 10.5 KB
Newer Older
S
Simon Kobyda 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
/*
 * vsh-table.c: table printing helper
 *
 * Copyright (C) 2018 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include "vsh-table.h"

#include <stdarg.h>
#include <stddef.h>
#include <wchar.h>
#include <wctype.h>
#include "c-ctype.h"

#include "viralloc.h"
#include "virbuffer.h"
#include "virstring.h"
#include "virsh-util.h"

#define HEX_ENCODE_LENGTH 4 /* represents length of '\xNN' */

struct _vshTableRow {
    char **cells;
    size_t ncells;
};


struct _vshTable {
    vshTableRowPtr *rows;
    size_t nrows;
};


static void
vshTableRowFree(vshTableRowPtr row)
{
    size_t i;

    if (!row)
        return;

    for (i = 0; i < row->ncells; i++)
        VIR_FREE(row->cells[i]);

    VIR_FREE(row->cells);
    VIR_FREE(row);
}


void
vshTableFree(vshTablePtr table)
{
    size_t i;

    if (!table)
        return;

    for (i = 0; i < table->nrows; i++)
        vshTableRowFree(table->rows[i]);
    VIR_FREE(table->rows);
    VIR_FREE(table);
}


/**
 * vshTableRowNew:
 * @arg: the first argument.
 * @ap: list of variadic arguments
 *
 * Create a new row in the table. Each argument passed
 * represents a cell in the row.
 *
 * Return: pointer to vshTableRowPtr row or NULL.
 */
static vshTableRowPtr
vshTableRowNew(const char *arg, va_list ap)
{
    vshTableRowPtr row = NULL;

    if (!arg) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Table row cannot be empty"));
        goto error;
    }

    if (VIR_ALLOC(row) < 0)
        goto error;

    while (arg) {
        char *tmp = NULL;

107
        tmp = g_strdup(arg);
S
Simon Kobyda 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

        if (VIR_APPEND_ELEMENT(row->cells, row->ncells, tmp) < 0) {
            VIR_FREE(tmp);
            goto error;
        }

        arg = va_arg(ap, const char *);
    }

    return row;

 error:
    vshTableRowFree(row);
    return NULL;
}


/**
 * vshTableNew:
 * @arg: List of column names (NULL terminated)
 *
 * Create a new table.
 *
 * Returns: pointer to table or NULL.
 */
vshTablePtr
vshTableNew(const char *arg, ...)
{
    vshTablePtr table = NULL;
    vshTableRowPtr header = NULL;
    va_list ap;

    if (VIR_ALLOC(table) < 0)
        goto error;

    va_start(ap, arg);
    header = vshTableRowNew(arg, ap);
    va_end(ap);

    if (!header)
        goto error;

    if (VIR_APPEND_ELEMENT(table->rows, table->nrows, header) < 0)
        goto error;

    return table;
 error:
    vshTableRowFree(header);
    vshTableFree(table);
    return NULL;
}


/**
 * vshTableRowAppend:
 * @table: table to append to
 * @arg: cells of the row (NULL terminated)
 *
 * Append new row into the @table. The number of cells in the row has
 * to be equal to the number of cells in the table header.
 *
 * Returns: 0 if succeeded, -1 if failed.
 */
int
vshTableRowAppend(vshTablePtr table, const char *arg, ...)
{
    vshTableRowPtr row = NULL;
    size_t ncolumns = table->rows[0]->ncells;
    va_list ap;
    int ret = -1;

    va_start(ap, arg);
    row = vshTableRowNew(arg, ap);
    va_end(ap);

    if (!row)
        goto cleanup;

    if (ncolumns != row->ncells) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Incorrect number of cells in a table row"));
        goto cleanup;
    }

    if (VIR_APPEND_ELEMENT(table->rows, table->nrows, row) < 0)
        goto cleanup;

    ret = 0;
 cleanup:
    vshTableRowFree(row);
    return ret;
}


/**
 * Function pulled from util-linux
 *
 * Function's name in util-linux: mbs_safe_encode_to_buffer
 *
 * Returns allocated string where all control and non-printable chars are
 * replaced with \x?? hex sequence, or NULL.
 */
static char *
vshTableSafeEncode(const char *s, size_t *width)
{
    const char *p = s;
    size_t sz = s ? strlen(s) : 0;
    char *buf;
    char *ret;
    mbstate_t st;

    memset(&st, 0, sizeof(st));

    if (VIR_ALLOC_N(buf, (sz * HEX_ENCODE_LENGTH) + 1) < 0)
        return NULL;

    ret = buf;
    *width = 0;

    while (p && *p) {
        if ((*p == '\\' && *(p + 1) == 'x') ||
229
            g_ascii_iscntrl(*p)) {
230
            g_snprintf(buf, HEX_ENCODE_LENGTH + 1, "\\x%02x", *p);
S
Simon Kobyda 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
            buf += HEX_ENCODE_LENGTH;
            *width += HEX_ENCODE_LENGTH;
            p++;
        } else {
            wchar_t wc;
            size_t len = mbrtowc(&wc, p, MB_CUR_MAX, &st);

            if (len == 0)
                break;		/* end of string */

            if (len == (size_t) -1 || len == (size_t) -2) {
                len = 1;
                /*
                 * Not valid multibyte sequence -- maybe it's
                 * printable char according to the current locales.
                 */
                if (!c_isprint(*p)) {
248
                    g_snprintf(buf, HEX_ENCODE_LENGTH + 1, "\\x%02x", *p);
S
Simon Kobyda 已提交
249 250 251 252 253 254 255 256 257
                    buf += HEX_ENCODE_LENGTH;
                    *width += HEX_ENCODE_LENGTH;
                } else {
                    *buf++ = *p;
                    (*width)++;
                }
            } else if (!iswprint(wc)) {
                size_t i;
                for (i = 0; i < len; i++) {
258
                    g_snprintf(buf, HEX_ENCODE_LENGTH + 1, "\\x%02x", p[i]);
S
Simon Kobyda 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
                    buf += HEX_ENCODE_LENGTH;
                    *width += HEX_ENCODE_LENGTH;
                }
            } else {
                memcpy(buf, p, len);
                buf += len;
                *width += wcwidth(wc);
            }
            p += len;
        }
    }

    *buf = '\0';
    return ret;
}


/**
 * vshTableGetColumnsWidths:
 * @table: table
 * @maxwidths: maximum count of characters for each columns
 * @widths: count of characters for each cell in the table
 *
 * Fill passed @maxwidths and @widths arrays with maximum number
 * of characters for columns and number of character per each
 * table cell, respectively.
 * Handle unicode strings (user must have multibyte locale)
 *
 * Return 0 in case of success, -1 otherwise.
 */
static int
vshTableGetColumnsWidths(vshTablePtr table,
                         size_t *maxwidths,
                         size_t **widths,
                         bool header)
{
    size_t i;

    i = header? 0 : 1;
    for (; i < table->nrows; i++) {
        vshTableRowPtr row = table->rows[i];
        size_t j;

        for (j = 0; j < row->ncells; j++) {
            size_t size = 0;
            /* need to replace nonprintable and control characters,
             * because width of some of those characters (e.g. \t, \v, \b ...)
             * cannot be counted properly */
            char *tmp = vshTableSafeEncode(row->cells[j], &size);
            if (!tmp)
                return -1;

            VIR_FREE(row->cells[j]);
            row->cells[j] = tmp;
            widths[i][j] = size;

            if (widths[i][j] > maxwidths[j])
                maxwidths[j] = widths[i][j];
        }
    }

    return 0;
}


/**
 * vshTableRowPrint:
 * @row: table to append to
 * @maxwidths: maximum count of characters for each columns
 * @widths: count of character for each cell in this row
 * @buf: buffer to store table (only if @toStdout == true)
 */
static void
vshTableRowPrint(vshTableRowPtr row,
                 size_t *maxwidths,
                 size_t *widths,
                 virBufferPtr buf)
{
    size_t i;
    size_t j;

    for (i = 0; i < row->ncells; i++) {
        virBufferAsprintf(buf, " %s", row->cells[i]);

343 344 345 346
        if (i < (row->ncells - 1)) {
            for (j = 0; j < maxwidths[i] - widths[i] + 2; j++)
                virBufferAddChar(buf, ' ');
        }
S
Simon Kobyda 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
    }
    virBufferAddChar(buf, '\n');
}


/**
 * vshTablePrint:
 * @table: table to print
 * @header: whetever to print to header (true) or not (false)
 * this argument is relevant only if @ctl == NULL
 *
 * Get table. To get an alignment of columns right, function
 * fills 2d array @widths with count of characters in each cell and
 * array @maxwidths maximum count of character in each column.
 * Function then prints tables header and content.
 *
 * Return string containing table, or NULL
 */
static char *
vshTablePrint(vshTablePtr table, bool header)
{
    size_t i;
    size_t j;
    size_t *maxwidths;
    size_t **widths;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *ret = NULL;

    if (VIR_ALLOC_N(maxwidths, table->rows[0]->ncells))
        goto cleanup;

    if (VIR_ALLOC_N(widths, table->nrows))
        goto cleanup;

    /* retrieve widths of columns */
    for (i = 0; i < table->nrows; i++) {
        if (VIR_ALLOC_N(widths[i], table->rows[0]->ncells))
            goto cleanup;
    }

    if (vshTableGetColumnsWidths(table, maxwidths, widths, header) < 0)
        goto cleanup;

    if (header) {
        /* print header */
        vshTableRowPrint(table->rows[0], maxwidths, widths[0], &buf);

        /* print dividing line  */
        for (i = 0; i < table->rows[0]->ncells; i++) {
            for (j = 0; j < maxwidths[i] + 3; j++)
                virBufferAddChar(&buf, '-');
        }
        virBufferAddChar(&buf, '\n');
    }
    /* print content */
    for (i = 1; i < table->nrows; i++)
        vshTableRowPrint(table->rows[i], maxwidths, widths[i], &buf);

    ret = virBufferContentAndReset(&buf);

 cleanup:
    VIR_FREE(maxwidths);
    for (i = 0; i < table->nrows; i++)
        VIR_FREE(widths[i]);
    VIR_FREE(widths);
    return ret;
}


/**
 * vshTablePrintToStdout:
 * @table: table to print
 * @ctl virtshell control structure
 *
 * Print table returned in string to stdout.
422
 * If effect on vshControl structure on printing function changes in future
S
Simon Kobyda 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
 * (apart from quiet mode) this code may need update
 */
void
vshTablePrintToStdout(vshTablePtr table, vshControl *ctl)
{
    bool header;
    char *out;

    header = ctl ? !ctl->quiet : true;

    out = vshTablePrintToString(table, header);
    if (out)
        vshPrint(ctl, "%s", out);

    VIR_FREE(out);
}


/**
 * vshTablePrintToString:
 * @table: table to print
 * @header: whetever to print to header (true) or not (false)
 *
 * Return string containing table, or NULL if table was printed to
 * stdout. User will have to free returned string.
 */
char *
vshTablePrintToString(vshTablePtr table, bool header)
{
    return vshTablePrint(table, header);
}