]> git.ipfire.org Git - people/ms/u-boot.git/blame - cmd/test.c
ARM: uniphier: disable CONFIG_ARCH_FIXUP_FDT_MEMORY
[people/ms/u-boot.git] / cmd / test.c
CommitLineData
6b8f5ad1
PT
1/*
2 * Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
6b8f5ad1
PT
6 */
7
8#include <common.h>
9#include <command.h>
e5e897c0 10#include <fs.h>
6b8f5ad1 11
490ba833 12#define OP_INVALID 0
d9b651ce 13#define OP_NOT 1
490ba833
SW
14#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
e5e897c0 28#define OP_FILE_EXISTS 16
490ba833
SW
29
30const struct {
31 int arg;
32 const char *str;
33 int op;
34 int adv;
35} op_adv[] = {
490ba833
SW
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},
d9b651ce 46 {0, "!", OP_NOT, 1},
4c80f29e
SW
47 {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},
e5e897c0 51 {0, "-e", OP_FILE_EXISTS, 4},
490ba833
SW
52};
53
088f1b19 54static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
6b8f5ad1 55{
54841ab5 56 char * const *ap;
d9b651ce 57 int i, op, left, adv, expr, last_expr, last_unop, last_binop;
6b8f5ad1
PT
58
59 /* args? */
60 if (argc < 3)
61 return 1;
62
b0fe6abd 63#ifdef DEBUG
6b8f5ad1 64 {
b0fe6abd 65 debug("test(%d):", argc);
6b8f5ad1
PT
66 left = 1;
67 while (argv[left])
b0fe6abd 68 debug(" '%s'", argv[left++]);
6b8f5ad1
PT
69 }
70#endif
71
d9b651ce
SW
72 left = argc - 1;
73 ap = argv + 1;
2453de99 74 expr = 0;
d9b651ce
SW
75 last_unop = OP_INVALID;
76 last_binop = OP_INVALID;
6b8f5ad1
PT
77 last_expr = -1;
78 while (left > 0) {
490ba833
SW
79 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)) {
6b8f5ad1
PT
89 expr = 1;
90 break;
91 }
490ba833
SW
92 if (left < adv) {
93 expr = 1;
94 break;
6b8f5ad1
PT
95 }
96
490ba833
SW
97 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;
e5e897c0
SW
140 case OP_FILE_EXISTS:
141 expr = file_exists(ap[1], ap[2], ap[3], FS_TYPE_ANY);
142 break;
6b8f5ad1
PT
143 }
144
490ba833
SW
145 switch (op) {
146 case OP_OR:
147 last_expr = expr;
d9b651ce 148 last_binop = OP_OR;
490ba833
SW
149 break;
150 case OP_AND:
151 last_expr = expr;
d9b651ce
SW
152 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;
490ba833
SW
159 break;
160 default:
d9b651ce
SW
161 if (last_unop == OP_NOT) {
162 expr = !expr;
163 last_unop = OP_INVALID;
164 }
165
166 if (last_binop == OP_OR)
6b8f5ad1 167 expr = last_expr || expr;
d9b651ce 168 else if (last_binop == OP_AND)
6b8f5ad1 169 expr = last_expr && expr;
d9b651ce
SW
170 last_binop = OP_INVALID;
171
490ba833 172 break;
6b8f5ad1
PT
173 }
174
175 ap += adv; left -= adv;
176 }
177
6b8f5ad1
PT
178 expr = !expr;
179
180 debug (": returns %d\n", expr);
181
182 return expr;
183}
184
09c32807
HS
185#undef true
186#undef false
187
6b8f5ad1
PT
188U_BOOT_CMD(
189 test, CONFIG_SYS_MAXARGS, 1, do_test,
190 "minimal test like /bin/sh",
191 "[args..]"
192);
396fd173 193
088f1b19 194static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
396fd173
PT
195{
196 return 1;
197}
198
199U_BOOT_CMD(
200 false, CONFIG_SYS_MAXARGS, 1, do_false,
201 "do nothing, unsuccessfully",
202 NULL
203);
204
088f1b19 205static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
396fd173
PT
206{
207 return 0;
208}
209
210U_BOOT_CMD(
211 true, CONFIG_SYS_MAXARGS, 1, do_true,
212 "do nothing, successfully",
213 NULL
214);