blob: 7285f75469fb15f7cf0daa1e9541ca6b3c887834 [file] [log] [blame]
Peter Tysereabe6d92009-10-16 17:36:25 -05001/*
2 * Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Peter Tysereabe6d92009-10-16 17:36:25 -05006 */
7
8#include <common.h>
9#include <command.h>
Stephen Warren0700db62014-02-03 13:21:06 -070010#include <fs.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
Kim Phillipsdc00a682012-10-29 13:34:31 +000054static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tysereabe6d92009-10-16 17:36:25 -050055{
Wolfgang Denk6262d0212010-06-28 22:00:46 +020056 char * const *ap;
Stephen Warren08b763b2014-02-03 13:21:04 -070057 int i, op, left, adv, expr, last_expr, last_unop, last_binop;
Peter Tysereabe6d92009-10-16 17:36:25 -050058
59 /* args? */
60 if (argc < 3)
61 return 1;
62
Joe Hershbergereeff2282012-08-17 10:56:12 +000063#ifdef DEBUG
Peter Tysereabe6d92009-10-16 17:36:25 -050064 {
Joe Hershbergereeff2282012-08-17 10:56:12 +000065 debug("test(%d):", argc);
Peter Tysereabe6d92009-10-16 17:36:25 -050066 left = 1;
67 while (argv[left])
Joe Hershbergereeff2282012-08-17 10:56:12 +000068 debug(" '%s'", argv[left++]);
Peter Tysereabe6d92009-10-16 17:36:25 -050069 }
70#endif
71
Stephen Warren08b763b2014-02-03 13:21:04 -070072 left = argc - 1;
73 ap = argv + 1;
Stephen Warren4f6e1aa2014-02-03 13:21:05 -070074 expr = 0;
Stephen Warren08b763b2014-02-03 13:21:04 -070075 last_unop = OP_INVALID;
76 last_binop = OP_INVALID;
Peter Tysereabe6d92009-10-16 17:36:25 -050077 last_expr = -1;
78 while (left > 0) {
Stephen Warren28cca552014-02-03 13:21:02 -070079 for (i = 0; i < ARRAY_SIZE(op_adv); i++) {
80 if (left <= op_adv[i].arg)
81 continue;
82 if (!strcmp(ap[op_adv[i].arg], op_adv[i].str)) {
83 op = op_adv[i].op;
84 adv = op_adv[i].adv;
85 break;
86 }
87 }
88 if (i == ARRAY_SIZE(op_adv)) {
Peter Tysereabe6d92009-10-16 17:36:25 -050089 expr = 1;
90 break;
91 }
Stephen Warren28cca552014-02-03 13:21:02 -070092 if (left < adv) {
93 expr = 1;
94 break;
Peter Tysereabe6d92009-10-16 17:36:25 -050095 }
96
Stephen Warren28cca552014-02-03 13:21:02 -070097 switch (op) {
98 case OP_STR_EMPTY:
99 expr = strlen(ap[1]) == 0 ? 1 : 0;
100 break;
101 case OP_STR_NEMPTY:
102 expr = strlen(ap[1]) == 0 ? 0 : 1;
103 break;
104 case OP_STR_EQ:
105 expr = strcmp(ap[0], ap[2]) == 0;
106 break;
107 case OP_STR_NEQ:
108 expr = strcmp(ap[0], ap[2]) != 0;
109 break;
110 case OP_STR_LT:
111 expr = strcmp(ap[0], ap[2]) < 0;
112 break;
113 case OP_STR_GT:
114 expr = strcmp(ap[0], ap[2]) > 0;
115 break;
116 case OP_INT_EQ:
117 expr = simple_strtol(ap[0], NULL, 10) ==
118 simple_strtol(ap[2], NULL, 10);
119 break;
120 case OP_INT_NEQ:
121 expr = simple_strtol(ap[0], NULL, 10) !=
122 simple_strtol(ap[2], NULL, 10);
123 break;
124 case OP_INT_LT:
125 expr = simple_strtol(ap[0], NULL, 10) <
126 simple_strtol(ap[2], NULL, 10);
127 break;
128 case OP_INT_LE:
129 expr = simple_strtol(ap[0], NULL, 10) <=
130 simple_strtol(ap[2], NULL, 10);
131 break;
132 case OP_INT_GT:
133 expr = simple_strtol(ap[0], NULL, 10) >
134 simple_strtol(ap[2], NULL, 10);
135 break;
136 case OP_INT_GE:
137 expr = simple_strtol(ap[0], NULL, 10) >=
138 simple_strtol(ap[2], NULL, 10);
139 break;
Stephen Warren0700db62014-02-03 13:21:06 -0700140 case OP_FILE_EXISTS:
141 expr = file_exists(ap[1], ap[2], ap[3], FS_TYPE_ANY);
142 break;
Peter Tysereabe6d92009-10-16 17:36:25 -0500143 }
144
Stephen Warren28cca552014-02-03 13:21:02 -0700145 switch (op) {
146 case OP_OR:
147 last_expr = expr;
Stephen Warren08b763b2014-02-03 13:21:04 -0700148 last_binop = OP_OR;
Stephen Warren28cca552014-02-03 13:21:02 -0700149 break;
150 case OP_AND:
151 last_expr = expr;
Stephen Warren08b763b2014-02-03 13:21:04 -0700152 last_binop = OP_AND;
153 break;
154 case OP_NOT:
155 if (last_unop == OP_NOT)
156 last_unop = OP_INVALID;
157 else
158 last_unop = OP_NOT;
Stephen Warren28cca552014-02-03 13:21:02 -0700159 break;
160 default:
Stephen Warren08b763b2014-02-03 13:21:04 -0700161 if (last_unop == OP_NOT) {
162 expr = !expr;
163 last_unop = OP_INVALID;
164 }
165
166 if (last_binop == OP_OR)
Peter Tysereabe6d92009-10-16 17:36:25 -0500167 expr = last_expr || expr;
Stephen Warren08b763b2014-02-03 13:21:04 -0700168 else if (last_binop == OP_AND)
Peter Tysereabe6d92009-10-16 17:36:25 -0500169 expr = last_expr && expr;
Stephen Warren08b763b2014-02-03 13:21:04 -0700170 last_binop = OP_INVALID;
171
Stephen Warren28cca552014-02-03 13:21:02 -0700172 break;
Peter Tysereabe6d92009-10-16 17:36:25 -0500173 }
174
175 ap += adv; left -= adv;
176 }
177
Peter Tysereabe6d92009-10-16 17:36:25 -0500178 expr = !expr;
179
180 debug (": returns %d\n", expr);
181
182 return expr;
183}
184
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200185#undef true
186#undef false
187
Peter Tysereabe6d92009-10-16 17:36:25 -0500188U_BOOT_CMD(
189 test, CONFIG_SYS_MAXARGS, 1, do_test,
190 "minimal test like /bin/sh",
191 "[args..]"
192);
Peter Tysercc35c492009-10-16 17:36:27 -0500193
Kim Phillipsdc00a682012-10-29 13:34:31 +0000194static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tysercc35c492009-10-16 17:36:27 -0500195{
196 return 1;
197}
198
199U_BOOT_CMD(
200 false, CONFIG_SYS_MAXARGS, 1, do_false,
201 "do nothing, unsuccessfully",
202 NULL
203);
204
Kim Phillipsdc00a682012-10-29 13:34:31 +0000205static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tysercc35c492009-10-16 17:36:27 -0500206{
207 return 0;
208}
209
210U_BOOT_CMD(
211 true, CONFIG_SYS_MAXARGS, 1, do_true,
212 "do nothing, successfully",
213 NULL
214);