cmd_test.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright 2000-2009
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Define _STDBOOL_H here to avoid macro expansion of true and false.
  9. * If the future code requires macro true or false, remove this define
  10. * and undef true and false before U_BOOT_CMD. This define and comment
  11. * shall be removed if change to U_BOOT_CMD is made to take string
  12. * instead of stringifying it.
  13. */
  14. #define _STDBOOL_H
  15. #include <common.h>
  16. #include <command.h>
  17. #define OP_INVALID 0
  18. #define OP_NOT 1
  19. #define OP_OR 2
  20. #define OP_AND 3
  21. #define OP_STR_EMPTY 4
  22. #define OP_STR_NEMPTY 5
  23. #define OP_STR_EQ 6
  24. #define OP_STR_NEQ 7
  25. #define OP_STR_LT 8
  26. #define OP_STR_GT 9
  27. #define OP_INT_EQ 10
  28. #define OP_INT_NEQ 11
  29. #define OP_INT_LT 12
  30. #define OP_INT_LE 13
  31. #define OP_INT_GT 14
  32. #define OP_INT_GE 15
  33. const struct {
  34. int arg;
  35. const char *str;
  36. int op;
  37. int adv;
  38. } op_adv[] = {
  39. {1, "=", OP_STR_EQ, 3},
  40. {1, "!=", OP_STR_NEQ, 3},
  41. {1, "<", OP_STR_LT, 3},
  42. {1, ">", OP_STR_GT, 3},
  43. {1, "-eq", OP_INT_EQ, 3},
  44. {1, "-ne", OP_INT_NEQ, 3},
  45. {1, "-lt", OP_INT_LT, 3},
  46. {1, "-le", OP_INT_LE, 3},
  47. {1, "-gt", OP_INT_GT, 3},
  48. {1, "-ge", OP_INT_GE, 3},
  49. {0, "!", OP_NOT, 1},
  50. {0, "-o", OP_OR, 1},
  51. {0, "-a", OP_AND, 1},
  52. {0, "-z", OP_STR_EMPTY, 2},
  53. {0, "-n", OP_STR_NEMPTY, 2},
  54. };
  55. static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  56. {
  57. char * const *ap;
  58. int i, op, left, adv, expr, last_expr, last_unop, last_binop;
  59. /* args? */
  60. if (argc < 3)
  61. return 1;
  62. #ifdef DEBUG
  63. {
  64. debug("test(%d):", argc);
  65. left = 1;
  66. while (argv[left])
  67. debug(" '%s'", argv[left++]);
  68. }
  69. #endif
  70. left = argc - 1;
  71. ap = argv + 1;
  72. expr = 0;
  73. last_unop = OP_INVALID;
  74. last_binop = OP_INVALID;
  75. last_expr = -1;
  76. while (left > 0) {
  77. for (i = 0; i < ARRAY_SIZE(op_adv); i++) {
  78. if (left <= op_adv[i].arg)
  79. continue;
  80. if (!strcmp(ap[op_adv[i].arg], op_adv[i].str)) {
  81. op = op_adv[i].op;
  82. adv = op_adv[i].adv;
  83. break;
  84. }
  85. }
  86. if (i == ARRAY_SIZE(op_adv)) {
  87. expr = 1;
  88. break;
  89. }
  90. if (left < adv) {
  91. expr = 1;
  92. break;
  93. }
  94. switch (op) {
  95. case OP_STR_EMPTY:
  96. expr = strlen(ap[1]) == 0 ? 1 : 0;
  97. break;
  98. case OP_STR_NEMPTY:
  99. expr = strlen(ap[1]) == 0 ? 0 : 1;
  100. break;
  101. case OP_STR_EQ:
  102. expr = strcmp(ap[0], ap[2]) == 0;
  103. break;
  104. case OP_STR_NEQ:
  105. expr = strcmp(ap[0], ap[2]) != 0;
  106. break;
  107. case OP_STR_LT:
  108. expr = strcmp(ap[0], ap[2]) < 0;
  109. break;
  110. case OP_STR_GT:
  111. expr = strcmp(ap[0], ap[2]) > 0;
  112. break;
  113. case OP_INT_EQ:
  114. expr = simple_strtol(ap[0], NULL, 10) ==
  115. simple_strtol(ap[2], NULL, 10);
  116. break;
  117. case OP_INT_NEQ:
  118. expr = simple_strtol(ap[0], NULL, 10) !=
  119. simple_strtol(ap[2], NULL, 10);
  120. break;
  121. case OP_INT_LT:
  122. expr = simple_strtol(ap[0], NULL, 10) <
  123. simple_strtol(ap[2], NULL, 10);
  124. break;
  125. case OP_INT_LE:
  126. expr = simple_strtol(ap[0], NULL, 10) <=
  127. simple_strtol(ap[2], NULL, 10);
  128. break;
  129. case OP_INT_GT:
  130. expr = simple_strtol(ap[0], NULL, 10) >
  131. simple_strtol(ap[2], NULL, 10);
  132. break;
  133. case OP_INT_GE:
  134. expr = simple_strtol(ap[0], NULL, 10) >=
  135. simple_strtol(ap[2], NULL, 10);
  136. break;
  137. }
  138. switch (op) {
  139. case OP_OR:
  140. last_expr = expr;
  141. last_binop = OP_OR;
  142. break;
  143. case OP_AND:
  144. last_expr = expr;
  145. last_binop = OP_AND;
  146. break;
  147. case OP_NOT:
  148. if (last_unop == OP_NOT)
  149. last_unop = OP_INVALID;
  150. else
  151. last_unop = OP_NOT;
  152. break;
  153. default:
  154. if (last_unop == OP_NOT) {
  155. expr = !expr;
  156. last_unop = OP_INVALID;
  157. }
  158. if (last_binop == OP_OR)
  159. expr = last_expr || expr;
  160. else if (last_binop == OP_AND)
  161. expr = last_expr && expr;
  162. last_binop = OP_INVALID;
  163. break;
  164. }
  165. ap += adv; left -= adv;
  166. }
  167. expr = !expr;
  168. debug (": returns %d\n", expr);
  169. return expr;
  170. }
  171. U_BOOT_CMD(
  172. test, CONFIG_SYS_MAXARGS, 1, do_test,
  173. "minimal test like /bin/sh",
  174. "[args..]"
  175. );
  176. static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  177. {
  178. return 1;
  179. }
  180. U_BOOT_CMD(
  181. false, CONFIG_SYS_MAXARGS, 1, do_false,
  182. "do nothing, unsuccessfully",
  183. NULL
  184. );
  185. static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  186. {
  187. return 0;
  188. }
  189. U_BOOT_CMD(
  190. true, CONFIG_SYS_MAXARGS, 1, do_true,
  191. "do nothing, successfully",
  192. NULL
  193. );