summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsaac David <isacdaavid@isacdaavid.info>2017-10-07 22:48:24 -0500
committerIsaac David <isacdaavid@isacdaavid.info>2017-10-07 22:48:24 -0500
commit37c8e4a16bf03509b032385482a1c13041adaf65 (patch)
tree9a65776954e680740d894d3ee367db2675389c4a
parenta5de5ff031412c0319dc292d40ee60898d90e064 (diff)
blacklist.sh: make `blacklist-normalize` more maintainable
For about the same amount of code we can save us the hassle of messing with sed expressions every time the number of fields changes in blacklist.txt. by knowing the delimiter character(s) and the number of times it's supposed to appear, a corresponding normalizing expression is constructed on the fly.
-rwxr-xr-xsrc/lib/blacklist.sh14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/lib/blacklist.sh b/src/lib/blacklist.sh
index 6c507f4..10eafef 100755
--- a/src/lib/blacklist.sh
+++ b/src/lib/blacklist.sh
@@ -2,6 +2,7 @@
# This may be included with or without `set -euE`
# Copyright (C) 2013-2014, 2016-2017 Luke Shumaker <lukeshu@sbcglobal.net>
+# Copyright (C) 2017 Isaac David <isacdaavid@isacdaavid.info>
#
# License: GNU GPLv2+
#
@@ -24,11 +25,14 @@
# Usage: blacklist-normalize <$file
# Normalizes the syntax of the blacklist on stdin.
blacklist-normalize() {
- sed -r -e '/^#/d' \
- -e 's/^[^:]*$/&::::/' \
- -e 's/^[^:]*:[^:]*$/&:::/' \
- -e 's/^[^:]*(:[^:]*){2}$/&::/' \
- -e 's/^[^:]*(:[^:]*){3}$/&:/'
+ # dynamically build sed expression based on number of fields
+ local -a expr
+ local fields=5 sep=: i
+ for (( i = $fields - 2; i >= 0; --i )); do
+ expr+=('-e' "s/^[^:]*(:[^:]*){${i}}$/&${sep}/")
+ sep+=${sep:0:1}
+ done
+ sed -r -e '/^#/d' "${expr[@]}"
}
# Usage: blacklist-cat