script.subr revision 245695
1245052Sdteskeif [ ! "$_SCRIPT_SUBR" ]; then _SCRIPT_SUBR=1 2245052Sdteske# 3245052Sdteske# Copyright (c) 2012 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 245695 2013-01-20 17:48:56Z dteske $ 28245052Sdteske# 29245052Sdteske############################################################ INCLUDES 30245052Sdteske 31245052SdteskeBSDCFG_SHARE="/usr/share/bsdconfig" 32245052Sdteske. $BSDCFG_SHARE/common.subr || exit 1 33245052Sdteskef_dprintf "%s: loading includes..." script.subr 34245052Sdteskef_include $BSDCFG_SHARE/variable.subr 35245052Sdteske 36245052Sdteske############################################################ GLOBALS 37245052Sdteske 38245052SdteskeRESWORDS= 39245052Sdteske 40245052Sdteske############################################################ FUNCTIONS 41245052Sdteske 42245052Sdteske# f_resword_new $resword $function 43245052Sdteske# 44245052Sdteske# Create a new `reserved' word for scripting purposes. Reswords call pre- 45245052Sdteske# defined functions but differ from those functions in the following ways: 46245052Sdteske# 47245052Sdteske# + Reswords do not take arguments but instead get all their data from 48245052Sdteske# the environment variable namespace. 49245052Sdteske# + Unless noError is set (must be non-NULL), if calling the resword 50245052Sdteske# results in failure, the application will terminate prematurely. 51245052Sdteske# + noError is unset after each/every resword is called. 52245052Sdteske# 53245052Sdteske# Reswords should not be used in bsdconfig itself (hence the name `reserved 54245052Sdteske# word') but instead only in scripts loaded through f_script_load()). 55245052Sdteske# 56245052Sdteskef_resword_new() 57245052Sdteske{ 58245052Sdteske local resword="$1" func="$2" 59245052Sdteske [ "$resword" ] || return $FAILURE 60245052Sdteske f_dprintf "script.subr: New resWord %s -> %s" "$resword" "$func" 61245052Sdteske eval $resword\(\){ f_dispatch $func $resword\; } 62245052Sdteske RESWORDS="$RESWORDS${RESWORDS:+ }$resword" 63245052Sdteske} 64245052Sdteske 65245052Sdteske# f_dispatch $func [$resword] 66245052Sdteske# 67245052Sdteske# Wrapper function used by `reserved words' (reswords) to call other functions. 68245052Sdteske# If $noError is set and non-NULL, a failure result from $func is ignored, 69245052Sdteske# otherwise the application is prematurely terminated using f_die(). 70245052Sdteske# 71245052Sdteske# NOTE: $noError is unset after every call. 72245052Sdteske# 73245052Sdteskef_dispatch() 74245052Sdteske{ 75245052Sdteske local func="$1" resword="${2:-$1}" 76245052Sdteske f_dprintf "f_dispatch: calling resword \`%s'" "$resword" 77245052Sdteske eval $func 78245052Sdteske local retval=$? _ignore_this_error 79245052Sdteske f_getvar $VAR_NO_ERROR _ignore_this_error 80245052Sdteske [ $retval -eq $SUCCESS ] || 81245052Sdteske [ "$_ignore_this_error" ] || f_die $retval \ 82245052Sdteske "$msg_command_failed_rest_of_script_aborted" "$resword" 83245052Sdteske unset $VAR_NO_ERROR 84245052Sdteske} 85245052Sdteske 86245052Sdteske# f_script_load [$file] 87245052Sdteske# 88245052Sdteske# Load a script (usually filled with reswords). If $file is missing or NULL, 89245052Sdteske# use one of the following instead (in order): 90245052Sdteske# 91245052Sdteske# $configFile 92245052Sdteske# install.cfg 93245052Sdteske# /stand/install.fg 94245052Sdteske# /tmp/install.cfg 95245052Sdteske# 96245052Sdteske# Unknown/unregistered reswords will generate sh(1) syntax errors but not cause 97245052Sdteske# premature termination. 98245052Sdteske# 99245052Sdteske# Returns success if a script was loaded and itself returned success. 100245052Sdteske# 101245052Sdteskef_script_load() 102245052Sdteske{ 103245695Sdteske local script="$1" config_file retval=$SUCCESS 104245052Sdteske 105245052Sdteske f_dprintf "f_script_load: script=[%s]" "$script" 106245052Sdteske if [ ! "$script" ]; then 107245052Sdteske f_getvar $VAR_CONFIG_FILE config_file 108245052Sdteske for script in \ 109245052Sdteske $config_file \ 110245052Sdteske install.cfg \ 111245052Sdteske /stand/install.cfg \ 112245052Sdteske /tmp/install.cfg \ 113245052Sdteske ; do 114245052Sdteske [ -e "$script" ] && break 115245052Sdteske done 116245695Sdteske fi 117245695Sdteske 118245695Sdteske local old_interactive= 119245695Sdteske f_getvar $VAR_NONINTERACTIVE old_interactive # save a copy 120245695Sdteske 121245695Sdteske # Hint to others that we're running from a script, should they care 122245695Sdteske setvar $VAR_NONINTERACTIVE yes 123245695Sdteske 124245695Sdteske if [ "$script" = "-" ]; then 125245052Sdteske f_dprintf "f_script_load: Loading script from stdin" 126245052Sdteske eval "$( cat )" 127245695Sdteske retval=$? 128245052Sdteske else 129245052Sdteske f_dprintf "f_script_load: Loading script \`%s'" "$script" 130245052Sdteske if [ ! -e "$script" ]; then 131245052Sdteske f_show_msg "$msg_unable_to_open" "$script" 132245052Sdteske return $FAILURE 133245052Sdteske fi 134245052Sdteske . "$script" 135245695Sdteske retval=$? 136245052Sdteske fi 137245695Sdteske 138245695Sdteske [ "$old_interactive" ] && 139245695Sdteske setvar $VAR_NONINTERACTIVE "$old_interactive" 140245695Sdteske 141245695Sdteske return $retval 142245052Sdteske} 143245052Sdteske 144245052Sdteske############################################################ MAIN 145245052Sdteske 146245052Sdteske# 147245052Sdteske# Reserved words meant for scripting 148245052Sdteske# 149245052Sdteskef_resword_new dumpVariables f_dump_variables 150245052Sdteskef_resword_new loadConfig f_script_load 151245052Sdteske 152245052Sdteskef_dprintf "%s: Successfully loaded." script.subr 153245052Sdteske 154245052Sdteskefi # ! $_SCRIPT_SUBR 155