}
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;
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,
}
/* 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--;
}
opt->option->is_default = option_is_default(script, opt, id);
- discover_context_add_boot_option(script->ctx, opt);
+ list_add_tail(&script->options, &opt->list);
script->n_options++;
script->opt = NULL;
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]);
}
list_add(&script->symtab, &entry->list);
}
+static void set_fallback_default(struct grub2_script *script)
+{
+ struct discover_boot_option *opt, *first = NULL;
+ bool have_default = false;
+
+ list_for_each_entry(&script->options, opt, list) {
+ if (!first)
+ first = opt;
+ have_default = have_default || opt->option->is_default;
+ }
+
+ if (!have_default && first) {
+ const char *env = script_env_get(script, "default");
+
+ pb_log("grub: no explicit default (env default=%s), "
+ "falling back to first option (%s)\n",
+ env ?: "unset", first->option->name);
+
+ first->option->is_default = true;
+ }
+}
void script_execute(struct grub2_script *script)
{
+ struct discover_boot_option *opt, *tmp;
+
+ if (!script)
+ return;
+
init_env(script);
statements_execute(script, script->statements);
+
+ set_fallback_default(script);
+
+ list_for_each_entry_safe(&script->options, opt, tmp, list)
+ discover_context_add_boot_option(script->ctx, opt);
+
+ /* Our option list will be invalid, as we've added all options to the
+ * discover context */
+ list_init(&script->options);
}
struct grub2_script *create_script(struct grub2_parser *parser,
script->ctx = ctx;
list_init(&script->symtab);
+ list_init(&script->options);
register_builtins(script);
return script;