1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2008 Freescale Semiconductor, Inc.
4 * Copyright 2013 Wolfgang Denk <wd@denx.de>
8 * This file provides a shell like 'expr' function to return.
18 static ulong get_arg(char *s, int w)
21 * If the parameter starts with a '*' then assume it is a pointer to
29 addr = simple_strtoul(&s[1], NULL, 16);
32 p = map_sysmem(addr, sizeof(uchar));
33 val = (ulong)*(uchar *)p;
37 p = map_sysmem(addr, sizeof(ushort));
38 val = (ulong)*(ushort *)p;
43 p = map_sysmem(addr, sizeof(ulong));
49 return simple_strtoul(s, NULL, 16);
57 #define SLRE_BUFSZ 16384
58 #define SLRE_PATSZ 4096
61 * memstr - Find the first substring in memory
62 * @s1: The string to be searched
63 * @s2: The string to search for
65 * Similar to and based on strstr(),
66 * but strings do not need to be NUL terminated.
68 static char *memstr(const char *s1, int l1, const char *s2, int l2)
75 if (!memcmp(s1, s2, l2))
82 static char *substitute(char *string, /* string buffer */
83 int *slen, /* current string length */
84 int ssize, /* string bufer size */
85 const char *old,/* old (replaced) string */
86 int olen, /* length of old string */
87 const char *new,/* new (replacement) string */
88 int nlen) /* length of new string */
90 char *p = memstr(string, *slen, old, olen);
95 debug("## Match at pos %ld: match len %d, subst len %d\n",
96 (long)(p - string), olen, nlen);
98 /* make sure replacement matches */
99 if (*slen + nlen - olen > ssize) {
100 printf("## error: substitution buffer overflow\n");
104 /* move tail if needed */
108 len = (olen > nlen) ? olen : nlen;
110 tail = ssize - (p + len - string);
112 debug("## tail len %d\n", tail);
114 memmove(p + nlen, p + olen, tail);
117 /* insert substitue */
118 memcpy(p, new, nlen);
120 *slen += nlen - olen;
126 * Perform regex operations on a environment variable
128 * Returns 0 if OK, 1 in case of errors.
130 static int regex_sub(const char *name,
131 const char *r, const char *s, const char *t,
135 char data[SLRE_BUFSZ];
138 int res, len, nlen, loop;
143 if (slre_compile(&slre, r) == 0) {
144 printf("Error compiling regex: %s\n", slre.err_str);
149 value = env_get(name);
152 printf("## Error: variable \"%s\" not defined\n", name);
158 debug("REGEX on %s=%s\n", name, t);
159 debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n",
160 r, s ? s : "<NULL>", global);
163 if (len + 1 > SLRE_BUFSZ) {
164 printf("## error: subst buffer overflow: have %d, need %d\n",
165 SLRE_BUFSZ, len + 1);
176 for (loop = 0;; loop++) {
177 struct cap caps[slre.num_caps + 2];
178 char nbuf[SLRE_PATSZ];
183 (void) memset(caps, 0, sizeof(caps));
185 res = slre_match(&slre, datap, len, caps);
187 debug("Result: %d\n", res);
189 for (i = 0; i < slre.num_caps; i++) {
190 if (caps[i].len > 0) {
191 debug("Substring %d: [%.*s]\n", i,
192 caps[i].len, caps[i].ptr);
198 printf("%s: No match\n", t);
205 debug("## MATCH ## %s\n", data);
208 printf("%s=%s\n", name, t);
215 if (nlen + 1 >= SLRE_PATSZ) {
216 printf("## error: pattern buffer overflow: have %d, need %d\n",
217 SLRE_BUFSZ, nlen + 1);
222 debug("## SUBST(1) ## %s\n", nbuf);
225 * Handle back references
227 * Support for \0 ... \9, where \0 is the
228 * whole matched pattern (similar to &).
230 * Implementation is a bit simpleminded as
231 * backrefs are substituted sequentially, one
232 * by one. This will lead to somewhat
233 * unexpected results if the replacement
234 * strings contain any \N strings then then
235 * may get substitued, too. We accept this
236 * restriction for the sake of simplicity.
238 for (i = 0; i < 10; ++i) {
244 if (caps[i].len == 0)
249 debug("## BACKREF %d: replace \"%.*s\" by \"%.*s\" in \"%s\"\n",
252 caps[i].len, caps[i].ptr,
256 char *p = memstr(np, nlen, backref, 2);
261 np = substitute(np, &nlen,
264 caps[i].ptr, caps[i].len);
270 debug("## SUBST(2) ## %s\n", nbuf);
272 datap = substitute(datap, &len, SLRE_BUFSZ,
279 debug("## REMAINDER: %s\n", datap);
281 debug("## RESULT: %s\n", data);
286 debug("## FINAL (now env_set()) : %s\n", data);
288 printf("%s=%s\n", name, data);
290 return env_set(name, data);
294 static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
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 env_set_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 env_set_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"