9da5ceeed2a013ca171658d92a00d73998a042ca
[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                 const char      *name;
17         };
18         bool                    split;
19         struct grub2_word       *next;
20         struct grub2_word       *last;
21         struct list_item        argv_list;
22 };
23
24 struct grub2_argv {
25         struct list             words;
26
27         /* postprocessing (with process_expansions) populates these to hand to
28          * the grub2_command callbacks */
29         char                    **argv;
30         int                     argc;
31 };
32
33 struct grub2_statements {
34         struct list             list;
35 };
36
37 struct grub2_statement {
38         struct list_item        list;
39         enum {
40                 STMT_TYPE_SIMPLE,
41                 STMT_TYPE_MENUENTRY,
42                 STMT_TYPE_IF,
43                 STMT_TYPE_BLOCK,
44         } type;
45         int                     (*exec)(struct grub2_script *,
46                                         struct grub2_statement *);
47 };
48
49 struct grub2_statement_simple {
50         struct grub2_statement  st;
51         struct grub2_argv       *argv;
52 };
53
54 struct grub2_statement_menuentry {
55         struct grub2_statement  st;
56         struct grub2_argv       *argv;
57         struct grub2_statements *statements;
58 };
59
60 struct grub2_statement_if {
61         struct grub2_statement  st;
62         struct grub2_statement  *condition;
63         struct grub2_statements *true_case;
64         struct grub2_statements *false_case;
65 };
66
67 struct grub2_statement_block {
68         struct grub2_statement  st;
69         struct grub2_statements *statements;
70 };
71
72 struct grub2_command {
73         const char              *name;
74         int                     (*exec)(struct grub2_script *script,
75                                         int argc, char *argv[]);
76         struct list_item        list;
77 };
78
79 struct grub2_script {
80         struct grub2_statements *statements;
81         struct list             environment;
82         struct list             commands;
83         struct list             symtab;
84 };
85
86 struct grub2_parser {
87         void                    *scanner;
88         struct grub2_script     *script;
89 };
90
91 struct grub2_statements *create_statements(struct grub2_parser *parser);
92
93 struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
94                 struct grub2_argv *argv);
95
96 struct grub2_statement *create_statement_menuentry(struct grub2_parser *parser,
97                 struct grub2_argv *argv, struct grub2_statements *stmts);
98
99 struct grub2_statement *create_statement_if(struct grub2_parser *parser,
100                 struct grub2_statement *condition,
101                 struct grub2_statements *true_case,
102                 struct grub2_statements *false_case);
103
104 struct grub2_statement *create_statement_block(struct grub2_parser *parser,
105                 struct grub2_statements *stmts);
106
107 struct grub2_word *create_word_text(struct grub2_parser *parser,
108                 const char *text);
109
110 struct grub2_word *create_word_var(struct grub2_parser *parser,
111                 const char *name, bool split);
112
113 struct grub2_argv *create_argv(struct grub2_parser *parser);
114
115 void statement_append(struct grub2_statements *stmts,
116                 struct grub2_statement *stmt);
117
118 void argv_append(struct grub2_argv *argv, struct grub2_word *word);
119
120 void word_append(struct grub2_word *w1, struct grub2_word *w2);
121
122 /* script interface */
123 void script_execute(struct grub2_script *script);
124
125 int statement_simple_execute(struct grub2_script *script,
126                 struct grub2_statement *statement);
127 int statement_if_execute(struct grub2_script *script,
128                 struct grub2_statement *statement);
129 int statement_menuentry_execute(struct grub2_script *script,
130                 struct grub2_statement *statement);
131
132 struct grub2_script *create_script(void *ctx);
133
134 const char *script_env_get(struct grub2_script *script, const char *name);
135
136 void script_env_set(struct grub2_script *script,
137                 const char *name, const char *value);
138
139 void script_register_command(struct grub2_script *script,
140                 struct grub2_command *command);
141
142 struct grub2_command *script_lookup_command(struct grub2_script *script,
143                 const char *name);
144
145 void register_builtins(struct grub2_script *script);
146 #endif /* GRUB2_H */
147