Re: [PATCH 08/30] kconfig: add built-in function support
From: Ulf Magnusson
Date: Sun Apr 15 2018 - 03:58:24 EST
On Fri, Apr 13, 2018 at 7:06 AM, Masahiro Yamada
<yamada.masahiro@xxxxxxxxxxxxx> wrote:
> This commit adds a new concept 'function' to do more text processing
> in Kconfig.
>
> A function call looks like this:
>
> $(function arg1, arg2, arg3, ...)
>
> This commit adds the basic infrastructure to expand functions.
> Change the text expansion helpers to take arguments.
>
> Signed-off-by: Masahiro Yamada <yamada.masahiro@xxxxxxxxxxxxx>
> ---
>
> Changes in v3:
> - Split base infrastructure and 'shell' function
> into separate patches.
>
> Changes in v2:
> - Use 'shell' for getting stdout from the comment.
> It was 'shell-stdout' in the previous version.
> - Symplify the implementation since the expansion has been moved to
> lexer.
>
> scripts/kconfig/preprocess.c | 142 +++++++++++++++++++++++++++++++++++++++----
> 1 file changed, 130 insertions(+), 12 deletions(-)
>
> diff --git a/scripts/kconfig/preprocess.c b/scripts/kconfig/preprocess.c
> index fa4abc8..e77cf7c 100644
> --- a/scripts/kconfig/preprocess.c
> +++ b/scripts/kconfig/preprocess.c
> @@ -8,6 +8,10 @@
>
> #include "list.h"
>
> +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
> +
> +static char *expand_string_with_args(const char *in, int argc, char *argv[]);
> +
> /*
> * Environment variables
> */
> @@ -74,9 +78,47 @@ void env_write_dep(FILE *f, const char *autoconfig_name)
> }
> }
>
> -static char *eval_clause(const char *in)
> +/*
> + * Built-in Functions
> + */
> +struct function {
> + char *name;
> + char *(*func)(int argc, char *argv[]);
> +};
> +
> +static const struct function function_table[] = {
> +};
> +
> +static char *function_call(const char *name, int argc, char *argv[])
> {
> - char *res, *name;
> + const struct function *f;
> + int i;
> +
> + for (i = 0; i < ARRAY_SIZE(function_table); i++) {
> + f = &function_table[i];
> + if (!strcmp(f->name, name))
> + return f->func(argc, argv);
> + }
> +
> + return NULL;
> +}
> +
> +#define FUNCTION_MAX_ARGS 16
> +
> +/*
> + * Evaluate a clause with arguments. argc/argv are arguments from the upper
> + * function call.
> + *
> + * Returned string must be freed when done
> + */
> +static char *eval_clause(const char *in, int argc, char *argv[])
> +{
> + char *tmp, *prev, *p, *res, *name;
> + char delim = ' ';
> + int new_argc = 0;
> + char *new_argv[FUNCTION_MAX_ARGS];
> + int nest = 0;
> + int i;
>
> /*
> * Returns an empty string because '$()' should be evaluated
> @@ -85,10 +127,73 @@ static char *eval_clause(const char *in)
> if (!*in)
> return xstrdup("");
>
> - name = expand_string(in);
> + tmp = xstrdup(in);
> +
> + prev = p = tmp;
>
> - res = env_expand(name);
> + /*
> + * Split into tokens
> + * The function name and the first argument are separated by a space.
> + * Arguments are separated by a comma.
> + * For example, if the function call is like this:
> + * $(foo abc,$(x),$(y))
> + *
> + * The input string for this helper should be:
> + * foo abc,$(x),$(y)
> + *
> + * and split into:
> + * new_argv[0]: foo
> + * new_argv[1]: abc
> + * new_argv[2]: $(x)
> + * new_argv[3]: $(y)
> + */
> + while (*p) {
> + if (nest == 0 && *p == delim) {
> + *p = 0;
> + new_argv[new_argc++] = prev;
Erroring out would be nicer than overflowing the array.
Cheers,
Ulf