hush: Add default value substitution support
PATCH 1/3 hush: Add default value substitution support
Use standard sh syntax:
${VAR:-default}
Use default value: if VAR is set and non-null, expands to $VAR. Otherwise, expands to default.
${VAR:=default}
Set default value: if VAR is set and non-null, expands to $VAR. Otherwise, sets hush VAR to default and expands to default.
${VAR:+default}
If VAR is set and non-null, expands to the empty string. Otherwise, expands to default.
--- common/hush.c | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-)
common/hush.c +40 -3
@@ -2743,13 +2743,50 @@ static int parse_group(o_string *dest, struct p_context *ctx,static char *lookup_param (char *src) {char *p; +char *sep; +char *default_val =NULL ; +int assign =0 ; +int expand_empty =0 ;if (!src)return NULL ; - p =getenv (src); -if (!p) - p =get_local_var (src); + sep =strchr (src,':' ); + +if (sep) { + *sep ='\0' ; +if (*(sep +1 ) =='-' ) + default_val = sep+2 ; +if (*(sep +1 ) =='=' ) { + default_val = sep+2 ; + assign =1 ; + } +if (*(sep +1 ) =='+' ) { + default_val = sep+2 ; + expand_empty =1 ; + } + } + + p =getenv (src); +if (!p) + p =get_local_var (src); + +if (!p ||strlen (p) ==0 ) { + p = default_val; +if (assign) { +char *var =malloc (strlen (src)+strlen (default_val)+2 ); +if (var) { +sprintf (var,"%s=%s" , src, default_val); +set_local_var (var,0 ); + } +free (var); + } + }else if (expand_empty) { + p +=strlen (p); + } + +if (sep) + *sep =':' ;return p; }
Signature · 1 line
1.7.11.5
PATCH 2/3 hush: Don't parse the contents of a dereferenced var
When a variable which contains a user-supplied value is dereferenced (e.g. to be echo'ed), make sure that the value is not further parsed by hush.
Set the hush local variable "HUSH_NO_EVAL=1" to enable this behavior.
Without this patch, a sequence like this occurs:
Panda # env set my_user_string Bob\'s favorite device Panda # print my_user_string my_user_string=Bob's favorite device
Panda # echo $my_user_string
syntax error hush.c:3007
With this patch, it looks like this:
Panda # HUSH_NO_EVAL=1
Panda # env set my_user_string Bob\'s favorite device Panda # print my_user_string my_user_string=Bob's favorite device
Panda # echo $my_user_string
Bob's favorite device
--- common/hush.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 3 deletions(-)
common/hush.c +61 -3
@@ -127,6 +127,7 @@#endif #endif #define SPECIAL_VAR_SYMBOL 03 +#define SUBSTED_VAR_SYMBOL 04#ifndef __U_BOOT__#define FLAG_EXIT_FROM_LOOP 1#define FLAG_PARSE_SEMICOLON (1 << 1)/* symbol ';' is special for parser */
@@ -499,6 +500,7 @@ static void remove_bg_job(struct pipe *pi);/* local variable support */ static char **make_list_in (char **inp,char *name);static char *insert_var_value (char *inp); +static char *insert_var_value_sub (char *inp,int tag_subst);#ifndef __U_BOOT__/* Table of built-in functions. They can be forked or not, depending on
@@ -3088,6 +3090,21 @@ int parse_stream(o_string *dest, struct p_context *ctx,return 1 ;break ;#endif +case SUBSTED_VAR_SYMBOL : + dest->nonnull =1 ; +while (ch =b_getch (input), ch !=EOF && + ch !=SUBSTED_VAR_SYMBOL ) { +debug_printf ("subst, pass=%d\n" , ch); +if (input->__promptme ==0 ) +return 1 ; +b_addchr (dest, ch); + } +debug_printf ("subst, term=%d\n" , ch); +if (ch ==EOF ) { +syntax (); +return 1 ; + } +break ;default :syntax ();/* this is really an internal logic error */ return 1 ;
@@ -3129,6 +3146,10 @@ void update_ifs_map(void)mapset ((uchar *)"\\$'\"`" ,3 );/* never flow through */ mapset ((uchar *)"<>;&|(){}#" ,1 );/* flow through if quoted */ #else + { +uchar subst[2 ] = {SUBSTED_VAR_SYMBOL ,0 }; +mapset (subst,3 );/* never flow through */ + }mapset ((uchar *)"\\$'\"" ,3 );/* never flow through */ mapset ((uchar *)";&|#" ,1 );/* flow through if quoted */ #endif
@@ -3468,25 +3489,57 @@ final_return:static char *insert_var_value (char *inp) { +return insert_var_value_sub (inp,0 ); +} + +static char *insert_var_value_sub (char *inp,int tag_subst) +{int res_str_len =0 ;int len;int done =0 ;char *p, *p1, *res_str =NULL ;while ((p =strchr (inp,SPECIAL_VAR_SYMBOL ))) { +/* check the beginning of the string for normal charachters */ if (p != inp) { +/* copy any charachters to the result string */ len = p - inp; res_str =xrealloc (res_str, (res_str_len + len));strncpy ((res_str + res_str_len), inp, len); res_str_len += len; } inp = ++p; +/* find the ending marker */ p =strchr (inp,SPECIAL_VAR_SYMBOL ); *p ='\0' ; +/* look up the value to substitute */ if ((p1 =lookup_param (inp))) { - len = res_str_len +strlen (p1); +if (tag_subst) + len = res_str_len +strlen (p1) +2 ; +else + len = res_str_len +strlen (p1); res_str =xrealloc (res_str, (1 + len)); -strcpy ((res_str + res_str_len), p1); +if (tag_subst) { +/* +* copy the variable value to the result +* string +*/ +strcpy ((res_str + res_str_len +1 ), p1); + +/* +* mark the replaced text to be accepted as +* is +*/ + res_str[res_str_len] =SUBSTED_VAR_SYMBOL ; + res_str[res_str_len +1 +strlen (p1)] = +SUBSTED_VAR_SYMBOL ; + }else +/* +* copy the variable value to the result +* string +*/ +strcpy ((res_str + res_str_len), p1); + res_str_len = len; } *p =SPECIAL_VAR_SYMBOL ;
@@ -3550,9 +3603,14 @@ static char * make_string(char ** inp)char *str =NULL ;int n;int len =2 ; +char *noeval_str; +int noeval =0 ; + noeval_str =get_local_var ("HUSH_NO_EVAL" ); +if (noeval_str !=NULL && *noeval_str !='0' && *noeval_str !='\0' ) + noeval =1 ;for (n =0 ; inp[n]; n++) { - p =insert_var_value (inp[n]); + p =insert_var_value_sub (inp[n], noeval); str =xrealloc (str, (len +strlen (p)));if (n) {strcat (str," " );
Signature · 1 line
1.7.11.5
Wolfgang Denk
AppliedPATCH 2/3 hush: Don't parse the contents of a dereferenced var
Dear Joe Hershberger,
Quoted 23 lines · In message <[email protected]> you wrote:
When a variable which contains a user-supplied value is dereferenced (e.g. to be echo'ed), make sure that the value is not further parsed by hush.
Set the hush local variable "HUSH_NO_EVAL=1" to enable this behavior.
Without this patch, a sequence like this occurs:
Panda # env set
my_user_stringBob\'s favorite device Panda # printmy_user_stringmy_user_string=Bob's favorite device
Panda # echo $my_user_string
syntax error hush.c:3007With this patch, it looks like this:
Panda # HUSH_NO_EVAL=1
Panda # env setmy_user_stringBob\'s favorite device Panda # printmy_user_stringmy_user_string=Bob's favorite device
Panda # echo $my_user_string
Bob's favorite device
Quoted 4 lines
Signed-off-by Joe Hershberger <[email protected]>--- common/hush.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 3 deletions(-)
Applied, thanks.
Best regards,
Wolfgang Denk
Signature · 4 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de Democracy is mob rule, but with income taxes.
PATCH 3/3 hush: Include file and line number when reporting syntax errors
In reply to Joe HershbergerMake debugging script problems easier just like non-u-boot.
--- common/hush.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
common/hush.c +4 -2
@@ -383,9 +383,11 @@ static inline void debug_printf(const char *format, ...) { }#define final_printf debug_printf#ifdef __U_BOOT__ -static void syntax_err (void ) { -printf ("syntax error\n" ); +static void __syntax_err (char *file,int line) +{ +printf ("syntax error %s:%d\n" , file, line); } +#define syntax_err () __syntax_err(__FILE__, __LINE__)#else static void __syntax (char *file,int line) {error_msg ("syntax error %s:%d" , file, line);
Signature · 1 line
1.7.11.5
PATCH 3/3 hush: Include file and line number when reporting syntax errors
Quoted 1 line · On Friday 17 August 2012 16:26:31 Joe Hershberger wrote:
Make debugging script problems easier just like non-u-boot.
err, but you're posting "hush.c" all the time, and the line number of the source code in hush.c, not the line of the shell script
common/hush.c static inline void debug_printf(const char *format, ...) { } · line 386
Quoted 7 lines
common/hush.c +1
-2#ifdef __U_BOOT__ -static void syntax_err (void ) { -printf ("syntax error\n" ); +static void __syntax_err (char *file,int line)
const char *file
common/hush.c static inline void debug_printf(const char *format, ...) { } · line 390
Quoted 4 lines
common/hush.c +3
-0+{ +printf ("syntax error %s:%d\n" , file, line); } +#define syntax_err () __syntax_err(__FILE__, __LINE__)
shouldn't this be behind a DEBUG define ?
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120817/eb3fd5ac/attachment.pgp>
PATCH 3/3 hush: Include file and line number when reporting syntax errors
In reply to Joe HershbergerDear Joe Hershberger,
Quoted 6 lines · In message <[email protected]> you wrote:
Make debugging script problems easier just like non-u-boot.
Signed-off-by Joe Hershberger <[email protected]>--- common/hush.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
Skipped (i. e. not applied). See Mike's comments.
Best regards,
Wolfgang Denk
Signature · 6 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
News is what a chap who doesn't care much about anything wants to
read. And it's only news until he's read it. After that it's dead.
- Evelyn Waugh _Scoop_ (1938) bk. 1, ch. 5PATCH 1/3 hush: Add default value substitution support
In reply to Joe HershbergerQuoted 10 lines · On Friday 17 August 2012 16:26:29 Joe Hershberger wrote:
Use standard sh syntax:
${VAR:-default}
Use default value: if VAR is set and non-null, expands to $VAR. Otherwise, expands to default.
${VAR:=default}
Set default value: if VAR is set and non-null, expands to $VAR. Otherwise, sets hush VAR to default and expands to default.
${VAR:+default}
If VAR is set and non-null, expands to the empty string. Otherwise, expands to default.
how about ${VAR-default} and ${VAR=default} and ${VAR+default} ?
common/hush.c static int parse_group(o_string *dest, struct p_context *ctx, · line 2758
Quoted 6 lines
common/hush.c +3
-0+if (sep) { + *sep ='\0' ; +if (*(sep +1 ) =='-' )
switch (sep[1]) {
case '-':
...
common/hush.c
Quoted 1 line
common/hush.c +1
-0+ default_val = sep+2 ;
sep + 2
common/hush.c static int parse_group(o_string *dest, struct p_context *ctx, · line 2774
Quoted 1 line
common/hush.c +1
-0+if (!p ||strlen (p) ==0 ) {
if (!p || !p[0])
common/hush.c static int parse_group(o_string *dest, struct p_context *ctx, · line 2777
Quoted 2 lines
common/hush.c +2
-0+if (assign) { +char *var =malloc (strlen (src)+strlen (default_val)+2 );
please put spaces around the "+"
common/hush.c static int parse_group(o_string *dest, struct p_context *ctx, · line 2781
Quoted 4 lines
common/hush.c +4
-0+if (var) { +sprintf (var,"%s=%s" , src, default_val); +set_local_var (var,0 ); + }
isn't there a helper func for this ?
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20120817/bebecff0/attachment.pgp>
PATCH 1/3 hush: Add default value substitution support
Dear Joe,
Quoted 2 lines · In message <[email protected]> Mike Frysinger wrote:
Quoted 2 lines
${VAR:-default}
...
Quoted 1 line
Quoted 1 line
${VAR:=default}
...
Quoted 1 line
Quoted 1 line
${VAR:+default}
...
Quoted 1 line
how about ${VAR-default} and ${VAR=default} and ${VAR+default} ?
Maybe we (= you?) can add these, too?
Quoted 4 lines
common/hush.c static int parse_group(o_string *dest, struct p_context *ctx, · line 2777
Quoted 2 lines
common/hush.c +2
-0+if (assign) { +char *var =malloc (strlen (src)+strlen (default_val)+2 );please put spaces around the "+"
Ah, I missed that.
Quoted 6 lines
common/hush.c static int parse_group(o_string *dest, struct p_context *ctx, · line 2781
Quoted 4 lines
common/hush.c +4
-0+if (var) { +sprintf (var,"%s=%s" , src, default_val); +set_local_var (var,0 ); + }isn't there a helper func for this ?
Eventually we can lean this up in another patch, please?
Best regards,
Wolfgang Denk
Signature · 6 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de The ideal situation is to have massive computing power right at home. Something that dims the streetlights and shrinks the picture on the neighbours' TVs when you boot it up.
Wolfgang Denk
AppliedPATCH 1/3 hush: Add default value substitution support
In reply to Joe HershbergerDear Joe Hershberger,
Quoted 11 lines · In message <[email protected]> you wrote:
Use standard sh syntax:
${VAR:-default}
Use default value: if VAR is set and non-null, expands to $VAR. Otherwise, expands to default.
${VAR:=default}
Set default value: if VAR is set and non-null, expands to $VAR. Otherwise, sets hush VAR to default and expands to default.
${VAR:+default}
If VAR is set and non-null, expands to the empty string. Otherwise, expands to default.
Quoted 4 lines
Signed-off-by Joe Hershberger <[email protected]>--- common/hush.c | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-)
Applied, thanks.
Best regards,
Wolfgang Denk
Signature · 5 lines
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de It is the quality rather than the quantity that matters. - Lucius Annaeus Seneca (4 B.C. - A.D. 65)