summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZachary T Welch <zw@superlucidity.net>2009-10-22 07:44:54 -0700
committerZachary T Welch <zw@superlucidity.net>2009-11-05 17:27:25 -0800
commit36a3646c2205474345482188c8c05e50d1f67e44 (patch)
tree28e687c4ef383f98be76467ed6e5a86dbaa00e74 /src
parent68785af4da7727238c86dcf8858e0b5975bc988e (diff)
downloadopenocd_libswd-36a3646c2205474345482188c8c05e50d1f67e44.tar.gz
openocd_libswd-36a3646c2205474345482188c8c05e50d1f67e44.tar.bz2
openocd_libswd-36a3646c2205474345482188c8c05e50d1f67e44.tar.xz
openocd_libswd-36a3646c2205474345482188c8c05e50d1f67e44.zip
Add macro for parsing numeric command arguments.
This helper eliminates significant amount of redundant code in command handler functions throughout the system. It wraps the lower-level parse_* macros to implement a policy for reporting parse errors to the active command context (cmd_ctx). If errors do occur, this macro causes the calling function to abort with the proper return code.
Diffstat (limited to 'src')
-rw-r--r--src/helper/command.h21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/helper/command.h b/src/helper/command.h
index ba825bcb..2d0142fd 100644
--- a/src/helper/command.h
+++ b/src/helper/command.h
@@ -138,6 +138,27 @@ DECLARE_PARSE_WRAPPER(_s32, int32_t);
DECLARE_PARSE_WRAPPER(_s16, int16_t);
DECLARE_PARSE_WRAPPER(_s8, int8_t);
+/**
+ * @brief parses the string @a in into @a out as a @a type, or prints
+ * a command error and passes the error code to the caller. If an error
+ * does occur, the calling function will return the error code produced
+ * by the parsing function (one of ERROR_COMMAND_ARGUMENT_*).
+ *
+ * This function may cause the calling function to return immediately,
+ * so it should be used carefully to avoid leaking resources. In most
+ * situations, parsing should be completed in full before proceding
+ * to allocate resources, and this strategy will most prevents leaks.
+ */
+#define COMMAND_PARSE_NUMBER(type, in, out) \
+ do { \
+ int retval = parse_##type(in, &(out)); \
+ if (ERROR_OK != retval) { \
+ command_print(cmd_ctx, stringify(out) \
+ " option value ('%s') is not valid", in); \
+ return retval; \
+ } \
+ } while (0)
+
void script_debug(Jim_Interp *interp, const char *cmd, int argc, Jim_Obj *const *argv);
#endif /* COMMAND_H */