~ubuntu-branches/ubuntu/trusty/grub2/trusty-updates

« back to all changes in this revision

Viewing changes to kern/rescue_reader.c

Tags: upstream-1.99~20101122
ImportĀ upstreamĀ versionĀ 1.99~20101122

Show diffs side-by-side

added added

removed removed

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