parseopts: normalize options into an array

Modify parse_options logic to fill an array instead of printing parsed
options. Avoid eval like the plague. Because it is the plague.

Fixes bugs such as FS#28445.

Signed-off-by: Dave Reisner <dreisner@archlinux.org>
Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dave Reisner 2012-02-15 22:50:51 -05:00 committed by Dan McGee
parent 242006933d
commit ca41427141
3 changed files with 17 additions and 27 deletions

View file

@ -23,17 +23,15 @@ parse_options() {
[[ ${match} = ${1:2}:: && -n $2 && ${2:0:1} != "-" ]] && needsargument=1 [[ ${match} = ${1:2}:: && -n $2 && ${2:0:1} != "-" ]] && needsargument=1
if (( ! needsargument )); then if (( ! needsargument )); then
printf ' %s' "$1" OPTRET+=("$1")
else else
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
printf ' %s ' "$1" OPTRET+=("$1" "$2")
shift shift
printf "'%q" "$1"
while [[ -n $2 && ${2:0:1} != "-" ]]; do while [[ -n $2 && ${2:0:1} != "-" ]]; do
shift shift
printf " %q" "$1" OPTRET+=("$1")
done done
printf "'"
else else
printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'$1'" >&2 printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'$1'" >&2
ret=1 ret=1
@ -57,26 +55,22 @@ parse_options() {
( -n ${1:$i+1} || ( -n $2 && ${2:0:1} != "-" ) ) ]] && needsargument=1 ( -n ${1:$i+1} || ( -n $2 && ${2:0:1} != "-" ) ) ]] && needsargument=1
if (( ! needsargument )); then if (( ! needsargument )); then
printf ' -%s' "${1:i:1}" OPTRET+=("-${1:i:1}")
else else
if [[ -n ${1:$i+1} ]]; then if [[ -n ${1:$i+1} ]]; then
printf ' -%s ' "${1:i:1}" OPTRET+=("-${1:i:1}" "${1:i+1}")
printf "'%q" "${1:$i+1}"
while [[ -n $2 && ${2:0:1} != "-" ]]; do while [[ -n $2 && ${2:0:1} != "-" ]]; do
shift shift
printf " %q" "$1" OPTRET+=("$1")
done done
printf "'"
else else
if [[ -n $2 ]]; then if [[ -n $2 ]]; then
printf ' -%s ' "${1:i:1}" OPTRET+=("-${1:i:1}" "$2")
shift shift
printf "'%q" "$1"
while [[ -n $2 && ${2:0:1} != "-" ]]; do while [[ -n $2 && ${2:0:1} != "-" ]]; do
shift shift
printf " %q" "$1" OPTRET+=("$1")
done done
printf "'"
else else
printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'-${1:i:1}'" >&2 printf "@SCRIPTNAME@: $(gettext "option %s requires an argument\n")" "'-${1:i:1}'" >&2
@ -91,15 +85,11 @@ parse_options() {
fi fi
done done
else else
unused_options="${unused_options} '$1'" unused_options+=("$1")
fi fi
shift shift
done done
printf " --" OPTRET+=('--' "${unused_options[@]}")
[[ $unused_options ]] && printf ' %s' "${unused_options[@]}"
[[ $1 ]] && printf " '%s'" "$@"
printf "\n"
return $ret return $ret
} }

View file

@ -1906,11 +1906,11 @@ OPT_LONG+=",version,config:"
# Pacman Options # Pacman Options
OPT_LONG+=",noconfirm,noprogressbar" OPT_LONG+=",noconfirm,noprogressbar"
if ! OPT_TEMP="$(parse_options $OPT_SHORT $OPT_LONG "$@")"; then if ! parse_options $OPT_SHORT $OPT_LONG "$@"; then
echo; usage; exit 1 # E_INVALID_OPTION; echo; usage; exit 1 # E_INVALID_OPTION;
fi fi
eval set -- "$OPT_TEMP" set -- "${OPTRET[@]}"
unset OPT_SHORT OPT_LONG OPT_TEMP unset OPT_SHORT OPT_LONG OPTRET
while true; do while true; do
case "$1" in case "$1" in

View file

@ -504,11 +504,11 @@ OPT_LONG="add::,config:,delete:,edit-key:,export::,finger::,gpgdir:"
OPT_LONG+=",help,import:,import-trustdb:,init,keyserver:,list-keys::,list-sigs::" OPT_LONG+=",help,import:,import-trustdb:,init,keyserver:,list-keys::,list-sigs::"
OPT_LONG+=",lsign-key:,populate::,recv-keys:,refresh-keys::,updatedb" OPT_LONG+=",lsign-key:,populate::,recv-keys:,refresh-keys::,updatedb"
OPT_LONG+=",verify:,version" OPT_LONG+=",verify:,version"
if ! OPT_TEMP="$(parse_options $OPT_SHORT $OPT_LONG "$@")"; then if ! parse_options $OPT_SHORT $OPT_LONG "$@"; then
echo; usage; exit 1 # E_INVALID_OPTION; echo; usage; exit 1 # E_INVALID_OPTION;
fi fi
eval set -- "$OPT_TEMP" set -- "${OPTRET[@]}"
unset OPT_SHORT OPT_LONG OPT_TEMP unset OPT_SHORT OPT_LONG OPTRET
if [[ $1 == "--" ]]; then if [[ $1 == "--" ]]; then
usage; usage;