2 # gsu -- the global subcommand utility
3 # (C) 2006-2009 Andre Noll
9 E_GSU_BAD_COMMAND invalid command
10 E_GSU_NOT_A_NUMBER not a number
11 E_GSU_SOURCE error in config file
12 E_GSU_CONFIG bad/missing config file option
13 E_GSU_BAD_CONFIG_VAR invalid config variable
14 E_GSU_NEED_VALUE value required but not given
15 E_GSU_BAD_BOOL bad value for boolian option
16 E_GSU_BAD_OPTION_TYPE invalid option type
17 E_NO_DEFAULT missing default value
33 export -f _gsu_init_errors
35 # check if $1 is a number
39 if test "$1" -eq "$1" &> /dev/null; then
42 ret=-$E_GSU_NOT_A_NUMBER
45 export -f gsu_is_a_number
51 export -f gsu_short_msg
55 gsu_short_msg "$_gsu_self: $1"
61 gsu_short_msg "$_gsu_self $(date): $1"
63 export -f gsu_date_msg
67 local txt="*** $_gsu_self --"
68 if test -z "$gsu_banner_txt"; then
69 txt="$txt set \$gsu_banner_txt to customize this message"
71 txt="$txt $gsu_banner_txt"
73 gsu_short_msg "$txt ***"
75 export -f _gsu_banner_msg
79 local txt="$result" err
81 gsu_is_a_number "$ret"
82 if test $ret -lt 0; then
83 gsu_msg "unknown error ($ret:$txt)"
86 if test $result -ge 0; then
87 gsu_msg "unknown error ($result:$txt)"
91 if test -n "$txt"; then
92 txt="$txt: ${gsu_error_txt[$err]}"
94 txt="${gsu_error_txt[$err]}"
102 gsu_short_msg "Usage: $_gsu_self command [options]"
106 _gsu_available_commands()
108 result="$( (printf "help\nman\nprefs\n"; grep "^com_[a-z_]\+()" $0) \
109 | sed -e 's/^com_//' -e 's/()//' \
114 export -f _gsu_available_commands
116 _gsu_print_available_commands()
119 gsu_short_msg "Available commands:"
120 for i in $gsu_cmds; do
122 count=$(($count + 1))
123 if test $(($count % 4)) -eq 0; then
127 if test ${#i} -lt 8; then
135 export -f _gsu_print_available_commands
137 export gsu_prefs_txt="
138 Print the current preferences.
142 Print out a list of all cmt config variables, together with their current value
143 and the default value."
148 for ((i=0; i < ${#gsu_options[@]}; i++)); do
149 local name= option_type= default_value= required=
150 local description= help_text=
151 eval "${gsu_options[$i]}"
152 eval val='"$'${gsu_config_var_prefix}_$name'"'
161 printf " $option_type: $description"
162 if [[ "$required" != "yes" && "$required" != "true" ]]; then
163 printf " [$default_value]"
166 [[ -n "$help_text" ]] && sed -e '/^[ ]*$/d; s/^[ ]*/# /g' <<< "$help_text"
168 [[ "$val" == "$default_value" ]] && printf " # default"
181 local equal_signs="=================================================="
182 local minus_signs="--------------------------------------------------"
185 echo "$_gsu_self (_${gsu_banner_txt}_) manual"
186 echo "${equal_signs:0:${#_gsu_self} + ${#gsu_banner_txt} + 16}"
189 sed -e '1,/^#\{70,\}/d' -e '/^#\{70,\}/,$d' $0 -e 's/^# *//'
192 echo "$_gsu_self usage"
193 echo "${minus_signs:0:${#_gsu_self} + 6}"
196 echo "Each command has its own set of options as described below."
200 echo "Available commands:"
202 _gsu_available_commands
203 for com in $result; do
205 if test $num -lt 4; then
208 echo "${minus_signs:0:$num}"
210 echo "${minus_signs:0:$num}"
218 export gsu_help_txt="
221 Usage: help [command]
223 Without arguments, print the list of available commands. Otherwise,
224 print the help text for the given command."
229 if test -z "$1"; then
233 (printf "com_help()\n$gsu_help_txt" | head -n 4; echo "--"
234 printf "com_man()\n$gsu_man_txt" | head -n 4; echo "--"
235 printf "com_prefs()\n$gsu_prefs_txt" | head -n 4; echo "--"
237 grep -A 2 "^com_\([a-zA-Z_0-9]\+\)()" $0) \
239 | sed -e '/^com_\([a-zA-Z_0-9]\+\)()/bs' \
240 -e 'H;$!d;x;s/\n//g;b' \
242 -e 'x;s/\n//g;${p;x;}' \
243 | sed -e 's/^com_\([a-zA-Z_0-9]\+\)()#*/\1\t/' \
247 if test ${#a} -lt 8; then
253 echo "Try $_gsu_self help <command> for info on <command>."
257 if test "$1" = "help"; then
262 if test "$1" = "man"; then
267 if test "$1" = "prefs"; then
268 echo "$gsu_prefs_txt"
273 if grep -q "^com_$1()" $0; then
274 sed -e "1,/com_$1()/d" -e '/^{/,$d' -e 's/^## *//' $0
277 _gsu_print_available_commands
279 ret=-$E_GSU_BAD_COMMAND
283 # internal gsu function that syntactically checks the gsu_options array
284 # for errors and parses the config file.
287 local i conf="${gsu_config_file:=$HOME/.$gsu_name.rc}"
289 [[ -r "$conf" ]] && source "$conf"
291 for ((i=0; i < ${#gsu_options[@]}; i++)); do
292 local name= option_type= default_value= required=
293 local description= help_text=
296 eval "${gsu_options[$i]}"
299 # Check name. It must be non-empty and consist of [a-zA-Z_0-9]
300 # only. Moreover it must not start with [a-zA-Z].
302 ret=-$E_GSU_BAD_CONFIG_VAR
304 # bash's =~ works only for 3.2 and newer, so use grep
305 echo "$name" | grep '^[a-zA-Z][a-zA-Z_0123456789]*$' &> /dev/null;
306 [[ $? -ne 0 ]] && return
308 eval val='"'\$$name'"'
311 ret=-$E_GSU_NEED_VALUE
313 [[ -z "$val" ]] && return
319 result="required: $required, name: $name, val: $val"
323 eval ${gsu_config_var_prefix}_$name='"'${val:=$default_value}'"'
325 # Check option type. ATM, only num and string are supported
326 # Other types may be added without breaking compatibility
327 case "$option_type" in
331 gsu_is_a_number "$val"
332 [[ $ret -lt 0 ]] && return
335 ret=-$E_GSU_BAD_OPTION_TYPE
336 result="$name/$option_type"
342 export -f _gsu_check_options
348 _gsu_self="$(basename $0)"
349 gsu_name="${gsu_name:=$_gsu_self}"
350 gsu_config_var_prefix="${gsu_config_var_prefix:=$gsu_name}"
353 if [[ "$ret" -lt 0 ]]; then
354 if [[ "$1" != "help" && "$1" != "man" && "$1" != "prefs" ]]; then
359 _gsu_available_commands
361 if test $# -eq 0; then
363 _gsu_print_available_commands
368 for i in $gsu_cmds; do
369 if test "$arg" = "$i"; then
371 if test $ret -lt 0; then
378 ret=-$E_GSU_BAD_COMMAND
381 _gsu_print_available_commands
386 # TODO: gsu_strerror: get error string