]>
Commit | Line | Data |
---|---|---|
6b8f5ad1 PT |
1 | /* |
2 | * Copyright 2000-2009 | |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
4 | * | |
5 | * See file CREDITS for list of people who contributed to this | |
6 | * project. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public License as | |
10 | * published by the Free Software Foundation; either version 2 of | |
11 | * the License, or (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
21 | * MA 02111-1307 USA | |
22 | */ | |
23 | ||
24 | #include <common.h> | |
25 | #include <command.h> | |
26 | ||
54841ab5 | 27 | int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
6b8f5ad1 | 28 | { |
54841ab5 | 29 | char * const *ap; |
6b8f5ad1 PT |
30 | int left, adv, expr, last_expr, neg, last_cmp; |
31 | ||
32 | /* args? */ | |
33 | if (argc < 3) | |
34 | return 1; | |
35 | ||
36 | #if 0 | |
37 | { | |
38 | printf("test:"); | |
39 | left = 1; | |
40 | while (argv[left]) | |
41 | printf(" %s", argv[left++]); | |
42 | } | |
43 | #endif | |
44 | ||
45 | last_expr = 0; | |
46 | left = argc - 1; ap = argv + 1; | |
47 | if (left > 0 && strcmp(ap[0], "!") == 0) { | |
48 | neg = 1; | |
49 | ap++; | |
50 | left--; | |
51 | } else | |
52 | neg = 0; | |
53 | ||
54 | expr = -1; | |
55 | last_cmp = -1; | |
56 | last_expr = -1; | |
57 | while (left > 0) { | |
58 | ||
59 | if (strcmp(ap[0], "-o") == 0 || strcmp(ap[0], "-a") == 0) | |
60 | adv = 1; | |
61 | else if (strcmp(ap[0], "-z") == 0 || strcmp(ap[0], "-n") == 0) | |
62 | adv = 2; | |
63 | else | |
64 | adv = 3; | |
65 | ||
66 | if (left < adv) { | |
67 | expr = 1; | |
68 | break; | |
69 | } | |
70 | ||
71 | if (adv == 1) { | |
72 | if (strcmp(ap[0], "-o") == 0) { | |
73 | last_expr = expr; | |
74 | last_cmp = 0; | |
75 | } else if (strcmp(ap[0], "-a") == 0) { | |
76 | last_expr = expr; | |
77 | last_cmp = 1; | |
78 | } else { | |
79 | expr = 1; | |
80 | break; | |
81 | } | |
82 | } | |
83 | ||
84 | if (adv == 2) { | |
85 | if (strcmp(ap[0], "-z") == 0) | |
86 | expr = strlen(ap[1]) == 0 ? 1 : 0; | |
87 | else if (strcmp(ap[0], "-n") == 0) | |
88 | expr = strlen(ap[1]) == 0 ? 0 : 1; | |
89 | else { | |
90 | expr = 1; | |
91 | break; | |
92 | } | |
93 | ||
94 | if (last_cmp == 0) | |
95 | expr = last_expr || expr; | |
96 | else if (last_cmp == 1) | |
97 | expr = last_expr && expr; | |
98 | last_cmp = -1; | |
99 | } | |
100 | ||
101 | if (adv == 3) { | |
102 | if (strcmp(ap[1], "=") == 0) | |
103 | expr = strcmp(ap[0], ap[2]) == 0; | |
104 | else if (strcmp(ap[1], "!=") == 0) | |
105 | expr = strcmp(ap[0], ap[2]) != 0; | |
106 | else if (strcmp(ap[1], ">") == 0) | |
107 | expr = strcmp(ap[0], ap[2]) > 0; | |
108 | else if (strcmp(ap[1], "<") == 0) | |
109 | expr = strcmp(ap[0], ap[2]) < 0; | |
110 | else if (strcmp(ap[1], "-eq") == 0) | |
111 | expr = simple_strtol(ap[0], NULL, 10) == simple_strtol(ap[2], NULL, 10); | |
112 | else if (strcmp(ap[1], "-ne") == 0) | |
113 | expr = simple_strtol(ap[0], NULL, 10) != simple_strtol(ap[2], NULL, 10); | |
114 | else if (strcmp(ap[1], "-lt") == 0) | |
115 | expr = simple_strtol(ap[0], NULL, 10) < simple_strtol(ap[2], NULL, 10); | |
116 | else if (strcmp(ap[1], "-le") == 0) | |
117 | expr = simple_strtol(ap[0], NULL, 10) <= simple_strtol(ap[2], NULL, 10); | |
118 | else if (strcmp(ap[1], "-gt") == 0) | |
119 | expr = simple_strtol(ap[0], NULL, 10) > simple_strtol(ap[2], NULL, 10); | |
120 | else if (strcmp(ap[1], "-ge") == 0) | |
121 | expr = simple_strtol(ap[0], NULL, 10) >= simple_strtol(ap[2], NULL, 10); | |
122 | else { | |
123 | expr = 1; | |
124 | break; | |
125 | } | |
126 | ||
127 | if (last_cmp == 0) | |
128 | expr = last_expr || expr; | |
129 | else if (last_cmp == 1) | |
130 | expr = last_expr && expr; | |
131 | last_cmp = -1; | |
132 | } | |
133 | ||
134 | ap += adv; left -= adv; | |
135 | } | |
136 | ||
137 | if (neg) | |
138 | expr = !expr; | |
139 | ||
140 | expr = !expr; | |
141 | ||
142 | debug (": returns %d\n", expr); | |
143 | ||
144 | return expr; | |
145 | } | |
146 | ||
147 | U_BOOT_CMD( | |
148 | test, CONFIG_SYS_MAXARGS, 1, do_test, | |
149 | "minimal test like /bin/sh", | |
150 | "[args..]" | |
151 | ); | |
396fd173 | 152 | |
54841ab5 | 153 | int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
396fd173 PT |
154 | { |
155 | return 1; | |
156 | } | |
157 | ||
158 | U_BOOT_CMD( | |
159 | false, CONFIG_SYS_MAXARGS, 1, do_false, | |
160 | "do nothing, unsuccessfully", | |
161 | NULL | |
162 | ); | |
163 | ||
54841ab5 | 164 | int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
396fd173 PT |
165 | { |
166 | return 0; | |
167 | } | |
168 | ||
169 | U_BOOT_CMD( | |
170 | true, CONFIG_SYS_MAXARGS, 1, do_true, | |
171 | "do nothing, successfully", | |
172 | NULL | |
173 | ); |