[U-Boot] [PATCH 2/2] env: add regex support for environment variables

Detlev Zundel dzu at denx.de
Mon Nov 7 12:07:42 CET 2011


Hi Wolfgang,

this really is an interesting addition!

> Syntax:  env regex [-g] [-s subst] regex name [...]
>
> The code is based on SLRE (http://slre.sourceforge.net/)
> which provides a tiny subset of Perl regular expressions.
>
> Without options, this will implement regex pattern matching on
> environment variables.  Variables with matching values will be printd
> as with "env print", so this basicly performs a "grep" on the given
> list of variables.

Ok, this usage looks fine.

> With "-s subst", the matching pattern gets replaced with the string
> given in "subst".  Back references '\0' ... '\9' are allowed, where
> '\0' stands for the whole matched string, and '\1', '\2', ... are
> replaced with the first, second, ... sub-pattern.
>
> "-g" allows for global replacement.

But IMHO this usage doesn't really belong into the "env" command.  It
much rather is a further operation of the setexpr command:

  set environment variable as the result of eval expression",
  name value1 <op> value2\n"
      - set environment variable 'name' to the result of the evaluated\n"
        express specified by <op>.  <op> can be &, |, ^, +, -, *, /, %"


We could add the <op>erations for regsubst and regsubstg.  For actual
names for the operations, I'm somewhat unsure.  Maybe "function like",
i.e. "regsubst(string, pattern, replacement)" and "regsubstg(string,
pattern, replacement)"?

> Examples:
> 	=> setenv foo abcdefghijklmnop
> 	=> env reg 'A' '[bdgmo]' foo
> 	=> env reg -s 'A' '[bdgmo]' foo
> 	foo=aAcdefghijklmnop
> 	=> env reg -g -s 'B' '[bdgmo]' foo
> 	foo=aAcBefBhijklBnBp
> 	=> env reg -g -s '\\2--\\1' '(Be).*(kl)' foo
> 	foo=aAckl--BeBnBp

So I'd vote for

=> setenv result regsubst($foo, '[bdgmo]', 'A')

What do you think?

Cheers
  Detlev

-- 
To you I'm an atheist; to God, I'm the Loyal Opposition.
                                        -- Woody Allen
--
DENX Software Engineering GmbH,      MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich,  Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-40 Fax: (+49)-8142-66989-80 Email: dzu at denx.de


More information about the U-Boot mailing list