summaryrefslogtreecommitdiff
path: root/src/helper/command.c
diff options
context:
space:
mode:
authorzwelch <zwelch@b42882b7-edfa-0310-969c-e2dbd0fdcd60>2009-06-17 00:30:36 +0000
committerzwelch <zwelch@b42882b7-edfa-0310-969c-e2dbd0fdcd60>2009-06-17 00:30:36 +0000
commit0e28997989ffbb65ad765dd1f0c72c251dd86609 (patch)
treef668bd394d5829c7285e450be0932672df82a863 /src/helper/command.c
parent06a1bb335e0971a7b986d7726c48d1502e2517c4 (diff)
downloadopenocd+libswd-0e28997989ffbb65ad765dd1f0c72c251dd86609.tar.gz
openocd+libswd-0e28997989ffbb65ad765dd1f0c72c251dd86609.tar.bz2
openocd+libswd-0e28997989ffbb65ad765dd1f0c72c251dd86609.tar.xz
openocd+libswd-0e28997989ffbb65ad765dd1f0c72c251dd86609.zip
Move definition of parse_type helpers to command.c:
- Add declarations in header file. - Improve wrapper implementations to check for underflow. git-svn-id: svn://svn.berlios.de/openocd/trunk@2257 b42882b7-edfa-0310-969c-e2dbd0fdcd60
Diffstat (limited to 'src/helper/command.c')
-rw-r--r--src/helper/command.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/helper/command.c b/src/helper/command.c
index 9a8907d6..bb3de2ce 100644
--- a/src/helper/command.c
+++ b/src/helper/command.c
@@ -880,3 +880,32 @@ DEFINE_PARSE_NUM_TYPE(_ulong, unsigned long , strtoul, 0, ULONG_MAX)
DEFINE_PARSE_NUM_TYPE(_ullong, unsigned long long, strtoull, 0, ULLONG_MAX)
DEFINE_PARSE_NUM_TYPE(_long, long , strtol, LONG_MIN, LONG_MAX)
DEFINE_PARSE_NUM_TYPE(_llong, long long, strtoll, LLONG_MIN, LLONG_MAX)
+
+#define DEFINE_PARSE_WRAPPER(name, type, min, max, functype, funcname) \
+ int parse_##name(const char *str, type *ul) \
+ { \
+ functype n; \
+ int retval = parse##funcname(str, &n); \
+ if (ERROR_OK != retval) \
+ return retval; \
+ if (n > max) \
+ return ERROR_COMMAND_SYNTAX_ERROR; \
+ if (min) \
+ return ERROR_COMMAND_SYNTAX_ERROR; \
+ *ul = n; \
+ return ERROR_OK; \
+ }
+
+#define DEFINE_PARSE_ULONG(name, type, min, max) \
+ DEFINE_PARSE_WRAPPER(name, type, min, max, unsigned long, _ulong)
+DEFINE_PARSE_ULONG(uint, unsigned, 0, UINT_MAX)
+DEFINE_PARSE_ULONG(u32, uint32_t, 0, UINT32_MAX)
+DEFINE_PARSE_ULONG(u16, uint16_t, 0, UINT16_MAX)
+DEFINE_PARSE_ULONG(u8, uint8_t, 0, UINT8_MAX)
+
+#define DEFINE_PARSE_LONG(name, type, min, max) \
+ DEFINE_PARSE_WRAPPER(name, type, min, max, long, _long)
+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)