gui: Conform to $ret and $result conventions.
[gsu.git] / 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 E_GSU_DIALOG                    dialog error
19 E_GSU_MKTEMP                    mktemp error
20 E_GSU_MENU_TREE                 invalid menu tree
21 $gsu_errors
22 "
23         local a b i=0
24         while read a b; do
25                 if test -z "$a"; then
26                         continue
27                 fi
28                 #echo "a:$a,  b: $b"
29                 gsu_error_txt[i]="$b"
30                 eval $a=$i
31                 i=$(($i + 1))
32         done << EOF
33         $gsu_errors
34 EOF
35 }
36
37 # check if $1 is a number
38 gsu_is_a_number()
39 {
40         result="$1"
41         if (("$1" == "$1")) &> /dev/null; then
42                 ret=$GSU_SUCCESS
43         else
44                 ret=-$E_GSU_NOT_A_NUMBER
45         fi
46 }
47
48 gsu_short_msg()
49 {
50         echo "$1" 1>&2
51 }
52
53 gsu_msg()
54 {
55         gsu_short_msg "$gsu_name: $1"
56 }
57
58 gsu_date_msg()
59 {
60         gsu_short_msg "$gsu_name $(date): $1"
61 }
62
63 gsu_err_msg()
64 {
65         local txt="$result" err
66
67         gsu_is_a_number "$ret"
68         if (($ret < 0)); then
69                 gsu_msg "unknown error ($ret:$txt)"
70                 exit 1
71         fi
72         if (($result >= 0)); then
73                 gsu_msg "unknown error ($result:$txt)"
74                 exit 1
75         fi
76         err=$((0 - $result))
77         if test -n "$txt"; then
78                 txt="$txt: ${gsu_error_txt[$err]}"
79         else
80                 txt="${gsu_error_txt[$err]}"
81         fi
82         gsu_msg "$txt"
83 }
84
85 _gsu_setup()
86 {
87         gsu_name="${gsu_name:-${0##*/}}"
88         gsu_config_var_prefix="${gsu_config_var_prefix:=$gsu_name}"
89         gsu_banner_txt="${gsu_banner_txt:-set \$gsu_banner_txt to customize this message}"
90         _gsu_init_errors
91 }