script.subr revision 249779
121673Sjkhif [ ! "$_SCRIPT_SUBR" ]; then _SCRIPT_SUBR=1
212554Sjkh#
318450Swosch# Copyright (c) 2012-2013 Devin Teske
418450Swosch# All Rights Reserved.
518450Swosch#
618450Swosch# Redistribution and use in source and binary forms, with or without
715734Swosch# modification, are permitted provided that the following conditions
818450Swosch# are met:
918450Swosch# 1. Redistributions of source code must retain the above copyright
1015750Swosch#    notice, this list of conditions and the following disclaimer.
1122855Sjoerg# 2. Redistributions in binary form must reproduce the above copyright
1222855Sjoerg#    notice, this list of conditions and the following disclaimer in the
1322855Sjoerg#    documentation and/or other materials provided with the distribution.
1422855Sjoerg#
1522855Sjoerg# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1618450Swosch# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
1718450Swosch# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1818450Swosch# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1918450Swosch# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2018450Swosch# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2118450Swosch# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2218450Swosch# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2318450Swosch# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2418450Swosch# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2515734Swosch# SUCH DAMAGE.
2615734Swosch#
2715734Swosch# $FreeBSD: head/usr.sbin/bsdconfig/share/script.subr 249779 2013-04-22 21:03:44Z dteske $
2815734Swosch#
2915750Swosch############################################################ INCLUDES
3015750Swosch
3115750SwoschBSDCFG_SHARE="/usr/share/bsdconfig"
3215750Swosch. $BSDCFG_SHARE/common.subr || exit 1
3315750Swoschf_dprintf "%s: loading includes..." script.subr
3415750Swoschf_include $BSDCFG_SHARE/device.subr
3515750Swoschf_include $BSDCFG_SHARE/packages.subr
3615750Swoschf_include $BSDCFG_SHARE/variable.subr
3715750Swoschf_include $BSDCFG_SHARE/media/any.subr
3815750Swoschf_include $BSDCFG_SHARE/media/tcpip.subr
3915750Swosch
4015750Swosch############################################################ GLOBALS
4115750Swosch
4215750SwoschRESWORDS=
4318450Swosch
4415750Swosch############################################################ FUNCTIONS
4515750Swosch
4615750Swosch# f_resword_new $resword $function
4715750Swosch#
4815750Swosch# Create a new `reserved' word for scripting purposes. Reswords call pre-
4915750Swosch# defined functions but differ from those functions in the following ways:
5015750Swosch#
5115750Swosch# 	+ Reswords do not take arguments but instead get all their data from
5215750Swosch# 	  the environment variable namespace.
53# 	+ Unless noError is set (must be non-NULL), if calling the resword
54# 	  results in failure, the application will terminate prematurely.
55# 	+ noError is unset after each/every resword is called.
56#
57# Reswords should not be used in bsdconfig itself (hence the name `reserved
58# word') but instead only in scripts loaded through f_script_load()).
59#
60f_resword_new()
61{
62	local resword="$1" func="$2"
63	[ "$resword" ] || return $FAILURE
64	f_dprintf "script.subr: New resWord %s -> %s" "$resword" "$func"
65	eval $resword\(\){ f_dispatch $func $resword\; }
66	RESWORDS="$RESWORDS${RESWORDS:+ }$resword"
67}
68
69# f_dispatch $func [$resword]
70#
71# Wrapper function used by `reserved words' (reswords) to call other functions.
72# If $noError is set and non-NULL, a failure result from $func is ignored,
73# otherwise the application is prematurely terminated using f_die().
74#
75# NOTE: $noError is unset after every call.
76#
77f_dispatch()
78{
79	local func="$1" resword="${2:-$1}"
80	f_dprintf "f_dispatch: calling resword \`%s'" "$resword"
81	eval $func
82	local retval=$?
83	if [ $retval -ne $SUCCESS ]; then
84		local _ignore_this_error
85		f_getvar $VAR_NO_ERROR _ignore_this_error
86		[ "$_ignore_this_error" ] || f_die $retval \
87			"$msg_command_failed_rest_of_script_aborted" "$resword"
88	fi
89	unset $VAR_NO_ERROR
90}
91
92# f_script_load [$file]
93#
94# Load a script (usually filled with reswords). If $file is missing or NULL,
95# use one of the following instead (in order):
96#
97# 	$configFile
98# 	install.cfg
99# 	/stand/install.fg
100# 	/tmp/install.cfg
101#
102# Unknown/unregistered reswords will generate sh(1) syntax errors but not cause
103# premature termination.
104#
105# Returns success if a script was loaded and itself returned success.
106#
107f_script_load()
108{
109	local script="$1" config_file retval=$SUCCESS
110
111	f_dprintf "f_script_load: script=[%s]" "$script"
112	if [ ! "$script" ]; then
113		f_getvar $VAR_CONFIG_FILE config_file
114		for script in \
115			$config_file \
116			install.cfg \
117			/stand/install.cfg \
118			/tmp/install.cfg \
119		; do
120			[ -e "$script" ] && break
121		done
122	fi
123
124	local old_interactive=
125	f_getvar $VAR_NONINTERACTIVE old_interactive # save a copy
126
127	# Hint to others that we're running from a script, should they care
128	setvar $VAR_NONINTERACTIVE yes
129
130	if [ "$script" = "-" ]; then
131		f_dprintf "f_script_load: Loading script from stdin"
132		eval "$( cat )"
133		retval=$?
134	else
135		f_dprintf "f_script_load: Loading script \`%s'" "$script"
136		if [ ! -e "$script" ]; then
137			f_show_msg "$msg_unable_to_open" "$script"
138			return $FAILURE
139		fi
140		. "$script"
141		retval=$?
142	fi
143
144	[ "$old_interactive" ] &&
145		setvar $VAR_NONINTERACTIVE "$old_interactive"
146
147	return $retval
148}
149
150############################################################ MAIN
151
152#
153# Reserved words meant for scripting
154#
155
156f_resword_new loadConfig		f_script_load      # this file
157f_resword_new deviceRescan		f_device_rescan    # device.subr
158
159# variable.subr
160f_resword_new installVarDefaults	f_variable_set_defaults
161f_resword_new dumpVariables		f_dump_variables
162
163# media/common.subr
164f_resword_new mediaOpen			f_media_open
165f_resword_new mediaClose		f_media_close
166
167f_resword_new mediaGetType		f_media_get_type   # media/any.subr
168f_resword_new mediaSetCDROM		f_media_set_cdrom  # media/cdrom.subr
169f_resword_new mediaSetDOS		f_media_set_dos    # media/dos.subr
170f_resword_new mediaSetFloppy		f_media_set_floppy # media/floppy.subr
171f_resword_new mediaSetNFS		f_media_set_nfs    # media/nfs.subr
172
173# media/ftp.subr
174f_resword_new mediaSetFTP		f_media_set_ftp
175f_resword_new mediaSetFTPActive		f_media_set_ftp_active
176f_resword_new mediaSetFTPPassive	f_media_set_ftp_passive
177f_resword_new mediaSetFTPUserPass	f_media_set_ftp_userpass
178
179# media/httpproxy.subr
180f_resword_new mediaSetHTTP		f_media_set_http_proxy
181f_resword_new mediaSetHTTPProxy		f_media_set_http_proxy
182
183f_resword_new mediaSetUFS	f_media_set_ufs            # media/ufs.subr
184f_resword_new mediaSetUSB	f_media_set_usb            # media/usb.subr
185f_resword_new optionsEditor	f_media_options_menu       # media/options.subr
186f_resword_new tcpMenuSelect	f_dialog_menu_select_tcp   # media/tcp.subr
187
188f_dprintf "%s: Successfully loaded." script.subr
189
190fi # ! $_SCRIPT_SUBR
191