]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: support normalized column names on command line
authorKarel Zak <kzak@redhat.com>
Tue, 26 Sep 2023 12:45:13 +0000 (14:45 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 20 Nov 2023 21:25:46 +0000 (22:25 +0100)
For example column name "LOG-SEC" is possible to use as LOG_SEC in
some output formats and in filter expressions.

Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/lsblk.c

index 7d207b5de1e4d7eb7400c60c213c336d3dac8629..47b73fee2c4d94f1d633abdeb870fb9c6aff9886 100644 (file)
@@ -345,12 +345,30 @@ static int column_name_to_id(const char *name, size_t namesz)
 {
        size_t i;
 
+       /* name as diplayed for users */
        for (i = 0; i < ARRAY_SIZE(infos); i++) {
                const char *cn = infos[i].name;
 
                if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
                        return i;
        }
+
+       /* name as used in expressions, JSON output etc. */
+       if (strnchr(name, namesz, '_')) {
+               char *buf = NULL;
+               size_t bufsz = 0;
+
+               for (i = 0; i < ARRAY_SIZE(infos); i++) {
+                       if (scols_shellvar_name(infos[i].name, &buf, &bufsz) != 0)
+                               continue;
+                       if (!strncasecmp(name, buf, namesz) && !*(buf + namesz)) {
+                               free(buf);
+                               return i;
+                       }
+               }
+               free(buf);
+       }
+
        warnx(_("unknown column: %s"), name);
        return -1;
 }