script_sh.h 11.7 KB
Newer Older
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 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  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 21
#ifndef GRUB_NORMAL_PARSER_HEADER
#define GRUB_NORMAL_PARSER_HEADER	1
22

23 24
#include <grub/types.h>
#include <grub/err.h>
25
#include <grub/parser.h>
BVK Chaitanya's avatar
BVK Chaitanya committed
26
#include <grub/command.h>
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

struct grub_script_mem;

/* The generic header for each scripting command or structure.  */
struct grub_script_cmd
{
  /* This function is called to execute the command.  */
  grub_err_t (*exec) (struct grub_script_cmd *cmd);

  /* The next command.  This can be used by the parent to form a chain
     of commands.  */
  struct grub_script_cmd *next;
};

struct grub_script
{
43
  unsigned refcnt;
44 45
  struct grub_script_mem *mem;
  struct grub_script_cmd *cmd;
46

47 48
  /* grub_scripts from block arguments.  */
  struct grub_script *next_siblings;
49
  struct grub_script *children;
50 51 52 53
};

typedef enum
{
54 55
  GRUB_SCRIPT_ARG_TYPE_VAR,
  GRUB_SCRIPT_ARG_TYPE_TEXT,
56
  GRUB_SCRIPT_ARG_TYPE_GETTEXT,
57 58
  GRUB_SCRIPT_ARG_TYPE_DQVAR,
  GRUB_SCRIPT_ARG_TYPE_DQSTR,
BVK Chaitanya's avatar
BVK Chaitanya committed
59 60
  GRUB_SCRIPT_ARG_TYPE_SQSTR,
  GRUB_SCRIPT_ARG_TYPE_BLOCK
61 62 63 64 65 66 67 68 69
} grub_script_arg_type_t;

/* A part of an argument.  */
struct grub_script_arg
{
  grub_script_arg_type_t type;

  char *str;

BVK Chaitanya's avatar
BVK Chaitanya committed
70
  /* Parsed block argument.  */
71
  struct grub_script *script;
BVK Chaitanya's avatar
BVK Chaitanya committed
72

73 74 75 76
  /* Next argument part.  */
  struct grub_script_arg *next;
};

77 78 79
/* An argument vector.  */
struct grub_script_argv
{
80
  unsigned argc;
81
  char **args;
82
  struct grub_script *script;
83 84
};

BVK Chaitanya's avatar
BVK Chaitanya committed
85 86 87 88 89
/* Pluggable wildcard translator.  */
struct grub_script_wildcard_translator
{
  grub_err_t (*expand) (const char *str, char ***expansions);
};
BVK Chaitanya's avatar
BVK Chaitanya committed
90
extern struct grub_script_wildcard_translator *grub_wildcard_translator;
BVK Chaitanya's avatar
BVK Chaitanya committed
91
extern struct grub_script_wildcard_translator grub_filename_translator;
BVK Chaitanya's avatar
BVK Chaitanya committed
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
/* A complete argument.  It consists of a list of one or more `struct
   grub_script_arg's.  */
struct grub_script_arglist
{
  struct grub_script_arglist *next;
  struct grub_script_arg *arg;
  /* Only stored in the first link.  */
  int argcount;
};

/* A single command line.  */
struct grub_script_cmdline
{
  struct grub_script_cmd cmd;

  /* The arguments for this command.  */
  struct grub_script_arglist *arglist;
};

/* An if statement.  */
struct grub_script_cmdif
{
  struct grub_script_cmd cmd;

117 118
  /* The command used to check if the 'if' is true or false.  */
  struct grub_script_cmd *exec_to_evaluate;
119

120 121
  /* The code executed in case the result of 'if' was true.  */
  struct grub_script_cmd *exec_on_true;
122

123 124
  /* The code executed in case the result of 'if' was false.  */
  struct grub_script_cmd *exec_on_false;
125 126
};

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/* A for statement.  */
struct grub_script_cmdfor
{
  struct grub_script_cmd cmd;

  /* The name used as looping variable.  */
  struct grub_script_arg *name;

  /* The words loop iterates over.  */
  struct grub_script_arglist *words;

  /* The command list executed in each loop.  */
  struct grub_script_cmd *list;
};

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/* A while/until command.  */
struct grub_script_cmdwhile
{
  struct grub_script_cmd cmd;

  /* The command list used as condition.  */
  struct grub_script_cmd *cond;

  /* The command list executed in each loop.  */
  struct grub_script_cmd *list;

  /* The flag to indicate this as "until" loop.  */
  int until;
};

157 158 159 160 161
/* State of the lexer as passed to the lexer.  */
struct grub_lexer_param
{
  /* Function used by the lexer to get a new line when more input is
     expected, but not available.  */
162
  grub_reader_getline_t getline;
163

164 165 166
  /* Caller-supplied data passed to `getline'.  */
  void *getline_data;

167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  /* A reference counter.  If this is >0 it means that the parser
     expects more tokens and `getline' should be called to fetch more.
     Otherwise the lexer can stop processing if the current buffer is
     depleted.  */
  int refs;

