Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.54 KB | None | 0 0
  1. diff --git a/options/m_property.c b/options/m_property.c
  2. index 9dad34f..c67c651 100644
  3. --- a/options/m_property.c
  4. +++ b/options/m_property.c
  5. @@ -264,15 +264,44 @@ char *m_properties_expand_string(const struct m_property *prop_list,
  6.      int level = 0, skip_level = 0;
  7.      bstr str = bstr0(str0);
  8.  
  9. +    // Stack of output start pos for each level, where at index i is the position
  10. +    // where level i+1 starts if it needs to be re-expanded, or -1 if it doesn't.
  11. +    // e.g. for the string "${X}${+Y}", the array will be [-1] just after X starts
  12. +    // expanding, and [N] just after "${+Y}" starts expanding, where N is the
  13. +    // output size thus far just before starting to process "${+Y}".
  14. +    int *reexpand_stack = NULL;
  15. +    int reexpand_len = 0;
  16. +
  17.      while (str.len) {
  18.          if (level > 0 && bstr_eatstart0(&str, "}")) {
  19.              if (skip && level <= skip_level)
  20.                  skip = false;
  21.              level--;
  22. +
  23. +            int from = reexpand_stack[level];
  24. +            if (from >= 0) {
  25. +                // re-expand ret's suffix from "from". Effectively update ret to:
  26. +                // ret[0 .. from] + expand( ret[from .. ret_len] )
  27. +                MP_TARRAY_APPEND(NULL, ret, ret_len, '\0');
  28. +                char *re_out0 = m_properties_expand_string(prop_list,
  29. +                                                           ret + from,
  30. +                                                           ctx);
  31. +                int out_len = strlen(re_out0);
  32. +                ret = talloc_realloc(NULL, ret, char, from + out_len);
  33. +                memcpy(ret + from, re_out0, out_len);
  34. +                ret_len = from + out_len;
  35. +                talloc_free(re_out0);
  36. +            }
  37.          } else if (bstr_startswith0(str, "${") && bstr_find0(str, "}") >= 0) {
  38.              str = bstr_cut(str, 2);
  39.              level++;
  40.  
  41. +            int from = bstr_eatstart0(&str, "+") ? ret_len : -1;  // re-expand
  42. +            if (level > reexpand_len)
  43. +                MP_TARRAY_APPEND(NULL, reexpand_stack, reexpand_len, from);
  44. +            else
  45. +                reexpand_stack[level - 1] = from;
  46. +
  47.              // Assume ":" and "}" can't be part of the property name
  48.              // => if ":" comes before "}", it must be for the fallback
  49.              int term_pos = bstrcspn(str, ":}");
  50. @@ -308,6 +337,8 @@ char *m_properties_expand_string(const struct m_property *prop_list,
  51.      }
  52.  
  53.      MP_TARRAY_APPEND(NULL, ret, ret_len, '\0');
  54. +    if (reexpand_stack)
  55. +        talloc_free(reexpand_stack);
  56.      return ret;
  57.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement