3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
9 * Define _STDBOOL_H here to avoid macro expansion of true and false.
10 * If the future code requires macro true or false, remove this define
11 * and undef true and false before U_BOOT_CMD. This define and comment
12 * shall be removed if change to U_BOOT_CMD is made to take string
13 * instead of stringifying it.
23 #define OP_STR_EMPTY 4
24 #define OP_STR_NEMPTY 5
42 {1, "=", OP_STR_EQ, 3},
43 {1, "!=", OP_STR_NEQ, 3},
44 {1, "<", OP_STR_LT, 3},
45 {1, ">", OP_STR_GT, 3},
46 {1, "-eq", OP_INT_EQ, 3},
47 {1, "-ne", OP_INT_NEQ, 3},
48 {1, "-lt", OP_INT_LT, 3},
49 {1, "-le", OP_INT_LE, 3},
50 {1, "-gt", OP_INT_GT, 3},
51 {1, "-ge", OP_INT_GE, 3},
54 {0, "-z", OP_STR_EMPTY, 2},
55 {0, "-n", OP_STR_NEMPTY, 2},
58 static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
61 int i, op, left, adv, expr, last_expr, neg, last_cmp;
69 debug("test(%d):", argc);
72 debug(" '%s'", argv[left++]);
77 left = argc - 1; ap = argv + 1;
78 if (left > 0 && strcmp(ap[0], "!") == 0) {
86 last_cmp = OP_INVALID;
89 for (i = 0; i < ARRAY_SIZE(op_adv); i++) {
90 if (left <= op_adv[i].arg)
92 if (!strcmp(ap[op_adv[i].arg], op_adv[i].str)) {
98 if (i == ARRAY_SIZE(op_adv)) {
109 expr = strlen(ap[1]) == 0 ? 1 : 0;
112 expr = strlen(ap[1]) == 0 ? 0 : 1;
115 expr = strcmp(ap[0], ap[2]) == 0;
118 expr = strcmp(ap[0], ap[2]) != 0;
121 expr = strcmp(ap[0], ap[2]) < 0;
124 expr = strcmp(ap[0], ap[2]) > 0;
127 expr = simple_strtol(ap[0], NULL, 10) ==
128 simple_strtol(ap[2], NULL, 10);
131 expr = simple_strtol(ap[0], NULL, 10) !=
132 simple_strtol(ap[2], NULL, 10);
135 expr = simple_strtol(ap[0], NULL, 10) <
136 simple_strtol(ap[2], NULL, 10);
139 expr = simple_strtol(ap[0], NULL, 10) <=
140 simple_strtol(ap[2], NULL, 10);
143 expr = simple_strtol(ap[0], NULL, 10) >
144 simple_strtol(ap[2], NULL, 10);
147 expr = simple_strtol(ap[0], NULL, 10) >=
148 simple_strtol(ap[2], NULL, 10);
162 if (last_cmp == OP_OR)
163 expr = last_expr || expr;
164 else if (last_cmp == OP_AND)
165 expr = last_expr && expr;
166 last_cmp = OP_INVALID;
170 ap += adv; left -= adv;
178 debug (": returns %d\n", expr);
184 test, CONFIG_SYS_MAXARGS, 1, do_test,
185 "minimal test like /bin/sh",
189 static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
195 false, CONFIG_SYS_MAXARGS, 1, do_false,
196 "do nothing, unsuccessfully",
200 static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
206 true, CONFIG_SYS_MAXARGS, 1, do_true,
207 "do nothing, successfully",