]>
Commit | Line | Data |
---|---|---|
2d1a537d WD |
1 | /* |
2 | * (C) Copyright 2003 | |
3 | * Tait Electronics Limited, Christchurch, New Zealand | |
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 | |
cd0a9de6 | 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
2d1a537d WD |
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 | ||
24 | /* | |
25 | * This file provides a shell like 'test' function to return | |
26 | * true/false from an integer or string compare of two memory | |
27 | * locations or a location and a scalar/literal. | |
28 | * A few parts were lifted from bash 'test' command | |
29 | */ | |
30 | ||
31 | #include <common.h> | |
32 | #include <config.h> | |
33 | #include <command.h> | |
34 | ||
35 | #define EQ 0 | |
36 | #define NE 1 | |
37 | #define LT 2 | |
38 | #define GT 3 | |
39 | #define LE 4 | |
40 | #define GE 5 | |
41 | ||
42 | struct op_tbl_s { | |
43 | char *op; /* operator string */ | |
44 | int opcode; /* internal representation of opcode */ | |
45 | }; | |
46 | ||
47 | typedef struct op_tbl_s op_tbl_t; | |
48 | ||
49 | op_tbl_t op_table [] = { | |
50 | { "-lt", LT }, | |
51 | { "<" , LT }, | |
52 | { "-gt", GT }, | |
53 | { ">" , GT }, | |
54 | { "-eq", EQ }, | |
55 | { "==" , EQ }, | |
56 | { "-ne", NE }, | |
57 | { "!=" , NE }, | |
58 | { "<>" , NE }, | |
59 | { "-ge", GE }, | |
60 | { ">=" , GE }, | |
61 | { "-le", LE }, | |
62 | { "<=" , LE }, | |
63 | }; | |
64 | ||
65 | #define op_tbl_size (sizeof(op_table)/sizeof(op_table[0])) | |
66 | ||
67 | extern int cmd_get_data_size(char* arg, int default_size); | |
68 | ||
69 | static long evalexp(char *s, int w) | |
70 | { | |
71 | long l, *p; | |
72 | ||
73 | /* if the parameter starts with a * then assume is a pointer to the value we want */ | |
74 | if (s[0] == '*') { | |
75 | p = (long *)simple_strtoul(&s[1], NULL, 16); | |
76 | l = *p; | |
77 | } else { | |
78 | l = simple_strtoul(s, NULL, 16); | |
79 | } | |
80 | ||
81 | return (l & ((1 << (w * 8)) - 1)); | |
82 | } | |
83 | ||
84 | static char * evalstr(char *s) | |
85 | { | |
86 | /* if the parameter starts with a * then assume a string pointer else its a literal */ | |
87 | if (s[0] == '*') { | |
88 | return (char *)simple_strtoul(&s[1], NULL, 16); | |
89 | } else { | |
90 | return s; | |
91 | } | |
92 | } | |
93 | ||
94 | static int stringcomp(char *s, char *t, int op) | |
95 | { | |
96 | int n, p; | |
97 | char *l, *r; | |
98 | ||
99 | l = evalstr(s); | |
100 | r = evalstr(t); | |
101 | ||
102 | /* we'll do a compare based on the length of the shortest string */ | |
103 | n = min(strlen(l), strlen(r)); | |
104 | ||
105 | p = strncmp(l, r, n); | |
106 | switch (op) { | |
107 | case EQ: return (p == 0); | |
108 | case NE: return (p != 0); | |
109 | case LT: return (p < 0); | |
110 | case GT: return (p > 0); | |
111 | case LE: return (p <= 0); | |
112 | case GE: return (p >= 0); | |
113 | } | |
114 | return (0); | |
115 | } | |
116 | ||
117 | static int arithcomp (char *s, char *t, int op, int w) | |
118 | { | |
119 | long l, r; | |
120 | ||
121 | l = evalexp (s, w); | |
122 | r = evalexp (t, w); | |
123 | ||
124 | switch (op) { | |
125 | case EQ: return (l == r); | |
126 | case NE: return (l != r); | |
127 | case LT: return (l < r); | |
128 | case GT: return (l > r); | |
129 | case LE: return (l <= r); | |
130 | case GE: return (l >= r); | |
131 | } | |
132 | return (0); | |
133 | } | |
134 | ||
135 | int binary_test (char *op, char *arg1, char *arg2, int w) | |
136 | { | |
137 | int len, i; | |
138 | op_tbl_t *optp; | |
139 | ||
140 | len = strlen(op); | |
141 | ||
142 | for (optp = (op_tbl_t *)&op_table, i = 0; | |
143 | i < op_tbl_size; | |
144 | optp++, i++) { | |
145 | ||
146 | if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) { | |
147 | if (w == 0) { | |
148 | return (stringcomp(arg1, arg2, optp->opcode)); | |
149 | } else { | |
150 | return (arithcomp (arg1, arg2, optp->opcode, w)); | |
151 | } | |
152 | } | |
153 | } | |
154 | ||
155 | printf("Unknown operator '%s'\n", op); | |
156 | return 0; /* op code not found */ | |
157 | } | |
158 | ||
159 | /* command line interface to the shell test */ | |
160 | int do_itest ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] ) | |
161 | { | |
cd0a9de6 | 162 | int value, w; |
2d1a537d | 163 | |
cd0a9de6 WD |
164 | /* Validate arguments */ |
165 | if ((argc != 4)){ | |
2d1a537d | 166 | printf("Usage:\n%s\n", cmdtp->usage); |
cd0a9de6 WD |
167 | return 1; |
168 | } | |
2d1a537d WD |
169 | |
170 | /* Check for a data width specification. | |
171 | * Defaults to long (4) if no specification. | |
172 | * Uses -2 as 'width' for .s (string) so as not to upset existing code | |
173 | */ | |
174 | switch (w = cmd_get_data_size(argv[0], 4)) { | |
175 | case 1: | |
176 | case 2: | |
177 | case 4: | |
178 | value = binary_test (argv[2], argv[1], argv[3], w); | |
179 | break; | |
180 | case -2: | |
181 | value = binary_test (argv[2], argv[1], argv[3], 0); | |
182 | break; | |
183 | case -1: | |
184 | default: | |
185 | puts("Invalid data width specifier\n"); | |
186 | value = 0; | |
187 | break; | |
188 | } | |
189 | ||
190 | return !value; | |
191 | } | |
192 | ||
193 | U_BOOT_CMD( | |
194 | itest, 4, 0, do_itest, | |
aa5590b6 | 195 | "itest\t- return true/false on integer compare\n", |
2d1a537d WD |
196 | "[.b, .w, .l, .s] [*]value1 <op> [*]value2\n" |
197 | ); |