Add home page link.
[gsu.git] / common
1 #!/bin/bash
2 # Copyright (C) 2006 Andre Noll
3 # Licensed under the LGPL, version 3. See COPYING and COPYING.LESSER.
4
5 _gsu_init_errors()
6 {
7         gsu_errors="
8 GSU_SUCCESS                     success
9 E_GSU_BAD_COMMAND               invalid command
10 E_GSU_NOT_A_NUMBER              not a number
11 E_GSU_BAD_CONFIG_VAR            invalid config variable
12 E_GSU_NEED_VALUE                value required but not given
13 E_GSU_BAD_BOOL                  bad value for boolian option
14 E_GSU_BAD_OPTION_TYPE           invalid option type
15 E_GSU_BAD_ARG_COUNT             invalid number of arguments
16 E_GSU_EDITOR                    failed to execute editor
17 E_GSU_INVAL                     invalid argument
18 E_GSU_MKDIR                     failed to create directory
19 E_GSU_GETOPTS                   getopts error
20 E_GSU_DIALOG                    dialog error
21 E_GSU_MKTEMP                    mktemp error
22 E_GSU_MENU_TREE                 invalid menu tree
23 E_GSU_XCMD                      external command failed
24 ${gsu_errors:-}
25 "
26         local a b i=0
27         while read a b; do
28                 if test -z "$a"; then
29                         continue
30                 fi
31                 #echo "a:$a,  b: $b"
32                 gsu_error_txt[i]="$b"
33                 eval $a=$i
34                 i=$((i + 1))
35         done << EOF
36         $gsu_errors
37 EOF
38 }
39
40 # check if $1 is a number
41 gsu_is_a_number()
42 {
43         result="$1"
44         if [ "$1" -eq "$1" ] &> /dev/null; then
45                 ret=$GSU_SUCCESS
46         else
47                 ret=-$E_GSU_NOT_A_NUMBER
48         fi
49 }
50
51 gsu_short_msg()
52 {
53         echo "$1" 1>&2
54 }
55
56 gsu_msg()
57 {
58         gsu_short_msg "$gsu_name: $1"
59 }
60
61 gsu_date_msg()
62 {
63         gsu_short_msg "$gsu_name $(date): $1"
64 }
65
66 gsu_err_msg()
67 {
68         local txt="$result" err
69
70         gsu_is_a_number "$ret"
71         if ((ret < 0)); then
72                 gsu_msg "unknown error ($ret:$txt)"
73                 exit 1
74         fi
75         if ((result >= 0)); then
76                 gsu_msg "unknown error ($result:$txt)"
77                 exit 1
78         fi
79         err=$((0 - result))
80         if test -n "$txt"; then
81                 txt="$txt: ${gsu_error_txt[$err]}"
82         else
83                 txt="${gsu_error_txt[$err]}"
84         fi
85         gsu_msg "$txt"
86 }
87
88 _gsu_setup()
89 {
90         gsu_name="${gsu_name:-${0##*/}}"
91         gsu_config_var_prefix="${gsu_config_var_prefix:=$gsu_name}"
92         gsu_banner_txt="${gsu_banner_txt:-set \$gsu_banner_txt to customize this message}"
93         _gsu_init_errors
94 }
95
96 # We'd love to use mktemp -t here, but on Linux -t is deprecated in favor of
97 # --tempdir, which is not supported on *BSD. Hence we have to implement our own
98 # logic for -t.
99 #
100 # The second parameter to this function is optional. It is ignored if the
101 # template ($1) is an absolute path. Conversely, if the template is a relative
102 # path and a second parameter is given, $2 is assumed to be the directory in
103 # which the temporary file should be created.
104 gsu_make_tempfile()
105 {
106         local template="$1"
107         local dir
108
109         if [[ "${template:0:1}" != '/' ]]; then # relative path
110                 if (($# > 1)); then
111                         dir="$2"
112                 elif [[ -n "${TMPDIR:-}" ]]; then
113                         dir="$TMPDIR"
114                 else
115                         dir="/tmp"
116                 fi
117                 template="$dir/$template"
118         fi
119         result="$(mktemp "$template")"
120         if (($? != 0)); then
121                 ret=-$E_GSU_MKTEMP
122                 result="template: $template"
123                 return
124         fi
125         ret=$GSU_SUCCESS
126 }