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.
16 static ulong get_arg(char *s, int w)
19 * If the parameter starts with a '*' then assume it is a pointer to
27 addr = simple_strtoul(&s[1], NULL, 16);
30 p = map_sysmem(addr, sizeof(uchar));
31 val = (ulong)*(uchar *)p;
35 p = map_sysmem(addr, sizeof(ushort));
36 val = (ulong)*(ushort *)p;
41 p = map_sysmem(addr, sizeof(ulong));
47 return simple_strtoul(s, NULL, 16);
55 #define SLRE_BUFSZ 16384
56 #define SLRE_PATSZ 4096
59 * memstr - Find the first substring in memory
60 * @s1: The string to be searched
61 * @s2: The string to search for
63 * Similar to and based on strstr(),
64 * but strings do not need to be NUL terminated.
66 static char *memstr(const char *s1, int l1, const char *s2, int l2)
73 if (!memcmp(s1, s2, l2))
80 static char *substitute(char *string, /* string buffer */
81 int *slen, /* current string length */
82 int ssize, /* string bufer size */
83 const char *old,/* old (replaced) string */
84 int olen, /* length of old string */
85 const char *new,/* new (replacement) string */
86 int nlen) /* length of new string */
88 char *p = memstr(string, *slen, old, olen);
93 debug("## Match at pos %ld: match len %d, subst len %d\n",
94 (long)(p - string), olen, nlen);
96 /* make sure replacement matches */
97 if (*slen + nlen - olen > ssize) {
98 printf("## error: substitution buffer overflow\n");
102 /* move tail if needed */
106 len = (olen > nlen) ? olen : nlen;
108 tail = ssize - (p + len - string);
110 debug("## tail len %d\n", tail);
112 memmove(p + nlen, p + olen, tail);
115 /* insert substitue */
116 memcpy(p, new, nlen);
118 *slen += nlen - olen;
124 * Perform regex operations on a environment variable
126 * Returns 0 if OK, 1 in case of errors.
128 static int regex_sub(const char *name,
129 const char *r, const char *s, const char *t,
133 char data[SLRE_BUFSZ];
136 int res, len, nlen, loop;
141 if (slre_compile(&slre, r) == 0) {
142 printf("Error compiling regex: %s\n", slre.err_str);
147 value = env_get(name);
150 printf("## Error: variable \"%s\" not defined\n", name);
156 debug("REGEX on %s=%s\n", name, t);
157 debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n",
158 r, s ? s : "<NULL>", global);
161 if (len + 1 > SLRE_BUFSZ) {
162 printf("## error: subst buffer overflow: have %d, need %d\n",
163 SLRE_BUFSZ, len + 1);
174 for (loop = 0;; loop++) {
175 struct cap caps[slre.num_caps + 2];
176 char nbuf[SLRE_PATSZ];
181 (void) memset(caps, 0, sizeof(caps));
183 res = slre_match(&slre, datap, len, caps);
185 debug("Result: %d\n", res);
187 for (i = 0; i < slre.num_caps; i++) {
188 if (caps[i].len > 0) {
189 debug("Substring %d: [%.*s]\n", i,
190 caps[i].len, caps[i].ptr);
196 printf("%s: No match\n", t);
203 debug("## MATCH ## %s\n", data);
206 printf("%s=%s\n", name, t);
213 if (nlen + 1 >= SLRE_PATSZ) {
214 printf("## error: pattern buffer overflow: have %d, need %d\n",
215 SLRE_BUFSZ, nlen + 1);
220 debug("## SUBST(1) ## %s\n", nbuf);
223 * Handle back references
225 * Support for \0 ... \9, where \0 is the
226 * whole matched pattern (similar to &).
228 * Implementation is a bit simpleminded as
229 * backrefs are substituted sequentially, one
230 * by one. This will lead to somewhat
231 * unexpected results if the replacement
232 * strings contain any \N strings then then
233 * may get substitued, too. We accept this
234 * restriction for the sake of simplicity.
236 for (i = 0; i < 10; ++i) {
242 if (caps[i].len == 0)
247 debug("## BACKREF %d: replace \"%.*s\" by \"%.*s\" in \"%s\"\n",
250 caps[i].len, caps[i].ptr,
254 char *p = memstr(np, nlen, backref, 2);
259 np = substitute(np, &nlen,
262 caps[i].ptr, caps[i].len);
268 debug("## SUBST(2) ## %s\n", nbuf);
270 datap = substitute(datap, &len, SLRE_BUFSZ,
277 debug("## REMAINDER: %s\n", datap);
279 debug("## RESULT: %s\n", data);
284 debug("## FINAL (now env_set()) : %s\n", data);
286 printf("%s=%s\n", name, data);
288 return env_set(name, data);
292 static int do_setexpr(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
299 * We take 3, 5, or 6 arguments:
300 * 3 : setexpr name value
301 * 5 : setexpr name val1 op val2
302 * setexpr name [g]sub r s
303 * 6 : setexpr name [g]sub r s t
306 /* > 6 already tested by max command args */
307 if ((argc < 3) || (argc == 4))
308 return CMD_RET_USAGE;
310 w = cmd_get_data_size(argv[0], 4);
312 a = get_arg(argv[2], w);
314 /* plain assignment: "setexpr name value" */
316 env_set_hex(argv[1], a);
320 /* 5 or 6 args (6 args only with [g]sub) */
323 * rexep handling: "setexpr name [g]sub r s [t]"
324 * with 5 args, "t" will be NULL
326 if (strcmp(argv[2], "gsub") == 0)
327 return regex_sub(argv[1], argv[3], argv[4], argv[5], 1);
329 if (strcmp(argv[2], "sub") == 0)
330 return regex_sub(argv[1], argv[3], argv[4], argv[5], 0);
333 /* standard operators: "setexpr name val1 op val2" */
335 return CMD_RET_USAGE;
337 if (strlen(argv[3]) != 1)
338 return CMD_RET_USAGE;
340 b = get_arg(argv[4], w);
342 switch (argv[3][0]) {
368 printf("invalid op\n");
372 env_set_hex(argv[1], value);
378 setexpr, 6, 0, do_setexpr,
379 "set environment variable as the result of eval expression",
380 "[.b, .w, .l] name [*]value1 <op> [*]value2\n"
381 " - set environment variable 'name' to the result of the evaluated\n"
382 " expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n"
383 " size argument is only meaningful if value1 and/or value2 are\n"
384 " memory addresses (*)\n"
385 "setexpr[.b, .w, .l] name [*]value\n"
386 " - load a value into a variable"
389 "setexpr name gsub r s [t]\n"
390 " - For each substring matching the regular expression <r> in the\n"
391 " string <t>, substitute the string <s>. The result is\n"
392 " assigned to <name>. If <t> is not supplied, use the old\n"
394 "setexpr name sub r s [t]\n"
395 " - Just like gsub(), but replace only the first matching substring"