openrc/sh/functions.sh
Roy Marples 5af58b4514 Rewrite the core parts in C. We now provide librc so other programs can
query runlevels, services and state without using bash. We also provide
libeinfo so other programs can easily use our informational functions.

As such, we have dropped the requirement of using bash as the init script
shell. We now use /bin/sh and have strived to make the scripts as portable
as possible. Shells that work are bash and dash. busybox works provided
you disable s-s-d. If you have WIPE_TMP set to yes in conf.d/bootmisc you
should disable find too.
zsh and ksh do not work at this time.

Networking support is currently being re-vamped also as it was heavily bash
array based. As such, a new config format is available like so
config_eth0="1.2.3.4/24 5.6.7.8/16"
or like so
config_eth0="'1.2.3.4 netmask 255.255.255.0' '5.6.7.8 netmask 255.255.0.0'"

We will still support the old bash array format provided that /bin/sh IS
a link it bash.

ChangeLog for baselayout-1 can be found in our SVN repo.
2007-04-05 11:18:42 +00:00

158 lines
3.3 KiB
Bash

# Copyright 1999-2007 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# Please keep this useable by every shell in portage
RC_GOT_FUNCTIONS="yes"
eindent() {
if [ -n "${RC_EBUFFER}" ] ; then
"${RC_LIBDIR}"/bin/eindent
else
RC_EINDENT=$((${RC_EINDENT:-0} + 2))
[ "${RC_EINDENT}" -gt 40 ] && RC_EINDENT=40
export RC_EINDENT
fi
}
eoutdent() {
if [ -n "${RC_EBUFFER}" ] ; then
"${RC_LIBDIR}"/bin/eoutdent
else
RC_EINDENT=$((${RC_EINDENT:-0} - 2))
[ "${RC_EINDENT}" -lt 0 ] && RC_EINDENT=0
fi
return 0
}
# void esyslog(char* priority, char* tag, char* message)
#
# use the system logger to log a message
#
esyslog() {
local pri= tag=
if [ -x /usr/bin/logger ] ; then
pri="$1"
tag="$2"
shift 2
[ -z "$*" ] && return 0
/usr/bin/logger -p "${pri}" -t "${tag}" -- "$*"
fi
return 0
}
# Safer way to list the contents of a directory,
# as it do not have the "empty dir bug".
#
# char *dolisting(param)
#
# print a list of the directory contents
#
# NOTE: quote the params if they contain globs.
# also, error checking is not that extensive ...
#
dolisting() {
local x= y= mylist= mypath="$*"
# Here we use file globbing instead of ls to save on forking
for x in ${mypath} ; do
[ ! -e "${x}" ] && continue
if [ -L "${x}" -o -f "${x}" ] ; then
mylist="${mylist} "${x}
elif [ -d "${x}" ] ; then
[ "${x%/}" != "${x}" ] && x=${x%/}
for y in "${x}"/* ; do
[ -e "${y}" ] && mylist="${mylist} ${y}"
done
fi
done
echo "${mylist# *}"
}
# bool is_older_than(reference, files/dirs to check)
#
# return 0 if any of the files/dirs are newer than
# the reference file
#
# EXAMPLE: if is_older_than a.out *.o ; then ...
is_older_than() {
local x= ref="$1"
shift
for x in "$@" ; do
[ -e "${x}" ] || continue
# We need to check the mtime if it's a directory too as the
# contents may have changed.
[ "${x}" -nt "${ref}" ] && return 0
[ -d "${x}" ] && is_older_than "${ref}" "${x}"/* && return 0
done
return 1
}
uniqify() {
local result=
while [ -n "$1" ] ; do
case " ${result} " in
*" $1 "*) ;;
*) result="${result} $1" ;;
esac
shift
done
echo "${result# *}"
}
KV_to_int() {
[ -z $1 ] && return 1
local KV_MAJOR=${1%%.*}
local x=${1#*.}
local KV_MINOR=${x%%.*}
x=${1#*.*.}
local KV_MICRO=${x%%-*}
local KV_int=$((${KV_MAJOR} * 65536 + ${KV_MINOR} * 256 + ${KV_MICRO} ))
# We make version 2.2.0 the minimum version we will handle as
# a sanity check ... if its less, we fail ...
[ "${KV_int}" -lt 131584 ] && return 1
echo "${KV_int}"
}
# Setup a basic $PATH. Just add system default to existing.
# This should solve both /sbin and /usr/sbin not present when
# doing 'su -c foo', or for something like: PATH= rcscript start
case "${PATH}" in
/lib/rcscripts/bin:/bin:/sbin:/usr/bin:/usr/sbin) ;;
/lib/rcscripts/bin:/bin:/sbin:/usr/bin:/usr/sbin:*) ;;
*) export PATH="/lib/rcscripts/bin:/bin:/sbin:/usr/bin:/usr/sbin:${PATH}" ;;
esac
for arg in "$@" ; do
case "${arg}" in
--nocolor|--nocolour)
export RC_NOCOLOR="yes"
;;
esac
done
if [ "${RC_NOCOLOR}" != "yes" -a -z "${GOOD}" ] ; then
if color_terminal ; then
GOOD=$'\e[32;01m'
WARN=$'\e[33;01m'
BAD=$'\e[31;01m'
HILITE=$'\e[36;01m'
BRACKET=$'\e[34;01m'
NORMAL=$'\e[0m'
fi
fi
# vim: set ts=4 :