cmd_test.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include <common.h>
  24. #include <command.h>
  25. int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  26. {
  27. char **ap;
  28. int left, adv, expr, last_expr, neg, last_cmp;
  29. /* args? */
  30. if (argc < 3)
  31. return 1;
  32. #if 0
  33. {
  34. printf("test:");
  35. left = 1;
  36. while (argv[left])
  37. printf(" %s", argv[left++]);
  38. }
  39. #endif
  40. last_expr = 0;
  41. left = argc - 1; ap = argv + 1;
  42. if (left > 0 && strcmp(ap[0], "!") == 0) {
  43. neg = 1;
  44. ap++;
  45. left--;
  46. } else
  47. neg = 0;
  48. expr = -1;
  49. last_cmp = -1;
  50. last_expr = -1;
  51. while (left > 0) {
  52. if (strcmp(ap[0], "-o") == 0 || strcmp(ap[0], "-a") == 0)
  53. adv = 1;
  54. else if (strcmp(ap[0], "-z") == 0 || strcmp(ap[0], "-n") == 0)
  55. adv = 2;
  56. else
  57. adv = 3;
  58. if (left < adv) {
  59. expr = 1;
  60. break;
  61. }
  62. if (adv == 1) {
  63. if (strcmp(ap[0], "-o") == 0) {
  64. last_expr = expr;
  65. last_cmp = 0;
  66. } else if (strcmp(ap[0], "-a") == 0) {
  67. last_expr = expr;
  68. last_cmp = 1;
  69. } else {
  70. expr = 1;
  71. break;
  72. }
  73. }
  74. if (adv == 2) {
  75. if (strcmp(ap[0], "-z") == 0)
  76. expr = strlen(ap[1]) == 0 ? 1 : 0;
  77. else if (strcmp(ap[0], "-n") == 0)
  78. expr = strlen(ap[1]) == 0 ? 0 : 1;
  79. else {
  80. expr = 1;
  81. break;
  82. }
  83. if (last_cmp == 0)
  84. expr = last_expr || expr;
  85. else if (last_cmp == 1)
  86. expr = last_expr && expr;
  87. last_cmp = -1;
  88. }
  89. if (adv == 3) {
  90. if (strcmp(ap[1], "=") == 0)
  91. expr = strcmp(ap[0], ap[2]) == 0;
  92. else if (strcmp(ap[1], "!=") == 0)
  93. expr = strcmp(ap[0], ap[2]) != 0;
  94. else if (strcmp(ap[1], ">") == 0)
  95. expr = strcmp(ap[0], ap[2]) > 0;
  96. else if (strcmp(ap[1], "<") == 0)
  97. expr = strcmp(ap[0], ap[2]) < 0;
  98. else if (strcmp(ap[1], "-eq") == 0)
  99. expr = simple_strtol(ap[0], NULL, 10) == simple_strtol(ap[2], NULL, 10);
  100. else if (strcmp(ap[1], "-ne") == 0)
  101. expr = simple_strtol(ap[0], NULL, 10) != simple_strtol(ap[2], NULL, 10);
  102. else if (strcmp(ap[1], "-lt") == 0)
  103. expr = simple_strtol(ap[0], NULL, 10) < simple_strtol(ap[2], NULL, 10);
  104. else if (strcmp(ap[1], "-le") == 0)
  105. expr = simple_strtol(ap[0], NULL, 10) <= simple_strtol(ap[2], NULL, 10);
  106. else if (strcmp(ap[1], "-gt") == 0)
  107. expr = simple_strtol(ap[0], NULL, 10) > simple_strtol(ap[2], NULL, 10);
  108. else if (strcmp(ap[1], "-ge") == 0)
  109. expr = simple_strtol(ap[0], NULL, 10) >= simple_strtol(ap[2], NULL, 10);
  110. else {
  111. expr = 1;
  112. break;
  113. }
  114. if (last_cmp == 0)
  115. expr = last_expr || expr;
  116. else if (last_cmp == 1)
  117. expr = last_expr && expr;
  118. last_cmp = -1;
  119. }
  120. ap += adv; left -= adv;
  121. }
  122. if (neg)
  123. expr = !expr;
  124. expr = !expr;
  125. debug (": returns %d\n", expr);
  126. return expr;
  127. }
  128. U_BOOT_CMD(
  129. test, CONFIG_SYS_MAXARGS, 1, do_test,
  130. "minimal test like /bin/sh",
  131. "[args..]"
  132. );
  133. int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  134. {
  135. return 1;
  136. }
  137. U_BOOT_CMD(
  138. false, CONFIG_SYS_MAXARGS, 1, do_false,
  139. "do nothing, unsuccessfully",
  140. NULL
  141. );
  142. int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  143. {
  144. return 0;
  145. }
  146. U_BOOT_CMD(
  147. true, CONFIG_SYS_MAXARGS, 1, do_true,
  148. "do nothing, successfully",
  149. NULL
  150. );