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