1if [ ! "$_SCRIPT_SUBR" ]; then _SCRIPT_SUBR=1
2#
3# Copyright (c) 2012-2014 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 (INCLUDING, 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$
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/device.subr
35f_include $BSDCFG_SHARE/media/any.subr
36f_include $BSDCFG_SHARE/media/tcpip.subr
37f_include $BSDCFG_SHARE/mustberoot.subr
38f_include $BSDCFG_SHARE/networking/services.subr
39f_include $BSDCFG_SHARE/packages/packages.subr
40f_include $BSDCFG_SHARE/usermgmt/group.subr
41f_include $BSDCFG_SHARE/usermgmt/user.subr
42f_include $BSDCFG_SHARE/variable.subr
43
44############################################################ GLOBALS
45
46RESWORDS=
47
48############################################################ FUNCTIONS
49
50# f_resword_new $resword $function
51#
52# Create a new `reserved' word for scripting purposes. Reswords call pre-
53# defined functions but differ from those functions in the following ways:
54#
55# 	+ Unless noError is set (must be non-NULL), if calling the resword
56# 	  results in failure, the application will terminate prematurely.
57# 	+ noError is unset after each/every resword is called.
58#
59# Reswords should not be used in bsdconfig itself (hence the name `reserved
60# word') but instead only in scripts loaded through f_script_load().
61#
62f_resword_new()
63{
64	local resword="$1" func="$2"
65	[ "$resword" ] || return $FAILURE
66	f_dprintf "script.subr: New resWord %s -> %s" "$resword" "$func"
67	eval $resword\(\){ f_dispatch $func $resword \"\$@\"\; }
68	RESWORDS="$RESWORDS${RESWORDS:+ }$resword"
69}
70
71# f_dispatch $func $resword
72#
73# Wrapper function used by `reserved words' (reswords) to call other functions.
74# If $noError is set and non-NULL, a failure result from $func is ignored,
75# otherwise the application is prematurely terminated using f_die().
76#
77# NOTE: $noError is unset after every call.
78#
79f_dispatch()
80{
81	local func="$1" resword="$2"
82	shift 2 # func resword
83	f_dprintf "f_dispatch: calling resword \`%s'" "$resword"
84	eval $func "$@"
85	local retval=$?
86	if [ $retval -ne $SUCCESS ]; then
87		local _ignore_this_error
88		f_getvar $VAR_NO_ERROR _ignore_this_error
89		[ "$_ignore_this_error" ] || f_die $retval \
90			"$msg_command_failed_rest_of_script_aborted" "$resword"
91	fi
92	unset $VAR_NO_ERROR
93}
94
95# f_script_load [$file]
96#
97# Load a script (usually filled with reswords). If $file is missing or NULL,
98# use one of the following instead (in order):
99#
100# 	$configFile (global)
101# 	install.cfg
102# 	/stand/install.fg
103# 	/tmp/install.cfg
104#
105# Unknown/unregistered reswords will generate sh(1) syntax errors but not cause
106# premature termination.
107#
108# Returns success if a script was loaded and itself returned success.
109#
110f_script_load()
111{
112	local funcname=f_script_load
113	local script="$1" config_file retval=$SUCCESS
114
115	f_dprintf "$funcname: script=[%s]" "$script"
116	if [ ! "$script" ]; then
117		f_getvar $VAR_CONFIG_FILE config_file
118		for script in \
119			$config_file \
120			install.cfg \
121			/stand/install.cfg \
122			/tmp/install.cfg \
123		; do
124			[ -e "$script" ] && break
125		done
126	fi
127
128	local old_interactive=
129	f_getvar $VAR_NONINTERACTIVE old_interactive # save a copy
130
131	# Hint to others that we're running from a script, should they care
132	setvar $VAR_NONINTERACTIVE yes
133
134	if [ "$script" = "-" ]; then
135		f_dprintf "$funcname: Loading script from stdin"
136		eval "$( cat )"
137		retval=$?
138	else
139		f_dprintf "$funcname: Loading script \`%s'" "$script"
140		if [ ! -e "$script" ]; then
141			f_show_msg "$msg_unable_to_open" "$script"
142			return $FAILURE
143		fi
144		. "$script"
145		retval=$?
146	fi
147
148	[ "$old_interactive" ] &&
149		setvar $VAR_NONINTERACTIVE "$old_interactive"
150
151	return $retval
152}
153
154############################################################ MAIN
155
156#
157# Reserved words meant for scripting
158#
159
160# this file
161f_resword_new loadConfig	f_script_load
162
163# device.subr
164f_resword_new deviceRescan	f_device_rescan
165
166# media/common.subr
167f_resword_new mediaOpen		f_media_open
168f_resword_new mediaClose	f_media_close
169
170# media includes
171f_resword_new mediaGetType	f_media_get_type         # media/any.subr
172f_resword_new mediaSetCDROM	f_media_set_cdrom        # media/cdrom.subr
173f_resword_new mediaSetDOS	f_media_set_dos          # media/dos.subr
174f_resword_new mediaSetDirectory	f_media_set_directory    # media/directory.subr
175f_resword_new mediaSetFloppy	f_media_set_floppy       # media/floppy.subr
176f_resword_new mediaSetNFS	f_media_set_nfs          # media/nfs.subr
177f_resword_new mediaSetUFS	f_media_set_ufs          # media/ufs.subr
178f_resword_new mediaSetUSB	f_media_set_usb          # media/usb.subr
179f_resword_new optionsEditor	f_media_options_menu     # media/options.subr
180f_resword_new tcpMenuSelect	f_dialog_menu_select_tcp # media/tcp.subr
181
182# media/ftp.subr
183f_resword_new mediaSetFTP		f_media_set_ftp
184f_resword_new mediaSetFTPActive		f_media_set_ftp_active
185f_resword_new mediaSetFTPPassive	f_media_set_ftp_passive
186f_resword_new mediaSetFTPUserPass	f_media_set_ftp_userpass
187
188# media/http.subr
189f_resword_new mediaSetHTTP	f_media_set_http
190
191# media/httpproxy.subr
192f_resword_new mediaSetHTTPProxy	f_media_set_http_proxy
193
194# networking/services.subr
195f_resword_new configPCNFSD	f_config_pcnfsd
196
197# packages/packages.subr
198f_resword_new configPackages	f_package_config
199f_resword_new packageAdd	f_package_add
200f_resword_new packageDelete	f_package_delete
201f_resword_new packageReinstall	f_package_reinstall
202
203# usermgmt/group.subr
204f_resword_new addGroup		f_group_add
205f_resword_new deleteGroup	f_group_delete
206f_resword_new editGroup		f_group_edit
207
208# usermgmt/user.subr
209f_resword_new addUser		f_user_add
210f_resword_new deleteUser	f_user_delete
211f_resword_new editUser		f_user_edit
212
213# variable.subr
214f_resword_new installVarDefaults	f_variable_set_defaults
215f_resword_new dumpVariables		f_dump_variables
216
217f_dprintf "%s: Successfully loaded." script.subr
218
219fi # ! $_SCRIPT_SUBR
220