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 <operand1> <operator> <operand2>
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 <zhengjunbo1@xiaomi.com>
This commit is contained in:
Junbo Zheng 2024-01-22 22:34:14 +08:00 committed by Xiang Xiao
parent 596328ccca
commit c699b05f74
2 changed files with 82 additions and 0 deletions

View File

@ -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

View File

@ -26,6 +26,7 @@
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#ifdef CONFIG_NSH_BUILTIN_APPS
# include <nuttx/lib/builtin.h>
@ -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,
"<operand1> <operator> <operand2>"),
#endif
#ifndef CONFIG_NSH_DISABLE_EXPORT
CMD_MAP("export", cmd_export, 2, 3, "[<name> [<value>]]"),
#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 <operand1> <operator> <operand2>\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
****************************************************************************/