variable.subr revision 245052
1100616Smpif [ ! "$_VARIABLE_SUBR" ]; then _VARIABLE_SUBR=1 259243Sobrien# 359243Sobrien# Copyright (c) 2012 Devin Teske 459243Sobrien# All Rights Reserved. 559243Sobrien# 659243Sobrien# Redistribution and use in source and binary forms, with or without 759243Sobrien# modification, are permitted provided that the following conditions 859243Sobrien# are met: 959243Sobrien# 1. Redistributions of source code must retain the above copyright 1059243Sobrien# notice, this list of conditions and the following disclaimer. 1159243Sobrien# 2. Redistributions in binary form must reproduce the above copyright 1259243Sobrien# notice, this list of conditions and the following disclaimer in the 1359243Sobrien# documentation and/or other materials provided with the distribution. 1459243Sobrien# 1559243Sobrien# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1659243Sobrien# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 17100616Smp# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1859243Sobrien# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1959243Sobrien# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2059243Sobrien# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2159243Sobrien# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2259243Sobrien# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2359243Sobrien# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2459243Sobrien# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2559243Sobrien# SUCH DAMAGE. 2659243Sobrien# 2759243Sobrien# $FreeBSD: head/usr.sbin/bsdconfig/share/variable.subr 245052 2013-01-05 02:08:47Z dteske $ 2859243Sobrien# 2959243Sobrien############################################################ INCLUDES 3059243Sobrien 3159243SobrienBSDCFG_SHARE="/usr/share/bsdconfig" 3259243Sobrien. $BSDCFG_SHARE/common.subr || exit 1 3359243Sobrienf_dprintf "%s: loading includes..." variable.subr 3459243Sobrienf_include $BSDCFG_SHARE/dialog.subr 3559243Sobrien 3669408Sache############################################################ GLOBALS 3759243Sobrien 3869408SacheVARIABLES= 3959243Sobrien 4059243Sobrien# 4159243Sobrien# Default behavior is to call f_variable_set_defaults() when loaded. 4259243Sobrien# 4359243Sobrien: ${VARIABLE_SELF_INITIALIZE=1} 4459243Sobrien 4559243Sobrien# 4659243Sobrien# File to write when f_dump_variables() is called. 4759243Sobrien# 4859243Sobrien: ${VARIABLE_DUMPFILE:=/etc/bsdconfig.vars} 4959243Sobrien 5069408Sache############################################################ FUNCTIONS 5169408Sache 5269408Sache# f_variable_new $handle $variable 5369408Sache# 5459243Sobrien# Register a new variable named $variable with the given reference-handle 5559243Sobrien# $handle. The environment variable $handle is set to $variable allowing you to 5659243Sobrien# use the f_getvar() function (from common.subr) with $handle to get the value 5759243Sobrien# of environment variable $variable. For example: 5859243Sobrien# 5959243Sobrien# f_variable_new VAR_ABC abc 6059243Sobrien# 6159243Sobrien# allows the later indirection: 6259243Sobrien# 6359243Sobrien# f_getvar $VAR_ABC 6459243Sobrien# 6559243Sobrien# to return the value of environment variable `abc'. Variables registered in 6659243Sobrien# this manner are recorded in the $VARIABLES environment variable for later 6759243Sobrien# allowing dynamic enumeration of so-called `registered/advertised' variables. 6859243Sobrien# 6959243Sobrienf_variable_new() 7059243Sobrien{ 7159243Sobrien local handle="$1" variable="$2" 7259243Sobrien [ "$handle" ] || return $FAILURE 7359243Sobrien f_dprintf "variable.subr: New variable %s -> %s" "$handle" "$variable" 7459243Sobrien setvar $handle $variable 7559243Sobrien VARIABLES="$VARIABLES${VARIABLES:+ }$handle" 7659243Sobrien} 7759243Sobrien 7859243Sobrien# f_variable_get_value $var [ $fmt [ $opts ... ] ] 7959243Sobrien# 8059243Sobrien# Unless nonInteractive is set, prompt the user with a given value (pre-filled 8159243Sobrien# with the value of $var) and give them the chance to change the value. 8259243Sobrien# 8359243Sobrien# Unlike f_getvar() (from common.subr) which can return a variable to the 8459243Sobrien# caller on standard output, this function has no [meaningful] output. 8569408Sache# 8659243Sobrien# Returns success unless $var is either NULL or missing. 8769408Sache# 8859243Sobrienf_variable_get_value() 8959243Sobrien{ 9059243Sobrien local var="$1" cp 9159243Sobrien 9259243Sobrien [ "$var" ] || return $FAILURE 9359243Sobrien 9459243Sobrien if ! { f_getvar $var cp && ! f_interactive; }; then 9559243Sobrien shift 1 # var 9659243Sobrien cp=$( f_dialog_input "$( printf "$@" )" "$cp" ) && 9759243Sobrien setvar $var "$cp" 9859243Sobrien fi 9959243Sobrien 10059243Sobrien return $SUCCESS 10159243Sobrien} 10259243Sobrien 10359243Sobrien# f_variable_set_defaults 10459243Sobrien# 10559243Sobrien# Installs sensible defaults for registered/advertised variables. 10659243Sobrien# 10759243Sobrienf_variable_set_defaults() 10859243Sobrien{ 10959243Sobrien # 11059243Sobrien # Initialize various user-edittable values to their defaults 11159243Sobrien # 11259243Sobrien setvar $VAR_RELNAME "$UNAME_R" 11359243Sobrien 11459243Sobrien f_dprintf "f_variable_set_defaults: Defaults initialized." 11559243Sobrien} 11659243Sobrien 11759243Sobrien# f_dump_variables 11859243Sobrien# 11959243Sobrien# Dump a list of registered/advertised variables and their respective values to 12059243Sobrien# $VARIABLE_DUMPFILE. Returns success unless the file couldn't be written. If 12159243Sobrien# an error occurs, it is displayed using f_show_msg() (from common.subr). 12259243Sobrien# 12359243Sobrienf_dump_variables() 12459243Sobrien{ 12559243Sobrien local err sanitize_awk="{ gsub(/'/, \"'\\\\''\"); print }" 12659243Sobrien if ! err=$( 12759243Sobrien ( for handle in $VARIABLES; do 12859243Sobrien f_getvar $handle var || continue 12959243Sobrien f_getvar $var value || continue 13059243Sobrien value=$( echo "$value" | awk "$sanitize_awk" ) 13159243Sobrien printf "%s='%s'\n" "$var" "$value" 13259243Sobrien done > "$VARIABLE_DUMPFILE" ) 2>&1 13359243Sobrien ); then 13459243Sobrien f_show_msg "%s" "$err" 13559243Sobrien return $FAILURE 13659243Sobrien fi 13759243Sobrien} 13859243Sobrien 13959243Sobrien# f_debugging 14059243Sobrien# 14159243Sobrien# Are we in debug mode? Returns success if extra DEBUG information has been 14259243Sobrien# requested (by setting $debug to non-NULL), otherwise false. 14359243Sobrien# 14459243Sobrienf_debugging() 14559243Sobrien{ 14659243Sobrien local value 14759243Sobrien f_getvar $VAR_DEBUG value && [ "$value" ] 14859243Sobrien} 14959243Sobrien 15059243Sobrien# f_interactive() 15159243Sobrien# 15259243Sobrien# Are we running interactively? Return error if $nonInteractive is set and non- 15359243Sobrien# NULL, otherwise return success. 15459243Sobrien# 15559243Sobrienf_interactive() 15659243Sobrien{ 15759243Sobrien local value 15859243Sobrien ! f_getvar $VAR_NONINTERACTIVE value || [ ! "$value" ] 15959243Sobrien} 16059243Sobrien 16159243Sobrien############################################################ MAIN 16259243Sobrien 16359243Sobrien# 16459243Sobrien# Variables that can be tweaked from config files 16559243Sobrien# 166100616Smpf_variable_new VAR_CONFIG_FILE configFile 167100616Smpf_variable_new VAR_DEBUG debug 168100616Smpf_variable_new VAR_DEBUG_FILE debugFile 16959243Sobrienf_variable_new VAR_NO_ERROR noError 17059243Sobrienf_variable_new VAR_NONINTERACTIVE nonInteractive 17159243Sobrienf_variable_new VAR_RELNAME releaseName 17259243Sobrien 17359243Sobrien# 17459243Sobrien# Self-initialize unless requested otherwise 17559243Sobrien# 17659243Sobrienf_dprintf "%s: VARIABLE_SELF_INITIALIZE=[%s]" \ 17759243Sobrien variable.subr "$VARIABLE_SELF_INITIALIZE" 17859243Sobriencase "$VARIABLE_SELF_INITIALIZE" in 17959243Sobrien""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;; 18059243Sobrien*) f_variable_set_defaults 18159243Sobrienesac 18259243Sobrien 18359243Sobrienf_dprintf "%s: Successfully loaded." variable.subr 18459243Sobrien 18559243Sobrienfi # ! $_VARIABLE_SUBR 18659243Sobrien