summaryrefslogtreecommitdiffstats
path: root/discover/grub2
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2019-10-31 17:31:10 +0800
committerJeremy Kerr <jk@ozlabs.org>2019-11-29 13:54:10 +0800
commit61ede5e0bea7d999acfdda9931e5c1f3c13c0694 (patch)
treec04dd631b7fff42b2b6511016b9e056d80102f27 /discover/grub2
parente558da19475d747e6f8e83d07305d35da33102f9 (diff)
downloadtalos-petitboot-61ede5e0bea7d999acfdda9931e5c1f3c13c0694.tar.gz
talos-petitboot-61ede5e0bea7d999acfdda9931e5c1f3c13c0694.zip
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 <jk@ozlabs.org>
Diffstat (limited to 'discover/grub2')
-rw-r--r--discover/grub2/builtins.c33
1 files changed, 28 insertions, 5 deletions
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 <getopt.h>
#include <stdio.h>
#include <string.h>
@@ -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);
OpenPOWER on IntegriCloud