1 # gsu -- the global subcommand utility
2 # (C) 2006-2007 Andre Noll
8 E_GSU_BAD_COMMAND invalid command
9 E_GSU_NOT_A_NUMBER not a number
26 export -f gsu_init_errors
28 # check if $1 is a number
32 if test "$1" -eq "$1" &> /dev/null; then
35 ret=-$E_GSU_NOT_A_NUMBER
38 export -f gsu_is_a_number
44 export -f gsu_short_msg
48 gsu_short_msg "$gsu_self: $1"
54 gsu_short_msg "$gsu_self $(date): $1"
56 export -f gsu_date_msg
60 local txt="*** $gsu_self --"
61 if test -z "$gsu_banner_txt"; then
62 txt="$txt set \$gsu_banner_txt to customize this message"
64 txt="$txt $gsu_banner_txt"
66 gsu_short_msg "$txt ***"
68 export -f gsu_banner_msg
72 local txt="$result" err
74 gsu_is_a_number "$ret"
75 if test $ret -lt 0; then
76 gsu_msg "unknown error ($ret:$txt)"
79 if test $result -ge 0; then
80 gsu_msg "unknown error ($result:$txt)"
84 if test -n "$txt"; then
85 txt="$txt: ${gsu_error_txt[$err]}"
87 txt="${gsu_error_txt[$err]}"
89 echo "$gsu_self: $txt" 1>&2
95 gsu_short_msg "Usage: $gsu_self command [options]"
102 Usage: help [command]
104 Without arguments, print the list of available commands. Otherwise,
105 print the help text for the given command."
109 if test -z "$1"; then
113 grep -A 2 "^com_\([a-zA-Z_0-9]\+\)()" $0 \
115 | sed -e '/^com_\([a-zA-Z_0-9]\+\)()/bs' \
116 -e 'H;$!d;x;s/\n//g;b' \
118 -e 'x;s/\n//g;${p;x;}' \
119 | sed -e 's/^com_\([a-zA-Z_0-9]\+\)()#*/\1\t/' \
123 if test ${#a} -lt 8; then
129 gsu_msg "Try $gsu_self help <command> for info on <command>."
133 if test "$1" = "help"; then
134 gsu_short_msg "$gsu_help_txt"
139 if grep -q "^com_$1()" $0; then
140 sed -e "1,/com_$1()/d" -e '/^{/,$d' -e 's/^## *//' $0
143 gsu_print_available_commands
145 ret=-$E_GSU_BAD_COMMAND
149 gsu_available_commands()
151 result="$( (echo help; grep "^com_[a-z_]\+()" $0) \
152 | sed -e 's/^com_//' -e 's/()//' \
157 export -f gsu_available_commands
159 gsu_print_available_commands()
162 gsu_short_msg "Available commands:"
163 for i in $gsu_cmds; do
166 if test $((count % 4)) -eq 0; then
170 if test ${#i} -lt 8; then
178 export -f gsu_print_available_commands
182 gsu_self="$(basename $0)"
184 gsu_available_commands
186 if test $# -eq 0; then
188 gsu_print_available_commands
193 for i in $gsu_cmds; do
194 if test "$arg" = "$i"; then
196 if test $ret -lt 0; then
203 ret=-$E_GSU_BAD_COMMAND
206 gsu_print_available_commands
209 # no need to export this