cmd_test.c 3.5 KB

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