Peter Tyser | 6b8f5ad | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2000-2009 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Peter Tyser | 6b8f5ad | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 6 | */ |
| 7 | |
York Sun | a614270 | 2013-04-04 11:52:53 +0000 | [diff] [blame] | 8 | /* |
| 9 | * Define _STDBOOL_H here to avoid macro expansion of true and false. |
| 10 | * If the future code requires macro true or false, remove this define |
| 11 | * and undef true and false before U_BOOT_CMD. This define and comment |
| 12 | * shall be removed if change to U_BOOT_CMD is made to take string |
| 13 | * instead of stringifying it. |
| 14 | */ |
| 15 | #define _STDBOOL_H |
| 16 | |
Peter Tyser | 6b8f5ad | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 17 | #include <common.h> |
| 18 | #include <command.h> |
| 19 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 20 | static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Peter Tyser | 6b8f5ad | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 21 | { |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 22 | char * const *ap; |
Peter Tyser | 6b8f5ad | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 23 | int left, adv, expr, last_expr, neg, last_cmp; |
| 24 | |
| 25 | /* args? */ |
| 26 | if (argc < 3) |
| 27 | return 1; |
| 28 | |
Joe Hershberger | b0fe6ab | 2012-08-17 10:56:12 +0000 | [diff] [blame] | 29 | #ifdef DEBUG |
Peter Tyser | 6b8f5ad | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 30 | { |
Joe Hershberger | b0fe6ab | 2012-08-17 10:56:12 +0000 | [diff] [blame] | 31 | debug("test(%d):", argc); |
Peter Tyser | 6b8f5ad | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 32 | left = 1; |
| 33 | while (argv[left]) |
Joe Hershberger | b0fe6ab | 2012-08-17 10:56:12 +0000 | [diff] [blame] | 34 | debug(" '%s'", argv[left++]); |
Peter Tyser | 6b8f5ad | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 35 | } |
| 36 | #endif |
| 37 | |
| 38 | last_expr = 0; |
| 39 | left = argc - 1; ap = argv + 1; |
| 40 | if (left > 0 && strcmp(ap[0], "!") == 0) { |
| 41 | neg = 1; |
| 42 | ap++; |
| 43 | left--; |
| 44 | } else |
| 45 | neg = 0; |
| 46 | |
| 47 | expr = -1; |
| 48 | last_cmp = -1; |
| 49 | last_expr = -1; |
| 50 | while (left > 0) { |
| 51 | |
| 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 | |
| 59 | if (left < adv) { |
| 60 | expr = 1; |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | if (adv == 1) { |
| 65 | if (strcmp(ap[0], "-o") == 0) { |
| 66 | last_expr = expr; |
| 67 | last_cmp = 0; |
| 68 | } else if (strcmp(ap[0], "-a") == 0) { |
| 69 | last_expr = expr; |
| 70 | last_cmp = 1; |
| 71 | } else { |
| 72 | expr = 1; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (adv == 2) { |
| 78 | if (strcmp(ap[0], "-z") == 0) |
| 79 | expr = strlen(ap[1]) == 0 ? 1 : 0; |
| 80 | else if (strcmp(ap[0], "-n") == 0) |
| 81 | expr = strlen(ap[1]) == 0 ? 0 : 1; |
| 82 | else { |
| 83 | expr = 1; |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | if (last_cmp == 0) |
| 88 | expr = last_expr || expr; |
| 89 | else if (last_cmp == 1) |
| 90 | expr = last_expr && expr; |
| 91 | last_cmp = -1; |
| 92 | } |
| 93 | |
| 94 | if (adv == 3) { |
| 95 | if (strcmp(ap[1], "=") == 0) |
| 96 | expr = strcmp(ap[0], ap[2]) == 0; |
| 97 | else if (strcmp(ap[1], "!=") == 0) |
| 98 | expr = strcmp(ap[0], ap[2]) != 0; |
| 99 | else if (strcmp(ap[1], ">") == 0) |
| 100 | expr = strcmp(ap[0], ap[2]) > 0; |
| 101 | else if (strcmp(ap[1], "<") == 0) |
| 102 | expr = strcmp(ap[0], ap[2]) < 0; |
| 103 | else if (strcmp(ap[1], "-eq") == 0) |
| 104 | expr = simple_strtol(ap[0], NULL, 10) == simple_strtol(ap[2], NULL, 10); |
| 105 | else if (strcmp(ap[1], "-ne") == 0) |
| 106 | expr = simple_strtol(ap[0], NULL, 10) != simple_strtol(ap[2], NULL, 10); |
| 107 | else if (strcmp(ap[1], "-lt") == 0) |
| 108 | expr = simple_strtol(ap[0], NULL, 10) < simple_strtol(ap[2], NULL, 10); |
| 109 | else if (strcmp(ap[1], "-le") == 0) |
| 110 | expr = simple_strtol(ap[0], NULL, 10) <= simple_strtol(ap[2], NULL, 10); |
| 111 | else if (strcmp(ap[1], "-gt") == 0) |
| 112 | expr = simple_strtol(ap[0], NULL, 10) > simple_strtol(ap[2], NULL, 10); |
| 113 | else if (strcmp(ap[1], "-ge") == 0) |
| 114 | expr = simple_strtol(ap[0], NULL, 10) >= simple_strtol(ap[2], NULL, 10); |
| 115 | else { |
| 116 | expr = 1; |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | if (last_cmp == 0) |
| 121 | expr = last_expr || expr; |
| 122 | else if (last_cmp == 1) |
| 123 | expr = last_expr && expr; |
| 124 | last_cmp = -1; |
| 125 | } |
| 126 | |
| 127 | ap += adv; left -= adv; |
| 128 | } |
| 129 | |
| 130 | if (neg) |
| 131 | expr = !expr; |
| 132 | |
| 133 | expr = !expr; |
| 134 | |
| 135 | debug (": returns %d\n", expr); |
| 136 | |
| 137 | return expr; |
| 138 | } |
| 139 | |
| 140 | U_BOOT_CMD( |
| 141 | test, CONFIG_SYS_MAXARGS, 1, do_test, |
| 142 | "minimal test like /bin/sh", |
| 143 | "[args..]" |
| 144 | ); |
Peter Tyser | 396fd17 | 2009-10-16 17:36:27 -0500 | [diff] [blame] | 145 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 146 | static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Peter Tyser | 396fd17 | 2009-10-16 17:36:27 -0500 | [diff] [blame] | 147 | { |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | U_BOOT_CMD( |
| 152 | false, CONFIG_SYS_MAXARGS, 1, do_false, |
| 153 | "do nothing, unsuccessfully", |
| 154 | NULL |
| 155 | ); |
| 156 | |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 157 | static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Peter Tyser | 396fd17 | 2009-10-16 17:36:27 -0500 | [diff] [blame] | 158 | { |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | U_BOOT_CMD( |
| 163 | true, CONFIG_SYS_MAXARGS, 1, do_true, |
| 164 | "do nothing, successfully", |
| 165 | NULL |
| 166 | ); |