cmd_itest.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #if (CONFIG_COMMANDS & CFG_CMD_ITEST) || defined(CONFIG_CMD_ITEST)
  33. #define EQ 0
  34. #define NE 1
  35. #define LT 2
  36. #define GT 3
  37. #define LE 4
  38. #define GE 5
  39. struct op_tbl_s {
  40. char *op; /* operator string */
  41. int opcode; /* internal representation of opcode */
  42. };
  43. typedef struct op_tbl_s op_tbl_t;
  44. op_tbl_t op_table [] = {
  45. { "-lt", LT },
  46. { "<" , LT },
  47. { "-gt", GT },
  48. { ">" , GT },
  49. { "-eq", EQ },
  50. { "==" , EQ },
  51. { "-ne", NE },
  52. { "!=" , NE },
  53. { "<>" , NE },
  54. { "-ge", GE },
  55. { ">=" , GE },
  56. { "-le", LE },
  57. { "<=" , LE },
  58. };
  59. #define op_tbl_size (sizeof(op_table)/sizeof(op_table[0]))
  60. extern int cmd_get_data_size(char* arg, int default_size);
  61. static long evalexp(char *s, int w)
  62. {
  63. long l, *p;
  64. /* if the parameter starts with a * then assume is a pointer to the value we want */
  65. if (s[0] == '*') {
  66. p = (long *)simple_strtoul(&s[1], NULL, 16);
  67. l = *p;
  68. } else {
  69. l = simple_strtoul(s, NULL, 16);
  70. }
  71. return (l & ((1 << (w * 8)) - 1));
  72. }
  73. static char * evalstr(char *s)
  74. {
  75. /* if the parameter starts with a * then assume a string pointer else its a literal */
  76. if (s[0] == '*') {
  77. return (char *)simple_strtoul(&s[1], NULL, 16);
  78. } else {
  79. return s;
  80. }
  81. }
  82. static int stringcomp(char *s, char *t, int op)
  83. {
  84. int n, p;
  85. char *l, *r;
  86. l = evalstr(s);
  87. r = evalstr(t);
  88. /* we'll do a compare based on the length of the shortest string */
  89. n = min(strlen(l), strlen(r));
  90. p = strncmp(l, r, n);
  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. int binary_test (char *op, char *arg1, char *arg2, int w)
  117. {
  118. int len, i;
  119. op_tbl_t *optp;
  120. len = strlen(op);
  121. for (optp = (op_tbl_t *)&op_table, i = 0;
  122. i < op_tbl_size;
  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. int do_itest ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] )
  137. {
  138. int value, w;
  139. /* Validate arguments */
  140. if ((argc != 4)){
  141. printf("Usage:\n%s\n", cmdtp->usage);
  142. return 1;
  143. }
  144. /* Check for a data width specification.
  145. * Defaults to long (4) if no specification.
  146. * Uses -2 as 'width' for .s (string) so as not to upset existing code
  147. */
  148. switch (w = cmd_get_data_size(argv[0], 4)) {
  149. case 1:
  150. case 2:
  151. case 4:
  152. value = binary_test (argv[2], argv[1], argv[3], w);
  153. break;
  154. case -2:
  155. value = binary_test (argv[2], argv[1], argv[3], 0);
  156. break;
  157. case -1:
  158. default:
  159. puts("Invalid data width specifier\n");
  160. value = 0;
  161. break;
  162. }
  163. return !value;
  164. }
  165. U_BOOT_CMD(
  166. itest, 4, 0, do_itest,
  167. "itest\t- return true/false on integer compare\n",
  168. "[.b, .w, .l, .s] [*]value1 <op> [*]value2\n"
  169. );
  170. #endif /* CONFIG_COMMANDS & CFG_CMD_ITEST */