hush: implement brace expansion
authorDenys Vlasenko <vda.linux@googlemail.com>
Sun, 3 Oct 2010 12:28:04 +0000 (14:28 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 3 Oct 2010 12:28:04 +0000 (14:28 +0200)
When enabled:

function                                             old     new   delta
glob_brace                                             -     402    +402
next_brace_sub                                         -      70     +70
expand_on_ifs                                        185     231     +46
bbconfig_config_bz2                                 4923    4929      +6
o_save_ptr                                           282     140    -142
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 2/1 up/down: 524/-142)          Total: 382 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/hush.c

index 3b9362e10227dd8fa13a915fba3c5d5faa73c04d..5b73f0c53e862aea70f8f510c9dbbe7b1058daa4 100644 (file)
 //config:      help
 //config:        Enable bash-compatible extensions.
 //config:
+//config:config HUSH_BRACE_EXPANSION
+//config:      bool "Brace expansion"
+//config:      default y
+//config:      depends on HUSH_BASH_COMPAT
+//config:      help
+//config:        Enable {abc,def} extension.
+//config:
 //config:config HUSH_HELP
 //config:      bool "help builtin"
 //config:      default y
@@ -2000,7 +2007,6 @@ static void o_addstr_with_NUL(o_string *o, const char *str)
        o_addblock(o, str, strlen(str) + 1);
 }
 
-#undef HUSH_BRACE_EXPANSION
 /*
  * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side.
  * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v.
@@ -2012,7 +2018,7 @@ static void o_addstr_with_NUL(o_string *o, const char *str)
  * We have only second one.
  */
 
-#ifdef HUSH_BRACE_EXPANSION
+#if ENABLE_HUSH_BRACE_EXPANSION
 # define MAYBE_BRACES "{}"
 #else
 # define MAYBE_BRACES ""
@@ -2180,7 +2186,7 @@ static int o_get_last_ptr(o_string *o, int n)
        return ((int)(uintptr_t)list[n-1]) + string_start;
 }
 
-#ifdef HUSH_BRACE_EXPANSION
+#if ENABLE_HUSH_BRACE_EXPANSION
 /* There in a GNU extension, GLOB_BRACE, but it is not usable:
  * first, it processes even {a} (no commas), second,
  * I didn't manage to make it return strings when they don't match
@@ -4375,8 +4381,17 @@ static int process_command_subs(o_string *dest, const char *s);
 static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len)
 {
        while (--len >= 0) {
-               o_addchr(o, *str);
-               if (*str++ == '\\') {
+               char c = *str++;
+#if ENABLE_HUSH_BRACE_EXPANSION
+               if (c == '{' || c == '}') {
+                       /* { -> \{, } -> \} */
+                       o_addchr(o, '\\');
+                       o_addchr(o, c);
+                       continue;
+               }
+#endif
+               o_addchr(o, c);
+               if (c == '\\') {
                        /* \z -> \\\z; \<eol> -> \\<eol> */
                        o_addchr(o, '\\');
                        if (len) {