discover/grub2: Handle var tokens in lexer
[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 list_item        argv_list;
23 };
24
25 struct grub2_argv {
26         struct list             words;
27 };
28
29 struct grub2_statements {
30         struct list             list;
31 };
32
33 struct grub2_statement {
34         struct list_item        list;
35         enum {
36                 STMT_TYPE_SIMPLE,
37                 STMT_TYPE_MENUENTRY,
38                 STMT_TYPE_IF,
39                 STMT_TYPE_BLOCK,
40         } type;
41         int                     (*exec)(struct grub2_script *,
42                                         struct grub2_statement *);
43 };
44
45 struct grub2_statement_simple {
46         struct grub2_statement  st;
47         struct grub2_argv       *argv;
48 };
49
50 struct grub2_statement_menuentry {
51         struct grub2_statement  st;
52         struct grub2_argv       *argv;
53         struct grub2_statements *statements;
54 };
55
56 struct grub2_statement_if {
57         struct grub2_statement  st;
58         struct grub2_statement  *condition;
59         struct grub2_statements *true_case;
60         struct grub2_statements *false_case;
61 };
62
63 struct grub2_statement_block {
64         struct grub2_statement  st;
65         struct grub2_statements *statements;
66 };
67
68 struct grub2_script {
69         struct grub2_statements *statements;
70         struct list             environment;
71 };
72
73 struct grub2_parser {
74         void                    *scanner;
75         struct grub2_script     *script;
76 };
77
78 struct grub2_statements *create_statements(struct grub2_parser *parser);
79
80 struct grub2_statement *create_statement_simple(struct grub2_parser *parser,
81                 struct grub2_argv *argv);
82
83 struct grub2_statement *create_statement_menuentry(struct grub2_parser *parser,
84                 struct grub2_argv *argv, struct grub2_statements *stmts);
85
86 struct grub2_statement *create_statement_if(struct grub2_parser *parser,
87                 struct grub2_statement *condition,
88                 struct grub2_statements *true_case,
89                 struct grub2_statements *false_case);
90
91 struct grub2_statement *create_statement_block(struct grub2_parser *parser,
92                 struct grub2_statements *stmts);
93
94 struct grub2_word *create_word_text(struct grub2_parser *parser,
95                 const char *text);
96
97 struct grub2_word *create_word_var(struct grub2_parser *parser,
98                 const char *name, bool split);
99
100 struct grub2_argv *create_argv(struct grub2_parser *parser);
101
102 void statement_append(struct grub2_statements *stmts,
103                 struct grub2_statement *stmt);
104
105 void argv_append(struct grub2_argv *argv, struct grub2_word *word);
106
107 void word_append(struct grub2_word *w1, struct grub2_word *w2);
108
109 /* script interface */
110 void script_execute(struct grub2_script *script);
111
112 int statement_simple_execute(struct grub2_script *script,
113                 struct grub2_statement *statement);
114 int statement_if_execute(struct grub2_script *script,
115                 struct grub2_statement *statement);
116
117 struct grub2_script *create_script(void *ctx);
118
119 #endif /* GRUB2_H */
120