From c699b05f74a4ea55c421f21dd973e9b2a30ab338 Mon Sep 17 00:00:00 2001 From: Junbo Zheng Date: Mon, 22 Jan 2024 22:34:14 +0800 Subject: [PATCH] nshlib: add `expr` command support It is a mini version for the `expr` command, which implements the features of addition, subtraction, multiplication, division and mod. Reference: https://www.geeksforgeeks.org/expr-command-in-linux-with-examples/ bl2> bl2> expr 1 + 2 3 bl2> expr Usage: expr bl2> expr 5 - 2 3 bl2> set hello 10 bl2> expr $hello - 2 8 bl2> expr 8 a 9 Unknown operator bl2> expr 20 / 5 4 bl2> expr 10 % 4 2 bl2> expr 10 / 0 operand2 invalid bl2> bl2> expr mi + 100 invalid parameter bl2> expr 100 + mi invalid parameter bl2> expr 100 + 0 100 Signed-off-by: Junbo Zheng --- nshlib/Kconfig | 4 +++ nshlib/nsh_command.c | 78 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/nshlib/Kconfig b/nshlib/Kconfig index c3306e07b..a1f281032 100644 --- a/nshlib/Kconfig +++ b/nshlib/Kconfig @@ -359,6 +359,10 @@ config NSH_DISABLE_EXIT bool "Disable exit" default DEFAULT_SMALL && !NSH_TELNET +config NSH_DISABLE_EXPR + bool "Disable expr" + default DEFAULT_SMALL + config NSH_DISABLE_EXPORT bool "Disable export" default DEFAULT_SMALL diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index cb0420af5..ea3023c3a 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -26,6 +26,7 @@ #include #include +#include #ifdef CONFIG_NSH_BUILTIN_APPS # include @@ -91,6 +92,10 @@ static int cmd_false(FAR struct nsh_vtbl_s *vtbl, int argc, static int cmd_exit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv); #endif +#ifndef CONFIG_NSH_DISABLE_EXPR +static int cmd_expr(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv); +#endif + static int cmd_unrecognized(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv); @@ -226,6 +231,11 @@ static const struct cmdmap_s g_cmdmap[] = CMD_MAP("exit", cmd_exit, 1, 1, NULL), #endif +#ifndef CONFIG_NSH_DISABLE_EXPR + CMD_MAP("expr", cmd_expr, 4, 4, + " "), +#endif + #ifndef CONFIG_NSH_DISABLE_EXPORT CMD_MAP("export", cmd_export, 2, 3, "[ []]"), #endif @@ -1091,6 +1101,74 @@ static int cmd_exit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) } #endif +#ifndef CONFIG_NSH_DISABLE_EXPR +static int cmd_expr(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) +{ + int operand1; + int operand2; + int result; + FAR char *endptr; + + if (argc != 4) + { + nsh_output(vtbl, "Usage: %s \n", + argv[0]); + return ERROR; + } + + operand1 = strtol(argv[1], &endptr, 0); + if (*endptr != '\0') + { + nsh_output(vtbl, "operand1 invalid\n"); + return ERROR; + } + + operand2 = strtol(argv[3], &endptr, 0); + if (*endptr != '\0') + { + nsh_output(vtbl, "operand2 invalid\n"); + return ERROR; + } + + switch (argv[2][0]) + { + case '+': + result = operand1 + operand2; + break; + case '-': + result = operand1 - operand2; + break; + case '*': + result = operand1 * operand2; + break; + case '/': + if (operand2 == 0) + { + nsh_output(vtbl, "operand2 invalid\n"); + return ERROR; + } + + result = operand1 / operand2; + break; + case '%': + if (operand2 == 0) + { + nsh_output(vtbl, "operand2 invalid\n"); + return ERROR; + } + + result = operand1 % operand2; + break; + default: + nsh_output(vtbl, "Unknown operator\n"); + return ERROR; + } + + nsh_output(vtbl, "%d\n", result); + return OK; +} +#endif + /**************************************************************************** * Public Functions ****************************************************************************/