summaryrefslogtreecommitdiffstats
path: root/tools/stringmangle.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-09 13:16:35 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-09 13:16:35 +0000
commite2bbf175a2184bd76f6c54ccf8456babeb1a46fc (patch)
treef0b76550d6e6f500ada964a3a4ee933a45e5a6f1 /tools/stringmangle.py
parentInitial commit. (diff)
downloadfrr-e2bbf175a2184bd76f6c54ccf8456babeb1a46fc.tar.xz
frr-e2bbf175a2184bd76f6c54ccf8456babeb1a46fc.zip
Adding upstream version 9.1.upstream/9.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/stringmangle.py')
-rw-r--r--tools/stringmangle.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/tools/stringmangle.py b/tools/stringmangle.py
new file mode 100644
index 0000000..29fcb86
--- /dev/null
+++ b/tools/stringmangle.py
@@ -0,0 +1,62 @@
+# SPDX-License-Identifier: NONE
+# 2020 by David Lamparter, placed in the public domain.
+
+import sys
+import os
+import re
+import argparse
+
+wrap_res = [
+ (re.compile(r'(?<!\\n)"\s*\n\s*"', re.M), r""),
+]
+pri_res = [
+ (re.compile(r'(PRI[udx][0-9]+)\s*\n\s*"', re.M), r'\1"'),
+ (re.compile(r'"\s*PRI([udx])32\s*"'), r"\1"),
+ (re.compile(r'"\s*PRI([udx])32'), r'\1"'),
+ (re.compile(r'"\s*PRI([udx])16\s*"'), r"h\1"),
+ (re.compile(r'"\s*PRI([udx])16'), r'h\1"'),
+ (re.compile(r'"\s*PRI([udx])8\s*"'), r"hh\1"),
+ (re.compile(r'"\s*PRI([udx])8'), r'hh\1"'),
+]
+
+
+def main():
+ argp = argparse.ArgumentParser(description="C string mangler")
+ argp.add_argument("--unwrap", action="store_const", const=True)
+ argp.add_argument("--pri8-16-32", action="store_const", const=True)
+ argp.add_argument("files", type=str, nargs="+")
+ args = argp.parse_args()
+
+ regexes = []
+ if args.unwrap:
+ regexes.extend(wrap_res)
+ if args.pri8_16_32:
+ regexes.extend(pri_res)
+ if len(regexes) == 0:
+ sys.stderr.write("no action selected to execute\n")
+ sys.exit(1)
+
+ l = 0
+
+ for fn in args.files:
+ sys.stderr.write(fn + "\033[K\r")
+ with open(fn, "r") as ifd:
+ data = ifd.read()
+
+ newdata = data
+ n = 0
+ for regex, repl in regexes:
+ newdata, m = regex.subn(repl, newdata)
+ n += m
+
+ if n > 0:
+ sys.stderr.write("changed: %s\n" % fn)
+ with open(fn + ".new", "w") as ofd:
+ ofd.write(newdata)
+ os.rename(fn + ".new", fn)
+ l += 1
+
+ sys.stderr.write("%d files changed.\n" % (l))
+
+
+main()