3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
16 #define OP_STR_EMPTY 4
17 #define OP_STR_NEMPTY 5
28 #define OP_FILE_EXISTS 16
36 {1, "=", OP_STR_EQ, 3},
37 {1, "!=", OP_STR_NEQ, 3},
38 {1, "<", OP_STR_LT, 3},
39 {1, ">", OP_STR_GT, 3},
40 {1, "-eq", OP_INT_EQ, 3},
41 {1, "-ne", OP_INT_NEQ, 3},
42 {1, "-lt", OP_INT_LT, 3},
43 {1, "-le", OP_INT_LE, 3},
44 {1, "-gt", OP_INT_GT, 3},
45 {1, "-ge", OP_INT_GE, 3},
49 {0, "-z", OP_STR_EMPTY, 2},
50 {0, "-n", OP_STR_NEMPTY, 2},
51 {0, "-e", OP_FILE_EXISTS, 4},
54 static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
57 int i, op, left, adv, expr, last_expr, last_unop, last_binop;
65 debug("test(%d):", argc);
68 debug(" '%s'", argv[left++]);
75 last_unop = OP_INVALID;
76 last_binop = OP_INVALID;
79 for (i = 0; i < ARRAY_SIZE(op_adv); i++) {
80 if (left <= op_adv[i].arg)
82 if (!strcmp(ap[op_adv[i].arg], op_adv[i].str)) {
88 if (i == ARRAY_SIZE(op_adv)) {
99 expr = strlen(ap[1]) == 0 ? 1 : 0;
102 expr = strlen(ap[1]) == 0 ? 0 : 1;
105 expr = strcmp(ap[0], ap[2]) == 0;
108 expr = strcmp(ap[0], ap[2]) != 0;
111 expr = strcmp(ap[0], ap[2]) < 0;
114 expr = strcmp(ap[0], ap[2]) > 0;
117 expr = simple_strtol(ap[0], NULL, 10) ==
118 simple_strtol(ap[2], NULL, 10);
121 expr = simple_strtol(ap[0], NULL, 10) !=
122 simple_strtol(ap[2], NULL, 10);
125 expr = simple_strtol(ap[0], NULL, 10) <
126 simple_strtol(ap[2], NULL, 10);
129 expr = simple_strtol(ap[0], NULL, 10) <=
130 simple_strtol(ap[2], NULL, 10);
133 expr = simple_strtol(ap[0], NULL, 10) >
134 simple_strtol(ap[2], NULL, 10);
137 expr = simple_strtol(ap[0], NULL, 10) >=
138 simple_strtol(ap[2], NULL, 10);
141 expr = file_exists(ap[1], ap[2], ap[3], FS_TYPE_ANY);
155 if (last_unop == OP_NOT)
156 last_unop = OP_INVALID;
161 if (last_unop == OP_NOT) {
163 last_unop = OP_INVALID;
166 if (last_binop == OP_OR)
167 expr = last_expr || expr;
168 else if (last_binop == OP_AND)
169 expr = last_expr && expr;
170 last_binop = OP_INVALID;
175 ap += adv; left -= adv;
180 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",