2008-05-29 Pavel Roskin <proski@gnu.org>
[grub2/bean.git] / include / grub / script.h
blob5218d13522873230dc07ca200ecd929c00b85a94
1 /* script.h */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007 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_SCRIPT_HEADER
21 #define GRUB_SCRIPT_HEADER 1
23 #include <grub/types.h>
24 #include <grub/err.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
36 of commands. */
37 struct grub_script_cmd *next;
40 struct grub_script
42 struct grub_script_mem *mem;
43 struct grub_script_cmd *cmd;
46 typedef enum
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;
57 char *str;
59 /* Next argument part. */
60 struct grub_script_arg *next;
63 /* A complete argument. It consists of a list of one or more `struct
64 grub_script_arg's. */
65 struct grub_script_arglist
67 struct grub_script_arglist *next;
68 struct grub_script_arg *arg;
69 /* Only stored in the first link. */
70 int argcount;
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;
81 /* The command name of this command. XXX: Perhaps an argument
82 should be used for this so we can use variables as command
83 name. */
84 char *cmdname;
87 /* A block of commands, this can be used to group commands. */
88 struct grub_script_cmdblock
90 struct grub_script_cmd cmd;
92 /* A chain of commands. */
93 struct grub_script_cmd *cmdlist;
96 /* An if statement. */
97 struct grub_script_cmdif
99 struct grub_script_cmd cmd;
101 /* The command used to check if the 'if' is true or false. */
102 struct grub_script_cmd *exec_to_evaluate;
104 /* The code executed in case the result of 'if' was true. */
105 struct grub_script_cmd *exec_on_true;
107 /* The code executed in case the result of 'if' was false. */
108 struct grub_script_cmd *exec_on_false;
111 /* A menu entry generate statement. */
112 struct grub_script_cmd_menuentry
114 struct grub_script_cmd cmd;
116 /* The title of the menu entry. */
117 struct grub_script_arg *title;
119 /* The sourcecode the entry will be generated from. */
120 const char *sourcecode;
122 /* Options. XXX: Not used yet. */
123 int options;
126 /* State of the lexer as passed to the lexer. */
127 struct grub_lexer_param
129 /* Set to 0 when the lexer is done. */
130 int done;
132 /* State of the state machine. */
133 grub_parser_state_t state;
135 /* Function used by the lexer to get a new line when more input is
136 expected, but not available. */
137 grub_err_t (*getline) (char **);
139 /* A reference counter. If this is >0 it means that the parser
140 expects more tokens and `getline' should be called to fetch more.
141 Otherwise the lexer can stop processing if the current buffer is
142 depleted. */
143 int refs;
145 /* The character stream that has to be parsed. */
146 char *script;
147 char *newscript; /* XXX */
149 /* While walking through the databuffer, `record' the characters to
150 this other buffer. It can be used to edit the menu entry at a
151 later moment. */
153 /* If true, recording is enabled. */
154 int record;
156 /* Points to the recording. */
157 char *recording;
159 /* index in the RECORDING. */
160 int recordpos;
162 /* Size of RECORDING. */
163 int recordlen;
166 /* State of the parser as passes to the parser. */
167 struct grub_parser_param
169 /* Keep track of the memory allocated for this specific
170 function. */
171 struct grub_script_mem *func_mem;
173 /* When set to 0, no errors have occured during parsing. */
174 int err;
176 /* The memory that was used while parsing and scanning. */
177 struct grub_script_mem *memused;
179 /* The result of the parser. */
180 struct grub_script_cmd *parsed;
182 struct grub_lexer_param *lexerstate;
185 struct grub_script_arglist *
186 grub_script_create_arglist (struct grub_parser_param *state);
188 struct grub_script_arglist *
189 grub_script_add_arglist (struct grub_parser_param *state,
190 struct grub_script_arglist *list,
191 struct grub_script_arg *arg);
192 struct grub_script_cmd *
193 grub_script_create_cmdline (struct grub_parser_param *state,
194 char *cmdname,
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_arg *title,
208 char *sourcecode,
209 int options);
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_err_t (*getline) (char **));
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_err_t (*getline) (char **));
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. */
240 union YYSTYPE;
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
263 /* The name. */
264 char *name;
266 /* The script function. */
267 struct grub_script *func;
269 /* The flags. */
270 unsigned flags;
272 /* The next element. */
273 struct grub_script_function *next;
275 int references;
277 typedef struct grub_script_function *grub_script_function_t;
279 grub_script_function_t grub_script_function_create (char *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);
287 #endif /* ! GRUB_SCRIPT_HEADER */