cmd_test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2000-2009
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  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. * Define _STDBOOL_H here to avoid macro expansion of true and false.
  25. * If the future code requires macro true or false, remove this define
  26. * and undef true and false before U_BOOT_CMD. This define and comment
  27. * shall be removed if change to U_BOOT_CMD is made to take string
  28. * instead of stringifying it.
  29. */
  30. #define _STDBOOL_H
  31. #include <common.h>
  32. #include <command.h>
  33. static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  34. {
  35. char * const *ap;
  36. int left, adv, expr, last_expr, neg, last_cmp;
  37. /* args? */
  38. if (argc < 3)
  39. return 1;
  40. #ifdef DEBUG
  41. {
  42. debug("test(%d):", argc);
  43. left = 1;
  44. while (argv[left])
  45. debug(" '%s'", argv[left++]);
  46. }
  47. #endif
  48. last_expr = 0;
  49. left = argc - 1; ap = argv + 1;
  50. if (left > 0 && strcmp(ap[0], "!") == 0) {
  51. neg = 1;
  52. ap++;
  53. left--;
  54. } else
  55. neg = 0;
  56. expr = -1;
  57. last_cmp = -1;
  58. last_expr = -1;
  59. while (left > 0) {
  60. if (strcmp(ap[0], "-o") == 0 || strcmp(ap[0], "-a") == 0)
  61. adv = 1;
  62. else if (strcmp(ap[0], "-z") == 0 || strcmp(ap[0], "-n") == 0)
  63. adv = 2;
  64. else
  65. adv = 3;
  66. if (left < adv) {
  67. expr = 1;
  68. break;
  69. }
  70. if (adv == 1) {
  71. if (strcmp(ap[0], "-o") == 0) {
  72. last_expr = expr;
  73. last_cmp = 0;
  74. } else if (strcmp(ap[0], "-a") == 0) {
  75. last_expr = expr;
  76. last_cmp = 1;
  77. } else {
  78. expr = 1;
  79. break;
  80. }
  81. }
  82. if (adv == 2) {
  83. if (strcmp(ap[0], "-z") == 0)
  84. expr = strlen(ap[1]) == 0 ? 1 : 0;
  85. else if (strcmp(ap[0], "-n") == 0)
  86. expr = strlen(ap[1]) == 0 ? 0 : 1;
  87. else {
  88. expr = 1;
  89. break;
  90. }
  91. if (last_cmp == 0)
  92. expr = last_expr || expr;
  93. else if (last_cmp == 1)
  94. expr = last_expr && expr;
  95. last_cmp = -1;
  96. }
  97. if (adv == 3) {
  98. if (strcmp(ap[1], "=") == 0)
  99. expr = strcmp(ap[0], ap[2]) == 0;
  100. else if (strcmp(ap[1], "!=") == 0)
  101. expr = strcmp(ap[0], ap[2]) != 0;
  102. else if (strcmp(ap[1], ">") == 0)
  103. expr = strcmp(ap[0], ap[2]) > 0;
  104. else if (strcmp(ap[1], "<") == 0)
  105. expr = strcmp(ap[0], ap[2]) < 0;
  106. else if (strcmp(ap[1], "-eq") == 0)
  107. expr = simple_strtol(ap[0], NULL, 10) == simple_strtol(ap[2], NULL, 10);
  108. else if (strcmp(ap[1], "-ne") == 0)
  109. expr = simple_strtol(ap[0], NULL, 10) != simple_strtol(ap[2], NULL, 10);
  110. else if (strcmp(ap[1], "-lt") == 0)
  111. expr = simple_strtol(ap[0], NULL, 10) < simple_strtol(ap[2], NULL, 10);
  112. else if (strcmp(ap[1], "-le") == 0)
  113. expr = simple_strtol(ap[0], NULL, 10) <= simple_strtol(ap[2], NULL, 10);
  114. else if (strcmp(ap[1], "-gt") == 0)
  115. expr = simple_strtol(ap[0], NULL, 10) > simple_strtol(ap[2], NULL, 10);
  116. else if (strcmp(ap[1], "-ge") == 0)
  117. expr = simple_strtol(ap[0], NULL, 10) >= simple_strtol(ap[2], NULL, 10);
  118. else {
  119. expr = 1;
  120. break;
  121. }
  122. if (last_cmp == 0)
  123. expr = last_expr || expr;
  124. else if (last_cmp == 1)
  125. expr = last_expr && expr;
  126. last_cmp = -1;
  127. }
  128. ap += adv; left -= adv;
  129. }
  130. if (neg)
  131. expr = !expr;
  132. expr = !expr;
  133. debug (": returns %d\n", expr);
  134. return expr;
  135. }
  136. U_BOOT_CMD(
  137. test, CONFIG_SYS_MAXARGS, 1, do_test,
  138. "minimal test like /bin/sh",
  139. "[args..]"
  140. );
  141. static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  142. {
  143. return 1;
  144. }
  145. U_BOOT_CMD(
  146. false, CONFIG_SYS_MAXARGS, 1, do_false,
  147. "do nothing, unsuccessfully",
  148. NULL
  149. );
  150. static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  151. {
  152. return 0;
  153. }
  154. U_BOOT_CMD(
  155. true, CONFIG_SYS_MAXARGS, 1, do_true,
  156. "do nothing, successfully",
  157. NULL
  158. );