diff options
author | Joergen Ibsen <ji@ibse.dk> | 2017-11-23 11:53:34 +0100 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2017-11-26 13:21:21 +0200 |
commit | 91a01265900c6ee6bf805a7d860f7cde86ccff08 (patch) | |
tree | 7ef97d83d7c05ba542a7817a11d753cfc5aa87fc /mesonbuild/mesonlib.py | |
parent | 325b2c25b611d1e00a753cefe38f6dd24a988575 (diff) | |
download | meson-91a01265900c6ee6bf805a7d860f7cde86ccff08.zip meson-91a01265900c6ee6bf805a7d860f7cde86ccff08.tar.gz meson-91a01265900c6ee6bf805a7d860f7cde86ccff08.tar.bz2 |
Improve escaping in configuration files
Replace pairs of backslashes before '@' or '\@' with singles (allows
escaping the escape character). Do not consume next '@' after '\@'.
Diffstat (limited to 'mesonbuild/mesonlib.py')
-rw-r--r-- | mesonbuild/mesonlib.py | 35 |
1 files changed, 22 insertions, 13 deletions
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'(?<!\\)@([-a-zA-Z0-9_]+)@') + regex = re.compile(r'(?:\\\\)+(?=\\?@)|\\@|@([-a-zA-Z0-9_]+)@') result = [] missing_variables = set() for line in data: |