/* read.c - Command to read variables from user. */ /* * 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 . */ #include #include #include #include #include #include #include #include #include VAS_EBOOT_MOD_LICENSE ("GPLv3+"); static const struct VasEBoot_arg_option options[] = { {"silent", 's', 0, N_("Do not echo input"), 0, 0}, {0, 0, 0, 0, 0, 0} }; static char * VasEBoot_getline (int silent) { VasEBoot_size_t i; char *line; char *tmp; int c; VasEBoot_size_t alloc_size; i = 0; line = VasEBoot_malloc (1 + sizeof('\0')); if (! line) return NULL; while (1) { c = VasEBoot_getkey (); if ((c == '\n') || (c == '\r')) break; if (!VasEBoot_isprint (c)) continue; line[i] = (char) c; if (!silent) VasEBoot_printf ("%c", c); if (VasEBoot_add (i, 1, &i)) { VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, N_("overflow is detected")); return NULL; } if (VasEBoot_add (i, 1 + sizeof('\0'), &alloc_size)) { VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, N_("overflow is detected")); return NULL; } tmp = VasEBoot_realloc (line, alloc_size); if (! tmp) { VasEBoot_free (line); return NULL; } line = tmp; } line[i] = '\0'; return line; } static VasEBoot_err_t VasEBoot_cmd_read (VasEBoot_extcmd_context_t ctxt, int argc, char **args) { struct VasEBoot_arg_list *state = ctxt->state; char *line = VasEBoot_getline (state[0].set); if (! line) return VasEBoot_errno; if (argc > 0) VasEBoot_env_set (args[0], line); VasEBoot_free (line); return 0; } static VasEBoot_extcmd_t cmd; VAS_EBOOT_MOD_INIT(read) { cmd = VasEBoot_register_extcmd ("read", VasEBoot_cmd_read, 0, N_("[-s] [ENVVAR]"), N_("Set variable with user input."), options); } VAS_EBOOT_MOD_FINI(read) { VasEBoot_unregister_extcmd (cmd); }