]>
Commit | Line | Data |
---|---|---|
04746079 LB |
1 | #!/usr/bin/env python |
2 | # -*- coding: utf-8 -*- | |
3 | # | |
4 | # Script used to replace deprecated quagga/frr mactors/types/etc. | |
5 | # | |
6 | # loosly based on indent.py, 2017 by David Lamparter | |
7 | # 2018 by Lou Berger, placed in public domain | |
8 | ||
9 | import sys, re, subprocess, os | |
10 | ||
701a0192 | 11 | |
04746079 | 12 | class replaceEntry: |
701a0192 | 13 | compiled = None # compiled regex |
14 | repl = None # regex | |
15 | ||
04746079 LB |
16 | def __init__(self, c, r): |
17 | self.compiled = c | |
18 | self.repl = r | |
19 | ||
701a0192 | 20 | |
04746079 LB |
21 | rList = [ |
22 | # old #define VNL, VTYNL, VTY_NEWLINE | |
701a0192 | 23 | replaceEntry(re.compile(r"(VNL|VTYNL|VTY_NEWLINE)"), r'"\\n"'), |
04746079 LB |
24 | # old #define VTY_GET_INTEGER(desc, v, str) |
25 | # old #define VTY_GET_INTEGER_RANGE(desc, v, str, min, max) | |
26 | # old #define VTY_GET_ULONG(desc, v, str) | |
701a0192 | 27 | replaceEntry( |
28 | re.compile( | |
29 | r"(VTY_GET_INTEGER(_RANGE|)|VTY_GET_ULONG)[\s\(]*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)(\s*|)(\)|,).*?;", | |
30 | re.M | re.S, | |
31 | ), | |
32 | r"(\4) = strtoul((\5), NULL, 10);\t/* \3 */", | |
33 | ), | |
04746079 | 34 | # old #define VTY_GET_ULL(desc, v, str) |
701a0192 | 35 | replaceEntry( |
36 | re.compile( | |
37 | r"VTY_GET_ULL[\s\(]*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)(\s*|)(\)|,).*?;", | |
38 | re.M | re.S, | |
39 | ), | |
40 | r"(\2) = strtoull((\3), NULL, 10);\t/* \1 */", | |
41 | ), | |
04746079 | 42 | # old #define VTY_GET_IPV4_ADDRESS(desc, v, str) |
701a0192 | 43 | replaceEntry( |
44 | re.compile( | |
45 | r"VTY_GET_IPV4_ADDRESS[\s\(]*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)(\s*|)(\)|,).*?;", | |
46 | re.M | re.S, | |
47 | ), | |
48 | r"inet_aton((\3), &(\2));\t/* \1 */", | |
49 | ), | |
04746079 | 50 | # old #define VTY_GET_IPV4_PREFIX(desc, v, str) |
701a0192 | 51 | replaceEntry( |
52 | re.compile( | |
53 | r"VTY_GET_IPV4_PREFIX[\s\(]*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)(\s*|)(\)|,).*?;", | |
54 | re.M | re.S, | |
55 | ), | |
56 | r"str2prefix_ipv4((\3), &(\2));\t/* \1 */", | |
57 | ), | |
04746079 | 58 | # old #define vty_outln(vty, str, ...) |
701a0192 | 59 | replaceEntry( |
60 | re.compile(r'vty_outln[\s\(]*(.*?)\s*,\s*(".*?"|.*?)\s*(\)|,)', re.M | re.S), | |
61 | r'vty_out(\1, \2 "\\n"\3', | |
62 | ), | |
63 | ] | |
64 | ||
04746079 LB |
65 | |
66 | def fixup_file(fn): | |
701a0192 | 67 | with open(fn, "r") as fd: |
04746079 LB |
68 | text = fd.read() |
69 | ||
70 | for re in rList: | |
701a0192 | 71 | text = re.compiled.sub(re.repl, text) |
04746079 | 72 | |
701a0192 | 73 | tmpname = fn + ".fixup" |
74 | with open(tmpname, "w") as ofd: | |
04746079 LB |
75 | ofd.write(text) |
76 | os.rename(tmpname, fn) | |
77 | ||
701a0192 | 78 | |
79 | if __name__ == "__main__": | |
04746079 LB |
80 | for fn in sys.argv[1:]: |
81 | fixup_file(fn) |