From e2bbf175a2184bd76f6c54ccf8456babeb1a46fc Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Tue, 9 Apr 2024 15:16:35 +0200 Subject: Adding upstream version 9.1. Signed-off-by: Daniel Baumann --- tools/indent.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 tools/indent.py (limited to 'tools/indent.py') diff --git a/tools/indent.py b/tools/indent.py new file mode 100755 index 0000000..fe9eb7c --- /dev/null +++ b/tools/indent.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# SPDX-License-Identifier: NONE +# 2017 by David Lamparter, placed in public domain + +import sys, re, subprocess, os + +# find all DEFUNs +defun_re = re.compile( + r"^((DEF(UN(|_ATTR|_CMD_(ELEMENT|FUNC_(DECL|TEXT))|_DEPRECATED|_NOSH|_HIDDEN|SH(|_ATTR|_DEPRECATED|_HIDDEN))?|PY|PY_ATTR|PY_HIDDEN)|ALIAS)\s*\(.*?)^(?=\s*\{)", + re.M | re.S, +) +define_re = re.compile(r"((^#\s*define[^\n]+[^\\]\n)+)", re.M | re.S) +# find clang-format control that we just inserted +clean_re = re.compile( + r"^.*/\* \$FRR indent\$ \*/\s*\n\s*/\* clang-format (on|off) \*/\s*\n", re.M +) + + +def wrap_file(fn): + with open(fn, "r") as fd: + text = fd.read() + + repl = ( + r"/* $FRR indent$ */\n/* clang-format off */\n" + + r"\1" + + r"/* $FRR indent$ */\n/* clang-format on */\n" + ) + + # around each DEFUN, insert an indent-on/off comment + text = defun_re.sub(repl, text) + text = define_re.sub(repl, text) + + ci = subprocess.Popen( + ["clang-format"], stdin=subprocess.PIPE, stdout=subprocess.PIPE + ) + stdout, ign = ci.communicate(text) + ci.wait() + if ci.returncode != 0: + raise IOError("clang-format returned %d" % (ci.returncode)) + + # remove the bits we inserted above + final = clean_re.sub("", stdout) + + tmpname = fn + ".indent" + with open(tmpname, "w") as ofd: + ofd.write(final) + os.rename(tmpname, fn) + + +if __name__ == "__main__": + for fn in sys.argv[1:]: + wrap_file(fn) -- cgit v1.2.3