[ipxe-devel] [PATCH] added function chop and upper

JUNG, Christian christian.jung at shsservices.org
Wed Feb 24 09:29:11 UTC 2016


From: Christian Jung <christian.jung at shsservices.org>
Date: Wed, 24 Feb 2016 10:16:26 +0100
Signed-off-by: Christian Jung <christian.jung at shsservices.org>

This patch adds the functions chop and upper.

chop removes the last character of a string setting
    iPXE> set myvar Hello World
    iPXE> chop myvar
    iPXE> echo ${myvar}
    Hello Worl

upper converts a string setting to upper case
    iPXE> set myvar hello world
    iPXE> upper myvar
    iPXE> echo ${myvar}
    HELLO WORLD

---
 src/hci/commands/nvo_cmd.c | 119 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 119 insertions(+)

diff --git a/src/hci/commands/nvo_cmd.c b/src/hci/commands/nvo_cmd.c
index ac0d606..adbf2d7 100644
--- a/src/hci/commands/nvo_cmd.c
+++ b/src/hci/commands/nvo_cmd.c
@@ -32,6 +32,7 @@
 #include <ipxe/command.h>
 #include <ipxe/parseopt.h>
 #include <readline/readline.h>
+#include <ctype.h>
 
 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 
@@ -326,6 +327,116 @@ static int inc_exec ( int argc, char **argv ) {
 	return rc;
 }
 
+/** "chop" options */
+struct chop_options {};
+
+/** "chop" option list */
+static struct option_descriptor chop_opts[] = {};
+
+/** "chop" command descriptor */
+static struct command_descriptor chop_cmd =
+	COMMAND_DESC ( struct chop_options, chop_opts, 1, 1,
+		       "<setting>" );
+
+/**
+ * "chop" command (chop the last character of a setting)
+ *
+ * @v argc		Argument count
+ * @v argv		Argument list
+ * @ret rc		Return status code
+ */
+static int chop_exec ( int argc, char **argv ) {
+	struct chop_options opts;
+	struct named_setting setting;
+	char *value;
+	int rc;
+
+	/* Parse options */
+	if ( ( rc = parse_options ( argc, argv, &chop_cmd, &opts ) ) != 0 )
+		goto err_parse_options;
+
+	/* Parse setting name */
+	if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
+		goto err_parse_setting;
+
+	/* Read existing value */
+	if ( ( rc = fetch_string_setting_copy( setting.settings,  &setting.setting,
+					&value ) ) < 0 ) {
+		goto err_parse_setting;
+	}
+	if ( rc > 0 ) {
+		if ( ( rc = store_setting ( setting.settings, &setting.setting,
+					     value, rc - 1 ) ) != 0 ) {
+			printf ( "Could not store \"%s\": %s\n",
+				 setting.setting.name, strerror ( rc ) );
+			goto err_store;
+		}
+	}
+
+ err_store:
+	free( value );
+ err_parse_setting:
+ err_parse_options:
+	return rc;
+}
+
+/** "upper" options */
+struct upper_options {};
+
+/** "upper" option list */
+static struct option_descriptor upper_opts[] = {};
+
+/** "upper" command descriptor */
+static struct command_descriptor upper_cmd =
+	COMMAND_DESC ( struct upper_options, upper_opts, 1, 1,
+		       "<setting>" );
+
+/**
+ * "upper" command (convert setting to upper case)
+ *
+ * @v argc		Argument count
+ * @v argv		Argument list
+ * @ret rc		Return status code
+ */
+static int upper_exec ( int argc, char **argv ) {
+	struct upper_options opts;
+	struct named_setting setting;
+	char *value;
+	char *p;
+	int rc;
+
+	/* Parse options */
+	if ( ( rc = parse_options ( argc, argv, &upper_cmd, &opts ) ) != 0 )
+		goto err_parse_options;
+
+	/* Parse setting name */
+	if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
+		goto err_parse_setting;
+
+	/* Read existing value */
+	if ( ( rc = fetch_string_setting_copy( setting.settings,  &setting.setting,
+					&value ) ) <= 0 ) {
+		goto err_parse_setting;
+	}
+
+	for ( p = value; *p != '\0'; p++ ) {
+		*p = toupper(*p);
+	}
+
+	if ( ( rc = store_setting ( setting.settings, &setting.setting,
+					 value, rc ) ) != 0 ) {
+		printf ( "Could not store \"%s\": %s\n",
+			 setting.setting.name, strerror ( rc ) );
+		goto err_store;
+	}
+
+ err_store:
+	free( value );
+ err_parse_setting:
+ err_parse_options:
+	return rc;
+}
+
 /** Non-volatile option commands */
 struct command nvo_commands[] __command = {
 	{
@@ -348,4 +459,12 @@ struct command nvo_commands[] __command = {
 		.name = "inc",
 		.exec = inc_exec,
 	},
+	{
+		.name = "chop",
+		.exec = chop_exec,
+	},
+	{
+		.name = "upper",
+		.exec = upper_exec,
+	},
 };
-- 
2.1.4



More information about the ipxe-devel mailing list