1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
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(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
58 int i
, op
, left
, adv
, expr
, last_expr
, last_unop
, last_binop
;
66 debug("test(%d):", argc
);
69 debug(" '%s'", argv
[left
++]);
76 last_unop
= OP_INVALID
;
77 last_binop
= OP_INVALID
;
80 for (i
= 0; i
< ARRAY_SIZE(op_adv
); i
++) {
81 if (left
<= op_adv
[i
].arg
)
83 if (!strcmp(ap
[op_adv
[i
].arg
], op_adv
[i
].str
)) {
89 if (i
== ARRAY_SIZE(op_adv
)) {
100 expr
= strlen(ap
[1]) == 0 ? 1 : 0;
103 expr
= strlen(ap
[1]) == 0 ? 0 : 1;
106 expr
= strcmp(ap
[0], ap
[2]) == 0;
109 expr
= strcmp(ap
[0], ap
[2]) != 0;
112 expr
= strcmp(ap
[0], ap
[2]) < 0;
115 expr
= strcmp(ap
[0], ap
[2]) > 0;
118 expr
= simple_strtol(ap
[0], NULL
, 0) ==
119 simple_strtol(ap
[2], NULL
, 0);
122 expr
= simple_strtol(ap
[0], NULL
, 0) !=
123 simple_strtol(ap
[2], NULL
, 0);
126 expr
= simple_strtol(ap
[0], NULL
, 0) <
127 simple_strtol(ap
[2], NULL
, 0);
130 expr
= simple_strtol(ap
[0], NULL
, 0) <=
131 simple_strtol(ap
[2], NULL
, 0);
134 expr
= simple_strtol(ap
[0], NULL
, 0) >
135 simple_strtol(ap
[2], NULL
, 0);
138 expr
= simple_strtol(ap
[0], NULL
, 0) >=
139 simple_strtol(ap
[2], NULL
, 0);
142 expr
= file_exists(ap
[1], ap
[2], ap
[3], FS_TYPE_ANY
);
156 if (last_unop
== OP_NOT
)
157 last_unop
= OP_INVALID
;
162 if (last_unop
== OP_NOT
) {
164 last_unop
= OP_INVALID
;
167 if (last_binop
== OP_OR
)
168 expr
= last_expr
|| expr
;
169 else if (last_binop
== OP_AND
)
170 expr
= last_expr
&& expr
;
171 last_binop
= OP_INVALID
;
176 ap
+= adv
; left
-= adv
;
181 debug (": returns %d\n", expr
);
190 test
, CONFIG_SYS_MAXARGS
, 1, do_test
,
191 "minimal test like /bin/sh",
195 static int do_false(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
202 false, CONFIG_SYS_MAXARGS
, 1, do_false
,
203 "do nothing, unsuccessfully",
207 static int do_true(struct cmd_tbl
*cmdtp
, int flag
, int argc
,
214 true, CONFIG_SYS_MAXARGS
, 1, do_true
,
215 "do nothing, successfully",