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

Subversion Repositories radiohdl

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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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