From e7cb4a591c6f54432c25cf32d339b7510587c906 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Mon, 26 Nov 2018 01:18:08 +0100 Subject: [PATCH] fill_config: fix multiple command expansions If there are multiple ${foo()} commands expanded in one iteration, there would be offset mismatches because the changed string was used with regex offsets from before the match. Re-run the regex after each change. Change-Id: I69de60d840ff3ba115c37d1ede7ccbc8879c82eb --- net/fill_config.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/net/fill_config.py b/net/fill_config.py index 665926a..e97dab7 100755 --- a/net/fill_config.py +++ b/net/fill_config.py @@ -189,9 +189,12 @@ def insert_foreach(tmpl, tmpl_dir, tmpl_src, match, local_config, arg): return ''.join(expanded) def handle_commands(tmpl, tmpl_dir, tmpl_src, local_config): - handled = 0 - for m in command_re.finditer(tmpl): - handled += 1 + while True: + # make sure to re-run the regex after each expansion to get proper string + # offsets each time + m = command_re.search(tmpl) + if not m: + break; cmd = m.group(1) arg = m.group(2) if cmd == 'include': @@ -202,6 +205,9 @@ def handle_commands(tmpl, tmpl_dir, tmpl_src, local_config): print('Error: unknown command: %r in %r' % (cmd, tmpl_src)) exit(1) + # There was some command expansion. Go again. + continue + return tmpl for tmpl_name in sorted(os.listdir(tmpl_dir)):