Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @initialize:python@
- @@
- import re
- def fmt_parser(_st):
- # a bunch of (non-% or %%), and then (% followed by non-%).
- REG = re.compile('([^%]|%%)*(%[^%])')
- retval = {}
- pos = 0 # where to start searching for next time
- i = 0
- while True:
- match = REG.match(_st, pos)
- if match is None:
- return retval
- fmt = match.group(2)
- pos = match.end()
- idx = pos - len(fmt)
- retval[i] = { 'idx': idx, 'fmt': fmt }
- i += 1
- def fmt_replace_by_pos(_str, _idx, _fmt):
- try:
- fmts = fmt_parser(_str)
- new = _str
- if _idx == -1:
- _idx = [item for item in range(0, len(fmts))]
- for _i in _idx:
- f = fmts[_i]
- idx = f['idx']
- fmt = f['fmt']
- fmt_l = len(fmt)
- new = new[:idx] + _fmt + new[idx + fmt_l:]
- return ''.join(new)
- except Exception as e:
- print("** ERROR: Something wrong in fmt_replace_by_pos():\n {}\n".format(str(e)))
- @r1@
- format list fl;
- identifier fn;
- expression list e;
- position p;
- @@
- fn("%@fl@", e@p)
- @script:python s1@
- fl << r1.fl;
- fn << r1.fn;
- e << r1.e;
- p << r1.p;
- new_fmt;
- to_e;
- @@
- // Update the %fmt by the position (Position currently hardcode)
- new_fmt = fmt_replace_by_pos(coccinelle.fl, { 1, 3, 4 }, "%m")
- coccinelle.new_fmt = cocci.make_expr("\"{}\"".format(new_fmt))
- // Debug
- print("* Debug: {}")
- _e = str(coccinelle.e).replace(" ", "")
- print("\t->old: {}(\"{}\", {})".format(coccinelle.fn, coccinelle.fl, _e))
- print("\t->new: {}(\"{}\", {})".format(coccinelle.fn, new_fmt, _e))
- print("")
- @main depends on s1 && r1@
- format list r1.fl;
- expression s1.new_fmt;
- identifier r1.fn;
- expression list r1.e;
- expression list s1.to_e;
- position r1.p;
- //struct mydata SMD;
- //struct mydata* SMDP;
- @@
- fn(
- -"%@fl@"
- +new_fmt
- ,
- e@p
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement