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.
24 #define OP_STR_EMPTY 4
25 #define OP_STR_NEMPTY 5
43 {1, "=", OP_STR_EQ, 3},
44 {1, "!=", OP_STR_NEQ, 3},
45 {1, "<", OP_STR_LT, 3},
46 {1, ">", OP_STR_GT, 3},
47 {1, "-eq", OP_INT_EQ, 3},
48 {1, "-ne", OP_INT_NEQ, 3},
49 {1, "-lt", OP_INT_LT, 3},
50 {1, "-le", OP_INT_LE, 3},
51 {1, "-gt", OP_INT_GT, 3},
52 {1, "-ge", OP_INT_GE, 3},
56 {0, "-z", OP_STR_EMPTY, 2},
57 {0, "-n", OP_STR_NEMPTY, 2},
60 static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
63 int i, op, left, adv, expr, last_expr, last_unop, last_binop;
71 debug("test(%d):", argc);
74 debug(" '%s'", argv[left++]);
81 last_unop = OP_INVALID;
82 last_binop = OP_INVALID;
85 for (i = 0; i < ARRAY_SIZE(op_adv); i++) {
86 if (left <= op_adv[i].arg)
88 if (!strcmp(ap[op_adv[i].arg], op_adv[i].str)) {
94 if (i == ARRAY_SIZE(op_adv)) {
105 expr = strlen(ap[1]) == 0 ? 1 : 0;
108 expr = strlen(ap[1]) == 0 ? 0 : 1;
111 expr = strcmp(ap[0], ap[2]) == 0;
114 expr = strcmp(ap[0], ap[2]) != 0;
117 expr = strcmp(ap[0], ap[2]) < 0;
120 expr = strcmp(ap[0], ap[2]) > 0;
123 expr = simple_strtol(ap[0], NULL, 10) ==
124 simple_strtol(ap[2], NULL, 10);
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);
158 if (last_unop == OP_NOT)
159 last_unop = OP_INVALID;
164 if (last_unop == OP_NOT) {
166 last_unop = OP_INVALID;
169 if (last_binop == OP_OR)
170 expr = last_expr || expr;
171 else if (last_binop == OP_AND)
172 expr = last_expr && expr;
173 last_binop = OP_INVALID;
178 ap += adv; left -= adv;
183 debug (": returns %d\n", expr);
189 test, CONFIG_SYS_MAXARGS, 1, do_test,
190 "minimal test like /bin/sh",
194 static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
200 false, CONFIG_SYS_MAXARGS, 1, do_false,
201 "do nothing, unsuccessfully",
205 static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
211 true, CONFIG_SYS_MAXARGS, 1, do_true,
212 "do nothing, successfully",