Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / commands / extcmd.c
blob69574e2b05b90f84b9d6d8855ede15178fa2f8b2
1 /* extcmd.c - support extended command */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 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/mm.h>
21 #include <grub/list.h>
22 #include <grub/misc.h>
23 #include <grub/extcmd.h>
24 #include <grub/script_sh.h>
25 #include <grub/dl.h>
27 GRUB_MOD_LICENSE ("GPLv3+");
29 grub_err_t
30 grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args,
31 struct grub_script *script)
33 int new_argc;
34 char **new_args;
35 struct grub_arg_list *state;
36 struct grub_extcmd_context context;
37 grub_err_t ret;
38 grub_extcmd_t ext = cmd->data;
40 context.state = 0;
41 context.extcmd = ext;
42 context.script = script;
44 if (! ext->options)
46 ret = (ext->func) (&context, argc, args);
47 return ret;
50 state = grub_arg_list_alloc (ext, argc, args);
51 if (grub_arg_parse (ext, argc, args, state, &new_args, &new_argc))
53 context.state = state;
54 ret = (ext->func) (&context, new_argc, new_args);
55 grub_free (new_args);
56 grub_free (state);
57 return ret;
60 grub_free (state);
61 return grub_errno;
64 static grub_err_t
65 grub_extcmd_dispatch (struct grub_command *cmd, int argc, char **args)
67 return grub_extcmd_dispatcher (cmd, argc, args, 0);
70 grub_extcmd_t
71 grub_register_extcmd_prio (const char *name, grub_extcmd_func_t func,
72 grub_command_flags_t flags, const char *summary,
73 const char *description,
74 const struct grub_arg_option *parser,
75 int prio)
77 grub_extcmd_t ext;
78 grub_command_t cmd;
80 ext = (grub_extcmd_t) grub_malloc (sizeof (*ext));
81 if (! ext)
82 return 0;
84 cmd = grub_register_command_prio (name, grub_extcmd_dispatch,
85 summary, description, prio);
86 if (! cmd)
88 grub_free (ext);
89 return 0;
92 cmd->flags = (flags | GRUB_COMMAND_FLAG_EXTCMD);
93 cmd->data = ext;
95 ext->cmd = cmd;
96 ext->func = func;
97 ext->options = parser;
98 ext->data = 0;
100 return ext;
103 grub_extcmd_t
104 grub_register_extcmd (const char *name, grub_extcmd_func_t func,
105 grub_command_flags_t flags, const char *summary,
106 const char *description,
107 const struct grub_arg_option *parser)
109 return grub_register_extcmd_prio (name, func, flags,
110 summary, description, parser, 1);
113 void
114 grub_unregister_extcmd (grub_extcmd_t ext)
116 grub_unregister_command (ext->cmd);
117 grub_free (ext);