discover/grub2: Add option state in menuentry processing
[petitboot] / discover / grub2 / grub2.h
1 #ifndef GRUB2_H
2 #define GRUB2_H
3
4 #include <discover/device-handler.h>
5
6 #include <stdbool.h>
7 #include <list/list.h>
8
9 struct grub2_script;
10
11 struct grub2_word {
12         enum {
13                 GRUB2_WORD_TEXT,
14                 GRUB2_WORD_VAR,
15         } type;
16         union {
17                 char            *text;
18                 const char      *name;
19         };
20         bool                    split;
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         /* postprocessing (with process_expansions) populates these to hand to
30          * the grub2_command callbacks */
31         char                    **argv;
32         int                     argc;
33 };
34
35 struct grub2_statements {
36         struct list             list;
37 };
38
39 struct grub2_statement {
40         struct list_item        list;
41         enum {
42                 STMT_TYPE_SIMPLE,
43                 STMT_TYPE_MENUENTRY,
44                 STMT_TYPE_IF,
45                 STMT_TYPE_BLOCK,
46         } type;
47         int                     (*exec)(struct grub2_script *,
48                                         struct grub2_statement *);
49 };
50
51 struct grub2_statement_simple {
52         struct grub2_statement  st;
53         struct grub2_argv       *argv;
54 };
55
56 struct grub2_statement_menuentry {
57         struct grub2_statement  st;
58         struct grub2_argv       *argv;
59         struct grub2_statements *statements;
60 };
61
62 struct grub2_statement_if {
63         struct grub2_statement  st;
64         struct grub2_statement  *condition;
65         struct grub2_statements *true_case;
66         struct grub2_statements *false_case;
67 };
68
69 struct grub2_statement_block {
70         struct grub2_statement  st;
71         struct grub2_statements *statements;
72 };
73
74 struct grub2_command {
75         const char              *name;
76         int                     (*exec)(struct grub2_script *script,
77                                         int argc, char *argv[]);
78         struct list_item        list;
79 };
80
81 struct grub2_script {
82         struct grub2_statements         *statements;
83         struct list                     environment;
84         struct list                     commands;
85         struct list                     symtab;
86         struct discover_context         *ctx;
87         struct discover_boot_option     *opt;
88 };
89
90 struct grub2_parser {
91         void                    *scanner;
92         struct grub2_script     *script;
93 };
94
95 struct grub2_root {
96         char *uuid;
97 };
98
99 struct grub2_statements *create_statements(struct grub2_parser *parser);
100
101 struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
102                 struct grub2_argv *argv);
103
104 struct grub2_statement *create_statement_menuentry(struct grub2_parser *parser,
105                 struct grub2_argv *argv, struct grub2_statements *stmts);
106
107 struct grub2_statement *create_statement_if(struct grub2_parser *parser,
108                 struct grub2_statement *condition,
109                 struct grub2_statements *true_case,
110                 struct grub2_statements *false_case);
111
112 struct grub2_statement *create_statement_block(struct grub2_parser *parser,
113                 struct grub2_statements *stmts);
114
115 struct grub2_word *create_word_text(struct grub2_parser *parser,
116                 const char *text);
117
118 struct grub2_word *create_word_var(struct grub2_parser *parser,
119                 const char *name, bool split);
120
121 struct grub2_argv *create_argv(struct grub2_parser *parser);
122
123 void statement_append(struct grub2_statements *stmts,
124                 struct grub2_statement *stmt);
125
126 void argv_append(struct grub2_argv *argv, struct grub2_word *word);
127
128 void word_append(struct grub2_word *w1, struct grub2_word *w2);
129
130 /* script interface */
131 void script_execute(struct grub2_script *script);
132
133 int statement_simple_execute(struct grub2_script *script,
134                 struct grub2_statement *statement);
135 int statement_if_execute(struct grub2_script *script,
136                 struct grub2_statement *statement);
137 int statement_menuentry_execute(struct grub2_script *script,
138                 struct grub2_statement *statement);
139
140 struct grub2_script *create_script(struct grub2_parser *parser,
141                 struct discover_context *ctx);
142
143 const char *script_env_get(struct grub2_script *script, const char *name);
144
145 void script_env_set(struct grub2_script *script,
146                 const char *name, const char *value);
147
148 void script_register_command(struct grub2_script *script,
149                 struct grub2_command *command);
150
151 struct grub2_command *script_lookup_command(struct grub2_script *script,
152                 const char *name);
153
154 void register_builtins(struct grub2_script *script);
155
156 /* resources */
157 struct resource *create_grub2_resource(void *ctx,
158                 struct discover_device *orig_device,
159                 struct grub2_root *root, const char *path);
160
161 bool resolve_grub2_resource(struct device_handler *handler,
162                 struct resource *res);
163
164 /* external parser api */
165 struct grub2_parser *grub2_parser_create(struct discover_context *ctx);
166 void grub2_parser_parse(struct grub2_parser *parser, char *buf, int len);
167 #endif /* GRUB2_H */
168