2013-04-26 22:02:23 +00:00
|
|
|
|
/* console.c -- console for GRUB. */
|
2003-11-17 18:07:09 +00:00
|
|
|
|
/*
|
2004-04-04 13:46:03 +00:00
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2013-04-26 22:02:23 +00:00
|
|
|
|
* Copyright (C) 2013 Free Software Foundation, Inc.
|
2003-11-17 18:07:09 +00:00
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
2003-11-17 18:07:09 +00:00
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-21 23:32:33 +00:00
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
2003-11-17 18:07:09 +00:00
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
2007-07-21 23:32:33 +00:00
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
2003-11-17 18:07:09 +00:00
|
|
|
|
* 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
|
2007-07-21 23:32:33 +00:00
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
2003-11-17 18:07:09 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2005-08-07 17:12:52 +00:00
|
|
|
|
#include <config.h>
|
2010-09-19 20:22:43 +00:00
|
|
|
|
#include <config-util.h>
|
2005-08-07 17:12:52 +00:00
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
#include <grub/term.h>
|
|
|
|
|
#include <grub/types.h>
|
2013-04-26 22:02:23 +00:00
|
|
|
|
#include <grub/misc.h>
|
|
|
|
|
#include <grub/mm.h>
|
|
|
|
|
#include <grub/time.h>
|
|
|
|
|
#include <grub/terminfo.h>
|
|
|
|
|
#include <grub/dl.h>
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <termios.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <langinfo.h>
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
2013-04-26 22:02:23 +00:00
|
|
|
|
#include <grub/emu/console.h>
|
2008-08-05 14:20:00 +00:00
|
|
|
|
|
2013-04-26 22:02:23 +00:00
|
|
|
|
extern struct grub_terminfo_output_state grub_console_terminfo_output;
|
|
|
|
|
static int original_fl;
|
|
|
|
|
static int saved_orig;
|
|
|
|
|
static struct termios orig_tty;
|
|
|
|
|
static struct termios new_tty;
|
2008-08-05 14:20:00 +00:00
|
|
|
|
|
2003-11-17 18:07:09 +00:00
|
|
|
|
static void
|
2013-04-26 22:02:23 +00:00
|
|
|
|
put (struct grub_term_output *term __attribute__ ((unused)), const int c)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
2013-04-26 22:02:23 +00:00
|
|
|
|
char chr = c;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
2013-04-26 22:02:23 +00:00
|
|
|
|
write (STDOUT_FILENO, &chr, 1);
|
2003-11-17 18:07:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2013-04-26 22:02:23 +00:00
|
|
|
|
readkey (struct grub_term_input *term __attribute__ ((unused)))
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
2013-04-26 22:02:23 +00:00
|
|
|
|
grub_uint8_t c;
|
|
|
|
|
ssize_t actual;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
2013-04-26 22:02:23 +00:00
|
|
|
|
actual = read (STDIN_FILENO, &c, 1);
|
|
|
|
|
if (actual > 0)
|
|
|
|
|
return c;
|
|
|
|
|
return -1;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-26 22:02:23 +00:00
|
|
|
|
static grub_err_t
|
|
|
|
|
grub_console_init_input (struct grub_term_input *term)
|
2005-08-12 19:53:33 +00:00
|
|
|
|
{
|
2013-04-26 22:02:23 +00:00
|
|
|
|
if (!saved_orig)
|
|
|
|
|
{
|
|
|
|
|
original_fl = fcntl (STDIN_FILENO, F_GETFL);
|
|
|
|
|
fcntl (STDIN_FILENO, F_SETFL, original_fl | O_NONBLOCK);
|
|
|
|
|
}
|
2005-08-12 19:53:33 +00:00
|
|
|
|
|
2013-04-26 22:02:23 +00:00
|
|
|
|
saved_orig = 1;
|
2005-08-12 19:53:33 +00:00
|
|
|
|
|
2013-04-26 22:02:23 +00:00
|
|
|
|
tcgetattr(STDIN_FILENO, &orig_tty);
|
|
|
|
|
new_tty = orig_tty;
|
|
|
|
|
new_tty.c_lflag &= ~(ICANON | ECHO);
|
|
|
|
|
new_tty.c_cc[VMIN] = 1;
|
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &new_tty);
|
2005-08-12 19:53:33 +00:00
|
|
|
|
|
2013-04-26 22:02:23 +00:00
|
|
|
|
return grub_terminfo_input_init (term);
|
2003-11-17 18:07:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-04-26 22:02:23 +00:00
|
|
|
|
static grub_err_t
|
|
|
|
|
grub_console_fini_input (struct grub_term_input *term
|
|
|
|
|
__attribute__ ((unused)))
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
2013-04-26 22:02:23 +00:00
|
|
|
|
fcntl (STDIN_FILENO, F_SETFL, original_fl);
|
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &orig_tty);
|
|
|
|
|
saved_orig = 0;
|
|
|
|
|
return GRUB_ERR_NONE;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2004-04-04 13:46:03 +00:00
|
|
|
|
static grub_err_t
|
2013-04-26 22:02:23 +00:00
|
|
|
|
grub_console_init_output (struct grub_term_output *term)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
2013-04-26 22:02:23 +00:00
|
|
|
|
struct winsize size;
|
|
|
|
|
if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &size) >= 0)
|
2008-08-05 14:20:00 +00:00
|
|
|
|
{
|
2013-10-19 21:59:32 +00:00
|
|
|
|
grub_console_terminfo_output.size.x = size.ws_col;
|
|
|
|
|
grub_console_terminfo_output.size.y = size.ws_row;
|
2013-04-26 22:02:23 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-10-19 21:59:32 +00:00
|
|
|
|
grub_console_terminfo_output.size.x = 80;
|
|
|
|
|
grub_console_terminfo_output.size.y = 24;
|
2008-08-05 14:20:00 +00:00
|
|
|
|
}
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
2013-04-26 22:02:23 +00:00
|
|
|
|
grub_terminfo_output_init (term);
|
2003-11-17 18:07:09 +00:00
|
|
|
|
|
2004-03-14 17:48:25 +00:00
|
|
|
|
return 0;
|
2003-11-17 18:07:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-04-26 22:02:23 +00:00
|
|
|
|
|
|
|
|
|
struct grub_terminfo_input_state grub_console_terminfo_input =
|
|
|
|
|
{
|
|
|
|
|
.readkey = readkey
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct grub_terminfo_output_state grub_console_terminfo_output =
|
|
|
|
|
{
|
|
|
|
|
.put = put,
|
2013-10-19 21:59:32 +00:00
|
|
|
|
.size = { 80, 24 }
|
2013-04-26 22:02:23 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct grub_term_input grub_console_term_input =
|
2008-11-08 12:17:51 +00:00
|
|
|
|
{
|
|
|
|
|
.name = "console",
|
2013-04-26 22:02:23 +00:00
|
|
|
|
.init = grub_console_init_input,
|
|
|
|
|
.fini = grub_console_fini_input,
|
|
|
|
|
.getkey = grub_terminfo_getkey,
|
|
|
|
|
.data = &grub_console_terminfo_input
|
2008-11-08 12:17:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-04-26 22:02:23 +00:00
|
|
|
|
static struct grub_term_output grub_console_term_output =
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
|
|
|
|
.name = "console",
|
2013-04-26 22:02:23 +00:00
|
|
|
|
.init = grub_console_init_output,
|
|
|
|
|
.putchar = grub_terminfo_putchar,
|
|
|
|
|
.getxy = grub_terminfo_getxy,
|
|
|
|
|
.getwh = grub_terminfo_getwh,
|
|
|
|
|
.gotoxy = grub_terminfo_gotoxy,
|
|
|
|
|
.cls = grub_terminfo_cls,
|
|
|
|
|
.setcolorstate = grub_terminfo_setcolorstate,
|
|
|
|
|
.setcursor = grub_terminfo_setcursor,
|
|
|
|
|
.data = &grub_console_terminfo_output,
|
2013-10-22 18:42:20 +00:00
|
|
|
|
.progress_update_divisor = GRUB_PROGRESS_FAST
|
2003-11-17 18:07:09 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void
|
2004-04-04 13:46:03 +00:00
|
|
|
|
grub_console_init (void)
|
2003-11-17 18:07:09 +00:00
|
|
|
|
{
|
2013-04-26 22:02:23 +00:00
|
|
|
|
const char *cs = nl_langinfo (CODESET);
|
|
|
|
|
if (cs && grub_strcasecmp (cs, "UTF-8"))
|
|
|
|
|
grub_console_term_output.flags = GRUB_TERM_CODE_TYPE_UTF8_LOGICAL;
|
|
|
|
|
else
|
|
|
|
|
grub_console_term_output.flags = GRUB_TERM_CODE_TYPE_ASCII;
|
|
|
|
|
grub_term_register_input ("console", &grub_console_term_input);
|
|
|
|
|
grub_term_register_output ("console", &grub_console_term_output);
|
|
|
|
|
grub_terminfo_init ();
|
|
|
|
|
grub_terminfo_output_register (&grub_console_term_output, "vt100-color");
|
2003-11-17 18:07:09 +00:00
|
|
|
|
}
|
2005-02-15 00:07:01 +00:00
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
grub_console_fini (void)
|
|
|
|
|
{
|
2013-04-26 22:02:23 +00:00
|
|
|
|
if (saved_orig)
|
|
|
|
|
{
|
|
|
|
|
fcntl (STDIN_FILENO, F_SETFL, original_fl);
|
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &orig_tty);
|
|
|
|
|
}
|
|
|
|
|
saved_orig = 0;
|
2005-02-15 00:07:01 +00:00
|
|
|
|
}
|