rescue_reader.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* rescue_reader.c - rescue mode reader */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009,2010 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/reader.h>
  21. #include <grub/parser.h>
  22. #include <grub/misc.h>
  23. #include <grub/term.h>
  24. #include <grub/mm.h>
  25. #define GRUB_RESCUE_BUF_SIZE 256
  26. static char linebuf[GRUB_RESCUE_BUF_SIZE];
  27. /* Prompt to input a command and read the line. */
  28. static grub_err_t
  29. grub_rescue_read_line (char **line, int cont,
  30. void *closure __attribute__ ((unused)))
  31. {
  32. int c;
  33. int pos = 0;
  34. grub_printf ((cont) ? "> " : "grub rescue> ");
  35. grub_memset (linebuf, 0, GRUB_RESCUE_BUF_SIZE);
  36. while ((c = GRUB_TERM_ASCII_CHAR (grub_getkey ())) != '\n' && c != '\r')
  37. {
  38. if (grub_isprint (c))
  39. {
  40. if (pos < GRUB_RESCUE_BUF_SIZE - 1)
  41. {
  42. linebuf[pos++] = c;
  43. grub_putchar (c);
  44. }
  45. }
  46. else if (c == '\b')
  47. {
  48. if (pos > 0)
  49. {
  50. linebuf[--pos] = 0;
  51. grub_putchar (c);
  52. grub_putchar (' ');
  53. grub_putchar (c);
  54. }
  55. }
  56. grub_refresh ();
  57. }
  58. grub_putchar ('\n');
  59. grub_refresh ();
  60. *line = grub_strdup (linebuf);
  61. return 0;
  62. }
  63. void
  64. grub_rescue_run (void)
  65. {
  66. grub_printf ("Entering rescue mode...\n");
  67. while (1)
  68. {
  69. char *line;
  70. /* Print an error, if any. */
  71. grub_print_error ();
  72. grub_errno = GRUB_ERR_NONE;
  73. grub_rescue_read_line (&line, 0, 0);
  74. if (! line || line[0] == '\0')
  75. continue;
  76. grub_parser_get_current ()->parse_line (line, grub_rescue_read_line, 0);
  77. grub_free (line);
  78. }
  79. }