blob: acc0ecf99b760c0de60b98145f41a0d4b4a066a0 [file] [log] [blame]
Peter Tyser6b8f5ad2009-10-16 17:36:25 -05001/*
2 * Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
York Suna6142702013-04-04 11:52:53 +000024/*
25 * Define _STDBOOL_H here to avoid macro expansion of true and false.
26 * If the future code requires macro true or false, remove this define
27 * and undef true and false before U_BOOT_CMD. This define and comment
28 * shall be removed if change to U_BOOT_CMD is made to take string
29 * instead of stringifying it.
30 */
31#define _STDBOOL_H
32
Peter Tyser6b8f5ad2009-10-16 17:36:25 -050033#include <common.h>
34#include <command.h>
35
Kim Phillips088f1b12012-10-29 13:34:31 +000036static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tyser6b8f5ad2009-10-16 17:36:25 -050037{
Wolfgang Denk54841ab2010-06-28 22:00:46 +020038 char * const *ap;
Peter Tyser6b8f5ad2009-10-16 17:36:25 -050039 int left, adv, expr, last_expr, neg, last_cmp;
40
41 /* args? */
42 if (argc < 3)
43 return 1;
44
Joe Hershbergerb0fe6ab2012-08-17 10:56:12 +000045#ifdef DEBUG
Peter Tyser6b8f5ad2009-10-16 17:36:25 -050046 {
Joe Hershbergerb0fe6ab2012-08-17 10:56:12 +000047 debug("test(%d):", argc);
Peter Tyser6b8f5ad2009-10-16 17:36:25 -050048 left = 1;
49 while (argv[left])
Joe Hershbergerb0fe6ab2012-08-17 10:56:12 +000050 debug(" '%s'", argv[left++]);
Peter Tyser6b8f5ad2009-10-16 17:36:25 -050051 }
52#endif
53
54 last_expr = 0;
55 left = argc - 1; ap = argv + 1;
56 if (left > 0 && strcmp(ap[0], "!") == 0) {
57 neg = 1;
58 ap++;
59 left--;
60 } else
61 neg = 0;
62
63 expr = -1;
64 last_cmp = -1;
65 last_expr = -1;
66 while (left > 0) {
67
68 if (strcmp(ap[0], "-o") == 0 || strcmp(ap[0], "-a") == 0)
69 adv = 1;
70 else if (strcmp(ap[0], "-z") == 0 || strcmp(ap[0], "-n") == 0)
71 adv = 2;
72 else
73 adv = 3;
74
75 if (left < adv) {
76 expr = 1;
77 break;
78 }
79
80 if (adv == 1) {
81 if (strcmp(ap[0], "-o") == 0) {
82 last_expr = expr;
83 last_cmp = 0;
84 } else if (strcmp(ap[0], "-a") == 0) {
85 last_expr = expr;
86 last_cmp = 1;
87 } else {
88 expr = 1;
89 break;
90 }
91 }
92
93 if (adv == 2) {
94 if (strcmp(ap[0], "-z") == 0)
95 expr = strlen(ap[1]) == 0 ? 1 : 0;
96 else if (strcmp(ap[0], "-n") == 0)
97 expr = strlen(ap[1]) == 0 ? 0 : 1;
98 else {
99 expr = 1;
100 break;
101 }
102
103 if (last_cmp == 0)
104 expr = last_expr || expr;
105 else if (last_cmp == 1)
106 expr = last_expr && expr;
107 last_cmp = -1;
108 }
109
110 if (adv == 3) {
111 if (strcmp(ap[1], "=") == 0)
112 expr = strcmp(ap[0], ap[2]) == 0;
113 else if (strcmp(ap[1], "!=") == 0)
114 expr = strcmp(ap[0], ap[2]) != 0;
115 else if (strcmp(ap[1], ">") == 0)
116 expr = strcmp(ap[0], ap[2]) > 0;
117 else if (strcmp(ap[1], "<") == 0)
118 expr = strcmp(ap[0], ap[2]) < 0;
119 else if (strcmp(ap[1], "-eq") == 0)
120 expr = simple_strtol(ap[0], NULL, 10) == simple_strtol(ap[2], NULL, 10);
121 else if (strcmp(ap[1], "-ne") == 0)
122 expr = simple_strtol(ap[0], NULL, 10) != simple_strtol(ap[2], NULL, 10);
123 else if (strcmp(ap[1], "-lt") == 0)
124 expr = simple_strtol(ap[0], NULL, 10) < simple_strtol(ap[2], NULL, 10);
125 else if (strcmp(ap[1], "-le") == 0)
126 expr = simple_strtol(ap[0], NULL, 10) <= simple_strtol(ap[2], NULL, 10);
127 else if (strcmp(ap[1], "-gt") == 0)
128 expr = simple_strtol(ap[0], NULL, 10) > simple_strtol(ap[2], NULL, 10);
129 else if (strcmp(ap[1], "-ge") == 0)
130 expr = simple_strtol(ap[0], NULL, 10) >= simple_strtol(ap[2], NULL, 10);
131 else {
132 expr = 1;
133 break;
134 }
135
136 if (last_cmp == 0)
137 expr = last_expr || expr;
138 else if (last_cmp == 1)
139 expr = last_expr && expr;
140 last_cmp = -1;
141 }
142
143 ap += adv; left -= adv;
144 }
145
146 if (neg)
147 expr = !expr;
148
149 expr = !expr;
150
151 debug (": returns %d\n", expr);
152
153 return expr;
154}
155
156U_BOOT_CMD(
157 test, CONFIG_SYS_MAXARGS, 1, do_test,
158 "minimal test like /bin/sh",
159 "[args..]"
160);
Peter Tyser396fd172009-10-16 17:36:27 -0500161
Kim Phillips088f1b12012-10-29 13:34:31 +0000162static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tyser396fd172009-10-16 17:36:27 -0500163{
164 return 1;
165}
166
167U_BOOT_CMD(
168 false, CONFIG_SYS_MAXARGS, 1, do_false,
169 "do nothing, unsuccessfully",
170 NULL
171);
172
Kim Phillips088f1b12012-10-29 13:34:31 +0000173static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tyser396fd172009-10-16 17:36:27 -0500174{
175 return 0;
176}
177
178U_BOOT_CMD(
179 true, CONFIG_SYS_MAXARGS, 1, do_true,
180 "do nothing, successfully",
181 NULL
182);