variable.subr revision 250323
1245052Sdteskeif [ ! "$_VARIABLE_SUBR" ]; then _VARIABLE_SUBR=1 2245052Sdteske# 3247280Sdteske# Copyright (c) 2012-2013 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 250323 2013-05-07 05:40:20Z 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 # 112247280Sdteske setvar $VAR_EDITOR "${EDITOR:-/usr/bin/ee}" 113247280Sdteske setvar $VAR_FTP_STATE "passive" 114247280Sdteske setvar $VAR_FTP_USER "ftp" 115247280Sdteske setvar $VAR_HOSTNAME "$( hostname )" 116247280Sdteske setvar $VAR_MEDIA_TIMEOUT "300" 117247280Sdteske setvar $VAR_NFS_SECURE "NO" 118247280Sdteske setvar $VAR_NFS_TCP "NO" 119247280Sdteske setvar $VAR_NFS_V3 "YES" 120250323Sdteske setvar $VAR_PKG_TMPDIR "/var/tmp" 121247280Sdteske setvar $VAR_RELNAME "$UNAME_R" 122245052Sdteske 123245052Sdteske f_dprintf "f_variable_set_defaults: Defaults initialized." 124245052Sdteske} 125245052Sdteske 126245052Sdteske# f_dump_variables 127245052Sdteske# 128245052Sdteske# Dump a list of registered/advertised variables and their respective values to 129245052Sdteske# $VARIABLE_DUMPFILE. Returns success unless the file couldn't be written. If 130245437Sdteske# an error occurs, it is displayed using f_dialog_msgbox() (from dialog.subr). 131245052Sdteske# 132245052Sdteskef_dump_variables() 133245052Sdteske{ 134245052Sdteske local err sanitize_awk="{ gsub(/'/, \"'\\\\''\"); print }" 135245052Sdteske if ! err=$( 136245052Sdteske ( for handle in $VARIABLES; do 137245052Sdteske f_getvar $handle var || continue 138245052Sdteske f_getvar $var value || continue 139245052Sdteske value=$( echo "$value" | awk "$sanitize_awk" ) 140245052Sdteske printf "%s='%s'\n" "$var" "$value" 141245052Sdteske done > "$VARIABLE_DUMPFILE" ) 2>&1 142245052Sdteske ); then 143245437Sdteske f_dialog_msgbox "$err" 144245052Sdteske return $FAILURE 145245052Sdteske fi 146245052Sdteske} 147245052Sdteske 148245052Sdteske# f_debugging 149245052Sdteske# 150245052Sdteske# Are we in debug mode? Returns success if extra DEBUG information has been 151245052Sdteske# requested (by setting $debug to non-NULL), otherwise false. 152245052Sdteske# 153245052Sdteskef_debugging() 154245052Sdteske{ 155245052Sdteske local value 156245052Sdteske f_getvar $VAR_DEBUG value && [ "$value" ] 157245052Sdteske} 158245052Sdteske 159245052Sdteske# f_interactive() 160245052Sdteske# 161245052Sdteske# Are we running interactively? Return error if $nonInteractive is set and non- 162245052Sdteske# NULL, otherwise return success. 163245052Sdteske# 164245052Sdteskef_interactive() 165245052Sdteske{ 166245052Sdteske local value 167245052Sdteske ! f_getvar $VAR_NONINTERACTIVE value || [ ! "$value" ] 168245052Sdteske} 169245052Sdteske 170247280Sdteske# f_netinteractive() 171247280Sdteske# 172247280Sdteske# Has the user specifically requested the network-portion of configuration and 173247280Sdteske# setup to be performed interactively? Returns success if the user has asked 174247280Sdteske# for the network configuration to be done interactively even if perhaps over- 175247280Sdteske# all non-interactive mode has been requested (by setting nonInteractive). 176247280Sdteske# 177247280Sdteske# Returns success if $netInteractive is set and non-NULL. 178247280Sdteske# 179247280Sdteskef_netinteractive() 180247280Sdteske{ 181247280Sdteske local value 182247280Sdteske f_getvar $VAR_NETINTERACTIVE value && [ "$value" ] 183247280Sdteske} 184247280Sdteske 185245052Sdteske############################################################ MAIN 186245052Sdteske 187245052Sdteske# 188245052Sdteske# Variables that can be tweaked from config files 189245052Sdteske# 190247280Sdteske# Handle Variable Name 191245052Sdteskef_variable_new VAR_CONFIG_FILE configFile 192245052Sdteskef_variable_new VAR_DEBUG debug 193245052Sdteskef_variable_new VAR_DEBUG_FILE debugFile 194247280Sdteskef_variable_new VAR_DIRECTORY_PATH _directoryPath 195247280Sdteskef_variable_new VAR_DOMAINNAME domainname 196247280Sdteskef_variable_new VAR_EDITOR editor 197247280Sdteskef_variable_new VAR_EXTRAS ifconfig_ 198247280Sdteskef_variable_new VAR_FTP_DIR ftpDirectory 199247280Sdteskef_variable_new VAR_FTP_HOST ftpHost 200247280Sdteskef_variable_new VAR_FTP_PASS ftpPass 201247280Sdteskef_variable_new VAR_FTP_PATH _ftpPath 202247280Sdteskef_variable_new VAR_FTP_PORT ftpPort 203247280Sdteskef_variable_new VAR_FTP_STATE ftpState 204247280Sdteskef_variable_new VAR_FTP_USER ftpUser 205247280Sdteskef_variable_new VAR_GATEWAY defaultrouter 206247280Sdteskef_variable_new VAR_HOSTNAME hostname 207247280Sdteskef_variable_new VAR_HTTP_FTP_MODE httpFtpMode 208247280Sdteskef_variable_new VAR_HTTP_PROXY httpProxy 209247280Sdteskef_variable_new VAR_HTTP_PROXY_HOST httpProxyHost 210247280Sdteskef_variable_new VAR_HTTP_PROXY_PATH _httpProxyPath 211247280Sdteskef_variable_new VAR_HTTP_PROXY_PORT httpProxyPort 212247280Sdteskef_variable_new VAR_IFCONFIG ifconfig_ 213247280Sdteskef_variable_new VAR_IPADDR ipaddr 214247280Sdteskef_variable_new VAR_IPV6ADDR ipv6addr 215247280Sdteskef_variable_new VAR_IPV6_ENABLE ipv6_activate_all_interfaces 216247280Sdteskef_variable_new VAR_MEDIA_TIMEOUT MEDIA_TIMEOUT 217247280Sdteskef_variable_new VAR_MEDIA_TYPE mediaType 218247280Sdteskef_variable_new VAR_NAMESERVER nameserver 219247280Sdteskef_variable_new VAR_NETINTERACTIVE netInteractive 220247280Sdteskef_variable_new VAR_NETMASK netmask 221247280Sdteskef_variable_new VAR_NETWORK_DEVICE netDev 222247280Sdteskef_variable_new VAR_NFS_HOST nfsHost 223247280Sdteskef_variable_new VAR_NFS_PATH nfsPath 224247280Sdteskef_variable_new VAR_NFS_SECURE nfs_reserved_port_only 225247280Sdteskef_variable_new VAR_NFS_TCP nfs_use_tcp 226247280Sdteskef_variable_new VAR_NFS_V3 nfs_use_v3 227247280Sdteskef_variable_new VAR_NONINTERACTIVE nonInteractive 228250323Sdteskef_variable_new VAR_NO_CONFIRM noConfirm 229245052Sdteskef_variable_new VAR_NO_ERROR noError 230247280Sdteskef_variable_new VAR_NO_INET6 noInet6 231250323Sdteskef_variable_new VAR_PACKAGE package 232250323Sdteskef_variable_new VAR_PKG_TMPDIR PKG_TMPDIR 233250323Sdteskef_variable_new VAR_PORTS_PATH ports 234245052Sdteskef_variable_new VAR_RELNAME releaseName 235247280Sdteskef_variable_new VAR_SLOW_ETHER slowEthernetCard 236247280Sdteskef_variable_new VAR_TRY_DHCP tryDHCP 237247280Sdteskef_variable_new VAR_TRY_RTSOL tryRTSOL 238247280Sdteskef_variable_new VAR_UFS_PATH ufs 239245052Sdteske 240245052Sdteske# 241245052Sdteske# Self-initialize unless requested otherwise 242245052Sdteske# 243245052Sdteskef_dprintf "%s: VARIABLE_SELF_INITIALIZE=[%s]" \ 244245052Sdteske variable.subr "$VARIABLE_SELF_INITIALIZE" 245245052Sdteskecase "$VARIABLE_SELF_INITIALIZE" in 246245052Sdteske""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;; 247245052Sdteske*) f_variable_set_defaults 248245052Sdteskeesac 249245052Sdteske 250245052Sdteskef_dprintf "%s: Successfully loaded." variable.subr 251245052Sdteske 252245052Sdteskefi # ! $_VARIABLE_SUBR 253