1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright 2000-2009 4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 5 */ 6 7 #include <common.h> 8 #include <command.h> 9 #include <fs.h> 10 11 #define OP_INVALID 0 12 #define OP_NOT 1 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 27 #define OP_FILE_EXISTS 16 28 29 const struct { 30 int arg; 31 const char *str; 32 int op; 33 int adv; 34 } op_adv[] = { 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}, 45 {0, "!", OP_NOT, 1}, 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}, 50 {0, "-e", OP_FILE_EXISTS, 4}, 51 }; 52 53 static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 54 { 55 char * const *ap; 56 int i, op, left, adv, expr, last_expr, last_unop, last_binop; 57 58 /* args? */ 59 if (argc < 3) 60 return 1; 61 62 #ifdef DEBUG 63 { 64 debug("test(%d):", argc); 65 left = 1; 66 while (argv[left]) 67 debug(" '%s'", argv[left++]); 68 } 69 #endif 70 71 left = argc - 1; 72 ap = argv + 1; 73 expr = 0; 74 last_unop = OP_INVALID; 75 last_binop = OP_INVALID; 76 last_expr = -1; 77 while (left > 0) { 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 } 91 if (left < adv) { 92 expr = 1; 93 break; 94 } 95 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: 116 expr = simple_strtol(ap[0], NULL, 10) == 117 simple_strtol(ap[2], NULL, 10); 118 break; 119 case OP_INT_NEQ: 120 expr = simple_strtol(ap[0], NULL, 10) != 121 simple_strtol(ap[2], NULL, 10); 122 break; 123 case OP_INT_LT: 124 expr = simple_strtol(ap[0], NULL, 10) < 125 simple_strtol(ap[2], NULL, 10); 126 break; 127 case OP_INT_LE: 128 expr = simple_strtol(ap[0], NULL, 10) <= 129 simple_strtol(ap[2], NULL, 10); 130 break; 131 case OP_INT_GT: 132 expr = simple_strtol(ap[0], NULL, 10) > 133 simple_strtol(ap[2], NULL, 10); 134 break; 135 case OP_INT_GE: 136 expr = simple_strtol(ap[0], NULL, 10) >= 137 simple_strtol(ap[2], NULL, 10); 138 break; 139 case OP_FILE_EXISTS: 140 expr = file_exists(ap[1], ap[2], ap[3], FS_TYPE_ANY); 141 break; 142 } 143 144 switch (op) { 145 case OP_OR: 146 last_expr = expr; 147 last_binop = OP_OR; 148 break; 149 case OP_AND: 150 last_expr = expr; 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; 158 break; 159 default: 160 if (last_unop == OP_NOT) { 161 expr = !expr; 162 last_unop = OP_INVALID; 163 } 164 165 if (last_binop == OP_OR) 166 expr = last_expr || expr; 167 else if (last_binop == OP_AND) 168 expr = last_expr && expr; 169 last_binop = OP_INVALID; 170 171 break; 172 } 173 174 ap += adv; left -= adv; 175 } 176 177 expr = !expr; 178 179 debug (": returns %d\n", expr); 180 181 return expr; 182 } 183 184 #undef true 185 #undef false 186 187 U_BOOT_CMD( 188 test, CONFIG_SYS_MAXARGS, 1, do_test, 189 "minimal test like /bin/sh", 190 "[args..]" 191 ); 192 193 static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 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 204 static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 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 ); 214