]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
test_mkfds: use libsmartcols in -l, --list output
authorMasatake YAMATO <yamato@redhat.com>
Thu, 4 Sep 2025 09:13:44 +0000 (18:13 +0900)
committerMasatake YAMATO <yamato@redhat.com>
Thu, 6 Aug 2026 12:10:03 +0000 (21:10 +0900)
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
meson.build
tests/helpers/Makemodule.am
tests/helpers/test_mkfds.c

index 5c1b1f7142d2655dd69e3fdf835eb154042f7a6d..e80d2a4e979b89bd47e8e9e733888ca4a1519fed 100644 (file)
@@ -4113,6 +4113,7 @@ if LINUX and lib_rt.found()
     'tests/helpers/test_mkfds.h',
     'tests/helpers/test_mkfds_ppoll.c',
     include_directories : includes,
+    link_with : lib_smartcols.get_static_lib(),
     dependencies : [lib_rt],
     c_args : test_mkfds_c_args,
     build_by_default: program_tests)
index 5b0f0b706dba0bd8d44898130e0a972b8ec14608..86d4e2a0dbe3a59cc7c1796b66296796b1a145ad 100644 (file)
@@ -51,7 +51,9 @@ if LINUX
 check_PROGRAMS += test_mkfds
 test_mkfds_SOURCES = tests/helpers/test_mkfds.c tests/helpers/test_mkfds.h \
        tests/helpers/test_mkfds_ppoll.c
-test_mkfds_LDADD = $(LDADD) $(MQ_LIBS)
+test_mkfds_LDFLAGS = -static
+test_mkfds_LDADD = $(LDADD) $(MQ_LIBS) libsmartcols.la
+test_mkfds_CFLAGS = $(AM_CFLAGS) -I$(ul_libsmartcols_incdir)
 
 check_PROGRAMS += test_enosys
 test_enosys_SOURCES = tests/helpers/test_enosys.c
index a4daca9771838f64e8b83d4505990b4433b3c372..a40752a8687ce0abe57671384516898b63aafee2 100644 (file)
@@ -22,6 +22,7 @@
 #include "test_mkfds.h"
 #include "exitcodes.h"
 #include "pidfd-utils.h"
+#include "libsmartcols.h"
 
 #include <arpa/inet.h>
 #include <ctype.h>
@@ -4599,22 +4600,113 @@ static int count_parameters(const struct factory *factory)
        return p - factory->params;
 }
 
-static void print_factory(const struct factory *factory)
+struct colinfo {
+       const char *name;
+       double whint;
+       int flags;
+       const char *help;
+};
+
+static void list_items(const char *tbname,
+                      const struct colinfo * colinfos,
+                      size_t n_columns,
+                      const void * items,
+                      size_t n_items,
+                      int (* fill_column) (struct libscols_line *, int, const void *, int),
+                      const char *sort_key_name)
+
+{
+       struct libscols_table *tb;
+       struct libscols_column *col;
+       struct libscols_column *sort_key_col;
+
+       scols_init_debug(0);
+       tb = scols_new_table();
+       scols_table_set_name(tb, tbname);
+
+       for (size_t i = 0; i < n_columns; i++) {
+               const struct colinfo *colinfo = colinfos + i;
+               col = scols_table_new_column(tb, colinfo->name, colinfo->whint, colinfo->flags);
+               if (!col)
+                       errx(EXIT_FAILURE, "failed to allocate output column");
+       }
+
+       for (size_t i = 0; i < n_items; i++) {
+               struct libscols_line *ln = scols_table_new_line(tb, NULL);
+               for (size_t j = 0; j < n_columns; j++) {
+                       (* fill_column) (ln, i, items, j); /* TODO: retval */
+               }
+       }
+
+       if (sort_key_name) {
+               sort_key_col = scols_table_get_column_by_name(tb, sort_key_name);
+               assert(sort_key_col);
+               scols_column_set_cmpfunc(sort_key_col, scols_cmpstr_cells, NULL);
+               scols_sort_table(tb, sort_key_col);
+       }
+       scols_print_table(tb);
+       scols_unref_table(tb);
+}
+
+enum {
+       COL_FACTORY_NAME,
+       COL_FACTORY_PRIV,
+       COL_FACTORY_COUNT,
+       COL_FACTORY_NRETURN,
+       COL_FACTORY_NPARAM,
+       COL_FACTORY_DESCRIPTION,
+       FACTORY_N_COLS
+};
+
+static const struct colinfo factory_infos[] = {
+       [COL_FACTORY_NAME]        = { "FACTORY", 0, 0,
+                                     "the name of factory" },
+       [COL_FACTORY_PRIV]        = { "PRIV", 0, SCOLS_FL_RIGHT,
+                                     "whether root privilege is needed or not" },
+       [COL_FACTORY_COUNT]       = { "COUNT", 0, SCOLS_FL_RIGHT,
+                                      "the number of file descriptors this factory may open" },
+       [COL_FACTORY_NRETURN]     = { "NRETURN", 0, SCOLS_FL_RIGHT,
+                                     "the number of values the factory may returns via stdout" },
+       [COL_FACTORY_NPARAM]      = { "NPARAM", 0, SCOLS_FL_RIGHT,
+                                     "the number of parameters the factory may takes from command line" },
+       [COL_FACTORY_DESCRIPTION] = { "DESCRIPTION", 0, 0,
+                                     "the description about this factory" },
+};
+
+static int factory_fill_column(struct libscols_line *ln, int nth_item, const void *data,
+                              int nth_column)
 {
-       printf("%-20s %4s %5d %7d %6d %s\n",
-              factory->name,
-              factory->priv? "yes": "no",
-              factory->N,
-              factory->EX_O + 1,
-              count_parameters(factory),
-              factory->desc);
+       const struct factory *f = ((struct factory *)data) + nth_item;
+
+       switch (nth_column) {
+       case COL_FACTORY_NAME:
+               scols_line_sprintf(ln, nth_column, "%s", f->name);
+               break;
+       case COL_FACTORY_PRIV:
+               scols_line_sprintf(ln, nth_column, "%s", f->priv? "yes": "no");
+               break;
+       case COL_FACTORY_COUNT:
+               scols_line_sprintf(ln, nth_column, "%d", f->N);
+               break;
+       case COL_FACTORY_NRETURN:
+               scols_line_sprintf(ln, nth_column, "%d", f->EX_O + 1);
+               break;
+       case COL_FACTORY_NPARAM:
+               scols_line_sprintf(ln, nth_column, "%d", count_parameters(f));
+               break;
+       case COL_FACTORY_DESCRIPTION:
+               scols_line_sprintf(ln, nth_column, "%s", f->desc);
+               break;
+       }
+
+       return 0;
 }
 
 static void list_factories(void)
 {
-       printf("%-20s PRIV COUNT NRETURN NPARAM DESCRIPTION\n", "FACTORY");
-       for (size_t i = 0; i < ARRAY_SIZE(factories); i++)
-               print_factory(factories + i);
+       list_items ("factories", factory_infos, FACTORY_N_COLS,
+                   factories, ARRAY_SIZE(factories),
+                   factory_fill_column, factory_infos[COL_FACTORY_NAME].name);
 }
 
 static const struct factory *find_factory(const char *name)