1 if [ ! "$_VARIABLE_SUBR" ]; then _VARIABLE_SUBR=1
3 # Copyright (c) 2012-2014 Devin Teske
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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 (INCLUDING, 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
29 ############################################################ INCLUDES
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." variable.subr
34 f_include $BSDCFG_SHARE/dialog.subr
35 f_include $BSDCFG_SHARE/strings.subr
37 ############################################################ GLOBALS
42 # Default behavior is to call f_variable_set_defaults() when loaded.
44 : ${VARIABLE_SELF_INITIALIZE=1}
47 # File to write when f_dump_variables() is called.
49 : ${VARIABLE_DUMPFILE:=/etc/bsdconfig.vars}
51 ############################################################ FUNCTIONS
53 # f_variable_new $handle $variable
55 # Register a new variable named $variable with the given reference-handle
56 # $handle. The environment variable $handle is set to $variable allowing you to
57 # use the f_getvar() function (from common.subr) with $handle to get the value
58 # of environment variable $variable. For example:
60 # f_variable_new VAR_ABC abc
62 # allows the later indirection:
66 # to return the value of environment variable `abc'. Variables registered in
67 # this manner are recorded in the $VARIABLES environment variable for later
68 # allowing dynamic enumeration of so-called `registered/advertised' variables.
72 local handle="$1" variable="$2"
73 [ "$handle" ] || return $FAILURE
74 f_dprintf "variable.subr: New variable %s -> %s" "$handle" "$variable"
75 setvar $handle $variable
76 VARIABLES="$VARIABLES${VARIABLES:+ }$handle"
79 # f_variable_get_value $var [ $fmt [ $opts ... ] ]
81 # Unless nonInteractive is set, prompt the user with a given value (pre-filled
82 # with the value of $var) and give them the chance to change the value.
84 # Unlike f_getvar() (from common.subr) which can return a variable to the
85 # caller on standard output, this function has no [meaningful] output.
87 # Returns success unless $var is either NULL or missing.
89 f_variable_get_value()
93 [ "$var" ] || return $FAILURE
95 if ! { f_getvar $var cp && ! f_interactive; }; then
97 f_dialog_input cp "$( printf "$@" )" "$cp" && setvar $var "$cp"
103 # f_variable_set_defaults
105 # Installs sensible defaults for registered/advertised variables.
107 f_variable_set_defaults()
109 f_dprintf "f_variable_set_defaults: Initializing defaults..."
112 # Initialize various user-edittable values to their defaults
114 setvar $VAR_EDITOR "${EDITOR:-/usr/bin/ee}"
115 setvar $VAR_FTP_STATE "auto"
116 setvar $VAR_FTP_USER "ftp"
117 setvar $VAR_HOSTNAME "$( hostname )"
118 setvar $VAR_MEDIA_TIMEOUT "300"
119 setvar $VAR_NFS_SECURE "NO"
120 setvar $VAR_NFS_TCP "NO"
121 setvar $VAR_NFS_V3 "YES"
122 setvar $VAR_PKG_TMPDIR "/var/tmp"
123 setvar $VAR_RELNAME "$UNAME_R"
142 f_quietly f_getvar $var
146 f_dprintf "f_variable_set_defaults: Defaults initialized."
151 # Dump a list of registered/advertised variables and their respective values to
152 # $VARIABLE_DUMPFILE. Returns success unless the file couldn't be written. If
153 # an error occurs, it is displayed using f_dialog_msgbox() (from dialog.subr).
159 ( for handle in $VARIABLES; do
160 f_getvar $handle var || continue
161 f_getvar $var value || continue
162 f_shell_escape "$value" value
163 printf "%s='%s'\n" "$var" "$value"
164 done > "$VARIABLE_DUMPFILE" ) 2>&1
166 f_dialog_msgbox "$err"
173 # Are we in debug mode? Returns success if extra DEBUG information has been
174 # requested (by setting $debug to non-NULL), otherwise false.
179 f_getvar $VAR_DEBUG value && [ "$value" ]
184 # Are we running interactively? Return error if $nonInteractive is set and non-
185 # NULL, otherwise return success.
190 ! f_getvar $VAR_NONINTERACTIVE value || [ ! "$value" ]
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).
200 # Returns success if $netInteractive is set and non-NULL.
205 f_getvar $VAR_NETINTERACTIVE value && [ "$value" ]
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).
215 # Returns success if $zfsInteractive is set and non-NULL.
220 f_getvar $VAR_ZFSINTERACTIVE value && [ "$value" ]
223 ############################################################ MAIN
226 # Variables that can be tweaked from config files
228 # Handle Variable Name
229 f_variable_new VAR_CONFIG_FILE configFile
230 f_variable_new VAR_DEBUG debug
231 f_variable_new VAR_DEBUG_FILE debugFile
232 f_variable_new VAR_DIRECTORY_PATH _directoryPath
233 f_variable_new VAR_DOMAINNAME domainname
234 f_variable_new VAR_EDITOR editor
235 f_variable_new VAR_EXTRAS ifconfig_
236 f_variable_new VAR_FTP_DIR ftpDirectory
237 f_variable_new VAR_FTP_HOST ftpHost
238 f_variable_new VAR_FTP_PASS ftpPass
239 f_variable_new VAR_FTP_PATH _ftpPath
240 f_variable_new VAR_FTP_PORT ftpPort
241 f_variable_new VAR_FTP_STATE ftpState
242 f_variable_new VAR_FTP_USER ftpUser
243 f_variable_new VAR_GATEWAY defaultrouter
244 f_variable_new VAR_GROUP group
245 f_variable_new VAR_GROUP_GID groupGid
246 f_variable_new VAR_GROUP_MEMBERS groupMembers
247 f_variable_new VAR_GROUP_PASSWORD groupPassword
248 f_variable_new VAR_HOSTNAME hostname
249 f_variable_new VAR_HTTP_DIR httpDirectory
250 f_variable_new VAR_HTTP_FTP_MODE httpFtpMode
251 f_variable_new VAR_HTTP_HOST httpHost
252 f_variable_new VAR_HTTP_PATH _httpPath
253 f_variable_new VAR_HTTP_PORT httpPort
254 f_variable_new VAR_HTTP_PROXY httpProxy
255 f_variable_new VAR_HTTP_PROXY_HOST httpProxyHost
256 f_variable_new VAR_HTTP_PROXY_PATH _httpProxyPath
257 f_variable_new VAR_HTTP_PROXY_PORT httpProxyPort
258 f_variable_new VAR_IFCONFIG ifconfig_
259 f_variable_new VAR_IPADDR ipaddr
260 f_variable_new VAR_IPV6ADDR ipv6addr
261 f_variable_new VAR_IPV6_ENABLE ipv6_activate_all_interfaces
262 f_variable_new VAR_KEYMAP keymap
263 f_variable_new VAR_MEDIA_TIMEOUT MEDIA_TIMEOUT
264 f_variable_new VAR_MEDIA_TYPE mediaType
265 f_variable_new VAR_NAMESERVER nameserver
266 f_variable_new VAR_NETINTERACTIVE netInteractive
267 f_variable_new VAR_NETMASK netmask
268 f_variable_new VAR_NETWORK_DEVICE netDev
269 f_variable_new VAR_NFS_HOST nfsHost
270 f_variable_new VAR_NFS_PATH nfsPath
271 f_variable_new VAR_NFS_SECURE nfs_reserved_port_only
272 f_variable_new VAR_NFS_TCP nfs_use_tcp
273 f_variable_new VAR_NFS_V3 nfs_use_v3
274 f_variable_new VAR_NONINTERACTIVE nonInteractive
275 f_variable_new VAR_NO_CONFIRM noConfirm
276 f_variable_new VAR_NO_ERROR noError
277 f_variable_new VAR_NO_INET6 noInet6
278 f_variable_new VAR_PACKAGE package
279 f_variable_new VAR_PKG_TMPDIR PKG_TMPDIR
280 f_variable_new VAR_PORTS_PATH ports
281 f_variable_new VAR_RELNAME releaseName
282 f_variable_new VAR_SLOW_ETHER slowEthernetCard
283 f_variable_new VAR_TRY_DHCP tryDHCP
284 f_variable_new VAR_TRY_RTSOL tryRTSOL
285 f_variable_new VAR_UFS_PATH ufs
286 f_variable_new VAR_USER user
287 f_variable_new VAR_USER_ACCOUNT_EXPIRE userAccountExpire
288 f_variable_new VAR_USER_DOTFILES_CREATE userDotfilesCreate
289 f_variable_new VAR_USER_GECOS userGecos
290 f_variable_new VAR_USER_GID userGid
291 f_variable_new VAR_USER_GROUPS userGroups
292 f_variable_new VAR_USER_GROUP_DELETE userGroupDelete
293 f_variable_new VAR_USER_HOME userHome
294 f_variable_new VAR_USER_HOME_CREATE userHomeCreate
295 f_variable_new VAR_USER_HOME_DELETE userHomeDelete
296 f_variable_new VAR_USER_LOGIN_CLASS userLoginClass
297 f_variable_new VAR_USER_PASSWORD userPassword
298 f_variable_new VAR_USER_PASSWORD_EXPIRE userPasswordExpire
299 f_variable_new VAR_USER_SHELL userShell
300 f_variable_new VAR_USER_UID userUid
301 f_variable_new VAR_ZFSINTERACTIVE zfsInteractive
304 # Self-initialize unless requested otherwise
306 f_dprintf "%s: VARIABLE_SELF_INITIALIZE=[%s]" \
307 variable.subr "$VARIABLE_SELF_INITIALIZE"
308 case "$VARIABLE_SELF_INITIALIZE" in
309 ""|0|[Nn][Oo]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]) : do nothing ;;
310 *) f_variable_set_defaults
313 f_dprintf "%s: Successfully loaded." variable.subr
315 fi # ! $_VARIABLE_SUBR