From: Masatake YAMATO Date: Fri, 24 Nov 2023 16:07:03 +0000 (+0900) Subject: add helper functions for implementing -H/--list-columns option X-Git-Tag: v2.40-rc1~130^2~4 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=abccd63d8a32a5164899f4db4dcf1c8dc772e306;p=thirdparty%2Futil-linux.git add helper functions for implementing -H/--list-columns option Signed-off-by: Masatake YAMATO --- diff --git a/include/Makemodule.am b/include/Makemodule.am index 52657f7d15..c08e24c2da 100644 --- a/include/Makemodule.am +++ b/include/Makemodule.am @@ -13,6 +13,7 @@ dist_noinst_HEADERS += \ include/closestream.h \ include/colors.h \ include/color-names.h \ + include/column-list-table.h \ include/coverage.h \ include/cpuset.h \ include/crc32.h \ diff --git a/include/column-list-table.h b/include/column-list-table.h new file mode 100644 index 0000000000..1629336bc5 --- /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