summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntony Pavlov <antonynpavlov@gmail.com>2018-01-15 00:22:51 +0300
committerSascha Hauer <s.hauer@pengutronix.de>2018-01-17 10:44:30 +0100
commitee8e839569db30fc1d1a7fb712c4a81f6c0089b7 (patch)
tree40035b4c262995eb0b84e835b521139ce1f9ce65
parente1ac1912755a8999bc110f2e6cf6e17965bece3d (diff)
downloadbarebox-ee8e839569db30fc1d1a7fb712c4a81f6c0089b7.tar.gz
barebox-ee8e839569db30fc1d1a7fb712c4a81f6c0089b7.tar.xz
parseopt: introduce parseopt_u16() and parseopt_str()
Signed-off-by: Antony Pavlov <antonynpavlov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--include/parseopt.h2
-rw-r--r--lib/parseopt.c64
2 files changed, 66 insertions, 0 deletions
diff --git a/include/parseopt.h b/include/parseopt.h
index becc734c2b..1f9763f8c9 100644
--- a/include/parseopt.h
+++ b/include/parseopt.h
@@ -3,5 +3,7 @@
void parseopt_b(const char *options, const char *opt, bool *val);
void parseopt_hu(const char *options, const char *opt, unsigned short *val);
+void parseopt_u16(const char *options, const char *opt, uint16_t *val);
+void parseopt_str(const char *options, const char *opt, char **val);
#endif /* __PARSEOPT_H__ */
diff --git a/lib/parseopt.c b/lib/parseopt.c
index 8ff83019a7..8211733e3b 100644
--- a/lib/parseopt.c
+++ b/lib/parseopt.c
@@ -58,3 +58,67 @@ again:
if (*endp == ',' || *endp == '\0')
*val = v;
}
+
+void parseopt_u16(const char *options, const char *opt, uint16_t *val)
+{
+ const char *start;
+ size_t optlen = strlen(opt);
+ ulong v;
+ char *endp;
+
+again:
+ start = strstr(options, opt);
+
+ if (!start)
+ return;
+
+ if (start > options && start[-1] != ',') {
+ options = start;
+ goto again;
+ }
+
+ if (start[optlen] != '=') {
+ options = start;
+ goto again;
+ }
+
+ v = simple_strtoul(start + optlen + 1, &endp, 0);
+ if (v > U16_MAX)
+ return;
+
+ if (*endp == ',' || *endp == '\0')
+ *val = v;
+}
+
+void parseopt_str(const char *options, const char *opt, char **val)
+{
+ const char *start;
+ size_t optlen = strlen(opt);
+ char *endp;
+ char *parsed;
+
+again:
+ start = strstr(options, opt);
+
+ if (!start)
+ return;
+
+ if (start > options && start[-1] != ',') {
+ options = start;
+ goto again;
+ }
+
+ if (start[optlen] != '=') {
+ options = start;
+ goto again;
+ }
+
+ parsed = (char *)start + optlen + 1;
+ endp = parsed;
+ while (*endp != '\0' && *endp != ',') {
+
+ endp++;
+ }
+
+ *val = xstrndup(parsed, endp - parsed);
+}