]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_test.c
karo: tx51: remove duplicate CONFIG_SYS_SDRAM_CLK definition
[karo-tx-uboot.git] / common / cmd_test.c
1 /*
2  * Copyright 2000-2009
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 /*
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
17 #include <common.h>
18 #include <command.h>
19
20 static int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
21 {
22         char * const *ap;
23         int left, adv, expr, last_expr, neg, last_cmp;
24
25         /* args? */
26         if (argc < 3)
27                 return 1;
28
29 #ifdef DEBUG
30         {
31                 debug("test(%d):", argc);
32                 left = 1;
33                 while (argv[left])
34                         debug(" '%s'", argv[left++]);
35         }
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
140 U_BOOT_CMD(
141         test,   CONFIG_SYS_MAXARGS,     1,      do_test,
142         "minimal test like /bin/sh",
143         "[args..]"
144 );
145
146 static int do_false(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
147 {
148         return 1;
149 }
150
151 U_BOOT_CMD(
152         false,  CONFIG_SYS_MAXARGS,     1,      do_false,
153         "do nothing, unsuccessfully",
154         NULL
155 );
156
157 static int do_true(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
158 {
159         return 0;
160 }
161
162 U_BOOT_CMD(
163         true,   CONFIG_SYS_MAXARGS,     1,      do_true,
164         "do nothing, successfully",
165         NULL
166 );