script.subr revision 247280
1245052Sdteskeif [ ! "$_SCRIPT_SUBR" ]; then _SCRIPT_SUBR=1 2245052Sdteske# 3247280Sdteske# Copyright (c) 2012-2013 Devin Teske 4245052Sdteske# All Rights Reserved. 5245052Sdteske# 6245052Sdteske# Redistribution and use in source and binary forms, with or without 7245052Sdteske# modification, are permitted provided that the following conditions 8245052Sdteske# are met: 9245052Sdteske# 1. Redistributions of source code must retain the above copyright 10245052Sdteske# notice, this list of conditions and the following disclaimer. 11245052Sdteske# 2. Redistributions in binary form must reproduce the above copyright 12245052Sdteske# notice, this list of conditions and the following disclaimer in the 13245052Sdteske# documentation and/or other materials provided with the distribution. 14245052Sdteske# 15245052Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16245052Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, THE 17245052Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18245052Sdteske# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19245052Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20245052Sdteske# DAMAGES (INLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21245052Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22245052Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23245052Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24245052Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25245052Sdteske# SUCH DAMAGE. 26245052Sdteske# 27245052Sdteske# $FreeBSD: head/usr.sbin/bsdconfig/share/script.subr 247280 2013-02-25 19:55:32Z dteske $ 28245052Sdteske# 29245052Sdteske############################################################ INCLUDES 30245052Sdteske 31245052SdteskeBSDCFG_SHARE="/usr/share/bsdconfig" 32245052Sdteske. $BSDCFG_SHARE/common.subr || exit 1 33245052Sdteskef_dprintf "%s: loading includes..." script.subr 34247280Sdteskef_include $BSDCFG_SHARE/device.subr 35245052Sdteskef_include $BSDCFG_SHARE/variable.subr 36247280Sdteskef_include $BSDCFG_SHARE/media/any.subr 37247280Sdteskef_include $BSDCFG_SHARE/media/tcpip.subr 38245052Sdteske 39245052Sdteske############################################################ GLOBALS 40245052Sdteske 41245052SdteskeRESWORDS= 42245052Sdteske 43245052Sdteske############################################################ FUNCTIONS 44245052Sdteske 45245052Sdteske# f_resword_new $resword $function 46245052Sdteske# 47245052Sdteske# Create a new `reserved' word for scripting purposes. Reswords call pre- 48245052Sdteske# defined functions but differ from those functions in the following ways: 49245052Sdteske# 50245052Sdteske# + Reswords do not take arguments but instead get all their data from 51245052Sdteske# the environment variable namespace. 52245052Sdteske# + Unless noError is set (must be non-NULL), if calling the resword 53245052Sdteske# results in failure, the application will terminate prematurely. 54245052Sdteske# + noError is unset after each/every resword is called. 55245052Sdteske# 56245052Sdteske# Reswords should not be used in bsdconfig itself (hence the name `reserved 57245052Sdteske# word') but instead only in scripts loaded through f_script_load()). 58245052Sdteske# 59245052Sdteskef_resword_new() 60245052Sdteske{ 61245052Sdteske local resword="$1" func="$2" 62245052Sdteske [ "$resword" ] || return $FAILURE 63245052Sdteske f_dprintf "script.subr: New resWord %s -> %s" "$resword" "$func" 64245052Sdteske eval $resword\(\){ f_dispatch $func $resword\; } 65245052Sdteske RESWORDS="$RESWORDS${RESWORDS:+ }$resword" 66245052Sdteske} 67245052Sdteske 68245052Sdteske# f_dispatch $func [$resword] 69245052Sdteske# 70245052Sdteske# Wrapper function used by `reserved words' (reswords) to call other functions. 71245052Sdteske# If $noError is set and non-NULL, a failure result from $func is ignored, 72245052Sdteske# otherwise the application is prematurely terminated using f_die(). 73245052Sdteske# 74245052Sdteske# NOTE: $noError is unset after every call. 75245052Sdteske# 76245052Sdteskef_dispatch() 77245052Sdteske{ 78245052Sdteske local func="$1" resword="${2:-$1}" 79245052Sdteske f_dprintf "f_dispatch: calling resword \`%s'" "$resword" 80245052Sdteske eval $func 81247280Sdteske local retval=$? 82247280Sdteske if [ $retval -ne $SUCCESS ]; then 83247280Sdteske local _ignore_this_error 84247280Sdteske f_getvar $VAR_NO_ERROR _ignore_this_error 85245052Sdteske [ "$_ignore_this_error" ] || f_die $retval \ 86247280Sdteske "$msg_command_failed_rest_of_script_aborted" "$resword" 87247280Sdteske fi 88245052Sdteske unset $VAR_NO_ERROR 89245052Sdteske} 90245052Sdteske 91245052Sdteske# f_script_load [$file] 92245052Sdteske# 93245052Sdteske# Load a script (usually filled with reswords). If $file is missing or NULL, 94245052Sdteske# use one of the following instead (in order): 95245052Sdteske# 96245052Sdteske# $configFile 97245052Sdteske# install.cfg 98245052Sdteske# /stand/install.fg 99245052Sdteske# /tmp/install.cfg 100245052Sdteske# 101245052Sdteske# Unknown/unregistered reswords will generate sh(1) syntax errors but not cause 102245052Sdteske# premature termination. 103245052Sdteske# 104245052Sdteske# Returns success if a script was loaded and itself returned success. 105245052Sdteske# 106245052Sdteskef_script_load() 107245052Sdteske{ 108245695Sdteske local script="$1" config_file retval=$SUCCESS 109245052Sdteske 110245052Sdteske f_dprintf "f_script_load: script=[%s]" "$script" 111245052Sdteske if [ ! "$script" ]; then 112245052Sdteske f_getvar $VAR_CONFIG_FILE config_file 113245052Sdteske for script in \ 114245052Sdteske $config_file \ 115245052Sdteske install.cfg \ 116245052Sdteske /stand/install.cfg \ 117245052Sdteske /tmp/install.cfg \ 118245052Sdteske ; do 119245052Sdteske [ -e "$script" ] && break 120245052Sdteske done 121245695Sdteske fi 122245695Sdteske 123245695Sdteske local old_interactive= 124245695Sdteske f_getvar $VAR_NONINTERACTIVE old_interactive # save a copy 125245695Sdteske 126245695Sdteske # Hint to others that we're running from a script, should they care 127245695Sdteske setvar $VAR_NONINTERACTIVE yes 128245695Sdteske 129245695Sdteske if [ "$script" = "-" ]; then 130245052Sdteske f_dprintf "f_script_load: Loading script from stdin" 131245052Sdteske eval "$( cat )" 132245695Sdteske retval=$? 133245052Sdteske else 134245052Sdteske f_dprintf "f_script_load: Loading script \`%s'" "$script" 135245052Sdteske if [ ! -e "$script" ]; then 136245052Sdteske f_show_msg "$msg_unable_to_open" "$script" 137245052Sdteske return $FAILURE 138245052Sdteske fi 139245052Sdteske . "$script" 140245695Sdteske retval=$? 141245052Sdteske fi 142245695Sdteske 143245695Sdteske [ "$old_interactive" ] && 144245695Sdteske setvar $VAR_NONINTERACTIVE "$old_interactive" 145245695Sdteske 146245695Sdteske return $retval 147245052Sdteske} 148245052Sdteske 149245052Sdteske############################################################ MAIN 150245052Sdteske 151245052Sdteske# 152245052Sdteske# Reserved words meant for scripting 153245052Sdteske# 154247280Sdteskef_resword_new deviceRescan f_device_rescan 155245052Sdteskef_resword_new dumpVariables f_dump_variables 156245052Sdteskef_resword_new loadConfig f_script_load 157247280Sdteskef_resword_new mediaClose f_media_close 158247280Sdteskef_resword_new mediaGetType f_media_get_type 159247280Sdteskef_resword_new mediaOpen f_media_open 160247280Sdteskef_resword_new mediaSetCDROM f_media_set_cdrom 161247280Sdteskef_resword_new mediaSetDOS f_media_set_dos 162247280Sdteskef_resword_new mediaSetFTP f_media_set_ftp 163247280Sdteskef_resword_new mediaSetFTPActive f_media_set_ftp_active 164247280Sdteskef_resword_new mediaSetFTPPassive f_media_set_ftp_passive 165247280Sdteskef_resword_new mediaSetFTPUserPass f_media_set_ftp_userpass 166247280Sdteskef_resword_new mediaSetFloppy f_media_set_floppy 167247280Sdteskef_resword_new mediaSetHTTP f_media_set_http_proxy 168247280Sdteskef_resword_new mediaSetHTTPProxy f_media_set_http_proxy 169247280Sdteskef_resword_new mediaSetNFS f_media_set_nfs 170247280Sdteskef_resword_new mediaSetUFS f_media_set_ufs 171247280Sdteskef_resword_new mediaSetUSB f_media_set_usb 172247280Sdteskef_resword_new optionsEditor f_media_options_menu 173247280Sdteskef_resword_new tcpMenuSelect f_dialog_menu_select_tcp 174245052Sdteske 175245052Sdteskef_dprintf "%s: Successfully loaded." script.subr 176245052Sdteske 177245052Sdteskefi # ! $_SCRIPT_SUBR 178