1if [ ! "$_VARIABLE_SUBR" ]; then _VARIABLE_SUBR=1 2# 3# Copyright (c) 2012-2013 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$ 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_EDITOR "${EDITOR:-/usr/bin/ee}" 113 setvar $VAR_FTP_STATE "passive" 114 setvar $VAR_FTP_USER "ftp" 115 setvar $VAR_HOSTNAME "$( hostname )" 116 setvar $VAR_MEDIA_TIMEOUT "300" 117 setvar $VAR_NFS_SECURE "NO" 118 setvar $VAR_NFS_TCP "NO" 119 setvar $VAR_NFS_V3 "YES" 120 setvar $VAR_RELNAME "$UNAME_R" 121 122 f_dprintf "f_variable_set_defaults: Defaults initialized." 123} 124 125# f_dump_variables 126# 127# Dump a list of registered/advertised variables and their respective values to 128# $VARIABLE_DUMPFILE. Returns success unless the file couldn't be written. If 129# an error occurs, it is displayed using f_dialog_msgbox() (from dialog.subr). 130# 131f_dump_variables() 132{ 133 local err sanitize_awk="{ gsub(/'/, \"'\\\\''\"); print }" 134 if ! err=$( 135 ( for handle in $VARIABLES; do 136 f_getvar $handle var || continue 137 f_getvar $var value || continue 138 value=$( echo "$value" | awk "$sanitize_awk" ) 139 printf "%s='%s'\n" "$var" "$value" 140 done > "$VARIABLE_DUMPFILE" ) 2>&1 141 ); then 142 f_dialog_msgbox "$err" 143 return $FAILURE 144 fi 145} 146 147# f_debugging 148# 149# Are we in debug mode? Returns success if extra DEBUG information has been 150# requested (by setting $debug to non-NULL), otherwise false. 151# 152f_debugging() 153{ 154 local value 155 f_getvar $VAR_DEBUG value && [ "$value" ] 156} 157 158# f_interactive() 159# 160# Are we running interactively? Return error if $nonInteractive is set and non- 161# NULL, otherwise return success. 162# 163f_interactive() 164{ 165 local value 166 ! f_getvar $VAR_NONINTERACTIVE value || [ ! "$value" ] 167} 168 169# f_netinteractive() 170# 171# Has the user specifically requested the network-portion of configuration and 172# setup to be performed interactively? Returns success if the user has asked 173# for the network configuration to be done interactively even if perhaps over- 174# all non-interactive mode has been requested (by setting nonInteractive). 175# 176# Returns success if $netInteractive is set and non-NULL. 177# 178f_netinteractive() 179{ 180 local value 181 f_getvar $VAR_NETINTERACTIVE value && [ "$value" ] 182} 183 184############################################################ MAIN 185 186# 187# Variables that can be tweaked from config files 188# 189# Handle Variable Name 190f_variable_new VAR_CONFIG_FILE configFile 191f_variable_new VAR_DEBUG debug 192f_variable_new VAR_DEBUG_FILE debugFile 193f_variable_new VAR_DIRECTORY_PATH _directoryPath 194f_variable_new VAR_DOMAINNAME domainname 195f_variable_new VAR_EDITOR editor 196f_variable_new VAR_EXTRAS ifconfig_ 197f_variable_new VAR_FTP_DIR ftpDirectory 198f_variable_new VAR_FTP_HOST ftpHost 199f_variable_new VAR_FTP_PASS ftpPass 200f_variable_new VAR_FTP_PATH _ftpPath 201f_variable_new VAR_FTP_PORT ftpPort 202f_variable_new VAR_FTP_STATE ftpState 203f_variable_new VAR_FTP_USER ftpUser 204f_variable_new VAR_GATEWAY defaultrouter 205f_variable_new VAR_HOSTNAME hostname 206f_variable_new VAR_HTTP_FTP_MODE httpFtpMode 207f_variable_new VAR_HTTP_PROXY httpProxy 208f_variable_new VAR_HTTP_PROXY_HOST httpProxyHost 209f_variable_new VAR_HTTP_PROXY_PATH _httpProxyPath 210f_variable_new VAR_HTTP_PROXY_PORT httpProxyPort 211f_variable_new VAR_IFCONFIG ifconfig_ 212f_variable_new VAR_IPADDR ipaddr 213f_variable_new VAR_IPV6ADDR ipv6addr 214f_variable_new VAR_IPV6_ENABLE ipv6_activate_all_interfaces 215f_variable_new VAR_MEDIA_TIMEOUT MEDIA_TIMEOUT 216f_variable_new VAR_MEDIA_TYPE mediaType 217f_variable_new VAR_NAMESERVER nameserver 218f_variable_new VAR_NETINTERACTIVE netInteractive 219f_variable_new VAR_NETMASK netmask 220f_variable_new VAR_NETWORK_DEVICE netDev 221f_variable_new VAR_NFS_HOST nfsHost 222f_variable_new VAR_NFS_PATH nfsPath 223f_variable_new VAR_NFS_SECURE nfs_reserved_port_only 224f_variable_new VAR_NFS_TCP nfs_use_tcp 225f_variable_new VAR_NFS_V3 nfs_use_v3 226f_variable_new VAR_NONINTERACTIVE nonInteractive 227f_variable_new VAR_NO_ERROR noError 228f_variable_new VAR_NO_INET6 noInet6 229f_variable_new VAR_RELNAME releaseName 230f_variable_new VAR_SLOW_ETHER slowEthernetCard 231f_variable_new VAR_TRY_DHCP tryDHCP 232f_variable_new VAR_TRY_RTSOL tryRTSOL 233f_variable_new VAR_UFS_PATH ufs 234 235# 236# Self-initialize unless requested otherwise 237# 238f_dprintf "%s: VARIABLE_SELF_INITIALIZE=[%s]" \ 239 variable.subr "$VARIABLE_SELF_INITIALIZE" 240case "$VARIABLE_SELF_INITIALIZE" in 241""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;; 242*) f_variable_set_defaults 243esac 244 245f_dprintf "%s: Successfully loaded." variable.subr 246 247fi # ! $_VARIABLE_SUBR 248