openrc/sh/functions.sh.in

117 lines
2.7 KiB
Bash
Raw Normal View History

2009-04-27 02:43:26 +05:30
# Copyright 2007-2009 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.
# Allow any sh script to work with einfo functions and friends
# We also provide a few helpful functions for other programs to use
RC_GOT_FUNCTIONS="yes"
eindent()
{
EINFO_INDENT=$((${EINFO_INDENT:-0} + 2))
2009-04-27 02:43:26 +05:30
[ "$EINFO_INDENT" -gt 40 ] && EINFO_INDENT=40
export EINFO_INDENT
}
eoutdent()
{
EINFO_INDENT=$((${EINFO_INDENT:-0} - 2))
2009-04-27 02:43:26 +05:30
[ "$EINFO_INDENT" -lt 0 ] && EINFO_INDENT=0
return 0
}
yesno()
{
[ -z "$1" ] && return 1
case "$1" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) return 0;;
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) return 1;;
esac
local value=
eval value=\$${1}
2009-04-27 02:43:26 +05:30
case "$value" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) return 0;;
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) return 1;;
2009-04-27 02:43:26 +05:30
*) vewarn "\$$1 is not set properly"; return 1;;
esac
}
rc_runlevel() {
rc-status --runlevel
}
_sanitize_path()
{
2007-11-28 21:15:03 +05:30
local IFS=":" p= path=
2009-04-27 02:43:26 +05:30
for p in $PATH; do
case "$p" in
@PREFIX@/@LIB@/rc/bin|@PREFIX@/@LIB@/rc/sbin);;
@PREFIX@/bin|@PREFIX@/sbin|/usr/bin|/usr/sbin);;
@PKG_PREFIX@/bin|@PKG_PREFIX@/sbin);;
@LOCAL_PREFIX@/bin|@LOCAL_PREFIX@/sbin);;
2009-04-27 02:43:26 +05:30
*) path="$path${path:+:}$p";;
esac
done
2009-04-27 02:43:26 +05:30
echo "$path"
}
2007-07-16 21:52:37 +05:30
# Allow our scripts to support zsh
if [ -n "$ZSH_VERSION" ]; then
emulate sh
NULLCMD=:
alias -g '${1+"$@"}'='"$@"'
setopt NO_GLOB_SUBST
2007-07-16 21:52:37 +05:30
fi
# Use a special shell feature to save forking
if [ -n "$BASH_VERSION" -o "$ZSH_VERSION" ]; then
shell_var() { LC_ALL=C echo "${@//[![:word:]]/_}"; }
fi
# Make a sane PATH
2008-03-21 01:27:31 +05:30
_PREFIX=@PREFIX@
_PKG_PREFIX=@PKG_PREFIX@
_LOCAL_PREFIX=@LOCAL_PREFIX@
2008-03-21 01:27:31 +05:30
_LOCAL_PREFIX=${_LOCAL_PREFIX:-/usr/local}
2008-03-27 22:23:22 +05:30
_PATH=@PREFIX@/@LIB@/rc/bin
2009-04-27 02:43:26 +05:30
case "$_PREFIX" in
"$_PKG_PREFIX"|"$_LOCAL_PREFIX") ;;
*) _PATH="$_PATH:$_PREFIX/bin:$_PREFIX/sbin";;
2008-03-21 06:00:22 +05:30
esac
2009-04-27 02:43:26 +05:30
_PATH="$_PATH":/bin:/sbin:/usr/bin:/usr/sbin
2008-03-21 06:00:22 +05:30
2009-04-27 02:43:26 +05:30
if [ -n "$_PKG_PREFIX" ]; then
_PATH="$_PATH:$_PKG_PREFIX/bin:$_PKG_PREFIX/sbin"
fi
2009-04-27 02:43:26 +05:30
if [ -n "$_LOCAL_PREFIX" ]; then
_PATH="$_PATH:$_LOCAL_PREFIX/bin:$_LOCAL_PREFIX/sbin"
fi
2009-04-27 02:43:26 +05:30
_path="$(_sanitize_path "$PATH")"
export PATH="$_PATH${_path:+:}$_path"
unset _sanitize_path _PREFIX _PKG_PREFIX _LOCAL_PREFIX _PATH _path
2007-11-29 16:53:06 +05:30
for arg; do
2009-04-27 02:43:26 +05:30
case "$arg" in
2007-09-29 00:59:59 +05:30
--nocolor|--nocolour|-C)
export EINFO_COLOR="NO"
;;
esac
done
if [ -t 1 ] && yesno "${EINFO_COLOR:-YES}"; then
2009-04-27 02:43:26 +05:30
if [ -z "$GOOD" ]; then
eval $(eval_ecolors)
fi
else
# We need to have shell stub functions so our init scripts can remember
# the last ecmd
for _e in ebegin eend error errorn einfo einfon ewarn ewarnn ewend \
vebegin veend veinfo vewarn vewend; do
2009-04-27 02:43:26 +05:30
eval "$_e() { local _r; @PREFIX@/@LIB@/rc/bin/$_e \"\$@\"; _r=$?; \
export EINFO_LASTCMD=$_e; return \$_r; }"
done
2008-03-24 11:24:18 +05:30
unset _e
fi