blob: a9ac07e6143559247163cf79840ba0da0dfcda12 [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>
Rasmus Villemoes1f1b2322025-05-13 10:40:23 +020010#include <slre.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060011#include <vsprintf.h>
Tom Riniee8ed542025-05-14 16:46:00 -060012#include <linux/string.h>
Peter Tysereabe6d92009-10-16 17:36:25 -050013
Stephen Warren28cca552014-02-03 13:21:02 -070014#define OP_INVALID 0
Stephen Warren08b763b2014-02-03 13:21:04 -070015#define OP_NOT 1
Stephen Warren28cca552014-02-03 13:21:02 -070016#define OP_OR 2
17#define OP_AND 3
18#define OP_STR_EMPTY 4
19#define OP_STR_NEMPTY 5
20#define OP_STR_EQ 6
21#define OP_STR_NEQ 7
22#define OP_STR_LT 8
23#define OP_STR_GT 9
24#define OP_INT_EQ 10
25#define OP_INT_NEQ 11
26#define OP_INT_LT 12
27#define OP_INT_LE 13
28#define OP_INT_GT 14
29#define OP_INT_GE 15
Stephen Warren0700db62014-02-03 13:21:06 -070030#define OP_FILE_EXISTS 16
Rasmus Villemoes1f1b2322025-05-13 10:40:23 +020031#define OP_REGEX 17
Stephen Warren28cca552014-02-03 13:21:02 -070032
33const struct {
34 int arg;
35 const char *str;
36 int op;
37 int adv;
38} op_adv[] = {
Stephen Warren28cca552014-02-03 13:21:02 -070039 {1, "=", OP_STR_EQ, 3},
40 {1, "!=", OP_STR_NEQ, 3},
41 {1, "<", OP_STR_LT, 3},
42 {1, ">", OP_STR_GT, 3},
43 {1, "-eq", OP_INT_EQ, 3},
44 {1, "-ne", OP_INT_NEQ, 3},
45 {1, "-lt", OP_INT_LT, 3},
46 {1, "-le", OP_INT_LE, 3},
47 {1, "-gt", OP_INT_GT, 3},
48 {1, "-ge", OP_INT_GE, 3},
Stephen Warren08b763b2014-02-03 13:21:04 -070049 {0, "!", OP_NOT, 1},
Stephen Warren4c1b2812014-02-03 13:21:03 -070050 {0, "-o", OP_OR, 1},
51 {0, "-a", OP_AND, 1},
52 {0, "-z", OP_STR_EMPTY, 2},
53 {0, "-n", OP_STR_NEMPTY, 2},
Stephen Warren0700db62014-02-03 13:21:06 -070054 {0, "-e", OP_FILE_EXISTS, 4},
Rasmus Villemoes1f1b2322025-05-13 10:40:23 +020055#ifdef CONFIG_REGEX
56 {1, "=~", OP_REGEX, 3},
57#endif
Stephen Warren28cca552014-02-03 13:21:02 -070058};
59
Simon Glassed38aef2020-05-10 11:40:03 -060060static int do_test(struct cmd_tbl *cmdtp, int flag, int argc,
61 char *const argv[])
Peter Tysereabe6d92009-10-16 17:36:25 -050062{
Wolfgang Denk6262d0212010-06-28 22:00:46 +020063 char * const *ap;
Stephen Warren08b763b2014-02-03 13:21:04 -070064 int i, op, left, adv, expr, last_expr, last_unop, last_binop;
Peter Tysereabe6d92009-10-16 17:36:25 -050065
66 /* args? */
67 if (argc < 3)
68 return 1;
69
Joe Hershbergereeff2282012-08-17 10:56:12 +000070#ifdef DEBUG
Peter Tysereabe6d92009-10-16 17:36:25 -050071 {
Joe Hershbergereeff2282012-08-17 10:56:12 +000072 debug("test(%d):", argc);
Peter Tysereabe6d92009-10-16 17:36:25 -050073 left = 1;
74 while (argv[left])
Joe Hershbergereeff2282012-08-17 10:56:12 +000075 debug(" '%s'", argv[left++]);
Peter Tysereabe6d92009-10-16 17:36:25 -050076 }
77#endif
78
Stephen Warren08b763b2014-02-03 13:21:04 -070079 left = argc - 1;
80 ap = argv + 1;
Stephen Warren4f6e1aa2014-02-03 13:21:05 -070081 expr = 0;
Stephen Warren08b763b2014-02-03 13:21:04 -070082 last_unop = OP_INVALID;
83 last_binop = OP_INVALID;
Peter Tysereabe6d92009-10-16 17:36:25 -050084 last_expr = -1;
85 while (left > 0) {
Stephen Warren28cca552014-02-03 13:21:02 -070086 for (i = 0; i < ARRAY_SIZE(op_adv); i++) {
87 if (left <= op_adv[i].arg)
88 continue;
89 if (!strcmp(ap[op_adv[i].arg], op_adv[i].str)) {
90 op = op_adv[i].op;
91 adv = op_adv[i].adv;
92 break;
93 }
94 }
95 if (i == ARRAY_SIZE(op_adv)) {
Peter Tysereabe6d92009-10-16 17:36:25 -050096 expr = 1;
97 break;
98 }
Stephen Warren28cca552014-02-03 13:21:02 -070099 if (left < adv) {
100 expr = 1;
101 break;
Peter Tysereabe6d92009-10-16 17:36:25 -0500102 }
103
Stephen Warren28cca552014-02-03 13:21:02 -0700104 switch (op) {
105 case OP_STR_EMPTY:
106 expr = strlen(ap[1]) == 0 ? 1 : 0;
107 break;
108 case OP_STR_NEMPTY:
109 expr = strlen(ap[1]) == 0 ? 0 : 1;
110 break;
111 case OP_STR_EQ:
112 expr = strcmp(ap[0], ap[2]) == 0;
113 break;
114 case OP_STR_NEQ:
115 expr = strcmp(ap[0], ap[2]) != 0;
116 break;
117 case OP_STR_LT:
118 expr = strcmp(ap[0], ap[2]) < 0;
119 break;
120 case OP_STR_GT:
121 expr = strcmp(ap[0], ap[2]) > 0;
122 break;
123 case OP_INT_EQ:
T Karthik Reddyd3964102019-09-11 15:39:53 +0200124 expr = simple_strtol(ap[0], NULL, 0) ==
125 simple_strtol(ap[2], NULL, 0);
Stephen Warren28cca552014-02-03 13:21:02 -0700126 break;
127 case OP_INT_NEQ:
T Karthik Reddyd3964102019-09-11 15:39:53 +0200128 expr = simple_strtol(ap[0], NULL, 0) !=
129 simple_strtol(ap[2], NULL, 0);
Stephen Warren28cca552014-02-03 13:21:02 -0700130 break;
131 case OP_INT_LT:
T Karthik Reddyd3964102019-09-11 15:39:53 +0200132 expr = simple_strtol(ap[0], NULL, 0) <
133 simple_strtol(ap[2], NULL, 0);
Stephen Warren28cca552014-02-03 13:21:02 -0700134 break;
135 case OP_INT_LE:
T Karthik Reddyd3964102019-09-11 15:39:53 +0200136 expr = simple_strtol(ap[0], NULL, 0) <=
137 simple_strtol(ap[2], NULL, 0);
Stephen Warren28cca552014-02-03 13:21:02 -0700138 break;
139 case OP_INT_GT:
T Karthik Reddyd3964102019-09-11 15:39:53 +0200140 expr = simple_strtol(ap[0], NULL, 0) >
141 simple_strtol(ap[2], NULL, 0);
Stephen Warren28cca552014-02-03 13:21:02 -0700142 break;
143 case OP_INT_GE:
T Karthik Reddyd3964102019-09-11 15:39:53 +0200144 expr = simple_strtol(ap[0], NULL, 0) >=
145 simple_strtol(ap[2], NULL, 0);
Stephen Warren28cca552014-02-03 13:21:02 -0700146 break;
Stephen Warren0700db62014-02-03 13:21:06 -0700147 case OP_FILE_EXISTS:
148 expr = file_exists(ap[1], ap[2], ap[3], FS_TYPE_ANY);
149 break;
Rasmus Villemoes1f1b2322025-05-13 10:40:23 +0200150#ifdef CONFIG_REGEX
151 case OP_REGEX: {
152 struct slre slre;
153
154 if (slre_compile(&slre, ap[2]) == 0) {
155 printf("Error compiling regex: %s\n", slre.err_str);
156 expr = 0;
157 break;
158 }
159
160 expr = slre_match(&slre, ap[0], strlen(ap[0]), NULL);
161 break;
Peter Tysereabe6d92009-10-16 17:36:25 -0500162 }
Rasmus Villemoes1f1b2322025-05-13 10:40:23 +0200163#endif
164 }
Peter Tysereabe6d92009-10-16 17:36:25 -0500165
Stephen Warren28cca552014-02-03 13:21:02 -0700166 switch (op) {
167 case OP_OR:
168 last_expr = expr;
Stephen Warren08b763b2014-02-03 13:21:04 -0700169 last_binop = OP_OR;
Stephen Warren28cca552014-02-03 13:21:02 -0700170 break;
171 case OP_AND:
172 last_expr = expr;
Stephen Warren08b763b2014-02-03 13:21:04 -0700173 last_binop = OP_AND;
174 break;
175 case OP_NOT:
176 if (last_unop == OP_NOT)
177 last_unop = OP_INVALID;
178 else
179 last_unop = OP_NOT;
Stephen Warren28cca552014-02-03 13:21:02 -0700180 break;
181 default:
Stephen Warren08b763b2014-02-03 13:21:04 -0700182 if (last_unop == OP_NOT) {
183 expr = !expr;
184 last_unop = OP_INVALID;
185 }
186
187 if (last_binop == OP_OR)
Peter Tysereabe6d92009-10-16 17:36:25 -0500188 expr = last_expr || expr;
Stephen Warren08b763b2014-02-03 13:21:04 -0700189 else if (last_binop == OP_AND)
Peter Tysereabe6d92009-10-16 17:36:25 -0500190 expr = last_expr && expr;
Stephen Warren08b763b2014-02-03 13:21:04 -0700191 last_binop = OP_INVALID;
192
Stephen Warren28cca552014-02-03 13:21:02 -0700193 break;
Peter Tysereabe6d92009-10-16 17:36:25 -0500194 }
195
196 ap += adv; left -= adv;
197 }
198
Peter Tysereabe6d92009-10-16 17:36:25 -0500199 expr = !expr;
200
201 debug (": returns %d\n", expr);
202
203 return expr;
204}
205
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200206#undef true
207#undef false
208
Peter Tysereabe6d92009-10-16 17:36:25 -0500209U_BOOT_CMD(
210 test, CONFIG_SYS_MAXARGS, 1, do_test,
211 "minimal test like /bin/sh",
212 "[args..]"
213);
Peter Tysercc35c492009-10-16 17:36:27 -0500214
Simon Glassed38aef2020-05-10 11:40:03 -0600215static int do_false(struct cmd_tbl *cmdtp, int flag, int argc,
216 char *const argv[])
Peter Tysercc35c492009-10-16 17:36:27 -0500217{
218 return 1;
219}
220
221U_BOOT_CMD(
222 false, CONFIG_SYS_MAXARGS, 1, do_false,
223 "do nothing, unsuccessfully",
224 NULL
225);
226
Simon Glassed38aef2020-05-10 11:40:03 -0600227static int do_true(struct cmd_tbl *cmdtp, int flag, int argc,
228 char *const argv[])
Peter Tysercc35c492009-10-16 17:36:27 -0500229{
230 return 0;
231}
232
233U_BOOT_CMD(
234 true, CONFIG_SYS_MAXARGS, 1, do_true,
235 "do nothing, successfully",
236 NULL
237);