From c4c738797892095209e429ea1462b6cfafe6a87f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20=C5=A0karvada?= Date: Thu, 6 Apr 2017 10:41:19 +0200 Subject: [PATCH] builtin functions: fixed parser to correctly skip quoted vars / funcs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now it skips: \${VAR} \${f:FUNC} Instead of: \${f:FUNC\} This second form is now not supported. Also simplified the parser. Signed-off-by: Jaroslav Škarvada --- tuned/profiles/functions/functions.py | 33 ++++++++++++++------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/tuned/profiles/functions/functions.py b/tuned/profiles/functions/functions.py index 68ce234..acf7077 100644 --- a/tuned/profiles/functions/functions.py +++ b/tuned/profiles/functions/functions.py @@ -32,8 +32,8 @@ class Functions(): def _curr_substr(self, _len): return self._str[self._cnt:self._cnt + _len] - def _push_pos(self): - self._stack.append(self._cnt) + def _push_pos(self, esc): + self._stack.append((esc, self._cnt)) def _sub(self, a, b, s): self._str = self._str[:a] + s + self._str[b + 1:] @@ -63,20 +63,21 @@ class Functions(): def _process(self, s): self._parse_init(s) while self._cnt < self._len: - if self._esc: - self._esc = False + if self._curr_char() == "}": + try: + si = self._stack.pop() + except IndexError: + log.error("invalid variable syntax, non pair '}' in: '%s'" % s) + return self._str + # if not escaped + if not si[0]: + self._process_func(si[1]) + elif self._curr_substr(2) == "${": + self._push_pos(self._esc) + if self._curr_char() == "\\": + self._esc = True else: - if self._curr_char() == "\\": - self._esc = True - elif self._curr_char() == "}": - try: - _from = self._stack.pop() - except IndexError: - log.error("invalid variable syntax, non pair '}' in: '%s'" % s) - return self._str - self._process_func(_from) - elif self._curr_substr(2) == "${": - self._push_pos() + self._esc = False self._cnt += 1 if len(self._stack): log.error("invalid varialbe syntax, non pair '{' in: '%s'" % s) @@ -86,4 +87,4 @@ class Functions(): if s is None or s == "": return s # expand functions and convert all \${f:*} to ${f:*} (unescape) - return re.sub(r'\\(\${f:.*\\})', r'\1', self._process(s)) + return re.sub(r'\\(\${f:.*})', r'\1', self._process(s))