2 * Copyright 2008 Freescale Semiconductor, Inc.
3 * Copyright 2013 Wolfgang Denk <wd@denx.de>
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
25 * This file provides a shell like 'expr' function to return.
32 static ulong get_arg(char *s, int w)
37 * if the parameter starts with a '*' then assume
38 * it is a pointer to the value we want
42 p = (ulong *)simple_strtoul(&s[1], NULL, 16);
44 case 1: return((ulong)(*(uchar *)p));
45 case 2: return((ulong)(*(ushort *)p));
50 return simple_strtoul(s, NULL, 16);
58 #define SLRE_BUFSZ 16384
59 #define SLRE_PATSZ 4096
62 * memstr - Find the first substring in memory
63 * @s1: The string to be searched
64 * @s2: The string to search for
66 * Similar to and based on strstr(),
67 * but strings do not need to be NUL terminated.
69 static char *memstr(const char *s1, int l1, const char *s2, int l2)
76 if (!memcmp(s1, s2, l2))
83 static 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 */
91 char *p = memstr(string, *slen, old, olen);
96 debug("## Match at pos %ld: match len %d, subst len %d\n",
97 (long)(p - string), olen, nlen);
99 /* make sure replacement matches */
100 if (*slen + nlen - olen > ssize) {
101 printf("## error: substitution buffer overflow\n");
105 /* move tail if needed */
109 len = (olen > nlen) ? olen : nlen;
111 tail = ssize - (p + len - string);
113 debug("## tail len %d\n", tail);
115 memmove(p + nlen, p + olen, tail);
118 /* insert substitue */
119 memcpy(p, new, nlen);
121 *slen += nlen - olen;
127 * Perform regex operations on a environment variable
129 * Returns 0 if OK, 1 in case of errors.
131 static int regex_sub(const char *name,
132 const char *r, const char *s, const char *t,
136 char data[SLRE_BUFSZ];
139 int res, len, nlen, loop;
144 if (slre_compile(&slre, r) == 0) {
145 printf("Error compiling regex: %s\n", slre.err_str);
150 value = getenv(name);
153 printf("## Error: variable \"%s\" not defined\n", name);
159 debug("REGEX on %s=%s\n", name, t);
160 debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n",
161 r, s ? s : "<NULL>", global);
164 if (len + 1 > SLRE_BUFSZ) {
165 printf("## error: subst buffer overflow: have %d, need %d\n",
166 SLRE_BUFSZ, len + 1);
177 for (loop = 0;; loop++) {
178 struct cap caps[slre.num_caps + 2];
179 char nbuf[SLRE_PATSZ];
184 (void) memset(caps, 0, sizeof(caps));
186 res = slre_match(&slre, datap, len, caps);
188 debug("Result: %d\n", res);
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);
199 printf("%s: No match\n", t);
206 debug("## MATCH ## %s\n", data);
209 printf("%s=%s\n", name, t);
216 if (nlen + 1 >= SLRE_PATSZ) {
217 printf("## error: pattern buffer overflow: have %d, need %d\n",
218 SLRE_BUFSZ, nlen + 1);
223 debug("## SUBST(1) ## %s\n", nbuf);
226 * Handle back references
228 * Support for \0 ... \9, where \0 is the
229 * whole matched pattern (similar to &).
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.
239 for (i = 0; i < 10; ++i) {
245 if (caps[i].len == 0)
250 debug("## BACKREF %d: replace \"%.*s\" by \"%.*s\" in \"%s\"\n",
253 caps[i].len, caps[i].ptr,
257 char *p = memstr(np, nlen, backref, 2);
262 np = substitute(np, &nlen,
265 caps[i].ptr, caps[i].len);
271 debug("## SUBST(2) ## %s\n", nbuf);
273 datap = substitute(datap, &len, SLRE_BUFSZ,
280 debug("## REMAINDER: %s\n", datap);
282 debug("## RESULT: %s\n", data);
287 debug("## FINAL (now setenv()) : %s\n", data);
289 printf("%s=%s\n", name, data);
291 return setenv(name, data);
295 static int do_setexpr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
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
309 /* > 6 already tested by max command args */
310 if ((argc < 3) || (argc == 4))
311 return CMD_RET_USAGE;
313 w = cmd_get_data_size(argv[0], 4);
315 a = get_arg(argv[2], w);
317 /* plain assignment: "setexpr name value" */
319 setenv_hex(argv[1], a);
323 /* 5 or 6 args (6 args only with [g]sub) */
326 * rexep handling: "setexpr name [g]sub r s [t]"
327 * with 5 args, "t" will be NULL
329 if (strcmp(argv[2], "gsub") == 0)
330 return regex_sub(argv[1], argv[3], argv[4], argv[5], 1);
332 if (strcmp(argv[2], "sub") == 0)
333 return regex_sub(argv[1], argv[3], argv[4], argv[5], 0);
336 /* standard operators: "setexpr name val1 op val2" */
338 return CMD_RET_USAGE;
340 if (strlen(argv[3]) != 1)
341 return CMD_RET_USAGE;
343 b = get_arg(argv[4], w);
345 switch (argv[3][0]) {
371 printf("invalid op\n");
375 setenv_hex(argv[1], value);
381 setexpr, 6, 0, do_setexpr,
382 "set environment variable as the result of eval expression",
383 "[.b, .w, .l] name [*]value1 <op> [*]value2\n"
384 " - set environment variable 'name' to the result of the evaluated\n"
385 " expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n"
386 " size argument is only meaningful if value1 and/or value2 are\n"
387 " memory addresses (*)\n"
388 "setexpr[.b, .w, .l] name [*]value\n"
389 " - load a value into a variable"
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"
397 "setexpr name sub r s [t]\n"
398 " - Just like gsub(), but replace only the first matching substring"