  /* While walking through the databuffer, `record' the characters to
     this other buffer.  It can be used to edit the menu entry at a
     later moment.  */

  /* If true, recording is enabled.  */
  int record;

  /* Points to the recording.  */
  char *recording;

183
  /* index in the RECORDING.  */
184 185 186 187
  int recordpos;

  /* Size of RECORDING.  */
  int recordlen;
188

189 190 191 192 193 194 195
  /* End of file reached.  */
  int eof;

  /* Merge multiple word tokens.  */
  int merge_start;
  int merge_end;

196
  /* Part of a multi-part token.  */
197
  char *text;
198 199
  unsigned used;
  unsigned size;
200

201 202 203
  /* Type of text.  */
  grub_script_arg_type_t type;

BVK Chaitanya's avatar
BVK Chaitanya committed
204 205 206 207 208 209
  /* Flag to indicate resplit in progres.  */
  unsigned resplit;

  /* Text that is unput.  */
  char *prefix;

210 211 212 213 214
  /* Flex scanner.  */
  void *yyscanner;

  /* Flex scanner buffer.  */
  void *buffer;
215 216
};

217 218
#define GRUB_LEXER_INITIAL_TEXT_SIZE   32
#define GRUB_LEXER_INITIAL_RECORD_SIZE 256
219

220 221 222 223 224 225 226
/* State of the parser as passes to the parser.  */
struct grub_parser_param
{
  /* Keep track of the memory allocated for this specific
     function.  */
  struct grub_script_mem *func_mem;

227
  /* When set to 0, no errors have occurred during parsing.  */
228 229 230 231 232
  int err;

  /* The memory that was used while parsing and scanning.  */
  struct grub_script_mem *memused;

233 234 235
  /* The block argument scripts.  */
  struct grub_script *scripts;

236 237 238 239 240 241
  /* The result of the parser.  */
  struct grub_script_cmd *parsed;

  struct grub_lexer_param *lexerstate;
};

BVK Chaitanya's avatar
BVK Chaitanya committed
242 243 244
void grub_script_init (void);
void grub_script_fini (void);

245 246
void grub_script_mem_free (struct grub_script_mem *mem);

247
void grub_script_argv_free    (struct grub_script_argv *argv);
248
int grub_script_argv_make     (struct grub_script_argv *argv, int argc, char **args);
249
int grub_script_argv_next     (struct grub_script_argv *argv);
250 251 252
int grub_script_argv_append   (struct grub_script_argv *argv, const char *s,
			       grub_size_t slen);
int grub_script_argv_split_append (struct grub_script_argv *argv, const char *s);
253

254
struct grub_script_arglist *
255
grub_script_create_arglist (struct grub_parser_param *state);
256 257

struct grub_script_arglist *
258 259
grub_script_add_arglist (struct grub_parser_param *state,
			 struct grub_script_arglist *list,
260 261
			 struct grub_script_arg *arg);
struct grub_script_cmd *
262
grub_script_create_cmdline (struct grub_parser_param *state,
263 264 265
			    struct grub_script_arglist *arglist);

struct grub_script_cmd *
266
grub_script_create_cmdif (struct grub_parser_param *state,
267 268 269
			  struct grub_script_cmd *exec_to_evaluate,
			  struct grub_script_cmd *exec_on_true,
			  struct grub_script_cmd *exec_on_false);
270

271 272 273 274 275 276
struct grub_script_cmd *
grub_script_create_cmdfor (struct grub_parser_param *state,
			   struct grub_script_arg *name,
			   struct grub_script_arglist *words,
			   struct grub_script_cmd *list);

277 278 279 280 281 282
struct grub_script_cmd *
grub_script_create_cmdwhile (struct grub_parser_param *state,
			     struct grub_script_cmd *cond,
			     struct grub_script_cmd *list,
			     int is_an_until_loop);

283
struct grub_script_cmd *
284 285 286
grub_script_append_cmd (struct grub_parser_param *state,
			struct grub_script_cmd *list,
			struct grub_script_cmd *last);
287
struct grub_script_arg *
288 289
grub_script_arg_add (struct grub_parser_param *state,
		     struct grub_script_arg *arg,
290 291 292
		     grub_script_arg_type_t type, char *str);

struct grub_script *grub_script_parse (char *script,
293 294
				       grub_reader_getline_t getline_func,
				       void *getline_func_data);
295 296 297 298
void grub_script_free (struct grub_script *script);
struct grub_script *grub_script_create (struct grub_script_cmd *cmd,
					struct grub_script_mem *mem);

299 300
struct grub_lexer_param *grub_script_lexer_init (struct grub_parser_param *parser,
						 char *script,
301 302
						 grub_reader_getline_t getline_func,
						 void *getline_func_data);
