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;
42 p = map_sysmem(addr, sizeof(u32));
47 p = map_sysmem(addr, sizeof(ulong));
53 return simple_strtoul(s, NULL, 16);
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))
84 * substitute() - Substitute part of one string with another
86 * This updates @string so that the first occurrence of @old is replaced with
89 * @string: String buffer containing string to update at the start
90 * @slen: Pointer to current string length, updated on success
91 * @ssize: Size of string buffer
92 * @old: Old string to find in the buffer (no terminator needed)
93 * @olen: Length of @old excluding terminator
94 * @new: New string to replace @old with
95 * @nlen: Length of @new excluding terminator
96 * @return pointer to immediately after the copied @new in @string, or NULL if
97 * no replacement took place
99 static char *substitute(char *string, int *slen, int ssize,
100 const char *old, int olen, const char *new, int nlen)
102 char *p = memstr(string, *slen, old, olen);
107 debug("## Match at pos %ld: match len %d, subst len %d\n",
108 (long)(p - string), olen, nlen);
110 /* make sure replacement matches */
111 if (*slen + nlen - olen > ssize) {
112 printf("## error: substitution buffer overflow\n");
116 /* move tail if needed */
120 len = (olen > nlen) ? olen : nlen;
122 tail = ssize - (p + len - string);
124 debug("## tail len %d\n", tail);
126 memmove(p + nlen, p + olen, tail);
129 /* insert substitute */
130 memcpy(p, new, nlen);
132 *slen += nlen - olen;
137 int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size,
138 const char *r, const char *s, bool global)
142 int res, len, nlen, loop;
144 if (slre_compile(&slre, r) == 0) {
145 printf("Error compiling regex: %s\n", slre.err_str);
150 for (loop = 0;; loop++) {
151 struct cap caps[slre.num_caps + 2];
156 (void) memset(caps, 0, sizeof(caps));
158 res = slre_match(&slre, datap, len, caps);
160 debug("Result: %d\n", res);
162 for (i = 0; i < slre.num_caps; i++) {
163 if (caps[i].len > 0) {
164 debug("Substring %d: [%.*s]\n", i,
165 caps[i].len, caps[i].ptr);
171 printf("%s: No match\n", data);
178 debug("## MATCH ## %s\n", data);
187 if (nlen + 1 >= nbuf_size) {
188 printf("## error: pattern buffer overflow: have %d, need %d\n",
189 nbuf_size, nlen + 1);
194 debug("## SUBST(1) ## %s\n", nbuf);
197 * Handle back references
199 * Support for \0 ... \9, where \0 is the
200 * whole matched pattern (similar to &).
202 * Implementation is a bit simpleminded as
203 * backrefs are substituted sequentially, one
204 * by one. This will lead to somewhat
205 * unexpected results if the replacement
206 * strings contain any \N strings then then
207 * may get substitued, too. We accept this
208 * restriction for the sake of simplicity.
210 for (i = 0; i < 10; ++i) {
216 if (caps[i].len == 0)
221 debug("## BACKREF %d: replace \"%.*s\" by \"%.*s\" in \"%s\"\n",
224 caps[i].len, caps[i].ptr,
228 char *p = memstr(np, nlen, backref, 2);
233 np = substitute(np, &nlen,
236 caps[i].ptr, caps[i].len);
242 debug("## SUBST(2) ## %s\n", nbuf);
244 datap = substitute(datap, &len, data_size, old, olen,
250 debug("## REMAINDER: %s\n", datap);
252 debug("## RESULT: %s\n", data);
257 debug("## FINAL (now env_set()) : %s\n", data);
262 #define SLRE_BUFSZ 16384
263 #define SLRE_PATSZ 4096
266 * Perform regex operations on a environment variable
268 * Returns 0 if OK, 1 in case of errors.
270 static int regex_sub_var(const char *name, const char *r, const char *s,
271 const char *t, int global)
274 char data[SLRE_BUFSZ];
275 char nbuf[SLRE_PATSZ];
283 if (slre_compile(&slre, r) == 0) {
284 printf("Error compiling regex: %s\n", slre.err_str);
289 value = env_get(name);
291 printf("## Error: variable \"%s\" not defined\n", name);
297 debug("REGEX on %s=%s\n", name, t);
298 debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n", r, s ? s : "<NULL>",
302 if (len + 1 > SLRE_BUFSZ) {
303 printf("## error: subst buffer overflow: have %d, need %d\n",
304 SLRE_BUFSZ, len + 1);
310 ret = setexpr_regex_sub(data, SLRE_BUFSZ, nbuf, SLRE_PATSZ, r, s,
315 printf("%s=%s\n", name, data);
317 return env_set(name, data);
321 static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
329 * We take 3, 5, or 6 arguments:
330 * 3 : setexpr name value
331 * 5 : setexpr name val1 op val2
332 * setexpr name [g]sub r s
333 * 6 : setexpr name [g]sub r s t
336 /* > 6 already tested by max command args */
337 if ((argc < 3) || (argc == 4))
338 return CMD_RET_USAGE;
340 w = cmd_get_data_size(argv[0], 4);
342 a = get_arg(argv[2], w);
344 /* plain assignment: "setexpr name value" */
346 env_set_hex(argv[1], a);
350 /* 5 or 6 args (6 args only with [g]sub) */
353 * rexep handling: "setexpr name [g]sub r s [t]"
354 * with 5 args, "t" will be NULL
356 if (strcmp(argv[2], "gsub") == 0)
357 return regex_sub_var(argv[1], argv[3], argv[4], argv[5], 1);
359 if (strcmp(argv[2], "sub") == 0)
360 return regex_sub_var(argv[1], argv[3], argv[4], argv[5], 0);
363 /* standard operators: "setexpr name val1 op val2" */
365 return CMD_RET_USAGE;
367 if (strlen(argv[3]) != 1)
368 return CMD_RET_USAGE;
370 b = get_arg(argv[4], w);
372 switch (argv[3][0]) {
398 printf("invalid op\n");
402 env_set_hex(argv[1], value);
408 setexpr, 6, 0, do_setexpr,
409 "set environment variable as the result of eval expression",
410 "[.b, .w, .l] name [*]value1 <op> [*]value2\n"
411 " - set environment variable 'name' to the result of the evaluated\n"
412 " expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n"
413 " size argument is only meaningful if value1 and/or value2 are\n"
414 " memory addresses (*)\n"
415 "setexpr[.b, .w, .l] name [*]value\n"
416 " - load a value into a variable"
419 "setexpr name gsub r s [t]\n"
420 " - For each substring matching the regular expression <r> in the\n"
421 " string <t>, substitute the string <s>. The result is\n"
422 " assigned to <name>. If <t> is not supplied, use the old\n"
424 "setexpr name sub r s [t]\n"
425 " - Just like gsub(), but replace only the first matching substring"