aboutsummaryrefslogtreecommitdiff
path: root/functions
diff options
context:
space:
mode:
authorVictor Lowther <victor.lowther@gmail.com>2010-07-03 11:26:31 -0500
committerVictor Lowther <victor.lowther@gmail.com>2010-07-11 20:23:42 -0500
commit350cbc9ab1b2f320bfd8b7a09bda49c8f3e7f83c (patch)
treebb67811441614fe61a77ed72bbe51986a3c87122 /functions
parent9c920cd09d1b0a9a53a4ac62981ed41431a610df (diff)
downloadinitscripts-350cbc9ab1b2f320bfd8b7a09bda49c8f3e7f83c.tar.xz
Finish bashifying functions.
Slightly simplify hook-running infrastructure. Go ahead and declare add_hook and run_hook as readonly functions to prevent hooks from overwriting them. Too bad bash does not have lexically scoped variables -- if it does, we could do the same with the hook_funcs associative array. If $CONSOLEFONT is not declared, then just return out of set_consolefont. We do this early so that the entire body of the function is not in an if block. Replace trivial use of grep with bash regex conditional. Bash has regex support, and it allows us to replace most trivial uses of sed, grep, and awk. The fewer processes we create, the faster we go, and every little bit helps. I also think it is more readable to use a bash regex for the trivial stuff. Replace if statement with parameter expansion. ${foo:+-p ${foo}} expands to nothing if foo is not set, -p $foo if foo is set. Replace slightly too long echo staement with a here document. This adds a line, but making things more readable is worth it. Make sourcing functions.d files a tiny bit shorter and faster.
Diffstat (limited to 'functions')
-rw-r--r--functions61
1 files changed, 28 insertions, 33 deletions
diff --git a/functions b/functions
index 8b4ac18..3fc6fa9 100644
--- a/functions
+++ b/functions
@@ -234,55 +234,50 @@ ck_status() {
# single_postkillall: after all processes have been killed in rc.single
# shutdown_poweroff: directly before powering off in rc.shutdown
#
-# Make sure to never override the add_hook and run_hook functions via functions.d
+# Declare add_hook and run_hook as read-only to prevent overwriting them.
+# Too bad we cannot do the same thing with hook_funcs
declare -A hook_funcs
add_hook() {
- [ -z "$1" -o -z "$2" ] && return 1
- hook_funcs["$1"]="${hook_funcs["$1"]} $2"
+ [[ $1 && $2 ]] || return 1
+ hook_funcs["$1"]+=" $2"
}
run_hook() {
- local func
-
- [ -z "$1" ] && return 1
- for func in ${hook_funcs["$1"]}; do
- ${func}
- done
+ [[ $1 ]] || return 1
+ local func
+ for func in ${hook_funcs["$1"]}; do
+ "${func}"
+ done
}
+declare -r add_hook run_hook
+
# Function for setting console font if required
set_consolefont() {
- if [ -n "$CONSOLEFONT" ]; then
- stat_busy "Loading Console Font: $CONSOLEFONT"
- #CONSOLEMAP in UTF-8 shouldn't be used
- if [ -n "$CONSOLEMAP" ] && echo "$LOCALE" | /bin/grep -qi utf ; then
- CONSOLEMAP=""
- fi
- for i in /dev/tty[0-9]*; do
- if [ -n "$CONSOLEMAP" ]; then
- /usr/bin/setfont -m $CONSOLEMAP $CONSOLEFONT -C ${i} >/dev/null 2>&1
- else
- /usr/bin/setfont $CONSOLEFONT -C ${i} >/dev/null 2>&1
- fi
- done
- if [ $? -ne 0 ]; then
- stat_fail
- else
- if [ -n "$CONSOLEMAP" ]; then
- echo 'if [ "$CONSOLE" = "" -a "$TERM" = "linux" -a -t 1 ]; then printf "\033(K"; fi' >>/etc/profile.d/locale.sh
- fi
- stat_done
- fi
+ [[ $CONSOLEFONT ]] || return 0
+ stat_busy "Loading Console Font: $CONSOLEFONT"
+ #CONSOLEMAP in UTF-8 shouldn't be used
+ [[ $CONSOLEMAP && ${LOCALE,,} =~ utf ]] && CONSOLEMAP=""
+ for i in /dev/tty[0-9]*; do
+ /usr/bin/setfont ${CONSOLEMAP:+-m ${CONSOLEMAP}} \
+ $CONSOLEFONT -C ${i} >/dev/null 2>&1
+ done
+ if (($? != 0)); then
+ stat_fail
+ elif [[ $CONSOLEMAP ]]; then
+ cat <<"EOF" >>/etc/profile.d/locale.sh
+if [ "$CONSOLE" = "" -a "$TERM" = "linux" -a -t 1 ]; then printf "\033(K"; fi
+
+EOF
+ stat_done
fi
}
# Source additional functions at the end to allow overrides
for f in /etc/rc.d/functions.d/*; do
- if [ -e $f ]; then
- . $f
- fi
+ [[ -e $f ]] && . "$f"
done
# End of file