rescue_parser.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. return grub_errno;
  37. if (n == 0)
  38. return GRUB_ERR_NONE;
  39. /* In case of an assignment set the environment accordingly
  40. instead of calling a function. */
  41. if (n == 1)
  42. {
  43. char *val = grub_strchr (args[0], '=');
  44. if (val)
  45. {
  46. val[0] = 0;
  47. grub_env_set (args[0], val + 1);
  48. val[0] = '=';
  49. goto quit;
  50. }
  51. }
  52. /* Get the command name. */
  53. name = args[0];
  54. /* If nothing is specified, restart. */
  55. if (*name == '\0')
  56. goto quit;
  57. cmd = grub_command_find (name);
  58. if (cmd)
  59. {
  60. (cmd->func) (cmd, n - 1, &args[1]);
  61. }
  62. else
  63. {
  64. grub_printf_ (N_("Unknown command `%s'.\n"), name);
  65. if (grub_command_find ("help"))
  66. grub_printf ("Try `help' for usage\n");
  67. }
  68. quit:
  69. /* Arguments are returned in single memory chunk separated by zeroes */
  70. grub_free (args[0]);
  71. grub_free (args);
  72. return grub_errno;
  73. }