2 * Copyright 2008 Freescale Semiconductor, Inc.
3 * Copyright 2013 Wolfgang Denk <wd@denx.de>
5 * SPDX-License-Identifier: GPL-2.0+
9 * This file provides a shell like 'expr' function to return.
16 static ulong get_arg(char *s, int w)
21 * if the parameter starts with a '*' then assume
22 * it is a pointer to the value we want
26 p = (ulong *)simple_strtoul(&s[1], NULL, 16);
28 case 1: return((ulong)(*(uchar *)p));
29 case 2: return((ulong)(*(ushort *)p));
34 return simple_strtoul(s, NULL, 16);
42 #define SLRE_BUFSZ 16384
43 #define SLRE_PATSZ 4096
46 * memstr - Find the first substring in memory
47 * @s1: The string to be searched
48 * @s2: The string to search for
50 * Similar to and based on strstr(),
51 * but strings do not need to be NUL terminated.
53 static char *memstr(const char *s1, int l1, const char *s2, int l2)
60 if (!memcmp(s1, s2, l2))
67 static char *substitute(char *string, /* string buffer */
68 int *slen, /* current string length */
69 int ssize, /* string bufer size */
70 const char *old,/* old (replaced) string */
71 int olen, /* length of old string */
72 const char *new,/* new (replacement) string */
73 int nlen) /* length of new string */
75 char *p = memstr(string, *slen, old, olen);
80 debug("## Match at pos %ld: match len %d, subst len %d\n",
81 (long)(p - string), olen, nlen);
83 /* make sure replacement matches */
84 if (*slen + nlen - olen > ssize) {
85 printf("## error: substitution buffer overflow\n");
89 /* move tail if needed */
93 len = (olen > nlen) ? olen : nlen;
95 tail = ssize - (p + len - string);
97 debug("## tail len %d\n", tail);
99 memmove(p + nlen, p + olen, tail);
102 /* insert substitue */
103 memcpy(p, new, nlen);
105 *slen += nlen - olen;
111 * Perform regex operations on a environment variable
113 * Returns 0 if OK, 1 in case of errors.
115 static int regex_sub(const char *name,
116 const char *r, const char *s, const char *t,
120 char data[SLRE_BUFSZ];
123 int res, len, nlen, loop;
128 if (slre_compile(&slre, r) == 0) {
129 printf("Error compiling regex: %s\n", slre.err_str);
134 value = getenv(name);
137 printf("## Error: variable \"%s\" not defined\n", name);
143 debug("REGEX on %s=%s\n", name, t);
144 debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n",
145 r, s ? s : "<NULL>", global);
148 if (len + 1 > SLRE_BUFSZ) {
149 printf("## error: subst buffer overflow: have %d, need %d\n",
150 SLRE_BUFSZ, len + 1);
161 for (loop = 0;; loop++) {
162 struct cap caps[slre.num_caps + 2];
163 char nbuf[SLRE_PATSZ];
168 (void) memset(caps, 0, sizeof(caps));
170 res = slre_match(&slre, datap, len, caps);
172 debug("Result: %d\n", res);
174 for (i = 0; i < slre.num_caps; i++) {
175 if (caps[i].len > 0) {
176 debug("Substring %d: [%.*s]\n", i,
177 caps[i].len, caps[i].ptr);
183 printf("%s: No match\n", t);
190 debug("## MATCH ## %s\n", data);
193 printf("%s=%s\n", name, t);
200 if (nlen + 1 >= SLRE_PATSZ) {
201 printf("## error: pattern buffer overflow: have %d, need %d\n",
202 SLRE_BUFSZ, nlen + 1);
207 debug("## SUBST(1) ## %s\n", nbuf);
210 * Handle back references
212 * Support for \0 ... \9, where \0 is the
213 * whole matched pattern (similar to &).
215 * Implementation is a bit simpleminded as
216 * backrefs are substituted sequentially, one
217 * by one. This will lead to somewhat
218 * unexpected results if the replacement
219 * strings contain any \N strings then then
220 * may get substitued, too. We accept this
221 * restriction for the sake of simplicity.
223 for (i = 0; i < 10; ++i) {
229 if (caps[i].len == 0)
234 debug("## BACKREF %d: replace \"%.*s\" by \"%.*s\" in \"%s\"\n",
237 caps[i].len, caps[i].ptr,
241 char *p = memstr(np, nlen, backref, 2);
246 np = substitute(np, &nlen,
249 caps[i].ptr, caps[i].len);
255 debug("## SUBST(2) ## %s\n", nbuf);
257 datap = substitute(datap, &len, SLRE_BUFSZ,
264 debug("## REMAINDER: %s\n", datap);
266 debug("## RESULT: %s\n", data);
271 debug("## FINAL (now setenv()) : %s\n", data);
273 printf("%s=%s\n", name, data);
275 return setenv(name, data);
279 static int do_setexpr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
286 * We take 3, 5, or 6 arguments:
287 * 3 : setexpr name value
288 * 5 : setexpr name val1 op val2
289 * setexpr name [g]sub r s
290 * 6 : setexpr name [g]sub r s t
293 /* > 6 already tested by max command args */
294 if ((argc < 3) || (argc == 4))
295 return CMD_RET_USAGE;
297 w = cmd_get_data_size(argv[0], 4);
299 a = get_arg(argv[2], w);
301 /* plain assignment: "setexpr name value" */
303 setenv_hex(argv[1], a);
307 /* 5 or 6 args (6 args only with [g]sub) */
310 * rexep handling: "setexpr name [g]sub r s [t]"
311 * with 5 args, "t" will be NULL
313 if (strcmp(argv[2], "gsub") == 0)
314 return regex_sub(argv[1], argv[3], argv[4], argv[5], 1);
316 if (strcmp(argv[2], "sub") == 0)
317 return regex_sub(argv[1], argv[3], argv[4], argv[5], 0);
320 /* standard operators: "setexpr name val1 op val2" */
322 return CMD_RET_USAGE;
324 if (strlen(argv[3]) != 1)
325 return CMD_RET_USAGE;
327 b = get_arg(argv[4], w);
329 switch (argv[3][0]) {
355 printf("invalid op\n");
359 setenv_hex(argv[1], value);
365 setexpr, 6, 0, do_setexpr,
366 "set environment variable as the result of eval expression",
367 "[.b, .w, .l] name [*]value1 <op> [*]value2\n"
368 " - set environment variable 'name' to the result of the evaluated\n"
369 " expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n"
370 " size argument is only meaningful if value1 and/or value2 are\n"
371 " memory addresses (*)\n"
372 "setexpr[.b, .w, .l] name [*]value\n"
373 " - load a value into a variable"
376 "setexpr name gsub r s [t]\n"
377 " - For each substring matching the regular expression <r> in the\n"
378 " string <t>, substitute the string <s>. The result is\n"
379 " assigned to <name>. If <t> is not supplied, use the old\n"
381 "setexpr name sub r s [t]\n"
382 " - Just like gsub(), but replace only the first matching substring"