aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2024-04-15 12:34:31 +0200
committerKarel Zak <kzak@redhat.com>2024-04-15 12:53:51 +0200
commite0657d46914f0bd055e914ef9f4f0c9b88419a78 (patch)
tree291f4b46cd994329d134610745228bb9e5016ac3
parent506ad7fb88a3230f1dfbf99a2e9bc971a8201235 (diff)
downloadutil-linux-e0657d46914f0bd055e914ef9f4f0c9b88419a78.tar.gz
libsmartcols: (filter) accept prefixes like k, M, G as a parts of a number
Co-Author: Masatake YAMATO <yamato@redhat.com> Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r--libsmartcols/scols-filter.5.adoc7
-rw-r--r--libsmartcols/src/filter-scanner.l25
2 files changed, 30 insertions, 2 deletions
diff --git a/libsmartcols/scols-filter.5.adoc b/libsmartcols/scols-filter.5.adoc
index ec1d95c5a7..dcf1fa5d9d 100644
--- a/libsmartcols/scols-filter.5.adoc
+++ b/libsmartcols/scols-filter.5.adoc
@@ -43,6 +43,8 @@ param: integer
| holder
integer: [0-9]*
+ | [0-9]*[KMGTPEZY]
+ | [0-9]*[KMGTPEZY]iB
float: integer.integer
@@ -103,8 +105,9 @@ The precedences within operators is `or`, `and`, and `eq`, `ne`, `le`, `gt`, `ge
About `float` and `integer` typed values, the filter engine supports only
non-negative numbers. The `integer` is unsigned 64-bit number, and `float` is
-long double.
-
+long double. The `integer` may be followed by the multiplicative suffixes KiB,
+GiB, TiB, PiB, EiB, ZiB, and YiB (the "iB" is optional, e.g., "K" has the same
+meaning as "KiB").
== AUTHORS
diff --git a/libsmartcols/src/filter-scanner.l b/libsmartcols/src/filter-scanner.l
index 5d2571d157..3b1d0b0fcc 100644
--- a/libsmartcols/src/filter-scanner.l
+++ b/libsmartcols/src/filter-scanner.l
@@ -2,6 +2,8 @@
#include "smartcolsP.h"
#include "filter-parser.h" /* define tokens (T_*) */
+void yyerror(yyscan_t *locp, struct libscols_filter *fltr, char const *fmt, ...);
+
%}
%option reentrant bison-bridge noyywrap noinput nounput
@@ -46,6 +48,29 @@ true|TRUE return T_TRUE;
return T_FLOAT;
}
+{int}+([KMGTPEZY](iB)?) {
+ uintmax_t res;
+ int e;
+
+ errno = 0;
+ e = strtosize(yytext, &res);
+ if (e < 0) {
+ if (errno)
+ yyerror(yyscanner, yyextra, "\"%s\" token error: %m", yytext);
+ else
+ yyerror(yyscanner, yyextra, "\"%s\" token error", yytext);
+ return YYerror;
+ }
+
+ if (res > ULLONG_MAX) {
+ yyerror(yyscanner, yyextra, "\"%s\" number too large", yytext);
+ return YYerror;
+ }
+
+ yylval->param_number = (unsigned long long) res;
+ return T_NUMBER;
+}
+
{int}+ {
yylval->param_number = (int64_t) strtoumax(yytext, NULL, 10);
return T_NUMBER;