/* menu.c - General supporting functionality for menus. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2003,2004,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/menu_viewer.h>
#include <grub/script_sh.h>
entry failing to boot. */
/* Wait until the user pushes any key so that the user
can see what happened. */
void
grub_wait_after_message (void)
{
grub_xputs ("\n");
grub_refresh ();
while (grub_get_time_ms () < endtime)
if (grub_checkkey () >= 0)
{
grub_getkey ();
break;
}
grub_xputs ("\n");
}
/* Get a menu entry by its index in the entry list. */
{
;
return e;
}
/* Return the current timeout. If the variable "timeout" is not set or
invalid, return -1. */
int
grub_menu_get_timeout (void)
{
const char *val;
int timeout;
if (! val)
return -1;
grub_error_push ();
/* If the value is invalid, unset the variable. */
if (grub_errno != GRUB_ERR_NONE)
{
grub_env_unset ("timeout");
timeout = -1;
}
grub_error_pop ();
return timeout;
}
/* Set current timeout in the variable "timeout". */
void
{
/* Ignore TIMEOUT if it is zero, because it will be unset really soon. */
if (timeout > 0)
{
}
}
/* Get the first entry number from the value of the environment variable NAME,
which is a space-separated list of non-negative integers. The entry number
which is returned is stripped from the value of NAME. If no entry number
can be found, -1 is returned. */
static int
{
const char *val;
char *tail;
int entry;
if (! val)
return -1;
grub_error_push ();
if (grub_errno == GRUB_ERR_NONE)
{
/* Skip whitespace to find the next digit. */
tail++;
}
else
{
entry = -1;
}
grub_error_pop ();
return entry;
}
/* Run a menu entry. */
static void
{
int errs_before;
if (entry->restricted)
if (err)
{
grub_print_error ();
return;
}
{
if (! menu)
return;
if (auto_boot)
}
if (chosen)
{
if (!oldchosen)
grub_print_error ();
}
if (def)
{
if (!olddefault)
grub_print_error ();
}
sz++;
if (chosen)
sz++;
if (!buf)
grub_print_error ();
else
{
if (chosen)
{
*optr++ = '>';
}
{
if (*ptr == '>')
*optr++ = '>';
}
*optr = 0;
grub_env_export ("chosen");
}
{
{
ptr++;
continue;
}
if (ptr[0] == '>')
break;
}
else
grub_env_unset ("default");
if (errs_before != grub_err_printed_errors)
/* Implicit execution of boot, only if something is loaded. */
grub_command_execute ("boot", 0, 0);
{
{
}
}
if (oldchosen)
else
grub_env_unset ("chosen");
if (olddefault)
else
grub_env_unset ("default");
grub_env_unset ("timeout");
}
/* Execute ENTRY from the menu MENU, falling back to entries specified
in the environment variable "fallback" if it fails. CALLBACK is a
pointer to a struct of function pointers which are used to allow the
caller provide feedback to the user. */
static void
int autobooted,
void *callback_data)
{
int fallback_entry;
/* Deal with fallback entries. */
>= 0)
{
grub_print_error ();
/* If the function call to execute the entry returns at all, then this is
taken to indicate a boot failure. For menu entries that do something
other than actually boot an operating system, this could assume
incorrectly that something failed. */
}
if (!autobooted)
}
static void
{
}
static void
{
}
static void
menu_fini (void)
{
{
}
}
static void
{
int gfxmenu = 0;
{
if (grub_env_get ("theme"))
{
if (!grub_gfxmenu_try_hook)
{
grub_dl_load ("gfxmenu");
grub_print_error ();
}
{
if(!err)
{
gfxmenu = 1;
break;
}
}
else
grub_print_error ();
}
break;
}
{
continue;
if(!err)
continue;
grub_print_error ();
}
}
static void
clear_timeout (void)
{
}
void
{
}
static int
{
while (1)
{
return 1;
return 0;
if (*ptr2 == '>')
ptr2++;
return 0;
if (*ptr1 == 0)
return 1;
ptr1++;
ptr2++;
}
}
/* Get the entry number from the variable NAME. */
static int
{
const char *val;
int entry;
if (! val)
return -1;
grub_error_push ();
if (grub_errno == GRUB_ERR_BAD_NUMBER)
{
/* See if the variable matches the title of a menu entry. */
int i;
for (i = 0; e; i++)
{
{
entry = i;
break;
}
e = e->next;
}
if (! e)
entry = -1;
}
if (grub_errno != GRUB_ERR_NONE)
{
entry = -1;
}
grub_error_pop ();
return entry;
}
/* Show the menu and handle menu entry selection. Returns the menu entry
index that should be executed or -1 if no entry should be executed (e.g.,
Esc pressed to exit a sub-menu or switching menu viewers).
If the return value is not -1, then *AUTO_BOOT is nonzero iff the menu
entry to be executed is a result of an automatic default selection because
of the timeout. */
static int
{
int timeout;
/* If DEFAULT_ENTRY is not within the menu entries, fall back to
the first entry. */
default_entry = 0;
/* If timeout is 0, drawing is pointless (and ugly). */
if (grub_menu_get_timeout () == 0)
{
*auto_boot = 1;
return default_entry;
}
/* Initialize the time. */
saved_time = grub_get_time_ms ();
timeout = grub_menu_get_timeout ();
if (timeout > 0)
else
clear_timeout ();
while (1)
{
int c;
timeout = grub_menu_get_timeout ();
return -1;
if (timeout > 0)
{
current_time = grub_get_time_ms ();
{
timeout--;
}
}
if (timeout == 0)
{
grub_env_unset ("timeout");
*auto_boot = 1;
menu_fini ();
return default_entry;
}
if (grub_checkkey () >= 0 || timeout < 0)
{
c = grub_getkey ();
if (timeout >= 0)
{
grub_env_unset ("timeout");
grub_env_unset ("fallback");
clear_timeout ();
}
switch (c)
{
case GRUB_TERM_KEY_HOME:
case GRUB_TERM_CTRL | 'a':
current_entry = 0;
break;
case GRUB_TERM_KEY_END:
case GRUB_TERM_CTRL | 'e':
break;
case GRUB_TERM_KEY_UP:
case GRUB_TERM_CTRL | 'p':
case '^':
if (current_entry > 0)
break;
case GRUB_TERM_CTRL | 'n':
case GRUB_TERM_KEY_DOWN:
case 'v':
break;
case GRUB_TERM_CTRL | 'g':
case GRUB_TERM_KEY_PPAGE:
if (current_entry < GRUB_MENU_PAGE_SIZE)
current_entry = 0;
else
break;
case GRUB_TERM_CTRL | 'c':
case GRUB_TERM_KEY_NPAGE:
else
break;
case '\n':
case '\r':
case GRUB_TERM_KEY_RIGHT:
case GRUB_TERM_CTRL | 'f':
menu_fini ();
*auto_boot = 0;
return current_entry;
case '\e':
if (nested)
{
menu_fini ();
return -1;
}
break;
case 'c':
menu_fini ();
grub_cmdline_run (1);
goto refresh;
case 'e':
menu_fini ();
{
if (e)
grub_menu_entry_run (e);
}
goto refresh;
default:
{
int i;
{
menu_fini ();
*auto_boot = 0;
return i;
}
}
break;
}
}
}
/* Never reach here. */
}
/* Callback invoked immediately before a menu entry is executed. */
static void
{
grub_printf (" ");
grub_printf ("\n\n");
}
/* Callback invoked when a default menu entry executed because of a timeout
has failed and an attempt will be made to execute the next fallback
entry, ENTRY. */
static void
{
grub_printf ("\n ");
grub_printf ("\n\n");
}
/* Callback invoked when a menu entry has failed and there is no remaining
fallback entry to attempt. */
static void
{
if (grub_errno != GRUB_ERR_NONE)
{
grub_print_error ();
}
grub_printf ("\n ");
}
/* Callbacks used by the text menu to provide user feedback when menu entries
are executed. */
{
};
static grub_err_t
{
while (1)
{
int boot_entry;
int auto_boot;
if (boot_entry < 0)
break;
if (! e)
continue; /* Menu is empty. */
grub_cls ();
if (auto_boot)
&execution_callback, 0);
else
grub_menu_execute_entry (e, 0);
if (autobooted)
break;
}
return GRUB_ERR_NONE;
}
{
while (1)
{
autoboot = 0;
grub_print_error ();
break;
if (err2)
{
grub_print_error ();
continue;
}
break;
}
return err1;
}