aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2012-03-21 12:48:03 +0000
committerNatanael Copa <ncopa@alpinelinux.org>2012-03-21 12:48:03 +0000
commite18c8a75c22f320bfc3db31425f48fc90079369f (patch)
treeceb17ecb2516e369dfaea9e35e47336181395b9a
parent752c73a67e2620ca764993ea34f072d5908a6612 (diff)
downloadalpine-conf-e18c8a75c22f320bfc3db31425f48fc90079369f.tar.bz2
alpine-conf-e18c8a75c22f320bfc3db31425f48fc90079369f.tar.xz
libalpine: add various functions from openbsd installer
isin - is element in list rmel - remove element from list ask - Ask a question and read response ask_until - repeat a question til a non-blank response ask_which - repeat a question til a listed option is entered
-rw-r--r--[-rwxr-xr-x]libalpine.sh.in126
1 files changed, 119 insertions, 7 deletions
diff --git a/libalpine.sh.in b/libalpine.sh.in
index 8661643..4ea5201 100755..100644
--- a/libalpine.sh.in
+++ b/libalpine.sh.in
@@ -71,6 +71,16 @@ cfg_add() {
[ -z "$NOCOMMIT" ] && lbu_add "$@"
}
+# Detect if we are running Xen
+is_xen() {
+ test -d /proc/xen
+}
+
+# Detect if we are running Xen Dom0
+is_xen_dom0() {
+ is_xen && \
+ grep -q "control_d" /proc/xen/capabilities
+}
# from OpenBSD installer
# Ask for a password, saving the input in $resp.
@@ -106,13 +116,115 @@ askpassword() {
IFS=$_oifs
}
-# Detect if we are running Xen
-is_xen() {
- test -d /proc/xen
+# test the first argument against the remaining ones, return success on a match
+isin() {
+ local _a=$1 _b
+ shift
+ for _b; do
+ [ "$_a" = "$_b" ] && return 0
+ done
+ return 1
}
-# Detect if we are running Xen Dom0
-is_xen_dom0() {
- is_xen && \
- grep -q "control_d" /proc/xen/capabilities
+# remove all occurrences of first argument from list formed by
+# the remaining arguments
+rmel() {
+ local _a=$1 _b
+
+ shift
+ for _b; do
+ [ "$_a" != "$_b" ] && echo -n "$_b "
+ done
+}
+
+# Issue a read into the global variable $resp.
+_ask() {
+ local _redo=0
+
+ read resp
+ case "$resp" in
+ !) echo "Type 'exit' to return to setup."
+ sh
+ _redo=1
+ ;;
+ !*) eval "${resp#?}"
+ _redo=1
+ ;;
+ esac
+ return $_redo
+}
+
+# Ask for user input.
+#
+# $1 = the question to ask the user
+# $2 = the default answer
+#
+# Save the user input (or the default) in $resp.
+#
+# Allow the user to escape to shells ('!') or execute commands
+# ('!foo') before entering the input.
+ask() {
+ local _question=$1 _default=$2
+
+ while :; do
+ echo -n "$_question "
+ [ -z "$_default" ] || echo -n "[$_default] "
+ _ask && : ${resp:=$_default} && break
+ done
}
+
+# Ask for user input until a non-empty reply is entered.
+#
+# $1 = the question to ask the user
+# $2 = the default answer
+#
+# Save the user input (or the default) in $resp.
+ask_until() {
+ resp=
+ while [ -z "$resp" ] ; do
+ ask "$1" "$2"
+ done
+}
+
+# Ask for the user to select one value from a list, or 'done'.
+#
+# $1 = name of the list items (disk, cd, etc.)
+# $2 = question to ask
+# $3 = list of valid choices
+# $4 = default choice, if it is not specified use the first item in $3
+#
+# N.B.! $3 and $4 will be "expanded" using eval, so be sure to escape them
+# if they contain spooky stuff
+#
+# At exit $resp holds selected item, or 'done'
+ask_which() {
+ local _name=$1 _query=$2 _list=$3 _def=$4 _dynlist _dyndef
+
+ while :; do
+ # Put both lines in ask prompt, rather than use a
+ # separate 'echo' to ensure the entire question is
+ # re-ask'ed after a '!' or '!foo' shell escape.
+ eval "_dynlist=\"$_list\""
+ eval "_dyndef=\"$_def\""
+
+ # Clean away whitespace and determine the default
+ set -o noglob
+ set -- $_dyndef; _dyndef="$1"
+ set -- $_dynlist; _dynlist="$*"
+ set +o noglob
+ [ $# -lt 1 ] && resp=done && return
+
+ : ${_dyndef:=$1}
+ echo "Available ${_name}s are: $_dynlist."
+ echo -n "Which one $_query? (or 'done') "
+ [ -n "$_dyndef" ] && echo -n "[$_dyndef] "
+ _ask || continue
+ [ -z "$resp" ] && resp="$_dyndef"
+
+ # Quote $resp to prevent user from confusing isin() by
+ # entering something like 'a a'.
+ isin "$resp" $_dynlist done && break
+ echo "'$resp' is not a valid choice."
+ done
+}
+