exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

commit d3cedf873448f998a3becbece6ed37fe5af1fc94
parent 3f99db063a3bd23296163ac4fff8d51459f92f78
Author: Florian Dold <florian@dold.me>
Date:   Fri,  8 May 2026 13:31:14 +0200

put word splitting in taler util

The merchant and possibly other components in the future need this
functionality for calling helpers with support for arguments in the
configuration.

The Taler components should consistently support commands with arguments
in the configuration for helpers, as otherwise tests have to jump
through ugly hoops when tests use special helpers with context.

Diffstat:
Msrc/include/taler/taler_util.h | 21+++++++++++++++++++++
Msrc/kyclogic/kyclogic_api.c | 103+++++++------------------------------------------------------------------------
Msrc/util/util.c | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 103 insertions(+), 94 deletions(-)

diff --git a/src/include/taler/taler_util.h b/src/include/taler/taler_util.h @@ -1075,6 +1075,27 @@ TALER_SECMOD_rsa_run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg); +/** + * Perform very primitive word splitting of a command. + * Primarily used to split helper commands from the configurations. + * + * @param command command to split + * @param extra_args extra arguments to append after the word + * @returns NULL-terminated array of words, free wiith TALER_words_destroy + */ +char ** +TALER_words_split (const char *command, + const char **extra_args); + + +/** + * Free arguments allocated with split_words. + * + * @param args NULL-terminated array of strings to free. + */ +void +TALER_words_destroy (char **args); + #undef __TALER_UTIL_LIB_H_INSIDE__ #endif diff --git a/src/kyclogic/kyclogic_api.c b/src/kyclogic/kyclogic_api.c @@ -1768,91 +1768,6 @@ TALER_KYCLOGIC_rule2priority ( /** - * Perform very primitive word splitting of a command. - * - * @param command command to split - * @param extra_args extra arguments to append after the word - * @returns NULL-terminated array of words - */ -static char ** -split_words (const char *command, - const char **extra_args) -{ - unsigned int i = 0; - unsigned int j = 0; - unsigned int n = 0; - char **res = NULL; - - /* Result is always NULL-terminated */ - GNUNET_array_append (res, n, NULL); - - /* Split command into words */ - while (1) - { - char *c; - - /* Skip initial whitespace before word */ - while (' ' == command[i]) - i++; - - /* Start of new word */ - j = i; - - /* Scan to end of word */ - while ( (0 != command[j]) && (' ' != command[j]) ) - j++; - - /* No new word found */ - if (i == j) - break; - - /* Append word to result */ - c = GNUNET_malloc (j - i + 1); - memcpy (c, &command[i], j - i); - c[j - i] = 0; - res[n - 1] = c; - GNUNET_array_append (res, n, NULL); - - /* Continue at end of word */ - i = j; - } - - /* Append extra args */ - if (NULL != extra_args) - { - for (const char **m = extra_args; *m; m++) - { - res[n - 1] = GNUNET_strdup (*m); - GNUNET_array_append (res, - n, - NULL); - } - } - - return res; -} - - -/** - * Free arguments allocated with split_words. - * - * @param args NULL-terminated array of strings to free. - */ -static void -destroy_words (char **args) -{ - if (NULL == args) - return; - for (char **m = args; *m; m++) - { - GNUNET_free (*m); - *m = NULL; - } - GNUNET_free (args); -} - - -/** * Run @a command with @a argument and return the * respective output from stdout. * @@ -1894,8 +1809,8 @@ command_output (const char *command, { char **argv; - argv = split_words (command, - extra_args); + argv = TALER_words_split (command, + extra_args); GNUNET_break (0 == close (sout[0])); @@ -1908,7 +1823,7 @@ command_output (const char *command, close (sout[1])); execvp (argv[0], argv); - destroy_words (argv); + TALER_words_destroy (argv); GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "exec", command); @@ -4791,8 +4706,8 @@ handle_aml_timeout (void *cls) }; char **args; - args = split_words (fprogram->command, - extra_args); + args = TALER_words_split (fprogram->command, + extra_args); GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running fallback measure `%s' (%s)\n", fallback_measure, @@ -4803,7 +4718,7 @@ handle_aml_timeout (void *cls) aprh, args[0], (const char **) args); - destroy_words (args); + TALER_words_destroy (args); json_decref (input); } aprh->async_cb = GNUNET_SCHEDULER_add_delayed (aprh->timeout, @@ -5050,8 +4965,8 @@ TALER_KYCLOGIC_run_aml_program2 ( GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running AML program %s\n", prog->command); - args = split_words (prog->command, - extra_args); + args = TALER_words_split (prog->command, + extra_args); GNUNET_assert (NULL != args); GNUNET_assert (NULL != args[0]); #if DEBUG @@ -5065,7 +4980,7 @@ TALER_KYCLOGIC_run_aml_program2 ( aprh, args[0], (const char **) args); - destroy_words (args); + TALER_words_destroy (args); json_decref (input); } aprh->timeout = timeout; diff --git a/src/util/util.c b/src/util/util.c @@ -387,6 +387,79 @@ TALER_json_hash (const json_t *j, } +char ** +TALER_words_split (const char *command, + const char **extra_args) +{ + unsigned int i = 0; + unsigned int j = 0; + unsigned int n = 0; + char **res = NULL; + + /* Result is always NULL-terminated */ + GNUNET_array_append (res, n, NULL); + + /* Split command into words */ + while (1) + { + char *c; + + /* Skip initial whitespace before word */ + while (' ' == command[i]) + i++; + + /* Start of new word */ + j = i; + + /* Scan to end of word */ + while ( (0 != command[j]) && (' ' != command[j]) ) + j++; + + /* No new word found */ + if (i == j) + break; + + /* Append word to result */ + c = GNUNET_malloc (j - i + 1); + memcpy (c, &command[i], j - i); + c[j - i] = 0; + res[n - 1] = c; + GNUNET_array_append (res, n, NULL); + + /* Continue at end of word */ + i = j; + } + + /* Append extra args */ + if (NULL != extra_args) + { + for (const char **m = extra_args; *m; m++) + { + res[n - 1] = GNUNET_strdup (*m); + GNUNET_array_append (res, + n, + NULL); + } + } + + return res; +} + + +void +TALER_words_destroy (char **args) +{ + if (NULL == args) + return; + for (char **m = args; *m; m++) + { + GNUNET_free (*m); + *m = NULL; + } + GNUNET_free (args); +} + + #ifdef __APPLE__ char * strchrnul (const char *s,