mirror of
git://git.code.sf.net/p/cdesktopenv/code
synced 2025-02-13 11:42:21 +00:00
Now that the Make Abstract Machine files are maintained manually and not generated automatically, unused variables are an annoying distraction -- and there are many. But the language/format is very simple and very parseable using shell, awk, etc. -- so this was easy to automate. All variables are declared with 'setv' and they are used if an expansion of the form ${varname} exists (the braces are mandatory in Mamfiles). bin/Mamfile_rm_unused_vars: - Added for reference and future use. src/*/*/Mamfile: - Remove all unused 'setv' variable declarations.
30 lines
989 B
Bash
Executable file
30 lines
989 B
Bash
Executable file
#!/usr/bin/env sh
|
|
IFS=''; set -fCu # safe mode: no split/glob = no quoting headaches
|
|
CCn='
|
|
' # newline
|
|
let() { return $((!($1))); }
|
|
|
|
# Remove unused variable definitions from a Mamfile.
|
|
# Usage: Mamfile_rm_unused_vars <Mamfile >Mamfile.new
|
|
#
|
|
# Should work on all current POSIX compliant shells.
|
|
# By Martijn Dekker <martijn@inlv.org>, 2021. Public domain.
|
|
#
|
|
# All variables are declared with 'setv' and they are used if an expansion
|
|
# of the form ${varname} exists (the braces are mandatory in Mamfiles).
|
|
|
|
mamfile=$(let $# && cat "$1" || cat)
|
|
vars=$(printf '%s\n' $mamfile | awk '$1 == "setv" { print $2; }')
|
|
rm_unused_ere=
|
|
IFS=$CCn; for varname in $vars; do IFS=
|
|
case $mamfile in
|
|
*"\${$varname}"* )
|
|
;;
|
|
* ) # add with '|' separator for Extended Regular Expression
|
|
rm_unused_ere="${rm_unused_ere:+$rm_unused_ere|}setv[[:blank:]]+$varname([[:blank:]]|$)" ;;
|
|
esac
|
|
done
|
|
case $rm_unused_ere in
|
|
'') printf '%s\n' $mamfile ;;
|
|
*) printf '%s\n' $mamfile | grep -vE $rm_unused_ere ;;
|
|
esac
|