303
void grub_script_lexer_fini (struct grub_lexer_param *);
304 305
void grub_script_lexer_ref (struct grub_lexer_param *);
void grub_script_lexer_deref (struct grub_lexer_param *);
BVK Chaitanya's avatar
BVK Chaitanya committed
306 307
unsigned grub_script_lexer_record_start (struct grub_parser_param *);
char *grub_script_lexer_record_stop (struct grub_parser_param *, unsigned);
BVK Chaitanya's avatar
BVK Chaitanya committed
308
int  grub_script_lexer_yywrap (struct grub_parser_param *, const char *input);
309
void grub_script_lexer_record (struct grub_parser_param *, char *);
310 311

/* Functions to track allocated memory.  */
312
struct grub_script_mem *grub_script_mem_record (struct grub_parser_param *state);
313
struct grub_script_mem *grub_script_mem_record_stop (struct grub_parser_param *state,
314 315
						     struct grub_script_mem *restore);
void *grub_script_malloc (struct grub_parser_param *state, grub_size_t size);
316 317

/* Functions used by bison.  */
318 319
union YYSTYPE;
int grub_script_yylex (union YYSTYPE *, struct grub_parser_param *);
320 321
int grub_script_yyparse (struct grub_parser_param *);
void grub_script_yyerror (struct grub_parser_param *, char const *);
322 323 324

/* Commands to execute, don't use these directly.  */
grub_err_t grub_script_execute_cmdline (struct grub_script_cmd *cmd);
325
grub_err_t grub_script_execute_cmdlist (struct grub_script_cmd *cmd);
326
grub_err_t grub_script_execute_cmdif (struct grub_script_cmd *cmd);
327
grub_err_t grub_script_execute_cmdfor (struct grub_script_cmd *cmd);
328
grub_err_t grub_script_execute_cmdwhile (struct grub_script_cmd *cmd);
329 330 331

/* Execute any GRUB pre-parsed command or script.  */
grub_err_t grub_script_execute (struct grub_script *script);
332 333
grub_err_t grub_script_execute_sourcecode (const char *source);
grub_err_t grub_script_execute_new_scope (const char *source, int argc, char **args);
334

BVK Chaitanya's avatar
BVK Chaitanya committed
335 336 337
/* Break command for loops.  */
grub_err_t grub_script_break (grub_command_t cmd, int argc, char *argv[]);

BVK Chaitanya's avatar
BVK Chaitanya committed
338
/* SHIFT command for GRUB script.  */
BVK Chaitanya's avatar
BVK Chaitanya committed
339
grub_err_t grub_script_shift (grub_command_t cmd, int argc, char *argv[]);
BVK Chaitanya's avatar
BVK Chaitanya committed
340

341 342 343
/* SETPARAMS command for GRUB script functions.  */
grub_err_t grub_script_setparams (grub_command_t cmd, int argc, char *argv[]);

344 345 346
/* RETURN command for functions.  */
grub_err_t grub_script_return (grub_command_t cmd, int argc, char *argv[]);

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
/* This variable points to the parsed command.  This is used to
   communicate with the bison code.  */
extern struct grub_script_cmd *grub_script_parsed;



/* The function description.  */
struct grub_script_function
{
  /* The name.  */
  char *name;

  /* The script function.  */
  struct grub_script *func;

  /* The flags.  */
  unsigned flags;

  /* The next element.  */
  struct grub_script_function *next;

  int references;
};
typedef struct grub_script_function *grub_script_function_t;

372 373 374 375 376
extern grub_script_function_t grub_script_function_list;

#define FOR_SCRIPT_FUNCTIONS(var) for((var) = grub_script_function_list; \
				      (var); (var) = (var)->next)

377
grub_script_function_t grub_script_function_create (struct grub_script_arg *functionname,
378 379 380
						    struct grub_script *cmd);
void grub_script_function_remove (const char *name);
grub_script_function_t grub_script_function_find (char *functionname);
BVK Chaitanya's avatar
BVK Chaitanya committed
381

382 383
grub_err_t grub_script_function_call (grub_script_function_t func,
				      int argc, char **args);
384

385
char **
386
grub_script_execute_arglist_to_argv (struct grub_script_arglist *arglist, int *count);
387

388
grub_err_t
389
grub_normal_parse_line (char *line,
390 391
			grub_reader_getline_t getline_func,
			void *getline_func_data);
392

393
static inline struct grub_script *
394
grub_script_ref (struct grub_script *script)
395
{
396 397
  if (script)
    script->refcnt++;
398 399 400 401
  return script;
}

static inline void
402
grub_script_unref (struct grub_script *script)
403
{
404 405 406
  if (! script)
    return;

407 408 409 410 411 412
  if (script->refcnt == 0)
    grub_script_free (script);
  else
    script->refcnt--;
}

413
#endif /* ! GRUB_NORMAL_PARSER_HEADER */