rescue_parser.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* rescue_parser.c - rescue mode parser */
  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/types.h>
  20. #include <grub/mm.h>
  21. #include <grub/env.h>
  22. #include <grub/parser.h>
  23. #include <grub/misc.h>
  24. #include <grub/command.h>
  25. static grub_err_t
  26. grub_rescue_parse_line (char *line, grub_reader_getline_t getline,
  27. void *closure)
  28. {
  29. char *name;
  30. int n;
  31. grub_command_t cmd;
  32. char **args;
  33. if (grub_parser_split_cmdline (line, getline, closure, &n, &args) || n < 0)
  34. return grub_errno;
  35. if (n == 0)
  36. return GRUB_ERR_NONE;
  37. /* In case of an assignment set the environment accordingly
  38. instead of calling a function. */
  39. if (n == 1 && grub_strchr (line, '='))
  40. {
  41. char *val = grub_strchr (args[0], '=');
  42. val[0] = 0;
  43. grub_env_set (args[0], val + 1);
  44. val[0] = '=';
  45. goto quit;
  46. }
  47. /* Get the command name. */
  48. name = args[0];
  49. /* If nothing is specified, restart. */
  50. if (*name == '\0')
  51. goto quit;
  52. cmd = grub_command_find (name);
  53. if (cmd)
  54. {
  55. (cmd->func) (cmd, n - 1, &args[1]);
  56. }
  57. else
  58. {
  59. grub_printf ("Unknown command `%s'\n", name);
  60. if (grub_command_find ("help"))
  61. grub_printf ("Try `help' for usage\n");
  62. }
  63. quit:
  64. grub_free (args[0]);
  65. grub_free (args);
  66. return grub_errno;
  67. }
  68. static struct grub_parser grub_rescue_parser =
  69. {
  70. .name = "rescue",
  71. .parse_line = grub_rescue_parse_line
  72. };
  73. void
  74. grub_register_rescue_parser (void)
  75. {
  76. grub_parser_register ("rescue", &grub_rescue_parser);
  77. }