chiark / gitweb /
keyfunc.sh.in: Infrastructure for built-in subcommands.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 15 Feb 2012 00:45:09 +0000 (00:45 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 23 Feb 2012 03:14:37 +0000 (03:14 +0000)
This is the `defcmd' machinery from elsewhere, in yet another guise.

cryptop.in
keyfunc.sh.in
keys.in

index 064b595..75a553b 100755 (executable)
@@ -31,7 +31,7 @@ export ETC KEYS KEYSLIB
 
 . "$KEYSLIB"/keyfunc.sh
 
-usage="usage: $quis COMMAND [ARGUMENTS ...]"
+usage="COMMAND [ARGUMENTS ...]"
 prefix=cryptop
 
 runas @user@ cryptop "$@"
index 89deccc..952a095 100644 (file)
@@ -636,7 +636,7 @@ recover () {
 
 defhelp () {
   read umsg
-  usage="usage: $quis${umsg+ }$umsg"
+  usage=$umsg
   help=$(cat)
   case "$KEYS_HELP" in t) help; exit ;; esac
 }
@@ -644,13 +644,17 @@ defhelp () {
 help () { showhelp; }
 showhelp () {
   cat <<EOF
-$usage
+Usage: $quis${usage:+ $usage}
 
 $help
 EOF
 }
 
-usage_err () { echo >&2 "$usage"; exit 1; }
+usage () {
+  : ${cmdargs=$usage}
+  echo "usage: $quis${cmdname:+ $cmdname}${cmdargs:+ $cmdargs}"
+}
+usage_err () { usage >&2; exit 1; }
 
 ###--------------------------------------------------------------------------
 ### Subcommand handling.
@@ -659,6 +663,20 @@ version () {
   echo "$PACKAGE version $VERSION"
 }
 
+unset cmdargs
+unset cmdname
+cmds=""
+defcmd () {
+  cmd=$1; shift; args=$*
+  help=$(cat)
+  eval help_$cmd=\$help
+  cmds="${cmds:+$cmds
+}$cmd $args"
+}
+
+defcmd help "[COMMAND ...]" <<EOF
+Show help about the COMMANDs, or about $quis if none are named.
+EOF
 cmd_help () {
   rc=0
   version
@@ -666,30 +684,50 @@ cmd_help () {
     0)
       cat <<EOF
 
-$usage
+Usage: $quis${usage:+ $usage}
 
 Options:
   -h           Show this help text.
   -v           Show the program version number.
 
-Commands installed:
+Commands provided:
 EOF
-      cd "$KEYSLIB"
-      for i in $prefix.*; do
-       if [ ! -x "$i" ]; then continue; fi
-       sed -n "/<<HELP/{n;s/^/ ${i#$prefix.} /;p;q;}" "$i"
-      done
+      while read cmd args; do echo "   $cmd${args:+ $args}"; done <<EOF
+$cmds
+EOF
+      case ${prefix+t} in
+       t)
+         cd $KEYSLIB
+         for i in $prefix.*; do
+           if [ ! -x "$i" ]; then continue; fi
+           sed -n "/<<HELP/{n;s/^/     ${i#$prefix.} /;p;q;}" "$i"
+         done
+         ;;
+      esac
       ;;
     *)
-      for i in "$@"; do
+      for cmd in "$@"; do
        echo
-       if [ ! -x "$KEYSLIB/$prefix.$i" ]; then
-         echo >&2 "$quis: unrecognized command \`$i'"
-         rc=1
-         continue
-       elif ! KEYS_HELP=t "$KEYSLIB/$prefix.$i"; then
-         rc=1
-       fi
+       foundp=nil
+       while read cmdname cmdargs; do
+         case $cmdname in "$cmd") foundp=t; break ;; esac
+       done <<EOF
+$cmds
+EOF
+       case $foundp in
+         t)
+           eval help=\$help_$cmdname; echo "$help"
+           ;;
+         nil)
+           if [ ! -x "$KEYSLIB/$prefix.$cmd" ]; then
+             echo >&2 "$quis: unrecognized command \`$cmd'"
+             rc=1
+             continue
+           elif ! KEYS_HELP=t "$KEYSLIB/$prefix.$cmd"; then
+             rc=1
+           fi
+           ;;
+       esac
       done
       ;;
   esac
@@ -697,16 +735,28 @@ EOF
 }
 
 dispatch () {
-  case $# in 0) echo >&2 "$usage"; exit 1 ;; esac
+  case $# in 0) usage_err ;; esac
   cmd=$1; shift
-  case "$cmd" in help) cmd_help "$@"; exit ;; esac
-  if [ ! -x "$KEYSLIB/$prefix.$cmd" ]; then
-    echo >&2 "$quis: unrecognized command \`$cmd'"
-    exit 1
-  fi
-
-  unset KEYS_HELP
-  exec "$KEYSLIB/$prefix.$cmd" "$@"
+  foundp=nil
+  while read cmdname cmdargs; do
+    case $cmdname in "$cmd") foundp=t; break ;; esac
+  done <<EOF
+$cmds
+EOF
+  case $foundp in
+    t)
+      OPTIND=1
+      cmd_$cmdname "$@"
+      ;;
+    nil)
+      if [ ! -x "$KEYSLIB/$prefix.$cmd" ]; then
+       echo >&2 "$quis: unrecognized command \`$cmd'"
+       exit 1
+      fi
+      unset KEYS_HELP
+      exec "$KEYSLIB/$prefix.$cmd" "$@"
+      ;;
+  esac
 }
 
 ###----- That's all, folks --------------------------------------------------
diff --git a/keys.in b/keys.in
index 5baa9f5..cee261c 100755 (executable)
--- a/keys.in
+++ b/keys.in
@@ -31,7 +31,7 @@ export ETC KEYS KEYSLIB
 
 . "$KEYSLIB"/keyfunc.sh
 
-usage="usage: $quis COMMAND [ARGUMENTS ...]"
+usage="COMMAND [ARGUMENTS ...]"
 prefix=keys
 
 runas @user@ keys "$@"