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
25 export -f gsu_init_errors
27 # check if $1 is a number
31 if test "$1" -eq "$1" &> /dev/null; then
34 ret=-$E_GSU_NOT_A_NUMBER
37 export -f gsu_is_a_number
43 export -f gsu_short_msg
47 gsu_short_msg "$gsu_self: $1"
53 gsu_short_msg "$gsu_self $(date): $1"
55 export -f gsu_date_msg
59 local txt="*** $gsu_self --"
60 if test -z "$gsu_banner_txt"; then
61 txt="$txt set \$gsu_banner_txt to customize this message"
63 txt="$txt $gsu_banner_txt"
65 gsu_short_msg "$txt ***"
67 export -f gsu_banner_msg
71 local txt="$result" err
73 gsu_is_a_number "$ret"
74 if test $ret -lt 0; then
75 gsu_msg "unknown error ($ret:$txt)"
78 if test $result -ge 0; then
79 gsu_msg "unknown error ($result:$txt)"
83 if test -n "$txt"; then
84 txt="$txt: ${gsu_error_txt[$err]}"
86 txt="${gsu_error_txt[$err]}"
94 gsu_short_msg "Usage: $gsu_self command [options]"
98 gsu_available_commands()
100 result="$( (printf "help\nman\n"; grep "^com_[a-z_]\+()" $0) \
101 | sed -e 's/^com_//' -e 's/()//' \
106 export -f gsu_available_commands
108 gsu_print_available_commands()
111 gsu_short_msg "Available commands:"
112 for i in $gsu_cmds; do
114 count=$(($count + 1))
115 if test $(($count % 4)) -eq 0; then
119 if test ${#i} -lt 8; then
127 export -f gsu_print_available_commands
136 local equal_signs="=================================================="
137 local minus_signs="--------------------------------------------------"
140 echo "$gsu_self (_${gsu_banner_txt}_) manual"
141 echo "${equal_signs:0:${#gsu_self} + ${#gsu_banner_txt} + 16}"
144 sed -e '1,/^#\{70,\}/d' -e '/^#\{70,\}/,$d' $0 -e 's/^# *//'
147 echo "$gsu_self usage"
148 echo "${minus_signs:0:${#gsu_self} + 6}"
151 echo "Each command has its own set of options as described below."
155 echo "Available commands:"
157 gsu_available_commands
158 for com in $result; do
160 if test $num -lt 4; then
163 echo "${minus_signs:0:$num}"
165 echo "${minus_signs:0:$num}"
173 export gsu_help_txt="
176 Usage: help [command]
178 Without arguments, print the list of available commands. Otherwise,
179 print the help text for the given command."
184 if test -z "$1"; then
188 (printf "com_help()\n$gsu_help_txt" | head -n 4; echo "--"
189 printf "com_man()\n$gsu_man_txt" | head -n 4; echo "--"
191 grep -A 2 "^com_\([a-zA-Z_0-9]\+\)()" $0) \
193 | sed -e '/^com_\([a-zA-Z_0-9]\+\)()/bs' \
194 -e 'H;$!d;x;s/\n//g;b' \
196 -e 'x;s/\n//g;${p;x;}' \
197 | sed -e 's/^com_\([a-zA-Z_0-9]\+\)()#*/\1\t/' \
201 if test ${#a} -lt 8; then
207 echo "Try $gsu_self help <command> for info on <command>."
211 if test "$1" = "help"; then
216 if test "$1" = "man"; then
222 if grep -q "^com_$1()" $0; then
223 sed -e "1,/com_$1()/d" -e '/^{/,$d' -e 's/^## *//' $0
226 gsu_print_available_commands
228 ret=-$E_GSU_BAD_COMMAND
236 gsu_self="$(basename $0)"
238 gsu_available_commands
240 if test $# -eq 0; then
242 gsu_print_available_commands
247 for i in $gsu_cmds; do
248 if test "$arg" = "$i"; then
250 if test $ret -lt 0; then
257 ret=-$E_GSU_BAD_COMMAND
260 gsu_print_available_commands
263 # no need to export this