rescue_parser.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include <grub/i18n.h>
  26. grub_err_t
  27. grub_rescue_parse_line (char *line,
  28. grub_reader_getline_t getline, void *getline_data)
  29. {
  30. char *name;
  31. int n;
  32. grub_command_t cmd;
  33. char **args;
  34. if (grub_parser_split_cmdline (line, getline, getline_data, &n, &args)
  35. || n < 0)
  36. {
  37. grub_free(args);
  38. return grub_errno;
  39. }
  40. if (n == 0)
  41. {
  42. grub_free(args);
  43. return GRUB_ERR_NONE;
  44. }
  45. /* In case of an assignment set the environment accordingly
  46. instead of calling a function. */
  47. if (n == 1)
  48. {
  49. char *val = grub_strchr (args[0], '=');
  50. if (val)
  51. {
  52. val[0] = 0;
  53. grub_env_set (args[0], val + 1);
  54. val[0] = '=';
  55. goto quit;
  56. }
  57. }
  58. /* Get the command name. */
  59. name = args[0];
  60. /* If nothing is specified, restart. */
  61. if (*name == '\0')
  62. goto quit;
  63. cmd = grub_command_find (name);
  64. if (cmd)
  65. {
  66. (cmd->func) (cmd, n - 1, &args[1]);
  67. }
  68. else
  69. {
  70. grub_printf_ (N_("Unknown command `%s'.\n"), name);
  71. if (grub_command_find ("help"))
  72. grub_printf ("Try `help' for usage\n");
  73. }
  74. quit:
  75. /* Arguments are returned in single memory chunk separated by zeroes */
  76. grub_free (args[0]);
  77. grub_free (args);
  78. return grub_errno;
  79. }