summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bsdconfig/share/script.subr
blob: 84d1cab4c20d314b17d4ca314389d7be3dace036 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
if [ ! "$_SCRIPT_SUBR" ]; then _SCRIPT_SUBR=1
#
# Copyright (c) 2012 Devin Teske
# All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#
############################################################ INCLUDES

BSDCFG_SHARE="/usr/share/bsdconfig"
. $BSDCFG_SHARE/common.subr || exit 1
f_dprintf "%s: loading includes..." script.subr
f_include $BSDCFG_SHARE/variable.subr

############################################################ GLOBALS

RESWORDS=

############################################################ FUNCTIONS

# f_resword_new $resword $function
#
# Create a new `reserved' word for scripting purposes. Reswords call pre-
# defined functions but differ from those functions in the following ways:
#
# 	+ Reswords do not take arguments but instead get all their data from
# 	  the environment variable namespace.
# 	+ Unless noError is set (must be non-NULL), if calling the resword
# 	  results in failure, the application will terminate prematurely.
# 	+ noError is unset after each/every resword is called.
#
# Reswords should not be used in bsdconfig itself (hence the name `reserved
# word') but instead only in scripts loaded through f_script_load()).
#
f_resword_new()
{
	local resword="$1" func="$2"
	[ "$resword" ] || return $FAILURE
	f_dprintf "script.subr: New resWord %s -> %s" "$resword" "$func"
	eval $resword\(\){ f_dispatch $func $resword\; }
	RESWORDS="$RESWORDS${RESWORDS:+ }$resword"
}

# f_dispatch $func [$resword]
#
# Wrapper function used by `reserved words' (reswords) to call other functions.
# If $noError is set and non-NULL, a failure result from $func is ignored,
# otherwise the application is prematurely terminated using f_die().
#
# NOTE: $noError is unset after every call.
#
f_dispatch()
{
	local func="$1" resword="${2:-$1}"
	f_dprintf "f_dispatch: calling resword \`%s'" "$resword"
	eval $func
	local retval=$? _ignore_this_error
	f_getvar $VAR_NO_ERROR _ignore_this_error
	[ $retval -eq $SUCCESS ] ||
		[ "$_ignore_this_error" ] || f_die $retval \
		"$msg_command_failed_rest_of_script_aborted" "$resword"
	unset $VAR_NO_ERROR
}

# f_script_load [$file]
#
# Load a script (usually filled with reswords). If $file is missing or NULL,
# use one of the following instead (in order):
#
# 	$configFile
# 	install.cfg
# 	/stand/install.fg
# 	/tmp/install.cfg
#
# Unknown/unregistered reswords will generate sh(1) syntax errors but not cause
# premature termination.
#
# Returns success if a script was loaded and itself returned success.
#
f_script_load()
{
	local script="$1" config_file retval=$SUCCESS

	f_dprintf "f_script_load: script=[%s]" "$script"
	if [ ! "$script" ]; then
		f_getvar $VAR_CONFIG_FILE config_file
		for script in \
			$config_file \
			install.cfg \
			/stand/install.cfg \
			/tmp/install.cfg \
		; do
			[ -e "$script" ] && break
		done
	fi

	local old_interactive=
	f_getvar $VAR_NONINTERACTIVE old_interactive # save a copy

	# Hint to others that we're running from a script, should they care
	setvar $VAR_NONINTERACTIVE yes

	if [ "$script" = "-" ]; then
		f_dprintf "f_script_load: Loading script from stdin"
		eval "$( cat )"
		retval=$?
	else
		f_dprintf "f_script_load: Loading script \`%s'" "$script"
		if [ ! -e "$script" ]; then
			f_show_msg "$msg_unable_to_open" "$script"
			return $FAILURE
		fi
		. "$script"
		retval=$?
	fi

	[ "$old_interactive" ] &&
		setvar $VAR_NONINTERACTIVE "$old_interactive"

	return $retval
}

############################################################ MAIN

#
# Reserved words meant for scripting
#
f_resword_new dumpVariables		f_dump_variables
f_resword_new loadConfig		f_script_load

f_dprintf "%s: Successfully loaded." script.subr

fi # ! $_SCRIPT_SUBR
OpenPOWER on IntegriCloud