Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.28 KB | None | 0 0
  1. diff --git a/options/m_property.c b/options/m_property.c
  2. index 9dad34f..0d3f6b9 100644
  3. --- a/options/m_property.c
  4. +++ b/options/m_property.c
  5. @@ -263,16 +263,39 @@ char *m_properties_expand_string(const struct m_property *prop_list,
  6.      bool skip = false;
  7.      int level = 0, skip_level = 0;
  8.      bstr str = bstr0(str0);
  9. +    void *tmp = talloc_new(NULL);
  10. +
  11. +    // Stack where at index i we store -1 if level i+1 doesn't need re-expand
  12. +    // once it's done, or the output position from which we should re-expand.
  13. +    // E.g. for the string "${X}${+Y:abc${Z}}", the array will be [-1] while
  14. +    // expanding X, [N] while expanding Y, and [N, -1] while (if) expanding Z,
  15. +    // where N is the output size after expanding ${X}.
  16. +    int *reexpand_stack = NULL;
  17. +    int reexpand_len = 0;  // identical to level, kept separate for readability.
  18.  
  19.      while (str.len) {
  20.          if (level > 0 && bstr_eatstart0(&str, "}")) {
  21.              if (skip && level <= skip_level)
  22.                  skip = false;
  23.              level--;
  24. +
  25. +            int from = reexpand_stack[--reexpand_len];  // pop
  26. +            if (from >= 0)  {
  27. +                // re-expand the level's output by prepending it to the input.
  28. +                bstr new_in = bstrdup(tmp, (bstr){ret + from, ret_len - from});
  29. +                bstr_xappend(tmp, &new_in, str);
  30. +                str = new_in;
  31. +                ret_len = from;  // MP_TARRAY_APPEND can handle it cleanly.
  32. +            }
  33.          } else if (bstr_startswith0(str, "${") && bstr_find0(str, "}") >= 0) {
  34.              str = bstr_cut(str, 2);
  35.              level++;
  36.  
  37. +            // "${+" means we need to re-expand the output of the level.
  38. +            // Record the current output pos as a starting point, or -1 if no need.
  39. +            int from = bstr_eatstart0(&str, "+") ? ret_len : -1;
  40. +            MP_TARRAY_APPEND(tmp, reexpand_stack, reexpand_len, from); // push
  41. +
  42.              // Assume ":" and "}" can't be part of the property name
  43.              // => if ":" comes before "}", it must be for the fallback
  44.              int term_pos = bstrcspn(str, ":}");
  45. @@ -308,6 +331,7 @@ char *m_properties_expand_string(const struct m_property *prop_list,
  46.      }
  47.  
  48.      MP_TARRAY_APPEND(NULL, ret, ret_len, '\0');
  49. +    talloc_free(tmp);
  50.      return ret;
  51.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement