command.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* command.c - support basic 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/command.h>
  21. grub_command_t grub_command_list;
  22. grub_command_t
  23. grub_register_command_prio (const char *name,
  24. grub_command_func_t func,
  25. const char *summary,
  26. const char *description,
  27. int prio)
  28. {
  29. grub_command_t cmd;
  30. int inactive = 0;
  31. grub_command_t *p, q;
  32. cmd = (grub_command_t) grub_zalloc (sizeof (*cmd));
  33. if (! cmd)
  34. return 0;
  35. cmd->name = name;
  36. cmd->func = func;
  37. cmd->summary = (summary) ? summary : "";
  38. cmd->description = description;
  39. cmd->flags = 0;
  40. cmd->prio = prio;
  41. for (p = &grub_command_list, q = *p; q; p = &(q->next), q = q->next)
  42. {
  43. int r;
  44. r = grub_strcmp (cmd->name, q->name);
  45. if (r < 0)
  46. break;
  47. if (r > 0)
  48. continue;
  49. if (cmd->prio >= (q->prio & GRUB_COMMAND_PRIO_MASK))
  50. {
  51. q->prio &= ~GRUB_COMMAND_FLAG_ACTIVE;
  52. break;
  53. }
  54. inactive = 1;
  55. }
  56. *p = cmd;
  57. cmd->next = q;
  58. if (q)
  59. q->prev = &cmd->next;
  60. cmd->prev = p;
  61. if (! inactive)
  62. cmd->prio |= GRUB_COMMAND_FLAG_ACTIVE;
  63. return cmd;
  64. }
  65. void
  66. grub_unregister_command (grub_command_t cmd)
  67. {
  68. if ((cmd->prio & GRUB_COMMAND_FLAG_ACTIVE) && (cmd->next))
  69. cmd->next->prio |= GRUB_COMMAND_FLAG_ACTIVE;
  70. grub_list_remove (GRUB_AS_LIST (cmd));
  71. grub_free (cmd);
  72. }