char_io.c revision e7cbe64f7a72dae5cb44f100db60ca88f3313c65
/* char_io.c - basic console input and output */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1999,2000,2001,2002,2004 Free Software Foundation, Inc.
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <shared.h>
#include <term.h>
#ifdef SUPPORT_HERCULES
# include <hercules.h>
#endif
#ifdef SUPPORT_SERIAL
# include <serial.h>
#endif
#ifndef STAGE1_5
struct term_entry term_table[] =
{
{
"console",
0,
24,
0,
0
},
#ifdef SUPPORT_SERIAL
{
"serial",
/* A serial device must be initialized. */
24,
0,
0,
0,
0
},
#endif /* SUPPORT_SERIAL */
#ifdef SUPPORT_HERCULES
{
"hercules",
0,
24,
0,
0
},
#endif /* SUPPORT_HERCULES */
#ifdef SUPPORT_GRAPHICS
{ "graphics",
TERM_NEED_INIT, /* flags */
30, /* number of lines */
graphics_putchar, /* putchar */
console_checkkey, /* checkkey */
console_getkey, /* getkey */
graphics_getxy, /* getxy */
graphics_gotoxy, /* gotoxy */
graphics_cls, /* cls */
graphics_setcolorstate, /* setcolorstate */
graphics_setcolor, /* setcolor */
graphics_setcursor, /* nocursor */
graphics_init, /* initialize */
graphics_end /* shutdown */
},
#endif /* SUPPORT_GRAPHICS */
/* This must be the last entry. */
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
/* This must be console. */
int max_lines = 24;
int count_lines = -1;
int use_pager = 1;
#endif
void
print_error (void)
{
#ifndef STAGE1_5
/* printf("\7\n %s\n", err_list[errnum]); */
#else /* STAGE1_5 */
#endif /* STAGE1_5 */
}
char *
convert_to_ascii (char *buf, int c,...)
{
#ifndef STAGE1_5
if (c == 'x' || c == 'X')
mult = 16;
{
*(ptr++) = '-';
buf++;
}
#endif
do
{
}
/* reorder to correct direction!! */
{
{
ptr1--;
ptr2++;
}
}
return ptr;
}
void
grub_putstr (const char *str)
{
while (*str)
grub_putchar (*str++);
}
static void
{
char c, str[16];
while ((c = *(format++)) != 0)
{
if (c != '%')
grub_putchar (c);
else
switch (c = *(format++))
{
#ifndef STAGE1_5
case 'd':
case 'x':
case 'X':
#endif
case 'u':
grub_putstr (str);
break;
#ifndef STAGE1_5
case 'c':
break;
case 's':
grub_putstr ((char *) *(dataptr++));
break;
#endif
}
}
}
#ifndef STAGE1_5
void
init_page (void)
{
cls ();
grub_printf ("\n GNU GRUB version %s (%dK lower / %dK upper memory)\n\n",
}
/* The number of the history entries. */
static int num_history = 0;
/* Get the NOth history. If NO is less than zero or greater than or
equal to NUM_HISTORY, return NULL. Otherwise return a valid string. */
static char *
get_history (int no)
{
return 0;
}
/* Add CMDLINE to the history buffer. */
static void
{
if (num_history < HISTORY_SIZE)
num_history++;
}
static int
{
/* This is a rather complicated function. So explain the concept.
A command-line consists of ``section''s. A section is a part of the
line which may be displayed on the screen, but a section is never
displayed with another section simultaneously.
Each section is basically 77 or less characters, but the exception
is the first section, which is 78 or less characters, because the
starting point is special. See below.
The first section contains a prompt and a command-line (or the
first part of a command-line when it is too long to be fit in the
screen). So, in the first section, the number of command-line
characters displayed is 78 minus the length of the prompt (or
less). If the command-line has more characters, `>' is put at the
position 78 (zero-origin), to inform the user of the hidden
characters.
Other sections always have `<' at the first position, since there
is absolutely a section before each section. If there is a section
after another section, this section consists of 77 characters and
`>' at the last position. The last section has 77 or less
characters and doesn't have `>'.
Each section other than the last shares some characters with the
previous section. This region is called ``margin''. If the cursor
is put at the magin which is shared by the first section and the
second, the first section is displayed. Otherwise, a displayed
section is switched to another section, only if the cursor is put
outside that section. */
/* XXX: These should be defined in shared.h, but I leave these here,
until this code is freezed. */
#define CMDLINE_WIDTH 78
#define CMDLINE_MARGIN 10
/* The length of PROMPT. */
int plen;
/* The length of the command-line. */
int llen;
/* The index for the history. */
int history = -1;
/* The working buffer for the command-line. */
char *buf = (char *) CMDLINE_BUF;
/* The kill buffer. */
/* Nested function definitions for code simplicity. */
/* The forward declarations of nested functions are prefixed
with `auto'. */
auto void cl_backward (int count);
auto void cl_forward (int count);
auto void cl_init (void);
/* Move the cursor backward. */
void cl_backward (int count)
{
/* If the cursor is in the first section, display the first section
instead of the second. */
cl_refresh (1, 0);
cl_refresh (1, 0);
else
{
{
int i;
for (i = 0; i < count; i++)
grub_putchar ('\b');
}
else
}
}
/* Move the cursor forward. */
void cl_forward (int count)
{
/* If the cursor goes outside, scroll the screen to the right. */
cl_refresh (1, 0);
else
{
{
int i;
{
if (! echo_char)
grub_putchar (buf[i]);
else
}
}
else
}
}
/* Refresh the screen. If FULL is true, redraw the full line, otherwise,
only LEN characters from LPOS. */
{
int i;
int start;
if (full)
{
/* Recompute the section number. */
section = 0;
else
/* From the start to the end. */
len = CMDLINE_WIDTH;
pos = 0;
grub_putchar ('\r');
/* If SECTION is the first section, print the prompt, otherwise,
print `<'. */
if (section == 0)
{
}
else
{
grub_putchar ('<');
len--;
pos++;
}
}
/* Compute the index to start writing BUF and the resulting position
on the screen. */
if (section == 0)
{
int offset = 0;
if (! full)
start = 0;
}
else
{
int offset = 0;
if (! full)
}
/* Print BUF. If ECHO_CHAR is not zero, put it instead. */
{
if (! echo_char)
grub_putchar (buf[i]);
else
pos++;
}
/* Fill up the rest of the line with spaces. */
{
grub_putchar (' ');
pos++;
}
/* If the cursor is at the last position, put `>' or a space,
depending on if there are more characters in BUF. */
if (pos == CMDLINE_WIDTH)
{
grub_putchar ('>');
else
grub_putchar (' ');
pos++;
}
/* Back to XPOS. */
{
grub_putchar ('\b');
}
else
}
/* Initialize the command-line. */
void cl_init (void)
{
/* Distinguish us from other lines and error messages! */
grub_putchar ('\n');
/* Print full line and set position here. */
cl_refresh (1, 0);
}
/* Insert STR to BUF. */
{
int l = grub_strlen (str);
{
else
{
}
llen += l;
lpos += l;
if (xpos + l >= CMDLINE_WIDTH)
cl_refresh (1, 0);
else
}
}
/* Delete COUNT characters in BUF. */
{
else
}
if (maxlen > MAX_CMDLINE)
{
if (llen >= MAX_CMDLINE)
{
cmdline[MAX_CMDLINE] = 0;
}
}
cl_init ();
{
/* If READLINE is non-zero, handle readline-like key bindings. */
if (readline)
{
switch (c)
{
case 9: /* TAB lists completions */
{
int i;
/* POS points to the first space after a command. */
int pos = 0;
int ret;
char *completion_buffer = (char *) COMPLETION_BUF;
int equal_pos = -1;
int is_filename;
/* Find the first word. */
pos++;
pos++;
/* Find the position of the equal character after a
command, and replace it with a space. */
if (buf[i] == '=')
{
equal_pos = i;
buf[i] = ' ';
break;
}
/* Find the position of the first character in this
word. */
;
/* Invalidate the cache, because the user may exchange
removable disks. */
buf_drive = -1;
/* Copy this word to COMPLETION_BUFFER and do the
completion. */
completion_buffer[lpos - i] = 0;
if (ret >= 0)
{
/* Found, so insert COMPLETION_BUFFER. */
if (ret > 0)
{
/* There are more than one candidates, so print
the list. */
grub_putchar ('\n');
print_completions (is_filename, 0);
}
}
/* Restore the command-line. */
if (equal_pos >= 0)
if (ret)
cl_init ();
}
break;
case 1: /* C-a go to beginning of line */
cl_backward (lpos);
break;
case 5: /* C-e go to end of line */
break;
case 6: /* C-f forward one character */
cl_forward (1);
break;
case 2: /* C-b backward one character */
if (lpos > 0)
cl_backward (1);
break;
case 21: /* C-u kill to beginning of line */
if (lpos == 0)
break;
/* Copy the string being deleted to KILL_BUF. */
{
/* XXX: Not very clever. */
cl_backward (lpos);
}
break;
case 11: /* C-k kill to end of line */
break;
/* Copy the string being deleted to KILL_BUF. */
break;
case 25: /* C-y yank the kill buffer */
break;
case 16: /* C-p fetch the previous command */
{
char *p;
if (history < 0)
/* Save the working buffer. */
/* If BUF is modified, add it into the history list. */
history++;
p = get_history (history);
if (! p)
{
history--;
break;
}
grub_strcpy (buf, p);
cl_refresh (1, 0);
}
break;
case 14: /* C-n fetch the next command */
{
char *p;
if (history < 0)
{
break;
}
/* If BUF is modified, add it into the history list. */
history--;
p = get_history (history);
if (! p)
p = cmdline;
grub_strcpy (buf, p);
cl_refresh (1, 0);
}
break;
}
}
/* ESC, C-d and C-h are always handled. Actually C-d is not
functional if READLINE is zero, as the cursor cannot go
backward, but that's ok. */
switch (c)
{
case 27: /* ESC immediately return 1 */
return 1;
case 4: /* C-d delete character under cursor */
break;
cl_delete (1);
break;
case 8: /* C-h backspace */
# ifdef GRUB_UTIL
case 127: /* also backspace */
# endif
if (lpos > 0)
{
cl_backward (1);
cl_delete (1);
}
break;
default: /* insert printable character into line */
if (c >= ' ' && c <= '~')
{
char str[2];
str[0] = c;
str[1] = 0;
}
}
}
grub_putchar ('\n');
/* If ECHO_CHAR is NUL, remove the leading spaces. */
lpos = 0;
if (! echo_char)
lpos++;
/* Copy the working buffer to CMDLINE. */
/* If the readline-like feature is turned on and CMDLINE is not
empty, add it into the history list. */
add_history (cmdline, 0);
return 0;
}
/* Don't use this with a MAXLEN greater than 1600 or so! The problem
is that GET_CMDLINE depends on the everything fitting on the screen
at once. So, the whole screen is about 2000 characters, minus the
PROMPT, and space for error and status lines, etc. MAXLEN must be
at least 1, and PROMPT and CMDLINE must be valid strings (not NULL
or zero-length).
If ECHO_CHAR is nonzero, echo it instead of the typed character. */
int
{
int old_cursor;
int ret;
/* Because it is hard to deal with different conditions simultaneously,
less functional cases are handled here. Assume that TERM_NO_ECHO
implies TERM_NO_EDIT. */
{
char *p = cmdline;
int c;
/* Make sure that MAXLEN is not too large. */
if (maxlen > MAX_CMDLINE)
/* Print only the prompt. The contents of CMDLINE is simply discarded,
even if it is not empty. */
/* Gather characters until a newline is gotten. */
{
/* Return immediately if ESC is pressed. */
if (c == 27)
{
return 1;
}
/* Printable characters are added into CMDLINE. */
if (c >= ' ' && c <= '~')
{
grub_putchar (c);
/* Preceding space characters must be ignored. */
if (c != ' ' || p != cmdline)
*p++ = c;
}
}
*p = 0;
grub_putchar ('\n');
return 0;
}
/* Complicated features are left to real_get_cmdline. */
return ret;
}
int
{
int myint = 0;
/*
* Is this a hex number?
*/
{
ptr += 2;
mult = 16;
}
while (1)
{
/* A bit tricky. This below makes use of the equivalence:
(A >= B && A <= C) <=> ((A - B) <= (C - B))
when C > B and A is unsigned. */
unsigned int digit;
if (digit > 9)
{
break;
digit += 10;
}
found = 1;
{
return 0;
}
ptr++;
}
if (!found)
{
return 0;
}
return 1;
}
#endif /* STAGE1_5 */
static int
{
/* XXX hohmuth
ugly hack -- should unify with printf() */
int len = 0;
while ((c = *format++) != 0)
{
if (c != '%') {
if (buffer)
*bp++ = c; /* putchar(c); */
len++;
} else {
switch (c = *(format++))
{
case 'd': case 'u': case 'x':
while (*ptr) {
if (buffer)
else
ptr++;
len++;
}
break;
case 'c':
if (buffer)
else
dataptr++;
len++;
/* putchar((*(dataptr++))&0xff); */
break;
case 's':
while ((c = *ptr++) != 0) {
if (buffer)
*bp++ = c; /* putchar(c); */
len++;
}
break;
}
}
}
*bp = 0;
return (len);
}
int
{
dataptr++;
}
#endif /* !defined(STAGE1_5) || defined(FSYS_ZFS) */
void
noisy_printf (const char *format,...)
{
dataptr++;
}
/*
* print to a buffer, unless verbose mode is on
* if verbos mode is switched on, the buffer is dumped in verbose_func()
*/
void
grub_printf (const char *format,...)
{
int len;
dataptr++;
#ifndef STAGE1_5
#endif
#ifndef STAGE1_5
else {
SCREENBUF) {
}
}
#endif
}
int
grub_tolower (int c)
{
if (c >= 'A' && c <= 'Z')
return (c + ('a' - 'A'));
return c;
}
#endif /* ! STAGE1_5 || FSYS_FAT */
int
grub_isspace (int c)
{
switch (c)
{
case ' ':
case '\t':
case '\r':
case '\n':
return 1;
default:
break;
}
return 0;
}
#if !defined(STAGE1_5) || defined(FSYS_ISO9660)
int
{
while (n)
{
return -1;
return 1;
s1++;
s2++;
n--;
}
return 0;
}
#endif /* ! STAGE1_5 || FSYS_ISO9660 */
#ifndef STAGE1_5
int
{
int i = -1;
while (++i < n && s1[i] != 0);
s1[n - 1] = 0;
if (i >= n)
return 0;
s1[i] = 0;
return 1;
}
#endif /* ! STAGE1_5 */
/* XXX: This below is an evil hack. Certainly, we should change the
strategy to determine what should be defined and what shouldn't be
defined for each image. For example, it would be better to create
a static library supporting minimal standard C functions and link
each image with the library. Complicated things should be left to
computer, definitely. -okuji */
/* Make some grub_str* routines available to ZFS plug-in as well */
int
{
{
return -1;
return 1;
s1 ++;
s2 ++;
}
return 0;
}
int
{
return (0);
n++;
if (*s1++ == '\0')
return (0);
}
#endif /* ! STAGE1_5 || FSYS_VSTAFS || defined(FSYS_ZFS) */
#ifndef STAGE1_5
/* Wait for a keypress and return its code. */
int
getkey (void)
{
return current_term->getkey ();
}
/* Check if a key code is available. */
int
checkkey (void)
{
return current_term->checkkey ();
}
#endif /* ! STAGE1_5 */
/* Display an ASCII character. */
void
grub_putchar (int c)
{
if (c == '\n')
grub_putchar ('\r');
#ifndef STAGE1_5
{
int n;
while (n--)
grub_putchar (' ');
return;
}
#endif /* ! STAGE1_5 */
#ifdef STAGE1_5
/* In Stage 1.5, only the normal console is supported. */
console_putchar (c);
#else /* ! STAGE1_5 */
if (c == '\n')
{
/* Internal `more'-like feature. */
if (count_lines >= 0)
{
count_lines++;
{
int tmp;
/* It's important to disable the feature temporarily, because
the following grub_printf call will print newlines. */
count_lines = -1;
grub_printf("\n");
if (current_term->setcolorstate)
grub_printf ("[Hit return to continue]");
if (current_term->setcolorstate)
do
{
}
grub_printf ("\r \r");
/* Restart to count lines. */
count_lines = 0;
return;
}
}
}
current_term->putchar (c);
#endif /* ! STAGE1_5 */
}
#ifndef STAGE1_5
void
gotoxy (int x, int y)
{
current_term->gotoxy (x, y);
}
int
getxy (void)
{
return current_term->getxy ();
}
void
cls (void)
{
/* If the terminal is dumb, there is no way to clean the terminal. */
grub_putchar ('\n');
else
current_term->cls ();
}
int
{
if (current_term->setcursor)
return 1;
}
#endif /* ! STAGE1_5 */
int
{
{
/* The strings match exactly. */
if (! *(s1++))
return 0;
s2 ++;
}
/* S1 is a substring of S2. */
if (*s1 == 0)
return -1;
/* S1 isn't a substring. */
return 1;
}
char *
{
while (*s1)
{
return (char *) s1;
s1++;
}
return 0;
}
int
grub_strlen (const char *str)
{
int len = 0;
while (*str++)
len++;
return len;
}
#endif /* !defined(STAGE1_5) || defined(FSYS_ZFS) */
#ifndef STAGE1_5
/* Terminate the string STR with NUL. */
int
nul_terminate (char *str)
{
int ch;
str++;
*str = 0;
return ch;
}
#endif /* ! STAGE1_5 */
int
{
int local_errnum = 0;
#ifdef GRUB_UTIL
static unsigned long start_addr (void)
{
int ret;
# if defined(HAVE_START_SYMBOL)
# elif defined(HAVE_USCORE_START_SYMBOL)
# endif
return ret;
}
static unsigned long end_addr (void)
{
int ret;
# if defined(HAVE_END_SYMBOL)
# elif defined(HAVE_USCORE_END_SYMBOL)
# endif
return ret;
}
return ! local_errnum;
#endif /* GRUB_UTIL */
if (errnum == 0) /* preserve original errnum */
return ! local_errnum;
}
void
{
int i;
for (i = 0; i < len; i++)
d[i] = s[i];
}
void *
{
{
/* This assembly code is stolen from
linux-2.2.2/include/asm-i386/string.h. This is not very fast
but compact. */
{
asm volatile ("cld\n\t"
"rep\n\t"
"movsb"
: "memory");
}
else
{
asm volatile ("std\n\t"
"rep\n\t"
"movsb\n\t"
"cld"
: "0" (len),
: "memory");
}
return to;
}
return NULL;
}
void *
{
char *p = start;
{
while (len -- > 0)
*p ++ = c;
}
}
#ifndef STAGE1_5
char *
{
return dest;
}
#endif /* ! STAGE1_5 */
#ifndef GRUB_UTIL
/* GCC emits references to memcpy() for struct copies etc. */
#endif