From: Karel Zak Date: Mon, 15 Apr 2024 10:34:31 +0000 (+0200) Subject: libsmartcols: (filter) accept prefixes like k, M, G as a parts of a number X-Git-Tag: v2.42-start~408 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e0657d46914f0bd055e914ef9f4f0c9b88419a78;p=thirdparty%2Futil-linux.git libsmartcols: (filter) accept prefixes like k, M, G as a parts of a number Co-Author: Masatake YAMATO Signed-off-by: Karel Zak --- diff --git a/libsmartcols/scols-filter.5.adoc b/libsmartcols/scols-filter.5.adoc index ec1d95c5a..dcf1fa5d9 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 5d2571d15..3b1d0b0fc 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;