blob: bacc3684069b89f39b899cb21a19f33379328bbe [file] [log] [blame]
Peter Tysereabe6d92009-10-16 17:36:25 -05001/*
2 * Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Peter Tysereabe6d92009-10-16 17:36:25 -05006 */
7
York Sunc6b36262013-04-04 11:52:53 +00008/*
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 Tysereabe6d92009-10-16 17:36:25 -050017#include <common.h>
18#include <command.h>
19
Kim Phillipsdc00a682012-10-29 13:34:31 +000020static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tysereabe6d92009-10-16 17:36:25 -050021{
Wolfgang Denk6262d0212010-06-28 22:00:46 +020022 char * const *ap;
Peter Tysereabe6d92009-10-16 17:36:25 -050023 int left, adv, expr, last_expr, neg, last_cmp;
24
25 /* args? */
26 if (argc < 3)
27 return 1;
28
Joe Hershbergereeff2282012-08-17 10:56:12 +000029#ifdef DEBUG
Peter Tysereabe6d92009-10-16 17:36:25 -050030 {
Joe Hershbergereeff2282012-08-17 10:56:12 +000031 debug("test(%d):", argc);
Peter Tysereabe6d92009-10-16 17:36:25 -050032 left = 1;
33 while (argv[left])
Joe Hershbergereeff2282012-08-17 10:56:12 +000034 debug(" '%s'", argv[left++]);
Peter Tysereabe6d92009-10-16 17:36:25 -050035 }
36#endif
37
38 last_expr = 0;
39 left = argc - 1; ap = argv + 1;
40 if (left > 0 && strcmp(ap[0], "!") == 0) {
41 neg = 1;
42 ap++;
43 left--;
44 } else
45 neg = 0;
46
47 expr = -1;
48 last_cmp = -1;
49 last_expr = -1;
50 while (left > 0) {
51
52 if (strcmp(ap[0], "-o") == 0 || strcmp(ap[0], "-a") == 0)
53 adv = 1;
54 else if (strcmp(ap[0], "-z") == 0 || strcmp(ap[0], "-n") == 0)
55 adv = 2;
56 else
57 adv = 3;
58
59 if (left < adv) {
60 expr = 1;
61 break;
62 }
63
64 if (adv == 1) {
65 if (strcmp(ap[0], "-o") == 0) {
66 last_expr = expr;
67 last_cmp = 0;
68 } else if (strcmp(ap[0], "-a") == 0) {
69 last_expr = expr;
70 last_cmp = 1;
71 } else {
72 expr = 1;
73 break;
74 }
75 }
76
77 if (adv == 2) {
78 if (strcmp(ap[0], "-z") == 0)
79 expr = strlen(ap[1]) == 0 ? 1 : 0;
80 else if (strcmp(ap[0], "-n") == 0)
81 expr = strlen(ap[1]) == 0 ? 0 : 1;
82 else {
83 expr = 1;
84 break;
85 }
86
87 if (last_cmp == 0)
88 expr = last_expr || expr;
89 else if (last_cmp == 1)
90 expr = last_expr && expr;
91 last_cmp = -1;
92 }
93
94 if (adv == 3) {
95 if (strcmp(ap[1], "=") == 0)
96 expr = strcmp(ap[0], ap[2]) == 0;
97 else if (strcmp(ap[1], "!=") == 0)
98 expr = strcmp(ap[0], ap[2]) != 0;
99 else if (strcmp(ap[1], ">") == 0)
100 expr = strcmp(ap[0], ap[2]) > 0;
101 else if (strcmp(ap[1], "<") == 0)
102 expr = strcmp(ap[0], ap[2]) < 0;
103 else if (strcmp(ap[1], "-eq") == 0)
104 expr = simple_strtol(ap[0], NULL, 10) == simple_strtol(ap[2], NULL, 10);
105 else if (strcmp(ap[1], "-ne") == 0)
106 expr = simple_strtol(ap[0], NULL, 10) != simple_strtol(ap[2], NULL, 10);
107 else if (strcmp(ap[1], "-lt") == 0)
108 expr = simple_strtol(ap[0], NULL, 10) < simple_strtol(ap[2], NULL, 10);
109 else if (strcmp(ap[1], "-le") == 0)
110 expr = simple_strtol(ap[0], NULL, 10) <= simple_strtol(ap[2], NULL, 10);
111 else if (strcmp(ap[1], "-gt") == 0)
112 expr = simple_strtol(ap[0], NULL, 10) > simple_strtol(ap[2], NULL, 10);
113 else if (strcmp(ap[1], "-ge") == 0)
114 expr = simple_strtol(ap[0], NULL, 10) >= simple_strtol(ap[2], NULL, 10);
115 else {
116 expr = 1;
117 break;
118 }
119
120 if (last_cmp == 0)
121 expr = last_expr || expr;
122 else if (last_cmp == 1)
123 expr = last_expr && expr;
124 last_cmp = -1;
125 }
126
127 ap += adv; left -= adv;
128 }
129
130 if (neg)
131 expr = !expr;
132
133 expr = !expr;
134
135 debug (": returns %d\n", expr);
136
137 return expr;
138}
139
140U_BOOT_CMD(
141 test, CONFIG_SYS_MAXARGS, 1, do_test,
142 "minimal test like /bin/sh",
143 "[args..]"
144);
Peter Tysercc35c492009-10-16 17:36:27 -0500145
Kim Phillipsdc00a682012-10-29 13:34:31 +0000146static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tysercc35c492009-10-16 17:36:27 -0500147{
148 return 1;
149}
150
151U_BOOT_CMD(
152 false, CONFIG_SYS_MAXARGS, 1, do_false,
153 "do nothing, unsuccessfully",
154 NULL
155);
156
Kim Phillipsdc00a682012-10-29 13:34:31 +0000157static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Peter Tysercc35c492009-10-16 17:36:27 -0500158{
159 return 0;
160}
161
162U_BOOT_CMD(
163 true, CONFIG_SYS_MAXARGS, 1, do_true,
164 "do nothing, successfully",
165 NULL
166);