summaryrefslogtreecommitdiff
path: root/common/command.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/command.c')
-rw-r--r--common/command.c153
1 files changed, 153 insertions, 0 deletions
diff --git a/common/command.c b/common/command.c
index df5d3e9..2b48a1c 100644
--- a/common/command.c
+++ b/common/command.c
@@ -74,6 +74,159 @@ U_BOOT_CMD(
" - echo args to console; \\c suppresses newline\n"
);
+#ifdef CFG_HUSH_PARSER
+
+int
+do_test (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+ char **ap;
+ int left, adv, expr, last_expr, neg, last_cmp;
+
+ /* args? */
+ if (argc < 3)
+ return 1;
+
+#if 0
+ {
+ printf("test:");
+ left = 1;
+ while (argv[left])
+ printf(" %s", argv[left++]);
+ }
+#endif
+
+ last_expr = 0;
+ left = argc - 1; ap = argv + 1;
+ if (left > 0 && strcmp(ap[0], "!") == 0) {
+ neg = 1;
+ ap++;
+ left--;
+ } else
+ neg = 0;
+
+ expr = -1;
+ last_cmp = -1;
+ last_expr = -1;
+ while (left > 0) {
+
+ if (strcmp(ap[0], "-o") == 0 || strcmp(ap[0], "-a") == 0)
+ adv = 1;
+ else if (strcmp(ap[0], "-z") == 0 || strcmp(ap[0], "-n") == 0)
+ adv = 2;
+ else
+ adv = 3;
+
+ if (left < adv) {
+ expr = 1;
+ break;
+ }
+
+ if (adv == 1) {
+ if (strcmp(ap[0], "-o") == 0) {
+ last_expr = expr;
+ last_cmp = 0;
+ } else if (strcmp(ap[0], "-a") == 0) {
+ last_expr = expr;
+ last_cmp = 1;
+ } else {
+ expr = 1;
+ break;
+ }
+ }
+
+ if (adv == 2) {
+ if (strcmp(ap[0], "-z") == 0)
+ expr = strlen(ap[1]) == 0 ? 0 : 1;
+ else if (strcmp(ap[0], "-n") == 0)
+ expr = strlen(ap[1]) == 0 ? 1 : 0;
+ else {
+ expr = 1;
+ break;
+ }
+
+ if (last_cmp == 0)
+ expr = last_expr || expr;
+ else if (last_cmp == 1)
+ expr = last_expr && expr;
+ last_cmp = -1;
+ }
+
+ if (adv == 3) {
+ if (strcmp(ap[1], "=") == 0)
+ expr = strcmp(ap[0], ap[2]) == 0;
+ else if (strcmp(ap[1], "!=") == 0)
+ expr = strcmp(ap[0], ap[2]) != 0;
+ else if (strcmp(ap[1], ">") == 0)
+ expr = strcmp(ap[0], ap[2]) > 0;
+ else if (strcmp(ap[1], "<") == 0)
+ expr = strcmp(ap[0], ap[2]) < 0;
+ else if (strcmp(ap[1], "-eq") == 0)
+ expr = simple_strtol(ap[0], NULL, 10) == simple_strtol(ap[2], NULL, 10);
+ else if (strcmp(ap[1], "-ne") == 0)
+ expr = simple_strtol(ap[0], NULL, 10) != simple_strtol(ap[2], NULL, 10);
+ else if (strcmp(ap[1], "-lt") == 0)
+ expr = simple_strtol(ap[0], NULL, 10) < simple_strtol(ap[2], NULL, 10);
+ else if (strcmp(ap[1], "-le") == 0)
+ expr = simple_strtol(ap[0], NULL, 10) <= simple_strtol(ap[2], NULL, 10);
+ else if (strcmp(ap[1], "-gt") == 0)
+ expr = simple_strtol(ap[0], NULL, 10) > simple_strtol(ap[2], NULL, 10);
+ else if (strcmp(ap[1], "-ge") == 0)
+ expr = simple_strtol(ap[0], NULL, 10) >= simple_strtol(ap[2], NULL, 10);
+ else {
+ expr = 1;
+ break;
+ }
+
+ if (last_cmp == 0)
+ expr = last_expr || expr;
+ else if (last_cmp == 1)
+ expr = last_expr && expr;
+ last_cmp = -1;
+ }
+
+ ap += adv; left -= adv;
+ }
+
+ if (neg)
+ expr = !expr;
+
+ expr = !expr;
+
+#if 0
+ printf(": returns %d\n", expr);
+#endif
+
+ return expr;
+}
+
+U_BOOT_CMD(
+ test, CFG_MAXARGS, 1, do_test,
+ "test - minimal test like /bin/sh\n",
+ "[args..]\n"
+ " - test functionality\n"
+);
+
+int
+do_exit (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+{
+ int r;
+
+ r = 0;
+ if (argc > 1)
+ r = simple_strtoul(argv[1], NULL, 10);
+
+ return -r - 2;
+}
+
+U_BOOT_CMD(
+ exit, 2, 1, do_exit,
+ "exit - exit script\n",
+ " - exit functionality\n"
+);
+
+
+#endif
+
/*
* Use puts() instead of printf() to avoid printf buffer overflow
* for long help messages