From e94f778f1c5efdc4d11c61dec0e4a4ab073899d7 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Fri, 26 Jul 2024 00:56:13 -0400 Subject: plugin argparse update to add userdata to callback fix a couple of gotchas if you aren't using NULL terminated strings Change-Id: If5d2a60c0c3e1653e26df50bfda7d3191989bca9 --- apps/plugins/lib/arg_helper.c | 25 +++++++++++++++++++------ apps/plugins/lib/arg_helper.h | 4 ++-- 2 files changed, 21 insertions(+), 8 deletions(-) (limited to 'apps/plugins/lib') diff --git a/apps/plugins/lib/arg_helper.c b/apps/plugins/lib/arg_helper.c index 3ea5ba714d..63b7acbb2b 100644 --- a/apps/plugins/lib/arg_helper.c +++ b/apps/plugins/lib/arg_helper.c @@ -31,7 +31,7 @@ #ifdef PLUGIN #define strchr rb->strchr #endif -int string_parse(const char **parameter, char* buf, size_t buf_sz) +int string_parse(const char **parameter, char *buf, size_t buf_sz) { /* fills buf with a string upto buf_sz, null terminates the buffer * strings break on WS by default but can be enclosed in single or double quotes @@ -44,6 +44,11 @@ int string_parse(const char **parameter, char* buf, size_t buf_sz) char stopchars[] = "\'\""; int skipped = 0; int found = 0; + if (!parameter || !*parameter) + { + *buf = '\0'; + return 0; + } const char* start = *parameter; if (strchr(stopchars, *start)) @@ -79,7 +84,7 @@ int string_parse(const char **parameter, char* buf, size_t buf_sz) return found + skipped; } -int char_parse(const char **parameter, char* character) +int char_parse(const char **parameter, char *character) { /* passes *character a single character eats remaining non-WS characters */ char buf[2]; @@ -95,6 +100,8 @@ int bool_parse(const char **parameter, bool *choice) /* determine true false using the first character the rest are skipped/ignored */ int found = 0; const char tf_val[]="fn0ty1";/* false chars on left f/t should be balanced fffttt */ + if (!parameter || !*parameter) + return 0; const char* start = *parameter; @@ -133,7 +140,9 @@ int longnum_parse(const char **parameter, long *number, long *decimal) int neg = 0; int digits = 0; //logf ("n: %s\n", *parameter); - const char *start = *parameter; + if (!parameter || !*parameter) + return 0; + const char* start = *parameter; if (*start == '-') { @@ -209,7 +218,8 @@ int num_parse(const char **parameter, int *number, int *decimal) * Note: WS at beginning is stripped, **parameter starts at the first NON WS char * return 0 for arg_callback to quit parsing immediately */ -void argparse(const char *parameter, int parameter_len, int (*arg_callback)(char argchar, const char **parameter)) +void argparse(const char *parameter, int parameter_len, void *userdata, + int (*arg_callback)(char argchar, const char **parameter, void *userdata)) { bool lastchr; char argchar; @@ -222,7 +232,10 @@ void argparse(const char *parameter, int parameter_len, int (*arg_callback)(char { if ((*parameter) == '\0') return; - logf ("%s\n",parameter); + + if (parameter_len < 0) { logf ("%s\n", parameter); } + else { logf ("%.*s\n", plen, parameter); } + argchar = *parameter; lastchr = (*(parameter + 1) == '\0'); while (*++parameter || lastchr) @@ -230,7 +243,7 @@ void argparse(const char *parameter, int parameter_len, int (*arg_callback)(char lastchr = false; if (isspace(*parameter)) continue; /* eat spaces at beginning */ - if (!arg_callback(argchar, ¶meter)) + if (!arg_callback(argchar, ¶meter, userdata)) return; break; } diff --git a/apps/plugins/lib/arg_helper.h b/apps/plugins/lib/arg_helper.h index 2cf94ba1dd..638279ee42 100644 --- a/apps/plugins/lib/arg_helper.h +++ b/apps/plugins/lib/arg_helper.h @@ -54,7 +54,7 @@ int num_parse(const char **parameter, int *number, int *decimal); * Note: WS at beginning is stripped, **parameter starts at the first NON WS char * return 0 for arg_callback to quit parsing immediately */ -void argparse(const char *parameter, int parameter_len, - int (*arg_callback)(char argchar, const char **parameter)); +void argparse(const char *parameter, int parameter_len, void *userdata, + int (*arg_callback)(char argchar, const char **parameter, void *userdata)); #endif /* _LIB_ARG_HELPER_H_ */ -- cgit v1.2.3