3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2009 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
;
42 struct grub_script_mem
*mem
;
43 struct grub_script_cmd
*cmd
;
48 GRUB_SCRIPT_ARG_TYPE_STR
,
49 GRUB_SCRIPT_ARG_TYPE_VAR
50 } grub_script_arg_type_t
;
52 /* A part of an argument. */
53 struct grub_script_arg
55 grub_script_arg_type_t type
;
59 /* Next argument part. */
60 struct grub_script_arg
*next
;
63 /* A complete argument. It consists of a list of one or more `struct
65 struct grub_script_arglist
67 struct grub_script_arglist
*next
;
68 struct grub_script_arg
*arg
;
69 /* Only stored in the first link. */
73 /* A single command line. */
74 struct grub_script_cmdline
76 struct grub_script_cmd cmd
;
78 /* The arguments for this command. */
79 struct grub_script_arglist
*arglist
;
82 /* A block of commands, this can be used to group commands. */
83 struct grub_script_cmdblock
85 struct grub_script_cmd cmd
;
87 /* A chain of commands. */
88 struct grub_script_cmd
*cmdlist
;
91 /* An if statement. */
92 struct grub_script_cmdif
94 struct grub_script_cmd cmd
;
96 /* The command used to check if the 'if' is true or false. */
97 struct grub_script_cmd
*exec_to_evaluate
;
99 /* The code executed in case the result of 'if' was true. */
100 struct grub_script_cmd
*exec_on_true
;
102 /* The code executed in case the result of 'if' was false. */
103 struct grub_script_cmd
*exec_on_false
;
106 /* A menu entry generate statement. */
107 struct grub_script_cmd_menuentry
109 struct grub_script_cmd cmd
;
111 /* The arguments for this menu entry. */
112 struct grub_script_arglist
*arglist
;
114 /* The sourcecode the entry will be generated from. */
115 const char *sourcecode
;
117 /* Options. XXX: Not used yet. */
121 /* State of the lexer as passed to the lexer. */
122 struct grub_lexer_param
124 /* Set to 0 when the lexer is done. */
127 /* State of the state machine. */
128 grub_parser_state_t state
;
130 /* Function used by the lexer to get a new line when more input is
131 expected, but not available. */
132 grub_reader_getline_t getline
;
134 /* A reference counter. If this is >0 it means that the parser
135 expects more tokens and `getline' should be called to fetch more.
136 Otherwise the lexer can stop processing if the current buffer is
140 /* The character stream that has to be parsed. */
142 char *newscript
; /* XXX */
144 /* While walking through the databuffer, `record' the characters to
145 this other buffer. It can be used to edit the menu entry at a
148 /* If true, recording is enabled. */
151 /* Points to the recording. */
154 /* index in the RECORDING. */
157 /* Size of RECORDING. */
160 /* The token that is already parsed but not yet returned. */
163 /* Was the last token a newline? */
167 /* State of the parser as passes to the parser. */
168 struct grub_parser_param
170 /* Keep track of the memory allocated for this specific
172 struct grub_script_mem
*func_mem
;
174 /* When set to 0, no errors have occurred during parsing. */
177 /* The memory that was used while parsing and scanning. */
178 struct grub_script_mem
*memused
;
180 /* The result of the parser. */
181 struct grub_script_cmd
*parsed
;
183 struct grub_lexer_param
*lexerstate
;
186 struct grub_script_arglist
*
187 grub_script_create_arglist (struct grub_parser_param
*state
);
189 struct grub_script_arglist
*
190 grub_script_add_arglist (struct grub_parser_param
*state
,
191 struct grub_script_arglist
*list
,
192 struct grub_script_arg
*arg
);
193 struct grub_script_cmd
*
194 grub_script_create_cmdline (struct grub_parser_param
*state
,
195 struct grub_script_arglist
*arglist
);
196 struct grub_script_cmd
*
197 grub_script_create_cmdblock (struct grub_parser_param
*state
);
199 struct grub_script_cmd
*
200 grub_script_create_cmdif (struct grub_parser_param
*state
,
201 struct grub_script_cmd
*exec_to_evaluate
,
202 struct grub_script_cmd
*exec_on_true
,
203 struct grub_script_cmd
*exec_on_false
);
205 struct grub_script_cmd
*
206 grub_script_create_cmdmenu (struct grub_parser_param
*state
,
207 struct grub_script_arglist
*arglist
,
211 struct grub_script_cmd
*
212 grub_script_add_cmd (struct grub_parser_param
*state
,
213 struct grub_script_cmdblock
*cmdblock
,
214 struct grub_script_cmd
*cmd
);
215 struct grub_script_arg
*
216 grub_script_arg_add (struct grub_parser_param
*state
,
217 struct grub_script_arg
*arg
,
218 grub_script_arg_type_t type
, char *str
);
220 struct grub_script
*grub_script_parse (char *script
,
221 grub_reader_getline_t getline
);
222 void grub_script_free (struct grub_script
*script
);
223 struct grub_script
*grub_script_create (struct grub_script_cmd
*cmd
,
224 struct grub_script_mem
*mem
);
226 struct grub_lexer_param
*grub_script_lexer_init (char *s
,
227 grub_reader_getline_t getline
);
228 void grub_script_lexer_ref (struct grub_lexer_param
*);
229 void grub_script_lexer_deref (struct grub_lexer_param
*);
230 void grub_script_lexer_record_start (struct grub_lexer_param
*);
231 char *grub_script_lexer_record_stop (struct grub_lexer_param
*);
233 /* Functions to track allocated memory. */
234 struct grub_script_mem
*grub_script_mem_record (struct grub_parser_param
*state
);
235 struct grub_script_mem
*grub_script_mem_record_stop (struct grub_parser_param
*state
,
236 struct grub_script_mem
*restore
);
237 void *grub_script_malloc (struct grub_parser_param
*state
, grub_size_t size
);
239 /* Functions used by bison. */
241 int grub_script_yylex (union YYSTYPE
*, struct grub_parser_param
*);
242 int grub_script_yyparse (struct grub_parser_param
*);
243 void grub_script_yyerror (struct grub_parser_param
*, char const *);
245 /* Commands to execute, don't use these directly. */
246 grub_err_t
grub_script_execute_cmdline (struct grub_script_cmd
*cmd
);
247 grub_err_t
grub_script_execute_cmdblock (struct grub_script_cmd
*cmd
);
248 grub_err_t
grub_script_execute_cmdif (struct grub_script_cmd
*cmd
);
249 grub_err_t
grub_script_execute_menuentry (struct grub_script_cmd
*cmd
);
251 /* Execute any GRUB pre-parsed command or script. */
252 grub_err_t
grub_script_execute (struct grub_script
*script
);
254 /* This variable points to the parsed command. This is used to
255 communicate with the bison code. */
256 extern struct grub_script_cmd
*grub_script_parsed
;
260 /* The function description. */
261 struct grub_script_function
266 /* The script function. */
267 struct grub_script
*func
;
272 /* The next element. */
273 struct grub_script_function
*next
;
277 typedef struct grub_script_function
*grub_script_function_t
;
279 grub_script_function_t
grub_script_function_create (struct grub_script_arg
*functionname
,
280 struct grub_script
*cmd
);
281 void grub_script_function_remove (const char *name
);
282 grub_script_function_t
grub_script_function_find (char *functionname
);
283 int grub_script_function_iterate (int (*iterate
) (grub_script_function_t
));
284 int grub_script_function_call (grub_script_function_t func
,
285 int argc
, char **args
);
288 grub_script_execute_argument_to_string (struct grub_script_arg
*arg
);
290 #endif /* ! GRUB_NORMAL_PARSER_HEADER */