discover/grub2: Add menuentry execution
[petitboot] / discover / grub2 / grub2.h
1 #ifndef GRUB2_H
2 #define GRUB2_H
3
4 #include <stdbool.h>
5 #include <list/list.h>
6
7 struct grub2_script;
8
9 struct grub2_word {
10         enum {
11                 GRUB2_WORD_TEXT,
12                 GRUB2_WORD_VAR,
13         } type;
14         union {
15                 char    *text;
16                 struct {
17                         const char      *name;
18                         bool            split;
19                 } var;
20         };
21         struct grub2_word       *next;
22         struct grub2_word       *last;
23         struct list_item        argv_list;
24 };
25
26 struct grub2_argv {
27         struct list             words;
28 };
29
30 struct grub2_statements {
31         struct list             list;
32 };
33
34 struct grub2_statement {
35         struct list_item        list;
36         enum {
37                 STMT_TYPE_SIMPLE,
38                 STMT_TYPE_MENUENTRY,
39                 STMT_TYPE_IF,
40                 STMT_TYPE_BLOCK,
41         } type;
42         int                     (*exec)(struct grub2_script *,
43                                         struct grub2_statement *);
44 };
45
46 struct grub2_statement_simple {
47         struct grub2_statement  st;
48         struct grub2_argv       *argv;
49 };
50
51 struct grub2_statement_menuentry {
52         struct grub2_statement  st;
53         struct grub2_argv       *argv;
54         struct grub2_statements *statements;
55 };
56
57 struct grub2_statement_if {
58         struct grub2_statement  st;
59         struct grub2_statement  *condition;
60         struct grub2_statements *true_case;
61         struct grub2_statements *false_case;
62 };
63
64 struct grub2_statement_block {
65         struct grub2_statement  st;
66         struct grub2_statements *statements;
67 };
68
69 struct grub2_script {
70         struct grub2_statements *statements;
71         struct list             environment;
72 };
73
74 struct grub2_parser {
75         void                    *scanner;
76         struct grub2_script     *script;
77 };
78
79 struct grub2_statements *create_statements(struct grub2_parser *parser);
80
81 struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
82                 struct grub2_argv *argv);
83
84 struct grub2_statement *create_statement_menuentry(struct grub2_parser *parser,
85                 struct grub2_argv *argv, struct grub2_statements *stmts);
86
87 struct grub2_statement *create_statement_if(struct grub2_parser *parser,
88                 struct grub2_statement *condition,
89                 struct grub2_statements *true_case,
90                 struct grub2_statements *false_case);
91
92 struct grub2_statement *create_statement_block(struct grub2_parser *parser,
93                 struct grub2_statements *stmts);
94
95 struct grub2_word *create_word_text(struct grub2_parser *parser,
96                 const char *text);
97
98 struct grub2_word *create_word_var(struct grub2_parser *parser,
99                 const char *name, bool split);
100
101 struct grub2_argv *create_argv(struct grub2_parser *parser);
102
103 void statement_append(struct grub2_statements *stmts,
104                 struct grub2_statement *stmt);
105
106 void argv_append(struct grub2_argv *argv, struct grub2_word *word);
107
108 void word_append(struct grub2_word *w1, struct grub2_word *w2);
109
110 /* script interface */
111 void script_execute(struct grub2_script *script);
112
113 int statement_simple_execute(struct grub2_script *script,
114                 struct grub2_statement *statement);
115 int statement_if_execute(struct grub2_script *script,
116                 struct grub2_statement *statement);
117 int statement_menuentry_execute(struct grub2_script *script,
118                 struct grub2_statement *statement);
119
120 struct grub2_script *create_script(void *ctx);
121
122 #endif /* GRUB2_H */
123