variable.subr revision 245437
1if [ ! "$_VARIABLE_SUBR" ]; then _VARIABLE_SUBR=1 2# 3# Copyright (c) 2012 Devin Teske 4# All Rights Reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 9# 1. Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# 2. Redistributions in binary form must reproduce the above copyright 12# notice, this list of conditions and the following disclaimer in the 13# documentation and/or other materials provided with the distribution. 14# 15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25# SUCH DAMAGE. 26# 27# $FreeBSD: head/usr.sbin/bsdconfig/share/variable.subr 245437 2013-01-14 21:03:34Z dteske $ 28# 29############################################################ INCLUDES 30 31BSDCFG_SHARE="/usr/share/bsdconfig" 32. $BSDCFG_SHARE/common.subr || exit 1 33f_dprintf "%s: loading includes..." variable.subr 34f_include $BSDCFG_SHARE/dialog.subr 35 36############################################################ GLOBALS 37 38VARIABLES= 39 40# 41# Default behavior is to call f_variable_set_defaults() when loaded. 42# 43: ${VARIABLE_SELF_INITIALIZE=1} 44 45# 46# File to write when f_dump_variables() is called. 47# 48: ${VARIABLE_DUMPFILE:=/etc/bsdconfig.vars} 49 50############################################################ FUNCTIONS 51 52# f_variable_new $handle $variable 53# 54# Register a new variable named $variable with the given reference-handle 55# $handle. The environment variable $handle is set to $variable allowing you to 56# use the f_getvar() function (from common.subr) with $handle to get the value 57# of environment variable $variable. For example: 58# 59# f_variable_new VAR_ABC abc 60# 61# allows the later indirection: 62# 63# f_getvar $VAR_ABC 64# 65# to return the value of environment variable `abc'. Variables registered in 66# this manner are recorded in the $VARIABLES environment variable for later 67# allowing dynamic enumeration of so-called `registered/advertised' variables. 68# 69f_variable_new() 70{ 71 local handle="$1" variable="$2" 72 [ "$handle" ] || return $FAILURE 73 f_dprintf "variable.subr: New variable %s -> %s" "$handle" "$variable" 74 setvar $handle $variable 75 VARIABLES="$VARIABLES${VARIABLES:+ }$handle" 76} 77 78# f_variable_get_value $var [ $fmt [ $opts ... ] ] 79# 80# Unless nonInteractive is set, prompt the user with a given value (pre-filled 81# with the value of $var) and give them the chance to change the value. 82# 83# Unlike f_getvar() (from common.subr) which can return a variable to the 84# caller on standard output, this function has no [meaningful] output. 85# 86# Returns success unless $var is either NULL or missing. 87# 88f_variable_get_value() 89{ 90 local var="$1" cp 91 92 [ "$var" ] || return $FAILURE 93 94 if ! { f_getvar $var cp && ! f_interactive; }; then 95 shift 1 # var 96 cp=$( f_dialog_input "$( printf "$@" )" "$cp" ) && 97 setvar $var "$cp" 98 fi 99 100 return $SUCCESS 101} 102 103# f_variable_set_defaults 104# 105# Installs sensible defaults for registered/advertised variables. 106# 107f_variable_set_defaults() 108{ 109 # 110 # Initialize various user-edittable values to their defaults 111 # 112 setvar $VAR_RELNAME "$UNAME_R" 113 114 f_dprintf "f_variable_set_defaults: Defaults initialized." 115} 116 117# f_dump_variables 118# 119# Dump a list of registered/advertised variables and their respective values to 120# $VARIABLE_DUMPFILE. Returns success unless the file couldn't be written. If 121# an error occurs, it is displayed using f_dialog_msgbox() (from dialog.subr). 122# 123f_dump_variables() 124{ 125 local err sanitize_awk="{ gsub(/'/, \"'\\\\''\"); print }" 126 if ! err=$( 127 ( for handle in $VARIABLES; do 128 f_getvar $handle var || continue 129 f_getvar $var value || continue 130 value=$( echo "$value" | awk "$sanitize_awk" ) 131 printf "%s='%s'\n" "$var" "$value" 132 done > "$VARIABLE_DUMPFILE" ) 2>&1 133 ); then 134 f_dialog_msgbox "$err" 135 return $FAILURE 136 fi 137} 138 139# f_debugging 140# 141# Are we in debug mode? Returns success if extra DEBUG information has been 142# requested (by setting $debug to non-NULL), otherwise false. 143# 144f_debugging() 145{ 146 local value 147 f_getvar $VAR_DEBUG value && [ "$value" ] 148} 149 150# f_interactive() 151# 152# Are we running interactively? Return error if $nonInteractive is set and non- 153# NULL, otherwise return success. 154# 155f_interactive() 156{ 157 local value 158 ! f_getvar $VAR_NONINTERACTIVE value || [ ! "$value" ] 159} 160 161############################################################ MAIN 162 163# 164# Variables that can be tweaked from config files 165# 166f_variable_new VAR_CONFIG_FILE configFile 167f_variable_new VAR_DEBUG debug 168f_variable_new VAR_DEBUG_FILE debugFile 169f_variable_new VAR_NO_ERROR noError 170f_variable_new VAR_NONINTERACTIVE nonInteractive 171f_variable_new VAR_RELNAME releaseName 172 173# 174# Self-initialize unless requested otherwise 175# 176f_dprintf "%s: VARIABLE_SELF_INITIALIZE=[%s]" \ 177 variable.subr "$VARIABLE_SELF_INITIALIZE" 178case "$VARIABLE_SELF_INITIALIZE" in 179""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;; 180*) f_variable_set_defaults 181esac 182 183f_dprintf "%s: Successfully loaded." variable.subr 184 185fi # ! $_VARIABLE_SUBR 186