Skip to content

Commit c699b05

Browse files
Junbo-Zhengxiaoxiang781216
authored andcommitted
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>
1 parent 596328c commit c699b05

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

nshlib/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ config NSH_DISABLE_EXIT
359359
bool "Disable exit"
360360
default DEFAULT_SMALL && !NSH_TELNET
361361

362+
config NSH_DISABLE_EXPR
363+
bool "Disable expr"
364+
default DEFAULT_SMALL
365+
362366
config NSH_DISABLE_EXPORT
363367
bool "Disable export"
364368
default DEFAULT_SMALL

nshlib/nsh_command.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <string.h>
2828
#include <assert.h>
29+
#include <stdlib.h>
2930

3031
#ifdef CONFIG_NSH_BUILTIN_APPS
3132
# include <nuttx/lib/builtin.h>
@@ -91,6 +92,10 @@ static int cmd_false(FAR struct nsh_vtbl_s *vtbl, int argc,
9192
static int cmd_exit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
9293
#endif
9394

95+
#ifndef CONFIG_NSH_DISABLE_EXPR
96+
static int cmd_expr(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
97+
#endif
98+
9499
static int cmd_unrecognized(FAR struct nsh_vtbl_s *vtbl, int argc,
95100
FAR char **argv);
96101

@@ -226,6 +231,11 @@ static const struct cmdmap_s g_cmdmap[] =
226231
CMD_MAP("exit", cmd_exit, 1, 1, NULL),
227232
#endif
228233

234+
#ifndef CONFIG_NSH_DISABLE_EXPR
235+
CMD_MAP("expr", cmd_expr, 4, 4,
236+
"<operand1> <operator> <operand2>"),
237+
#endif
238+
229239
#ifndef CONFIG_NSH_DISABLE_EXPORT
230240
CMD_MAP("export", cmd_export, 2, 3, "[<name> [<value>]]"),
231241
#endif
@@ -1091,6 +1101,74 @@ static int cmd_exit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
10911101
}
10921102
#endif
10931103

1104+
#ifndef CONFIG_NSH_DISABLE_EXPR
1105+
static int cmd_expr(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
1106+
{
1107+
int operand1;
1108+
int operand2;
1109+
int result;
1110+
FAR char *endptr;
1111+
1112+
if (argc != 4)
1113+
{
1114+
nsh_output(vtbl, "Usage: %s <operand1> <operator> <operand2>\n",
1115+
argv[0]);
1116+
return ERROR;
1117+
}
1118+
1119+
operand1 = strtol(argv[1], &endptr, 0);
1120+
if (*endptr != '\0')
1121+
{
1122+
nsh_output(vtbl, "operand1 invalid\n");
1123+
return ERROR;
1124+
}
1125+
1126+
operand2 = strtol(argv[3], &endptr, 0);
1127+
if (*endptr != '\0')
1128+
{
1129+
nsh_output(vtbl, "operand2 invalid\n");
1130+
return ERROR;
1131+
}
1132+
1133+
switch (argv[2][0])
1134+
{
1135+
case '+':
1136+
result = operand1 + operand2;
1137+
break;
1138+
case '-':
1139+
result = operand1 - operand2;
1140+
break;
1141+
case '*':
1142+
result = operand1 * operand2;
1143+
break;
1144+
case '/':
1145+
if (operand2 == 0)
1146+
{
1147+
nsh_output(vtbl, "operand2 invalid\n");
1148+
return ERROR;
1149+
}
1150+
1151+
result = operand1 / operand2;
1152+
break;
1153+
case '%':
1154+
if (operand2 == 0)
1155+
{
1156+
nsh_output(vtbl, "operand2 invalid\n");
1157+
return ERROR;
1158+
}
1159+
1160+
result = operand1 % operand2;
1161+
break;
1162+
default:
1163+
nsh_output(vtbl, "Unknown operator\n");
1164+
return ERROR;
1165+
}
1166+
1167+
nsh_output(vtbl, "%d\n", result);
1168+
return OK;
1169+
}
1170+
#endif
1171+
10941172
/****************************************************************************
10951173
* Public Functions
10961174
****************************************************************************/

0 commit comments

Comments
 (0)