]> git.ozlabs.org Git - petitboot/blobdiff - discover/grub2/script.c
discover/grub2: Allow using title for default even if id was defined
[petitboot] / discover / grub2 / script.c
index f52168157cfb03aa3e3eb49e5ff3fd00039e66f2..c910bf8bc1a28bf86901459b11d7a99ad059bc35 100644 (file)
@@ -103,7 +103,7 @@ static bool is_delim(char c)
 }
 
 static bool option_is_default(struct grub2_script *script,
-               struct discover_boot_option *opt, const char *id)
+                             struct discover_boot_option *opt, const char *id)
 {
        unsigned int default_idx;
        const char *var;
@@ -117,12 +117,10 @@ static bool option_is_default(struct grub2_script *script,
        if (end != var && *end == '\0')
                return default_idx == script->n_options;
 
-       /* if we don't have an explicit id for this option, fall back to
-        * the name */
-       if (!id)
-               id = opt->option->name;
+       if (id && !strcmp(id, var))
+               return true;
 
-       return !strcmp(id, var);
+       return !strcmp(opt->option->name, var);
 }
 
 static void append_text_to_current_arg(struct grub2_argv *argv,
@@ -227,7 +225,7 @@ static void process_expansions(struct grub2_script *script,
        }
 
        /* we may have allocated an extra argv element but not populated it */
-       if (!argv->argv[argv->argc - 1])
+       if (argv->argv && !argv->argv[argv->argc - 1])
                argv->argc--;
 }
 
@@ -379,8 +377,8 @@ static int function_invoke(struct grub2_script *script,
        int i;
 
        /* set positional parameters */
-       for (i = 0; i < argc; i++) {
-               name = talloc_asprintf(script, "$%d", i);
+       for (i = 1; i < argc; i++) {
+               name = talloc_asprintf(script, "%d", i);
                script_env_set(script, name, argv[i]);
        }
 
@@ -489,6 +487,9 @@ void script_execute(struct grub2_script *script)
 {
        struct discover_boot_option *opt, *tmp;
 
+       if (!script)
+               return;
+
        init_env(script);
        statements_execute(script, script->statements);