]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Merge pull request #14525 from yuwata/mount-use-format-table
authorLennart Poettering <lennart@poettering.net>
Thu, 9 Jan 2020 14:47:24 +0000 (15:47 +0100)
committerGitHub <noreply@github.com>
Thu, 9 Jan 2020 14:47:24 +0000 (15:47 +0100)
systemd-mount: use format-table.[ch]

man/systemd-mount.xml
src/mount/mount-tool.c
src/shared/format-table.c
src/shared/format-table.h

index 4a7c33f5584244261e9de09cc7f9fe25ab09849e..82ac61a447fffdfcf39fc959fad2368558dbb7d0 100644 (file)
@@ -96,6 +96,7 @@
       </varlistentry>
 
       <xi:include href="standard-options.xml" xpointer="no-pager"/>
+      <xi:include href="standard-options.xml" xpointer="no-legend" />
       <xi:include href="standard-options.xml" xpointer="no-ask-password"/>
 
       <varlistentry>
index 9f0220a75e7e4a4959dda6b34d79f8aa4936f902..69f140fb3e0f405c75be9679b5579c5ea94b0f42 100644 (file)
@@ -14,6 +14,7 @@
 #include "escape.h"
 #include "fd-util.h"
 #include "fileio.h"
+#include "format-table.h"
 #include "format-util.h"
 #include "fs-util.h"
 #include "fstab-util.h"
