#include <ipxe/settings.h>
#include <ipxe/command.h>
#include <ipxe/parseopt.h>
+#include <readline/readline.h>
FILE_LICENCE ( GPL2_OR_LATER );
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" command
+ * "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_exec ( int argc, char **argv ) {
- struct set_options opts;
+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, &set_cmd, &opts ) ) != 0 )
+ if ( ( rc = parse_options ( argc, argv, 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;
- }
+ 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 set \"%s\"=\"%s\": %s\n",
- name, value, strerror ( rc ) );
+ printf ( "Could not %s \"%s\": %s\n",
+ argv[0], name, strerror ( rc ) );
goto err_store;
}
err_store:
free ( value );
- err_concat_args:
+ err_get_value:
err_parse_options:
return rc;
}
-/** "clear" options */
-struct clear_options {};
+/**
+ * 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
+ *
+ * @v argc Argument count
+ * @v argv Argument list
+ * @ret rc Return status code
+ */
+static int set_exec ( int argc, char **argv ) {
+ return set_core_exec ( argc, argv, &set_cmd, set_value );
+}
-/** "clear" option list */
-static struct option_descriptor clear_opts[] = {};
+/**
+ * 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" command descriptor */
-static struct command_descriptor clear_cmd =
- COMMAND_DESC ( struct clear_options, clear_opts, 1, 1, "<setting>" );
+ *value = NULL;
+ return 0;
+}
/**
* "clear" command
* @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 = {
{
.name = "clear",
.exec = clear_exec,
},
+ {
+ .name = "read",
+ .exec = read_exec,
+ },
};