From 91a01265900c6ee6bf805a7d860f7cde86ccff08 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Thu, 23 Nov 2017 11:53:34 +0100 Subject: Improve escaping in configuration files Replace pairs of backslashes before '@' or '\@' with singles (allows escaping the escape character). Do not consume next '@' after '\@'. --- mesonbuild/mesonlib.py | 35 +++++++++++++++--------- test cases/common/16 configure file/config6.h.in | 19 +++++++++++++ test cases/common/16 configure file/meson.build | 11 ++++++++ test cases/common/16 configure file/prog6.c | 11 ++++++++ 4 files changed, 63 insertions(+), 13 deletions(-) create mode 100644 test cases/common/16 configure file/config6.h.in create mode 100644 test cases/common/16 configure file/prog6.c diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py index 56d347e..b432bf3 100644 --- a/mesonbuild/mesonlib.py +++ b/mesonbuild/mesonlib.py @@ -428,19 +428,28 @@ def do_replacement(regex, line, confdata): missing_variables = set() def variable_replace(match): - varname = match.group(1) - if varname in confdata: - (var, desc) = confdata.get(varname) - if isinstance(var, str): - pass - elif isinstance(var, int): - var = str(var) - else: - raise RuntimeError('Tried to replace a variable with something other than a string or int.') + # Pairs of escape characters before '@' or '\@' + if match.group(0).endswith('\\'): + num_escapes = match.end(0) - match.start(0) + return '\\' * (num_escapes // 2) + # Single escape character and '@' + elif match.group(0) == '\\@': + return '@' + # Template variable to be replaced else: - missing_variables.add(varname) - var = '' - return var + varname = match.group(1) + if varname in confdata: + (var, desc) = confdata.get(varname) + if isinstance(var, str): + pass + elif isinstance(var, int): + var = str(var) + else: + raise RuntimeError('Tried to replace a variable with something other than a string or int.') + else: + missing_variables.add(varname) + var = '' + return var return re.sub(regex, variable_replace, line), missing_variables def do_mesondefine(line, confdata): @@ -473,7 +482,7 @@ def do_conf_file(src, dst, confdata): raise MesonException('Could not read input file %s: %s' % (src, str(e))) # Only allow (a-z, A-Z, 0-9, _, -) as valid characters for a define # Also allow escaping '@' with '\@' - regex = re.compile(r'(? +#include + +int main(int argc, char **argv) { + return strcmp(MESSAGE1, "foo") + || strcmp(MESSAGE2, "@var1@") + || strcmp(MESSAGE3, "\\foo") + || strcmp(MESSAGE4, "\\@var1@") + || strcmp(MESSAGE5, "@var1bar") + || strcmp(MESSAGE6, "\\ @ @ \\@ \\@"); +} -- cgit v1.1