5eda2e
#!/usr/bin/env python3
5eda2e
#
5eda2e
# Copyright (C) 2021 Red Hat, Inc.
5eda2e
#
5eda2e
# This library is free software; you can redistribute it and/or
5eda2e
# modify it under the terms of the GNU Lesser General Public
5eda2e
# License as published by the Free Software Foundation; either
5eda2e
# version 2.1 of the License, or (at your option) any later version.
5eda2e
#
5eda2e
# This library is distributed in the hope that it will be useful,
5eda2e
# but WITHOUT ANY WARRANTY; without even the implied warranty of
5eda2e
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5eda2e
# Lesser General Public License for more details.
5eda2e
#
5eda2e
# You should have received a copy of the GNU Lesser General Public
5eda2e
# License along with this library.  If not, see
5eda2e
# <http://www.gnu.org/licenses/>.
5eda2e
5eda2e
# About:
5eda2e
# When provided with an interface file, this script outputs a new interface
5eda2e
# file, where all original interfaces are enclosed in an ifndef statement
5eda2e
# dependent on their name. All indentation inside the interface file should
5eda2e
# be converted to tabs before running this script.
5eda2e
# 
5eda2e
# Known issues:
5eda2e
# - interface already enclosed in an ifndef statement is not ignored
5eda2e
# - "`"  and "'" inside comments are not ignored
5eda2e
# - \t is always used for indentation - may result in mixture of spaces and tabs
5eda2e
5eda2e
import sys
5eda2e
import os
5eda2e
import re
5eda2e
5eda2e
if len(sys.argv) < 1:
5eda2e
    print(("Usage: {} <policy>.if > <output>.if").format(sys.argv[0]), file=sys.stderr)
5eda2e
    exit(os.EX_USAGE)
5eda2e
5eda2e
# ending index of interface
5eda2e
end = 0
5eda2e
with open(sys.argv[1], 'r') as f:
5eda2e
    interfaces = f.read()
5eda2e
    file_len = len(interfaces)
5eda2e
5eda2e
    while end < file_len:
5eda2e
        start = interfaces.find("interface(`", end)
5eda2e
        if start < 0:
5eda2e
            #no more interfaces
5eda2e
            print(interfaces[end:], end ="")
5eda2e
            break
5eda2e
        name_end = interfaces.find("'", start+11)
5eda2e
        name = interfaces[start+11:name_end]
5eda2e
5eda2e
        # locate the end of the interface - i.e. ending apostrophe
5eda2e
        reg = re.compile(r"[`']")
5eda2e
        # skip the ' after interface name
5eda2e
        i = name_end+1
5eda2e
        # counter for the depth (` opens a new block, while ' closes it)
5eda2e
        graves = 0
5eda2e
        while i < file_len:
5eda2e
            match = reg.search(interfaces, i)
5eda2e
            if not match:
5eda2e
                print("Malformed interface: {}".format(name), file=sys.stderr)
5eda2e
                exit(1)
5eda2e
5eda2e
            i = match.end()
5eda2e
            if match.group(0) == '`':
5eda2e
                graves += 1
5eda2e
            else:
5eda2e
                graves -= 1
5eda2e
                if graves == 0:
5eda2e
                    break
5eda2e
5eda2e
        # keep everything between the end of previous interface and start of a new one
5eda2e
        print(interfaces[end:start], end ="")
5eda2e
        # interface ends in "')" -- add 1 for the edning bracket
5eda2e
        end = i+1
5eda2e
        # print the whole interface enclosed in "ifndef"
5eda2e
        print("ifndef(`{}',`".format(name))
5eda2e
        print('\n'.join([('\t' + x) if x else x for x in interfaces[start:end].split('\n')]))
5eda2e
        print("')", end ="")