aboutsummaryrefslogtreecommitdiff
path: root/src/helper
diff options
context:
space:
mode:
authorzwelch <zwelch@b42882b7-edfa-0310-969c-e2dbd0fdcd60>2009-06-12 01:39:44 +0000
committerzwelch <zwelch@b42882b7-edfa-0310-969c-e2dbd0fdcd60>2009-06-12 01:39:44 +0000
commit5c123481a12b229df1f20515515024aa26457726 (patch)
tree6a4e4bc049afe85d41ccea34b3e98995bacc8c84 /src/helper
parent5bb0f1d29a71776018eba6f84e98a5ff24c21f50 (diff)
downloadriscv-openocd-5c123481a12b229df1f20515515024aa26457726.zip
riscv-openocd-5c123481a12b229df1f20515515024aa26457726.tar.gz
riscv-openocd-5c123481a12b229df1f20515515024aa26457726.tar.bz2
Add new parse_uinttype wrappers for strtoul in src/helper/command.[ch].
- Used to improve command argument parsing of unsigned integers values. git-svn-id: svn://svn.berlios.de/openocd/trunk@2206 b42882b7-edfa-0310-969c-e2dbd0fdcd60
Diffstat (limited to 'src/helper')
-rw-r--r--src/helper/command.c15
-rw-r--r--src/helper/command.h16
2 files changed, 31 insertions, 0 deletions
diff --git a/src/helper/command.c b/src/helper/command.c
index bda5182..bd6b693 100644
--- a/src/helper/command.c
+++ b/src/helper/command.c
@@ -848,3 +848,18 @@ long jim_global_long(const char *variable)
}
return 0;
}
+
+int parse_ullong(const char *str, unsigned long long *ul)
+{
+ char *end;
+ *ul = strtoull(str, &end, 0);
+ bool okay = *str && !*end && ULLONG_MAX != *ul;
+ return okay ? ERROR_OK : ERROR_COMMAND_SYNTAX_ERROR;
+}
+int parse_ulong(const char *str, unsigned long *ul)
+{
+ char *end;
+ *ul = strtoul(str, &end, 0);
+ bool okay = *str && !*end && ULONG_MAX != *ul;
+ return okay ? ERROR_OK : ERROR_COMMAND_SYNTAX_ERROR;
+}
diff --git a/src/helper/command.h b/src/helper/command.h
index 75e513d..d5b8764 100644
--- a/src/helper/command.h
+++ b/src/helper/command.h
@@ -107,4 +107,20 @@ void register_jim(command_context_t *context, const char *name, int (*cmd)(Jim_I
long jim_global_long(const char *variable);
+int parse_ulong(const char *str, unsigned long *ul);
+int parse_ullong(const char *str, unsigned long long *ul);
+
+#define DEFINE_PARSE_ULONG(name, type, max) \
+ static inline int parse_##name(const char *str, type *ul) \
+ { \
+ unsigned long n; \
+ int retval = parse_ulong(str, &n); \
+ *ul = n; \
+ return n > (max) ? ERROR_COMMAND_SYNTAX_ERROR : retval; \
+ }
+DEFINE_PARSE_ULONG(uint, unsigned, UINT_MAX)
+DEFINE_PARSE_ULONG(u32, uint32_t, UINT32_MAX)
+DEFINE_PARSE_ULONG(u16, uint16_t, UINT16_MAX)
+DEFINE_PARSE_ULONG(u8, uint8_t, UINT8_MAX)
+
#endif /* COMMAND_H */