3 * Tait Electronics Limited, Christchurch, New Zealand
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
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
43 char *op; /* operator string */
44 int opcode; /* internal representation of opcode */
47 typedef struct op_tbl_s op_tbl_t;
49 op_tbl_t op_table [] = {
65 #define op_tbl_size (sizeof(op_table)/sizeof(op_table[0]))
67 static long evalexp(char *s, int w)
72 /* if the parameter starts with a * then assume is a pointer to the value we want */
74 p = (long *)simple_strtoul(&s[1], NULL, 16);
76 case 1: return((long)(*(unsigned char *)p));
77 case 2: return((long)(*(unsigned short *)p));
81 l = simple_strtoul(s, NULL, 16);
84 return (l & ((1 << (w * 8)) - 1));
87 static char * evalstr(char *s)
89 /* if the parameter starts with a * then assume a string pointer else its a literal */
91 return (char *)simple_strtoul(&s[1], NULL, 16);
97 static int stringcomp(char *s, char *t, int op)
105 /* we'll do a compare based on the length of the shortest string */
106 n = min(strlen(l), strlen(r));
108 p = strncmp(l, r, n);
110 case EQ: return (p == 0);
111 case NE: return (p != 0);
112 case LT: return (p < 0);
113 case GT: return (p > 0);
114 case LE: return (p <= 0);
115 case GE: return (p >= 0);
120 static int arithcomp (char *s, char *t, int op, int w)
128 case EQ: return (l == r);
129 case NE: return (l != r);
130 case LT: return (l < r);
131 case GT: return (l > r);
132 case LE: return (l <= r);
133 case GE: return (l >= r);
138 int binary_test (char *op, char *arg1, char *arg2, int w)
145 for (optp = (op_tbl_t *)&op_table, i = 0;
149 if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) {
151 return (stringcomp(arg1, arg2, optp->opcode));
153 return (arithcomp (arg1, arg2, optp->opcode, w));
158 printf("Unknown operator '%s'\n", op);
159 return 0; /* op code not found */
162 /* command line interface to the shell test */
163 int do_itest ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] )
167 /* Validate arguments */
173 /* Check for a data width specification.
174 * Defaults to long (4) if no specification.
175 * Uses -2 as 'width' for .s (string) so as not to upset existing code
177 switch (w = cmd_get_data_size(argv[0], 4)) {
181 value = binary_test (argv[2], argv[1], argv[3], w);
184 value = binary_test (argv[2], argv[1], argv[3], 0);
188 puts("Invalid data width specifier\n");
197 itest, 4, 0, do_itest,
198 "return true/false on integer compare",
199 "[.b, .w, .l, .s] [*]value1 <op> [*]value2"