xref: /freebsd/usr.sbin/bsdconfig/share/variable.subr (revision 5e7aa7574445f1fc90140bc93ec75de83e850fbe)
199bc932eSDevin Teskeif [ ! "$_VARIABLE_SUBR" ]; then _VARIABLE_SUBR=1
299bc932eSDevin Teske#
327c43fe1SDevin Teske# Copyright (c) 2012-2014 Devin Teske
4f8ea072aSDevin 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
168e37a7c8SDevin 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
208e37a7c8SDevin Teske# DAMAGES (INCLUDING, 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#
2899bc932eSDevin Teske############################################################ INCLUDES
2999bc932eSDevin Teske
3099bc932eSDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
3199bc932eSDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
3299bc932eSDevin Teskef_dprintf "%s: loading includes..." variable.subr
3399bc932eSDevin Teskef_include $BSDCFG_SHARE/dialog.subr
3444392705SDevin Teskef_include $BSDCFG_SHARE/strings.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
96ec7120b5SDevin Teske		f_dialog_input cp "$( printf "$@" )" "$cp" && setvar $var "$cp"
9799bc932eSDevin Teske	fi
9899bc932eSDevin Teske
9999bc932eSDevin Teske	return $SUCCESS
10099bc932eSDevin Teske}
10199bc932eSDevin Teske
10299bc932eSDevin Teske# f_variable_set_defaults
10399bc932eSDevin Teske#
10499bc932eSDevin Teske# Installs sensible defaults for registered/advertised variables.
10599bc932eSDevin Teske#
10699bc932eSDevin Teskef_variable_set_defaults()
10799bc932eSDevin Teske{
108f687c95dSDevin Teske	f_dprintf "f_variable_set_defaults: Initializing defaults..."
109f687c95dSDevin Teske
11099bc932eSDevin Teske	#
11199bc932eSDevin Teske	# Initialize various user-edittable values to their defaults
11299bc932eSDevin Teske	#
1137323adacSDevin Teske	setvar $VAR_EDITOR		"${EDITOR:-/usr/bin/ee}"
1147323adacSDevin Teske	setvar $VAR_HOSTNAME		"$( hostname )"
1157323adacSDevin Teske	setvar $VAR_MEDIA_TIMEOUT	"300"
1167323adacSDevin Teske	setvar $VAR_NFS_SECURE		"NO"
1177323adacSDevin Teske	setvar $VAR_NFS_TCP		"NO"
1187323adacSDevin Teske	setvar $VAR_NFS_V3		"YES"
11987c16275SDevin Teske	setvar $VAR_PKG_TMPDIR		"/var/tmp"
12099bc932eSDevin Teske	setvar $VAR_RELNAME		"$UNAME_R"
12199bc932eSDevin Teske
122f687c95dSDevin Teske	#
123f687c95dSDevin Teske	# Debugging
124f687c95dSDevin Teske	#
125f687c95dSDevin Teske	if f_debugging; then
126f687c95dSDevin Teske		local var
127f687c95dSDevin Teske		for var in \
128f687c95dSDevin Teske			$VAR_EDITOR		\
129f687c95dSDevin Teske			$VAR_HOSTNAME		\
130f687c95dSDevin Teske			$VAR_MEDIA_TIMEOUT	\
131f687c95dSDevin Teske			$VAR_NFS_SECURE		\
132f687c95dSDevin Teske			$VAR_NFS_TCP		\
133f687c95dSDevin Teske			$VAR_NFS_V3		\
134f687c95dSDevin Teske			$VAR_PKG_TMPDIR		\
135f687c95dSDevin Teske			$VAR_RELNAME		\
136f687c95dSDevin Teske		; do
137f687c95dSDevin Teske			f_quietly f_getvar $var
138f687c95dSDevin Teske		done
139f687c95dSDevin Teske	fi
140f687c95dSDevin Teske
14199bc932eSDevin Teske	f_dprintf "f_variable_set_defaults: Defaults initialized."
14299bc932eSDevin Teske}
14399bc932eSDevin Teske
14499bc932eSDevin Teske# f_dump_variables
14599bc932eSDevin Teske#
14699bc932eSDevin Teske# Dump a list of registered/advertised variables and their respective values to
14799bc932eSDevin Teske# $VARIABLE_DUMPFILE. Returns success unless the file couldn't be written. If
148db7b0ba7SDevin Teske# an error occurs, it is displayed using f_dialog_msgbox() (from dialog.subr).
14999bc932eSDevin Teske#
15099bc932eSDevin Teskef_dump_variables()
15199bc932eSDevin Teske{
15244392705SDevin Teske	local err
15399bc932eSDevin Teske	if ! err=$(
15499bc932eSDevin Teske		( for handle in $VARIABLES; do
15599bc932eSDevin Teske			f_getvar $handle var || continue
15699bc932eSDevin Teske			f_getvar $var value || continue
15744392705SDevin Teske			f_shell_escape "$value" value
15899bc932eSDevin Teske			printf "%s='%s'\n" "$var" "$value"
15999bc932eSDevin Teske		  done > "$VARIABLE_DUMPFILE" ) 2>&1
16099bc932eSDevin Teske	); then
161db7b0ba7SDevin Teske		f_dialog_msgbox "$err"
16299bc932eSDevin Teske		return $FAILURE
16399bc932eSDevin Teske	fi
16499bc932eSDevin Teske}
16599bc932eSDevin Teske
16699bc932eSDevin Teske# f_debugging
16799bc932eSDevin Teske#
16899bc932eSDevin Teske# Are we in debug mode? Returns success if extra DEBUG information has been
16999bc932eSDevin Teske# requested (by setting $debug to non-NULL), otherwise false.
17099bc932eSDevin Teske#
17199bc932eSDevin Teskef_debugging()
17299bc932eSDevin Teske{
17399bc932eSDevin Teske	local value
17499bc932eSDevin Teske	f_getvar $VAR_DEBUG value && [ "$value" ]
17599bc932eSDevin Teske}
17699bc932eSDevin Teske
177ad8f8a1fSDevin Teske# f_interactive
17899bc932eSDevin Teske#
17999bc932eSDevin Teske# Are we running interactively? Return error if $nonInteractive is set and non-
18099bc932eSDevin Teske# NULL, otherwise return success.
18199bc932eSDevin Teske#
18299bc932eSDevin Teskef_interactive()
18399bc932eSDevin Teske{
18499bc932eSDevin Teske	local value
18599bc932eSDevin Teske	! f_getvar $VAR_NONINTERACTIVE value || [ ! "$value" ]
18699bc932eSDevin Teske}
18799bc932eSDevin Teske
188ad8f8a1fSDevin Teske# f_netinteractive
1897323adacSDevin Teske#
1907323adacSDevin Teske# Has the user specifically requested the network-portion of configuration and
1917323adacSDevin Teske# setup to be performed interactively? Returns success if the user has asked
1927323adacSDevin Teske# for the network configuration to be done interactively even if perhaps over-
1937323adacSDevin Teske# all non-interactive mode has been requested (by setting nonInteractive).
1947323adacSDevin Teske#
1957323adacSDevin Teske# Returns success if $netInteractive is set and non-NULL.
1967323adacSDevin Teske#
1977323adacSDevin Teskef_netinteractive()
1987323adacSDevin Teske{
1997323adacSDevin Teske	local value
2007323adacSDevin Teske	f_getvar $VAR_NETINTERACTIVE value && [ "$value" ]
2017323adacSDevin Teske}
2027323adacSDevin Teske
203ad8f8a1fSDevin Teske# f_zfsinteractive
2040c20fe6fSDevin Teske#
2050c20fe6fSDevin Teske# Has the user specifically requested the ZFS-portion of configuration and
2060c20fe6fSDevin Teske# setup to be performed interactively? Returns success if the user has asked
2070c20fe6fSDevin Teske# for the ZFS configuration to be done interactively even if perhaps overall
2080c20fe6fSDevin Teske# non-interactive mode has been requested (by setting nonInteractive).
2090c20fe6fSDevin Teske#
2100c20fe6fSDevin Teske# Returns success if $zfsInteractive is set and non-NULL.
2110c20fe6fSDevin Teske#
2120c20fe6fSDevin Teskef_zfsinteractive()
2130c20fe6fSDevin Teske{
2140c20fe6fSDevin Teske	local value
2150c20fe6fSDevin Teske	f_getvar $VAR_ZFSINTERACTIVE value && [ "$value" ]
2160c20fe6fSDevin Teske}
2170c20fe6fSDevin Teske
21899bc932eSDevin Teske############################################################ MAIN
21999bc932eSDevin Teske
22099bc932eSDevin Teske#
22199bc932eSDevin Teske# Variables that can be tweaked from config files
22299bc932eSDevin Teske#
2237323adacSDevin Teske#              Handle                   Variable Name
22499bc932eSDevin Teskef_variable_new VAR_CONFIG_FILE		configFile
22599bc932eSDevin Teskef_variable_new VAR_DEBUG		debug
22699bc932eSDevin Teskef_variable_new VAR_DEBUG_FILE		debugFile
2277323adacSDevin Teskef_variable_new VAR_DIRECTORY_PATH	_directoryPath
2287323adacSDevin Teskef_variable_new VAR_DOMAINNAME		domainname
2297323adacSDevin Teskef_variable_new VAR_EDITOR		editor
2307323adacSDevin Teskef_variable_new VAR_EXTRAS		ifconfig_
2317323adacSDevin Teskef_variable_new VAR_GATEWAY		defaultrouter
23227c43fe1SDevin Teskef_variable_new VAR_GROUP		group
23327c43fe1SDevin Teskef_variable_new VAR_GROUP_GID		groupGid
23427c43fe1SDevin Teskef_variable_new VAR_GROUP_MEMBERS	groupMembers
23527c43fe1SDevin Teskef_variable_new VAR_GROUP_PASSWORD	groupPassword
2367323adacSDevin Teskef_variable_new VAR_HOSTNAME		hostname
23747b73aa2SDevin Teskef_variable_new VAR_HTTP_DIR		httpDirectory
2387323adacSDevin Teskef_variable_new VAR_HTTP_FTP_MODE	httpFtpMode
23947b73aa2SDevin Teskef_variable_new VAR_HTTP_HOST		httpHost
24047b73aa2SDevin Teskef_variable_new VAR_HTTP_PATH		_httpPath
24147b73aa2SDevin Teskef_variable_new VAR_HTTP_PORT		httpPort
2427323adacSDevin Teskef_variable_new VAR_HTTP_PROXY		httpProxy
2437323adacSDevin Teskef_variable_new VAR_HTTP_PROXY_HOST	httpProxyHost
2447323adacSDevin Teskef_variable_new VAR_HTTP_PROXY_PATH	_httpProxyPath
2457323adacSDevin Teskef_variable_new VAR_HTTP_PROXY_PORT	httpProxyPort
2467323adacSDevin Teskef_variable_new VAR_IFCONFIG		ifconfig_
2477323adacSDevin Teskef_variable_new VAR_IPADDR		ipaddr
2487323adacSDevin Teskef_variable_new VAR_IPV6ADDR		ipv6addr
2497323adacSDevin Teskef_variable_new VAR_IPV6_ENABLE		ipv6_activate_all_interfaces
2503636c235SDevin Teskef_variable_new VAR_KEYMAP		keymap
2517323adacSDevin Teskef_variable_new VAR_MEDIA_TIMEOUT	MEDIA_TIMEOUT
2527323adacSDevin Teskef_variable_new VAR_MEDIA_TYPE		mediaType
2537323adacSDevin Teskef_variable_new VAR_NAMESERVER		nameserver
2547323adacSDevin Teskef_variable_new VAR_NETINTERACTIVE	netInteractive
2557323adacSDevin Teskef_variable_new VAR_NETMASK		netmask
2567323adacSDevin Teskef_variable_new VAR_NETWORK_DEVICE	netDev
2577323adacSDevin Teskef_variable_new VAR_NFS_HOST		nfsHost
2587323adacSDevin Teskef_variable_new VAR_NFS_PATH		nfsPath
2597323adacSDevin Teskef_variable_new VAR_NFS_SECURE		nfs_reserved_port_only
2607323adacSDevin Teskef_variable_new VAR_NFS_TCP		nfs_use_tcp
2617323adacSDevin Teskef_variable_new VAR_NFS_V3		nfs_use_v3
26299bc932eSDevin Teskef_variable_new VAR_NONINTERACTIVE	nonInteractive
26387c16275SDevin Teskef_variable_new VAR_NO_CONFIRM		noConfirm
2647323adacSDevin Teskef_variable_new VAR_NO_ERROR		noError
2657323adacSDevin Teskef_variable_new VAR_NO_INET6		noInet6
26687c16275SDevin Teskef_variable_new VAR_PACKAGE		package
26787c16275SDevin Teskef_variable_new VAR_PKG_TMPDIR		PKG_TMPDIR
26887c16275SDevin Teskef_variable_new VAR_PORTS_PATH		ports
26999bc932eSDevin Teskef_variable_new VAR_RELNAME		releaseName
2707323adacSDevin Teskef_variable_new VAR_SLOW_ETHER		slowEthernetCard
2717323adacSDevin Teskef_variable_new VAR_TRY_DHCP		tryDHCP
2727323adacSDevin Teskef_variable_new VAR_TRY_RTSOL		tryRTSOL
2737323adacSDevin Teskef_variable_new VAR_UFS_PATH		ufs
274*f589320aSDevin Teskef_variable_new VAR_USER			user
275*f589320aSDevin Teskef_variable_new VAR_USER_ACCOUNT_EXPIRE	userAccountExpire
276*f589320aSDevin Teskef_variable_new VAR_USER_DOTFILES_CREATE	userDotfilesCreate
277*f589320aSDevin Teskef_variable_new VAR_USER_GECOS		userGecos
278*f589320aSDevin Teskef_variable_new VAR_USER_GID		userGid
279*f589320aSDevin Teskef_variable_new VAR_USER_GROUPS		userGroups
280*f589320aSDevin Teskef_variable_new VAR_USER_GROUP_DELETE	userGroupDelete
281*f589320aSDevin Teskef_variable_new VAR_USER_HOME		userHome
282*f589320aSDevin Teskef_variable_new VAR_USER_HOME_CREATE	userHomeCreate
283*f589320aSDevin Teskef_variable_new VAR_USER_HOME_DELETE	userHomeDelete
284*f589320aSDevin Teskef_variable_new VAR_USER_LOGIN_CLASS	userLoginClass
285*f589320aSDevin Teskef_variable_new VAR_USER_PASSWORD	userPassword
286*f589320aSDevin Teskef_variable_new VAR_USER_PASSWORD_EXPIRE	userPasswordExpire
287*f589320aSDevin Teskef_variable_new VAR_USER_SHELL		userShell
288*f589320aSDevin Teskef_variable_new VAR_USER_UID		userUid
2890c20fe6fSDevin Teskef_variable_new VAR_ZFSINTERACTIVE	zfsInteractive
29099bc932eSDevin Teske
29199bc932eSDevin Teske#
29299bc932eSDevin Teske# Self-initialize unless requested otherwise
29399bc932eSDevin Teske#
29499bc932eSDevin Teskef_dprintf "%s: VARIABLE_SELF_INITIALIZE=[%s]" \
29599bc932eSDevin Teske          variable.subr "$VARIABLE_SELF_INITIALIZE"
29699bc932eSDevin Teskecase "$VARIABLE_SELF_INITIALIZE" in
29799bc932eSDevin Teske""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
29899bc932eSDevin Teske*) f_variable_set_defaults
29999bc932eSDevin Teskeesac
30099bc932eSDevin Teske
30199bc932eSDevin Teskef_dprintf "%s: Successfully loaded." variable.subr
30299bc932eSDevin Teske
30399bc932eSDevin Teskefi # ! $_VARIABLE_SUBR
304