From dc0e9c9ba6fbc96f73166c91d99f7465826375a5 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 16 Jul 2019 12:42:19 +0200 Subject: [PATCH 1/5] format-table: fix parameter name --- src/shared/format-table.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/format-table.h b/src/shared/format-table.h index ada59f34238..62e2d146db7 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -52,7 +52,7 @@ int table_set_weight(Table *t, TableCell *cell, unsigned weight); int table_set_align_percent(Table *t, TableCell *cell, unsigned percent); int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent); int table_set_color(Table *t, TableCell *cell, const char *color); -int table_set_url(Table *t, TableCell *cell, const char *color); +int table_set_url(Table *t, TableCell *cell, const char *url); int table_set_uppercase(Table *t, TableCell *cell, bool b); int table_update(Table *t, TableCell *cell, TableDataType type, const void *data); From dea55040df2e29562024c00818a1ebe9aa9b38c7 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 16 Jul 2019 12:42:43 +0200 Subject: [PATCH 2/5] test: make sure colors don't confuse our test --- src/test/test-format-table.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/test-format-table.c b/src/test/test-format-table.c index 5bede5c75b4..a8b6e1ffe32 100644 --- a/src/test/test-format-table.c +++ b/src/test/test-format-table.c @@ -34,6 +34,7 @@ int main(int argc, char *argv[]) { _cleanup_(table_unrefp) Table *t = NULL; _cleanup_free_ char *formatted = NULL; + assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0); assert_se(setenv("COLUMNS", "40", 1) >= 0); assert_se(t = table_new("one", "two", "three")); From 6268974f886ce1a6a63b67c3b0aa5ede586a6b0e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 16 Jul 2019 12:43:42 +0200 Subject: [PATCH 3/5] format-table: add ability to set cell attributes within table_add_many() table_add_many() is so much shorter and easier to read than table_add_cell() with its accessors. Let's teach table_add_many() more tricks, so that reverting to table_add_cell() is not needed that often anymore. --- src/shared/format-table.c | 54 ++++++++++++++++++++++++++++++++++++++- src/shared/format-table.h | 12 +++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 54ca1972bde..0422709f27a 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -678,6 +678,7 @@ int table_update(Table *t, TableCell *cell, TableDataType type, const void *data int table_add_many_internal(Table *t, TableDataType first_type, ...) { TableDataType type; va_list ap; + TableCell *last_cell = NULL; int r; assert(t); @@ -770,6 +771,55 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { data = &buffer.ifindex; break; + case TABLE_SET_MINIMUM_WIDTH: { + size_t w = va_arg(ap, size_t); + + r = table_set_minimum_width(t, last_cell, w); + break; + } + + case TABLE_SET_MAXIMUM_WIDTH: { + size_t w = va_arg(ap, size_t); + r = table_set_maximum_width(t, last_cell, w); + break; + } + + case TABLE_SET_WEIGHT: { + unsigned w = va_arg(ap, unsigned); + r = table_set_weight(t, last_cell, w); + break; + } + + case TABLE_SET_ALIGN_PERCENT: { + unsigned p = va_arg(ap, unsigned); + r = table_set_align_percent(t, last_cell, p); + break; + } + + case TABLE_SET_ELLIPSIZE_PERCENT: { + unsigned p = va_arg(ap, unsigned); + r = table_set_ellipsize_percent(t, last_cell, p); + break; + } + + case TABLE_SET_COLOR: { + const char *c = va_arg(ap, const char*); + r = table_set_color(t, last_cell, c); + break; + } + + case TABLE_SET_URL: { + const char *u = va_arg(ap, const char*); + r = table_set_url(t, last_cell, u); + break; + } + + case TABLE_SET_UPPERCASE: { + int u = va_arg(ap, int); + r = table_set_uppercase(t, last_cell, u); + break; + } + case _TABLE_DATA_TYPE_MAX: /* Used as end marker */ va_end(ap); @@ -779,7 +829,9 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { assert_not_reached("Uh? Unexpected data type."); } - r = table_add_cell(t, NULL, type, data); + if (type < _TABLE_DATA_TYPE_MAX) + r = table_add_cell(t, &last_cell, type, data); + if (r < 0) { va_end(ap); return r; diff --git a/src/shared/format-table.h b/src/shared/format-table.h index 62e2d146db7..e1c0dab4d0e 100644 --- a/src/shared/format-table.h +++ b/src/shared/format-table.h @@ -25,6 +25,18 @@ typedef enum TableDataType { TABLE_PERCENT, TABLE_IFINDEX, _TABLE_DATA_TYPE_MAX, + + /* The following are not really data types, but commands for table_add_cell_many() to make changes to + * a cell just added. */ + TABLE_SET_MINIMUM_WIDTH, + TABLE_SET_MAXIMUM_WIDTH, + TABLE_SET_WEIGHT, + TABLE_SET_ALIGN_PERCENT, + TABLE_SET_ELLIPSIZE_PERCENT, + TABLE_SET_COLOR, + TABLE_SET_URL, + TABLE_SET_UPPERCASE, + _TABLE_DATA_TYPE_INVALID = -1, } TableDataType; From 8792cecc1f27b46a81bca9a760a854b533a926ef Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 16 Jul 2019 12:45:13 +0200 Subject: [PATCH 4/5] format-table: add some minimal testing for new table_add_many() features --- src/test/test-format-table.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/test/test-format-table.c b/src/test/test-format-table.c index a8b6e1ffe32..def9cbda2f2 100644 --- a/src/test/test-format-table.c +++ b/src/test/test-format-table.c @@ -49,6 +49,7 @@ int main(int argc, char *argv[]) { assert_se(table_add_many(t, TABLE_STRING, "a long field", TABLE_STRING, "yyy", + TABLE_SET_UPPERCASE, 1, TABLE_BOOLEAN, false) >= 0); assert_se(table_format(t, &formatted) >= 0); @@ -57,7 +58,7 @@ int main(int argc, char *argv[]) { assert_se(streq(formatted, "ONE TWO THREE\n" "xxx yyy yes\n" - "a long field yyy no\n")); + "a long field YYY no\n")); formatted = mfree(formatted); @@ -69,7 +70,7 @@ int main(int argc, char *argv[]) { assert_se(streq(formatted, "ONE TWO THREE\n" "xxx yyy yes\n" - "a long field yyy no\n")); + "a long field YYY no\n")); formatted = mfree(formatted); @@ -80,7 +81,7 @@ int main(int argc, char *argv[]) { assert_se(streq(formatted, "ONE TWO THR…\n" "xxx yyy yes\n" - "a … yyy no\n")); + "a … YYY no\n")); formatted = mfree(formatted); @@ -114,7 +115,7 @@ int main(int argc, char *argv[]) { assert_se(streq(formatted, "ONE TWO THREE\n" - "a long field yyy no\n" + "a long field YYY no\n" "xxx yyy yes\n")); formatted = mfree(formatted); @@ -140,7 +141,7 @@ int main(int argc, char *argv[]) { printf("%s\n", formatted); assert_se(streq(formatted, - "a long field yyy no\n" + "a long field YYY no\n" "fäää zzz no\n" "fäää uuu yes\n" "xxx yyy yes\n" From 2cb434cfc10827e981624c865d306895987b3c17 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 16 Jul 2019 12:45:38 +0200 Subject: [PATCH 5/5] analyze: port over one part of systemd-analyze to use new table_add_many() concepts --- src/analyze/analyze-security.c | 36 ++++++++-------------------------- 1 file changed, 8 insertions(+), 28 deletions(-) diff --git a/src/analyze/analyze-security.c b/src/analyze/analyze-security.c index 3cf6515f5f3..134b2d28c3b 100644 --- a/src/analyze/analyze-security.c +++ b/src/analyze/analyze-security.c @@ -1502,35 +1502,15 @@ static int assess(const struct security_info *info, Table *overview_table, Analy if (color) (void) table_set_color(details_table, cell, color); - r = table_add_cell(details_table, &cell, TABLE_STRING, a->id); + r = table_add_many(details_table, + TABLE_STRING, a->id, TABLE_SET_URL, a->url, + TABLE_STRING, description, + TABLE_UINT64, a->weight, TABLE_SET_ALIGN_PERCENT, 100, + TABLE_UINT64, badness, TABLE_SET_ALIGN_PERCENT, 100, + TABLE_UINT64, a->range, TABLE_SET_ALIGN_PERCENT, 100, + TABLE_EMPTY, TABLE_SET_ALIGN_PERCENT, 100); if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - if (a->url) - (void) table_set_url(details_table, cell, a->url); - - r = table_add_cell(details_table, NULL, TABLE_STRING, description); - if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - - r = table_add_cell(details_table, &cell, TABLE_UINT64, &a->weight); - if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - (void) table_set_align_percent(details_table, cell, 100); - - r = table_add_cell(details_table, &cell, TABLE_UINT64, &badness); - if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - (void) table_set_align_percent(details_table, cell, 100); - - r = table_add_cell(details_table, &cell, TABLE_UINT64, &a->range); - if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - (void) table_set_align_percent(details_table, cell, 100); - - r = table_add_cell(details_table, &cell, TABLE_EMPTY, NULL); - if (r < 0) - return log_error_errno(r, "Failed to add cell to table: %m"); - (void) table_set_align_percent(details_table, cell, 100); + return log_error_errno(r, "Failed to add cells to table: %m"); } }