cmd_itest.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. 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. #define op_tbl_size (sizeof(op_table)/sizeof(op_table[0]))
  59. static long evalexp(char *s, int w)
  60. {
  61. long l, *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. l = *p;
  66. } else {
  67. l = simple_strtoul(s, NULL, 16);
  68. }
  69. return (l & ((1 << (w * 8)) - 1));
  70. }
  71. static char * evalstr(char *s)
  72. {
  73. /* if the parameter starts with a * then assume a string pointer else its a literal */
  74. if (s[0] == '*') {
  75. return (char *)simple_strtoul(&s[1], NULL, 16);
  76. } else {
  77. return s;
  78. }
  79. }
  80. static int stringcomp(char *s, char *t, int op)
  81. {
  82. int n, p;
  83. char *l, *r;
  84. l = evalstr(s);
  85. r = evalstr(t);
  86. /* we'll do a compare based on the length of the shortest string */
  87. n = min(strlen(l), strlen(r));
  88. p = strncmp(l, r, n);
  89. switch (op) {
  90. case EQ: return (p == 0);
  91. case NE: return (p != 0);
  92. case LT: return (p < 0);
  93. case GT: return (p > 0);
  94. case LE: return (p <= 0);
  95. case GE: return (p >= 0);
  96. }
  97. return (0);
  98. }
  99. static int arithcomp (char *s, char *t, int op, int w)
  100. {
  101. long l, r;
  102. l = evalexp (s, w);
  103. r = evalexp (t, w);
  104. switch (op) {
  105. case EQ: return (l == r);
  106. case NE: return (l != r);
  107. case LT: return (l < r);
  108. case GT: return (l > r);
  109. case LE: return (l <= r);
  110. case GE: return (l >= r);
  111. }
  112. return (0);
  113. }
  114. int binary_test (char *op, char *arg1, char *arg2, int w)
  115. {
  116. int len, i;
  117. op_tbl_t *optp;
  118. len = strlen(op);
  119. for (optp = (op_tbl_t *)&op_table, i = 0;
  120. i < op_tbl_size;
  121. optp++, i++) {
  122. if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) {
  123. if (w == 0) {
  124. return (stringcomp(arg1, arg2, optp->opcode));
  125. } else {
  126. return (arithcomp (arg1, arg2, optp->opcode, w));
  127. }
  128. }
  129. }
  130. printf("Unknown operator '%s'\n", op);
  131. return 0; /* op code not found */
  132. }
  133. /* command line interface to the shell test */
  134. int do_itest ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] )
  135. {
  136. int value, w;
  137. /* Validate arguments */
  138. if ((argc != 4)){
  139. cmd_usage(cmdtp);
  140. return 1;
  141. }
  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. );