summaryrefslogtreecommitdiffstats
path: root/tools/fixup-deprecated.py
blob: ad3ae54aba2e1671f945bbd2b3d8130d068d9c8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: NONE
#
# Script used to replace deprecated quagga/frr mactors/types/etc.
#
# loosly based on indent.py, 2017 by David Lamparter
# 2018 by Lou Berger, placed in public domain

import sys, re, subprocess, os


class replaceEntry:
    compiled = None  # compiled regex
    repl = None  # regex

    def __init__(self, c, r):
        self.compiled = c
        self.repl = r


rList = [
    # old #define VNL, VTYNL, VTY_NEWLINE
    replaceEntry(re.compile(r"(VNL|VTYNL|VTY_NEWLINE)"), r'"\\n"'),
    # old #define VTY_GET_INTEGER(desc, v, str)
    # old #define VTY_GET_INTEGER_RANGE(desc, v, str, min, max)
    # old #define VTY_GET_ULONG(desc, v, str)
    replaceEntry(
        re.compile(
            r"(VTY_GET_INTEGER(_RANGE|)|VTY_GET_ULONG)[\s\(]*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)(\s*|)(\)|,).*?;",
            re.M | re.S,
        ),
        r"(\4) = strtoul((\5), NULL, 10);\t/* \3 */",
    ),
    # old #define VTY_GET_ULL(desc, v, str)
    replaceEntry(
        re.compile(
            r"VTY_GET_ULL[\s\(]*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)(\s*|)(\)|,).*?;",
            re.M | re.S,
        ),
        r"(\2) = strtoull((\3), NULL, 10);\t/* \1 */",
    ),
    # old #define VTY_GET_IPV4_ADDRESS(desc, v, str)
    replaceEntry(
        re.compile(
            r"VTY_GET_IPV4_ADDRESS[\s\(]*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)(\s*|)(\)|,).*?;",
            re.M | re.S,
        ),
        r"inet_aton((\3), &(\2));\t/* \1 */",
    ),
    # old #define VTY_GET_IPV4_PREFIX(desc, v, str)
    replaceEntry(
        re.compile(
            r"VTY_GET_IPV4_PREFIX[\s\(]*(.*?)\s*,\s*(.*?)\s*,\s*(.*?)(\s*|)(\)|,).*?;",
            re.M | re.S,
        ),
        r"str2prefix_ipv4((\3), &(\2));\t/* \1 */",
    ),
    # old #define vty_outln(vty, str, ...)
    replaceEntry(
        re.compile(r'vty_outln[\s\(]*(.*?)\s*,\s*(".*?"|.*?)\s*(\)|,)', re.M | re.S),
        r'vty_out(\1, \2 "\\n"\3',
    ),
]


def fixup_file(fn):
    with open(fn, "r") as fd:
        text = fd.read()

        for re in rList:
            text = re.compiled.sub(re.repl, text)

        tmpname = fn + ".fixup"
        with open(tmpname, "w") as ofd:
            ofd.write(text)
        os.rename(tmpname, fn)


if __name__ == "__main__":
    for fn in sys.argv[1:]:
        fixup_file(fn)