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