ctdb-tests: Avoid use of non-portable getopt in stubs
authorMartin Schwenke <martin@meltin.net>
Sun, 1 Jul 2018 09:58:02 +0000 (19:58 +1000)
committerMartin Schwenke <martins@samba.org>
Sat, 28 Jul 2018 01:50:10 +0000 (03:50 +0200)
getopt is being used with non-portable options.  In most cases use
simpler, POSIX-compliant getopts instead.

In the case of the ctdb test stub command, options can appear after
other arguments, so this requires an additional nested loop.

In the case of smnotify, there are no short options, so handle the
long options manually.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=13520

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/tests/eventscripts/stubs/ctdb
ctdb/tests/eventscripts/stubs/rpcinfo
ctdb/tests/eventscripts/stubs/smnotify
ctdb/tests/eventscripts/stubs/ss

index c5a6ceecce0a9efa13f3f3f864fde6ebc112f65f..c3ea0d50550d8bb6c7167057f5b3628b34ef996f 100755 (executable)
@@ -27,29 +27,35 @@ not_implemented ()
     exit $not_implemented_exit_code
 }
 
-# Don't set $POSIXLY_CORRECT here.
-_temp=$(getopt -n "$prog" -o "Xvhn:" -l help -- "$@") || \
-    usage
-
-eval set -- "$_temp"
-
 verbose=false
 machine_readable=false
 nodespec=""
 
-args="$*"
+args=""
+
+# Options and command argument can appear in any order, so when
+# getopts thinks it is done, process any non-option arguments and go
+# around again.
+while [ $# -gt 0 ] ; do
+       while getopts "Xvhn:?" opt ; do
+               case "$opt" in
+               X) machine_readable=true ;;
+               v) verbose=true ;;
+               n) nodespec="$OPTARG" ;;
+               \?|*) usage ;;
+               esac
+       done
+       shift $((OPTIND - 1))
 
-while true ; do
-    case "$1" in
-       -X) machine_readable=true ; shift ;;
-       -v) verbose=true ; shift ;;
-       -n) nodespec="$2" ; shift 2 ;;
-       --) shift ; break ;;
-       -h|--help|*) usage ;; # * shouldn't happen, so this is reasonable.
-    esac
+       # Anything left over must be a non-option arg
+       if [ $# -gt 0 ] ; then
+               args="${args}${args:+ }${1}"
+               shift
+       fi
 done
 
-[ $# -ge 1 ] || usage
+[ -n "$args" ] || usage
+set -- $args
 
 setup_tickles ()
 {
index 1866b59560e08f24035154568e80868a141c3053..bf21197a9d484487ae834c711ce31fa63b1628b2 100755 (executable)
@@ -16,29 +16,23 @@ EOF
 
 parse_options ()
 {
-    _temp=$(getopt -n "$prog" -o "T:h" -- "$@")
+       while getopts "T:h?" opt ; do
+               case "$opt" in
+               T) netid="$OPTARG" ;;
+               \?|h) usage ;;
+               esac
+       done
+       shift $((OPTIND - 1))
 
-    [ $? != 0 ] && usage
+       [ "$netid" = "tcp" ] || usage
 
-    eval set -- "$_temp"
+       host="$1" ; shift
+       [ "$host" = "localhost" -o "$host" = "127.0.0.1" ] || usage
 
-    while true ; do
-       case "$1" in
-           -T) netid="$2"; shift 2 ;;
-           --) shift ; break ;;
-           -h|*) usage ;; # * shouldn't happen, so this is reasonable.
-       esac
-    done
+       [ 1 -le $# -a $# -le 2 ] || usage
 
-    [ "$netid" = "tcp" ] || usage
-
-    host="$1" ; shift
-    [ "$host" = "localhost" -o "$host" = "127.0.0.1" ] || usage
-
-    [ 1 -le $# -a $# -le 2 ] || usage
-
-    p="$1"
-    v="$2"
+       p="$1"
+       v="$2"
 }
 
 parse_options "$@"
index 2bace779df06389ab2a588ebbdb3a1dfe0fb6c79..78710346998860d02501935d59bef162f9f5cedd 100755 (executable)
@@ -9,28 +9,27 @@ EOF
        exit 1
 }
 
-temp=$(getopt -n "smnotify" -o "h" -l client:,ip:,server:,stateval: -- "$@")
-if [ $? != 0 ] ; then
-       usage
-fi
-
-eval set -- "$temp"
-
 cip=""
 sip=""
 mon_name=""
 state=""
 
-while : ; do
+while [ $# -gt 0 ] ; do
        case "$1" in
        --client) cip="$2" ; shift 2 ;;
+       --client=*) cip="${1#*=}" ; shift ;;
        --ip) sip="$2" ; shift 2 ;;
+       --ip=*) sip="${1#*=}" ; shift ;;
        --server)  mon_name="$2" ; shift 2 ;;
+       --server=*)  mon_name="${1#*=}" ; shift ;;
        --stateval) state="$2" ; shift 2 ;;
+       --stateval=*) state="${1#*=}" ; shift ;;
        --) shift ; break ;;
-       *) usage ;;
+       -*) usage ;;
+       *) break ;;
        esac
 done
+[ $# -eq 0 ] || usage
 
 if [ -z "$cip" -o -z "$sip" -o -z "$mon_name" -o -z "$state" ] ; then
        usage
index bb291b2b091e2ea6fb38601cccc423d1ee15592b..30f9b89556b87351a260890efb3d2c895d4993ad 100755 (executable)
@@ -152,23 +152,19 @@ listen=false
 header=true
 
 orig="$*"
-temp=$(getopt -n "$prog" -o "txnalHh" -l tcp -l unix -l help -- "$@")
-[ $? -eq 0 ] || usage
-
-eval set -- "$temp"
-
-while true ; do
-       case "$1" in
-       --tcp|-t) tcp=true ; shift ;;
-       --unix|-x) unix=true ; shift ;;
-       -l) listen=true ; shift ;;
-       -a) all=true ; shift ;;
-       -H) header=false ; shift ;;
-       -n) shift ;;
-       --) shift ; break ;;
-       -h|--help|*) usage ;;
+
+while getopts "txnalHh?" opt ; do
+       case "$opt" in
+       t) tcp=true ;;
+       x) unix=true ;;
+       l) listen=true ;;
+       a) all=true ;;
+       H) header=false ;;
+       n) : ;;
+       \?|h) usage ;;
        esac
 done
+shift $((OPTIND - 1))
 
 $tcp || $unix || not_supported "$*"