blob: 93cb255b22599311d5364987aaaa440953830083 [file] [log] [blame]
Kumar Galaaed83342008-02-14 20:44:42 -06001/*
2 * Copyright 2008 Freescale Semiconductor, Inc.
Wolfgang Denkb04ae782013-03-23 23:50:34 +00003 * Copyright 2013 Wolfgang Denk <wd@denx.de>
Kumar Galaaed83342008-02-14 20:44:42 -06004 *
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/*
25 * This file provides a shell like 'expr' function to return.
26 */
27
28#include <common.h>
29#include <config.h>
30#include <command.h>
31
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +010032static ulong get_arg(char *s, int w)
33{
34 ulong *p;
35
Wolfgang Denk9a183d22010-06-23 20:50:54 +020036 /*
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +010037 * if the parameter starts with a '*' then assume
38 * it is a pointer to the value we want
39 */
40
41 if (s[0] == '*') {
42 p = (ulong *)simple_strtoul(&s[1], NULL, 16);
43 switch (w) {
44 case 1: return((ulong)(*(uchar *)p));
45 case 2: return((ulong)(*(ushort *)p));
46 case 4:
47 default: return(*p);
48 }
49 } else {
50 return simple_strtoul(s, NULL, 16);
51 }
52}
53
Wolfgang Denkb04ae782013-03-23 23:50:34 +000054#ifdef CONFIG_REGEX
55
56#include <slre.h>
57
58#define SLRE_BUFSZ 16384
59#define SLRE_PATSZ 4096
60
61/*
62 * memstr - Find the first substring in memory
63 * @s1: The string to be searched
64 * @s2: The string to search for
65 *
66 * Similar to and based on strstr(),
67 * but strings do not need to be NUL terminated.
68 */
69static char *memstr(const char *s1, int l1, const char *s2, int l2)
70{
71 if (!l2)
72 return (char *)s1;
73
74 while (l1 >= l2) {
75 l1--;
76 if (!memcmp(s1, s2, l2))
77 return (char *)s1;
78 s1++;
79 }
80 return NULL;
81}
82
83static char *substitute(char *string, /* string buffer */
84 int *slen, /* current string length */
85 int ssize, /* string bufer size */
86 const char *old,/* old (replaced) string */
87 int olen, /* length of old string */
88 const char *new,/* new (replacement) string */
89 int nlen) /* length of new string */
90{
91 char *p = memstr(string, *slen, old, olen);
92
93 if (p == NULL)
94 return NULL;
95
96 debug("## Match at pos %ld: match len %d, subst len %d\n",
97 (long)(p - string), olen, nlen);
98
99 /* make sure replacement matches */
100 if (*slen + nlen - olen > ssize) {
101 printf("## error: substitution buffer overflow\n");
102 return NULL;
103 }
104
105 /* move tail if needed */
106 if (olen != nlen) {
107 int tail, len;
108
109 len = (olen > nlen) ? olen : nlen;
110
111 tail = ssize - (p + len - string);
112
113 debug("## tail len %d\n", tail);
114
115 memmove(p + nlen, p + olen, tail);
116 }
117
118 /* insert substitue */
119 memcpy(p, new, nlen);
120
121 *slen += nlen - olen;
122
123 return p + nlen;
124}
125
126/*
127 * Perform regex operations on a environment variable
128 *
129 * Returns 0 if OK, 1 in case of errors.
130 */
131static int regex_sub(const char *name,
132 const char *r, const char *s, const char *t,
133 int global)
134{
135 struct slre slre;
136 char data[SLRE_BUFSZ];
137 char *datap = data;
138 const char *value;
139 int res, len, nlen, loop;
140
141 if (name == NULL)
142 return 1;
143
144 if (slre_compile(&slre, r) == 0) {
145 printf("Error compiling regex: %s\n", slre.err_str);
146 return 1;
147 }
148
149 if (t == NULL) {
150 value = getenv(name);
151
152 if (value == NULL) {
153 printf("## Error: variable \"%s\" not defined\n", name);
154 return 1;
155 }
156 t = value;
157 }
158
159 debug("REGEX on %s=%s\n", name, t);
160 debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n",
161 r, s ? s : "<NULL>", global);
162
163 len = strlen(t);
164 if (len + 1 > SLRE_BUFSZ) {
165 printf("## error: subst buffer overflow: have %d, need %d\n",
166 SLRE_BUFSZ, len + 1);
167 return 1;
168 }
169
170 strcpy(data, t);
171
172 if (s == NULL)
173 nlen = 0;
174 else
175 nlen = strlen(s);
176
177 for (loop = 0;; loop++) {
178 struct cap caps[slre.num_caps + 2];
179 char nbuf[SLRE_PATSZ];
180 const char *old;
181 char *np;
182 int i, olen;
183
184 (void) memset(caps, 0, sizeof(caps));
185
186 res = slre_match(&slre, datap, len, caps);
187
188 debug("Result: %d\n", res);
189
190 for (i = 0; i < slre.num_caps; i++) {
191 if (caps[i].len > 0) {
192 debug("Substring %d: [%.*s]\n", i,
193 caps[i].len, caps[i].ptr);
194 }
195 }
196
197 if (res == 0) {
198 if (loop == 0) {
199 printf("%s: No match\n", t);
200 return 1;
201 } else {
202 break;
203 }
204 }
205
206 debug("## MATCH ## %s\n", data);
207
208 if (s == NULL) {
209 printf("%s=%s\n", name, t);
210 return 1;
211 }
212
213 old = caps[0].ptr;
214 olen = caps[0].len;
215
216 if (nlen + 1 >= SLRE_PATSZ) {
217 printf("## error: pattern buffer overflow: have %d, need %d\n",
218 SLRE_BUFSZ, nlen + 1);
219 return 1;
220 }
221 strcpy(nbuf, s);
222
223 debug("## SUBST(1) ## %s\n", nbuf);
224
225 /*
226 * Handle back references
227 *
228 * Support for \0 ... \9, where \0 is the
229 * whole matched pattern (similar to &).
230 *
231 * Implementation is a bit simpleminded as
232 * backrefs are substituted sequentially, one
233 * by one. This will lead to somewhat
234 * unexpected results if the replacement
235 * strings contain any \N strings then then
236 * may get substitued, too. We accept this
237 * restriction for the sake of simplicity.
238 */
239 for (i = 0; i < 10; ++i) {
240 char backref[2] = {
241 '\\',
242 '0',
243 };
244
245 if (caps[i].len == 0)
246 break;
247
248 backref[1] += i;
249
250 debug("## BACKREF %d: replace \"%.*s\" by \"%.*s\" in \"%s\"\n",
251 i,
252 2, backref,
253 caps[i].len, caps[i].ptr,
254 nbuf);
255
256 for (np = nbuf;;) {
257 char *p = memstr(np, nlen, backref, 2);
258
259 if (p == NULL)
260 break;
261
262 np = substitute(np, &nlen,
263 SLRE_PATSZ,
264 backref, 2,
265 caps[i].ptr, caps[i].len);
266
267 if (np == NULL)
268 return 1;
269 }
270 }
271 debug("## SUBST(2) ## %s\n", nbuf);
272
273 datap = substitute(datap, &len, SLRE_BUFSZ,
274 old, olen,
275 nbuf, nlen);
276
277 if (datap == NULL)
278 return 1;
279
280 debug("## REMAINDER: %s\n", datap);
281
282 debug("## RESULT: %s\n", data);
283
284 if (!global)
285 break;
286 }
287 debug("## FINAL (now setenv()) : %s\n", data);
288
289 printf("%s=%s\n", name, data);
290
291 return setenv(name, data);
292}
293#endif
294
Kim Phillipsdc00a682012-10-29 13:34:31 +0000295static int do_setexpr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Kumar Galaaed83342008-02-14 20:44:42 -0600296{
297 ulong a, b;
Simon Glass103789d2013-02-24 17:33:22 +0000298 ulong value;
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +0100299 int w;
Kumar Galaaed83342008-02-14 20:44:42 -0600300
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000301 /*
302 * We take 3, 5, or 6 arguments:
303 * 3 : setexpr name value
304 * 5 : setexpr name val1 op val2
305 * setexpr name [g]sub r s
306 * 6 : setexpr name [g]sub r s t
307 */
308
309 /* > 6 already tested by max command args */
310 if ((argc < 3) || (argc == 4))
Simon Glassa06dfc72011-12-10 08:44:01 +0000311 return CMD_RET_USAGE;
Kumar Galaaed83342008-02-14 20:44:42 -0600312
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +0100313 w = cmd_get_data_size(argv[0], 4);
314
315 a = get_arg(argv[2], w);
Joe Hershberger0e640e62012-11-01 16:21:14 +0000316
Wolfgang Denkb2234d62013-03-23 23:50:33 +0000317 /* plain assignment: "setexpr name value" */
Joe Hershberger0e640e62012-11-01 16:21:14 +0000318 if (argc == 3) {
Simon Glass103789d2013-02-24 17:33:22 +0000319 setenv_hex(argv[1], a);
Joe Hershberger0e640e62012-11-01 16:21:14 +0000320 return 0;
321 }
322
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000323 /* 5 or 6 args (6 args only with [g]sub) */
324#ifdef CONFIG_REGEX
325 /*
326 * rexep handling: "setexpr name [g]sub r s [t]"
327 * with 5 args, "t" will be NULL
328 */
329 if (strcmp(argv[2], "gsub") == 0)
330 return regex_sub(argv[1], argv[3], argv[4], argv[5], 1);
331
332 if (strcmp(argv[2], "sub") == 0)
333 return regex_sub(argv[1], argv[3], argv[4], argv[5], 0);
334#endif
335
Wolfgang Denkb2234d62013-03-23 23:50:33 +0000336 /* standard operators: "setexpr name val1 op val2" */
337 if (argc != 5)
338 return CMD_RET_USAGE;
339
340 if (strlen(argv[3]) != 1)
341 return CMD_RET_USAGE;
342
Frans Meulenbroeks2347d4e2010-02-26 14:00:19 +0100343 b = get_arg(argv[4], w);
Kumar Galaaed83342008-02-14 20:44:42 -0600344
345 switch (argv[3][0]) {
Simon Glass103789d2013-02-24 17:33:22 +0000346 case '|':
347 value = a | b;
348 break;
349 case '&':
350 value = a & b;
351 break;
352 case '+':
353 value = a + b;
354 break;
355 case '^':
356 value = a ^ b;
357 break;
358 case '-':
359 value = a - b;
360 break;
361 case '*':
362 value = a * b;
363 break;
364 case '/':
365 value = a / b;
366 break;
367 case '%':
368 value = a % b;
369 break;
Kumar Galaaed83342008-02-14 20:44:42 -0600370 default:
371 printf("invalid op\n");
372 return 1;
373 }
374
Simon Glass103789d2013-02-24 17:33:22 +0000375 setenv_hex(argv[1], value);
Kumar Galaaed83342008-02-14 20:44:42 -0600376
377 return 0;
378}
379
380U_BOOT_CMD(
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000381 setexpr, 6, 0, do_setexpr,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600382 "set environment variable as the result of eval expression",
Joe Hershberger0e640e62012-11-01 16:21:14 +0000383 "[.b, .w, .l] name [*]value1 <op> [*]value2\n"
Kumar Galaaed83342008-02-14 20:44:42 -0600384 " - set environment variable 'name' to the result of the evaluated\n"
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000385 " expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n"
Joe Hershberger0e640e62012-11-01 16:21:14 +0000386 " size argument is only meaningful if value1 and/or value2 are\n"
387 " memory addresses (*)\n"
Wolfgang Denkb2234d62013-03-23 23:50:33 +0000388 "setexpr[.b, .w, .l] name [*]value\n"
389 " - load a value into a variable"
Wolfgang Denkb04ae782013-03-23 23:50:34 +0000390#ifdef CONFIG_REGEX
391 "\n"
392 "setexpr name gsub r s [t]\n"
393 " - For each substring matching the regular expression <r> in the\n"
394 " string <t>, substitute the string <s>. The result is\n"
395 " assigned to <name>. If <t> is not supplied, use the old\n"
396 " value of <name>\n"
397 "setexpr name sub r s [t]\n"
398 " - Just like gsub(), but replace only the first matching substring"
399#endif
Kumar Galaaed83342008-02-14 20:44:42 -0600400);