aboutsummaryrefslogtreecommitdiff
blob: 6287b601846376ca6d1d1b34fa49b3c1e5e13e6c (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
#!/bin/bash

# Copyright (c) 2005 Gentoo Foundation.
# $Header$
# 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, write to the Free Software Foundation, Inc., 59 Temple
# Place, Suite 330, Boston, MA  02111-1307  USA

# 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"

    # 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)}"

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

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

# is_function function PUBLIC
# Test whether function exists
is_function() {
    [[ $(type -t "${1}" ) == "function" ]]
}

# 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
}

# 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 2

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

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

    modfile=$( ec_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
    )
}

# has varname item
has() {
    local var=${1} item
    for item in ${var} ; do
        [[ ${item} == ${2} ]] && return 0
    done
    return 1
}

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

# vim: set sw=4 et sts=4 tw=80 :