1
0
mirror of https://github.com/samba-team/samba.git synced 2025-09-02 01:49:29 +03:00

ctdb-tests: Avoid use of non-portable getopt in stubs

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>
This commit is contained in:
Martin Schwenke
2018-07-01 19:58:02 +10:00
committed by Martin Schwenke
parent 56ffca3e79
commit 896c77df1c
4 changed files with 53 additions and 58 deletions

View File

@ -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=""
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
# 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))
# 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 ()
{

View File

@ -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 "$@"

View File

@ -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

View File

@ -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 "$*"