discover/grub2: Implement multiple-arv variable splitting
[petitboot] / discover / grub2 / parser-api.c
1
2 #include <talloc/talloc.h>
3
4 #include "grub2.h"
5
6 struct grub2_statements *create_statements(struct grub2_parser *parser)
7 {
8         struct grub2_statements *stmts = talloc(parser,
9                         struct grub2_statements);
10         list_init(&stmts->list);
11         return stmts;
12 }
13
14 struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
15                 struct grub2_argv *argv)
16 {
17         struct grub2_statement_simple *stmt =
18                 talloc(parser, struct grub2_statement_simple);
19         stmt->st.type = STMT_TYPE_SIMPLE;
20         stmt->st.exec = statement_simple_execute;
21         stmt->argv = argv;
22         return &stmt->st;
23 }
24
25 struct grub2_statement *create_statement_menuentry(struct grub2_parser *parser,
26                 struct grub2_argv *argv, struct grub2_statements *stmts)
27 {
28         struct grub2_statement_menuentry *stmt =
29                 talloc(parser, struct grub2_statement_menuentry);
30         stmt->st.type = STMT_TYPE_MENUENTRY;
31         stmt->st.exec = statement_menuentry_execute;
32         stmt->argv = argv;
33         stmt->statements = stmts;
34         return &stmt->st;
35 }
36
37 struct grub2_statement *create_statement_if(struct grub2_parser *parser,
38                 struct grub2_statement *condition,
39                 struct grub2_statements *true_case,
40                 struct grub2_statements *false_case)
41 {
42         struct grub2_statement_if *stmt =
43                 talloc(parser, struct grub2_statement_if);
44         stmt->st.type = STMT_TYPE_IF;
45         stmt->st.exec = statement_if_execute;
46         stmt->condition = condition;
47         stmt->true_case = true_case;
48         stmt->false_case = false_case;
49         return &stmt->st;
50 }
51
52 struct grub2_statement *create_statement_block(struct grub2_parser *parser,
53                 struct grub2_statements *stmts)
54 {
55         struct grub2_statement_block *stmt =
56                 talloc(parser, struct grub2_statement_block);
57         stmt->st.type = STMT_TYPE_BLOCK;
58         stmt->st.exec = NULL;
59         stmt->statements = stmts;
60         return &stmt->st;
61 }
62
63 void statement_append(struct grub2_statements *stmts,
64                 struct grub2_statement *stmt)
65 {
66         if (!stmt)
67                 return;
68         list_add_tail(&stmts->list, &stmt->list);
69 }
70
71 struct grub2_word *create_word_text(struct grub2_parser *parser,
72                 const char *text)
73 {
74         struct grub2_word *word = talloc(parser, struct grub2_word);
75         word->type = GRUB2_WORD_TEXT;
76         word->split = false;
77         word->text = talloc_strdup(word, text);
78         word->next = NULL;
79         word->last = word;
80         return word;
81 }
82
83 struct grub2_word *create_word_var(struct grub2_parser *parser,
84                 const char *name, bool split)
85 {
86         struct grub2_word *word = talloc(parser, struct grub2_word);
87         word->type = GRUB2_WORD_VAR;
88         word->name = talloc_strdup(word, name);
89         word->split = split;
90         word->next = NULL;
91         word->last = word;
92         return word;
93 }
94
95 struct grub2_argv *create_argv(struct grub2_parser *parser)
96 {
97         struct grub2_argv *argv = talloc(parser, struct grub2_argv);
98         list_init(&argv->words);
99         return argv;
100 }
101
102 void argv_append(struct grub2_argv *argv, struct grub2_word *word)
103 {
104         list_add_tail(&argv->words, &word->argv_list);
105 }
106
107 void word_append(struct grub2_word *w1, struct grub2_word *w2)
108 {
109         w1->last->next = w2;
110         w1->last = w2;
111 }