script.subr revision 251997
113685Swoschif [ ! "$_SCRIPT_SUBR" ]; then _SCRIPT_SUBR=1
213685Swosch#
313685Swosch# Copyright (c) 2012-2013 Devin Teske
450477Speter# All Rights Reserved.
513685Swosch#
613685Swosch# Redistribution and use in source and binary forms, with or without
713685Swosch# modification, are permitted provided that the following conditions
813685Swosch# are met:
913685Swosch# 1. Redistributions of source code must retain the above copyright
101590Srgrimes#    notice, this list of conditions and the following disclaimer.
1113792Smpp# 2. Redistributions in binary form must reproduce the above copyright
121590Srgrimes#    notice, this list of conditions and the following disclaimer in the
131590Srgrimes#    documentation and/or other materials provided with the distribution.
141590Srgrimes#
151590Srgrimes# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
161590Srgrimes# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
171590Srgrimes# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181590Srgrimes# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191590Srgrimes# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201590Srgrimes# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211590Srgrimes# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221590Srgrimes# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231590Srgrimes# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241590Srgrimes# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251590Srgrimes# SUCH DAMAGE.
261590Srgrimes#
271590Srgrimes# $FreeBSD: head/usr.sbin/bsdconfig/share/script.subr 251997 2013-06-19 17:14:59Z dteske $
281590Srgrimes#
291590Srgrimes############################################################ INCLUDES
301590Srgrimes
311590SrgrimesBSDCFG_SHARE="/usr/share/bsdconfig"
321590Srgrimes. $BSDCFG_SHARE/common.subr || exit 1
331590Srgrimesf_dprintf "%s: loading includes..." script.subr
341590Srgrimesf_include $BSDCFG_SHARE/device.subr
351590Srgrimesf_include $BSDCFG_SHARE/media/any.subr
361590Srgrimesf_include $BSDCFG_SHARE/media/tcpip.subr
371590Srgrimesf_include $BSDCFG_SHARE/packages/packages.subr
3822152Smppf_include $BSDCFG_SHARE/variable.subr
391590Srgrimes
401590Srgrimes############################################################ GLOBALS
411590Srgrimes
421590SrgrimesRESWORDS=
431590Srgrimes
441590Srgrimes############################################################ FUNCTIONS
451590Srgrimes
461590Srgrimes# f_resword_new $resword $function
471590Srgrimes#
481590Srgrimes# Create a new `reserved' word for scripting purposes. Reswords call pre-
491590Srgrimes# defined functions but differ from those functions in the following ways:
501590Srgrimes#
5154758Scpiazza# 	+ Reswords do not take arguments but instead get all their data from
521590Srgrimes# 	  the environment variable namespace.
5313792Smpp# 	+ Unless noError is set (must be non-NULL), if calling the resword
541590Srgrimes# 	  results in failure, the application will terminate prematurely.
551590Srgrimes# 	+ noError is unset after each/every resword is called.
561590Srgrimes#
571590Srgrimes# Reswords should not be used in bsdconfig itself (hence the name `reserved
581590Srgrimes# word') but instead only in scripts loaded through f_script_load()).
591590Srgrimes#
601590Srgrimesf_resword_new()
611590Srgrimes{
621590Srgrimes	local resword="$1" func="$2"
631590Srgrimes	[ "$resword" ] || return $FAILURE
641590Srgrimes	f_dprintf "script.subr: New resWord %s -> %s" "$resword" "$func"
651590Srgrimes	eval $resword\(\){ f_dispatch $func $resword\; }
661590Srgrimes	RESWORDS="$RESWORDS${RESWORDS:+ }$resword"
671590Srgrimes}
681590Srgrimes
691590Srgrimes# f_dispatch $func [$resword]
701590Srgrimes#
711590Srgrimes# Wrapper function used by `reserved words' (reswords) to call other functions.
721590Srgrimes# If $noError is set and non-NULL, a failure result from $func is ignored,
731590Srgrimes# otherwise the application is prematurely terminated using f_die().
741590Srgrimes#
751590Srgrimes# NOTE: $noError is unset after every call.
761590Srgrimes#
771590Srgrimesf_dispatch()
781590Srgrimes{
791590Srgrimes	local func="$1" resword="${2:-$1}"
801590Srgrimes	f_dprintf "f_dispatch: calling resword \`%s'" "$resword"
811590Srgrimes	eval $func
821590Srgrimes	local retval=$?
831590Srgrimes	if [ $retval -ne $SUCCESS ]; then
841590Srgrimes		local _ignore_this_error
851590Srgrimes		f_getvar $VAR_NO_ERROR _ignore_this_error
861590Srgrimes		[ "$_ignore_this_error" ] || f_die $retval \
8723094Smpp			"$msg_command_failed_rest_of_script_aborted" "$resword"
881590Srgrimes	fi
891590Srgrimes	unset $VAR_NO_ERROR
901590Srgrimes}
911590Srgrimes
92102501Sgrog# f_script_load [$file]
931590Srgrimes#
941590Srgrimes# Load a script (usually filled with reswords). If $file is missing or NULL,
951590Srgrimes# use one of the following instead (in order):
961590Srgrimes#
971590Srgrimes# 	$configFile
981590Srgrimes# 	install.cfg
991590Srgrimes# 	/stand/install.fg
1001590Srgrimes# 	/tmp/install.cfg
1011590Srgrimes#
1021590Srgrimes# Unknown/unregistered reswords will generate sh(1) syntax errors but not cause
1031590Srgrimes# premature termination.
1041590Srgrimes#
1051590Srgrimes# Returns success if a script was loaded and itself returned success.
1061590Srgrimes#
1071590Srgrimesf_script_load()
1081590Srgrimes{
1091590Srgrimes	local script="$1" config_file retval=$SUCCESS
1101590Srgrimes
1111590Srgrimes	f_dprintf "f_script_load: script=[%s]" "$script"
1121590Srgrimes	if [ ! "$script" ]; then
1131590Srgrimes		f_getvar $VAR_CONFIG_FILE config_file
1141590Srgrimes		for script in \
1151590Srgrimes			$config_file \
11692217Sgrog			install.cfg \
1171590Srgrimes			/stand/install.cfg \
1181590Srgrimes			/tmp/install.cfg \
1191590Srgrimes		; do
1201590Srgrimes			[ -e "$script" ] && break
1211590Srgrimes		done
12250848Snik	fi
1231590Srgrimes
12450848Snik	local old_interactive=
1251590Srgrimes	f_getvar $VAR_NONINTERACTIVE old_interactive # save a copy
1261590Srgrimes
1271590Srgrimes	# Hint to others that we're running from a script, should they care
1281590Srgrimes	setvar $VAR_NONINTERACTIVE yes
1291590Srgrimes
1301590Srgrimes	if [ "$script" = "-" ]; then
1311590Srgrimes		f_dprintf "f_script_load: Loading script from stdin"
1321590Srgrimes		eval "$( cat )"
1331590Srgrimes		retval=$?
1341590Srgrimes	else
1351590Srgrimes		f_dprintf "f_script_load: Loading script \`%s'" "$script"
1361590Srgrimes		if [ ! -e "$script" ]; then
13713792Smpp			f_show_msg "$msg_unable_to_open" "$script"
1381590Srgrimes			return $FAILURE
13913792Smpp		fi
1401590Srgrimes		. "$script"
1411590Srgrimes		retval=$?
1421590Srgrimes	fi
1431590Srgrimes
1441590Srgrimes	[ "$old_interactive" ] &&
1451590Srgrimes		setvar $VAR_NONINTERACTIVE "$old_interactive"
1461590Srgrimes
1471590Srgrimes	return $retval
1481590Srgrimes}
1491590Srgrimes
1501590Srgrimes############################################################ MAIN
1511590Srgrimes
1521590Srgrimes#
1531590Srgrimes# Reserved words meant for scripting
1541590Srgrimes#
1551590Srgrimes
1561590Srgrimes# this file
1571590Srgrimesf_resword_new loadConfig	f_script_load
1581590Srgrimes
1591590Srgrimes# device.subr
1601590Srgrimesf_resword_new deviceRescan	f_device_rescan
1611590Srgrimes
1621590Srgrimes# media/common.subr
1631590Srgrimesf_resword_new mediaOpen		f_media_open
164102545Sgrogf_resword_new mediaClose	f_media_close
1651590Srgrimes
1661590Srgrimes# media includes
1671590Srgrimesf_resword_new mediaGetType	f_media_get_type         # media/any.subr
1681590Srgrimesf_resword_new mediaSetCDROM	f_media_set_cdrom        # media/cdrom.subr
1691590Srgrimesf_resword_new mediaSetDOS	f_media_set_dos          # media/dos.subr
1701590Srgrimesf_resword_new mediaSetDirectory	f_media_set_directory    # media/directory.subr
1711590Srgrimesf_resword_new mediaSetFloppy	f_media_set_floppy       # media/floppy.subr
1721590Srgrimesf_resword_new mediaSetNFS	f_media_set_nfs          # media/nfs.subr
1731590Srgrimesf_resword_new mediaSetUFS	f_media_set_ufs          # media/ufs.subr
1741590Srgrimesf_resword_new mediaSetUSB	f_media_set_usb          # media/usb.subr
17513792Smppf_resword_new optionsEditor	f_media_options_menu     # media/options.subr
1761590Srgrimesf_resword_new tcpMenuSelect	f_dialog_menu_select_tcp # media/tcp.subr
1771590Srgrimes
1781590Srgrimes# media/ftp.subr
1791590Srgrimesf_resword_new mediaSetFTP		f_media_set_ftp
1801590Srgrimesf_resword_new mediaSetFTPActive		f_media_set_ftp_active
1811590Srgrimesf_resword_new mediaSetFTPPassive	f_media_set_ftp_passive
1821590Srgrimesf_resword_new mediaSetFTPUserPass	f_media_set_ftp_userpass
1831590Srgrimes
1841590Srgrimes# media/httpproxy.subr
1851590Srgrimesf_resword_new mediaSetHTTP	f_media_set_http_proxy
1861590Srgrimesf_resword_new mediaSetHTTPProxy	f_media_set_http_proxy
1871590Srgrimes
1881590Srgrimes# packages/packages.subr
1891590Srgrimesf_resword_new configPackages	f_package_config
1901590Srgrimes
1911590Srgrimes# variable.subr
1921590Srgrimesf_resword_new installVarDefaults	f_variable_set_defaults
1931590Srgrimesf_resword_new dumpVariables		f_dump_variables
1941590Srgrimes
1951590Srgrimesf_dprintf "%s: Successfully loaded." script.subr
1961590Srgrimes
1971590Srgrimesfi # ! $_SCRIPT_SUBR
1981590Srgrimes