]> git.proxmox.com Git - grub2.git/blob - include/grub/script_sh.h
pull-in block-arg branch
[grub2.git] / include / grub / script_sh.h
1 /* normal_parser.h */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2009,2010 Free Software Foundation, Inc.
5 *
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef GRUB_NORMAL_PARSER_HEADER
21 #define GRUB_NORMAL_PARSER_HEADER 1
22
23 #include <grub/types.h>
24 #include <grub/err.h>
25 #include <grub/parser.h>
26
27 struct grub_script_mem;
28
29 /* The generic header for each scripting command or structure. */
30 struct grub_script_cmd
31 {
32 /* This function is called to execute the command. */
33 grub_err_t (*exec) (struct grub_script_cmd *cmd);
34
35 /* The next command. This can be used by the parent to form a chain
36 of commands. */
37 struct grub_script_cmd *next;
38 };
39
40 struct grub_script
41 {
42 unsigned refcnt;
43 struct grub_script_mem *mem;
44 struct grub_script_cmd *cmd;
45 };
46 \f
47 typedef enum
48 {
49 GRUB_SCRIPT_ARG_TYPE_VAR,
50 GRUB_SCRIPT_ARG_TYPE_TEXT,
51 GRUB_SCRIPT_ARG_TYPE_DQVAR,
52 GRUB_SCRIPT_ARG_TYPE_DQSTR,
53 GRUB_SCRIPT_ARG_TYPE_SQSTR,
54 GRUB_SCRIPT_ARG_TYPE_BLOCK
55 } grub_script_arg_type_t;
56
57 /* A part of an argument. */
58 struct grub_script_arg
59 {
60 grub_script_arg_type_t type;
61
62 char *str;
63
64 /* Parsed block argument. */
65 struct grub_script *block;
66
67 /* Next argument part. */
68 struct grub_script_arg *next;
69 };
70
71 /* An argument vector. */
72 struct grub_script_argv
73 {
74 unsigned argc;
75 char **args;
76 struct grub_script **scripts;
77 };
78
79 /* A complete argument. It consists of a list of one or more `struct
80 grub_script_arg's. */
81 struct grub_script_arglist
82 {
83 struct grub_script_arglist *next;
84 struct grub_script_arg *arg;
85 /* Only stored in the first link. */
86 int argcount;
87 };
88
89 /* A single command line. */
90 struct grub_script_cmdline
91 {
92 struct grub_script_cmd cmd;
93
94 /* The arguments for this command. */
95 struct grub_script_arglist *arglist;
96 };
97
98 /* An if statement. */
99 struct grub_script_cmdif
100 {
101 struct grub_script_cmd cmd;
102
103 /* The command used to check if the 'if' is true or false. */
104 struct grub_script_cmd *exec_to_evaluate;
105
106 /* The code executed in case the result of 'if' was true. */
107 struct grub_script_cmd *exec_on_true;
108
109 /* The code executed in case the result of 'if' was false. */
110 struct grub_script_cmd *exec_on_false;
111 };
112
113 /* A for statement. */
114 struct grub_script_cmdfor
115 {
116 struct grub_script_cmd cmd;
117
118 /* The name used as looping variable. */
119 struct grub_script_arg *name;
120
121 /* The words loop iterates over. */
122 struct grub_script_arglist *words;
123
124 /* The command list executed in each loop. */
125 struct grub_script_cmd *list;
126 };
127
128 /* A while/until command. */
129 struct grub_script_cmdwhile
130 {
131 struct grub_script_cmd cmd;
132
133 /* The command list used as condition. */
134 struct grub_script_cmd *cond;
135
136 /* The command list executed in each loop. */
137 struct grub_script_cmd *list;
138
139 /* The flag to indicate this as "until" loop. */
140 int until;
141 };
142
143 /* State of the lexer as passed to the lexer. */
144 struct grub_lexer_param
145 {
146 /* Function used by the lexer to get a new line when more input is
147 expected, but not available. */
148 grub_reader_getline_t getline;
149
150 /* A reference counter. If this is >0 it means that the parser
151 expects more tokens and `getline' should be called to fetch more.
152 Otherwise the lexer can stop processing if the current buffer is
153 depleted. */
154 int refs;
155
156 /* While walking through the databuffer, `record' the characters to
157 this other buffer. It can be used to edit the menu entry at a
158 later moment. */
159
160 /* If true, recording is enabled. */
161 int record;
162
163 /* Points to the recording. */
164 char *recording;
165
166 /* index in the RECORDING. */
167 int recordpos;
168
169 /* Size of RECORDING. */
170 int recordlen;
171
172 /* End of file reached. */
173 int eof;
174
175 /* Merge multiple word tokens. */
176 int merge_start;
177 int merge_end;
178
179 /* Part of a multi-part token. */
180 char *text;
181 unsigned used;
182 unsigned size;
183
184 /* Type of text. */
185 grub_script_arg_type_t type;
186
187 /* Flex scanner. */
188 void *yyscanner;
189
190 /* Flex scanner buffer. */
191 void *buffer;
192 };
193
194 #define GRUB_LEXER_INITIAL_TEXT_SIZE 32
195 #define GRUB_LEXER_INITIAL_RECORD_SIZE 256
196
197 /* State of the parser as passes to the parser. */
198 struct grub_parser_param
199 {
200 /* Keep track of the memory allocated for this specific
201 function. */
202 struct grub_script_mem *func_mem;
203
204 /* When set to 0, no errors have occurred during parsing. */
205 int err;
206
207 /* The memory that was used while parsing and scanning. */
208 struct grub_script_mem *memused;
209
210 /* The result of the parser. */
211 struct grub_script_cmd *parsed;
212
213 struct grub_lexer_param *lexerstate;
214 };
215
216 void grub_script_mem_free (struct grub_script_mem *mem);
217
218 void grub_script_argv_free (struct grub_script_argv *argv);
219 int grub_script_argv_next (struct grub_script_argv *argv);
220 int grub_script_argv_append (struct grub_script_argv *argv, const char *s);
221 int grub_script_argv_split_append (struct grub_script_argv *argv, char *s);
222 int grub_script_argv_script_append (struct grub_script_argv *argv,
223 struct grub_script *s);
224
225 struct grub_script_arglist *
226 grub_script_create_arglist (struct grub_parser_param *state);
227
228 struct grub_script_arglist *
229 grub_script_add_arglist (struct grub_parser_param *state,
230 struct grub_script_arglist *list,
231 struct grub_script_arg *arg);
232 struct grub_script_cmd *
233 grub_script_create_cmdline (struct grub_parser_param *state,
234 struct grub_script_arglist *arglist);
235
236 struct grub_script_cmd *
237 grub_script_create_cmdif (struct grub_parser_param *state,
238 struct grub_script_cmd *exec_to_evaluate,
239 struct grub_script_cmd *exec_on_true,
240 struct grub_script_cmd *exec_on_false);
241
242 struct grub_script_cmd *
243 grub_script_create_cmdfor (struct grub_parser_param *state,
244 struct grub_script_arg *name,
245 struct grub_script_arglist *words,
246 struct grub_script_cmd *list);
247
248 struct grub_script_cmd *
249 grub_script_create_cmdwhile (struct grub_parser_param *state,
250 struct grub_script_cmd *cond,
251 struct grub_script_cmd *list,
252 int is_an_until_loop);
253
254 struct grub_script_cmd *
255 grub_script_append_cmd (struct grub_parser_param *state,
256 struct grub_script_cmd *list,
257 struct grub_script_cmd *last);
258 struct grub_script_arg *
259 grub_script_arg_add (struct grub_parser_param *state,
260 struct grub_script_arg *arg,
261 grub_script_arg_type_t type, char *str);
262
263 struct grub_script *grub_script_parse (char *script,
264 grub_reader_getline_t getline);
265 void grub_script_free (struct grub_script *script);
266 struct grub_script *grub_script_create (struct grub_script_cmd *cmd,
267 struct grub_script_mem *mem);
268
269 struct grub_lexer_param *grub_script_lexer_init (struct grub_parser_param *parser,
270 char *script,
271 grub_reader_getline_t getline);
272 void grub_script_lexer_fini (struct grub_lexer_param *);
273 void grub_script_lexer_ref (struct grub_lexer_param *);
274 void grub_script_lexer_deref (struct grub_lexer_param *);
275 unsigned grub_script_lexer_record_start (struct grub_parser_param *);
276 char *grub_script_lexer_record_stop (struct grub_parser_param *, unsigned);
277 int grub_script_lexer_yywrap (struct grub_parser_param *);
278 void grub_script_lexer_record (struct grub_parser_param *, char *);
279
280 /* Functions to track allocated memory. */
281 struct grub_script_mem *grub_script_mem_record (struct grub_parser_param *state);
282 struct grub_script_mem *grub_script_mem_record_stop (struct grub_parser_param *state,
283 struct grub_script_mem *restore);
284 void *grub_script_malloc (struct grub_parser_param *state, grub_size_t size);
285
286 /* Functions used by bison. */
287 union YYSTYPE;
288 int grub_script_yylex (union YYSTYPE *, struct grub_parser_param *);
289 int grub_script_yyparse (struct grub_parser_param *);
290 void grub_script_yyerror (struct grub_parser_param *, char const *);
291
292 /* Commands to execute, don't use these directly. */
293 grub_err_t grub_script_execute_cmdline (struct grub_script_cmd *cmd);
294 grub_err_t grub_script_execute_cmdlist (struct grub_script_cmd *cmd);
295 grub_err_t grub_script_execute_cmdif (struct grub_script_cmd *cmd);
296 grub_err_t grub_script_execute_cmdfor (struct grub_script_cmd *cmd);
297 grub_err_t grub_script_execute_cmdwhile (struct grub_script_cmd *cmd);
298
299 /* Execute any GRUB pre-parsed command or script. */
300 grub_err_t grub_script_execute (struct grub_script *script);
301
302 /* This variable points to the parsed command. This is used to
303 communicate with the bison code. */
304 extern struct grub_script_cmd *grub_script_parsed;
305
306 \f
307
308 /* The function description. */
309 struct grub_script_function
310 {
311 /* The name. */
312 char *name;
313
314 /* The script function. */
315 struct grub_script *func;
316
317 /* The flags. */
318 unsigned flags;
319
320 /* The next element. */
321 struct grub_script_function *next;
322
323 int references;
324 };
325 typedef struct grub_script_function *grub_script_function_t;
326
327 grub_script_function_t grub_script_function_create (struct grub_script_arg *functionname,
328 struct grub_script *cmd);
329 void grub_script_function_remove (const char *name);
330 grub_script_function_t grub_script_function_find (char *functionname);
331 int grub_script_function_iterate (int (*iterate) (grub_script_function_t));
332 grub_err_t grub_script_function_call (grub_script_function_t func,
333 int argc, char **args);
334
335 char **
336 grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *count);
337
338 static inline struct grub_script *
339 grub_script_get (struct grub_script *script)
340 {
341 script->refcnt++;
342 return script;
343 }
344
345 static inline void
346 grub_script_put (struct grub_script *script)
347 {
348 if (script->refcnt == 0)
349 grub_script_free (script);
350 else
351 script->refcnt--;
352 }
353
354 #endif /* ! GRUB_NORMAL_PARSER_HEADER */