From nobody Thu Apr 25 17:36:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1535039633419760.028249396921; Thu, 23 Aug 2018 08:53:53 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A3AC58CDD; Thu, 23 Aug 2018 15:53:51 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6A0605F726; Thu, 23 Aug 2018 15:53:51 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 194F918005D0; Thu, 23 Aug 2018 15:53:51 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w7NFrnKe028062 for ; Thu, 23 Aug 2018 11:53:49 -0400 Received: by smtp.corp.redhat.com (Postfix) id 4785510CD893; Thu, 23 Aug 2018 15:53:49 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.43.2.88]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9AA5710CD890; Thu, 23 Aug 2018 15:53:48 +0000 (UTC) From: Simon Kobyda To: libvir-list@redhat.com Date: Thu, 23 Aug 2018 17:53:41 +0200 Message-Id: <20180823155343.29044-2-skobyda@redhat.com> In-Reply-To: <20180823155343.29044-1-skobyda@redhat.com> References: <20180823155343.29044-1-skobyda@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-loop: libvir-list@redhat.com Cc: Simon Kobyda Subject: [libvirt] [PATCH v5 1/3] vsh: Add API for printing tables. X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 23 Aug 2018 15:53:52 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" It solves problems with alignment of columns. Width of each column is calculated by its biggest cell. Should solve unicode bug. In future, it may be implemented in virsh, virt-admin... This API has 5 public functions: - vshTableNew - adds new table and defines its header - vshTableRowAppend - appends new row (for same number of columns as in header) - vshTablePrintToStdout - vshTablePrintToString - vshTableFree https://bugzilla.redhat.com/show_bug.cgi?id=3D1574624 https://bugzilla.redhat.com/show_bug.cgi?id=3D1584630 Signed-off-by: Simon Kobyda --- tools/Makefile.am | 4 +- tools/vsh-table.c | 449 ++++++++++++++++++++++++++++++++++++++++++++++ tools/vsh-table.h | 42 +++++ 3 files changed, 494 insertions(+), 1 deletion(-) create mode 100644 tools/vsh-table.c create mode 100644 tools/vsh-table.h diff --git a/tools/Makefile.am b/tools/Makefile.am index 1452d984a0..f069167acc 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -144,7 +144,9 @@ libvirt_shell_la_LIBADD =3D \ $(READLINE_LIBS) \ ../gnulib/lib/libgnu.la \ $(NULL) -libvirt_shell_la_SOURCES =3D vsh.c vsh.h +libvirt_shell_la_SOURCES =3D \ + vsh.c vsh.h \ + vsh-table.c vsh-table.h =20 virt_host_validate_SOURCES =3D \ virt-host-validate.c \ diff --git a/tools/vsh-table.c b/tools/vsh-table.c new file mode 100644 index 0000000000..ca4e9265c5 --- /dev/null +++ b/tools/vsh-table.c @@ -0,0 +1,449 @@ +/* + * 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 + * . + * + * Authors: + * Simon Kobyda + * + */ + +#include +#include "vsh-table.h" + +#include +#include +#include +#include +#include +#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 =3D 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 =3D 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 =3D 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 =3D NULL; + + if (VIR_STRDUP(tmp, arg) < 0) + goto error; + + if (VIR_APPEND_ELEMENT(row->cells, row->ncells, tmp) < 0) { + VIR_FREE(tmp); + goto error; + } + + arg =3D 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 =3D NULL; + vshTableRowPtr header =3D NULL; + va_list ap; + + if (VIR_ALLOC(table) < 0) + goto error; + + va_start(ap, arg); + header =3D 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 =3D NULL; + size_t ncolumns =3D table->rows[0]->ncells; + va_list ap; + int ret =3D -1; + + va_start(ap, arg); + row =3D vshTableRowNew(arg, ap); + va_end(ap); + + if (!row) + goto cleanup; + + if (ncolumns !=3D 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 =3D 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 =3D s; + size_t sz =3D 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; + + if (!sz) + return NULL; + + ret =3D buf; + *width =3D 0; + + while (p && *p) { + if ((*p =3D=3D '\\' && *(p + 1) =3D=3D 'x') || + c_iscntrl(*p)) { + snprintf(buf, HEX_ENCODE_LENGTH + 1, "\\x%02x", *p); + buf +=3D HEX_ENCODE_LENGTH; + *width +=3D HEX_ENCODE_LENGTH; + p++; + } else { + wchar_t wc; + size_t len =3D mbrtowc(&wc, p, MB_CUR_MAX, &st); + + if (len =3D=3D 0) + break; /* end of string */ + + if (len =3D=3D (size_t) -1 || len =3D=3D (size_t) -2) { + len =3D 1; + /* + * Not valid multibyte sequence -- maybe it's + * printable char according to the current locales. + */ + if (!c_isprint(*p)) { + snprintf(buf, HEX_ENCODE_LENGTH + 1, "\\x%02x", *p); + buf +=3D HEX_ENCODE_LENGTH; + *width +=3D HEX_ENCODE_LENGTH; + } else { + (*width)++; + *buf++ =3D *p; + } + } else if (!iswprint(wc)) { + size_t i; + for (i =3D 0; i < len; i++) { + snprintf(buf, HEX_ENCODE_LENGTH + 1, "\\x%02x", p[i]); + buf +=3D HEX_ENCODE_LENGTH; + *width +=3D HEX_ENCODE_LENGTH; + } + } else { + memcpy(buf, p, len); + buf +=3D len; + *width +=3D wcwidth(wc); + } + p +=3D len; + } + } + + *buf =3D '\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; + size_t j; + + i =3D header? 0 : 1; + for (; i < table->nrows; i++) { + vshTableRowPtr row =3D table->rows[i]; + + for (j =3D 0; j < row->ncells; j++) { + size_t size =3D 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 =3D vshTableSafeEncode(row->cells[j], &size); + if (!tmp) + return -1; + + VIR_FREE(row->cells[j]); + row->cells[j] =3D tmp; + widths[i][j] =3D size; + + if (widths[i][j] > maxwidths[j]) + maxwidths[j] =3D 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 =3D=3D true) + */ +static void +vshTableRowPrint(vshTableRowPtr row, + size_t *maxwidths, + size_t *widths, + virBufferPtr buf) +{ + size_t i; + size_t j; + + for (i =3D 0; i < row->ncells; i++) { + virBufferAsprintf(buf, " %s", row->cells[i]); + + for (j =3D 0; j < maxwidths[i] - widths[i] + 2; j++) + virBufferAddChar(buf, ' '); + } + 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 =3D=3D 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 =3D VIR_BUFFER_INITIALIZER; + char *ret =3D 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 =3D 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 =3D 0; i < table->rows[0]->ncells; i++) { + for (j =3D 0; j < maxwidths[i] + 3; j++) + virBufferAddChar(&buf, '-'); + } + virBufferAddChar(&buf, '\n'); + } + /* print content */ + for (i =3D 1; i < table->nrows; i++) + vshTableRowPrint(table->rows[i], maxwidths, widths[i], &buf); + + ret =3D virBufferContentAndReset(&buf); + + cleanup: + VIR_FREE(maxwidths); + for (i =3D 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. + * If effect on vshControl structure on priting function changes in future + * (apart from quiet mode) this code may need update + */ +void +vshTablePrintToStdout(vshTablePtr table, vshControl *ctl) +{ + bool header; + char *out; + + header =3D ctl ? !ctl->quiet : true; + + out =3D 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); +} diff --git a/tools/vsh-table.h b/tools/vsh-table.h new file mode 100644 index 0000000000..e4e9582b9f --- /dev/null +++ b/tools/vsh-table.h @@ -0,0 +1,42 @@ +/* + * vsh-table.h: 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 + * . + * + * Authors: + * Simon Kobyda + * + */ + +#ifndef VSH_TABLE_H +# define VSH_TABLE_H + +# include "vsh.h" + +/* forward declarations */ +typedef struct _vshTable vshTable; +typedef struct _vshTableRow vshTableRow; +typedef vshTable *vshTablePtr; +typedef vshTableRow *vshTableRowPtr; + +void vshTableFree(vshTablePtr table); +vshTablePtr vshTableNew(const char *format, ...); +int vshTableRowAppend(vshTablePtr table, const char *arg, ...); +void vshTablePrintToStdout(vshTablePtr table, vshControl *ctl); +char *vshTablePrintToString(vshTablePtr table, bool header); + +#endif /* VSH_TABLE_H */ --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu Apr 25 17:36:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 153503979220080.6107587663065; Thu, 23 Aug 2018 08:56:32 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7553030832D7; Thu, 23 Aug 2018 15:56:30 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2EC501001F5D; Thu, 23 Aug 2018 15:56:30 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id CFBC0181A12F; Thu, 23 Aug 2018 15:56:29 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w7NFroq4028070 for ; Thu, 23 Aug 2018 11:53:50 -0400 Received: by smtp.corp.redhat.com (Postfix) id 176AA10CD893; Thu, 23 Aug 2018 15:53:50 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.43.2.88]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8661D10CD890; Thu, 23 Aug 2018 15:53:49 +0000 (UTC) From: Simon Kobyda To: libvir-list@redhat.com Date: Thu, 23 Aug 2018 17:53:42 +0200 Message-Id: <20180823155343.29044-3-skobyda@redhat.com> In-Reply-To: <20180823155343.29044-1-skobyda@redhat.com> References: <20180823155343.29044-1-skobyda@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-loop: libvir-list@redhat.com Cc: Simon Kobyda Subject: [libvirt] [PATCH v5 2/3] virsh: Implement new table API for virsh list X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Thu, 23 Aug 2018 15:56:31 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Instead of printing it straight in virsh, it creates table struct which is filled with header and rows(domains). It allows us to know more about table before printing to calculate alignment right. Signed-off-by: Simon Kobyda --- tests/virshtest.c | 14 ++++++------ tools/virsh-domain-monitor.c | 43 ++++++++++++++++++++---------------- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/tests/virshtest.c b/tests/virshtest.c index 94548a82d1..10cd0d356b 100644 --- a/tests/virshtest.c +++ b/tests/virshtest.c @@ -98,9 +98,9 @@ static int testCompareListDefault(const void *data ATTRIB= UTE_UNUSED) { const char *const argv[] =3D { VIRSH_DEFAULT, "list", NULL }; const char *exp =3D "\ - Id Name State\n\ -----------------------------------------------------\n\ - 1 test running\n\ + Id Name State \n\ +----------------------\n\ + 1 test running \n\ \n"; return testCompareOutputLit(exp, NULL, argv); } @@ -109,10 +109,10 @@ static int testCompareListCustom(const void *data ATT= RIBUTE_UNUSED) { const char *const argv[] =3D { VIRSH_CUSTOM, "list", NULL }; const char *exp =3D "\ - Id Name State\n\ -----------------------------------------------------\n\ - 1 fv0 running\n\ - 2 fc4 running\n\ + Id Name State \n\ +----------------------\n\ + 1 fv0 running \n\ + 2 fc4 running \n\ \n"; return testCompareOutputLit(exp, NULL, argv); } diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c index b9b4f9739b..adc5bb1a7a 100644 --- a/tools/virsh-domain-monitor.c +++ b/tools/virsh-domain-monitor.c @@ -39,6 +39,7 @@ #include "virmacaddr.h" #include "virxml.h" #include "virstring.h" +#include "vsh-table.h" =20 VIR_ENUM_DECL(virshDomainIOError) VIR_ENUM_IMPL(virshDomainIOError, @@ -1901,6 +1902,7 @@ cmdList(vshControl *ctl, const vshCmd *cmd) char id_buf[INT_BUFSIZE_BOUND(unsigned int)]; unsigned int id; unsigned int flags =3D VIR_CONNECT_LIST_DOMAINS_ACTIVE; + vshTablePtr table =3D NULL; =20 /* construct filter flags */ if (vshCommandOptBool(cmd, "inactive") || @@ -1940,15 +1942,12 @@ cmdList(vshControl *ctl, const vshCmd *cmd) /* print table header in legacy mode */ if (optTable) { if (optTitle) - vshPrintExtra(ctl, " %-5s %-30s %-10s %-20s\n%s\n", - _("Id"), _("Name"), _("State"), _("Title"), - "-----------------------------------------" - "-----------------------------------------"); + table =3D vshTableNew("Id", "Name", "State", "Title", NULL); else - vshPrintExtra(ctl, " %-5s %-30s %s\n%s\n", - _("Id"), _("Name"), _("State"), - "-----------------------------------------" - "-----------"); + table =3D vshTableNew("Id", "Name", "State", NULL); + + if (!table) + goto cleanup; } =20 for (i =3D 0; i < list->ndomains; i++) { @@ -1973,20 +1972,22 @@ cmdList(vshControl *ctl, const vshCmd *cmd) if (optTitle) { if (!(title =3D virshGetDomainDescription(ctl, dom, true, = 0))) goto cleanup; - - vshPrint(ctl, " %-5s %-30s %-10s %-20s\n", id_buf, - virDomainGetName(dom), - state =3D=3D -2 ? _("saved") - : virshDomainStateToString(state), - title); - + if (vshTableRowAppend(table, id_buf, + virDomainGetName(dom), + state =3D=3D -2 ? _("saved") + : virshDomainStateToString(state), + title, NULL) < 0) + goto cleanup; VIR_FREE(title); } else { - vshPrint(ctl, " %-5s %-30s %s\n", id_buf, - virDomainGetName(dom), - state =3D=3D -2 ? _("saved") - : virshDomainStateToString(state)); + if (vshTableRowAppend(table, id_buf, + virDomainGetName(dom), + state =3D=3D -2 ? _("saved") + : virshDomainStateToString(state), + NULL) < 0) + goto cleanup; } + } else if (optUUID && optName) { if (virDomainGetUUIDString(dom, uuid) < 0) { vshError(ctl, "%s", _("Failed to get domain's UUID")); @@ -2004,8 +2005,12 @@ cmdList(vshControl *ctl, const vshCmd *cmd) } } =20 + if (optTable) + vshTablePrintToStdout(table, ctl); + ret =3D true; cleanup: + vshTableFree(table); virshDomainListFree(list); return ret; } --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Thu Apr 25 17:36:42 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 153503963974445.983333502641244; Thu, 23 Aug 2018 08:53:59 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 79E7630832C2; Thu, 23 Aug 2018 15:53:57 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3ED802010D15; Thu, 23 Aug 2018 15:53:57 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id E40A5181A130; Thu, 23 Aug 2018 15:53:56 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w7NFrpaZ028075 for ; Thu, 23 Aug 2018 11:53:51 -0400 Received: by smtp.corp.redhat.com (Postfix) id E330610CD893; Thu, 23 Aug 2018 15:53:50 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.43.2.88]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5648310CD890; Thu, 23 Aug 2018 15:53:50 +0000 (UTC) From: Simon Kobyda To: libvir-list@redhat.com Date: Thu, 23 Aug 2018 17:53:43 +0200 Message-Id: <20180823155343.29044-4-skobyda@redhat.com> In-Reply-To: <20180823155343.29044-1-skobyda@redhat.com> References: <20180823155343.29044-1-skobyda@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-loop: libvir-list@redhat.com Cc: Simon Kobyda Subject: [libvirt] [PATCH v5 3/3] vsh: Added tests X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Thu, 23 Aug 2018 15:53:58 +0000 (UTC) X-ZohoMail: RDMRC_0 RSF_0 Z_629925259 SPT_0 For now, there are 9 test cases - testVshTableNew: Creating table with empty header - testVshTableHeader: Printing table with/without header - testVshTableRowAppend: Appending row with various number of cells. Only row with same number of cells as in header is accepted. - testUnicode: Printing table with unicode characters. Checking correct alignment. - testUnicodeArabic: test opposite (right to left) writing - testUnicodeZeroWidthChar - testUnicodeCombiningChar - testUnicodeNonPrintableChar, - testNTables: Create and print varios types of tables - one column, one row table, table without content, standart table... Signed-off-by: Simon Kobyda --- tests/Makefile.am | 8 + tests/vshtabletest.c | 377 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 385 insertions(+) create mode 100644 tests/vshtabletest.c diff --git a/tests/Makefile.am b/tests/Makefile.am index 21a6c823d9..136fe16f71 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -206,6 +206,7 @@ test_programs =3D virshtest sockettest \ virhostdevtest \ virnetdevtest \ virtypedparamtest \ + vshtabletest \ $(NULL) =20 test_libraries =3D libshunload.la \ @@ -938,6 +939,13 @@ metadatatest_SOURCES =3D \ testutils.c testutils.h metadatatest_LDADD =3D $(LDADDS) $(LIBXML_LIBS) =20 +vshtabletest_SOURCES =3D \ + vshtabletest.c \ + testutils.c testutils.h +vshtabletest_LDADD =3D \ + $(LDADDS) \ + ../tools/libvirt_shell.la + virshtest_SOURCES =3D \ virshtest.c \ testutils.c testutils.h diff --git a/tests/vshtabletest.c b/tests/vshtabletest.c new file mode 100644 index 0000000000..1b07c37c56 --- /dev/null +++ b/tests/vshtabletest.c @@ -0,0 +1,377 @@ +/* + * 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 + * . + */ + +#include + +#include +#include +#include + +#include "internal.h" +#include "testutils.h" +#include "viralloc.h" +#include "../tools/vsh-table.h" + +static int +testVshTableNew(const void *opaque ATTRIBUTE_UNUSED) +{ + if (vshTableNew(NULL)) { + fprintf(stderr, "expected failure when passing null to vshTableNew= \n"); + return -1; + } + + return 0; +} + +static int +testVshTableHeader(const void *opaque ATTRIBUTE_UNUSED) +{ + int ret =3D 0; + char *act =3D NULL; + const char *exp =3D +" 1 fedora28 running \n" +" 2 rhel7.5 running \n"; + const char *exp2 =3D +" Id Name State \n" +"--------------------------\n" +" 1 fedora28 running \n" +" 2 rhel7.5 running \n"; + + vshTablePtr table =3D vshTableNew("Id", "Name", "State", + NULL); //to ask about return + if (!table) + goto cleanup; + + vshTableRowAppend(table, "1", "fedora28", "running", NULL); + vshTableRowAppend(table, "2", "rhel7.5", "running", + NULL); + + act =3D vshTablePrintToString(table, false); + if (virTestCompareToString(exp, act) < 0) + ret =3D -1; + + VIR_FREE(act); + act =3D vshTablePrintToString(table, true); + if (virTestCompareToString(exp2, act) < 0) + ret =3D -1; + + cleanup: + VIR_FREE(act); + vshTableFree(table); + return ret; +} + +static int +testVshTableRowAppend(const void *opaque ATTRIBUTE_UNUSED) +{ + int ret =3D 0; + + vshTablePtr table =3D vshTableNew("Id", "Name", NULL); + if (!table) + goto cleanup; + + if (vshTableRowAppend(table, NULL) >=3D 0) { + fprintf(stderr, "Appending NULL shouldn't work\n"); + ret =3D -1; + } + + if (vshTableRowAppend(table, "2", NULL) >=3D 0) { + fprintf(stderr, "Appending less items than in header\n"); + ret =3D -1; + } + + if (vshTableRowAppend(table, "2", "rhel7.5", "running", + NULL) >=3D 0) { + fprintf(stderr, "Appending more items than in header\n"); + ret =3D -1; + } + + if (vshTableRowAppend(table, "2", "rhel7.5", NULL) < 0) { + fprintf(stderr, "Appending same number of items as in header" + " should not return NULL\n"); + ret =3D -1; + } + + cleanup: + vshTableFree(table); + return ret; +} + +static int +testUnicode(const void *opaque ATTRIBUTE_UNUSED) +{ + + int ret =3D 0; + char *act =3D NULL; + + const char *exp =3D +" Id =E5=90=8D=E7=A8=B1 =D0=B3=D0=BE=D1=81=D1=83=D0=B4= =D0=B0=D1=80=D1=81=D1=82=D0=B2=D0=BE \n" +"-----------------------------------------\n" +" 1 fedora28 running \n" +" 2 =F0=9F=99=8A=F0=9F=99=89=F0=9F=99=88rhel7.5=F0=9F=99=86=F0=9F=99=86= =F0=9F=99=85 running \n"; + vshTablePtr table; + + table =3D vshTableNew("Id", "=E5=90=8D=E7=A8=B1", "=D0=B3=D0=BE=D1=81= =D1=83=D0=B4=D0=B0=D1=80=D1=81=D1=82=D0=B2=D0=BE", NULL); + if (!table) + goto cleanup; + + vshTableRowAppend(table, "1", "fedora28", "running", NULL); + vshTableRowAppend(table, "2", "=F0=9F=99=8A=F0=9F=99=89=F0=9F=99=88rhe= l7.5=F0=9F=99=86=F0=9F=99=86=F0=9F=99=85", "running", + NULL); + + act =3D vshTablePrintToString(table, true); + if (virTestCompareToString(exp, act) < 0) + ret =3D -1; + + cleanup: + VIR_FREE(act); + vshTableFree(table); + return ret; +} + +/* Point of this test is to see how table behaves with right to left writi= ng*/ +static int +testUnicodeArabic(const void *opaque ATTRIBUTE_UNUSED) +{ + + int ret =3D 0; + char *act =3D NULL; + + const char *exp =3D +" =EF=BB=A1=EF=BA=8D =EF=BB=A2=EF=BB=A3=EF=BA=8D =EF=BB=95=EF=BA=8E=EF=BA= =8C=EF=BB=A3=EF=BA=93 =EF=BA=93 =EF=BA=8E=EF=BB=A0=EF=BA=BC=EF=BB=8B = =EF=BA=8D=EF=BB=B8=EF=BA=9C=EF= =BB=A7=EF=BA=8E=EF=BB=A7 \n" +"-------------------------------------------------------------------------= ------------------\n" +" 1 =EF=BB=89=EF=BA=AA=EF=BB=B4=EF=BB=9F =EF=BA=8E=EF=BB=A0= =EF=BA=9C=EF=BB=98=EF=BB=B4=EF=BB=9F =EF=BB=95=EF=BA=8E=EF=BB=A3 =EF=BB=8A= =EF=BB=A7, =D9=A3=D9=A0 =EF=BB=8E=EF=BB=B4=EF=BB=A8=EF=BB=B3=EF=BA=8D =EF= =BB=AE=EF=BA=98=EF=BB=A7=EF=BA=8E=EF=BB=A4=EF=BA=97 =EF=BA=8E=EF=BB=A0=EF= =BA=9B=EF=BA=8E=EF=BB=A0=EF=BA=9B=D8=8C =EF=BA=84=EF=BA=B3=EF=BA=AD, =EF=BA= =A9=EF=BB=AE=EF=BB=9F =EF=BA=A9=EF=BB=AE=EF=BB=9F. =EF=BA=84=EF=BB=A3=EF= =BA=8E=EF=BB=A3 =EF=BA=8D =EF=BA=8E=EF=BB=A7 =EF=BB=B2=EF=BB=9C=EF=BB=A7 \= n" +" =EF=BA=BA=EF=BB=94=EF=BA=A3=EF=BA=93 =EF=BA=96=EF=BB=9C=EF=BA= =98=EF=BB=B4=EF=BB=9B=EF=BA=8D=D9=8B =EF=BB=8A=EF=BB=9F, =EF=BA=8E=EF=BB=A0= =EF=BA=A0=EF=BB=A7=EF=BB=AD=EF=BA=A9 =EF=BB=AD=EF=BA=8E=EF=BB=A0=EF=BB=8C= =EF=BA=97=EF=BA=8D=EF=BA=A9 =EF=BA=B5=EF=BA=AD= \n"; + vshTablePtr table; + + table =3D vshTableNew("=EF=BB=A1=EF=BA=8D =EF=BB=A2=EF=BB=A3=EF=BA=8D = =EF=BB=95=EF=BA=8E=EF=BA=8C=EF=BB=A3=EF=BA=93", "=EF=BA=93 =EF=BA=8E=EF=BB= =A0=EF=BA=BC=EF=BB=8B", "=EF=BA=8D=EF=BB=B8=EF=BA=9C=EF=BB=A7=EF=BA=8E=EF= =BB=A7", NULL); + if (!table) + goto cleanup; + vshTableRowAppend(table, + "1", + "=EF=BB=89=EF=BA=AA=EF=BB=B4=EF=BB=9F =EF=BA=8E=EF= =BB=A0=EF=BA=9C=EF=BB=98=EF=BB=B4=EF=BB=9F =EF=BB=95=EF=BA=8E=EF=BB=A3 =EF= =BB=8A=EF=BB=A7, =D9=A3=D9=A0 =EF=BB=8E=EF=BB=B4=EF=BB=A8=EF=BB=B3=EF=BA=8D= =EF=BB=AE=EF=BA=98=EF=BB=A7=EF=BA=8E=EF=BB=A4=EF=BA=97 =EF=BA=8E=EF=BB=A0= =EF=BA=9B=EF=BA=8E=EF=BB=A0=EF=BA=9B=D8=8C =EF=BA=84=EF=BA=B3=EF=BA=AD, =EF= =BA=A9=EF=BB=AE=EF=BB=9F", + "=EF=BA=A9=EF=BB=AE=EF=BB=9F. =EF=BA=84=EF=BB=A3=EF= =BA=8E=EF=BB=A3 =EF=BA=8D =EF=BA=8E=EF=BB=A7 =EF=BB=B2=EF=BB=9C=EF=BB=A7", + NULL); + vshTableRowAppend(table, "=EF=BA=BA=EF=BB=94=EF=BA=A3=EF=BA=93", "=EF= =BA=96=EF=BB=9C=EF=BA=98=EF=BB=B4=EF=BB=9B=EF=BA=8D=D9=8B =EF=BB=8A=EF=BB= =9F, =EF=BA=8E=EF=BB=A0=EF=BA=A0=EF=BB=A7=EF=BB=AD=EF=BA=A9 =EF=BB=AD=EF=BA= =8E=EF=BB=A0=EF=BB=8C=EF=BA=97=EF=BA=8D=EF=BA=A9", "=EF=BA=B5=EF=BA=AD", + NULL); + act =3D vshTablePrintToString(table, true); + if (virTestCompareToString(exp, act) < 0) + ret =3D -1; + + cleanup: + VIR_FREE(act); + vshTableFree(table); + return ret; +} + +/* Testing zero-width characters by inserting few zero-width spaces */ +static int +testUnicodeZeroWidthChar(const void *opaque ATTRIBUTE_UNUSED) +{ + + int ret =3D 0; + vshTablePtr table =3D NULL; + const char *exp =3D +" I\u200Bd Name \u200BStatus \n" +"--------------------------\n" +" 1\u200B fedora28 run\u200Bning \n" +" 2 rhel7.5 running \n"; + char *act =3D NULL; + + table =3D vshTableNew("I\u200Bd", "Name", "\u200BStatus", NULL); + if (!table) + goto cleanup; + vshTableRowAppend(table, "1\u200B", "fedora28", "run\u200Bning", NULL); + vshTableRowAppend(table, "2", "rhel7.5", "running", NULL); + act =3D vshTablePrintToString(table, true); + + if (virTestCompareToString(exp, act) < 0) + ret =3D -1; + + cleanup: + VIR_FREE(act); + vshTableFree(table); + return ret; +} + +static int +testUnicodeCombiningChar(const void *opaque ATTRIBUTE_UNUSED) +{ + int ret =3D 0; + vshTablePtr table =3D NULL; + const char *exp =3D +" Id N=C3=A1me =E2=93=88tatus \n" +"--------------------------\n" +" 1 f=C4=9Bdora28 running \n" +" 2 rhel running \n"; + char *act =3D NULL; + + table =3D vshTableNew("Id", "N=C3=A1me", "=E2=93=88tatus", NULL); + if (!table) + goto cleanup; + vshTableRowAppend(table, "1", "f=C4=9Bdora28", "running", NULL); + vshTableRowAppend(table, "2", "rhel", "running", NULL); + act =3D vshTablePrintToString(table, true); + + if (virTestCompareToString(exp, act) < 0) + ret =3D -1; + + cleanup: + VIR_FREE(act); + vshTableFree(table); + return ret; +} + +/* Testing zero-width characters by inserting few zero-width spaces */ +static int +testUnicodeNonPrintableChar(const void *opaque ATTRIBUTE_UNUSED) +{ + int ret =3D 0; + vshTablePtr table =3D NULL; + const char *exp =3D +" I\\x09d Name Status \n" +"----------------------------------\n" +" 1 f\\x07edora28 running \n" +" 2 rhel7.5 running \n"; + char *act =3D NULL; + + table =3D vshTableNew("I\td", "Name", "Status", NULL); + if (!table) + goto cleanup; + vshTableRowAppend(table, "1", "f\aedora28", "running", NULL); + vshTableRowAppend(table, "2", "rhel7.5", "running", NULL); + act =3D vshTablePrintToString(table, true); + + if (virTestCompareToString(exp, act) < 0) + ret =3D -1; + + cleanup: + VIR_FREE(act); + vshTableFree(table); + return ret; +} + +static int +testNTables(const void *opaque ATTRIBUTE_UNUSED) +{ + int ret =3D 0; + vshTablePtr table1 =3D NULL; + vshTablePtr table2 =3D NULL; + vshTablePtr table3 =3D NULL; + const char *exp1 =3D +" Id Name Status \n" +"--------------------------\n" +" 1 fedora28 running \n" +" 2 rhel7.5 running \n"; + const char *exp2 =3D +" Id Name Status \n" +"---------------------\n"; + const char *exp3 =3D +" Id \n" +"-----\n" +" 1 \n" +" 2 \n" +" 3 \n" +" 4 \n"; + char *act1 =3D NULL; + char *act2 =3D NULL; + char *act3 =3D NULL; + + table1 =3D vshTableNew("Id", "Name", "Status", NULL); + if (!table1) + goto cleanup; + vshTableRowAppend(table1, "1", "fedora28", "running", NULL); + vshTableRowAppend(table1, "2", "rhel7.5", "running", NULL); + act1 =3D vshTablePrintToString(table1, true); + + table2 =3D vshTableNew("Id", "Name", "Status", NULL); + if (!table2) + goto cleanup; + act2 =3D vshTablePrintToString(table2, true); + + table3 =3D vshTableNew("Id", NULL); + if (!table3) + goto cleanup; + vshTableRowAppend(table3, "1", NULL); + vshTableRowAppend(table3, "2", NULL); + vshTableRowAppend(table3, "3", NULL); + vshTableRowAppend(table3, "4", NULL); + act3 =3D vshTablePrintToString(table3, true); + + if (virTestCompareToString(exp1, act1) < 0) + ret =3D -1; + if (virTestCompareToString(exp2, act2) < 0) + ret =3D -1; + if (virTestCompareToString(exp3, act3) < 0) + ret =3D -1; + + cleanup: + VIR_FREE(act1); + VIR_FREE(act2); + VIR_FREE(act3); + vshTableFree(table1); + vshTableFree(table2); + vshTableFree(table3); + return ret; +} + +static int +mymain(void) +{ + int ret =3D 0; + + if (!setlocale(LC_CTYPE, "en_US.UTF-8")) + return EXIT_AM_SKIP; + + if (virTestRun("testVshTableNew", testVshTableNew, NULL) < 0) + ret =3D -1; + + if (virTestRun("testVshTableHeader", testVshTableHeader, NULL) < 0) + ret =3D -1; + + if (virTestRun("testVshTableRowAppend", testVshTableRowAppend, NULL) <= 0) + ret =3D -1; + + if (virTestRun("testUnicode", testUnicode, NULL) < 0) + ret =3D -1; + + if (virTestRun("testUnicodeArabic", testUnicodeArabic, NULL) < 0) + ret =3D -1; + + if (virTestRun("testUnicodeZeroWidthChar", + testUnicodeZeroWidthChar, + NULL) < 0) + ret =3D -1; + + if (virTestRun("testUnicodeCombiningChar", + testUnicodeCombiningChar, + NULL) < 0) + ret =3D -1; + + if (virTestRun("testUnicodeNonPrintableChar", + testUnicodeNonPrintableChar, + NULL) < 0) + ret =3D -1; + + if (virTestRun("testNTables", testNTables, NULL) < 0) + ret =3D -1; + + return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} + +VIR_TEST_MAIN(mymain) --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list