blob: fa0c349f082747e593b4bd21cd62e5fbbeed1d0d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peter Tyser6b8f5ad2009-10-16 17:36:25 -05002/*
3 * Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Peter Tyser6b8f5ad2009-10-16 17:36:25 -05005 */
6
7#include <common.h>
8#include <command.h>
Stephen Warrene5e897c2014-02-03 13:21:06 -07009#include <fs.h>
Peter Tyser6b8f5ad2009-10-16 17:36:25 -050010
Stephen Warren490ba832014-02-03 13:21:02 -070011#define OP_INVALID 0
Stephen Warrend9b651c2014-02-03 13:21:04 -070012#define OP_NOT 1
Stephen Warren490ba832014-02-03 13:21:02 -070013#define OP_OR 2
14#define OP_AND 3
15#define OP_STR_EMPTY 4
16#define OP_STR_NEMPTY 5
17#define OP_STR_EQ 6
18#define OP_STR_NEQ 7
19#define OP_STR_LT 8
20#define OP_STR_GT 9
21#define OP_INT_EQ 10
22#define OP_INT_NEQ 11
23#define OP_INT_LT 12
24#define OP_INT_LE 13
25#define OP_INT_GT 14
26#define OP_INT_GE 15
Stephen Warrene5e897c2014-02-03 13:21:06 -070027#define OP_FILE_EXISTS 16
Stephen Warren490ba832014-02-03 13:21:02 -070028
29const struct {
30 int arg;
31 const char *str;
32 int op;
33 int adv;
34} op_adv[] = {
Stephen Warren490ba832014-02-03 13:21:02 -070035 {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},
Stephen Warrend9b651c2014-02-03 13:21:04 -070045 {0, "!", OP_NOT, 1},
Stephen Warren4c80f292014-02-03 13:21:03 -070046 {0, "-o", OP_OR, 1},
47 {0, "-a", OP_AND, 1},
48 {0, "-z", OP_STR_EMPTY, 2},
49 {0, "-n", OP_STR_NEMPTY, 2},
Stephen Warrene5e897c2014-02-03 13:21:06 -070050 {0, "-e", OP_FILE_EXISTS, 4},
Stephen Warren490ba832014-02-03 13:21:02 -070051};
52
Kim Phillips088f1b12012-10-29 13:34:31 +000053static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tyser6b8f5ad2009-10-16 17:36:25 -050054{
Wolfgang Denk54841ab2010-06-28 22:00:46 +020055 char * const *ap;
Stephen Warrend9b651c2014-02-03 13:21:04 -070056 int i, op, left, adv, expr, last_expr, last_unop, last_binop;
Peter Tyser6b8f5ad2009-10-16 17:36:25 -050057
58 /* args? */
59 if (argc < 3)
60 return 1;
61
Joe Hershbergerb0fe6ab2012-08-17 10:56:12 +000062#ifdef DEBUG
Peter Tyser6b8f5ad2009-10-16 17:36:25 -050063 {
Joe Hershbergerb0fe6ab2012-08-17 10:56:12 +000064 debug("test(%d):", argc);
Peter Tyser6b8f5ad2009-10-16 17:36:25 -050065 left = 1;
66 while (argv[left])
Joe Hershbergerb0fe6ab2012-08-17 10:56:12 +000067 debug(" '%s'", argv[left++]);
Peter Tyser6b8f5ad2009-10-16 17:36:25 -050068 }
69#endif
70
Stephen Warrend9b651c2014-02-03 13:21:04 -070071 left = argc - 1;
72 ap = argv + 1;
Stephen Warren2453de92014-02-03 13:21:05 -070073 expr = 0;
Stephen Warrend9b651c2014-02-03 13:21:04 -070074 last_unop = OP_INVALID;
75 last_binop = OP_INVALID;
Peter Tyser6b8f5ad2009-10-16 17:36:25 -050076 last_expr = -1;
77 while (left > 0) {
Stephen Warren490ba832014-02-03 13:21:02 -070078 for (i = 0; i < ARRAY_SIZE(op_adv); i++) {
79 if (left <= op_adv[i].arg)
80 continue;
81 if (!strcmp(ap[op_adv[i].arg], op_adv[i].str)) {
82 op = op_adv[i].op;
83 adv = op_adv[i].adv;
84 break;
85 }
86 }
87 if (i == ARRAY_SIZE(op_adv)) {
88 expr = 1;
89 break;
90 }
Peter Tyser6b8f5ad2009-10-16 17:36:25 -050091 if (left < adv) {
92 expr = 1;
93 break;
94 }
95
Stephen Warren490ba832014-02-03 13:21:02 -070096 switch (op) {
97 case OP_STR_EMPTY:
98 expr = strlen(ap[1]) == 0 ? 1 : 0;
99 break;
100 case OP_STR_NEMPTY:
101 expr = strlen(ap[1]) == 0 ? 0 : 1;
102 break;
103 case OP_STR_EQ:
104 expr = strcmp(ap[0], ap[2]) == 0;
105 break;
106 case OP_STR_NEQ:
107 expr = strcmp(ap[0], ap[2]) != 0;
108 break;
109 case OP_STR_LT:
110 expr = strcmp(ap[0], ap[2]) < 0;
111 break;
112 case OP_STR_GT:
113 expr = strcmp(ap[0], ap[2]) > 0;
114 break;
115 case OP_INT_EQ:
116 expr = simple_strtol(ap[0], NULL, 10) ==
117 simple_strtol(ap[2], NULL, 10);
118 break;
119 case OP_INT_NEQ:
120 expr = simple_strtol(ap[0], NULL, 10) !=
121 simple_strtol(ap[2], NULL, 10);
122 break;
123 case OP_INT_LT:
124 expr = simple_strtol(ap[0], NULL, 10) <
125 simple_strtol(ap[2], NULL, 10);
126 break;
127 case OP_INT_LE:
128 expr = simple_strtol(ap[0], NULL, 10) <=
129 simple_strtol(ap[2], NULL, 10);
130 break;
131 case OP_INT_GT:
132 expr = simple_strtol(ap[0], NULL, 10) >
133 simple_strtol(ap[2], NULL, 10);
134 break;
135 case OP_INT_GE:
136 expr = simple_strtol(ap[0], NULL, 10) >=
137 simple_strtol(ap[2], NULL, 10);
138 break;
Stephen Warrene5e897c2014-02-03 13:21:06 -0700139 case OP_FILE_EXISTS:
140 expr = file_exists(ap[1], ap[2], ap[3], FS_TYPE_ANY);
141 break;
Peter Tyser6b8f5ad2009-10-16 17:36:25 -0500142 }
143
Stephen Warren490ba832014-02-03 13:21:02 -0700144 switch (op) {
145 case OP_OR:
146 last_expr = expr;
Stephen Warrend9b651c2014-02-03 13:21:04 -0700147 last_binop = OP_OR;
Stephen Warren490ba832014-02-03 13:21:02 -0700148 break;
149 case OP_AND:
150 last_expr = expr;
Stephen Warrend9b651c2014-02-03 13:21:04 -0700151 last_binop = OP_AND;
152 break;
153 case OP_NOT:
154 if (last_unop == OP_NOT)
155 last_unop = OP_INVALID;
156 else
157 last_unop = OP_NOT;
Stephen Warren490ba832014-02-03 13:21:02 -0700158 break;
159 default:
Stephen Warrend9b651c2014-02-03 13:21:04 -0700160 if (last_unop == OP_NOT) {
161 expr = !expr;
162 last_unop = OP_INVALID;
163 }
164
165 if (last_binop == OP_OR)
Peter Tyser6b8f5ad2009-10-16 17:36:25 -0500166 expr = last_expr || expr;
Stephen Warrend9b651c2014-02-03 13:21:04 -0700167 else if (last_binop == OP_AND)
Peter Tyser6b8f5ad2009-10-16 17:36:25 -0500168 expr = last_expr && expr;
Stephen Warrend9b651c2014-02-03 13:21:04 -0700169 last_binop = OP_INVALID;
170
Stephen Warren490ba832014-02-03 13:21:02 -0700171 break;
Peter Tyser6b8f5ad2009-10-16 17:36:25 -0500172 }
173
174 ap += adv; left -= adv;
175 }
176
Peter Tyser6b8f5ad2009-10-16 17:36:25 -0500177 expr = !expr;
178
179 debug (": returns %d\n", expr);
180
181 return expr;
182}
183
Heiko Schocher09c32802015-04-27 07:42:05 +0200184#undef true
185#undef false
186
Peter Tyser6b8f5ad2009-10-16 17:36:25 -0500187U_BOOT_CMD(
188 test, CONFIG_SYS_MAXARGS, 1, do_test,
189 "minimal test like /bin/sh",
190 "[args..]"
191);
Peter Tyser396fd172009-10-16 17:36:27 -0500192
Kim Phillips088f1b12012-10-29 13:34:31 +0000193static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tyser396fd172009-10-16 17:36:27 -0500194{
195 return 1;
196}
197
198U_BOOT_CMD(
199 false, CONFIG_SYS_MAXARGS, 1, do_false,
200 "do nothing, unsuccessfully",
201 NULL
202);
203
Kim Phillips088f1b12012-10-29 13:34:31 +0000204static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tyser396fd172009-10-16 17:36:27 -0500205{
206 return 0;
207}
208
209U_BOOT_CMD(
210 true, CONFIG_SYS_MAXARGS, 1, do_true,
211 "do nothing, successfully",
212 NULL
213);