variable.subr revision 245052
11556Srgrimesif [ ! "$_VARIABLE_SUBR" ]; then _VARIABLE_SUBR=1
21556Srgrimes#
31556Srgrimes# Copyright (c) 2012 Devin Teske
41556Srgrimes# All Rights Reserved.
51556Srgrimes#
61556Srgrimes# Redistribution and use in source and binary forms, with or without
71556Srgrimes# modification, are permitted provided that the following conditions
81556Srgrimes# are met:
91556Srgrimes# 1. Redistributions of source code must retain the above copyright
101556Srgrimes#    notice, this list of conditions and the following disclaimer.
111556Srgrimes# 2. Redistributions in binary form must reproduce the above copyright
121556Srgrimes#    notice, this list of conditions and the following disclaimer in the
131556Srgrimes#    documentation and/or other materials provided with the distribution.
141556Srgrimes#
151556Srgrimes# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
161556Srgrimes# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
171556Srgrimes# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181556Srgrimes# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191556Srgrimes# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201556Srgrimes# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211556Srgrimes# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221556Srgrimes# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231556Srgrimes# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241556Srgrimes# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251556Srgrimes# SUCH DAMAGE.
261556Srgrimes#
271556Srgrimes# $FreeBSD: head/usr.sbin/bsdconfig/share/variable.subr 245052 2013-01-05 02:08:47Z dteske $
281556Srgrimes#
291556Srgrimes############################################################ INCLUDES
301556Srgrimes
311556SrgrimesBSDCFG_SHARE="/usr/share/bsdconfig"
323044Sdg. $BSDCFG_SHARE/common.subr || exit 1
338168Sbdef_dprintf "%s: loading includes..." variable.subr
341556Srgrimesf_include $BSDCFG_SHARE/dialog.subr
351556Srgrimes
361556Srgrimes############################################################ GLOBALS
371556Srgrimes
381556SrgrimesVARIABLES=
391556Srgrimes
401556Srgrimes#
411556Srgrimes# Default behavior is to call f_variable_set_defaults() when loaded.
421556Srgrimes#
431556Srgrimes: ${VARIABLE_SELF_INITIALIZE=1}
441556Srgrimes
451556Srgrimes#
461556Srgrimes# File to write when f_dump_variables() is called.
471556Srgrimes#
481556Srgrimes: ${VARIABLE_DUMPFILE:=/etc/bsdconfig.vars}
491556Srgrimes
501556Srgrimes############################################################ FUNCTIONS
511556Srgrimes
521556Srgrimes# f_variable_new $handle $variable
531556Srgrimes#
541556Srgrimes# Register a new variable named $variable with the given reference-handle
551556Srgrimes# $handle. The environment variable $handle is set to $variable allowing you to
561556Srgrimes# use the f_getvar() function (from common.subr) with $handle to get the value
571556Srgrimes# of environment variable $variable. For example:
581556Srgrimes#
591556Srgrimes# 	f_variable_new VAR_ABC abc
601556Srgrimes#
611556Srgrimes# allows the later indirection:
621556Srgrimes#
631556Srgrimes# 	f_getvar $VAR_ABC
641556Srgrimes#
651556Srgrimes# to return the value of environment variable `abc'. Variables registered in
661556Srgrimes# this manner are recorded in the $VARIABLES environment variable for later
671556Srgrimes# allowing dynamic enumeration of so-called `registered/advertised' variables.
681556Srgrimes#
691556Srgrimesf_variable_new()
701556Srgrimes{
711556Srgrimes	local handle="$1" variable="$2"
721556Srgrimes	[ "$handle" ] || return $FAILURE
731556Srgrimes	f_dprintf "variable.subr: New variable %s -> %s" "$handle" "$variable"
741556Srgrimes	setvar $handle $variable
751556Srgrimes	VARIABLES="$VARIABLES${VARIABLES:+ }$handle"
761556Srgrimes}
771556Srgrimes
781556Srgrimes# f_variable_get_value $var [ $fmt [ $opts ... ] ]
791556Srgrimes#
801556Srgrimes# Unless nonInteractive is set, prompt the user with a given value (pre-filled
811556Srgrimes# with the value of $var) and give them the chance to change the value.
821556Srgrimes#
831556Srgrimes# Unlike f_getvar() (from common.subr) which can return a variable to the
841556Srgrimes# caller on standard output, this function has no [meaningful] output.
851556Srgrimes#
861556Srgrimes# Returns success unless $var is either NULL or missing.
871556Srgrimes#
881556Srgrimesf_variable_get_value()
891556Srgrimes{
901556Srgrimes	local var="$1" cp
911556Srgrimes
921556Srgrimes	[ "$var" ] || return $FAILURE
931556Srgrimes
941556Srgrimes	if ! { f_getvar $var cp && ! f_interactive; }; then
951556Srgrimes		shift 1 # var
961556Srgrimes		cp=$( f_dialog_input "$( printf "$@" )" "$cp" ) &&
971556Srgrimes			setvar $var "$cp"
981556Srgrimes	fi
991556Srgrimes
1001556Srgrimes	return $SUCCESS
1011556Srgrimes}
1021556Srgrimes
1031556Srgrimes# f_variable_set_defaults
1041556Srgrimes#
1051556Srgrimes# Installs sensible defaults for registered/advertised variables.
1061556Srgrimes#
1071556Srgrimesf_variable_set_defaults()
1081556Srgrimes{
1091556Srgrimes	#
1101556Srgrimes	# Initialize various user-edittable values to their defaults
1111556Srgrimes	#
1121556Srgrimes	setvar $VAR_RELNAME "$UNAME_R"
1131556Srgrimes
1141556Srgrimes	f_dprintf "f_variable_set_defaults: Defaults initialized."
1151556Srgrimes}
1161556Srgrimes
1171556Srgrimes# f_dump_variables
1181556Srgrimes#
1191556Srgrimes# Dump a list of registered/advertised variables and their respective values to
1201556Srgrimes# $VARIABLE_DUMPFILE. Returns success unless the file couldn't be written. If
1211556Srgrimes# an error occurs, it is displayed using f_show_msg() (from common.subr).
1221556Srgrimes#
1231556Srgrimesf_dump_variables()
1241556Srgrimes{
1251556Srgrimes	local err sanitize_awk="{ gsub(/'/, \"'\\\\''\"); print }"
1261556Srgrimes	if ! err=$(
1271556Srgrimes		( for handle in $VARIABLES; do
1281556Srgrimes			f_getvar $handle var || continue
1291556Srgrimes			f_getvar $var value || continue
1301556Srgrimes			value=$( echo "$value" | awk "$sanitize_awk" )
1311556Srgrimes			printf "%s='%s'\n" "$var" "$value"
1321556Srgrimes		  done > "$VARIABLE_DUMPFILE" ) 2>&1
1331556Srgrimes	); then
1341556Srgrimes		f_show_msg "%s" "$err"
1351556Srgrimes		return $FAILURE
1361556Srgrimes	fi
1371556Srgrimes}
1381556Srgrimes
1391556Srgrimes# f_debugging
1401556Srgrimes#
1411556Srgrimes# Are we in debug mode? Returns success if extra DEBUG information has been
1421556Srgrimes# requested (by setting $debug to non-NULL), otherwise false.
1438168Sbde#
1441556Srgrimesf_debugging()
1451556Srgrimes{
1461556Srgrimes	local value
1471556Srgrimes	f_getvar $VAR_DEBUG value && [ "$value" ]
1481556Srgrimes}
1491556Srgrimes
1501556Srgrimes# f_interactive()
1511556Srgrimes#
1521556Srgrimes# Are we running interactively? Return error if $nonInteractive is set and non-
1531556Srgrimes# NULL, otherwise return success.
1541556Srgrimes#
1551556Srgrimesf_interactive()
1561556Srgrimes{
1571556Srgrimes	local value
1581556Srgrimes	! f_getvar $VAR_NONINTERACTIVE value || [ ! "$value" ]
1591556Srgrimes}
1601556Srgrimes
1611556Srgrimes############################################################ MAIN
1621556Srgrimes
1631556Srgrimes#
1641556Srgrimes# 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