cmd_itest.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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
  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. /*
  24. * This file provides a shell like 'test' function to return
  25. * true/false from an integer or string compare of two memory
  26. * locations or a location and a scalar/literal.
  27. * A few parts were lifted from bash 'test' command
  28. */
  29. #include <common.h>
  30. #include <config.h>
  31. #include <command.h>
  32. #define EQ 0
  33. #define NE 1
  34. #define LT 2
  35. #define GT 3
  36. #define LE 4
  37. #define GE 5
  38. struct op_tbl_s {
  39. char *op; /* operator string */
  40. int opcode; /* internal representation of opcode */
  41. };
  42. typedef struct op_tbl_s op_tbl_t;
  43. static const op_tbl_t op_table [] = {
  44. { "-lt", LT },
  45. { "<" , LT },
  46. { "-gt", GT },
  47. { ">" , GT },
  48. { "-eq", EQ },
  49. { "==" , EQ },
  50. { "-ne", NE },
  51. { "!=" , NE },
  52. { "<>" , NE },
  53. { "-ge", GE },
  54. { ">=" , GE },
  55. { "-le", LE },
  56. { "<=" , LE },
  57. };
  58. static long evalexp(char *s, int w)
  59. {
  60. long l = 0;
  61. long *p;
  62. /* if the parameter starts with a * then assume is a pointer to the value we want */
  63. if (s[0] == '*') {
  64. p = (long *)simple_strtoul(&s[1], NULL, 16);
  65. switch (w) {
  66. case 1: return((long)(*(unsigned char *)p));
  67. case 2: return((long)(*(unsigned short *)p));
  68. case 4: return(*p);
  69. }
  70. } else {
  71. l = simple_strtoul(s, NULL, 16);
  72. }
  73. return (l & ((1 << (w * 8)) - 1));
  74. }
  75. static char * evalstr(char *s)
  76. {
  77. /* if the parameter starts with a * then assume a string pointer else its a literal */
  78. if (s[0] == '*') {
  79. return (char *)simple_strtoul(&s[1], NULL, 16);
  80. } else {
  81. return s;
  82. }
  83. }
  84. static int stringcomp(char *s, char *t, int op)
  85. {
  86. int p;
  87. char *l, *r;
  88. l = evalstr(s);
  89. r = evalstr(t);
  90. p = strcmp(l, r);
  91. switch (op) {
  92. case EQ: return (p == 0);
  93. case NE: return (p != 0);
  94. case LT: return (p < 0);
  95. case GT: return (p > 0);
  96. case LE: return (p <= 0);
  97. case GE: return (p >= 0);
  98. }
  99. return (0);
  100. }
  101. static int arithcomp (char *s, char *t, int op, int w)
  102. {
  103. long l, r;
  104. l = evalexp (s, w);
  105. r = evalexp (t, w);
  106. switch (op) {
  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);
  113. }
  114. return (0);
  115. }
  116. static int binary_test(char *op, char *arg1, char *arg2, int w)
  117. {
  118. int len, i;
  119. const op_tbl_t *optp;
  120. len = strlen(op);
  121. for (optp = (op_tbl_t *)&op_table, i = 0;
  122. i < ARRAY_SIZE(op_table);
  123. optp++, i++) {
  124. if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) {
  125. if (w == 0) {
  126. return (stringcomp(arg1, arg2, optp->opcode));
  127. } else {
  128. return (arithcomp (arg1, arg2, optp->opcode, w));
  129. }
  130. }
  131. }
  132. printf("Unknown operator '%s'\n", op);
  133. return 0; /* op code not found */
  134. }
  135. /* command line interface to the shell test */
  136. static int do_itest(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  137. {
  138. int value, w;
  139. /* Validate arguments */
  140. if ((argc != 4))
  141. return CMD_RET_USAGE;
  142. /* Check for a data width specification.
  143. * Defaults to long (4) if no specification.
  144. * Uses -2 as 'width' for .s (string) so as not to upset existing code
  145. */
  146. switch (w = cmd_get_data_size(argv[0], 4)) {
  147. case 1:
  148. case 2:
  149. case 4:
  150. value = binary_test (argv[2], argv[1], argv[3], w);
  151. break;
  152. case -2:
  153. value = binary_test (argv[2], argv[1], argv[3], 0);
  154. break;
  155. case -1:
  156. default:
  157. puts("Invalid data width specifier\n");
  158. value = 0;
  159. break;
  160. }
  161. return !value;
  162. }
  163. U_BOOT_CMD(
  164. itest, 4, 0, do_itest,
  165. "return true/false on integer compare",
  166. "[.b, .w, .l, .s] [*]value1 <op> [*]value2"
  167. );