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.
25 #define OP_STR_EMPTY 4
26 #define OP_STR_NEMPTY 5
37 #define OP_FILE_EXISTS 16
45 {1, "=", OP_STR_EQ, 3},
46 {1, "!=", OP_STR_NEQ, 3},
47 {1, "<", OP_STR_LT, 3},
48 {1, ">", OP_STR_GT, 3},
49 {1, "-eq", OP_INT_EQ, 3},
50 {1, "-ne", OP_INT_NEQ, 3},
51 {1, "-lt", OP_INT_LT, 3},
52 {1, "-le", OP_INT_LE, 3},
53 {1, "-gt", OP_INT_GT, 3},
54 {1, "-ge", OP_INT_GE, 3},
58 {0, "-z", OP_STR_EMPTY, 2},
59 {0, "-n", OP_STR_NEMPTY, 2},
60 {0, "-e", OP_FILE_EXISTS, 4},
63 static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
66 int i, op, left, adv, expr, last_expr, last_unop, last_binop;
74 debug("test(%d):", argc);
77 debug(" '%s'", argv[left++]);
84 last_unop = OP_INVALID;
85 last_binop = OP_INVALID;
88 for (i = 0; i < ARRAY_SIZE(op_adv); i++) {
89 if (left <= op_adv[i].arg)
91 if (!strcmp(ap[op_adv[i].arg], op_adv[i].str)) {
97 if (i == ARRAY_SIZE(op_adv)) {
108 expr = strlen(ap[1]) == 0 ? 1 : 0;
111 expr = strlen(ap[1]) == 0 ? 0 : 1;
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 = strcmp(ap[0], ap[2]) > 0;
126 expr = simple_strtol(ap[0], NULL, 10) ==
127 simple_strtol(ap[2], NULL, 10);
130 expr = simple_strtol(ap[0], NULL, 10) !=
131 simple_strtol(ap[2], NULL, 10);
134 expr = simple_strtol(ap[0], NULL, 10) <
135 simple_strtol(ap[2], NULL, 10);
138 expr = simple_strtol(ap[0], NULL, 10) <=
139 simple_strtol(ap[2], NULL, 10);
142 expr = simple_strtol(ap[0], NULL, 10) >
143 simple_strtol(ap[2], NULL, 10);
146 expr = simple_strtol(ap[0], NULL, 10) >=
147 simple_strtol(ap[2], NULL, 10);
150 expr = file_exists(ap[1], ap[2], ap[3], FS_TYPE_ANY);
164 if (last_unop == OP_NOT)
165 last_unop = OP_INVALID;
170 if (last_unop == OP_NOT) {
172 last_unop = OP_INVALID;
175 if (last_binop == OP_OR)
176 expr = last_expr || expr;
177 else if (last_binop == OP_AND)
178 expr = last_expr && expr;
179 last_binop = OP_INVALID;
184 ap += adv; left -= adv;
189 debug (": returns %d\n", expr);
195 test, CONFIG_SYS_MAXARGS, 1, do_test,
196 "minimal test like /bin/sh",
200 static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
206 false, CONFIG_SYS_MAXARGS, 1, do_false,
207 "do nothing, unsuccessfully",
211 static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
217 true, CONFIG_SYS_MAXARGS, 1, do_true,
218 "do nothing, successfully",