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