From 350cbc9ab1b2f320bfd8b7a09bda49c8f3e7f83c Mon Sep 17 00:00:00 2001 From: Victor Lowther Date: Sat, 3 Jul 2010 11:26:31 -0500 Subject: 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. --- functions | 61 ++++++++++++++++++++++++++++--------------------------------- 1 file changed, 28 insertions(+), 33 deletions(-) (limited to 'functions') 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 -- cgit v1.2.3