X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Fhelper%2Fcommand.c;h=b7c44efc91033058c43adab890945504eac0d0e5;hp=b9d0d087ad4f873ac61a6ce37e08e38f48de05d8;hb=4d8d1d32d0f0e0b8866a06cb1d3f304563fa6796;hpb=cffc98ad8047b6dc8d38a6422136638f2df992d2;ds=sidebyside diff --git a/src/helper/command.c b/src/helper/command.c index b9d0d087ad..b7c44efc91 100644 --- a/src/helper/command.c +++ b/src/helper/command.c @@ -438,7 +438,6 @@ char *command_name(struct command *c, char delim) static int run_command(struct command_context *context, struct command *c, const char *words[], unsigned num_words) { - int start_word = 0; if (!((context->mode == COMMAND_CONFIG) || (c->mode == COMMAND_ANY) || (c->mode == context->mode))) { /* Config commands can not run after the config stage */ @@ -449,8 +448,8 @@ static int run_command(struct command_context *context, struct command_invocation cmd = { .ctx = context, .name = c->name, - .argc = num_words - start_word - 1, - .argv = words + start_word + 1, + .argc = num_words - 1, + .argv = words + 1, }; int retval = c->handler(&cmd); if (retval == ERROR_COMMAND_SYNTAX_ERROR) @@ -767,10 +766,9 @@ COMMAND_HANDLER(handle_fast_command) } -struct command_context* command_init() +struct command_context* command_init(const char *startup_tcl) { struct command_context* context = malloc(sizeof(struct command_context)); - extern const char startup_tcl[]; const char *HostOs; context->mode = COMMAND_EXEC; @@ -956,3 +954,53 @@ DEFINE_PARSE_LONG(_int, int, n < INT_MIN, INT_MAX) DEFINE_PARSE_LONG(_s32, int32_t, n < INT32_MIN, INT32_MAX) DEFINE_PARSE_LONG(_s16, int16_t, n < INT16_MIN, INT16_MAX) DEFINE_PARSE_LONG(_s8, int8_t, n < INT8_MIN, INT8_MAX) + +static int command_parse_bool(const char *in, bool *out, + const char *on, const char *off) +{ + if (strcasecmp(in, on) == 0) + *out = true; + else if (strcasecmp(in, off) == 0) + *out = false; + else + return ERROR_COMMAND_SYNTAX_ERROR; + return ERROR_OK; +} + +int command_parse_bool_arg(const char *in, bool *out) +{ + if (command_parse_bool(in, out, "on", "off") == ERROR_OK) + return ERROR_OK; + if (command_parse_bool(in, out, "enable", "disable") == ERROR_OK) + return ERROR_OK; + if (command_parse_bool(in, out, "true", "false") == ERROR_OK) + return ERROR_OK; + if (command_parse_bool(in, out, "yes", "no") == ERROR_OK) + return ERROR_OK; + if (command_parse_bool(in, out, "1", "0") == ERROR_OK) + return ERROR_OK; + return ERROR_INVALID_ARGUMENTS; +} + +COMMAND_HELPER(handle_command_parse_bool, bool *out, const char *label) +{ + switch (CMD_ARGC) { + case 1: { + const char *in = CMD_ARGV[0]; + if (command_parse_bool_arg(in, out) != ERROR_OK) + { + LOG_ERROR("%s: argument '%s' is not valid", CMD_NAME, in); + return ERROR_INVALID_ARGUMENTS; + } + // fall through + } + case 0: + LOG_INFO("%s is %s", label, *out ? "enabled" : "disabled"); + break; + default: + return ERROR_INVALID_ARGUMENTS; + } + return ERROR_OK; +} + +