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