All of lore.kernel.org
 help / color / mirror / Atom feed
From: Masahiro Yamada <yamada.masahiro@socionext.com>
To: linux-kbuild@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Kees Cook <keescook@chromium.org>,
	Nicholas Piggin <npiggin@gmail.com>,
	"Luis R . Rodriguez" <mcgrof@suse.com>,
	Randy Dunlap <rdunlap@infradead.org>,
	Ulf Magnusson <ulfalizer@gmail.com>,
	Sam Ravnborg <sam@ravnborg.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Masahiro Yamada <yamada.masahiro@socionext.com>
Subject: [PATCH v5 08/31] kconfig: add built-in function support
Date: Mon, 28 May 2018 18:21:45 +0900	[thread overview]
Message-ID: <1527499328-13213-9-git-send-email-yamada.masahiro@socionext.com> (raw)
In-Reply-To: <1527499328-13213-1-git-send-email-yamada.masahiro@socionext.com>

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@socionext.com>
---

Changes in v5:
  - Drop delayed expansion of arguments.

Changes in v4:
  - Error out if arguments more than FUNCTION_MAX_ARGS are passed
  - Use a comma as a delimiter between the function name and the
    first argument
  - Check the number of arguments accepted by each function
  - Support delayed expansion of arguments.
    This will be needed to implement 'if' function

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.
  - Simplify 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 a2eb2eb..f32a496 100644
--- a/scripts/kconfig/preprocess.c
+++ b/scripts/kconfig/preprocess.c
@@ -10,6 +10,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[]);
+
 static void __attribute__((noreturn)) pperror(const char *format, ...)
 {
 	va_list ap;
@@ -92,20 +96,123 @@ void env_write_dep(FILE *f, const char *autoconfig_name)
 	}
 }
 
-static char *eval_clause(const char *str, size_t len)
+/*
+ * Built-in functions
+ */
+struct function {
+	const char *name;
+	unsigned int min_args;
+	unsigned int max_args;
+	char *(*func)(int argc, char *argv[]);
+};
+
+static const struct function function_table[] = {
+	/* Name		MIN	MAX	Function */
+};
+
+#define FUNCTION_MAX_ARGS		16
+
+static char *function_expand(const char *name, int argc, char *argv[])
 {
-	char *tmp, *name, *res;
+	const struct function *f;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(function_table); i++) {
+		f = &function_table[i];
+		if (strcmp(f->name, name))
+			continue;
+
+		if (argc < f->min_args)
+			pperror("too few function arguments passed to '%s'",
+				name);
+
+		if (argc > f->max_args)
+			pperror("too many function arguments passed to '%s'",
+				name);
+
+		return f->func(argc, argv);
+	}
+
+	return NULL;
+}
+
+/*
+ * 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 *str, size_t len, int argc, char *argv[])
+{
+	char *tmp, *name, *res, *prev, *p;
+	int new_argc = 0;
+	char *new_argv[FUNCTION_MAX_ARGS];
+	int nest = 0;
+	int i;
 
 	tmp = xstrndup(str, len);
 
-	name = expand_string(tmp);
+	prev = p = tmp;
+
+	/*
+	 * Split into tokens
+	 * The function name and arguments are separated by a comma.
+	 * For example, if the function call is like this:
+	 *   $(foo,$(x),$(y))
+	 *
+	 * The input string for this helper should be:
+	 *   foo,$(x),$(y)
+	 *
+	 * and split into:
+	 *   new_argv[0] = 'foo'
+	 *   new_argv[1] = '$(x)'
+	 *   new_argv[2] = '$(y)'
+	 */
+	while (*p) {
+		if (nest == 0 && *p == ',') {
+			*p = 0;
+			if (new_argc >= FUNCTION_MAX_ARGS)
+				pperror("too many function arguments");
+			new_argv[new_argc++] = prev;
+			prev = p + 1;
+		} else if (*p == '(') {
+			nest++;
+		} else if (*p == ')') {
+			nest--;
+		}
 
-	res = env_expand(name);
+		p++;
+	}
+	new_argv[new_argc++] = prev;
+
+	/*
+	 * Shift arguments
+	 * new_argv[0] represents a function name or a variable name.  Put it
+	 * into 'name', then shift the rest of the arguments.  This simplifies
+	 * 'const' handling.
+	 */
+	name = expand_string_with_args(new_argv[0], argc, argv);
+	new_argc--;
+	for (i = 0; i < new_argc; i++)
+		new_argv[i] = expand_string_with_args(new_argv[i + 1],
+						      argc, argv);
+
+	/* Look for built-in functions */
+	res = function_expand(name, new_argc, new_argv);
 	if (res)
 		goto free;
 
