|
26 | 26 |
|
27 | 27 | #include <string.h>
|
28 | 28 | #include <assert.h>
|
| 29 | +#include <stdlib.h> |
29 | 30 |
|
30 | 31 | #ifdef CONFIG_NSH_BUILTIN_APPS
|
31 | 32 | # include <nuttx/lib/builtin.h>
|
@@ -91,6 +92,10 @@ static int cmd_false(FAR struct nsh_vtbl_s *vtbl, int argc,
|
91 | 92 | static int cmd_exit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
|
92 | 93 | #endif
|
93 | 94 |
|
| 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 | + |
94 | 99 | static int cmd_unrecognized(FAR struct nsh_vtbl_s *vtbl, int argc,
|
95 | 100 | FAR char **argv);
|
96 | 101 |
|
@@ -226,6 +231,11 @@ static const struct cmdmap_s g_cmdmap[] =
|
226 | 231 | CMD_MAP("exit", cmd_exit, 1, 1, NULL),
|
227 | 232 | #endif
|
228 | 233 |
|
| 234 | +#ifndef CONFIG_NSH_DISABLE_EXPR |
| 235 | + CMD_MAP("expr", cmd_expr, 4, 4, |
| 236 | + "<operand1> <operator> <operand2>"), |
| 237 | +#endif |
| 238 | + |
229 | 239 | #ifndef CONFIG_NSH_DISABLE_EXPORT
|
230 | 240 | CMD_MAP("export", cmd_export, 2, 3, "[<name> [<value>]]"),
|
231 | 241 | #endif
|
@@ -1091,6 +1101,74 @@ static int cmd_exit(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
1091 | 1101 | }
|
1092 | 1102 | #endif
|
1093 | 1103 |
|
| 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 | + |
1094 | 1172 | /****************************************************************************
|
1095 | 1173 | * Public Functions
|
1096 | 1174 | ****************************************************************************/
|
|
0 commit comments