9ccf15eb94ffefdf7fe53ae96f3def672edaab2d
[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(struct grub2_parser *parser, const char *text,
72                 bool expand, bool split)
73 {
74         struct grub2_word *word = talloc(parser, struct grub2_word);
75         word->text = talloc_strdup(word, text);
76         word->expand = expand;
77         word->split = split;
78         word->next = NULL;
79         return word;
80 }
81
82 struct grub2_argv *create_argv(struct grub2_parser *parser)
83 {
84         struct grub2_argv *argv = talloc(parser, struct grub2_argv);
85         list_init(&argv->words);
86         return argv;
87 }
88
89 void argv_append(struct grub2_argv *argv, struct grub2_word *word)
90 {
91         list_add_tail(&argv->words, &word->argv_list);
92 }
93
94 void word_append(struct grub2_word *w1, struct grub2_word *w2)
95 {
96         w1->next = w2;
97 }