2 # gsu -- the global subcommand utility
3 # (C) 2006-2007 Andre Noll
9 E_GSU_BAD_COMMAND invalid command
10 E_GSU_NOT_A_NUMBER not a number
27 export -f gsu_init_errors
29 # check if $1 is a number
33 if test "$1" -eq "$1" &> /dev/null; then
36 ret=-$E_GSU_NOT_A_NUMBER
39 export -f gsu_is_a_number
45 export -f gsu_short_msg
49 gsu_short_msg "$gsu_self: $1"
55 gsu_short_msg "$gsu_self $(date): $1"
57 export -f gsu_date_msg
61 local txt="*** $gsu_self --"
62 if test -z "$gsu_banner_txt"; then
63 txt="$txt set \$gsu_banner_txt to customize this message"
65 txt="$txt $gsu_banner_txt"
67 gsu_short_msg "$txt ***"
69 export -f gsu_banner_msg
73 local txt="$result" err
75 gsu_is_a_number "$ret"
76 if test $ret -lt 0; then
77 gsu_msg "unknown error ($ret:$txt)"
80 if test $result -ge 0; then
81 gsu_msg "unknown error ($result:$txt)"
85 if test -n "$txt"; then
86 txt="$txt: ${gsu_error_txt[$err]}"
88 txt="${gsu_error_txt[$err]}"
90 echo "$gsu_self: $txt" 1>&2
96 gsu_short_msg "Usage: $gsu_self command [options]"
100 export gsu_help_txt="
103 Usage: help [command]
105 Without arguments, print the list of available commands. Otherwise,
106 print the help text for the given command."
110 if test -z "$1"; then
114 grep -A 2 "^com_\([a-zA-Z_0-9]\+\)()" $0 \
116 | sed -e '/^com_\([a-zA-Z_0-9]\+\)()/bs' \
117 -e 'H;$!d;x;s/\n//g;b' \
119 -e 'x;s/\n//g;${p;x;}' \
120 | sed -e 's/^com_\([a-zA-Z_0-9]\+\)()#*/\1\t/' \
124 if test ${#a} -lt 8; then
130 gsu_msg "Try $gsu_self help <command> for info on <command>."
134 if test "$1" = "help"; then
135 gsu_short_msg "$gsu_help_txt"
140 if grep -q "^com_$1()" $0; then
141 sed -e "1,/com_$1()/d" -e '/^{/,$d' -e 's/^## *//' $0
144 gsu_print_available_commands
146 ret=-$E_GSU_BAD_COMMAND
150 gsu_available_commands()
152 result="$( (echo help; grep "^com_[a-z_]\+()" $0) \
153 | sed -e 's/^com_//' -e 's/()//' \
158 export -f gsu_available_commands
160 gsu_print_available_commands()
163 gsu_short_msg "Available commands:"
164 for i in $gsu_cmds; do
167 if test $((count % 4)) -eq 0; then
171 if test ${#i} -lt 8; then
179 export -f gsu_print_available_commands
183 gsu_self="$(basename $0)"
185 gsu_available_commands
187 if test $# -eq 0; then
189 gsu_print_available_commands
194 for i in $gsu_cmds; do
195 if test "$arg" = "$i"; then
197 if test $ret -lt 0; then
204 ret=-$E_GSU_BAD_COMMAND
207 gsu_print_available_commands
210 # no need to export this