2009-07-07 Pavel Roskin <proski@gnu.org>
[grub2/bean.git] / commands / extcmd.c
bloba6053872a103f3d91b911e415926023992172945
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>
25 static grub_err_t
26 grub_extcmd_dispatcher (struct grub_command *cmd,
27 int argc, char **args)
29 int new_argc;
30 char **new_args;
31 struct grub_arg_option *parser;
32 struct grub_arg_list *state;
33 int maxargs = 0;
34 grub_err_t ret;
35 grub_extcmd_t ext;
37 ext = cmd->data;
38 parser = (struct grub_arg_option *) ext->options;
39 while (parser && (parser++)->doc)
40 maxargs++;
42 /* Set up the option state. */
43 state = grub_malloc (sizeof (struct grub_arg_list) * maxargs);
44 grub_memset (state, 0, sizeof (struct grub_arg_list) * maxargs);
46 if (grub_arg_parse (ext, argc, args, state, &new_args, &new_argc))
48 ext->state = state;
49 ret = (ext->func) (ext, new_argc, new_args);
50 grub_free (new_args);
52 else
53 ret = grub_errno;
55 grub_free (state);
57 return ret;
60 grub_extcmd_t
61 grub_register_extcmd (const char *name, grub_extcmd_func_t func,
62 unsigned flags, const char *summary,
63 const char *description,
64 const struct grub_arg_option *parser)
66 grub_extcmd_t ext;
67 grub_command_t cmd;
69 ext = (grub_extcmd_t) grub_malloc (sizeof (*ext));
70 if (! ext)
71 return 0;
73 cmd = grub_register_command_prio (name, grub_extcmd_dispatcher,
74 summary, description, 1);
75 if (! cmd)
77 grub_free (ext);
78 return 0;
81 cmd->flags = (flags | GRUB_COMMAND_FLAG_EXTCMD);
82 cmd->data = ext;
84 ext->cmd = cmd;
85 ext->func = func;
86 ext->options = parser;
87 ext->data = 0;
89 return ext;
92 void
93 grub_unregister_extcmd (grub_extcmd_t ext)
95 grub_unregister_command (ext->cmd);
96 grub_free (ext);