env: Provide programmatic equivalent to 'setenv -f'
PATCH 1/1 env: Provide programmatic equivalent to 'setenv -f'
Add env_force() to provide an equivalent to 'setenv -f' that can be used programmatically.
Also tighten up the definition of argv in _do_env_set() so that 'const char *' pointers are used.
---
cmd/nvedit.c | 43 +++++++++++++++++++++++++++++-------------- include/env.h | 13 +++++++++++++ 2 files changed, 42 insertions(+), 14 deletions(-)
cmd/nvedit.c +29 -14
@@ -221,10 +221,12 @@ DONE:* Set a new environment variable, * or replace or delete an existing one. */ -static int _do_env_set (int flag,int argc,char *const argv[],int env_flag) +static int _do_env_set (int flag,int argc,const char *const argv[], +int env_flag) {int i, len; -char *name, *value, *s; +const char *name; +char *value, *s;struct env_entry e, *ep;debug ("Initial value for argc=%d\n" , argc);
@@ -235,7 +237,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)#endif while (argc >1 && **(argv +1 ) =='-' ) { -char *arg = *++argv; +const char *arg = *++argv; --argc;while (*++arg) {
@@ -277,7 +279,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)return 1 ; }for (i =2 , s = value; i < argc; ++i) { -char *v = argv[i]; +const char *v = argv[i];while ((*s++ = *v++) !='\0' ) ;
@@ -299,18 +301,30 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)return 0 ; } -int env_set (const char *varname,const char *varvalue) +static int do_programmatic_env_set (int argc,const char *const argv[]) { -const char *const argv[4 ] = {"setenv" , varname, varvalue,NULL }; -/* before import into hashtable */ if (!(gd->flags &GD_FLG_ENV_READY ))return 1 ; -if (varvalue ==NULL || varvalue[0 ] =='\0' ) -return _do_env_set (0 ,2 , (char *const *)argv,H_PROGRAMMATIC ); -else -return _do_env_set (0 ,3 , (char *const *)argv,H_PROGRAMMATIC ); +if (!argv[argc -1 ] || argv[argc -1 ][0 ] =='\0' ) + argc--; + +return _do_env_set (0 , argc, argv,H_PROGRAMMATIC ); +} + +int env_set (const char *varname,const char *varvalue) +{ +const char *const argv[4 ] = {"setenv" , varname, varvalue,NULL }; + +return do_programmatic_env_set (3 , argv); +} + +int env_force (const char *varname,const char *varvalue) +{ +const char *const argv[5 ] = {"setenv" ,"-f" , varname, varvalue,NULL }; + +return do_programmatic_env_set (4 , argv); }/**
@@ -382,7 +396,8 @@ static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])if (argc <2 )return CMD_RET_USAGE ; -return _do_env_set (flag, argc, argv,H_INTERACTIVE ); +return _do_env_set (flag, argc, (const char *const *)argv, +H_INTERACTIVE ); }/*
@@ -643,12 +658,12 @@ static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,if (buffer[0 ] =='\0' ) {const char *const _argv[3 ] = {"setenv" , argv[1 ],NULL }; -return _do_env_set (0 ,2 , (char *const *)_argv,H_INTERACTIVE ); +return _do_env_set (0 ,2 , _argv,H_INTERACTIVE ); }else {const char *const _argv[4 ] = {"setenv" , argv[1 ], buffer,NULL }; -return _do_env_set (0 ,3 , (char *const *)_argv,H_INTERACTIVE ); +return _do_env_set (0 ,3 , _argv,H_INTERACTIVE ); } }#endif /* CONFIG_CMD_EDITENV */
include/env.h +13 -0
@@ -145,6 +145,19 @@ int env_get_yesno(const char *var);*/ int env_set (const char *varname,const char *value); +/** +* env_force() - forcibly set an environment variable +* +* This sets or deletes the value of an environment variable. It is the same +* as env_set(), except that the variable will be forcibly updated/deleted, +* even if it has access protection flags set. +* +* @varname: Variable to adjust +* @value: Value to set for the variable, or NULL or "" to delete the variable +* @return 0 if OK, 1 on error +*/ +int env_force (const char *varname,const char *varvalue); +/** * env_get_ulong() - Return an environment variable as an integer value *
Signature · 1 line
2.24.0
PATCH 1/1 env: Provide programmatic equivalent to 'setenv -f'
Am 19.11.2019 um 18:31 schrieb James Byrne:
Quoted 7 lines
Add
env_force()to provide an equivalent to 'setenv -f' that can be used programmatically.Also tighten up the definition of argv in
_do_env_set()so that 'const char *' pointers are used.Signed-off-by James Byrne <[email protected]>
OK, I'm on CC, so I'll give my two cent:
I always thought this code to be messed up a bit: I think it's better programming style to have the "string argument parsing" code call real C functions with typed arguments. The env code instead does it the other way round (here, you add do_programmatic_env_set() that sets up an argv[] array to call another function).
I'm not a maintainer for the ENV code, but maybe that should be sorted out instead of adding yet more code that goes this way?
Regards,
Simon
Quoted 132 lines
---
cmd/nvedit.c | 43 +++++++++++++++++++++++++++++-------------- include/env.h | 13 +++++++++++++ 2 files changed, 42 insertions(+), 14 deletions(-)cmd/nvedit.c +29
-14@@ -221,10 +221,12 @@ DONE:* Set a new environment variable, * or replace or delete an existing one. */ -static int _do_env_set (int flag,int argc,char *const argv[],int env_flag) +static int _do_env_set (int flag,int argc,const char *const argv[], +int env_flag) {int i, len; -char *name, *value, *s; +const char *name; +char *value, *s;struct env_entry e, *ep;debug ("Initial value for argc=%d\n" , argc);@@ -235,7 +237,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)#endif while (argc >1 && **(argv +1 ) =='-' ) { -char *arg = *++argv; +const char *arg = *++argv; --argc;while (*++arg) {@@ -277,7 +279,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)return 1 ; }for (i =2 , s = value; i < argc; ++i) { -char *v = argv[i]; +const char *v = argv[i];while ((*s++ = *v++) !='\0' ) ;@@ -299,18 +301,30 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)return 0 ; } -int env_set (const char *varname,const char *varvalue) +static int do_programmatic_env_set (int argc,const char *const argv[]) { -const char *const argv[4 ] = {"setenv" , varname, varvalue,NULL }; -/* before import into hashtable */ if (!(gd->flags &GD_FLG_ENV_READY ))return 1 ; -if (varvalue ==NULL || varvalue[0 ] =='\0' ) -return _do_env_set (0 ,2 , (char *const *)argv,H_PROGRAMMATIC ); -else -return _do_env_set (0 ,3 , (char *const *)argv,H_PROGRAMMATIC ); +if (!argv[argc -1 ] || argv[argc -1 ][0 ] =='\0' ) + argc--; + +return _do_env_set (0 , argc, argv,H_PROGRAMMATIC ); +} + +int env_set (const char *varname,const char *varvalue) +{ +const char *const argv[4 ] = {"setenv" , varname, varvalue,NULL }; + +return do_programmatic_env_set (3 , argv); +} + +int env_force (const char *varname,const char *varvalue) +{ +const char *const argv[5 ] = {"setenv" ,"-f" , varname, varvalue,NULL }; + +return do_programmatic_env_set (4 , argv); }/** @@ -382,7 +396,8 @@ static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])if (argc <2 )return CMD_RET_USAGE ; -return _do_env_set (flag, argc, argv,H_INTERACTIVE ); +return _do_env_set (flag, argc, (const char *const *)argv, +H_INTERACTIVE ); }/* @@ -643,12 +658,12 @@ static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,if (buffer[0 ] =='\0' ) {const char *const _argv[3 ] = {"setenv" , argv[1 ],NULL }; -return _do_env_set (0 ,2 , (char *const *)_argv,H_INTERACTIVE ); +return _do_env_set (0 ,2 , _argv,H_INTERACTIVE ); }else {const char *const _argv[4 ] = {"setenv" , argv[1 ], buffer,NULL }; -return _do_env_set (0 ,3 , (char *const *)_argv,H_INTERACTIVE ); +return _do_env_set (0 ,3 , _argv,H_INTERACTIVE ); } }#endif /* CONFIG_CMD_EDITENV */ include/env.h +13
-0@@ -145,6 +145,19 @@ int env_get_yesno(const char *var);*/ int env_set (const char *varname,const char *value); +/** +* env_force() - forcibly set an environment variable +* +* This sets or deletes the value of an environment variable. It is the same +* as env_set(), except that the variable will be forcibly updated/deleted, +* even if it has access protection flags set. +* +* @varname: Variable to adjust +* @value: Value to set for the variable, or NULL or "" to delete the variable +* @return 0 if OK, 1 on error +*/ +int env_force (const char *varname,const char *varvalue); +/** * env_get_ulong() - Return an environment variable as an integer value *
PATCH 1/1 env: Provide programmatic equivalent to 'setenv -f'
Quoted 11 lines · Simon Goldschmidt
Am 19.11.2019 um 18:31 schrieb James Byrne:
Quoted 7 lines
Add
env_force()to provide an equivalent to 'setenv -f' that can be used programmatically.Also tighten up the definition of argv in
_do_env_set()so that 'const char *' pointers are used.Signed-off-by James Byrne <[email protected]>OK, I'm on CC, so I'll give my two cent:
Quoted 8 lines
I always thought this code to be messed up a bit: I think it's better programming style to have the "string argument parsing" code call real C functions with typed arguments. The env code instead does it the other way round (here, you add
do_programmatic_env_set()that sets up an argv[] array to call another function).I'm not a maintainer for the ENV code, but maybe that should be sorted out instead of adding yet more code that goes this way?
There is no maintainer for the ENV code. Simon makes a valid point here. By creating a function for setting variables and separating it from parsing arguments you get the function you need for forcing the value of a variable for free.
Best regards
Heinrich
Quoted 136 lines
Regards,
SimonQuoted 130 lines
---
cmd/nvedit.c | 43 +++++++++++++++++++++++++++++--------------
include/env.h| 13 +++++++++++++2 files changed, 42 insertions(+), 14 deletions(-)cmd/nvedit.c +29
-14@@ -221,10 +221,12 @@ DONE:* Set a new environment variable, * or replace or delete an existing one. */ -static int _do_env_set (int flag,int argc,char *const argv[],int env_flag) +static int _do_env_set(int flag, int argc, const char * const argv[], + int env_flag) { int i, len; - char *name, *value, *s; + const char *name; + char *value, *s; struct env_entry e, *ep; debug("Initial value for argc=%d\n", argc);@@ -235,7 +237,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag) #endif while (argc > 1 && **(argv + 1) == '-') { - char *arg = *++argv; + const char *arg = *++argv; --argc; while (*++arg) {@@ -277,7 +279,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag) return 1; } for (i = 2, s = value; i < argc; ++i) { - char *v = argv[i]; + const char *v = argv[i]; while ((*s++ = *v++) != '\0') ;@@ -299,18 +301,30 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag) return 0; } -int env_set (const char *varname,const char *varvalue) +static int do_programmatic_env_set (int argc,const char *const argv[]) { - const char * const argv[4] = { "setenv", varname, varvalue, NULL }; - /* before import into hashtable */ if (!(gd->flags & GD_FLG_ENV_READY)) return 1; - if (varvalue == NULL || varvalue[0] == '\0') - return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC); - else - return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC); + if (!argv[argc - 1] || argv[argc - 1][0] == '\0') + argc--; + + return _do_env_set(0, argc, argv, H_PROGRAMMATIC); +} + +int env_set (const char *varname,const char *varvalue) +{ + const char * const argv[4] = {"setenv", varname, varvalue, NULL}; + + return do_programmatic_env_set(3, argv); +} + +int env_force (const char *varname,const char *varvalue) +{ + const char * const argv[5] = {"setenv", "-f", varname, varvalue,NULL }; + + return do_programmatic_env_set(4, argv); } /**@@ -382,7 +396,8 @@ static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) if (argc < 2) return CMD_RET_USAGE; - return _do_env_set(flag, argc, argv, H_INTERACTIVE); + return _do_env_set(flag, argc, (const char * const *)argv, + H_INTERACTIVE); } /*@@ -643,12 +658,12 @@ static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, if (buffer[0] == '\0') { const char * const _argv[3] = { "setenv", argv[1], NULL }; - return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE); + return _do_env_set(0, 2, _argv, H_INTERACTIVE); } else { const char * const _argv[4] = { "setenv", argv[1], buffer, NULL }; - return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE); + return _do_env_set(0, 3, _argv, H_INTERACTIVE); } } #endif /* CONFIG_CMD_EDITENV */include/env.h +4
-0@@ -145,6 +145,19 @@ int env_get_yesno(const char *var);*/ int env_set(const char *varname, const char *value); +/** +* env_force() - forcibly set an environment variable +* +* This sets or deletes the value of an environment variable. It is the same
+ * asenv_set(), except that the variable will be forcibly updated/deleted,
+ * even if it has access protection flags set.
+ *
+ * @varname: Variable to adjust
+ * @value: Value to set for the variable, or NULL or "" to delete the variable
+ * @return 0 if OK, 1 on error
+ */include/env.h +2
-0+int env_force (const char *varname,const char *varvalue); +/**
*env_get_ulong()- Return an environment variable as an integer value *
PATCH 1/1 env: Provide programmatic equivalent to 'setenv -f'
Heinrich Schuchardt <[email protected]> schrieb am Di., 19. Nov. 2019, 21:56:
Quoted 18 lines · Simon Goldschmidt
Quoted 17 lines
Am 19.11.2019 um 18:31 schrieb James Byrne:
Quoted 7 lines
Add
env_force()to provide an equivalent to 'setenv -f' that can be used programmatically.Also tighten up the definition of argv in
_do_env_set()so that 'const char *' pointers are used.Signed-off-by James Byrne <[email protected]>OK, I'm on CC, so I'll give my two cent:
I always thought this code to be messed up a bit: I think it's better programming style to have the "string argument parsing" code call real C functions with typed arguments. The env code instead does it the other way round (here, you add
do_programmatic_env_set()that sets up an argv[] array to call another function).
Quoted 8 lines
Quoted 2 lines
I'm not a maintainer for the ENV code, but maybe that should be sorted out instead of adding yet more code that goes this way?
There is no maintainer for the ENV code. Simon makes a valid point here. By creating a function for setting variables and separating it from parsing arguments you get the function you need for forcing the value of a variable for free.
Right. I thought someone had volunteered but a look at the maintainers file proves me wrong.
In any way, I'd be more open to review a cleanup patch than a patch continuing this messy code flow.
Regards,
Simon
Quoted 143 lines
Best regards
Heinrich
Quoted 124 lines
Regards,
SimonQuoted 120 lines
---
cmd/nvedit.c | 43 +++++++++++++++++++++++++++++-------------- include/env.h | 13 +++++++++++++ 2 files changed, 42 insertions(+), 14 deletions(-)cmd/nvedit.c +29
-14@@ -221,10 +221,12 @@ DONE:* Set a new environment variable, * or replace or delete an existing one. */ -static int _do_env_set (int flag,int argc,char *const argv[],int env_flag) +static int _do_env_set(int flag, int argc, const char * const argv[], +int env_flag) {int i, len; -char *name, *value, *s; +const char *name; +char *value, *s;struct env_entry e, *ep;debug ("Initial value for argc=%d\n" , argc);@@ -235,7 +237,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag) #endif while (argc > 1 && **(argv + 1) == '-') { - char *arg = *++argv; + const char *arg = *++argv; --argc; while (*++arg) {@@ -277,7 +279,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag) return 1; }for (i =2 , s = value; i < argc; ++i) { -char *v = argv[i]; +const char *v = argv[i];while ((*s++ = *v++) !='\0' ) ;@@ -299,18 +301,30 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)return 0 ; } -int env_set (const char *varname,const char *varvalue) +static int do_programmatic_env_set (int argc,const char *const argv[]) { -const char *const argv[4 ] = {"setenv" , varname, varvalue,NULL }; -/* before import into hashtable */ if (!(gd->flags &GD_FLG_ENV_READY ))return 1 ; -if (varvalue ==NULL || varvalue[0 ] =='\0' ) -return _do_env_set (0 ,2 , (char *const *)argv,H_PROGRAMMATIC ); -else -return _do_env_set (0 ,3 , (char *const *)argv,H_PROGRAMMATIC ); +if (!argv[argc -1 ] || argv[argc -1 ][0 ] =='\0' ) + argc--; + +return _do_env_set (0 , argc, argv,H_PROGRAMMATIC ); +} + +int env_set (const char *varname,const char *varvalue) +{ +const char *const argv[4 ] = {"setenv" , varname, varvalue,NULL }; + +return do_programmatic_env_set (3 , argv); +} + +int env_force (const char *varname,const char *varvalue) +{ +const char *const argv[5 ] = {"setenv" ,"-f" , varname, varvalue,NULL }; + +return do_programmatic_env_set (4 , argv); }/** @@ -382,7 +396,8 @@ static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])if (argc <2 )return CMD_RET_USAGE ; -return _do_env_set (flag, argc, argv,H_INTERACTIVE ); +return _do_env_set (flag, argc, (const char *const *)argv, +H_INTERACTIVE ); }/* @@ -643,12 +658,12 @@ static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, if (buffer[0] == '\0') {const char *const _argv[3 ] = {"setenv" , argv[1 ],NULL }; -return _do_env_set (0 ,2 , (char *const *)_argv,H_INTERACTIVE ); +return _do_env_set (0 ,2 , _argv,H_INTERACTIVE ); } else {const char *const _argv[4 ] = {"setenv" , argv[1 ], buffer,NULL }; -return _do_env_set (0 ,3 , (char *const *)_argv,H_INTERACTIVE ); +return _do_env_set (0 ,3 , _argv,H_INTERACTIVE ); } }#endif /* CONFIG_CMD_EDITENV */ include/env.h +4
-0@@ -145,6 +145,19 @@ int env_get_yesno(const char *var);*/ int env_set (const char *varname,const char *value); +/** +* env_force() - forcibly set an environment variable +* +* This sets or deletes the value of an environment variable. It is the same
+ * asenv_set(), except that the variable will be forcibly updated/deleted,
+ * even if it has access protection flags set.
+ *
+ * @varname: Variable to adjustQuoted 8 lines
include/env.h int env_get_yesno(const char *var);
Quoted 8 lines
+ * @value: Value to set for the variable, or NULL or "" to delete the variable
+ * @return 0 if OK, 1 on error
+ */include/env.h +2
-0+int env_force (const char *varname,const char *varvalue); +/** * env_get_ulong() - Return an environment variable as an integer value
Quoted 4 lines
Quoted 2 lines
*
PATCH 1/1 env: Provide programmatic equivalent to 'setenv -f'
Quoted 25 lines · Simon Goldschmidt
Heinrich Schuchardt <[email protected]> schrieb am Di., 19. Nov. 2019, 21:56:
Quoted 21 lines · Simon Goldschmidt
Quoted 19 lines
Am 19.11.2019 um 18:31 schrieb James Byrne:
Quoted 7 lines
Add
env_force()to provide an equivalent to 'setenv -f' that can be used programmatically.Also tighten up the definition of argv in
_do_env_set()so that 'const char *' pointers are used.Signed-off-by James Byrne <[email protected]>OK, I'm on CC, so I'll give my two cent:
I always thought this code to be messed up a bit: I think it's better programming style to have the "string argument parsing" code call real C functions with typed arguments. The env code instead does it the other way round (here, you add
do_programmatic_env_set()that sets up an argv[] array to call another function).I'm not a maintainer for the ENV code, but maybe that should be sorted out instead of adding yet more code that goes this way?
Quoted 8 lines
Quoted 5 lines
There is no maintainer for the ENV code. Simon makes a valid point here. By creating a function for setting variables and separating it from parsing arguments you get the function you need for forcing the value of a variable for free.
Right. I thought someone had volunteered but a look at the maintainers file proves me wrong.
I sent a patch [1] to Tom a while ago, but it hasn't made it in yet.
[1] - https://patchwork.ozlabs.org/patch/1166740/
Quoted 2 lines
In any way, I'd be more open to review a cleanup patch than a patch continuing this messy code flow.
I agree that this could be cleaner, but given that it is simple and following the existing pattern I don't think it needs to be rejected for not also including a refactoring. I would join you in encouraging it though.
Cheers,
-Joe
Quoted 151 lines
Regards,
SimonQuoted 129 lines
Best regards
Heinrich
Quoted 125 lines
Regards,
SimonQuoted 121 lines
---
cmd/nvedit.c | 43 +++++++++++++++++++++++++++++-------------- include/env.h | 13 +++++++++++++ 2 files changed, 42 insertions(+), 14 deletions(-)cmd/nvedit.c +29
-14@@ -221,10 +221,12 @@ DONE:* Set a new environment variable, * or replace or delete an existing one. */ -static int _do_env_set (int flag,int argc,char *const argv[],int env_flag) +static int _do_env_set(int flag, int argc, const char * const argv[], +int env_flag) {int i, len; -char *name, *value, *s; +const char *name; +char *value, *s;struct env_entry e, *ep;debug ("Initial value for argc=%d\n" , argc);@@ -235,7 +237,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag) #endif while (argc > 1 && **(argv + 1) == '-') { - char *arg = *++argv; + const char *arg = *++argv; --argc; while (*++arg) {@@ -277,7 +279,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag) return 1; }for (i =2 , s = value; i < argc; ++i) { -char *v = argv[i]; +const char *v = argv[i];while ((*s++ = *v++) !='\0' ) ;@@ -299,18 +301,30 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)return 0 ; } -int env_set (const char *varname,const char *varvalue) +static int do_programmatic_env_set (int argc,const char *const argv[]) { -const char *const argv[4 ] = {"setenv" , varname, varvalue,NULL }; -/* before import into hashtable */ if (!(gd->flags &GD_FLG_ENV_READY ))return 1 ; -if (varvalue ==NULL || varvalue[0 ] =='\0' ) -return _do_env_set (0 ,2 , (char *const *)argv,H_PROGRAMMATIC ); -else -return _do_env_set (0 ,3 , (char *const *)argv,H_PROGRAMMATIC ); +if (!argv[argc -1 ] || argv[argc -1 ][0 ] =='\0' ) + argc--; + +return _do_env_set (0 , argc, argv,H_PROGRAMMATIC ); +} + +int env_set (const char *varname,const char *varvalue) +{ +const char *const argv[4 ] = {"setenv" , varname, varvalue,NULL }; + +return do_programmatic_env_set (3 , argv); +} + +int env_force (const char *varname,const char *varvalue) +{ +const char *const argv[5 ] = {"setenv" ,"-f" , varname, varvalue,NULL }; + +return do_programmatic_env_set (4 , argv); }/** @@ -382,7 +396,8 @@ static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])if (argc <2 )return CMD_RET_USAGE ; -return _do_env_set (flag, argc, argv,H_INTERACTIVE ); +return _do_env_set (flag, argc, (const char *const *)argv, +H_INTERACTIVE ); }/* @@ -643,12 +658,12 @@ static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, if (buffer[0] == '\0') {const char *const _argv[3 ] = {"setenv" , argv[1 ],NULL }; -return _do_env_set (0 ,2 , (char *const *)_argv,H_INTERACTIVE ); +return _do_env_set (0 ,2 , _argv,H_INTERACTIVE ); } else {const char *const _argv[4 ] = {"setenv" , argv[1 ], buffer,NULL }; -return _do_env_set (0 ,3 , (char *const *)_argv,H_INTERACTIVE ); +return _do_env_set (0 ,3 , _argv,H_INTERACTIVE ); } }#endif /* CONFIG_CMD_EDITENV */ include/env.h +4
-0@@ -145,6 +145,19 @@ int env_get_yesno(const char *var);*/ int env_set (const char *varname,const char *value); +/** +* env_force() - forcibly set an environment variable +* +* This sets or deletes the value of an environment variable. It is the same
+ * asenv_set(), except that the variable will be forcibly updated/deleted,
+ * even if it has access protection flags set.
+ *
+ * @varname: Variable to adjust
+ * @value: Value to set for the variable, or NULL or "" to delete theQuoted 14 lines
Quoted 7 lines
include/env.h int env_get_yesno(const char *var);
Quoted 7 lines
variable
+ * @return 0 if OK, 1 on error
+ */include/env.h +2
-0+int env_force (const char *varname,const char *varvalue); +/** * env_get_ulong() - Return an environment variable as an integer value
Quoted 4 lines
Quoted 2 lines
*_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
https://lists.denx.de/listinfo/u-boot
PATCH 1/1 env: Provide programmatic equivalent to 'setenv -f'
Quoted 33 lines · Joe Hershberger
Quoted 31 lines · Simon Goldschmidt
Heinrich Schuchardt <[email protected]> schrieb am Di., 19. Nov. 2019, 21:56:
Quoted 26 lines · Simon Goldschmidt
Quoted 11 lines
Am 19.11.2019 um 18:31 schrieb James Byrne:
Quoted 7 lines
Add
env_force()to provide an equivalent to 'setenv -f' that can be used programmatically.Also tighten up the definition of argv in
_do_env_set()so that 'const char *' pointers are used.Signed-off-by James Byrne <[email protected]>OK, I'm on CC, so I'll give my two cent:
Quoted 8 lines
I always thought this code to be messed up a bit: I think it's better programming style to have the "string argument parsing" code call real C functions with typed arguments. The env code instead does it the other way round (here, you add
do_programmatic_env_set()that sets up an argv[] array to call another function).I'm not a maintainer for the ENV code, but maybe that should be sorted out instead of adding yet more code that goes this way?
There is no maintainer for the ENV code. Simon makes a valid point here. By creating a function for setting variables and separating it from parsing arguments you get the function you need for forcing the value of a variable for free.
Quoted 6 lines
Quoted 2 lines
Right. I thought someone had volunteered but a look at the maintainers file proves me wrong.
I sent a patch [1] to Tom a while ago, but it hasn't made it in yet.
Sorry, I was waiting for an update where you move Wolfgang down to just 'R' for review, and fix the thinko on his last name :)
Signature · 8 lines
Tom -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: not available URL: <http://lists.denx.de/pipermail/u-boot/attachments/20191120/0492fde9/attachment.sig>
PATCH 1/1 env: Provide programmatic equivalent to 'setenv -f'
In reply to Simon GoldschmidtQuoted 37 lines · Simon Goldschmidt
Heinrich Schuchardt <xypron.glpk at gmx.de <mailto:[email protected]>> schrieb am Di., 19. Nov. 2019, 21:56:
On 11/19/19 9:30 PM, Simon Goldschmidt wrote:
> Am 19.11.2019 um 18:31 schrieb James Byrne:
>> Addenv_force()to provide an equivalent to 'setenv -f' that can
be used
>> programmatically.
>>
>> Also tighten up the definition of argv in_do_env_set()so that >> 'const char *' pointers are used.
>>
>> Signed-off-by: James Byrne <[email protected]
<mailto:[email protected]>>
>
> OK, I'm on CC, so I'll give my two cent:
>
> I always thought this code to be messed up a bit: I think it's better > programming style to have the "string argument parsing" code call
real C
> functions with typed arguments. The env code instead does it the
other
> way round (here, you adddo_programmatic_env_set()that sets up an > argv[] array to call another function).
>
> I'm not a maintainer for the ENV code, but maybe that should be
sorted
> out instead of adding yet more code that goes this way?There is no maintainer for the ENV code. Simon makes a valid point here. By creating a function for setting variables and separating it from parsing arguments you get the function you need for forcing the value of a variable for free.
Quoted 5 lines
Right. I thought someone had volunteered but a look at the maintainers file proves me wrong.
In any way, I'd be more open to review a cleanup patch than a patch continuing this messy code flow.
Having looked at it again, I agree. I have now redone it, but I have ended up changing quite a lot more of the underlying code. I will resubmit a revised patch (probably tomorrow) in two parts, one to apply some tidying up to the env code, and one to add the new function. It will be a much bigger patch set though!
James
PATCH 1/1 env: Provide programmatic equivalent to 'setenv -f'
Am 20.11.2019 um 19:49 schrieb James Byrne:
Quoted 41 lines · Simon Goldschmidt
Quoted 40 lines
Heinrich Schuchardt <xypron.glpk at gmx.de <mailto:[email protected]>> schrieb am Di., 19. Nov. 2019, 21:56:
On 11/19/19 9:30 PM, Simon Goldschmidt wrote:
> Am 19.11.2019 um 18:31 schrieb James Byrne:
>> Addenv_force()to provide an equivalent to 'setenv -f' that can
be used
>> programmatically.
>>
>> Also tighten up the definition of argv in_do_env_set()so that >> 'const char *' pointers are used.
>>
>> Signed-off-by: James Byrne <[email protected]
<mailto:[email protected]>>
>
> OK, I'm on CC, so I'll give my two cent:
>
> I always thought this code to be messed up a bit: I think it's better > programming style to have the "string argument parsing" code call
real C
> functions with typed arguments. The env code instead does it the
other
> way round (here, you adddo_programmatic_env_set()that sets up an > argv[] array to call another function).
>
> I'm not a maintainer for the ENV code, but maybe that should be
sorted
> out instead of adding yet more code that goes this way?There is no maintainer for the ENV code. Simon makes a valid point here. By creating a function for setting variables and separating it from parsing arguments you get the function you need for forcing the value of a variable for free.
Right. I thought someone had volunteered but a look at the maintainers file proves me wrong.
Quoted 8 lines
Quoted 2 lines
In any way, I'd be more open to review a cleanup patch than a patch continuing this messy code flow.
Having looked at it again, I agree. I have now redone it, but I have ended up changing quite a lot more of the underlying code. I will resubmit a revised patch (probably tomorrow) in two parts, one to apply some tidying up to the env code, and one to add the new function. It will be a much bigger patch set though!
Cool. I wouldn't want to put this as a burdon on you (meaning to NACK this patch like it is), btu a cleanup in that direction would certainly be appreciated!
Thanks,
Simon
Quoted 3 lines
James
PATCH 1/1 env: Provide programmatic equivalent to 'setenv -f'
In reply to Heinrich SchuchardtQuoted 18 lines · Heinrich Schuchardt
Quoted 17 lines · Simon Goldschmidt
Am 19.11.2019 um 18:31 schrieb James Byrne:
Quoted 7 lines
Add
env_force()to provide an equivalent to 'setenv -f' that can be used programmatically.Also tighten up the definition of argv in
_do_env_set()so that 'const char *' pointers are used.Signed-off-by James Byrne <[email protected]>OK, I'm on CC, so I'll give my two cent:
I always thought this code to be messed up a bit: I think it's better programming style to have the "string argument parsing" code call real C functions with typed arguments. The env code instead does it the other way round (here, you add
do_programmatic_env_set()that sets up an argv[] array to call another function).
Quoted 7 lines
Quoted 2 lines
I'm not a maintainer for the ENV code, but maybe that should be sorted out instead of adding yet more code that goes this way?
There is no maintainer for the ENV code. Simon makes a valid point here. By creating a function for setting variables and separating it from parsing arguments you get the function you need for forcing the value of a variable for free.
+1
-Takahiro Akashi
Quoted 141 lines
Best regards
Heinrich
Quoted 136 lines
Regards,
SimonQuoted 130 lines
---
cmd/nvedit.c | 43 +++++++++++++++++++++++++++++--------------
include/env.h| 13 +++++++++++++2 files changed, 42 insertions(+), 14 deletions(-)cmd/nvedit.c +29
-14@@ -221,10 +221,12 @@ DONE:* Set a new environment variable, * or replace or delete an existing one. */ -static int _do_env_set (int flag,int argc,char *const argv[],int env_flag) +static int _do_env_set(int flag, int argc, const char * const argv[], + int env_flag) { int i, len; - char *name, *value, *s; + const char *name; + char *value, *s; struct env_entry e, *ep; debug("Initial value for argc=%d\n", argc);@@ -235,7 +237,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag) #endif while (argc > 1 && **(argv + 1) == '-') { - char *arg = *++argv; + const char *arg = *++argv; --argc; while (*++arg) {@@ -277,7 +279,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag) return 1; } for (i = 2, s = value; i < argc; ++i) { - char *v = argv[i]; + const char *v = argv[i]; while ((*s++ = *v++) != '\0') ;@@ -299,18 +301,30 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag) return 0; } -int env_set (const char *varname,const char *varvalue) +static int do_programmatic_env_set (int argc,const char *const argv[]) { - const char * const argv[4] = { "setenv", varname, varvalue, NULL }; - /* before import into hashtable */ if (!(gd->flags & GD_FLG_ENV_READY)) return 1; - if (varvalue == NULL || varvalue[0] == '\0') - return _do_env_set(0, 2, (char * const *)argv, H_PROGRAMMATIC); - else - return _do_env_set(0, 3, (char * const *)argv, H_PROGRAMMATIC); + if (!argv[argc - 1] || argv[argc - 1][0] == '\0') + argc--; + + return _do_env_set(0, argc, argv, H_PROGRAMMATIC); +} + +int env_set (const char *varname,const char *varvalue) +{ + const char * const argv[4] = {"setenv", varname, varvalue, NULL}; + + return do_programmatic_env_set(3, argv); +} + +int env_force (const char *varname,const char *varvalue) +{ + const char * const argv[5] = {"setenv", "-f", varname, varvalue,NULL }; + + return do_programmatic_env_set(4, argv); } /**@@ -382,7 +396,8 @@ static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) if (argc < 2) return CMD_RET_USAGE; - return _do_env_set(flag, argc, argv, H_INTERACTIVE); + return _do_env_set(flag, argc, (const char * const *)argv, + H_INTERACTIVE); } /*@@ -643,12 +658,12 @@ static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc, if (buffer[0] == '\0') { const char * const _argv[3] = { "setenv", argv[1], NULL }; - return _do_env_set(0, 2, (char * const *)_argv, H_INTERACTIVE); + return _do_env_set(0, 2, _argv, H_INTERACTIVE); } else { const char * const _argv[4] = { "setenv", argv[1], buffer, NULL }; - return _do_env_set(0, 3, (char * const *)_argv, H_INTERACTIVE); + return _do_env_set(0, 3, _argv, H_INTERACTIVE); } } #endif /* CONFIG_CMD_EDITENV */include/env.h +4
-0@@ -145,6 +145,19 @@ int env_get_yesno(const char *var);*/ int env_set(const char *varname, const char *value); +/** +* env_force() - forcibly set an environment variable +* +* This sets or deletes the value of an environment variable. It is the same
+ * asenv_set(), except that the variable will be forcibly updated/deleted,
+ * even if it has access protection flags set.
+ *
+ * @varname: Variable to adjust
+ * @value: Value to set for the variable, or NULL or "" to delete the variable
+ * @return 0 if OK, 1 on error
+ */include/env.h +2
-0+int env_force (const char *varname,const char *varvalue); +/**
*env_get_ulong()- Return an environment variable as an integer value *
PATCH 1/1 env: Provide programmatic equivalent to 'setenv -f'
In reply to James ByrneHi James,
Quoted 125 lines · James Byrne
Add
env_force()to provide an equivalent to 'setenv -f' that can be used programmatically.Also tighten up the definition of argv in
_do_env_set()so that 'const char *' pointers are used.Signed-off-by James Byrne <[email protected]>---
cmd/nvedit.c | 43 +++++++++++++++++++++++++++++-------------- include/env.h | 13 +++++++++++++ 2 files changed, 42 insertions(+), 14 deletions(-)cmd/nvedit.c +29
-14@@ -221,10 +221,12 @@ DONE:* Set a new environment variable, * or replace or delete an existing one. */ -static int _do_env_set (int flag,int argc,char *const argv[],int env_flag) +static int _do_env_set (int flag,int argc,const char *const argv[], +int env_flag) {int i, len; -char *name, *value, *s; +const char *name; +char *value, *s;struct env_entry e, *ep;debug ("Initial value for argc=%d\n" , argc);@@ -235,7 +237,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)#endif while (argc >1 && **(argv +1 ) =='-' ) { -char *arg = *++argv; +const char *arg = *++argv; --argc;while (*++arg) {@@ -277,7 +279,7 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)return 1 ; }for (i =2 , s = value; i < argc; ++i) { -char *v = argv[i]; +const char *v = argv[i];while ((*s++ = *v++) !='\0' ) ;@@ -299,18 +301,30 @@ static int _do_env_set(int flag, int argc, char * const argv[], int env_flag)return 0 ; } -int env_set (const char *varname,const char *varvalue) +static int do_programmatic_env_set (int argc,const char *const argv[]) { -const char *const argv[4 ] = {"setenv" , varname, varvalue,NULL }; -/* before import into hashtable */ if (!(gd->flags &GD_FLG_ENV_READY ))return 1 ; -if (varvalue ==NULL || varvalue[0 ] =='\0' ) -return _do_env_set (0 ,2 , (char *const *)argv,H_PROGRAMMATIC ); -else -return _do_env_set (0 ,3 , (char *const *)argv,H_PROGRAMMATIC ); +if (!argv[argc -1 ] || argv[argc -1 ][0 ] =='\0' ) + argc--; + +return _do_env_set (0 , argc, argv,H_PROGRAMMATIC ); +} + +int env_set (const char *varname,const char *varvalue) +{ +const char *const argv[4 ] = {"setenv" , varname, varvalue,NULL }; + +return do_programmatic_env_set (3 , argv); +} + +int env_force (const char *varname,const char *varvalue) +{ +const char *const argv[5 ] = {"setenv" ,"-f" , varname, varvalue,NULL }; + +return do_programmatic_env_set (4 , argv); }/** @@ -382,7 +396,8 @@ static int do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])if (argc <2 )return CMD_RET_USAGE ; -return _do_env_set (flag, argc, argv,H_INTERACTIVE ); +return _do_env_set (flag, argc, (const char *const *)argv, +H_INTERACTIVE ); }/* @@ -643,12 +658,12 @@ static int do_env_edit(cmd_tbl_t *cmdtp, int flag, int argc,if (buffer[0 ] =='\0' ) {const char *const _argv[3 ] = {"setenv" , argv[1 ],NULL }; -return _do_env_set (0 ,2 , (char *const *)_argv,H_INTERACTIVE ); +return _do_env_set (0 ,2 , _argv,H_INTERACTIVE ); }else {const char *const _argv[4 ] = {"setenv" , argv[1 ], buffer,NULL }; -return _do_env_set (0 ,3 , (char *const *)_argv,H_INTERACTIVE ); +return _do_env_set (0 ,3 , _argv,H_INTERACTIVE ); } }#endif /* CONFIG_CMD_EDITENV */ include/env.h +2
-0@@ -145,6 +145,19 @@ int env_get_yesno(const char *var);*/ int env_set (const char *varname,const char *value); +/** +* env_force() - forcibly set an environment variable
include/env.h int env_get_yesno(const char *var); · line 159
Quoted 10 lines
include/env.h +10
-0+* +* This sets or deletes the value of an environment variable. It is the same +* as env_set(), except that the variable will be forcibly updated/deleted, +* even if it has access protection flags set. +* +* @varname: Variable to adjust +* @value: Value to set for the variable, or NULL or "" to delete the variable +* @return 0 if OK, 1 on error +*/ +int env_force (const char *varname,const char *varvalue);
Please call it env_force_set()
Quoted 11 lines
include/env.h +1
-1+/** * env_get_ulong() - Return an environment variable as an integer value * -- 2.24.0
_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
https://lists.denx.de/listinfo/u-boot