--- /dev/null
+
+#include <stdio.h>
+#include <string.h>
+
+#include <talloc/talloc.h>
+#include <array-size/array-size.h>
+
+#include "grub2.h"
+
+
+static int builtin_set(struct grub2_script *script, int argc, char *argv[])
+{
+ char *name, *value, *p;
+ int i;
+
+ if (argc < 2)
+ return -1;
+
+ p = strchr(argv[1], '=');
+ if (!p)
+ return -1;
+
+ name = talloc_strndup(script, argv[1], p - argv[1]);
+ value = talloc_strdup(script, p+1);
+
+ for (i = 2; i < argc; i++)
+ value = talloc_asprintf_append(value, " %s", argv[i]);
+
+ script_env_set(script, name, value);
+
+ return 0;
+}
+
+static struct grub2_command commands[] = {
+ {
+ .name = "set",
+ .exec = builtin_set
+ },
+};
+
+void register_builtins(struct grub2_script *script)
+{
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(commands); i++)
+ script_register_command(script, &commands[i]);
+}
struct grub2_argv {
struct list words;
+
+ /* postprocessing (with process_expansions) populates these to hand to
+ * the grub2_command callbacks */
+ char **argv;
+ int argc;
};
struct grub2_statements {
struct grub2_statements *statements;
};
+struct grub2_command {
+ const char *name;
+ int (*exec)(struct grub2_script *script,
+ int argc, char *argv[]);
+ struct list_item list;
+};
+
struct grub2_script {
struct grub2_statements *statements;
struct list environment;
+ struct list commands;
+ struct list symtab;
};
struct grub2_parser {
struct grub2_script *create_script(void *ctx);
+const char *script_env_get(struct grub2_script *script, const char *name);
+
+void script_env_set(struct grub2_script *script,
+ const char *name, const char *value);
+
+void script_register_command(struct grub2_script *script,
+ struct grub2_command *command);
+
+struct grub2_command *script_lookup_command(struct grub2_script *script,
+ const char *name);
+
+void register_builtins(struct grub2_script *script);
#endif /* GRUB2_H */
struct list_item list;
};
-static const char *env_lookup(struct grub2_script *script,
- const char *name)
+const char *script_env_get(struct grub2_script *script, const char *name)
{
struct env_entry *entry;
return NULL;
}
+void script_env_set(struct grub2_script *script,
+ const char *name, const char *value)
+{
+ struct env_entry *e, *entry = NULL;
+
+ list_for_each_entry(&script->environment, e, list) {
+ if (!strcmp(e->name, name)) {
+ entry = e;
+ break;
+ }
+ }
+
+ if (!entry) {
+ entry = talloc(script, struct env_entry);
+ entry->name = name;
+ list_add(&script->environment, &entry->list);
+ }
+
+ entry->value = value;
+}
+
static bool expand_var(struct grub2_script *script, struct grub2_word *word)
{
const char *val;
- val = env_lookup(script, word->var.name);
+ val = script_env_get(script, word->var.name);
if (!val)
val = "";
return true;
}
-/* iterate through the words in an argv, looking for GRUB2_WORD_VAR expansions.
+/* iterate through the words in an argv_list, looking for GRUB2_WORD_VAR
+ * expansions.
*
* Once that's done, we may (if split == true) have to split the word to create
* new argv items
struct grub2_argv *argv)
{
struct grub2_word *top_word, *word;
+ int i;
+
+ argv->argc = 0;
list_for_each_entry(&argv->words, top_word, argv_list) {
+ argv->argc++;
+
/* expand vars and squash the list of words into the top struct.
* todo: splitting
*/
}
top_word->next = NULL;
}
+
+ argv->argv = talloc_array(script, char *, argv->argc);
+ i = 0;
+
+ list_for_each_entry(&argv->words, word, argv_list)
+ argv->argv[i++] = word->text;
}
int statements_execute(struct grub2_script *script,
struct grub2_statement *statement)
{
struct grub2_statement_simple *st = to_stmt_simple(statement);
+ struct grub2_command *cmd;
+ int rc;
if (!st->argv)
return 0;
process_expansions(script, st->argv);
- return 0;
+ if (!st->argv->argc)
+ return 0;
+
+ cmd = script_lookup_command(script, st->argv->argv[0]);
+ if (!cmd) {
+ fprintf(stderr, "undefined command '%s'\n", st->argv->argv[0]);
+ return 0;
+ }
+
+ rc = cmd->exec(script, st->argv->argc, st->argv->argv);
+
+ return rc;
}
int statement_if_execute(struct grub2_script *script,
list_add(&script->environment, &env->list);
}
+struct grub2_command *script_lookup_command(struct grub2_script *script,
+ const char *name)
+{
+ struct grub2_command *command;
+
+ list_for_each_entry(&script->commands, command, list) {
+ if (!strcmp(command->name, name))
+ return command;
+ }
+
+ return NULL;
+}
+
+void script_register_command(struct grub2_script *script,
+ struct grub2_command *command)
+{
+ list_add(&script->commands, &command->list);
+}
+
void script_execute(struct grub2_script *script)
{
script = talloc(ctx, struct grub2_script);
init_env(script);
+ list_init(&script->commands);
+ register_builtins(script);
return script;
}