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_GSU_BAD_ARG_COUNT invalid number of arguments
18 E_NO_DEFAULT missing default value
34 export -f _gsu_init_errors
36 # check if $1 is a number
40 if test "$1" -eq "$1" &> /dev/null; then
43 ret=-$E_GSU_NOT_A_NUMBER
46 export -f gsu_is_a_number
48 # Check number of arguments.
50 # Usage: gsu_check_arg_count <num_given> <num1> [<num2>]
52 # Check that <num_given> is between <num1> and <num2> inclusively.
53 # If only <num1> ist given, num2 is assumed to be infinity.
56 # 0 0 no argument allowed
57 # 1 1 exactly one argument required
58 # 0 2 at most two arguments admissible
59 # 2 at least two arguments reqired
63 ret=-$E_GSU_BAD_ARG_COUNT
64 if [[ $# -eq 2 ]]; then # only num1 is given
65 result="at least $2 args required, $1 given"
66 [[ $1 -lt $2 ]] && return
71 result="need at least $2 args, $1 given"
72 [[ $1 -lt $2 ]] && return
73 result="need at most $3 args, $1 given"
74 [[ $1 -gt $3 ]] && return
77 export -f gsu_check_arg_count
83 export -f gsu_short_msg
87 gsu_short_msg "$_gsu_self: $1"
93 gsu_short_msg "$_gsu_self $(date): $1"
95 export -f gsu_date_msg
101 local txt="*** $_gsu_self --"
102 if test -z "$gsu_banner_txt"; then
103 txt="$txt set \$gsu_banner_txt to customize this message"
105 txt="$txt $gsu_banner_txt"
107 gsu_short_msg "$txt ***"
109 export -f _gsu_banner_msg
113 local txt="$result" err
115 gsu_is_a_number "$ret"
116 if test $ret -lt 0; then
117 gsu_msg "unknown error ($ret:$txt)"
120 if test $result -ge 0; then
121 gsu_msg "unknown error ($result:$txt)"
125 if test -n "$txt"; then
126 txt="$txt: ${gsu_error_txt[$err]}"
128 txt="${gsu_error_txt[$err]}"
132 export -f gsu_err_msg
136 gsu_short_msg "Usage: $_gsu_self command [options]"
140 _gsu_available_commands()
142 result="$( (printf "help\nman\nprefs\n"; grep "^com_[a-z_]\+()" $0) \
143 | sed -e 's/^com_//' -e 's/()//' \
148 export -f _gsu_available_commands
150 _gsu_print_available_commands()
153 gsu_short_msg "Available commands:"
154 for i in $gsu_cmds; do
156 count=$(($count + 1))
157 if test $(($count % 4)) -eq 0; then
161 if test ${#i} -lt 8; then
169 export -f _gsu_print_available_commands
171 export gsu_prefs_txt="
172 Print the current preferences.
176 Print out a list of all cmt config variables, together with their current value
177 and the default value."
182 for ((i=0; i < ${#gsu_options[@]}; i++)); do
183 local name= option_type= default_value= required=
184 local description= help_text=
185 eval "${gsu_options[$i]}"
186 eval val='"$'${gsu_config_var_prefix}_$name'"'
195 printf " $option_type: $description"
196 if [[ "$required" != "yes" && "$required" != "true" ]]; then
197 printf " [$default_value]"
200 [[ -n "$help_text" ]] && sed -e '/^[ ]*$/d; s/^[ ]*/# /g' <<< "$help_text"
202 [[ "$val" == "$default_value" ]] && printf " # default"
215 local equal_signs="=================================================="
216 local minus_signs="--------------------------------------------------"
219 echo "$_gsu_self (_${gsu_banner_txt}_) manual"
220 echo "${equal_signs:0:${#_gsu_self} + ${#gsu_banner_txt} + 16}"
223 sed -e '1,/^#\{70,\}/d' -e '/^#\{70,\}/,$d' $0 -e 's/^# *//'
226 echo "$_gsu_self usage"
227 echo "${minus_signs:0:${#_gsu_self} + 6}"
230 echo "Each command has its own set of options as described below."
234 echo "Available commands:"
236 _gsu_available_commands
237 for com in $result; do
239 if test $num -lt 4; then
242 echo "${minus_signs:0:$num}"
244 echo "${minus_signs:0:$num}"
252 export gsu_help_txt="
255 Usage: help [command]
257 Without arguments, print the list of available commands. Otherwise,
258 print the help text for the given command."
263 if test -z "$1"; then
267 (printf "com_help()\n$gsu_help_txt" | head -n 4; echo "--"
268 printf "com_man()\n$gsu_man_txt" | head -n 4; echo "--"
269 printf "com_prefs()\n$gsu_prefs_txt" | head -n 4; echo "--"
271 grep -A 2 "^com_\([a-zA-Z_0-9]\+\)()" $0) \
273 | sed -e '/^com_\([a-zA-Z_0-9]\+\)()/bs' \
274 -e 'H;$!d;x;s/\n//g;b' \
276 -e 'x;s/\n//g;${p;x;}' \
277 | sed -e 's/^com_\([a-zA-Z_0-9]\+\)()#*/\1\t/' \
281 if test ${#a} -lt 8; then
287 echo "Try $_gsu_self help <command> for info on <command>."
291 if test "$1" = "help"; then
296 if test "$1" = "man"; then
301 if test "$1" = "prefs"; then
302 echo "$gsu_prefs_txt"
307 if grep -q "^com_$1()" $0; then
308 sed -e "1,/com_$1()/d" -e '/^{/,$d' -e 's/^## *//' $0
311 _gsu_print_available_commands
313 ret=-$E_GSU_BAD_COMMAND
317 # internal gsu function that syntactically checks the gsu_options array
318 # for errors and parses the config file.
321 local i conf="${gsu_config_file:=$HOME/.$gsu_name.rc}" val
323 for ((i=0; i < ${#gsu_options[@]}; i++)); do
324 eval "${gsu_options[$i]}"
325 eval val='"'\$$name'"'
326 eval orig_${gsu_config_var_prefix}_$name='"'${val}'"'
329 [[ -r "$conf" ]] && source "$conf"
331 for ((i=0; i < ${#gsu_options[@]}; i++)); do
332 local name= option_type= default_value= required=
333 local description= help_text=
336 eval "${gsu_options[$i]}"
339 # Check name. It must be non-empty and consist of [a-zA-Z_0-9]
340 # only. Moreover it must not start with [a-zA-Z].
342 ret=-$E_GSU_BAD_CONFIG_VAR
343 result="name: '$name'"
344 # bash's =~ works only for 3.2 and newer, so use grep
345 echo "$name" | grep '^[a-zA-Z][a-zA-Z_0123456789]*$' &> /dev/null;
346 [[ $? -ne 0 ]] && return
348 eval orig_val='"'\$orig_${gsu_config_var_prefix}_$name'"'
349 if [[ -z "$orig_val" ]]; then
350 eval val='"'\$$name'"'
356 ret=-$E_GSU_NEED_VALUE
358 [[ -z "$val" ]] && return
364 result="required: $required, name: $name, val: $val"
368 eval ${gsu_config_var_prefix}_$name='"'${val:=$default_value}'"'
369 # Check option type. ATM, only num and string are supported
370 # Other types may be added without breaking compatibility
371 case "$option_type" in
375 gsu_is_a_number "$val"
376 [[ $ret -lt 0 ]] && return
379 ret=-$E_GSU_BAD_OPTION_TYPE
380 result="$name/$option_type"
386 export -f _gsu_check_options
392 gsu_is_a_number "${BASH_VERSINFO[0]}"
393 if [[ $ret -lt 0 ]]; then
394 gsu_msg "fatal: failed to determine bash version"
398 if [[ "${BASH_VERSINFO[0]}" -lt 4 ]]; then
399 gsu_msg "fatal: This script requires at least bash 4.0"
402 _gsu_self="$(basename $0)"
403 gsu_name="${gsu_name:=$_gsu_self}"
404 gsu_config_var_prefix="${gsu_config_var_prefix:=$gsu_name}"
407 if [[ "$ret" -lt 0 ]]; then
408 if [[ "$1" != "help" && "$1" != "man" ]]; then
413 _gsu_available_commands
415 if test $# -eq 0; then
417 _gsu_print_available_commands
422 for i in $gsu_cmds; do
423 if test "$arg" = "$i"; then
425 if test $ret -lt 0; then
432 ret=-$E_GSU_BAD_COMMAND
435 _gsu_print_available_commands
440 # TODO: gsu_strerror: get error string