OpenCores
URL https://opencores.org/ocsvn/radiohdl/radiohdl/trunk

Subversion Repositories radiohdl

[/] [radiohdl/] [trunk/] [generic.sh] - Blame information for rev 4

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 danv
# This file contains a collection of convenience functions and definitions.
2
#
3
# automatically export to subsequent commands (-a)
4
set -a
5
#exits on any error in pipeline, not just the last error
6
set -o pipefail
7
 
8
# only set variables if we didn't set them before
9
if [ "${generic_read:-not_set}" = "not_set" ]; then
10
 
11
# display a (colourfull ...) error message.
12
#    the script will be terminated immediately
13
# exit with <errorcode> (default=1)
14
# usage:  hdl_error <caller> <message> [<errorcode>]
15
hdl_error() {
16
    caller=${1:-""}
17
    msg=${2:-""}
18
    exitcode=${3:-1}
19
    if [ -z "${caller}" -o -z "${msg}" ]; then
20
        echo "usage: hdl_error <caller's name> <message> [<exitcode>]"
21
        exit 1
22
    fi
23
    caller=`basename ${caller} | tr [a-z] [A-Z]`
24
    echo -n "$(tput setaf 6)$(tput bold)[${caller}] "
25
    echo -e "$(tput setaf 1)ERROR - ${msg}. $(tput sgr0)"
26
    # Exit if $NO_EXIT does not exist, else only return
27
    if [ -z ${NO_EXIT:-""} ]; then exit ${exitcode}; else return 1; fi
28
}
29
 
30
# Non-exiting version of hdl_error in case we wish to accumulate errors and
31
# call an exiting hdl_error after displaying accumulated errors.
32
hdl_error_noexit() {
33
    caller=${1:-""}
34
    msg=${2:-""}
35
    if [ -z "${caller}" -o -z "${msg}" ]; then
36
        echo "usage: hdl_error <caller's name> <message> [<exitcode>]"
37
        exit 1
38
    fi
39
    caller=`basename ${caller} | tr [a-z] [A-Z]`
40
    echo -n "$(tput setaf 6)$(tput bold)[${caller}] "
41
    echo -e "$(tput setaf 1)ERROR - ${msg}.$(tput sgr0)"
42
}
43
 
44
hdl_warning() {
45
    caller=${1:-""}
46
    msg=${2:-""}
47
    exitcode=${3:-1}
48
    if [ -z "${caller}" -o -z "${msg}" ]; then
49
        echo "usage: hdl_warning <caller's name> <message> [<exitcode>]"
50
        exit 1
51
    fi
52
    caller=`basename ${caller} | tr [a-z] [A-Z]`
53
    echo -n "$(tput setaf 6)$(tput bold)[${caller}] "
54
    echo -e "$(tput setaf 3)WARNING - ${msg}.$(tput sgr0)"
55
    return 0
56
}
57
 
58
 
59
# usage:  hdl_info <caller> <message>
60
hdl_info() {
61
    caller=${1:-""}
62
    shift
63
    if [ -z "${caller}" -o -z "$*" ]; then
64
        echo "usage: hdl_info <scriptname> <msg1> [<msg2> .. <msgN>]"
65
        exit 1
66
    fi
67
    caller=`basename ${caller} | tr [a-z] [A-Z]`
68
    echo -e "$(tput setaf 6)$(tput bold)[${caller}] $* $(tput sgr0)"
69
    return 0
70
}
71
 
72
# usage:
73
#   hdl_exec <calling script> [OPTS] <command to run>
74
#  OPTS:
75
#     [msg=<override defaultmsg>]
76
#           msg=no => suppress displaying of messages
77
#                     if command fails, do display the
78
#                     command that failed
79
#     [expect=<expected exit code>] (default: 0)
80
# exits with same exitcode as the command
81
hdl_exec() {
82
    # step one: extract calling scriptname, which is $1
83
    caller=$1; shift
84
    # anything left is supposedly the command to exec + args
85
    # prepare the "msg" to display
86
    msg=
87
    output=
88
    expect=0
89
    # unless someone gave msg="...." as orginal 2nd arg
90
    #  (and now, since the first "shift", it is 1st)
91
    for ac ; do
92
        case ${ac} in
93
            output=*)
94
                # well allrighty then, override default msg
95
                output=`echo "${ac}" | sed 's/^output=//'`
96
                shift
97
                ;;
98
            msg=*)
99
                # well allrighty then, override default msg
100
                msg=`echo "${ac}" | sed 's/^msg=//'`
101
                shift
102
                ;;
103
            expect=*)
104
                expect=`echo "${ac}" | sed 's/^expect=//'`
105
                shift
106
                ;;
107
            * )
108
                # first non-option argument; stop for loop!
109
                break
110
                ;;
111
        esac
112
    done
113
    if [ -z "${msg}" ]; then
114
        msg="Running \"$*\""
115
    fi
116
 
117
    # show usr what we're up to
118
    if [ "${msg}" != "no" ]; then
119
        hdl_info ${caller} "${msg}"
120
    fi
121
 
122
    # remember if errexit is switched on because we have to disable it
123
    exit_on_err=true
124
    if [ "${SHELLOPTS//*errexit*}" == "$SHELLOPTS" ]; then
125
        exit_on_err=false
126
    fi
127
    set +e
128
 
129
    # and let's actually do it!
130
    if [ "${output}" = "no" ]; then
131
      $* >/dev/null 2>/dev/null
132
    else
133
      $*
134
    fi
135
    exitcode=$?
136
 
137
    # switch errexit on if is was on when we were called (no side effects)
138
    if [ $exit_on_err ]; then
139
        set -e
140
    fi
141
    echo "exitcode=${exitcode}"
142
    # Finally check if the exitcode of the command we executed is the expected one.
143
    if [ "${exitcode}" -ne "${expect}" ]; then
144
        if [ "${msg}" == "no" ]; then
145
            echo "****** Failed command ****"
146
            echo $*
147
            exit ${exitcode}
148
        fi
149
        hdl_error ${caller} "\"${msg}\" failed" $?
150
    fi
151
}
152
 
153
# format the date in a specific form
154
# if changing the format, make sure
155
# that dateindent has the same length
156
# again (dateindent used for pretty
157
# printing multiline stuff without
158
# having to print the date in every line)
159
date="/bin/date +'%d %m %Y %T'"
160
# format    dd mm yyyy HH:MM:ss
161
dateindent='                   '
162
 
163
#
164
# Some generic, often used functions
165
#
166
 
167
# return the current date/time in a
168
# predefined format - see above
169
# Use eg as
170
# echo "`timestamp` Aaargh - Failed to clobber!"
171
timestamp() {
172
        echo ${date}
173
}
174
 
175
# Define function to add directories to a given environment variable
176
#     args: name_of_env_var new_path [new_path ...]
177
# Directories are only added when they exist.
178
pathadd() {
179
    for new_dir in ${@:2}
180
    do
181
        eval dir_to_add=`echo ${new_dir}`
182
        if [ ! -d ${dir_to_add} ]; then
183
            echo "WARNING: directory ${dir_to_add} NOT added to $1 because directory doesn't exist!"
184
        else
185
            if ! echo ${!1} | grep -E -q "(^|:)$dir_to_add($|:)" ; then
186
                eval export ${1}=${1:+${!1#:}:}${dir_to_add}
187
            fi
188
        fi
189
    done
190
    unset dir_to_add new_dir
191
}
192
 
193
# Mark the fact that we read this file and end the guarded part
194
generic_read="yes"
195
fi

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.