]>
git.ipfire.org Git - people/ms/u-boot.git/blob - cmd/setexpr.c
2 * Copyright 2008 Freescale Semiconductor, Inc.
3 * Copyright 2013 Wolfgang Denk <wd@denx.de>
5 * SPDX-License-Identifier: GPL-2.0+
9 * This file provides a shell like 'expr' function to return.
17 static ulong
get_arg(char *s
, int w
)
20 * If the parameter starts with a '*' then assume it is a pointer to
28 addr
= simple_strtoul(&s
[1], NULL
, 16);
31 p
= map_sysmem(addr
, sizeof(uchar
));
32 val
= (ulong
)*(uchar
*)p
;
36 p
= map_sysmem(addr
, sizeof(ushort
));
37 val
= (ulong
)*(ushort
*)p
;
42 p
= map_sysmem(addr
, sizeof(ulong
));
48 return simple_strtoul(s
, NULL
, 16);
56 #define SLRE_BUFSZ 16384
57 #define SLRE_PATSZ 4096
60 * memstr - Find the first substring in memory
61 * @s1: The string to be searched
62 * @s2: The string to search for
64 * Similar to and based on strstr(),
65 * but strings do not need to be NUL terminated.
67 static char *memstr(const char *s1
, int l1
, const char *s2
, int l2
)
74 if (!memcmp(s1
, s2
, l2
))
81 static char *substitute(char *string
, /* string buffer */
82 int *slen
, /* current string length */
83 int ssize
, /* string bufer size */
84 const char *old
,/* old (replaced) string */
85 int olen
, /* length of old string */
86 const char *new,/* new (replacement) string */
87 int nlen
) /* length of new string */
89 char *p
= memstr(string
, *slen
, old
, olen
);
94 debug("## Match at pos %ld: match len %d, subst len %d\n",
95 (long)(p
- string
), olen
, nlen
);
97 /* make sure replacement matches */
98 if (*slen
+ nlen
- olen
> ssize
) {
99 printf("## error: substitution buffer overflow\n");
103 /* move tail if needed */
107 len
= (olen
> nlen
) ? olen
: nlen
;
109 tail
= ssize
- (p
+ len
- string
);
111 debug("## tail len %d\n", tail
);
113 memmove(p
+ nlen
, p
+ olen
, tail
);
116 /* insert substitue */
117 memcpy(p
, new, nlen
);
119 *slen
+= nlen
- olen
;
125 * Perform regex operations on a environment variable
127 * Returns 0 if OK, 1 in case of errors.
129 static int regex_sub(const char *name
,
130 const char *r
, const char *s
, const char *t
,
134 char data
[SLRE_BUFSZ
];
137 int res
, len
, nlen
, loop
;
142 if (slre_compile(&slre
, r
) == 0) {
143 printf("Error compiling regex: %s\n", slre
.err_str
);
148 value
= getenv(name
);
151 printf("## Error: variable \"%s\" not defined\n", name
);
157 debug("REGEX on %s=%s\n", name
, t
);
158 debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n",
159 r
, s
? s
: "<NULL>", global
);
162 if (len
+ 1 > SLRE_BUFSZ
) {
163 printf("## error: subst buffer overflow: have %d, need %d\n",
164 SLRE_BUFSZ
, len
+ 1);
175 for (loop
= 0;; loop
++) {
176 struct cap caps
[slre
.num_caps
+ 2];
177 char nbuf
[SLRE_PATSZ
];
182 (void) memset(caps
, 0, sizeof(caps
));
184 res
= slre_match(&slre
, datap
, len
, caps
);
186 debug("Result: %d\n", res
);
188 for (i
= 0; i
< slre
.num_caps
; i
++) {
189 if (caps
[i
].len
> 0) {
190 debug("Substring %d: [%.*s]\n", i
,
191 caps
[i
].len
, caps
[i
].ptr
);
197 printf("%s: No match\n", t
);
204 debug("## MATCH ## %s\n", data
);
207 printf("%s=%s\n", name
, t
);
214 if (nlen
+ 1 >= SLRE_PATSZ
) {
215 printf("## error: pattern buffer overflow: have %d, need %d\n",
216 SLRE_BUFSZ
, nlen
+ 1);
221 debug("## SUBST(1) ## %s\n", nbuf
);
224 * Handle back references
226 * Support for \0 ... \9, where \0 is the
227 * whole matched pattern (similar to &).
229 * Implementation is a bit simpleminded as
230 * backrefs are substituted sequentially, one
231 * by one. This will lead to somewhat
232 * unexpected results if the replacement
233 * strings contain any \N strings then then
234 * may get substitued, too. We accept this
235 * restriction for the sake of simplicity.
237 for (i
= 0; i
< 10; ++i
) {
243 if (caps
[i
].len
== 0)
248 debug("## BACKREF %d: replace \"%.*s\" by \"%.*s\" in \"%s\"\n",
251 caps
[i
].len
, caps
[i
].ptr
,
255 char *p
= memstr(np
, nlen
, backref
, 2);
260 np
= substitute(np
, &nlen
,
263 caps
[i
].ptr
, caps
[i
].len
);
269 debug("## SUBST(2) ## %s\n", nbuf
);
271 datap
= substitute(datap
, &len
, SLRE_BUFSZ
,
278 debug("## REMAINDER: %s\n", datap
);
280 debug("## RESULT: %s\n", data
);
285 debug("## FINAL (now setenv()) : %s\n", data
);
287 printf("%s=%s\n", name
, data
);
289 return setenv(name
, data
);
293 static int do_setexpr(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
300 * We take 3, 5, or 6 arguments:
301 * 3 : setexpr name value
302 * 5 : setexpr name val1 op val2
303 * setexpr name [g]sub r s
304 * 6 : setexpr name [g]sub r s t
307 /* > 6 already tested by max command args */
308 if ((argc
< 3) || (argc
== 4))
309 return CMD_RET_USAGE
;
311 w
= cmd_get_data_size(argv
[0], 4);
313 a
= get_arg(argv
[2], w
);
315 /* plain assignment: "setexpr name value" */
317 setenv_hex(argv
[1], a
);
321 /* 5 or 6 args (6 args only with [g]sub) */
324 * rexep handling: "setexpr name [g]sub r s [t]"
325 * with 5 args, "t" will be NULL
327 if (strcmp(argv
[2], "gsub") == 0)
328 return regex_sub(argv
[1], argv
[3], argv
[4], argv
[5], 1);
330 if (strcmp(argv
[2], "sub") == 0)
331 return regex_sub(argv
[1], argv
[3], argv
[4], argv
[5], 0);
334 /* standard operators: "setexpr name val1 op val2" */
336 return CMD_RET_USAGE
;
338 if (strlen(argv
[3]) != 1)
339 return CMD_RET_USAGE
;
341 b
= get_arg(argv
[4], w
);
343 switch (argv
[3][0]) {
369 printf("invalid op\n");
373 setenv_hex(argv
[1], value
);
379 setexpr
, 6, 0, do_setexpr
,
380 "set environment variable as the result of eval expression",
381 "[.b, .w, .l] name [*]value1 <op> [*]value2\n"
382 " - set environment variable 'name' to the result of the evaluated\n"
383 " expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n"
384 " size argument is only meaningful if value1 and/or value2 are\n"
385 " memory addresses (*)\n"
386 "setexpr[.b, .w, .l] name [*]value\n"
387 " - load a value into a variable"
390 "setexpr name gsub r s [t]\n"
391 " - For each substring matching the regular expression <r> in the\n"
392 " string <t>, substitute the string <s>. The result is\n"
393 " assigned to <name>. If <t> is not supplied, use the old\n"
395 "setexpr name sub r s [t]\n"
396 " - Just like gsub(), but replace only the first matching substring"