1
/* execute.c -- Execute a GRUB script. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2005,2007,2008,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
#include <grub/misc.h>
23
#include <grub/script_sh.h>
24
#include <grub/command.h>
25
#include <grub/menu.h>
26
#include <grub/lib/arg.h>
27
#include <grub/normal.h>
28
#include <grub/extcmd.h>
30
/* Max digits for a char is 3 (0xFF is 255), similarly for an int it
31
is sizeof (int) * 3, and one extra for a possible -ve sign. */
32
#define ERRNO_DIGITS_MAX (sizeof (int) * 3 + 1)
34
static unsigned long is_continue;
35
static unsigned long active_loops;
36
static unsigned long active_breaks;
37
static unsigned long function_return;
39
#define GRUB_SCRIPT_SCOPE_MALLOCED 1
40
#define GRUB_SCRIPT_SCOPE_ARGS_MALLOCED 2
42
/* Scope for grub script functions. */
43
struct grub_script_scope
47
struct grub_script_argv argv;
49
static struct grub_script_scope *scope = 0;
51
/* Wildcard translator for GRUB script. */
52
struct grub_script_wildcard_translator *grub_wildcard_translator;
55
replace_scope (struct grub_script_scope *new_scope)
59
scope->argv.argc += scope->shifts;
60
scope->argv.args -= scope->shifts;
62
if (scope->flags & GRUB_SCRIPT_SCOPE_ARGS_MALLOCED)
63
grub_script_argv_free (&scope->argv);
65
if (scope->flags & GRUB_SCRIPT_SCOPE_MALLOCED)
72
grub_script_break (grub_command_t cmd, int argc, char *argv[])
80
else if ((argc > 1) || (count = grub_strtoul (argv[0], &p, 10)) == 0 ||
82
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad break");
84
is_continue = grub_strcmp (cmd->name, "break") ? 1 : 0;
85
active_breaks = grub_min (active_loops, count);
90
grub_script_shift (grub_command_t cmd __attribute__((unused)),
91
int argc, char *argv[])
103
return GRUB_ERR_BAD_ARGUMENT;
107
n = grub_strtoul (argv[0], &p, 10);
109
return GRUB_ERR_BAD_ARGUMENT;
112
if (n > scope->argv.argc)
113
return GRUB_ERR_BAD_ARGUMENT;
116
scope->argv.argc -= n;
117
scope->argv.args += n;
118
return GRUB_ERR_NONE;
122
grub_script_setparams (grub_command_t cmd __attribute__((unused)),
123
int argc, char **args)
125
struct grub_script_scope *new_scope;
126
struct grub_script_argv argv = { 0, 0, 0 };
129
return GRUB_ERR_INVALID_COMMAND;
131
new_scope = grub_malloc (sizeof (*new_scope));
135
if (grub_script_argv_make (&argv, argc, args))
137
grub_free (new_scope);
141
new_scope->shifts = 0;
142
new_scope->argv = argv;
143
new_scope->flags = GRUB_SCRIPT_SCOPE_MALLOCED |
144
GRUB_SCRIPT_SCOPE_ARGS_MALLOCED;
146
replace_scope (new_scope);
147
return GRUB_ERR_NONE;
151
grub_script_return (grub_command_t cmd __attribute__((unused)),
152
int argc, char *argv[])
157
if (! scope || argc > 1)
158
return grub_error (GRUB_ERR_BAD_ARGUMENT, "not in function scope");
163
return grub_strtoul (grub_env_get ("?"), NULL, 10);
166
n = grub_strtoul (argv[0], &p, 10);
168
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad argument");
171
return n ? grub_error (GRUB_ERR_TEST_FAILURE, "false") : GRUB_ERR_NONE;
175
grub_env_special (const char *name)
177
if (grub_isdigit (name[0]) ||
178
grub_strcmp (name, "#") == 0 ||
179
grub_strcmp (name, "*") == 0 ||
180
grub_strcmp (name, "@") == 0)
186
grub_script_env_get (const char *name, grub_script_arg_type_t type)
189
struct grub_script_argv result = { 0, 0, 0 };
191
if (grub_script_argv_next (&result))
194
if (! grub_env_special (name))
196
char *v = grub_env_get (name);
199
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
201
if (grub_script_argv_split_append (&result, v))
205
if (grub_script_argv_append (&result, v))
211
if (grub_script_argv_append (&result, 0))
214
else if (grub_strcmp (name, "#") == 0)
216
char buffer[ERRNO_DIGITS_MAX + 1];
217
grub_snprintf (buffer, sizeof (buffer), "%u", scope->argv.argc);
218
if (grub_script_argv_append (&result, buffer))
221
else if (grub_strcmp (name, "*") == 0)
223
for (i = 0; i < scope->argv.argc; i++)
224
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
226
if (i != 0 && grub_script_argv_next (&result))
229
if (grub_script_argv_split_append (&result, scope->argv.args[i]))
234
if (i != 0 && grub_script_argv_append (&result, " "))
237
if (grub_script_argv_append (&result, scope->argv.args[i]))
241
else if (grub_strcmp (name, "@") == 0)
243
for (i = 0; i < scope->argv.argc; i++)
245
if (i != 0 && grub_script_argv_next (&result))
248
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
250
if (grub_script_argv_split_append (&result, scope->argv.args[i]))
254
if (grub_script_argv_append (&result, scope->argv.args[i]))
260
unsigned long num = grub_strtoul (name, 0, 10);
262
; /* XXX no file name, for now. */
264
else if (num <= scope->argv.argc)
266
if (type == GRUB_SCRIPT_ARG_TYPE_VAR)
268
if (grub_script_argv_split_append (&result,
269
scope->argv.args[num - 1]))
273
if (grub_script_argv_append (&result, scope->argv.args[num - 1]))
282
grub_script_argv_free (&result);
287
grub_script_env_set (const char *name, const char *val)
289
if (grub_env_special (name))
290
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad variable name");
292
return grub_env_set (name, val);
295
/* Convert arguments in ARGLIST into ARGV form. */
297
grub_script_arglist_to_argv (struct grub_script_arglist *arglist,
298
struct grub_script_argv *argv)
302
struct grub_script_arg *arg = 0;
303
struct grub_script_argv result = { 0, 0, 0 };
305
auto int append (char *s, int escape_type);
306
int append (char *s, int escape_type)
311
if (! grub_wildcard_translator || escape_type == 0)
312
return grub_script_argv_append (&result, s);
315
p = grub_wildcard_translator->escape (s);
316
else if (escape_type < 0)
317
p = grub_wildcard_translator->unescape (s);
322
r = grub_script_argv_append (&result, p);
327
for (; arglist && arglist->arg; arglist = arglist->next)
329
if (grub_script_argv_next (&result))
337
case GRUB_SCRIPT_ARG_TYPE_VAR:
338
case GRUB_SCRIPT_ARG_TYPE_DQVAR:
339
values = grub_script_env_get (arg->str, arg->type);
340
for (i = 0; values && values[i]; i++)
342
if (i != 0 && grub_script_argv_next (&result))
345
if (arg->type == GRUB_SCRIPT_ARG_TYPE_VAR)
347
if (grub_script_argv_append (&result, values[i]))
352
if (append (values[i], 1))
356
grub_free (values[i]);
361
case GRUB_SCRIPT_ARG_TYPE_BLOCK:
362
if (grub_script_argv_append (&result, "{") ||
363
grub_script_argv_append (&result, arg->str) ||
364
grub_script_argv_append (&result, "}"))
366
result.script = arg->script;
369
case GRUB_SCRIPT_ARG_TYPE_TEXT:
370
if (grub_strlen (arg->str) &&
371
grub_script_argv_append (&result, arg->str))
375
case GRUB_SCRIPT_ARG_TYPE_DQSTR:
376
case GRUB_SCRIPT_ARG_TYPE_SQSTR:
377
if (append (arg->str, 1))
385
if (! result.args[result.argc - 1])
388
/* Perform wildcard expansion. */
390
if (grub_wildcard_translator)
394
char **expansions = 0;
395
struct grub_script_argv unexpanded = result;
399
for (i = 0; unexpanded.args[i]; i++)
401
if (grub_wildcard_translator->expand (unexpanded.args[i],
404
grub_script_argv_free (&unexpanded);
410
grub_script_argv_next (&result);
411
append (unexpanded.args[i], -1);
415
for (j = 0; expansions[j]; j++)
417
failed = (failed || grub_script_argv_next (&result) ||
418
append (expansions[j], -1));
419
grub_free (expansions[j]);
421
grub_free (expansions);
425
grub_script_argv_free (&unexpanded);
430
grub_script_argv_free (&unexpanded);
438
grub_script_argv_free (&result);
443
grub_script_execute_cmd (struct grub_script_cmd *cmd)
446
char errnobuf[ERRNO_DIGITS_MAX + 1];
451
ret = cmd->exec (cmd);
453
grub_snprintf (errnobuf, sizeof (errnobuf), "%d", ret);
454
grub_env_set ("?", errnobuf);
458
/* Execute a function call. */
460
grub_script_function_call (grub_script_function_t func, int argc, char **args)
463
unsigned long loops = active_loops;
464
struct grub_script_scope *old_scope;
465
struct grub_script_scope new_scope;
469
new_scope.shifts = 0;
470
new_scope.argv.argc = argc;
471
new_scope.argv.args = args;
476
ret = grub_script_execute (func->func);
479
active_loops = loops;
480
replace_scope (old_scope); /* free any scopes by setparams */
484
/* Execute a source script. */
486
grub_script_execute_sourcecode (const char *source, int argc, char **args)
489
struct grub_script *parsed_script;
490
struct grub_script_scope new_scope;
491
struct grub_script_scope *old_scope;
493
auto grub_err_t getline (char **line, int cont);
494
grub_err_t getline (char **line, int cont __attribute__ ((unused)))
504
p = grub_strchr (source, '\n');
507
*line = grub_strndup (source, p - source);
509
*line = grub_strdup (source);
510
source = p ? p + 1 : 0;
514
new_scope.argv.argc = argc;
515
new_scope.argv.args = args;
526
parsed_script = grub_script_parse (line, getline);
533
ret = grub_script_execute (parsed_script);
541
/* Execute a single command line. */
543
grub_script_execute_cmdline (struct grub_script_cmd *cmd)
545
struct grub_script_cmdline *cmdline = (struct grub_script_cmdline *) cmd;
546
grub_command_t grubcmd;
548
grub_script_function_t func = 0;
554
struct grub_script_argv argv = { 0, 0, 0 };
556
/* Lookup the command. */
557
if (grub_script_arglist_to_argv (cmdline->arglist, &argv) || ! argv.args[0])
561
argc = argv.argc - 1;
562
args = argv.args + 1;
563
cmdname = argv.args[0];
564
if (grub_strcmp (cmdname, "!") == 0)
566
if (argv.argc < 2 || ! argv.args[1])
568
grub_script_argv_free (&argv);
569
return grub_error (GRUB_ERR_BAD_ARGUMENT, "missing arguments");
573
argc = argv.argc - 2;
574
args = argv.args + 2;
575
cmdname = argv.args[1];
577
grubcmd = grub_command_find (cmdname);
580
grub_errno = GRUB_ERR_NONE;
582
/* It's not a GRUB command, try all functions. */
583
func = grub_script_function_find (cmdname);
586
/* As a last resort, try if it is an assignment. */
587
char *assign = grub_strdup (cmdname);
588
char *eq = grub_strchr (assign, '=');
592
/* This was set because the command was not found. */
593
grub_errno = GRUB_ERR_NONE;
595
/* Create two strings and set the variable. */
598
grub_script_env_set (assign, eq);
602
grub_snprintf (errnobuf, sizeof (errnobuf), "%d", grub_errno);
603
grub_script_env_set ("?", errnobuf);
605
grub_script_argv_free (&argv);
612
/* Execute the GRUB command or function. */
615
if (grub_extractor_level && !(grubcmd->flags
616
& GRUB_COMMAND_FLAG_EXTRACTOR))
617
ret = grub_error (GRUB_ERR_EXTRACTOR,
618
"%s isn't allowed to execute in an extractor",
620
else if ((grubcmd->flags & GRUB_COMMAND_FLAG_BLOCKS) &&
621
(grubcmd->flags & GRUB_COMMAND_FLAG_EXTCMD))
622
ret = grub_extcmd_dispatcher (grubcmd, argc, args, argv.script);
624
ret = (grubcmd->func) (grubcmd, argc, args);
627
ret = grub_script_function_call (func, argc, args);
631
if (ret == GRUB_ERR_TEST_FAILURE)
632
grub_errno = ret = GRUB_ERR_NONE;
633
else if (ret == GRUB_ERR_NONE)
634
ret = grub_error (GRUB_ERR_TEST_FAILURE, "false");
642
/* Free arguments. */
643
grub_script_argv_free (&argv);
645
if (grub_errno == GRUB_ERR_TEST_FAILURE)
646
grub_errno = GRUB_ERR_NONE;
650
grub_snprintf (errnobuf, sizeof (errnobuf), "%d", ret);
651
grub_env_set ("?", errnobuf);
656
/* Execute a block of one or more commands. */
658
grub_script_execute_cmdlist (struct grub_script_cmd *list)
661
struct grub_script_cmd *cmd;
663
/* Loop over every command and execute it. */
664
for (cmd = list->next; cmd; cmd = cmd->next)
669
ret = grub_script_execute_cmd (cmd);
678
/* Execute an if statement. */
680
grub_script_execute_cmdif (struct grub_script_cmd *cmd)
684
struct grub_script_cmdif *cmdif = (struct grub_script_cmdif *) cmd;
686
/* Check if the commands results in a true or a false. The value is
687
read from the env variable `?'. */
688
ret = grub_script_execute_cmd (cmdif->exec_to_evaluate);
692
result = grub_env_get ("?");
693
grub_errno = GRUB_ERR_NONE;
695
/* Execute the `if' or the `else' part depending on the value of
697
if (result && ! grub_strcmp (result, "0"))
698
return grub_script_execute_cmd (cmdif->exec_on_true);
700
return grub_script_execute_cmd (cmdif->exec_on_false);
703
/* Execute a for statement. */
705
grub_script_execute_cmdfor (struct grub_script_cmd *cmd)
709
struct grub_script_argv argv = { 0, 0, 0 };
710
struct grub_script_cmdfor *cmdfor = (struct grub_script_cmdfor *) cmd;
712
if (grub_script_arglist_to_argv (cmdfor->words, &argv))
717
for (i = 0; i < argv.argc; i++)
719
if (is_continue && active_breaks == 1)
724
grub_script_env_set (cmdfor->name->str, argv.args[i]);
725
result = grub_script_execute_cmd (cmdfor->list);
735
grub_script_argv_free (&argv);
739
/* Execute a "while" or "until" command. */
741
grub_script_execute_cmdwhile (struct grub_script_cmd *cmd)
744
struct grub_script_cmdwhile *cmdwhile = (struct grub_script_cmdwhile *) cmd;
748
result = grub_script_execute_cmd (cmdwhile->cond);
752
if (cmdwhile->until ? !result : result)
755
result = grub_script_execute_cmd (cmdwhile->list);
759
if (active_breaks == 1 && is_continue)
765
} while (1); /* XXX Put a check for ^C here */
774
/* Execute any GRUB pre-parsed command or script. */
776
grub_script_execute (struct grub_script *script)
781
return grub_script_execute_cmd (script->cmd);