test.c 4 KB
Newer Older
1 2 3 4
/*
 * Copyright 2000-2009
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
5
 * SPDX-License-Identifier:	GPL-2.0+
6 7 8 9
 */

#include <common.h>
#include <command.h>
10
#include <fs.h>
11

12
#define OP_INVALID	0
13
#define OP_NOT		1
14 15 16 17 18 19 20 21 22 23 24 25 26 27
#define OP_OR		2
#define OP_AND		3
#define OP_STR_EMPTY	4
#define OP_STR_NEMPTY	5
#define OP_STR_EQ	6
#define OP_STR_NEQ	7
#define OP_STR_LT	8
#define OP_STR_GT	9
#define OP_INT_EQ	10
#define OP_INT_NEQ	11
#define OP_INT_LT	12
#define OP_INT_LE	13
#define OP_INT_GT	14
#define OP_INT_GE	15
28
#define OP_FILE_EXISTS	16
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

const struct {
	int arg;
	const char *str;
	int op;
	int adv;
} op_adv[] = {
	{1, "=", OP_STR_EQ, 3},
	{1, "!=", OP_STR_NEQ, 3},
	{1, "<", OP_STR_LT, 3},
	{1, ">", OP_STR_GT, 3},
	{1, "-eq", OP_INT_EQ, 3},
	{1, "-ne", OP_INT_NEQ, 3},
	{1, "-lt", OP_INT_LT, 3},
	{1, "-le", OP_INT_LE, 3},
	{1, "-gt", OP_INT_GT, 3},
	{1, "-ge", OP_INT_GE, 3},
46
	{0, "!", OP_NOT, 1},
47 48 49 50
	{0, "-o", OP_OR, 1},
	{0, "-a", OP_AND, 1},
	{0, "-z", OP_STR_EMPTY, 2},
	{0, "-n", OP_STR_NEMPTY, 2},
51
	{0, "-e", OP_FILE_EXISTS, 4},
52 53
};

Kim Phillips's avatar
Kim Phillips committed
54
static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
55
{
56
	char * const *ap;
57
	int i, op, left, adv, expr, last_expr, last_unop, last_binop;
58 59 60 61 62

	/* args? */
	if (argc < 3)
		return 1;

63
#ifdef DEBUG
64
	{
65
		debug("test(%d):", argc);
66 67
		left = 1;
		while (argv[left])
68
			debug(" '%s'", argv[left++]);
69 70 71
	}
#endif

72 73
	left = argc - 1;
	ap = argv + 1;
74
	expr = 0;
75 76
	last_unop = OP_INVALID;
	last_binop = OP_INVALID;
77 78
	last_expr = -1;
	while (left > 0) {
79 80 81 82 83 84 85 86 87 88
		for (i = 0; i < ARRAY_SIZE(op_adv); i++) {
			if (left <= op_adv[i].arg)
				continue;
			if (!strcmp(ap[op_adv[i].arg], op_adv[i].str)) {
				op = op_adv[i].op;
				adv = op_adv[i].adv;
				break;
			}
		}
		if (i == ARRAY_SIZE(op_adv)) {
89 90 91
			expr = 1;
			break;
		}
92 93 94
		if (left < adv) {
			expr = 1;
			break;
95 96
		}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
		switch (op) {
		case OP_STR_EMPTY:
			expr = strlen(ap[1]) == 0 ? 1 : 0;
			break;
		case OP_STR_NEMPTY:
			expr = strlen(ap[1]) == 0 ? 0 : 1;
			break;
		case OP_STR_EQ:
			expr = strcmp(ap[0], ap[2]) == 0;
			break;
		case OP_STR_NEQ:
			expr = strcmp(ap[0], ap[2]) != 0;
			break;
		case OP_STR_LT:
			expr = strcmp(ap[0], ap[2]) < 0;
			break;
		case OP_STR_GT:
			expr = strcmp(ap[0], ap[2]) > 0;
			break;
		case OP_INT_EQ:
			expr = simple_strtol(ap[0], NULL, 10) ==
					simple_strtol(ap[2], NULL, 10);
			break;
		case OP_INT_NEQ:
			expr = simple_strtol(ap[0], NULL, 10) !=
					simple_strtol(ap[2], NULL, 10);
			break;
		case OP_INT_LT:
			expr = simple_strtol(ap[0], NULL, 10) <
					simple_strtol(ap[2], NULL, 10);
			break;
		case OP_INT_LE:
			expr = simple_strtol(ap[0], NULL, 10) <=
					simple_strtol(ap[2], NULL, 10);
			break;
		case OP_INT_GT:
			expr = simple_strtol(ap[0], NULL, 10) >
					simple_strtol(ap[2], NULL, 10);
			break;
		case OP_INT_GE:
			expr = simple_strtol(ap[0], NULL, 10) >=
					simple_strtol(ap[2], NULL, 10);
			break;
140 141 142
		case OP_FILE_EXISTS:
			expr = file_exists(ap[1], ap[2], ap[3], FS_TYPE_ANY);
			break;
143 144
		}

145 146 147
		switch (op) {
		case OP_OR:
			last_expr = expr;
148
			last_binop = OP_OR;
149 150 151
			break;
		case OP_AND:
			last_expr = expr;
152 153 154 155 156 157 158
			last_binop = OP_AND;
			break;
		case OP_NOT:
			if (last_unop == OP_NOT)
				last_unop = OP_INVALID;
			else
				last_unop = OP_NOT;
159 160
			break;
		default:
161 162 163 164 165 166
			if (last_unop == OP_NOT) {
				expr = !expr;
				last_unop = OP_INVALID;
			}

			if (last_binop == OP_OR)
167
				expr = last_expr || expr;
168
			else if (last_binop == OP_AND)
169
				expr = last_expr && expr;
170 171
			last_binop = OP_INVALID;

172
			break;
173 174 175 176 177 178 179 180 181 182 183 184
		}

		ap += adv; left -= adv;
	}

	expr = !expr;

	debug (": returns %d\n", expr);

	return expr;
}

185 186 187
#undef true
#undef false

188 189 190 191 192
U_BOOT_CMD(
	test,	CONFIG_SYS_MAXARGS,	1,	do_test,
	"minimal test like /bin/sh",
	"[args..]"
);
193

Kim Phillips's avatar
Kim Phillips committed
194
static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
195 196 197 198 199 200 201 202 203 204
{
	return 1;
}

U_BOOT_CMD(
	false,	CONFIG_SYS_MAXARGS,	1,	do_false,
	"do nothing, unsuccessfully",
	NULL
);

Kim Phillips's avatar
Kim Phillips committed
205
static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
206 207 208 209 210 211 212 213 214
{
	return 0;
}

U_BOOT_CMD(
	true,	CONFIG_SYS_MAXARGS,	1,	do_true,
	"do nothing, successfully",
	NULL
);