discover/pb-discover: #include <locale.h> for musl libc
[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                 STMT_TYPE_CONDITIONAL,
47         } type;
48         int                     (*exec)(struct grub2_script *,
49                                         struct grub2_statement *);
50 };
51
52 struct grub2_statement_simple {
53         struct grub2_statement  st;
54         struct grub2_argv       *argv;
55 };
56
57 struct grub2_statement_menuentry {
58         struct grub2_statement  st;
59         struct grub2_argv       *argv;
60         struct grub2_statements *statements;
61 };
62
63 struct grub2_statement_conditional {
64         struct grub2_statement  st;
65         struct grub2_statement  *condition;
66         struct grub2_statements *statements;
67 };
68
69 struct grub2_statement_if {
70         struct grub2_statement  st;
71         struct grub2_statements *conditionals;
72         struct grub2_statements *else_case;
73 };
74
75 struct grub2_statement_block {
76         struct grub2_statement  st;
77         struct grub2_statements *statements;
78 };
79
80 struct grub2_statement_function {
81         struct grub2_statement  st;
82         struct grub2_word       *name;
83         struct grub2_statements *body;
84 };
85
86 struct grub2_statement_for {
87         struct grub2_statement  st;
88         struct grub2_word       *var;
89         struct grub2_argv       *list;
90         struct grub2_statements *body;
91 };
92
93 struct grub2_script {
94         struct grub2_statements         *statements;
95         struct list                     environment;
96         struct list                     symtab;
97         struct discover_context         *ctx;
98         struct discover_boot_option     *opt;
99         const char                      *filename;
100         unsigned int                    n_options;
101         struct list                     options;
102 };
103
104 struct grub2_parser {
105         void                    *scanner;
106         struct grub2_script     *script;
107         bool                    inter_word;
108 };
109
110 /* type for builtin functions */
111 typedef int (*grub2_function)(struct grub2_script *script, void *data,
112                                 int argc, char *argv[]);
113
114 struct grub2_statements *create_statements(struct grub2_parser *parser);
115
116 struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
117                 struct grub2_argv *argv);
118
119 struct grub2_statement *create_statement_menuentry(struct grub2_parser *parser,
120                 struct grub2_argv *argv, struct grub2_statements *stmts);
121
122 struct grub2_statement *create_statement_conditional(
123                 struct grub2_parser *parser, struct grub2_statement *condition,
124                 struct grub2_statements *statements);
125
126 struct grub2_statement *create_statement_if(struct grub2_parser *parser,
127                 struct grub2_statement *conditional,
128                 struct grub2_statements *elifs,
129                 struct grub2_statements *else_case);
130
131 struct grub2_statement *create_statement_block(struct grub2_parser *parser,
132                 struct grub2_statements *stmts);
133
134 struct grub2_statement *create_statement_function(struct grub2_parser *parser,
135                 struct grub2_word *name, struct grub2_statements *body);
136
137 struct grub2_statement *create_statement_for(struct grub2_parser *parser,
138                 struct grub2_word *var, struct grub2_argv *list,
139                 struct grub2_statements *body);
140
141 struct grub2_word *create_word_text(struct grub2_parser *parser,
142                 const char *text);
143
144 struct grub2_word *create_word_var(struct grub2_parser *parser,
145                 const char *name, bool split);
146
147 struct grub2_argv *create_argv(struct grub2_parser *parser);
148
149 void statement_append(struct grub2_statements *stmts,
150                 struct grub2_statement *stmt);
151
152 void argv_append(struct grub2_argv *argv, struct grub2_word *word);
153
154 void word_append(struct grub2_word *w1, struct grub2_word *w2);
155
156 /* script interface */
157 void script_execute(struct grub2_script *script);
158
159 int statement_simple_execute(struct grub2_script *script,
160                 struct grub2_statement *statement);
161 int statement_block_execute(struct grub2_script *script,
162                 struct grub2_statement *statement);
163 int statement_if_execute(struct grub2_script *script,
164                 struct grub2_statement *statement);
165 int statement_menuentry_execute(struct grub2_script *script,
166                 struct grub2_statement *statement);
167 int statement_function_execute(struct grub2_script *script,
168                 struct grub2_statement *statement);
169 int statement_for_execute(struct grub2_script *script,
170                 struct grub2_statement *statement);
171
172 struct grub2_script *create_script(struct grub2_parser *parser,
173                 struct discover_context *ctx);
174
175 const char *script_env_get(struct grub2_script *script, const char *name);
176
177 void script_env_set(struct grub2_script *script,
178                 const char *name, const char *value);
179
180 void script_register_function(struct grub2_script *script,
181                 const char *name, grub2_function fn, void *data);
182
183 void register_builtins(struct grub2_script *script);
184
185 /* resources */
186 struct resource *create_grub2_resource(struct discover_boot_option *opt,
187                 struct discover_device *orig_device,
188                 const char *root, const char *path);
189
190 bool resolve_grub2_resource(struct device_handler *handler,
191                 struct resource *res);
192
193 /* external parser api */
194 struct grub2_parser *grub2_parser_create(struct discover_context *ctx);
195 void grub2_parser_parse(struct grub2_parser *parser, const char *filename,
196                 char *buf, int len);
197 #endif /* GRUB2_H */
198