mirror of https://github.com/ipxe/ipxe.git
[settings] Add "read" command
The "read" command allows a script to prompt a user to enter a setting. For example: echo -n Static IP address: read net0/ip Total cost: 17 bytes. Signed-off-by: Michael Brown <mcb30@ipxe.org>pull/1/head
parent
ac12324f52
commit
1cc991e132
|
@ -25,6 +25,7 @@
|
|||
#include <ipxe/settings.h>
|
||||
#include <ipxe/command.h>
|
||||
#include <ipxe/parseopt.h>
|
||||
#include <readline/readline.h>
|
||||
|
||||
FILE_LICENCE ( GPL2_OR_LATER );
|
||||
|
||||
|
@ -80,17 +81,83 @@ static int show_exec ( int argc, char **argv ) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
/** "set" options */
|
||||
struct set_options {};
|
||||
/** "set", "clear", and "read" options */
|
||||
struct set_core_options {};
|
||||
|
||||
/** "set" option list */
|
||||
static struct option_descriptor set_opts[] = {};
|
||||
/** "set", "clear", and "read" option list */
|
||||
static struct option_descriptor set_core_opts[] = {};
|
||||
|
||||
/** "set" command descriptor */
|
||||
static struct command_descriptor set_cmd =
|
||||
COMMAND_DESC ( struct set_options, set_opts, 1, MAX_ARGUMENTS,
|
||||
COMMAND_DESC ( struct set_core_options, set_core_opts, 1, MAX_ARGUMENTS,
|
||||
"<setting> <value>" );
|
||||
|
||||
/** "clear" and "read" command descriptor */
|
||||
static struct command_descriptor clear_read_cmd =
|
||||
COMMAND_DESC ( struct set_core_options, set_core_opts, 1, 1,
|
||||
"<setting>" );
|
||||
|
||||
/**
|
||||
* "set", "clear", and "read" command
|
||||
*
|
||||
* @v argc Argument count
|
||||
* @v argv Argument list
|
||||
* @v cmd Command descriptor
|
||||
* @v get_value Method to obtain setting value
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int set_core_exec ( int argc, char **argv,
|
||||
struct command_descriptor *cmd,
|
||||
int ( * get_value ) ( char **args, char **value ) ) {
|
||||
struct set_core_options opts;
|
||||
const char *name;
|
||||
char *value;
|
||||
int rc;
|
||||
|
||||
/* Parse options */
|
||||
if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
|
||||
goto err_parse_options;
|
||||
|
||||
/* Parse setting name */
|
||||
name = argv[optind];
|
||||
|
||||
/* Parse setting value */
|
||||
if ( ( rc = get_value ( &argv[ optind + 1 ], &value ) ) != 0 )
|
||||
goto err_get_value;
|
||||
|
||||
/* Determine total length of command line */
|
||||
if ( ( rc = storef_named_setting ( name, value ) ) != 0 ) {
|
||||
printf ( "Could not %s \"%s\": %s\n",
|
||||
argv[0], name, strerror ( rc ) );
|
||||
goto err_store;
|
||||
}
|
||||
|
||||
free ( value );
|
||||
return 0;
|
||||
|
||||
err_store:
|
||||
free ( value );
|
||||
err_get_value:
|
||||
err_parse_options:
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get setting value for "set" command
|
||||
*
|
||||
* @v args Remaining arguments
|
||||
* @ret value Setting value
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int set_value ( char **args, char **value ) {
|
||||
|
||||
*value = concat_args ( args );
|
||||
if ( ! *value )
|
||||
return -ENOMEM;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* "set" command
|
||||
*
|
||||
|
@ -99,51 +166,21 @@ static struct command_descriptor set_cmd =
|
|||
* @ret rc Return status code
|
||||
*/
|
||||
static int set_exec ( int argc, char **argv ) {
|
||||
struct set_options opts;
|
||||
const char *name;
|
||||
char *value;
|
||||
int rc;
|
||||
|
||||
/* Parse options */
|
||||
if ( ( rc = parse_options ( argc, argv, &set_cmd, &opts ) ) != 0 )
|
||||
goto err_parse_options;
|
||||
|
||||
/* Parse setting name */
|
||||
name = argv[optind];
|
||||
|
||||
/* Parse setting value */
|
||||
value = concat_args ( &argv[ optind + 1 ] );
|
||||
if ( ! value ) {
|
||||
rc = -ENOMEM;
|
||||
goto err_concat_args;
|
||||
}
|
||||
|
||||
/* Determine total length of command line */
|
||||
if ( ( rc = storef_named_setting ( name, value ) ) != 0 ) {
|
||||
printf ( "Could not set \"%s\"=\"%s\": %s\n",
|
||||
name, value, strerror ( rc ) );
|
||||
goto err_store;
|
||||
}
|
||||
|
||||
free ( value );
|
||||
return 0;
|
||||
|
||||
err_store:
|
||||
free ( value );
|
||||
err_concat_args:
|
||||
err_parse_options:
|
||||
return rc;
|
||||
return set_core_exec ( argc, argv, &set_cmd, set_value );
|
||||
}
|
||||
|
||||
/** "clear" options */
|
||||
struct clear_options {};
|
||||
/**
|
||||
* Get setting value for "clear" command
|
||||
*
|
||||
* @v args Remaining arguments
|
||||
* @ret value Setting value
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int clear_value ( char **args __unused, char **value ) {
|
||||
|
||||
/** "clear" option list */
|
||||
static struct option_descriptor clear_opts[] = {};
|
||||
|
||||
/** "clear" command descriptor */
|
||||
static struct command_descriptor clear_cmd =
|
||||
COMMAND_DESC ( struct clear_options, clear_opts, 1, 1, "<setting>" );
|
||||
*value = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* "clear" command
|
||||
|
@ -153,27 +190,35 @@ static struct command_descriptor clear_cmd =
|
|||
* @ret rc Return status code
|
||||
*/
|
||||
static int clear_exec ( int argc, char **argv ) {
|
||||
struct clear_options opts;
|
||||
const char *name;
|
||||
int rc;
|
||||
return set_core_exec ( argc, argv, &clear_read_cmd, clear_value );
|
||||
}
|
||||
|
||||
/* Parse options */
|
||||
if ( ( rc = parse_options ( argc, argv, &clear_cmd, &opts ) ) != 0 )
|
||||
return rc;
|
||||
/**
|
||||
* Get setting value for "read" command
|
||||
*
|
||||
* @ret value Setting value
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int read_value ( char **args __unused, char **value ) {
|
||||
|
||||
/* Parse setting name */
|
||||
name = argv[optind];
|
||||
*value = readline ( NULL );
|
||||
if ( ! *value )
|
||||
return -ENOMEM;
|
||||
|
||||
/* Clear setting */
|
||||
if ( ( rc = delete_named_setting ( name ) ) != 0 ) {
|
||||
printf ( "Could not clear \"%s\": %s\n",
|
||||
name, strerror ( rc ) );
|
||||
return rc;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* "read" command
|
||||
*
|
||||
* @v argc Argument count
|
||||
* @v argv Argument list
|
||||
* @ret rc Return status code
|
||||
*/
|
||||
static int read_exec ( int argc, char **argv ) {
|
||||
return set_core_exec ( argc, argv, &clear_read_cmd, read_value );
|
||||
}
|
||||
|
||||
/** Non-volatile option commands */
|
||||
struct command nvo_commands[] __command = {
|
||||
{
|
||||
|
@ -188,4 +233,8 @@ struct command nvo_commands[] __command = {
|
|||
.name = "clear",
|
||||
.exec = clear_exec,
|
||||
},
|
||||
{
|
||||
.name = "read",
|
||||
.exec = read_exec,
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue