From 8bb05ac73a5b448b339ce0bc8d396c82c459b47f Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 14 Apr 2024 21:33:32 +0200 Subject: Merging upstream version 2.40. Signed-off-by: Daniel Baumann --- include/column-list-table.h | 88 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 include/column-list-table.h (limited to 'include/column-list-table.h') diff --git a/include/column-list-table.h b/include/column-list-table.h new file mode 100644 index 0000000..1629336 --- /dev/null +++ b/include/column-list-table.h @@ -0,0 +1,88 @@ +/* + * column-list-table.h - helper functions for implementing -H/--list-columns option + * + * Copyright (C) 2023 Red Hat, Inc. All rights reserved. + * Written by Masatake YAMATO + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it would be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef UTIL_LINUX_COLUMN_LIST_TABLE_H +#define UTIL_LINUX_COLUMN_LIST_TABLE_H + +#include "libsmartcols.h" +#include + +enum { CLT_COL_HOLDER, CLT_COL_TYPE, CLT_COL_DESC }; + +static inline struct libscols_table *xcolumn_list_table_new(const char *table_name, + FILE *out, + int raw, + int json) +{ + struct clt_colinfo { + const char *name; + int flags; + }; + struct libscols_table *tb; + + scols_init_debug(0); + + tb = scols_new_table(); + if (!tb) + err(EXIT_FAILURE, _("failed to allocate output table")); + + scols_table_set_name(tb, table_name); + scols_table_set_stream(tb, out); + scols_table_enable_noheadings(tb, 1); + scols_table_enable_raw(tb, raw); + scols_table_enable_json(tb, json); + + if (!scols_table_new_column(tb, "HOLDER", 0, SCOLS_FL_RIGHT)) + goto failed; + if (!scols_table_new_column(tb, "TYPE", 0, 0)) + goto failed; + if (!scols_table_new_column(tb, "DESCRIPTION", 0, 0)) + goto failed; + return tb; + + failed: + err(EXIT_FAILURE, _("failed to allocate output column")); +} + +static inline void xcolumn_list_table_append_line(struct libscols_table *tb, + const char *name, + int json_type, const char *fallback_typename, + const char *desc) +{ + struct libscols_line *ln = scols_table_new_line(tb, NULL); + if (!ln) + err(EXIT_FAILURE, _("failed to allocate output line")); + + if (scols_line_set_data(ln, CLT_COL_HOLDER, name)) + err(EXIT_FAILURE, _("failed to add output data")); + if (scols_line_set_data(ln, CLT_COL_TYPE, (json_type == SCOLS_JSON_STRING ? "": + json_type == SCOLS_JSON_ARRAY_STRING ? "": + json_type == SCOLS_JSON_ARRAY_NUMBER ? "": + json_type == SCOLS_JSON_NUMBER ? "": + json_type == SCOLS_JSON_FLOAT ? "": + json_type == SCOLS_JSON_BOOLEAN ? "": + fallback_typename ?: ""))) + err(EXIT_FAILURE, _("failed to add output data")); + if (scols_line_set_data(ln, CLT_COL_DESC, desc)) + err(EXIT_FAILURE, _("failed to add output data")); +} + +#endif -- cgit v1.2.3