script.subr revision 262900
1139825Simpif [ ! "$_SCRIPT_SUBR" ]; then _SCRIPT_SUBR=1
277957Sbenno#
377957Sbenno# Copyright (c) 2012-2014 Devin Teske
477957Sbenno# All rights reserved.
577957Sbenno#
677957Sbenno# Redistribution and use in source and binary forms, with or without
777957Sbenno# modification, are permitted provided that the following conditions
877957Sbenno# are met:
977957Sbenno# 1. Redistributions of source code must retain the above copyright
1077957Sbenno#    notice, this list of conditions and the following disclaimer.
1177957Sbenno# 2. Redistributions in binary form must reproduce the above copyright
1277957Sbenno#    notice, this list of conditions and the following disclaimer in the
1377957Sbenno#    documentation and/or other materials provided with the distribution.
1477957Sbenno#
1577957Sbenno# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1677957Sbenno# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1777957Sbenno# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1877957Sbenno# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1977957Sbenno# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2077957Sbenno# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2177957Sbenno# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2277957Sbenno# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2377957Sbenno# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2477957Sbenno# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2577957Sbenno# SUCH DAMAGE.
2677957Sbenno#
2777957Sbenno# $FreeBSD: head/usr.sbin/bsdconfig/share/script.subr 262900 2014-03-07 20:24:59Z dteske $
2877957Sbenno#
2977957Sbenno############################################################ INCLUDES
3077957Sbenno
3177957SbennoBSDCFG_SHARE="/usr/share/bsdconfig"
3277957Sbenno. $BSDCFG_SHARE/common.subr || exit 1
3377957Sbennof_dprintf "%s: loading includes..." script.subr
3477957Sbennof_include $BSDCFG_SHARE/device.subr
3577957Sbennof_include $BSDCFG_SHARE/media/any.subr
3677957Sbennof_include $BSDCFG_SHARE/media/tcpip.subr
3777957Sbennof_include $BSDCFG_SHARE/mustberoot.subr
3877957Sbennof_include $BSDCFG_SHARE/networking/services.subr
3977957Sbennof_include $BSDCFG_SHARE/packages/packages.subr
4077957Sbennof_include $BSDCFG_SHARE/variable.subr
4177957Sbenno
42235013Snwhitehorn############################################################ GLOBALS
43235013Snwhitehorn
44235013SnwhitehornRESWORDS=
45235013Snwhitehorn
46235013Snwhitehorn############################################################ FUNCTIONS
47235013Snwhitehorn
48234579Snwhitehorn# f_resword_new $resword $function
4977957Sbenno#
5077957Sbenno# Create a new `reserved' word for scripting purposes. Reswords call pre-
5177957Sbenno# defined functions but differ from those functions in the following ways:
5277957Sbenno#
53234579Snwhitehorn# 	+ Unless noError is set (must be non-NULL), if calling the resword
5477957Sbenno# 	  results in failure, the application will terminate prematurely.
5577957Sbenno# 	+ noError is unset after each/every resword is called.
5677957Sbenno#
5777957Sbenno# Reswords should not be used in bsdconfig itself (hence the name `reserved
5877957Sbenno# word') but instead only in scripts loaded through f_script_load().
5977957Sbenno#
60234579Snwhitehornf_resword_new()
6177957Sbenno{
6277957Sbenno	local resword="$1" func="$2"
6377957Sbenno	[ "$resword" ] || return $FAILURE
6477957Sbenno	f_dprintf "script.subr: New resWord %s -> %s" "$resword" "$func"
6577957Sbenno	eval $resword\(\){ f_dispatch $func $resword \"\$@\"\; }
6677957Sbenno	RESWORDS="$RESWORDS${RESWORDS:+ }$resword"
67234579Snwhitehorn}
6877957Sbenno
6977957Sbenno# f_dispatch $func $resword
7077957Sbenno#
71193578Sraj# Wrapper function used by `reserved words' (reswords) to call other functions.
72193578Sraj# If $noError is set and non-NULL, a failure result from $func is ignored,
73193578Sraj# otherwise the application is prematurely terminated using f_die().
74234579Snwhitehorn#
75193578Sraj# NOTE: $noError is unset after every call.
76193578Sraj#
77193578Srajf_dispatch()
7877957Sbenno{
7977957Sbenno	local func="$1" resword="$2"
8077957Sbenno	shift 2 # func resword
81234579Snwhitehorn	f_dprintf "f_dispatch: calling resword \`%s'" "$resword"
8277957Sbenno	eval $func "$@"
8377957Sbenno	local retval=$?
8477957Sbenno	if [ $retval -ne $SUCCESS ]; then
8577957Sbenno		local _ignore_this_error
8677957Sbenno		f_getvar $VAR_NO_ERROR _ignore_this_error
8777957Sbenno		[ "$_ignore_this_error" ] || f_die $retval \
88234579Snwhitehorn			"$msg_command_failed_rest_of_script_aborted" "$resword"
8977957Sbenno	fi
9077957Sbenno	unset $VAR_NO_ERROR
9177957Sbenno}
9277957Sbenno
9377957Sbenno# f_script_load [$file]
9477957Sbenno#
9577957Sbenno# Load a script (usually filled with reswords). If $file is missing or NULL,
9677957Sbenno# use one of the following instead (in order):
97234579Snwhitehorn#
9877957Sbenno# 	$configFile (global)
9977957Sbenno# 	install.cfg
10077957Sbenno# 	/stand/install.fg
10177957Sbenno# 	/tmp/install.cfg
10277957Sbenno#
10377957Sbenno# Unknown/unregistered reswords will generate sh(1) syntax errors but not cause
10477957Sbenno# premature termination.
10577957Sbenno#
10677957Sbenno# Returns success if a script was loaded and itself returned success.
107234579Snwhitehorn#
10877957Sbennof_script_load()
10977957Sbenno{
11077957Sbenno	local funcname=f_script_load
11177957Sbenno	local script="$1" config_file retval=$SUCCESS
11277957Sbenno
11377957Sbenno	f_dprintf "$funcname: script=[%s]" "$script"
11477957Sbenno	if [ ! "$script" ]; then
11577957Sbenno		f_getvar $VAR_CONFIG_FILE config_file
11677957Sbenno		for script in \
117234579Snwhitehorn			$config_file \
11877957Sbenno			install.cfg \
11977957Sbenno			/stand/install.cfg \
12077957Sbenno			/tmp/install.cfg \
121193578Sraj		; do
122193578Sraj			[ -e "$script" ] && break
123193578Sraj		done
124193578Sraj	fi
125193578Sraj
126193578Sraj	local old_interactive=
127234579Snwhitehorn	f_getvar $VAR_NONINTERACTIVE old_interactive # save a copy
128193578Sraj
129193578Sraj	# Hint to others that we're running from a script, should they care
130193578Sraj	setvar $VAR_NONINTERACTIVE yes
13177957Sbenno
13277957Sbenno	if [ "$script" = "-" ]; then
13377957Sbenno		f_dprintf "$funcname: Loading script from stdin"
13477957Sbenno		eval "$( cat )"
13577957Sbenno		retval=$?
13677957Sbenno	else
137234579Snwhitehorn		f_dprintf "$funcname: Loading script \`%s'" "$script"
13877957Sbenno		if [ ! -e "$script" ]; then
13977957Sbenno			f_show_msg "$msg_unable_to_open" "$script"
14077957Sbenno			return $FAILURE
14177957Sbenno		fi
14277957Sbenno		. "$script"
14377957Sbenno		retval=$?
14477957Sbenno	fi
14577957Sbenno
14677957Sbenno	[ "$old_interactive" ] &&
147234579Snwhitehorn		setvar $VAR_NONINTERACTIVE "$old_interactive"
14877957Sbenno
14977957Sbenno	return $retval
15077957Sbenno}
15177957Sbenno
15277957Sbenno############################################################ MAIN
15377957Sbenno
15477957Sbenno#
15577957Sbenno# Reserved words meant for scripting
15677957Sbenno#
157193578Sraj
158193578Sraj# this file
15977957Sbennof_resword_new loadConfig	f_script_load
16077957Sbenno
16177957Sbenno# device.subr
16277957Sbennof_resword_new deviceRescan	f_device_rescan
16377957Sbenno
16477957Sbenno# media/common.subr
165193578Srajf_resword_new mediaOpen		f_media_open
166193578Srajf_resword_new mediaClose	f_media_close
16777957Sbenno
16877957Sbenno# media includes
16977957Sbennof_resword_new mediaGetType	f_media_get_type         # media/any.subr
17077957Sbennof_resword_new mediaSetCDROM	f_media_set_cdrom        # media/cdrom.subr
17177957Sbennof_resword_new mediaSetDOS	f_media_set_dos          # media/dos.subr
17277957Sbennof_resword_new mediaSetDirectory	f_media_set_directory    # media/directory.subr
17377957Sbennof_resword_new mediaSetFloppy	f_media_set_floppy       # media/floppy.subr
17477957Sbennof_resword_new mediaSetNFS	f_media_set_nfs          # media/nfs.subr
17577957Sbennof_resword_new mediaSetUFS	f_media_set_ufs          # media/ufs.subr
17677957Sbennof_resword_new mediaSetUSB	f_media_set_usb          # media/usb.subr
17777957Sbennof_resword_new optionsEditor	f_media_options_menu     # media/options.subr
17877957Sbennof_resword_new tcpMenuSelect	f_dialog_menu_select_tcp # media/tcp.subr
17977957Sbenno
18077957Sbenno# media/ftp.subr
18177957Sbennof_resword_new mediaSetFTP		f_media_set_ftp
18277957Sbennof_resword_new mediaSetFTPActive		f_media_set_ftp_active
18377957Sbennof_resword_new mediaSetFTPPassive	f_media_set_ftp_passive
18477957Sbennof_resword_new mediaSetFTPUserPass	f_media_set_ftp_userpass
185234579Snwhitehorn
18677957Sbenno# media/http.subr
18777957Sbennof_resword_new mediaSetHTTP	f_media_set_http
18877957Sbenno
18977957Sbenno# media/httpproxy.subr
19077957Sbennof_resword_new mediaSetHTTPProxy	f_media_set_http_proxy
19177957Sbenno
19277957Sbenno# networking/services.subr
193234579Snwhitehornf_resword_new configPCNFSD	f_config_pcnfsd
19477957Sbenno
19577957Sbenno# packages/packages.subr
19677957Sbennof_resword_new configPackages	f_package_config
19777957Sbennof_resword_new packageAdd	f_package_add
19877957Sbennof_resword_new packageDelete	f_package_delete
19977957Sbennof_resword_new packageReinstall	f_package_reinstall
20077957Sbenno
201234579Snwhitehorn# variable.subr
20277957Sbennof_resword_new installVarDefaults	f_variable_set_defaults
20377957Sbennof_resword_new dumpVariables		f_dump_variables
20477957Sbenno
205193578Srajf_dprintf "%s: Successfully loaded." script.subr
206193578Sraj
207193578Srajfi # ! $_SCRIPT_SUBR
208193578Sraj