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