09330f355cc5b16d8cb0288760f3c620f92c65ef
[gsu.git] / config
1 #!/bin/bash
2
3 # Syntactically check the gsu_options array for errors and parse the config
4 # file.
5 gsu_check_options()
6 {
7         local i conf="${gsu_config_file:=${HOME:-}/.$gsu_name.rc}" val
8
9         for ((i=0; i < ${#gsu_options[@]}; i++)); do
10                 eval "${gsu_options[$i]}"
11                 eval val='"'\${${name}:-}'"'
12                 eval orig_${gsu_config_var_prefix}_$name='"'${val}'"'
13         done
14
15         [[ -r "$conf" ]] && source "$conf"
16
17         for ((i=0; i < ${#gsu_options[@]}; i++)); do
18                 local name= option_type= default_value= required=
19                 local description= help_text=
20                 local val orig_val
21
22                 eval "${gsu_options[$i]}"
23
24                 # Check name. It must be non-empty and consist of [a-zA-Z_0-9]
25                 # only.  Moreover it must not start with [a-zA-Z].
26                 ret=-$E_GSU_BAD_CONFIG_VAR
27                 result="name: '$name'"
28                 # bash's =~ works only for 3.2 and newer, so use grep
29                 echo "$name" | grep '^[a-zA-Z][a-zA-Z_0123456789]*$' &> /dev/null;
30                 [[ $? -ne 0 ]] && return
31
32                 eval orig_val='"'\$orig_${gsu_config_var_prefix}_$name'"'
33                 if [[ -z "$orig_val" ]]; then
34                         eval val='"'\${$name:-}'"'
35                 else
36                         val="$orig_val"
37                 fi
38                 case "$required" in
39                 true|yes)
40                         ret=-$E_GSU_NEED_VALUE
41                         result="$name"
42                         [[ -z "$val" ]] && return
43                         ;;
44                 false|no)
45                         ;;
46                 *)
47                         ret=-$E_GSU_BAD_BOOL
48                         result="required: $required, name: $name, val: $val"
49                         return
50                 esac
51
52                 eval ${gsu_config_var_prefix}_$name='"'\${val:="$default_value"}'"'
53                 # Check option type. ATM, only num and string are supported
54                 # Other types may be added without breaking compatibility
55                 case "$option_type" in
56                 string)
57                         ;;
58                 num)
59                         gsu_is_a_number "$val"
60                         [[ $ret -lt 0 ]] && return
61                         ;;
62                 *)
63                         ret=-$E_GSU_BAD_OPTION_TYPE
64                         result="$name/$option_type"
65                         return
66                 esac
67         done
68         ret=$GSU_SUCCESS
69 }
70
71 # Call gsu_check_options(), die on errors.
72 gsu_check_options_or_die()
73 {
74         gsu_check_options
75         if (($ret < 0)); then
76                 gsu_err_msg
77                 exit 1
78         fi
79 }
80
81 if [[ "$(type -t _gsu_setup)" != "function" ]]; then
82         gsu_dir=${gsu_dir:=${HOME:-}/.gsu}
83         . $gsu_dir/common || exit 1
84         _gsu_setup
85 fi