+	/* Last, try environment variable */
+	if (new_argc == 0) {
+		res = env_expand(name);
+		if (res)
+			goto free;
+	}
+
 	res = xstrdup("");
 free:
+	for (i = 0; i < new_argc; i++)
+		free(new_argv[i]);
 	free(name);
 	free(tmp);
 
@@ -124,14 +231,14 @@ static char *eval_clause(const char *str, size_t len)
  * after the corresponding closing parenthesis, in this case, *str will be
  *     $(BAR)
  */
-char *expand_dollar(const char **str)
+static char *expand_dollar_with_args(const char **str, int argc, char *argv[])
 {
 	const char *p = *str;
 	const char *q;
 	int nest = 0;
 
 	/*
-	 * In Kconfig, variable references always start with "$(".
+	 * In Kconfig, variable/function references always start with "$(".
 	 * Neither single-letter variables as in $A nor curly braces as in ${CC}
 	 * are supported.  '$' not followed by '(' loses its special meaning.
 	 */
@@ -158,10 +265,16 @@ char *expand_dollar(const char **str)
 	/* Advance 'str' to after the expanded initial portion of the string */
 	*str = q + 1;
 
-	return eval_clause(p, q - p);
+	return eval_clause(p, q - p, argc, argv);
+}
+
+char *expand_dollar(const char **str)
+{
+	return expand_dollar_with_args(str, 0, NULL);
 }
 
-static char *__expand_string(const char **str, bool (*is_end)(char c))
+static char *__expand_string(const char **str, bool (*is_end)(char c),
+			     int argc, char *argv[])
 {
 	const char *in, *p;
 	char *expansion, *out;
@@ -177,7 +290,7 @@ static char *__expand_string(const char **str, bool (*is_end)(char c))
 		if (*p == '$') {
 			in_len = p - in;
 			p++;
-			expansion = expand_dollar(&p);
+			expansion = expand_dollar_with_args(&p, argc, argv);
 			out_len += in_len + strlen(expansion);
 			out = xrealloc(out, out_len);
 			strncat(out, in, in_len);
@@ -210,13 +323,18 @@ static bool is_end_of_str(char c)
 }
 
 /*
- * Expand variables in the given string.  Undefined variables
+ * Expand variables and functions in the given string.  Undefined variables
  * expand to an empty string.
  * The returned string must be freed when done.
  */
+static char *expand_string_with_args(const char *in, int argc, char *argv[])
+{
+	return __expand_string(&in, is_end_of_str, argc, argv);
+}
+
 char *expand_string(const char *in)
 {
-	return __expand_string(&in, is_end_of_str);
+	return expand_string_with_args(in, 0, NULL);
 }
 
 static bool is_end_of_token(char c)
@@ -234,5 +352,5 @@ static bool is_end_of_token(char c)
  */
 char *expand_one_token(const char **str)
 {
-	return __expand_string(str, is_end_of_token);
+	return __expand_string(str, is_end_of_token, 0, NULL);
 }
-- 
2.7.4

  parent reply	other threads:[~2018-05-28  9:29 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-28  9:21 [PATCH v5 00/31] kconfig: move compiler capability tests to Kconfig Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 01/31] kbuild: remove kbuild cache Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 02/31] kbuild: remove CONFIG_CROSS_COMPILE support Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 03/31] kconfig: reference environment variables directly and remove 'option env=' Masahiro Yamada
