blob: b4c3eabf9f604279285e941f82399c5c89a38215 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peter Tysereabe6d92009-10-16 17:36:25 -05002/*
3 * Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Peter Tysereabe6d92009-10-16 17:36:25 -05005 */
6
Peter Tysereabe6d92009-10-16 17:36:25 -05007#include <command.h>
Stephen Warren0700db62014-02-03 13:21:06 -07008#include <fs.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060010#include <vsprintf.h>
Peter Tysereabe6d92009-10-16 17:36:25 -050011
Stephen Warren28cca552014-02-03 13:21:02 -070012#define OP_INVALID 0
Stephen Warren08b763b2014-02-03 13:21:04 -070013#define OP_NOT 1
Stephen Warren28cca552014-02-03 13:21:02 -070014#define OP_OR 2
15#define OP_AND 3
16#define OP_STR_EMPTY 4
17#define OP_STR_NEMPTY 5
18#define OP_STR_EQ 6
19#define OP_STR_NEQ 7
20#define OP_STR_LT 8
21#define OP_STR_GT 9
22#define OP_INT_EQ 10
23#define OP_INT_NEQ 11
24#define OP_INT_LT 12
25#define OP_INT_LE 13
26#define OP_INT_GT 14
27#define OP_INT_GE 15
Stephen Warren0700db62014-02-03 13:21:06 -070028#define OP_FILE_EXISTS 16
Stephen Warren28cca552014-02-03 13:21:02 -070029
30const struct {
31 int arg;
32 const char *str;
33 int op;
34 int adv;
35} op_adv[] = {
Stephen Warren28cca552014-02-03 13:21:02 -070036 {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},
Stephen Warren08b763b2014-02-03 13:21:04 -070046 {0, "!", OP_NOT, 1},
Stephen Warren4c1b2812014-02-03 13:21:03 -070047 {0, "-o", OP_OR, 1},
48 {0, "-a", OP_AND, 1},
49 {0, "-z", OP_STR_EMPTY, 2},
50 {0, "-n", OP_STR_NEMPTY, 2},
Stephen Warren0700db62014-02-03 13:21:06 -070051 {0, "-e", OP_FILE_EXISTS, 4},
Stephen Warren28cca552014-02-03 13:21:02 -070052};
53
Simon Glassed38aef2020-05-10 11:40:03 -060054static int do_test(struct cmd_tbl *cmdtp, int flag, int argc,
55 char *const argv[])
Peter Tysereabe6d92009-10-16 17:36:25 -050056{
Wolfgang Denk6262d0212010-06-28 22:00:46 +020057 char * const *ap;
Stephen Warren08b763b2014-02-03 13:21:04 -070058 int i, op, left, adv, expr, last_expr, last_unop, last_binop;
Peter Tysereabe6d92009-10-16 17:36:25 -050059
60 /* args? */
61 if (argc < 3)
62 return 1;
63
Joe Hershbergereeff2282012-08-17 10:56:12 +000064#ifdef DEBUG
Peter Tysereabe6d92009-10-16 17:36:25 -050065 {
Joe Hershbergereeff2282012-08-17 10:56:12 +000066 debug("test(%d):", argc);
Peter Tysereabe6d92009-10-16 17:36:25 -050067 left = 1;
68 while (argv[left])
Joe Hershbergereeff2282012-08-17 10:56:12 +000069 debug(" '%s'", argv[left++]);
Peter Tysereabe6d92009-10-16 17:36:25 -050070 }
71#endif
72
Stephen Warren08b763b2014-02-03 13:21:04 -070073 left = argc - 1;
74 ap = argv + 1;
Stephen Warren4f6e1aa2014-02-03 13:21:05 -070075 expr = 0;
Stephen Warren08b763b2014-02-03 13:21:04 -070076 last_unop = OP_INVALID;
77 last_binop = OP_INVALID;
Peter Tysereabe6d92009-10-16 17:36:25 -050078 last_expr = -1;
79 while (left > 0) {
Stephen Warren28cca552014-02-03 13:21:02 -070080 for (i = 0; i < ARRAY_SIZE(op_adv); i++) {
81 if (left <= op_adv[i].arg)
82 continue;
83 if (!strcmp(ap[op_adv[i].arg], op_adv[i].str)) {
84 op = op_adv[i].op;
85 adv = op_adv[i].adv;
86 break;
87 }
88 }
89 if (i == ARRAY_SIZE(op_adv)) {
Peter Tysereabe6d92009-10-16 17:36:25 -050090 expr = 1;
91 break;
92 }
Stephen Warren28cca552014-02-03 13:21:02 -070093 if (left < adv) {
94 expr = 1;
95 break;
Peter Tysereabe6d92009-10-16 17:36:25 -050096 }
97
Stephen Warren28cca552014-02-03 13:21:02 -070098 switch (op) {
99 case OP_STR_EMPTY:
100 expr = strlen(ap[1]) == 0 ? 1 : 0;
101 break;
102 case OP_STR_NEMPTY:
103 expr = strlen(ap[1]) == 0 ? 0 : 1;
104 break;
105 case OP_STR_EQ:
106 expr = strcmp(ap[0], ap[2]) == 0;
107 break;
108 case OP_STR_NEQ:
109 expr = strcmp(ap[0], ap[2]) != 0;
110 break;
111 case OP_STR_LT:
112 expr = strcmp(ap[0], ap[2]) < 0;
113 break;
114 case OP_STR_GT:
115 expr = strcmp(ap[0], ap[2]) > 0;
116 break;
117 case OP_INT_EQ:
T Karthik Reddyd3964102019-09-11 15:39:53 +0200118 expr = simple_strtol(ap[0], NULL, 0) ==
119 simple_strtol(ap[2], NULL, 0);
Stephen Warren28cca552014-02-03 13:21:02 -0700120 break;
121 case OP_INT_NEQ:
T Karthik Reddyd3964102019-09-11 15:39:53 +0200122 expr = simple_strtol(ap[0], NULL, 0) !=
123 simple_strtol(ap[2], NULL, 0);
Stephen Warren28cca552014-02-03 13:21:02 -0700124 break;
125 case OP_INT_LT:
T Karthik Reddyd3964102019-09-11 15:39:53 +0200126 expr = simple_strtol(ap[0], NULL, 0) <
127 simple_strtol(ap[2], NULL, 0);
Stephen Warren28cca552014-02-03 13:21:02 -0700128 break;
129 case OP_INT_LE:
T Karthik Reddyd3964102019-09-11 15:39:53 +0200130 expr = simple_strtol(ap[0], NULL, 0) <=
131 simple_strtol(ap[2], NULL, 0);
Stephen Warren28cca552014-02-03 13:21:02 -0700132 break;
133 case OP_INT_GT:
T Karthik Reddyd3964102019-09-11 15:39:53 +0200134 expr = simple_strtol(ap[0], NULL, 0) >
135 simple_strtol(ap[2], NULL, 0);
Stephen Warren28cca552014-02-03 13:21:02 -0700136 break;
137 case OP_INT_GE:
T Karthik Reddyd3964102019-09-11 15:39:53 +0200138 expr = simple_strtol(ap[0], NULL, 0) >=
139 simple_strtol(ap[2], NULL, 0);
Stephen Warren28cca552014-02-03 13:21:02 -0700140 break;
Stephen Warren0700db62014-02-03 13:21:06 -0700141 case OP_FILE_EXISTS:
142 expr = file_exists(ap[1], ap[2], ap[3], FS_TYPE_ANY);
143 break;
Peter Tysereabe6d92009-10-16 17:36:25 -0500144 }
145
Stephen Warren28cca552014-02-03 13:21:02 -0700146 switch (op) {
147 case OP_OR:
148 last_expr = expr;
Stephen Warren08b763b2014-02-03 13:21:04 -0700149 last_binop = OP_OR;
Stephen Warren28cca552014-02-03 13:21:02 -0700150 break;
151 case OP_AND:
152 last_expr = expr;
Stephen Warren08b763b2014-02-03 13:21:04 -0700153 last_binop = OP_AND;
154 break;
155 case OP_NOT:
156 if (last_unop == OP_NOT)
157 last_unop = OP_INVALID;
158 else
159 last_unop = OP_NOT;
Stephen Warren28cca552014-02-03 13:21:02 -0700160 break;
161 default:
Stephen Warren08b763b2014-02-03 13:21:04 -0700162 if (last_unop == OP_NOT) {
163 expr = !expr;
164 last_unop = OP_INVALID;
165 }
166
167 if (last_binop == OP_OR)
Peter Tysereabe6d92009-10-16 17:36:25 -0500168 expr = last_expr || expr;
Stephen Warren08b763b2014-02-03 13:21:04 -0700169 else if (last_binop == OP_AND)
Peter Tysereabe6d92009-10-16 17:36:25 -0500170 expr = last_expr && expr;
Stephen Warren08b763b2014-02-03 13:21:04 -0700171 last_binop = OP_INVALID;
172
Stephen Warren28cca552014-02-03 13:21:02 -0700173 break;
Peter Tysereabe6d92009-10-16 17:36:25 -0500174 }
175
176 ap += adv; left -= adv;
177 }
178
Peter Tysereabe6d92009-10-16 17:36:25 -0500179 expr = !expr;
180
181 debug (": returns %d\n", expr);
182
183 return expr;
184}
185
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200186#undef true
187#undef false
188
Peter Tysereabe6d92009-10-16 17:36:25 -0500189U_BOOT_CMD(
190 test, CONFIG_SYS_MAXARGS, 1, do_test,
191 "minimal test like /bin/sh",
192 "[args..]"
193);
Peter Tysercc35c492009-10-16 17:36:27 -0500194
Simon Glassed38aef2020-05-10 11:40:03 -0600195static int do_false(struct cmd_tbl *cmdtp, int flag, int argc,
196 char *const argv[])
Peter Tysercc35c492009-10-16 17:36:27 -0500197{
198 return 1;
199}
200
201U_BOOT_CMD(
202 false, CONFIG_SYS_MAXARGS, 1, do_false,
203 "do nothing, unsuccessfully",
204 NULL
205);
206
Simon Glassed38aef2020-05-10 11:40:03 -0600207static int do_true(struct cmd_tbl *cmdtp, int flag, int argc,
208 char *const argv[])
Peter Tysercc35c492009-10-16 17:36:27 -0500209{
210 return 0;
211}
212
213U_BOOT_CMD(
214 true, CONFIG_SYS_MAXARGS, 1, do_true,
215 "do nothing, successfully",
216 NULL
217);