]> git.kernelconcepts.de Git - karo-tx-uboot.git/commit
setexpr: add regex substring matching and substitution
authorWolfgang Denk <wd@denx.de>
Sat, 23 Mar 2013 23:50:34 +0000 (23:50 +0000)
committerTom Rini <trini@ti.com>
Wed, 1 May 2013 20:24:01 +0000 (16:24 -0400)
commit855f18ea0e6ff83ec9ed5af74e82bc4bff30867d
tree2f1e664ce52eafdf3f3099437d4825fb8b774549
parent103c94b10481cf8479c9d0356e5202bc9200a04f
setexpr: add regex substring matching and substitution

Add "setexpr name gsub r s [t]" and "setexpr name sub r s [t]"
commands which implement substring matching for the regular
expression <r> in the string <t>, and substitution of the string <s>.
The result is assigned to the environment variable <name>.  If <t> is
not supplied, the previous value of <name> is used instead.  "gsub"
performs global substitution, while "sub" will replace only the first
substring.

Both commands are closely modeled after the gawk functions with the
same names.

Examples:

- Generate broadcast address by substituting the last two numbers of
  the IP address by "255.255":

   => print ipaddr
ipaddr=192.168.1.104
=> setexpr broadcast sub "(.*\\.).*\\..*" "\\1255.255" $ipaddr
broadcast=192.168.255.255

- Depending on keyboard configuration (German vs. US keyboard) a
  barcode scanner may initialize the MAC address as C0:E5:4E:02:06:DC
  or as C0>E5>4E>02>06>DC.  Make sure we always have a correct value:

=> print ethaddr
ethaddr=C0>E5>4E>02>06>DC
=> setexpr ethaddr gsub > :
ethaddr=C0:E5:4E:02:06:DC

- Do the same, but substitute one step at a time in a loop until no
  futher matches:

=> setenv ethaddr C0>E5>4E>02>06>DC
=> while setexpr ethaddr sub > :
> do
> echo -----
> done
ethaddr=C0:E5>4E>02>06>DC
-----
ethaddr=C0:E5:4E>02>06>DC
-----
ethaddr=C0:E5:4E:02>06>DC
-----
ethaddr=C0:E5:4E:02:06>DC
-----
ethaddr=C0:E5:4E:02:06:DC
-----
C0:E5:4E:02:06:DC: No match
=> print ethaddr
ethaddr=C0:E5:4E:02:06:DC

etc.

To enable this feature, the CONFIG_REGEX option has to be defined in
the board config file.

Signed-off-by: Wolfgang Denk <wd@denx.de>
common/cmd_setexpr.c