diff options
author | dteske <dteske@FreeBSD.org> | 2013-06-02 05:45:25 +0000 |
---|---|---|
committer | dteske <dteske@FreeBSD.org> | 2013-06-02 05:45:25 +0000 |
commit | 5b05f067655217732e94ad77b1c57a3cb7a3945e (patch) | |
tree | c82ae86b5bfe7b5e527099673025931ffda2e584 /usr.sbin/bsdconfig/share | |
parent | c13ee16ab4b2ed670be4b050ae59d428dab4196f (diff) | |
download | FreeBSD-src-5b05f067655217732e94ad77b1c57a3cb7a3945e.zip FreeBSD-src-5b05f067655217732e94ad77b1c57a3cb7a3945e.tar.gz |
Similar to r251236, improve the portion of dialog(1) API in dialog.subr
responsible for retrieving stored input (for the --inputbox and --password
widgets).
When we (Ron McDowell and I) developed the first version of bsdconfig, it
used temporary files to store responses from dialog(1). That hasn't been
true for a very long time, so the need to always execute some clean-up
function is long-deprecated. The function that used to perform these clean-
up routines for these widgets was f_dialog_inputstr().
We really don't need f_dialog_inputstr() for its originally designed purpose
as all dialog invocations no longer require temporary files.
Just as in r251236, redesign f_dialog_inputstr() in the following four ways:
1. Rename f_dialog_inputstr() to f_dialog_inputstr_fetch()
2. Introduce the new first-argument of $var_to_set to reduce forking
3. Create a corresponding f_dialog_inputstr_store() to abstract storage
4. Offload the sanitization to a new function, f_dialog_line_sanitize()
It should be noted that f_dialog_line_sanitize() -- unlike its cousin from
SVN r251236, f_dialog_data_sanitize() -- trims leading/trailing whitespace
from the user's input. This helps prevent errors and common mistakes caused
by the fact that the new cdialog implementation allows the right-arrow
cursor key to go beyond the last byte of realtime input (adding whitespace
at the end of the typed value).
While we're centralizing the sanitization, let's rewrite f_dialog_input()
while we're here to likewise reduce forking. The f_dialog_input() function
now expects the first argument of $var_to_set instead of producing results
on standard-out.
These changes greatly improve readability and also improve performance.
Diffstat (limited to 'usr.sbin/bsdconfig/share')
-rw-r--r-- | usr.sbin/bsdconfig/share/dialog.subr | 128 | ||||
-rw-r--r-- | usr.sbin/bsdconfig/share/media/tcpip.subr | 30 | ||||
-rw-r--r-- | usr.sbin/bsdconfig/share/mustberoot.subr | 12 | ||||
-rw-r--r-- | usr.sbin/bsdconfig/share/variable.subr | 3 |
4 files changed, 109 insertions, 64 deletions
diff --git a/usr.sbin/bsdconfig/share/dialog.subr b/usr.sbin/bsdconfig/share/dialog.subr index bfbe1db..2b65d31 100644 --- a/usr.sbin/bsdconfig/share/dialog.subr +++ b/usr.sbin/bsdconfig/share/dialog.subr @@ -144,6 +144,48 @@ f_dialog_data_sanitize() done } +# f_dialog_line_sanitize $var_to_edit ... +# +# When using dialog(1) or Xdialog(1) sometimes unintended warnings or errors +# are generated from underlying libraries. For example, if $LANG is set to an +# invalid or unknown locale, the warnings from the Xdialog(1) libraries will +# clutter the output. This function helps by providing a centralied function +# that removes spurious warnings from the dialog(1) (or Xdialog(1)) response. +# +# Simply pass the name of one or more variables that need to be sanitized. +# After execution, the variables will hold their newly-sanitized data. +# +# This function, unlike f_dialog_data_sanitize(), also removes leading/trailing +# whitespace from each line. +# +f_dialog_line_sanitize() +{ + if [ "$#" -eq 0 ]; then + f_dprintf "%s: called with zero arguments" \ + f_dialog_response_sanitize + return $FAILURE + fi + + local __var_to_edit + for __var_to_edit in $*; do + # Skip warnings and trim leading/trailing whitespace + setvar $__var_to_edit "$( f_getvar $__var_to_edit | awk ' + BEGIN { data = 0 } + { + if ( ! data ) + { + if ( $0 ~ /^$/ ) next + if ( $0 ~ /^Gdk-WARNING \*\*:/ ) next + data = 1 + } + sub(/^[[:space:]]*/, "") + sub(/[[:space:]]*$/, "") + print + } + ' )" + done +} + ############################################################ TITLE FUNCTIONS # f_dialog_title [$new_title] @@ -1588,33 +1630,45 @@ f_dialog_noyes() ############################################################ INPUT FUNCTIONS -# f_dialog_inputstr +# f_dialog_inputstr_store [-s] $text +# +# Store some text from a dialog(1) inputbox to be retrieved later by +# f_dialog_inputstr_fetch(). If the first argument is `-s', the text is +# sanitized before being stored. +# +f_dialog_inputstr_store() +{ + local sanitize= + [ "$1" = "-s" ] && sanitize=1 && shift 1 # -s + local text="$1" + + # Sanitize the line before storing it if desired + [ "$sanitize" ] && f_dialog_line_sanitize text + + setvar DIALOG_INPUTBOX_$$ "$text" +} + +# f_dialog_inputstr_fetch [$var_to_set] # # Obtain the inputstr entered by the user from the most recently displayed -# dialog(1) inputbox and clean up any temporary files/variables. +# dialog(1) inputbox (previously stored with f_dialog_inputstr_store() above). +# If $var_to_set is NULL or missing, output is printed to stdout (which is less +# recommended due to performance degradation; in a loop for example). # -f_dialog_inputstr() +f_dialog_inputstr_fetch() { - # Skip warnings and trim leading/trailing whitespace from user input - eval echo \"\$DIALOG_INPUTBOX_$$\" | awk ' - BEGIN { found = 0 } - { - if ( ! found ) - { - if ( $0 ~ /^$/ ) next - if ( $0 ~ /^Gdk-WARNING \*\*:/ ) next - found = 1 - } - sub(/^[[:space:]]*/, "") - sub(/[[:space:]]*$/, "") - print - } - ' + local __var_to_set="$1" __cp + + debug= f_getvar DIALOG_INPUTBOX_$$ "${__var_to_set:-__cp}" # get data setvar DIALOG_INPUTBOX_$$ "" # scrub memory in case data was sensitive + + # Return the line on standard-out if desired + [ "$__var_to_set" ] || echo "$__cp" + return $SUCCESS } -# f_dialog_input $prompt [$init [$hline]] +# f_dialog_input $var_to_set $prompt [$init [$hline]] # # Prompt the user with a dialog(1) inputbox to enter some value. The inputbox # remains until the the user presses ENTER or ESC, or otherwise ends the @@ -1629,34 +1683,38 @@ f_dialog_inputstr() # f_dialog_input() { - local prompt="$1" init="$2" hline="$3" - local height width - f_dialog_inputbox_size height width \ + local __var_to_set="$1" __prompt="$2" __init="$3" __hline="$4" + + # NOTE: Function name appended to prevent __var_{height,width} values + # from becoming local (and thus preventing setvar from working). + local __height_input __width_input + f_dialog_inputbox_size __height_input __width_input \ "$DIALOG_TITLE" "$DIALOG_BACKTITLE" \ - "$prompt" "$init" "$hline" + "$__prompt" "$__init" "$__hline" - local opterm="--" - [ "$USE_XDIALOG" ] && opterm= + local __opterm="--" + [ "$USE_XDIALOG" ] && __opterm= - local dialog_input - dialog_input=$( + local __dialog_input + __dialog_input=$( $DIALOG \ --title "$DIALOG_TITLE" \ --backtitle "$DIALOG_BACKTITLE" \ - --hline "$hline" \ + --hline "$__hline" \ --ok-label "$msg_ok" \ --cancel-label "$msg_cancel" \ - --inputbox "$prompt" \ - $height $width \ - $opterm "$init" \ + --inputbox "$__prompt" \ + $__height_input $__width_input \ + $__opterm "$__init" \ 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD ) - local retval=$? + local __retval=$? - setvar DIALOG_INPUTBOX_$$ "$dialog_input" - f_dialog_inputstr + # Remove warnings and leading/trailing whitespace from user input + f_dialog_line_sanitize __dialog_input - return $retval + setvar "$__var_to_set" "$__dialog_input" + return $__retval } ############################################################ MENU FUNCTIONS diff --git a/usr.sbin/bsdconfig/share/media/tcpip.subr b/usr.sbin/bsdconfig/share/media/tcpip.subr index f1b4ac7..b7b7b7e 100644 --- a/usr.sbin/bsdconfig/share/media/tcpip.subr +++ b/usr.sbin/bsdconfig/share/media/tcpip.subr @@ -1422,29 +1422,23 @@ f_device_dialog_tcp() "$_netmask" \ && break ;; "$msg_host_name_including_domain:") - cp=$( f_dialog_input "$cp" \ - "$_hostname" - ) && _hostname="$cp" ;; + f_dialog_input cp "$cp" "$_hostname" \ + && _hostname="$cp" ;; "$msg_ipv4_gateway:") - cp=$( f_dialog_input "$cp" \ - "$_gateway" - ) && _gateway="$cp" ;; + f_dialog_input cp "$cp" "$_gateway" \ + && _gateway="$cp" ;; "$msg_name_server:") - cp=$( f_dialog_input "$cp" \ - "$_nameserver" - ) && _nameserver="$cp" ;; + f_dialog_input cp "$cp" "$_nameserver" \ + && _nameserver="$cp" ;; "$msg_ipv4_address:") - cp=$( f_dialog_input "$cp" \ - "$_ipaddr" - ) && _ipaddr="$cp" ;; + f_dialog_input cp "$cp" "$_ipaddr" \ + && _ipaddr="$cp" ;; "$msg_netmask:") - cp=$( f_dialog_input "$cp" \ - "$_netmask" - ) && _netmask="$cp" ;; + f_dialog_input cp "$cp" "$_netmask" \ + && _netmask="$cp" ;; "$msg_extra_options_to_ifconfig") - cp=$( f_dialog_input "$cp" \ - "$_extras" - ) && _extras="$cp" ;; + f_dialog_input cp "$cp" "$_extras" \ + && _extras="$cp" ;; esac done diff --git a/usr.sbin/bsdconfig/share/mustberoot.subr b/usr.sbin/bsdconfig/share/mustberoot.subr index 3f23e11..bf8dd67 100644 --- a/usr.sbin/bsdconfig/share/mustberoot.subr +++ b/usr.sbin/bsdconfig/share/mustberoot.subr @@ -177,8 +177,7 @@ f_become_root_via_sudo() [ $retval -eq 255 ] && f_die $retval "$password" else - local dialog_inputbox - dialog_inputbox=$( $DIALOG \ + password=$( $DIALOG \ --title "$DIALOG_TITLE" \ --backtitle "$DIALOG_BACKTITLE" \ --hline "$hline" \ @@ -188,14 +187,9 @@ f_become_root_via_sudo() --passwordbox "$msg" \ $height $width \ 2>&1 >&$DIALOG_TERMINAL_PASSTHRU_FD - ) - retval=$? - setvar DIALOG_INPUTBOX_$$ "$dialog_inputbox" - password=$( f_dialog_inputstr ) + ) || exit $? fi - - # Exit if the user cancelled. - [ $retval -eq $SUCCESS ] || exit $retval + debug= f_dialog_line_sanitize password # # Validate sudo(8) credentials diff --git a/usr.sbin/bsdconfig/share/variable.subr b/usr.sbin/bsdconfig/share/variable.subr index f51ebd6..079c564 100644 --- a/usr.sbin/bsdconfig/share/variable.subr +++ b/usr.sbin/bsdconfig/share/variable.subr @@ -93,8 +93,7 @@ f_variable_get_value() if ! { f_getvar $var cp && ! f_interactive; }; then shift 1 # var - cp=$( f_dialog_input "$( printf "$@" )" "$cp" ) && - setvar $var "$cp" + f_dialog_input cp "$( printf "$@" )" "$cp" && setvar $var "$cp" fi return $SUCCESS |