added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / arch / common / grub2 / normal / execute.c
blob4462ddd59d29d9d3d75c2bd74cca11e0ce3eed37
1 /* execute.c -- Execute a GRUB script. */
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 #include <grub/misc.h>
21 #include <grub/mm.h>
22 #include <grub/normal.h>
23 #include <grub/arg.h>
24 #include <grub/env.h>
25 #include <grub/script.h>
27 static grub_err_t
28 grub_script_execute_cmd (struct grub_script_cmd *cmd)
30 if (cmd == 0)
31 return 0;
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. */
38 static char *
39 grub_script_execute_argument_to_string (struct grub_script_arg *arg)
41 int size = 0;
42 char *val;
43 char *chararg;
44 struct grub_script_arg *argi;
46 /* First determine the size of the argument. */
47 for (argi = arg; argi; argi = argi->next)
49 if (argi->type == 1)
51 val = grub_env_get (argi->str);
52 size += grub_strlen (val);
54 else
55 size += grub_strlen (argi->str);
58 /* Create the argument. */
59 chararg = grub_malloc (size + 1);
60 if (! chararg)
61 return 0;
63 *chararg = '\0';
64 /* First determine the size of the argument. */
65 for (argi = arg; argi; argi = argi->next)
67 if (argi->type == 1)
69 val = grub_env_get (argi->str);
70 grub_strcat (chararg, val);
72 else
73 grub_strcat (chararg, argi->str);
76 return chararg;
79 /* Execute a single command line. */
80 grub_err_t
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;
85 char **args = 0;
86 int i = 0;
87 grub_command_t grubcmd;
88 struct grub_arg_list *state;
89 struct grub_arg_option *parser;
90 int maxargs = 0;
91 char **parsed_arglist;
92 int numargs;
93 grub_err_t ret = 0;
94 int argcount = 0;
95 grub_script_function_t func = 0;
96 char errnobuf[6];
98 /* Lookup the command. */
99 grubcmd = grub_command_find (cmdline->cmdname);
100 if (! grubcmd)
102 /* It's not a GRUB command, try all functions. */
103 func = grub_script_function_find (cmdline->cmdname);
104 if (! func)
106 /* As a last resort, try if it is an assignment. */
107 char *assign = grub_strdup (cmdline->cmdname);
108 char *eq = grub_strchr (assign, '=');
110 if (eq)
112 /* Create two strings and set the variable. */
113 *eq = '\0';
114 eq++;
115 grub_env_set (assign, eq);
117 /* This was set because the command was not found. */
118 grub_errno = GRUB_ERR_NONE;
120 grub_free (assign);
121 return 0;
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)
133 char *str;
134 str = grub_script_execute_argument_to_string (arglist->arg);
135 args[i++] = str;
139 /* Execute the GRUB command or function. */
140 if (grubcmd)
142 /* Count the amount of options the command has. */
143 parser = (struct grub_arg_option *) grubcmd->options;
144 while (parser && (parser++)->doc)
145 maxargs++;
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);
157 else
158 ret = (grubcmd->func) (state, argcount, args);
160 grub_free (state);
162 else
163 ret = grub_script_function_call (func, argcount, args);
165 /* Free arguments. */
166 for (i = 0; i < argcount; i++)
167 grub_free (args[i]);
168 grub_free (args);
170 grub_sprintf (errnobuf, "%d", ret);
171 grub_env_set ("?", errnobuf);
173 return ret;
176 /* Execute a block of one or more commands. */
177 grub_err_t
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);
186 return 0;
189 /* Execute an if statement. */
190 grub_err_t
191 grub_script_execute_cmdif (struct grub_script_cmd *cmd)
193 struct grub_script_cmdif *cmdif = (struct grub_script_cmdif *) cmd;
194 char *result;
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
202 `?'. */
203 if (result && ! grub_strcmp (result, "0"))
204 return grub_script_execute_cmd (cmdif->exec_on_true);
205 else
206 return grub_script_execute_cmd (cmdif->exec_on_false);
209 /* Execute the menu entry generate statement. */
210 grub_err_t
211 grub_script_execute_menuentry (struct grub_script_cmd *cmd)
213 struct grub_script_cmd_menuentry *cmd_menuentry;
214 char *title;
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
220 from it. */
221 title = grub_script_execute_argument_to_string (cmd_menuentry->title);
222 if (! title)
223 return grub_errno;
225 /* Parse the menu entry *again*. */
226 script = grub_script_parse ((char *) cmd_menuentry->sourcecode, 0);
228 if (! script)
230 grub_free (title);
231 return grub_errno;
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. */
242 grub_err_t
243 grub_script_execute (struct grub_script *script)
245 if (script == 0)
246 return 0;
248 return grub_script_execute_cmd (script->cmd);