cmd_itest.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. extern int cmd_get_data_size(char* arg, int default_size);
  60. static long evalexp(char *s, int w)
  61. {
  62. long l, *p;
  63. /* if the parameter starts with a * then assume is a pointer to the value we want */
  64. if (s[0] == '*') {
  65. p = (long *)simple_strtoul(&s[1], NULL, 16);
  66. l = *p;
  67. } else {
  68. l = simple_strtoul(s, NULL, 16);
  69. }
  70. return (l & ((1 << (w * 8)) - 1));
  71. }
  72. static char * evalstr(char *s)
  73. {
  74. /* if the parameter starts with a * then assume a string pointer else its a literal */
  75. if (s[0] == '*') {
  76. return (char *)simple_strtoul(&s[1], NULL, 16);
  77. } else {
  78. return s;
  79. }
  80. }
  81. static int stringcomp(char *s, char *t, int op)
  82. {
  83. int n, p;
  84. char *l, *r;
  85. l = evalstr(s);
  86. r = evalstr(t);
  87. /* we'll do a compare based on the length of the shortest string */
  88. n = min(strlen(l), strlen(r));
  89. p = strncmp(l, r, n);
  90. switch (op) {
  91. case EQ: return (p == 0);
  92. case NE: return (p != 0);
  93. case LT: return (p < 0);
  94. case GT: return (p > 0);
  95. case LE: return (p <= 0);
  96. case GE: return (p >= 0);
  97. }
  98. return (0);
  99. }
  100. static int arithcomp (char *s, char *t, int op, int w)
  101. {
  102. long l, r;
  103. l = evalexp (s, w);
  104. r = evalexp (t, w);
  105. switch (op) {
  106. case EQ: return (l == r);
  107. case NE: return (l != r);
  108. case LT: return (l < r);
  109. case GT: return (l > r);
  110. case LE: return (l <= r);
  111. case GE: return (l >= r);
  112. }
  113. return (0);
  114. }
  115. int binary_test (char *op, char *arg1, char *arg2, int w)
  116. {
  117. int len, i;
  118. op_tbl_t *optp;
  119. len = strlen(op);
  120. for (optp = (op_tbl_t *)&op_table, i = 0;
  121. i < op_tbl_size;
  122. optp++, i++) {
  123. if ((strncmp (op, optp->op, len) == 0) && (len == strlen (optp->op))) {
  124. if (w == 0) {
  125. return (stringcomp(arg1, arg2, optp->opcode));
  126. } else {
  127. return (arithcomp (arg1, arg2, optp->opcode, w));
  128. }
  129. }
  130. }
  131. printf("Unknown operator '%s'\n", op);
  132. return 0; /* op code not found */
  133. }
  134. /* command line interface to the shell test */
  135. int do_itest ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] )
  136. {
  137. int value, w;
  138. /* Validate arguments */
  139. if ((argc != 4)){
  140. printf("Usage:\n%s\n", cmdtp->usage);
  141. return 1;
  142. }
  143. /* Check for a data width specification.
  144. * Defaults to long (4) if no specification.
  145. * Uses -2 as 'width' for .s (string) so as not to upset existing code
  146. */
  147. switch (w = cmd_get_data_size(argv[0], 4)) {
  148. case 1:
  149. case 2:
  150. case 4:
  151. value = binary_test (argv[2], argv[1], argv[3], w);
  152. break;
  153. case -2:
  154. value = binary_test (argv[2], argv[1], argv[3], 0);
  155. break;
  156. case -1:
  157. default:
  158. puts("Invalid data width specifier\n");
  159. value = 0;
  160. break;
  161. }
  162. return !value;
  163. }
  164. U_BOOT_CMD(
  165. itest, 4, 0, do_itest,
  166. "itest - return true/false on integer compare\n",
  167. "[.b, .w, .l, .s] [*]value1 <op> [*]value2\n"
  168. );