@@ -44,6 +45,7 @@ enum {
 
 static bool arg_no_block = false;
 static PagerFlags arg_pager_flags = 0;
+static bool arg_legend = true;
 static bool arg_ask_password = true;
 static bool arg_quiet = false;
 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
@@ -89,6 +91,7 @@ static int help(void) {
                "     --version                    Show package version\n"
                "     --no-block                   Do not wait until operation finished\n"
                "     --no-pager                   Do not pipe output into a pager\n"
+               "     --no-legend                  Do not show the headers\n"
                "     --no-ask-password            Do not prompt for password\n"
                "  -q --quiet                      Suppress information messages during runtime\n"
                "     --user                       Run as user unit\n"
@@ -124,6 +127,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_VERSION = 0x100,
                 ARG_NO_BLOCK,
                 ARG_NO_PAGER,
+                ARG_NO_LEGEND,
                 ARG_NO_ASK_PASSWORD,
                 ARG_USER,
                 ARG_SYSTEM,
@@ -145,6 +149,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "version",            no_argument,       NULL, ARG_VERSION            },
                 { "no-block",           no_argument,       NULL, ARG_NO_BLOCK           },
                 { "no-pager",           no_argument,       NULL, ARG_NO_PAGER           },
+                { "no-legend",          no_argument,       NULL, ARG_NO_LEGEND          },
                 { "no-ask-password",    no_argument,       NULL, ARG_NO_ASK_PASSWORD    },
                 { "quiet",              no_argument,       NULL, 'q'                    },
                 { "user",               no_argument,       NULL, ARG_USER               },
@@ -195,6 +200,10 @@ static int parse_argv(int argc, char *argv[]) {
                         arg_pager_flags |= PAGER_DISABLE;
                         break;
 
+                case ARG_NO_LEGEND:
+                        arg_legend = false;
+                        break;
+
                 case ARG_NO_ASK_PASSWORD:
                         arg_ask_password = false;
                         break;
@@ -1361,44 +1370,13 @@ enum {
         _COLUMN_MAX,
 };
 
-struct item {
-        char* columns[_COLUMN_MAX];
-};
-
-static int compare_item(const struct item *a, const struct item *b) {
-        if (a->columns[COLUMN_NODE] == b->columns[COLUMN_NODE])
-                return 0;
-        if (!a->columns[COLUMN_NODE])
-                return 1;
-        if (!b->columns[COLUMN_NODE])
-                return -1;
-
-        return path_compare(a->columns[COLUMN_NODE], b->columns[COLUMN_NODE]);
-}
-
 static int list_devices(void) {
-
-        static const char * const titles[_COLUMN_MAX] = {
-                [COLUMN_NODE] = "NODE",
-                [COLUMN_PATH] = "PATH",
-                [COLUMN_MODEL] = "MODEL",
-                [COLUMN_WWN] = "WWN",
-                [COLUMN_FSTYPE] = "TYPE",
-                [COLUMN_LABEL] = "LABEL",
-                [COLUMN_UUID] = "UUID"
-        };
-
         _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
-        size_t n_allocated = 0, n = 0, i;
-        size_t column_width[_COLUMN_MAX];
-        struct item *items = NULL;
+        _cleanup_(table_unrefp) Table *table = NULL;
         sd_device *d;
         unsigned c;
         int r;
 
-        for (c = 0; c < _COLUMN_MAX; c++)
-                column_width[c] = strlen(titles[c]);
-
         r = sd_device_enumerator_new(&e);
         if (r < 0)
                 return log_oom();
@@ -1411,19 +1389,19 @@ static int list_devices(void) {
         if (r < 0)
                 return log_error_errno(r, "Failed to add property match: %m");
 
-        FOREACH_DEVICE(e, d) {
-                struct item *j;
+        table = table_new("NODE", "PATH", "MODEL", "WWN", "TYPE", "LABEL", "UUID");
+        if (!table)
+                return log_oom();
 
-                if (!GREEDY_REALLOC0(items, n_allocated, n+1)) {
-                        r = log_oom();
-                        goto finish;
-                }
+        r = table_set_sort(table, 0, SIZE_MAX);
+        if (r < 0)
+                return log_error_errno(r, "Failed to set sort index: %m");
 
-                j = items + n++;
+        table_set_header(table, arg_legend);
 
+        FOREACH_DEVICE(e, d) {
                 for (c = 0; c < _COLUMN_MAX; c++) {
                         const char *x = NULL;
-                        size_t k;
 
                         switch (c) {
 
@@ -1456,59 +1434,19 @@ static int list_devices(void) {
                                 break;
                         }
 
-                        if (isempty(x))
-                                continue;
-
-                        j->columns[c] = strdup(x);
-                        if (!j->columns[c]) {
-                                r = log_oom();
-                                goto finish;
-                        }
-
-                        k = strlen(x);
-                        if (k > column_width[c])
-                                column_width[c] = k;
+                        r = table_add_cell(table, NULL, c == COLUMN_NODE ? TABLE_PATH : TABLE_STRING, strna(x));
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to add cell: %m");
                 }
         }
 
-        if (n == 0) {
-                log_info("No devices found.");
-                goto finish;
-        }
-
-        typesafe_qsort(items, n, compare_item);
-
         (void) pager_open(arg_pager_flags);
 
-        fputs(ansi_underline(), stdout);
-        for (c = 0; c < _COLUMN_MAX; c++) {
-                if (c > 0)
-                        fputc(' ', stdout);
-
-                printf("%-*s", (int) column_width[c], titles[c]);
-        }
-        fputs(ansi_normal(), stdout);
-        fputc('\n', stdout);
-
-        for (i = 0; i < n; i++) {
-                for (c = 0; c < _COLUMN_MAX; c++) {
-                        if (c > 0)
-                                fputc(' ', stdout);
-
-                        printf("%-*s", (int) column_width[c], strna(items[i].columns[c]));
-                }
-                fputc('\n', stdout);
-        }
-
-        r = 0;
-
-finish:
-        for (i = 0; i < n; i++)
-                for (c = 0; c < _COLUMN_MAX; c++)
-                        free(items[i].columns[c]);
+        r = table_print(table, NULL);
+        if (r < 0)
+                return log_error_errno(r, "Failed to print table: %m");
 
-        free(items);
-        return r;
+        return 0;
 }
 
 static int run(int argc, char* argv[]) {
index 4617ae8badc43728d6c4317190d960aa08365bd3..47d56dd085baf3676025c3c3cce28f4f7c431684 100644 (file)
@@ -14,6 +14,7 @@
 #include "memory-util.h"
 #include "pager.h"
 #include "parse-util.h"
+#include "path-util.h"
 #include "pretty-print.h"
 #include "sort-util.h"
 #include "string-util.h"
@@ -234,6 +235,7 @@ static size_t table_data_size(TableDataType type, const void *data) {
                 return 0;
 
         case TABLE_STRING:
+        case TABLE_PATH:
                 return strlen(data) + 1;
 
         case TABLE_BOOLEAN:
@@ -768,6 +770,7 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) {
                         break;
 
                 case TABLE_STRING:
+                case TABLE_PATH:
                         data = va_arg(ap, const char *);
                         break;
 
@@ -1038,6 +1041,9 @@ static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t
                 case TABLE_STRING:
                         return strcmp(a->string, b->string);
 
+                case TABLE_PATH:
+                        return path_compare(a->string, b->string);
+
                 case TABLE_BOOLEAN:
                         if (!a->boolean && b->boolean)
                                 return -1;
@@ -1151,6 +1157,7 @@ static const char *table_data_format(Table *t, TableData *d) {
                 return strempty(t->empty_string);
 
         case TABLE_STRING:
+        case TABLE_PATH:
                 if (d->uppercase) {
                         char *p, *q;
 
@@ -1897,6 +1904,7 @@ static int table_data_to_json(TableData *d, JsonVariant **ret) {
                 return json_variant_new_null(ret);
 
         case TABLE_STRING:
+        case TABLE_PATH:
                 return json_variant_new_string(ret, d->string);
 
         case TABLE_BOOLEAN:
index 80f319d054a30bf5f81c48ced3bccd05ce89e4fd..b23e4de99d0f6c13144f1c52850c75e37c90be98 100644 (file)
@@ -11,6 +11,7 @@
 typedef enum TableDataType {
         TABLE_EMPTY,
         TABLE_STRING,
+        TABLE_PATH,
         TABLE_BOOLEAN,
         TABLE_TIMESTAMP,
         TABLE_TIMESTAMP_UTC,