3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2009,2010 Free Software Foundation, Inc.
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.
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.
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/>.
20 #ifndef GRUB_NORMAL_PARSER_HEADER
21 #define GRUB_NORMAL_PARSER_HEADER 1
23 #include <grub/types.h>
25 #include <grub/parser.h>
27 struct grub_script_mem
;
29 /* The generic header for each scripting command or structure. */
30 struct grub_script_cmd
32 /* This function is called to execute the command. */
33 grub_err_t (*exec
) (struct grub_script_cmd
*cmd
);
35 /* The next command. This can be used by the parent to form a chain
37 struct grub_script_cmd
*next
;
43 struct grub_script_mem
*mem
;
44 struct grub_script_cmd
*cmd
;
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
;
57 /* A part of an argument. */
58 struct grub_script_arg
60 grub_script_arg_type_t type
;
64 /* Parsed block argument. */
65 struct grub_script
*block
;
67 /* Next argument part. */
68 struct grub_script_arg
*next
;
71 /* An argument vector. */
72 struct grub_script_argv
76 struct grub_script
**scripts
;
79 /* A complete argument. It consists of a list of one or more `struct
81 struct grub_script_arglist
83 struct grub_script_arglist
*next
;
84 struct grub_script_arg
*arg
;
85 /* Only stored in the first link. */
89 /* A single command line. */
90 struct grub_script_cmdline
92 struct grub_script_cmd cmd
;
94 /* The arguments for this command. */
95 struct grub_script_arglist
*arglist
;
98 /* An if statement. */
99 struct grub_script_cmdif
101 struct grub_script_cmd cmd
;
103 /* The command used to check if the 'if' is true or false. */
104 struct grub_script_cmd
*exec_to_evaluate
;
106 /* The code executed in case the result of 'if' was true. */
107 struct grub_script_cmd
*exec_on_true
;
109 /* The code executed in case the result of 'if' was false. */
110 struct grub_script_cmd
*exec_on_false
;
113 /* A for statement. */
114 struct grub_script_cmdfor
116 struct grub_script_cmd cmd
;
118 /* The name used as looping variable. */
119 struct grub_script_arg
*name
;
121 /* The words loop iterates over. */
122 struct grub_script_arglist
*words
;
124 /* The command list executed in each loop. */
125 struct grub_script_cmd
*list
;
128 /* A while/until command. */
129 struct grub_script_cmdwhile
131 struct grub_script_cmd cmd
;
133 /* The command list used as condition. */
134 struct grub_script_cmd
*cond
;
136 /* The command list executed in each loop. */
137 struct grub_script_cmd
*list
;
139 /* The flag to indicate this as "until" loop. */
143 /* State of the lexer as passed to the lexer. */
144 struct grub_lexer_param
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
;
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
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
160 /* If true, recording is enabled. */
163 /* Points to the recording. */
166 /* index in the RECORDING. */
169 /* Size of RECORDING. */
172 /* End of file reached. */
175 /* Merge multiple word tokens. */
179 /* Part of a multi-part token. */
185 grub_script_arg_type_t type
;
190 /* Flex scanner buffer. */
194 #define GRUB_LEXER_INITIAL_TEXT_SIZE 32
195 #define GRUB_LEXER_INITIAL_RECORD_SIZE 256
197 /* State of the parser as passes to the parser. */
198 struct grub_parser_param
200 /* Keep track of the memory allocated for this specific
202 struct grub_script_mem
*func_mem
;
204 /* When set to 0, no errors have occurred during parsing. */
207 /* The memory that was used while parsing and scanning. */
208 struct grub_script_mem
*memused
;
210 /* The result of the parser. */
211 struct grub_script_cmd
*parsed
;
213 struct grub_lexer_param
*lexerstate
;
216 void grub_script_mem_free (struct grub_script_mem
*mem
);
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
);
225 struct grub_script_arglist
*
226 grub_script_create_arglist (struct grub_parser_param
*state
);
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
);
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
);
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
);
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
);
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
);
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
);
269 struct grub_lexer_param
*grub_script_lexer_init (struct grub_parser_param
*parser
,
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 *);
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
);
286 /* Functions used by bison. */
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 *);
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
);
299 /* Execute any GRUB pre-parsed command or script. */
300 grub_err_t
grub_script_execute (struct grub_script
*script
);
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
;
308 /* The function description. */
309 struct grub_script_function
314 /* The script function. */
315 struct grub_script
*func
;
320 /* The next element. */
321 struct grub_script_function
*next
;
325 typedef struct grub_script_function
*grub_script_function_t
;
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
);
336 grub_script_execute_arglist_to_argv (struct grub_script_arglist
*arglist
, int *count
);
338 static inline struct grub_script
*
339 grub_script_get (struct grub_script
*script
)
346 grub_script_put (struct grub_script
*script
)
348 if (script
->refcnt
== 0)
349 grub_script_free (script
);
354 #endif /* ! GRUB_NORMAL_PARSER_HEADER */