#! /bin/sh #---------------------------------------------------------------------------- # /var/install/bin/ask - ask user # # Creation: 2003-07-19 fm # Rewrite to enable colors 2008-08-05 rg # # Last Update: $Id$ # # Copyright (c) 2001-2011 the eisfair team, team(at)eisfair(dot)org # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------- #---------------------------------------------------------------------------- # # Syntax: # # /var/install/bin/ask [tty [color]] QUESTION [DEFAULT] [PATTERN] [PATTERN] ... # # QUESTION - Question to be asked. # DEFAULT - Default answer # PATTERN - Possible answers: # empty - y(es) or n(o) # * - string input # + - string input (not empty!) # min-max - numerical input (range given) # WORD|WORD - a list of possible values # the first will be included into the question # the last will be returned if choosen # WORD=WORD - a list of possible values # all will be included into the question # the first will be returned if choosen # ^$ - ENTER # # Return Values: # # Exitcode: Zero-Based Index of the Choosen PATTERN # 255 ask was aborted by typing CTRL-C # Output: If Output is redirected, the output is either a part # of the matching pattern (if PATTERN is given) or the # entered text # # Examples: # ask "Kernel loeschen" # ask "Kernel loeschen" "no" # ask "Cronzyklus" "" "d|daily" "w|weekly" "m|monthly" # ask "Hostname" "eistest" "*" # ask "Hostname" "" "+" >result # ask "Weiter" >result # ask "Anzahl" "1" "1-7" >result # ask "Anzahl" "" "1-15" >result # # Example with colors from 'xen-import-domain': # _PRINTMODE=`get_printmode` # This Information provided by the invoking # shell after reading # . /var/install/include/eislib # this normaly returns 'tty' # importTarget=`/var/install/bin/ask $_PRINTMODE _INFO \ # "What kind of domU you want to import? please make your choice\n" \ # "i" \ # "disk [i]mage|i|diskImage" \ # "phisical [d]evice|d|phisicalDevice"` #---------------------------------------------------------------------------- # include eislib . /var/install/include/eislib #exec 2> /tmp/ask-trace$$.log #set -x # Color integration backwards compatible when no color information given. _INFO="" _BOLD="" _RESET="" _COLOR="" is_color () { if [ "$1" = "tty" ] then _BOLD="${_EISLIB_COLOR_TTY_MODE_BRIGHT}" _RESET="${_EISLIB_COLOR_TTY_RESET}" return 0 else return 1 fi } set_color () { if [ "$1" = "_INFO" ] then _COLOR="${_EISLIB_COLOR_TTY_INFO}" return 0 elif [ "$1" = "_WARN" ] then _COLOR="${_EISLIB_COLOR_TTY_WARN}" return 0 elif [ "$1" = "_ERROR" ] then _COLOR="${_EISLIB_COLOR_TTY_ERROR}" return 0 else return 1 fi } trap 'echo >/dev/tty;stty echo;exit 255' SIGINT if is_color "$1"; then shift; fi if set_color "$1"; then shift; fi question="$1"; shift if is_color "$1"; then shift; fi if set_color "$1"; then shift; fi default="$1"; shift ### parameter processing if [ $# -eq 0 ] # use yes/no as default then regexp_n=2 regexp_1="y|yes" regexp_2="n|no" values="${_COLOR}y/n${_RESET}" full_values=" y(es) or n(o)" elif [ $# -eq 1 -a "$1" = "*" ] # string (empty allowed) then regexp_n=1 regexp_1=".*" values="" full_values="" elif [ $# -eq 1 -a "$1" = "*hidden*" ] # string (empty allowed) then regexp_n=1 regexp_1=".*" values="" full_values="" hidden=true elif [ $# -eq 1 -a "$1" = "+" ] # string (not empty) then regexp_n=1 regexp_1=".+" values="" full_values="" elif [ $# -eq 1 -a "$1" = "+hidden+" ] # string (not empty) then regexp_n=1 regexp_1=".+" values="" full_values="" hidden=true else # list of values ### Build strings of possible values for output values="" full_values=" " regexp_n=0 while [ $# -gt 0 ] do regexp_n=`/usr/bin/expr $regexp_n + 1` if echo -e "$1" | grep -qsE '[0-9]+\-[0-9]+' then eval regexp_$regexp_n="[0-9]+" from=`echo "$1" | sed -e 's/-.*$//'` to=`echo "$1" | sed -e 's/^.*-//'` eval from_$regexp_n="$from" eval to_$regexp_n="$to" full_values="$full_values$from-$to" values="$values${_COLOR}$from${_RESET}-${_COLOR}$to${_RESET}" elif echo "$1" | grep -qsE '=' then eval regexp_$regexp_n=\"`echo -e "$1" | sed -e 's/=.*$//'`\" if echo -e "$1" | grep -qsE '\^[$]=' then eval regexp_$regexp_n=\"'$regexp_'$regexp_n'|'\" fi full_values="$full_values`echo -e "$1" | sed -e 's/\^[$]/ENTER/'`" values="$values${_COLOR}`echo -e "$1" | sed -e 's/\^[$]/ENTER/'`${_RESET}" else eval regexp_$regexp_n=\"$1\" if echo -e "$1" | grep -qsE '\^[$]$' then eval regexp_$regexp_n=\"'$regexp_'$regexp_n'|'\" fi full_values="$full_values`echo -e "$1" | sed -e 's/\(.*\)|/\1=/' -e 's/\^[$]/ENTER/'`" values="$values${_COLOR}`echo -e "$1" | sed -e 's/|.*//' | sed -e 's/\^[$]/ENTER/'`${_RESET}" fi shift if [ $# -gt 0 ] then full_values="$full_values or " values="$values, " fi done fi ### build question if [ -n "$values" ] then question="$question ($values)" fi if [ -n "$default" ] then question="$question [${_COLOR}${_BOLD}$default${_RESET}]" fi question="$question" #${hidden:-false} && question="$question" || question="$question" while [ 1 ] do ### ask the question #echo -e "$question" >/dev/tty # supress output if hidden is wanted ${hidden:-false} && q_h='-s' #stty -echo # ask the question - read the answer # -e enable use of libreadline, to navigate in the row # -r raw text, enable no backslash as escape character # -p promt the question, to no overwrite the row with 'cursor back' read -e -r ${q_h} -p "$(echo -e ${question}?) " a a=${a:-$default} ### re-enable output if hidden was wanted #${hidden:-false} && stty echo # 'strg c' desater, enable output in the trap handler # is the answer allowed? idx=1 while [ $idx -le $regexp_n ] do eval regexp='$'regexp_$idx if echo -e $a | grep -qsE "^($regexp)$" then eval from='$'from_$idx eval to='$'to_$idx if [ -z "${from}" ] || [ "$from" -le "$a" -a "$to" -ge "$a" ] then if ! /usr/bin/tty -s <&1 then # if pattern was in '*' '+' '^$' # or $from is not empty # write INPUT VALUE to file # else # write LAST PART of REGEXP to file # e.g. (d|daily) -> daily # e.g. (d|daily|d) former (d=daily) -> d if [ "$regexp" = ".*" ] || [ "$regexp" = ".+" ] \ || [ -n "${from}" ] || [ "$regexp" = "^$" ] then echo -e $a else echo "$regexp" | sed -e "s/.*|//" fi fi exit `/usr/bin/expr $idx - 1` fi fi idx=`/usr/bin/expr $idx + 1` done mecho --tty --warn "Please answer$full_values!" >/dev/tty done exit 255 # not reached #set +x