diff options
author | William Wilgus <wilgus.william@gmail.com> | 2024-07-26 00:56:13 -0400 |
---|---|---|
committer | William Wilgus <wilgus.william@gmail.com> | 2024-07-26 00:56:13 -0400 |
commit | e94f778f1c5efdc4d11c61dec0e4a4ab073899d7 (patch) | |
tree | d518af58cb2faf04082adda6aad40c0e70f37b9a | |
parent | e8d7a8baaffbd9060842c8fa959fb5dabf8c0c43 (diff) | |
download | rockbox-e94f778f1c.tar.gz rockbox-e94f778f1c.zip |
plugin argparse update to add userdata to callback
fix a couple of gotchas if you aren't using NULL terminated strings
Change-Id: If5d2a60c0c3e1653e26df50bfda7d3191989bca9
-rw-r--r-- | apps/plugins/lib/arg_helper.c | 25 | ||||
-rw-r--r-- | apps/plugins/lib/arg_helper.h | 4 | ||||
-rw-r--r-- | apps/plugins/pitch_screen.c | 5 |
3 files changed, 24 insertions, 10 deletions
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_ */ diff --git a/apps/plugins/pitch_screen.c b/apps/plugins/pitch_screen.c index e24e0240a2..4af34fed3b 100644 --- a/apps/plugins/pitch_screen.c +++ b/apps/plugins/pitch_screen.c @@ -1120,8 +1120,9 @@ int gui_syncpitchscreen_run(void) return 0; } -static int arg_callback(char argchar, const char **parameter) +static int arg_callback(char argchar, const char **parameter, void *userdata) { + (void)userdata; int ret; long num, dec; bool bret; @@ -1232,7 +1233,7 @@ enum plugin_status plugin_start(const void* parameter) struct pvars cur; fill_pitchvars(&cur); fill_pitchvars(&pitch_vars); - argparse((const char*) parameter, -1, &arg_callback); + argparse((const char*) parameter, -1, NULL, &arg_callback); if (pitch_vars.pitch != cur.pitch) { rb->sound_set_pitch(pitch_vars.pitch); |