rescue_reader.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 *data __attribute__ ((unused)))
  31. {
  32. int c;
  33. int pos = 0;
  34. char str[4];
  35. grub_printf ((cont) ? "> " : "grub rescue> ");
  36. grub_memset (linebuf, 0, GRUB_RESCUE_BUF_SIZE);
  37. while ((c = grub_getkey ()) != '\n' && c != '\r')
  38. {
  39. if (grub_isprint (c))
  40. {
  41. if (pos < GRUB_RESCUE_BUF_SIZE - 1)
  42. {
  43. str[0] = c;
  44. str[1] = 0;
  45. linebuf[pos++] = c;
  46. grub_xputs (str);
  47. }
  48. }
  49. else if (c == '\b')
  50. {
  51. if (pos > 0)
  52. {
  53. str[0] = c;
  54. str[1] = ' ';
  55. str[2] = c;
  56. str[3] = 0;
  57. linebuf[--pos] = 0;
  58. grub_xputs (str);
  59. }
  60. }
  61. grub_refresh ();
  62. }
  63. grub_xputs ("\n");
  64. grub_refresh ();
  65. *line = grub_strdup (linebuf);
  66. return 0;
  67. }
  68. void __attribute__ ((noreturn))
  69. grub_rescue_run (void)
  70. {
  71. grub_printf ("Entering rescue mode...\n");
  72. while (1)
  73. {
  74. char *line;
  75. /* Print an error, if any. */
  76. grub_print_error ();
  77. grub_errno = GRUB_ERR_NONE;
  78. grub_rescue_read_line (&line, 0, NULL);
  79. if (! line || line[0] == '\0')
  80. continue;
  81. grub_rescue_parse_line (line, grub_rescue_read_line, NULL);
  82. grub_free (line);
  83. }
  84. }