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