crosstool-ng/kconfig/preprocess.c

575 lines
11 KiB
C
Raw Normal View History

kconfig: Sync with upstream v4.18 This commit introduces the following upstream changes: 73d1c580f92b kconfig: loop boundary condition fix ecd53ac2f2c6 kconfig: handle P_SYMBOL in print_symbol() b2d00d7c61c8 kconfig: fix line numbers for if-entries in menu tree 8593080c0fcf kconfig: fix localmodconfig 2ae89c7a82ea kconfig: Avoid format overflow warning from GCC 8.1 bb6d83dde191 kbuild: Move last word of nconfig help to the previous line d6a0c8a1326b kconfig: Add testconfig into make help output 2bece88f89fa kconfig: test: add Kconfig macro language tests 915f64901eb3 kconfig: error out if a recursive variable references itself a702a6176e2f kconfig: add 'filename' and 'lineno' built-in variables 1d6272e6fe43 kconfig: add 'info', 'warning-if', and 'error-if' built-in functions 82bc8bd82e5c kconfig: expand lefthand side of assignment statement ed2a22f277c6 kconfig: support append assignment operator 1175c02506ff kconfig: support simply expanded variable 9ced3bddec08 kconfig: support user-defined function and recursively expanded variable 9de071536c87 kconfig: begin PARAM state only when seeing a command keyword 2fd5b09c201e kconfig: add 'shell' built-in function e298f3b49def kconfig: add built-in function support 137c0118a900 kconfig: make default prompt of mainmenu less specific 5b31a9746756 kconfig: remove sym_expand_string_value() 96d8e48da55a kconfig: remove string expansion for mainmenu after yyparse() bb222ceeb327 kconfig: remove string expansion in file_lookup() 104daea149c4 kconfig: reference environment variables directly and remove 'option env=' 694c49a7c01c kconfig: drop localization support 1c5af5cf9308 kconfig: refactor ncurses package checks for building mconf and nconf b464ef583dc7 kconfig: refactor GTK+ package checks for building gconf 0b669a5076fd kconfig: refactor Qt package checks for building qconf Signed-off-by: Chris Packham <judge.packham@gmail.com>
2020-12-09 08:24:45 +00:00
// SPDX-License-Identifier: GPL-2.0
//
// Copyright (C) 2018 Masahiro Yamada <yamada.masahiro@socionext.com>
kconfig: Sync with upstream v5.0 This commit introduces the following upstream changes: 2648ca1859bb kconfig: clean generated *conf-cfg files d86271af6460 kconfig: rename generated .*conf-cfg to *conf-cfg ba97df45581f kbuild: use assignment instead of define ... endef for filechk_* rules a5003571e627 kconfig: remove unused "file" field of yylval union f222b7f43661 kconfig: surround dbg_sym_flags with #ifdef DEBUG to fix gconf warning 3b541978562a kconfig: split images.c out of qconf.cc/gconf.c to fix gconf warnings 9abe42371b44 kconfig: add static qualifiers to fix gconf warnings cbafbf7f551c kconfig: split the lexer out of zconf.y 558e78e3ce84 kconfig: split some C files out of zconf.y 0c874100108f kconfig: convert to SPDX License Identifier 979f2b2f7936 kconfig: remove keyword lookup table entirely 4b31a32caf0a kconfig: update current_pos in the second lexer 824fa3b3b5e3 kconfig: switch to ASSIGN_VAL state in the second lexer b3d1d9d3c362 kconfig: stop associating kconf_id with yylval caaebb3c6de3 kconfig: refactor end token rules f5451582c4e2 kconfig: stop supporting '.' and '/' in unquoted words 171a515d0803 kconfig: use T_WORD instead of T_VARIABLE for variables c3d228713b10 kconfig: use specific tokens instead of T_ASSIGN for assignments ce2164ab5831 kconfig: refactor scanning and parsing "option" properties 3c8f317d4cf1 kconfig: use distinct tokens for type and default properties a01e5d242d93 kconfig: remove redundant token defines 4b5ec81bfeda kconfig: rename depends_list to comment_option_list 1f31be9ec0a9 kconfig: loosen the order of "visible" and "depends on" in menu entry 94d4e1b6021b kconfig: remove redundant menu_block rule 4891796c6f83 kconfig: remove redundant if_block rule 2f60e46e605a kconfig: remove grammatically ambiguous option_error 6900ae9eeee3 kconfig: remove grammatically ambiguous "unexpected option" diagnostic 723679339d08 kconfig: warn no new line at end of file 0bcc547ec4b0 kconfig: clean up EOF handling in the lexer cc66bca775ee kconfig: fix ambiguous grammar in terms of new lines 21c5ecf60472 kconfig: refactor pattern matching in STRING state be3c8075978a kconfig: remove unneeded pattern matching to whitespaces 413cd19d81fd kconfig: require T_EOL to reduce visible statement fbac5977d81c kconfig: fix memory leak when EOF is encountered in quotation 77c1c0fa8b14 kconfig: fix file name and line number of warn_ignored_character() 0cbe3ac439bf kconfig: remove k_invalid from expr_parse_string() return type 2aabbed6774f kconfig: remove S_OTHER symbol type and correct dependency tracking 1508fec82e39 kconfig: split out code touching a file to conf_touch_dep() 0849d212e395 kconfig: rename conf_split_config() to conf_touch_deps() 75889e9be78f kconfig: remove unneeded setsym label in conf_read_simple() a9b722847872 scripts/kconfig/merge_config: don't redefine 'y' to 'm' Signed-off-by: Chris Packham <judge.packham@gmail.com>
2020-12-09 09:13:29 +00:00
#include <ctype.h>
kconfig: Sync with upstream v4.18 This commit introduces the following upstream changes: 73d1c580f92b kconfig: loop boundary condition fix ecd53ac2f2c6 kconfig: handle P_SYMBOL in print_symbol() b2d00d7c61c8 kconfig: fix line numbers for if-entries in menu tree 8593080c0fcf kconfig: fix localmodconfig 2ae89c7a82ea kconfig: Avoid format overflow warning from GCC 8.1 bb6d83dde191 kbuild: Move last word of nconfig help to the previous line d6a0c8a1326b kconfig: Add testconfig into make help output 2bece88f89fa kconfig: test: add Kconfig macro language tests 915f64901eb3 kconfig: error out if a recursive variable references itself a702a6176e2f kconfig: add 'filename' and 'lineno' built-in variables 1d6272e6fe43 kconfig: add 'info', 'warning-if', and 'error-if' built-in functions 82bc8bd82e5c kconfig: expand lefthand side of assignment statement ed2a22f277c6 kconfig: support append assignment operator 1175c02506ff kconfig: support simply expanded variable 9ced3bddec08 kconfig: support user-defined function and recursively expanded variable 9de071536c87 kconfig: begin PARAM state only when seeing a command keyword 2fd5b09c201e kconfig: add 'shell' built-in function e298f3b49def kconfig: add built-in function support 137c0118a900 kconfig: make default prompt of mainmenu less specific 5b31a9746756 kconfig: remove sym_expand_string_value() 96d8e48da55a kconfig: remove string expansion for mainmenu after yyparse() bb222ceeb327 kconfig: remove string expansion in file_lookup() 104daea149c4 kconfig: reference environment variables directly and remove 'option env=' 694c49a7c01c kconfig: drop localization support 1c5af5cf9308 kconfig: refactor ncurses package checks for building mconf and nconf b464ef583dc7 kconfig: refactor GTK+ package checks for building gconf 0b669a5076fd kconfig: refactor Qt package checks for building qconf Signed-off-by: Chris Packham <judge.packham@gmail.com>
2020-12-09 08:24:45 +00:00
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
kconfig: Sync with upstream v5.0 This commit introduces the following upstream changes: 2648ca1859bb kconfig: clean generated *conf-cfg files d86271af6460 kconfig: rename generated .*conf-cfg to *conf-cfg ba97df45581f kbuild: use assignment instead of define ... endef for filechk_* rules a5003571e627 kconfig: remove unused "file" field of yylval union f222b7f43661 kconfig: surround dbg_sym_flags with #ifdef DEBUG to fix gconf warning 3b541978562a kconfig: split images.c out of qconf.cc/gconf.c to fix gconf warnings 9abe42371b44 kconfig: add static qualifiers to fix gconf warnings cbafbf7f551c kconfig: split the lexer out of zconf.y 558e78e3ce84 kconfig: split some C files out of zconf.y 0c874100108f kconfig: convert to SPDX License Identifier 979f2b2f7936 kconfig: remove keyword lookup table entirely 4b31a32caf0a kconfig: update current_pos in the second lexer 824fa3b3b5e3 kconfig: switch to ASSIGN_VAL state in the second lexer b3d1d9d3c362 kconfig: stop associating kconf_id with yylval caaebb3c6de3 kconfig: refactor end token rules f5451582c4e2 kconfig: stop supporting '.' and '/' in unquoted words 171a515d0803 kconfig: use T_WORD instead of T_VARIABLE for variables c3d228713b10 kconfig: use specific tokens instead of T_ASSIGN for assignments ce2164ab5831 kconfig: refactor scanning and parsing "option" properties 3c8f317d4cf1 kconfig: use distinct tokens for type and default properties a01e5d242d93 kconfig: remove redundant token defines 4b5ec81bfeda kconfig: rename depends_list to comment_option_list 1f31be9ec0a9 kconfig: loosen the order of "visible" and "depends on" in menu entry 94d4e1b6021b kconfig: remove redundant menu_block rule 4891796c6f83 kconfig: remove redundant if_block rule 2f60e46e605a kconfig: remove grammatically ambiguous option_error 6900ae9eeee3 kconfig: remove grammatically ambiguous "unexpected option" diagnostic 723679339d08 kconfig: warn no new line at end of file 0bcc547ec4b0 kconfig: clean up EOF handling in the lexer cc66bca775ee kconfig: fix ambiguous grammar in terms of new lines 21c5ecf60472 kconfig: refactor pattern matching in STRING state be3c8075978a kconfig: remove unneeded pattern matching to whitespaces 413cd19d81fd kconfig: require T_EOL to reduce visible statement fbac5977d81c kconfig: fix memory leak when EOF is encountered in quotation 77c1c0fa8b14 kconfig: fix file name and line number of warn_ignored_character() 0cbe3ac439bf kconfig: remove k_invalid from expr_parse_string() return type 2aabbed6774f kconfig: remove S_OTHER symbol type and correct dependency tracking 1508fec82e39 kconfig: split out code touching a file to conf_touch_dep() 0849d212e395 kconfig: rename conf_split_config() to conf_touch_deps() 75889e9be78f kconfig: remove unneeded setsym label in conf_read_simple() a9b722847872 scripts/kconfig/merge_config: don't redefine 'y' to 'm' Signed-off-by: Chris Packham <judge.packham@gmail.com>
2020-12-09 09:13:29 +00:00
#include "lkc.h"
kconfig: Sync with upstream v4.18 This commit introduces the following upstream changes: 73d1c580f92b kconfig: loop boundary condition fix ecd53ac2f2c6 kconfig: handle P_SYMBOL in print_symbol() b2d00d7c61c8 kconfig: fix line numbers for if-entries in menu tree 8593080c0fcf kconfig: fix localmodconfig 2ae89c7a82ea kconfig: Avoid format overflow warning from GCC 8.1 bb6d83dde191 kbuild: Move last word of nconfig help to the previous line d6a0c8a1326b kconfig: Add testconfig into make help output 2bece88f89fa kconfig: test: add Kconfig macro language tests 915f64901eb3 kconfig: error out if a recursive variable references itself a702a6176e2f kconfig: add 'filename' and 'lineno' built-in variables 1d6272e6fe43 kconfig: add 'info', 'warning-if', and 'error-if' built-in functions 82bc8bd82e5c kconfig: expand lefthand side of assignment statement ed2a22f277c6 kconfig: support append assignment operator 1175c02506ff kconfig: support simply expanded variable 9ced3bddec08 kconfig: support user-defined function and recursively expanded variable 9de071536c87 kconfig: begin PARAM state only when seeing a command keyword 2fd5b09c201e kconfig: add 'shell' built-in function e298f3b49def kconfig: add built-in function support 137c0118a900 kconfig: make default prompt of mainmenu less specific 5b31a9746756 kconfig: remove sym_expand_string_value() 96d8e48da55a kconfig: remove string expansion for mainmenu after yyparse() bb222ceeb327 kconfig: remove string expansion in file_lookup() 104daea149c4 kconfig: reference environment variables directly and remove 'option env=' 694c49a7c01c kconfig: drop localization support 1c5af5cf9308 kconfig: refactor ncurses package checks for building mconf and nconf b464ef583dc7 kconfig: refactor GTK+ package checks for building gconf 0b669a5076fd kconfig: refactor Qt package checks for building qconf Signed-off-by: Chris Packham <judge.packham@gmail.com>
2020-12-09 08:24:45 +00:00
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
static char *expand_string_with_args(const char *in, int argc, char *argv[]);
static char *expand_string(const char *in);
kconfig: Sync with upstream v4.18 This commit introduces the following upstream changes: 73d1c580f92b kconfig: loop boundary condition fix ecd53ac2f2c6 kconfig: handle P_SYMBOL in print_symbol() b2d00d7c61c8 kconfig: fix line numbers for if-entries in menu tree 8593080c0fcf kconfig: fix localmodconfig 2ae89c7a82ea kconfig: Avoid format overflow warning from GCC 8.1 bb6d83dde191 kbuild: Move last word of nconfig help to the previous line d6a0c8a1326b kconfig: Add testconfig into make help output 2bece88f89fa kconfig: test: add Kconfig macro language tests 915f64901eb3 kconfig: error out if a recursive variable references itself a702a6176e2f kconfig: add 'filename' and 'lineno' built-in variables 1d6272e6fe43 kconfig: add 'info', 'warning-if', and 'error-if' built-in functions 82bc8bd82e5c kconfig: expand lefthand side of assignment statement ed2a22f277c6 kconfig: support append assignment operator 1175c02506ff kconfig: support simply expanded variable 9ced3bddec08 kconfig: support user-defined function and recursively expanded variable 9de071536c87 kconfig: begin PARAM state only when seeing a command keyword 2fd5b09c201e kconfig: add 'shell' built-in function e298f3b49def kconfig: add built-in function support 137c0118a900 kconfig: make default prompt of mainmenu less specific 5b31a9746756 kconfig: remove sym_expand_string_value() 96d8e48da55a kconfig: remove string expansion for mainmenu after yyparse() bb222ceeb327 kconfig: remove string expansion in file_lookup() 104daea149c4 kconfig: reference environment variables directly and remove 'option env=' 694c49a7c01c kconfig: drop localization support 1c5af5cf9308 kconfig: refactor ncurses package checks for building mconf and nconf b464ef583dc7 kconfig: refactor GTK+ package checks for building gconf 0b669a5076fd kconfig: refactor Qt package checks for building qconf Signed-off-by: Chris Packham <judge.packham@gmail.com>
2020-12-09 08:24:45 +00:00
static void __attribute__((noreturn)) pperror(const char *format, ...)
{
va_list ap;
fprintf(stderr, "%s:%d: ", current_file->name, yylineno);
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
fprintf(stderr, "\n");
exit(1);
}
/*
* Environment variables
*/
static LIST_HEAD(env_list);
struct env {
char *name;
char *value;
struct list_head node;
};
static void env_add(const char *name, const char *value)
{
struct env *e;
e = xmalloc(sizeof(*e));
e->name = xstrdup(name);
e->value = xstrdup(value);
list_add_tail(&e->node, &env_list);
}
static void env_del(struct env *e)
{
list_del(&e->node);
free(e->name);
free(e->value);
free(e);
}
/* The returned pointer must be freed when done */
static char *env_expand(const char *name)
{
struct env *e;
const char *value;
if (!*name)
return NULL;
list_for_each_entry(e, &env_list, node) {
if (!strcmp(name, e->name))
return xstrdup(e->value);
}
value = getenv(name);
if (!value)
return NULL;
/*
* We need to remember all referenced environment variables.
* They will be written out to include/config/auto.conf.cmd
*/
env_add(name, value);
return xstrdup(value);
}
void env_write_dep(FILE *f, const char *autoconfig_name)
{
struct env *e, *tmp;
list_for_each_entry_safe(e, tmp, &env_list, node) {
fprintf(f, "ifneq \"$(%s)\" \"%s\"\n", e->name, e->value);
fprintf(f, "%s: FORCE\n", autoconfig_name);
fprintf(f, "endif\n");
env_del(e);
}
}
/*
* Built-in functions
*/
struct function {
const char *name;
unsigned int min_args;
unsigned int max_args;
char *(*func)(int argc, char *argv[]);
};
static char *do_error_if(int argc, char *argv[])
{
if (!strcmp(argv[0], "y"))
pperror("%s", argv[1]);
return NULL;
}
static char *do_filename(int argc, char *argv[])
{
return xstrdup(current_file->name);
}
static char *do_info(int argc, char *argv[])
{
printf("%s\n", argv[0]);
return xstrdup("");
}
static char *do_lineno(int argc, char *argv[])
{
char buf[16];
sprintf(buf, "%d", yylineno);
return xstrdup(buf);
}
static char *do_shell(int argc, char *argv[])
{
FILE *p;
char buf[256];
char *cmd;
size_t nread;
int i;
cmd = argv[0];
p = popen(cmd, "r");
if (!p) {
perror(cmd);
exit(1);
}
nread = fread(buf, 1, sizeof(buf), p);
if (nread == sizeof(buf))
nread--;
/* remove trailing new lines */
while (nread > 0 && buf[nread - 1] == '\n')
nread--;
buf[nread] = 0;
/* replace a new line with a space */
for (i = 0; i < nread; i++) {
if (buf[i] == '\n')
buf[i] = ' ';
}
if (pclose(p) == -1) {
perror(cmd);
exit(1);
}
return xstrdup(buf);
}
static char *do_warning_if(int argc, char *argv[])
{
if (!strcmp(argv[0], "y"))
fprintf(stderr, "%s:%d: %s\n",
current_file->name, yylineno, argv[1]);
return xstrdup("");
}
static const struct function function_table[] = {
/* Name MIN MAX Function */
{ "error-if", 2, 2, do_error_if },
{ "filename", 0, 0, do_filename },
{ "info", 1, 1, do_info },
{ "lineno", 0, 0, do_lineno },
{ "shell", 1, 1, do_shell },
{ "warning-if", 2, 2, do_warning_if },
};
#define FUNCTION_MAX_ARGS 16
static char *function_expand(const char *name, int argc, char *argv[])
{
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;
}
/*
* Variables (and user-defined functions)
*/
static LIST_HEAD(variable_list);
struct variable {
char *name;
char *value;
enum variable_flavor flavor;
int exp_count;
struct list_head node;
};
static struct variable *variable_lookup(const char *name)
{
struct variable *v;
list_for_each_entry(v, &variable_list, node) {
if (!strcmp(name, v->name))
return v;
}
return NULL;
}
static char *variable_expand(const char *name, int argc, char *argv[])
{
struct variable *v;
char *res;
v = variable_lookup(name);
if (!v)
return NULL;
if (argc == 0 && v->exp_count)
pperror("Recursive variable '%s' references itself (eventually)",
name);
if (v->exp_count > 1000)
pperror("Too deep recursive expansion");
v->exp_count++;
if (v->flavor == VAR_RECURSIVE)
res = expand_string_with_args(v->value, argc, argv);
else
res = xstrdup(v->value);
v->exp_count--;
return res;
}
void variable_add(const char *name, const char *value,
enum variable_flavor flavor)
{
struct variable *v;
char *new_value;
bool append = false;
v = variable_lookup(name);
if (v) {
/* For defined variables, += inherits the existing flavor */
if (flavor == VAR_APPEND) {
flavor = v->flavor;
append = true;
} else {
free(v->value);
}
} else {
/* For undefined variables, += assumes the recursive flavor */
if (flavor == VAR_APPEND)
flavor = VAR_RECURSIVE;
v = xmalloc(sizeof(*v));
v->name = xstrdup(name);
v->exp_count = 0;
list_add_tail(&v->node, &variable_list);
}
v->flavor = flavor;
if (flavor == VAR_SIMPLE)
new_value = expand_string(value);
else
new_value = xstrdup(value);
if (append) {
v->value = xrealloc(v->value,
strlen(v->value) + strlen(new_value) + 2);
strcat(v->value, " ");
strcat(v->value, new_value);
free(new_value);
} else {
v->value = new_value;
}
}
static void variable_del(struct variable *v)
{
list_del(&v->node);
free(v->name);
free(v->value);
free(v);
}
void variable_all_del(void)
{
struct variable *v, *tmp;
list_for_each_entry_safe(v, tmp, &variable_list, node)
variable_del(v);
}
/*
* 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, *endptr, *prev, *p;
int new_argc = 0;
char *new_argv[FUNCTION_MAX_ARGS];
int nest = 0;
int i;
unsigned long n;
tmp = xstrndup(str, len);
/*
* If variable name is '1', '2', etc. It is generally an argument
* from a user-function call (i.e. local-scope variable). If not
* available, then look-up global-scope variables.
*/
n = strtoul(tmp, &endptr, 10);
if (!*endptr && n > 0 && n <= argc) {
res = xstrdup(argv[n - 1]);
goto free_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--;
}
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);
/* Search for variables */
res = variable_expand(name, new_argc, new_argv);
if (res)
goto free;
/* 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:
free(tmp);
return res;
}
/*
* Expand a string that follows '$'
*
* For example, if the input string is
* ($(FOO)$($(BAR)))$(BAZ)
* this helper evaluates
* $($(FOO)$($(BAR)))
* and returns a new string containing the expansion (note that the string is
* recursively expanded), also advancing 'str' to point to the next character
* after the corresponding closing parenthesis, in this case, *str will be
* $(BAR)
*/
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/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.
*/
if (*p != '(') {
*str = p;
return xstrdup("$");
}
p++;
q = p;
while (*q) {
if (*q == '(') {
nest++;
} else if (*q == ')') {
if (nest-- == 0)
break;
}
q++;
}
if (!*q)
pperror("unterminated reference to '%s': missing ')'", p);
/* Advance 'str' to after the expanded initial portion of the string */
*str = q + 1;
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),
int argc, char *argv[])
{
const char *in, *p;
char *expansion, *out;
size_t in_len, out_len;
out = xmalloc(1);
*out = 0;
out_len = 1;
p = in = *str;
while (1) {
if (*p == '$') {
in_len = p - in;
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);
strcat(out, expansion);
free(expansion);
in = p;
continue;
}
if (is_end(*p))
break;
p++;
}
in_len = p - in;
out_len += in_len;
out = xrealloc(out, out_len);
strncat(out, in, in_len);
/* Advance 'str' to the end character */
*str = p;
return out;
}
static bool is_end_of_str(char c)
{
return !c;
}
/*
* 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);
}
static char *expand_string(const char *in)
kconfig: Sync with upstream v4.18 This commit introduces the following upstream changes: 73d1c580f92b kconfig: loop boundary condition fix ecd53ac2f2c6 kconfig: handle P_SYMBOL in print_symbol() b2d00d7c61c8 kconfig: fix line numbers for if-entries in menu tree 8593080c0fcf kconfig: fix localmodconfig 2ae89c7a82ea kconfig: Avoid format overflow warning from GCC 8.1 bb6d83dde191 kbuild: Move last word of nconfig help to the previous line d6a0c8a1326b kconfig: Add testconfig into make help output 2bece88f89fa kconfig: test: add Kconfig macro language tests 915f64901eb3 kconfig: error out if a recursive variable references itself a702a6176e2f kconfig: add 'filename' and 'lineno' built-in variables 1d6272e6fe43 kconfig: add 'info', 'warning-if', and 'error-if' built-in functions 82bc8bd82e5c kconfig: expand lefthand side of assignment statement ed2a22f277c6 kconfig: support append assignment operator 1175c02506ff kconfig: support simply expanded variable 9ced3bddec08 kconfig: support user-defined function and recursively expanded variable 9de071536c87 kconfig: begin PARAM state only when seeing a command keyword 2fd5b09c201e kconfig: add 'shell' built-in function e298f3b49def kconfig: add built-in function support 137c0118a900 kconfig: make default prompt of mainmenu less specific 5b31a9746756 kconfig: remove sym_expand_string_value() 96d8e48da55a kconfig: remove string expansion for mainmenu after yyparse() bb222ceeb327 kconfig: remove string expansion in file_lookup() 104daea149c4 kconfig: reference environment variables directly and remove 'option env=' 694c49a7c01c kconfig: drop localization support 1c5af5cf9308 kconfig: refactor ncurses package checks for building mconf and nconf b464ef583dc7 kconfig: refactor GTK+ package checks for building gconf 0b669a5076fd kconfig: refactor Qt package checks for building qconf Signed-off-by: Chris Packham <judge.packham@gmail.com>
2020-12-09 08:24:45 +00:00
{
return expand_string_with_args(in, 0, NULL);
}
static bool is_end_of_token(char c)
{
kconfig: Sync with upstream v5.0 This commit introduces the following upstream changes: 2648ca1859bb kconfig: clean generated *conf-cfg files d86271af6460 kconfig: rename generated .*conf-cfg to *conf-cfg ba97df45581f kbuild: use assignment instead of define ... endef for filechk_* rules a5003571e627 kconfig: remove unused "file" field of yylval union f222b7f43661 kconfig: surround dbg_sym_flags with #ifdef DEBUG to fix gconf warning 3b541978562a kconfig: split images.c out of qconf.cc/gconf.c to fix gconf warnings 9abe42371b44 kconfig: add static qualifiers to fix gconf warnings cbafbf7f551c kconfig: split the lexer out of zconf.y 558e78e3ce84 kconfig: split some C files out of zconf.y 0c874100108f kconfig: convert to SPDX License Identifier 979f2b2f7936 kconfig: remove keyword lookup table entirely 4b31a32caf0a kconfig: update current_pos in the second lexer 824fa3b3b5e3 kconfig: switch to ASSIGN_VAL state in the second lexer b3d1d9d3c362 kconfig: stop associating kconf_id with yylval caaebb3c6de3 kconfig: refactor end token rules f5451582c4e2 kconfig: stop supporting '.' and '/' in unquoted words 171a515d0803 kconfig: use T_WORD instead of T_VARIABLE for variables c3d228713b10 kconfig: use specific tokens instead of T_ASSIGN for assignments ce2164ab5831 kconfig: refactor scanning and parsing "option" properties 3c8f317d4cf1 kconfig: use distinct tokens for type and default properties a01e5d242d93 kconfig: remove redundant token defines 4b5ec81bfeda kconfig: rename depends_list to comment_option_list 1f31be9ec0a9 kconfig: loosen the order of "visible" and "depends on" in menu entry 94d4e1b6021b kconfig: remove redundant menu_block rule 4891796c6f83 kconfig: remove redundant if_block rule 2f60e46e605a kconfig: remove grammatically ambiguous option_error 6900ae9eeee3 kconfig: remove grammatically ambiguous "unexpected option" diagnostic 723679339d08 kconfig: warn no new line at end of file 0bcc547ec4b0 kconfig: clean up EOF handling in the lexer cc66bca775ee kconfig: fix ambiguous grammar in terms of new lines 21c5ecf60472 kconfig: refactor pattern matching in STRING state be3c8075978a kconfig: remove unneeded pattern matching to whitespaces 413cd19d81fd kconfig: require T_EOL to reduce visible statement fbac5977d81c kconfig: fix memory leak when EOF is encountered in quotation 77c1c0fa8b14 kconfig: fix file name and line number of warn_ignored_character() 0cbe3ac439bf kconfig: remove k_invalid from expr_parse_string() return type 2aabbed6774f kconfig: remove S_OTHER symbol type and correct dependency tracking 1508fec82e39 kconfig: split out code touching a file to conf_touch_dep() 0849d212e395 kconfig: rename conf_split_config() to conf_touch_deps() 75889e9be78f kconfig: remove unneeded setsym label in conf_read_simple() a9b722847872 scripts/kconfig/merge_config: don't redefine 'y' to 'm' Signed-off-by: Chris Packham <judge.packham@gmail.com>
2020-12-09 09:13:29 +00:00
return !(isalnum(c) || c == '_' || c == '-');
kconfig: Sync with upstream v4.18 This commit introduces the following upstream changes: 73d1c580f92b kconfig: loop boundary condition fix ecd53ac2f2c6 kconfig: handle P_SYMBOL in print_symbol() b2d00d7c61c8 kconfig: fix line numbers for if-entries in menu tree 8593080c0fcf kconfig: fix localmodconfig 2ae89c7a82ea kconfig: Avoid format overflow warning from GCC 8.1 bb6d83dde191 kbuild: Move last word of nconfig help to the previous line d6a0c8a1326b kconfig: Add testconfig into make help output 2bece88f89fa kconfig: test: add Kconfig macro language tests 915f64901eb3 kconfig: error out if a recursive variable references itself a702a6176e2f kconfig: add 'filename' and 'lineno' built-in variables 1d6272e6fe43 kconfig: add 'info', 'warning-if', and 'error-if' built-in functions 82bc8bd82e5c kconfig: expand lefthand side of assignment statement ed2a22f277c6 kconfig: support append assignment operator 1175c02506ff kconfig: support simply expanded variable 9ced3bddec08 kconfig: support user-defined function and recursively expanded variable 9de071536c87 kconfig: begin PARAM state only when seeing a command keyword 2fd5b09c201e kconfig: add 'shell' built-in function e298f3b49def kconfig: add built-in function support 137c0118a900 kconfig: make default prompt of mainmenu less specific 5b31a9746756 kconfig: remove sym_expand_string_value() 96d8e48da55a kconfig: remove string expansion for mainmenu after yyparse() bb222ceeb327 kconfig: remove string expansion in file_lookup() 104daea149c4 kconfig: reference environment variables directly and remove 'option env=' 694c49a7c01c kconfig: drop localization support 1c5af5cf9308 kconfig: refactor ncurses package checks for building mconf and nconf b464ef583dc7 kconfig: refactor GTK+ package checks for building gconf 0b669a5076fd kconfig: refactor Qt package checks for building qconf Signed-off-by: Chris Packham <judge.packham@gmail.com>
2020-12-09 08:24:45 +00:00
}
/*
* Expand variables in a token. The parsing stops when a token separater
* (in most cases, it is a whitespace) is encountered. 'str' is updated to
* point to the next character.
*
* The returned string must be freed when done.
*/
char *expand_one_token(const char **str)
{
return __expand_string(str, is_end_of_token, 0, NULL);
}