]> Git Repo - u-boot.git/blame - cmd/setexpr.c
Merge branch 'next' of https://gitlab.denx.de/u-boot/custodians/u-boot-x86 into next
[u-boot.git] / cmd / setexpr.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
d058698f
KG
2/*
3 * Copyright 2008 Freescale Semiconductor, Inc.
855f18ea 4 * Copyright 2013 Wolfgang Denk <[email protected]>
d058698f
KG
5 */
6
7/*
8 * This file provides a shell like 'expr' function to return.
9 */
10
11#include <common.h>
12#include <config.h>
13#include <command.h>
c7694dd4 14#include <env.h>
f7ae49fc 15#include <log.h>
2068cea1 16#include <mapmem.h>
d058698f 17
47ab5ad1
FM
18static ulong get_arg(char *s, int w)
19{
482126e2 20 /*
2068cea1
JH
21 * If the parameter starts with a '*' then assume it is a pointer to
22 * the value we want.
47ab5ad1 23 */
47ab5ad1 24 if (s[0] == '*') {
2068cea1
JH
25 ulong *p;
26 ulong addr;
27 ulong val;
28
29 addr = simple_strtoul(&s[1], NULL, 16);
47ab5ad1 30 switch (w) {
2068cea1
JH
31 case 1:
32 p = map_sysmem(addr, sizeof(uchar));
33 val = (ulong)*(uchar *)p;
34 unmap_sysmem(p);
35 return val;
36 case 2:
37 p = map_sysmem(addr, sizeof(ushort));
38 val = (ulong)*(ushort *)p;
39 unmap_sysmem(p);
40 return val;
47ab5ad1 41 case 4:
2068cea1
JH
42 default:
43 p = map_sysmem(addr, sizeof(ulong));
44 val = *p;
45 unmap_sysmem(p);
46 return val;
47ab5ad1
FM
47 }
48 } else {
49 return simple_strtoul(s, NULL, 16);
50 }
51}
52
855f18ea
WD
53#ifdef CONFIG_REGEX
54
55#include <slre.h>
56
57#define SLRE_BUFSZ 16384
58#define SLRE_PATSZ 4096
59
60/*
61 * memstr - Find the first substring in memory
62 * @s1: The string to be searched
63 * @s2: The string to search for
64 *
65 * Similar to and based on strstr(),
66 * but strings do not need to be NUL terminated.
67 */
68static char *memstr(const char *s1, int l1, const char *s2, int l2)
69{
70 if (!l2)
71 return (char *)s1;
72
73 while (l1 >= l2) {
74 l1--;
75 if (!memcmp(s1, s2, l2))
76 return (char *)s1;
77 s1++;
78 }
79 return NULL;
80}
81
82static char *substitute(char *string, /* string buffer */
83 int *slen, /* current string length */
84 int ssize, /* string bufer size */
85 const char *old,/* old (replaced) string */
86 int olen, /* length of old string */
87 const char *new,/* new (replacement) string */
88 int nlen) /* length of new string */
89{
90 char *p = memstr(string, *slen, old, olen);
91
92 if (p == NULL)
93 return NULL;
94
95 debug("## Match at pos %ld: match len %d, subst len %d\n",
96 (long)(p - string), olen, nlen);
97
98 /* make sure replacement matches */
99 if (*slen + nlen - olen > ssize) {
100 printf("## error: substitution buffer overflow\n");
101 return NULL;
102 }
103
104 /* move tail if needed */
105 if (olen != nlen) {
106 int tail, len;
107
108 len = (olen > nlen) ? olen : nlen;
109
110 tail = ssize - (p + len - string);
111
112 debug("## tail len %d\n", tail);
113
114 memmove(p + nlen, p + olen, tail);
115 }
116
117 /* insert substitue */
118 memcpy(p, new, nlen);
119
120 *slen += nlen - olen;
121
122 return p + nlen;
123}
124
125/*
126 * Perform regex operations on a environment variable
127 *
128 * Returns 0 if OK, 1 in case of errors.
129 */
130static int regex_sub(const char *name,
131 const char *r, const char *s, const char *t,
132 int global)
133{
134 struct slre slre;
135 char data[SLRE_BUFSZ];
136 char *datap = data;
137 const char *value;
138 int res, len, nlen, loop;
139
140 if (name == NULL)
141 return 1;
142
143 if (slre_compile(&slre, r) == 0) {
144 printf("Error compiling regex: %s\n", slre.err_str);
145 return 1;
146 }
147
148 if (t == NULL) {
00caae6d 149 value = env_get(name);
855f18ea
WD
150
151 if (value == NULL) {
152 printf("## Error: variable \"%s\" not defined\n", name);
153 return 1;
154 }
155 t = value;
156 }
157
158 debug("REGEX on %s=%s\n", name, t);
159 debug("REGEX=\"%s\", SUBST=\"%s\", GLOBAL=%d\n",
160 r, s ? s : "<NULL>", global);
161
162 len = strlen(t);
163 if (len + 1 > SLRE_BUFSZ) {
164 printf("## error: subst buffer overflow: have %d, need %d\n",
165 SLRE_BUFSZ, len + 1);
166 return 1;
167 }
168
169 strcpy(data, t);
170
171 if (s == NULL)
172 nlen = 0;
173 else
174 nlen = strlen(s);
175
176 for (loop = 0;; loop++) {
177 struct cap caps[slre.num_caps + 2];
178 char nbuf[SLRE_PATSZ];
179 const char *old;
180 char *np;
181 int i, olen;
182
183 (void) memset(caps, 0, sizeof(caps));
184
185 res = slre_match(&slre, datap, len, caps);
186
187 debug("Result: %d\n", res);
188
189 for (i = 0; i < slre.num_caps; i++) {
190 if (caps[i].len > 0) {
191 debug("Substring %d: [%.*s]\n", i,
192 caps[i].len, caps[i].ptr);
193 }
194 }
195
196 if (res == 0) {
197 if (loop == 0) {
198 printf("%s: No match\n", t);
199 return 1;
200 } else {
201 break;
202 }
203 }
204
205 debug("## MATCH ## %s\n", data);
206
207 if (s == NULL) {
208 printf("%s=%s\n", name, t);
209 return 1;
210 }
211
212 old = caps[0].ptr;
213 olen = caps[0].len;
214
215 if (nlen + 1 >= SLRE_PATSZ) {
216 printf("## error: pattern buffer overflow: have %d, need %d\n",
217 SLRE_BUFSZ, nlen + 1);
218 return 1;
219 }
220 strcpy(nbuf, s);
221
222 debug("## SUBST(1) ## %s\n", nbuf);
223
224 /*
225 * Handle back references
226 *
227 * Support for \0 ... \9, where \0 is the
228 * whole matched pattern (similar to &).
229 *
230 * Implementation is a bit simpleminded as
231 * backrefs are substituted sequentially, one
232 * by one. This will lead to somewhat
233 * unexpected results if the replacement
234 * strings contain any \N strings then then
235 * may get substitued, too. We accept this
236 * restriction for the sake of simplicity.
237 */
238 for (i = 0; i < 10; ++i) {
239 char backref[2] = {
240 '\\',
241 '0',
242 };
243
244 if (caps[i].len == 0)
245 break;
246
247 backref[1] += i;
248
249 debug("## BACKREF %d: replace \"%.*s\" by \"%.*s\" in \"%s\"\n",
250 i,
251 2, backref,
252 caps[i].len, caps[i].ptr,
253 nbuf);
254
255 for (np = nbuf;;) {
256 char *p = memstr(np, nlen, backref, 2);
257
258 if (p == NULL)
259 break;
260
261 np = substitute(np, &nlen,
262 SLRE_PATSZ,
263 backref, 2,
264 caps[i].ptr, caps[i].len);
265
266 if (np == NULL)
267 return 1;
268 }
269 }
270 debug("## SUBST(2) ## %s\n", nbuf);
271
272 datap = substitute(datap, &len, SLRE_BUFSZ,
273 old, olen,
274 nbuf, nlen);
275
276 if (datap == NULL)
277 return 1;
278
279 debug("## REMAINDER: %s\n", datap);
280
281 debug("## RESULT: %s\n", data);
282
283 if (!global)
284 break;
285 }
382bee57 286 debug("## FINAL (now env_set()) : %s\n", data);
855f18ea
WD
287
288 printf("%s=%s\n", name, data);
289
382bee57 290 return env_set(name, data);
855f18ea
WD
291}
292#endif
293
09140113
SG
294static int do_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
295 char *const argv[])
d058698f
KG
296{
297 ulong a, b;
41ef372c 298 ulong value;
47ab5ad1 299 int w;
d058698f 300
855f18ea
WD
301 /*
302 * We take 3, 5, or 6 arguments:
303 * 3 : setexpr name value
304 * 5 : setexpr name val1 op val2
305 * setexpr name [g]sub r s
306 * 6 : setexpr name [g]sub r s t
307 */
308
309 /* > 6 already tested by max command args */
310 if ((argc < 3) || (argc == 4))
4c12eeb8 311 return CMD_RET_USAGE;
d058698f 312
47ab5ad1
FM
313 w = cmd_get_data_size(argv[0], 4);
314
315 a = get_arg(argv[2], w);
4823b45d 316
103c94b1 317 /* plain assignment: "setexpr name value" */
4823b45d 318 if (argc == 3) {
018f5303 319 env_set_hex(argv[1], a);
4823b45d
JH
320 return 0;
321 }
322
855f18ea
WD
323 /* 5 or 6 args (6 args only with [g]sub) */
324#ifdef CONFIG_REGEX
325 /*
326 * rexep handling: "setexpr name [g]sub r s [t]"
327 * with 5 args, "t" will be NULL
328 */
329 if (strcmp(argv[2], "gsub") == 0)
330 return regex_sub(argv[1], argv[3], argv[4], argv[5], 1);
331
332 if (strcmp(argv[2], "sub") == 0)
333 return regex_sub(argv[1], argv[3], argv[4], argv[5], 0);
334#endif
335
103c94b1
WD
336 /* standard operators: "setexpr name val1 op val2" */
337 if (argc != 5)
338 return CMD_RET_USAGE;
339
340 if (strlen(argv[3]) != 1)
341 return CMD_RET_USAGE;
342
47ab5ad1 343 b = get_arg(argv[4], w);
d058698f
KG
344
345 switch (argv[3][0]) {
41ef372c
SG
346 case '|':
347 value = a | b;
348 break;
349 case '&':
350 value = a & b;
351 break;
352 case '+':
353 value = a + b;
354 break;
355 case '^':
356 value = a ^ b;
357 break;
358 case '-':
359 value = a - b;
360 break;
361 case '*':
362 value = a * b;
363 break;
364 case '/':
365 value = a / b;
366 break;
367 case '%':
368 value = a % b;
369 break;
d058698f
KG
370 default:
371 printf("invalid op\n");
372 return 1;
373 }
374
018f5303 375 env_set_hex(argv[1], value);
d058698f
KG
376
377 return 0;
378}
379
380U_BOOT_CMD(
855f18ea 381 setexpr, 6, 0, do_setexpr,
2fb2604d 382 "set environment variable as the result of eval expression",
4823b45d 383 "[.b, .w, .l] name [*]value1 <op> [*]value2\n"
d058698f 384 " - set environment variable 'name' to the result of the evaluated\n"
855f18ea 385 " expression specified by <op>. <op> can be &, |, ^, +, -, *, /, %\n"
4823b45d
JH
386 " size argument is only meaningful if value1 and/or value2 are\n"
387 " memory addresses (*)\n"
103c94b1
WD
388 "setexpr[.b, .w, .l] name [*]value\n"
389 " - load a value into a variable"
855f18ea
WD
390#ifdef CONFIG_REGEX
391 "\n"
392 "setexpr name gsub r s [t]\n"
393 " - For each substring matching the regular expression <r> in the\n"
394 " string <t>, substitute the string <s>. The result is\n"
395 " assigned to <name>. If <t> is not supplied, use the old\n"
396 " value of <name>\n"
397 "setexpr name sub r s [t]\n"
398 " - Just like gsub(), but replace only the first matching substring"
399#endif
d058698f 400);
This page took 0.356206 seconds and 4 git commands to generate.