2009-04-13 Felix Zielcke <fzielcke@z-51.de>
[grub2/phcoder/solaris.git] / normal / execute.c
blob8bf6d174557a7dc738278e52bb9ecf0c48adc5a4
1 /* execute.c -- Execute a GRUB script. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2008,2009 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/env.h>
24 #include <grub/script.h>
25 #include <grub/lib/arg.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 if (val)
53 size += grub_strlen (val);
55 else
56 size += grub_strlen (argi->str);
59 /* Create the argument. */
60 chararg = grub_malloc (size + 1);
61 if (! chararg)
62 return 0;
64 *chararg = '\0';
65 /* First determine the size of the argument. */
66 for (argi = arg; argi; argi = argi->next)
68 if (argi->type == 1)
70 val = grub_env_get (argi->str);
71 if (val)
72 grub_strcat (chararg, val);
74 else
75 grub_strcat (chararg, argi->str);
78 return chararg;
81 /* Execute a single command line. */
82 grub_err_t
83 grub_script_execute_cmdline (struct grub_script_cmd *cmd)
85 struct grub_script_cmdline *cmdline = (struct grub_script_cmdline *) cmd;
86 struct grub_script_arglist *arglist;
87 char **args = 0;
88 int i = 0;
89 grub_command_t grubcmd;
90 grub_err_t ret = 0;
91 int argcount = 0;
92 grub_script_function_t func = 0;
93 char errnobuf[6];
95 /* Lookup the command. */
96 grubcmd = grub_command_find (cmdline->cmdname);
97 if (! grubcmd)
99 /* Ignore errors. */
100 grub_errno = GRUB_ERR_NONE;
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)
141 ret = (grubcmd->func) (grubcmd, argcount, args);
142 else
143 ret = grub_script_function_call (func, argcount, args);
145 /* Free arguments. */
146 for (i = 0; i < argcount; i++)
147 grub_free (args[i]);
148 grub_free (args);
150 grub_sprintf (errnobuf, "%d", ret);
151 grub_env_set ("?", errnobuf);
153 return ret;
156 /* Execute a block of one or more commands. */
157 grub_err_t
158 grub_script_execute_cmdblock (struct grub_script_cmd *cmd)
160 struct grub_script_cmdblock *cmdblock = (struct grub_script_cmdblock *) cmd;
162 /* Loop over every command and execute it. */
163 for (cmd = cmdblock->cmdlist; cmd; cmd = cmd->next)
164 grub_script_execute_cmd (cmd);
166 return 0;
169 /* Execute an if statement. */
170 grub_err_t
171 grub_script_execute_cmdif (struct grub_script_cmd *cmd)
173 struct grub_script_cmdif *cmdif = (struct grub_script_cmdif *) cmd;
174 char *result;
176 /* Check if the commands results in a true or a false. The value is
177 read from the env variable `?'. */
178 grub_script_execute_cmd (cmdif->exec_to_evaluate);
179 result = grub_env_get ("?");
181 /* Execute the `if' or the `else' part depending on the value of
182 `?'. */
183 if (result && ! grub_strcmp (result, "0"))
184 return grub_script_execute_cmd (cmdif->exec_on_true);
185 else
186 return grub_script_execute_cmd (cmdif->exec_on_false);
189 /* Execute the menu entry generate statement. */
190 grub_err_t
191 grub_script_execute_menuentry (struct grub_script_cmd *cmd)
193 struct grub_script_cmd_menuentry *cmd_menuentry;
194 struct grub_script_arglist *arglist;
195 struct grub_script *script;
196 char **args = 0;
197 int argcount = 0;
198 int i = 0;
200 cmd_menuentry = (struct grub_script_cmd_menuentry *) cmd;
202 if (cmd_menuentry->arglist)
204 argcount = cmd_menuentry->arglist->argcount;
206 /* Create argv from the arguments. */
207 args = grub_malloc (sizeof (char *) * argcount);
209 if (! args)
211 return grub_errno;
214 for (arglist = cmd_menuentry->arglist; arglist; arglist = arglist->next)
216 char *str;
217 str = grub_script_execute_argument_to_string (arglist->arg);
218 args[i++] = str;
222 /* Parse the menu entry *again*. */
223 script = grub_script_parse ((char *) cmd_menuentry->sourcecode, 0);
225 /* Add new menu entry. */
226 if (script)
228 grub_normal_menu_addentry (argcount, (const char **)args,
229 script, cmd_menuentry->sourcecode);
232 /* Free arguments. */
233 for (i = 0; i < argcount; i++)
234 grub_free (args[i]);
235 grub_free (args);
237 return grub_errno;
242 /* Execute any GRUB pre-parsed command or script. */
243 grub_err_t
244 grub_script_execute (struct grub_script *script)
246 if (script == 0)
247 return 0;
249 return grub_script_execute_cmd (script->cmd);