1 /* execute.c -- Execute a GRUB script. */
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 #include <grub/misc.h>
22 #include <grub/normal.h>
25 #include <grub/script.h>
28 grub_script_execute_cmd (struct grub_script_cmd
*cmd
)
33 return cmd
->exec (cmd
);
36 /* Parse ARG and return the textual representation. Add strings are
37 concatenated and all values of the variables are filled in. */
39 grub_script_execute_argument_to_string (struct grub_script_arg
*arg
)
44 struct grub_script_arg
*argi
;
46 /* First determine the size of the argument. */
47 for (argi
= arg
; argi
; argi
= argi
->next
)
51 val
= grub_env_get (argi
->str
);
52 size
+= grub_strlen (val
);
55 size
+= grub_strlen (argi
->str
);
58 /* Create the argument. */
59 chararg
= grub_malloc (size
+ 1);
64 /* First determine the size of the argument. */
65 for (argi
= arg
; argi
; argi
= argi
->next
)
69 val
= grub_env_get (argi
->str
);
70 grub_strcat (chararg
, val
);
73 grub_strcat (chararg
, argi
->str
);
79 /* Execute a single command line. */
81 grub_script_execute_cmdline (struct grub_script_cmd
*cmd
)
83 struct grub_script_cmdline
*cmdline
= (struct grub_script_cmdline
*) cmd
;
84 struct grub_script_arglist
*arglist
;
87 grub_command_t grubcmd
;
88 struct grub_arg_list
*state
;
89 struct grub_arg_option
*parser
;
91 char **parsed_arglist
;
95 grub_script_function_t func
= 0;
98 /* Lookup the command. */
99 grubcmd
= grub_command_find (cmdline
->cmdname
);
102 /* It's not a GRUB command, try all functions. */
103 func
= grub_script_function_find (cmdline
->cmdname
);
106 /* As a last resort, try if it is an assignment. */
107 char *assign
= grub_strdup (cmdline
->cmdname
);
108 char *eq
= grub_strchr (assign
, '=');
112 /* Create two strings and set the variable. */
115 grub_env_set (assign
, eq
);
117 /* This was set because the command was not found. */
118 grub_errno
= GRUB_ERR_NONE
;
125 if (cmdline
->arglist
)
127 argcount
= cmdline
->arglist
->argcount
;
129 /* Create argv from the arguments. */
130 args
= grub_malloc (sizeof (char *) * argcount
);
131 for (arglist
= cmdline
->arglist
; arglist
; arglist
= arglist
->next
)
134 str
= grub_script_execute_argument_to_string (arglist
->arg
);
139 /* Execute the GRUB command or function. */
142 /* Count the amount of options the command has. */
143 parser
= (struct grub_arg_option
*) grubcmd
->options
;
144 while (parser
&& (parser
++)->doc
)
147 /* Set up the option state. */
148 state
= grub_malloc (sizeof (struct grub_arg_list
) * maxargs
);
149 grub_memset (state
, 0, sizeof (struct grub_arg_list
) * maxargs
);
151 /* Start the command. */
152 if (! (grubcmd
->flags
& GRUB_COMMAND_FLAG_NO_ARG_PARSE
))
154 if (grub_arg_parse (grubcmd
, argcount
, args
, state
, &parsed_arglist
, &numargs
))
155 ret
= (grubcmd
->func
) (state
, numargs
, parsed_arglist
);
158 ret
= (grubcmd
->func
) (state
, argcount
, args
);
163 ret
= grub_script_function_call (func
, argcount
, args
);
165 /* Free arguments. */
166 for (i
= 0; i
< argcount
; i
++)
170 grub_sprintf (errnobuf
, "%d", ret
);
171 grub_env_set ("?", errnobuf
);
176 /* Execute a block of one or more commands. */
178 grub_script_execute_cmdblock (struct grub_script_cmd
*cmd
)
180 struct grub_script_cmdblock
*cmdblock
= (struct grub_script_cmdblock
*) cmd
;
182 /* Loop over every command and execute it. */
183 for (cmd
= cmdblock
->cmdlist
; cmd
; cmd
= cmd
->next
)
184 grub_script_execute_cmd (cmd
);
189 /* Execute an if statement. */
191 grub_script_execute_cmdif (struct grub_script_cmd
*cmd
)
193 struct grub_script_cmdif
*cmdif
= (struct grub_script_cmdif
*) cmd
;
196 /* Check if the commands results in a true or a false. The value is
197 read from the env variable `?'. */
198 grub_script_execute_cmd (cmdif
->exec_to_evaluate
);
199 result
= grub_env_get ("?");
201 /* Execute the `if' or the `else' part depending on the value of
203 if (result
&& ! grub_strcmp (result
, "0"))
204 return grub_script_execute_cmd (cmdif
->exec_on_true
);
206 return grub_script_execute_cmd (cmdif
->exec_on_false
);
209 /* Execute the menu entry generate statement. */
211 grub_script_execute_menuentry (struct grub_script_cmd
*cmd
)
213 struct grub_script_cmd_menuentry
*cmd_menuentry
;
215 struct grub_script
*script
;
217 cmd_menuentry
= (struct grub_script_cmd_menuentry
*) cmd
;
219 /* The title can contain variables, parse them and generate a string
221 title
= grub_script_execute_argument_to_string (cmd_menuentry
->title
);
225 /* Parse the menu entry *again*. */
226 script
= grub_script_parse ((char *) cmd_menuentry
->sourcecode
, 0);
234 /* XXX: When this fails, the memory should be freed? */
235 return grub_normal_menu_addentry (title
, script
,
236 cmd_menuentry
->sourcecode
);
241 /* Execute any GRUB pre-parsed command or script. */
243 grub_script_execute (struct grub_script
*script
)
248 return grub_script_execute_cmd (script
->cmd
);