diff options
author | Masahiro Yamada <yamada.masahiro@socionext.com> | 2018-05-28 05:21:46 -0400 |
---|---|---|
committer | Masahiro Yamada <yamada.masahiro@socionext.com> | 2018-05-28 14:31:19 -0400 |
commit | 2fd5b09c201e20ab87299efd6a25f0bfc546e7c9 (patch) | |
tree | 712cb918bbc6c7f58314e03330ea51bec6612905 | |
parent | e298f3b49def8e67c2466bef8aed355462bfa7f1 (diff) |
kconfig: add 'shell' built-in function
This accepts a single command to execute. It returns the standard
output from it.
[Example code]
config HELLO
string
default "$(shell,echo hello world)"
config Y
def_bool $(shell,echo y)
[Result]
$ make -s alldefconfig && tail -n 2 .config
CONFIG_HELLO="hello world"
CONFIG_Y=y
Caveat:
Like environments, functions are expanded in the lexer. You cannot
pass symbols to function arguments. This is a limitation to simplify
the implementation. I want to avoid the dynamic function evaluation,
which would introduce much more complexity.
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
-rw-r--r-- | scripts/kconfig/preprocess.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/scripts/kconfig/preprocess.c b/scripts/kconfig/preprocess.c index f32a496626da..528be594e1d0 100644 --- a/scripts/kconfig/preprocess.c +++ b/scripts/kconfig/preprocess.c | |||
@@ -106,8 +106,49 @@ struct function { | |||
106 | char *(*func)(int argc, char *argv[]); | 106 | char *(*func)(int argc, char *argv[]); |
107 | }; | 107 | }; |
108 | 108 | ||
109 | static char *do_shell(int argc, char *argv[]) | ||
110 | { | ||
111 | FILE *p; | ||
112 | char buf[256]; | ||
113 | char *cmd; | ||
114 | size_t nread; | ||
115 | int i; | ||
116 | |||
117 | cmd = argv[0]; | ||
118 | |||
119 | p = popen(cmd, "r"); | ||
120 | if (!p) { | ||
121 | perror(cmd); | ||
122 | exit(1); | ||
123 | } | ||
124 | |||
125 | nread = fread(buf, 1, sizeof(buf), p); | ||
126 | if (nread == sizeof(buf)) | ||
127 | nread--; | ||
128 | |||
129 | /* remove trailing new lines */ | ||
130 | while (buf[nread - 1] == '\n') | ||
131 | nread--; | ||
132 | |||
133 | buf[nread] = 0; | ||
134 | |||
135 | /* replace a new line with a space */ | ||
136 | for (i = 0; i < nread; i++) { | ||
137 | if (buf[i] == '\n') | ||
138 | buf[i] = ' '; | ||
139 | } | ||
140 | |||
141 | if (pclose(p) == -1) { | ||
142 | perror(cmd); | ||
143 | exit(1); | ||
144 | } | ||
145 | |||
146 | return xstrdup(buf); | ||
147 | } | ||
148 | |||
109 | static const struct function function_table[] = { | 149 | static const struct function function_table[] = { |
110 | /* Name MIN MAX Function */ | 150 | /* Name MIN MAX Function */ |
151 | { "shell", 1, 1, do_shell }, | ||
111 | }; | 152 | }; |
112 | 153 | ||
113 | #define FUNCTION_MAX_ARGS 16 | 154 | #define FUNCTION_MAX_ARGS 16 |