From fdd9e679e280477cbd24764d81b0b7cee16ca4f4 Mon Sep 17 00:00:00 2001 From: Dieter Plaetinck Date: Fri, 17 Dec 2010 13:01:10 +0100 Subject: ask_option(): stricter argument checking and prevent endless loops on argument mismatch in cli mode --- libui.sh | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/libui.sh b/libui.sh index 035e4e4..456a0c9 100644 --- a/libui.sh +++ b/libui.sh @@ -229,6 +229,11 @@ ask_number () ask_option () { [ `type -t _${LIBUI_UI}_ask_option` == function ] || die_error "_${LIBUI_UI}_ask_option is not a function" + [ -z "$1" ] && debug 'UI' "ask_option args: $@" && die_error "ask_option \$1 must be the default item (or 'no' for none" + [ -z "$2" ] && debug 'UI' "ask_option args: $@" && die_error "ask_option \$2 must be the title" + TYPE=${4:-required} + [ "$TYPE" != required -a "$TYPE" != optional ] && debug 'UI' "ask_option args: $@" && die_error "ask option \$4 must be required or optional or ''. not $TYPE" + [ -z "$6" ] && debug 'UI' "ask_option args: $@" && die_error "ask_option makes only sense if you specify at least one option (with tag and name)" #nothing wrong with only 1 option. it still shows useful info to the user _${LIBUI_UI}_ask_option "$@" } @@ -425,11 +430,6 @@ _dia_ask_option () { DEFAULT="" [ "$1" != 'no' ] && DEFAULT="--default-item $1" - [ -z "$2" ] && die_error "ask_option \$2 must be the title" - # $3 is optional more info - TYPE=${4:-required} - [ "$TYPE" != required -a "$TYPE" != optional ] && debug 'UI' "_dia_ask_option args: $@" && die_error "ask option \$4 must be required or optional or ''. not $TYPE" - [ -z "$6" ] && debug 'UI' "_dia_ask_option args: $@" && die_error "ask_option makes only sense if you specify at least one option (with tag and name)" #nothing wrong with only 1 option. it still shows useful info to the user DIA_MENU_TITLE=$2 EXTRA_INFO=$3 @@ -633,12 +633,7 @@ _cli_ask_option () #TODO: strip out color codes #TODO: if user entered incorrect choice, ask him again DEFAULT= - [ "$1" != 'no' ] && DEFAULT=$1 #TODO: if user forgot to specify a default (eg all args are 1 pos to the left, we can end up in an endless loop :s) - [ -z "$2" ] && die_error "ask_option \$2 must be the title" - # $3 is optional more info - TYPE=${4:-required} - [ "$TYPE" != required -a "$TYPE" != optional ] && debug 'UI' "_dia_ask_option args: $@" && die_error "ask option \$4 must be required or optional or ''. not $TYPE" - [ -z "$6" ] && debug 'UI' "_dia_ask_option args: $@" && die_error "ask_option makes only sense if you specify at least one option (with tag and name)" #nothing wrong with only 1 option. it still shows useful info to the user + [ "$1" != 'no' ] && DEFAULT=$1 MENU_TITLE=$2 EXTRA_INFO=$3 @@ -648,6 +643,7 @@ _cli_ask_option () [ -n "$EXTRA_INFO" ] && echo "$EXTRA_INFO" while [ -n "$1" ] do + [ -z "$2" ] && die_error "ask_option error: tag $1 has no item" echo "$1 ] $2" shift 2 done -- cgit v1.2.3