[patch] install.sub - input cleanup

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Alexander Hall
Date: Saturday, May 2, 2009 - 4:49 pm

Input cleanup of install.sub

- Kill the "noglob" statements (which I strongly believe is pure legacy
  code). Please inform me if I'm wrong.
- ask() cleanup:
  - Strip everything from the first whitespace on by default (much code
    does not properly handle whitespace in $resp anyway).
  - A third parameter to ask() can be set to "no" if whitespace is to
    be kept.
  - Now uses "read -r" (since I cannot see a need for escapes).
  - Shell command (e.g. "! ls /mnt") uses 'sh -c' instead of eval
- Actually, [[ "$resp" == "$_password" ]] does _not_ need the quotes.
- Sanity check for new user input:
  - Username:  All lower-case characters (as prompted)
  - Full name: Must not contain `:'
- Cleanup of "${var:=default}" => "${var:-default}" usage where it was
  likely intended.

/Alexander


Index: install.sub
===================================================================
RCS file: /store/openbsd/cvs/src/distrib/miniroot/install.sub,v
retrieving revision 1.509
diff -u -p -r1.509 install.sub
--- install.sub	2 May 2009 17:01:26 -0000	1.509
+++ install.sub	2 May 2009 22:45:56 -0000
@@ -252,11 +252,9 @@ stdsize ()  {
 #    *Don't* echo input.
 #    *Don't* interpret "\" as escape character.
 askpass() {
-	set -o noglob
 	stty -echo
 	read -r resp?"$1 "
 	stty echo
-	set +o noglob
 	echo
 }
 
@@ -264,45 +262,43 @@ askpass() {
 #
 #    $1    = the question to ask the user
 #    $2    = the default answer
+#    $3    = whether to to strip $resp from the first whitespace or not
 #
 # 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
+	local _question=$1 _default=$2 _strip=${3:-yes}
 
-	set -o noglob
 	while :; do
 		echo -n "$_question "
 		[[ -z $_default ]] || echo -n "[$_default] "
-		read resp
+		read -r resp
 		case $resp in
 		!)	echo "Type 'exit' to return to install."
 			sh
 			;;
-		!*)	eval ${resp#?}
+		!*)	sh -c "${resp#?}"
 			;;
 		*)	: ${resp:=$_default}
 			break
 			;;
 		esac
 	done
-	set +o noglob
+	[[ $_strip == yes ]] && resp=${resp%% *}
 }
 
 # Ask for a password twice, saving the input in $_password
 askpassword() {
-	_oifs=$IFS
+	local _oifs=$IFS
 	IFS=
 	while :; do
 		askpass "Password for $1 account? (will not echo)"
 		_password=$resp
 
 		askpass "Password for $1 account? (again)"
-		# N.B.: Need quotes around $resp and $_password to preserve leading
-		#       or trailing spaces.
-		[[ "$resp" == "$_password" ]] && break
+		[[ $resp == $_password ]] && break
 
 		echo "Passwords do not match, try again."
 	done
@@ -312,14 +308,28 @@ askpassword() {
 user_setup() {
 	local _u _n _encr
 
-	ask "Setup a user? (enter a lower-case loginname, or 'no')" no
-
-	case $resp in
-	n|no)	return ;;
-	esac
+	while :; do
+		ask "Setup a user? (enter a lower-case loginname, or 'no')" no
+		case $resp in
+		n|no)	return ;;
+		+([a-z])) break ;;
+		esac
 
+		echo "Invalid user name"
+	done
 	_u=$resp
-	ask "Full user name for $_u?" $_u
+
+	while :; do
+		# N.B.: Does not strip whitespace from input
+		ask "Full user name for $_u?" $_u no
+		case $resp in
+		n|no)	return ;;
+		*:*)	;;
+		*)	break ;;
+		esac
+
+		echo "Invalid full user name"
+	done
 	_n=$resp
 
 	askpassword $_u
@@ -384,7 +394,7 @@ ask_which() {
 
 	set -- $_list
 	if (( $# < 1 )); then
-		echo "${_err:=No ${_name}s found}."
+		echo "${_err:-No ${_name}s found}."
 		resp=done
 		return
 	fi
@@ -771,7 +781,7 @@ v4_config() {
 		fi
 		;;
 	*)	_addr=$resp
-		ask_until "Netmask?" "${_mask:=255.255.255.0}"
+		ask_until "Netmask?" "${_mask:-255.255.255.0}"
 		ifconfig $_ifs -group dhcp >/dev/null 2>&1
 		if ifconfig $_ifs inet $_addr netmask $resp up ; then
 			addhostent "$_addr" "$_name"
@@ -808,7 +818,7 @@ v6_config() {
 	esac
 
 	_addr=$resp
-	ask_until "IPv6 prefix length for $_ifs?" "${_prefixlen:=64}"
+	ask_until "IPv6 prefix length for $_ifs?" "${_prefixlen:-64}"
 	ifconfig $_ifs inet6 $_addr prefixlen $resp up || return
 	echo "inet6 $_addr $resp" >>$_hn
 	addhostent "$_addr" "$_name"
@@ -1205,7 +1215,7 @@ install_url() {
 		# embedded blanks are preserved!
 		_oifs=$IFS
 		IFS=
-		ask_until "Login?" "${_ftp_server_login:=anonymous}"
+		ask_until "Login?" "${_ftp_server_login:-anonymous}"
 		_ftp_server_login=$resp
 
 		# Get password unless login in 'anonymous' or 'ftp'
@@ -1557,7 +1567,7 @@ get_fqdn() {
 	_dn=${_dn#$(hostname -s)}
 	_dn=${_dn#.}
 
-	echo "${_dn:=my.domain}"
+	echo "${_dn:-my.domain}"
 }
 
 donetconfig() {
@@ -1589,11 +1599,11 @@ donetconfig() {
 	fi
 
 	# Get & apply fully qualified domain name to hostname.
-	ask "DNS domain name? (e.g. 'bar.com')" "${_dn:=$(get_fqdn)}"
+	ask "DNS domain name? (e.g. 'bar.com')" "${_dn:-$(get_fqdn)}"
 	hostname "$(hostname -s).$resp"
 
 	# Get/Confirm nameservers, and construct appropriate resolv.conf.
-	ask "DNS nameservers? (IP address list or 'none')" "${_ns:=none}"
+	ask "DNS nameservers? (IP address list or 'none')" "${_ns:-none}"
 	if [[ $resp != none ]]; then
 		echo "lookup file bind" >/tmp/resolv.conf
 		for _ns in $resp; do
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[patch] install.sub - input cleanup, Alexander Hall, (Sat May 2, 4:49 pm)