handler.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* handler.c - commands to list or select handlers */
  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/dl.h>
  20. #include <grub/err.h>
  21. #include <grub/misc.h>
  22. #include <grub/term.h>
  23. #include <grub/handler.h>
  24. #include <grub/command.h>
  25. #include <grub/i18n.h>
  26. static int
  27. list_item (grub_named_list_t item, void *closure)
  28. {
  29. if (item == closure)
  30. grub_putchar ('*');
  31. grub_printf ("%s\n", item->name);
  32. return 0;
  33. }
  34. static grub_err_t
  35. grub_cmd_handler (struct grub_command *cmd __attribute__ ((unused)),
  36. int argc, char **args)
  37. {
  38. void *curr_item = 0;
  39. grub_handler_class_t head;
  40. head = grub_handler_class_list;
  41. if (argc == 0)
  42. {
  43. grub_list_iterate (GRUB_AS_LIST (head), (grub_list_hook_t) list_item,
  44. curr_item);
  45. }
  46. else
  47. {
  48. char *class_name;
  49. grub_handler_class_t class;
  50. class_name = args[0];
  51. argc--;
  52. args++;
  53. class = grub_named_list_find (GRUB_AS_NAMED_LIST (head), class_name);
  54. if (! class)
  55. return grub_error (GRUB_ERR_FILE_NOT_FOUND, "class not found");
  56. if (argc == 0)
  57. {
  58. curr_item = class->cur_handler;
  59. grub_list_iterate (GRUB_AS_LIST (class->handler_list),
  60. (grub_list_hook_t) list_item, curr_item);
  61. }
  62. else
  63. {
  64. grub_handler_t handler;
  65. handler =
  66. grub_named_list_find (GRUB_AS_NAMED_LIST (class->handler_list),
  67. args[0]);
  68. if (! handler)
  69. return grub_error (GRUB_ERR_FILE_NOT_FOUND, "handler not found");
  70. grub_handler_set_current (class, handler);
  71. }
  72. }
  73. return 0;
  74. }
  75. static grub_command_t cmd_handler;
  76. GRUB_MOD_INIT(handler)
  77. {
  78. cmd_handler =
  79. grub_register_command ("handler", grub_cmd_handler,
  80. N_("[class [handler]]"),
  81. N_("List or select a handler."));
  82. }
  83. GRUB_MOD_FINI(handler)
  84. {
  85. grub_unregister_command (cmd_handler);
  86. }