2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2003,2005,2006,2007 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/normal.h>
20 #include <grub/misc.h>
23 #include <grub/term.h>
26 #include <grub/parser.h>
27 #include <grub/script.h>
29 static grub_command_t grub_command_list
;
32 grub_register_command (const char *name
,
33 grub_err_t (*func
) (struct grub_arg_list
*state
,
34 int argc
, char **args
),
37 const char *description
,
38 const struct grub_arg_option
*options
)
40 grub_command_t cmd
, *p
;
42 cmd
= (grub_command_t
) grub_malloc (sizeof (*cmd
));
46 cmd
->name
= grub_strdup (name
);
55 cmd
->summary
= summary
;
56 cmd
->description
= description
;
57 cmd
->options
= options
;
60 /* Keep the list sorted for simplicity. */
61 p
= &grub_command_list
;
64 if (grub_strcmp ((*p
)->name
, name
) >= 0)
70 if (*p
&& grub_strcmp ((*p
)->name
, name
) == 0)
75 if (q
->flags
& GRUB_COMMAND_FLAG_NOT_LOADED
)
78 q
->flags
= cmd
->flags
;
79 q
->summary
= cmd
->summary
;
80 q
->description
= cmd
->description
;
81 q
->options
= cmd
->options
;
82 grub_free (cmd
->name
);
83 grub_free (cmd
->module_name
);
89 grub_free (cmd
->name
);
104 grub_unregister_command (const char *name
)
106 grub_command_t
*p
, q
;
108 for (p
= &grub_command_list
, q
= *p
; q
; p
= &(q
->next
), q
= q
->next
)
109 if (grub_strcmp (name
, q
->name
) == 0)
113 grub_free (q
->module_name
);
120 grub_command_find (char *cmdline
)
126 first_space
= grub_strchr (cmdline
, ' ');
132 for (cmd
= grub_command_list
; cmd
; cmd
= cmd
->next
)
133 if (grub_strcmp (cmdline
, cmd
->name
) == 0)
137 grub_error (GRUB_ERR_UNKNOWN_COMMAND
, "unknown command `%s'", cmdline
);
138 else if (cmd
->flags
& GRUB_COMMAND_FLAG_NOT_LOADED
)
140 /* Automatically load the command. */
146 module_name
= grub_strdup (cmd
->module_name
);
149 mod
= grub_dl_load (module_name
);
154 grub_free (module_name
);
158 grub_free (module_name
);
162 /* This module seems broken. */
163 grub_unregister_command (cmdline
);
164 grub_error (GRUB_ERR_UNKNOWN_COMMAND
, "unknown command `%s'", cmdline
);
175 grub_iterate_commands (int (*iterate
) (grub_command_t
))
179 for (cmd
= grub_command_list
; cmd
; cmd
= cmd
->next
)
187 grub_command_execute (char *cmdline
, int interactive
)
189 auto grub_err_t
cmdline_get (char **s
);
190 grub_err_t
cmdline_get (char **s
)
192 *s
= grub_malloc (GRUB_MAX_CMDLINE
);
194 return grub_cmdline_get (">", *s
, GRUB_MAX_CMDLINE
, 0, 1);
199 struct grub_script
*parsed_script
;
201 /* Enable the pager if the environment pager is set to 1. */
203 pager
= grub_env_get ("pager");
206 if (pager
&& (! grub_strcmp (pager
, "1")))
209 /* Parse the script. */
210 parsed_script
= grub_script_parse (cmdline
, cmdline_get
);
214 /* Execute the command(s). */
215 grub_script_execute (parsed_script
);
217 /* The parsed script was executed, throw it away. */
218 grub_script_free (parsed_script
);
221 if (pager
&& (! grub_strcmp (pager
, "1")))
228 rescue_command (struct grub_arg_list
*state
__attribute__ ((unused
)),
229 int argc
__attribute__ ((unused
)),
230 char **args
__attribute__ ((unused
)))
232 grub_longjmp (grub_exit_env
, 0);
234 /* Never reach here. */
240 set_command (struct grub_arg_list
*state
__attribute__ ((unused
)),
241 int argc
, char **args
)
246 auto int print_env (struct grub_env_var
*env
);
247 int print_env (struct grub_env_var
*env
)
249 grub_printf ("%s=%s\n", env
->name
, env
->value
);
255 grub_env_iterate (print_env
);
260 val
= grub_strchr (var
, '=');
263 grub_error (GRUB_ERR_BAD_ARGUMENT
, "not an assignment");
268 grub_env_set (var
, val
+ 1);
274 unset_command (struct grub_arg_list
*state
__attribute__ ((unused
)),
275 int argc
, char **args
)
278 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
279 "no environment variable specified");
281 grub_env_unset (args
[0]);
286 export_command (struct grub_arg_list
*state
__attribute__ ((unused
)),
287 int argc
, char **args
)
290 return grub_error (GRUB_ERR_BAD_ARGUMENT
,
291 "no environment variable specified");
293 grub_env_export (args
[0]);
298 insmod_command (struct grub_arg_list
*state
__attribute__ ((unused
)),
299 int argc
, char **args
)
305 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "no module specified");
307 p
= grub_strchr (args
[0], '/');
309 mod
= grub_dl_load (args
[0]);
311 mod
= grub_dl_load_file (args
[0]);
320 rmmod_command (struct grub_arg_list
*state
__attribute__ ((unused
)),
321 int argc
, char **args
)
326 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "no module specified");
328 mod
= grub_dl_get (args
[0]);
330 return grub_error (GRUB_ERR_BAD_ARGUMENT
, "no such module");
332 if (! grub_dl_unref (mod
))
333 grub_dl_unload (mod
);
339 lsmod_command (struct grub_arg_list
*state
__attribute__ ((unused
)),
340 int argc
__attribute__ ((unused
)),
341 char **args
__attribute__ ((unused
)))
343 auto int print_module (grub_dl_t mod
);
345 int print_module (grub_dl_t mod
)
349 grub_printf ("%s\t%d\t\t", mod
->name
, mod
->ref_count
);
350 for (dep
= mod
->dep
; dep
; dep
= dep
->next
)
355 grub_printf ("%s", dep
->mod
->name
);
363 grub_printf ("Name\tRef Count\tDependencies\n");
364 grub_dl_iterate (print_module
);
369 grub_command_init (void)
371 grub_register_command ("rescue", rescue_command
, GRUB_COMMAND_FLAG_BOTH
,
372 "rescue", "Go back to the rescue mode.", 0);
374 grub_register_command ("set", set_command
, GRUB_COMMAND_FLAG_BOTH
,
375 "set [ENVVAR=VALUE]",
376 "Set an environment variable.", 0);
378 grub_register_command ("unset", unset_command
, GRUB_COMMAND_FLAG_BOTH
,
379 "unset ENVVAR", "Remove an environment variable.", 0);
381 grub_register_command ("export", export_command
, GRUB_COMMAND_FLAG_BOTH
,
382 "export ENVVAR", "Export a variable.", 0);
384 grub_register_command ("insmod", insmod_command
, GRUB_COMMAND_FLAG_BOTH
,
386 "Insert a module. The argument can be a file or a module name.",
389 grub_register_command ("rmmod", rmmod_command
, GRUB_COMMAND_FLAG_BOTH
,
390 "rmmod MODULE", "Remove a module.", 0);
392 grub_register_command ("lsmod", lsmod_command
, GRUB_COMMAND_FLAG_BOTH
,
393 "lsmod", "Show loaded modules.", 0);