From patchwork Wed Mar 11 22:46:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?UTF-8?Q?Christoph_M=c3=bcllner?= X-Patchwork-Id: 243565 List-Id: U-Boot discussion From: christoph.muellner at theobroma-systems.com (Christoph Muellner) Date: Wed, 11 Mar 2020 23:46:52 +0100 Subject: [PATCH] cmd: test: Add operators to deal with hexadecimal numbers. Message-ID: <20200311224652.16900-1-christoph.muellner@theobroma-systems.com> The DLUG states the following: Almost all U-Boot commands expect numbers to be entered in hexadecimal input format. Besides this fact, also most commands output hex values. Given this facts, we need to be able to use the information provided by to/from commands in scripts. The test command does not support operating on hexadecimal input formats. This leads to very surprising tests like this (simple_strtol("ff", 10) == 0): => if test "ff" -eq 0; then echo "equal"; fi equal => This patch introduces comparison operators for the test command, that allow parameters in hexadecimal format. So the test above can be implemented as follows: => if test "ff" -heq 0; then echo "equal"; fi => [1] https://www.denx.de/wiki/view/DULG/UBootCommandLineInterface Signed-off-by: Christoph Muellner Reviewed-by: Simon Glass --- cmd/test.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/cmd/test.c b/cmd/test.c index 258bfd8806..dbaf23596b 100644 --- a/cmd/test.c +++ b/cmd/test.c @@ -25,6 +25,12 @@ #define OP_INT_GT 14 #define OP_INT_GE 15 #define OP_FILE_EXISTS 16 +#define OP_HEX_EQ 17 +#define OP_HEX_NEQ 18 +#define OP_HEX_LT 19 +#define OP_HEX_LE 20 +#define OP_HEX_GT 21 +#define OP_HEX_GE 22 const struct { int arg; @@ -48,6 +54,12 @@ const struct { {0, "-z", OP_STR_EMPTY, 2}, {0, "-n", OP_STR_NEMPTY, 2}, {0, "-e", OP_FILE_EXISTS, 4}, + {1, "-heq", OP_HEX_EQ, 3}, + {1, "-hne", OP_HEX_NEQ, 3}, + {1, "-hlt", OP_HEX_LT, 3}, + {1, "-hle", OP_HEX_LE, 3}, + {1, "-hgt", OP_HEX_GT, 3}, + {1, "-hge", OP_HEX_GE, 3}, }; static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) @@ -139,6 +151,30 @@ static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) case OP_FILE_EXISTS: expr = file_exists(ap[1], ap[2], ap[3], FS_TYPE_ANY); break; + case OP_HEX_EQ: + expr = simple_strtol(ap[0], NULL, 16) == + simple_strtol(ap[2], NULL, 16); + break; + case OP_HEX_NEQ: + expr = simple_strtol(ap[0], NULL, 16) != + simple_strtol(ap[2], NULL, 16); + break; + case OP_HEX_LT: + expr = simple_strtol(ap[0], NULL, 16) < + simple_strtol(ap[2], NULL, 16); + break; + case OP_HEX_LE: + expr = simple_strtol(ap[0], NULL, 16) <= + simple_strtol(ap[2], NULL, 16); + break; + case OP_HEX_GT: + expr = simple_strtol(ap[0], NULL, 16) > + simple_strtol(ap[2], NULL, 16); + break; + case OP_HEX_GE: + expr = simple_strtol(ap[0], NULL, 16) >= + simple_strtol(ap[2], NULL, 16); + break; } switch (op) {