xref: /freebsd/usr.sbin/bsdconfig/share/script.subr (revision 5e7aa7574445f1fc90140bc93ec75de83e850fbe)
199bc932eSDevin Teskeif [ ! "$_SCRIPT_SUBR" ]; then _SCRIPT_SUBR=1
299bc932eSDevin Teske#
3e5cb2e69SDevin 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..." script.subr
337323adacSDevin Teskef_include $BSDCFG_SHARE/device.subr
347323adacSDevin Teskef_include $BSDCFG_SHARE/media/any.subr
357323adacSDevin Teskef_include $BSDCFG_SHARE/media/tcpip.subr
360e30bd5bSDevin Teskef_include $BSDCFG_SHARE/mustberoot.subr
3738ae478bSDevin Teskef_include $BSDCFG_SHARE/networking/services.subr
3887c16275SDevin Teskef_include $BSDCFG_SHARE/packages/packages.subr
3927c43fe1SDevin Teskef_include $BSDCFG_SHARE/usermgmt/group.subr
40*f589320aSDevin Teskef_include $BSDCFG_SHARE/usermgmt/user.subr
41fa8b43c8SDevin Teskef_include $BSDCFG_SHARE/variable.subr
4299bc932eSDevin Teske
4399bc932eSDevin Teske############################################################ GLOBALS
4499bc932eSDevin Teske
4599bc932eSDevin TeskeRESWORDS=
4699bc932eSDevin Teske
4799bc932eSDevin Teske############################################################ FUNCTIONS
4899bc932eSDevin Teske
4999bc932eSDevin Teske# f_resword_new $resword $function
5099bc932eSDevin Teske#
5199bc932eSDevin Teske# Create a new `reserved' word for scripting purposes. Reswords call pre-
5299bc932eSDevin Teske# defined functions but differ from those functions in the following ways:
5399bc932eSDevin Teske#
5499bc932eSDevin Teske# 	+ Unless noError is set (must be non-NULL), if calling the resword
5599bc932eSDevin Teske# 	  results in failure, the application will terminate prematurely.
5699bc932eSDevin Teske# 	+ noError is unset after each/every resword is called.
5799bc932eSDevin Teske#
5899bc932eSDevin Teske# Reswords should not be used in bsdconfig itself (hence the name `reserved
59e5cb2e69SDevin Teske# word') but instead only in scripts loaded through f_script_load().
6099bc932eSDevin Teske#
6199bc932eSDevin Teskef_resword_new()
6299bc932eSDevin Teske{
6399bc932eSDevin Teske	local resword="$1" func="$2"
6499bc932eSDevin Teske	[ "$resword" ] || return $FAILURE
6599bc932eSDevin Teske	f_dprintf "script.subr: New resWord %s -> %s" "$resword" "$func"
66e5cb2e69SDevin Teske	eval $resword\(\){ f_dispatch $func $resword \"\$@\"\; }
6799bc932eSDevin Teske	RESWORDS="$RESWORDS${RESWORDS:+ }$resword"
6899bc932eSDevin Teske}
6999bc932eSDevin Teske
70e5cb2e69SDevin Teske# f_dispatch $func $resword
7199bc932eSDevin Teske#
7299bc932eSDevin Teske# Wrapper function used by `reserved words' (reswords) to call other functions.
7399bc932eSDevin Teske# If $noError is set and non-NULL, a failure result from $func is ignored,
7499bc932eSDevin Teske# otherwise the application is prematurely terminated using f_die().
7599bc932eSDevin Teske#
7699bc932eSDevin Teske# NOTE: $noError is unset after every call.
7799bc932eSDevin Teske#
7899bc932eSDevin Teskef_dispatch()
7999bc932eSDevin Teske{
80e5cb2e69SDevin Teske	local func="$1" resword="$2"
81e5cb2e69SDevin Teske	shift 2 # func resword
8299bc932eSDevin Teske	f_dprintf "f_dispatch: calling resword \`%s'" "$resword"
83e5cb2e69SDevin Teske	eval $func "$@"
847323adacSDevin Teske	local retval=$?
857323adacSDevin Teske	if [ $retval -ne $SUCCESS ]; then
867323adacSDevin Teske		local _ignore_this_error
8799bc932eSDevin Teske		f_getvar $VAR_NO_ERROR _ignore_this_error
8899bc932eSDevin Teske		[ "$_ignore_this_error" ] || f_die $retval \
8999bc932eSDevin Teske			"$msg_command_failed_rest_of_script_aborted" "$resword"
907323adacSDevin Teske	fi
9199bc932eSDevin Teske	unset $VAR_NO_ERROR
9299bc932eSDevin Teske}
9399bc932eSDevin Teske
9499bc932eSDevin Teske# f_script_load [$file]
9599bc932eSDevin Teske#
9699bc932eSDevin Teske# Load a script (usually filled with reswords). If $file is missing or NULL,
9799bc932eSDevin Teske# use one of the following instead (in order):
9899bc932eSDevin Teske#
994bed406fSDevin Teske# 	$configFile (global)
10099bc932eSDevin Teske# 	install.cfg
10199bc932eSDevin Teske# 	/stand/install.fg
10299bc932eSDevin Teske# 	/tmp/install.cfg
10399bc932eSDevin Teske#
10499bc932eSDevin Teske# Unknown/unregistered reswords will generate sh(1) syntax errors but not cause
10599bc932eSDevin Teske# premature termination.
10699bc932eSDevin Teske#
10799bc932eSDevin Teske# Returns success if a script was loaded and itself returned success.
10899bc932eSDevin Teske#
10999bc932eSDevin Teskef_script_load()
11099bc932eSDevin Teske{
1114bed406fSDevin Teske	local funcname=f_script_load
1128ff9900cSDevin Teske	local script="$1" config_file retval=$SUCCESS
11399bc932eSDevin Teske
1144bed406fSDevin Teske	f_dprintf "$funcname: script=[%s]" "$script"
11599bc932eSDevin Teske	if [ ! "$script" ]; then
11699bc932eSDevin Teske		f_getvar $VAR_CONFIG_FILE config_file
11799bc932eSDevin Teske		for script in \
11899bc932eSDevin Teske			$config_file \
11999bc932eSDevin Teske			install.cfg \
12099bc932eSDevin Teske			/stand/install.cfg \
12199bc932eSDevin Teske			/tmp/install.cfg \
12299bc932eSDevin Teske		; do
12399bc932eSDevin Teske			[ -e "$script" ] && break
12499bc932eSDevin Teske		done
1258ff9900cSDevin Teske	fi
1268ff9900cSDevin Teske
1278ff9900cSDevin Teske	local old_interactive=
1288ff9900cSDevin Teske	f_getvar $VAR_NONINTERACTIVE old_interactive # save a copy
1298ff9900cSDevin Teske
1308ff9900cSDevin Teske	# Hint to others that we're running from a script, should they care
1318ff9900cSDevin Teske	setvar $VAR_NONINTERACTIVE yes
1328ff9900cSDevin Teske
1338ff9900cSDevin Teske	if [ "$script" = "-" ]; then
1344bed406fSDevin Teske		f_dprintf "$funcname: Loading script from stdin"
13599bc932eSDevin Teske		eval "$( cat )"
1368ff9900cSDevin Teske		retval=$?
13799bc932eSDevin Teske	else
1384bed406fSDevin Teske		f_dprintf "$funcname: Loading script \`%s'" "$script"
13999bc932eSDevin Teske		if [ ! -e "$script" ]; then
14099bc932eSDevin Teske			f_show_msg "$msg_unable_to_open" "$script"
14199bc932eSDevin Teske			return $FAILURE
14299bc932eSDevin Teske		fi
14399bc932eSDevin Teske		. "$script"
1448ff9900cSDevin Teske		retval=$?
14599bc932eSDevin Teske	fi
1468ff9900cSDevin Teske
1478ff9900cSDevin Teske	[ "$old_interactive" ] &&
1488ff9900cSDevin Teske		setvar $VAR_NONINTERACTIVE "$old_interactive"
1498ff9900cSDevin Teske
1508ff9900cSDevin Teske	return $retval
15199bc932eSDevin Teske}
15299bc932eSDevin Teske
15399bc932eSDevin Teske############################################################ MAIN
15499bc932eSDevin Teske
15599bc932eSDevin Teske#
15699bc932eSDevin Teske# Reserved words meant for scripting
15799bc932eSDevin Teske#
15824e8d282SDevin Teske
15924e8d282SDevin Teske# this file
160d4fd7c13SDevin Teskef_resword_new loadConfig	f_script_load
16124e8d282SDevin Teske
16224e8d282SDevin Teske# device.subr
16324e8d282SDevin Teskef_resword_new deviceRescan	f_device_rescan
16424e8d282SDevin Teske
16524e8d282SDevin Teske# media/common.subr
166d4fd7c13SDevin Teskef_resword_new mediaOpen		f_media_open
16724e8d282SDevin Teskef_resword_new mediaClose	f_media_close
16824e8d282SDevin Teske
16924e8d282SDevin Teske# media includes
17024e8d282SDevin Teskef_resword_new mediaGetType	f_media_get_type         # media/any.subr
17124e8d282SDevin Teskef_resword_new mediaSetCDROM	f_media_set_cdrom        # media/cdrom.subr
17224e8d282SDevin Teskef_resword_new mediaSetDOS	f_media_set_dos          # media/dos.subr
17324e8d282SDevin Teskef_resword_new mediaSetDirectory	f_media_set_directory    # media/directory.subr
17424e8d282SDevin Teskef_resword_new mediaSetNFS	f_media_set_nfs          # media/nfs.subr
17524e8d282SDevin Teskef_resword_new mediaSetUFS	f_media_set_ufs          # media/ufs.subr
17624e8d282SDevin Teskef_resword_new mediaSetUSB	f_media_set_usb          # media/usb.subr
17724e8d282SDevin Teskef_resword_new optionsEditor	f_media_options_menu     # media/options.subr
17824e8d282SDevin Teskef_resword_new tcpMenuSelect	f_dialog_menu_select_tcp # media/tcp.subr
17924e8d282SDevin Teske
18047b73aa2SDevin Teske# media/http.subr
18147b73aa2SDevin Teskef_resword_new mediaSetHTTP	f_media_set_http
18247b73aa2SDevin Teske
18324e8d282SDevin Teske# media/httpproxy.subr
1847323adacSDevin Teskef_resword_new mediaSetHTTPProxy	f_media_set_http_proxy
18599bc932eSDevin Teske
18638ae478bSDevin Teske# networking/services.subr
18738ae478bSDevin Teskef_resword_new configPCNFSD	f_config_pcnfsd
18838ae478bSDevin Teske
18987c16275SDevin Teske# packages/packages.subr
19087c16275SDevin Teskef_resword_new configPackages	f_package_config
19131185df0SDevin Teskef_resword_new packageAdd	f_package_add
192542dd84bSDevin Teskef_resword_new packageDelete	f_package_delete
19352d41f91SDevin Teskef_resword_new packageReinstall	f_package_reinstall
19487c16275SDevin Teske
19527c43fe1SDevin Teske# usermgmt/group.subr
1969aab41b1SDevin Teskef_resword_new addGroup		f_group_add
1979aab41b1SDevin Teskef_resword_new deleteGroup	f_group_delete
1989aab41b1SDevin Teskef_resword_new editGroup		f_group_edit
19927c43fe1SDevin Teske
200*f589320aSDevin Teske# usermgmt/user.subr
201*f589320aSDevin Teskef_resword_new addUser		f_user_add
202*f589320aSDevin Teskef_resword_new deleteUser	f_user_delete
203*f589320aSDevin Teskef_resword_new editUser		f_user_edit
204*f589320aSDevin Teske
2053daac03bSDevin Teske# variable.subr
2063daac03bSDevin Teskef_resword_new installVarDefaults	f_variable_set_defaults
2073daac03bSDevin Teskef_resword_new dumpVariables		f_dump_variables
2083daac03bSDevin Teske
20999bc932eSDevin Teskef_dprintf "%s: Successfully loaded." script.subr
21099bc932eSDevin Teske
21199bc932eSDevin Teskefi # ! $_SCRIPT_SUBR
212