variable.subr revision 245437
1245052Sdteskeif [ ! "$_VARIABLE_SUBR" ]; then _VARIABLE_SUBR=1 2245052Sdteske# 3245052Sdteske# Copyright (c) 2012 Devin Teske 4245052Sdteske# All Rights Reserved. 5245052Sdteske# 6245052Sdteske# Redistribution and use in source and binary forms, with or without 7245052Sdteske# modification, are permitted provided that the following conditions 8245052Sdteske# are met: 9245052Sdteske# 1. Redistributions of source code must retain the above copyright 10245052Sdteske# notice, this list of conditions and the following disclaimer. 11245052Sdteske# 2. Redistributions in binary form must reproduce the above copyright 12245052Sdteske# notice, this list of conditions and the following disclaimer in the 13245052Sdteske# documentation and/or other materials provided with the distribution. 14245052Sdteske# 15245052Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16245052Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 17245052Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18245052Sdteske# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19245052Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20245052Sdteske# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21245052Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22245052Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23245052Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24245052Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25245052Sdteske# SUCH DAMAGE. 26245052Sdteske# 27245052Sdteske# $FreeBSD: head/usr.sbin/bsdconfig/share/variable.subr 245437 2013-01-14 21:03:34Z dteske $ 28245052Sdteske# 29245052Sdteske############################################################ INCLUDES 30245052Sdteske 31245052SdteskeBSDCFG_SHARE="/usr/share/bsdconfig" 32245052Sdteske. $BSDCFG_SHARE/common.subr || exit 1 33245052Sdteskef_dprintf "%s: loading includes..." variable.subr 34245052Sdteskef_include $BSDCFG_SHARE/dialog.subr 35245052Sdteske 36245052Sdteske############################################################ GLOBALS 37245052Sdteske 38245052SdteskeVARIABLES= 39245052Sdteske 40245052Sdteske# 41245052Sdteske# Default behavior is to call f_variable_set_defaults() when loaded. 42245052Sdteske# 43245052Sdteske: ${VARIABLE_SELF_INITIALIZE=1} 44245052Sdteske 45245052Sdteske# 46245052Sdteske# File to write when f_dump_variables() is called. 47245052Sdteske# 48245052Sdteske: ${VARIABLE_DUMPFILE:=/etc/bsdconfig.vars} 49245052Sdteske 50245052Sdteske############################################################ FUNCTIONS 51245052Sdteske 52245052Sdteske# f_variable_new $handle $variable 53245052Sdteske# 54245052Sdteske# Register a new variable named $variable with the given reference-handle 55245052Sdteske# $handle. The environment variable $handle is set to $variable allowing you to 56245052Sdteske# use the f_getvar() function (from common.subr) with $handle to get the value 57245052Sdteske# of environment variable $variable. For example: 58245052Sdteske# 59245052Sdteske# f_variable_new VAR_ABC abc 60245052Sdteske# 61245052Sdteske# allows the later indirection: 62245052Sdteske# 63245052Sdteske# f_getvar $VAR_ABC 64245052Sdteske# 65245052Sdteske# to return the value of environment variable `abc'. Variables registered in 66245052Sdteske# this manner are recorded in the $VARIABLES environment variable for later 67245052Sdteske# allowing dynamic enumeration of so-called `registered/advertised' variables. 68245052Sdteske# 69245052Sdteskef_variable_new() 70245052Sdteske{ 71245052Sdteske local handle="$1" variable="$2" 72245052Sdteske [ "$handle" ] || return $FAILURE 73245052Sdteske f_dprintf "variable.subr: New variable %s -> %s" "$handle" "$variable" 74245052Sdteske setvar $handle $variable 75245052Sdteske VARIABLES="$VARIABLES${VARIABLES:+ }$handle" 76245052Sdteske} 77245052Sdteske 78245052Sdteske# f_variable_get_value $var [ $fmt [ $opts ... ] ] 79245052Sdteske# 80245052Sdteske# Unless nonInteractive is set, prompt the user with a given value (pre-filled 81245052Sdteske# with the value of $var) and give them the chance to change the value. 82245052Sdteske# 83245052Sdteske# Unlike f_getvar() (from common.subr) which can return a variable to the 84245052Sdteske# caller on standard output, this function has no [meaningful] output. 85245052Sdteske# 86245052Sdteske# Returns success unless $var is either NULL or missing. 87245052Sdteske# 88245052Sdteskef_variable_get_value() 89245052Sdteske{ 90245052Sdteske local var="$1" cp 91245052Sdteske 92245052Sdteske [ "$var" ] || return $FAILURE 93245052Sdteske 94245052Sdteske if ! { f_getvar $var cp && ! f_interactive; }; then 95245052Sdteske shift 1 # var 96245052Sdteske cp=$( f_dialog_input "$( printf "$@" )" "$cp" ) && 97245052Sdteske setvar $var "$cp" 98245052Sdteske fi 99245052Sdteske 100245052Sdteske return $SUCCESS 101245052Sdteske} 102245052Sdteske 103245052Sdteske# f_variable_set_defaults 104245052Sdteske# 105245052Sdteske# Installs sensible defaults for registered/advertised variables. 106245052Sdteske# 107245052Sdteskef_variable_set_defaults() 108245052Sdteske{ 109245052Sdteske # 110245052Sdteske # Initialize various user-edittable values to their defaults 111245052Sdteske # 112245052Sdteske setvar $VAR_RELNAME "$UNAME_R" 113245052Sdteske 114245052Sdteske f_dprintf "f_variable_set_defaults: Defaults initialized." 115245052Sdteske} 116245052Sdteske 117245052Sdteske# f_dump_variables 118245052Sdteske# 119245052Sdteske# Dump a list of registered/advertised variables and their respective values to 120245052Sdteske# $VARIABLE_DUMPFILE. Returns success unless the file couldn't be written. If 121245437Sdteske# an error occurs, it is displayed using f_dialog_msgbox() (from dialog.subr). 122245052Sdteske# 123245052Sdteskef_dump_variables() 124245052Sdteske{ 125245052Sdteske local err sanitize_awk="{ gsub(/'/, \"'\\\\''\"); print }" 126245052Sdteske if ! err=$( 127245052Sdteske ( for handle in $VARIABLES; do 128245052Sdteske f_getvar $handle var || continue 129245052Sdteske f_getvar $var value || continue 130245052Sdteske value=$( echo "$value" | awk "$sanitize_awk" ) 131245052Sdteske printf "%s='%s'\n" "$var" "$value" 132245052Sdteske done > "$VARIABLE_DUMPFILE" ) 2>&1 133245052Sdteske ); then 134245437Sdteske f_dialog_msgbox "$err" 135245052Sdteske return $FAILURE 136245052Sdteske fi 137245052Sdteske} 138245052Sdteske 139245052Sdteske# f_debugging 140245052Sdteske# 141245052Sdteske# Are we in debug mode? Returns success if extra DEBUG information has been 142245052Sdteske# requested (by setting $debug to non-NULL), otherwise false. 143245052Sdteske# 144245052Sdteskef_debugging() 145245052Sdteske{ 146245052Sdteske local value 147245052Sdteske f_getvar $VAR_DEBUG value && [ "$value" ] 148245052Sdteske} 149245052Sdteske 150245052Sdteske# f_interactive() 151245052Sdteske# 152245052Sdteske# Are we running interactively? Return error if $nonInteractive is set and non- 153245052Sdteske# NULL, otherwise return success. 154245052Sdteske# 155245052Sdteskef_interactive() 156245052Sdteske{ 157245052Sdteske local value 158245052Sdteske ! f_getvar $VAR_NONINTERACTIVE value || [ ! "$value" ] 159245052Sdteske} 160245052Sdteske 161245052Sdteske############################################################ MAIN 162245052Sdteske 163245052Sdteske# 164245052Sdteske# Variables that can be tweaked from config files 165245052Sdteske# 166245052Sdteskef_variable_new VAR_CONFIG_FILE configFile 167245052Sdteskef_variable_new VAR_DEBUG debug 168245052Sdteskef_variable_new VAR_DEBUG_FILE debugFile 169245052Sdteskef_variable_new VAR_NO_ERROR noError 170245052Sdteskef_variable_new VAR_NONINTERACTIVE nonInteractive 171245052Sdteskef_variable_new VAR_RELNAME releaseName 172245052Sdteske 173245052Sdteske# 174245052Sdteske# Self-initialize unless requested otherwise 175245052Sdteske# 176245052Sdteskef_dprintf "%s: VARIABLE_SELF_INITIALIZE=[%s]" \ 177245052Sdteske variable.subr "$VARIABLE_SELF_INITIALIZE" 178245052Sdteskecase "$VARIABLE_SELF_INITIALIZE" in 179245052Sdteske""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;; 180245052Sdteske*) f_variable_set_defaults 181245052Sdteskeesac 182245052Sdteske 183245052Sdteskef_dprintf "%s: Successfully loaded." variable.subr 184245052Sdteske 185245052Sdteskefi # ! $_VARIABLE_SUBR 186