loadcfg.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * BURG - Brand-new Universal loadeR from GRUB
  3. * Copyright 2009 Bean Lee - All Rights Reserved
  4. *
  5. * BURG is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * BURG is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with BURG. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/dl.h>
  19. #include <grub/command.h>
  20. #include <grub/uitree.h>
  21. static grub_err_t
  22. grub_cmd_dump (grub_command_t cmd __attribute__ ((unused)),
  23. int argc, char **args)
  24. {
  25. grub_uitree_t root;
  26. root = &grub_uitree_root;
  27. if (argc)
  28. {
  29. root = grub_uitree_find (root, args[0]);
  30. if (! root)
  31. return grub_error (GRUB_ERR_BAD_ARGUMENT, "section not found");
  32. }
  33. grub_uitree_dump (root);
  34. return grub_errno;
  35. }
  36. static grub_err_t
  37. grub_cmd_free (grub_command_t cmd __attribute__ ((unused)),
  38. int argc, char **args)
  39. {
  40. while (argc)
  41. {
  42. grub_uitree_t node;
  43. node = grub_uitree_find (&grub_uitree_root, args[0]);
  44. if (node)
  45. {
  46. grub_tree_remove_node (GRUB_AS_TREE (node));
  47. grub_uitree_free (node);
  48. }
  49. argc--;
  50. args++;
  51. }
  52. return grub_errno;
  53. }
  54. static grub_err_t
  55. grub_cmd_loadcfg (grub_command_t cmd, int argc, char **args)
  56. {
  57. while (argc)
  58. {
  59. if (cmd->name[5] == 's')
  60. grub_uitree_load_string (&grub_uitree_root, *args,
  61. GRUB_UITREE_LOAD_FLAG_ROOT);
  62. else
  63. grub_uitree_load_file (&grub_uitree_root, *args,
  64. GRUB_UITREE_LOAD_FLAG_ROOT);
  65. if (grub_errno == GRUB_ERR_FILE_NOT_FOUND)
  66. grub_errno = 0;
  67. else if (grub_errno)
  68. break;
  69. argc--;
  70. args++;
  71. }
  72. return grub_errno;
  73. }
  74. static grub_command_t cmd_dump, cmd_load, cmd_str, cmd_free;
  75. GRUB_MOD_INIT(loadcfg)
  76. {
  77. cmd_dump =
  78. grub_register_command ("dump_config", grub_cmd_dump,
  79. "dump_config [NAMES]", "Dump config section.");
  80. cmd_free =
  81. grub_register_command ("free_config", grub_cmd_free,
  82. "free_config [NAMES]", "Free config section.");
  83. cmd_load =
  84. grub_register_command ("load_config", grub_cmd_loadcfg,
  85. "load_config [FILES]", "Load config file.");
  86. cmd_str =
  87. grub_register_command ("load_string", grub_cmd_loadcfg,
  88. "load_string [STRINGS]", "Load config string.");
  89. }
  90. GRUB_MOD_FINI(loadcfg)
  91. {
  92. grub_unregister_command (cmd_dump);
  93. grub_unregister_command (cmd_free);
  94. grub_unregister_command (cmd_load);
  95. grub_unregister_command (cmd_str);
  96. }