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