]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bsdconfig/share/script.subr
Dtrace: add optional size argument to tracemem().
[FreeBSD/FreeBSD.git] / usr.sbin / bsdconfig / share / script.subr
1 if [ ! "$_SCRIPT_SUBR" ]; then _SCRIPT_SUBR=1
2 #
3 # Copyright (c) 2012-2013 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$
28 #
29 ############################################################ INCLUDES
30
31 BSDCFG_SHARE="/usr/share/bsdconfig"
32 . $BSDCFG_SHARE/common.subr || exit 1
33 f_dprintf "%s: loading includes..." script.subr
34 f_include $BSDCFG_SHARE/device.subr
35 f_include $BSDCFG_SHARE/variable.subr
36 f_include $BSDCFG_SHARE/media/any.subr
37 f_include $BSDCFG_SHARE/media/tcpip.subr
38
39 ############################################################ GLOBALS
40
41 RESWORDS=
42
43 ############################################################ FUNCTIONS
44
45 # f_resword_new $resword $function
46 #
47 # Create a new `reserved' word for scripting purposes. Reswords call pre-
48 # defined functions but differ from those functions in the following ways:
49 #
50 #       + Reswords do not take arguments but instead get all their data from
51 #         the environment variable namespace.
52 #       + Unless noError is set (must be non-NULL), if calling the resword
53 #         results in failure, the application will terminate prematurely.
54 #       + noError is unset after each/every resword is called.
55 #
56 # Reswords should not be used in bsdconfig itself (hence the name `reserved
57 # word') but instead only in scripts loaded through f_script_load()).
58 #
59 f_resword_new()
60 {
61         local resword="$1" func="$2"
62         [ "$resword" ] || return $FAILURE
63         f_dprintf "script.subr: New resWord %s -> %s" "$resword" "$func"
64         eval $resword\(\){ f_dispatch $func $resword\; }
65         RESWORDS="$RESWORDS${RESWORDS:+ }$resword"
66 }
67
68 # f_dispatch $func [$resword]
69 #
70 # Wrapper function used by `reserved words' (reswords) to call other functions.
71 # If $noError is set and non-NULL, a failure result from $func is ignored,
72 # otherwise the application is prematurely terminated using f_die().
73 #
74 # NOTE: $noError is unset after every call.
75 #
76 f_dispatch()
77 {
78         local func="$1" resword="${2:-$1}"
79         f_dprintf "f_dispatch: calling resword \`%s'" "$resword"
80         eval $func
81         local retval=$?
82         if [ $retval -ne $SUCCESS ]; then
83                 local _ignore_this_error
84                 f_getvar $VAR_NO_ERROR _ignore_this_error
85                 [ "$_ignore_this_error" ] || f_die $retval \
86                         "$msg_command_failed_rest_of_script_aborted" "$resword"
87         fi
88         unset $VAR_NO_ERROR
89 }
90
91 # f_script_load [$file]
92 #
93 # Load a script (usually filled with reswords). If $file is missing or NULL,
94 # use one of the following instead (in order):
95 #
96 #       $configFile
97 #       install.cfg
98 #       /stand/install.fg
99 #       /tmp/install.cfg
100 #
101 # Unknown/unregistered reswords will generate sh(1) syntax errors but not cause
102 # premature termination.
103 #
104 # Returns success if a script was loaded and itself returned success.
105 #
106 f_script_load()
107 {
108         local script="$1" config_file retval=$SUCCESS
109
110         f_dprintf "f_script_load: script=[%s]" "$script"
111         if [ ! "$script" ]; then
112                 f_getvar $VAR_CONFIG_FILE config_file
113                 for script in \
114                         $config_file \
115                         install.cfg \
116                         /stand/install.cfg \
117                         /tmp/install.cfg \
118                 ; do
119                         [ -e "$script" ] && break
120                 done
121         fi
122
123         local old_interactive=
124         f_getvar $VAR_NONINTERACTIVE old_interactive # save a copy
125
126         # Hint to others that we're running from a script, should they care
127         setvar $VAR_NONINTERACTIVE yes
128
129         if [ "$script" = "-" ]; then
130                 f_dprintf "f_script_load: Loading script from stdin"
131                 eval "$( cat )"
132                 retval=$?
133         else
134                 f_dprintf "f_script_load: Loading script \`%s'" "$script"
135                 if [ ! -e "$script" ]; then
136                         f_show_msg "$msg_unable_to_open" "$script"
137                         return $FAILURE
138                 fi
139                 . "$script"
140                 retval=$?
141         fi
142
143         [ "$old_interactive" ] &&
144                 setvar $VAR_NONINTERACTIVE "$old_interactive"
145
146         return $retval
147 }
148
149 ############################################################ MAIN
150
151 #
152 # Reserved words meant for scripting
153 #
154 f_resword_new deviceRescan              f_device_rescan
155 f_resword_new dumpVariables             f_dump_variables
156 f_resword_new loadConfig                f_script_load
157 f_resword_new mediaClose                f_media_close
158 f_resword_new mediaGetType              f_media_get_type
159 f_resword_new mediaOpen                 f_media_open
160 f_resword_new mediaSetCDROM             f_media_set_cdrom
161 f_resword_new mediaSetDOS               f_media_set_dos
162 f_resword_new mediaSetFTP               f_media_set_ftp
163 f_resword_new mediaSetFTPActive         f_media_set_ftp_active
164 f_resword_new mediaSetFTPPassive        f_media_set_ftp_passive
165 f_resword_new mediaSetFTPUserPass       f_media_set_ftp_userpass
166 f_resword_new mediaSetFloppy            f_media_set_floppy
167 f_resword_new mediaSetHTTP              f_media_set_http_proxy
168 f_resword_new mediaSetHTTPProxy         f_media_set_http_proxy
169 f_resword_new mediaSetNFS               f_media_set_nfs
170 f_resword_new mediaSetUFS               f_media_set_ufs
171 f_resword_new mediaSetUSB               f_media_set_usb
172 f_resword_new optionsEditor             f_media_options_menu
173 f_resword_new tcpMenuSelect             f_dialog_menu_select_tcp
174
175 f_dprintf "%s: Successfully loaded." script.subr
176
177 fi # ! $_SCRIPT_SUBR