summaryrefslogtreecommitdiff
path: root/sh/functions.sh.in
blob: b8d3c95d25652743e98a9f9a31480a4a4835fa61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright 2007-2008 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))
	[ "${EINFO_INDENT}" -gt 40 ] && EINFO_INDENT=40
	export EINFO_INDENT
}

eoutdent()
{
	EINFO_INDENT=$((${EINFO_INDENT:-0} - 2))
	[ "${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}
	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;;
		*) vewarn "\$${1} is not set properly"; return 1;;
	esac
}

_sanitize_path()
{
	local IFS=":" p= path=
	for p in ${PATH}; do
		case "${p}" in
			@PREFIX@/lib/rc/sbin|@PREFIX@/bin|@PREFIX@/sbin|/usr/bin|/usr/sbin|@PKG_PREFIX@/bin|@PKG_PREFIX@/sbin);;
			*) path="${path}${path:+:}${p}";;
		esac
	done

	echo "${path}"
}

# Allow our scripts to support zsh
if [ -n "${ZSH_VERSION}" ]; then
	emulate sh
	NULLCMD=:
	alias -g '${1+"$@"}'='"$@"'
	setopt NO_GLOB_SUBST
fi

# Add our bin to $PATH
export PATH="@PREFIX@/lib/rc/bin:@PREFIX@/bin:@PREFIX@/sbin:/usr/bin:/usr/sbin:@PKG_PREFIX@/bin:@PKG_PREFIX@/sbin:$(_sanitize_path "${PATH}")"
unset _sanitize_path

for arg; do
	case "${arg}" in
		--nocolor|--nocolour|-C)
			export EINFO_COLOR="NO"
			;;
	esac
done

if [ -t 1 ] && yesno "${EINFO_COLOR:-YES}"; then
	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
		eval "${_e}() { local _r; /lib/rc/bin/${_e} \"\$@\"; _r=$?; \
		export EINFO_LASTCMD=${_e}; return \$_r; }"
	done
fi