cmd_itest.c 4.3 KB

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