summaryrefslogtreecommitdiffstats
path: root/discover/grub2/grub2.h
blob: 68176fbe3e18cca5f4ed010204812b7312f7b423 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#ifndef GRUB2_H
#define GRUB2_H

#include <discover/device-handler.h>

#include <stdbool.h>
#include <list/list.h>

struct grub2_script;

struct grub2_word {
	enum {
		GRUB2_WORD_TEXT,
		GRUB2_WORD_VAR,
	} type;
	union {
		char		*text;
		const char	*name;
	};
	bool			split;
	struct grub2_word	*next;
	struct grub2_word	*last;
	struct list_item	argv_list;
};

struct grub2_argv {
	struct list		words;

	/* postprocessing (with process_expansions) populates these to hand to
	 * the grub2_command callbacks */
	char			**argv;
	int			argc;
};

struct grub2_statements {
	struct list		list;
};

struct grub2_statement {
	struct list_item	list;
	enum {
		STMT_TYPE_SIMPLE,
		STMT_TYPE_MENUENTRY,
		STMT_TYPE_IF,
		STMT_TYPE_BLOCK,
		STMT_TYPE_CONDITIONAL,
	} type;
	int			(*exec)(struct grub2_script *,
					struct grub2_statement *);
};

struct grub2_statement_simple {
	struct grub2_statement	st;
	struct grub2_argv	*argv;
};

struct grub2_statement_menuentry {
	struct grub2_statement	st;
	struct grub2_argv	*argv;
	struct grub2_statements	*statements;
};

struct grub2_statement_conditional {
	struct grub2_statement	st;
	struct grub2_statement	*condition;
	struct grub2_statements	*statements;
};

struct grub2_statement_if {
	struct grub2_statement	st;
	struct grub2_statements	*conditionals;
	struct grub2_statements	*else_case;
};

struct grub2_statement_block {
	struct grub2_statement	st;
	struct grub2_statements	*statements;
};

struct grub2_statement_function {
	struct grub2_statement	st;
	struct grub2_word	*name;
	struct grub2_statements	*body;
};

struct grub2_statement_for {
	struct grub2_statement	st;
	struct grub2_word	*var;
	struct grub2_argv	*list;
	struct grub2_statements	*body;
};

struct grub2_script {
	struct grub2_statements		*statements;
	struct list			environment;
	struct list			symtab;
	struct discover_context		*ctx;
	struct discover_boot_option	*opt;
	const char			*filename;
	unsigned int			n_options;
	struct list			options;
};

struct grub2_parser {
	void			*scanner;
	struct grub2_script	*script;
	bool			inter_word;
};

/* type for builtin functions */
typedef int (*grub2_function)(struct grub2_script *script, void *data,
				int argc, char *argv[]);

struct grub2_statements *create_statements(struct grub2_parser *parser);

struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
		struct grub2_argv *argv);

struct grub2_statement *create_statement_menuentry(struct grub2_parser *parser,
		struct grub2_argv *argv, struct grub2_statements *stmts);

struct grub2_statement *create_statement_conditional(
		struct grub2_parser *parser, struct grub2_statement *condition,
		struct grub2_statements *statements);

struct grub2_statement *create_statement_if(struct grub2_parser *parser,
		struct grub2_statement *conditional,
		struct grub2_statements *elifs,
		struct grub2_statements *else_case);

struct grub2_statement *create_statement_block(struct grub2_parser *parser,
		struct grub2_statements *stmts);

struct grub2_statement *create_statement_function(struct grub2_parser *parser,
		struct grub2_word *name, struct grub2_statements *body);

struct grub2_statement *create_statement_for(struct grub2_parser *parser,
		struct grub2_word *var, struct grub2_argv *list,
		struct grub2_statements *body);

struct grub2_word *create_word_text(struct grub2_parser *parser,
		const char *text);

struct grub2_word *create_word_var(struct grub2_parser *parser,
		const char *name, bool split);

struct grub2_argv *create_argv(struct grub2_parser *parser);

void statement_append(struct grub2_statements *stmts,
		struct grub2_statement *stmt);

void argv_append(struct grub2_argv *argv, struct grub2_word *word);

void word_append(struct grub2_word *w1, struct grub2_word *w2);

/* script interface */
void script_execute(struct grub2_script *script);

int statement_simple_execute(struct grub2_script *script,
		struct grub2_statement *statement);
int statement_block_execute(struct grub2_script *script,
		struct grub2_statement *statement);
int statement_if_execute(struct grub2_script *script,
		struct grub2_statement *statement);
int statement_menuentry_execute(struct grub2_script *script,
		struct grub2_statement *statement);
int statement_function_execute(struct grub2_script *script,
		struct grub2_statement *statement);
int statement_for_execute(struct grub2_script *script,
		struct grub2_statement *statement);

struct grub2_script *create_script(struct grub2_parser *parser,
		struct discover_context *ctx);

const char *script_env_get(struct grub2_script *script, const char *name);

void script_env_set(struct grub2_script *script,
		const char *name, const char *value);

void script_register_function(struct grub2_script *script,
		const char *name, grub2_function fn, void *data);

void register_builtins(struct grub2_script *script);

/* resources */
struct resource *create_grub2_resource(struct discover_boot_option *opt,
		struct discover_device *orig_device,
		const char *root, const char *path);

bool resolve_grub2_resource(struct device_handler *handler,
		struct resource *res);

/* external parser api */
struct grub2_parser *grub2_parser_create(struct discover_context *ctx);
void grub2_parser_parse(struct grub2_parser *parser, const char *filename,
		char *buf, int len);
#endif /* GRUB2_H */

OpenPOWER on IntegriCloud