summaryrefslogtreecommitdiff
path: root/src/helper/command.c
diff options
context:
space:
mode:
authorZachary T Welch <zw@superlucidity.net>2009-11-18 06:58:27 -0800
committerZachary T Welch <zw@superlucidity.net>2009-11-18 15:51:07 -0800
commit7e4adfe1c53aa18d4feba1e58ceb5c5aaa470775 (patch)
treeb6e359af900dc57c08f10fa8c7ab535a40e35379 /src/helper/command.c
parent410fab9ea8c6632da2e4967d960f66eecc7821ec (diff)
downloadopenocd+libswd-7e4adfe1c53aa18d4feba1e58ceb5c5aaa470775.tar.gz
openocd+libswd-7e4adfe1c53aa18d4feba1e58ceb5c5aaa470775.tar.bz2
openocd+libswd-7e4adfe1c53aa18d4feba1e58ceb5c5aaa470775.tar.xz
openocd+libswd-7e4adfe1c53aa18d4feba1e58ceb5c5aaa470775.zip
add handle_command_parse_bool command helper
Rewrite arm11_handle_bool to provide a generic on/off command helper. Refactors COMMAND_PARSE_BOOL to use new command_parse_bool helper, which gets reused by the new command_parse_bool_any helper. This later helper is called by the new command helper function to accepts any on/off, enable/disable, true/false, yes/no, or 0/1 parameter.
Diffstat (limited to 'src/helper/command.c')
-rw-r--r--src/helper/command.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/helper/command.c b/src/helper/command.c
index 708a8024..5f3fae51 100644
--- a/src/helper/command.c
+++ b/src/helper/command.c
@@ -954,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)
+
+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_any(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_any(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;
+}
+
+