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]}"
88 echo "$gsu_self: $txt" 1>&2
94 gsu_short_msg "Usage: $gsu_self command [options]"
101 Usage: help [command]
103 Without arguments, print the list of available commands. Otherwise,
104 print the help text for the given command."
108 if test -z "$1"; then
112 grep -A 2 "^com_\([a-zA-Z_0-9]\+\)()" $0 \
114 | sed -e '/^com_\([a-zA-Z_0-9]\+\)()/bs' \
115 -e 'H;$!d;x;s/\n//g;b' \
117 -e 'x;s/\n//g;${p;x;}' \
118 | sed -e 's/^com_\([a-zA-Z_0-9]\+\)()#*/\1\t/' \
122 if test ${#a} -lt 8; then
128 gsu_msg "Try $gsu_self help <command> for info on <command>."
132 if test "$1" = "help"; then
133 gsu_short_msg "$gsu_help_txt"
138 if grep -q "^com_$1()" $0; then
139 sed -e "1,/com_$1()/d" -e '/^{/,$d' -e 's/^## *//' $0
142 gsu_print_available_commands
144 ret=-$E_GSU_BAD_COMMAND
148 gsu_available_commands()
150 result="$( (echo help; grep "^com_[a-z_]\+()" $0) \
151 | sed -e 's/^com_//' -e 's/()//' \
156 export -f gsu_available_commands
158 gsu_print_available_commands()
161 gsu_short_msg "Available commands:"
162 for i in $gsu_cmds; do
165 if test $((count % 4)) -eq 0; then
169 if test ${#i} -lt 8; then
177 export -f gsu_print_available_commands
181 gsu_self="$(basename $0)"
183 gsu_available_commands
185 if test $# -eq 0; then
187 gsu_print_available_commands
192 for i in $gsu_cmds; do
193 if test "$arg" = "$i"; then
195 if test $ret -lt 0; then
202 ret=-$E_GSU_BAD_COMMAND
205 gsu_print_available_commands
208 # no need to export this