Commit b64b7c3df7906342ca8abe8ae31c0c12ced3f401

Authored by Gerlando Falauto
Committed by Tom Rini
1 parent 152874b65b

env: make "env default" selective, check and apply

Change the syntax (user API) for "env default":
  -f: override write-once variables
  var... : accept individual variable(s)
  -a: all (resetting the whole env is NOT the default behavior)

Enable variable checking and make changes effective by
enabling do_apply argument to himport_r().

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>

Showing 3 changed files with 64 additions and 7 deletions Side-by-side Diff

... ... @@ -656,14 +656,41 @@
656 656 return -1;
657 657 }
658 658  
659   -static int do_env_default(cmd_tbl_t *cmdtp, int flag,
  659 +static int do_env_default(cmd_tbl_t *cmdtp, int __flag,
660 660 int argc, char * const argv[])
661 661 {
662   - if (argc != 2 || strcmp(argv[1], "-f") != 0)
663   - return CMD_RET_USAGE;
  662 + int all = 0, flag = 0;
664 663  
665   - set_default_env("## Resetting to default environment\n");
666   - return 0;
  664 + debug("Initial value for argc=%d\n", argc);
  665 + while (--argc > 0 && **++argv == '-') {
  666 + char *arg = *argv;
  667 +
  668 + while (*++arg) {
  669 + switch (*arg) {
  670 + case 'a': /* default all */
  671 + all = 1;
  672 + break;
  673 + case 'f': /* force */
  674 + flag |= H_FORCE;
  675 + break;
  676 + default:
  677 + return cmd_usage(cmdtp);
  678 + }
  679 + }
  680 + }
  681 + debug("Final value for argc=%d\n", argc);
  682 + if (all && (argc == 0)) {
  683 + /* Reset the whole environment */
  684 + set_default_env("## Resetting to default environment\n");
  685 + return 0;
  686 + }
  687 + if (!all && (argc > 0)) {
  688 + /* Reset individual variables */
  689 + set_default_vars(argc, argv);
  690 + return 0;
  691 + }
  692 +
  693 + return cmd_usage(cmdtp);
667 694 }
668 695  
669 696 static int do_env_delete(cmd_tbl_t *cmdtp, int flag,
... ... @@ -994,7 +1021,8 @@
994 1021 #if defined(CONFIG_CMD_ASKENV)
995 1022 "ask name [message] [size] - ask for environment variable\nenv "
996 1023 #endif
997   - "default -f - reset default environment\n"
  1024 + "default [-f] -a - [forcibly] reset default environment\n"
  1025 + "env default [-f] var [...] - [forcibly] reset variable(s) to their default values\n"
998 1026 #if defined(CONFIG_CMD_EDITENV)
999 1027 "env edit name - edit environment variable\n"
1000 1028 #endif
... ... @@ -180,6 +180,11 @@
180 180  
181 181 void set_default_env(const char *s)
182 182 {
  183 + /*
  184 + * By default, do not apply changes as they will eventually
  185 + * be applied by someone else
  186 + */
  187 + int do_apply = 0;
183 188 if (sizeof(default_environment) > ENV_SIZE) {
184 189 puts("*** Error - default environment is too large\n\n");
185 190 return;
... ... @@ -191,6 +196,14 @@
191 196 "using default environment\n\n",
192 197 s + 1);
193 198 } else {
  199 + /*
  200 + * This set_to_default was explicitly asked for
  201 + * by the user, as opposed to being a recovery
  202 + * mechanism. Therefore we check every single
  203 + * variable and apply changes to the system
  204 + * right away (e.g. baudrate, console).
  205 + */
  206 + do_apply = 1;
194 207 puts(s);
195 208 }
196 209 } else {
197 210  
... ... @@ -199,10 +212,23 @@
199 212  
200 213 if (himport_r(&env_htab, (char *)default_environment,
201 214 sizeof(default_environment), '\0', 0,
202   - 0, NULL, 0 /* do_apply */) == 0)
  215 + 0, NULL, do_apply) == 0)
203 216 error("Environment import failed: errno = %d\n", errno);
204 217  
205 218 gd->flags |= GD_FLG_ENV_READY;
  219 +}
  220 +
  221 +
  222 +/* [re]set individual variables to their value in the default environment */
  223 +int set_default_vars(int nvars, char * const vars[])
  224 +{
  225 + /*
  226 + * Special use-case: import from default environment
  227 + * (and use \0 as a separator)
  228 + */
  229 + return himport_r(&env_htab, (const char *)default_environment,
  230 + sizeof(default_environment), '\0', H_NOCLEAR,
  231 + nvars, vars, 1 /* do_apply */);
206 232 }
207 233  
208 234 /*
include/environment.h
... ... @@ -181,6 +181,9 @@
181 181 /* [re]set to the default environment */
182 182 void set_default_env(const char *s);
183 183  
  184 +/* [re]set individual variables to their value in the default environment */
  185 +int set_default_vars(int nvars, char * const vars[]);
  186 +
184 187 /* Import from binary representation into hash table */
185 188 int env_import(const char *buf, int check);
186 189