extcmd.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* extcmd.c - support extended command */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  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.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. #include <grub/mm.h>
  20. #include <grub/list.h>
  21. #include <grub/misc.h>
  22. #include <grub/extcmd.h>
  23. #include <grub/script_sh.h>
  24. #include <grub/dl.h>
  25. GRUB_MOD_LICENSE ("GPLv3+");
  26. grub_err_t
  27. grub_extcmd_dispatcher (struct grub_command *cmd, int argc, char **args,
  28. struct grub_script *script)
  29. {
  30. int new_argc;
  31. char **new_args;
  32. struct grub_arg_list *state;
  33. struct grub_extcmd_context context;
  34. grub_err_t ret;
  35. grub_extcmd_t ext = cmd->data;
  36. context.state = 0;
  37. context.extcmd = ext;
  38. context.script = script;
  39. if (! ext->options)
  40. {
  41. ret = (ext->func) (&context, argc, args);
  42. return ret;
  43. }
  44. state = grub_arg_list_alloc (ext, argc, args);
  45. if (grub_arg_parse (ext, argc, args, state, &new_args, &new_argc))
  46. {
  47. context.state = state;
  48. ret = (ext->func) (&context, new_argc, new_args);
  49. grub_free (new_args);
  50. grub_free (state);
  51. return ret;
  52. }
  53. grub_free (state);
  54. return grub_errno;
  55. }
  56. static grub_err_t
  57. grub_extcmd_dispatch (struct grub_command *cmd, int argc, char **args)
  58. {
  59. return grub_extcmd_dispatcher (cmd, argc, args, 0);
  60. }
  61. grub_extcmd_t
  62. grub_register_extcmd_prio (const char *name, grub_extcmd_func_t func,
  63. grub_command_flags_t flags, const char *summary,
  64. const char *description,
  65. const struct grub_arg_option *parser,
  66. int prio)
  67. {
  68. grub_extcmd_t ext;
  69. grub_command_t cmd;
  70. ext = (grub_extcmd_t) grub_malloc (sizeof (*ext));
  71. if (! ext)
  72. return 0;
  73. cmd = grub_register_command_prio (name, grub_extcmd_dispatch,
  74. summary, description, prio);
  75. if (! cmd)
  76. {
  77. grub_free (ext);
  78. return 0;
  79. }
  80. cmd->flags = (flags | GRUB_COMMAND_FLAG_EXTCMD);
  81. cmd->data = ext;
  82. ext->cmd = cmd;
  83. ext->func = func;
  84. ext->options = parser;
  85. ext->data = 0;
  86. return ext;
  87. }
  88. grub_extcmd_t
  89. grub_register_extcmd (const char *name, grub_extcmd_func_t func,
  90. grub_command_flags_t flags, const char *summary,
  91. const char *description,
  92. const struct grub_arg_option *parser)
  93. {
  94. return grub_register_extcmd_prio (name, func, flags,
  95. summary, description, parser, 1);
  96. }
  97. void
  98. grub_unregister_extcmd (grub_extcmd_t ext)
  99. {
  100. grub_unregister_command (ext->cmd);
  101. grub_free (ext);
  102. }