aboutsummaryrefslogtreecommitdiff
path: root/util/style.sh
diff options
context:
space:
mode:
authorMikhail Romanko <me@blankhex.com>2025-03-11 09:49:43 +0300
committerMikhail Romanko <me@blankhex.com>2025-04-05 13:56:00 +0300
commit69515af77f36bad1f501f3145eba4436e8216965 (patch)
treeb8762c125b848df320f8022c7192e67b672ccce0 /util/style.sh
parent80094983311f853ed105f7f48f51c8e9363f03a4 (diff)
downloadbhlib-69515af77f36bad1f501f3145eba4436e8216965.tar.gz
Fix styling issues, rename scripts
Diffstat (limited to 'util/style.sh')
-rwxr-xr-xutil/style.sh77
1 files changed, 77 insertions, 0 deletions
diff --git a/util/style.sh b/util/style.sh
new file mode 100755
index 0000000..c892f7d
--- /dev/null
+++ b/util/style.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+
+# Check args
+if [ $# -lt 1 ]; then
+ echo "error: specify file"
+ exit 1
+fi
+
+if [ ! -f $1 ]; then
+ echo "error: file '$1' not found"
+ exit 1
+fi
+
+
+# Message about trailing space before EOL
+eolerrors=$(grep -n "\s$" "$1" | cut -f1 -d: | while read -r line; do
+ if [ "$line" ]; then
+ echo "$1:$line: error: whitepace before EOL"
+ fi
+done)
+
+
+# Message about non-empty last-line
+linecount=$(($(wc -l "$1" | cut -f1 -d " ") + 1))
+lastline=$(tail -n "+$linecount" $1)
+
+llerrors=$(if [ "$lastline" ]; then
+ echo "$1:$linecount: error: no EOL before EOF"
+fi)
+
+
+# Compute ranges of new lines
+count=0; start=0; next=0
+lines="$(grep -n "^$" "$1" | cut -f1 -d:)
+0"
+ranges=$(echo "$lines" | while read -r line; do
+ if [ "$line" ]; then
+ if [ $start -eq 0 ]; then
+ start="$line"; count=1; next=$(($start + 1))
+ elif [ "$line" -ne $next ]; then
+ echo $start $count
+ start="$line"; count=1; next=$(($start + 1))
+ else
+ count=$(($count + 1)); next=$(($next + 1))
+ fi
+ fi
+done)
+
+
+# Check that we have no more then 3 EOLs in the row
+fewerrors=$(echo "$ranges" | while read -r line; do
+ number=$(echo $line | cut -f1 -d " ")
+ size=$(echo $line | cut -f2 -d " ")
+ if [ $size -gt 2 ]; then
+ echo "$1:$number: error: too many EOLs"
+ fi
+done)
+
+# Print out errors
+code=0
+if [ "$eolerrors" ]; then
+ echo "$eolerrors" 1>&2
+ code=1
+fi
+
+if [ "$llerrors" ]; then
+ echo "$llerrors" 1>&2
+ code=1
+fi
+
+if [ "$fewerrors" ]; then
+ echo "$fewerrors" 1>&2
+ code=1
+fi
+
+exit $code