2 # gsu -- the global subcommand utility
3 # (C) 2006-2010 Andre Noll
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_MKDIR failed to create directory
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
47 # Check number of arguments.
49 # Usage: gsu_check_arg_count <num_given> <num1> [<num2>]
51 # Check that <num_given> is between <num1> and <num2> inclusively.
52 # If only <num1> ist given, num2 is assumed to be infinity.
55 # 0 0 no argument allowed
56 # 1 1 exactly one argument required
57 # 0 2 at most two arguments admissible
58 # 2 at least two arguments reqired
62 ret=-$E_GSU_BAD_ARG_COUNT
63 if [[ $# -eq 2 ]]; then # only num1 is given
64 result="at least $2 args required, $1 given"
65 [[ $1 -lt $2 ]] && return
70 result="need at least $2 args, $1 given"
71 [[ $1 -lt $2 ]] && return
72 result="need at most $3 args, $1 given"
73 [[ $1 -gt $3 ]] && return
76 export -f gsu_check_arg_count
82 export -f gsu_short_msg
86 gsu_short_msg "$_gsu_self: $1"
92 gsu_short_msg "$_gsu_self $(date): $1"
94 export -f gsu_date_msg
100 local txt="### $_gsu_self --"
101 if test -z "$gsu_banner_txt"; then
102 txt="$txt set \$gsu_banner_txt to customize this message"
104 txt="$txt $gsu_banner_txt"
106 gsu_short_msg "$txt ###"
108 export -f _gsu_banner_msg
112 local txt="$result" err
114 gsu_is_a_number "$ret"
115 if test $ret -lt 0; then
116 gsu_msg "unknown error ($ret:$txt)"
119 if test $result -ge 0; then
120 gsu_msg "unknown error ($result:$txt)"
124 if test -n "$txt"; then
125 txt="$txt: ${gsu_error_txt[$err]}"
127 txt="${gsu_error_txt[$err]}"
131 export -f gsu_err_msg
135 gsu_short_msg "# Usage: $_gsu_self command [options]"
139 _gsu_available_commands()
141 result="$( (printf "help\nman\nprefs\n"; grep "^com_[a-z_]\+()" $0) \
142 | sed -e 's/^com_//' -e 's/()//' \
147 export -f _gsu_available_commands
149 _gsu_print_available_commands()
152 gsu_short_msg "Available commands:"
153 for i in $gsu_cmds; do
155 count=$(($count + 1))
156 if test $(($count % 4)) -eq 0; then
160 if test ${#i} -lt 8; then
168 export -f _gsu_print_available_commands
170 export gsu_prefs_txt="
171 Print the current preferences.
175 If -e is given, the config file is opened with the default editor. Without
176 options, the command prints out a list of all cmt config variables, together
177 with their current value and the default value."
180 local i conf="${gsu_config_file:=$HOME/.$gsu_name.rc}"
182 if [[ "$1" = "-e" ]]; then
186 [[ $? -ne 0 ]] && return
188 result="${EDITOR:-vi}"
190 [[ $? -ne 0 ]] && return
195 for ((i=0; i < ${#gsu_options[@]}; i++)); do
196 local name= option_type= default_value= required=
197 local description= help_text=
198 eval "${gsu_options[$i]}"
199 eval val='"$'${gsu_config_var_prefix}_$name'"'
208 printf " $option_type: $description"
209 if [[ "$required" != "yes" && "$required" != "true" ]]; then
210 printf " [$default_value]"
213 [[ -n "$help_text" ]] && sed -e '/^[ ]*$/d; s/^[ ]*/# /g' <<< "$help_text"
215 [[ "$val" == "$default_value" ]] && printf " # default"
228 local equal_signs="=================================================="
229 local minus_signs="--------------------------------------------------"
232 echo "$_gsu_self (_${gsu_banner_txt}_) manual"
233 echo "${equal_signs:0:${#_gsu_self} + ${#gsu_banner_txt} + 16}"
236 sed -e '1,/^#\{70,\}/d' -e '/^#\{70,\}/,$d' $0 -e 's/^# *//'
239 echo "$_gsu_self usage"
240 echo "${minus_signs:0:${#_gsu_self} + 6}"
243 echo "Each command has its own set of options as described below."
247 echo "Available commands:"
249 _gsu_available_commands
250 for com in $result; do
252 if test $num -lt 4; then
255 echo "${minus_signs:0:$num}"
257 echo "${minus_signs:0:$num}"
265 export gsu_help_txt="
268 Usage: help [command]
270 Without arguments, print the list of available commands. Otherwise,
271 print the help text for the given command."
276 if test -z "$1"; then
280 printf "com_help()\n$gsu_help_txt" | head -n 4; echo "--"
281 printf "com_man()\n$gsu_man_txt" | head -n 4; echo "--"
282 printf "com_prefs()\n$gsu_prefs_txt" | head -n 4; echo "--"
283 grep -A 2 "^com_\([a-zA-Z_0-9]\+\)()" $0
284 } | grep -v -- '--' \
285 | sed -e '/^com_\([a-zA-Z_0-9]\+\)()/bs' \
286 -e 'H;$!d;x;s/\n//g;b' \
288 -e 'x;s/\n//g;${p;x;}' \
289 | sed -e 's/^com_\([a-zA-Z_0-9]\+\)()#*/\1\t/' \
293 if test ${#a} -lt 8; then
299 echo "# Try $_gsu_self help <command> for info on <command>."
303 if test "$1" = "help"; then
308 if test "$1" = "man"; then
313 if test "$1" = "prefs"; then
314 echo "$gsu_prefs_txt"
319 if grep -q "^com_$1()" $0; then
320 sed -e "1,/^com_$1()$/d" -e '/^{/,$d' -e 's/^## *//' $0
323 _gsu_print_available_commands
325 ret=-$E_GSU_BAD_COMMAND
329 # internal gsu function that syntactically checks the gsu_options array
330 # for errors and parses the config file.
333 local i conf="${gsu_config_file:=$HOME/.$gsu_name.rc}" val
335 for ((i=0; i < ${#gsu_options[@]}; i++)); do
336 eval "${gsu_options[$i]}"
337 eval val='"'\$$name'"'
338 eval orig_${gsu_config_var_prefix}_$name='"'${val}'"'
341 [[ -r "$conf" ]] && source "$conf"
343 for ((i=0; i < ${#gsu_options[@]}; i++)); do
344 local name= option_type= default_value= required=
345 local description= help_text=
348 eval "${gsu_options[$i]}"
351 # Check name. It must be non-empty and consist of [a-zA-Z_0-9]
352 # only. Moreover it must not start with [a-zA-Z].
354 ret=-$E_GSU_BAD_CONFIG_VAR
355 result="name: '$name'"
356 # bash's =~ works only for 3.2 and newer, so use grep
357 echo "$name" | grep '^[a-zA-Z][a-zA-Z_0123456789]*$' &> /dev/null;
358 [[ $? -ne 0 ]] && return
360 eval orig_val='"'\$orig_${gsu_config_var_prefix}_$name'"'
361 if [[ -z "$orig_val" ]]; then
362 eval val='"'\$$name'"'
368 ret=-$E_GSU_NEED_VALUE
370 [[ -z "$val" ]] && return
376 result="required: $required, name: $name, val: $val"
380 eval ${gsu_config_var_prefix}_$name='"'${val:=$default_value}'"'
381 # Check option type. ATM, only num and string are supported
382 # Other types may be added without breaking compatibility
383 case "$option_type" in
387 gsu_is_a_number "$val"
388 [[ $ret -lt 0 ]] && return
391 ret=-$E_GSU_BAD_OPTION_TYPE
392 result="$name/$option_type"
398 export -f _gsu_check_options
403 _gsu_self="$(basename $0)"
404 gsu_name="${gsu_name:=$_gsu_self}"
405 gsu_config_var_prefix="${gsu_config_var_prefix:=$gsu_name}"
408 if [[ "$ret" -lt 0 ]]; then
409 if [[ "$1" != "help" && "$1" != "man" ]]; then
414 _gsu_available_commands
416 if test $# -eq 0; then
418 _gsu_print_available_commands
423 # check internal commands
424 if [[ "$arg" = "help" || "$arg" = "man" || "$arg" = "prefs" ]]; then
426 if [[ "$ret" -lt 0 ]]; then
434 for i in $gsu_cmds; do
435 if test "$arg" = "$i"; then
437 if [[ "$ret" -lt 0 ]]; then
445 ret=-$E_GSU_BAD_COMMAND
448 _gsu_print_available_commands
453 # TODO: gsu_strerror: get error string