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 #include #include +#include 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, + "" ); + +/** + * "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, + "" ); + +/** + * "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, + }, };