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;
138 * regex_sub() - Replace a regex pattern with a string
140 * @data: Buffer containing the string to update
141 * @data_size: Size of buffer (must be large enough for the new string)
142 * @nbuf: Back-reference buffer
143 * @nbuf_size: Size of back-reference buffer (must be larger enough for @s plus
144 * all back-reference expansions)
145 * @r: Regular expression to find
146 * @s: String to replace with
147 * @global: true to replace all matches in @data, false to replace just the
149 * @return 0 if OK, 1 on error
151 static int regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size,
152 const char *r, const char *s, bool global)
156 int res, len, nlen, loop;
158 if (slre_compile(&slre, r) == 0) {
159 printf("Error compiling regex: %s\n", slre.err_str);
169 for (loop = 0;; loop++) {
170 struct cap caps[slre.num_caps + 2];
175 (void) memset(caps, 0, sizeof(caps));
177 res = slre_match(&slre, datap, len, caps);
179 debug("Result: %d\n", res);
181 for (i = 0; i < slre.num_caps; i++) {
182 if (caps[i].len > 0) {
183 debug("Substring %d: [%.*s]\n", i,
184 caps[i].len, caps[i].ptr);
190 printf("%s: No match\n", data);
197 debug("## MATCH ## %s\n", data);
205 if (nlen + 1 >= nbuf_size) {
206 printf("## error: pattern buffer overflow: have %d, need %d\n",
207 nbuf_size, nlen + 1);
212 debug("## SUBST(1) ## %s\n", nbuf);
215 * Handle back references
217 * Support for \0 ... \9, where \0 is the
218 * whole matched pattern (similar to &).
220 * Implementation is a bit simpleminded as
221 * backrefs are substituted sequentially, one
222 * by one. This will lead to somewhat
223 * unexpected results if the replacement
224 * strings contain any \N strings then then
225 * may get substitued, too. We accept this
226 * restriction for the sake of simplicity.
228 for (i = 0; i < 10; ++i) {
234 if (caps[i].len == 0)
239 debug("## BACKREF %d: replace \"%.*s\" by \"%.*s\" in \"%s\"\n",
242 caps[i].len, caps[i].ptr,
246 char *p = memstr(np, nlen, backref, 2);
251 np = substitute(np, &nlen,
254 caps[i].ptr, caps[i].len);
260 debug("## SUBST(2) ## %s\n", nbuf);
262 datap = substitute(datap, &len, data_size, old, olen,
268 debug("## REMAINDER: %s\n", datap);
270 debug("## RESULT: %s\n", data);
275 debug("## FINAL (now env_set()) : %s\n", data);
280 #define SLRE_BUFSZ 16384
281 #define SLRE_PATSZ 4096
284 * Perform regex operations on a environment variable
286 * Returns 0 if OK, 1 in case of errors.
288 static int regex_sub_var(const char *name, const char *r, const char *s,
289 const char *t, int global)
292 char data[SLRE_BUFSZ];
293 char nbuf[SLRE_PATSZ];
301 if (slre_compile(&slre, r) == 0) {
302 printf("Error compiling regex: %s\n", slre.err_str);
307 value = env_get(name);
309 printf("## Error: variable \"%s\" not defined\n", name);
315 debug("REGEX on %s=%s\n", name, t);
316 debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n", r, s ? s : "<NULL>",
320 if (len + 1 > SLRE_BUFSZ) {
321 printf("## error: subst buffer overflow: have %d, need %d\n",
322 SLRE_BUFSZ, len + 1);
328 ret = regex_sub(data, SLRE_BUFSZ, nbuf, SLRE_PATSZ, r, s, global);
332 printf("%s=%s\n", name, data);
334 return env_set(name, data);
338 static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
346 * We take 3, 5, or 6 arguments:
347 * 3 : setexpr name value
348 * 5 : setexpr name val1 op val2
349 * setexpr name [g]sub r s
350 * 6 : setexpr name [g]sub r s t
353 /* > 6 already tested by max command args */
354 if ((argc < 3) || (argc == 4))
355 return CMD_RET_USAGE;
357 w = cmd_get_data_size(argv[0], 4);
359 a = get_arg(argv[2], w);
361 /* plain assignment: "setexpr name value" */
363 env_set_hex(argv[1], a);
367 /* 5 or 6 args (6 args only with [g]sub) */
370 * rexep handling: "setexpr name [g]sub r s [t]"
371 * with 5 args, "t" will be NULL
373 if (strcmp(argv[2], "gsub") == 0)
374 return regex_sub_var(argv[1], argv[3], argv[4], argv[5], 1);
376 if (strcmp(argv[2], "sub") == 0)
377 return regex_sub_var(argv[1], argv[3], argv[4], argv[5], 0);
380 /* standard operators: "setexpr name val1 op val2" */
382 return CMD_RET_USAGE;
384 if (strlen(argv[3]) != 1)
385 return CMD_RET_USAGE;
387 b = get_arg(argv[4], w);
389 switch (argv[3][0]) {
415 printf("invalid op\n");
419 env_set_hex(argv[1], value);
425 setexpr, 6, 0, do_setexpr,
426 "set environment variable as the result of eval expression",
427 "[.b, .w, .l] name [*]value1 <op> [*]value2\n"
428 " - set environment variable 'name' to the result of the evaluated\n"
429 " expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n"
430 " size argument is only meaningful if value1 and/or value2 are\n"
431 " memory addresses (*)\n"
432 "setexpr[.b, .w, .l] name [*]value\n"
433 " - load a value into a variable"
436 "setexpr name gsub r s [t]\n"
437 " - For each substring matching the regular expression <r> in the\n"
438 " string <t>, substitute the string <s>. The result is\n"
439 " assigned to <name>. If <t> is not supplied, use the old\n"
441 "setexpr name sub r s [t]\n"
442 " - Just like gsub(), but replace only the first matching substring"