2018-06-08 17:53   ` [v5, " Andrei Vagin
2018-05-28  9:21 ` [PATCH v5 04/31] kconfig: remove string expansion in file_lookup() Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 05/31] kconfig: remove string expansion for mainmenu after yyparse() Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 06/31] kconfig: remove sym_expand_string_value() Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 07/31] kconfig: make default prompt of mainmenu less specific Masahiro Yamada
2018-05-28  9:21 ` Masahiro Yamada [this message]
2018-05-28  9:21 ` [PATCH v5 09/31] kconfig: add 'shell' built-in function Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 10/31] kconfig: replace $(UNAME_RELEASE) with function call Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 11/31] kconfig: begin PARAM state only when seeing a command keyword Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 12/31] kconfig: support user-defined function and recursively expanded variable Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 13/31] kconfig: support simply " Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 14/31] kconfig: support append assignment operator Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 15/31] kconfig: expand lefthand side of assignment statement Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 16/31] kconfig: add 'info', 'warning-if', and 'error-if' built-in functions Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 17/31] kconfig: add 'filename' and 'lineno' built-in variables Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 18/31] kconfig: error out if a recursive variable references itself Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 19/31] Documentation: kconfig: document a new Kconfig macro language Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 20/31] kconfig: test: add Kconfig macro language tests Masahiro Yamada
2018-05-28  9:21 ` [PATCH v5 21/31] kconfig: show compiler version text in the top comment Masahiro Yamada
2018-06-07  8:42   ` Geert Uytterhoeven
2018-06-07  8:58     ` Masahiro Yamada
2018-06-08  7:04       ` Masahiro Yamada
2018-06-08  8:22         ` Geert Uytterhoeven
2018-05-28  9:21 ` [PATCH v5 22/31] kconfig: add basic helper macros to scripts/Kconfig.include Masahiro Yamada
2018-05-28  9:22 ` [PATCH v5 23/31] stack-protector: test compiler capability in Kconfig and drop AUTO mode Masahiro Yamada
2018-05-28  9:22 ` [PATCH v5 24/31] kconfig: add CC_IS_GCC and GCC_VERSION Masahiro Yamada
2018-06-04 21:49   ` Stefan Agner
2018-06-05  0:07     ` Masahiro Yamada
2018-06-05  5:50       ` Stefan Agner
2018-06-05  6:27         ` Masahiro Yamada
2018-06-05  8:37           ` Stefan Agner
2018-05-28  9:22 ` [PATCH v5 25/31] kconfig: add CC_IS_CLANG and CLANG_VERSION Masahiro Yamada
2018-05-28  9:22 ` [PATCH v5 26/31] gcov: remove CONFIG_GCOV_FORMAT_AUTODETECT Masahiro Yamada
2018-05-28  9:22 ` [PATCH v5 27/31] kcov: test compiler capability in Kconfig and correct dependency Masahiro Yamada
2018-06-10 14:19   ` Masahiro Yamada
2018-05-28  9:22 ` [PATCH v5 28/31] gcc-plugins: move GCC version check for PowerPC to Kconfig Masahiro Yamada
2018-05-28  9:22 ` [PATCH v5 29/31] gcc-plugins: test plugin support in Kconfig and clean up Makefile Masahiro Yamada
2018-05-28  9:22 ` [PATCH v5 30/31] gcc-plugins: allow to enable GCC_PLUGINS for COMPILE_TEST Masahiro Yamada
2018-05-28  9:22 ` [PATCH v5 31/31] Documentation: kconfig: add recommended way to describe compiler support Masahiro Yamada
2018-05-28 12:23 ` [PATCH v5 00/31] kconfig: move compiler capability tests to Kconfig Masahiro Yamada
2018-05-30  0:38   ` Masahiro Yamada
2018-06-01  8:31     ` Arnd Bergmann
2018-06-01 10:29       ` Masahiro Yamada
2018-06-01 11:09         ` Arnd Bergmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1527499328-13213-9-git-send-email-yamada.masahiro@socionext.com \
    --to=yamada.masahiro@socionext.com \
    --cc=keescook@chromium.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mcgrof@suse.com \
    --cc=npiggin@gmail.com \
    --cc=rdunlap@infradead.org \
    --cc=sam@ravnborg.org \
    --cc=torvalds@linux-foundation.org \
    --cc=ulfalizer@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.