xref: /freebsd/usr.sbin/bsdconfig/share/variable.subr (revision 7323adac99d399e58bdec71236a0db9ee6bfb6d2)
199bc932eSDevin Teskeif [ ! "$_VARIABLE_SUBR" ]; then _VARIABLE_SUBR=1
299bc932eSDevin Teske#
3*7323adacSDevin Teske# Copyright (c) 2012-2013 Devin Teske
499bc932eSDevin Teske# All Rights Reserved.
599bc932eSDevin Teske#
699bc932eSDevin Teske# Redistribution and use in source and binary forms, with or without
799bc932eSDevin Teske# modification, are permitted provided that the following conditions
899bc932eSDevin Teske# are met:
999bc932eSDevin Teske# 1. Redistributions of source code must retain the above copyright
1099bc932eSDevin Teske#    notice, this list of conditions and the following disclaimer.
1199bc932eSDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
1299bc932eSDevin Teske#    notice, this list of conditions and the following disclaimer in the
1399bc932eSDevin Teske#    documentation and/or other materials provided with the distribution.
1499bc932eSDevin Teske#
1599bc932eSDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1699bc932eSDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
1799bc932eSDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1899bc932eSDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1999bc932eSDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2099bc932eSDevin Teske# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2199bc932eSDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2299bc932eSDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2399bc932eSDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2499bc932eSDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2599bc932eSDevin Teske# SUCH DAMAGE.
2699bc932eSDevin Teske#
2799bc932eSDevin Teske# $FreeBSD$
2899bc932eSDevin Teske#
2999bc932eSDevin Teske############################################################ INCLUDES
3099bc932eSDevin Teske
3199bc932eSDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
3299bc932eSDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
3399bc932eSDevin Teskef_dprintf "%s: loading includes..." variable.subr
3499bc932eSDevin Teskef_include $BSDCFG_SHARE/dialog.subr
3599bc932eSDevin Teske
3699bc932eSDevin Teske############################################################ GLOBALS
3799bc932eSDevin Teske
3899bc932eSDevin TeskeVARIABLES=
3999bc932eSDevin Teske
4099bc932eSDevin Teske#
4199bc932eSDevin Teske# Default behavior is to call f_variable_set_defaults() when loaded.
4299bc932eSDevin Teske#
4399bc932eSDevin Teske: ${VARIABLE_SELF_INITIALIZE=1}
4499bc932eSDevin Teske
4599bc932eSDevin Teske#
4699bc932eSDevin Teske# File to write when f_dump_variables() is called.
4799bc932eSDevin Teske#
4899bc932eSDevin Teske: ${VARIABLE_DUMPFILE:=/etc/bsdconfig.vars}
4999bc932eSDevin Teske
5099bc932eSDevin Teske############################################################ FUNCTIONS
5199bc932eSDevin Teske
5299bc932eSDevin Teske# f_variable_new $handle $variable
5399bc932eSDevin Teske#
5499bc932eSDevin Teske# Register a new variable named $variable with the given reference-handle
5599bc932eSDevin Teske# $handle. The environment variable $handle is set to $variable allowing you to
5699bc932eSDevin Teske# use the f_getvar() function (from common.subr) with $handle to get the value
5799bc932eSDevin Teske# of environment variable $variable. For example:
5899bc932eSDevin Teske#
5999bc932eSDevin Teske# 	f_variable_new VAR_ABC abc
6099bc932eSDevin Teske#
6199bc932eSDevin Teske# allows the later indirection:
6299bc932eSDevin Teske#
6399bc932eSDevin Teske# 	f_getvar $VAR_ABC
6499bc932eSDevin Teske#
6599bc932eSDevin Teske# to return the value of environment variable `abc'. Variables registered in
6699bc932eSDevin Teske# this manner are recorded in the $VARIABLES environment variable for later
6799bc932eSDevin Teske# allowing dynamic enumeration of so-called `registered/advertised' variables.
6899bc932eSDevin Teske#
6999bc932eSDevin Teskef_variable_new()
7099bc932eSDevin Teske{
7199bc932eSDevin Teske	local handle="$1" variable="$2"
7299bc932eSDevin Teske	[ "$handle" ] || return $FAILURE
7399bc932eSDevin Teske	f_dprintf "variable.subr: New variable %s -> %s" "$handle" "$variable"
7499bc932eSDevin Teske	setvar $handle $variable
7599bc932eSDevin Teske	VARIABLES="$VARIABLES${VARIABLES:+ }$handle"
7699bc932eSDevin Teske}
7799bc932eSDevin Teske
7899bc932eSDevin Teske# f_variable_get_value $var [ $fmt [ $opts ... ] ]
7999bc932eSDevin Teske#
8099bc932eSDevin Teske# Unless nonInteractive is set, prompt the user with a given value (pre-filled
8199bc932eSDevin Teske# with the value of $var) and give them the chance to change the value.
8299bc932eSDevin Teske#
8399bc932eSDevin Teske# Unlike f_getvar() (from common.subr) which can return a variable to the
8499bc932eSDevin Teske# caller on standard output, this function has no [meaningful] output.
8599bc932eSDevin Teske#
8699bc932eSDevin Teske# Returns success unless $var is either NULL or missing.
8799bc932eSDevin Teske#
8899bc932eSDevin Teskef_variable_get_value()
8999bc932eSDevin Teske{
9099bc932eSDevin Teske	local var="$1" cp
9199bc932eSDevin Teske
9299bc932eSDevin Teske	[ "$var" ] || return $FAILURE
9399bc932eSDevin Teske
9499bc932eSDevin Teske	if ! { f_getvar $var cp && ! f_interactive; }; then
9599bc932eSDevin Teske		shift 1 # var
9699bc932eSDevin Teske		cp=$( f_dialog_input "$( printf "$@" )" "$cp" ) &&
9799bc932eSDevin Teske			setvar $var "$cp"
9899bc932eSDevin Teske	fi
9999bc932eSDevin Teske
10099bc932eSDevin Teske	return $SUCCESS
10199bc932eSDevin Teske}
10299bc932eSDevin Teske
10399bc932eSDevin Teske# f_variable_set_defaults
10499bc932eSDevin Teske#
10599bc932eSDevin Teske# Installs sensible defaults for registered/advertised variables.
10699bc932eSDevin Teske#
10799bc932eSDevin Teskef_variable_set_defaults()
10899bc932eSDevin Teske{
10999bc932eSDevin Teske	#
11099bc932eSDevin Teske	# Initialize various user-edittable values to their defaults
11199bc932eSDevin Teske	#
112*7323adacSDevin Teske	setvar $VAR_EDITOR		"${EDITOR:-/usr/bin/ee}"
113*7323adacSDevin Teske	setvar $VAR_FTP_STATE		"passive"
114*7323adacSDevin Teske	setvar $VAR_FTP_USER		"ftp"
115*7323adacSDevin Teske	setvar $VAR_HOSTNAME		"$( hostname )"
116*7323adacSDevin Teske	setvar $VAR_MEDIA_TIMEOUT	"300"
117*7323adacSDevin Teske	setvar $VAR_NFS_SECURE		"NO"
118*7323adacSDevin Teske	setvar $VAR_NFS_TCP		"NO"
119*7323adacSDevin Teske	setvar $VAR_NFS_V3		"YES"
12099bc932eSDevin Teske	setvar $VAR_RELNAME		"$UNAME_R"
12199bc932eSDevin Teske
12299bc932eSDevin Teske	f_dprintf "f_variable_set_defaults: Defaults initialized."
12399bc932eSDevin Teske}
12499bc932eSDevin Teske
12599bc932eSDevin Teske# f_dump_variables
12699bc932eSDevin Teske#
12799bc932eSDevin Teske# Dump a list of registered/advertised variables and their respective values to
12899bc932eSDevin Teske# $VARIABLE_DUMPFILE. Returns success unless the file couldn't be written. If
129db7b0ba7SDevin Teske# an error occurs, it is displayed using f_dialog_msgbox() (from dialog.subr).
13099bc932eSDevin Teske#
13199bc932eSDevin Teskef_dump_variables()
13299bc932eSDevin Teske{
13399bc932eSDevin Teske	local err sanitize_awk="{ gsub(/'/, \"'\\\\''\"); print }"
13499bc932eSDevin Teske	if ! err=$(
13599bc932eSDevin Teske		( for handle in $VARIABLES; do
13699bc932eSDevin Teske			f_getvar $handle var || continue
13799bc932eSDevin Teske			f_getvar $var value || continue
13899bc932eSDevin Teske			value=$( echo "$value" | awk "$sanitize_awk" )
13999bc932eSDevin Teske			printf "%s='%s'\n" "$var" "$value"
14099bc932eSDevin Teske		  done > "$VARIABLE_DUMPFILE" ) 2>&1
14199bc932eSDevin Teske	); then
142db7b0ba7SDevin Teske		f_dialog_msgbox "$err"
14399bc932eSDevin Teske		return $FAILURE
14499bc932eSDevin Teske	fi
14599bc932eSDevin Teske}
14699bc932eSDevin Teske
14799bc932eSDevin Teske# f_debugging
14899bc932eSDevin Teske#
14999bc932eSDevin Teske# Are we in debug mode? Returns success if extra DEBUG information has been
15099bc932eSDevin Teske# requested (by setting $debug to non-NULL), otherwise false.
15199bc932eSDevin Teske#
15299bc932eSDevin Teskef_debugging()
15399bc932eSDevin Teske{
15499bc932eSDevin Teske	local value
15599bc932eSDevin Teske	f_getvar $VAR_DEBUG value && [ "$value" ]
15699bc932eSDevin Teske}
15799bc932eSDevin Teske
15899bc932eSDevin Teske# f_interactive()
15999bc932eSDevin Teske#
16099bc932eSDevin Teske# Are we running interactively? Return error if $nonInteractive is set and non-
16199bc932eSDevin Teske# NULL, otherwise return success.
16299bc932eSDevin Teske#
16399bc932eSDevin Teskef_interactive()
16499bc932eSDevin Teske{
16599bc932eSDevin Teske	local value
16699bc932eSDevin Teske	! f_getvar $VAR_NONINTERACTIVE value || [ ! "$value" ]
16799bc932eSDevin Teske}
16899bc932eSDevin Teske
169*7323adacSDevin Teske# f_netinteractive()
170*7323adacSDevin Teske#
171*7323adacSDevin Teske# Has the user specifically requested the network-portion of configuration and
172*7323adacSDevin Teske# setup to be performed interactively? Returns success if the user has asked
173*7323adacSDevin Teske# for the network configuration to be done interactively even if perhaps over-
174*7323adacSDevin Teske# all non-interactive mode has been requested (by setting nonInteractive).
175*7323adacSDevin Teske#
176*7323adacSDevin Teske# Returns success if $netInteractive is set and non-NULL.
177*7323adacSDevin Teske#
178*7323adacSDevin Teskef_netinteractive()
179*7323adacSDevin Teske{
180*7323adacSDevin Teske	local value
181*7323adacSDevin Teske	f_getvar $VAR_NETINTERACTIVE value && [ "$value" ]
182*7323adacSDevin Teske}
183*7323adacSDevin Teske
18499bc932eSDevin Teske############################################################ MAIN
18599bc932eSDevin Teske
18699bc932eSDevin Teske#
18799bc932eSDevin Teske# Variables that can be tweaked from config files
18899bc932eSDevin Teske#
189*7323adacSDevin Teske#              Handle                   Variable Name
19099bc932eSDevin Teskef_variable_new VAR_CONFIG_FILE		configFile
19199bc932eSDevin Teskef_variable_new VAR_DEBUG		debug
19299bc932eSDevin Teskef_variable_new VAR_DEBUG_FILE		debugFile
193*7323adacSDevin Teskef_variable_new VAR_DIRECTORY_PATH	_directoryPath
194*7323adacSDevin Teskef_variable_new VAR_DOMAINNAME		domainname
195*7323adacSDevin Teskef_variable_new VAR_EDITOR		editor
196*7323adacSDevin Teskef_variable_new VAR_EXTRAS		ifconfig_
197*7323adacSDevin Teskef_variable_new VAR_FTP_DIR		ftpDirectory
198*7323adacSDevin Teskef_variable_new VAR_FTP_HOST		ftpHost
199*7323adacSDevin Teskef_variable_new VAR_FTP_PASS		ftpPass
200*7323adacSDevin Teskef_variable_new VAR_FTP_PATH		_ftpPath
201*7323adacSDevin Teskef_variable_new VAR_FTP_PORT		ftpPort
202*7323adacSDevin Teskef_variable_new VAR_FTP_STATE		ftpState
203*7323adacSDevin Teskef_variable_new VAR_FTP_USER		ftpUser
204*7323adacSDevin Teskef_variable_new VAR_GATEWAY		defaultrouter
205*7323adacSDevin Teskef_variable_new VAR_HOSTNAME		hostname
206*7323adacSDevin Teskef_variable_new VAR_HTTP_FTP_MODE	httpFtpMode
207*7323adacSDevin Teskef_variable_new VAR_HTTP_PROXY		httpProxy
208*7323adacSDevin Teskef_variable_new VAR_HTTP_PROXY_HOST	httpProxyHost
209*7323adacSDevin Teskef_variable_new VAR_HTTP_PROXY_PATH	_httpProxyPath
210*7323adacSDevin Teskef_variable_new VAR_HTTP_PROXY_PORT	httpProxyPort
211*7323adacSDevin Teskef_variable_new VAR_IFCONFIG		ifconfig_
212*7323adacSDevin Teskef_variable_new VAR_IPADDR		ipaddr
213*7323adacSDevin Teskef_variable_new VAR_IPV6ADDR		ipv6addr
214*7323adacSDevin Teskef_variable_new VAR_IPV6_ENABLE		ipv6_activate_all_interfaces
215*7323adacSDevin Teskef_variable_new VAR_MEDIA_TIMEOUT	MEDIA_TIMEOUT
216*7323adacSDevin Teskef_variable_new VAR_MEDIA_TYPE		mediaType
217*7323adacSDevin Teskef_variable_new VAR_NAMESERVER		nameserver
218*7323adacSDevin Teskef_variable_new VAR_NETINTERACTIVE	netInteractive
219*7323adacSDevin Teskef_variable_new VAR_NETMASK		netmask
220*7323adacSDevin Teskef_variable_new VAR_NETWORK_DEVICE	netDev
221*7323adacSDevin Teskef_variable_new VAR_NFS_HOST		nfsHost
222*7323adacSDevin Teskef_variable_new VAR_NFS_PATH		nfsPath
223*7323adacSDevin Teskef_variable_new VAR_NFS_SECURE		nfs_reserved_port_only
224*7323adacSDevin Teskef_variable_new VAR_NFS_TCP		nfs_use_tcp
225*7323adacSDevin Teskef_variable_new VAR_NFS_V3		nfs_use_v3
22699bc932eSDevin Teskef_variable_new VAR_NONINTERACTIVE	nonInteractive
227*7323adacSDevin Teskef_variable_new VAR_NO_ERROR		noError
228*7323adacSDevin Teskef_variable_new VAR_NO_INET6		noInet6
22999bc932eSDevin Teskef_variable_new VAR_RELNAME		releaseName
230*7323adacSDevin Teskef_variable_new VAR_SLOW_ETHER		slowEthernetCard
231*7323adacSDevin Teskef_variable_new VAR_TRY_DHCP		tryDHCP
232*7323adacSDevin Teskef_variable_new VAR_TRY_RTSOL		tryRTSOL
233*7323adacSDevin Teskef_variable_new VAR_UFS_PATH		ufs
23499bc932eSDevin Teske
23599bc932eSDevin Teske#
23699bc932eSDevin Teske# Self-initialize unless requested otherwise
23799bc932eSDevin Teske#
23899bc932eSDevin Teskef_dprintf "%s: VARIABLE_SELF_INITIALIZE=[%s]" \
23999bc932eSDevin Teske          variable.subr "$VARIABLE_SELF_INITIALIZE"
24099bc932eSDevin Teskecase "$VARIABLE_SELF_INITIALIZE" in
24199bc932eSDevin Teske""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
24299bc932eSDevin Teske*) f_variable_set_defaults
24399bc932eSDevin Teskeesac
24499bc932eSDevin Teske
24599bc932eSDevin Teskef_dprintf "%s: Successfully loaded." variable.subr
24699bc932eSDevin Teske
24799bc932eSDevin Teskefi # ! $_VARIABLE_SUBR
248