[PATCH] Ash support for replace and subsitution

Denys Vlasenko vda.linux at googlemail.com
Tue Mar 25 01:17:41 UTC 2008


On Monday 24 March 2008 17:17, James Simmons wrote:
> 
> diff -urwN busybox-1.10.0.orig/shell/ash.c busybox-1.10.0/shell/ash.c
> --- busybox-1.10.0.orig/shell/ash.c	2008-03-24 11:58:50.000000000 -0400
> +++ busybox-1.10.0/shell/ash.c	2008-03-24 12:28:26.000000000 -0400
> @@ -476,6 +476,9 @@
>  #define VSTRIMLEFT      0x8             /* ${var#pattern} */
>  #define VSTRIMLEFTMAX   0x9             /* ${var##pattern} */
>  #define VSLENGTH        0xa             /* ${#var} */
> +#define VSSUBSTR	0xc		/* ${var:position:length} */
> +#define VSREPLACE	0xd		/* ${var/pattern/replacement} */
> +#define VSREPLACEALL	0xe		/* ${var//pattern/replacement} */

If it really works as advertised, it's very nice!

I want to frame it by ENABLE_FEATURE_BASH_COMPAT
and add testsuite entries for new (and old, but not covered
by testsuite) functionalty.

I'm scared of breaking ash in particular. It's much bigger
than my small brain. Other applets are easy.

+               if (*loc++ == ':') {
+                       len = atoi(loc);
+               } else {
+                       len = orig_len;
+                       while (*loc && *loc != ':')
+                               loc++;
+                       if (*loc++ == ':')
+                               len = atoi(loc);

atoi()... can you convert to bb_strtou(), and abort on bad input.
bash does this:

bash-3.2# bash -c 'echo ${PATH:2:3r}'
bash: PATH: 3r: value too great for base (error token is "3r")
bash-3.2# bash -c 'echo ${PATH:2:3%}'
bash: PATH: 3%: syntax error: operand expected (error token is "%")
bash-3.2# bash -c 'echo ${PATH:2:-3}'
bash: -3: substring expression < 0

(You do not need to match error message _text_).

I am going to apply attached patch to svn.

BASH_COMPAT off:
function                                             old     new   delta
scanleft                                             101     262    +161
subevalvar                                           346     335     -11
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 161/-11)           Total: 150 bytes

BASH_COMPAT on:
function                                             old     new   delta
subevalvar                                           346    1397   +1051
scanleft                                             101     262    +161
readtoken1                                          2739    2807     +68
cmdputs                                              397     399      +2
static.vstype                                         64      48     -16
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/1 up/down: 1282/-16)         Total: 1266 bytes
   text    data     bss     dec     hex filename
 797701     641    7380  805722   c4b5a busybox_old
 799025     641    7380  807046   c5086 busybox_unstripped


Thanks a lot!
--
vda

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 5.patch
Type: text/x-diff
Size: 11592 bytes
Desc: not available
Url : http://lists.busybox.net/pipermail/busybox/attachments/20080325/8be25c73/attachment.bin 


More information about the busybox mailing list