variable.subr revision 262904
1203287Srnolandif [ ! "$_VARIABLE_SUBR" ]; then _VARIABLE_SUBR=1 2203287Srnoland# 3203287Srnoland# Copyright (c) 2012-2014 Devin Teske 4203287Srnoland# All rights reserved. 5203287Srnoland# 6203287Srnoland# Redistribution and use in source and binary forms, with or without 7203287Srnoland# modification, are permitted provided that the following conditions 8203287Srnoland# are met: 9203287Srnoland# 1. Redistributions of source code must retain the above copyright 10203287Srnoland# notice, this list of conditions and the following disclaimer. 11203287Srnoland# 2. Redistributions in binary form must reproduce the above copyright 12203287Srnoland# notice, this list of conditions and the following disclaimer in the 13203287Srnoland# documentation and/or other materials provided with the distribution. 14203287Srnoland# 15203287Srnoland# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16203287Srnoland# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17203287Srnoland# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18203287Srnoland# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19203287Srnoland# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20203287Srnoland# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21203287Srnoland# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22203287Srnoland# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23203287Srnoland# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24203287Srnoland# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25203287Srnoland# SUCH DAMAGE. 26203287Srnoland# 27203287Srnoland# $FreeBSD: head/usr.sbin/bsdconfig/share/variable.subr 262904 2014-03-07 20:44:19Z dteske $ 28203287Srnoland# 29203287Srnoland############################################################ INCLUDES 30203287Srnoland 31203287SrnolandBSDCFG_SHARE="/usr/share/bsdconfig" 32203287Srnoland. $BSDCFG_SHARE/common.subr || exit 1 33203287Srnolandf_dprintf "%s: loading includes..." variable.subr 34203287Srnolandf_include $BSDCFG_SHARE/dialog.subr 35203287Srnolandf_include $BSDCFG_SHARE/strings.subr 36203287Srnoland 37203287Srnoland############################################################ GLOBALS 38203287Srnoland 39203287SrnolandVARIABLES= 40203287Srnoland 41203287Srnoland# 42203287Srnoland# Default behavior is to call f_variable_set_defaults() when loaded. 43203287Srnoland# 44203287Srnoland: ${VARIABLE_SELF_INITIALIZE=1} 45203287Srnoland 46203287Srnoland# 47203287Srnoland# File to write when f_dump_variables() is called. 48203287Srnoland# 49203287Srnoland: ${VARIABLE_DUMPFILE:=/etc/bsdconfig.vars} 50203287Srnoland 51203287Srnoland############################################################ FUNCTIONS 52203287Srnoland 53203287Srnoland# f_variable_new $handle $variable 54203287Srnoland# 55203287Srnoland# Register a new variable named $variable with the given reference-handle 56203287Srnoland# $handle. The environment variable $handle is set to $variable allowing you to 57203287Srnoland# use the f_getvar() function (from common.subr) with $handle to get the value 58203287Srnoland# of environment variable $variable. For example: 59203287Srnoland# 60203287Srnoland# f_variable_new VAR_ABC abc 61203287Srnoland# 62203287Srnoland# allows the later indirection: 63203287Srnoland# 64203287Srnoland# f_getvar $VAR_ABC 65203287Srnoland# 66203287Srnoland# to return the value of environment variable `abc'. Variables registered in 67203287Srnoland# this manner are recorded in the $VARIABLES environment variable for later 68203287Srnoland# allowing dynamic enumeration of so-called `registered/advertised' variables. 69203287Srnoland# 70203287Srnolandf_variable_new() 71203287Srnoland{ 72203287Srnoland local handle="$1" variable="$2" 73203287Srnoland [ "$handle" ] || return $FAILURE 74203287Srnoland f_dprintf "variable.subr: New variable %s -> %s" "$handle" "$variable" 75203287Srnoland setvar $handle $variable 76203287Srnoland VARIABLES="$VARIABLES${VARIABLES:+ }$handle" 77203287Srnoland} 78203287Srnoland 79203287Srnoland# f_variable_get_value $var [ $fmt [ $opts ... ] ] 80203287Srnoland# 81203287Srnoland# Unless nonInteractive is set, prompt the user with a given value (pre-filled 82203287Srnoland# with the value of $var) and give them the chance to change the value. 83203287Srnoland# 84203287Srnoland# Unlike f_getvar() (from common.subr) which can return a variable to the 85203287Srnoland# caller on standard output, this function has no [meaningful] output. 86203287Srnoland# 87203287Srnoland# Returns success unless $var is either NULL or missing. 88203287Srnoland# 89203287Srnolandf_variable_get_value() 90203287Srnoland{ 91203287Srnoland local var="$1" cp 92203287Srnoland 93203287Srnoland [ "$var" ] || return $FAILURE 94203287Srnoland 95203287Srnoland if ! { f_getvar $var cp && ! f_interactive; }; then 96203287Srnoland shift 1 # var 97203287Srnoland f_dialog_input cp "$( printf "$@" )" "$cp" && setvar $var "$cp" 98203287Srnoland fi 99203287Srnoland 100203287Srnoland return $SUCCESS 101203287Srnoland} 102203287Srnoland 103203287Srnoland# f_variable_set_defaults 104203287Srnoland# 105203287Srnoland# Installs sensible defaults for registered/advertised variables. 106203287Srnoland# 107203287Srnolandf_variable_set_defaults() 108203287Srnoland{ 109203287Srnoland f_dprintf "f_variable_set_defaults: Initializing defaults..." 110203287Srnoland 111203287Srnoland # 112203287Srnoland # Initialize various user-edittable values to their defaults 113203287Srnoland # 114203287Srnoland setvar $VAR_EDITOR "${EDITOR:-/usr/bin/ee}" 115203287Srnoland setvar $VAR_FTP_STATE "auto" 116203287Srnoland setvar $VAR_FTP_USER "ftp" 117203287Srnoland setvar $VAR_HOSTNAME "$( hostname )" 118203287Srnoland setvar $VAR_MEDIA_TIMEOUT "300" 119203287Srnoland setvar $VAR_NFS_SECURE "NO" 120203287Srnoland setvar $VAR_NFS_TCP "NO" 121203287Srnoland setvar $VAR_NFS_V3 "YES" 122203287Srnoland setvar $VAR_PKG_TMPDIR "/var/tmp" 123203287Srnoland setvar $VAR_RELNAME "$UNAME_R" 124203287Srnoland 125203287Srnoland # 126203287Srnoland # Debugging 127203287Srnoland # 128203287Srnoland if f_debugging; then 129203287Srnoland local var 130203287Srnoland for var in \ 131203287Srnoland $VAR_EDITOR \ 132203287Srnoland $VAR_FTP_STATE \ 133203287Srnoland $VAR_FTP_USER \ 134203287Srnoland $VAR_HOSTNAME \ 135203287Srnoland $VAR_MEDIA_TIMEOUT \ 136203287Srnoland $VAR_NFS_SECURE \ 137203287Srnoland $VAR_NFS_TCP \ 138203287Srnoland $VAR_NFS_V3 \ 139203287Srnoland $VAR_PKG_TMPDIR \ 140203287Srnoland $VAR_RELNAME \ 141203287Srnoland ; do 142203287Srnoland f_quietly f_getvar $var 143203287Srnoland done 144203287Srnoland fi 145203287Srnoland 146203287Srnoland f_dprintf "f_variable_set_defaults: Defaults initialized." 147203287Srnoland} 148203287Srnoland 149203287Srnoland# f_dump_variables 150203287Srnoland# 151203287Srnoland# Dump a list of registered/advertised variables and their respective values to 152203287Srnoland# $VARIABLE_DUMPFILE. Returns success unless the file couldn't be written. If 153203287Srnoland# an error occurs, it is displayed using f_dialog_msgbox() (from dialog.subr). 154203287Srnoland# 155203287Srnolandf_dump_variables() 156203287Srnoland{ 157203287Srnoland local err 158203287Srnoland if ! err=$( 159203287Srnoland ( for handle in $VARIABLES; do 160203287Srnoland f_getvar $handle var || continue 161203287Srnoland f_getvar $var value || continue 162203287Srnoland f_shell_escape "$value" value 163203287Srnoland printf "%s='%s'\n" "$var" "$value" 164203287Srnoland done > "$VARIABLE_DUMPFILE" ) 2>&1 165203287Srnoland ); then 166203287Srnoland f_dialog_msgbox "$err" 167203287Srnoland return $FAILURE 168203287Srnoland fi 169203287Srnoland} 170203287Srnoland 171203287Srnoland# f_debugging 172203287Srnoland# 173203287Srnoland# Are we in debug mode? Returns success if extra DEBUG information has been 174203287Srnoland# requested (by setting $debug to non-NULL), otherwise false. 175203287Srnoland# 176203287Srnolandf_debugging() 177203287Srnoland{ 178203287Srnoland local value 179203287Srnoland f_getvar $VAR_DEBUG value && [ "$value" ] 180203287Srnoland} 181203287Srnoland 182# f_interactive 183# 184# Are we running interactively? Return error if $nonInteractive is set and non- 185# NULL, otherwise return success. 186# 187f_interactive() 188{ 189 local value 190 ! f_getvar $VAR_NONINTERACTIVE value || [ ! "$value" ] 191} 192 193# f_netinteractive 194# 195# Has the user specifically requested the network-portion of configuration and 196# setup to be performed interactively? Returns success if the user has asked 197# for the network configuration to be done interactively even if perhaps over- 198# all non-interactive mode has been requested (by setting nonInteractive). 199# 200# Returns success if $netInteractive is set and non-NULL. 201# 202f_netinteractive() 203{ 204 local value 205 f_getvar $VAR_NETINTERACTIVE value && [ "$value" ] 206} 207 208# f_zfsinteractive 209# 210# Has the user specifically requested the ZFS-portion of configuration and 211# setup to be performed interactively? Returns success if the user has asked 212# for the ZFS configuration to be done interactively even if perhaps overall 213# non-interactive mode has been requested (by setting nonInteractive). 214# 215# Returns success if $zfsInteractive is set and non-NULL. 216# 217f_zfsinteractive() 218{ 219 local value 220 f_getvar $VAR_ZFSINTERACTIVE value && [ "$value" ] 221} 222 223############################################################ MAIN 224 225# 226# Variables that can be tweaked from config files 227# 228# Handle Variable Name 229f_variable_new VAR_CONFIG_FILE configFile 230f_variable_new VAR_DEBUG debug 231f_variable_new VAR_DEBUG_FILE debugFile 232f_variable_new VAR_DIRECTORY_PATH _directoryPath 233f_variable_new VAR_DOMAINNAME domainname 234f_variable_new VAR_EDITOR editor 235f_variable_new VAR_EXTRAS ifconfig_ 236f_variable_new VAR_FTP_DIR ftpDirectory 237f_variable_new VAR_FTP_HOST ftpHost 238f_variable_new VAR_FTP_PASS ftpPass 239f_variable_new VAR_FTP_PATH _ftpPath 240f_variable_new VAR_FTP_PORT ftpPort 241f_variable_new VAR_FTP_STATE ftpState 242f_variable_new VAR_FTP_USER ftpUser 243f_variable_new VAR_GATEWAY defaultrouter 244f_variable_new VAR_GROUP group 245f_variable_new VAR_GROUP_GID groupGid 246f_variable_new VAR_GROUP_MEMBERS groupMembers 247f_variable_new VAR_GROUP_PASSWORD groupPassword 248f_variable_new VAR_HOSTNAME hostname 249f_variable_new VAR_HTTP_DIR httpDirectory 250f_variable_new VAR_HTTP_FTP_MODE httpFtpMode 251f_variable_new VAR_HTTP_HOST httpHost 252f_variable_new VAR_HTTP_PATH _httpPath 253f_variable_new VAR_HTTP_PORT httpPort 254f_variable_new VAR_HTTP_PROXY httpProxy 255f_variable_new VAR_HTTP_PROXY_HOST httpProxyHost 256f_variable_new VAR_HTTP_PROXY_PATH _httpProxyPath 257f_variable_new VAR_HTTP_PROXY_PORT httpProxyPort 258f_variable_new VAR_IFCONFIG ifconfig_ 259f_variable_new VAR_IPADDR ipaddr 260f_variable_new VAR_IPV6ADDR ipv6addr 261f_variable_new VAR_IPV6_ENABLE ipv6_activate_all_interfaces 262f_variable_new VAR_KEYMAP keymap 263f_variable_new VAR_MEDIA_TIMEOUT MEDIA_TIMEOUT 264f_variable_new VAR_MEDIA_TYPE mediaType 265f_variable_new VAR_NAMESERVER nameserver 266f_variable_new VAR_NETINTERACTIVE netInteractive 267f_variable_new VAR_NETMASK netmask 268f_variable_new VAR_NETWORK_DEVICE netDev 269f_variable_new VAR_NFS_HOST nfsHost 270f_variable_new VAR_NFS_PATH nfsPath 271f_variable_new VAR_NFS_SECURE nfs_reserved_port_only 272f_variable_new VAR_NFS_TCP nfs_use_tcp 273f_variable_new VAR_NFS_V3 nfs_use_v3 274f_variable_new VAR_NONINTERACTIVE nonInteractive 275f_variable_new VAR_NO_CONFIRM noConfirm 276f_variable_new VAR_NO_ERROR noError 277f_variable_new VAR_NO_INET6 noInet6 278f_variable_new VAR_PACKAGE package 279f_variable_new VAR_PKG_TMPDIR PKG_TMPDIR 280f_variable_new VAR_PORTS_PATH ports 281f_variable_new VAR_RELNAME releaseName 282f_variable_new VAR_SLOW_ETHER slowEthernetCard 283f_variable_new VAR_TRY_DHCP tryDHCP 284f_variable_new VAR_TRY_RTSOL tryRTSOL 285f_variable_new VAR_UFS_PATH ufs 286f_variable_new VAR_ZFSINTERACTIVE zfsInteractive 287 288# 289# Self-initialize unless requested otherwise 290# 291f_dprintf "%s: VARIABLE_SELF_INITIALIZE=[%s]" \ 292 variable.subr "$VARIABLE_SELF_INITIALIZE" 293case "$VARIABLE_SELF_INITIALIZE" in 294""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;; 295*) f_variable_set_defaults 296esac 297 298f_dprintf "%s: Successfully loaded." variable.subr 299 300fi # ! $_VARIABLE_SUBR 301