script.subr revision 245052
1if [ ! "$_SCRIPT_SUBR" ]; then _SCRIPT_SUBR=1
2#
3# Copyright (c) 2012 Devin Teske
4# All Rights Reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: head/usr.sbin/bsdconfig/share/script.subr 245052 2013-01-05 02:08:47Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." script.subr
34f_include $BSDCFG_SHARE/variable.subr
35
36############################################################ GLOBALS
37
38RESWORDS=
39
40############################################################ FUNCTIONS
41
42# f_resword_new $resword $function
43#
44# Create a new `reserved' word for scripting purposes. Reswords call pre-
45# defined functions but differ from those functions in the following ways:
46#
47# 	+ Reswords do not take arguments but instead get all their data from
48# 	  the environment variable namespace.
49# 	+ Unless noError is set (must be non-NULL), if calling the resword
50# 	  results in failure, the application will terminate prematurely.
51# 	+ noError is unset after each/every resword is called.
52#
53# Reswords should not be used in bsdconfig itself (hence the name `reserved
54# word') but instead only in scripts loaded through f_script_load()).
55#
56f_resword_new()
57{
58	local resword="$1" func="$2"
59	[ "$resword" ] || return $FAILURE
60	f_dprintf "script.subr: New resWord %s -> %s" "$resword" "$func"
61	eval $resword\(\){ f_dispatch $func $resword\; }
62	RESWORDS="$RESWORDS${RESWORDS:+ }$resword"
63}
64
65# f_dispatch $func [$resword]
66#
67# Wrapper function used by `reserved words' (reswords) to call other functions.
68# If $noError is set and non-NULL, a failure result from $func is ignored,
69# otherwise the application is prematurely terminated using f_die().
70#
71# NOTE: $noError is unset after every call.
72#
73f_dispatch()
74{
75	local func="$1" resword="${2:-$1}"
76	f_dprintf "f_dispatch: calling resword \`%s'" "$resword"
77	eval $func
78	local retval=$? _ignore_this_error
79	f_getvar $VAR_NO_ERROR _ignore_this_error
80	[ $retval -eq $SUCCESS ] ||
81		[ "$_ignore_this_error" ] || f_die $retval \
82		"$msg_command_failed_rest_of_script_aborted" "$resword"
83	unset $VAR_NO_ERROR
84}
85
86# f_script_load [$file]
87#
88# Load a script (usually filled with reswords). If $file is missing or NULL,
89# use one of the following instead (in order):
90#
91# 	$configFile
92# 	install.cfg
93# 	/stand/install.fg
94# 	/tmp/install.cfg
95#
96# Unknown/unregistered reswords will generate sh(1) syntax errors but not cause
97# premature termination.
98#
99# Returns success if a script was loaded and itself returned success.
100#
101f_script_load()
102{
103	local script="$1" config_file
104
105	f_dprintf "f_script_load: script=[%s]" "$script"
106	if [ ! "$script" ]; then
107		f_getvar $VAR_CONFIG_FILE config_file
108		for script in \
109			$config_file \
110			install.cfg \
111			/stand/install.cfg \
112			/tmp/install.cfg \
113		; do
114			[ -e "$script" ] && break
115		done
116	elif [ "$script" = "-" ]; then
117		f_dprintf "f_script_load: Loading script from stdin"
118		eval "$( cat )"
119	else
120		f_dprintf "f_script_load: Loading script \`%s'" "$script"
121		if [ ! -e "$script" ]; then
122			f_show_msg "$msg_unable_to_open" "$script"
123			return $FAILURE
124		fi
125		. "$script"
126	fi
127}
128
129############################################################ MAIN
130
131#
132# Reserved words meant for scripting
133#
134f_resword_new dumpVariables		f_dump_variables
135f_resword_new loadConfig		f_script_load
136
137f_dprintf "%s: Successfully loaded." script.subr
138
139fi # ! $_SCRIPT_SUBR
140