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 #include <linux/sizes.h>
21 * struct expr_arg: Holds an argument to an expression
23 * @ival: Integer value (if width is not CMD_DATA_SIZE_STR)
24 * @sval: String value (if width is CMD_DATA_SIZE_STR)
33 static int get_arg(char *s, int w, struct expr_arg *argp)
38 * If the parameter starts with a '*' then assume it is a pointer to
48 addr = simple_strtoul(&s[1], NULL, 16);
51 p = map_sysmem(addr, sizeof(uchar));
52 val = (ulong)*(uchar *)p;
57 p = map_sysmem(addr, sizeof(ushort));
58 val = (ulong)*(ushort *)p;
62 case CMD_DATA_SIZE_STR:
63 p = map_sysmem(addr, SZ_64K);
65 /* Maximum string length of 64KB plus terminator */
66 len = strnlen((char *)p, SZ_64K) + 1;
69 printf("Out of memory\n");
78 p = map_sysmem(addr, sizeof(u32));
84 p = map_sysmem(addr, sizeof(ulong));
91 if (w == CMD_DATA_SIZE_STR)
93 arg.ival = simple_strtoul(s, NULL, 16);
105 * memstr - Find the first substring in memory
106 * @s1: The string to be searched
107 * @s2: The string to search for
109 * Similar to and based on strstr(),
110 * but strings do not need to be NUL terminated.
112 static char *memstr(const char *s1, int l1, const char *s2, int l2)
119 if (!memcmp(s1, s2, l2))
127 * substitute() - Substitute part of one string with another
129 * This updates @string so that the first occurrence of @old is replaced with
132 * @string: String buffer containing string to update at the start
133 * @slen: Pointer to current string length, updated on success
134 * @ssize: Size of string buffer
135 * @old: Old string to find in the buffer (no terminator needed)
136 * @olen: Length of @old excluding terminator
137 * @new: New string to replace @old with
138 * @nlen: Length of @new excluding terminator
139 * @return pointer to immediately after the copied @new in @string, or NULL if
140 * no replacement took place
142 static char *substitute(char *string, int *slen, int ssize,
143 const char *old, int olen, const char *new, int nlen)
145 char *p = memstr(string, *slen, old, olen);
150 debug("## Match at pos %ld: match len %d, subst len %d\n",
151 (long)(p - string), olen, nlen);
153 /* make sure replacement matches */
154 if (*slen + nlen - olen > ssize) {
155 printf("## error: substitution buffer overflow\n");
159 /* move tail if needed */
163 len = (olen > nlen) ? olen : nlen;
165 tail = ssize - (p + len - string);
167 debug("## tail len %d\n", tail);
169 memmove(p + nlen, p + olen, tail);
172 /* insert substitute */
173 memcpy(p, new, nlen);
175 *slen += nlen - olen;
180 int setexpr_regex_sub(char *data, uint data_size, char *nbuf, uint nbuf_size,
181 const char *r, const char *s, bool global)
185 int res, len, nlen, loop;
187 if (slre_compile(&slre, r) == 0) {
188 printf("Error compiling regex: %s\n", slre.err_str);
193 for (loop = 0;; loop++) {
194 struct cap caps[slre.num_caps + 2];
199 (void) memset(caps, 0, sizeof(caps));
201 res = slre_match(&slre, datap, len - (datap - data), caps);
203 debug("Result: %d\n", res);
205 for (i = 0; i <= slre.num_caps; i++) {
206 if (caps[i].len > 0) {
207 debug("Substring %d: [%.*s]\n", i,
208 caps[i].len, caps[i].ptr);
214 printf("%s: No match\n", data);
221 debug("## MATCH ## %s\n", data);
230 if (nlen + 1 >= nbuf_size) {
231 printf("## error: pattern buffer overflow: have %d, need %d\n",
232 nbuf_size, nlen + 1);
237 debug("## SUBST(1) ## %s\n", nbuf);
240 * Handle back references
242 * Support for \0 ... \9, where \0 is the
243 * whole matched pattern (similar to &).
245 * Implementation is a bit simpleminded as
246 * backrefs are substituted sequentially, one
247 * by one. This will lead to somewhat
248 * unexpected results if the replacement
249 * strings contain any \N strings then then
250 * may get substitued, too. We accept this
251 * restriction for the sake of simplicity.
253 for (i = 0; i < 10; ++i) {
259 if (caps[i].len == 0)
264 debug("## BACKREF %d: replace \"%.*s\" by \"%.*s\" in \"%s\"\n",
267 caps[i].len, caps[i].ptr,
271 char *p = memstr(np, nlen, backref, 2);
276 np = substitute(np, &nlen,
277 nbuf_size - (np - nbuf),
279 caps[i].ptr, caps[i].len);
285 debug("## SUBST(2) ## %s\n", nbuf);
287 datap = substitute(datap, &len, data_size - (datap - data),
288 old, olen, nbuf, nlen);
293 debug("## REMAINDER: %s\n", datap);
295 debug("## RESULT: %s\n", data);
300 debug("## FINAL (now env_set()) : %s\n", data);
305 #define SLRE_BUFSZ 16384
306 #define SLRE_PATSZ 4096
309 * Perform regex operations on a environment variable
311 * Returns 0 if OK, 1 in case of errors.
313 static int regex_sub_var(const char *name, const char *r, const char *s,
314 const char *t, int global)
317 char data[SLRE_BUFSZ];
318 char nbuf[SLRE_PATSZ];
326 if (slre_compile(&slre, r) == 0) {
327 printf("Error compiling regex: %s\n", slre.err_str);
332 value = env_get(name);
334 printf("## Error: variable \"%s\" not defined\n", name);
340 debug("REGEX on %s=%s\n", name, t);
341 debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n", r, s ? s : "<NULL>",
345 if (len + 1 > SLRE_BUFSZ) {
346 printf("## error: subst buffer overflow: have %d, need %d\n",
347 SLRE_BUFSZ, len + 1);
353 ret = setexpr_regex_sub(data, SLRE_BUFSZ, nbuf, SLRE_PATSZ, r, s,
358 printf("%s=%s\n", name, data);
360 return env_set(name, data);
364 static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
367 struct expr_arg aval, bval;
373 * We take 3, 5, or 6 arguments:
374 * 3 : setexpr name value
375 * 5 : setexpr name val1 op val2
376 * setexpr name [g]sub r s
377 * 6 : setexpr name [g]sub r s t
380 /* > 6 already tested by max command args */
381 if ((argc < 3) || (argc == 4))
382 return CMD_RET_USAGE;
384 w = cmd_get_data_size(argv[0], 4);
386 if (get_arg(argv[2], w, &aval))
387 return CMD_RET_FAILURE;
389 /* plain assignment: "setexpr name value" */
391 if (w == CMD_DATA_SIZE_STR) {
392 ret = env_set(argv[1], aval.sval);
395 ret = env_set_hex(argv[1], aval.ival);
401 /* 5 or 6 args (6 args only with [g]sub) */
404 * rexep handling: "setexpr name [g]sub r s [t]"
405 * with 5 args, "t" will be NULL
407 if (strcmp(argv[2], "gsub") == 0)
408 return regex_sub_var(argv[1], argv[3], argv[4], argv[5], 1);
410 if (strcmp(argv[2], "sub") == 0)
411 return regex_sub_var(argv[1], argv[3], argv[4], argv[5], 0);
414 /* standard operators: "setexpr name val1 op val2" */
416 return CMD_RET_USAGE;
418 if (strlen(argv[3]) != 1)
419 return CMD_RET_USAGE;
421 if (get_arg(argv[4], w, &bval)) {
422 if (w == CMD_DATA_SIZE_STR)
424 return CMD_RET_FAILURE;
427 if (w == CMD_DATA_SIZE_STR) {
431 switch (argv[3][0]) {
433 len = strlen(aval.sval) + strlen(bval.sval) + 1;
436 printf("Out of memory\n");
437 ret = CMD_RET_FAILURE;
439 /* These were copied out and checked earlier */
440 strcpy(str, aval.sval);
441 strcat(str, bval.sval);
442 ret = env_set(argv[1], str);
444 printf("Could not set var\n");
449 printf("invalid op\n");
456 switch (argv[3][0]) {
482 printf("invalid op\n");
486 env_set_hex(argv[1], value);
489 if (w == CMD_DATA_SIZE_STR) {
498 setexpr, 6, 0, do_setexpr,
499 "set environment variable as the result of eval expression",
500 "[.b, .w, .l, .s] name [*]value1 <op> [*]value2\n"
501 " - set environment variable 'name' to the result of the evaluated\n"
502 " expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n"
503 " (for strings only + is supported)\n"
504 " size argument is only meaningful if value1 and/or value2 are\n"
505 " memory addresses (*)\n"
506 "setexpr[.b, .w, .l] name [*]value\n"
507 " - load a value into a variable"
510 "setexpr name gsub r s [t]\n"
511 " - For each substring matching the regular expression <r> in the\n"
512 " string <t>, substitute the string <s>. The result is\n"
513 " assigned to <name>. If <t> is not supplied, use the old\n"
515 "setexpr name sub r s [t]\n"
516 " - Just like gsub(), but replace only the first matching substring"