vaseboot/VasEBoot-core/commands/sleep.c

118 lines
2.8 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* sleep.c - Command to wait a specified number of seconds. */
/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
*
* VAS_EBOOT is free software: you can redistribute it and/or modify
* 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.
*
* VAS_EBOOT 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 VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/dl.h>
#include <VasEBoot/term.h>
#include <VasEBoot/time.h>
#include <VasEBoot/types.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/extcmd.h>
#include <VasEBoot/i18n.h>
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
static const struct VasEBoot_arg_option options[] =
{
{"verbose", 'v', 0, N_("Verbose countdown."), 0, 0},
{"interruptible", 'i', 0, N_("Allow to interrupt with ESC."), 0, 0},
{0, 0, 0, 0, 0, 0}
};
static struct VasEBoot_term_coordinate *pos;
static void
do_print (int n)
{
VasEBoot_term_restore_pos (pos);
/* NOTE: Do not remove the trailing space characters.
They are required to clear the line. */
VasEBoot_printf ("%d ", n);
VasEBoot_refresh ();
}
/* Based on VasEBoot_millisleep() from kern/generic/millisleep.c. */
static int
VasEBoot_interruptible_millisleep (VasEBoot_uint32_t ms)
{
VasEBoot_uint64_t start;
start = VasEBoot_get_time_ms ();
while (VasEBoot_get_time_ms () - start < ms)
if (VasEBoot_key_is_interrupt (VasEBoot_getkey_noblock ()))
return 1;
return 0;
}
static VasEBoot_err_t
VasEBoot_cmd_sleep (VasEBoot_extcmd_context_t ctxt, int argc, char **args)
{
struct VasEBoot_arg_list *state = ctxt->state;
int n;
if (argc != 1)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("one argument expected"));
n = VasEBoot_strtoul (args[0], 0, 10);
if (n == 0)
{
/* Either `0' or broken input. */
return 0;
}
VasEBoot_refresh ();
pos = VasEBoot_term_save_pos ();
for (; n; n--)
{
if (state[0].set)
do_print (n);
if (state[1].set)
{
if (VasEBoot_interruptible_millisleep (1000))
return 1;
}
else
VasEBoot_millisleep (1000);
}
if (state[0].set)
do_print (0);
return 0;
}
static VasEBoot_extcmd_t cmd;
VAS_EBOOT_MOD_INIT(sleep)
{
cmd = VasEBoot_register_extcmd ("sleep", VasEBoot_cmd_sleep, 0,
N_("NUMBER_OF_SECONDS"),
N_("Wait for a specified number of seconds."),
options);
}
VAS_EBOOT_MOD_FINI(sleep)
{
VasEBoot_unregister_extcmd (cmd);
}