]> git.tuebingen.mpg.de Git - gsu.git/blobdiff - funcs/gsu
gsu: Remove unused variable "tilde_signs".
[gsu.git] / funcs / gsu
index 0403039f90661a3ac36ce534c5e8befff37e5a4b..37092a3985ee835768dbfc5ec7d4fd2fcd68f572 100644 (file)
--- a/funcs/gsu
+++ b/funcs/gsu
@@ -1,4 +1,3 @@
-#!/bin/bash
 # gsu -- the global subcommand utility
 # (C) 2006-2007 Andre Noll
 
@@ -10,8 +9,7 @@ E_GSU_BAD_COMMAND              invalid command
 E_GSU_NOT_A_NUMBER             not a number
 $gsu_errors
 "
-       local a b i
-       local i=0
+       local a b i=0
        while read a b; do
                if test -z "$a"; then
                        continue
@@ -19,7 +17,7 @@ $gsu_errors
                #echo "a:$a,  b: $b"
                gsu_error_txt[i]="$b"
                eval $a=$i
-               i=$((i + 1))
+               i=$(($i + 1))
        done << EOF
        $gsu_errors
 EOF
@@ -87,7 +85,7 @@ gsu_err_msg()
        else
                txt="${gsu_error_txt[$err]}"
        fi
-        echo "$gsu_self: $txt" 1>&2
+       gsu_msg "$txt"
 }
 export -f gsu_err_msg
 
@@ -97,6 +95,81 @@ gsu_usage()
 }
 export -f gsu_usage
 
+gsu_available_commands()
+{
+       result="$( (printf "help\nman\n"; grep "^com_[a-z_]\+()" $0) \
+               | sed -e 's/^com_//' -e 's/()//' \
+               | sort \
+               | tr '\n' ' ')"
+       ret=$GSU_SUCCESS
+}
+export -f gsu_available_commands
+
+gsu_print_available_commands()
+{(
+       local i count
+       gsu_short_msg "Available commands:"
+       for i in $gsu_cmds; do
+               printf "$i"
+               count=$(($count + 1))
+               if test $(($count % 4)) -eq 0; then
+                       echo
+               else
+                       printf "\t"
+                       if test ${#i} -lt 8; then
+                               printf "\t"
+                       fi
+               fi
+       done
+       echo
+) 2>&1
+}
+export -f gsu_print_available_commands
+
+export gsu_man_txt="
+Print the manual.
+
+Usage: man"
+
+com_man()
+{
+       local equal_signs="=================================================="
+       local minus_signs="--------------------------------------------------"
+        local com num
+
+       echo "$gsu_self (_${gsu_banner_txt}_) manual"
+       echo "${equal_signs:0:${#gsu_self} + ${#gsu_banner_txt} + 16}"
+       echo
+
+        sed -e '1,/^#\{70,\}/d' -e '/^#\{70,\}/,$d' $0 -e 's/^# *//'
+       echo "----"
+        echo
+       echo "$gsu_self usage"
+       echo "${minus_signs:0:${#gsu_self} + 6}"
+       printf "\t"
+       gsu_usage 2>&1
+       echo "Each command has its own set of options as described below."
+       echo
+       echo "----"
+       echo
+       echo "Available commands:"
+
+       gsu_available_commands
+        for com in $result; do
+               num=${#com}
+               if test $num -lt 4; then
+                       num=4
+               fi
+                echo "${minus_signs:0:$num}"
+                echo "$com"
+                echo "${minus_signs:0:$num}"
+                $0 help $com
+               echo
+        done
+        ret=$GSU_SUCCESS
+}
+export -f com_man
+
 export gsu_help_txt="
 Print online help.
 
@@ -104,14 +177,18 @@ Usage: help [command]
 
 Without arguments, print the list of available commands. Otherwise,
 print the help text for the given command."
+
 com_help()
 {
        local a b
        if test -z "$1"; then
-               gsu_banner_msg
-               gsu_usage
+               gsu_banner_msg 2>&1
+               gsu_usage 2>&1
                # sed is magic, baby
-               grep -A 2 "^com_\([a-zA-Z_0-9]\+\)()" $0 \
+               (printf "com_help()\n$gsu_help_txt" | head -n 4; echo "--"
+               printf "com_man()\n$gsu_man_txt" | head -n 4; echo "--"
+
+               grep -A 2 "^com_\([a-zA-Z_0-9]\+\)()" $0) \
                        | grep -v -- '--' \
                        | sed -e '/^com_\([a-zA-Z_0-9]\+\)()/bs' \
                                -e 'H;$!d;x;s/\n//g;b' \
@@ -120,19 +197,24 @@ com_help()
                        | sed -e 's/^com_\([a-zA-Z_0-9]\+\)()#*/\1\t/' \
                        | sort \
                        | while read a b; do
-                                echo -en "$a\t"
-                                if test ${#a} -lt 8; then
-                                        echo -en "\t"
-                                fi
-                                echo "$b"
+                               printf "$a\t"
+                               if test ${#a} -lt 8; then
+                                       printf "\t"
+                               fi
+                               echo "$b"
                         done
                echo
-               gsu_msg "Try $0 help <command> for info on <command>."
+               echo "Try $gsu_self help <command> for info on <command>."
                ret=$GSU_SUCCESS
                return
        fi
        if test "$1" = "help"; then
-               gsu_short_msg "$gsu_help_txt"
+               echo "$gsu_help_txt"
+               ret=$GSU_SUCCESS
+               return
+       fi
+       if test "$1" = "man"; then
+               echo "$gsu_man_txt"
                ret=$GSU_SUCCESS
                return
        fi
@@ -147,39 +229,10 @@ com_help()
 }
 export -f com_help
 
-gsu_available_commands()
-{
-       result="$( (echo help; grep "^com_[a-z_]\+()" $0) \
-               | sed -e 's/^com_//' -e 's/()//' \
-               | sort \
-               | tr '\n' ' ')"
-       ret=$SUCCESS
-}
-export -f gsu_available_commands
-
-gsu_print_available_commands()
-{(
-       local i count
-       gsu_short_msg "Available commands:"
-       for i in $gsu_cmds; do
-               printf "$i"
-               count=$((count + 1))
-               if test $((count % 4)) -eq 0; then
-                       echo
-               else
-                       printf "\t"
-                       if test ${#i} -lt 8; then
-                               printf "\t"
-                       fi
-               fi
-       done
-       echo
-) 2>&1
-}
-export -f gsu_print_available_commands
-
 gsu()
 {
+       local i
+
        gsu_self="$(basename $0)"
        gsu_init_errors
        gsu_available_commands
@@ -193,7 +246,7 @@ gsu()
        shift
        for i in $gsu_cmds; do
                if test "$arg" = "$i"; then
-                       com_$arg $*
+                       com_$arg "$@"
                        if test $ret -lt 0; then
                                gsu_err_msg
                                exit 1