]> git.tuebingen.mpg.de Git - gsu.git/blob - misc/gsu/common
63b2ab6f0b3cfcac825e5ba3de179761a934b81a
[gsu.git] / misc / gsu / common
1 #!/bin/bash
2 # (C) 2006-2011 Andre Noll
3
4 _gsu_init_errors()
5 {
6         gsu_errors="
7 GSU_SUCCESS                     success
8 E_GSU_BAD_COMMAND               invalid command
9 E_GSU_NOT_A_NUMBER              not a number
10 E_GSU_BAD_CONFIG_VAR            invalid config variable
11 E_GSU_NEED_VALUE                value required but not given
12 E_GSU_BAD_BOOL                  bad value for boolian option
13 E_GSU_BAD_OPTION_TYPE           invalid option type
14 E_GSU_BAD_ARG_COUNT             invalid number of arguments
15 E_GSU_EDITOR                    failed to execute editor
16 E_GSU_MKDIR                     failed to create directory
17 E_GSU_GETOPTS                   getopts error
18 $gsu_errors
19 "
20         local a b i=0
21         while read a b; do
22                 if test -z "$a"; then
23                         continue
24                 fi
25                 #echo "a:$a,  b: $b"
26                 gsu_error_txt[i]="$b"
27                 eval $a=$i
28                 i=$(($i + 1))
29         done << EOF
30         $gsu_errors
31 EOF
32 }
33 export -f _gsu_init_errors
34
35 # check if $1 is a number
36 gsu_is_a_number()
37 {
38         result="$1"
39         if test "$1" -eq "$1" &> /dev/null; then
40                 ret=$GSU_SUCCESS
41         else
42                 ret=-$E_GSU_NOT_A_NUMBER
43         fi
44 }
45 export -f gsu_is_a_number
46
47 gsu_short_msg()
48 {
49         echo "$1" 1>&2
50 }
51 export -f gsu_short_msg
52
53 gsu_msg()
54 {
55         gsu_short_msg "$_gsu_self: $1"
56 }
57 export -f gsu_msg
58
59 gsu_date_msg()
60 {
61         gsu_short_msg "$_gsu_self $(date): $1"
62 }
63 export -f gsu_date_msg
64
65 gsu_err_msg()
66 {
67         local txt="$result" err
68
69         gsu_is_a_number "$ret"
70         if test $ret -lt 0; then
71                 gsu_msg "unknown error ($ret:$txt)"
72                 exit 1
73         fi
74         if test $result -ge 0; then
75                 gsu_msg "unknown error ($result:$txt)"
76                 exit 1
77         fi
78         err=$((0 - $result))
79         if test -n "$txt"; then
80                 txt="$txt: ${gsu_error_txt[$err]}"
81         else
82                 txt="${gsu_error_txt[$err]}"
83         fi
84         gsu_msg "$txt"
85 }
86 export -f gsu_err_msg
87
88 _gsu_setup()
89 {
90         _gsu_self="$(basename $0)"
91         gsu_name="${gsu_name:=$_gsu_self}"
92         gsu_config_var_prefix="${gsu_config_var_prefix:=$gsu_name}"
93         _gsu_init_errors
94 }
95 export -f _gsu_setup