]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libsmartcols: (filter) accept prefixes like k, M, G as a parts of a number
authorKarel Zak <kzak@redhat.com>
Mon, 15 Apr 2024 10:34:31 +0000 (12:34 +0200)
committerKarel Zak <kzak@redhat.com>
Mon, 15 Apr 2024 10:53:51 +0000 (12:53 +0200)
Co-Author: Masatake YAMATO <yamato@redhat.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
libsmartcols/scols-filter.5.adoc
libsmartcols/src/filter-scanner.l

index ec1d95c5a7c7c972af113c179f2c1266b6300f47..dcf1fa5d9db13219d46df648de6697e931ab0352 100644 (file)
@@ -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
 
index 5d2571d157d879e7a7c8d58dc8c60ad44ccf91e6..3b1d0b0fcc8a5551a0a04745207daa8609994d26 100644 (file)
@@ -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;