hush: tackling "local" in functions

Denys Vlasenko vda.linux at googlemail.com
Wed Jun 3 10:10:31 UTC 2009


On Thu, May 28, 2009 at 8:11 AM, Mike Frysinger <vapier at gentoo.org> wrote:
> about the only major thing i see missing in hush after testing on random
> packages out there is the "local" keyword.
>
> x=1
> f() { local x=$((x + 3)) ; echo $x ; }
> g() { local x=$((x + 1)) ; f ; echo $x ; }
> g
> echo $x
>
> the keyword causes the variable to only apply to children of the active
> function scope.  so here, we should see:
> 5 - global 1 added to g()'s 1 added to f()'s 3
> 2 - global 1 added to g()'s 1 unaffected by f()
> 1 - global 1 unaffected by g() or f()
>
> another example:
> x=1
> f() { x=2; }
> g() { local x=3; f; echo $x; }
> g
> we see the x is declared local to g()'s scope, and f() modifies it since it is
> called inside that scope.
>
> based on these needs, i'm thinking:
>  - new variable state for set_local_var()
>  - when a variable is marked local and it shadows an existing variable, we
> insert it into the linked list of variables before the existing copy
>  - at the end of run_and_free_list(), we walk the variable list destroying all
> variables marked local

Please try attached patch.
--
vda
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 2.patch
Type: text/x-patch
Size: 14216 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20090603/3e10e3f4/attachment.bin>


More information about the busybox mailing list