aboutsummaryrefslogtreecommitdiff
blob: 46c08bdd5c93085c2a9070531d82ea615d7a0724 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*-eselect-*-  vim: ft=eselect
# Copyright (c) 2005-2019 Gentoo Authors
#
# This file is part of the 'eselect' tools framework.
#
# eselect is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 2 of the License, or (at your option) any later
# version.
#
# eselect is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# eselect.  If not, see <http://www.gnu.org/licenses/>.

# check_do function args
# Check that function exists, and call it with args.
check_do() {
	local function=$1
	shift
	if is_function "${function}" ; then
		${function} "$@"
	else
		die "No function ${function}"
	fi
}

# die [-q] "Message" PUBLIC
# Display "Message" as an error. If -q is not provided, gives a stacktrace.
die() {
	local item funcname="" sourcefile="" lineno="" n e s="yes"

	# Restore stderr
	[[ -n ${ESELECT_STDERR} ]] && exec 2>&${ESELECT_STDERR}

	# do we have a working write_error_msg?
	if is_function "write_error_msg"; then
		e="write_error_msg"
	else
		e="echo"
	fi

	# quiet?
	if [[ $1 == "-q" ]]; then
		s=""
		shift
	fi

	$e "${@:-(no message)}" >&2

	if [[ -n ${s} ]]; then
		echo "Call stack:" >&2
		for (( n = 1; n < ${#FUNCNAME[@]}; ++n )); do
			funcname=${FUNCNAME[n]}
			sourcefile=$(basename ${BASH_SOURCE[n]})
			lineno=${BASH_LINENO[n-1]}
			echo "    * ${funcname} (${sourcefile}:${lineno})" >&2
		done
	fi

	# Evil, but effective.
	kill ${ESELECT_KILL_TARGET}
	exit 249
}

# find_module module PRIVATE
# Find module and echo its filename. Die if module doesn't exist.
find_module() {
	local modname=$1 modpath
	for modpath in "${ESELECT_MODULES_PATH[@]}"; do
		if [[ -f ${modpath}/${modname}.eselect ]]; then
			echo "${modpath}/${modname}.eselect"
			return
		fi
	done
	die -q "Can't load module ${modname}"
}

# do_action action args...
# Load and do 'action' with the specified args
do_action() {
	local action="${1##--}" modfile="" subaction="${2##--}"
	[[ -z ${action} ]] && die "Usage: do_action <action> <args>"
	shift; shift

	ESELECT_MODULE_NAME="${action}"
	ESELECT_COMMAND="${ESELECT_PROGRAM_NAME} ${ESELECT_MODULE_NAME}"

	[[ ${ESELECT_BINARY_NAME##*/} != "${ESELECT_PROGRAM_NAME}" ]] \
		&& ESELECT_COMMAND="${ESELECT_BINARY_NAME##*/}"

	modfile=$(find_module "${action}")
	(
		source "$ESELECT_DEFAULT_ACTIONS" 2>/dev/null \
			|| die "Couldn't source ${ESELECT_DEFAULT_ACTIONS}"
		source "${modfile}" 2>/dev/null \
			|| die "Couldn't source ${modfile}"
		if [[ -z ${subaction} ]]; then
			check_do "do_${DEFAULT_ACTION:-usage}" "$@"
		else
			is_function "do_${subaction}" \
				|| die -q "Action ${subaction} unknown"
			check_do "do_${subaction}" "$@"
		fi
	)
}

# inherit module PUBLIC
# Sources a given eselect library file
inherit() {
	local x
	for x; do
		[[ -e ${ESELECT_CORE_PATH}/${x}.bash ]] \
			|| die "Couldn't find ${x}.bash"
		source "${ESELECT_CORE_PATH}/${x}.bash" \
			|| die "Couldn't source ${x}.bash"
	done
}

# make eval not work, because it's evil
eval() {
	die "Don't use eval. Find another way."
}

# GNU sed wrapper (sed or gsed, as determined by configure)
sed() {
	command @SED@ "$@"
}