vaseboot/VasEBoot-core/commands/read.c

93 lines
2.1 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.

/* read.c - Command to read variables from user. */
/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
*
* VasEBoot 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.
*
* VasEBoot 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 VasEBoot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/dl.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/env.h>
#include <VasEBoot/term.h>
#include <VasEBoot/types.h>
#include <VasEBoot/command.h>
#include <VasEBoot/i18n.h>
VasEBoot_MOD_LICENSE ("GPLv3+");
static char *
VasEBoot_getline (void)
{
int i;
char *line;
char *tmp;
char c;
i = 0;
line = VasEBoot_malloc (1 + i + sizeof('\0'));
if (! line)
return NULL;
while (1)
{
c = VasEBoot_getkey ();
if ((c == '\n') || (c == '\r'))
break;
line[i] = c;
if (VasEBoot_isprint (c))
VasEBoot_printf ("%c", c);
i++;
tmp = VasEBoot_realloc (line, 1 + i + sizeof('\0'));
if (! tmp)
{
VasEBoot_free (line);
return NULL;
}
line = tmp;
}
line[i] = '\0';
return line;
}
static VasEBoot_err_t
VasEBoot_cmd_read (VasEBoot_command_t cmd __attribute__ ((unused)), int argc, char **args)
{
char *line = VasEBoot_getline ();
if (! line)
return VasEBoot_errno;
if (argc > 0)
VasEBoot_env_set (args[0], line);
VasEBoot_free (line);
return 0;
}
static VasEBoot_command_t cmd;
VasEBoot_MOD_INIT(read)
{
cmd = VasEBoot_register_command ("read", VasEBoot_cmd_read,
N_("[ENVVAR]"),
N_("Set variable with user input."));
}
VasEBoot_MOD_FINI(read)
{
VasEBoot_unregister_command (cmd);
}