3 * Tait Electronics Limited, Christchurch, New Zealand
5 * SPDX-License-Identifier: GPL-2.0+
9 * This file provides a shell like 'test' function to return
10 * true/false from an integer or string compare of two memory
11 * locations or a location and a scalar/literal.
12 * A few parts were lifted from bash 'test' command
27 char *op; /* operator string */
28 int opcode; /* internal representation of opcode */
31 typedef struct op_tbl_s op_tbl_t;
33 static const op_tbl_t op_table [] = {
49 static long evalexp(char *s, int w)
54 /* if the parameter starts with a * then assume is a pointer to the value we want */
56 p = (long *)simple_strtoul(&s[1], NULL, 16);
58 case 1: return((long)(*(unsigned char *)p));
59 case 2: return((long)(*(unsigned short *)p));
63 l = simple_strtoul(s, NULL, 16);
66 return (l & ((1 << (w * 8)) - 1));
69 static char * evalstr(char *s)
71 /* if the parameter starts with a * then assume a string pointer else its a literal */
73 return (char *)simple_strtoul(&s[1], NULL, 16);
79 static int stringcomp(char *s, char *t, int op)
89 case EQ: return (p == 0);
90 case NE: return (p != 0);
91 case LT: return (p < 0);
92 case GT: return (p > 0);
93 case LE: return (p <= 0);
94 case GE: return (p >= 0);
99 static int arithcomp (char *s, char *t, int op, int w)
107 case EQ: return (l == r);
108 case NE: return (l != r);
109 case LT: return (l < r);
110 case GT: return (l > r);
111 case LE: return (l <= r);
112 case GE: return (l >= r);
117 static int binary_test(char *op, char *arg1, char *arg2, int w)
120 const op_tbl_t *optp;
124 for (optp = (op_tbl_t *)&op_table, i = 0;
125 i < ARRAY_SIZE(op_table);
128 if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) {
130 return (stringcomp(arg1, arg2, optp->opcode));
132 return (arithcomp (arg1, arg2, optp->opcode, w));
137 printf("Unknown operator '%s'\n", op);
138 return 0; /* op code not found */
141 /* command line interface to the shell test */
142 static int do_itest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
146 /* Validate arguments */
148 return CMD_RET_USAGE;
150 /* Check for a data width specification.
151 * Defaults to long (4) if no specification.
152 * Uses -2 as 'width' for .s (string) so as not to upset existing code
154 switch (w = cmd_get_data_size(argv[0], 4)) {
158 value = binary_test (argv[2], argv[1], argv[3], w);
161 value = binary_test (argv[2], argv[1], argv[3], 0);
165 puts("Invalid data width specifier\n");
174 itest, 4, 0, do_itest,
175 "return true/false on integer compare",
176 "[.b, .w, .l, .s] [*]value1 <op> [*]value2"