1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
15 #define OP_STR_EMPTY 4
16 #define OP_STR_NEMPTY 5
27 #define OP_FILE_EXISTS 16
35 {1, "=", OP_STR_EQ, 3},
36 {1, "!=", OP_STR_NEQ, 3},
37 {1, "<", OP_STR_LT, 3},
38 {1, ">", OP_STR_GT, 3},
39 {1, "-eq", OP_INT_EQ, 3},
40 {1, "-ne", OP_INT_NEQ, 3},
41 {1, "-lt", OP_INT_LT, 3},
42 {1, "-le", OP_INT_LE, 3},
43 {1, "-gt", OP_INT_GT, 3},
44 {1, "-ge", OP_INT_GE, 3},
48 {0, "-z", OP_STR_EMPTY, 2},
49 {0, "-n", OP_STR_NEMPTY, 2},
50 {0, "-e", OP_FILE_EXISTS, 4},
53 static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
56 int i, op, left, adv, expr, last_expr, last_unop, last_binop;
64 debug("test(%d):", argc);
67 debug(" '%s'", argv[left++]);
74 last_unop = OP_INVALID;
75 last_binop = OP_INVALID;
78 for (i = 0; i < ARRAY_SIZE(op_adv); i++) {
79 if (left <= op_adv[i].arg)
81 if (!strcmp(ap[op_adv[i].arg], op_adv[i].str)) {
87 if (i == ARRAY_SIZE(op_adv)) {
98 expr = strlen(ap[1]) == 0 ? 1 : 0;
101 expr = strlen(ap[1]) == 0 ? 0 : 1;
104 expr = strcmp(ap[0], ap[2]) == 0;
107 expr = strcmp(ap[0], ap[2]) != 0;
110 expr = strcmp(ap[0], ap[2]) < 0;
113 expr = strcmp(ap[0], ap[2]) > 0;
116 expr = simple_strtol(ap[0], NULL, 10) ==
117 simple_strtol(ap[2], NULL, 10);
120 expr = simple_strtol(ap[0], NULL, 10) !=
121 simple_strtol(ap[2], NULL, 10);
124 expr = simple_strtol(ap[0], NULL, 10) <
125 simple_strtol(ap[2], NULL, 10);
128 expr = simple_strtol(ap[0], NULL, 10) <=
129 simple_strtol(ap[2], NULL, 10);
132 expr = simple_strtol(ap[0], NULL, 10) >
133 simple_strtol(ap[2], NULL, 10);
136 expr = simple_strtol(ap[0], NULL, 10) >=
137 simple_strtol(ap[2], NULL, 10);
140 expr = file_exists(ap[1], ap[2], ap[3], FS_TYPE_ANY);
154 if (last_unop == OP_NOT)
155 last_unop = OP_INVALID;
160 if (last_unop == OP_NOT) {
162 last_unop = OP_INVALID;
165 if (last_binop == OP_OR)
166 expr = last_expr || expr;
167 else if (last_binop == OP_AND)
168 expr = last_expr && expr;
169 last_binop = OP_INVALID;
174 ap += adv; left -= adv;
179 debug (": returns %d\n", expr);
188 test, CONFIG_SYS_MAXARGS, 1, do_test,
189 "minimal test like /bin/sh",
193 static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
199 false, CONFIG_SYS_MAXARGS, 1, do_false,
200 "do nothing, unsuccessfully",
204 static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
210 true, CONFIG_SYS_MAXARGS, 1, do_true,
211 "do nothing, successfully",