2N/A/* sleep.c - Command to wait a specified number of seconds. */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/dl.h>
2N/A#include <grub/term.h>
2N/A#include <grub/time.h>
2N/A#include <grub/types.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/extcmd.h>
2N/A#include <grub/i18n.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Astatic const struct grub_arg_option options[] =
2N/A {
2N/A {"verbose", 'v', 0, N_("Verbose countdown."), 0, 0},
2N/A {"interruptible", 'i', 0, N_("Interruptible with ESC."), 0, 0},
2N/A {0, 0, 0, 0, 0, 0}
2N/A };
2N/A
2N/Astatic grub_uint16_t *pos;
2N/A
2N/Astatic void
2N/Ado_print (int n)
2N/A{
2N/A grub_term_restore_pos (pos);
2N/A /* NOTE: Do not remove the trailing space characters.
2N/A They are required to clear the line. */
2N/A grub_printf ("%d ", n);
2N/A}
2N/A
2N/A/* Based on grub_millisleep() from kern/generic/millisleep.c. */
2N/Astatic int
2N/Agrub_interruptible_millisleep (grub_uint32_t ms)
2N/A{
2N/A grub_uint64_t start;
2N/A
2N/A start = grub_get_time_ms ();
2N/A
2N/A while (grub_get_time_ms () - start < ms)
2N/A if (grub_checkkey () >= 0 && grub_getkey () == GRUB_TERM_ESC)
2N/A return 1;
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_cmd_sleep (grub_extcmd_context_t ctxt, int argc, char **args)
2N/A{
2N/A struct grub_arg_list *state = ctxt->state;
2N/A int n;
2N/A
2N/A if (argc != 1)
2N/A return grub_error (GRUB_ERR_BAD_ARGUMENT, "missing operand");
2N/A
2N/A n = grub_strtoul (args[0], 0, 10);
2N/A
2N/A if (n == 0)
2N/A {
2N/A /* Either `0' or broken input. */
2N/A return 0;
2N/A }
2N/A
2N/A pos = grub_term_save_pos ();
2N/A
2N/A for (; n; n--)
2N/A {
2N/A if (state[0].set)
2N/A do_print (n);
2N/A
2N/A if (state[1].set)
2N/A {
2N/A if (grub_interruptible_millisleep (1000))
2N/A return 1;
2N/A }
2N/A else
2N/A grub_millisleep (1000);
2N/A }
2N/A if (state[0].set)
2N/A do_print (0);
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_extcmd_t cmd;
2N/A
2N/AGRUB_MOD_INIT(sleep)
2N/A{
2N/A cmd = grub_register_extcmd ("sleep", grub_cmd_sleep, 0,
2N/A N_("NUMBER_OF_SECONDS"),
2N/A N_("Wait for a specified number of seconds."),
2N/A options);
2N/A}
2N/A
2N/AGRUB_MOD_FINI(sleep)
2N/A{
2N/A grub_unregister_extcmd (cmd);
2N/A}