]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[settings] Add "read" command
authorMichael Brown <mcb30@ipxe.org>
Sun, 27 Mar 2011 12:23:18 +0000 (13:23 +0100)
committerMichael Brown <mcb30@ipxe.org>
Sun, 27 Mar 2011 12:24:21 +0000 (13:24 +0100)
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>
src/hci/commands/nvo_cmd.c

index 88d412700c51e508177e26b98347d29415a0c5a0..a7e0f4a039474852501fb8d56bf0671479312bc1 100644 (file)
@@ -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,48 +81,54 @@ 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" 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;
        }
 
@@ -130,20 +137,50 @@ static int set_exec ( int argc, char **argv ) {
 
  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
@@ -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,
+       },
 };