Hi,
Here are some hush related fixes/improvements:
Sascha Hauer (3):
hush: pass context around in shell
hush: fix nasty memory leak in hush
hush: implement getopt builtin
common/hush.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 88 insertions(+), 10 deletions(-)
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
PATCH 1/3 hush: pass context around in shell
--- common/hush.c | 18 +++++++++--------- 1 files changed, 9 insertions(+), 9 deletions(-)
common/hush.c +9 -9
@@ -267,8 +267,8 @@ static void setup_string_in_str(struct in_str *i, const char *s);static int free_pipe_list (struct pipe *head,int indent);static int free_pipe (struct pipe *pi,int indent);/* really run the final data structures: */ -static int run_list_real (struct pipe *pi); -static int run_pipe_real (struct pipe *pi); +static int run_list_real (struct p_context *ctx,struct pipe *pi); +static int run_pipe_real (struct p_context *ctx,struct pipe *pi);/* extended glob support: */ /* variable assignment: */ static int is_assignment (const char *s);
@@ -515,7 +515,7 @@ static void setup_string_in_str(struct in_str *i, const char *s)* now has its stdout directed to the input of the appropriate pipe, * so this routine is noticeably simpler. */ -static int run_pipe_real (struct pipe *pi) +static int run_pipe_real (struct p_context *ctx,struct pipe *pi) {int i;int nextin;
@@ -541,7 +541,7 @@ static int run_pipe_real(struct pipe *pi)if (pi->num_progs ==1 && child->group) {int rcode;debug ("non-subshell grouping\n" ); - rcode =run_list_real (child->group); + rcode =run_list_real (ctx, child->group);return rcode; }else if (pi->num_progs ==1 && pi->progs[0 ].argv !=NULL ) {for (i=0 ;is_assignment (child->argv[i]); i++) {/* nothing */ }
@@ -601,7 +601,7 @@ static int run_pipe_real(struct pipe *pi)return -1 ; } -static int run_list_real (struct pipe *pi) +static int run_list_real (struct p_context *ctx,struct pipe *pi) {char *save_name =NULL ;char **list =NULL ;
@@ -699,7 +699,7 @@ static int run_list_real(struct pipe *pi) }if (pi->num_progs ==0 )continue ; - rcode =run_pipe_real (pi); + rcode =run_pipe_real (ctx, pi);debug ("run_pipe_real returned %d\n" ,rcode);if (rcode <-1 ) { last_return_code = -rcode -2 ;
@@ -853,11 +853,11 @@ static int xglob(o_string *dest, int flags, glob_t *pglob) }/* Select which version we will use */ -static int run_list (struct pipe *pi) +static int run_list (struct p_context *ctx,struct pipe *pi) {int rcode =0 ; - rcode =run_list_real (pi); + rcode =run_list_real (ctx, pi);/* free_pipe_list has the side effect of clearing memory * In the long run that function can be merged with run_list_real,
@@ -1371,7 +1371,7 @@ static int parse_stream_outer(struct p_context *ctx, struct in_str *inp, int fladone_word (&temp, ctx);done_pipe (ctx,PIPE_SEQ );if (ctx->list_head->num_progs) { - code =run_list (ctx->list_head); + code =run_list (ctx, ctx->list_head); }else {free_pipe_list (ctx->list_head,0 );continue ;
Signature · 7 lines
1.7.2.3 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 2/3 hush: fix nasty memory leak in hush
In reply to Sascha HauerFix memory leak in globbing part of hush. a simple '[' on the command line was enough to trigger it.
We must call globfree() before setting the glob structure to zero.
--- common/hush.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
common/hush.c +1 -0
@@ -790,6 +790,7 @@ static int globhack(const char *src, int flags, glob_t *pglob) } dest =xmalloc (cnt);if (!(flags &GLOB_APPEND )) { +globfree (pglob); pglob->gl_pathv =NULL ; pglob->gl_pathc =0 ; pglob->gl_offs =0 ;
Signature · 7 lines
1.7.2.3 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH 3/3 hush: implement getopt builtin
In reply to Sascha HauerPositional parameters are not nice, so implement a getopt function. This has to be done as a builtin because otherwise we have no access to the parents argc/argv.
getopt works as expected, here is a little example:
while getopt "hs:" OPT
do
if [ $OPT = h ]; then
echo "usage"
exit 1
else
echo "scr: opt: $OPT optarg: $OPTARG"
fi
done
--- common/hush.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 78 insertions(+), 1 deletions(-)
common/hush.c +78 -1
@@ -120,6 +120,8 @@#include <fs.h> #include <libbb.h> #include <glob.h> +#include <getopt.h> +#include <linux/list.h> /*cmd_boot.c*/ extern int do_bootd (struct command *cmdtp,int flag,int argc,char *argv[]);/* do_bootd */
@@ -174,6 +176,12 @@ typedef enum {#define FLAG_PARSE_SEMICOLON (1 << 1)/* symbol ';' is special for parser */ #define FLAG_REPARSING (1 << 2)/* >=2nd pass */ +struct option { +struct list_head list; +char opt; +char *optarg; +}; +/* This holds pointers to the various results of parsing */ struct p_context {struct child_prog *child;
@@ -187,6 +195,9 @@ struct p_context {char **global_argv;unsigned int global_argc; + +int options_parsed; +struct list_head options; };
@@ -274,6 +285,7 @@ static int run_pipe_real(struct p_context *ctx, struct pipe *pi);static int is_assignment (const char *s);/* data structure manipulation: */ static void initialize_context (struct p_context *ctx); +static void release_context (struct p_context *ctx);static int done_word (o_string *dest,struct p_context *ctx);static int done_command (struct p_context *ctx);static int done_pipe (struct p_context *ctx,pipe_style type);
@@ -498,6 +510,48 @@ static void setup_string_in_str(struct in_str *i, const char *s) i->p = s; } +static int builtin_getopt (struct p_context *ctx,struct child_prog *child) +{ +char *optstring, *var; +int opt; +char opta[2 ]; +struct option *o; + +if (child->argc !=3 ) +return -1 ; + + optstring = child->argv[1 ]; + var = child->argv[2 ]; + +getopt_reset (); + +if (!ctx->options_parsed) { +while ((opt =getopt (ctx->global_argc, ctx->global_argv, optstring)) >0 ) { + o =xzalloc (sizeof (*o)); + o->opt = opt; + o->optarg =xstrdup (optarg); +list_add_tail (&o->list, &ctx->options); + } + } + + ctx->options_parsed =1 ; + +if (list_empty (&ctx->options)) +return -1 ; + + o = list_first_entry(&ctx->options, struct option, list); + + opta[0 ] = o->opt; + opta[1 ] =0 ; +setenv (var, opta); +setenv ("OPTARG" , o->optarg); + +free (o->optarg); +list_del (&o->list); +free (o); + +return 0 ; +}/* run_pipe_real() starts all the jobs, but doesn't wait for anything * to finish. See checkjobs().
@@ -583,10 +637,14 @@ static int run_pipe_real(struct p_context *ctx, struct pipe *pi)struct p_context ctx; str =make_string ((child->argv + i));parse_string_outer (&ctx, str,FLAG_EXIT_FROM_LOOP |FLAG_REPARSING ); +release_context (&ctx);free (str);return last_return_code; } +if (!strcmp (child->argv[i],"getopt" )) +return builtin_getopt (ctx, child); +if (strchr (child->argv[i],'/' )) {return execute_script (child->argv[i], child->argc-i, &child->argv[i]); }
@@ -929,9 +987,21 @@ static void initialize_context(struct p_context *ctx) ctx->w=RES_NONE ; ctx->stack=NULL ; ctx->old_flag=0 ; + ctx->options_parsed =0 ; +INIT_LIST_HEAD (&ctx->options);done_command (ctx);/* creates the memory for working child */ } +static void release_context (struct p_context *ctx) +{ +struct option *opt, *tmp; + +list_for_each_entry_safe (opt, tmp, &ctx->options, list) { +free (opt->optarg); +free (opt); + } +} +/* normal return is 0 * if a reserved word is found, and processed, return 1 * should handle if, then, elif, else, fi, for, while, until, do, done.
@@ -1534,7 +1604,12 @@ static char * make_string(char ** inp)int run_command (const char *cmd,int flag) {struct p_context ctx; -return parse_string_outer (&ctx, cmd,FLAG_PARSE_SEMICOLON ); +int ret; + + ret =parse_string_outer (&ctx, cmd,FLAG_PARSE_SEMICOLON ); +release_context (&ctx); + +return ret; }static int execute_script (const char *path,int argc,char *argv[])
@@ -1565,6 +1640,7 @@ static int source_script(const char *path, int argc, char *argv[]) ret =parse_string_outer (&ctx, script,FLAG_PARSE_SEMICOLON ); +release_context (&ctx);free (script);return ret;
@@ -1578,6 +1654,7 @@ int run_shell(void)setup_file_in_str (&input); rcode =parse_stream_outer (&ctx, &input,FLAG_PARSE_SEMICOLON ); +release_context (&ctx);return rcode; }
Signature · 7 lines
1.7.2.3 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Re: [PATCH 3/3] hush: implement getopt builtin
Quoted 16 lines · Sascha Hauer
Positional parameters are not nice, so implement a getopt function. This has to be done as a builtin because otherwise we have no access to the parents argc/argv.
getopt works as expected, here is a little example:
while getopt "hs:" OPT
do
if [ $OPT = h ]; then
echo "usage"
exit 1
else
echo "scr: opt: $OPT optarg: $OPTARG"
fi
done
Quoted 1 line
Signed-off-by Sascha Hauer <[email protected]>
how about make it optional?
Best Regards,
J.
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Re: [PATCH 3/3] hush: implement getopt builtin
Quoted 17 lines · Jean-Christophe PLAGNIOL-VILLARD
Quoted 16 lines · Sascha Hauer
Positional parameters are not nice, so implement a getopt function. This has to be done as a builtin because otherwise we have no access to the parents argc/argv.
getopt works as expected, here is a little example:
while getopt "hs:" OPT
do
if [ $OPT = h ]; then
echo "usage"
exit 1
else
echo "scr: opt: $OPT optarg: $OPTARG"
fi
done
Quoted 2 lines
Quoted 1 line
Signed-off-by Sascha Hauer <[email protected]>how about make it optional?
Probably a good idea. I just had in mind that we need to adjust the defconfigs then to still have a working defaultenv.
How about making it optional and combined with your defaultenv-Kconfig patch we could do a
select HUSH
select HUSH_GETOPT
to make sure all commands needed by the defaultenv are in place.
Sascha
Signature · 9 lines
Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Re: [PATCH 3/3] hush: implement getopt builtin
Quoted 24 lines · Sascha Hauer
Quoted 17 lines · Jean-Christophe PLAGNIOL-VILLARD
Quoted 16 lines · Sascha Hauer
Positional parameters are not nice, so implement a getopt function. This has to be done as a builtin because otherwise we have no access to the parents argc/argv.
getopt works as expected, here is a little example:
while getopt "hs:" OPT
do
if [ $OPT = h ]; then
echo "usage"
exit 1
else
echo "scr: opt: $OPT optarg: $OPTARG"
fi
doneQuoted 2 lines
Quoted 1 line
Signed-off-by Sascha Hauer <[email protected]>how about make it optional?
Probably a good idea. I just had in mind that we need to adjust the defconfigs then to still have a working defaultenv.
Quoted 7 lines
How about making it optional and combined with your defaultenv-Kconfig patch we could do a
select HUSH
selectHUSH_GETOPTto make sure all commands needed by the defaultenv are in place.
that's why I move the defaultenv to Kconfig, I'm still in vacation. I'll send the patch tomorrow
Best Regards,
J.
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
PATCH v2 3/3 hush: implement getopt builtin
In reply to Sascha HauerPositional parameters are not nice, so implement a getopt function. This has to be done as a builtin because otherwise we have no access to the parents argc/argv.
getopt works as expected, here is a little example:
while getopt "hs:" OPT
do
if [ $OPT = h ]; then
echo "usage"
exit 1
else
echo "scr: opt: $OPT optarg: $OPTARG"
fi
done
---
v2: make getopt optional, add help text.
common/Kconfig | 7 ++++ common/hush.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 115 insertions(+), 2 deletions(-)
common/Kconfig +7 -0
@@ -248,6 +248,13 @@ config HUSH_FANCY_PROMPT Allow to set PS1 from the command line. PS1 can have several escaped commands like \h for CONFIG_BOARDINFO or \w for the current working directory. +config HUSH_GETOPT + bool + depends on SHELL_HUSH + prompt "enable builtin getopt" + help + This enables a getopt function builtin to hush. + config CMDLINE_EDITING bool prompt "Enable command line editing"
common/hush.c +108 -2
182 lines not on this page · raw message
Signature · 12 lines
1.7.2.3 -- Pengutronix e.K. | | Industrial Linux Solutions | http://www.pengutronix.de/ | Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox