Peter Tyser | eabe6d9 | 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 | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 6 | */ |
| 7 | |
York Sun | c6b3626 | 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 | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 17 | #include <common.h> |
| 18 | #include <command.h> |
| 19 | |
Stephen Warren | 28cca55 | 2014-02-03 13:21:02 -0700 | [diff] [blame] | 20 | #define OP_INVALID 0 |
Stephen Warren | 08b763b | 2014-02-03 13:21:04 -0700 | [diff] [blame^] | 21 | #define OP_NOT 1 |
Stephen Warren | 28cca55 | 2014-02-03 13:21:02 -0700 | [diff] [blame] | 22 | #define OP_OR 2 |
| 23 | #define OP_AND 3 |
| 24 | #define OP_STR_EMPTY 4 |
| 25 | #define OP_STR_NEMPTY 5 |
| 26 | #define OP_STR_EQ 6 |
| 27 | #define OP_STR_NEQ 7 |
| 28 | #define OP_STR_LT 8 |
| 29 | #define OP_STR_GT 9 |
| 30 | #define OP_INT_EQ 10 |
| 31 | #define OP_INT_NEQ 11 |
| 32 | #define OP_INT_LT 12 |
| 33 | #define OP_INT_LE 13 |
| 34 | #define OP_INT_GT 14 |
| 35 | #define OP_INT_GE 15 |
| 36 | |
| 37 | const struct { |
| 38 | int arg; |
| 39 | const char *str; |
| 40 | int op; |
| 41 | int adv; |
| 42 | } op_adv[] = { |
Stephen Warren | 28cca55 | 2014-02-03 13:21:02 -0700 | [diff] [blame] | 43 | {1, "=", OP_STR_EQ, 3}, |
| 44 | {1, "!=", OP_STR_NEQ, 3}, |
| 45 | {1, "<", OP_STR_LT, 3}, |
| 46 | {1, ">", OP_STR_GT, 3}, |
| 47 | {1, "-eq", OP_INT_EQ, 3}, |
| 48 | {1, "-ne", OP_INT_NEQ, 3}, |
| 49 | {1, "-lt", OP_INT_LT, 3}, |
| 50 | {1, "-le", OP_INT_LE, 3}, |
| 51 | {1, "-gt", OP_INT_GT, 3}, |
| 52 | {1, "-ge", OP_INT_GE, 3}, |
Stephen Warren | 08b763b | 2014-02-03 13:21:04 -0700 | [diff] [blame^] | 53 | {0, "!", OP_NOT, 1}, |
Stephen Warren | 4c1b281 | 2014-02-03 13:21:03 -0700 | [diff] [blame] | 54 | {0, "-o", OP_OR, 1}, |
| 55 | {0, "-a", OP_AND, 1}, |
| 56 | {0, "-z", OP_STR_EMPTY, 2}, |
| 57 | {0, "-n", OP_STR_NEMPTY, 2}, |
Stephen Warren | 28cca55 | 2014-02-03 13:21:02 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 60 | static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 61 | { |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 62 | char * const *ap; |
Stephen Warren | 08b763b | 2014-02-03 13:21:04 -0700 | [diff] [blame^] | 63 | int i, op, left, adv, expr, last_expr, last_unop, last_binop; |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 64 | |
| 65 | /* args? */ |
| 66 | if (argc < 3) |
| 67 | return 1; |
| 68 | |
Joe Hershberger | eeff228 | 2012-08-17 10:56:12 +0000 | [diff] [blame] | 69 | #ifdef DEBUG |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 70 | { |
Joe Hershberger | eeff228 | 2012-08-17 10:56:12 +0000 | [diff] [blame] | 71 | debug("test(%d):", argc); |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 72 | left = 1; |
| 73 | while (argv[left]) |
Joe Hershberger | eeff228 | 2012-08-17 10:56:12 +0000 | [diff] [blame] | 74 | debug(" '%s'", argv[left++]); |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 75 | } |
| 76 | #endif |
| 77 | |
Stephen Warren | 08b763b | 2014-02-03 13:21:04 -0700 | [diff] [blame^] | 78 | left = argc - 1; |
| 79 | ap = argv + 1; |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 80 | expr = -1; |
Stephen Warren | 08b763b | 2014-02-03 13:21:04 -0700 | [diff] [blame^] | 81 | last_unop = OP_INVALID; |
| 82 | last_binop = OP_INVALID; |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 83 | last_expr = -1; |
| 84 | while (left > 0) { |
Stephen Warren | 28cca55 | 2014-02-03 13:21:02 -0700 | [diff] [blame] | 85 | for (i = 0; i < ARRAY_SIZE(op_adv); i++) { |
| 86 | if (left <= op_adv[i].arg) |
| 87 | continue; |
| 88 | if (!strcmp(ap[op_adv[i].arg], op_adv[i].str)) { |
| 89 | op = op_adv[i].op; |
| 90 | adv = op_adv[i].adv; |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | if (i == ARRAY_SIZE(op_adv)) { |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 95 | expr = 1; |
| 96 | break; |
| 97 | } |
Stephen Warren | 28cca55 | 2014-02-03 13:21:02 -0700 | [diff] [blame] | 98 | if (left < adv) { |
| 99 | expr = 1; |
| 100 | break; |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 101 | } |
| 102 | |
Stephen Warren | 28cca55 | 2014-02-03 13:21:02 -0700 | [diff] [blame] | 103 | switch (op) { |
| 104 | case OP_STR_EMPTY: |
| 105 | expr = strlen(ap[1]) == 0 ? 1 : 0; |
| 106 | break; |
| 107 | case OP_STR_NEMPTY: |
| 108 | expr = strlen(ap[1]) == 0 ? 0 : 1; |
| 109 | break; |
| 110 | case OP_STR_EQ: |
| 111 | expr = strcmp(ap[0], ap[2]) == 0; |
| 112 | break; |
| 113 | case OP_STR_NEQ: |
| 114 | expr = strcmp(ap[0], ap[2]) != 0; |
| 115 | break; |
| 116 | case OP_STR_LT: |
| 117 | expr = strcmp(ap[0], ap[2]) < 0; |
| 118 | break; |
| 119 | case OP_STR_GT: |
| 120 | expr = strcmp(ap[0], ap[2]) > 0; |
| 121 | break; |
| 122 | case OP_INT_EQ: |
| 123 | expr = simple_strtol(ap[0], NULL, 10) == |
| 124 | simple_strtol(ap[2], NULL, 10); |
| 125 | break; |
| 126 | case OP_INT_NEQ: |
| 127 | expr = simple_strtol(ap[0], NULL, 10) != |
| 128 | simple_strtol(ap[2], NULL, 10); |
| 129 | break; |
| 130 | case OP_INT_LT: |
| 131 | expr = simple_strtol(ap[0], NULL, 10) < |
| 132 | simple_strtol(ap[2], NULL, 10); |
| 133 | break; |
| 134 | case OP_INT_LE: |
| 135 | expr = simple_strtol(ap[0], NULL, 10) <= |
| 136 | simple_strtol(ap[2], NULL, 10); |
| 137 | break; |
| 138 | case OP_INT_GT: |
| 139 | expr = simple_strtol(ap[0], NULL, 10) > |
| 140 | simple_strtol(ap[2], NULL, 10); |
| 141 | break; |
| 142 | case OP_INT_GE: |
| 143 | expr = simple_strtol(ap[0], NULL, 10) >= |
| 144 | simple_strtol(ap[2], NULL, 10); |
| 145 | break; |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 146 | } |
| 147 | |
Stephen Warren | 28cca55 | 2014-02-03 13:21:02 -0700 | [diff] [blame] | 148 | switch (op) { |
| 149 | case OP_OR: |
| 150 | last_expr = expr; |
Stephen Warren | 08b763b | 2014-02-03 13:21:04 -0700 | [diff] [blame^] | 151 | last_binop = OP_OR; |
Stephen Warren | 28cca55 | 2014-02-03 13:21:02 -0700 | [diff] [blame] | 152 | break; |
| 153 | case OP_AND: |
| 154 | last_expr = expr; |
Stephen Warren | 08b763b | 2014-02-03 13:21:04 -0700 | [diff] [blame^] | 155 | last_binop = OP_AND; |
| 156 | break; |
| 157 | case OP_NOT: |
| 158 | if (last_unop == OP_NOT) |
| 159 | last_unop = OP_INVALID; |
| 160 | else |
| 161 | last_unop = OP_NOT; |
Stephen Warren | 28cca55 | 2014-02-03 13:21:02 -0700 | [diff] [blame] | 162 | break; |
| 163 | default: |
Stephen Warren | 08b763b | 2014-02-03 13:21:04 -0700 | [diff] [blame^] | 164 | if (last_unop == OP_NOT) { |
| 165 | expr = !expr; |
| 166 | last_unop = OP_INVALID; |
| 167 | } |
| 168 | |
| 169 | if (last_binop == OP_OR) |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 170 | expr = last_expr || expr; |
Stephen Warren | 08b763b | 2014-02-03 13:21:04 -0700 | [diff] [blame^] | 171 | else if (last_binop == OP_AND) |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 172 | expr = last_expr && expr; |
Stephen Warren | 08b763b | 2014-02-03 13:21:04 -0700 | [diff] [blame^] | 173 | last_binop = OP_INVALID; |
| 174 | |
Stephen Warren | 28cca55 | 2014-02-03 13:21:02 -0700 | [diff] [blame] | 175 | break; |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | ap += adv; left -= adv; |
| 179 | } |
| 180 | |
Peter Tyser | eabe6d9 | 2009-10-16 17:36:25 -0500 | [diff] [blame] | 181 | expr = !expr; |
| 182 | |
| 183 | debug (": returns %d\n", expr); |
| 184 | |
| 185 | return expr; |
| 186 | } |
| 187 | |
| 188 | U_BOOT_CMD( |
| 189 | test, CONFIG_SYS_MAXARGS, 1, do_test, |
| 190 | "minimal test like /bin/sh", |
| 191 | "[args..]" |
| 192 | ); |
Peter Tyser | cc35c49 | 2009-10-16 17:36:27 -0500 | [diff] [blame] | 193 | |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 194 | static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Peter Tyser | cc35c49 | 2009-10-16 17:36:27 -0500 | [diff] [blame] | 195 | { |
| 196 | return 1; |
| 197 | } |
| 198 | |
| 199 | U_BOOT_CMD( |
| 200 | false, CONFIG_SYS_MAXARGS, 1, do_false, |
| 201 | "do nothing, unsuccessfully", |
| 202 | NULL |
| 203 | ); |
| 204 | |
Kim Phillips | dc00a68 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 205 | static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Peter Tyser | cc35c49 | 2009-10-16 17:36:27 -0500 | [diff] [blame] | 206 | { |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | U_BOOT_CMD( |
| 211 | true, CONFIG_SYS_MAXARGS, 1, do_true, |
| 212 | "do nothing, successfully", |
| 213 | NULL |
| 214 | ); |