script.subr revision 263980
10SN/Aif [ ! "$_SCRIPT_SUBR" ]; then _SCRIPT_SUBR=1
29330SN/A#
30SN/A# Copyright (c) 2012-2014 Devin Teske
40SN/A# All rights reserved.
50SN/A#
60SN/A# Redistribution and use in source and binary forms, with or without
72362SN/A# modification, are permitted provided that the following conditions
80SN/A# are met:
92362SN/A# 1. Redistributions of source code must retain the above copyright
100SN/A#    notice, this list of conditions and the following disclaimer.
110SN/A# 2. Redistributions in binary form must reproduce the above copyright
120SN/A#    notice, this list of conditions and the following disclaimer in the
130SN/A#    documentation and/or other materials provided with the distribution.
140SN/A#
150SN/A# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
160SN/A# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
170SN/A# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
180SN/A# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
190SN/A# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
200SN/A# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
212362SN/A# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
222362SN/A# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
232362SN/A# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
240SN/A# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
250SN/A# SUCH DAMAGE.
260SN/A#
270SN/A# $FreeBSD: stable/10/usr.sbin/bsdconfig/share/script.subr 263980 2014-04-01 00:19:13Z dteske $
280SN/A#
290SN/A############################################################ INCLUDES
300SN/A
310SN/ABSDCFG_SHARE="/usr/share/bsdconfig"
320SN/A. $BSDCFG_SHARE/common.subr || exit 1
330SN/Af_dprintf "%s: loading includes..." script.subr
340SN/Af_include $BSDCFG_SHARE/device.subr
350SN/Af_include $BSDCFG_SHARE/media/any.subr
360SN/Af_include $BSDCFG_SHARE/media/tcpip.subr
370SN/Af_include $BSDCFG_SHARE/mustberoot.subr
380SN/Af_include $BSDCFG_SHARE/networking/services.subr
390SN/Af_include $BSDCFG_SHARE/packages/packages.subr
400SN/Af_include $BSDCFG_SHARE/usermgmt/group.subr
410SN/Af_include $BSDCFG_SHARE/usermgmt/user.subr
420SN/Af_include $BSDCFG_SHARE/variable.subr
430SN/A
440SN/A############################################################ GLOBALS
450SN/A
460SN/ARESWORDS=
4710071SN/A
480SN/A############################################################ FUNCTIONS
490SN/A
500SN/A# f_resword_new $resword $function
510SN/A#
520SN/A# Create a new `reserved' word for scripting purposes. Reswords call pre-
530SN/A# defined functions but differ from those functions in the following ways:
540SN/A#
550SN/A# 	+ Unless noError is set (must be non-NULL), if calling the resword
560SN/A# 	  results in failure, the application will terminate prematurely.
570SN/A# 	+ noError is unset after each/every resword is called.
580SN/A#
590SN/A# Reswords should not be used in bsdconfig itself (hence the name `reserved
600SN/A# word') but instead only in scripts loaded through f_script_load().
610SN/A#
620SN/Af_resword_new()
630SN/A{
640SN/A	local resword="$1" func="$2"
650SN/A	[ "$resword" ] || return $FAILURE
660SN/A	f_dprintf "script.subr: New resWord %s -> %s" "$resword" "$func"
670SN/A	eval $resword\(\){ f_dispatch $func $resword \"\$@\"\; }
680SN/A	RESWORDS="$RESWORDS${RESWORDS:+ }$resword"
690SN/A}
700SN/A
710SN/A# f_dispatch $func $resword
720SN/A#
730SN/A# Wrapper function used by `reserved words' (reswords) to call other functions.
740SN/A# If $noError is set and non-NULL, a failure result from $func is ignored,
750SN/A# otherwise the application is prematurely terminated using f_die().
760SN/A#
770SN/A# NOTE: $noError is unset after every call.
780SN/A#
790SN/Af_dispatch()
800SN/A{
810SN/A	local func="$1" resword="$2"
820SN/A	shift 2 # func resword
830SN/A	f_dprintf "f_dispatch: calling resword \`%s'" "$resword"
840SN/A	eval $func "$@"
850SN/A	local retval=$?
860SN/A	if [ $retval -ne $SUCCESS ]; then
870SN/A		local _ignore_this_error
880SN/A		f_getvar $VAR_NO_ERROR _ignore_this_error
890SN/A		[ "$_ignore_this_error" ] || f_die $retval \
900SN/A			"$msg_command_failed_rest_of_script_aborted" "$resword"
910SN/A	fi
920SN/A	unset $VAR_NO_ERROR
930SN/A}
940SN/A
950SN/A# f_script_load [$file]
960SN/A#
970SN/A# Load a script (usually filled with reswords). If $file is missing or NULL,
980SN/A# use one of the following instead (in order):
990SN/A#
1000SN/A# 	$configFile (global)
1010SN/A# 	install.cfg
1020SN/A# 	/stand/install.fg
1030SN/A# 	/tmp/install.cfg
1040SN/A#
1050SN/A# Unknown/unregistered reswords will generate sh(1) syntax errors but not cause
1060SN/A# premature termination.
1070SN/A#
1080SN/A# Returns success if a script was loaded and itself returned success.
1090SN/A#
1100SN/Af_script_load()
1110SN/A{
1120SN/A	local funcname=f_script_load
1130SN/A	local script="$1" config_file retval=$SUCCESS
1140SN/A
1150SN/A	f_dprintf "$funcname: script=[%s]" "$script"
1160SN/A	if [ ! "$script" ]; then
1170SN/A		f_getvar $VAR_CONFIG_FILE config_file
1180SN/A		for script in \
1190SN/A			$config_file \
1200SN/A			install.cfg \
1210SN/A			/stand/install.cfg \
1220SN/A			/tmp/install.cfg \
1230SN/A		; do
1240SN/A			[ -e "$script" ] && break
1250SN/A		done
1260SN/A	fi
1270SN/A
1280SN/A	local old_interactive=
1290SN/A	f_getvar $VAR_NONINTERACTIVE old_interactive # save a copy
1300SN/A
1310SN/A	# Hint to others that we're running from a script, should they care
1320SN/A	setvar $VAR_NONINTERACTIVE yes
1330SN/A
1340SN/A	if [ "$script" = "-" ]; then
1350SN/A		f_dprintf "$funcname: Loading script from stdin"
1360SN/A		eval "$( cat )"
1378621SN/A		retval=$?
1388621SN/A	else
1398621SN/A		f_dprintf "$funcname: Loading script \`%s'" "$script"
1408621SN/A		if [ ! -e "$script" ]; then
1418621SN/A			f_show_msg "$msg_unable_to_open" "$script"
1420SN/A			return $FAILURE
1430SN/A		fi
1440SN/A		. "$script"
1450SN/A		retval=$?
1460SN/A	fi
1470SN/A
1480SN/A	[ "$old_interactive" ] &&
1490SN/A		setvar $VAR_NONINTERACTIVE "$old_interactive"
1500SN/A
1510SN/A	return $retval
1520SN/A}
1530SN/A
1540SN/A############################################################ MAIN
1550SN/A
1560SN/A#
1570SN/A# Reserved words meant for scripting
1580SN/A#
1598621SN/A
1608621SN/A# this file
1618621SN/Af_resword_new loadConfig	f_script_load
1628621SN/A
1630SN/A# device.subr
1640SN/Af_resword_new deviceRescan	f_device_rescan
1650SN/A
1660SN/A# media/common.subr
1670SN/Af_resword_new mediaOpen		f_media_open
1680SN/Af_resword_new mediaClose	f_media_close
1690SN/A
1700SN/A# media includes
1710SN/Af_resword_new mediaGetType	f_media_get_type         # media/any.subr
1720SN/Af_resword_new mediaSetCDROM	f_media_set_cdrom        # media/cdrom.subr
1730SN/Af_resword_new mediaSetDOS	f_media_set_dos          # media/dos.subr
1740SN/Af_resword_new mediaSetDirectory	f_media_set_directory    # media/directory.subr
1750SN/Af_resword_new mediaSetFloppy	f_media_set_floppy       # media/floppy.subr
1760SN/Af_resword_new mediaSetNFS	f_media_set_nfs          # media/nfs.subr
1770SN/Af_resword_new mediaSetUFS	f_media_set_ufs          # media/ufs.subr
1780SN/Af_resword_new mediaSetUSB	f_media_set_usb          # media/usb.subr
1798621SN/Af_resword_new optionsEditor	f_media_options_menu     # media/options.subr
1808621SN/Af_resword_new tcpMenuSelect	f_dialog_menu_select_tcp # media/tcp.subr
1818621SN/A
1828621SN/A# media/ftp.subr
1838621SN/Af_resword_new mediaSetFTP		f_media_set_ftp
1848621SN/Af_resword_new mediaSetFTPActive		f_media_set_ftp_active
1850SN/Af_resword_new mediaSetFTPPassive	f_media_set_ftp_passive
1860SN/Af_resword_new mediaSetFTPUserPass	f_media_set_ftp_userpass
1870SN/A
1880SN/A# media/http.subr
1890SN/Af_resword_new mediaSetHTTP	f_media_set_http
1900SN/A
1910SN/A# media/httpproxy.subr
1920SN/Af_resword_new mediaSetHTTPProxy	f_media_set_http_proxy
1930SN/A
1940SN/A# networking/services.subr
1950SN/Af_resword_new configPCNFSD	f_config_pcnfsd
1960SN/A
1970SN/A# packages/packages.subr
1980SN/Af_resword_new configPackages	f_package_config
1990SN/Af_resword_new packageAdd	f_package_add
2008621SN/Af_resword_new packageDelete	f_package_delete
2018621SN/Af_resword_new packageReinstall	f_package_reinstall
2028621SN/A
2038621SN/A# usermgmt/group.subr
2048621SN/Af_resword_new addGroup		f_group_add
2058621SN/Af_resword_new deleteGroup	f_group_delete
2068621SN/Af_resword_new editGroup		f_group_edit
2078621SN/A
2088621SN/A# usermgmt/user.subr
2098621SN/Af_resword_new addUser		f_user_add
2100SN/Af_resword_new deleteUser	f_user_delete
2110SN/Af_resword_new editUser		f_user_edit
2120SN/A
2130SN/A# variable.subr
2140SN/Af_resword_new installVarDefaults	f_variable_set_defaults
2150SN/Af_resword_new dumpVariables		f_dump_variables
2160SN/A
2170SN/Af_dprintf "%s: Successfully loaded." script.subr
2180SN/A
2190SN/Afi # ! $_SCRIPT_SUBR
2200SN/A