From 61ede5e0bea7d999acfdda9931e5c1f3c13c0694 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Thu, 31 Oct 2019 17:31:10 +0800 Subject: discover/grub2: Use getopt for `search` argument parsing The search command will be extended to add the full set of grub2-style arguments, so switch to using getopt, rather than manual parsing. This means we now support `--set=foo` and `--set foo` style arguments, both of which appear in the docs and common grub configs. Also, add a small test for the search argument handling. Signed-off-by: Jeremy Kerr --- discover/grub2/builtins.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'discover/grub2') diff --git a/discover/grub2/builtins.c b/discover/grub2/builtins.c index 3f09319..ab6b0ec 100644 --- a/discover/grub2/builtins.c +++ b/discover/grub2/builtins.c @@ -1,4 +1,7 @@ +#define _GNU_SOURCE + +#include #include #include @@ -106,18 +109,35 @@ static int builtin_initrd(struct grub2_script *script, return 0; } +static const struct option search_options[] = { + { + .name = "set", + .has_arg = required_argument, + .val = 's', + }, + { 0 }, +}; + static int builtin_search(struct grub2_script *script, void *data __attribute__((unused)), int argc, char *argv[]) { const char *env_var, *spec; - int i; env_var = "root"; + optind = 0; + + for (;;) { + int c = getopt_long(argc, argv, ":", search_options, NULL); + if (c == -1) + break; - for (i = 1; i < argc - 1; i++) { - if (!strncmp(argv[i], "--set=", strlen("--set="))) { - env_var = argv[i] + strlen("--set="); + switch (c) { + case 's': + env_var = optarg; + break; + case '?': + case ':': break; } } @@ -125,7 +145,10 @@ static int builtin_search(struct grub2_script *script, if (!strlen(env_var)) return 0; - spec = argv[argc - 1]; + if (optind >= argc) + return -1; + + spec = argv[optind]; script_env_set(script, env_var, spec); -- cgit v1.2.1