765bd873e2fa4d333062fb5ce9484647593c2db2
[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 = NULL;
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->text = talloc_strdup(word, text);
77         word->next = NULL;
78         return word;
79 }
80
81 struct grub2_word *create_word_var(struct grub2_parser *parser,
82                 const char *name, bool split)
83 {
84         struct grub2_word *word = talloc(parser, struct grub2_word);
85         word->type = GRUB2_WORD_VAR;
86         word->var.name = talloc_strdup(word, name);
87         word->var.split = split;
88         word->next = NULL;
89         return word;
90 }
91
92 struct grub2_argv *create_argv(struct grub2_parser *parser)
93 {
94         struct grub2_argv *argv = talloc(parser, struct grub2_argv);
95         list_init(&argv->words);
96         return argv;
97 }
98
99 void argv_append(struct grub2_argv *argv, struct grub2_word *word)
100 {
101         list_add_tail(&argv->words, &word->argv_list);
102 }
103
104 void word_append(struct grub2_word *w1, struct grub2_word *w2)
105 {
106         w1->next = w2;
107 }