source: grub-pc/trunk/fuentes/include/grub/script_sh.h @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

File size: 11.7 KB
Line 
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#include <grub/command.h>
27
28struct grub_script_mem;
29
30/* The generic header for each scripting command or structure.  */
31struct grub_script_cmd
32{
33  /* This function is called to execute the command.  */
34  grub_err_t (*exec) (struct grub_script_cmd *cmd);
35
36  /* The next command.  This can be used by the parent to form a chain
37     of commands.  */
38  struct grub_script_cmd *next;
39};
40
41struct grub_script
42{
43  unsigned refcnt;
44  struct grub_script_mem *mem;
45  struct grub_script_cmd *cmd;
46
47  /* grub_scripts from block arguments.  */
48  struct grub_script *next_siblings;
49  struct grub_script *children;
50};
51
52typedef enum
53{
54  GRUB_SCRIPT_ARG_TYPE_VAR,
55  GRUB_SCRIPT_ARG_TYPE_TEXT,
56  GRUB_SCRIPT_ARG_TYPE_GETTEXT,
57  GRUB_SCRIPT_ARG_TYPE_DQVAR,
58  GRUB_SCRIPT_ARG_TYPE_DQSTR,
59  GRUB_SCRIPT_ARG_TYPE_SQSTR,
60  GRUB_SCRIPT_ARG_TYPE_BLOCK
61} grub_script_arg_type_t;
62
63/* A part of an argument.  */
64struct grub_script_arg
65{
66  grub_script_arg_type_t type;
67
68  char *str;
69
70  /* Parsed block argument.  */
71  struct grub_script *script;
72
73  /* Next argument part.  */
74  struct grub_script_arg *next;
75};
76
77/* An argument vector.  */
78struct grub_script_argv
79{
80  unsigned argc;
81  char **args;
82  struct grub_script *script;
83};
84
85/* Pluggable wildcard translator.  */
86struct grub_script_wildcard_translator
87{
88  grub_err_t (*expand) (const char *str, char ***expansions);
89};
90extern struct grub_script_wildcard_translator *grub_wildcard_translator;
91extern struct grub_script_wildcard_translator grub_filename_translator;
92
93/* A complete argument.  It consists of a list of one or more `struct
94   grub_script_arg's.  */
95struct grub_script_arglist
96{
97  struct grub_script_arglist *next;
98  struct grub_script_arg *arg;
99  /* Only stored in the first link.  */
100  int argcount;
101};
102
103/* A single command line.  */
104struct grub_script_cmdline
105{
106  struct grub_script_cmd cmd;
107
108  /* The arguments for this command.  */
109  struct grub_script_arglist *arglist;
110};
111
112/* An if statement.  */
113struct grub_script_cmdif
114{
115  struct grub_script_cmd cmd;
116
117  /* The command used to check if the 'if' is true or false.  */
118  struct grub_script_cmd *exec_to_evaluate;
119
120  /* The code executed in case the result of 'if' was true.  */
121  struct grub_script_cmd *exec_on_true;
122
123  /* The code executed in case the result of 'if' was false.  */
124  struct grub_script_cmd *exec_on_false;
125};
126
127/* A for statement.  */
128struct grub_script_cmdfor
129{
130  struct grub_script_cmd cmd;
131
132  /* The name used as looping variable.  */
133  struct grub_script_arg *name;
134
135  /* The words loop iterates over.  */
136  struct grub_script_arglist *words;
137
138  /* The command list executed in each loop.  */
139  struct grub_script_cmd *list;
140};
141
142/* A while/until command.  */
143struct grub_script_cmdwhile
144{
145  struct grub_script_cmd cmd;
146
147  /* The command list used as condition.  */
148  struct grub_script_cmd *cond;
149
150  /* The command list executed in each loop.  */
151  struct grub_script_cmd *list;
152
153  /* The flag to indicate this as "until" loop.  */
154  int until;
155};
156
157/* State of the lexer as passed to the lexer.  */
158struct grub_lexer_param
159{
160  /* Function used by the lexer to get a new line when more input is
161     expected, but not available.  */
162  grub_reader_getline_t getline;
163
164  /* Caller-supplied data passed to `getline'.  */
165  void *getline_data;
166
167  /* A reference counter.  If this is >0 it means that the parser
168     expects more tokens and `getline' should be called to fetch more.
169     Otherwise the lexer can stop processing if the current buffer is
170     depleted.  */
171  int refs;
172
173  /* While walking through the databuffer, `record' the characters to
174     this other buffer.  It can be used to edit the menu entry at a
175     later moment.  */
176
177  /* If true, recording is enabled.  */
178  int record;
179
180  /* Points to the recording.  */
181  char *recording;
182
183  /* index in the RECORDING.  */
184  int recordpos;
185
186  /* Size of RECORDING.  */
187  int recordlen;
188
189  /* End of file reached.  */
190  int eof;
191
192  /* Merge multiple word tokens.  */
193  int merge_start;
194  int merge_end;
195
196  /* Part of a multi-part token.  */
197  char *text;
198  unsigned used;
199  unsigned size;
200
201  /* Type of text.  */
202  grub_script_arg_type_t type;
203
204  /* Flag to indicate resplit in progres.  */
205  unsigned resplit;
206
207  /* Text that is unput.  */
208  char *prefix;
209
210  /* Flex scanner.  */
211  void *yyscanner;
212
213  /* Flex scanner buffer.  */
214  void *buffer;
215};
216
217#define GRUB_LEXER_INITIAL_TEXT_SIZE   32
218#define GRUB_LEXER_INITIAL_RECORD_SIZE 256
219
220/* State of the parser as passes to the parser.  */
221struct grub_parser_param
222{
223  /* Keep track of the memory allocated for this specific
224     function.  */
225  struct grub_script_mem *func_mem;
226
227  /* When set to 0, no errors have occurred during parsing.  */
228  int err;
229
230  /* The memory that was used while parsing and scanning.  */
231  struct grub_script_mem *memused;
232
233  /* The block argument scripts.  */
234  struct grub_script *scripts;
235
236  /* The result of the parser.  */
237  struct grub_script_cmd *parsed;
238
239  struct grub_lexer_param *lexerstate;
240};
241
242void grub_script_init (void);
243void grub_script_fini (void);
244
245void grub_script_mem_free (struct grub_script_mem *mem);
246
247void grub_script_argv_free    (struct grub_script_argv *argv);
248int grub_script_argv_make     (struct grub_script_argv *argv, int argc, char **args);
249int grub_script_argv_next     (struct grub_script_argv *argv);
250int grub_script_argv_append   (struct grub_script_argv *argv, const char *s,
251                               grub_size_t slen);
252int grub_script_argv_split_append (struct grub_script_argv *argv, const char *s);
253
254struct grub_script_arglist *
255grub_script_create_arglist (struct grub_parser_param *state);
256
257struct grub_script_arglist *
258grub_script_add_arglist (struct grub_parser_param *state,
259                         struct grub_script_arglist *list,
260                         struct grub_script_arg *arg);
261struct grub_script_cmd *
262grub_script_create_cmdline (struct grub_parser_param *state,
263                            struct grub_script_arglist *arglist);
264
265struct grub_script_cmd *
266grub_script_create_cmdif (struct grub_parser_param *state,
267                          struct grub_script_cmd *exec_to_evaluate,
268                          struct grub_script_cmd *exec_on_true,
269                          struct grub_script_cmd *exec_on_false);
270
271struct grub_script_cmd *
272grub_script_create_cmdfor (struct grub_parser_param *state,
273                           struct grub_script_arg *name,
274                           struct grub_script_arglist *words,
275                           struct grub_script_cmd *list);
276
277struct grub_script_cmd *
278grub_script_create_cmdwhile (struct grub_parser_param *state,
279                             struct grub_script_cmd *cond,
280                             struct grub_script_cmd *list,
281                             int is_an_until_loop);
282
283struct grub_script_cmd *
284grub_script_append_cmd (struct grub_parser_param *state,
285                        struct grub_script_cmd *list,
286                        struct grub_script_cmd *last);
287struct grub_script_arg *
288grub_script_arg_add (struct grub_parser_param *state,
289                     struct grub_script_arg *arg,
290                     grub_script_arg_type_t type, char *str);
291
292struct grub_script *grub_script_parse (char *script,
293                                       grub_reader_getline_t getline_func,
294                                       void *getline_func_data);
295void grub_script_free (struct grub_script *script);
296struct grub_script *grub_script_create (struct grub_script_cmd *cmd,
297                                        struct grub_script_mem *mem);
298
299struct grub_lexer_param *grub_script_lexer_init (struct grub_parser_param *parser,
300                                                 char *script,
301                                                 grub_reader_getline_t getline_func,
302                                                 void *getline_func_data);
303void grub_script_lexer_fini (struct grub_lexer_param *);
304void grub_script_lexer_ref (struct grub_lexer_param *);
305void grub_script_lexer_deref (struct grub_lexer_param *);
306unsigned grub_script_lexer_record_start (struct grub_parser_param *);
307char *grub_script_lexer_record_stop (struct grub_parser_param *, unsigned);
308int  grub_script_lexer_yywrap (struct grub_parser_param *, const char *input);
309void grub_script_lexer_record (struct grub_parser_param *, char *);
310
311/* Functions to track allocated memory.  */
312struct grub_script_mem *grub_script_mem_record (struct grub_parser_param *state);
313struct grub_script_mem *grub_script_mem_record_stop (struct grub_parser_param *state,
314                                                     struct grub_script_mem *restore);
315void *grub_script_malloc (struct grub_parser_param *state, grub_size_t size);
316
317/* Functions used by bison.  */
318union YYSTYPE;
319int grub_script_yylex (union YYSTYPE *, struct grub_parser_param *);
320int grub_script_yyparse (struct grub_parser_param *);
321void grub_script_yyerror (struct grub_parser_param *, char const *);
322
323/* Commands to execute, don't use these directly.  */
324grub_err_t grub_script_execute_cmdline (struct grub_script_cmd *cmd);
325grub_err_t grub_script_execute_cmdlist (struct grub_script_cmd *cmd);
326grub_err_t grub_script_execute_cmdif (struct grub_script_cmd *cmd);
327grub_err_t grub_script_execute_cmdfor (struct grub_script_cmd *cmd);
328grub_err_t grub_script_execute_cmdwhile (struct grub_script_cmd *cmd);
329
330/* Execute any GRUB pre-parsed command or script.  */
331grub_err_t grub_script_execute (struct grub_script *script);
332grub_err_t grub_script_execute_sourcecode (const char *source);
333grub_err_t grub_script_execute_new_scope (const char *source, int argc, char **args);
334
335/* Break command for loops.  */
336grub_err_t grub_script_break (grub_command_t cmd, int argc, char *argv[]);
337
338/* SHIFT command for GRUB script.  */
339grub_err_t grub_script_shift (grub_command_t cmd, int argc, char *argv[]);
340
341/* SETPARAMS command for GRUB script functions.  */
342grub_err_t grub_script_setparams (grub_command_t cmd, int argc, char *argv[]);
343
344/* RETURN command for functions.  */
345grub_err_t grub_script_return (grub_command_t cmd, int argc, char *argv[]);
346
347/* This variable points to the parsed command.  This is used to
348   communicate with the bison code.  */
349extern struct grub_script_cmd *grub_script_parsed;
350
351
352
353/* The function description.  */
354struct grub_script_function
355{
356  /* The name.  */
357  char *name;
358
359  /* The script function.  */
360  struct grub_script *func;
361
362  /* The flags.  */
363  unsigned flags;
364
365  /* The next element.  */
366  struct grub_script_function *next;
367
368  int references;
369};
370typedef struct grub_script_function *grub_script_function_t;
371
372extern grub_script_function_t grub_script_function_list;
373
374#define FOR_SCRIPT_FUNCTIONS(var) for((var) = grub_script_function_list; \
375                                      (var); (var) = (var)->next)
376
377grub_script_function_t grub_script_function_create (struct grub_script_arg *functionname,
378                                                    struct grub_script *cmd);
379void grub_script_function_remove (const char *name);
380grub_script_function_t grub_script_function_find (char *functionname);
381
382grub_err_t grub_script_function_call (grub_script_function_t func,
383                                      int argc, char **args);
384
385char **
386grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *count);
387
388grub_err_t
389grub_normal_parse_line (char *line,
390                        grub_reader_getline_t getline_func,
391                        void *getline_func_data);
392
393static inline struct grub_script *
394grub_script_ref (struct grub_script *script)
395{
396  if (script)
397    script->refcnt++;
398  return script;
399}
400
401static inline void
402grub_script_unref (struct grub_script *script)
403{
404  if (! script)
405    return;
406
407  if (script->refcnt == 0)
408    grub_script_free (script);
409  else
410    script->refcnt--;
411}
412
413#endif /* ! GRUB_NORMAL_PARSER_HEADER */
Note: See TracBrowser for help on using the repository browser.